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