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. | |
2066 TEST_F(ClientSocketPoolBaseTest, DisableCleanupTimerReuse) { | |
2067 // Disable cleanup timer. | |
2068 internal::ClientSocketPoolBaseHelper::set_cleanup_timer_enabled(false); | |
2069 | |
2070 CreatePoolWithIdleTimeouts( | |
2071 kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, | |
2072 base::TimeDelta(), // Time out unused sockets immediately. | |
2073 base::TimeDelta::FromDays(1)); // Don't time out used sockets. | |
2074 | |
2075 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); | |
2076 | |
2077 ClientSocketHandle handle; | |
2078 TestCompletionCallback callback; | |
2079 int rv = handle.Init("a", params_, LOWEST, | |
2080 ClientSocketPool::RespectLimits::ENABLED, | |
2081 callback.callback(), pool_.get(), BoundNetLog()); | |
2082 ASSERT_EQ(ERR_IO_PENDING, rv); | |
2083 EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", &handle)); | |
2084 ASSERT_EQ(OK, callback.WaitForResult()); | |
2085 | |
2086 // Use and release the socket. | |
2087 EXPECT_EQ(1, handle.socket()->Write(NULL, 1, CompletionCallback())); | |
2088 TestLoadTimingInfoConnectedNotReused(handle); | |
2089 handle.Reset(); | |
2090 | |
2091 // Should now have one idle socket. | |
2092 ASSERT_EQ(1, pool_->IdleSocketCount()); | |
2093 | |
2094 // Request a new socket. This should reuse the old socket and complete | |
2095 // synchronously. | |
2096 BoundTestNetLog log; | |
2097 rv = handle.Init("a", params_, LOWEST, | |
2098 ClientSocketPool::RespectLimits::ENABLED, | |
2099 CompletionCallback(), pool_.get(), log.bound()); | |
2100 ASSERT_EQ(OK, rv); | |
2101 EXPECT_TRUE(handle.is_reused()); | |
2102 TestLoadTimingInfoConnectedReused(handle); | |
2103 | |
2104 ASSERT_TRUE(pool_->HasGroup("a")); | |
2105 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a")); | |
2106 EXPECT_EQ(1, pool_->NumActiveSocketsInGroup("a")); | |
2107 | |
2108 TestNetLogEntry::List entries; | |
2109 log.GetEntries(&entries); | |
2110 EXPECT_TRUE(LogContainsEntryWithType( | |
2111 entries, 1, NetLog::TYPE_SOCKET_POOL_REUSED_AN_EXISTING_SOCKET)); | |
2112 } | |
2113 | |
2114 #if defined(OS_IOS) | |
2115 // TODO(droger): Enable this test (crbug.com/512595). | |
2116 #define MAYBE_DisableCleanupTimerNoReuse DISABLED_DisableCleanupTimerNoReuse | |
2117 #else | |
2118 #define MAYBE_DisableCleanupTimerNoReuse DisableCleanupTimerNoReuse | |
2119 #endif | |
2120 // Make sure we cleanup old unused sockets when the cleanup timer is disabled. | |
2121 TEST_F(ClientSocketPoolBaseTest, MAYBE_DisableCleanupTimerNoReuse) { | |
2122 // Disable cleanup timer. | |
2123 internal::ClientSocketPoolBaseHelper::set_cleanup_timer_enabled(false); | |
2124 | |
2125 CreatePoolWithIdleTimeouts( | |
2126 kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, | |
2127 base::TimeDelta(), // Time out unused sockets immediately | |
2128 base::TimeDelta()); // Time out used sockets immediately | |
2129 | |
2130 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); | |
2131 | |
2132 // Startup two mock pending connect jobs, which will sit in the MessageLoop. | |
2133 | |
2134 ClientSocketHandle handle; | |
2135 TestCompletionCallback callback; | |
2136 int rv = handle.Init("a", params_, LOWEST, | |
2137 ClientSocketPool::RespectLimits::ENABLED, | |
2138 callback.callback(), pool_.get(), BoundNetLog()); | |
2139 ASSERT_EQ(ERR_IO_PENDING, rv); | |
2140 EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", &handle)); | |
2141 | |
2142 ClientSocketHandle handle2; | |
2143 TestCompletionCallback callback2; | |
2144 rv = handle2.Init("a", params_, LOWEST, | |
2145 ClientSocketPool::RespectLimits::ENABLED, | |
2146 callback2.callback(), pool_.get(), BoundNetLog()); | |
2147 ASSERT_EQ(ERR_IO_PENDING, rv); | |
2148 EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", &handle2)); | |
2149 | |
2150 // Cancel one of the requests. Wait for the other, which will get the first | |
2151 // job. Release the socket. Run the loop again to make sure the second | |
2152 // socket is sitting idle and the first one is released (since ReleaseSocket() | |
2153 // just posts a DoReleaseSocket() task). | |
2154 | |
2155 handle.Reset(); | |
2156 ASSERT_EQ(OK, callback2.WaitForResult()); | |
2157 // Use the socket. | |
2158 EXPECT_EQ(1, handle2.socket()->Write(NULL, 1, CompletionCallback())); | |
2159 handle2.Reset(); | |
2160 | |
2161 // We post all of our delayed tasks with a 2ms delay. I.e. they don't | |
2162 // actually become pending until 2ms after they have been created. In order | |
2163 // to flush all tasks, we need to wait so that we know there are no | |
2164 // soon-to-be-pending tasks waiting. | |
2165 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10)); | |
2166 base::MessageLoop::current()->RunUntilIdle(); | |
2167 | |
2168 // Both sockets should now be idle. | |
2169 ASSERT_EQ(2, pool_->IdleSocketCount()); | |
2170 | |
2171 // Request a new socket. This should cleanup the unused and timed out ones. | |
2172 // A new socket will be created rather than reusing the idle one. | |
2173 BoundTestNetLog log; | |
2174 TestCompletionCallback callback3; | |
2175 rv = handle.Init("a", params_, LOWEST, | |
2176 ClientSocketPool::RespectLimits::ENABLED, | |
2177 callback3.callback(), pool_.get(), log.bound()); | |
2178 ASSERT_EQ(ERR_IO_PENDING, rv); | |
2179 ASSERT_EQ(OK, callback3.WaitForResult()); | |
2180 EXPECT_FALSE(handle.is_reused()); | |
2181 | |
2182 // Make sure the idle socket is closed. | |
2183 ASSERT_TRUE(pool_->HasGroup("a")); | |
2184 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a")); | |
2185 EXPECT_EQ(1, pool_->NumActiveSocketsInGroup("a")); | |
2186 | |
2187 TestNetLogEntry::List entries; | |
2188 log.GetEntries(&entries); | |
2189 EXPECT_FALSE(LogContainsEntryWithType( | |
2190 entries, 1, NetLog::TYPE_SOCKET_POOL_REUSED_AN_EXISTING_SOCKET)); | |
2191 } | |
2192 | |
2193 TEST_F(ClientSocketPoolBaseTest, CleanupTimedOutIdleSockets) { | 2060 TEST_F(ClientSocketPoolBaseTest, CleanupTimedOutIdleSockets) { |
mmenke
2016/05/18 21:59:45
Think it makes more sense to remove this test, and
martijnc
2016/05/19 19:51:41
Done.
| |
2194 CreatePoolWithIdleTimeouts( | 2061 CreatePoolWithIdleTimeouts( |
2195 kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, | 2062 kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
2196 base::TimeDelta(), // Time out unused sockets immediately. | 2063 base::TimeDelta(), // Time out unused sockets immediately. |
2197 base::TimeDelta::FromDays(1)); // Don't time out used sockets. | 2064 base::TimeDelta::FromDays(1)); // Don't time out used sockets. |
2198 | 2065 |
2199 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); | 2066 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
2200 | 2067 |
2201 // Startup two mock pending connect jobs, which will sit in the MessageLoop. | 2068 // Startup two mock pending connect jobs, which will sit in the MessageLoop. |
2202 | 2069 |
2203 ClientSocketHandle handle; | 2070 ClientSocketHandle handle; |
(...skipping 1629 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
3833 request(1)->handle()->Reset(); | 3700 request(1)->handle()->Reset(); |
3834 ASSERT_EQ(1, pool_->NumConnectJobsInGroup("a")); | 3701 ASSERT_EQ(1, pool_->NumConnectJobsInGroup("a")); |
3835 | 3702 |
3836 EXPECT_EQ(OK, request(2)->WaitForResult()); | 3703 EXPECT_EQ(OK, request(2)->WaitForResult()); |
3837 EXPECT_FALSE(request(1)->have_result()); | 3704 EXPECT_FALSE(request(1)->have_result()); |
3838 } | 3705 } |
3839 | 3706 |
3840 } // namespace | 3707 } // namespace |
3841 | 3708 |
3842 } // namespace net | 3709 } // namespace net |
OLD | NEW |