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