Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(442)

Unified Diff: net/socket/client_socket_pool_base_unittest.cc

Issue 8526006: Close idle sockets next time we are about to send data. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: code conventions Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: net/socket/client_socket_pool_base_unittest.cc
diff --git a/net/socket/client_socket_pool_base_unittest.cc b/net/socket/client_socket_pool_base_unittest.cc
index 985a4c59988a7fb4793d3ac2903346f98d639279..7bdb2e67e67be63add6e94cdd446bc2e7a21cc28 100644
--- a/net/socket/client_socket_pool_base_unittest.cc
+++ b/net/socket/client_socket_pool_base_unittest.cc
@@ -398,10 +398,12 @@ class TestClientSocketPool : public ClientSocketPool {
ClientSocketPoolHistograms* histograms,
base::TimeDelta unused_idle_socket_timeout,
base::TimeDelta used_idle_socket_timeout,
- TestClientSocketPoolBase::ConnectJobFactory* connect_job_factory)
+ TestClientSocketPoolBase::ConnectJobFactory* connect_job_factory,
+ const ClientSocketPoolOptions& options = ClientSocketPoolOptions())
: base_(max_sockets, max_sockets_per_group, histograms,
unused_idle_socket_timeout, used_idle_socket_timeout,
- connect_job_factory) {}
+ connect_job_factory,
+ options) {}
virtual ~TestClientSocketPool() {}
@@ -572,7 +574,8 @@ class ClientSocketPoolBaseTest : public testing::Test {
void CreatePoolWithIdleTimeouts(
int max_sockets, int max_sockets_per_group,
base::TimeDelta unused_idle_socket_timeout,
- base::TimeDelta used_idle_socket_timeout) {
+ base::TimeDelta used_idle_socket_timeout,
+ const ClientSocketPoolOptions& options = ClientSocketPoolOptions()) {
DCHECK(!pool_.get());
connect_job_factory_ = new TestConnectJobFactory(&client_socket_factory_);
pool_.reset(new TestClientSocketPool(max_sockets,
@@ -580,7 +583,8 @@ class ClientSocketPoolBaseTest : public testing::Test {
&histograms_,
unused_idle_socket_timeout,
used_idle_socket_timeout,
- connect_job_factory_));
+ connect_job_factory_,
+ options));
}
int StartRequest(const std::string& group_name,
@@ -1946,6 +1950,83 @@ TEST_F(ClientSocketPoolBaseTest, AdditionalErrorStateAsynchronous) {
EXPECT_FALSE(handle.ssl_error_response_info().headers.get() == NULL);
}
+TEST_F(ClientSocketPoolBaseTest, DisableCleanupTimer) {
+ CreatePoolWithIdleTimeouts(
+ kDefaultMaxSockets, kDefaultMaxSocketsPerGroup,
+ base::TimeDelta::FromMilliseconds(10), // Time out unused sockets
+ base::TimeDelta::FromMilliseconds(10),
+ ClientSocketPoolOptions(false)); // Time out used sockets
+
+ connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
+
+ // Startup two mock pending connect jobs, which will sit in the MessageLoop.
+
+ ClientSocketHandle handle;
+ TestOldCompletionCallback callback;
+ int rv = handle.Init("a",
+ params_,
+ LOWEST,
+ &callback,
+ pool_.get(),
+ BoundNetLog());
+ EXPECT_EQ(ERR_IO_PENDING, rv);
+ EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", &handle));
+
+ ClientSocketHandle handle2;
+ TestOldCompletionCallback callback2;
+ rv = handle2.Init("a",
+ params_,
+ LOWEST,
+ &callback2,
+ pool_.get(),
+ BoundNetLog());
+ EXPECT_EQ(ERR_IO_PENDING, rv);
+ EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", &handle2));
+
+ // Cancel one of the requests. Wait for the other, which will get the first
+ // job. Release the socket. Run the loop again to make sure the second
+ // socket is sitting idle and the first one is released (since ReleaseSocket()
+ // just posts a DoReleaseSocket() task).
+
+ handle.Reset();
+ EXPECT_EQ(OK, callback2.WaitForResult());
+ // Use the socket.
+ EXPECT_EQ(1, handle2.socket()->Write(NULL, 1, NULL));
+ handle2.Reset();
+
+ // We post all of our delayed tasks with a 2ms delay. I.e. they don't
+ // actually become pending until 2ms after they have been created. In order
+ // to flush all tasks, we need to wait so that we know there are no
+ // soon-to-be-pending tasks waiting.
+ base::PlatformThread::Sleep(20);
+ MessageLoop::current()->RunAllPending();
+
+ ASSERT_EQ(2, pool_->IdleSocketCount());
+
+ // Request a new socket. This should cleanup the unused and timed out ones.
+ // A new socket will be created rather than reusing the idle one.
+ CapturingBoundNetLog log(CapturingNetLog::kUnbounded);
+ rv = handle.Init("a",
+ params_,
+ LOWEST,
+ &callback,
+ pool_.get(),
+ log.bound());
+ EXPECT_EQ(ERR_IO_PENDING, rv);
+ EXPECT_EQ(OK, callback.WaitForResult());
+ EXPECT_FALSE(handle.is_reused());
+
+ // Make sure the idle socket is closed
+ ASSERT_TRUE(pool_->HasGroup("a"));
+ EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
+ EXPECT_EQ(1, pool_->NumActiveSocketsInGroup("a"));
+
+ net::CapturingNetLog::EntryList entries;
+ log.GetEntries(&entries);
+ EXPECT_FALSE(LogContainsEntryWithType(
+ entries, 1, NetLog::TYPE_SOCKET_POOL_REUSED_AN_EXISTING_SOCKET));
+}
+
TEST_F(ClientSocketPoolBaseTest, CleanupTimedOutIdleSockets) {
CreatePoolWithIdleTimeouts(
kDefaultMaxSockets, kDefaultMaxSocketsPerGroup,

Powered by Google App Engine
This is Rietveld 408576698