| 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 536 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 547 }; | 547 }; |
| 548 | 548 |
| 549 class ClientSocketPoolBaseTest : public testing::Test { | 549 class ClientSocketPoolBaseTest : public testing::Test { |
| 550 protected: | 550 protected: |
| 551 ClientSocketPoolBaseTest() | 551 ClientSocketPoolBaseTest() |
| 552 : params_(new TestSocketParams()), | 552 : params_(new TestSocketParams()), |
| 553 histograms_("ClientSocketPoolTest") { | 553 histograms_("ClientSocketPoolTest") { |
| 554 connect_backup_jobs_enabled_ = | 554 connect_backup_jobs_enabled_ = |
| 555 internal::ClientSocketPoolBaseHelper::connect_backup_jobs_enabled(); | 555 internal::ClientSocketPoolBaseHelper::connect_backup_jobs_enabled(); |
| 556 internal::ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(true); | 556 internal::ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(true); |
| 557 cleanup_timer_enabled_ = |
| 558 internal::ClientSocketPoolBaseHelper::cleanup_timer_enabled(); |
| 557 } | 559 } |
| 558 | 560 |
| 559 virtual ~ClientSocketPoolBaseTest() { | 561 virtual ~ClientSocketPoolBaseTest() { |
| 560 internal::ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled( | 562 internal::ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled( |
| 561 connect_backup_jobs_enabled_); | 563 connect_backup_jobs_enabled_); |
| 564 internal::ClientSocketPoolBaseHelper::set_cleanup_timer_enabled( |
| 565 cleanup_timer_enabled_); |
| 562 } | 566 } |
| 563 | 567 |
| 564 void CreatePool(int max_sockets, int max_sockets_per_group) { | 568 void CreatePool(int max_sockets, int max_sockets_per_group) { |
| 565 CreatePoolWithIdleTimeouts( | 569 CreatePoolWithIdleTimeouts( |
| 566 max_sockets, | 570 max_sockets, |
| 567 max_sockets_per_group, | 571 max_sockets_per_group, |
| 568 ClientSocketPool::unused_idle_socket_timeout(), | 572 ClientSocketPool::unused_idle_socket_timeout(), |
| 569 ClientSocketPool::used_idle_socket_timeout()); | 573 ClientSocketPool::used_idle_socket_timeout()); |
| 570 } | 574 } |
| 571 | 575 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 601 void ReleaseAllConnections(ClientSocketPoolTest::KeepAlive keep_alive) { | 605 void ReleaseAllConnections(ClientSocketPoolTest::KeepAlive keep_alive) { |
| 602 test_base_.ReleaseAllConnections(keep_alive); | 606 test_base_.ReleaseAllConnections(keep_alive); |
| 603 } | 607 } |
| 604 | 608 |
| 605 TestSocketRequest* request(int i) { return test_base_.request(i); } | 609 TestSocketRequest* request(int i) { return test_base_.request(i); } |
| 606 size_t requests_size() const { return test_base_.requests_size(); } | 610 size_t requests_size() const { return test_base_.requests_size(); } |
| 607 ScopedVector<TestSocketRequest>* requests() { return test_base_.requests(); } | 611 ScopedVector<TestSocketRequest>* requests() { return test_base_.requests(); } |
| 608 size_t completion_count() const { return test_base_.completion_count(); } | 612 size_t completion_count() const { return test_base_.completion_count(); } |
| 609 | 613 |
| 610 bool connect_backup_jobs_enabled_; | 614 bool connect_backup_jobs_enabled_; |
| 615 bool cleanup_timer_enabled_; |
| 611 MockClientSocketFactory client_socket_factory_; | 616 MockClientSocketFactory client_socket_factory_; |
| 612 TestConnectJobFactory* connect_job_factory_; | 617 TestConnectJobFactory* connect_job_factory_; |
| 613 scoped_refptr<TestSocketParams> params_; | 618 scoped_refptr<TestSocketParams> params_; |
| 614 ClientSocketPoolHistograms histograms_; | 619 ClientSocketPoolHistograms histograms_; |
| 615 scoped_ptr<TestClientSocketPool> pool_; | 620 scoped_ptr<TestClientSocketPool> pool_; |
| 616 ClientSocketPoolTest test_base_; | 621 ClientSocketPoolTest test_base_; |
| 617 }; | 622 }; |
| 618 | 623 |
| 619 TEST_F(ClientSocketPoolBaseTest, AssignIdleSocketToGroup_WarmestSocket) { | 624 TEST_F(ClientSocketPoolBaseTest, AssignIdleSocketToGroup_WarmestSocket) { |
| 620 CreatePool(4, 4); | 625 CreatePool(4, 4); |
| (...skipping 1318 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1939 pool_.get(), | 1944 pool_.get(), |
| 1940 BoundNetLog())); | 1945 BoundNetLog())); |
| 1941 EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", &handle)); | 1946 EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", &handle)); |
| 1942 EXPECT_EQ(ERR_CONNECTION_FAILED, callback.WaitForResult()); | 1947 EXPECT_EQ(ERR_CONNECTION_FAILED, callback.WaitForResult()); |
| 1943 EXPECT_FALSE(handle.is_initialized()); | 1948 EXPECT_FALSE(handle.is_initialized()); |
| 1944 EXPECT_FALSE(handle.socket()); | 1949 EXPECT_FALSE(handle.socket()); |
| 1945 EXPECT_TRUE(handle.is_ssl_error()); | 1950 EXPECT_TRUE(handle.is_ssl_error()); |
| 1946 EXPECT_FALSE(handle.ssl_error_response_info().headers.get() == NULL); | 1951 EXPECT_FALSE(handle.ssl_error_response_info().headers.get() == NULL); |
| 1947 } | 1952 } |
| 1948 | 1953 |
| 1954 TEST_F(ClientSocketPoolBaseTest, DisableCleanupTimer) { |
| 1955 // Disable cleanup timer. |
| 1956 internal::ClientSocketPoolBaseHelper::set_cleanup_timer_enabled(false); |
| 1957 |
| 1958 CreatePoolWithIdleTimeouts( |
| 1959 kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 1960 base::TimeDelta::FromMilliseconds(10), // Time out unused sockets |
| 1961 base::TimeDelta::FromMilliseconds(10)); // Time out used sockets |
| 1962 |
| 1963 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1964 |
| 1965 // Startup two mock pending connect jobs, which will sit in the MessageLoop. |
| 1966 |
| 1967 ClientSocketHandle handle; |
| 1968 TestOldCompletionCallback callback; |
| 1969 int rv = handle.Init("a", |
| 1970 params_, |
| 1971 LOWEST, |
| 1972 &callback, |
| 1973 pool_.get(), |
| 1974 BoundNetLog()); |
| 1975 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1976 EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", &handle)); |
| 1977 |
| 1978 ClientSocketHandle handle2; |
| 1979 TestOldCompletionCallback callback2; |
| 1980 rv = handle2.Init("a", |
| 1981 params_, |
| 1982 LOWEST, |
| 1983 &callback2, |
| 1984 pool_.get(), |
| 1985 BoundNetLog()); |
| 1986 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1987 EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", &handle2)); |
| 1988 |
| 1989 // Cancel one of the requests. Wait for the other, which will get the first |
| 1990 // job. Release the socket. Run the loop again to make sure the second |
| 1991 // socket is sitting idle and the first one is released (since ReleaseSocket() |
| 1992 // just posts a DoReleaseSocket() task). |
| 1993 |
| 1994 handle.Reset(); |
| 1995 EXPECT_EQ(OK, callback2.WaitForResult()); |
| 1996 // Use the socket. |
| 1997 EXPECT_EQ(1, handle2.socket()->Write(NULL, 1, NULL)); |
| 1998 handle2.Reset(); |
| 1999 |
| 2000 // The idle socket timeout value was set to 10 milliseconds. Wait 20 |
| 2001 // milliseconds so the sockets timeout. |
| 2002 base::PlatformThread::Sleep(20); |
| 2003 MessageLoop::current()->RunAllPending(); |
| 2004 |
| 2005 ASSERT_EQ(2, pool_->IdleSocketCount()); |
| 2006 |
| 2007 // Request a new socket. This should cleanup the unused and timed out ones. |
| 2008 // A new socket will be created rather than reusing the idle one. |
| 2009 CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
| 2010 rv = handle.Init("a", |
| 2011 params_, |
| 2012 LOWEST, |
| 2013 &callback, |
| 2014 pool_.get(), |
| 2015 log.bound()); |
| 2016 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 2017 EXPECT_EQ(OK, callback.WaitForResult()); |
| 2018 EXPECT_FALSE(handle.is_reused()); |
| 2019 |
| 2020 // Make sure the idle socket is closed |
| 2021 ASSERT_TRUE(pool_->HasGroup("a")); |
| 2022 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a")); |
| 2023 EXPECT_EQ(1, pool_->NumActiveSocketsInGroup("a")); |
| 2024 |
| 2025 net::CapturingNetLog::EntryList entries; |
| 2026 log.GetEntries(&entries); |
| 2027 EXPECT_FALSE(LogContainsEntryWithType( |
| 2028 entries, 1, NetLog::TYPE_SOCKET_POOL_REUSED_AN_EXISTING_SOCKET)); |
| 2029 } |
| 2030 |
| 1949 TEST_F(ClientSocketPoolBaseTest, CleanupTimedOutIdleSockets) { | 2031 TEST_F(ClientSocketPoolBaseTest, CleanupTimedOutIdleSockets) { |
| 1950 CreatePoolWithIdleTimeouts( | 2032 CreatePoolWithIdleTimeouts( |
| 1951 kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, | 2033 kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 1952 base::TimeDelta(), // Time out unused sockets immediately. | 2034 base::TimeDelta(), // Time out unused sockets immediately. |
| 1953 base::TimeDelta::FromDays(1)); // Don't time out used sockets. | 2035 base::TimeDelta::FromDays(1)); // Don't time out used sockets. |
| 1954 | 2036 |
| 1955 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); | 2037 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1956 | 2038 |
| 1957 // Startup two mock pending connect jobs, which will sit in the MessageLoop. | 2039 // Startup two mock pending connect jobs, which will sit in the MessageLoop. |
| 1958 | 2040 |
| (...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 | 3348 // The hung connect job should still be there, but everything else should be |
| 3267 // complete. | 3349 // complete. |
| 3268 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a")); | 3350 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a")); |
| 3269 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a")); | 3351 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a")); |
| 3270 EXPECT_EQ(1, pool_->NumActiveSocketsInGroup("a")); | 3352 EXPECT_EQ(1, pool_->NumActiveSocketsInGroup("a")); |
| 3271 } | 3353 } |
| 3272 | 3354 |
| 3273 } // namespace | 3355 } // namespace |
| 3274 | 3356 |
| 3275 } // namespace net | 3357 } // namespace net |
| OLD | NEW |