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