| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/socket/client_socket_pool_base.h" | 5 #include "net/socket/client_socket_pool_base.h" |
| 6 | 6 |
| 7 #include "base/callback.h" | 7 #include "base/callback.h" |
| 8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
| 9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
| 10 #include "base/memory/scoped_vector.h" | 10 #include "base/memory/scoped_vector.h" |
| (...skipping 380 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 391 }; | 391 }; |
| 392 | 392 |
| 393 class TestClientSocketPool : public ClientSocketPool { | 393 class TestClientSocketPool : public ClientSocketPool { |
| 394 public: | 394 public: |
| 395 TestClientSocketPool( | 395 TestClientSocketPool( |
| 396 int max_sockets, | 396 int max_sockets, |
| 397 int max_sockets_per_group, | 397 int max_sockets_per_group, |
| 398 ClientSocketPoolHistograms* histograms, | 398 ClientSocketPoolHistograms* histograms, |
| 399 base::TimeDelta unused_idle_socket_timeout, | 399 base::TimeDelta unused_idle_socket_timeout, |
| 400 base::TimeDelta used_idle_socket_timeout, | 400 base::TimeDelta used_idle_socket_timeout, |
| 401 TestClientSocketPoolBase::ConnectJobFactory* connect_job_factory) | 401 TestClientSocketPoolBase::ConnectJobFactory* connect_job_factory, |
| 402 const ClientSocketPoolOptions& options = ClientSocketPoolOptions()) |
| 402 : base_(max_sockets, max_sockets_per_group, histograms, | 403 : base_(max_sockets, max_sockets_per_group, histograms, |
| 403 unused_idle_socket_timeout, used_idle_socket_timeout, | 404 unused_idle_socket_timeout, used_idle_socket_timeout, |
| 404 connect_job_factory) {} | 405 connect_job_factory, |
| 406 options) {} |
| 405 | 407 |
| 406 virtual ~TestClientSocketPool() {} | 408 virtual ~TestClientSocketPool() {} |
| 407 | 409 |
| 408 virtual int RequestSocket( | 410 virtual int RequestSocket( |
| 409 const std::string& group_name, | 411 const std::string& group_name, |
| 410 const void* params, | 412 const void* params, |
| 411 net::RequestPriority priority, | 413 net::RequestPriority priority, |
| 412 ClientSocketHandle* handle, | 414 ClientSocketHandle* handle, |
| 413 OldCompletionCallback* callback, | 415 OldCompletionCallback* callback, |
| 414 const BoundNetLog& net_log) { | 416 const BoundNetLog& net_log) { |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 565 CreatePoolWithIdleTimeouts( | 567 CreatePoolWithIdleTimeouts( |
| 566 max_sockets, | 568 max_sockets, |
| 567 max_sockets_per_group, | 569 max_sockets_per_group, |
| 568 ClientSocketPool::unused_idle_socket_timeout(), | 570 ClientSocketPool::unused_idle_socket_timeout(), |
| 569 ClientSocketPool::used_idle_socket_timeout()); | 571 ClientSocketPool::used_idle_socket_timeout()); |
| 570 } | 572 } |
| 571 | 573 |
| 572 void CreatePoolWithIdleTimeouts( | 574 void CreatePoolWithIdleTimeouts( |
| 573 int max_sockets, int max_sockets_per_group, | 575 int max_sockets, int max_sockets_per_group, |
| 574 base::TimeDelta unused_idle_socket_timeout, | 576 base::TimeDelta unused_idle_socket_timeout, |
| 575 base::TimeDelta used_idle_socket_timeout) { | 577 base::TimeDelta used_idle_socket_timeout, |
| 578 const ClientSocketPoolOptions& options = ClientSocketPoolOptions()) { |
| 576 DCHECK(!pool_.get()); | 579 DCHECK(!pool_.get()); |
| 577 connect_job_factory_ = new TestConnectJobFactory(&client_socket_factory_); | 580 connect_job_factory_ = new TestConnectJobFactory(&client_socket_factory_); |
| 578 pool_.reset(new TestClientSocketPool(max_sockets, | 581 pool_.reset(new TestClientSocketPool(max_sockets, |
| 579 max_sockets_per_group, | 582 max_sockets_per_group, |
| 580 &histograms_, | 583 &histograms_, |
| 581 unused_idle_socket_timeout, | 584 unused_idle_socket_timeout, |
| 582 used_idle_socket_timeout, | 585 used_idle_socket_timeout, |
| 583 connect_job_factory_)); | 586 connect_job_factory_, |
| 587 options)); |
| 584 } | 588 } |
| 585 | 589 |
| 586 int StartRequest(const std::string& group_name, | 590 int StartRequest(const std::string& group_name, |
| 587 net::RequestPriority priority) { | 591 net::RequestPriority priority) { |
| 588 return test_base_.StartRequestUsingPool< | 592 return test_base_.StartRequestUsingPool< |
| 589 TestClientSocketPool, TestSocketParams>( | 593 TestClientSocketPool, TestSocketParams>( |
| 590 pool_.get(), group_name, priority, params_); | 594 pool_.get(), group_name, priority, params_); |
| 591 } | 595 } |
| 592 | 596 |
| 593 int GetOrderOfRequest(size_t index) const { | 597 int GetOrderOfRequest(size_t index) const { |
| (...skipping 1345 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1939 pool_.get(), | 1943 pool_.get(), |
| 1940 BoundNetLog())); | 1944 BoundNetLog())); |
| 1941 EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", &handle)); | 1945 EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", &handle)); |
| 1942 EXPECT_EQ(ERR_CONNECTION_FAILED, callback.WaitForResult()); | 1946 EXPECT_EQ(ERR_CONNECTION_FAILED, callback.WaitForResult()); |
| 1943 EXPECT_FALSE(handle.is_initialized()); | 1947 EXPECT_FALSE(handle.is_initialized()); |
| 1944 EXPECT_FALSE(handle.socket()); | 1948 EXPECT_FALSE(handle.socket()); |
| 1945 EXPECT_TRUE(handle.is_ssl_error()); | 1949 EXPECT_TRUE(handle.is_ssl_error()); |
| 1946 EXPECT_FALSE(handle.ssl_error_response_info().headers.get() == NULL); | 1950 EXPECT_FALSE(handle.ssl_error_response_info().headers.get() == NULL); |
| 1947 } | 1951 } |
| 1948 | 1952 |
| 1953 TEST_F(ClientSocketPoolBaseTest, DisableCleanupTimer) { |
| 1954 CreatePoolWithIdleTimeouts( |
| 1955 kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 1956 base::TimeDelta::FromMilliseconds(10), // Time out unused sockets |
| 1957 base::TimeDelta::FromMilliseconds(10), |
| 1958 ClientSocketPoolOptions(false)); // Time out used sockets |
| 1959 |
| 1960 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1961 |
| 1962 // Startup two mock pending connect jobs, which will sit in the MessageLoop. |
| 1963 |
| 1964 ClientSocketHandle handle; |
| 1965 TestOldCompletionCallback callback; |
| 1966 int rv = handle.Init("a", |
| 1967 params_, |
| 1968 LOWEST, |
| 1969 &callback, |
| 1970 pool_.get(), |
| 1971 BoundNetLog()); |
| 1972 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1973 EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", &handle)); |
| 1974 |
| 1975 ClientSocketHandle handle2; |
| 1976 TestOldCompletionCallback callback2; |
| 1977 rv = handle2.Init("a", |
| 1978 params_, |
| 1979 LOWEST, |
| 1980 &callback2, |
| 1981 pool_.get(), |
| 1982 BoundNetLog()); |
| 1983 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1984 EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", &handle2)); |
| 1985 |
| 1986 // Cancel one of the requests. Wait for the other, which will get the first |
| 1987 // job. Release the socket. Run the loop again to make sure the second |
| 1988 // socket is sitting idle and the first one is released (since ReleaseSocket() |
| 1989 // just posts a DoReleaseSocket() task). |
| 1990 |
| 1991 handle.Reset(); |
| 1992 EXPECT_EQ(OK, callback2.WaitForResult()); |
| 1993 // Use the socket. |
| 1994 EXPECT_EQ(1, handle2.socket()->Write(NULL, 1, NULL)); |
| 1995 handle2.Reset(); |
| 1996 |
| 1997 // We post all of our delayed tasks with a 2ms delay. I.e. they don't |
| 1998 // actually become pending until 2ms after they have been created. In order |
| 1999 // to flush all tasks, we need to wait so that we know there are no |
| 2000 // soon-to-be-pending tasks waiting. |
| 2001 base::PlatformThread::Sleep(20); |
| 2002 MessageLoop::current()->RunAllPending(); |
| 2003 |
| 2004 ASSERT_EQ(2, pool_->IdleSocketCount()); |
| 2005 |
| 2006 // Request a new socket. This should cleanup the unused and timed out ones. |
| 2007 // A new socket will be created rather than reusing the idle one. |
| 2008 CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
| 2009 rv = handle.Init("a", |
| 2010 params_, |
| 2011 LOWEST, |
| 2012 &callback, |
| 2013 pool_.get(), |
| 2014 log.bound()); |
| 2015 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 2016 EXPECT_EQ(OK, callback.WaitForResult()); |
| 2017 EXPECT_FALSE(handle.is_reused()); |
| 2018 |
| 2019 // Make sure the idle socket is closed |
| 2020 ASSERT_TRUE(pool_->HasGroup("a")); |
| 2021 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a")); |
| 2022 EXPECT_EQ(1, pool_->NumActiveSocketsInGroup("a")); |
| 2023 |
| 2024 net::CapturingNetLog::EntryList entries; |
| 2025 log.GetEntries(&entries); |
| 2026 EXPECT_FALSE(LogContainsEntryWithType( |
| 2027 entries, 1, NetLog::TYPE_SOCKET_POOL_REUSED_AN_EXISTING_SOCKET)); |
| 2028 } |
| 2029 |
| 1949 TEST_F(ClientSocketPoolBaseTest, CleanupTimedOutIdleSockets) { | 2030 TEST_F(ClientSocketPoolBaseTest, CleanupTimedOutIdleSockets) { |
| 1950 CreatePoolWithIdleTimeouts( | 2031 CreatePoolWithIdleTimeouts( |
| 1951 kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, | 2032 kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 1952 base::TimeDelta(), // Time out unused sockets immediately. | 2033 base::TimeDelta(), // Time out unused sockets immediately. |
| 1953 base::TimeDelta::FromDays(1)); // Don't time out used sockets. | 2034 base::TimeDelta::FromDays(1)); // Don't time out used sockets. |
| 1954 | 2035 |
| 1955 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); | 2036 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1956 | 2037 |
| 1957 // Startup two mock pending connect jobs, which will sit in the MessageLoop. | 2038 // Startup two mock pending connect jobs, which will sit in the MessageLoop. |
| 1958 | 2039 |
| (...skipping 1307 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3266 // The hung connect job should still be there, but everything else should be | 3347 // The hung connect job should still be there, but everything else should be |
| 3267 // complete. | 3348 // complete. |
| 3268 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a")); | 3349 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a")); |
| 3269 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a")); | 3350 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a")); |
| 3270 EXPECT_EQ(1, pool_->NumActiveSocketsInGroup("a")); | 3351 EXPECT_EQ(1, pool_->NumActiveSocketsInGroup("a")); |
| 3271 } | 3352 } |
| 3272 | 3353 |
| 3273 } // namespace | 3354 } // namespace |
| 3274 | 3355 |
| 3275 } // namespace net | 3356 } // namespace net |
| OLD | NEW |