Chromium Code Reviews| 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..0996340d8054e37d029d46da5f34640651ff5c25 100644 |
| --- a/net/socket/client_socket_pool_base_unittest.cc |
| +++ b/net/socket/client_socket_pool_base_unittest.cc |
| @@ -492,6 +492,10 @@ class TestClientSocketPool : public ClientSocketPool { |
| void EnableConnectBackupJobs() { base_.EnableConnectBackupJobs(); } |
| + void UseCleanupTimer(bool enabled) { |
| + base_.set_cleanup_timer_enabled(enabled); |
| + } |
| + |
| private: |
| TestClientSocketPoolBase base_; |
| @@ -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 |
|
mmenke
2011/11/11 03:33:01
nit: Google style is two spaces between code and
selim
2011/11/11 19:30:58
done
|
| + base::TimeDelta::FromMilliseconds(10)); // Time out used sockets |
| + |
| + // disable cleanup timer |
|
mmenke
2011/11/11 03:33:01
nit: "Disable cleanup timer."
selim
2011/11/11 19:30:58
done
|
| + pool_->UseCleanupTimer(false); |
| + |
| + 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. |
| + CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
| + rv = handle.Init("a", |
| + params_, |
| + LOWEST, |
| + &callback, |
| + pool_.get(), |
| + log.bound()); |
| + EXPECT_EQ(OK, rv); |
| + EXPECT_TRUE(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_TRUE(LogContainsEntryWithType( |
| + entries, 1, NetLog::TYPE_SOCKET_POOL_REUSED_AN_EXISTING_SOCKET)); |
| +} |
| + |
| TEST_F(ClientSocketPoolBaseTest, CleanupTimedOutIdleSockets) { |
| CreatePoolWithIdleTimeouts( |
| kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |