| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <stdint.h> | 7 #include <stdint.h> |
| 8 #include <utility> | 8 #include <utility> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 646 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 657 bool waiting_for_result_; | 657 bool waiting_for_result_; |
| 658 int result_; | 658 int result_; |
| 659 }; | 659 }; |
| 660 | 660 |
| 661 class ClientSocketPoolBaseTest : public testing::Test { | 661 class ClientSocketPoolBaseTest : public testing::Test { |
| 662 protected: | 662 protected: |
| 663 ClientSocketPoolBaseTest() : params_(new TestSocketParams()) { | 663 ClientSocketPoolBaseTest() : params_(new TestSocketParams()) { |
| 664 connect_backup_jobs_enabled_ = | 664 connect_backup_jobs_enabled_ = |
| 665 internal::ClientSocketPoolBaseHelper::connect_backup_jobs_enabled(); | 665 internal::ClientSocketPoolBaseHelper::connect_backup_jobs_enabled(); |
| 666 internal::ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(true); | 666 internal::ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(true); |
| 667 cleanup_timer_enabled_ = | |
| 668 internal::ClientSocketPoolBaseHelper::cleanup_timer_enabled(); | |
| 669 } | 667 } |
| 670 | 668 |
| 671 ~ClientSocketPoolBaseTest() override { | 669 ~ClientSocketPoolBaseTest() override { |
| 672 internal::ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled( | 670 internal::ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled( |
| 673 connect_backup_jobs_enabled_); | 671 connect_backup_jobs_enabled_); |
| 674 internal::ClientSocketPoolBaseHelper::set_cleanup_timer_enabled( | |
| 675 cleanup_timer_enabled_); | |
| 676 } | 672 } |
| 677 | 673 |
| 678 void CreatePool(int max_sockets, int max_sockets_per_group) { | 674 void CreatePool(int max_sockets, int max_sockets_per_group) { |
| 679 CreatePoolWithIdleTimeouts( | 675 CreatePoolWithIdleTimeouts( |
| 680 max_sockets, | 676 max_sockets, |
| 681 max_sockets_per_group, | 677 max_sockets_per_group, |
| 682 ClientSocketPool::unused_idle_socket_timeout(), | 678 ClientSocketPool::unused_idle_socket_timeout(), |
| 683 ClientSocketPool::used_idle_socket_timeout()); | 679 ClientSocketPool::used_idle_socket_timeout()); |
| 684 } | 680 } |
| 685 | 681 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 724 | 720 |
| 725 TestSocketRequest* request(int i) { return test_base_.request(i); } | 721 TestSocketRequest* request(int i) { return test_base_.request(i); } |
| 726 size_t requests_size() const { return test_base_.requests_size(); } | 722 size_t requests_size() const { return test_base_.requests_size(); } |
| 727 std::vector<std::unique_ptr<TestSocketRequest>>* requests() { | 723 std::vector<std::unique_ptr<TestSocketRequest>>* requests() { |
| 728 return test_base_.requests(); | 724 return test_base_.requests(); |
| 729 } | 725 } |
| 730 size_t completion_count() const { return test_base_.completion_count(); } | 726 size_t completion_count() const { return test_base_.completion_count(); } |
| 731 | 727 |
| 732 TestNetLog net_log_; | 728 TestNetLog net_log_; |
| 733 bool connect_backup_jobs_enabled_; | 729 bool connect_backup_jobs_enabled_; |
| 734 bool cleanup_timer_enabled_; | |
| 735 MockClientSocketFactory client_socket_factory_; | 730 MockClientSocketFactory client_socket_factory_; |
| 736 TestConnectJobFactory* connect_job_factory_; | 731 TestConnectJobFactory* connect_job_factory_; |
| 737 scoped_refptr<TestSocketParams> params_; | 732 scoped_refptr<TestSocketParams> params_; |
| 738 std::unique_ptr<TestClientSocketPool> pool_; | 733 std::unique_ptr<TestClientSocketPool> pool_; |
| 739 ClientSocketPoolTest test_base_; | 734 ClientSocketPoolTest test_base_; |
| 740 }; | 735 }; |
| 741 | 736 |
| 742 // Even though a timeout is specified, it doesn't time out on a synchronous | 737 // Even though a timeout is specified, it doesn't time out on a synchronous |
| 743 // completion. | 738 // completion. |
| 744 TEST_F(ClientSocketPoolBaseTest, ConnectJob_NoTimeoutOnSynchronousCompletion) { | 739 TEST_F(ClientSocketPoolBaseTest, ConnectJob_NoTimeoutOnSynchronousCompletion) { |
| (...skipping 1310 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2055 ClientSocketPool::RespectLimits::ENABLED, | 2050 ClientSocketPool::RespectLimits::ENABLED, |
| 2056 callback.callback(), pool_.get(), BoundNetLog())); | 2051 callback.callback(), pool_.get(), BoundNetLog())); |
| 2057 EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", &handle)); | 2052 EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", &handle)); |
| 2058 EXPECT_EQ(ERR_CONNECTION_FAILED, callback.WaitForResult()); | 2053 EXPECT_EQ(ERR_CONNECTION_FAILED, callback.WaitForResult()); |
| 2059 EXPECT_FALSE(handle.is_initialized()); | 2054 EXPECT_FALSE(handle.is_initialized()); |
| 2060 EXPECT_FALSE(handle.socket()); | 2055 EXPECT_FALSE(handle.socket()); |
| 2061 EXPECT_TRUE(handle.is_ssl_error()); | 2056 EXPECT_TRUE(handle.is_ssl_error()); |
| 2062 EXPECT_FALSE(handle.ssl_error_response_info().headers.get() == NULL); | 2057 EXPECT_FALSE(handle.ssl_error_response_info().headers.get() == NULL); |
| 2063 } | 2058 } |
| 2064 | 2059 |
| 2065 // Make sure we can reuse sockets when the cleanup timer is disabled. | 2060 // Make sure we can reuse sockets. |
| 2066 TEST_F(ClientSocketPoolBaseTest, DisableCleanupTimerReuse) { | 2061 TEST_F(ClientSocketPoolBaseTest, CleanupTimedOutIdleSocketsReuse) { |
| 2067 // Disable cleanup timer. | |
| 2068 internal::ClientSocketPoolBaseHelper::set_cleanup_timer_enabled(false); | |
| 2069 | |
| 2070 CreatePoolWithIdleTimeouts( | 2062 CreatePoolWithIdleTimeouts( |
| 2071 kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, | 2063 kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 2072 base::TimeDelta(), // Time out unused sockets immediately. | 2064 base::TimeDelta(), // Time out unused sockets immediately. |
| 2073 base::TimeDelta::FromDays(1)); // Don't time out used sockets. | 2065 base::TimeDelta::FromDays(1)); // Don't time out used sockets. |
| 2074 | 2066 |
| 2075 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); | 2067 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2076 | 2068 |
| 2077 ClientSocketHandle handle; | 2069 ClientSocketHandle handle; |
| 2078 TestCompletionCallback callback; | 2070 TestCompletionCallback callback; |
| 2079 int rv = handle.Init("a", params_, LOWEST, | 2071 int rv = handle.Init("a", params_, LOWEST, |
| (...skipping 26 matching lines...) Expand all Loading... |
| 2106 EXPECT_EQ(1, pool_->NumActiveSocketsInGroup("a")); | 2098 EXPECT_EQ(1, pool_->NumActiveSocketsInGroup("a")); |
| 2107 | 2099 |
| 2108 TestNetLogEntry::List entries; | 2100 TestNetLogEntry::List entries; |
| 2109 log.GetEntries(&entries); | 2101 log.GetEntries(&entries); |
| 2110 EXPECT_TRUE(LogContainsEntryWithType( | 2102 EXPECT_TRUE(LogContainsEntryWithType( |
| 2111 entries, 1, NetLog::TYPE_SOCKET_POOL_REUSED_AN_EXISTING_SOCKET)); | 2103 entries, 1, NetLog::TYPE_SOCKET_POOL_REUSED_AN_EXISTING_SOCKET)); |
| 2112 } | 2104 } |
| 2113 | 2105 |
| 2114 #if defined(OS_IOS) | 2106 #if defined(OS_IOS) |
| 2115 // TODO(droger): Enable this test (crbug.com/512595). | 2107 // TODO(droger): Enable this test (crbug.com/512595). |
| 2116 #define MAYBE_DisableCleanupTimerNoReuse DISABLED_DisableCleanupTimerNoReuse | 2108 #define MAYBE_CleanupTimedOutIdleSocketsNoReuse \ |
| 2109 DISABLED_CleanupTimedOutIdleSocketsNoReuse |
| 2117 #else | 2110 #else |
| 2118 #define MAYBE_DisableCleanupTimerNoReuse DisableCleanupTimerNoReuse | 2111 #define MAYBE_CleanupTimedOutIdleSocketsNoReuse \ |
| 2112 CleanupTimedOutIdleSocketsNoReuse |
| 2119 #endif | 2113 #endif |
| 2120 // Make sure we cleanup old unused sockets when the cleanup timer is disabled. | 2114 // Make sure we cleanup old unused sockets. |
| 2121 TEST_F(ClientSocketPoolBaseTest, MAYBE_DisableCleanupTimerNoReuse) { | 2115 TEST_F(ClientSocketPoolBaseTest, MAYBE_CleanupTimedOutIdleSocketsNoReuse) { |
| 2122 // Disable cleanup timer. | |
| 2123 internal::ClientSocketPoolBaseHelper::set_cleanup_timer_enabled(false); | |
| 2124 | |
| 2125 CreatePoolWithIdleTimeouts( | 2116 CreatePoolWithIdleTimeouts( |
| 2126 kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, | 2117 kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 2127 base::TimeDelta(), // Time out unused sockets immediately | 2118 base::TimeDelta(), // Time out unused sockets immediately |
| 2128 base::TimeDelta()); // Time out used sockets immediately | 2119 base::TimeDelta()); // Time out used sockets immediately |
| 2129 | 2120 |
| 2130 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); | 2121 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2131 | 2122 |
| 2132 // Startup two mock pending connect jobs, which will sit in the MessageLoop. | 2123 // Startup two mock pending connect jobs, which will sit in the MessageLoop. |
| 2133 | 2124 |
| 2134 ClientSocketHandle handle; | 2125 ClientSocketHandle handle; |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2183 ASSERT_TRUE(pool_->HasGroup("a")); | 2174 ASSERT_TRUE(pool_->HasGroup("a")); |
| 2184 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a")); | 2175 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a")); |
| 2185 EXPECT_EQ(1, pool_->NumActiveSocketsInGroup("a")); | 2176 EXPECT_EQ(1, pool_->NumActiveSocketsInGroup("a")); |
| 2186 | 2177 |
| 2187 TestNetLogEntry::List entries; | 2178 TestNetLogEntry::List entries; |
| 2188 log.GetEntries(&entries); | 2179 log.GetEntries(&entries); |
| 2189 EXPECT_FALSE(LogContainsEntryWithType( | 2180 EXPECT_FALSE(LogContainsEntryWithType( |
| 2190 entries, 1, NetLog::TYPE_SOCKET_POOL_REUSED_AN_EXISTING_SOCKET)); | 2181 entries, 1, NetLog::TYPE_SOCKET_POOL_REUSED_AN_EXISTING_SOCKET)); |
| 2191 } | 2182 } |
| 2192 | 2183 |
| 2193 TEST_F(ClientSocketPoolBaseTest, CleanupTimedOutIdleSockets) { | |
| 2194 CreatePoolWithIdleTimeouts( | |
| 2195 kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, | |
| 2196 base::TimeDelta(), // Time out unused sockets immediately. | |
| 2197 base::TimeDelta::FromDays(1)); // Don't time out used sockets. | |
| 2198 | |
| 2199 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); | |
| 2200 | |
| 2201 // Startup two mock pending connect jobs, which will sit in the MessageLoop. | |
| 2202 | |
| 2203 ClientSocketHandle handle; | |
| 2204 TestCompletionCallback callback; | |
| 2205 int rv = handle.Init("a", params_, LOWEST, | |
| 2206 ClientSocketPool::RespectLimits::ENABLED, | |
| 2207 callback.callback(), pool_.get(), BoundNetLog()); | |
| 2208 EXPECT_EQ(ERR_IO_PENDING, rv); | |
| 2209 EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", &handle)); | |
| 2210 | |
| 2211 ClientSocketHandle handle2; | |
| 2212 TestCompletionCallback callback2; | |
| 2213 rv = handle2.Init("a", params_, LOWEST, | |
| 2214 ClientSocketPool::RespectLimits::ENABLED, | |
| 2215 callback2.callback(), pool_.get(), BoundNetLog()); | |
| 2216 EXPECT_EQ(ERR_IO_PENDING, rv); | |
| 2217 EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", &handle2)); | |
| 2218 | |
| 2219 // Cancel one of the requests. Wait for the other, which will get the first | |
| 2220 // job. Release the socket. Run the loop again to make sure the second | |
| 2221 // socket is sitting idle and the first one is released (since ReleaseSocket() | |
| 2222 // just posts a DoReleaseSocket() task). | |
| 2223 | |
| 2224 handle.Reset(); | |
| 2225 EXPECT_EQ(OK, callback2.WaitForResult()); | |
| 2226 // Use the socket. | |
| 2227 EXPECT_EQ(1, handle2.socket()->Write(NULL, 1, CompletionCallback())); | |
| 2228 handle2.Reset(); | |
| 2229 | |
| 2230 // We post all of our delayed tasks with a 2ms delay. I.e. they don't | |
| 2231 // actually become pending until 2ms after they have been created. In order | |
| 2232 // to flush all tasks, we need to wait so that we know there are no | |
| 2233 // soon-to-be-pending tasks waiting. | |
| 2234 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10)); | |
| 2235 base::MessageLoop::current()->RunUntilIdle(); | |
| 2236 | |
| 2237 ASSERT_EQ(2, pool_->IdleSocketCount()); | |
| 2238 | |
| 2239 // Invoke the idle socket cleanup check. Only one socket should be left, the | |
| 2240 // used socket. Request it to make sure that it's used. | |
| 2241 | |
| 2242 pool_->CleanupTimedOutIdleSockets(); | |
| 2243 BoundTestNetLog log; | |
| 2244 rv = handle.Init("a", params_, LOWEST, | |
| 2245 ClientSocketPool::RespectLimits::ENABLED, | |
| 2246 callback.callback(), pool_.get(), log.bound()); | |
| 2247 EXPECT_EQ(OK, rv); | |
| 2248 EXPECT_TRUE(handle.is_reused()); | |
| 2249 | |
| 2250 TestNetLogEntry::List entries; | |
| 2251 log.GetEntries(&entries); | |
| 2252 EXPECT_TRUE(LogContainsEntryWithType( | |
| 2253 entries, 1, NetLog::TYPE_SOCKET_POOL_REUSED_AN_EXISTING_SOCKET)); | |
| 2254 } | |
| 2255 | |
| 2256 // Make sure that we process all pending requests even when we're stalling | 2184 // Make sure that we process all pending requests even when we're stalling |
| 2257 // because of multiple releasing disconnected sockets. | 2185 // because of multiple releasing disconnected sockets. |
| 2258 TEST_F(ClientSocketPoolBaseTest, MultipleReleasingDisconnectedSockets) { | 2186 TEST_F(ClientSocketPoolBaseTest, MultipleReleasingDisconnectedSockets) { |
| 2259 CreatePoolWithIdleTimeouts( | 2187 CreatePoolWithIdleTimeouts( |
| 2260 kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, | 2188 kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 2261 base::TimeDelta(), // Time out unused sockets immediately. | 2189 base::TimeDelta(), // Time out unused sockets immediately. |
| 2262 base::TimeDelta::FromDays(1)); // Don't time out used sockets. | 2190 base::TimeDelta::FromDays(1)); // Don't time out used sockets. |
| 2263 | 2191 |
| 2264 connect_job_factory_->set_job_type(TestConnectJob::kMockJob); | 2192 connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 2265 | 2193 |
| (...skipping 1567 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3833 request(1)->handle()->Reset(); | 3761 request(1)->handle()->Reset(); |
| 3834 ASSERT_EQ(1, pool_->NumConnectJobsInGroup("a")); | 3762 ASSERT_EQ(1, pool_->NumConnectJobsInGroup("a")); |
| 3835 | 3763 |
| 3836 EXPECT_EQ(OK, request(2)->WaitForResult()); | 3764 EXPECT_EQ(OK, request(2)->WaitForResult()); |
| 3837 EXPECT_FALSE(request(1)->have_result()); | 3765 EXPECT_FALSE(request(1)->have_result()); |
| 3838 } | 3766 } |
| 3839 | 3767 |
| 3840 } // namespace | 3768 } // namespace |
| 3841 | 3769 |
| 3842 } // namespace net | 3770 } // namespace net |
| OLD | NEW |