[email protected] | e34400c3 | 2012-01-24 02:49:33 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 5 | #include "net/socket/client_socket_pool_base.h" |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 6 | |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 7 | #include <vector> |
| 8 | |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 9 | #include "base/bind.h" |
| 10 | #include "base/bind_helpers.h" |
[email protected] | 2041cf34 | 2010-02-19 03:15:59 | [diff] [blame] | 11 | #include "base/callback.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 12 | #include "base/memory/ref_counted.h" |
| 13 | #include "base/memory/scoped_vector.h" |
[email protected] | 6ea7b15 | 2011-12-21 21:21:13 | [diff] [blame] | 14 | #include "base/memory/weak_ptr.h" |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 15 | #include "base/message_loop.h" |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 16 | #include "base/run_loop.h" |
[email protected] | 1870d5cf | 2011-05-12 01:55:40 | [diff] [blame] | 17 | #include "base/stringprintf.h" |
[email protected] | e83326f | 2010-07-31 17:29:25 | [diff] [blame] | 18 | #include "base/string_number_conversions.h" |
[email protected] | f214f879 | 2011-01-01 02:17:08 | [diff] [blame] | 19 | #include "base/threading/platform_thread.h" |
[email protected] | f3a1c64 | 2011-07-12 19:15:03 | [diff] [blame] | 20 | #include "base/values.h" |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 21 | #include "net/base/load_timing_info.h" |
[email protected] | b258e079 | 2013-01-12 07:11:59 | [diff] [blame] | 22 | #include "net/base/load_timing_info_test_util.h" |
[email protected] | d8eb8424 | 2010-09-25 02:25:06 | [diff] [blame] | 23 | #include "net/base/net_errors.h" |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 24 | #include "net/base/net_log.h" |
| 25 | #include "net/base/net_log_unittest.h" |
[email protected] | ac790b4 | 2009-12-02 04:31:31 | [diff] [blame] | 26 | #include "net/base/request_priority.h" |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 27 | #include "net/base/test_completion_callback.h" |
[email protected] | 277d594 | 2010-08-11 21:02:35 | [diff] [blame] | 28 | #include "net/http/http_response_headers.h" |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 29 | #include "net/socket/client_socket_factory.h" |
| 30 | #include "net/socket/client_socket_handle.h" |
[email protected] | b89f7e4 | 2010-05-20 20:37:00 | [diff] [blame] | 31 | #include "net/socket/client_socket_pool_histograms.h" |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 32 | #include "net/socket/socket_test_util.h" |
[email protected] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame] | 33 | #include "net/socket/stream_socket.h" |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 34 | #include "testing/gmock/include/gmock/gmock.h" |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 35 | #include "testing/gtest/include/gtest/gtest.h" |
| 36 | |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 37 | using ::testing::Invoke; |
| 38 | using ::testing::Return; |
| 39 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 40 | namespace net { |
| 41 | |
| 42 | namespace { |
| 43 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 44 | const int kDefaultMaxSockets = 4; |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 45 | const int kDefaultMaxSocketsPerGroup = 2; |
[email protected] | a554a826 | 2010-05-20 00:13:52 | [diff] [blame] | 46 | const net::RequestPriority kDefaultPriority = MEDIUM; |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 47 | |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 48 | // Make sure |handle| sets load times correctly when it has been assigned a |
| 49 | // reused socket. |
| 50 | void TestLoadTimingInfoConnectedReused(const ClientSocketHandle& handle) { |
| 51 | LoadTimingInfo load_timing_info; |
| 52 | // Only pass true in as |is_reused|, as in general, HttpStream types should |
| 53 | // have stricter concepts of reuse than socket pools. |
| 54 | EXPECT_TRUE(handle.GetLoadTimingInfo(true, &load_timing_info)); |
| 55 | |
| 56 | EXPECT_EQ(true, load_timing_info.socket_reused); |
| 57 | EXPECT_NE(NetLog::Source::kInvalidId, load_timing_info.socket_log_id); |
| 58 | |
[email protected] | b258e079 | 2013-01-12 07:11:59 | [diff] [blame] | 59 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 60 | ExpectLoadTimingHasOnlyConnectionTimes(load_timing_info); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | // Make sure |handle| sets load times correctly when it has been assigned a |
| 64 | // fresh socket. Also runs TestLoadTimingInfoConnectedReused, since the owner |
| 65 | // of a connection where |is_reused| is false may consider the connection |
| 66 | // reused. |
| 67 | void TestLoadTimingInfoConnectedNotReused(const ClientSocketHandle& handle) { |
| 68 | EXPECT_FALSE(handle.is_reused()); |
| 69 | |
| 70 | LoadTimingInfo load_timing_info; |
| 71 | EXPECT_TRUE(handle.GetLoadTimingInfo(false, &load_timing_info)); |
| 72 | |
| 73 | EXPECT_FALSE(load_timing_info.socket_reused); |
| 74 | EXPECT_NE(NetLog::Source::kInvalidId, load_timing_info.socket_log_id); |
| 75 | |
[email protected] | b258e079 | 2013-01-12 07:11:59 | [diff] [blame] | 76 | ExpectConnectTimingHasTimes(load_timing_info.connect_timing, |
| 77 | CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY); |
| 78 | ExpectLoadTimingHasOnlyConnectionTimes(load_timing_info); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 79 | |
| 80 | TestLoadTimingInfoConnectedReused(handle); |
| 81 | } |
| 82 | |
| 83 | // Make sure |handle| sets load times correctly, in the case that it does not |
| 84 | // currently have a socket. |
| 85 | void TestLoadTimingInfoNotConnected(const ClientSocketHandle& handle) { |
| 86 | // Should only be set to true once a socket is assigned, if at all. |
| 87 | EXPECT_FALSE(handle.is_reused()); |
| 88 | |
| 89 | LoadTimingInfo load_timing_info; |
| 90 | EXPECT_FALSE(handle.GetLoadTimingInfo(false, &load_timing_info)); |
| 91 | |
| 92 | EXPECT_FALSE(load_timing_info.socket_reused); |
| 93 | EXPECT_EQ(NetLog::Source::kInvalidId, load_timing_info.socket_log_id); |
| 94 | |
[email protected] | b258e079 | 2013-01-12 07:11:59 | [diff] [blame] | 95 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 96 | ExpectLoadTimingHasOnlyConnectionTimes(load_timing_info); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 97 | } |
| 98 | |
[email protected] | df4b4ef | 2010-07-12 18:25:21 | [diff] [blame] | 99 | class TestSocketParams : public base::RefCounted<TestSocketParams> { |
[email protected] | 5acdce1 | 2011-03-30 13:00:20 | [diff] [blame] | 100 | public: |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 101 | TestSocketParams() : ignore_limits_(false) {} |
| 102 | |
| 103 | void set_ignore_limits(bool ignore_limits) { |
| 104 | ignore_limits_ = ignore_limits; |
| 105 | } |
| 106 | bool ignore_limits() { return ignore_limits_; } |
| 107 | |
[email protected] | df4b4ef | 2010-07-12 18:25:21 | [diff] [blame] | 108 | private: |
| 109 | friend class base::RefCounted<TestSocketParams>; |
| 110 | ~TestSocketParams() {} |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 111 | |
| 112 | bool ignore_limits_; |
[email protected] | df4b4ef | 2010-07-12 18:25:21 | [diff] [blame] | 113 | }; |
[email protected] | 7fc5b09a | 2010-02-27 00:07:38 | [diff] [blame] | 114 | typedef ClientSocketPoolBase<TestSocketParams> TestClientSocketPoolBase; |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 115 | |
[email protected] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame] | 116 | class MockClientSocket : public StreamSocket { |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 117 | public: |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 118 | explicit MockClientSocket(net::NetLog* net_log) |
| 119 | : connected_(false), |
| 120 | net_log_(BoundNetLog::Make(net_log, net::NetLog::SOURCE_SOCKET)), |
[email protected] | e86df8dc | 2013-03-30 13:18:28 | [diff] [blame] | 121 | was_used_to_convey_data_(false) { |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 122 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 123 | |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 124 | // Socket implementation. |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 125 | virtual int Read( |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 126 | IOBuffer* /* buf */, int len, |
| 127 | const CompletionCallback& /* callback */) OVERRIDE { |
[email protected] | e86df8dc | 2013-03-30 13:18:28 | [diff] [blame] | 128 | return ERR_UNEXPECTED; |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 129 | } |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 130 | |
| 131 | virtual int Write( |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 132 | IOBuffer* /* buf */, int len, |
| 133 | const CompletionCallback& /* callback */) OVERRIDE { |
[email protected] | 0f873e8 | 2010-09-02 16:09:01 | [diff] [blame] | 134 | was_used_to_convey_data_ = true; |
| 135 | return len; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 136 | } |
[email protected] | 46fadfd | 2013-02-06 09:40:16 | [diff] [blame] | 137 | virtual bool SetReceiveBufferSize(int32 size) OVERRIDE { return true; } |
| 138 | virtual bool SetSendBufferSize(int32 size) OVERRIDE { return true; } |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 139 | |
[email protected] | dbf036f | 2011-12-06 23:33:24 | [diff] [blame] | 140 | // StreamSocket implementation. |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 141 | virtual int Connect(const CompletionCallback& callback) OVERRIDE { |
[email protected] | dbf036f | 2011-12-06 23:33:24 | [diff] [blame] | 142 | connected_ = true; |
| 143 | return OK; |
| 144 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 145 | |
[email protected] | 46fadfd | 2013-02-06 09:40:16 | [diff] [blame] | 146 | virtual void Disconnect() OVERRIDE { connected_ = false; } |
| 147 | virtual bool IsConnected() const OVERRIDE { return connected_; } |
| 148 | virtual bool IsConnectedAndIdle() const OVERRIDE { return connected_; } |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 149 | |
[email protected] | 46fadfd | 2013-02-06 09:40:16 | [diff] [blame] | 150 | virtual int GetPeerAddress(IPEndPoint* /* address */) const OVERRIDE { |
[email protected] | 9f864b3 | 2010-01-20 15:01:16 | [diff] [blame] | 151 | return ERR_UNEXPECTED; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 152 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 153 | |
[email protected] | 46fadfd | 2013-02-06 09:40:16 | [diff] [blame] | 154 | virtual int GetLocalAddress(IPEndPoint* /* address */) const OVERRIDE { |
[email protected] | e7f74da | 2011-04-19 23:49:35 | [diff] [blame] | 155 | return ERR_UNEXPECTED; |
| 156 | } |
| 157 | |
[email protected] | 46fadfd | 2013-02-06 09:40:16 | [diff] [blame] | 158 | virtual const BoundNetLog& NetLog() const OVERRIDE { |
[email protected] | a2006ece | 2010-04-23 16:44:02 | [diff] [blame] | 159 | return net_log_; |
| 160 | } |
| 161 | |
[email protected] | 46fadfd | 2013-02-06 09:40:16 | [diff] [blame] | 162 | virtual void SetSubresourceSpeculation() OVERRIDE {} |
| 163 | virtual void SetOmniboxSpeculation() OVERRIDE {} |
| 164 | virtual bool WasEverUsed() const OVERRIDE { |
[email protected] | e86df8dc | 2013-03-30 13:18:28 | [diff] [blame] | 165 | return was_used_to_convey_data_; |
[email protected] | 5e6efa5 | 2011-06-27 17:26:41 | [diff] [blame] | 166 | } |
[email protected] | 46fadfd | 2013-02-06 09:40:16 | [diff] [blame] | 167 | virtual bool UsingTCPFastOpen() const OVERRIDE { return false; } |
[email protected] | 46fadfd | 2013-02-06 09:40:16 | [diff] [blame] | 168 | virtual bool WasNpnNegotiated() const OVERRIDE { |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 169 | return false; |
| 170 | } |
[email protected] | 46fadfd | 2013-02-06 09:40:16 | [diff] [blame] | 171 | virtual NextProto GetNegotiatedProtocol() const OVERRIDE { |
[email protected] | 33661e48 | 2012-04-03 16:16:26 | [diff] [blame] | 172 | return kProtoUnknown; |
| 173 | } |
[email protected] | 46fadfd | 2013-02-06 09:40:16 | [diff] [blame] | 174 | virtual bool GetSSLInfo(SSLInfo* ssl_info) OVERRIDE { |
[email protected] | 2d88e7d | 2012-07-19 17:55:17 | [diff] [blame] | 175 | return false; |
| 176 | } |
[email protected] | 9b5614a | 2010-08-25 20:29:45 | [diff] [blame] | 177 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 178 | private: |
| 179 | bool connected_; |
[email protected] | a2006ece | 2010-04-23 16:44:02 | [diff] [blame] | 180 | BoundNetLog net_log_; |
[email protected] | 0f873e8 | 2010-09-02 16:09:01 | [diff] [blame] | 181 | bool was_used_to_convey_data_; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 182 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 183 | DISALLOW_COPY_AND_ASSIGN(MockClientSocket); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 184 | }; |
| 185 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 186 | class TestConnectJob; |
| 187 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 188 | class MockClientSocketFactory : public ClientSocketFactory { |
| 189 | public: |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 190 | MockClientSocketFactory() : allocation_count_(0) {} |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 191 | |
[email protected] | 98b0e58 | 2011-06-22 14:31:41 | [diff] [blame] | 192 | virtual DatagramClientSocket* CreateDatagramClientSocket( |
[email protected] | 5370c01 | 2011-06-29 03:47:04 | [diff] [blame] | 193 | DatagramSocket::BindType bind_type, |
| 194 | const RandIntCallback& rand_int_cb, |
[email protected] | 98b0e58 | 2011-06-22 14:31:41 | [diff] [blame] | 195 | NetLog* net_log, |
[email protected] | 46fadfd | 2013-02-06 09:40:16 | [diff] [blame] | 196 | const NetLog::Source& source) OVERRIDE { |
[email protected] | 98b0e58 | 2011-06-22 14:31:41 | [diff] [blame] | 197 | NOTREACHED(); |
| 198 | return NULL; |
| 199 | } |
| 200 | |
[email protected] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame] | 201 | virtual StreamSocket* CreateTransportClientSocket( |
[email protected] | 0a0b768 | 2010-08-25 17:08:07 | [diff] [blame] | 202 | const AddressList& addresses, |
| 203 | NetLog* /* net_log */, |
[email protected] | 46fadfd | 2013-02-06 09:40:16 | [diff] [blame] | 204 | const NetLog::Source& /*source*/) OVERRIDE { |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 205 | allocation_count_++; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 206 | return NULL; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | virtual SSLClientSocket* CreateSSLClientSocket( |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 210 | ClientSocketHandle* transport_socket, |
[email protected] | 4f4de7e6 | 2010-11-12 19:55:27 | [diff] [blame] | 211 | const HostPortPair& host_and_port, |
[email protected] | 7ab5bbd1 | 2010-10-19 13:33:21 | [diff] [blame] | 212 | const SSLConfig& ssl_config, |
[email protected] | 46fadfd | 2013-02-06 09:40:16 | [diff] [blame] | 213 | const SSLClientSocketContext& context) OVERRIDE { |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 214 | NOTIMPLEMENTED(); |
| 215 | return NULL; |
| 216 | } |
| 217 | |
[email protected] | 46fadfd | 2013-02-06 09:40:16 | [diff] [blame] | 218 | virtual void ClearSSLSessionCache() OVERRIDE { |
[email protected] | 25f4735 | 2011-02-25 16:31:59 | [diff] [blame] | 219 | NOTIMPLEMENTED(); |
| 220 | } |
| 221 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 222 | void WaitForSignal(TestConnectJob* job) { waiting_jobs_.push_back(job); } |
| 223 | void SignalJobs(); |
| 224 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 225 | int allocation_count() const { return allocation_count_; } |
| 226 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 227 | private: |
| 228 | int allocation_count_; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 229 | std::vector<TestConnectJob*> waiting_jobs_; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 230 | }; |
| 231 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 232 | class TestConnectJob : public ConnectJob { |
| 233 | public: |
| 234 | enum JobType { |
| 235 | kMockJob, |
| 236 | kMockFailingJob, |
| 237 | kMockPendingJob, |
| 238 | kMockPendingFailingJob, |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 239 | kMockWaitingJob, |
| 240 | kMockAdvancingLoadStateJob, |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 241 | kMockRecoverableJob, |
| 242 | kMockPendingRecoverableJob, |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 243 | kMockAdditionalErrorStateJob, |
| 244 | kMockPendingAdditionalErrorStateJob, |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 245 | }; |
| 246 | |
[email protected] | 994d493 | 2010-07-12 17:55:13 | [diff] [blame] | 247 | // The kMockPendingJob uses a slight delay before allowing the connect |
| 248 | // to complete. |
| 249 | static const int kPendingConnectDelay = 2; |
| 250 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 251 | TestConnectJob(JobType job_type, |
| 252 | const std::string& group_name, |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 253 | const TestClientSocketPoolBase::Request& request, |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 254 | base::TimeDelta timeout_duration, |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 255 | ConnectJob::Delegate* delegate, |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 256 | MockClientSocketFactory* client_socket_factory, |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 257 | NetLog* net_log) |
| 258 | : ConnectJob(group_name, timeout_duration, delegate, |
| 259 | BoundNetLog::Make(net_log, NetLog::SOURCE_CONNECT_JOB)), |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 260 | job_type_(job_type), |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 261 | client_socket_factory_(client_socket_factory), |
[email protected] | aa249b5 | 2013-04-30 01:04:32 | [diff] [blame] | 262 | weak_factory_(this), |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 263 | load_state_(LOAD_STATE_IDLE), |
| 264 | store_additional_error_state_(false) {} |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 265 | |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 266 | void Signal() { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 267 | DoConnect(waiting_success_, true /* async */, false /* recoverable */); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 268 | } |
| 269 | |
[email protected] | 46fadfd | 2013-02-06 09:40:16 | [diff] [blame] | 270 | virtual LoadState GetLoadState() const OVERRIDE { return load_state_; } |
[email protected] | 4645135 | 2009-09-01 14:54:21 | [diff] [blame] | 271 | |
[email protected] | 46fadfd | 2013-02-06 09:40:16 | [diff] [blame] | 272 | virtual void GetAdditionalErrorState(ClientSocketHandle* handle) OVERRIDE { |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 273 | if (store_additional_error_state_) { |
| 274 | // Set all of the additional error state fields in some way. |
| 275 | handle->set_is_ssl_error(true); |
[email protected] | 8b49869 | 2010-07-16 17:11:43 | [diff] [blame] | 276 | HttpResponseInfo info; |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 277 | info.headers = new HttpResponseHeaders(std::string()); |
[email protected] | 8b49869 | 2010-07-16 17:11:43 | [diff] [blame] | 278 | handle->set_ssl_error_response_info(info); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 282 | private: |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 283 | // ConnectJob implementation. |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 284 | |
[email protected] | 46fadfd | 2013-02-06 09:40:16 | [diff] [blame] | 285 | virtual int ConnectInternal() OVERRIDE { |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 286 | AddressList ignored; |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 287 | client_socket_factory_->CreateTransportClientSocket( |
[email protected] | 0a0b768 | 2010-08-25 17:08:07 | [diff] [blame] | 288 | ignored, NULL, net::NetLog::Source()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 289 | set_socket(new MockClientSocket(net_log().net_log())); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 290 | switch (job_type_) { |
| 291 | case kMockJob: |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 292 | return DoConnect(true /* successful */, false /* sync */, |
| 293 | false /* recoverable */); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 294 | case kMockFailingJob: |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 295 | return DoConnect(false /* error */, false /* sync */, |
| 296 | false /* recoverable */); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 297 | case kMockPendingJob: |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 298 | set_load_state(LOAD_STATE_CONNECTING); |
[email protected] | 6b17538 | 2009-10-13 06:47:47 | [diff] [blame] | 299 | |
| 300 | // Depending on execution timings, posting a delayed task can result |
| 301 | // in the task getting executed the at the earliest possible |
| 302 | // opportunity or only after returning once from the message loop and |
| 303 | // then a second call into the message loop. In order to make behavior |
| 304 | // more deterministic, we change the default delay to 2ms. This should |
| 305 | // always require us to wait for the second call into the message loop. |
| 306 | // |
| 307 | // N.B. The correct fix for this and similar timing problems is to |
| 308 | // abstract time for the purpose of unittests. Unfortunately, we have |
| 309 | // a lot of third-party components that directly call the various |
| 310 | // time functions, so this change would be rather invasive. |
| 311 | MessageLoop::current()->PostDelayedTask( |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 312 | FROM_HERE, |
[email protected] | 6ea7b15 | 2011-12-21 21:21:13 | [diff] [blame] | 313 | base::Bind(base::IgnoreResult(&TestConnectJob::DoConnect), |
| 314 | weak_factory_.GetWeakPtr(), |
| 315 | true /* successful */, |
| 316 | true /* async */, |
| 317 | false /* recoverable */), |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 318 | base::TimeDelta::FromMilliseconds(kPendingConnectDelay)); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 319 | return ERR_IO_PENDING; |
| 320 | case kMockPendingFailingJob: |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 321 | set_load_state(LOAD_STATE_CONNECTING); |
[email protected] | 6b17538 | 2009-10-13 06:47:47 | [diff] [blame] | 322 | MessageLoop::current()->PostDelayedTask( |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 323 | FROM_HERE, |
[email protected] | 6ea7b15 | 2011-12-21 21:21:13 | [diff] [blame] | 324 | base::Bind(base::IgnoreResult(&TestConnectJob::DoConnect), |
| 325 | weak_factory_.GetWeakPtr(), |
| 326 | false /* error */, |
| 327 | true /* async */, |
| 328 | false /* recoverable */), |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 329 | base::TimeDelta::FromMilliseconds(2)); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 330 | return ERR_IO_PENDING; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 331 | case kMockWaitingJob: |
| 332 | client_socket_factory_->WaitForSignal(this); |
| 333 | waiting_success_ = true; |
| 334 | return ERR_IO_PENDING; |
| 335 | case kMockAdvancingLoadStateJob: |
[email protected] | e6ec67b | 2010-06-16 00:12:46 | [diff] [blame] | 336 | MessageLoop::current()->PostTask( |
[email protected] | 6ea7b15 | 2011-12-21 21:21:13 | [diff] [blame] | 337 | FROM_HERE, base::Bind(&TestConnectJob::AdvanceLoadState, |
| 338 | weak_factory_.GetWeakPtr(), load_state_)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 339 | return ERR_IO_PENDING; |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 340 | case kMockRecoverableJob: |
| 341 | return DoConnect(false /* error */, false /* sync */, |
| 342 | true /* recoverable */); |
| 343 | case kMockPendingRecoverableJob: |
| 344 | set_load_state(LOAD_STATE_CONNECTING); |
| 345 | MessageLoop::current()->PostDelayedTask( |
| 346 | FROM_HERE, |
[email protected] | 6ea7b15 | 2011-12-21 21:21:13 | [diff] [blame] | 347 | base::Bind(base::IgnoreResult(&TestConnectJob::DoConnect), |
| 348 | weak_factory_.GetWeakPtr(), |
| 349 | false /* error */, |
| 350 | true /* async */, |
| 351 | true /* recoverable */), |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 352 | base::TimeDelta::FromMilliseconds(2)); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 353 | return ERR_IO_PENDING; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 354 | case kMockAdditionalErrorStateJob: |
| 355 | store_additional_error_state_ = true; |
| 356 | return DoConnect(false /* error */, false /* sync */, |
| 357 | false /* recoverable */); |
| 358 | case kMockPendingAdditionalErrorStateJob: |
| 359 | set_load_state(LOAD_STATE_CONNECTING); |
| 360 | store_additional_error_state_ = true; |
| 361 | MessageLoop::current()->PostDelayedTask( |
| 362 | FROM_HERE, |
[email protected] | 6ea7b15 | 2011-12-21 21:21:13 | [diff] [blame] | 363 | base::Bind(base::IgnoreResult(&TestConnectJob::DoConnect), |
| 364 | weak_factory_.GetWeakPtr(), |
| 365 | false /* error */, |
| 366 | true /* async */, |
| 367 | false /* recoverable */), |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 368 | base::TimeDelta::FromMilliseconds(2)); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 369 | return ERR_IO_PENDING; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 370 | default: |
| 371 | NOTREACHED(); |
[email protected] | 6e713f0 | 2009-08-06 02:56:40 | [diff] [blame] | 372 | set_socket(NULL); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 373 | return ERR_FAILED; |
| 374 | } |
| 375 | } |
| 376 | |
[email protected] | 4645135 | 2009-09-01 14:54:21 | [diff] [blame] | 377 | void set_load_state(LoadState load_state) { load_state_ = load_state; } |
| 378 | |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 379 | int DoConnect(bool succeed, bool was_async, bool recoverable) { |
| 380 | int result = OK; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 381 | if (succeed) { |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 382 | socket()->Connect(CompletionCallback()); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 383 | } else if (recoverable) { |
| 384 | result = ERR_PROXY_AUTH_REQUESTED; |
[email protected] | 6e713f0 | 2009-08-06 02:56:40 | [diff] [blame] | 385 | } else { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 386 | result = ERR_CONNECTION_FAILED; |
[email protected] | 6e713f0 | 2009-08-06 02:56:40 | [diff] [blame] | 387 | set_socket(NULL); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 388 | } |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 389 | |
| 390 | if (was_async) |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 391 | NotifyDelegateOfCompletion(result); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 392 | return result; |
| 393 | } |
| 394 | |
[email protected] | cfa8228c | 2010-06-17 01:07:56 | [diff] [blame] | 395 | // This function helps simulate the progress of load states on a ConnectJob. |
| 396 | // Each time it is called it advances the load state and posts a task to be |
| 397 | // called again. It stops at the last connecting load state (the one |
| 398 | // before LOAD_STATE_SENDING_REQUEST). |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 399 | void AdvanceLoadState(LoadState state) { |
| 400 | int tmp = state; |
| 401 | tmp++; |
[email protected] | cfa8228c | 2010-06-17 01:07:56 | [diff] [blame] | 402 | if (tmp < LOAD_STATE_SENDING_REQUEST) { |
| 403 | state = static_cast<LoadState>(tmp); |
| 404 | set_load_state(state); |
| 405 | MessageLoop::current()->PostTask( |
[email protected] | 6ea7b15 | 2011-12-21 21:21:13 | [diff] [blame] | 406 | FROM_HERE, base::Bind(&TestConnectJob::AdvanceLoadState, |
| 407 | weak_factory_.GetWeakPtr(), state)); |
[email protected] | cfa8228c | 2010-06-17 01:07:56 | [diff] [blame] | 408 | } |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | bool waiting_success_; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 412 | const JobType job_type_; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 413 | MockClientSocketFactory* const client_socket_factory_; |
[email protected] | 6ea7b15 | 2011-12-21 21:21:13 | [diff] [blame] | 414 | base::WeakPtrFactory<TestConnectJob> weak_factory_; |
[email protected] | 4645135 | 2009-09-01 14:54:21 | [diff] [blame] | 415 | LoadState load_state_; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 416 | bool store_additional_error_state_; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 417 | |
| 418 | DISALLOW_COPY_AND_ASSIGN(TestConnectJob); |
| 419 | }; |
| 420 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 421 | class TestConnectJobFactory |
| 422 | : public TestClientSocketPoolBase::ConnectJobFactory { |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 423 | public: |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 424 | TestConnectJobFactory(MockClientSocketFactory* client_socket_factory, |
| 425 | NetLog* net_log) |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 426 | : job_type_(TestConnectJob::kMockJob), |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 427 | job_types_(NULL), |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 428 | client_socket_factory_(client_socket_factory), |
| 429 | net_log_(net_log) { |
| 430 | } |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 431 | |
| 432 | virtual ~TestConnectJobFactory() {} |
| 433 | |
| 434 | void set_job_type(TestConnectJob::JobType job_type) { job_type_ = job_type; } |
| 435 | |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 436 | void set_job_types(std::list<TestConnectJob::JobType>* job_types) { |
| 437 | job_types_ = job_types; |
| 438 | CHECK(!job_types_->empty()); |
| 439 | } |
| 440 | |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 441 | void set_timeout_duration(base::TimeDelta timeout_duration) { |
| 442 | timeout_duration_ = timeout_duration; |
| 443 | } |
| 444 | |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 445 | // ConnectJobFactory implementation. |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 446 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 447 | virtual ConnectJob* NewConnectJob( |
| 448 | const std::string& group_name, |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 449 | const TestClientSocketPoolBase::Request& request, |
[email protected] | 46fadfd | 2013-02-06 09:40:16 | [diff] [blame] | 450 | ConnectJob::Delegate* delegate) const OVERRIDE { |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 451 | EXPECT_TRUE(!job_types_ || !job_types_->empty()); |
| 452 | TestConnectJob::JobType job_type = job_type_; |
| 453 | if (job_types_ && !job_types_->empty()) { |
| 454 | job_type = job_types_->front(); |
| 455 | job_types_->pop_front(); |
| 456 | } |
| 457 | return new TestConnectJob(job_type, |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 458 | group_name, |
| 459 | request, |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 460 | timeout_duration_, |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 461 | delegate, |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 462 | client_socket_factory_, |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 463 | net_log_); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 464 | } |
| 465 | |
[email protected] | 46fadfd | 2013-02-06 09:40:16 | [diff] [blame] | 466 | virtual base::TimeDelta ConnectionTimeout() const OVERRIDE { |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 467 | return timeout_duration_; |
| 468 | } |
| 469 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 470 | private: |
| 471 | TestConnectJob::JobType job_type_; |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 472 | std::list<TestConnectJob::JobType>* job_types_; |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 473 | base::TimeDelta timeout_duration_; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 474 | MockClientSocketFactory* const client_socket_factory_; |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 475 | NetLog* net_log_; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 476 | |
| 477 | DISALLOW_COPY_AND_ASSIGN(TestConnectJobFactory); |
| 478 | }; |
| 479 | |
| 480 | class TestClientSocketPool : public ClientSocketPool { |
| 481 | public: |
| 482 | TestClientSocketPool( |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 483 | int max_sockets, |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 484 | int max_sockets_per_group, |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 485 | ClientSocketPoolHistograms* histograms, |
[email protected] | 9bf28db | 2009-08-29 01:35:16 | [diff] [blame] | 486 | base::TimeDelta unused_idle_socket_timeout, |
| 487 | base::TimeDelta used_idle_socket_timeout, |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 488 | TestClientSocketPoolBase::ConnectJobFactory* connect_job_factory) |
[email protected] | b89f7e4 | 2010-05-20 20:37:00 | [diff] [blame] | 489 | : base_(max_sockets, max_sockets_per_group, histograms, |
[email protected] | 9bf28db | 2009-08-29 01:35:16 | [diff] [blame] | 490 | unused_idle_socket_timeout, used_idle_socket_timeout, |
[email protected] | 66761b95 | 2010-06-25 21:30:38 | [diff] [blame] | 491 | connect_job_factory) {} |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 492 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 493 | virtual ~TestClientSocketPool() {} |
| 494 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 495 | virtual int RequestSocket( |
| 496 | const std::string& group_name, |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 497 | const void* params, |
[email protected] | ac790b4 | 2009-12-02 04:31:31 | [diff] [blame] | 498 | net::RequestPriority priority, |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 499 | ClientSocketHandle* handle, |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 500 | const CompletionCallback& callback, |
[email protected] | d4dfdab | 2011-12-07 16:56:59 | [diff] [blame] | 501 | const BoundNetLog& net_log) OVERRIDE { |
[email protected] | df4b4ef | 2010-07-12 18:25:21 | [diff] [blame] | 502 | const scoped_refptr<TestSocketParams>* casted_socket_params = |
| 503 | static_cast<const scoped_refptr<TestSocketParams>*>(params); |
| 504 | return base_.RequestSocket(group_name, *casted_socket_params, priority, |
| 505 | handle, callback, net_log); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 506 | } |
| 507 | |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 508 | virtual void RequestSockets(const std::string& group_name, |
| 509 | const void* params, |
| 510 | int num_sockets, |
[email protected] | d4dfdab | 2011-12-07 16:56:59 | [diff] [blame] | 511 | const BoundNetLog& net_log) OVERRIDE { |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 512 | const scoped_refptr<TestSocketParams>* casted_params = |
| 513 | static_cast<const scoped_refptr<TestSocketParams>*>(params); |
| 514 | |
| 515 | base_.RequestSockets(group_name, *casted_params, num_sockets, net_log); |
| 516 | } |
| 517 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 518 | virtual void CancelRequest( |
| 519 | const std::string& group_name, |
[email protected] | d4dfdab | 2011-12-07 16:56:59 | [diff] [blame] | 520 | ClientSocketHandle* handle) OVERRIDE { |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 521 | base_.CancelRequest(group_name, handle); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | virtual void ReleaseSocket( |
| 525 | const std::string& group_name, |
[email protected] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame] | 526 | StreamSocket* socket, |
[email protected] | d4dfdab | 2011-12-07 16:56:59 | [diff] [blame] | 527 | int id) OVERRIDE { |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 528 | base_.ReleaseSocket(group_name, socket, id); |
| 529 | } |
| 530 | |
[email protected] | 7af985a | 2012-12-14 22:40:42 | [diff] [blame] | 531 | virtual void FlushWithError(int error) OVERRIDE { |
| 532 | base_.FlushWithError(error); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 533 | } |
| 534 | |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 535 | virtual bool IsStalled() const OVERRIDE { |
| 536 | return base_.IsStalled(); |
| 537 | } |
| 538 | |
[email protected] | d4dfdab | 2011-12-07 16:56:59 | [diff] [blame] | 539 | virtual void CloseIdleSockets() OVERRIDE { |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 540 | base_.CloseIdleSockets(); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 541 | } |
| 542 | |
[email protected] | d4dfdab | 2011-12-07 16:56:59 | [diff] [blame] | 543 | virtual int IdleSocketCount() const OVERRIDE { |
| 544 | return base_.idle_socket_count(); |
| 545 | } |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 546 | |
[email protected] | d4dfdab | 2011-12-07 16:56:59 | [diff] [blame] | 547 | virtual int IdleSocketCountInGroup( |
| 548 | const std::string& group_name) const OVERRIDE { |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 549 | return base_.IdleSocketCountInGroup(group_name); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 550 | } |
| 551 | |
[email protected] | d4dfdab | 2011-12-07 16:56:59 | [diff] [blame] | 552 | virtual LoadState GetLoadState( |
| 553 | const std::string& group_name, |
| 554 | const ClientSocketHandle* handle) const OVERRIDE { |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 555 | return base_.GetLoadState(group_name, handle); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 556 | } |
| 557 | |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 558 | virtual void AddLayeredPool(LayeredPool* pool) OVERRIDE { |
| 559 | base_.AddLayeredPool(pool); |
| 560 | } |
| 561 | |
| 562 | virtual void RemoveLayeredPool(LayeredPool* pool) OVERRIDE { |
| 563 | base_.RemoveLayeredPool(pool); |
| 564 | } |
| 565 | |
[email protected] | d4dfdab | 2011-12-07 16:56:59 | [diff] [blame] | 566 | virtual DictionaryValue* GetInfoAsValue( |
| 567 | const std::string& name, |
| 568 | const std::string& type, |
| 569 | bool include_nested_pools) const OVERRIDE { |
[email protected] | 59d7a5a | 2010-08-30 16:44:27 | [diff] [blame] | 570 | return base_.GetInfoAsValue(name, type); |
| 571 | } |
| 572 | |
[email protected] | d4dfdab | 2011-12-07 16:56:59 | [diff] [blame] | 573 | virtual base::TimeDelta ConnectionTimeout() const OVERRIDE { |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 574 | return base_.ConnectionTimeout(); |
| 575 | } |
| 576 | |
[email protected] | d4dfdab | 2011-12-07 16:56:59 | [diff] [blame] | 577 | virtual ClientSocketPoolHistograms* histograms() const OVERRIDE { |
[email protected] | b89f7e4 | 2010-05-20 20:37:00 | [diff] [blame] | 578 | return base_.histograms(); |
| 579 | } |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 580 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 581 | const TestClientSocketPoolBase* base() const { return &base_; } |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 582 | |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 583 | int NumUnassignedConnectJobsInGroup(const std::string& group_name) const { |
| 584 | return base_.NumUnassignedConnectJobsInGroup(group_name); |
| 585 | } |
| 586 | |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 587 | int NumConnectJobsInGroup(const std::string& group_name) const { |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 588 | return base_.NumConnectJobsInGroup(group_name); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 589 | } |
| 590 | |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 591 | int NumActiveSocketsInGroup(const std::string& group_name) const { |
| 592 | return base_.NumActiveSocketsInGroup(group_name); |
| 593 | } |
| 594 | |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 595 | bool HasGroup(const std::string& group_name) const { |
| 596 | return base_.HasGroup(group_name); |
| 597 | } |
| 598 | |
[email protected] | 9bf28db | 2009-08-29 01:35:16 | [diff] [blame] | 599 | void CleanupTimedOutIdleSockets() { base_.CleanupIdleSockets(false); } |
| 600 | |
[email protected] | 06d9404 | 2010-08-25 01:45:22 | [diff] [blame] | 601 | void EnableConnectBackupJobs() { base_.EnableConnectBackupJobs(); } |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 602 | |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 603 | bool CloseOneIdleConnectionInLayeredPool() { |
| 604 | return base_.CloseOneIdleConnectionInLayeredPool(); |
| 605 | } |
| 606 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 607 | private: |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 608 | TestClientSocketPoolBase base_; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 609 | |
| 610 | DISALLOW_COPY_AND_ASSIGN(TestClientSocketPool); |
| 611 | }; |
| 612 | |
[email protected] | a937a06d | 2009-08-19 21:19:24 | [diff] [blame] | 613 | } // namespace |
| 614 | |
[email protected] | 7fc5b09a | 2010-02-27 00:07:38 | [diff] [blame] | 615 | REGISTER_SOCKET_PARAMS_FOR_POOL(TestClientSocketPool, TestSocketParams); |
[email protected] | a937a06d | 2009-08-19 21:19:24 | [diff] [blame] | 616 | |
| 617 | namespace { |
| 618 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 619 | void MockClientSocketFactory::SignalJobs() { |
| 620 | for (std::vector<TestConnectJob*>::iterator it = waiting_jobs_.begin(); |
| 621 | it != waiting_jobs_.end(); ++it) { |
| 622 | (*it)->Signal(); |
| 623 | } |
| 624 | waiting_jobs_.clear(); |
| 625 | } |
| 626 | |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 627 | class TestConnectJobDelegate : public ConnectJob::Delegate { |
| 628 | public: |
| 629 | TestConnectJobDelegate() |
| 630 | : have_result_(false), waiting_for_result_(false), result_(OK) {} |
| 631 | virtual ~TestConnectJobDelegate() {} |
| 632 | |
[email protected] | 46fadfd | 2013-02-06 09:40:16 | [diff] [blame] | 633 | virtual void OnConnectJobComplete(int result, ConnectJob* job) OVERRIDE { |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 634 | result_ = result; |
[email protected] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame] | 635 | scoped_ptr<StreamSocket> socket(job->ReleaseSocket()); |
[email protected] | 9b6fee1 | 2009-09-29 18:13:07 | [diff] [blame] | 636 | // socket.get() should be NULL iff result != OK |
| 637 | EXPECT_EQ(socket.get() == NULL, result != OK); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 638 | delete job; |
| 639 | have_result_ = true; |
| 640 | if (waiting_for_result_) |
| 641 | MessageLoop::current()->Quit(); |
| 642 | } |
| 643 | |
| 644 | int WaitForResult() { |
| 645 | DCHECK(!waiting_for_result_); |
| 646 | while (!have_result_) { |
| 647 | waiting_for_result_ = true; |
| 648 | MessageLoop::current()->Run(); |
| 649 | waiting_for_result_ = false; |
| 650 | } |
| 651 | have_result_ = false; // auto-reset for next callback |
| 652 | return result_; |
| 653 | } |
| 654 | |
| 655 | private: |
| 656 | bool have_result_; |
| 657 | bool waiting_for_result_; |
| 658 | int result_; |
| 659 | }; |
| 660 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 661 | class ClientSocketPoolBaseTest : public testing::Test { |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 662 | protected: |
[email protected] | b89f7e4 | 2010-05-20 20:37:00 | [diff] [blame] | 663 | ClientSocketPoolBaseTest() |
[email protected] | df4b4ef | 2010-07-12 18:25:21 | [diff] [blame] | 664 | : params_(new TestSocketParams()), |
[email protected] | 636b825 | 2011-04-08 19:56:54 | [diff] [blame] | 665 | histograms_("ClientSocketPoolTest") { |
| 666 | connect_backup_jobs_enabled_ = |
| 667 | internal::ClientSocketPoolBaseHelper::connect_backup_jobs_enabled(); |
| 668 | internal::ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(true); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 669 | cleanup_timer_enabled_ = |
| 670 | internal::ClientSocketPoolBaseHelper::cleanup_timer_enabled(); |
[email protected] | 636b825 | 2011-04-08 19:56:54 | [diff] [blame] | 671 | } |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 672 | |
[email protected] | 636b825 | 2011-04-08 19:56:54 | [diff] [blame] | 673 | virtual ~ClientSocketPoolBaseTest() { |
| 674 | internal::ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled( |
| 675 | connect_backup_jobs_enabled_); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 676 | internal::ClientSocketPoolBaseHelper::set_cleanup_timer_enabled( |
| 677 | cleanup_timer_enabled_); |
[email protected] | 636b825 | 2011-04-08 19:56:54 | [diff] [blame] | 678 | } |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 679 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 680 | void CreatePool(int max_sockets, int max_sockets_per_group) { |
[email protected] | 9bf28db | 2009-08-29 01:35:16 | [diff] [blame] | 681 | CreatePoolWithIdleTimeouts( |
| 682 | max_sockets, |
| 683 | max_sockets_per_group, |
[email protected] | 82b8c96 | 2011-10-12 09:17:30 | [diff] [blame] | 684 | ClientSocketPool::unused_idle_socket_timeout(), |
| 685 | ClientSocketPool::used_idle_socket_timeout()); |
[email protected] | 9bf28db | 2009-08-29 01:35:16 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | void CreatePoolWithIdleTimeouts( |
| 689 | int max_sockets, int max_sockets_per_group, |
| 690 | base::TimeDelta unused_idle_socket_timeout, |
| 691 | base::TimeDelta used_idle_socket_timeout) { |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 692 | DCHECK(!pool_.get()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 693 | connect_job_factory_ = new TestConnectJobFactory(&client_socket_factory_, |
| 694 | &net_log_); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 695 | pool_.reset(new TestClientSocketPool(max_sockets, |
| 696 | max_sockets_per_group, |
| 697 | &histograms_, |
| 698 | unused_idle_socket_timeout, |
| 699 | used_idle_socket_timeout, |
| 700 | connect_job_factory_)); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 701 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 702 | |
[email protected] | ac790b4 | 2009-12-02 04:31:31 | [diff] [blame] | 703 | int StartRequest(const std::string& group_name, |
| 704 | net::RequestPriority priority) { |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 705 | return test_base_.StartRequestUsingPool< |
| 706 | TestClientSocketPool, TestSocketParams>( |
| 707 | pool_.get(), group_name, priority, params_); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 708 | } |
| 709 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 710 | int GetOrderOfRequest(size_t index) const { |
| 711 | return test_base_.GetOrderOfRequest(index); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 712 | } |
| 713 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 714 | bool ReleaseOneConnection(ClientSocketPoolTest::KeepAlive keep_alive) { |
| 715 | return test_base_.ReleaseOneConnection(keep_alive); |
| 716 | } |
| 717 | |
| 718 | void ReleaseAllConnections(ClientSocketPoolTest::KeepAlive keep_alive) { |
| 719 | test_base_.ReleaseAllConnections(keep_alive); |
| 720 | } |
| 721 | |
| 722 | TestSocketRequest* request(int i) { return test_base_.request(i); } |
| 723 | size_t requests_size() const { return test_base_.requests_size(); } |
| 724 | ScopedVector<TestSocketRequest>* requests() { return test_base_.requests(); } |
| 725 | size_t completion_count() const { return test_base_.completion_count(); } |
| 726 | |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 727 | CapturingNetLog net_log_; |
[email protected] | 636b825 | 2011-04-08 19:56:54 | [diff] [blame] | 728 | bool connect_backup_jobs_enabled_; |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 729 | bool cleanup_timer_enabled_; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 730 | MockClientSocketFactory client_socket_factory_; |
[email protected] | 17a0c6c | 2009-08-04 00:07:04 | [diff] [blame] | 731 | TestConnectJobFactory* connect_job_factory_; |
[email protected] | df4b4ef | 2010-07-12 18:25:21 | [diff] [blame] | 732 | scoped_refptr<TestSocketParams> params_; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 733 | ClientSocketPoolHistograms histograms_; |
| 734 | scoped_ptr<TestClientSocketPool> pool_; |
| 735 | ClientSocketPoolTest test_base_; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 736 | }; |
| 737 | |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 738 | // Even though a timeout is specified, it doesn't time out on a synchronous |
| 739 | // completion. |
| 740 | TEST_F(ClientSocketPoolBaseTest, ConnectJob_NoTimeoutOnSynchronousCompletion) { |
| 741 | TestConnectJobDelegate delegate; |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 742 | ClientSocketHandle ignored; |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 743 | TestClientSocketPoolBase::Request request( |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 744 | &ignored, CompletionCallback(), kDefaultPriority, |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 745 | internal::ClientSocketPoolBaseHelper::NORMAL, |
[email protected] | 5acdce1 | 2011-03-30 13:00:20 | [diff] [blame] | 746 | false, params_, BoundNetLog()); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 747 | scoped_ptr<TestConnectJob> job( |
| 748 | new TestConnectJob(TestConnectJob::kMockJob, |
[email protected] | ec08bb2 | 2009-08-12 00:25:12 | [diff] [blame] | 749 | "a", |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 750 | request, |
| 751 | base::TimeDelta::FromMicroseconds(1), |
| 752 | &delegate, |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 753 | &client_socket_factory_, |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 754 | NULL)); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 755 | EXPECT_EQ(OK, job->Connect()); |
| 756 | } |
| 757 | |
| 758 | TEST_F(ClientSocketPoolBaseTest, ConnectJob_TimedOut) { |
| 759 | TestConnectJobDelegate delegate; |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 760 | ClientSocketHandle ignored; |
[email protected] | 333bdf6 | 2012-06-08 22:57:29 | [diff] [blame] | 761 | CapturingNetLog log; |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 762 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 763 | TestClientSocketPoolBase::Request request( |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 764 | &ignored, CompletionCallback(), kDefaultPriority, |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 765 | internal::ClientSocketPoolBaseHelper::NORMAL, |
[email protected] | 5acdce1 | 2011-03-30 13:00:20 | [diff] [blame] | 766 | false, params_, BoundNetLog()); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 767 | // Deleted by TestConnectJobDelegate. |
| 768 | TestConnectJob* job = |
| 769 | new TestConnectJob(TestConnectJob::kMockPendingJob, |
[email protected] | ec08bb2 | 2009-08-12 00:25:12 | [diff] [blame] | 770 | "a", |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 771 | request, |
| 772 | base::TimeDelta::FromMicroseconds(1), |
| 773 | &delegate, |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 774 | &client_socket_factory_, |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 775 | &log); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 776 | ASSERT_EQ(ERR_IO_PENDING, job->Connect()); |
[email protected] | 26b997396 | 2012-01-28 00:57:00 | [diff] [blame] | 777 | base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(1)); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 778 | EXPECT_EQ(ERR_TIMED_OUT, delegate.WaitForResult()); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 779 | |
[email protected] | 333bdf6 | 2012-06-08 22:57:29 | [diff] [blame] | 780 | CapturingNetLog::CapturedEntryList entries; |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 781 | log.GetEntries(&entries); |
| 782 | |
| 783 | EXPECT_EQ(6u, entries.size()); |
[email protected] | e9002a9 | 2010-01-29 07:10:46 | [diff] [blame] | 784 | EXPECT_TRUE(LogContainsBeginEvent( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 785 | entries, 0, NetLog::TYPE_SOCKET_POOL_CONNECT_JOB)); |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 786 | EXPECT_TRUE(LogContainsBeginEvent( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 787 | entries, 1, NetLog::TYPE_SOCKET_POOL_CONNECT_JOB_CONNECT)); |
[email protected] | e9002a9 | 2010-01-29 07:10:46 | [diff] [blame] | 788 | EXPECT_TRUE(LogContainsEvent( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 789 | entries, 2, NetLog::TYPE_CONNECT_JOB_SET_SOCKET, |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 790 | NetLog::PHASE_NONE)); |
| 791 | EXPECT_TRUE(LogContainsEvent( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 792 | entries, 3, NetLog::TYPE_SOCKET_POOL_CONNECT_JOB_TIMED_OUT, |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 793 | NetLog::PHASE_NONE)); |
[email protected] | e9002a9 | 2010-01-29 07:10:46 | [diff] [blame] | 794 | EXPECT_TRUE(LogContainsEndEvent( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 795 | entries, 4, NetLog::TYPE_SOCKET_POOL_CONNECT_JOB_CONNECT)); |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 796 | EXPECT_TRUE(LogContainsEndEvent( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 797 | entries, 5, NetLog::TYPE_SOCKET_POOL_CONNECT_JOB)); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 798 | } |
| 799 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 800 | TEST_F(ClientSocketPoolBaseTest, BasicSynchronous) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 801 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 802 | |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 803 | TestCompletionCallback callback; |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 804 | ClientSocketHandle handle; |
[email protected] | 333bdf6 | 2012-06-08 22:57:29 | [diff] [blame] | 805 | CapturingBoundNetLog log; |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 806 | TestLoadTimingInfoNotConnected(handle); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 807 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 808 | EXPECT_EQ(OK, |
| 809 | handle.Init("a", |
| 810 | params_, |
| 811 | kDefaultPriority, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 812 | callback.callback(), |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 813 | pool_.get(), |
| 814 | log.bound())); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 815 | EXPECT_TRUE(handle.is_initialized()); |
| 816 | EXPECT_TRUE(handle.socket()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 817 | TestLoadTimingInfoConnectedNotReused(handle); |
| 818 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 819 | handle.Reset(); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 820 | TestLoadTimingInfoNotConnected(handle); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 821 | |
[email protected] | 333bdf6 | 2012-06-08 22:57:29 | [diff] [blame] | 822 | CapturingNetLog::CapturedEntryList entries; |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 823 | log.GetEntries(&entries); |
| 824 | |
| 825 | EXPECT_EQ(4u, entries.size()); |
[email protected] | e9002a9 | 2010-01-29 07:10:46 | [diff] [blame] | 826 | EXPECT_TRUE(LogContainsBeginEvent( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 827 | entries, 0, NetLog::TYPE_SOCKET_POOL)); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 828 | EXPECT_TRUE(LogContainsEvent( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 829 | entries, 1, NetLog::TYPE_SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 830 | NetLog::PHASE_NONE)); |
| 831 | EXPECT_TRUE(LogContainsEvent( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 832 | entries, 2, NetLog::TYPE_SOCKET_POOL_BOUND_TO_SOCKET, |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 833 | NetLog::PHASE_NONE)); |
| 834 | EXPECT_TRUE(LogContainsEndEvent( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 835 | entries, 3, NetLog::TYPE_SOCKET_POOL)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 836 | } |
| 837 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 838 | TEST_F(ClientSocketPoolBaseTest, InitConnectionFailure) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 839 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 840 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 841 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
[email protected] | 333bdf6 | 2012-06-08 22:57:29 | [diff] [blame] | 842 | CapturingBoundNetLog log; |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 843 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 844 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 845 | TestCompletionCallback callback; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 846 | // Set the additional error state members to ensure that they get cleared. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 847 | handle.set_is_ssl_error(true); |
[email protected] | 8b49869 | 2010-07-16 17:11:43 | [diff] [blame] | 848 | HttpResponseInfo info; |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 849 | info.headers = new HttpResponseHeaders(std::string()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 850 | handle.set_ssl_error_response_info(info); |
| 851 | EXPECT_EQ(ERR_CONNECTION_FAILED, |
| 852 | handle.Init("a", |
| 853 | params_, |
| 854 | kDefaultPriority, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 855 | callback.callback(), |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 856 | pool_.get(), |
| 857 | log.bound())); |
| 858 | EXPECT_FALSE(handle.socket()); |
| 859 | EXPECT_FALSE(handle.is_ssl_error()); |
| 860 | EXPECT_TRUE(handle.ssl_error_response_info().headers.get() == NULL); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 861 | TestLoadTimingInfoNotConnected(handle); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 862 | |
[email protected] | 333bdf6 | 2012-06-08 22:57:29 | [diff] [blame] | 863 | CapturingNetLog::CapturedEntryList entries; |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 864 | log.GetEntries(&entries); |
| 865 | |
| 866 | EXPECT_EQ(3u, entries.size()); |
[email protected] | 5a1d7ca | 2010-04-28 20:12:27 | [diff] [blame] | 867 | EXPECT_TRUE(LogContainsBeginEvent( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 868 | entries, 0, NetLog::TYPE_SOCKET_POOL)); |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 869 | EXPECT_TRUE(LogContainsEvent( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 870 | entries, 1, NetLog::TYPE_SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 871 | NetLog::PHASE_NONE)); |
[email protected] | a2006ece | 2010-04-23 16:44:02 | [diff] [blame] | 872 | EXPECT_TRUE(LogContainsEndEvent( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 873 | entries, 2, NetLog::TYPE_SOCKET_POOL)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 874 | } |
| 875 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 876 | TEST_F(ClientSocketPoolBaseTest, TotalLimit) { |
| 877 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 878 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 879 | // TODO(eroman): Check that the NetLog contains this event. |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 880 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 881 | EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); |
| 882 | EXPECT_EQ(OK, StartRequest("b", kDefaultPriority)); |
| 883 | EXPECT_EQ(OK, StartRequest("c", kDefaultPriority)); |
| 884 | EXPECT_EQ(OK, StartRequest("d", kDefaultPriority)); |
| 885 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 886 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 887 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 888 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 889 | |
| 890 | EXPECT_EQ(ERR_IO_PENDING, StartRequest("e", kDefaultPriority)); |
| 891 | EXPECT_EQ(ERR_IO_PENDING, StartRequest("f", kDefaultPriority)); |
| 892 | EXPECT_EQ(ERR_IO_PENDING, StartRequest("g", kDefaultPriority)); |
| 893 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 894 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 895 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 896 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 897 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 898 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 899 | |
| 900 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 901 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 902 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 903 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 904 | EXPECT_EQ(5, GetOrderOfRequest(5)); |
| 905 | EXPECT_EQ(6, GetOrderOfRequest(6)); |
| 906 | EXPECT_EQ(7, GetOrderOfRequest(7)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 907 | |
| 908 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 909 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(8)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 910 | } |
| 911 | |
| 912 | TEST_F(ClientSocketPoolBaseTest, TotalLimitReachedNewGroup) { |
| 913 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 914 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 915 | // TODO(eroman): Check that the NetLog contains this event. |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 916 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 917 | // Reach all limits: max total sockets, and max sockets per group. |
| 918 | EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); |
| 919 | EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); |
| 920 | EXPECT_EQ(OK, StartRequest("b", kDefaultPriority)); |
| 921 | EXPECT_EQ(OK, StartRequest("b", kDefaultPriority)); |
| 922 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 923 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 924 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 925 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 926 | |
| 927 | // Now create a new group and verify that we don't starve it. |
| 928 | EXPECT_EQ(ERR_IO_PENDING, StartRequest("c", kDefaultPriority)); |
| 929 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 930 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 931 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 932 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 933 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 934 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 935 | |
| 936 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 937 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 938 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 939 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 940 | EXPECT_EQ(5, GetOrderOfRequest(5)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 941 | |
| 942 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 943 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(6)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 944 | } |
| 945 | |
| 946 | TEST_F(ClientSocketPoolBaseTest, TotalLimitRespectsPriority) { |
| 947 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 948 | |
[email protected] | ac790b4 | 2009-12-02 04:31:31 | [diff] [blame] | 949 | EXPECT_EQ(OK, StartRequest("b", LOWEST)); |
| 950 | EXPECT_EQ(OK, StartRequest("a", MEDIUM)); |
| 951 | EXPECT_EQ(OK, StartRequest("b", HIGHEST)); |
| 952 | EXPECT_EQ(OK, StartRequest("a", LOWEST)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 953 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 954 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 955 | client_socket_factory_.allocation_count()); |
| 956 | |
[email protected] | ac790b4 | 2009-12-02 04:31:31 | [diff] [blame] | 957 | EXPECT_EQ(ERR_IO_PENDING, StartRequest("c", LOWEST)); |
| 958 | EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", MEDIUM)); |
| 959 | EXPECT_EQ(ERR_IO_PENDING, StartRequest("b", HIGHEST)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 960 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 961 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 962 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 963 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 964 | |
| 965 | // First 4 requests don't have to wait, and finish in order. |
| 966 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 967 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 968 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 969 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 970 | |
[email protected] | ac790b4 | 2009-12-02 04:31:31 | [diff] [blame] | 971 | // Request ("b", HIGHEST) has the highest priority, then ("a", MEDIUM), |
| 972 | // and then ("c", LOWEST). |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 973 | EXPECT_EQ(7, GetOrderOfRequest(5)); |
| 974 | EXPECT_EQ(6, GetOrderOfRequest(6)); |
| 975 | EXPECT_EQ(5, GetOrderOfRequest(7)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 976 | |
| 977 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 978 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(9)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 979 | } |
| 980 | |
| 981 | TEST_F(ClientSocketPoolBaseTest, TotalLimitRespectsGroupLimit) { |
| 982 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 983 | |
[email protected] | ac790b4 | 2009-12-02 04:31:31 | [diff] [blame] | 984 | EXPECT_EQ(OK, StartRequest("a", LOWEST)); |
| 985 | EXPECT_EQ(OK, StartRequest("a", LOW)); |
| 986 | EXPECT_EQ(OK, StartRequest("b", HIGHEST)); |
| 987 | EXPECT_EQ(OK, StartRequest("b", MEDIUM)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 988 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 989 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 990 | client_socket_factory_.allocation_count()); |
| 991 | |
[email protected] | ac790b4 | 2009-12-02 04:31:31 | [diff] [blame] | 992 | EXPECT_EQ(ERR_IO_PENDING, StartRequest("c", MEDIUM)); |
| 993 | EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOW)); |
| 994 | EXPECT_EQ(ERR_IO_PENDING, StartRequest("b", HIGHEST)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 995 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 996 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 997 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 998 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 999 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1000 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1001 | |
| 1002 | // First 4 requests don't have to wait, and finish in order. |
| 1003 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1004 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1005 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 1006 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 1007 | |
| 1008 | // Request ("b", 7) has the highest priority, but we can't make new socket for |
| 1009 | // group "b", because it has reached the per-group limit. Then we make |
| 1010 | // socket for ("c", 6), because it has higher priority than ("a", 4), |
| 1011 | // and we still can't make a socket for group "b". |
| 1012 | EXPECT_EQ(5, GetOrderOfRequest(5)); |
| 1013 | EXPECT_EQ(6, GetOrderOfRequest(6)); |
| 1014 | EXPECT_EQ(7, GetOrderOfRequest(7)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1015 | |
| 1016 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1017 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(8)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1018 | } |
| 1019 | |
| 1020 | // Make sure that we count connecting sockets against the total limit. |
| 1021 | TEST_F(ClientSocketPoolBaseTest, TotalLimitCountsConnectingSockets) { |
| 1022 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1023 | |
| 1024 | EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); |
| 1025 | EXPECT_EQ(OK, StartRequest("b", kDefaultPriority)); |
| 1026 | EXPECT_EQ(OK, StartRequest("c", kDefaultPriority)); |
| 1027 | |
| 1028 | // Create one asynchronous request. |
| 1029 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1030 | EXPECT_EQ(ERR_IO_PENDING, StartRequest("d", kDefaultPriority)); |
| 1031 | |
[email protected] | 6b17538 | 2009-10-13 06:47:47 | [diff] [blame] | 1032 | // We post all of our delayed tasks with a 2ms delay. I.e. they don't |
| 1033 | // actually become pending until 2ms after they have been created. In order |
| 1034 | // to flush all tasks, we need to wait so that we know there are no |
| 1035 | // soon-to-be-pending tasks waiting. |
[email protected] | 26b997396 | 2012-01-28 00:57:00 | [diff] [blame] | 1036 | base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10)); |
[email protected] | b4c62eb | 2012-11-14 18:36:51 | [diff] [blame] | 1037 | MessageLoop::current()->RunUntilIdle(); |
[email protected] | 6b17538 | 2009-10-13 06:47:47 | [diff] [blame] | 1038 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1039 | // The next synchronous request should wait for its turn. |
| 1040 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 1041 | EXPECT_EQ(ERR_IO_PENDING, StartRequest("e", kDefaultPriority)); |
| 1042 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1043 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1044 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1045 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1046 | client_socket_factory_.allocation_count()); |
| 1047 | |
| 1048 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1049 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1050 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 1051 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1052 | EXPECT_EQ(5, GetOrderOfRequest(5)); |
| 1053 | |
| 1054 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1055 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(6)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1056 | } |
| 1057 | |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1058 | TEST_F(ClientSocketPoolBaseTest, CorrectlyCountStalledGroups) { |
| 1059 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 1060 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 1061 | |
| 1062 | EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); |
| 1063 | EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); |
| 1064 | EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); |
| 1065 | EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); |
| 1066 | |
| 1067 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 1068 | |
| 1069 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1070 | |
| 1071 | EXPECT_EQ(ERR_IO_PENDING, StartRequest("b", kDefaultPriority)); |
| 1072 | EXPECT_EQ(ERR_IO_PENDING, StartRequest("c", kDefaultPriority)); |
| 1073 | |
| 1074 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1075 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1076 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1077 | EXPECT_EQ(kDefaultMaxSockets + 1, client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1078 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1079 | EXPECT_EQ(kDefaultMaxSockets + 2, client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1080 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
| 1081 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1082 | EXPECT_EQ(kDefaultMaxSockets + 2, client_socket_factory_.allocation_count()); |
| 1083 | } |
| 1084 | |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 1085 | TEST_F(ClientSocketPoolBaseTest, StallAndThenCancelAndTriggerAvailableSocket) { |
| 1086 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 1087 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1088 | |
| 1089 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1090 | TestCompletionCallback callback; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1091 | EXPECT_EQ(ERR_IO_PENDING, |
| 1092 | handle.Init("a", |
| 1093 | params_, |
| 1094 | kDefaultPriority, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1095 | callback.callback(), |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1096 | pool_.get(), |
| 1097 | BoundNetLog())); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 1098 | |
| 1099 | ClientSocketHandle handles[4]; |
| 1100 | for (size_t i = 0; i < arraysize(handles); ++i) { |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1101 | TestCompletionCallback callback; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1102 | EXPECT_EQ(ERR_IO_PENDING, |
| 1103 | handles[i].Init("b", |
| 1104 | params_, |
| 1105 | kDefaultPriority, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1106 | callback.callback(), |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1107 | pool_.get(), |
| 1108 | BoundNetLog())); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 1109 | } |
| 1110 | |
| 1111 | // One will be stalled, cancel all the handles now. |
| 1112 | // This should hit the OnAvailableSocketSlot() code where we previously had |
| 1113 | // stalled groups, but no longer have any. |
| 1114 | for (size_t i = 0; i < arraysize(handles); ++i) |
| 1115 | handles[i].Reset(); |
| 1116 | } |
| 1117 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1118 | TEST_F(ClientSocketPoolBaseTest, CancelStalledSocketAtSocketLimit) { |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1119 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1120 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 1121 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1122 | { |
| 1123 | ClientSocketHandle handles[kDefaultMaxSockets]; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1124 | TestCompletionCallback callbacks[kDefaultMaxSockets]; |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1125 | for (int i = 0; i < kDefaultMaxSockets; ++i) { |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1126 | EXPECT_EQ(OK, handles[i].Init(base::IntToString(i), |
| 1127 | params_, |
[email protected] | e83326f | 2010-07-31 17:29:25 | [diff] [blame] | 1128 | kDefaultPriority, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1129 | callbacks[i].callback(), |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1130 | pool_.get(), |
| 1131 | BoundNetLog())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1132 | } |
| 1133 | |
| 1134 | // Force a stalled group. |
| 1135 | ClientSocketHandle stalled_handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1136 | TestCompletionCallback callback; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1137 | EXPECT_EQ(ERR_IO_PENDING, stalled_handle.Init("foo", |
| 1138 | params_, |
| 1139 | kDefaultPriority, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1140 | callback.callback(), |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1141 | pool_.get(), |
| 1142 | BoundNetLog())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1143 | |
| 1144 | // Cancel the stalled request. |
| 1145 | stalled_handle.Reset(); |
| 1146 | |
| 1147 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1148 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 1149 | |
| 1150 | // Dropping out of scope will close all handles and return them to idle. |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1151 | } |
| 1152 | |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1153 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1154 | EXPECT_EQ(kDefaultMaxSockets, pool_->IdleSocketCount()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1155 | } |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1156 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1157 | TEST_F(ClientSocketPoolBaseTest, CancelPendingSocketAtSocketLimit) { |
| 1158 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1159 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 1160 | |
| 1161 | { |
| 1162 | ClientSocketHandle handles[kDefaultMaxSockets]; |
| 1163 | for (int i = 0; i < kDefaultMaxSockets; ++i) { |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1164 | TestCompletionCallback callback; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1165 | EXPECT_EQ(ERR_IO_PENDING, handles[i].Init(base::IntToString(i), |
| 1166 | params_, |
| 1167 | kDefaultPriority, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1168 | callback.callback(), |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1169 | pool_.get(), |
| 1170 | BoundNetLog())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1171 | } |
| 1172 | |
| 1173 | // Force a stalled group. |
| 1174 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1175 | ClientSocketHandle stalled_handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1176 | TestCompletionCallback callback; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1177 | EXPECT_EQ(ERR_IO_PENDING, stalled_handle.Init("foo", |
| 1178 | params_, |
| 1179 | kDefaultPriority, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1180 | callback.callback(), |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1181 | pool_.get(), |
| 1182 | BoundNetLog())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1183 | |
| 1184 | // Since it is stalled, it should have no connect jobs. |
| 1185 | EXPECT_EQ(0, pool_->NumConnectJobsInGroup("foo")); |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 1186 | EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("foo")); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1187 | |
| 1188 | // Cancel the stalled request. |
| 1189 | handles[0].Reset(); |
| 1190 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1191 | // Now we should have a connect job. |
| 1192 | EXPECT_EQ(1, pool_->NumConnectJobsInGroup("foo")); |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 1193 | EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("foo")); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1194 | |
| 1195 | // The stalled socket should connect. |
| 1196 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 1197 | |
| 1198 | EXPECT_EQ(kDefaultMaxSockets + 1, |
| 1199 | client_socket_factory_.allocation_count()); |
| 1200 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 1201 | EXPECT_EQ(0, pool_->NumConnectJobsInGroup("foo")); |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 1202 | EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("foo")); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1203 | |
| 1204 | // Dropping out of scope will close all handles and return them to idle. |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1205 | } |
| 1206 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1207 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 1208 | } |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1209 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1210 | TEST_F(ClientSocketPoolBaseTest, WaitForStalledSocketAtSocketLimit) { |
| 1211 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1212 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1213 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1214 | ClientSocketHandle stalled_handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1215 | TestCompletionCallback callback; |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1216 | { |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 1217 | EXPECT_FALSE(pool_->IsStalled()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1218 | ClientSocketHandle handles[kDefaultMaxSockets]; |
| 1219 | for (int i = 0; i < kDefaultMaxSockets; ++i) { |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1220 | TestCompletionCallback callback; |
[email protected] | 1870d5cf | 2011-05-12 01:55:40 | [diff] [blame] | 1221 | EXPECT_EQ(OK, handles[i].Init(base::StringPrintf( |
| 1222 | "Take 2: %d", i), |
| 1223 | params_, |
| 1224 | kDefaultPriority, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1225 | callback.callback(), |
[email protected] | 1870d5cf | 2011-05-12 01:55:40 | [diff] [blame] | 1226 | pool_.get(), |
| 1227 | BoundNetLog())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1228 | } |
| 1229 | |
| 1230 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1231 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 1232 | EXPECT_FALSE(pool_->IsStalled()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1233 | |
| 1234 | // Now we will hit the socket limit. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1235 | EXPECT_EQ(ERR_IO_PENDING, stalled_handle.Init("foo", |
| 1236 | params_, |
| 1237 | kDefaultPriority, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1238 | callback.callback(), |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1239 | pool_.get(), |
| 1240 | BoundNetLog())); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 1241 | EXPECT_TRUE(pool_->IsStalled()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1242 | |
| 1243 | // Dropping out of scope will close all handles and return them to idle. |
| 1244 | } |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1245 | |
| 1246 | // But if we wait for it, the released idle sockets will be closed in |
| 1247 | // preference of the waiting request. |
[email protected] | 8ae03f4 | 2010-07-07 19:08:10 | [diff] [blame] | 1248 | EXPECT_EQ(OK, callback.WaitForResult()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1249 | |
| 1250 | EXPECT_EQ(kDefaultMaxSockets + 1, client_socket_factory_.allocation_count()); |
| 1251 | EXPECT_EQ(3, pool_->IdleSocketCount()); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1252 | } |
| 1253 | |
| 1254 | // Regression test for http://crbug.com/40952. |
| 1255 | TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketAtSocketLimitDeleteGroup) { |
| 1256 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 06d9404 | 2010-08-25 01:45:22 | [diff] [blame] | 1257 | pool_->EnableConnectBackupJobs(); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1258 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 1259 | |
| 1260 | for (int i = 0; i < kDefaultMaxSockets; ++i) { |
| 1261 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1262 | TestCompletionCallback callback; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1263 | EXPECT_EQ(OK, handle.Init(base::IntToString(i), |
| 1264 | params_, |
| 1265 | kDefaultPriority, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1266 | callback.callback(), |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1267 | pool_.get(), |
| 1268 | BoundNetLog())); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1269 | } |
| 1270 | |
| 1271 | // Flush all the DoReleaseSocket tasks. |
[email protected] | b4c62eb | 2012-11-14 18:36:51 | [diff] [blame] | 1272 | MessageLoop::current()->RunUntilIdle(); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1273 | |
| 1274 | // Stall a group. Set a pending job so it'll trigger a backup job if we don't |
| 1275 | // reuse a socket. |
| 1276 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1277 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1278 | TestCompletionCallback callback; |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1279 | |
| 1280 | // "0" is special here, since it should be the first entry in the sorted map, |
| 1281 | // which is the one which we would close an idle socket for. We shouldn't |
| 1282 | // close an idle socket though, since we should reuse the idle socket. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1283 | EXPECT_EQ(OK, handle.Init("0", |
| 1284 | params_, |
| 1285 | kDefaultPriority, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1286 | callback.callback(), |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1287 | pool_.get(), |
[email protected] | df4b4ef | 2010-07-12 18:25:21 | [diff] [blame] | 1288 | BoundNetLog())); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1289 | |
| 1290 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1291 | EXPECT_EQ(kDefaultMaxSockets - 1, pool_->IdleSocketCount()); |
| 1292 | } |
| 1293 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1294 | TEST_F(ClientSocketPoolBaseTest, PendingRequests) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1295 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1296 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1297 | EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); |
| 1298 | EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); |
[email protected] | c9c6f5c | 2010-07-31 01:30:03 | [diff] [blame] | 1299 | EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", IDLE)); |
[email protected] | ac790b4 | 2009-12-02 04:31:31 | [diff] [blame] | 1300 | EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOWEST)); |
| 1301 | EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", MEDIUM)); |
| 1302 | EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", HIGHEST)); |
| 1303 | EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOW)); |
| 1304 | EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOWEST)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1305 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1306 | ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1307 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1308 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
| 1309 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1310 | EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup, |
| 1311 | completion_count()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1312 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1313 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1314 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
[email protected] | c9c6f5c | 2010-07-31 01:30:03 | [diff] [blame] | 1315 | EXPECT_EQ(8, GetOrderOfRequest(3)); |
| 1316 | EXPECT_EQ(6, GetOrderOfRequest(4)); |
| 1317 | EXPECT_EQ(4, GetOrderOfRequest(5)); |
| 1318 | EXPECT_EQ(3, GetOrderOfRequest(6)); |
| 1319 | EXPECT_EQ(5, GetOrderOfRequest(7)); |
| 1320 | EXPECT_EQ(7, GetOrderOfRequest(8)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1321 | |
| 1322 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1323 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(9)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1324 | } |
| 1325 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1326 | TEST_F(ClientSocketPoolBaseTest, PendingRequests_NoKeepAlive) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1327 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1328 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1329 | EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); |
| 1330 | EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); |
[email protected] | ac790b4 | 2009-12-02 04:31:31 | [diff] [blame] | 1331 | EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOWEST)); |
| 1332 | EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", MEDIUM)); |
| 1333 | EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", HIGHEST)); |
| 1334 | EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOW)); |
| 1335 | EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOWEST)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1336 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1337 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1338 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1339 | for (size_t i = kDefaultMaxSocketsPerGroup; i < requests_size(); ++i) |
| 1340 | EXPECT_EQ(OK, request(i)->WaitForResult()); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1341 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1342 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1343 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1344 | EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup, |
| 1345 | completion_count()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1346 | } |
| 1347 | |
| 1348 | // This test will start up a RequestSocket() and then immediately Cancel() it. |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1349 | // The pending connect job will be cancelled and should not call back into |
| 1350 | // ClientSocketPoolBase. |
| 1351 | TEST_F(ClientSocketPoolBaseTest, CancelRequestClearGroup) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1352 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1353 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1354 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1355 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1356 | TestCompletionCallback callback; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1357 | EXPECT_EQ(ERR_IO_PENDING, handle.Init("a", |
| 1358 | params_, |
| 1359 | kDefaultPriority, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1360 | callback.callback(), |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1361 | pool_.get(), |
| 1362 | BoundNetLog())); |
| 1363 | handle.Reset(); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1364 | } |
| 1365 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1366 | TEST_F(ClientSocketPoolBaseTest, ConnectCancelConnect) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1367 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1368 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1369 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 1370 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1371 | TestCompletionCallback callback; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1372 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1373 | EXPECT_EQ(ERR_IO_PENDING, handle.Init("a", |
| 1374 | params_, |
| 1375 | kDefaultPriority, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1376 | callback.callback(), |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1377 | pool_.get(), |
| 1378 | BoundNetLog())); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1379 | |
| 1380 | handle.Reset(); |
| 1381 | |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1382 | TestCompletionCallback callback2; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1383 | EXPECT_EQ(ERR_IO_PENDING, |
| 1384 | handle.Init("a", |
| 1385 | params_, |
| 1386 | kDefaultPriority, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1387 | callback2.callback(), |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1388 | pool_.get(), |
| 1389 | BoundNetLog())); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1390 | |
| 1391 | EXPECT_EQ(OK, callback2.WaitForResult()); |
| 1392 | EXPECT_FALSE(callback.have_result()); |
| 1393 | |
| 1394 | handle.Reset(); |
| 1395 | } |
| 1396 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1397 | TEST_F(ClientSocketPoolBaseTest, CancelRequest) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1398 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1399 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1400 | EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); |
| 1401 | EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); |
[email protected] | ac790b4 | 2009-12-02 04:31:31 | [diff] [blame] | 1402 | EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOWEST)); |
| 1403 | EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", MEDIUM)); |
| 1404 | EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", HIGHEST)); |
| 1405 | EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOW)); |
| 1406 | EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOWEST)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1407 | |
| 1408 | // Cancel a request. |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1409 | size_t index_to_cancel = kDefaultMaxSocketsPerGroup + 2; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1410 | EXPECT_FALSE((*requests())[index_to_cancel]->handle()->is_initialized()); |
| 1411 | (*requests())[index_to_cancel]->handle()->Reset(); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1412 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1413 | ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1414 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1415 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
| 1416 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1417 | EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup - 1, |
| 1418 | completion_count()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1419 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1420 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1421 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1422 | EXPECT_EQ(5, GetOrderOfRequest(3)); |
| 1423 | EXPECT_EQ(3, GetOrderOfRequest(4)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1424 | EXPECT_EQ(ClientSocketPoolTest::kRequestNotFound, |
| 1425 | GetOrderOfRequest(5)); // Canceled request. |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1426 | EXPECT_EQ(4, GetOrderOfRequest(6)); |
| 1427 | EXPECT_EQ(6, GetOrderOfRequest(7)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1428 | |
| 1429 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1430 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(8)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1431 | } |
| 1432 | |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1433 | class RequestSocketCallback : public TestCompletionCallbackBase { |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1434 | public: |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1435 | RequestSocketCallback(ClientSocketHandle* handle, |
[email protected] | a937a06d | 2009-08-19 21:19:24 | [diff] [blame] | 1436 | TestClientSocketPool* pool, |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1437 | TestConnectJobFactory* test_connect_job_factory, |
| 1438 | TestConnectJob::JobType next_job_type) |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1439 | : handle_(handle), |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 1440 | pool_(pool), |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1441 | within_callback_(false), |
| 1442 | test_connect_job_factory_(test_connect_job_factory), |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1443 | next_job_type_(next_job_type), |
[email protected] | aa249b5 | 2013-04-30 01:04:32 | [diff] [blame] | 1444 | callback_(base::Bind(&RequestSocketCallback::OnComplete, |
| 1445 | base::Unretained(this))) { |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1446 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1447 | |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1448 | virtual ~RequestSocketCallback() {} |
| 1449 | |
| 1450 | const CompletionCallback& callback() const { return callback_; } |
| 1451 | |
| 1452 | private: |
| 1453 | void OnComplete(int result) { |
| 1454 | SetResult(result); |
| 1455 | ASSERT_EQ(OK, result); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1456 | |
| 1457 | if (!within_callback_) { |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1458 | test_connect_job_factory_->set_job_type(next_job_type_); |
[email protected] | 5edbf8d | 2010-01-13 18:44:11 | [diff] [blame] | 1459 | |
| 1460 | // Don't allow reuse of the socket. Disconnect it and then release it and |
| 1461 | // run through the MessageLoop once to get it completely released. |
| 1462 | handle_->socket()->Disconnect(); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1463 | handle_->Reset(); |
[email protected] | 5edbf8d | 2010-01-13 18:44:11 | [diff] [blame] | 1464 | { |
[email protected] | b5717a4 | 2012-02-14 19:33:52 | [diff] [blame] | 1465 | // TODO: Resolve conflicting intentions of stopping recursion with the |
[email protected] | b4c62eb | 2012-11-14 18:36:51 | [diff] [blame] | 1466 | // |!within_callback_| test (above) and the call to |RunUntilIdle()| |
[email protected] | b5717a4 | 2012-02-14 19:33:52 | [diff] [blame] | 1467 | // below. http://crbug.com/114130. |
| 1468 | MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current()); |
[email protected] | b4c62eb | 2012-11-14 18:36:51 | [diff] [blame] | 1469 | MessageLoop::current()->RunUntilIdle(); |
[email protected] | 5edbf8d | 2010-01-13 18:44:11 | [diff] [blame] | 1470 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1471 | within_callback_ = true; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1472 | TestCompletionCallback next_job_callback; |
[email protected] | ad8e04a | 2010-11-01 04:16:27 | [diff] [blame] | 1473 | scoped_refptr<TestSocketParams> params(new TestSocketParams()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1474 | int rv = handle_->Init("a", |
| 1475 | params, |
| 1476 | kDefaultPriority, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1477 | next_job_callback.callback(), |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1478 | pool_, |
| 1479 | BoundNetLog()); |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1480 | switch (next_job_type_) { |
| 1481 | case TestConnectJob::kMockJob: |
| 1482 | EXPECT_EQ(OK, rv); |
| 1483 | break; |
| 1484 | case TestConnectJob::kMockPendingJob: |
| 1485 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 6b17538 | 2009-10-13 06:47:47 | [diff] [blame] | 1486 | |
| 1487 | // For pending jobs, wait for new socket to be created. This makes |
| 1488 | // sure there are no more pending operations nor any unclosed sockets |
| 1489 | // when the test finishes. |
| 1490 | // We need to give it a little bit of time to run, so that all the |
| 1491 | // operations that happen on timers (e.g. cleanup of idle |
| 1492 | // connections) can execute. |
[email protected] | 5edbf8d | 2010-01-13 18:44:11 | [diff] [blame] | 1493 | { |
[email protected] | b5717a4 | 2012-02-14 19:33:52 | [diff] [blame] | 1494 | MessageLoop::ScopedNestableTaskAllower allow( |
[email protected] | 5edbf8d | 2010-01-13 18:44:11 | [diff] [blame] | 1495 | MessageLoop::current()); |
[email protected] | 26b997396 | 2012-01-28 00:57:00 | [diff] [blame] | 1496 | base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10)); |
[email protected] | 5edbf8d | 2010-01-13 18:44:11 | [diff] [blame] | 1497 | EXPECT_EQ(OK, next_job_callback.WaitForResult()); |
| 1498 | } |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1499 | break; |
| 1500 | default: |
| 1501 | FAIL() << "Unexpected job type: " << next_job_type_; |
| 1502 | break; |
| 1503 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1504 | } |
| 1505 | } |
| 1506 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1507 | ClientSocketHandle* const handle_; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1508 | TestClientSocketPool* const pool_; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1509 | bool within_callback_; |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1510 | TestConnectJobFactory* const test_connect_job_factory_; |
| 1511 | TestConnectJob::JobType next_job_type_; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1512 | CompletionCallback callback_; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1513 | }; |
| 1514 | |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1515 | TEST_F(ClientSocketPoolBaseTest, RequestPendingJobTwice) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1516 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1517 | |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 1518 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 1519 | ClientSocketHandle handle; |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1520 | RequestSocketCallback callback( |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 1521 | &handle, pool_.get(), connect_job_factory_, |
| 1522 | TestConnectJob::kMockPendingJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1523 | int rv = handle.Init("a", |
| 1524 | params_, |
| 1525 | kDefaultPriority, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1526 | callback.callback(), |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1527 | pool_.get(), |
[email protected] | df4b4ef | 2010-07-12 18:25:21 | [diff] [blame] | 1528 | BoundNetLog()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1529 | ASSERT_EQ(ERR_IO_PENDING, rv); |
| 1530 | |
| 1531 | EXPECT_EQ(OK, callback.WaitForResult()); |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1532 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1533 | |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1534 | TEST_F(ClientSocketPoolBaseTest, RequestPendingJobThenSynchronous) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1535 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1536 | |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 1537 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 1538 | ClientSocketHandle handle; |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1539 | RequestSocketCallback callback( |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 1540 | &handle, pool_.get(), connect_job_factory_, TestConnectJob::kMockJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1541 | int rv = handle.Init("a", |
| 1542 | params_, |
| 1543 | kDefaultPriority, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1544 | callback.callback(), |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1545 | pool_.get(), |
[email protected] | df4b4ef | 2010-07-12 18:25:21 | [diff] [blame] | 1546 | BoundNetLog()); |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1547 | ASSERT_EQ(ERR_IO_PENDING, rv); |
| 1548 | |
| 1549 | EXPECT_EQ(OK, callback.WaitForResult()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1550 | } |
| 1551 | |
| 1552 | // Make sure that pending requests get serviced after active requests get |
| 1553 | // cancelled. |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1554 | TEST_F(ClientSocketPoolBaseTest, CancelActiveRequestWithPendingRequests) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1555 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1556 | |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 1557 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1558 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1559 | EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); |
| 1560 | EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); |
| 1561 | EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); |
| 1562 | EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); |
| 1563 | EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); |
| 1564 | EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); |
| 1565 | EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1566 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1567 | // Now, kDefaultMaxSocketsPerGroup requests should be active. |
| 1568 | // Let's cancel them. |
| 1569 | for (int i = 0; i < kDefaultMaxSocketsPerGroup; ++i) { |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1570 | ASSERT_FALSE(request(i)->handle()->is_initialized()); |
| 1571 | request(i)->handle()->Reset(); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1572 | } |
| 1573 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1574 | // Let's wait for the rest to complete now. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1575 | for (size_t i = kDefaultMaxSocketsPerGroup; i < requests_size(); ++i) { |
| 1576 | EXPECT_EQ(OK, request(i)->WaitForResult()); |
| 1577 | request(i)->handle()->Reset(); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1578 | } |
| 1579 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1580 | EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup, |
| 1581 | completion_count()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1582 | } |
| 1583 | |
| 1584 | // Make sure that pending requests get serviced after active requests fail. |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1585 | TEST_F(ClientSocketPoolBaseTest, FailingActiveRequestWithPendingRequests) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1586 | const size_t kMaxSockets = 5; |
| 1587 | CreatePool(kMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1588 | |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 1589 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1590 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1591 | const size_t kNumberOfRequests = 2 * kDefaultMaxSocketsPerGroup + 1; |
| 1592 | ASSERT_LE(kNumberOfRequests, kMaxSockets); // Otherwise the test will hang. |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1593 | |
| 1594 | // Queue up all the requests |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1595 | for (size_t i = 0; i < kNumberOfRequests; ++i) |
| 1596 | EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1597 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1598 | for (size_t i = 0; i < kNumberOfRequests; ++i) |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1599 | EXPECT_EQ(ERR_CONNECTION_FAILED, request(i)->WaitForResult()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1600 | } |
| 1601 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1602 | TEST_F(ClientSocketPoolBaseTest, CancelActiveRequestThenRequestSocket) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1603 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1604 | |
| 1605 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1606 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1607 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1608 | TestCompletionCallback callback; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1609 | int rv = handle.Init("a", |
| 1610 | params_, |
| 1611 | kDefaultPriority, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1612 | callback.callback(), |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1613 | pool_.get(), |
| 1614 | BoundNetLog()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1615 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1616 | |
| 1617 | // Cancel the active request. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1618 | handle.Reset(); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1619 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1620 | rv = handle.Init("a", |
| 1621 | params_, |
| 1622 | kDefaultPriority, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1623 | callback.callback(), |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1624 | pool_.get(), |
| 1625 | BoundNetLog()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1626 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1627 | EXPECT_EQ(OK, callback.WaitForResult()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1628 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1629 | EXPECT_FALSE(handle.is_reused()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 1630 | TestLoadTimingInfoConnectedNotReused(handle); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1631 | EXPECT_EQ(2, client_socket_factory_.allocation_count()); |
| 1632 | } |
| 1633 | |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1634 | // Regression test for http://crbug.com/17985. |
| 1635 | TEST_F(ClientSocketPoolBaseTest, GroupWithPendingRequestsIsNotEmpty) { |
| 1636 | const int kMaxSockets = 3; |
| 1637 | const int kMaxSocketsPerGroup = 2; |
| 1638 | CreatePool(kMaxSockets, kMaxSocketsPerGroup); |
| 1639 | |
[email protected] | ac790b4 | 2009-12-02 04:31:31 | [diff] [blame] | 1640 | const RequestPriority kHighPriority = HIGHEST; |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1641 | |
| 1642 | EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); |
| 1643 | EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); |
| 1644 | |
| 1645 | // This is going to be a pending request in an otherwise empty group. |
| 1646 | EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); |
| 1647 | |
| 1648 | // Reach the maximum socket limit. |
| 1649 | EXPECT_EQ(OK, StartRequest("b", kDefaultPriority)); |
| 1650 | |
| 1651 | // Create a stalled group with high priorities. |
| 1652 | EXPECT_EQ(ERR_IO_PENDING, StartRequest("c", kHighPriority)); |
| 1653 | EXPECT_EQ(ERR_IO_PENDING, StartRequest("c", kHighPriority)); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1654 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1655 | // Release the first two sockets from "a". Because this is a keepalive, |
| 1656 | // the first release will unblock the pending request for "a". The |
| 1657 | // second release will unblock a request for "c", becaue it is the next |
| 1658 | // high priority socket. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1659 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
| 1660 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1661 | |
| 1662 | // Closing idle sockets should not get us into trouble, but in the bug |
| 1663 | // we were hitting a CHECK here. |
[email protected] | 93054cc1 | 2010-06-08 06:12:41 | [diff] [blame] | 1664 | EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a")); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1665 | pool_->CloseIdleSockets(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1666 | |
[email protected] | b4c62eb | 2012-11-14 18:36:51 | [diff] [blame] | 1667 | MessageLoop::current()->RunUntilIdle(); // Run the released socket wakeups |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1668 | } |
| 1669 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 1670 | TEST_F(ClientSocketPoolBaseTest, BasicAsynchronous) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1671 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1672 | |
| 1673 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1674 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1675 | TestCompletionCallback callback; |
[email protected] | 333bdf6 | 2012-06-08 22:57:29 | [diff] [blame] | 1676 | CapturingBoundNetLog log; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1677 | int rv = handle.Init("a", |
| 1678 | params_, |
| 1679 | LOWEST, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1680 | callback.callback(), |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1681 | pool_.get(), |
| 1682 | log.bound()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1683 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1684 | EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", &handle)); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 1685 | TestLoadTimingInfoNotConnected(handle); |
| 1686 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1687 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 1688 | EXPECT_TRUE(handle.is_initialized()); |
| 1689 | EXPECT_TRUE(handle.socket()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 1690 | TestLoadTimingInfoConnectedNotReused(handle); |
| 1691 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1692 | handle.Reset(); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 1693 | TestLoadTimingInfoNotConnected(handle); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 1694 | |
[email protected] | 333bdf6 | 2012-06-08 22:57:29 | [diff] [blame] | 1695 | CapturingNetLog::CapturedEntryList entries; |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 1696 | log.GetEntries(&entries); |
| 1697 | |
| 1698 | EXPECT_EQ(4u, entries.size()); |
[email protected] | e9002a9 | 2010-01-29 07:10:46 | [diff] [blame] | 1699 | EXPECT_TRUE(LogContainsBeginEvent( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 1700 | entries, 0, NetLog::TYPE_SOCKET_POOL)); |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 1701 | EXPECT_TRUE(LogContainsEvent( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 1702 | entries, 1, NetLog::TYPE_SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 1703 | NetLog::PHASE_NONE)); |
| 1704 | EXPECT_TRUE(LogContainsEvent( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 1705 | entries, 2, NetLog::TYPE_SOCKET_POOL_BOUND_TO_SOCKET, |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 1706 | NetLog::PHASE_NONE)); |
[email protected] | e9002a9 | 2010-01-29 07:10:46 | [diff] [blame] | 1707 | EXPECT_TRUE(LogContainsEndEvent( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 1708 | entries, 3, NetLog::TYPE_SOCKET_POOL)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1709 | } |
| 1710 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 1711 | TEST_F(ClientSocketPoolBaseTest, |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1712 | InitConnectionAsynchronousFailure) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1713 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1714 | |
| 1715 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1716 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1717 | TestCompletionCallback callback; |
[email protected] | 333bdf6 | 2012-06-08 22:57:29 | [diff] [blame] | 1718 | CapturingBoundNetLog log; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 1719 | // Set the additional error state members to ensure that they get cleared. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1720 | handle.set_is_ssl_error(true); |
[email protected] | 8b49869 | 2010-07-16 17:11:43 | [diff] [blame] | 1721 | HttpResponseInfo info; |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 1722 | info.headers = new HttpResponseHeaders(std::string()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1723 | handle.set_ssl_error_response_info(info); |
| 1724 | EXPECT_EQ(ERR_IO_PENDING, handle.Init("a", |
| 1725 | params_, |
| 1726 | kDefaultPriority, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1727 | callback.callback(), |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1728 | pool_.get(), |
| 1729 | log.bound())); |
| 1730 | EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", &handle)); |
| 1731 | EXPECT_EQ(ERR_CONNECTION_FAILED, callback.WaitForResult()); |
| 1732 | EXPECT_FALSE(handle.is_ssl_error()); |
| 1733 | EXPECT_TRUE(handle.ssl_error_response_info().headers.get() == NULL); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 1734 | |
[email protected] | 333bdf6 | 2012-06-08 22:57:29 | [diff] [blame] | 1735 | CapturingNetLog::CapturedEntryList entries; |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 1736 | log.GetEntries(&entries); |
| 1737 | |
| 1738 | EXPECT_EQ(3u, entries.size()); |
[email protected] | e9002a9 | 2010-01-29 07:10:46 | [diff] [blame] | 1739 | EXPECT_TRUE(LogContainsBeginEvent( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 1740 | entries, 0, NetLog::TYPE_SOCKET_POOL)); |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 1741 | EXPECT_TRUE(LogContainsEvent( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 1742 | entries, 1, NetLog::TYPE_SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 1743 | NetLog::PHASE_NONE)); |
[email protected] | 6b624c6 | 2010-03-14 08:37:32 | [diff] [blame] | 1744 | EXPECT_TRUE(LogContainsEndEvent( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 1745 | entries, 2, NetLog::TYPE_SOCKET_POOL)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1746 | } |
| 1747 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 1748 | TEST_F(ClientSocketPoolBaseTest, TwoRequestsCancelOne) { |
[email protected] | b22b516 | 2010-03-16 07:53:10 | [diff] [blame] | 1749 | // TODO(eroman): Add back the log expectations! Removed them because the |
| 1750 | // ordering is difficult, and some may fire during destructor. |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1751 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1752 | |
| 1753 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1754 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1755 | TestCompletionCallback callback; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1756 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1757 | TestCompletionCallback callback2; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1758 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1759 | EXPECT_EQ(ERR_IO_PENDING, |
| 1760 | handle.Init("a", |
| 1761 | params_, |
| 1762 | kDefaultPriority, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1763 | callback.callback(), |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1764 | pool_.get(), |
| 1765 | BoundNetLog())); |
[email protected] | 333bdf6 | 2012-06-08 22:57:29 | [diff] [blame] | 1766 | CapturingBoundNetLog log2; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1767 | EXPECT_EQ(ERR_IO_PENDING, |
| 1768 | handle2.Init("a", |
| 1769 | params_, |
| 1770 | kDefaultPriority, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1771 | callback2.callback(), |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1772 | pool_.get(), |
| 1773 | BoundNetLog())); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1774 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1775 | handle.Reset(); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1776 | |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 1777 | |
| 1778 | // At this point, request 2 is just waiting for the connect job to finish. |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 1779 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1780 | EXPECT_EQ(OK, callback2.WaitForResult()); |
| 1781 | handle2.Reset(); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 1782 | |
| 1783 | // Now request 2 has actually finished. |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 1784 | // TODO(eroman): Add back log expectations. |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1785 | } |
| 1786 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 1787 | TEST_F(ClientSocketPoolBaseTest, CancelRequestLimitsJobs) { |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 1788 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1789 | |
[email protected] | 17a0c6c | 2009-08-04 00:07:04 | [diff] [blame] | 1790 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1791 | |
[email protected] | ac790b4 | 2009-12-02 04:31:31 | [diff] [blame] | 1792 | EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOWEST)); |
| 1793 | EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOW)); |
| 1794 | EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", MEDIUM)); |
| 1795 | EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", HIGHEST)); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 1796 | |
| 1797 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, pool_->NumConnectJobsInGroup("a")); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1798 | (*requests())[2]->handle()->Reset(); |
| 1799 | (*requests())[3]->handle()->Reset(); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 1800 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, pool_->NumConnectJobsInGroup("a")); |
| 1801 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1802 | (*requests())[1]->handle()->Reset(); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 1803 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, pool_->NumConnectJobsInGroup("a")); |
| 1804 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1805 | (*requests())[0]->handle()->Reset(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1806 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, pool_->NumConnectJobsInGroup("a")); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 1807 | } |
| 1808 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1809 | // When requests and ConnectJobs are not coupled, the request will get serviced |
| 1810 | // by whatever comes first. |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 1811 | TEST_F(ClientSocketPoolBaseTest, ReleaseSockets) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1812 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1813 | |
| 1814 | // Start job 1 (async OK) |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 1815 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1816 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1817 | std::vector<TestSocketRequest*> request_order; |
| 1818 | size_t completion_count; // unused |
| 1819 | TestSocketRequest req1(&request_order, &completion_count); |
| 1820 | int rv = req1.handle()->Init("a", |
| 1821 | params_, |
| 1822 | kDefaultPriority, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1823 | req1.callback(), pool_.get(), |
[email protected] | df4b4ef | 2010-07-12 18:25:21 | [diff] [blame] | 1824 | BoundNetLog()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1825 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1826 | EXPECT_EQ(OK, req1.WaitForResult()); |
| 1827 | |
| 1828 | // Job 1 finished OK. Start job 2 (also async OK). Request 3 is pending |
| 1829 | // without a job. |
| 1830 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 1831 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1832 | TestSocketRequest req2(&request_order, &completion_count); |
| 1833 | rv = req2.handle()->Init("a", |
| 1834 | params_, |
| 1835 | kDefaultPriority, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1836 | req2.callback(), |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1837 | pool_.get(), |
[email protected] | df4b4ef | 2010-07-12 18:25:21 | [diff] [blame] | 1838 | BoundNetLog()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1839 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1840 | TestSocketRequest req3(&request_order, &completion_count); |
| 1841 | rv = req3.handle()->Init("a", |
| 1842 | params_, |
| 1843 | kDefaultPriority, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1844 | req3.callback(), |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1845 | pool_.get(), |
[email protected] | df4b4ef | 2010-07-12 18:25:21 | [diff] [blame] | 1846 | BoundNetLog()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1847 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1848 | |
| 1849 | // Both Requests 2 and 3 are pending. We release socket 1 which should |
| 1850 | // service request 2. Request 3 should still be waiting. |
[email protected] | a6c59f6 | 2009-07-29 16:33:33 | [diff] [blame] | 1851 | req1.handle()->Reset(); |
[email protected] | b4c62eb | 2012-11-14 18:36:51 | [diff] [blame] | 1852 | MessageLoop::current()->RunUntilIdle(); // Run the released socket wakeups |
[email protected] | a6c59f6 | 2009-07-29 16:33:33 | [diff] [blame] | 1853 | ASSERT_TRUE(req2.handle()->socket()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1854 | EXPECT_EQ(OK, req2.WaitForResult()); |
[email protected] | a6c59f6 | 2009-07-29 16:33:33 | [diff] [blame] | 1855 | EXPECT_FALSE(req3.handle()->socket()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1856 | |
| 1857 | // Signal job 2, which should service request 3. |
| 1858 | |
| 1859 | client_socket_factory_.SignalJobs(); |
| 1860 | EXPECT_EQ(OK, req3.WaitForResult()); |
| 1861 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1862 | ASSERT_EQ(3U, request_order.size()); |
| 1863 | EXPECT_EQ(&req1, request_order[0]); |
| 1864 | EXPECT_EQ(&req2, request_order[1]); |
| 1865 | EXPECT_EQ(&req3, request_order[2]); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1866 | EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a")); |
| 1867 | } |
| 1868 | |
| 1869 | // The requests are not coupled to the jobs. So, the requests should finish in |
| 1870 | // their priority / insertion order. |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 1871 | TEST_F(ClientSocketPoolBaseTest, PendingJobCompletionOrder) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1872 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1873 | // First two jobs are async. |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 1874 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1875 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1876 | std::vector<TestSocketRequest*> request_order; |
| 1877 | size_t completion_count; // unused |
| 1878 | TestSocketRequest req1(&request_order, &completion_count); |
| 1879 | int rv = req1.handle()->Init("a", |
| 1880 | params_, |
| 1881 | kDefaultPriority, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1882 | req1.callback(), |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1883 | pool_.get(), |
[email protected] | df4b4ef | 2010-07-12 18:25:21 | [diff] [blame] | 1884 | BoundNetLog()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1885 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1886 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1887 | TestSocketRequest req2(&request_order, &completion_count); |
| 1888 | rv = req2.handle()->Init("a", |
| 1889 | params_, |
| 1890 | kDefaultPriority, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1891 | req2.callback(), |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1892 | pool_.get(), |
[email protected] | df4b4ef | 2010-07-12 18:25:21 | [diff] [blame] | 1893 | BoundNetLog()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1894 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1895 | |
| 1896 | // The pending job is sync. |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 1897 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1898 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1899 | TestSocketRequest req3(&request_order, &completion_count); |
| 1900 | rv = req3.handle()->Init("a", |
| 1901 | params_, |
| 1902 | kDefaultPriority, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1903 | req3.callback(), |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1904 | pool_.get(), |
[email protected] | df4b4ef | 2010-07-12 18:25:21 | [diff] [blame] | 1905 | BoundNetLog()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1906 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1907 | |
| 1908 | EXPECT_EQ(ERR_CONNECTION_FAILED, req1.WaitForResult()); |
| 1909 | EXPECT_EQ(OK, req2.WaitForResult()); |
| 1910 | EXPECT_EQ(ERR_CONNECTION_FAILED, req3.WaitForResult()); |
| 1911 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1912 | ASSERT_EQ(3U, request_order.size()); |
| 1913 | EXPECT_EQ(&req1, request_order[0]); |
| 1914 | EXPECT_EQ(&req2, request_order[1]); |
| 1915 | EXPECT_EQ(&req3, request_order[2]); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1916 | } |
| 1917 | |
[email protected] | e6ec67b | 2010-06-16 00:12:46 | [diff] [blame] | 1918 | TEST_F(ClientSocketPoolBaseTest, LoadState) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1919 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1920 | connect_job_factory_->set_job_type( |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 1921 | TestConnectJob::kMockAdvancingLoadStateJob); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1922 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1923 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1924 | TestCompletionCallback callback; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1925 | int rv = handle.Init("a", |
| 1926 | params_, |
| 1927 | kDefaultPriority, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1928 | callback.callback(), |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1929 | pool_.get(), |
| 1930 | BoundNetLog()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1931 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1932 | EXPECT_EQ(LOAD_STATE_IDLE, handle.GetLoadState()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1933 | |
[email protected] | b4c62eb | 2012-11-14 18:36:51 | [diff] [blame] | 1934 | MessageLoop::current()->RunUntilIdle(); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1935 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1936 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1937 | TestCompletionCallback callback2; |
| 1938 | rv = handle2.Init("a", params_, kDefaultPriority, callback2.callback(), |
| 1939 | pool_.get(), BoundNetLog()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1940 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1941 | EXPECT_NE(LOAD_STATE_IDLE, handle.GetLoadState()); |
| 1942 | EXPECT_NE(LOAD_STATE_IDLE, handle2.GetLoadState()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1943 | } |
| 1944 | |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 1945 | TEST_F(ClientSocketPoolBaseTest, Recoverable) { |
| 1946 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1947 | connect_job_factory_->set_job_type(TestConnectJob::kMockRecoverableJob); |
| 1948 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1949 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1950 | TestCompletionCallback callback; |
| 1951 | EXPECT_EQ(ERR_PROXY_AUTH_REQUESTED, |
| 1952 | handle.Init("a", params_, kDefaultPriority, callback.callback(), |
| 1953 | pool_.get(), BoundNetLog())); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1954 | EXPECT_TRUE(handle.is_initialized()); |
| 1955 | EXPECT_TRUE(handle.socket()); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 1956 | } |
| 1957 | |
| 1958 | TEST_F(ClientSocketPoolBaseTest, AsyncRecoverable) { |
| 1959 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1960 | |
| 1961 | connect_job_factory_->set_job_type( |
| 1962 | TestConnectJob::kMockPendingRecoverableJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1963 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1964 | TestCompletionCallback callback; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1965 | EXPECT_EQ(ERR_IO_PENDING, |
| 1966 | handle.Init("a", |
| 1967 | params_, |
| 1968 | kDefaultPriority, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1969 | callback.callback(), |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1970 | pool_.get(), |
| 1971 | BoundNetLog())); |
| 1972 | EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", &handle)); |
| 1973 | EXPECT_EQ(ERR_PROXY_AUTH_REQUESTED, callback.WaitForResult()); |
| 1974 | EXPECT_TRUE(handle.is_initialized()); |
| 1975 | EXPECT_TRUE(handle.socket()); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 1976 | } |
| 1977 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 1978 | TEST_F(ClientSocketPoolBaseTest, AdditionalErrorStateSynchronous) { |
| 1979 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1980 | connect_job_factory_->set_job_type( |
| 1981 | TestConnectJob::kMockAdditionalErrorStateJob); |
| 1982 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1983 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1984 | TestCompletionCallback callback; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1985 | EXPECT_EQ(ERR_CONNECTION_FAILED, |
| 1986 | handle.Init("a", |
| 1987 | params_, |
| 1988 | kDefaultPriority, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1989 | callback.callback(), |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1990 | pool_.get(), |
| 1991 | BoundNetLog())); |
| 1992 | EXPECT_FALSE(handle.is_initialized()); |
| 1993 | EXPECT_FALSE(handle.socket()); |
| 1994 | EXPECT_TRUE(handle.is_ssl_error()); |
| 1995 | EXPECT_FALSE(handle.ssl_error_response_info().headers.get() == NULL); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 1996 | } |
| 1997 | |
| 1998 | TEST_F(ClientSocketPoolBaseTest, AdditionalErrorStateAsynchronous) { |
| 1999 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2000 | |
| 2001 | connect_job_factory_->set_job_type( |
| 2002 | TestConnectJob::kMockPendingAdditionalErrorStateJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2003 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2004 | TestCompletionCallback callback; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2005 | EXPECT_EQ(ERR_IO_PENDING, |
| 2006 | handle.Init("a", |
| 2007 | params_, |
| 2008 | kDefaultPriority, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2009 | callback.callback(), |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2010 | pool_.get(), |
| 2011 | BoundNetLog())); |
| 2012 | EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", &handle)); |
| 2013 | EXPECT_EQ(ERR_CONNECTION_FAILED, callback.WaitForResult()); |
| 2014 | EXPECT_FALSE(handle.is_initialized()); |
| 2015 | EXPECT_FALSE(handle.socket()); |
| 2016 | EXPECT_TRUE(handle.is_ssl_error()); |
| 2017 | EXPECT_FALSE(handle.ssl_error_response_info().headers.get() == NULL); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2018 | } |
| 2019 | |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2020 | // Make sure we can reuse sockets when the cleanup timer is disabled. |
| 2021 | TEST_F(ClientSocketPoolBaseTest, DisableCleanupTimerReuse) { |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2022 | // Disable cleanup timer. |
| 2023 | internal::ClientSocketPoolBaseHelper::set_cleanup_timer_enabled(false); |
| 2024 | |
| 2025 | CreatePoolWithIdleTimeouts( |
| 2026 | kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2027 | base::TimeDelta(), // Time out unused sockets immediately. |
| 2028 | base::TimeDelta::FromDays(1)); // Don't time out used sockets. |
| 2029 | |
| 2030 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2031 | |
| 2032 | ClientSocketHandle handle; |
| 2033 | TestCompletionCallback callback; |
| 2034 | int rv = handle.Init("a", |
| 2035 | params_, |
| 2036 | LOWEST, |
| 2037 | callback.callback(), |
| 2038 | pool_.get(), |
| 2039 | BoundNetLog()); |
| 2040 | ASSERT_EQ(ERR_IO_PENDING, rv); |
| 2041 | EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", &handle)); |
| 2042 | ASSERT_EQ(OK, callback.WaitForResult()); |
| 2043 | |
| 2044 | // Use and release the socket. |
| 2045 | EXPECT_EQ(1, handle.socket()->Write(NULL, 1, CompletionCallback())); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 2046 | TestLoadTimingInfoConnectedNotReused(handle); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2047 | handle.Reset(); |
| 2048 | |
| 2049 | // Should now have one idle socket. |
| 2050 | ASSERT_EQ(1, pool_->IdleSocketCount()); |
| 2051 | |
| 2052 | // Request a new socket. This should reuse the old socket and complete |
| 2053 | // synchronously. |
[email protected] | 333bdf6 | 2012-06-08 22:57:29 | [diff] [blame] | 2054 | CapturingBoundNetLog log; |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2055 | rv = handle.Init("a", |
| 2056 | params_, |
| 2057 | LOWEST, |
| 2058 | CompletionCallback(), |
| 2059 | pool_.get(), |
| 2060 | log.bound()); |
| 2061 | ASSERT_EQ(OK, rv); |
| 2062 | EXPECT_TRUE(handle.is_reused()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 2063 | TestLoadTimingInfoConnectedReused(handle); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2064 | |
| 2065 | ASSERT_TRUE(pool_->HasGroup("a")); |
| 2066 | EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a")); |
| 2067 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroup("a")); |
| 2068 | |
[email protected] | 333bdf6 | 2012-06-08 22:57:29 | [diff] [blame] | 2069 | CapturingNetLog::CapturedEntryList entries; |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2070 | log.GetEntries(&entries); |
| 2071 | EXPECT_TRUE(LogContainsEntryWithType( |
| 2072 | entries, 1, NetLog::TYPE_SOCKET_POOL_REUSED_AN_EXISTING_SOCKET)); |
| 2073 | } |
| 2074 | |
| 2075 | // Make sure we cleanup old unused sockets when the cleanup timer is disabled. |
| 2076 | TEST_F(ClientSocketPoolBaseTest, DisableCleanupTimerNoReuse) { |
| 2077 | // Disable cleanup timer. |
| 2078 | internal::ClientSocketPoolBaseHelper::set_cleanup_timer_enabled(false); |
| 2079 | |
| 2080 | CreatePoolWithIdleTimeouts( |
| 2081 | kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 2082 | base::TimeDelta(), // Time out unused sockets immediately |
| 2083 | base::TimeDelta()); // Time out used sockets immediately |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2084 | |
| 2085 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2086 | |
| 2087 | // Startup two mock pending connect jobs, which will sit in the MessageLoop. |
| 2088 | |
| 2089 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2090 | TestCompletionCallback callback; |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2091 | int rv = handle.Init("a", |
| 2092 | params_, |
| 2093 | LOWEST, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2094 | callback.callback(), |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2095 | pool_.get(), |
| 2096 | BoundNetLog()); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2097 | ASSERT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2098 | EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", &handle)); |
| 2099 | |
| 2100 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2101 | TestCompletionCallback callback2; |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2102 | rv = handle2.Init("a", |
| 2103 | params_, |
| 2104 | LOWEST, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2105 | callback2.callback(), |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2106 | pool_.get(), |
| 2107 | BoundNetLog()); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2108 | ASSERT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2109 | EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", &handle2)); |
| 2110 | |
| 2111 | // Cancel one of the requests. Wait for the other, which will get the first |
| 2112 | // job. Release the socket. Run the loop again to make sure the second |
| 2113 | // socket is sitting idle and the first one is released (since ReleaseSocket() |
| 2114 | // just posts a DoReleaseSocket() task). |
| 2115 | |
| 2116 | handle.Reset(); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2117 | ASSERT_EQ(OK, callback2.WaitForResult()); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2118 | // Use the socket. |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 2119 | EXPECT_EQ(1, handle2.socket()->Write(NULL, 1, CompletionCallback())); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2120 | handle2.Reset(); |
| 2121 | |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2122 | // We post all of our delayed tasks with a 2ms delay. I.e. they don't |
| 2123 | // actually become pending until 2ms after they have been created. In order |
| 2124 | // to flush all tasks, we need to wait so that we know there are no |
| 2125 | // soon-to-be-pending tasks waiting. |
| 2126 | base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10)); |
[email protected] | b4c62eb | 2012-11-14 18:36:51 | [diff] [blame] | 2127 | MessageLoop::current()->RunUntilIdle(); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2128 | |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2129 | // Both sockets should now be idle. |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2130 | ASSERT_EQ(2, pool_->IdleSocketCount()); |
| 2131 | |
| 2132 | // Request a new socket. This should cleanup the unused and timed out ones. |
| 2133 | // A new socket will be created rather than reusing the idle one. |
[email protected] | 333bdf6 | 2012-06-08 22:57:29 | [diff] [blame] | 2134 | CapturingBoundNetLog log; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2135 | TestCompletionCallback callback3; |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2136 | rv = handle.Init("a", |
| 2137 | params_, |
| 2138 | LOWEST, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2139 | callback3.callback(), |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2140 | pool_.get(), |
| 2141 | log.bound()); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2142 | ASSERT_EQ(ERR_IO_PENDING, rv); |
| 2143 | ASSERT_EQ(OK, callback3.WaitForResult()); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2144 | EXPECT_FALSE(handle.is_reused()); |
| 2145 | |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2146 | // Make sure the idle socket is closed. |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2147 | ASSERT_TRUE(pool_->HasGroup("a")); |
| 2148 | EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a")); |
| 2149 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroup("a")); |
| 2150 | |
[email protected] | 333bdf6 | 2012-06-08 22:57:29 | [diff] [blame] | 2151 | CapturingNetLog::CapturedEntryList entries; |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2152 | log.GetEntries(&entries); |
| 2153 | EXPECT_FALSE(LogContainsEntryWithType( |
| 2154 | entries, 1, NetLog::TYPE_SOCKET_POOL_REUSED_AN_EXISTING_SOCKET)); |
| 2155 | } |
| 2156 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2157 | TEST_F(ClientSocketPoolBaseTest, CleanupTimedOutIdleSockets) { |
[email protected] | 9bf28db | 2009-08-29 01:35:16 | [diff] [blame] | 2158 | CreatePoolWithIdleTimeouts( |
| 2159 | kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 2160 | base::TimeDelta(), // Time out unused sockets immediately. |
| 2161 | base::TimeDelta::FromDays(1)); // Don't time out used sockets. |
| 2162 | |
| 2163 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2164 | |
| 2165 | // Startup two mock pending connect jobs, which will sit in the MessageLoop. |
| 2166 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2167 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2168 | TestCompletionCallback callback; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2169 | int rv = handle.Init("a", |
| 2170 | params_, |
| 2171 | LOWEST, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2172 | callback.callback(), |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2173 | pool_.get(), |
| 2174 | BoundNetLog()); |
[email protected] | 9bf28db | 2009-08-29 01:35:16 | [diff] [blame] | 2175 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2176 | EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", &handle)); |
[email protected] | 9bf28db | 2009-08-29 01:35:16 | [diff] [blame] | 2177 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2178 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2179 | TestCompletionCallback callback2; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2180 | rv = handle2.Init("a", |
| 2181 | params_, |
| 2182 | LOWEST, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2183 | callback2.callback(), |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2184 | pool_.get(), |
| 2185 | BoundNetLog()); |
[email protected] | 9bf28db | 2009-08-29 01:35:16 | [diff] [blame] | 2186 | EXPECT_EQ(ERR_IO_PENDING, rv); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2187 | EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", &handle2)); |
[email protected] | 9bf28db | 2009-08-29 01:35:16 | [diff] [blame] | 2188 | |
| 2189 | // Cancel one of the requests. Wait for the other, which will get the first |
| 2190 | // job. Release the socket. Run the loop again to make sure the second |
| 2191 | // socket is sitting idle and the first one is released (since ReleaseSocket() |
| 2192 | // just posts a DoReleaseSocket() task). |
| 2193 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2194 | handle.Reset(); |
| 2195 | EXPECT_EQ(OK, callback2.WaitForResult()); |
[email protected] | 0f873e8 | 2010-09-02 16:09:01 | [diff] [blame] | 2196 | // Use the socket. |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 2197 | EXPECT_EQ(1, handle2.socket()->Write(NULL, 1, CompletionCallback())); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2198 | handle2.Reset(); |
[email protected] | 6b17538 | 2009-10-13 06:47:47 | [diff] [blame] | 2199 | |
| 2200 | // We post all of our delayed tasks with a 2ms delay. I.e. they don't |
| 2201 | // actually become pending until 2ms after they have been created. In order |
| 2202 | // to flush all tasks, we need to wait so that we know there are no |
| 2203 | // soon-to-be-pending tasks waiting. |
[email protected] | 26b997396 | 2012-01-28 00:57:00 | [diff] [blame] | 2204 | base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10)); |
[email protected] | b4c62eb | 2012-11-14 18:36:51 | [diff] [blame] | 2205 | MessageLoop::current()->RunUntilIdle(); |
[email protected] | 9bf28db | 2009-08-29 01:35:16 | [diff] [blame] | 2206 | |
| 2207 | ASSERT_EQ(2, pool_->IdleSocketCount()); |
[email protected] | d3f6657 | 2009-09-09 22:38:04 | [diff] [blame] | 2208 | |
[email protected] | 9bf28db | 2009-08-29 01:35:16 | [diff] [blame] | 2209 | // Invoke the idle socket cleanup check. Only one socket should be left, the |
| 2210 | // used socket. Request it to make sure that it's used. |
| 2211 | |
| 2212 | pool_->CleanupTimedOutIdleSockets(); |
[email protected] | 333bdf6 | 2012-06-08 22:57:29 | [diff] [blame] | 2213 | CapturingBoundNetLog log; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2214 | rv = handle.Init("a", |
| 2215 | params_, |
| 2216 | LOWEST, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2217 | callback.callback(), |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2218 | pool_.get(), |
| 2219 | log.bound()); |
[email protected] | 9bf28db | 2009-08-29 01:35:16 | [diff] [blame] | 2220 | EXPECT_EQ(OK, rv); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2221 | EXPECT_TRUE(handle.is_reused()); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 2222 | |
[email protected] | 333bdf6 | 2012-06-08 22:57:29 | [diff] [blame] | 2223 | CapturingNetLog::CapturedEntryList entries; |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 2224 | log.GetEntries(&entries); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2225 | EXPECT_TRUE(LogContainsEntryWithType( |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 2226 | entries, 1, NetLog::TYPE_SOCKET_POOL_REUSED_AN_EXISTING_SOCKET)); |
[email protected] | 9bf28db | 2009-08-29 01:35:16 | [diff] [blame] | 2227 | } |
| 2228 | |
[email protected] | 2041cf34 | 2010-02-19 03:15:59 | [diff] [blame] | 2229 | // Make sure that we process all pending requests even when we're stalling |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2230 | // because of multiple releasing disconnected sockets. |
| 2231 | TEST_F(ClientSocketPoolBaseTest, MultipleReleasingDisconnectedSockets) { |
| 2232 | CreatePoolWithIdleTimeouts( |
| 2233 | kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 2234 | base::TimeDelta(), // Time out unused sockets immediately. |
| 2235 | base::TimeDelta::FromDays(1)); // Don't time out used sockets. |
| 2236 | |
| 2237 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 2238 | |
| 2239 | // Startup 4 connect jobs. Two of them will be pending. |
| 2240 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2241 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2242 | TestCompletionCallback callback; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2243 | int rv = handle.Init("a", |
| 2244 | params_, |
| 2245 | LOWEST, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2246 | callback.callback(), |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2247 | pool_.get(), |
| 2248 | BoundNetLog()); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2249 | EXPECT_EQ(OK, rv); |
| 2250 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2251 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2252 | TestCompletionCallback callback2; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2253 | rv = handle2.Init("a", |
| 2254 | params_, |
| 2255 | LOWEST, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2256 | callback2.callback(), |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2257 | pool_.get(), |
| 2258 | BoundNetLog()); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2259 | EXPECT_EQ(OK, rv); |
| 2260 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2261 | ClientSocketHandle handle3; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2262 | TestCompletionCallback callback3; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2263 | rv = handle3.Init("a", |
| 2264 | params_, |
| 2265 | LOWEST, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2266 | callback3.callback(), |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2267 | pool_.get(), |
| 2268 | BoundNetLog()); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2269 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 2270 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2271 | ClientSocketHandle handle4; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2272 | TestCompletionCallback callback4; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2273 | rv = handle4.Init("a", |
| 2274 | params_, |
| 2275 | LOWEST, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2276 | callback4.callback(), |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2277 | pool_.get(), |
| 2278 | BoundNetLog()); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2279 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 2280 | |
| 2281 | // Release two disconnected sockets. |
| 2282 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2283 | handle.socket()->Disconnect(); |
| 2284 | handle.Reset(); |
| 2285 | handle2.socket()->Disconnect(); |
| 2286 | handle2.Reset(); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2287 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2288 | EXPECT_EQ(OK, callback3.WaitForResult()); |
| 2289 | EXPECT_FALSE(handle3.is_reused()); |
| 2290 | EXPECT_EQ(OK, callback4.WaitForResult()); |
| 2291 | EXPECT_FALSE(handle4.is_reused()); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2292 | } |
| 2293 | |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2294 | // Regression test for http://crbug.com/42267. |
| 2295 | // When DoReleaseSocket() is processed for one socket, it is blocked because the |
| 2296 | // other stalled groups all have releasing sockets, so no progress can be made. |
| 2297 | TEST_F(ClientSocketPoolBaseTest, SocketLimitReleasingSockets) { |
| 2298 | CreatePoolWithIdleTimeouts( |
| 2299 | 4 /* socket limit */, 4 /* socket limit per group */, |
| 2300 | base::TimeDelta(), // Time out unused sockets immediately. |
| 2301 | base::TimeDelta::FromDays(1)); // Don't time out used sockets. |
| 2302 | |
| 2303 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 2304 | |
| 2305 | // Max out the socket limit with 2 per group. |
| 2306 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2307 | ClientSocketHandle handle_a[4]; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2308 | TestCompletionCallback callback_a[4]; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2309 | ClientSocketHandle handle_b[4]; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2310 | TestCompletionCallback callback_b[4]; |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2311 | |
| 2312 | for (int i = 0; i < 2; ++i) { |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2313 | EXPECT_EQ(OK, handle_a[i].Init("a", |
| 2314 | params_, |
| 2315 | LOWEST, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2316 | callback_a[i].callback(), |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2317 | pool_.get(), |
| 2318 | BoundNetLog())); |
| 2319 | EXPECT_EQ(OK, handle_b[i].Init("b", |
| 2320 | params_, |
| 2321 | LOWEST, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2322 | callback_b[i].callback(), |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2323 | pool_.get(), |
| 2324 | BoundNetLog())); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2325 | } |
[email protected] | b89f7e4 | 2010-05-20 20:37:00 | [diff] [blame] | 2326 | |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2327 | // Make 4 pending requests, 2 per group. |
| 2328 | |
| 2329 | for (int i = 2; i < 4; ++i) { |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2330 | EXPECT_EQ(ERR_IO_PENDING, |
| 2331 | handle_a[i].Init("a", |
| 2332 | params_, |
| 2333 | LOWEST, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2334 | callback_a[i].callback(), |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2335 | pool_.get(), |
| 2336 | BoundNetLog())); |
| 2337 | EXPECT_EQ(ERR_IO_PENDING, |
| 2338 | handle_b[i].Init("b", |
| 2339 | params_, |
| 2340 | LOWEST, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2341 | callback_b[i].callback(), |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2342 | pool_.get(), |
| 2343 | BoundNetLog())); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2344 | } |
| 2345 | |
| 2346 | // Release b's socket first. The order is important, because in |
| 2347 | // DoReleaseSocket(), we'll process b's released socket, and since both b and |
| 2348 | // a are stalled, but 'a' is lower lexicographically, we'll process group 'a' |
| 2349 | // first, which has a releasing socket, so it refuses to start up another |
| 2350 | // ConnectJob. So, we used to infinite loop on this. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2351 | handle_b[0].socket()->Disconnect(); |
| 2352 | handle_b[0].Reset(); |
| 2353 | handle_a[0].socket()->Disconnect(); |
| 2354 | handle_a[0].Reset(); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2355 | |
| 2356 | // Used to get stuck here. |
[email protected] | b4c62eb | 2012-11-14 18:36:51 | [diff] [blame] | 2357 | MessageLoop::current()->RunUntilIdle(); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2358 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2359 | handle_b[1].socket()->Disconnect(); |
| 2360 | handle_b[1].Reset(); |
| 2361 | handle_a[1].socket()->Disconnect(); |
| 2362 | handle_a[1].Reset(); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2363 | |
| 2364 | for (int i = 2; i < 4; ++i) { |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2365 | EXPECT_EQ(OK, callback_b[i].WaitForResult()); |
| 2366 | EXPECT_EQ(OK, callback_a[i].WaitForResult()); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2367 | } |
| 2368 | } |
| 2369 | |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2370 | TEST_F(ClientSocketPoolBaseTest, |
| 2371 | ReleasingDisconnectedSocketsMaintainsPriorityOrder) { |
| 2372 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2373 | |
| 2374 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2375 | |
| 2376 | EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); |
| 2377 | EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); |
| 2378 | EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); |
| 2379 | EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); |
| 2380 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2381 | EXPECT_EQ(OK, (*requests())[0]->WaitForResult()); |
| 2382 | EXPECT_EQ(OK, (*requests())[1]->WaitForResult()); |
| 2383 | EXPECT_EQ(2u, completion_count()); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2384 | |
| 2385 | // Releases one connection. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2386 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::NO_KEEP_ALIVE)); |
| 2387 | EXPECT_EQ(OK, (*requests())[2]->WaitForResult()); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2388 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2389 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::NO_KEEP_ALIVE)); |
| 2390 | EXPECT_EQ(OK, (*requests())[3]->WaitForResult()); |
| 2391 | EXPECT_EQ(4u, completion_count()); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2392 | |
| 2393 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 2394 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 2395 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 2396 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 2397 | |
| 2398 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2399 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(5)); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2400 | } |
| 2401 | |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2402 | class TestReleasingSocketRequest : public TestCompletionCallbackBase { |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2403 | public: |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2404 | TestReleasingSocketRequest(TestClientSocketPool* pool, |
| 2405 | int expected_result, |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2406 | bool reset_releasing_handle) |
| 2407 | : pool_(pool), |
| 2408 | expected_result_(expected_result), |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2409 | reset_releasing_handle_(reset_releasing_handle), |
[email protected] | aa249b5 | 2013-04-30 01:04:32 | [diff] [blame] | 2410 | callback_(base::Bind(&TestReleasingSocketRequest::OnComplete, |
| 2411 | base::Unretained(this))) { |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2412 | } |
| 2413 | |
| 2414 | virtual ~TestReleasingSocketRequest() {} |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2415 | |
| 2416 | ClientSocketHandle* handle() { return &handle_; } |
| 2417 | |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2418 | const CompletionCallback& callback() const { return callback_; } |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2419 | |
| 2420 | private: |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2421 | void OnComplete(int result) { |
| 2422 | SetResult(result); |
| 2423 | if (reset_releasing_handle_) |
| 2424 | handle_.Reset(); |
| 2425 | |
| 2426 | scoped_refptr<TestSocketParams> con_params(new TestSocketParams()); |
| 2427 | EXPECT_EQ(expected_result_, |
| 2428 | handle2_.Init("a", con_params, kDefaultPriority, |
| 2429 | callback2_.callback(), pool_, BoundNetLog())); |
| 2430 | } |
| 2431 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2432 | TestClientSocketPool* const pool_; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2433 | int expected_result_; |
| 2434 | bool reset_releasing_handle_; |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2435 | ClientSocketHandle handle_; |
| 2436 | ClientSocketHandle handle2_; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2437 | CompletionCallback callback_; |
| 2438 | TestCompletionCallback callback2_; |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2439 | }; |
| 2440 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2441 | |
| 2442 | TEST_F(ClientSocketPoolBaseTest, AdditionalErrorSocketsDontUseSlot) { |
| 2443 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2444 | |
| 2445 | EXPECT_EQ(OK, StartRequest("b", kDefaultPriority)); |
| 2446 | EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); |
| 2447 | EXPECT_EQ(OK, StartRequest("b", kDefaultPriority)); |
| 2448 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2449 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2450 | client_socket_factory_.allocation_count()); |
| 2451 | |
| 2452 | connect_job_factory_->set_job_type( |
| 2453 | TestConnectJob::kMockPendingAdditionalErrorStateJob); |
| 2454 | TestReleasingSocketRequest req(pool_.get(), OK, false); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2455 | EXPECT_EQ(ERR_IO_PENDING, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2456 | req.handle()->Init("a", params_, kDefaultPriority, req.callback(), |
| 2457 | pool_.get(), BoundNetLog())); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2458 | // The next job should complete synchronously |
| 2459 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 2460 | |
| 2461 | EXPECT_EQ(ERR_CONNECTION_FAILED, req.WaitForResult()); |
| 2462 | EXPECT_FALSE(req.handle()->is_initialized()); |
| 2463 | EXPECT_FALSE(req.handle()->socket()); |
| 2464 | EXPECT_TRUE(req.handle()->is_ssl_error()); |
[email protected] | 8b49869 | 2010-07-16 17:11:43 | [diff] [blame] | 2465 | EXPECT_FALSE(req.handle()->ssl_error_response_info().headers.get() == NULL); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2466 | } |
| 2467 | |
[email protected] | b6501d3d | 2010-06-03 23:53:34 | [diff] [blame] | 2468 | // http://crbug.com/44724 regression test. |
| 2469 | // We start releasing the pool when we flush on network change. When that |
| 2470 | // happens, the only active references are in the ClientSocketHandles. When a |
| 2471 | // ConnectJob completes and calls back into the last ClientSocketHandle, that |
| 2472 | // callback can release the last reference and delete the pool. After the |
| 2473 | // callback finishes, we go back to the stack frame within the now-deleted pool. |
| 2474 | // Executing any code that refers to members of the now-deleted pool can cause |
| 2475 | // crashes. |
| 2476 | TEST_F(ClientSocketPoolBaseTest, CallbackThatReleasesPool) { |
| 2477 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2478 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
| 2479 | |
| 2480 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2481 | TestCompletionCallback callback; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2482 | EXPECT_EQ(ERR_IO_PENDING, handle.Init("a", |
| 2483 | params_, |
| 2484 | kDefaultPriority, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2485 | callback.callback(), |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2486 | pool_.get(), |
| 2487 | BoundNetLog())); |
[email protected] | b6501d3d | 2010-06-03 23:53:34 | [diff] [blame] | 2488 | |
[email protected] | 7af985a | 2012-12-14 22:40:42 | [diff] [blame] | 2489 | pool_->FlushWithError(ERR_NETWORK_CHANGED); |
[email protected] | b6501d3d | 2010-06-03 23:53:34 | [diff] [blame] | 2490 | |
| 2491 | // We'll call back into this now. |
| 2492 | callback.WaitForResult(); |
| 2493 | } |
| 2494 | |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2495 | TEST_F(ClientSocketPoolBaseTest, DoNotReuseSocketAfterFlush) { |
| 2496 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2497 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2498 | |
| 2499 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2500 | TestCompletionCallback callback; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2501 | EXPECT_EQ(ERR_IO_PENDING, handle.Init("a", |
| 2502 | params_, |
| 2503 | kDefaultPriority, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2504 | callback.callback(), |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2505 | pool_.get(), |
| 2506 | BoundNetLog())); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2507 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 2508 | EXPECT_EQ(ClientSocketHandle::UNUSED, handle.reuse_type()); |
| 2509 | |
[email protected] | 7af985a | 2012-12-14 22:40:42 | [diff] [blame] | 2510 | pool_->FlushWithError(ERR_NETWORK_CHANGED); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2511 | |
| 2512 | handle.Reset(); |
[email protected] | b4c62eb | 2012-11-14 18:36:51 | [diff] [blame] | 2513 | MessageLoop::current()->RunUntilIdle(); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2514 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2515 | EXPECT_EQ(ERR_IO_PENDING, handle.Init("a", |
| 2516 | params_, |
| 2517 | kDefaultPriority, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2518 | callback.callback(), |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2519 | pool_.get(), |
| 2520 | BoundNetLog())); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2521 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 2522 | EXPECT_EQ(ClientSocketHandle::UNUSED, handle.reuse_type()); |
| 2523 | } |
| 2524 | |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2525 | class ConnectWithinCallback : public TestCompletionCallbackBase { |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2526 | public: |
| 2527 | ConnectWithinCallback( |
| 2528 | const std::string& group_name, |
| 2529 | const scoped_refptr<TestSocketParams>& params, |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2530 | TestClientSocketPool* pool) |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2531 | : group_name_(group_name), |
| 2532 | params_(params), |
| 2533 | pool_(pool), |
[email protected] | aa249b5 | 2013-04-30 01:04:32 | [diff] [blame] | 2534 | callback_(base::Bind(&ConnectWithinCallback::OnComplete, |
| 2535 | base::Unretained(this))) { |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2536 | } |
| 2537 | |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2538 | virtual ~ConnectWithinCallback() {} |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2539 | |
| 2540 | int WaitForNestedResult() { |
| 2541 | return nested_callback_.WaitForResult(); |
| 2542 | } |
| 2543 | |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2544 | const CompletionCallback& callback() const { return callback_; } |
| 2545 | |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2546 | private: |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2547 | void OnComplete(int result) { |
| 2548 | SetResult(result); |
| 2549 | EXPECT_EQ(ERR_IO_PENDING, |
| 2550 | handle_.Init(group_name_, |
| 2551 | params_, |
| 2552 | kDefaultPriority, |
| 2553 | nested_callback_.callback(), |
| 2554 | pool_, |
| 2555 | BoundNetLog())); |
| 2556 | } |
| 2557 | |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2558 | const std::string group_name_; |
| 2559 | const scoped_refptr<TestSocketParams> params_; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2560 | TestClientSocketPool* const pool_; |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2561 | ClientSocketHandle handle_; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2562 | CompletionCallback callback_; |
| 2563 | TestCompletionCallback nested_callback_; |
| 2564 | |
| 2565 | DISALLOW_COPY_AND_ASSIGN(ConnectWithinCallback); |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2566 | }; |
| 2567 | |
| 2568 | TEST_F(ClientSocketPoolBaseTest, AbortAllRequestsOnFlush) { |
| 2569 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2570 | |
| 2571 | // First job will be waiting until it gets aborted. |
| 2572 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2573 | |
| 2574 | ClientSocketHandle handle; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2575 | ConnectWithinCallback callback("a", params_, pool_.get()); |
| 2576 | EXPECT_EQ(ERR_IO_PENDING, handle.Init("a", |
| 2577 | params_, |
| 2578 | kDefaultPriority, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2579 | callback.callback(), |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2580 | pool_.get(), |
| 2581 | BoundNetLog())); |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2582 | |
| 2583 | // Second job will be started during the first callback, and will |
| 2584 | // asynchronously complete with OK. |
| 2585 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 7af985a | 2012-12-14 22:40:42 | [diff] [blame] | 2586 | pool_->FlushWithError(ERR_NETWORK_CHANGED); |
| 2587 | EXPECT_EQ(ERR_NETWORK_CHANGED, callback.WaitForResult()); |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2588 | EXPECT_EQ(OK, callback.WaitForNestedResult()); |
| 2589 | } |
| 2590 | |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 2591 | // Cancel a pending socket request while we're at max sockets, |
| 2592 | // and verify that the backup socket firing doesn't cause a crash. |
| 2593 | TEST_F(ClientSocketPoolBaseTest, BackupSocketCancelAtMaxSockets) { |
| 2594 | // Max 4 sockets globally, max 4 sockets per group. |
| 2595 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
[email protected] | 06d9404 | 2010-08-25 01:45:22 | [diff] [blame] | 2596 | pool_->EnableConnectBackupJobs(); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 2597 | |
[email protected] | 4baaf9d | 2010-08-31 15:15:44 | [diff] [blame] | 2598 | // Create the first socket and set to ERR_IO_PENDING. This starts the backup |
| 2599 | // timer. |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 2600 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2601 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2602 | TestCompletionCallback callback; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2603 | EXPECT_EQ(ERR_IO_PENDING, handle.Init("bar", |
| 2604 | params_, |
| 2605 | kDefaultPriority, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2606 | callback.callback(), |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2607 | pool_.get(), |
| 2608 | BoundNetLog())); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 2609 | |
| 2610 | // Start (MaxSockets - 1) connected sockets to reach max sockets. |
| 2611 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 2612 | ClientSocketHandle handles[kDefaultMaxSockets]; |
| 2613 | for (int i = 1; i < kDefaultMaxSockets; ++i) { |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2614 | TestCompletionCallback callback; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2615 | EXPECT_EQ(OK, handles[i].Init("bar", |
| 2616 | params_, |
| 2617 | kDefaultPriority, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2618 | callback.callback(), |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2619 | pool_.get(), |
| 2620 | BoundNetLog())); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 2621 | } |
| 2622 | |
[email protected] | b4c62eb | 2012-11-14 18:36:51 | [diff] [blame] | 2623 | MessageLoop::current()->RunUntilIdle(); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 2624 | |
| 2625 | // Cancel the pending request. |
| 2626 | handle.Reset(); |
| 2627 | |
| 2628 | // Wait for the backup timer to fire (add some slop to ensure it fires) |
[email protected] | 26b997396 | 2012-01-28 00:57:00 | [diff] [blame] | 2629 | base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds( |
| 2630 | ClientSocketPool::kMaxConnectRetryIntervalMs / 2 * 3)); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 2631 | |
[email protected] | b4c62eb | 2012-11-14 18:36:51 | [diff] [blame] | 2632 | MessageLoop::current()->RunUntilIdle(); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 2633 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 2634 | } |
| 2635 | |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 2636 | TEST_F(ClientSocketPoolBaseTest, CancelBackupSocketAfterCancelingAllRequests) { |
[email protected] | 4baaf9d | 2010-08-31 15:15:44 | [diff] [blame] | 2637 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 2638 | pool_->EnableConnectBackupJobs(); |
| 2639 | |
| 2640 | // Create the first socket and set to ERR_IO_PENDING. This starts the backup |
| 2641 | // timer. |
| 2642 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2643 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2644 | TestCompletionCallback callback; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2645 | EXPECT_EQ(ERR_IO_PENDING, handle.Init("bar", |
| 2646 | params_, |
| 2647 | kDefaultPriority, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2648 | callback.callback(), |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2649 | pool_.get(), |
| 2650 | BoundNetLog())); |
[email protected] | 4baaf9d | 2010-08-31 15:15:44 | [diff] [blame] | 2651 | ASSERT_TRUE(pool_->HasGroup("bar")); |
| 2652 | EXPECT_EQ(1, pool_->NumConnectJobsInGroup("bar")); |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 2653 | EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("bar")); |
[email protected] | 4baaf9d | 2010-08-31 15:15:44 | [diff] [blame] | 2654 | |
| 2655 | // Cancel the socket request. This should cancel the backup timer. Wait for |
| 2656 | // the backup time to see if it indeed got canceled. |
| 2657 | handle.Reset(); |
| 2658 | // Wait for the backup timer to fire (add some slop to ensure it fires) |
[email protected] | 26b997396 | 2012-01-28 00:57:00 | [diff] [blame] | 2659 | base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds( |
| 2660 | ClientSocketPool::kMaxConnectRetryIntervalMs / 2 * 3)); |
[email protected] | b4c62eb | 2012-11-14 18:36:51 | [diff] [blame] | 2661 | MessageLoop::current()->RunUntilIdle(); |
[email protected] | 4baaf9d | 2010-08-31 15:15:44 | [diff] [blame] | 2662 | ASSERT_TRUE(pool_->HasGroup("bar")); |
| 2663 | EXPECT_EQ(1, pool_->NumConnectJobsInGroup("bar")); |
| 2664 | } |
| 2665 | |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 2666 | TEST_F(ClientSocketPoolBaseTest, CancelBackupSocketAfterFinishingAllRequests) { |
| 2667 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 2668 | pool_->EnableConnectBackupJobs(); |
| 2669 | |
| 2670 | // Create the first socket and set to ERR_IO_PENDING. This starts the backup |
| 2671 | // timer. |
| 2672 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2673 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2674 | TestCompletionCallback callback; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2675 | EXPECT_EQ(ERR_IO_PENDING, handle.Init("bar", |
| 2676 | params_, |
| 2677 | kDefaultPriority, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2678 | callback.callback(), |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2679 | pool_.get(), |
| 2680 | BoundNetLog())); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 2681 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2682 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2683 | TestCompletionCallback callback2; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2684 | EXPECT_EQ(ERR_IO_PENDING, handle2.Init("bar", |
| 2685 | params_, |
| 2686 | kDefaultPriority, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2687 | callback2.callback(), |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2688 | pool_.get(), |
| 2689 | BoundNetLog())); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 2690 | ASSERT_TRUE(pool_->HasGroup("bar")); |
| 2691 | EXPECT_EQ(2, pool_->NumConnectJobsInGroup("bar")); |
| 2692 | |
| 2693 | // Cancel request 1 and then complete request 2. With the requests finished, |
| 2694 | // the backup timer should be cancelled. |
| 2695 | handle.Reset(); |
| 2696 | EXPECT_EQ(OK, callback2.WaitForResult()); |
| 2697 | // Wait for the backup timer to fire (add some slop to ensure it fires) |
[email protected] | 26b997396 | 2012-01-28 00:57:00 | [diff] [blame] | 2698 | base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds( |
| 2699 | ClientSocketPool::kMaxConnectRetryIntervalMs / 2 * 3)); |
[email protected] | b4c62eb | 2012-11-14 18:36:51 | [diff] [blame] | 2700 | MessageLoop::current()->RunUntilIdle(); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 2701 | } |
| 2702 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 2703 | // Test delayed socket binding for the case where we have two connects, |
| 2704 | // and while one is waiting on a connect, the other frees up. |
| 2705 | // The socket waiting on a connect should switch immediately to the freed |
| 2706 | // up socket. |
| 2707 | TEST_F(ClientSocketPoolBaseTest, DelayedSocketBindingWaitingForConnect) { |
| 2708 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2709 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2710 | |
| 2711 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2712 | TestCompletionCallback callback; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2713 | EXPECT_EQ(ERR_IO_PENDING, |
| 2714 | handle1.Init("a", |
| 2715 | params_, |
| 2716 | kDefaultPriority, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2717 | callback.callback(), |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2718 | pool_.get(), |
| 2719 | BoundNetLog())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 2720 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 2721 | |
| 2722 | // No idle sockets, no pending jobs. |
| 2723 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 2724 | EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a")); |
| 2725 | |
| 2726 | // Create a second socket to the same host, but this one will wait. |
| 2727 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2728 | ClientSocketHandle handle2; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2729 | EXPECT_EQ(ERR_IO_PENDING, |
| 2730 | handle2.Init("a", |
| 2731 | params_, |
| 2732 | kDefaultPriority, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2733 | callback.callback(), |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2734 | pool_.get(), |
| 2735 | BoundNetLog())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 2736 | // No idle sockets, and one connecting job. |
| 2737 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 2738 | EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a")); |
| 2739 | |
| 2740 | // Return the first handle to the pool. This will initiate the delayed |
| 2741 | // binding. |
| 2742 | handle1.Reset(); |
| 2743 | |
[email protected] | b4c62eb | 2012-11-14 18:36:51 | [diff] [blame] | 2744 | MessageLoop::current()->RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 2745 | |
| 2746 | // Still no idle sockets, still one pending connect job. |
| 2747 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 2748 | EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a")); |
| 2749 | |
| 2750 | // The second socket connected, even though it was a Waiting Job. |
| 2751 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 2752 | |
| 2753 | // And we can see there is still one job waiting. |
| 2754 | EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a")); |
| 2755 | |
| 2756 | // Finally, signal the waiting Connect. |
| 2757 | client_socket_factory_.SignalJobs(); |
| 2758 | EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a")); |
| 2759 | |
[email protected] | b4c62eb | 2012-11-14 18:36:51 | [diff] [blame] | 2760 | MessageLoop::current()->RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 2761 | } |
| 2762 | |
| 2763 | // Test delayed socket binding when a group is at capacity and one |
| 2764 | // of the group's sockets frees up. |
| 2765 | TEST_F(ClientSocketPoolBaseTest, DelayedSocketBindingAtGroupCapacity) { |
| 2766 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2767 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2768 | |
| 2769 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2770 | TestCompletionCallback callback; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2771 | EXPECT_EQ(ERR_IO_PENDING, |
| 2772 | handle1.Init("a", |
| 2773 | params_, |
| 2774 | kDefaultPriority, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2775 | callback.callback(), |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2776 | pool_.get(), |
| 2777 | BoundNetLog())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 2778 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 2779 | |
| 2780 | // No idle sockets, no pending jobs. |
| 2781 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 2782 | EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a")); |
| 2783 | |
| 2784 | // Create a second socket to the same host, but this one will wait. |
| 2785 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2786 | ClientSocketHandle handle2; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2787 | EXPECT_EQ(ERR_IO_PENDING, |
| 2788 | handle2.Init("a", |
| 2789 | params_, |
| 2790 | kDefaultPriority, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2791 | callback.callback(), |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2792 | pool_.get(), |
| 2793 | BoundNetLog())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 2794 | // No idle sockets, and one connecting job. |
| 2795 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 2796 | EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a")); |
| 2797 | |
| 2798 | // Return the first handle to the pool. This will initiate the delayed |
| 2799 | // binding. |
| 2800 | handle1.Reset(); |
| 2801 | |
[email protected] | b4c62eb | 2012-11-14 18:36:51 | [diff] [blame] | 2802 | MessageLoop::current()->RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 2803 | |
| 2804 | // Still no idle sockets, still one pending connect job. |
| 2805 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 2806 | EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a")); |
| 2807 | |
| 2808 | // The second socket connected, even though it was a Waiting Job. |
| 2809 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 2810 | |
| 2811 | // And we can see there is still one job waiting. |
| 2812 | EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a")); |
| 2813 | |
| 2814 | // Finally, signal the waiting Connect. |
| 2815 | client_socket_factory_.SignalJobs(); |
| 2816 | EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a")); |
| 2817 | |
[email protected] | b4c62eb | 2012-11-14 18:36:51 | [diff] [blame] | 2818 | MessageLoop::current()->RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 2819 | } |
| 2820 | |
| 2821 | // Test out the case where we have one socket connected, one |
| 2822 | // connecting, when the first socket finishes and goes idle. |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 2823 | // Although the second connection is pending, the second request |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 2824 | // should complete, by taking the first socket's idle socket. |
| 2825 | TEST_F(ClientSocketPoolBaseTest, DelayedSocketBindingAtStall) { |
| 2826 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2827 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2828 | |
| 2829 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2830 | TestCompletionCallback callback; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2831 | EXPECT_EQ(ERR_IO_PENDING, |
| 2832 | handle1.Init("a", |
| 2833 | params_, |
| 2834 | kDefaultPriority, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2835 | callback.callback(), |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2836 | pool_.get(), |
| 2837 | BoundNetLog())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 2838 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 2839 | |
| 2840 | // No idle sockets, no pending jobs. |
| 2841 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 2842 | EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a")); |
| 2843 | |
| 2844 | // Create a second socket to the same host, but this one will wait. |
| 2845 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2846 | ClientSocketHandle handle2; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2847 | EXPECT_EQ(ERR_IO_PENDING, |
| 2848 | handle2.Init("a", |
| 2849 | params_, |
| 2850 | kDefaultPriority, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2851 | callback.callback(), |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2852 | pool_.get(), |
| 2853 | BoundNetLog())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 2854 | // No idle sockets, and one connecting job. |
| 2855 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 2856 | EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a")); |
| 2857 | |
| 2858 | // Return the first handle to the pool. This will initiate the delayed |
| 2859 | // binding. |
| 2860 | handle1.Reset(); |
| 2861 | |
[email protected] | b4c62eb | 2012-11-14 18:36:51 | [diff] [blame] | 2862 | MessageLoop::current()->RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 2863 | |
| 2864 | // Still no idle sockets, still one pending connect job. |
| 2865 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 2866 | EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a")); |
| 2867 | |
| 2868 | // The second socket connected, even though it was a Waiting Job. |
| 2869 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 2870 | |
| 2871 | // And we can see there is still one job waiting. |
| 2872 | EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a")); |
| 2873 | |
| 2874 | // Finally, signal the waiting Connect. |
| 2875 | client_socket_factory_.SignalJobs(); |
| 2876 | EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a")); |
| 2877 | |
[email protected] | b4c62eb | 2012-11-14 18:36:51 | [diff] [blame] | 2878 | MessageLoop::current()->RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 2879 | } |
| 2880 | |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 2881 | // Cover the case where on an available socket slot, we have one pending |
| 2882 | // request that completes synchronously, thereby making the Group empty. |
| 2883 | TEST_F(ClientSocketPoolBaseTest, SynchronouslyProcessOnePendingRequest) { |
| 2884 | const int kUnlimitedSockets = 100; |
| 2885 | const int kOneSocketPerGroup = 1; |
| 2886 | CreatePool(kUnlimitedSockets, kOneSocketPerGroup); |
| 2887 | |
| 2888 | // Make the first request asynchronous fail. |
| 2889 | // This will free up a socket slot later. |
| 2890 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
| 2891 | |
| 2892 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2893 | TestCompletionCallback callback1; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2894 | EXPECT_EQ(ERR_IO_PENDING, |
| 2895 | handle1.Init("a", |
| 2896 | params_, |
| 2897 | kDefaultPriority, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2898 | callback1.callback(), |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2899 | pool_.get(), |
| 2900 | BoundNetLog())); |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 2901 | EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a")); |
| 2902 | |
| 2903 | // Make the second request synchronously fail. This should make the Group |
| 2904 | // empty. |
| 2905 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
| 2906 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2907 | TestCompletionCallback callback2; |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 2908 | // It'll be ERR_IO_PENDING now, but the TestConnectJob will synchronously fail |
| 2909 | // when created. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2910 | EXPECT_EQ(ERR_IO_PENDING, |
| 2911 | handle2.Init("a", |
| 2912 | params_, |
| 2913 | kDefaultPriority, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2914 | callback2.callback(), |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2915 | pool_.get(), |
| 2916 | BoundNetLog())); |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 2917 | |
| 2918 | EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a")); |
| 2919 | |
| 2920 | EXPECT_EQ(ERR_CONNECTION_FAILED, callback1.WaitForResult()); |
| 2921 | EXPECT_EQ(ERR_CONNECTION_FAILED, callback2.WaitForResult()); |
| 2922 | EXPECT_FALSE(pool_->HasGroup("a")); |
| 2923 | } |
| 2924 | |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 2925 | TEST_F(ClientSocketPoolBaseTest, PreferUsedSocketToUnusedSocket) { |
| 2926 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 2927 | |
| 2928 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2929 | |
| 2930 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2931 | TestCompletionCallback callback1; |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 2932 | EXPECT_EQ(ERR_IO_PENDING, handle1.Init("a", |
| 2933 | params_, |
| 2934 | kDefaultPriority, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2935 | callback1.callback(), |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 2936 | pool_.get(), |
| 2937 | BoundNetLog())); |
| 2938 | |
| 2939 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2940 | TestCompletionCallback callback2; |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 2941 | EXPECT_EQ(ERR_IO_PENDING, handle2.Init("a", |
| 2942 | params_, |
| 2943 | kDefaultPriority, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2944 | callback2.callback(), |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 2945 | pool_.get(), |
| 2946 | BoundNetLog())); |
| 2947 | ClientSocketHandle handle3; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2948 | TestCompletionCallback callback3; |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 2949 | EXPECT_EQ(ERR_IO_PENDING, handle3.Init("a", |
| 2950 | params_, |
| 2951 | kDefaultPriority, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2952 | callback3.callback(), |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 2953 | pool_.get(), |
| 2954 | BoundNetLog())); |
| 2955 | |
| 2956 | EXPECT_EQ(OK, callback1.WaitForResult()); |
| 2957 | EXPECT_EQ(OK, callback2.WaitForResult()); |
| 2958 | EXPECT_EQ(OK, callback3.WaitForResult()); |
| 2959 | |
| 2960 | // Use the socket. |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 2961 | EXPECT_EQ(1, handle1.socket()->Write(NULL, 1, CompletionCallback())); |
| 2962 | EXPECT_EQ(1, handle3.socket()->Write(NULL, 1, CompletionCallback())); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 2963 | |
| 2964 | handle1.Reset(); |
| 2965 | handle2.Reset(); |
| 2966 | handle3.Reset(); |
| 2967 | |
| 2968 | EXPECT_EQ(OK, handle1.Init("a", |
| 2969 | params_, |
| 2970 | kDefaultPriority, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2971 | callback1.callback(), |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 2972 | pool_.get(), |
| 2973 | BoundNetLog())); |
| 2974 | EXPECT_EQ(OK, handle2.Init("a", |
| 2975 | params_, |
| 2976 | kDefaultPriority, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2977 | callback2.callback(), |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 2978 | pool_.get(), |
| 2979 | BoundNetLog())); |
| 2980 | EXPECT_EQ(OK, handle3.Init("a", |
| 2981 | params_, |
| 2982 | kDefaultPriority, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2983 | callback3.callback(), |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 2984 | pool_.get(), |
| 2985 | BoundNetLog())); |
| 2986 | |
| 2987 | EXPECT_TRUE(handle1.socket()->WasEverUsed()); |
| 2988 | EXPECT_TRUE(handle2.socket()->WasEverUsed()); |
| 2989 | EXPECT_FALSE(handle3.socket()->WasEverUsed()); |
| 2990 | } |
| 2991 | |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 2992 | TEST_F(ClientSocketPoolBaseTest, RequestSockets) { |
| 2993 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2994 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2995 | |
| 2996 | pool_->RequestSockets("a", ¶ms_, 2, BoundNetLog()); |
| 2997 | |
| 2998 | ASSERT_TRUE(pool_->HasGroup("a")); |
| 2999 | EXPECT_EQ(2, pool_->NumConnectJobsInGroup("a")); |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3000 | EXPECT_EQ(2, pool_->NumUnassignedConnectJobsInGroup("a")); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3001 | EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a")); |
| 3002 | |
| 3003 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3004 | TestCompletionCallback callback1; |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3005 | EXPECT_EQ(ERR_IO_PENDING, handle1.Init("a", |
| 3006 | params_, |
| 3007 | kDefaultPriority, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3008 | callback1.callback(), |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3009 | pool_.get(), |
| 3010 | BoundNetLog())); |
| 3011 | |
| 3012 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3013 | TestCompletionCallback callback2; |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3014 | EXPECT_EQ(ERR_IO_PENDING, handle2.Init("a", |
| 3015 | params_, |
| 3016 | kDefaultPriority, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3017 | callback2.callback(), |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3018 | pool_.get(), |
| 3019 | BoundNetLog())); |
| 3020 | |
| 3021 | EXPECT_EQ(2, pool_->NumConnectJobsInGroup("a")); |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3022 | EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a")); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3023 | EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a")); |
| 3024 | |
| 3025 | EXPECT_EQ(OK, callback1.WaitForResult()); |
| 3026 | EXPECT_EQ(OK, callback2.WaitForResult()); |
| 3027 | handle1.Reset(); |
| 3028 | handle2.Reset(); |
| 3029 | |
| 3030 | EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a")); |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3031 | EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a")); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3032 | EXPECT_EQ(2, pool_->IdleSocketCountInGroup("a")); |
| 3033 | } |
| 3034 | |
| 3035 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsWhenAlreadyHaveAConnectJob) { |
| 3036 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3037 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3038 | |
| 3039 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3040 | TestCompletionCallback callback1; |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3041 | EXPECT_EQ(ERR_IO_PENDING, handle1.Init("a", |
| 3042 | params_, |
| 3043 | kDefaultPriority, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3044 | callback1.callback(), |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3045 | pool_.get(), |
| 3046 | BoundNetLog())); |
| 3047 | |
| 3048 | ASSERT_TRUE(pool_->HasGroup("a")); |
| 3049 | EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a")); |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3050 | EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a")); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3051 | EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a")); |
| 3052 | |
| 3053 | pool_->RequestSockets("a", ¶ms_, 2, BoundNetLog()); |
| 3054 | |
| 3055 | EXPECT_EQ(2, pool_->NumConnectJobsInGroup("a")); |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3056 | EXPECT_EQ(1, pool_->NumUnassignedConnectJobsInGroup("a")); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3057 | EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a")); |
| 3058 | |
| 3059 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3060 | TestCompletionCallback callback2; |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3061 | EXPECT_EQ(ERR_IO_PENDING, handle2.Init("a", |
| 3062 | params_, |
| 3063 | kDefaultPriority, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3064 | callback2.callback(), |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3065 | pool_.get(), |
| 3066 | BoundNetLog())); |
| 3067 | |
| 3068 | EXPECT_EQ(2, pool_->NumConnectJobsInGroup("a")); |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3069 | EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a")); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3070 | EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a")); |
| 3071 | |
| 3072 | EXPECT_EQ(OK, callback1.WaitForResult()); |
| 3073 | EXPECT_EQ(OK, callback2.WaitForResult()); |
| 3074 | handle1.Reset(); |
| 3075 | handle2.Reset(); |
| 3076 | |
| 3077 | EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a")); |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3078 | EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a")); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3079 | EXPECT_EQ(2, pool_->IdleSocketCountInGroup("a")); |
| 3080 | } |
| 3081 | |
| 3082 | TEST_F(ClientSocketPoolBaseTest, |
| 3083 | RequestSocketsWhenAlreadyHaveMultipleConnectJob) { |
| 3084 | CreatePool(4, 4); |
| 3085 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3086 | |
| 3087 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3088 | TestCompletionCallback callback1; |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3089 | EXPECT_EQ(ERR_IO_PENDING, handle1.Init("a", |
| 3090 | params_, |
| 3091 | kDefaultPriority, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3092 | callback1.callback(), |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3093 | pool_.get(), |
| 3094 | BoundNetLog())); |
| 3095 | |
| 3096 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3097 | TestCompletionCallback callback2; |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3098 | EXPECT_EQ(ERR_IO_PENDING, handle2.Init("a", |
| 3099 | params_, |
| 3100 | kDefaultPriority, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3101 | callback2.callback(), |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3102 | pool_.get(), |
| 3103 | BoundNetLog())); |
| 3104 | |
| 3105 | ClientSocketHandle handle3; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3106 | TestCompletionCallback callback3; |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3107 | EXPECT_EQ(ERR_IO_PENDING, handle3.Init("a", |
| 3108 | params_, |
| 3109 | kDefaultPriority, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3110 | callback3.callback(), |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3111 | pool_.get(), |
| 3112 | BoundNetLog())); |
| 3113 | |
| 3114 | ASSERT_TRUE(pool_->HasGroup("a")); |
| 3115 | EXPECT_EQ(3, pool_->NumConnectJobsInGroup("a")); |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3116 | EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a")); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3117 | EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a")); |
| 3118 | |
| 3119 | pool_->RequestSockets("a", ¶ms_, 2, BoundNetLog()); |
| 3120 | |
| 3121 | EXPECT_EQ(3, pool_->NumConnectJobsInGroup("a")); |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3122 | EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a")); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3123 | EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a")); |
| 3124 | |
| 3125 | EXPECT_EQ(OK, callback1.WaitForResult()); |
| 3126 | EXPECT_EQ(OK, callback2.WaitForResult()); |
| 3127 | EXPECT_EQ(OK, callback3.WaitForResult()); |
| 3128 | handle1.Reset(); |
| 3129 | handle2.Reset(); |
| 3130 | handle3.Reset(); |
| 3131 | |
| 3132 | EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a")); |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3133 | EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a")); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3134 | EXPECT_EQ(3, pool_->IdleSocketCountInGroup("a")); |
| 3135 | } |
| 3136 | |
| 3137 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsAtMaxSocketLimit) { |
| 3138 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 3139 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3140 | |
| 3141 | ASSERT_FALSE(pool_->HasGroup("a")); |
| 3142 | |
| 3143 | pool_->RequestSockets("a", ¶ms_, kDefaultMaxSockets, |
| 3144 | BoundNetLog()); |
| 3145 | |
| 3146 | ASSERT_TRUE(pool_->HasGroup("a")); |
| 3147 | EXPECT_EQ(kDefaultMaxSockets, pool_->NumConnectJobsInGroup("a")); |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3148 | EXPECT_EQ(kDefaultMaxSockets, pool_->NumUnassignedConnectJobsInGroup("a")); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3149 | |
| 3150 | ASSERT_FALSE(pool_->HasGroup("b")); |
| 3151 | |
| 3152 | pool_->RequestSockets("b", ¶ms_, kDefaultMaxSockets, |
| 3153 | BoundNetLog()); |
| 3154 | |
| 3155 | ASSERT_FALSE(pool_->HasGroup("b")); |
| 3156 | } |
| 3157 | |
| 3158 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsHitMaxSocketLimit) { |
| 3159 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 3160 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3161 | |
| 3162 | ASSERT_FALSE(pool_->HasGroup("a")); |
| 3163 | |
| 3164 | pool_->RequestSockets("a", ¶ms_, kDefaultMaxSockets - 1, |
| 3165 | BoundNetLog()); |
| 3166 | |
| 3167 | ASSERT_TRUE(pool_->HasGroup("a")); |
| 3168 | EXPECT_EQ(kDefaultMaxSockets - 1, pool_->NumConnectJobsInGroup("a")); |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3169 | EXPECT_EQ(kDefaultMaxSockets - 1, |
| 3170 | pool_->NumUnassignedConnectJobsInGroup("a")); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 3171 | EXPECT_FALSE(pool_->IsStalled()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3172 | |
| 3173 | ASSERT_FALSE(pool_->HasGroup("b")); |
| 3174 | |
| 3175 | pool_->RequestSockets("b", ¶ms_, kDefaultMaxSockets, |
| 3176 | BoundNetLog()); |
| 3177 | |
| 3178 | ASSERT_TRUE(pool_->HasGroup("b")); |
| 3179 | EXPECT_EQ(1, pool_->NumConnectJobsInGroup("b")); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 3180 | EXPECT_FALSE(pool_->IsStalled()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3181 | } |
| 3182 | |
| 3183 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsCountIdleSockets) { |
| 3184 | CreatePool(4, 4); |
| 3185 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3186 | |
| 3187 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3188 | TestCompletionCallback callback1; |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3189 | EXPECT_EQ(ERR_IO_PENDING, handle1.Init("a", |
| 3190 | params_, |
| 3191 | kDefaultPriority, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3192 | callback1.callback(), |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3193 | pool_.get(), |
| 3194 | BoundNetLog())); |
| 3195 | ASSERT_EQ(OK, callback1.WaitForResult()); |
| 3196 | handle1.Reset(); |
| 3197 | |
| 3198 | ASSERT_TRUE(pool_->HasGroup("a")); |
| 3199 | EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a")); |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3200 | EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a")); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3201 | EXPECT_EQ(1, pool_->IdleSocketCountInGroup("a")); |
| 3202 | |
| 3203 | pool_->RequestSockets("a", ¶ms_, 2, BoundNetLog()); |
| 3204 | |
| 3205 | EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a")); |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3206 | EXPECT_EQ(1, pool_->NumUnassignedConnectJobsInGroup("a")); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3207 | EXPECT_EQ(1, pool_->IdleSocketCountInGroup("a")); |
| 3208 | } |
| 3209 | |
| 3210 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsCountActiveSockets) { |
| 3211 | CreatePool(4, 4); |
| 3212 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3213 | |
| 3214 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3215 | TestCompletionCallback callback1; |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3216 | EXPECT_EQ(ERR_IO_PENDING, handle1.Init("a", |
| 3217 | params_, |
| 3218 | kDefaultPriority, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3219 | callback1.callback(), |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3220 | pool_.get(), |
| 3221 | BoundNetLog())); |
| 3222 | ASSERT_EQ(OK, callback1.WaitForResult()); |
| 3223 | |
| 3224 | ASSERT_TRUE(pool_->HasGroup("a")); |
| 3225 | EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a")); |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3226 | EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a")); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3227 | EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a")); |
| 3228 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroup("a")); |
| 3229 | |
| 3230 | pool_->RequestSockets("a", ¶ms_, 2, BoundNetLog()); |
| 3231 | |
| 3232 | EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a")); |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3233 | EXPECT_EQ(1, pool_->NumUnassignedConnectJobsInGroup("a")); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3234 | EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a")); |
| 3235 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroup("a")); |
| 3236 | } |
| 3237 | |
| 3238 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsSynchronous) { |
| 3239 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3240 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 3241 | |
| 3242 | pool_->RequestSockets("a", ¶ms_, kDefaultMaxSocketsPerGroup, |
| 3243 | BoundNetLog()); |
| 3244 | |
| 3245 | ASSERT_TRUE(pool_->HasGroup("a")); |
| 3246 | EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a")); |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3247 | EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a")); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3248 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, pool_->IdleSocketCountInGroup("a")); |
| 3249 | |
| 3250 | pool_->RequestSockets("b", ¶ms_, kDefaultMaxSocketsPerGroup, |
| 3251 | BoundNetLog()); |
| 3252 | |
| 3253 | EXPECT_EQ(0, pool_->NumConnectJobsInGroup("b")); |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3254 | EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("b")); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3255 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, pool_->IdleSocketCountInGroup("b")); |
| 3256 | } |
| 3257 | |
[email protected] | 3c819f52 | 2010-12-02 02:03:12 | [diff] [blame] | 3258 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsSynchronousError) { |
| 3259 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3260 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
| 3261 | |
| 3262 | pool_->RequestSockets("a", ¶ms_, kDefaultMaxSocketsPerGroup, |
| 3263 | BoundNetLog()); |
| 3264 | |
| 3265 | ASSERT_FALSE(pool_->HasGroup("a")); |
[email protected] | fd2e53e | 2011-01-14 20:40:52 | [diff] [blame] | 3266 | |
| 3267 | connect_job_factory_->set_job_type( |
| 3268 | TestConnectJob::kMockAdditionalErrorStateJob); |
| 3269 | pool_->RequestSockets("a", ¶ms_, kDefaultMaxSocketsPerGroup, |
| 3270 | BoundNetLog()); |
| 3271 | |
| 3272 | ASSERT_FALSE(pool_->HasGroup("a")); |
[email protected] | 3c819f52 | 2010-12-02 02:03:12 | [diff] [blame] | 3273 | } |
| 3274 | |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3275 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsMultipleTimesDoesNothing) { |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3276 | CreatePool(4, 4); |
| 3277 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3278 | |
| 3279 | pool_->RequestSockets("a", ¶ms_, 2, BoundNetLog()); |
| 3280 | |
| 3281 | ASSERT_TRUE(pool_->HasGroup("a")); |
| 3282 | EXPECT_EQ(2, pool_->NumConnectJobsInGroup("a")); |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3283 | EXPECT_EQ(2, pool_->NumUnassignedConnectJobsInGroup("a")); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3284 | EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a")); |
| 3285 | |
| 3286 | pool_->RequestSockets("a", ¶ms_, 2, BoundNetLog()); |
| 3287 | EXPECT_EQ(2, pool_->NumConnectJobsInGroup("a")); |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3288 | EXPECT_EQ(2, pool_->NumUnassignedConnectJobsInGroup("a")); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3289 | EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a")); |
| 3290 | |
| 3291 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3292 | TestCompletionCallback callback1; |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3293 | EXPECT_EQ(ERR_IO_PENDING, handle1.Init("a", |
| 3294 | params_, |
| 3295 | kDefaultPriority, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3296 | callback1.callback(), |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3297 | pool_.get(), |
| 3298 | BoundNetLog())); |
| 3299 | ASSERT_EQ(OK, callback1.WaitForResult()); |
| 3300 | |
| 3301 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3302 | TestCompletionCallback callback2; |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3303 | int rv = handle2.Init("a", |
| 3304 | params_, |
| 3305 | kDefaultPriority, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3306 | callback2.callback(), |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3307 | pool_.get(), |
| 3308 | BoundNetLog()); |
| 3309 | if (rv != OK) { |
| 3310 | EXPECT_EQ(ERR_IO_PENDING, rv); |
| 3311 | EXPECT_EQ(OK, callback2.WaitForResult()); |
| 3312 | } |
| 3313 | |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3314 | EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a")); |
| 3315 | EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a")); |
| 3316 | EXPECT_EQ(2, pool_->NumActiveSocketsInGroup("a")); |
| 3317 | EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a")); |
| 3318 | |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3319 | handle1.Reset(); |
| 3320 | handle2.Reset(); |
| 3321 | |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3322 | EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a")); |
| 3323 | EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a")); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3324 | EXPECT_EQ(2, pool_->IdleSocketCountInGroup("a")); |
| 3325 | |
| 3326 | pool_->RequestSockets("a", ¶ms_, 2, BoundNetLog()); |
| 3327 | EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a")); |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3328 | EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a")); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3329 | EXPECT_EQ(2, pool_->IdleSocketCountInGroup("a")); |
| 3330 | } |
| 3331 | |
| 3332 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsDifferentNumSockets) { |
| 3333 | CreatePool(4, 4); |
| 3334 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3335 | |
| 3336 | pool_->RequestSockets("a", ¶ms_, 1, BoundNetLog()); |
| 3337 | |
| 3338 | ASSERT_TRUE(pool_->HasGroup("a")); |
| 3339 | EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a")); |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3340 | EXPECT_EQ(1, pool_->NumUnassignedConnectJobsInGroup("a")); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3341 | EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a")); |
| 3342 | |
| 3343 | pool_->RequestSockets("a", ¶ms_, 2, BoundNetLog()); |
| 3344 | EXPECT_EQ(2, pool_->NumConnectJobsInGroup("a")); |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3345 | EXPECT_EQ(2, pool_->NumUnassignedConnectJobsInGroup("a")); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3346 | EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a")); |
| 3347 | |
| 3348 | pool_->RequestSockets("a", ¶ms_, 3, BoundNetLog()); |
| 3349 | EXPECT_EQ(3, pool_->NumConnectJobsInGroup("a")); |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3350 | EXPECT_EQ(3, pool_->NumUnassignedConnectJobsInGroup("a")); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3351 | EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a")); |
| 3352 | |
| 3353 | pool_->RequestSockets("a", ¶ms_, 1, BoundNetLog()); |
| 3354 | EXPECT_EQ(3, pool_->NumConnectJobsInGroup("a")); |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3355 | EXPECT_EQ(3, pool_->NumUnassignedConnectJobsInGroup("a")); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3356 | EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a")); |
| 3357 | } |
| 3358 | |
| 3359 | TEST_F(ClientSocketPoolBaseTest, PreconnectJobsTakenByNormalRequests) { |
| 3360 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3361 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3362 | |
| 3363 | pool_->RequestSockets("a", ¶ms_, 1, BoundNetLog()); |
| 3364 | |
| 3365 | ASSERT_TRUE(pool_->HasGroup("a")); |
| 3366 | EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a")); |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3367 | EXPECT_EQ(1, pool_->NumUnassignedConnectJobsInGroup("a")); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3368 | EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a")); |
| 3369 | |
| 3370 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3371 | TestCompletionCallback callback1; |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3372 | EXPECT_EQ(ERR_IO_PENDING, handle1.Init("a", |
| 3373 | params_, |
| 3374 | kDefaultPriority, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3375 | callback1.callback(), |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3376 | pool_.get(), |
| 3377 | BoundNetLog())); |
| 3378 | |
| 3379 | EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a")); |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3380 | EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a")); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3381 | EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a")); |
| 3382 | |
| 3383 | ASSERT_EQ(OK, callback1.WaitForResult()); |
| 3384 | |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 3385 | // Make sure if a preconneced socket is not fully connected when a request |
| 3386 | // starts, it has a connect start time. |
| 3387 | TestLoadTimingInfoConnectedNotReused(handle1); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3388 | handle1.Reset(); |
| 3389 | |
| 3390 | EXPECT_EQ(1, pool_->IdleSocketCountInGroup("a")); |
| 3391 | } |
| 3392 | |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 3393 | // Checks that fully connected preconnect jobs have no connect times, and are |
| 3394 | // marked as reused. |
| 3395 | TEST_F(ClientSocketPoolBaseTest, ConnectedPreconnectJobsHaveNoConnectTimes) { |
| 3396 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3397 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 3398 | pool_->RequestSockets("a", ¶ms_, 1, BoundNetLog()); |
| 3399 | |
| 3400 | ASSERT_TRUE(pool_->HasGroup("a")); |
| 3401 | EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a")); |
| 3402 | EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a")); |
| 3403 | EXPECT_EQ(1, pool_->IdleSocketCountInGroup("a")); |
| 3404 | |
| 3405 | ClientSocketHandle handle; |
| 3406 | TestCompletionCallback callback; |
| 3407 | EXPECT_EQ(OK, handle.Init("a", |
| 3408 | params_, |
| 3409 | kDefaultPriority, |
| 3410 | callback.callback(), |
| 3411 | pool_.get(), |
| 3412 | BoundNetLog())); |
| 3413 | |
| 3414 | // Make sure the idle socket was used. |
| 3415 | EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a")); |
| 3416 | |
| 3417 | TestLoadTimingInfoConnectedReused(handle); |
| 3418 | handle.Reset(); |
| 3419 | TestLoadTimingInfoNotConnected(handle); |
| 3420 | } |
| 3421 | |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3422 | // http://crbug.com/64940 regression test. |
| 3423 | TEST_F(ClientSocketPoolBaseTest, PreconnectClosesIdleSocketRemovesGroup) { |
| 3424 | const int kMaxTotalSockets = 3; |
| 3425 | const int kMaxSocketsPerGroup = 2; |
| 3426 | CreatePool(kMaxTotalSockets, kMaxSocketsPerGroup); |
| 3427 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3428 | |
| 3429 | // Note that group name ordering matters here. "a" comes before "b", so |
| 3430 | // CloseOneIdleSocket() will try to close "a"'s idle socket. |
| 3431 | |
| 3432 | // Set up one idle socket in "a". |
| 3433 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3434 | TestCompletionCallback callback1; |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3435 | EXPECT_EQ(ERR_IO_PENDING, handle1.Init("a", |
| 3436 | params_, |
| 3437 | kDefaultPriority, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3438 | callback1.callback(), |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3439 | pool_.get(), |
| 3440 | BoundNetLog())); |
| 3441 | |
| 3442 | ASSERT_EQ(OK, callback1.WaitForResult()); |
| 3443 | handle1.Reset(); |
| 3444 | EXPECT_EQ(1, pool_->IdleSocketCountInGroup("a")); |
| 3445 | |
| 3446 | // Set up two active sockets in "b". |
| 3447 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3448 | TestCompletionCallback callback2; |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3449 | EXPECT_EQ(ERR_IO_PENDING, handle1.Init("b", |
| 3450 | params_, |
| 3451 | kDefaultPriority, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3452 | callback1.callback(), |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3453 | pool_.get(), |
| 3454 | BoundNetLog())); |
| 3455 | EXPECT_EQ(ERR_IO_PENDING, handle2.Init("b", |
| 3456 | params_, |
| 3457 | kDefaultPriority, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3458 | callback2.callback(), |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3459 | pool_.get(), |
| 3460 | BoundNetLog())); |
| 3461 | |
| 3462 | ASSERT_EQ(OK, callback1.WaitForResult()); |
| 3463 | ASSERT_EQ(OK, callback2.WaitForResult()); |
| 3464 | EXPECT_EQ(0, pool_->IdleSocketCountInGroup("b")); |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3465 | EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("b")); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3466 | EXPECT_EQ(2, pool_->NumActiveSocketsInGroup("b")); |
| 3467 | |
| 3468 | // Now we have 1 idle socket in "a" and 2 active sockets in "b". This means |
| 3469 | // we've maxed out on sockets, since we set |kMaxTotalSockets| to 3. |
| 3470 | // Requesting 2 preconnected sockets for "a" should fail to allocate any more |
| 3471 | // sockets for "a", and "b" should still have 2 active sockets. |
| 3472 | |
| 3473 | pool_->RequestSockets("a", ¶ms_, 2, BoundNetLog()); |
| 3474 | EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a")); |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3475 | EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a")); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3476 | EXPECT_EQ(1, pool_->IdleSocketCountInGroup("a")); |
| 3477 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroup("a")); |
| 3478 | EXPECT_EQ(0, pool_->NumConnectJobsInGroup("b")); |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3479 | EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("b")); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3480 | EXPECT_EQ(0, pool_->IdleSocketCountInGroup("b")); |
| 3481 | EXPECT_EQ(2, pool_->NumActiveSocketsInGroup("b")); |
| 3482 | |
| 3483 | // Now release the 2 active sockets for "b". This will give us 1 idle socket |
| 3484 | // in "a" and 2 idle sockets in "b". Requesting 2 preconnected sockets for |
| 3485 | // "a" should result in closing 1 for "b". |
| 3486 | handle1.Reset(); |
| 3487 | handle2.Reset(); |
| 3488 | EXPECT_EQ(2, pool_->IdleSocketCountInGroup("b")); |
| 3489 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroup("b")); |
| 3490 | |
| 3491 | pool_->RequestSockets("a", ¶ms_, 2, BoundNetLog()); |
| 3492 | EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a")); |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3493 | EXPECT_EQ(1, pool_->NumUnassignedConnectJobsInGroup("a")); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3494 | EXPECT_EQ(1, pool_->IdleSocketCountInGroup("a")); |
| 3495 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroup("a")); |
| 3496 | EXPECT_EQ(0, pool_->NumConnectJobsInGroup("b")); |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3497 | EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("b")); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3498 | EXPECT_EQ(1, pool_->IdleSocketCountInGroup("b")); |
| 3499 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroup("b")); |
| 3500 | } |
| 3501 | |
[email protected] | b7b8be4 | 2011-07-12 12:46:41 | [diff] [blame] | 3502 | TEST_F(ClientSocketPoolBaseTest, PreconnectWithoutBackupJob) { |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 3503 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3504 | pool_->EnableConnectBackupJobs(); |
| 3505 | |
| 3506 | // Make the ConnectJob hang until it times out, shorten the timeout. |
| 3507 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3508 | connect_job_factory_->set_timeout_duration( |
| 3509 | base::TimeDelta::FromMilliseconds(500)); |
| 3510 | pool_->RequestSockets("a", ¶ms_, 1, BoundNetLog()); |
| 3511 | EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a")); |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3512 | EXPECT_EQ(1, pool_->NumUnassignedConnectJobsInGroup("a")); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 3513 | EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a")); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 3514 | |
[email protected] | b7b8be4 | 2011-07-12 12:46:41 | [diff] [blame] | 3515 | // Verify the backup timer doesn't create a backup job, by making |
| 3516 | // the backup job a pending job instead of a waiting job, so it |
| 3517 | // *would* complete if it were created. |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 3518 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 3519 | MessageLoop::current()->PostDelayedTask( |
| 3520 | FROM_HERE, MessageLoop::QuitClosure(), base::TimeDelta::FromSeconds(1)); |
[email protected] | b7b8be4 | 2011-07-12 12:46:41 | [diff] [blame] | 3521 | MessageLoop::current()->Run(); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 3522 | EXPECT_FALSE(pool_->HasGroup("a")); |
| 3523 | } |
| 3524 | |
[email protected] | b7b8be4 | 2011-07-12 12:46:41 | [diff] [blame] | 3525 | TEST_F(ClientSocketPoolBaseTest, PreconnectWithBackupJob) { |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 3526 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3527 | pool_->EnableConnectBackupJobs(); |
| 3528 | |
| 3529 | // Make the ConnectJob hang forever. |
| 3530 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3531 | pool_->RequestSockets("a", ¶ms_, 1, BoundNetLog()); |
| 3532 | EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a")); |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3533 | EXPECT_EQ(1, pool_->NumUnassignedConnectJobsInGroup("a")); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 3534 | EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a")); |
[email protected] | b4c62eb | 2012-11-14 18:36:51 | [diff] [blame] | 3535 | MessageLoop::current()->RunUntilIdle(); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 3536 | |
| 3537 | // Make the backup job be a pending job, so it completes normally. |
| 3538 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3539 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3540 | TestCompletionCallback callback; |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 3541 | EXPECT_EQ(ERR_IO_PENDING, handle.Init("a", |
| 3542 | params_, |
| 3543 | kDefaultPriority, |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3544 | callback.callback(), |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 3545 | pool_.get(), |
| 3546 | BoundNetLog())); |
[email protected] | b7b8be4 | 2011-07-12 12:46:41 | [diff] [blame] | 3547 | // Timer has started, but the backup connect job shouldn't be created yet. |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 3548 | EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a")); |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3549 | EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a")); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 3550 | EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a")); |
| 3551 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroup("a")); |
| 3552 | ASSERT_EQ(OK, callback.WaitForResult()); |
| 3553 | |
| 3554 | // The hung connect job should still be there, but everything else should be |
| 3555 | // complete. |
| 3556 | EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a")); |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3557 | EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a")); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 3558 | EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a")); |
| 3559 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroup("a")); |
| 3560 | } |
| 3561 | |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 3562 | class MockLayeredPool : public LayeredPool { |
| 3563 | public: |
| 3564 | MockLayeredPool(TestClientSocketPool* pool, |
| 3565 | const std::string& group_name) |
| 3566 | : pool_(pool), |
| 3567 | params_(new TestSocketParams), |
| 3568 | group_name_(group_name), |
| 3569 | can_release_connection_(true) { |
| 3570 | pool_->AddLayeredPool(this); |
| 3571 | } |
| 3572 | |
| 3573 | ~MockLayeredPool() { |
| 3574 | pool_->RemoveLayeredPool(this); |
| 3575 | } |
| 3576 | |
| 3577 | int RequestSocket(TestClientSocketPool* pool) { |
| 3578 | return handle_.Init(group_name_, params_, kDefaultPriority, |
| 3579 | callback_.callback(), pool, BoundNetLog()); |
| 3580 | } |
| 3581 | |
| 3582 | int RequestSocketWithoutLimits(TestClientSocketPool* pool) { |
| 3583 | params_->set_ignore_limits(true); |
| 3584 | return handle_.Init(group_name_, params_, kDefaultPriority, |
| 3585 | callback_.callback(), pool, BoundNetLog()); |
| 3586 | } |
| 3587 | |
| 3588 | bool ReleaseOneConnection() { |
| 3589 | if (!handle_.is_initialized() || !can_release_connection_) { |
| 3590 | return false; |
| 3591 | } |
| 3592 | handle_.socket()->Disconnect(); |
| 3593 | handle_.Reset(); |
| 3594 | return true; |
| 3595 | } |
| 3596 | |
| 3597 | void set_can_release_connection(bool can_release_connection) { |
| 3598 | can_release_connection_ = can_release_connection; |
| 3599 | } |
| 3600 | |
| 3601 | MOCK_METHOD0(CloseOneIdleConnection, bool()); |
| 3602 | |
| 3603 | private: |
| 3604 | TestClientSocketPool* const pool_; |
| 3605 | scoped_refptr<TestSocketParams> params_; |
| 3606 | ClientSocketHandle handle_; |
| 3607 | TestCompletionCallback callback_; |
| 3608 | const std::string group_name_; |
| 3609 | bool can_release_connection_; |
| 3610 | }; |
| 3611 | |
| 3612 | TEST_F(ClientSocketPoolBaseTest, FailToCloseIdleSocketsNotHeldByLayeredPool) { |
| 3613 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3614 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 3615 | |
| 3616 | MockLayeredPool mock_layered_pool(pool_.get(), "foo"); |
| 3617 | EXPECT_EQ(OK, mock_layered_pool.RequestSocket(pool_.get())); |
| 3618 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 3619 | .WillOnce(Return(false)); |
| 3620 | EXPECT_FALSE(pool_->CloseOneIdleConnectionInLayeredPool()); |
| 3621 | } |
| 3622 | |
| 3623 | TEST_F(ClientSocketPoolBaseTest, ForciblyCloseIdleSocketsHeldByLayeredPool) { |
| 3624 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3625 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 3626 | |
| 3627 | MockLayeredPool mock_layered_pool(pool_.get(), "foo"); |
| 3628 | EXPECT_EQ(OK, mock_layered_pool.RequestSocket(pool_.get())); |
| 3629 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 3630 | .WillOnce(Invoke(&mock_layered_pool, |
| 3631 | &MockLayeredPool::ReleaseOneConnection)); |
| 3632 | EXPECT_TRUE(pool_->CloseOneIdleConnectionInLayeredPool()); |
| 3633 | } |
| 3634 | |
| 3635 | // Tests the basic case of closing an idle socket in a higher layered pool when |
| 3636 | // a new request is issued and the lower layer pool is stalled. |
| 3637 | TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketsHeldByLayeredPoolWhenNeeded) { |
| 3638 | CreatePool(1, 1); |
| 3639 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 3640 | |
| 3641 | MockLayeredPool mock_layered_pool(pool_.get(), "foo"); |
| 3642 | EXPECT_EQ(OK, mock_layered_pool.RequestSocket(pool_.get())); |
| 3643 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 3644 | .WillOnce(Invoke(&mock_layered_pool, |
| 3645 | &MockLayeredPool::ReleaseOneConnection)); |
| 3646 | ClientSocketHandle handle; |
| 3647 | TestCompletionCallback callback; |
| 3648 | EXPECT_EQ(ERR_IO_PENDING, handle.Init("a", |
| 3649 | params_, |
| 3650 | kDefaultPriority, |
| 3651 | callback.callback(), |
| 3652 | pool_.get(), |
| 3653 | BoundNetLog())); |
| 3654 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 3655 | } |
| 3656 | |
| 3657 | // Same as above, but the idle socket is in the same group as the stalled |
| 3658 | // socket, and closes the only other request in its group when closing requests |
| 3659 | // in higher layered pools. This generally shouldn't happen, but it may be |
| 3660 | // possible if a higher level pool issues a request and the request is |
| 3661 | // subsequently cancelled. Even if it's not possible, best not to crash. |
| 3662 | TEST_F(ClientSocketPoolBaseTest, |
| 3663 | CloseIdleSocketsHeldByLayeredPoolWhenNeededSameGroup) { |
| 3664 | CreatePool(2, 2); |
| 3665 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 3666 | |
| 3667 | // Need a socket in another group for the pool to be stalled (If a group |
| 3668 | // has the maximum number of connections already, it's not stalled). |
| 3669 | ClientSocketHandle handle1; |
| 3670 | TestCompletionCallback callback1; |
| 3671 | EXPECT_EQ(OK, handle1.Init("group1", |
| 3672 | params_, |
| 3673 | kDefaultPriority, |
| 3674 | callback1.callback(), |
| 3675 | pool_.get(), |
| 3676 | BoundNetLog())); |
| 3677 | |
| 3678 | MockLayeredPool mock_layered_pool(pool_.get(), "group2"); |
| 3679 | EXPECT_EQ(OK, mock_layered_pool.RequestSocket(pool_.get())); |
| 3680 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 3681 | .WillOnce(Invoke(&mock_layered_pool, |
| 3682 | &MockLayeredPool::ReleaseOneConnection)); |
| 3683 | ClientSocketHandle handle; |
| 3684 | TestCompletionCallback callback2; |
| 3685 | EXPECT_EQ(ERR_IO_PENDING, handle.Init("group2", |
| 3686 | params_, |
| 3687 | kDefaultPriority, |
| 3688 | callback2.callback(), |
| 3689 | pool_.get(), |
| 3690 | BoundNetLog())); |
| 3691 | EXPECT_EQ(OK, callback2.WaitForResult()); |
| 3692 | } |
| 3693 | |
| 3694 | // Tests the case when an idle socket can be closed when a new request is |
| 3695 | // issued, and the new request belongs to a group that was previously stalled. |
| 3696 | TEST_F(ClientSocketPoolBaseTest, |
| 3697 | CloseIdleSocketsHeldByLayeredPoolInSameGroupWhenNeeded) { |
| 3698 | CreatePool(2, 2); |
| 3699 | std::list<TestConnectJob::JobType> job_types; |
| 3700 | job_types.push_back(TestConnectJob::kMockJob); |
| 3701 | job_types.push_back(TestConnectJob::kMockJob); |
| 3702 | job_types.push_back(TestConnectJob::kMockJob); |
| 3703 | job_types.push_back(TestConnectJob::kMockJob); |
| 3704 | connect_job_factory_->set_job_types(&job_types); |
| 3705 | |
| 3706 | ClientSocketHandle handle1; |
| 3707 | TestCompletionCallback callback1; |
| 3708 | EXPECT_EQ(OK, handle1.Init("group1", |
| 3709 | params_, |
| 3710 | kDefaultPriority, |
| 3711 | callback1.callback(), |
| 3712 | pool_.get(), |
| 3713 | BoundNetLog())); |
| 3714 | |
| 3715 | MockLayeredPool mock_layered_pool(pool_.get(), "group2"); |
| 3716 | EXPECT_EQ(OK, mock_layered_pool.RequestSocket(pool_.get())); |
| 3717 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 3718 | .WillRepeatedly(Invoke(&mock_layered_pool, |
| 3719 | &MockLayeredPool::ReleaseOneConnection)); |
| 3720 | mock_layered_pool.set_can_release_connection(false); |
| 3721 | |
| 3722 | // The third request is made when the socket pool is in a stalled state. |
| 3723 | ClientSocketHandle handle3; |
| 3724 | TestCompletionCallback callback3; |
| 3725 | EXPECT_EQ(ERR_IO_PENDING, handle3.Init("group3", |
| 3726 | params_, |
| 3727 | kDefaultPriority, |
| 3728 | callback3.callback(), |
| 3729 | pool_.get(), |
| 3730 | BoundNetLog())); |
| 3731 | |
| 3732 | base::RunLoop().RunUntilIdle(); |
| 3733 | EXPECT_FALSE(callback3.have_result()); |
| 3734 | |
| 3735 | // The fourth request is made when the pool is no longer stalled. The third |
| 3736 | // request should be serviced first, since it was issued first and has the |
| 3737 | // same priority. |
| 3738 | mock_layered_pool.set_can_release_connection(true); |
| 3739 | ClientSocketHandle handle4; |
| 3740 | TestCompletionCallback callback4; |
| 3741 | EXPECT_EQ(ERR_IO_PENDING, handle4.Init("group3", |
| 3742 | params_, |
| 3743 | kDefaultPriority, |
| 3744 | callback4.callback(), |
| 3745 | pool_.get(), |
| 3746 | BoundNetLog())); |
| 3747 | EXPECT_EQ(OK, callback3.WaitForResult()); |
| 3748 | EXPECT_FALSE(callback4.have_result()); |
| 3749 | |
| 3750 | // Closing a handle should free up another socket slot. |
| 3751 | handle1.Reset(); |
| 3752 | EXPECT_EQ(OK, callback4.WaitForResult()); |
| 3753 | } |
| 3754 | |
| 3755 | // Tests the case when an idle socket can be closed when a new request is |
| 3756 | // issued, and the new request belongs to a group that was previously stalled. |
| 3757 | // |
| 3758 | // The two differences from the above test are that the stalled requests are not |
| 3759 | // in the same group as the layered pool's request, and the the fourth request |
| 3760 | // has a higher priority than the third one, so gets a socket first. |
| 3761 | TEST_F(ClientSocketPoolBaseTest, |
| 3762 | CloseIdleSocketsHeldByLayeredPoolInSameGroupWhenNeeded2) { |
| 3763 | CreatePool(2, 2); |
| 3764 | std::list<TestConnectJob::JobType> job_types; |
| 3765 | job_types.push_back(TestConnectJob::kMockJob); |
| 3766 | job_types.push_back(TestConnectJob::kMockJob); |
| 3767 | job_types.push_back(TestConnectJob::kMockJob); |
| 3768 | job_types.push_back(TestConnectJob::kMockJob); |
| 3769 | connect_job_factory_->set_job_types(&job_types); |
| 3770 | |
| 3771 | ClientSocketHandle handle1; |
| 3772 | TestCompletionCallback callback1; |
| 3773 | EXPECT_EQ(OK, handle1.Init("group1", |
| 3774 | params_, |
| 3775 | kDefaultPriority, |
| 3776 | callback1.callback(), |
| 3777 | pool_.get(), |
| 3778 | BoundNetLog())); |
| 3779 | |
| 3780 | MockLayeredPool mock_layered_pool(pool_.get(), "group2"); |
| 3781 | EXPECT_EQ(OK, mock_layered_pool.RequestSocket(pool_.get())); |
| 3782 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 3783 | .WillRepeatedly(Invoke(&mock_layered_pool, |
| 3784 | &MockLayeredPool::ReleaseOneConnection)); |
| 3785 | mock_layered_pool.set_can_release_connection(false); |
| 3786 | |
| 3787 | // The third request is made when the socket pool is in a stalled state. |
| 3788 | ClientSocketHandle handle3; |
| 3789 | TestCompletionCallback callback3; |
| 3790 | EXPECT_EQ(ERR_IO_PENDING, handle3.Init("group3", |
| 3791 | params_, |
| 3792 | MEDIUM, |
| 3793 | callback3.callback(), |
| 3794 | pool_.get(), |
| 3795 | BoundNetLog())); |
| 3796 | |
| 3797 | base::RunLoop().RunUntilIdle(); |
| 3798 | EXPECT_FALSE(callback3.have_result()); |
| 3799 | |
| 3800 | // The fourth request is made when the pool is no longer stalled. This |
| 3801 | // request has a higher priority than the third request, so is serviced first. |
| 3802 | mock_layered_pool.set_can_release_connection(true); |
| 3803 | ClientSocketHandle handle4; |
| 3804 | TestCompletionCallback callback4; |
| 3805 | EXPECT_EQ(ERR_IO_PENDING, handle4.Init("group3", |
| 3806 | params_, |
| 3807 | HIGHEST, |
| 3808 | callback4.callback(), |
| 3809 | pool_.get(), |
| 3810 | BoundNetLog())); |
| 3811 | EXPECT_EQ(OK, callback4.WaitForResult()); |
| 3812 | EXPECT_FALSE(callback3.have_result()); |
| 3813 | |
| 3814 | // Closing a handle should free up another socket slot. |
| 3815 | handle1.Reset(); |
| 3816 | EXPECT_EQ(OK, callback3.WaitForResult()); |
| 3817 | } |
| 3818 | |
| 3819 | TEST_F(ClientSocketPoolBaseTest, |
| 3820 | CloseMultipleIdleSocketsHeldByLayeredPoolWhenNeeded) { |
| 3821 | CreatePool(1, 1); |
| 3822 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 3823 | |
| 3824 | MockLayeredPool mock_layered_pool1(pool_.get(), "foo"); |
| 3825 | EXPECT_EQ(OK, mock_layered_pool1.RequestSocket(pool_.get())); |
| 3826 | EXPECT_CALL(mock_layered_pool1, CloseOneIdleConnection()) |
| 3827 | .WillRepeatedly(Invoke(&mock_layered_pool1, |
| 3828 | &MockLayeredPool::ReleaseOneConnection)); |
| 3829 | MockLayeredPool mock_layered_pool2(pool_.get(), "bar"); |
| 3830 | EXPECT_EQ(OK, mock_layered_pool2.RequestSocketWithoutLimits(pool_.get())); |
| 3831 | EXPECT_CALL(mock_layered_pool2, CloseOneIdleConnection()) |
| 3832 | .WillRepeatedly(Invoke(&mock_layered_pool2, |
| 3833 | &MockLayeredPool::ReleaseOneConnection)); |
| 3834 | ClientSocketHandle handle; |
| 3835 | TestCompletionCallback callback; |
| 3836 | EXPECT_EQ(ERR_IO_PENDING, handle.Init("a", |
| 3837 | params_, |
| 3838 | kDefaultPriority, |
| 3839 | callback.callback(), |
| 3840 | pool_.get(), |
| 3841 | BoundNetLog())); |
| 3842 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 3843 | } |
| 3844 | |
| 3845 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 3846 | } // namespace |
| 3847 | |
| 3848 | } // namespace net |