[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 | |
tbansal | f82cc8e | 2015-10-14 20:05:49 | [diff] [blame] | 7 | #include <stdint.h> |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 8 | #include <utility> |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 9 | #include <vector> |
| 10 | |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 11 | #include "base/bind.h" |
| 12 | #include "base/bind_helpers.h" |
[email protected] | 2041cf34 | 2010-02-19 03:15:59 | [diff] [blame] | 13 | #include "base/callback.h" |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 14 | #include "base/location.h" |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 15 | #include "base/logging.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 16 | #include "base/memory/ref_counted.h" |
[email protected] | 6ea7b15 | 2011-12-21 21:21:13 | [diff] [blame] | 17 | #include "base/memory/weak_ptr.h" |
Alexander Timin | 4f9c35c | 2018-11-01 20:15:20 | [diff] [blame] | 18 | #include "base/message_loop/message_loop.h" |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 19 | #include "base/run_loop.h" |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 20 | #include "base/single_thread_task_runner.h" |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 21 | #include "base/stl_util.h" |
[email protected] | fc9be580 | 2013-06-11 10:56:51 | [diff] [blame] | 22 | #include "base/strings/string_number_conversions.h" |
[email protected] | 18b57741 | 2013-07-18 04:19:15 | [diff] [blame] | 23 | #include "base/strings/stringprintf.h" |
[email protected] | f214f879 | 2011-01-01 02:17:08 | [diff] [blame] | 24 | #include "base/threading/platform_thread.h" |
gab | f767595f | 2016-05-11 18:50:35 | [diff] [blame] | 25 | #include "base/threading/thread_task_runner_handle.h" |
[email protected] | f3a1c64 | 2011-07-12 19:15:03 | [diff] [blame] | 26 | #include "base/values.h" |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 27 | #include "net/base/load_timing_info.h" |
[email protected] | b258e079 | 2013-01-12 07:11:59 | [diff] [blame] | 28 | #include "net/base/load_timing_info_test_util.h" |
[email protected] | d8eb8424 | 2010-09-25 02:25:06 | [diff] [blame] | 29 | #include "net/base/net_errors.h" |
[email protected] | ac790b4 | 2009-12-02 04:31:31 | [diff] [blame] | 30 | #include "net/base/request_priority.h" |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 31 | #include "net/base/test_completion_callback.h" |
[email protected] | 277d594 | 2010-08-11 21:02:35 | [diff] [blame] | 32 | #include "net/http/http_response_headers.h" |
eroman | 87c53d6 | 2015-04-02 06:51:07 | [diff] [blame] | 33 | #include "net/log/net_log.h" |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 34 | #include "net/log/net_log_event_type.h" |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 35 | #include "net/log/net_log_source.h" |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 36 | #include "net/log/net_log_source_type.h" |
mmenke | 16a7cbdd | 2015-04-24 23:00:56 | [diff] [blame] | 37 | #include "net/log/test_net_log.h" |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 38 | #include "net/log/test_net_log_entry.h" |
| 39 | #include "net/log/test_net_log_util.h" |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 40 | #include "net/socket/client_socket_factory.h" |
| 41 | #include "net/socket/client_socket_handle.h" |
tfarina | 5dd13c2 | 2016-11-16 12:08:26 | [diff] [blame] | 42 | #include "net/socket/datagram_client_socket.h" |
tbansal | ca83c00 | 2016-04-28 20:56:28 | [diff] [blame] | 43 | #include "net/socket/socket_performance_watcher.h" |
Paul Jensen | 8d6f87ec | 2018-01-13 00:46:54 | [diff] [blame] | 44 | #include "net/socket/socket_tag.h" |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 45 | #include "net/socket/socket_test_util.h" |
[email protected] | 18ccfdb | 2013-08-15 00:13:44 | [diff] [blame] | 46 | #include "net/socket/ssl_client_socket.h" |
[email protected] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame] | 47 | #include "net/socket/stream_socket.h" |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 48 | #include "net/test/gtest_util.h" |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 49 | #include "net/test/test_with_scoped_task_environment.h" |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 50 | #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 51 | #include "testing/gmock/include/gmock/gmock.h" |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 52 | #include "testing/gtest/include/gtest/gtest.h" |
| 53 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 54 | using net::test::IsError; |
| 55 | using net::test::IsOk; |
| 56 | |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 57 | using ::testing::Invoke; |
| 58 | using ::testing::Return; |
| 59 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 60 | namespace net { |
| 61 | |
| 62 | namespace { |
| 63 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 64 | const int kDefaultMaxSockets = 4; |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 65 | const int kDefaultMaxSocketsPerGroup = 2; |
Tarun Bansal | a763509 | 2019-02-20 10:00:59 | [diff] [blame] | 66 | constexpr base::TimeDelta kUnusedIdleSocketTimeout = |
| 67 | base::TimeDelta::FromSeconds(10); |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 68 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 69 | ClientSocketPool::GroupId TestGroupId(const std::string& host, |
| 70 | int port = 80, |
| 71 | ClientSocketPool::SocketType socket_type = |
| 72 | ClientSocketPool::SocketType::kHttp, |
| 73 | bool privacy_mode = false) { |
| 74 | return ClientSocketPool::GroupId(HostPortPair(host, port), socket_type, |
| 75 | privacy_mode); |
| 76 | } |
| 77 | |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 78 | // Make sure |handle| sets load times correctly when it has been assigned a |
| 79 | // reused socket. |
| 80 | void TestLoadTimingInfoConnectedReused(const ClientSocketHandle& handle) { |
| 81 | LoadTimingInfo load_timing_info; |
| 82 | // Only pass true in as |is_reused|, as in general, HttpStream types should |
| 83 | // have stricter concepts of reuse than socket pools. |
| 84 | EXPECT_TRUE(handle.GetLoadTimingInfo(true, &load_timing_info)); |
| 85 | |
| 86 | EXPECT_EQ(true, load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 87 | EXPECT_NE(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 88 | |
[email protected] | b258e079 | 2013-01-12 07:11:59 | [diff] [blame] | 89 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 90 | ExpectLoadTimingHasOnlyConnectionTimes(load_timing_info); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | // Make sure |handle| sets load times correctly when it has been assigned a |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 94 | // fresh socket. Also runs TestLoadTimingInfoConnectedReused, since the owner |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 95 | // of a connection where |is_reused| is false may consider the connection |
| 96 | // reused. |
| 97 | void TestLoadTimingInfoConnectedNotReused(const ClientSocketHandle& handle) { |
| 98 | EXPECT_FALSE(handle.is_reused()); |
| 99 | |
| 100 | LoadTimingInfo load_timing_info; |
| 101 | EXPECT_TRUE(handle.GetLoadTimingInfo(false, &load_timing_info)); |
| 102 | |
| 103 | EXPECT_FALSE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 104 | EXPECT_NE(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 105 | |
[email protected] | b258e079 | 2013-01-12 07:11:59 | [diff] [blame] | 106 | ExpectConnectTimingHasTimes(load_timing_info.connect_timing, |
| 107 | CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY); |
| 108 | ExpectLoadTimingHasOnlyConnectionTimes(load_timing_info); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 109 | |
| 110 | TestLoadTimingInfoConnectedReused(handle); |
| 111 | } |
| 112 | |
| 113 | // Make sure |handle| sets load times correctly, in the case that it does not |
| 114 | // currently have a socket. |
| 115 | void TestLoadTimingInfoNotConnected(const ClientSocketHandle& handle) { |
| 116 | // Should only be set to true once a socket is assigned, if at all. |
| 117 | EXPECT_FALSE(handle.is_reused()); |
| 118 | |
| 119 | LoadTimingInfo load_timing_info; |
| 120 | EXPECT_FALSE(handle.GetLoadTimingInfo(false, &load_timing_info)); |
| 121 | |
| 122 | EXPECT_FALSE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 123 | EXPECT_EQ(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 124 | |
[email protected] | b258e079 | 2013-01-12 07:11:59 | [diff] [blame] | 125 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 126 | ExpectLoadTimingHasOnlyConnectionTimes(load_timing_info); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 127 | } |
| 128 | |
[email protected] | df4b4ef | 2010-07-12 18:25:21 | [diff] [blame] | 129 | class TestSocketParams : public base::RefCounted<TestSocketParams> { |
[email protected] | 5acdce1 | 2011-03-30 13:00:20 | [diff] [blame] | 130 | public: |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 131 | explicit TestSocketParams() = default; |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 132 | |
[email protected] | df4b4ef | 2010-07-12 18:25:21 | [diff] [blame] | 133 | private: |
| 134 | friend class base::RefCounted<TestSocketParams>; |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 135 | ~TestSocketParams() = default; |
[email protected] | df4b4ef | 2010-07-12 18:25:21 | [diff] [blame] | 136 | }; |
[email protected] | 7fc5b09a | 2010-02-27 00:07:38 | [diff] [blame] | 137 | typedef ClientSocketPoolBase<TestSocketParams> TestClientSocketPoolBase; |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 138 | |
[email protected] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame] | 139 | class MockClientSocket : public StreamSocket { |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 140 | public: |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 141 | explicit MockClientSocket(net::NetLog* net_log) |
| 142 | : connected_(false), |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 143 | has_unread_data_(false), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 144 | net_log_(NetLogWithSource::Make(net_log, NetLogSourceType::SOCKET)), |
Charlie Harrison | 3e4c062 | 2018-05-13 15:44:30 | [diff] [blame] | 145 | was_used_to_convey_data_(false) {} |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 146 | |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 147 | // Sets whether the socket has unread data. If true, the next call to Read() |
| 148 | // will return 1 byte and IsConnectedAndIdle() will return false. |
| 149 | void set_has_unread_data(bool has_unread_data) { |
| 150 | has_unread_data_ = has_unread_data; |
| 151 | } |
| 152 | |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 153 | // Socket implementation. |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 154 | int Read(IOBuffer* /* buf */, |
| 155 | int len, |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 156 | CompletionOnceCallback /* callback */) override { |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 157 | if (has_unread_data_ && len > 0) { |
| 158 | has_unread_data_ = false; |
| 159 | was_used_to_convey_data_ = true; |
| 160 | return 1; |
| 161 | } |
[email protected] | e86df8dc | 2013-03-30 13:18:28 | [diff] [blame] | 162 | return ERR_UNEXPECTED; |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 163 | } |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 164 | |
[email protected] | a2b2cfc | 2017-12-06 09:06:08 | [diff] [blame] | 165 | int Write( |
| 166 | IOBuffer* /* buf */, |
| 167 | int len, |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 168 | CompletionOnceCallback /* callback */, |
[email protected] | a2b2cfc | 2017-12-06 09:06:08 | [diff] [blame] | 169 | const NetworkTrafficAnnotationTag& /*traffic_annotation*/) override { |
[email protected] | 0f873e8 | 2010-09-02 16:09:01 | [diff] [blame] | 170 | was_used_to_convey_data_ = true; |
| 171 | return len; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 172 | } |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 173 | int SetReceiveBufferSize(int32_t size) override { return OK; } |
| 174 | int SetSendBufferSize(int32_t size) override { return OK; } |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 175 | |
[email protected] | dbf036f | 2011-12-06 23:33:24 | [diff] [blame] | 176 | // StreamSocket implementation. |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 177 | int Connect(CompletionOnceCallback callback) override { |
[email protected] | dbf036f | 2011-12-06 23:33:24 | [diff] [blame] | 178 | connected_ = true; |
| 179 | return OK; |
| 180 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 181 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 182 | void Disconnect() override { connected_ = false; } |
| 183 | bool IsConnected() const override { return connected_; } |
| 184 | bool IsConnectedAndIdle() const override { |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 185 | return connected_ && !has_unread_data_; |
| 186 | } |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 187 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 188 | int GetPeerAddress(IPEndPoint* /* address */) const override { |
[email protected] | 9f864b3 | 2010-01-20 15:01:16 | [diff] [blame] | 189 | return ERR_UNEXPECTED; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 190 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 191 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 192 | int GetLocalAddress(IPEndPoint* /* address */) const override { |
[email protected] | e7f74da | 2011-04-19 23:49:35 | [diff] [blame] | 193 | return ERR_UNEXPECTED; |
| 194 | } |
| 195 | |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 196 | const NetLogWithSource& NetLog() const override { return net_log_; } |
[email protected] | a2006ece | 2010-04-23 16:44:02 | [diff] [blame] | 197 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 198 | bool WasEverUsed() const override { return was_used_to_convey_data_; } |
tfarina | 2846404c | 2016-12-25 14:31:37 | [diff] [blame] | 199 | bool WasAlpnNegotiated() const override { return false; } |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 200 | NextProto GetNegotiatedProtocol() const override { return kProtoUnknown; } |
| 201 | bool GetSSLInfo(SSLInfo* ssl_info) override { return false; } |
ttuttle | 23fdb7b | 2015-05-15 01:28:03 | [diff] [blame] | 202 | void GetConnectionAttempts(ConnectionAttempts* out) const override { |
| 203 | out->clear(); |
| 204 | } |
| 205 | void ClearConnectionAttempts() override {} |
| 206 | void AddConnectionAttempts(const ConnectionAttempts& attempts) override {} |
tbansal | f82cc8e | 2015-10-14 20:05:49 | [diff] [blame] | 207 | int64_t GetTotalReceivedBytes() const override { |
| 208 | NOTIMPLEMENTED(); |
| 209 | return 0; |
| 210 | } |
Paul Jensen | 0f49dec | 2017-12-12 23:39:58 | [diff] [blame] | 211 | void ApplySocketTag(const SocketTag& tag) override {} |
[email protected] | 9b5614a | 2010-08-25 20:29:45 | [diff] [blame] | 212 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 213 | private: |
| 214 | bool connected_; |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 215 | bool has_unread_data_; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 216 | NetLogWithSource net_log_; |
[email protected] | 0f873e8 | 2010-09-02 16:09:01 | [diff] [blame] | 217 | bool was_used_to_convey_data_; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 218 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 219 | DISALLOW_COPY_AND_ASSIGN(MockClientSocket); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 220 | }; |
| 221 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 222 | class TestConnectJob; |
| 223 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 224 | class MockClientSocketFactory : public ClientSocketFactory { |
| 225 | public: |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 226 | MockClientSocketFactory() : allocation_count_(0) {} |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 227 | |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 228 | std::unique_ptr<DatagramClientSocket> CreateDatagramClientSocket( |
[email protected] | 5370c01 | 2011-06-29 03:47:04 | [diff] [blame] | 229 | DatagramSocket::BindType bind_type, |
[email protected] | 98b0e58 | 2011-06-22 14:31:41 | [diff] [blame] | 230 | NetLog* net_log, |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 231 | const NetLogSource& source) override { |
[email protected] | 98b0e58 | 2011-06-22 14:31:41 | [diff] [blame] | 232 | NOTREACHED(); |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 233 | return std::unique_ptr<DatagramClientSocket>(); |
[email protected] | 98b0e58 | 2011-06-22 14:31:41 | [diff] [blame] | 234 | } |
| 235 | |
Helen Li | d5bb922 | 2018-04-12 15:33:09 | [diff] [blame] | 236 | std::unique_ptr<TransportClientSocket> CreateTransportClientSocket( |
[email protected] | 0a0b768 | 2010-08-25 17:08:07 | [diff] [blame] | 237 | const AddressList& addresses, |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 238 | std::unique_ptr< |
| 239 | SocketPerformanceWatcher> /* socket_performance_watcher */, |
[email protected] | 0a0b768 | 2010-08-25 17:08:07 | [diff] [blame] | 240 | NetLog* /* net_log */, |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 241 | const NetLogSource& /*source*/) override { |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 242 | allocation_count_++; |
Helen Li | d5bb922 | 2018-04-12 15:33:09 | [diff] [blame] | 243 | return nullptr; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 244 | } |
| 245 | |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 246 | std::unique_ptr<SSLClientSocket> CreateSSLClientSocket( |
Matt Menke | 841fc41 | 2019-03-05 23:20:12 | [diff] [blame] | 247 | std::unique_ptr<StreamSocket> stream_socket, |
[email protected] | 4f4de7e6 | 2010-11-12 19:55:27 | [diff] [blame] | 248 | const HostPortPair& host_and_port, |
[email protected] | 7ab5bbd1 | 2010-10-19 13:33:21 | [diff] [blame] | 249 | const SSLConfig& ssl_config, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 250 | const SSLClientSocketContext& context) override { |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 251 | NOTIMPLEMENTED(); |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 252 | return std::unique_ptr<SSLClientSocket>(); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 253 | } |
Matt Menke | fd95692 | 2019-02-04 23:44:03 | [diff] [blame] | 254 | |
Matt Menke | 52cd95a | 2019-02-08 06:16:27 | [diff] [blame] | 255 | std::unique_ptr<ProxyClientSocket> CreateProxyClientSocket( |
| 256 | std::unique_ptr<StreamSocket> stream_socket, |
| 257 | const std::string& user_agent, |
| 258 | const HostPortPair& endpoint, |
| 259 | const ProxyServer& proxy_server, |
| 260 | HttpAuthController* http_auth_controller, |
| 261 | bool tunnel, |
| 262 | bool using_spdy, |
| 263 | NextProto negotiated_protocol, |
| 264 | ProxyDelegate* proxy_delegate, |
| 265 | bool is_https_proxy, |
| 266 | const NetworkTrafficAnnotationTag& traffic_annotation) override { |
| 267 | NOTIMPLEMENTED(); |
| 268 | return nullptr; |
| 269 | } |
| 270 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 271 | void WaitForSignal(TestConnectJob* job) { waiting_jobs_.push_back(job); } |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 272 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 273 | void SignalJobs(); |
| 274 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 275 | void SignalJob(size_t job); |
| 276 | |
| 277 | void SetJobLoadState(size_t job, LoadState load_state); |
| 278 | |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 279 | // Sets the HasConnectionEstablished value of the specified job to true, |
| 280 | // without invoking the callback. |
| 281 | void SetJobHasEstablishedConnection(size_t job); |
| 282 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 283 | int allocation_count() const { return allocation_count_; } |
| 284 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 285 | private: |
| 286 | int allocation_count_; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 287 | std::vector<TestConnectJob*> waiting_jobs_; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 288 | }; |
| 289 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 290 | class TestConnectJob : public ConnectJob { |
| 291 | public: |
| 292 | enum JobType { |
| 293 | kMockJob, |
| 294 | kMockFailingJob, |
| 295 | kMockPendingJob, |
| 296 | kMockPendingFailingJob, |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 297 | kMockWaitingJob, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 298 | |
| 299 | // Certificate errors return a socket in addition to an error code. |
| 300 | kMockCertErrorJob, |
| 301 | kMockPendingCertErrorJob, |
| 302 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 303 | kMockAdditionalErrorStateJob, |
| 304 | kMockPendingAdditionalErrorStateJob, |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 305 | kMockUnreadDataJob, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 306 | |
| 307 | kMockAuthChallengeOnceJob, |
| 308 | kMockAuthChallengeTwiceJob, |
| 309 | kMockAuthChallengeOnceFailingJob, |
| 310 | kMockAuthChallengeTwiceFailingJob, |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 311 | }; |
| 312 | |
[email protected] | 994d493 | 2010-07-12 17:55:13 | [diff] [blame] | 313 | // The kMockPendingJob uses a slight delay before allowing the connect |
| 314 | // to complete. |
| 315 | static const int kPendingConnectDelay = 2; |
| 316 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 317 | TestConnectJob(JobType job_type, |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 318 | const TestClientSocketPoolBase::Request& request, |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 319 | base::TimeDelta timeout_duration, |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 320 | const CommonConnectJobParams* common_connect_job_params, |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 321 | ConnectJob::Delegate* delegate, |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 322 | MockClientSocketFactory* client_socket_factory) |
Matt Menke | 1a6c92d | 2019-02-23 00:25:38 | [diff] [blame] | 323 | : ConnectJob(request.priority(), |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 324 | request.socket_tag(), |
Matt Menke | 1a6c92d | 2019-02-23 00:25:38 | [diff] [blame] | 325 | timeout_duration, |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 326 | common_connect_job_params, |
Matt Menke | 1a6c92d | 2019-02-23 00:25:38 | [diff] [blame] | 327 | delegate, |
| 328 | nullptr /* net_log */, |
| 329 | NetLogSourceType::TRANSPORT_CONNECT_JOB, |
| 330 | NetLogEventType::TRANSPORT_CONNECT_JOB_CONNECT), |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 331 | job_type_(job_type), |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 332 | client_socket_factory_(client_socket_factory), |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 333 | load_state_(LOAD_STATE_IDLE), |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 334 | has_established_connection_(false), |
[email protected] | d5492c5 | 2013-11-10 20:44:39 | [diff] [blame] | 335 | store_additional_error_state_(false), |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 336 | weak_factory_(this) {} |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 337 | |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 338 | void Signal() { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 339 | DoConnect(waiting_success_, true /* async */, false /* recoverable */); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 340 | } |
| 341 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 342 | void set_load_state(LoadState load_state) { load_state_ = load_state; } |
| 343 | |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 344 | void set_has_established_connection() { |
| 345 | DCHECK(!has_established_connection_); |
| 346 | has_established_connection_ = true; |
| 347 | } |
| 348 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 349 | // From ConnectJob: |
| 350 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 351 | LoadState GetLoadState() const override { return load_state_; } |
[email protected] | 4645135 | 2009-09-01 14:54:21 | [diff] [blame] | 352 | |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 353 | bool HasEstablishedConnection() const override { |
| 354 | return has_established_connection_; |
| 355 | } |
| 356 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 357 | void GetAdditionalErrorState(ClientSocketHandle* handle) override { |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 358 | if (store_additional_error_state_) { |
| 359 | // Set all of the additional error state fields in some way. |
| 360 | handle->set_is_ssl_error(true); |
[email protected] | 8b49869 | 2010-07-16 17:11:43 | [diff] [blame] | 361 | HttpResponseInfo info; |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 362 | info.headers = new HttpResponseHeaders(std::string()); |
[email protected] | 8b49869 | 2010-07-16 17:11:43 | [diff] [blame] | 363 | handle->set_ssl_error_response_info(info); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 364 | } |
| 365 | } |
| 366 | |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 367 | private: |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 368 | // From ConnectJob: |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 369 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 370 | int ConnectInternal() override { |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 371 | AddressList ignored; |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 372 | client_socket_factory_->CreateTransportClientSocket( |
| 373 | ignored, nullptr, nullptr, NetLogSource()); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 374 | switch (job_type_) { |
| 375 | case kMockJob: |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 376 | return DoConnect(true /* successful */, false /* sync */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 377 | false /* cert_error */); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 378 | case kMockFailingJob: |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 379 | return DoConnect(false /* error */, false /* sync */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 380 | false /* cert_error */); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 381 | case kMockPendingJob: |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 382 | set_load_state(LOAD_STATE_CONNECTING); |
[email protected] | 6b17538 | 2009-10-13 06:47:47 | [diff] [blame] | 383 | |
| 384 | // Depending on execution timings, posting a delayed task can result |
| 385 | // in the task getting executed the at the earliest possible |
| 386 | // opportunity or only after returning once from the message loop and |
| 387 | // then a second call into the message loop. In order to make behavior |
| 388 | // more deterministic, we change the default delay to 2ms. This should |
| 389 | // always require us to wait for the second call into the message loop. |
| 390 | // |
| 391 | // N.B. The correct fix for this and similar timing problems is to |
| 392 | // abstract time for the purpose of unittests. Unfortunately, we have |
| 393 | // a lot of third-party components that directly call the various |
| 394 | // time functions, so this change would be rather invasive. |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 395 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 396 | FROM_HERE, |
kylechar | f4fe517 | 2019-02-15 18:53:49 | [diff] [blame] | 397 | base::BindOnce(base::IgnoreResult(&TestConnectJob::DoConnect), |
| 398 | weak_factory_.GetWeakPtr(), true /* successful */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 399 | true /* async */, false /* cert_error */), |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 400 | base::TimeDelta::FromMilliseconds(kPendingConnectDelay)); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 401 | return ERR_IO_PENDING; |
| 402 | case kMockPendingFailingJob: |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 403 | set_load_state(LOAD_STATE_CONNECTING); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 404 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 405 | FROM_HERE, |
kylechar | f4fe517 | 2019-02-15 18:53:49 | [diff] [blame] | 406 | base::BindOnce(base::IgnoreResult(&TestConnectJob::DoConnect), |
| 407 | weak_factory_.GetWeakPtr(), false /* error */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 408 | true /* async */, false /* cert_error */), |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 409 | base::TimeDelta::FromMilliseconds(2)); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 410 | return ERR_IO_PENDING; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 411 | case kMockWaitingJob: |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 412 | set_load_state(LOAD_STATE_CONNECTING); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 413 | client_socket_factory_->WaitForSignal(this); |
| 414 | waiting_success_ = true; |
| 415 | return ERR_IO_PENDING; |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 416 | case kMockCertErrorJob: |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 417 | return DoConnect(false /* error */, false /* sync */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 418 | true /* cert_error */); |
| 419 | case kMockPendingCertErrorJob: |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 420 | set_load_state(LOAD_STATE_CONNECTING); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 421 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 422 | FROM_HERE, |
kylechar | f4fe517 | 2019-02-15 18:53:49 | [diff] [blame] | 423 | base::BindOnce(base::IgnoreResult(&TestConnectJob::DoConnect), |
| 424 | weak_factory_.GetWeakPtr(), false /* error */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 425 | true /* async */, true /* cert_error */), |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 426 | base::TimeDelta::FromMilliseconds(2)); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 427 | return ERR_IO_PENDING; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 428 | case kMockAdditionalErrorStateJob: |
| 429 | store_additional_error_state_ = true; |
| 430 | return DoConnect(false /* error */, false /* sync */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 431 | false /* cert_error */); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 432 | case kMockPendingAdditionalErrorStateJob: |
| 433 | set_load_state(LOAD_STATE_CONNECTING); |
| 434 | store_additional_error_state_ = true; |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 435 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 436 | FROM_HERE, |
kylechar | f4fe517 | 2019-02-15 18:53:49 | [diff] [blame] | 437 | base::BindOnce(base::IgnoreResult(&TestConnectJob::DoConnect), |
| 438 | weak_factory_.GetWeakPtr(), false /* error */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 439 | true /* async */, false /* cert_error */), |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 440 | base::TimeDelta::FromMilliseconds(2)); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 441 | return ERR_IO_PENDING; |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 442 | case kMockUnreadDataJob: { |
| 443 | int ret = DoConnect(true /* successful */, false /* sync */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 444 | false /* cert_error */); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 445 | static_cast<MockClientSocket*>(socket())->set_has_unread_data(true); |
| 446 | return ret; |
| 447 | } |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 448 | case kMockAuthChallengeOnceJob: |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 449 | set_load_state(LOAD_STATE_CONNECTING); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 450 | DoAdvanceAuthChallenge(1, true /* succeed_after_last_challenge */); |
| 451 | return ERR_IO_PENDING; |
| 452 | case kMockAuthChallengeTwiceJob: |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 453 | set_load_state(LOAD_STATE_CONNECTING); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 454 | DoAdvanceAuthChallenge(2, true /* succeed_after_last_challenge */); |
| 455 | return ERR_IO_PENDING; |
| 456 | case kMockAuthChallengeOnceFailingJob: |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 457 | set_load_state(LOAD_STATE_CONNECTING); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 458 | DoAdvanceAuthChallenge(1, false /* succeed_after_last_challenge */); |
| 459 | return ERR_IO_PENDING; |
| 460 | case kMockAuthChallengeTwiceFailingJob: |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 461 | set_load_state(LOAD_STATE_CONNECTING); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 462 | DoAdvanceAuthChallenge(2, false /* succeed_after_last_challenge */); |
| 463 | return ERR_IO_PENDING; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 464 | default: |
| 465 | NOTREACHED(); |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 466 | SetSocket(std::unique_ptr<StreamSocket>()); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 467 | return ERR_FAILED; |
| 468 | } |
| 469 | } |
| 470 | |
Lily Chen | 02ef29a | 2018-11-30 16:31:43 | [diff] [blame] | 471 | void ChangePriorityInternal(RequestPriority priority) override {} |
| 472 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 473 | int DoConnect(bool succeed, bool was_async, bool cert_error) { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 474 | int result = OK; |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 475 | has_established_connection_ = true; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 476 | if (succeed) { |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 477 | SetSocket(std::make_unique<MockClientSocket>(net_log().net_log())); |
Bence Béky | bdbb0e7 | 2018-08-07 21:42:59 | [diff] [blame] | 478 | socket()->Connect(CompletionOnceCallback()); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 479 | } else if (cert_error) { |
| 480 | SetSocket(std::make_unique<MockClientSocket>(net_log().net_log())); |
| 481 | result = ERR_CERT_COMMON_NAME_INVALID; |
[email protected] | 6e713f0 | 2009-08-06 02:56:40 | [diff] [blame] | 482 | } else { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 483 | result = ERR_CONNECTION_FAILED; |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 484 | SetSocket(std::unique_ptr<StreamSocket>()); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 485 | } |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 486 | |
| 487 | if (was_async) |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 488 | NotifyDelegateOfCompletion(result); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 489 | return result; |
| 490 | } |
| 491 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 492 | void DoAdvanceAuthChallenge(int remaining_challenges, |
| 493 | bool succeed_after_last_challenge) { |
| 494 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 495 | FROM_HERE, |
| 496 | base::BindOnce(&TestConnectJob::InvokeNextProxyAuthCallback, |
| 497 | weak_factory_.GetWeakPtr(), remaining_challenges, |
| 498 | succeed_after_last_challenge)); |
| 499 | } |
| 500 | |
| 501 | void InvokeNextProxyAuthCallback(int remaining_challenges, |
| 502 | bool succeed_after_last_challenge) { |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 503 | set_load_state(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 504 | if (remaining_challenges == 0) { |
| 505 | DoConnect(succeed_after_last_challenge, true /* was_async */, |
| 506 | false /* cert_error */); |
| 507 | return; |
| 508 | } |
| 509 | |
| 510 | // Integration tests make sure HttpResponseInfo and HttpAuthController work. |
| 511 | // The auth tests here are just focused on ConnectJob bookkeeping. |
| 512 | HttpResponseInfo info; |
| 513 | NotifyDelegateOfProxyAuth( |
| 514 | info, nullptr /* http_auth_controller */, |
| 515 | base::BindOnce(&TestConnectJob::DoAdvanceAuthChallenge, |
| 516 | weak_factory_.GetWeakPtr(), remaining_challenges - 1, |
| 517 | succeed_after_last_challenge)); |
| 518 | } |
| 519 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 520 | bool waiting_success_; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 521 | const JobType job_type_; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 522 | MockClientSocketFactory* const client_socket_factory_; |
[email protected] | 4645135 | 2009-09-01 14:54:21 | [diff] [blame] | 523 | LoadState load_state_; |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 524 | bool has_established_connection_; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 525 | bool store_additional_error_state_; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 526 | |
[email protected] | d5492c5 | 2013-11-10 20:44:39 | [diff] [blame] | 527 | base::WeakPtrFactory<TestConnectJob> weak_factory_; |
| 528 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 529 | DISALLOW_COPY_AND_ASSIGN(TestConnectJob); |
| 530 | }; |
| 531 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 532 | class TestConnectJobFactory |
| 533 | : public TestClientSocketPoolBase::ConnectJobFactory { |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 534 | public: |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 535 | TestConnectJobFactory(MockClientSocketFactory* client_socket_factory, |
| 536 | NetLog* net_log) |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 537 | : common_connect_job_params_( |
| 538 | nullptr /* client_socket_factory */, |
| 539 | nullptr /* host_resolver */, |
Matt Menke | b88837e | 2019-03-20 11:50:40 | [diff] [blame^] | 540 | nullptr /* http_auth_cache */, |
| 541 | nullptr /* http_auth_handler_factory */, |
| 542 | nullptr /* spdy_session_pool */, |
| 543 | nullptr /* quic_stream_factory */, |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 544 | nullptr /* proxy_delegate */, |
Matt Menke | d732ea4 | 2019-03-08 12:05:00 | [diff] [blame] | 545 | nullptr /* http_user_agent_settings */, |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 546 | SSLClientSocketContext(), |
| 547 | SSLClientSocketContext(), |
| 548 | nullptr /* socket_performance_watcher_factory */, |
| 549 | nullptr /* network_quality_estimator */, |
| 550 | net_log, |
| 551 | nullptr /* websocket_endpoint_lock_manager */), |
| 552 | job_type_(TestConnectJob::kMockJob), |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 553 | job_types_(nullptr), |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 554 | client_socket_factory_(client_socket_factory) {} |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 555 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 556 | ~TestConnectJobFactory() override = default; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 557 | |
| 558 | void set_job_type(TestConnectJob::JobType job_type) { job_type_ = job_type; } |
| 559 | |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 560 | void set_job_types(std::list<TestConnectJob::JobType>* job_types) { |
| 561 | job_types_ = job_types; |
| 562 | CHECK(!job_types_->empty()); |
| 563 | } |
| 564 | |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 565 | void set_timeout_duration(base::TimeDelta timeout_duration) { |
| 566 | timeout_duration_ = timeout_duration; |
| 567 | } |
| 568 | |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 569 | // ConnectJobFactory implementation. |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 570 | |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 571 | std::unique_ptr<ConnectJob> NewConnectJob( |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 572 | const TestClientSocketPoolBase::Request& request, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 573 | ConnectJob::Delegate* delegate) const override { |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 574 | EXPECT_TRUE(!job_types_ || !job_types_->empty()); |
| 575 | TestConnectJob::JobType job_type = job_type_; |
| 576 | if (job_types_ && !job_types_->empty()) { |
| 577 | job_type = job_types_->front(); |
| 578 | job_types_->pop_front(); |
| 579 | } |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 580 | return std::make_unique<TestConnectJob>( |
| 581 | job_type, request, timeout_duration_, &common_connect_job_params_, |
| 582 | delegate, client_socket_factory_); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | private: |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 586 | const CommonConnectJobParams common_connect_job_params_; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 587 | TestConnectJob::JobType job_type_; |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 588 | std::list<TestConnectJob::JobType>* job_types_; |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 589 | base::TimeDelta timeout_duration_; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 590 | MockClientSocketFactory* const client_socket_factory_; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 591 | |
| 592 | DISALLOW_COPY_AND_ASSIGN(TestConnectJobFactory); |
| 593 | }; |
| 594 | |
| 595 | class TestClientSocketPool : public ClientSocketPool { |
| 596 | public: |
[email protected] | 12322e7e | 2013-08-15 17:49:26 | [diff] [blame] | 597 | typedef TestSocketParams SocketParams; |
| 598 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 599 | TestClientSocketPool( |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 600 | int max_sockets, |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 601 | int max_sockets_per_group, |
[email protected] | 9bf28db | 2009-08-29 01:35:16 | [diff] [blame] | 602 | base::TimeDelta unused_idle_socket_timeout, |
| 603 | base::TimeDelta used_idle_socket_timeout, |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 604 | TestClientSocketPoolBase::ConnectJobFactory* connect_job_factory) |
Matt Menke | 83367864 | 2019-03-05 22:05:51 | [diff] [blame] | 605 | : base_(max_sockets, |
rkaplow | d90695c | 2015-03-25 22:12:41 | [diff] [blame] | 606 | max_sockets_per_group, |
| 607 | unused_idle_socket_timeout, |
| 608 | used_idle_socket_timeout, |
[email protected] | 66761b95 | 2010-06-25 21:30:38 | [diff] [blame] | 609 | connect_job_factory) {} |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 610 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 611 | ~TestClientSocketPool() override = default; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 612 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 613 | int RequestSocket(const GroupId& group_id, |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 614 | const void* params, |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 615 | RequestPriority priority, |
Paul Jensen | 8d6f87ec | 2018-01-13 00:46:54 | [diff] [blame] | 616 | const SocketTag& socket_tag, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 617 | RespectLimits respect_limits, |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 618 | ClientSocketHandle* handle, |
Bence Béky | 5a8662b | 2018-07-03 13:04:03 | [diff] [blame] | 619 | CompletionOnceCallback callback, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 620 | const ProxyAuthCallback& proxy_auth_callback, |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 621 | const NetLogWithSource& net_log) override { |
[email protected] | df4b4ef | 2010-07-12 18:25:21 | [diff] [blame] | 622 | const scoped_refptr<TestSocketParams>* casted_socket_params = |
| 623 | static_cast<const scoped_refptr<TestSocketParams>*>(params); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 624 | return base_.RequestSocket( |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 625 | group_id, *casted_socket_params, priority, socket_tag, respect_limits, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 626 | handle, std::move(callback), proxy_auth_callback, net_log); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 627 | } |
| 628 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 629 | void RequestSockets(const GroupId& group_id, |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 630 | const void* params, |
| 631 | int num_sockets, |
Charlie Harrison | 55ce608 | 2018-05-14 02:25:57 | [diff] [blame] | 632 | const NetLogWithSource& net_log) override { |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 633 | const scoped_refptr<TestSocketParams>* casted_params = |
| 634 | static_cast<const scoped_refptr<TestSocketParams>*>(params); |
| 635 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 636 | base_.RequestSockets(group_id, *casted_params, num_sockets, net_log); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 637 | } |
| 638 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 639 | void SetPriority(const GroupId& group_id, |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 640 | ClientSocketHandle* handle, |
| 641 | RequestPriority priority) override { |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 642 | base_.SetPriority(group_id, handle, priority); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 643 | } |
| 644 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 645 | void CancelRequest(const GroupId& group_id, |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 646 | ClientSocketHandle* handle) override { |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 647 | base_.CancelRequest(group_id, handle); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 648 | } |
| 649 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 650 | void ReleaseSocket(const GroupId& group_id, |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 651 | std::unique_ptr<StreamSocket> socket, |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 652 | int id) override { |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 653 | base_.ReleaseSocket(group_id, std::move(socket), id); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 654 | } |
| 655 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 656 | void FlushWithError(int error) override { base_.FlushWithError(error); } |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 657 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 658 | bool IsStalled() const override { return base_.IsStalled(); } |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 659 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 660 | void CloseIdleSockets() override { base_.CloseIdleSockets(); } |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 661 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 662 | void CloseIdleSocketsInGroup(const GroupId& group_id) override { |
| 663 | base_.CloseIdleSocketsInGroup(group_id); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 664 | } |
| 665 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 666 | int IdleSocketCount() const override { return base_.idle_socket_count(); } |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 667 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 668 | size_t IdleSocketCountInGroup(const GroupId& group_id) const override { |
| 669 | return base_.IdleSocketCountInGroup(group_id); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 670 | } |
| 671 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 672 | LoadState GetLoadState(const GroupId& group_id, |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 673 | const ClientSocketHandle* handle) const override { |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 674 | return base_.GetLoadState(group_id, handle); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 675 | } |
| 676 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 677 | void AddHigherLayeredPool(HigherLayeredPool* higher_pool) override { |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 678 | base_.AddHigherLayeredPool(higher_pool); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 679 | } |
| 680 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 681 | void RemoveHigherLayeredPool(HigherLayeredPool* higher_pool) override { |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 682 | base_.RemoveHigherLayeredPool(higher_pool); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 683 | } |
| 684 | |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 685 | std::unique_ptr<base::DictionaryValue> GetInfoAsValue( |
[email protected] | d4dfdab | 2011-12-07 16:56:59 | [diff] [blame] | 686 | const std::string& name, |
Matt Menke | 83367864 | 2019-03-05 22:05:51 | [diff] [blame] | 687 | const std::string& type) const override { |
[email protected] | 59d7a5a | 2010-08-30 16:44:27 | [diff] [blame] | 688 | return base_.GetInfoAsValue(name, type); |
| 689 | } |
| 690 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 691 | const TestClientSocketPoolBase* base() const { return &base_; } |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 692 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 693 | size_t NumNeverAssignedConnectJobsInGroup(const GroupId& group_id) const { |
| 694 | return base_.NumNeverAssignedConnectJobsInGroup(group_id); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 695 | } |
| 696 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 697 | size_t NumUnassignedConnectJobsInGroup(const GroupId& group_id) const { |
| 698 | return base_.NumUnassignedConnectJobsInGroup(group_id); |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 699 | } |
| 700 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 701 | size_t NumConnectJobsInGroup(const GroupId& group_id) const { |
| 702 | return base_.NumConnectJobsInGroup(group_id); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 703 | } |
| 704 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 705 | int NumActiveSocketsInGroup(const GroupId& group_id) const { |
| 706 | return base_.NumActiveSocketsInGroup(group_id); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 707 | } |
| 708 | |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 709 | bool RequestInGroupWithHandleHasJobForTesting( |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 710 | const GroupId& group_id, |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 711 | const ClientSocketHandle* handle) const { |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 712 | return base_.RequestInGroupWithHandleHasJobForTesting(group_id, handle); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 713 | } |
| 714 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 715 | bool HasGroup(const GroupId& group_id) const { |
| 716 | return base_.HasGroup(group_id); |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 717 | } |
| 718 | |
[email protected] | 9bf28db | 2009-08-29 01:35:16 | [diff] [blame] | 719 | void CleanupTimedOutIdleSockets() { base_.CleanupIdleSockets(false); } |
| 720 | |
[email protected] | 06d9404 | 2010-08-25 01:45:22 | [diff] [blame] | 721 | void EnableConnectBackupJobs() { base_.EnableConnectBackupJobs(); } |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 722 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 723 | private: |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 724 | TestClientSocketPoolBase base_; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 725 | |
| 726 | DISALLOW_COPY_AND_ASSIGN(TestClientSocketPool); |
| 727 | }; |
| 728 | |
[email protected] | a937a06d | 2009-08-19 21:19:24 | [diff] [blame] | 729 | } // namespace |
| 730 | |
[email protected] | a937a06d | 2009-08-19 21:19:24 | [diff] [blame] | 731 | namespace { |
| 732 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 733 | void MockClientSocketFactory::SignalJobs() { |
jdoerrie | 22a91d8b9 | 2018-10-05 08:43:26 | [diff] [blame] | 734 | for (auto it = waiting_jobs_.begin(); it != waiting_jobs_.end(); ++it) { |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 735 | (*it)->Signal(); |
| 736 | } |
| 737 | waiting_jobs_.clear(); |
| 738 | } |
| 739 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 740 | void MockClientSocketFactory::SignalJob(size_t job) { |
| 741 | ASSERT_LT(job, waiting_jobs_.size()); |
| 742 | waiting_jobs_[job]->Signal(); |
| 743 | waiting_jobs_.erase(waiting_jobs_.begin() + job); |
| 744 | } |
| 745 | |
| 746 | void MockClientSocketFactory::SetJobLoadState(size_t job, |
| 747 | LoadState load_state) { |
| 748 | ASSERT_LT(job, waiting_jobs_.size()); |
| 749 | waiting_jobs_[job]->set_load_state(load_state); |
| 750 | } |
| 751 | |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 752 | void MockClientSocketFactory::SetJobHasEstablishedConnection(size_t job) { |
| 753 | ASSERT_LT(job, waiting_jobs_.size()); |
| 754 | waiting_jobs_[job]->set_has_established_connection(); |
| 755 | } |
| 756 | |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 757 | class ClientSocketPoolBaseTest : public TestWithScopedTaskEnvironment { |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 758 | protected: |
Alex Clarke | 0def209 | 2018-12-10 12:01:45 | [diff] [blame] | 759 | ClientSocketPoolBaseTest() |
| 760 | : TestWithScopedTaskEnvironment( |
| 761 | base::test::ScopedTaskEnvironment::MainThreadType::MOCK_TIME), |
| 762 | params_(new TestSocketParams()) { |
[email protected] | 636b825 | 2011-04-08 19:56:54 | [diff] [blame] | 763 | connect_backup_jobs_enabled_ = |
| 764 | internal::ClientSocketPoolBaseHelper::connect_backup_jobs_enabled(); |
| 765 | internal::ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(true); |
| 766 | } |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 767 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 768 | ~ClientSocketPoolBaseTest() override { |
[email protected] | 636b825 | 2011-04-08 19:56:54 | [diff] [blame] | 769 | internal::ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled( |
| 770 | connect_backup_jobs_enabled_); |
| 771 | } |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 772 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 773 | void CreatePool(int max_sockets, int max_sockets_per_group) { |
Tarun Bansal | a763509 | 2019-02-20 10:00:59 | [diff] [blame] | 774 | CreatePoolWithIdleTimeouts(max_sockets, max_sockets_per_group, |
| 775 | kUnusedIdleSocketTimeout, |
| 776 | ClientSocketPool::used_idle_socket_timeout()); |
[email protected] | 9bf28db | 2009-08-29 01:35:16 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | void CreatePoolWithIdleTimeouts( |
| 780 | int max_sockets, int max_sockets_per_group, |
| 781 | base::TimeDelta unused_idle_socket_timeout, |
| 782 | base::TimeDelta used_idle_socket_timeout) { |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 783 | DCHECK(!pool_.get()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 784 | connect_job_factory_ = new TestConnectJobFactory(&client_socket_factory_, |
| 785 | &net_log_); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 786 | pool_.reset(new TestClientSocketPool(max_sockets, |
| 787 | max_sockets_per_group, |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 788 | unused_idle_socket_timeout, |
| 789 | used_idle_socket_timeout, |
| 790 | connect_job_factory_)); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 791 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 792 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 793 | int StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 794 | const ClientSocketPool::GroupId& group_id, |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 795 | RequestPriority priority, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 796 | ClientSocketPool::RespectLimits respect_limits) { |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 797 | return test_base_.StartRequestUsingPool(pool_.get(), group_id, priority, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 798 | respect_limits, params_); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 799 | } |
| 800 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 801 | int StartRequest(const ClientSocketPool::GroupId& group_id, |
| 802 | RequestPriority priority) { |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 803 | return StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 804 | group_id, priority, ClientSocketPool::RespectLimits::ENABLED); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 805 | } |
| 806 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 807 | int GetOrderOfRequest(size_t index) const { |
| 808 | return test_base_.GetOrderOfRequest(index); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 809 | } |
| 810 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 811 | bool ReleaseOneConnection(ClientSocketPoolTest::KeepAlive keep_alive) { |
| 812 | return test_base_.ReleaseOneConnection(keep_alive); |
| 813 | } |
| 814 | |
| 815 | void ReleaseAllConnections(ClientSocketPoolTest::KeepAlive keep_alive) { |
| 816 | test_base_.ReleaseAllConnections(keep_alive); |
| 817 | } |
| 818 | |
| 819 | TestSocketRequest* request(int i) { return test_base_.request(i); } |
| 820 | size_t requests_size() const { return test_base_.requests_size(); } |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 821 | std::vector<std::unique_ptr<TestSocketRequest>>* requests() { |
olli.raula | 9d66b7d | 2015-11-23 08:30:42 | [diff] [blame] | 822 | return test_base_.requests(); |
| 823 | } |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 824 | // Only counts the requests that get sockets asynchronously; |
| 825 | // synchronous completions are not registered by this count. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 826 | size_t completion_count() const { return test_base_.completion_count(); } |
| 827 | |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 828 | TestNetLog net_log_; |
[email protected] | 636b825 | 2011-04-08 19:56:54 | [diff] [blame] | 829 | bool connect_backup_jobs_enabled_; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 830 | MockClientSocketFactory client_socket_factory_; |
[email protected] | 17a0c6c | 2009-08-04 00:07:04 | [diff] [blame] | 831 | TestConnectJobFactory* connect_job_factory_; |
[email protected] | df4b4ef | 2010-07-12 18:25:21 | [diff] [blame] | 832 | scoped_refptr<TestSocketParams> params_; |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 833 | std::unique_ptr<TestClientSocketPool> pool_; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 834 | ClientSocketPoolTest test_base_; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 835 | }; |
| 836 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 837 | TEST_F(ClientSocketPoolBaseTest, BasicSynchronous) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 838 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 839 | |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 840 | TestCompletionCallback callback; |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 841 | ClientSocketHandle handle; |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 842 | BoundTestNetLog log; |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 843 | TestLoadTimingInfoNotConnected(handle); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 844 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 845 | EXPECT_EQ( |
| 846 | OK, handle.Init( |
| 847 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
| 848 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 849 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound())); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 850 | EXPECT_TRUE(handle.is_initialized()); |
| 851 | EXPECT_TRUE(handle.socket()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 852 | TestLoadTimingInfoConnectedNotReused(handle); |
| 853 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 854 | handle.Reset(); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 855 | TestLoadTimingInfoNotConnected(handle); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 856 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 857 | TestNetLogEntry::List entries; |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 858 | log.GetEntries(&entries); |
| 859 | |
| 860 | EXPECT_EQ(4u, entries.size()); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 861 | EXPECT_TRUE(LogContainsBeginEvent(entries, 0, NetLogEventType::SOCKET_POOL)); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 862 | EXPECT_TRUE(LogContainsEvent( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 863 | entries, 1, NetLogEventType::SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
| 864 | NetLogEventPhase::NONE)); |
| 865 | EXPECT_TRUE(LogContainsEvent(entries, 2, |
| 866 | NetLogEventType::SOCKET_POOL_BOUND_TO_SOCKET, |
| 867 | NetLogEventPhase::NONE)); |
| 868 | EXPECT_TRUE(LogContainsEndEvent(entries, 3, NetLogEventType::SOCKET_POOL)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 869 | } |
| 870 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 871 | TEST_F(ClientSocketPoolBaseTest, InitConnectionFailure) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 872 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 873 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 874 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 875 | BoundTestNetLog log; |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 876 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 877 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 878 | TestCompletionCallback callback; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 879 | // Set the additional error state members to ensure that they get cleared. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 880 | handle.set_is_ssl_error(true); |
[email protected] | 8b49869 | 2010-07-16 17:11:43 | [diff] [blame] | 881 | HttpResponseInfo info; |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 882 | info.headers = new HttpResponseHeaders(std::string()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 883 | handle.set_ssl_error_response_info(info); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 884 | EXPECT_EQ( |
| 885 | ERR_CONNECTION_FAILED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 886 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 887 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 888 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 889 | log.bound())); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 890 | EXPECT_FALSE(handle.socket()); |
| 891 | EXPECT_FALSE(handle.is_ssl_error()); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 892 | EXPECT_TRUE(handle.ssl_error_response_info().headers.get() == nullptr); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 893 | TestLoadTimingInfoNotConnected(handle); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 894 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 895 | TestNetLogEntry::List entries; |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 896 | log.GetEntries(&entries); |
| 897 | |
| 898 | EXPECT_EQ(3u, entries.size()); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 899 | EXPECT_TRUE(LogContainsBeginEvent(entries, 0, NetLogEventType::SOCKET_POOL)); |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 900 | EXPECT_TRUE(LogContainsEvent( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 901 | entries, 1, NetLogEventType::SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
| 902 | NetLogEventPhase::NONE)); |
| 903 | EXPECT_TRUE(LogContainsEndEvent(entries, 2, NetLogEventType::SOCKET_POOL)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 904 | } |
| 905 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 906 | // Make sure different groups do not share sockets. |
| 907 | TEST_F(ClientSocketPoolBaseTest, GroupSeparation) { |
| 908 | CreatePool(1000 /* max_sockets */, 2 /* max_sockets_per_group */); |
| 909 | |
| 910 | const HostPortPair kHostPortPairs[] = { |
| 911 | {"a", 80}, |
| 912 | {"a", 443}, |
| 913 | {"b", 80}, |
| 914 | }; |
| 915 | |
| 916 | const ClientSocketPool::SocketType kSocketTypes[] = { |
| 917 | ClientSocketPool::SocketType::kHttp, |
| 918 | ClientSocketPool::SocketType::kSsl, |
| 919 | ClientSocketPool::SocketType::kSslVersionInterferenceProbe, |
| 920 | ClientSocketPool::SocketType::kFtp, |
| 921 | }; |
| 922 | |
| 923 | const bool kPrivacyModes[] = {false, true}; |
| 924 | |
| 925 | int total_idle_sockets = 0; |
| 926 | |
| 927 | // Walk through each GroupId, making sure that requesting a socket for one |
| 928 | // group does not return a previously connected socket for another group. |
| 929 | for (const auto& host_port_pair : kHostPortPairs) { |
| 930 | SCOPED_TRACE(host_port_pair.ToString()); |
| 931 | for (const auto& socket_type : kSocketTypes) { |
| 932 | SCOPED_TRACE(static_cast<int>(socket_type)); |
| 933 | for (const auto& privacy_mode : kPrivacyModes) { |
| 934 | SCOPED_TRACE(privacy_mode); |
| 935 | |
| 936 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 937 | |
| 938 | ClientSocketPool::GroupId group_id(host_port_pair, socket_type, |
| 939 | privacy_mode); |
| 940 | |
| 941 | EXPECT_FALSE(pool_->HasGroup(group_id)); |
| 942 | |
| 943 | TestCompletionCallback callback; |
| 944 | ClientSocketHandle handle; |
| 945 | |
| 946 | // Since the group is empty, requesting a socket should not complete |
| 947 | // synchronously. |
| 948 | EXPECT_THAT( |
| 949 | handle.Init(group_id, params_, DEFAULT_PRIORITY, SocketTag(), |
| 950 | ClientSocketPool::RespectLimits::ENABLED, |
| 951 | callback.callback(), |
| 952 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 953 | NetLogWithSource()), |
| 954 | IsError(ERR_IO_PENDING)); |
| 955 | EXPECT_TRUE(pool_->HasGroup(group_id)); |
| 956 | EXPECT_EQ(total_idle_sockets, pool_->IdleSocketCount()); |
| 957 | |
| 958 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 959 | EXPECT_TRUE(handle.socket()); |
| 960 | EXPECT_TRUE(pool_->HasGroup(group_id)); |
| 961 | EXPECT_EQ(total_idle_sockets, pool_->IdleSocketCount()); |
| 962 | |
| 963 | // Return socket to pool. |
| 964 | handle.Reset(); |
| 965 | EXPECT_EQ(total_idle_sockets + 1, pool_->IdleSocketCount()); |
| 966 | |
| 967 | // Requesting a socket again should return the same socket as before, so |
| 968 | // should complete synchronously. |
| 969 | EXPECT_THAT( |
| 970 | handle.Init(group_id, params_, DEFAULT_PRIORITY, SocketTag(), |
| 971 | ClientSocketPool::RespectLimits::ENABLED, |
| 972 | callback.callback(), |
| 973 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 974 | NetLogWithSource()), |
| 975 | IsOk()); |
| 976 | EXPECT_TRUE(handle.socket()); |
| 977 | EXPECT_EQ(total_idle_sockets, pool_->IdleSocketCount()); |
| 978 | |
| 979 | // Return socket to pool again. |
| 980 | handle.Reset(); |
| 981 | EXPECT_EQ(total_idle_sockets + 1, pool_->IdleSocketCount()); |
| 982 | |
| 983 | ++total_idle_sockets; |
| 984 | } |
| 985 | } |
| 986 | } |
| 987 | } |
| 988 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 989 | TEST_F(ClientSocketPoolBaseTest, TotalLimit) { |
| 990 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 991 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 992 | // TODO(eroman): Check that the NetLog contains this event. |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 993 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 994 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 995 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
| 996 | EXPECT_THAT(StartRequest(TestGroupId("c"), DEFAULT_PRIORITY), IsOk()); |
| 997 | EXPECT_THAT(StartRequest(TestGroupId("d"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 998 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 999 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1000 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1001 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1002 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1003 | EXPECT_THAT(StartRequest(TestGroupId("e"), DEFAULT_PRIORITY), |
| 1004 | IsError(ERR_IO_PENDING)); |
| 1005 | EXPECT_THAT(StartRequest(TestGroupId("f"), DEFAULT_PRIORITY), |
| 1006 | IsError(ERR_IO_PENDING)); |
| 1007 | EXPECT_THAT(StartRequest(TestGroupId("g"), DEFAULT_PRIORITY), |
| 1008 | IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1009 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1010 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1011 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1012 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1013 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1014 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1015 | |
| 1016 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1017 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1018 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 1019 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 1020 | EXPECT_EQ(5, GetOrderOfRequest(5)); |
| 1021 | EXPECT_EQ(6, GetOrderOfRequest(6)); |
| 1022 | EXPECT_EQ(7, GetOrderOfRequest(7)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1023 | |
| 1024 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1025 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(8)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1026 | } |
| 1027 | |
| 1028 | TEST_F(ClientSocketPoolBaseTest, TotalLimitReachedNewGroup) { |
| 1029 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1030 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 1031 | // TODO(eroman): Check that the NetLog contains this event. |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 1032 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1033 | // Reach all limits: max total sockets, and max sockets per group. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1034 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1035 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1036 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
| 1037 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1038 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1039 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1040 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1041 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1042 | |
| 1043 | // Now create a new group and verify that we don't starve it. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1044 | EXPECT_THAT(StartRequest(TestGroupId("c"), DEFAULT_PRIORITY), |
| 1045 | IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1046 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1047 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1048 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1049 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1050 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1051 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1052 | |
| 1053 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1054 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1055 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 1056 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 1057 | EXPECT_EQ(5, GetOrderOfRequest(5)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1058 | |
| 1059 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1060 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(6)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1061 | } |
| 1062 | |
| 1063 | TEST_F(ClientSocketPoolBaseTest, TotalLimitRespectsPriority) { |
| 1064 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1065 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1066 | EXPECT_THAT(StartRequest(TestGroupId("b"), LOWEST), IsOk()); |
| 1067 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsOk()); |
| 1068 | EXPECT_THAT(StartRequest(TestGroupId("b"), HIGHEST), IsOk()); |
| 1069 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1070 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1071 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1072 | client_socket_factory_.allocation_count()); |
| 1073 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1074 | EXPECT_THAT(StartRequest(TestGroupId("c"), LOWEST), IsError(ERR_IO_PENDING)); |
| 1075 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1076 | EXPECT_THAT(StartRequest(TestGroupId("b"), HIGHEST), IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1077 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1078 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1079 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1080 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1081 | |
| 1082 | // First 4 requests don't have to wait, and finish in order. |
| 1083 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1084 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1085 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 1086 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 1087 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1088 | // Request ("b", HIGHEST) has the highest priority, then (TestGroupId("a"), |
| 1089 | // MEDIUM), and then ("c", LOWEST). |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1090 | EXPECT_EQ(7, GetOrderOfRequest(5)); |
| 1091 | EXPECT_EQ(6, GetOrderOfRequest(6)); |
| 1092 | EXPECT_EQ(5, GetOrderOfRequest(7)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1093 | |
| 1094 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1095 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(9)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1096 | } |
| 1097 | |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1098 | // Test reprioritizing a request before completion doesn't interfere with |
| 1099 | // its completion. |
| 1100 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeOne) { |
| 1101 | CreatePool(kDefaultMaxSockets, 1); |
| 1102 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1103 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1104 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1105 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1106 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1107 | |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 1108 | request(1)->handle()->SetPriority(HIGHEST); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1109 | |
| 1110 | ReleaseOneConnection(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1111 | |
| 1112 | EXPECT_TRUE(request(1)->handle()->socket()); |
| 1113 | } |
| 1114 | |
| 1115 | // Reprioritize a request up past another one and make sure that changes the |
| 1116 | // completion order. |
| 1117 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeUpReorder) { |
| 1118 | CreatePool(kDefaultMaxSockets, 1); |
| 1119 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1120 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1121 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1122 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1123 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1124 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1125 | EXPECT_FALSE(request(2)->handle()->socket()); |
| 1126 | |
| 1127 | request(2)->handle()->SetPriority(HIGHEST); |
| 1128 | |
| 1129 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1130 | |
| 1131 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1132 | EXPECT_EQ(3, GetOrderOfRequest(2)); |
| 1133 | EXPECT_EQ(2, GetOrderOfRequest(3)); |
| 1134 | } |
| 1135 | |
| 1136 | // Reprioritize a request without changing relative priorities and check |
| 1137 | // that the order doesn't change. |
| 1138 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeUpNoReorder) { |
| 1139 | CreatePool(kDefaultMaxSockets, 1); |
| 1140 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1141 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1142 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1143 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1144 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1145 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1146 | EXPECT_FALSE(request(2)->handle()->socket()); |
| 1147 | |
| 1148 | request(2)->handle()->SetPriority(MEDIUM); |
| 1149 | |
| 1150 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1151 | |
| 1152 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1153 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1154 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 1155 | } |
| 1156 | |
| 1157 | // Reprioritize a request past down another one and make sure that changes the |
| 1158 | // completion order. |
| 1159 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeDownReorder) { |
| 1160 | CreatePool(kDefaultMaxSockets, 1); |
| 1161 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1162 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1163 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1164 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1165 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1166 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1167 | EXPECT_FALSE(request(2)->handle()->socket()); |
| 1168 | |
| 1169 | request(1)->handle()->SetPriority(LOW); |
| 1170 | |
| 1171 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1172 | |
| 1173 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1174 | EXPECT_EQ(3, GetOrderOfRequest(2)); |
| 1175 | EXPECT_EQ(2, GetOrderOfRequest(3)); |
| 1176 | } |
| 1177 | |
| 1178 | // Reprioritize a request to the same level as another and confirm it is |
| 1179 | // put after the old request. |
| 1180 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeResetFIFO) { |
| 1181 | CreatePool(kDefaultMaxSockets, 1); |
| 1182 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1183 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1184 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1185 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1186 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1187 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1188 | EXPECT_FALSE(request(2)->handle()->socket()); |
| 1189 | |
| 1190 | request(1)->handle()->SetPriority(MEDIUM); |
| 1191 | |
| 1192 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1193 | |
| 1194 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1195 | EXPECT_EQ(3, GetOrderOfRequest(2)); |
| 1196 | EXPECT_EQ(2, GetOrderOfRequest(3)); |
| 1197 | } |
| 1198 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1199 | TEST_F(ClientSocketPoolBaseTest, TotalLimitRespectsGroupLimit) { |
| 1200 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1201 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1202 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsOk()); |
| 1203 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsOk()); |
| 1204 | EXPECT_THAT(StartRequest(TestGroupId("b"), HIGHEST), IsOk()); |
| 1205 | EXPECT_THAT(StartRequest(TestGroupId("b"), MEDIUM), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1206 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1207 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1208 | client_socket_factory_.allocation_count()); |
| 1209 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1210 | EXPECT_THAT(StartRequest(TestGroupId("c"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1211 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 1212 | EXPECT_THAT(StartRequest(TestGroupId("b"), HIGHEST), IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1213 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1214 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1215 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1216 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1217 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1218 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1219 | |
| 1220 | // First 4 requests don't have to wait, and finish in order. |
| 1221 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1222 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1223 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 1224 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 1225 | |
| 1226 | // Request ("b", 7) has the highest priority, but we can't make new socket for |
| 1227 | // group "b", because it has reached the per-group limit. Then we make |
| 1228 | // socket for ("c", 6), because it has higher priority than ("a", 4), |
| 1229 | // and we still can't make a socket for group "b". |
| 1230 | EXPECT_EQ(5, GetOrderOfRequest(5)); |
| 1231 | EXPECT_EQ(6, GetOrderOfRequest(6)); |
| 1232 | EXPECT_EQ(7, GetOrderOfRequest(7)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1233 | |
| 1234 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1235 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(8)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1236 | } |
| 1237 | |
| 1238 | // Make sure that we count connecting sockets against the total limit. |
| 1239 | TEST_F(ClientSocketPoolBaseTest, TotalLimitCountsConnectingSockets) { |
| 1240 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1241 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1242 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1243 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
| 1244 | EXPECT_THAT(StartRequest(TestGroupId("c"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1245 | |
| 1246 | // Create one asynchronous request. |
| 1247 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1248 | EXPECT_THAT(StartRequest(TestGroupId("d"), DEFAULT_PRIORITY), |
| 1249 | IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1250 | |
[email protected] | 6b17538 | 2009-10-13 06:47:47 | [diff] [blame] | 1251 | // We post all of our delayed tasks with a 2ms delay. I.e. they don't |
| 1252 | // actually become pending until 2ms after they have been created. In order |
| 1253 | // to flush all tasks, we need to wait so that we know there are no |
| 1254 | // soon-to-be-pending tasks waiting. |
Alex Clarke | 0def209 | 2018-12-10 12:01:45 | [diff] [blame] | 1255 | FastForwardBy(base::TimeDelta::FromMilliseconds(10)); |
[email protected] | 6b17538 | 2009-10-13 06:47:47 | [diff] [blame] | 1256 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1257 | // The next synchronous request should wait for its turn. |
| 1258 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1259 | EXPECT_THAT(StartRequest(TestGroupId("e"), DEFAULT_PRIORITY), |
| 1260 | IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1261 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1262 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1263 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1264 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1265 | client_socket_factory_.allocation_count()); |
| 1266 | |
| 1267 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1268 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1269 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 1270 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1271 | EXPECT_EQ(5, GetOrderOfRequest(5)); |
| 1272 | |
| 1273 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1274 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(6)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1275 | } |
| 1276 | |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1277 | TEST_F(ClientSocketPoolBaseTest, CorrectlyCountStalledGroups) { |
| 1278 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 1279 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 1280 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1281 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1282 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1283 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1284 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1285 | |
| 1286 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 1287 | |
| 1288 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1289 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1290 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), |
| 1291 | IsError(ERR_IO_PENDING)); |
| 1292 | EXPECT_THAT(StartRequest(TestGroupId("c"), DEFAULT_PRIORITY), |
| 1293 | IsError(ERR_IO_PENDING)); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1294 | |
| 1295 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1296 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1297 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1298 | EXPECT_EQ(kDefaultMaxSockets + 1, client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1299 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1300 | EXPECT_EQ(kDefaultMaxSockets + 2, client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1301 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
| 1302 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1303 | EXPECT_EQ(kDefaultMaxSockets + 2, client_socket_factory_.allocation_count()); |
| 1304 | } |
| 1305 | |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 1306 | TEST_F(ClientSocketPoolBaseTest, StallAndThenCancelAndTriggerAvailableSocket) { |
| 1307 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 1308 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1309 | |
| 1310 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1311 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1312 | EXPECT_EQ( |
| 1313 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1314 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1315 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1316 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 1317 | NetLogWithSource())); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 1318 | |
| 1319 | ClientSocketHandle handles[4]; |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 1320 | for (size_t i = 0; i < base::size(handles); ++i) { |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1321 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1322 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1323 | handles[i].Init( |
| 1324 | TestGroupId("b"), params_, DEFAULT_PRIORITY, SocketTag(), |
| 1325 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1326 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 1327 | NetLogWithSource())); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 1328 | } |
| 1329 | |
| 1330 | // One will be stalled, cancel all the handles now. |
| 1331 | // This should hit the OnAvailableSocketSlot() code where we previously had |
| 1332 | // stalled groups, but no longer have any. |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 1333 | for (size_t i = 0; i < base::size(handles); ++i) |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 1334 | handles[i].Reset(); |
| 1335 | } |
| 1336 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1337 | TEST_F(ClientSocketPoolBaseTest, CancelStalledSocketAtSocketLimit) { |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1338 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1339 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 1340 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1341 | { |
| 1342 | ClientSocketHandle handles[kDefaultMaxSockets]; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1343 | TestCompletionCallback callbacks[kDefaultMaxSockets]; |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1344 | for (int i = 0; i < kDefaultMaxSockets; ++i) { |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1345 | EXPECT_EQ(OK, handles[i].Init(TestGroupId(base::NumberToString(i)), |
| 1346 | params_, DEFAULT_PRIORITY, SocketTag(), |
Paul Jensen | 8d6f87ec | 2018-01-13 00:46:54 | [diff] [blame] | 1347 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1348 | callbacks[i].callback(), |
| 1349 | ClientSocketPool::ProxyAuthCallback(), |
| 1350 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1351 | } |
| 1352 | |
| 1353 | // Force a stalled group. |
| 1354 | ClientSocketHandle stalled_handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1355 | TestCompletionCallback callback; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1356 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1357 | stalled_handle.Init( |
| 1358 | TestGroupId("foo"), params_, DEFAULT_PRIORITY, SocketTag(), |
| 1359 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1360 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 1361 | NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1362 | |
| 1363 | // Cancel the stalled request. |
| 1364 | stalled_handle.Reset(); |
| 1365 | |
| 1366 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1367 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 1368 | |
| 1369 | // Dropping out of scope will close all handles and return them to idle. |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1370 | } |
| 1371 | |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1372 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1373 | EXPECT_EQ(kDefaultMaxSockets, pool_->IdleSocketCount()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1374 | } |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1375 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1376 | TEST_F(ClientSocketPoolBaseTest, CancelPendingSocketAtSocketLimit) { |
| 1377 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1378 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 1379 | |
| 1380 | { |
| 1381 | ClientSocketHandle handles[kDefaultMaxSockets]; |
| 1382 | for (int i = 0; i < kDefaultMaxSockets; ++i) { |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1383 | TestCompletionCallback callback; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1384 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1385 | handles[i].Init(TestGroupId(base::NumberToString(i)), params_, |
| 1386 | DEFAULT_PRIORITY, SocketTag(), |
| 1387 | ClientSocketPool::RespectLimits::ENABLED, |
| 1388 | callback.callback(), |
| 1389 | ClientSocketPool::ProxyAuthCallback(), |
| 1390 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1391 | } |
| 1392 | |
| 1393 | // Force a stalled group. |
| 1394 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1395 | ClientSocketHandle stalled_handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1396 | TestCompletionCallback callback; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1397 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1398 | stalled_handle.Init( |
| 1399 | TestGroupId("foo"), params_, DEFAULT_PRIORITY, SocketTag(), |
| 1400 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1401 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 1402 | NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1403 | |
| 1404 | // Since it is stalled, it should have no connect jobs. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1405 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroup(TestGroupId("foo"))); |
| 1406 | EXPECT_EQ(0u, |
| 1407 | pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("foo"))); |
| 1408 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("foo"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1409 | |
| 1410 | // Cancel the stalled request. |
| 1411 | handles[0].Reset(); |
| 1412 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1413 | // Now we should have a connect job. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1414 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("foo"))); |
| 1415 | EXPECT_EQ(0u, |
| 1416 | pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("foo"))); |
| 1417 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("foo"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1418 | |
| 1419 | // The stalled socket should connect. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1420 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1421 | |
| 1422 | EXPECT_EQ(kDefaultMaxSockets + 1, |
| 1423 | client_socket_factory_.allocation_count()); |
| 1424 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1425 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroup(TestGroupId("foo"))); |
| 1426 | EXPECT_EQ(0u, |
| 1427 | pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("foo"))); |
| 1428 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("foo"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1429 | |
| 1430 | // Dropping out of scope will close all handles and return them to idle. |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1431 | } |
| 1432 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1433 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 1434 | } |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1435 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1436 | TEST_F(ClientSocketPoolBaseTest, WaitForStalledSocketAtSocketLimit) { |
| 1437 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1438 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1439 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1440 | ClientSocketHandle stalled_handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1441 | TestCompletionCallback callback; |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1442 | { |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 1443 | EXPECT_FALSE(pool_->IsStalled()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1444 | ClientSocketHandle handles[kDefaultMaxSockets]; |
| 1445 | for (int i = 0; i < kDefaultMaxSockets; ++i) { |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1446 | TestCompletionCallback callback; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1447 | EXPECT_EQ( |
| 1448 | OK, handles[i].Init(TestGroupId(base::StringPrintf("Take 2: %d", i)), |
| 1449 | params_, DEFAULT_PRIORITY, SocketTag(), |
| 1450 | ClientSocketPool::RespectLimits::ENABLED, |
| 1451 | callback.callback(), |
| 1452 | ClientSocketPool::ProxyAuthCallback(), |
| 1453 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1454 | } |
| 1455 | |
| 1456 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1457 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 1458 | EXPECT_FALSE(pool_->IsStalled()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1459 | |
| 1460 | // Now we will hit the socket limit. |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1461 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1462 | stalled_handle.Init( |
| 1463 | TestGroupId("foo"), params_, DEFAULT_PRIORITY, SocketTag(), |
| 1464 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1465 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 1466 | NetLogWithSource())); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 1467 | EXPECT_TRUE(pool_->IsStalled()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1468 | |
| 1469 | // Dropping out of scope will close all handles and return them to idle. |
| 1470 | } |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1471 | |
| 1472 | // But if we wait for it, the released idle sockets will be closed in |
| 1473 | // preference of the waiting request. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1474 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1475 | |
| 1476 | EXPECT_EQ(kDefaultMaxSockets + 1, client_socket_factory_.allocation_count()); |
| 1477 | EXPECT_EQ(3, pool_->IdleSocketCount()); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1478 | } |
| 1479 | |
| 1480 | // Regression test for http://crbug.com/40952. |
| 1481 | TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketAtSocketLimitDeleteGroup) { |
| 1482 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 06d9404 | 2010-08-25 01:45:22 | [diff] [blame] | 1483 | pool_->EnableConnectBackupJobs(); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1484 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 1485 | |
| 1486 | for (int i = 0; i < kDefaultMaxSockets; ++i) { |
| 1487 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1488 | TestCompletionCallback callback; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1489 | EXPECT_EQ(OK, handle.Init(TestGroupId(base::NumberToString(i)), params_, |
| 1490 | DEFAULT_PRIORITY, SocketTag(), |
| 1491 | ClientSocketPool::RespectLimits::ENABLED, |
| 1492 | callback.callback(), |
| 1493 | ClientSocketPool::ProxyAuthCallback(), |
| 1494 | pool_.get(), NetLogWithSource())); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1495 | } |
| 1496 | |
| 1497 | // Flush all the DoReleaseSocket tasks. |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 1498 | base::RunLoop().RunUntilIdle(); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1499 | |
| 1500 | // Stall a group. Set a pending job so it'll trigger a backup job if we don't |
| 1501 | // reuse a socket. |
| 1502 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1503 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1504 | TestCompletionCallback callback; |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1505 | |
| 1506 | // "0" is special here, since it should be the first entry in the sorted map, |
| 1507 | // which is the one which we would close an idle socket for. We shouldn't |
| 1508 | // close an idle socket though, since we should reuse the idle socket. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1509 | EXPECT_EQ(OK, handle.Init( |
| 1510 | TestGroupId("0"), params_, DEFAULT_PRIORITY, SocketTag(), |
| 1511 | ClientSocketPool::RespectLimits::ENABLED, |
| 1512 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1513 | pool_.get(), NetLogWithSource())); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1514 | |
| 1515 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1516 | EXPECT_EQ(kDefaultMaxSockets - 1, pool_->IdleSocketCount()); |
| 1517 | } |
| 1518 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1519 | TEST_F(ClientSocketPoolBaseTest, PendingRequests) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1520 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1521 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1522 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1523 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1524 | EXPECT_THAT(StartRequest(TestGroupId("a"), IDLE), IsError(ERR_IO_PENDING)); |
| 1525 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
| 1526 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1527 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1528 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 1529 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1530 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1531 | ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1532 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
| 1533 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1534 | EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup, |
| 1535 | completion_count()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1536 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1537 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1538 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
[email protected] | c9c6f5c | 2010-07-31 01:30:03 | [diff] [blame] | 1539 | EXPECT_EQ(8, GetOrderOfRequest(3)); |
| 1540 | EXPECT_EQ(6, GetOrderOfRequest(4)); |
| 1541 | EXPECT_EQ(4, GetOrderOfRequest(5)); |
| 1542 | EXPECT_EQ(3, GetOrderOfRequest(6)); |
| 1543 | EXPECT_EQ(5, GetOrderOfRequest(7)); |
| 1544 | EXPECT_EQ(7, GetOrderOfRequest(8)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1545 | |
| 1546 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1547 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(9)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1548 | } |
| 1549 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1550 | TEST_F(ClientSocketPoolBaseTest, PendingRequests_NoKeepAlive) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1551 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1552 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1553 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1554 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1555 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
| 1556 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1557 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1558 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 1559 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1560 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1561 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1562 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1563 | for (size_t i = kDefaultMaxSocketsPerGroup; i < requests_size(); ++i) |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1564 | EXPECT_THAT(request(i)->WaitForResult(), IsOk()); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1565 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1566 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1567 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1568 | EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup, |
| 1569 | completion_count()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1570 | } |
| 1571 | |
| 1572 | // This test will start up a RequestSocket() and then immediately Cancel() it. |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1573 | // The pending connect job will be cancelled and should not call back into |
| 1574 | // ClientSocketPoolBase. |
| 1575 | TEST_F(ClientSocketPoolBaseTest, CancelRequestClearGroup) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1576 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1577 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1578 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1579 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1580 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1581 | EXPECT_EQ( |
| 1582 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1583 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1584 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1585 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 1586 | NetLogWithSource())); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1587 | handle.Reset(); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1588 | } |
| 1589 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1590 | TEST_F(ClientSocketPoolBaseTest, ConnectCancelConnect) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1591 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1592 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1593 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 1594 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1595 | TestCompletionCallback callback; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1596 | |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1597 | EXPECT_EQ( |
| 1598 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1599 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1600 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1601 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 1602 | NetLogWithSource())); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1603 | |
| 1604 | handle.Reset(); |
| 1605 | |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1606 | TestCompletionCallback callback2; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1607 | EXPECT_EQ( |
| 1608 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1609 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1610 | ClientSocketPool::RespectLimits::ENABLED, |
| 1611 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1612 | pool_.get(), NetLogWithSource())); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1613 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1614 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1615 | EXPECT_FALSE(callback.have_result()); |
| 1616 | |
| 1617 | handle.Reset(); |
| 1618 | } |
| 1619 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1620 | TEST_F(ClientSocketPoolBaseTest, CancelRequest) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1621 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1622 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1623 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1624 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1625 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
| 1626 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1627 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1628 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 1629 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1630 | |
| 1631 | // Cancel a request. |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1632 | size_t index_to_cancel = kDefaultMaxSocketsPerGroup + 2; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1633 | EXPECT_FALSE((*requests())[index_to_cancel]->handle()->is_initialized()); |
| 1634 | (*requests())[index_to_cancel]->handle()->Reset(); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1635 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1636 | ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1637 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1638 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
| 1639 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1640 | EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup - 1, |
| 1641 | completion_count()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1642 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1643 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1644 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1645 | EXPECT_EQ(5, GetOrderOfRequest(3)); |
| 1646 | EXPECT_EQ(3, GetOrderOfRequest(4)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1647 | EXPECT_EQ(ClientSocketPoolTest::kRequestNotFound, |
| 1648 | GetOrderOfRequest(5)); // Canceled request. |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1649 | EXPECT_EQ(4, GetOrderOfRequest(6)); |
| 1650 | EXPECT_EQ(6, GetOrderOfRequest(7)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1651 | |
| 1652 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1653 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(8)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1654 | } |
| 1655 | |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1656 | // Function to be used as a callback on socket request completion. It first |
| 1657 | // disconnects the successfully connected socket from the first request, and |
| 1658 | // then reuses the ClientSocketHandle to request another socket. |
| 1659 | // |
| 1660 | // |nested_callback| is called with the result of the second socket request. |
| 1661 | void RequestSocketOnComplete(ClientSocketHandle* handle, |
| 1662 | TestClientSocketPool* pool, |
| 1663 | TestConnectJobFactory* test_connect_job_factory, |
| 1664 | TestConnectJob::JobType next_job_type, |
Bence Béky | a4a5093 | 2018-08-10 13:39:41 | [diff] [blame] | 1665 | TestCompletionCallback* nested_callback, |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1666 | int first_request_result) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1667 | EXPECT_THAT(first_request_result, IsOk()); |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1668 | |
| 1669 | test_connect_job_factory->set_job_type(next_job_type); |
| 1670 | |
| 1671 | // Don't allow reuse of the socket. Disconnect it and then release it. |
| 1672 | if (handle->socket()) |
| 1673 | handle->socket()->Disconnect(); |
| 1674 | handle->Reset(); |
| 1675 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 1676 | scoped_refptr<TestSocketParams> params(new TestSocketParams()); |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1677 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1678 | int rv = handle->Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1679 | TestGroupId("a"), params, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1680 | ClientSocketPool::RespectLimits::ENABLED, nested_callback->callback(), |
| 1681 | ClientSocketPool::ProxyAuthCallback(), pool, NetLogWithSource()); |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1682 | if (rv != ERR_IO_PENDING) { |
| 1683 | DCHECK_EQ(TestConnectJob::kMockJob, next_job_type); |
Bence Béky | a4a5093 | 2018-08-10 13:39:41 | [diff] [blame] | 1684 | nested_callback->callback().Run(rv); |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1685 | } else { |
| 1686 | DCHECK_EQ(TestConnectJob::kMockPendingJob, next_job_type); |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1687 | } |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1688 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1689 | |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1690 | // Tests the case where a second socket is requested in a completion callback, |
| 1691 | // and the second socket connects asynchronously. Reuses the same |
| 1692 | // ClientSocketHandle for the second socket, after disconnecting the first. |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1693 | TEST_F(ClientSocketPoolBaseTest, RequestPendingJobTwice) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1694 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1695 | |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 1696 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 1697 | ClientSocketHandle handle; |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1698 | TestCompletionCallback second_result_callback; |
| 1699 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1700 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Paul Jensen | 8d6f87ec | 2018-01-13 00:46:54 | [diff] [blame] | 1701 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1702 | base::BindOnce(&RequestSocketOnComplete, &handle, pool_.get(), |
| 1703 | connect_job_factory_, TestConnectJob::kMockPendingJob, |
| 1704 | &second_result_callback), |
| 1705 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1706 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1707 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1708 | EXPECT_THAT(second_result_callback.WaitForResult(), IsOk()); |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1709 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1710 | |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1711 | // Tests the case where a second socket is requested in a completion callback, |
| 1712 | // and the second socket connects synchronously. Reuses the same |
| 1713 | // ClientSocketHandle for the second socket, after disconnecting the first. |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1714 | TEST_F(ClientSocketPoolBaseTest, RequestPendingJobThenSynchronous) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1715 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1716 | |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 1717 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 1718 | ClientSocketHandle handle; |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1719 | TestCompletionCallback second_result_callback; |
| 1720 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1721 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Paul Jensen | 8d6f87ec | 2018-01-13 00:46:54 | [diff] [blame] | 1722 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1723 | base::BindOnce(&RequestSocketOnComplete, &handle, pool_.get(), |
| 1724 | connect_job_factory_, TestConnectJob::kMockPendingJob, |
| 1725 | &second_result_callback), |
| 1726 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1727 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1728 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1729 | EXPECT_THAT(second_result_callback.WaitForResult(), IsOk()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1730 | } |
| 1731 | |
| 1732 | // Make sure that pending requests get serviced after active requests get |
| 1733 | // cancelled. |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1734 | TEST_F(ClientSocketPoolBaseTest, CancelActiveRequestWithPendingRequests) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1735 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1736 | |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 1737 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1738 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1739 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1740 | IsError(ERR_IO_PENDING)); |
| 1741 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1742 | IsError(ERR_IO_PENDING)); |
| 1743 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1744 | IsError(ERR_IO_PENDING)); |
| 1745 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1746 | IsError(ERR_IO_PENDING)); |
| 1747 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1748 | IsError(ERR_IO_PENDING)); |
| 1749 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1750 | IsError(ERR_IO_PENDING)); |
| 1751 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1752 | IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1753 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1754 | // Now, kDefaultMaxSocketsPerGroup requests should be active. |
| 1755 | // Let's cancel them. |
| 1756 | for (int i = 0; i < kDefaultMaxSocketsPerGroup; ++i) { |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1757 | ASSERT_FALSE(request(i)->handle()->is_initialized()); |
| 1758 | request(i)->handle()->Reset(); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1759 | } |
| 1760 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1761 | // Let's wait for the rest to complete now. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1762 | for (size_t i = kDefaultMaxSocketsPerGroup; i < requests_size(); ++i) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1763 | EXPECT_THAT(request(i)->WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1764 | request(i)->handle()->Reset(); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1765 | } |
| 1766 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1767 | EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup, |
| 1768 | completion_count()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1769 | } |
| 1770 | |
| 1771 | // Make sure that pending requests get serviced after active requests fail. |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1772 | TEST_F(ClientSocketPoolBaseTest, FailingActiveRequestWithPendingRequests) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1773 | const size_t kMaxSockets = 5; |
| 1774 | CreatePool(kMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1775 | |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 1776 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1777 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1778 | const size_t kNumberOfRequests = 2 * kDefaultMaxSocketsPerGroup + 1; |
| 1779 | ASSERT_LE(kNumberOfRequests, kMaxSockets); // Otherwise the test will hang. |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1780 | |
| 1781 | // Queue up all the requests |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1782 | for (size_t i = 0; i < kNumberOfRequests; ++i) |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1783 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1784 | IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1785 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1786 | for (size_t i = 0; i < kNumberOfRequests; ++i) |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1787 | EXPECT_THAT(request(i)->WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1788 | } |
| 1789 | |
mmenke | 9d72fe4 | 2017-05-18 22:36:07 | [diff] [blame] | 1790 | // Make sure that pending requests that complete synchronously get serviced |
| 1791 | // after active requests fail. See https://crbug.com/723748 |
| 1792 | TEST_F(ClientSocketPoolBaseTest, HandleMultipleSyncFailuresAfterAsyncFailure) { |
| 1793 | const size_t kNumberOfRequests = 10; |
| 1794 | const size_t kMaxSockets = 1; |
| 1795 | CreatePool(kMaxSockets, kMaxSockets); |
| 1796 | |
| 1797 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
| 1798 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1799 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1800 | IsError(ERR_IO_PENDING)); |
mmenke | 9d72fe4 | 2017-05-18 22:36:07 | [diff] [blame] | 1801 | |
| 1802 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
| 1803 | |
| 1804 | // Queue up all the other requests |
| 1805 | for (size_t i = 1; i < kNumberOfRequests; ++i) |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1806 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1807 | IsError(ERR_IO_PENDING)); |
mmenke | 9d72fe4 | 2017-05-18 22:36:07 | [diff] [blame] | 1808 | |
| 1809 | // Make sure all requests fail, instead of hanging. |
| 1810 | for (size_t i = 0; i < kNumberOfRequests; ++i) |
| 1811 | EXPECT_THAT(request(i)->WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 1812 | } |
| 1813 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1814 | TEST_F(ClientSocketPoolBaseTest, CancelActiveRequestThenRequestSocket) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1815 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1816 | |
| 1817 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1818 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1819 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1820 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1821 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1822 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1823 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1824 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1825 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1826 | |
| 1827 | // Cancel the active request. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1828 | handle.Reset(); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1829 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1830 | rv = handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 1831 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1832 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1833 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1834 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 1835 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1836 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1837 | EXPECT_FALSE(handle.is_reused()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 1838 | TestLoadTimingInfoConnectedNotReused(handle); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1839 | EXPECT_EQ(2, client_socket_factory_.allocation_count()); |
| 1840 | } |
| 1841 | |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1842 | TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketsForced) { |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1843 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1844 | ClientSocketHandle handle; |
| 1845 | TestCompletionCallback callback; |
| 1846 | BoundTestNetLog log; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1847 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1848 | TestGroupId("a"), params_, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1849 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1850 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1851 | EXPECT_THAT(rv, IsOk()); |
| 1852 | handle.Reset(); |
| 1853 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 1854 | pool_->CloseIdleSockets(); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1855 | } |
| 1856 | |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1857 | TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketsInGroupForced) { |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1858 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1859 | TestCompletionCallback callback; |
| 1860 | BoundTestNetLog log; |
| 1861 | ClientSocketHandle handle1; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1862 | int rv = handle1.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1863 | TestGroupId("a"), params_, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1864 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1865 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1866 | EXPECT_THAT(rv, IsOk()); |
| 1867 | ClientSocketHandle handle2; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1868 | rv = handle2.Init(TestGroupId("a"), params_, LOWEST, SocketTag(), |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1869 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1870 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1871 | pool_.get(), log.bound()); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1872 | ClientSocketHandle handle3; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1873 | rv = handle3.Init(TestGroupId("b"), params_, LOWEST, SocketTag(), |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1874 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1875 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1876 | pool_.get(), log.bound()); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1877 | EXPECT_THAT(rv, IsOk()); |
| 1878 | handle1.Reset(); |
| 1879 | handle2.Reset(); |
| 1880 | handle3.Reset(); |
| 1881 | EXPECT_EQ(3, pool_->IdleSocketCount()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1882 | pool_->CloseIdleSocketsInGroup(TestGroupId("a")); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1883 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1884 | } |
| 1885 | |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1886 | TEST_F(ClientSocketPoolBaseTest, CleanUpUnusableIdleSockets) { |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1887 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1888 | ClientSocketHandle handle; |
| 1889 | TestCompletionCallback callback; |
| 1890 | BoundTestNetLog log; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1891 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1892 | TestGroupId("a"), params_, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1893 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1894 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1895 | EXPECT_THAT(rv, IsOk()); |
| 1896 | StreamSocket* socket = handle.socket(); |
| 1897 | handle.Reset(); |
| 1898 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 1899 | |
| 1900 | // Disconnect socket now to make the socket unusable. |
| 1901 | socket->Disconnect(); |
| 1902 | ClientSocketHandle handle2; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1903 | rv = handle2.Init(TestGroupId("a"), params_, LOWEST, SocketTag(), |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1904 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1905 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1906 | pool_.get(), log.bound()); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1907 | EXPECT_THAT(rv, IsOk()); |
| 1908 | EXPECT_FALSE(handle2.is_reused()); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1909 | } |
| 1910 | |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1911 | // Regression test for http://crbug.com/17985. |
| 1912 | TEST_F(ClientSocketPoolBaseTest, GroupWithPendingRequestsIsNotEmpty) { |
| 1913 | const int kMaxSockets = 3; |
| 1914 | const int kMaxSocketsPerGroup = 2; |
| 1915 | CreatePool(kMaxSockets, kMaxSocketsPerGroup); |
| 1916 | |
[email protected] | ac790b4 | 2009-12-02 04:31:31 | [diff] [blame] | 1917 | const RequestPriority kHighPriority = HIGHEST; |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1918 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1919 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1920 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1921 | |
| 1922 | // This is going to be a pending request in an otherwise empty group. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1923 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1924 | IsError(ERR_IO_PENDING)); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1925 | |
| 1926 | // Reach the maximum socket limit. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1927 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1928 | |
| 1929 | // Create a stalled group with high priorities. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1930 | EXPECT_THAT(StartRequest(TestGroupId("c"), kHighPriority), |
| 1931 | IsError(ERR_IO_PENDING)); |
| 1932 | EXPECT_THAT(StartRequest(TestGroupId("c"), kHighPriority), |
| 1933 | IsError(ERR_IO_PENDING)); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1934 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1935 | // Release the first two sockets from TestGroupId("a"). Because this is a |
| 1936 | // keepalive, the first release will unblock the pending request for |
| 1937 | // TestGroupId("a"). The second release will unblock a request for "c", |
| 1938 | // because it is the next high priority socket. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1939 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
| 1940 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1941 | |
| 1942 | // Closing idle sockets should not get us into trouble, but in the bug |
| 1943 | // we were hitting a CHECK here. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1944 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1945 | pool_->CloseIdleSockets(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1946 | |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 1947 | // Run the released socket wakeups. |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 1948 | base::RunLoop().RunUntilIdle(); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1949 | } |
| 1950 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 1951 | TEST_F(ClientSocketPoolBaseTest, BasicAsynchronous) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1952 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1953 | |
| 1954 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1955 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1956 | TestCompletionCallback callback; |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 1957 | BoundTestNetLog log; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1958 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1959 | TestGroupId("a"), params_, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1960 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1961 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1962 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1963 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 1964 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 1965 | TestLoadTimingInfoNotConnected(handle); |
| 1966 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1967 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1968 | EXPECT_TRUE(handle.is_initialized()); |
| 1969 | EXPECT_TRUE(handle.socket()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 1970 | TestLoadTimingInfoConnectedNotReused(handle); |
| 1971 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1972 | handle.Reset(); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 1973 | TestLoadTimingInfoNotConnected(handle); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 1974 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 1975 | TestNetLogEntry::List entries; |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 1976 | log.GetEntries(&entries); |
| 1977 | |
| 1978 | EXPECT_EQ(4u, entries.size()); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 1979 | EXPECT_TRUE(LogContainsBeginEvent(entries, 0, NetLogEventType::SOCKET_POOL)); |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 1980 | EXPECT_TRUE(LogContainsEvent( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 1981 | entries, 1, NetLogEventType::SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
| 1982 | NetLogEventPhase::NONE)); |
| 1983 | EXPECT_TRUE(LogContainsEvent(entries, 2, |
| 1984 | NetLogEventType::SOCKET_POOL_BOUND_TO_SOCKET, |
| 1985 | NetLogEventPhase::NONE)); |
| 1986 | EXPECT_TRUE(LogContainsEndEvent(entries, 3, NetLogEventType::SOCKET_POOL)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1987 | } |
| 1988 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 1989 | TEST_F(ClientSocketPoolBaseTest, |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1990 | InitConnectionAsynchronousFailure) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1991 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1992 | |
| 1993 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1994 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1995 | TestCompletionCallback callback; |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 1996 | BoundTestNetLog log; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 1997 | // Set the additional error state members to ensure that they get cleared. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1998 | handle.set_is_ssl_error(true); |
[email protected] | 8b49869 | 2010-07-16 17:11:43 | [diff] [blame] | 1999 | HttpResponseInfo info; |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 2000 | info.headers = new HttpResponseHeaders(std::string()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2001 | handle.set_ssl_error_response_info(info); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2002 | EXPECT_EQ( |
| 2003 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2004 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2005 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2006 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2007 | log.bound())); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2008 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2009 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2010 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2011 | EXPECT_FALSE(handle.is_ssl_error()); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 2012 | EXPECT_TRUE(handle.ssl_error_response_info().headers.get() == nullptr); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 2013 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 2014 | TestNetLogEntry::List entries; |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 2015 | log.GetEntries(&entries); |
| 2016 | |
| 2017 | EXPECT_EQ(3u, entries.size()); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 2018 | EXPECT_TRUE(LogContainsBeginEvent(entries, 0, NetLogEventType::SOCKET_POOL)); |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 2019 | EXPECT_TRUE(LogContainsEvent( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 2020 | entries, 1, NetLogEventType::SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
| 2021 | NetLogEventPhase::NONE)); |
| 2022 | EXPECT_TRUE(LogContainsEndEvent(entries, 2, NetLogEventType::SOCKET_POOL)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2023 | } |
| 2024 | |
mmenke | 6be122f | 2015-03-09 22:22:47 | [diff] [blame] | 2025 | // Check that an async ConnectJob failure does not result in creation of a new |
| 2026 | // ConnectJob when there's another pending request also waiting on its own |
| 2027 | // ConnectJob. See http://crbug.com/463960. |
| 2028 | TEST_F(ClientSocketPoolBaseTest, AsyncFailureWithPendingRequestWithJob) { |
| 2029 | CreatePool(2, 2); |
| 2030 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
| 2031 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2032 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2033 | IsError(ERR_IO_PENDING)); |
| 2034 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2035 | IsError(ERR_IO_PENDING)); |
mmenke | 6be122f | 2015-03-09 22:22:47 | [diff] [blame] | 2036 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2037 | EXPECT_THAT(request(0)->WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 2038 | EXPECT_THAT(request(1)->WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
mmenke | 6be122f | 2015-03-09 22:22:47 | [diff] [blame] | 2039 | |
| 2040 | EXPECT_EQ(2, client_socket_factory_.allocation_count()); |
| 2041 | } |
| 2042 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2043 | TEST_F(ClientSocketPoolBaseTest, TwoRequestsCancelOne) { |
[email protected] | b22b516 | 2010-03-16 07:53:10 | [diff] [blame] | 2044 | // TODO(eroman): Add back the log expectations! Removed them because the |
| 2045 | // ordering is difficult, and some may fire during destructor. |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2046 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2047 | |
| 2048 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2049 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2050 | TestCompletionCallback callback; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2051 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2052 | TestCompletionCallback callback2; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2053 | |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2054 | EXPECT_EQ( |
| 2055 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2056 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2057 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2058 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2059 | NetLogWithSource())); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 2060 | BoundTestNetLog log2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 2061 | EXPECT_EQ( |
| 2062 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2063 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 2064 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2065 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2066 | pool_.get(), NetLogWithSource())); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2067 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2068 | handle.Reset(); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2069 | |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 2070 | |
| 2071 | // 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] | 2072 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2073 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2074 | handle2.Reset(); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 2075 | |
| 2076 | // Now request 2 has actually finished. |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 2077 | // TODO(eroman): Add back log expectations. |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2078 | } |
| 2079 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2080 | TEST_F(ClientSocketPoolBaseTest, CancelRequestLimitsJobs) { |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 2081 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2082 | |
[email protected] | 17a0c6c | 2009-08-04 00:07:04 | [diff] [blame] | 2083 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2084 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2085 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
| 2086 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 2087 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 2088 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 2089 | |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2090 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2091 | static_cast<int>(pool_->NumConnectJobsInGroup(TestGroupId("a")))); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2092 | (*requests())[2]->handle()->Reset(); |
| 2093 | (*requests())[3]->handle()->Reset(); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2094 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2095 | static_cast<int>(pool_->NumConnectJobsInGroup(TestGroupId("a")))); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 2096 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2097 | (*requests())[1]->handle()->Reset(); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2098 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2099 | static_cast<int>(pool_->NumConnectJobsInGroup(TestGroupId("a")))); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 2100 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2101 | (*requests())[0]->handle()->Reset(); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2102 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2103 | static_cast<int>(pool_->NumConnectJobsInGroup(TestGroupId("a")))); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 2104 | } |
| 2105 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2106 | // When requests and ConnectJobs are not coupled, the request will get serviced |
| 2107 | // by whatever comes first. |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2108 | TEST_F(ClientSocketPoolBaseTest, ReleaseSockets) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2109 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2110 | |
| 2111 | // Start job 1 (async OK) |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 2112 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2113 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2114 | std::vector<TestSocketRequest*> request_order; |
| 2115 | size_t completion_count; // unused |
| 2116 | TestSocketRequest req1(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2117 | int rv = req1.handle()->Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2118 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2119 | ClientSocketPool::RespectLimits::ENABLED, req1.callback(), |
| 2120 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2121 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 2122 | EXPECT_THAT(req1.WaitForResult(), IsOk()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2123 | |
| 2124 | // Job 1 finished OK. Start job 2 (also async OK). Request 3 is pending |
| 2125 | // without a job. |
| 2126 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2127 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2128 | TestSocketRequest req2(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2129 | rv = req2.handle()->Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2130 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2131 | ClientSocketPool::RespectLimits::ENABLED, req2.callback(), |
| 2132 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2133 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2134 | TestSocketRequest req3(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2135 | rv = req3.handle()->Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2136 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2137 | ClientSocketPool::RespectLimits::ENABLED, req3.callback(), |
| 2138 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2139 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2140 | |
| 2141 | // Both Requests 2 and 3 are pending. We release socket 1 which should |
| 2142 | // service request 2. Request 3 should still be waiting. |
[email protected] | a6c59f6 | 2009-07-29 16:33:33 | [diff] [blame] | 2143 | req1.handle()->Reset(); |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 2144 | // Run the released socket wakeups. |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 2145 | base::RunLoop().RunUntilIdle(); |
[email protected] | a6c59f6 | 2009-07-29 16:33:33 | [diff] [blame] | 2146 | ASSERT_TRUE(req2.handle()->socket()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2147 | EXPECT_THAT(req2.WaitForResult(), IsOk()); |
[email protected] | a6c59f6 | 2009-07-29 16:33:33 | [diff] [blame] | 2148 | EXPECT_FALSE(req3.handle()->socket()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2149 | |
| 2150 | // Signal job 2, which should service request 3. |
| 2151 | |
| 2152 | client_socket_factory_.SignalJobs(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2153 | EXPECT_THAT(req3.WaitForResult(), IsOk()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2154 | |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2155 | ASSERT_EQ(3u, request_order.size()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2156 | EXPECT_EQ(&req1, request_order[0]); |
| 2157 | EXPECT_EQ(&req2, request_order[1]); |
| 2158 | EXPECT_EQ(&req3, request_order[2]); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2159 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2160 | } |
| 2161 | |
| 2162 | // The requests are not coupled to the jobs. So, the requests should finish in |
| 2163 | // their priority / insertion order. |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2164 | TEST_F(ClientSocketPoolBaseTest, PendingJobCompletionOrder) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2165 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2166 | // First two jobs are async. |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 2167 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2168 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2169 | std::vector<TestSocketRequest*> request_order; |
| 2170 | size_t completion_count; // unused |
| 2171 | TestSocketRequest req1(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2172 | int rv = req1.handle()->Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2173 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2174 | ClientSocketPool::RespectLimits::ENABLED, req1.callback(), |
| 2175 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2176 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2177 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2178 | TestSocketRequest req2(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2179 | rv = req2.handle()->Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2180 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2181 | ClientSocketPool::RespectLimits::ENABLED, req2.callback(), |
| 2182 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2183 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2184 | |
| 2185 | // The pending job is sync. |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 2186 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2187 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2188 | TestSocketRequest req3(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2189 | rv = req3.handle()->Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2190 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2191 | ClientSocketPool::RespectLimits::ENABLED, req3.callback(), |
| 2192 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2193 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2194 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2195 | EXPECT_THAT(req1.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 2196 | EXPECT_THAT(req2.WaitForResult(), IsOk()); |
| 2197 | EXPECT_THAT(req3.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2198 | |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2199 | ASSERT_EQ(3u, request_order.size()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2200 | EXPECT_EQ(&req1, request_order[0]); |
| 2201 | EXPECT_EQ(&req2, request_order[1]); |
| 2202 | EXPECT_EQ(&req3, request_order[2]); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2203 | } |
| 2204 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2205 | // Test GetLoadState in the case there's only one socket request. |
| 2206 | TEST_F(ClientSocketPoolBaseTest, LoadStateOneRequest) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2207 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2208 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2209 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2210 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2211 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2212 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2213 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2214 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2215 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2216 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2217 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2218 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2219 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE); |
| 2220 | EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle.GetLoadState()); |
| 2221 | |
| 2222 | // No point in completing the connection, since ClientSocketHandles only |
| 2223 | // expect the LoadState to be checked while connecting. |
| 2224 | } |
| 2225 | |
| 2226 | // Test GetLoadState in the case there are two socket requests. |
| 2227 | TEST_F(ClientSocketPoolBaseTest, LoadStateTwoRequests) { |
| 2228 | CreatePool(2, 2); |
| 2229 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2230 | |
| 2231 | ClientSocketHandle handle; |
| 2232 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2233 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2234 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2235 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2236 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2237 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2238 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_RESOLVING_HOST); |
| 2239 | |
| 2240 | ClientSocketHandle handle2; |
| 2241 | TestCompletionCallback callback2; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2242 | rv = handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 2243 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2244 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2245 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2246 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2247 | client_socket_factory_.SetJobLoadState(1, LOAD_STATE_RESOLVING_HOST); |
| 2248 | |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 2249 | // Each handle should reflect the state of its own job. |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2250 | EXPECT_EQ(LOAD_STATE_RESOLVING_HOST, handle.GetLoadState()); |
| 2251 | EXPECT_EQ(LOAD_STATE_RESOLVING_HOST, handle2.GetLoadState()); |
| 2252 | |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 2253 | // Update the state of the first job. |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2254 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_CONNECTING); |
| 2255 | |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 2256 | // Only the state of the first request should have changed. |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2257 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2258 | EXPECT_EQ(LOAD_STATE_RESOLVING_HOST, handle2.GetLoadState()); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 2259 | |
| 2260 | // Update the state of the second job. |
| 2261 | client_socket_factory_.SetJobLoadState(1, LOAD_STATE_SSL_HANDSHAKE); |
| 2262 | |
| 2263 | // Only the state of the second request should have changed. |
| 2264 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
| 2265 | EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle2.GetLoadState()); |
| 2266 | |
| 2267 | // Second job connects and the first request gets the socket. The |
| 2268 | // second handle switches to the state of the remaining ConnectJob. |
| 2269 | client_socket_factory_.SignalJob(1); |
| 2270 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 2271 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle2.GetLoadState()); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2272 | } |
| 2273 | |
| 2274 | // Test GetLoadState in the case the per-group limit is reached. |
| 2275 | TEST_F(ClientSocketPoolBaseTest, LoadStateGroupLimit) { |
| 2276 | CreatePool(2, 1); |
| 2277 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2278 | |
| 2279 | ClientSocketHandle handle; |
| 2280 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2281 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2282 | TestGroupId("a"), params_, MEDIUM, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2283 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2284 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2285 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2286 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
| 2287 | |
| 2288 | // Request another socket from the same pool, buth with a higher priority. |
| 2289 | // The first request should now be stalled at the socket group limit. |
| 2290 | ClientSocketHandle handle2; |
| 2291 | TestCompletionCallback callback2; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2292 | rv = handle2.Init(TestGroupId("a"), params_, HIGHEST, SocketTag(), |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 2293 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2294 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2295 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2296 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2297 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET, handle.GetLoadState()); |
| 2298 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle2.GetLoadState()); |
| 2299 | |
| 2300 | // The first handle should remain stalled as the other socket goes through |
| 2301 | // the connect process. |
| 2302 | |
| 2303 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE); |
| 2304 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET, handle.GetLoadState()); |
| 2305 | EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle2.GetLoadState()); |
| 2306 | |
| 2307 | client_socket_factory_.SignalJob(0); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2308 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2309 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET, handle.GetLoadState()); |
| 2310 | |
| 2311 | // Closing the second socket should cause the stalled handle to finally get a |
| 2312 | // ConnectJob. |
| 2313 | handle2.socket()->Disconnect(); |
| 2314 | handle2.Reset(); |
| 2315 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
| 2316 | } |
| 2317 | |
| 2318 | // Test GetLoadState in the case the per-pool limit is reached. |
| 2319 | TEST_F(ClientSocketPoolBaseTest, LoadStatePoolLimit) { |
| 2320 | CreatePool(2, 2); |
| 2321 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2322 | |
| 2323 | ClientSocketHandle handle; |
| 2324 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2325 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2326 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2327 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2328 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2329 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2330 | |
| 2331 | // Request for socket from another pool. |
| 2332 | ClientSocketHandle handle2; |
| 2333 | TestCompletionCallback callback2; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2334 | rv = handle2.Init(TestGroupId("b"), params_, DEFAULT_PRIORITY, SocketTag(), |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 2335 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2336 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2337 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2338 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2339 | |
| 2340 | // Request another socket from the first pool. Request should stall at the |
| 2341 | // socket pool limit. |
| 2342 | ClientSocketHandle handle3; |
| 2343 | TestCompletionCallback callback3; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2344 | rv = handle3.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 2345 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2346 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2347 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2348 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2349 | |
| 2350 | // The third handle should remain stalled as the other sockets in its group |
| 2351 | // goes through the connect process. |
| 2352 | |
| 2353 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
| 2354 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL, handle3.GetLoadState()); |
| 2355 | |
| 2356 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE); |
| 2357 | EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle.GetLoadState()); |
| 2358 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL, handle3.GetLoadState()); |
| 2359 | |
| 2360 | client_socket_factory_.SignalJob(0); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2361 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2362 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL, handle3.GetLoadState()); |
| 2363 | |
| 2364 | // Closing a socket should allow the stalled handle to finally get a new |
| 2365 | // ConnectJob. |
| 2366 | handle.socket()->Disconnect(); |
| 2367 | handle.Reset(); |
| 2368 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle3.GetLoadState()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2369 | } |
| 2370 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2371 | TEST_F(ClientSocketPoolBaseTest, CertError) { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2372 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2373 | connect_job_factory_->set_job_type(TestConnectJob::kMockCertErrorJob); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2374 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2375 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2376 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2377 | EXPECT_EQ( |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2378 | ERR_CERT_COMMON_NAME_INVALID, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2379 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2380 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2381 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2382 | NetLogWithSource())); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2383 | EXPECT_TRUE(handle.is_initialized()); |
| 2384 | EXPECT_TRUE(handle.socket()); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2385 | } |
| 2386 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2387 | TEST_F(ClientSocketPoolBaseTest, AsyncCertError) { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2388 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2389 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2390 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingCertErrorJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2391 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2392 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2393 | EXPECT_EQ( |
| 2394 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2395 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2396 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2397 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2398 | NetLogWithSource())); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2399 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2400 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2401 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_CERT_COMMON_NAME_INVALID)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2402 | EXPECT_TRUE(handle.is_initialized()); |
| 2403 | EXPECT_TRUE(handle.socket()); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2404 | } |
| 2405 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2406 | TEST_F(ClientSocketPoolBaseTest, AdditionalErrorStateSynchronous) { |
| 2407 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2408 | connect_job_factory_->set_job_type( |
| 2409 | TestConnectJob::kMockAdditionalErrorStateJob); |
| 2410 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2411 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2412 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2413 | EXPECT_EQ( |
| 2414 | ERR_CONNECTION_FAILED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2415 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2416 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2417 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2418 | NetLogWithSource())); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2419 | EXPECT_FALSE(handle.is_initialized()); |
| 2420 | EXPECT_FALSE(handle.socket()); |
| 2421 | EXPECT_TRUE(handle.is_ssl_error()); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 2422 | EXPECT_FALSE(handle.ssl_error_response_info().headers.get() == nullptr); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2423 | } |
| 2424 | |
| 2425 | TEST_F(ClientSocketPoolBaseTest, AdditionalErrorStateAsynchronous) { |
| 2426 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2427 | |
| 2428 | connect_job_factory_->set_job_type( |
| 2429 | TestConnectJob::kMockPendingAdditionalErrorStateJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2430 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2431 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2432 | EXPECT_EQ( |
| 2433 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2434 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2435 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2436 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2437 | NetLogWithSource())); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2438 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2439 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2440 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2441 | EXPECT_FALSE(handle.is_initialized()); |
| 2442 | EXPECT_FALSE(handle.socket()); |
| 2443 | EXPECT_TRUE(handle.is_ssl_error()); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 2444 | EXPECT_FALSE(handle.ssl_error_response_info().headers.get() == nullptr); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2445 | } |
| 2446 | |
martijn | 003cd61 | 2016-05-19 22:24:38 | [diff] [blame] | 2447 | // Make sure we can reuse sockets. |
| 2448 | TEST_F(ClientSocketPoolBaseTest, CleanupTimedOutIdleSocketsReuse) { |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2449 | CreatePoolWithIdleTimeouts( |
| 2450 | kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2451 | base::TimeDelta(), // Time out unused sockets immediately. |
| 2452 | base::TimeDelta::FromDays(1)); // Don't time out used sockets. |
| 2453 | |
| 2454 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2455 | |
| 2456 | ClientSocketHandle handle; |
| 2457 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2458 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2459 | TestGroupId("a"), params_, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2460 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2461 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2462 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2463 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2464 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2465 | ASSERT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2466 | |
| 2467 | // Use and release the socket. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 2468 | EXPECT_EQ(1, handle.socket()->Write(nullptr, 1, CompletionOnceCallback(), |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 2469 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 2470 | TestLoadTimingInfoConnectedNotReused(handle); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2471 | handle.Reset(); |
| 2472 | |
| 2473 | // Should now have one idle socket. |
| 2474 | ASSERT_EQ(1, pool_->IdleSocketCount()); |
| 2475 | |
| 2476 | // Request a new socket. This should reuse the old socket and complete |
| 2477 | // synchronously. |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 2478 | BoundTestNetLog log; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2479 | rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2480 | TestGroupId("a"), params_, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2481 | ClientSocketPool::RespectLimits::ENABLED, CompletionOnceCallback(), |
| 2482 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2483 | ASSERT_THAT(rv, IsOk()); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2484 | EXPECT_TRUE(handle.is_reused()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 2485 | TestLoadTimingInfoConnectedReused(handle); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2486 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2487 | ASSERT_TRUE(pool_->HasGroup(TestGroupId("a"))); |
| 2488 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
| 2489 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroup(TestGroupId("a"))); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2490 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 2491 | TestNetLogEntry::List entries; |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2492 | log.GetEntries(&entries); |
| 2493 | EXPECT_TRUE(LogContainsEntryWithType( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 2494 | entries, 1, NetLogEventType::SOCKET_POOL_REUSED_AN_EXISTING_SOCKET)); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2495 | } |
| 2496 | |
martijn | 003cd61 | 2016-05-19 22:24:38 | [diff] [blame] | 2497 | // Make sure we cleanup old unused sockets. |
Eric Roman | b49715e | 2018-04-24 22:41:17 | [diff] [blame] | 2498 | TEST_F(ClientSocketPoolBaseTest, CleanupTimedOutIdleSocketsNoReuse) { |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2499 | CreatePoolWithIdleTimeouts( |
| 2500 | kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 2501 | base::TimeDelta(), // Time out unused sockets immediately |
| 2502 | base::TimeDelta()); // Time out used sockets immediately |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2503 | |
| 2504 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2505 | |
| 2506 | // Startup two mock pending connect jobs, which will sit in the MessageLoop. |
| 2507 | |
| 2508 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2509 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2510 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2511 | TestGroupId("a"), params_, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2512 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2513 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2514 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2515 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2516 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2517 | |
| 2518 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2519 | TestCompletionCallback callback2; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2520 | rv = handle2.Init(TestGroupId("a"), params_, LOWEST, SocketTag(), |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 2521 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2522 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2523 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2524 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2525 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2526 | pool_->GetLoadState(TestGroupId("a"), &handle2)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2527 | |
| 2528 | // Cancel one of the requests. Wait for the other, which will get the first |
| 2529 | // job. Release the socket. Run the loop again to make sure the second |
| 2530 | // socket is sitting idle and the first one is released (since ReleaseSocket() |
| 2531 | // just posts a DoReleaseSocket() task). |
| 2532 | |
| 2533 | handle.Reset(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2534 | ASSERT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2535 | // Use the socket. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 2536 | EXPECT_EQ(1, handle2.socket()->Write(nullptr, 1, CompletionOnceCallback(), |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 2537 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2538 | handle2.Reset(); |
| 2539 | |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2540 | // We post all of our delayed tasks with a 2ms delay. I.e. they don't |
| 2541 | // actually become pending until 2ms after they have been created. In order |
| 2542 | // to flush all tasks, we need to wait so that we know there are no |
| 2543 | // soon-to-be-pending tasks waiting. |
Alex Clarke | 0def209 | 2018-12-10 12:01:45 | [diff] [blame] | 2544 | FastForwardBy(base::TimeDelta::FromMilliseconds(10)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2545 | |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2546 | // Both sockets should now be idle. |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2547 | ASSERT_EQ(2, pool_->IdleSocketCount()); |
| 2548 | |
| 2549 | // Request a new socket. This should cleanup the unused and timed out ones. |
| 2550 | // A new socket will be created rather than reusing the idle one. |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 2551 | BoundTestNetLog log; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2552 | TestCompletionCallback callback3; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2553 | rv = handle.Init(TestGroupId("a"), params_, LOWEST, SocketTag(), |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 2554 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2555 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2556 | pool_.get(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2557 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 2558 | ASSERT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2559 | EXPECT_FALSE(handle.is_reused()); |
| 2560 | |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2561 | // Make sure the idle socket is closed. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2562 | ASSERT_TRUE(pool_->HasGroup(TestGroupId("a"))); |
| 2563 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
| 2564 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroup(TestGroupId("a"))); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2565 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 2566 | TestNetLogEntry::List entries; |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2567 | log.GetEntries(&entries); |
| 2568 | EXPECT_FALSE(LogContainsEntryWithType( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 2569 | entries, 1, NetLogEventType::SOCKET_POOL_REUSED_AN_EXISTING_SOCKET)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2570 | } |
| 2571 | |
[email protected] | 2041cf34 | 2010-02-19 03:15:59 | [diff] [blame] | 2572 | // Make sure that we process all pending requests even when we're stalling |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2573 | // because of multiple releasing disconnected sockets. |
| 2574 | TEST_F(ClientSocketPoolBaseTest, MultipleReleasingDisconnectedSockets) { |
| 2575 | CreatePoolWithIdleTimeouts( |
| 2576 | kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 2577 | base::TimeDelta(), // Time out unused sockets immediately. |
| 2578 | base::TimeDelta::FromDays(1)); // Don't time out used sockets. |
| 2579 | |
| 2580 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 2581 | |
| 2582 | // Startup 4 connect jobs. Two of them will be pending. |
| 2583 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2584 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2585 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2586 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2587 | TestGroupId("a"), params_, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2588 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2589 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2590 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2591 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2592 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2593 | TestCompletionCallback callback2; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2594 | rv = handle2.Init(TestGroupId("a"), params_, LOWEST, SocketTag(), |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 2595 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2596 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2597 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2598 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2599 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2600 | ClientSocketHandle handle3; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2601 | TestCompletionCallback callback3; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2602 | rv = handle3.Init(TestGroupId("a"), params_, LOWEST, SocketTag(), |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 2603 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2604 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2605 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2606 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2607 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2608 | ClientSocketHandle handle4; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2609 | TestCompletionCallback callback4; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2610 | rv = handle4.Init(TestGroupId("a"), params_, LOWEST, SocketTag(), |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 2611 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2612 | callback4.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2613 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2614 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2615 | |
| 2616 | // Release two disconnected sockets. |
| 2617 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2618 | handle.socket()->Disconnect(); |
| 2619 | handle.Reset(); |
| 2620 | handle2.socket()->Disconnect(); |
| 2621 | handle2.Reset(); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2622 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2623 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2624 | EXPECT_FALSE(handle3.is_reused()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2625 | EXPECT_THAT(callback4.WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2626 | EXPECT_FALSE(handle4.is_reused()); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2627 | } |
| 2628 | |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2629 | // Regression test for http://crbug.com/42267. |
| 2630 | // When DoReleaseSocket() is processed for one socket, it is blocked because the |
| 2631 | // other stalled groups all have releasing sockets, so no progress can be made. |
| 2632 | TEST_F(ClientSocketPoolBaseTest, SocketLimitReleasingSockets) { |
| 2633 | CreatePoolWithIdleTimeouts( |
| 2634 | 4 /* socket limit */, 4 /* socket limit per group */, |
| 2635 | base::TimeDelta(), // Time out unused sockets immediately. |
| 2636 | base::TimeDelta::FromDays(1)); // Don't time out used sockets. |
| 2637 | |
| 2638 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 2639 | |
| 2640 | // Max out the socket limit with 2 per group. |
| 2641 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2642 | ClientSocketHandle handle_a[4]; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2643 | TestCompletionCallback callback_a[4]; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2644 | ClientSocketHandle handle_b[4]; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2645 | TestCompletionCallback callback_b[4]; |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2646 | |
| 2647 | for (int i = 0; i < 2; ++i) { |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2648 | EXPECT_EQ( |
| 2649 | OK, handle_a[i].Init(TestGroupId("a"), params_, LOWEST, SocketTag(), |
| 2650 | ClientSocketPool::RespectLimits::ENABLED, |
| 2651 | callback_a[i].callback(), |
| 2652 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2653 | NetLogWithSource())); |
| 2654 | EXPECT_EQ( |
| 2655 | OK, handle_b[i].Init(TestGroupId("b"), params_, LOWEST, SocketTag(), |
| 2656 | ClientSocketPool::RespectLimits::ENABLED, |
| 2657 | callback_b[i].callback(), |
| 2658 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2659 | NetLogWithSource())); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2660 | } |
[email protected] | b89f7e4 | 2010-05-20 20:37:00 | [diff] [blame] | 2661 | |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2662 | // Make 4 pending requests, 2 per group. |
| 2663 | |
| 2664 | for (int i = 2; i < 4; ++i) { |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 2665 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2666 | handle_a[i].Init(TestGroupId("a"), params_, LOWEST, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 2667 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2668 | callback_a[i].callback(), |
| 2669 | ClientSocketPool::ProxyAuthCallback(), |
| 2670 | pool_.get(), NetLogWithSource())); |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 2671 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2672 | handle_b[i].Init(TestGroupId("b"), params_, LOWEST, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 2673 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2674 | callback_b[i].callback(), |
| 2675 | ClientSocketPool::ProxyAuthCallback(), |
| 2676 | pool_.get(), NetLogWithSource())); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2677 | } |
| 2678 | |
| 2679 | // Release b's socket first. The order is important, because in |
| 2680 | // DoReleaseSocket(), we'll process b's released socket, and since both b and |
| 2681 | // a are stalled, but 'a' is lower lexicographically, we'll process group 'a' |
| 2682 | // first, which has a releasing socket, so it refuses to start up another |
| 2683 | // ConnectJob. So, we used to infinite loop on this. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2684 | handle_b[0].socket()->Disconnect(); |
| 2685 | handle_b[0].Reset(); |
| 2686 | handle_a[0].socket()->Disconnect(); |
| 2687 | handle_a[0].Reset(); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2688 | |
| 2689 | // Used to get stuck here. |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 2690 | base::RunLoop().RunUntilIdle(); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2691 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2692 | handle_b[1].socket()->Disconnect(); |
| 2693 | handle_b[1].Reset(); |
| 2694 | handle_a[1].socket()->Disconnect(); |
| 2695 | handle_a[1].Reset(); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2696 | |
| 2697 | for (int i = 2; i < 4; ++i) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2698 | EXPECT_THAT(callback_b[i].WaitForResult(), IsOk()); |
| 2699 | EXPECT_THAT(callback_a[i].WaitForResult(), IsOk()); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2700 | } |
| 2701 | } |
| 2702 | |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2703 | TEST_F(ClientSocketPoolBaseTest, |
| 2704 | ReleasingDisconnectedSocketsMaintainsPriorityOrder) { |
| 2705 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2706 | |
| 2707 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2708 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2709 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2710 | IsError(ERR_IO_PENDING)); |
| 2711 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2712 | IsError(ERR_IO_PENDING)); |
| 2713 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2714 | IsError(ERR_IO_PENDING)); |
| 2715 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2716 | IsError(ERR_IO_PENDING)); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2717 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2718 | EXPECT_THAT((*requests())[0]->WaitForResult(), IsOk()); |
| 2719 | EXPECT_THAT((*requests())[1]->WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2720 | EXPECT_EQ(2u, completion_count()); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2721 | |
| 2722 | // Releases one connection. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2723 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::NO_KEEP_ALIVE)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2724 | EXPECT_THAT((*requests())[2]->WaitForResult(), IsOk()); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2725 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2726 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::NO_KEEP_ALIVE)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2727 | EXPECT_THAT((*requests())[3]->WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2728 | EXPECT_EQ(4u, completion_count()); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2729 | |
| 2730 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 2731 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 2732 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 2733 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 2734 | |
| 2735 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2736 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(5)); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2737 | } |
| 2738 | |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2739 | class TestReleasingSocketRequest : public TestCompletionCallbackBase { |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2740 | public: |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2741 | TestReleasingSocketRequest(TestClientSocketPool* pool, |
| 2742 | int expected_result, |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2743 | bool reset_releasing_handle) |
| 2744 | : pool_(pool), |
| 2745 | expected_result_(expected_result), |
Bence Béky | 8ddc249 | 2018-06-13 01:02:04 | [diff] [blame] | 2746 | reset_releasing_handle_(reset_releasing_handle) {} |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2747 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 2748 | ~TestReleasingSocketRequest() override = default; |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2749 | |
| 2750 | ClientSocketHandle* handle() { return &handle_; } |
| 2751 | |
Bence Béky | 8ddc249 | 2018-06-13 01:02:04 | [diff] [blame] | 2752 | CompletionOnceCallback callback() { |
| 2753 | return base::BindOnce(&TestReleasingSocketRequest::OnComplete, |
| 2754 | base::Unretained(this)); |
| 2755 | } |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2756 | |
| 2757 | private: |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2758 | void OnComplete(int result) { |
| 2759 | SetResult(result); |
| 2760 | if (reset_releasing_handle_) |
| 2761 | handle_.Reset(); |
| 2762 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 2763 | scoped_refptr<TestSocketParams> con_params(new TestSocketParams()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2764 | EXPECT_EQ( |
| 2765 | expected_result_, |
| 2766 | handle2_.Init( |
| 2767 | TestGroupId("a"), con_params, DEFAULT_PRIORITY, SocketTag(), |
| 2768 | ClientSocketPool::RespectLimits::ENABLED, CompletionOnceCallback(), |
| 2769 | ClientSocketPool::ProxyAuthCallback(), pool_, NetLogWithSource())); |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2770 | } |
| 2771 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2772 | TestClientSocketPool* const pool_; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2773 | int expected_result_; |
| 2774 | bool reset_releasing_handle_; |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2775 | ClientSocketHandle handle_; |
| 2776 | ClientSocketHandle handle2_; |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2777 | }; |
| 2778 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2779 | |
| 2780 | TEST_F(ClientSocketPoolBaseTest, AdditionalErrorSocketsDontUseSlot) { |
| 2781 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2782 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2783 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
| 2784 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 2785 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2786 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2787 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2788 | client_socket_factory_.allocation_count()); |
| 2789 | |
| 2790 | connect_job_factory_->set_job_type( |
| 2791 | TestConnectJob::kMockPendingAdditionalErrorStateJob); |
| 2792 | TestReleasingSocketRequest req(pool_.get(), OK, false); |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 2793 | EXPECT_EQ( |
| 2794 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2795 | req.handle()->Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, |
| 2796 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2797 | req.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2798 | pool_.get(), NetLogWithSource())); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2799 | // The next job should complete synchronously |
| 2800 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 2801 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2802 | EXPECT_THAT(req.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2803 | EXPECT_FALSE(req.handle()->is_initialized()); |
| 2804 | EXPECT_FALSE(req.handle()->socket()); |
| 2805 | EXPECT_TRUE(req.handle()->is_ssl_error()); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 2806 | EXPECT_FALSE(req.handle()->ssl_error_response_info().headers.get() == |
| 2807 | nullptr); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2808 | } |
| 2809 | |
[email protected] | b6501d3d | 2010-06-03 23:53:34 | [diff] [blame] | 2810 | // http://crbug.com/44724 regression test. |
| 2811 | // We start releasing the pool when we flush on network change. When that |
| 2812 | // happens, the only active references are in the ClientSocketHandles. When a |
| 2813 | // ConnectJob completes and calls back into the last ClientSocketHandle, that |
| 2814 | // callback can release the last reference and delete the pool. After the |
| 2815 | // callback finishes, we go back to the stack frame within the now-deleted pool. |
| 2816 | // Executing any code that refers to members of the now-deleted pool can cause |
| 2817 | // crashes. |
| 2818 | TEST_F(ClientSocketPoolBaseTest, CallbackThatReleasesPool) { |
| 2819 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2820 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
| 2821 | |
| 2822 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2823 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2824 | EXPECT_EQ( |
| 2825 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2826 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2827 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2828 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2829 | NetLogWithSource())); |
[email protected] | b6501d3d | 2010-06-03 23:53:34 | [diff] [blame] | 2830 | |
[email protected] | 7af985a | 2012-12-14 22:40:42 | [diff] [blame] | 2831 | pool_->FlushWithError(ERR_NETWORK_CHANGED); |
[email protected] | b6501d3d | 2010-06-03 23:53:34 | [diff] [blame] | 2832 | |
| 2833 | // We'll call back into this now. |
| 2834 | callback.WaitForResult(); |
| 2835 | } |
| 2836 | |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2837 | TEST_F(ClientSocketPoolBaseTest, DoNotReuseSocketAfterFlush) { |
| 2838 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2839 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2840 | |
| 2841 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2842 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2843 | EXPECT_EQ( |
| 2844 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2845 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2846 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2847 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2848 | NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2849 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2850 | EXPECT_EQ(ClientSocketHandle::UNUSED, handle.reuse_type()); |
| 2851 | |
[email protected] | 7af985a | 2012-12-14 22:40:42 | [diff] [blame] | 2852 | pool_->FlushWithError(ERR_NETWORK_CHANGED); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2853 | |
| 2854 | handle.Reset(); |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 2855 | base::RunLoop().RunUntilIdle(); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2856 | |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2857 | EXPECT_EQ( |
| 2858 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2859 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2860 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2861 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2862 | NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2863 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2864 | EXPECT_EQ(ClientSocketHandle::UNUSED, handle.reuse_type()); |
| 2865 | } |
| 2866 | |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2867 | class ConnectWithinCallback : public TestCompletionCallbackBase { |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2868 | public: |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2869 | ConnectWithinCallback(const ClientSocketPool::GroupId& group_id, |
Bence Béky | 8ddc249 | 2018-06-13 01:02:04 | [diff] [blame] | 2870 | const scoped_refptr<TestSocketParams>& params, |
| 2871 | TestClientSocketPool* pool) |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2872 | : group_id_(group_id), params_(params), pool_(pool) {} |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2873 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 2874 | ~ConnectWithinCallback() override = default; |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2875 | |
| 2876 | int WaitForNestedResult() { |
| 2877 | return nested_callback_.WaitForResult(); |
| 2878 | } |
| 2879 | |
Bence Béky | 8ddc249 | 2018-06-13 01:02:04 | [diff] [blame] | 2880 | CompletionOnceCallback callback() { |
| 2881 | return base::BindOnce(&ConnectWithinCallback::OnComplete, |
| 2882 | base::Unretained(this)); |
| 2883 | } |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2884 | |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2885 | private: |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2886 | void OnComplete(int result) { |
| 2887 | SetResult(result); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2888 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2889 | handle_.Init(group_id_, params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2890 | ClientSocketPool::RespectLimits::ENABLED, |
| 2891 | nested_callback_.callback(), |
| 2892 | ClientSocketPool::ProxyAuthCallback(), pool_, |
| 2893 | NetLogWithSource())); |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2894 | } |
| 2895 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2896 | const ClientSocketPool::GroupId group_id_; |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2897 | const scoped_refptr<TestSocketParams> params_; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2898 | TestClientSocketPool* const pool_; |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2899 | ClientSocketHandle handle_; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2900 | TestCompletionCallback nested_callback_; |
| 2901 | |
| 2902 | DISALLOW_COPY_AND_ASSIGN(ConnectWithinCallback); |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2903 | }; |
| 2904 | |
| 2905 | TEST_F(ClientSocketPoolBaseTest, AbortAllRequestsOnFlush) { |
| 2906 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2907 | |
| 2908 | // First job will be waiting until it gets aborted. |
| 2909 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2910 | |
| 2911 | ClientSocketHandle handle; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2912 | ConnectWithinCallback callback(TestGroupId("a"), params_, pool_.get()); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2913 | EXPECT_EQ( |
| 2914 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2915 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2916 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2917 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2918 | NetLogWithSource())); |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2919 | |
| 2920 | // Second job will be started during the first callback, and will |
| 2921 | // asynchronously complete with OK. |
| 2922 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 7af985a | 2012-12-14 22:40:42 | [diff] [blame] | 2923 | pool_->FlushWithError(ERR_NETWORK_CHANGED); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2924 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_NETWORK_CHANGED)); |
| 2925 | EXPECT_THAT(callback.WaitForNestedResult(), IsOk()); |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2926 | } |
| 2927 | |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 2928 | TEST_F(ClientSocketPoolBaseTest, BackupSocketWaitsForHostResolution) { |
| 2929 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 2930 | pool_->EnableConnectBackupJobs(); |
| 2931 | |
| 2932 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2933 | ClientSocketHandle handle; |
| 2934 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2935 | EXPECT_EQ( |
| 2936 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2937 | handle.Init(TestGroupId("bar"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2938 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2939 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2940 | NetLogWithSource())); |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 2941 | // The backup timer fires but doesn't start a new ConnectJob while resolving |
| 2942 | // the hostname. |
| 2943 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_RESOLVING_HOST); |
| 2944 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
| 2945 | ClientSocketPool::kMaxConnectRetryIntervalMs * 100)); |
| 2946 | EXPECT_EQ(1, client_socket_factory_.allocation_count()); |
| 2947 | |
| 2948 | // Once the ConnectJob has finished resolving the hostname, the backup timer |
| 2949 | // will create a ConnectJob when it fires. |
| 2950 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_CONNECTING); |
| 2951 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
| 2952 | ClientSocketPool::kMaxConnectRetryIntervalMs)); |
| 2953 | EXPECT_EQ(2, client_socket_factory_.allocation_count()); |
| 2954 | } |
| 2955 | |
| 2956 | // Test that no backup socket is created when a ConnectJob connects before it |
| 2957 | // completes. |
| 2958 | TEST_F(ClientSocketPoolBaseTest, NoBackupSocketWhenConnected) { |
| 2959 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 2960 | pool_->EnableConnectBackupJobs(); |
| 2961 | |
| 2962 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2963 | ClientSocketHandle handle; |
| 2964 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2965 | EXPECT_EQ( |
| 2966 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2967 | handle.Init(TestGroupId("bar"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2968 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2969 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2970 | NetLogWithSource())); |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 2971 | // The backup timer fires but doesn't start a new ConnectJob while resolving |
| 2972 | // the hostname. |
| 2973 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_RESOLVING_HOST); |
| 2974 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
| 2975 | ClientSocketPool::kMaxConnectRetryIntervalMs * 100)); |
| 2976 | EXPECT_EQ(1, client_socket_factory_.allocation_count()); |
| 2977 | |
| 2978 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE); |
| 2979 | client_socket_factory_.SetJobHasEstablishedConnection(0); |
| 2980 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
| 2981 | ClientSocketPool::kMaxConnectRetryIntervalMs * 100)); |
| 2982 | EXPECT_EQ(1, client_socket_factory_.allocation_count()); |
| 2983 | } |
| 2984 | |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 2985 | // Cancel a pending socket request while we're at max sockets, |
| 2986 | // and verify that the backup socket firing doesn't cause a crash. |
| 2987 | TEST_F(ClientSocketPoolBaseTest, BackupSocketCancelAtMaxSockets) { |
| 2988 | // Max 4 sockets globally, max 4 sockets per group. |
| 2989 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
[email protected] | 06d9404 | 2010-08-25 01:45:22 | [diff] [blame] | 2990 | pool_->EnableConnectBackupJobs(); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 2991 | |
[email protected] | 4baaf9d | 2010-08-31 15:15:44 | [diff] [blame] | 2992 | // Create the first socket and set to ERR_IO_PENDING. This starts the backup |
| 2993 | // timer. |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 2994 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2995 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2996 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2997 | EXPECT_EQ( |
| 2998 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2999 | handle.Init(TestGroupId("bar"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3000 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 3001 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 3002 | NetLogWithSource())); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3003 | |
| 3004 | // Start (MaxSockets - 1) connected sockets to reach max sockets. |
| 3005 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 3006 | ClientSocketHandle handles[kDefaultMaxSockets]; |
| 3007 | for (int i = 1; i < kDefaultMaxSockets; ++i) { |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3008 | TestCompletionCallback callback; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3009 | EXPECT_EQ(OK, handles[i].Init(TestGroupId("bar"), params_, DEFAULT_PRIORITY, |
| 3010 | SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3011 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3012 | callback.callback(), |
| 3013 | ClientSocketPool::ProxyAuthCallback(), |
| 3014 | pool_.get(), NetLogWithSource())); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3015 | } |
| 3016 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3017 | base::RunLoop().RunUntilIdle(); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3018 | |
| 3019 | // Cancel the pending request. |
| 3020 | handle.Reset(); |
| 3021 | |
| 3022 | // Wait for the backup timer to fire (add some slop to ensure it fires) |
Alex Clarke | 0def209 | 2018-12-10 12:01:45 | [diff] [blame] | 3023 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
[email protected] | 26b997396 | 2012-01-28 00:57:00 | [diff] [blame] | 3024 | ClientSocketPool::kMaxConnectRetryIntervalMs / 2 * 3)); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3025 | |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3026 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 3027 | } |
| 3028 | |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3029 | TEST_F(ClientSocketPoolBaseTest, CancelBackupSocketAfterCancelingAllRequests) { |
[email protected] | 4baaf9d | 2010-08-31 15:15:44 | [diff] [blame] | 3030 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 3031 | pool_->EnableConnectBackupJobs(); |
| 3032 | |
| 3033 | // Create the first socket and set to ERR_IO_PENDING. This starts the backup |
| 3034 | // timer. |
| 3035 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3036 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3037 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3038 | EXPECT_EQ( |
| 3039 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3040 | handle.Init(TestGroupId("bar"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3041 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 3042 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 3043 | NetLogWithSource())); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3044 | ASSERT_TRUE(pool_->HasGroup(TestGroupId("bar"))); |
| 3045 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("bar"))); |
| 3046 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("bar"))); |
| 3047 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("bar"))); |
[email protected] | 4baaf9d | 2010-08-31 15:15:44 | [diff] [blame] | 3048 | |
| 3049 | // Cancel the socket request. This should cancel the backup timer. Wait for |
| 3050 | // the backup time to see if it indeed got canceled. |
| 3051 | handle.Reset(); |
| 3052 | // Wait for the backup timer to fire (add some slop to ensure it fires) |
Alex Clarke | 0def209 | 2018-12-10 12:01:45 | [diff] [blame] | 3053 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
[email protected] | 26b997396 | 2012-01-28 00:57:00 | [diff] [blame] | 3054 | ClientSocketPool::kMaxConnectRetryIntervalMs / 2 * 3)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3055 | ASSERT_TRUE(pool_->HasGroup(TestGroupId("bar"))); |
| 3056 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("bar"))); |
[email protected] | 4baaf9d | 2010-08-31 15:15:44 | [diff] [blame] | 3057 | } |
| 3058 | |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3059 | TEST_F(ClientSocketPoolBaseTest, CancelBackupSocketAfterFinishingAllRequests) { |
| 3060 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 3061 | pool_->EnableConnectBackupJobs(); |
| 3062 | |
| 3063 | // Create the first socket and set to ERR_IO_PENDING. This starts the backup |
| 3064 | // timer. |
| 3065 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3066 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3067 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3068 | EXPECT_EQ( |
| 3069 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3070 | handle.Init(TestGroupId("bar"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3071 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 3072 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 3073 | NetLogWithSource())); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3074 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3075 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3076 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3077 | EXPECT_EQ( |
| 3078 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3079 | handle2.Init(TestGroupId("bar"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3080 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3081 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3082 | pool_.get(), NetLogWithSource())); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3083 | ASSERT_TRUE(pool_->HasGroup(TestGroupId("bar"))); |
| 3084 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroup(TestGroupId("bar"))); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3085 | |
| 3086 | // Cancel request 1 and then complete request 2. With the requests finished, |
| 3087 | // the backup timer should be cancelled. |
| 3088 | handle.Reset(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3089 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3090 | // Wait for the backup timer to fire (add some slop to ensure it fires) |
Alex Clarke | 0def209 | 2018-12-10 12:01:45 | [diff] [blame] | 3091 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
[email protected] | 26b997396 | 2012-01-28 00:57:00 | [diff] [blame] | 3092 | ClientSocketPool::kMaxConnectRetryIntervalMs / 2 * 3)); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3093 | } |
| 3094 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3095 | // Test delayed socket binding for the case where we have two connects, |
| 3096 | // and while one is waiting on a connect, the other frees up. |
| 3097 | // The socket waiting on a connect should switch immediately to the freed |
| 3098 | // up socket. |
| 3099 | TEST_F(ClientSocketPoolBaseTest, DelayedSocketBindingWaitingForConnect) { |
| 3100 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3101 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3102 | |
| 3103 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3104 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3105 | EXPECT_EQ( |
| 3106 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3107 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3108 | ClientSocketPool::RespectLimits::ENABLED, |
| 3109 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3110 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3111 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3112 | |
| 3113 | // No idle sockets, no pending jobs. |
| 3114 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3115 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3116 | |
| 3117 | // Create a second socket to the same host, but this one will wait. |
| 3118 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3119 | ClientSocketHandle handle2; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3120 | EXPECT_EQ( |
| 3121 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3122 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3123 | ClientSocketPool::RespectLimits::ENABLED, |
| 3124 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3125 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3126 | // No idle sockets, and one connecting job. |
| 3127 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3128 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3129 | |
| 3130 | // Return the first handle to the pool. This will initiate the delayed |
| 3131 | // binding. |
| 3132 | handle1.Reset(); |
| 3133 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3134 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3135 | |
| 3136 | // Still no idle sockets, still one pending connect job. |
| 3137 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3138 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3139 | |
| 3140 | // The second socket connected, even though it was a Waiting Job. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3141 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3142 | |
| 3143 | // And we can see there is still one job waiting. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3144 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3145 | |
| 3146 | // Finally, signal the waiting Connect. |
| 3147 | client_socket_factory_.SignalJobs(); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3148 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3149 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3150 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3151 | } |
| 3152 | |
| 3153 | // Test delayed socket binding when a group is at capacity and one |
| 3154 | // of the group's sockets frees up. |
| 3155 | TEST_F(ClientSocketPoolBaseTest, DelayedSocketBindingAtGroupCapacity) { |
| 3156 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3157 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3158 | |
| 3159 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3160 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3161 | EXPECT_EQ( |
| 3162 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3163 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3164 | ClientSocketPool::RespectLimits::ENABLED, |
| 3165 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3166 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3167 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3168 | |
| 3169 | // No idle sockets, no pending jobs. |
| 3170 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3171 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3172 | |
| 3173 | // Create a second socket to the same host, but this one will wait. |
| 3174 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3175 | ClientSocketHandle handle2; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3176 | EXPECT_EQ( |
| 3177 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3178 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3179 | ClientSocketPool::RespectLimits::ENABLED, |
| 3180 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3181 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3182 | // No idle sockets, and one connecting job. |
| 3183 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3184 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3185 | |
| 3186 | // Return the first handle to the pool. This will initiate the delayed |
| 3187 | // binding. |
| 3188 | handle1.Reset(); |
| 3189 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3190 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3191 | |
| 3192 | // Still no idle sockets, still one pending connect job. |
| 3193 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3194 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3195 | |
| 3196 | // The second socket connected, even though it was a Waiting Job. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3197 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3198 | |
| 3199 | // And we can see there is still one job waiting. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3200 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3201 | |
| 3202 | // Finally, signal the waiting Connect. |
| 3203 | client_socket_factory_.SignalJobs(); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3204 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3205 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3206 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3207 | } |
| 3208 | |
| 3209 | // Test out the case where we have one socket connected, one |
| 3210 | // connecting, when the first socket finishes and goes idle. |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3211 | // Although the second connection is pending, the second request |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3212 | // should complete, by taking the first socket's idle socket. |
| 3213 | TEST_F(ClientSocketPoolBaseTest, DelayedSocketBindingAtStall) { |
| 3214 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3215 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3216 | |
| 3217 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3218 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3219 | EXPECT_EQ( |
| 3220 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3221 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3222 | ClientSocketPool::RespectLimits::ENABLED, |
| 3223 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3224 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3225 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3226 | |
| 3227 | // No idle sockets, no pending jobs. |
| 3228 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3229 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3230 | |
| 3231 | // Create a second socket to the same host, but this one will wait. |
| 3232 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3233 | ClientSocketHandle handle2; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3234 | EXPECT_EQ( |
| 3235 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3236 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3237 | ClientSocketPool::RespectLimits::ENABLED, |
| 3238 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3239 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3240 | // No idle sockets, and one connecting job. |
| 3241 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3242 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3243 | |
| 3244 | // Return the first handle to the pool. This will initiate the delayed |
| 3245 | // binding. |
| 3246 | handle1.Reset(); |
| 3247 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3248 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3249 | |
| 3250 | // Still no idle sockets, still one pending connect job. |
| 3251 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3252 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3253 | |
| 3254 | // The second socket connected, even though it was a Waiting Job. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3255 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3256 | |
| 3257 | // And we can see there is still one job waiting. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3258 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3259 | |
| 3260 | // Finally, signal the waiting Connect. |
| 3261 | client_socket_factory_.SignalJobs(); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3262 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3263 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3264 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3265 | } |
| 3266 | |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3267 | // Cover the case where on an available socket slot, we have one pending |
| 3268 | // request that completes synchronously, thereby making the Group empty. |
| 3269 | TEST_F(ClientSocketPoolBaseTest, SynchronouslyProcessOnePendingRequest) { |
| 3270 | const int kUnlimitedSockets = 100; |
| 3271 | const int kOneSocketPerGroup = 1; |
| 3272 | CreatePool(kUnlimitedSockets, kOneSocketPerGroup); |
| 3273 | |
| 3274 | // Make the first request asynchronous fail. |
| 3275 | // This will free up a socket slot later. |
| 3276 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
| 3277 | |
| 3278 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3279 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3280 | EXPECT_EQ( |
| 3281 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3282 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3283 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3284 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3285 | pool_.get(), NetLogWithSource())); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3286 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3287 | |
| 3288 | // Make the second request synchronously fail. This should make the Group |
| 3289 | // empty. |
| 3290 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
| 3291 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3292 | TestCompletionCallback callback2; |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3293 | // It'll be ERR_IO_PENDING now, but the TestConnectJob will synchronously fail |
| 3294 | // when created. |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3295 | EXPECT_EQ( |
| 3296 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3297 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3298 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3299 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3300 | pool_.get(), NetLogWithSource())); |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3301 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3302 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3303 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3304 | EXPECT_THAT(callback1.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 3305 | EXPECT_THAT(callback2.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3306 | EXPECT_FALSE(pool_->HasGroup(TestGroupId("a"))); |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3307 | } |
| 3308 | |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3309 | TEST_F(ClientSocketPoolBaseTest, PreferUsedSocketToUnusedSocket) { |
| 3310 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 3311 | |
| 3312 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3313 | |
| 3314 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3315 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3316 | EXPECT_EQ( |
| 3317 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3318 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3319 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3320 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3321 | pool_.get(), NetLogWithSource())); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3322 | |
| 3323 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3324 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3325 | EXPECT_EQ( |
| 3326 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3327 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3328 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3329 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3330 | pool_.get(), NetLogWithSource())); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3331 | ClientSocketHandle handle3; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3332 | TestCompletionCallback callback3; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3333 | EXPECT_EQ( |
| 3334 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3335 | handle3.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3336 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3337 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3338 | pool_.get(), NetLogWithSource())); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3339 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3340 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3341 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
| 3342 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3343 | |
| 3344 | // Use the socket. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 3345 | EXPECT_EQ(1, handle1.socket()->Write(nullptr, 1, CompletionOnceCallback(), |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 3346 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 3347 | EXPECT_EQ(1, handle3.socket()->Write(nullptr, 1, CompletionOnceCallback(), |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 3348 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3349 | |
| 3350 | handle1.Reset(); |
| 3351 | handle2.Reset(); |
| 3352 | handle3.Reset(); |
| 3353 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3354 | EXPECT_EQ(OK, handle1.Init( |
| 3355 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
| 3356 | ClientSocketPool::RespectLimits::ENABLED, |
| 3357 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3358 | pool_.get(), NetLogWithSource())); |
| 3359 | EXPECT_EQ(OK, handle2.Init( |
| 3360 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
| 3361 | ClientSocketPool::RespectLimits::ENABLED, |
| 3362 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3363 | pool_.get(), NetLogWithSource())); |
| 3364 | EXPECT_EQ(OK, handle3.Init( |
| 3365 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
| 3366 | ClientSocketPool::RespectLimits::ENABLED, |
| 3367 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3368 | pool_.get(), NetLogWithSource())); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3369 | |
| 3370 | EXPECT_TRUE(handle1.socket()->WasEverUsed()); |
| 3371 | EXPECT_TRUE(handle2.socket()->WasEverUsed()); |
| 3372 | EXPECT_FALSE(handle3.socket()->WasEverUsed()); |
| 3373 | } |
| 3374 | |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3375 | TEST_F(ClientSocketPoolBaseTest, RequestSockets) { |
| 3376 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3377 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3378 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3379 | pool_->RequestSockets(TestGroupId("a"), ¶ms_, 2, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3380 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3381 | ASSERT_TRUE(pool_->HasGroup(TestGroupId("a"))); |
| 3382 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 3383 | EXPECT_EQ(2u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 3384 | EXPECT_EQ(2u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 3385 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3386 | |
| 3387 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3388 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3389 | EXPECT_EQ( |
| 3390 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3391 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3392 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3393 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3394 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3395 | |
| 3396 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3397 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3398 | EXPECT_EQ( |
| 3399 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3400 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3401 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3402 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3403 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3404 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3405 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 3406 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 3407 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 3408 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3409 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3410 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3411 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3412 | handle1.Reset(); |
| 3413 | handle2.Reset(); |
| 3414 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3415 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 3416 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 3417 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 3418 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3419 | } |
| 3420 | |
| 3421 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsWhenAlreadyHaveAConnectJob) { |
| 3422 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3423 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3424 | |
| 3425 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3426 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3427 | EXPECT_EQ( |
| 3428 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3429 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3430 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3431 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3432 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3433 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3434 | ASSERT_TRUE(pool_->HasGroup(TestGroupId("a"))); |
| 3435 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 3436 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 3437 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 3438 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3439 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3440 | pool_->RequestSockets(TestGroupId("a"), ¶ms_, 2, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3441 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3442 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 3443 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 3444 | EXPECT_EQ(1u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 3445 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3446 | |
| 3447 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3448 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3449 | EXPECT_EQ( |
| 3450 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3451 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3452 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3453 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3454 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3455 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3456 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 3457 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 3458 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 3459 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3460 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3461 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3462 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3463 | handle1.Reset(); |
| 3464 | handle2.Reset(); |
| 3465 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3466 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 3467 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 3468 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 3469 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3470 | } |
| 3471 | |
| 3472 | TEST_F(ClientSocketPoolBaseTest, |
| 3473 | RequestSocketsWhenAlreadyHaveMultipleConnectJob) { |
| 3474 | CreatePool(4, 4); |
| 3475 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3476 | |
| 3477 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3478 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3479 | EXPECT_EQ( |
| 3480 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3481 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3482 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3483 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3484 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3485 | |
| 3486 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3487 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3488 | EXPECT_EQ( |
| 3489 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3490 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3491 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3492 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3493 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3494 | |
| 3495 | ClientSocketHandle handle3; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3496 | TestCompletionCallback callback3; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3497 | EXPECT_EQ( |
| 3498 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3499 | handle3.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3500 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3501 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3502 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3503 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3504 | ASSERT_TRUE(pool_->HasGroup(TestGroupId("a"))); |
| 3505 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 3506 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 3507 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 3508 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3509 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3510 | pool_->RequestSockets(TestGroupId("a"), ¶ms_, 2, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3511 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3512 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 3513 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 3514 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 3515 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3516 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3517 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3518 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
| 3519 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3520 | handle1.Reset(); |
| 3521 | handle2.Reset(); |
| 3522 | handle3.Reset(); |
| 3523 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3524 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 3525 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 3526 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 3527 | EXPECT_EQ(3u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3528 | } |
| 3529 | |
| 3530 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsAtMaxSocketLimit) { |
| 3531 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 3532 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3533 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3534 | ASSERT_FALSE(pool_->HasGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3535 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3536 | pool_->RequestSockets(TestGroupId("a"), ¶ms_, kDefaultMaxSockets, |
| 3537 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3538 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3539 | ASSERT_TRUE(pool_->HasGroup(TestGroupId("a"))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3540 | EXPECT_EQ(kDefaultMaxSockets, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3541 | static_cast<int>(pool_->NumConnectJobsInGroup(TestGroupId("a")))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3542 | EXPECT_EQ(kDefaultMaxSockets, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3543 | static_cast<int>( |
| 3544 | pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a")))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3545 | EXPECT_EQ(kDefaultMaxSockets, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3546 | static_cast<int>( |
| 3547 | pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a")))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3548 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3549 | ASSERT_FALSE(pool_->HasGroup(TestGroupId("b"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3550 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3551 | pool_->RequestSockets(TestGroupId("b"), ¶ms_, kDefaultMaxSockets, |
| 3552 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3553 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3554 | ASSERT_FALSE(pool_->HasGroup(TestGroupId("b"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3555 | } |
| 3556 | |
| 3557 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsHitMaxSocketLimit) { |
| 3558 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 3559 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3560 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3561 | ASSERT_FALSE(pool_->HasGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3562 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3563 | pool_->RequestSockets(TestGroupId("a"), ¶ms_, kDefaultMaxSockets - 1, |
Charlie Harrison | 55ce608 | 2018-05-14 02:25:57 | [diff] [blame] | 3564 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3565 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3566 | ASSERT_TRUE(pool_->HasGroup(TestGroupId("a"))); |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3567 | EXPECT_EQ(kDefaultMaxSockets - 1, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3568 | static_cast<int>(pool_->NumConnectJobsInGroup(TestGroupId("a")))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3569 | EXPECT_EQ(kDefaultMaxSockets - 1, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3570 | static_cast<int>( |
| 3571 | pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a")))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3572 | EXPECT_EQ(kDefaultMaxSockets - 1, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3573 | static_cast<int>( |
| 3574 | pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a")))); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 3575 | EXPECT_FALSE(pool_->IsStalled()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3576 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3577 | ASSERT_FALSE(pool_->HasGroup(TestGroupId("b"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3578 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3579 | pool_->RequestSockets(TestGroupId("b"), ¶ms_, kDefaultMaxSockets, |
| 3580 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3581 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3582 | ASSERT_TRUE(pool_->HasGroup(TestGroupId("b"))); |
| 3583 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("b"))); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 3584 | EXPECT_FALSE(pool_->IsStalled()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3585 | } |
| 3586 | |
| 3587 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsCountIdleSockets) { |
| 3588 | CreatePool(4, 4); |
| 3589 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3590 | |
| 3591 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3592 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3593 | EXPECT_EQ( |
| 3594 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3595 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3596 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3597 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3598 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3599 | ASSERT_THAT(callback1.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3600 | handle1.Reset(); |
| 3601 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3602 | ASSERT_TRUE(pool_->HasGroup(TestGroupId("a"))); |
| 3603 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 3604 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 3605 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 3606 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3607 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3608 | pool_->RequestSockets(TestGroupId("a"), ¶ms_, 2, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3609 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3610 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 3611 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 3612 | EXPECT_EQ(1u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 3613 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3614 | } |
| 3615 | |
| 3616 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsCountActiveSockets) { |
| 3617 | CreatePool(4, 4); |
| 3618 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3619 | |
| 3620 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3621 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3622 | EXPECT_EQ( |
| 3623 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3624 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3625 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3626 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3627 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3628 | ASSERT_THAT(callback1.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3629 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3630 | ASSERT_TRUE(pool_->HasGroup(TestGroupId("a"))); |
| 3631 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 3632 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 3633 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 3634 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
| 3635 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3636 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3637 | pool_->RequestSockets(TestGroupId("a"), ¶ms_, 2, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3638 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3639 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 3640 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 3641 | EXPECT_EQ(1u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 3642 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
| 3643 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3644 | } |
| 3645 | |
| 3646 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsSynchronous) { |
| 3647 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3648 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 3649 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3650 | pool_->RequestSockets(TestGroupId("a"), ¶ms_, kDefaultMaxSocketsPerGroup, |
Charlie Harrison | 55ce608 | 2018-05-14 02:25:57 | [diff] [blame] | 3651 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3652 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3653 | ASSERT_TRUE(pool_->HasGroup(TestGroupId("a"))); |
| 3654 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 3655 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 3656 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3657 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3658 | static_cast<int>(pool_->IdleSocketCountInGroup(TestGroupId("a")))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3659 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3660 | pool_->RequestSockets(TestGroupId("b"), ¶ms_, kDefaultMaxSocketsPerGroup, |
Charlie Harrison | 55ce608 | 2018-05-14 02:25:57 | [diff] [blame] | 3661 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3662 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3663 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroup(TestGroupId("b"))); |
| 3664 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("b"))); |
| 3665 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("b"))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3666 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3667 | static_cast<int>(pool_->IdleSocketCountInGroup(TestGroupId("b")))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3668 | } |
| 3669 | |
[email protected] | 3c819f52 | 2010-12-02 02:03:12 | [diff] [blame] | 3670 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsSynchronousError) { |
| 3671 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3672 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
| 3673 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3674 | pool_->RequestSockets(TestGroupId("a"), ¶ms_, kDefaultMaxSocketsPerGroup, |
Charlie Harrison | 55ce608 | 2018-05-14 02:25:57 | [diff] [blame] | 3675 | NetLogWithSource()); |
[email protected] | 3c819f52 | 2010-12-02 02:03:12 | [diff] [blame] | 3676 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3677 | ASSERT_FALSE(pool_->HasGroup(TestGroupId("a"))); |
[email protected] | fd2e53e | 2011-01-14 20:40:52 | [diff] [blame] | 3678 | |
| 3679 | connect_job_factory_->set_job_type( |
| 3680 | TestConnectJob::kMockAdditionalErrorStateJob); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3681 | pool_->RequestSockets(TestGroupId("a"), ¶ms_, kDefaultMaxSocketsPerGroup, |
Charlie Harrison | 55ce608 | 2018-05-14 02:25:57 | [diff] [blame] | 3682 | NetLogWithSource()); |
[email protected] | fd2e53e | 2011-01-14 20:40:52 | [diff] [blame] | 3683 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3684 | ASSERT_FALSE(pool_->HasGroup(TestGroupId("a"))); |
[email protected] | 3c819f52 | 2010-12-02 02:03:12 | [diff] [blame] | 3685 | } |
| 3686 | |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3687 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsMultipleTimesDoesNothing) { |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3688 | CreatePool(4, 4); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3689 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3690 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3691 | pool_->RequestSockets(TestGroupId("a"), ¶ms_, 2, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3692 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3693 | ASSERT_TRUE(pool_->HasGroup(TestGroupId("a"))); |
| 3694 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 3695 | EXPECT_EQ(2u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 3696 | EXPECT_EQ(2u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 3697 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroup(TestGroupId("a"))); |
| 3698 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3699 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3700 | pool_->RequestSockets(TestGroupId("a"), ¶ms_, 2, NetLogWithSource()); |
| 3701 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 3702 | EXPECT_EQ(2u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 3703 | EXPECT_EQ(2u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 3704 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroup(TestGroupId("a"))); |
| 3705 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3706 | |
| 3707 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3708 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3709 | EXPECT_EQ( |
| 3710 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3711 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3712 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3713 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3714 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3715 | |
| 3716 | client_socket_factory_.SignalJob(0); |
| 3717 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3718 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3719 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 3720 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 3721 | EXPECT_EQ(1u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 3722 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroup(TestGroupId("a"))); |
| 3723 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3724 | |
| 3725 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3726 | TestCompletionCallback callback2; |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3727 | EXPECT_EQ( |
| 3728 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3729 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3730 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3731 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3732 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3733 | client_socket_factory_.SignalJob(0); |
| 3734 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3735 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3736 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 3737 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 3738 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 3739 | EXPECT_EQ(2, pool_->NumActiveSocketsInGroup(TestGroupId("a"))); |
| 3740 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3741 | |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3742 | handle1.Reset(); |
| 3743 | handle2.Reset(); |
| 3744 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3745 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 3746 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 3747 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 3748 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroup(TestGroupId("a"))); |
| 3749 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3750 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3751 | pool_->RequestSockets(TestGroupId("a"), ¶ms_, 2, NetLogWithSource()); |
| 3752 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 3753 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 3754 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 3755 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroup(TestGroupId("a"))); |
| 3756 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3757 | } |
| 3758 | |
| 3759 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsDifferentNumSockets) { |
| 3760 | CreatePool(4, 4); |
| 3761 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3762 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3763 | pool_->RequestSockets(TestGroupId("a"), ¶ms_, 1, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3764 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3765 | ASSERT_TRUE(pool_->HasGroup(TestGroupId("a"))); |
| 3766 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 3767 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 3768 | EXPECT_EQ(1u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 3769 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3770 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3771 | pool_->RequestSockets(TestGroupId("a"), ¶ms_, 2, NetLogWithSource()); |
| 3772 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 3773 | EXPECT_EQ(2u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 3774 | EXPECT_EQ(2u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 3775 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3776 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3777 | pool_->RequestSockets(TestGroupId("a"), ¶ms_, 3, NetLogWithSource()); |
| 3778 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 3779 | EXPECT_EQ(3u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 3780 | EXPECT_EQ(3u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 3781 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3782 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3783 | pool_->RequestSockets(TestGroupId("a"), ¶ms_, 1, NetLogWithSource()); |
| 3784 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 3785 | EXPECT_EQ(3u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 3786 | EXPECT_EQ(3u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 3787 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3788 | } |
| 3789 | |
| 3790 | TEST_F(ClientSocketPoolBaseTest, PreconnectJobsTakenByNormalRequests) { |
| 3791 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3792 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3793 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3794 | pool_->RequestSockets(TestGroupId("a"), ¶ms_, 1, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3795 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3796 | ASSERT_TRUE(pool_->HasGroup(TestGroupId("a"))); |
| 3797 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 3798 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 3799 | EXPECT_EQ(1u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 3800 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3801 | |
| 3802 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3803 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3804 | EXPECT_EQ( |
| 3805 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3806 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3807 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3808 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3809 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3810 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3811 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 3812 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 3813 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 3814 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3815 | |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3816 | client_socket_factory_.SignalJobs(); |
| 3817 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3818 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3819 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 3820 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 3821 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 3822 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
| 3823 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3824 | |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 3825 | // Make sure if a preconnected socket is not fully connected when a request |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 3826 | // starts, it has a connect start time. |
| 3827 | TestLoadTimingInfoConnectedNotReused(handle1); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3828 | handle1.Reset(); |
| 3829 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3830 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3831 | } |
| 3832 | |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 3833 | // Checks that fully connected preconnect jobs have no connect times, and are |
| 3834 | // marked as reused. |
| 3835 | TEST_F(ClientSocketPoolBaseTest, ConnectedPreconnectJobsHaveNoConnectTimes) { |
| 3836 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3837 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3838 | pool_->RequestSockets(TestGroupId("a"), ¶ms_, 1, NetLogWithSource()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 3839 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3840 | ASSERT_TRUE(pool_->HasGroup(TestGroupId("a"))); |
| 3841 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 3842 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 3843 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 3844 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 3845 | |
| 3846 | ClientSocketHandle handle; |
| 3847 | TestCompletionCallback callback; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3848 | EXPECT_EQ(OK, handle.Init( |
| 3849 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
| 3850 | ClientSocketPool::RespectLimits::ENABLED, |
| 3851 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3852 | pool_.get(), NetLogWithSource())); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 3853 | |
| 3854 | // Make sure the idle socket was used. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3855 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 3856 | |
| 3857 | TestLoadTimingInfoConnectedReused(handle); |
| 3858 | handle.Reset(); |
| 3859 | TestLoadTimingInfoNotConnected(handle); |
| 3860 | } |
| 3861 | |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3862 | // http://crbug.com/64940 regression test. |
| 3863 | TEST_F(ClientSocketPoolBaseTest, PreconnectClosesIdleSocketRemovesGroup) { |
| 3864 | const int kMaxTotalSockets = 3; |
| 3865 | const int kMaxSocketsPerGroup = 2; |
| 3866 | CreatePool(kMaxTotalSockets, kMaxSocketsPerGroup); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3867 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3868 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 3869 | // Note that group id ordering matters here. "a" comes before "b", so |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3870 | // CloseOneIdleSocket() will try to close "a"'s idle socket. |
| 3871 | |
| 3872 | // Set up one idle socket in "a". |
| 3873 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3874 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3875 | EXPECT_EQ( |
| 3876 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3877 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3878 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3879 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3880 | pool_.get(), NetLogWithSource())); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3881 | ASSERT_TRUE(pool_->HasGroup(TestGroupId("a"))); |
| 3882 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 3883 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 3884 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 3885 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3886 | |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3887 | client_socket_factory_.SignalJobs(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3888 | ASSERT_THAT(callback1.WaitForResult(), IsOk()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3889 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 3890 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 3891 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 3892 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3893 | |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3894 | handle1.Reset(); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3895 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3896 | |
| 3897 | // Set up two active sockets in "b". |
| 3898 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3899 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3900 | EXPECT_EQ( |
| 3901 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3902 | handle1.Init(TestGroupId("b"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3903 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3904 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3905 | pool_.get(), NetLogWithSource())); |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3906 | EXPECT_EQ( |
| 3907 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3908 | handle2.Init(TestGroupId("b"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3909 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3910 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3911 | pool_.get(), NetLogWithSource())); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3912 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3913 | ASSERT_TRUE(pool_->HasGroup(TestGroupId("b"))); |
| 3914 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroup(TestGroupId("b"))); |
| 3915 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("b"))); |
| 3916 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("b"))); |
| 3917 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3918 | |
| 3919 | client_socket_factory_.SignalJobs(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3920 | ASSERT_THAT(callback1.WaitForResult(), IsOk()); |
| 3921 | ASSERT_THAT(callback2.WaitForResult(), IsOk()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3922 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
| 3923 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("b"))); |
| 3924 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("b"))); |
| 3925 | EXPECT_EQ(2, pool_->NumActiveSocketsInGroup(TestGroupId("b"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3926 | |
| 3927 | // Now we have 1 idle socket in "a" and 2 active sockets in "b". This means |
| 3928 | // we've maxed out on sockets, since we set |kMaxTotalSockets| to 3. |
| 3929 | // Requesting 2 preconnected sockets for "a" should fail to allocate any more |
| 3930 | // sockets for "a", and "b" should still have 2 active sockets. |
| 3931 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3932 | pool_->RequestSockets(TestGroupId("a"), ¶ms_, 2, NetLogWithSource()); |
| 3933 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 3934 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 3935 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 3936 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
| 3937 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroup(TestGroupId("a"))); |
| 3938 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroup(TestGroupId("b"))); |
| 3939 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("b"))); |
| 3940 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("b"))); |
| 3941 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
| 3942 | EXPECT_EQ(2, pool_->NumActiveSocketsInGroup(TestGroupId("b"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3943 | |
| 3944 | // Now release the 2 active sockets for "b". This will give us 1 idle socket |
| 3945 | // in "a" and 2 idle sockets in "b". Requesting 2 preconnected sockets for |
| 3946 | // "a" should result in closing 1 for "b". |
| 3947 | handle1.Reset(); |
| 3948 | handle2.Reset(); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3949 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
| 3950 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroup(TestGroupId("b"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3951 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3952 | pool_->RequestSockets(TestGroupId("a"), ¶ms_, 2, NetLogWithSource()); |
| 3953 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 3954 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 3955 | EXPECT_EQ(1u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 3956 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
| 3957 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroup(TestGroupId("a"))); |
| 3958 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroup(TestGroupId("b"))); |
| 3959 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("b"))); |
| 3960 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("b"))); |
| 3961 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
| 3962 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroup(TestGroupId("b"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3963 | } |
| 3964 | |
[email protected] | b7b8be4 | 2011-07-12 12:46:41 | [diff] [blame] | 3965 | TEST_F(ClientSocketPoolBaseTest, PreconnectWithoutBackupJob) { |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 3966 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3967 | pool_->EnableConnectBackupJobs(); |
| 3968 | |
| 3969 | // Make the ConnectJob hang until it times out, shorten the timeout. |
| 3970 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3971 | connect_job_factory_->set_timeout_duration( |
| 3972 | base::TimeDelta::FromMilliseconds(500)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3973 | pool_->RequestSockets(TestGroupId("a"), ¶ms_, 1, NetLogWithSource()); |
| 3974 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 3975 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 3976 | EXPECT_EQ(1u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 3977 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 3978 | |
[email protected] | b7b8be4 | 2011-07-12 12:46:41 | [diff] [blame] | 3979 | // Verify the backup timer doesn't create a backup job, by making |
| 3980 | // the backup job a pending job instead of a waiting job, so it |
| 3981 | // *would* complete if it were created. |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 3982 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 3983 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
Gabriel Charette | ea91801 | 2018-05-16 11:53:44 | [diff] [blame] | 3984 | FROM_HERE, base::RunLoop::QuitCurrentWhenIdleClosureDeprecated(), |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 3985 | base::TimeDelta::FromSeconds(1)); |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3986 | base::RunLoop().Run(); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3987 | EXPECT_FALSE(pool_->HasGroup(TestGroupId("a"))); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 3988 | } |
| 3989 | |
[email protected] | b7b8be4 | 2011-07-12 12:46:41 | [diff] [blame] | 3990 | TEST_F(ClientSocketPoolBaseTest, PreconnectWithBackupJob) { |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 3991 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3992 | pool_->EnableConnectBackupJobs(); |
| 3993 | |
| 3994 | // Make the ConnectJob hang forever. |
| 3995 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3996 | pool_->RequestSockets(TestGroupId("a"), ¶ms_, 1, NetLogWithSource()); |
| 3997 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 3998 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 3999 | EXPECT_EQ(1u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 4000 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 4001 | base::RunLoop().RunUntilIdle(); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4002 | |
| 4003 | // Make the backup job be a pending job, so it completes normally. |
| 4004 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 4005 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 4006 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4007 | EXPECT_EQ( |
| 4008 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4009 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4010 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 4011 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4012 | NetLogWithSource())); |
[email protected] | b7b8be4 | 2011-07-12 12:46:41 | [diff] [blame] | 4013 | // Timer has started, but the backup connect job shouldn't be created yet. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4014 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 4015 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 4016 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 4017 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
| 4018 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroup(TestGroupId("a"))); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4019 | ASSERT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4020 | |
| 4021 | // The hung connect job should still be there, but everything else should be |
| 4022 | // complete. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4023 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 4024 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 4025 | EXPECT_EQ(1u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 4026 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
| 4027 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroup(TestGroupId("a"))); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4028 | } |
| 4029 | |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4030 | // Tests that a preconnect that starts out with unread data can still be used. |
| 4031 | // http://crbug.com/334467 |
| 4032 | TEST_F(ClientSocketPoolBaseTest, PreconnectWithUnreadData) { |
| 4033 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4034 | connect_job_factory_->set_job_type(TestConnectJob::kMockUnreadDataJob); |
| 4035 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4036 | pool_->RequestSockets(TestGroupId("a"), ¶ms_, 1, NetLogWithSource()); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4037 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4038 | ASSERT_TRUE(pool_->HasGroup(TestGroupId("a"))); |
| 4039 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 4040 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 4041 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 4042 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4043 | |
| 4044 | // Fail future jobs to be sure that handle receives the preconnected socket |
| 4045 | // rather than closing it and making a new one. |
| 4046 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
| 4047 | ClientSocketHandle handle; |
| 4048 | TestCompletionCallback callback; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4049 | EXPECT_EQ(OK, handle.Init( |
| 4050 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
| 4051 | ClientSocketPool::RespectLimits::ENABLED, |
| 4052 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4053 | pool_.get(), NetLogWithSource())); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4054 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4055 | ASSERT_TRUE(pool_->HasGroup(TestGroupId("a"))); |
| 4056 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 4057 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 4058 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 4059 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
| 4060 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroup(TestGroupId("a"))); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4061 | |
| 4062 | // Drain the pending read. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 4063 | EXPECT_EQ(1, handle.socket()->Read(nullptr, 1, CompletionOnceCallback())); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4064 | |
| 4065 | TestLoadTimingInfoConnectedReused(handle); |
| 4066 | handle.Reset(); |
| 4067 | |
| 4068 | // The socket should be usable now that it's idle again. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4069 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4070 | } |
| 4071 | |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4072 | TEST_F(ClientSocketPoolBaseTest, RequestGetsAssignedJob) { |
| 4073 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4074 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4075 | |
| 4076 | ClientSocketHandle handle1; |
| 4077 | TestCompletionCallback callback1; |
| 4078 | EXPECT_EQ( |
| 4079 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4080 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4081 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4082 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4083 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4084 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4085 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 4086 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 4087 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 4088 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4089 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4090 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4091 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4092 | } |
| 4093 | |
| 4094 | TEST_F(ClientSocketPoolBaseTest, MultipleRequestsGetAssignedJobs) { |
| 4095 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4096 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4097 | |
| 4098 | ClientSocketHandle handle1; |
| 4099 | TestCompletionCallback callback1; |
| 4100 | EXPECT_EQ( |
| 4101 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4102 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4103 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4104 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4105 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4106 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4107 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 4108 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 4109 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 4110 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4111 | |
| 4112 | ClientSocketHandle handle2; |
| 4113 | TestCompletionCallback callback2; |
| 4114 | EXPECT_EQ( |
| 4115 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4116 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4117 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4118 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4119 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4120 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4121 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 4122 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 4123 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 4124 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4125 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4126 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4127 | &handle1)); |
| 4128 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4129 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4130 | |
| 4131 | // One job completes. The other request should still have its job. |
| 4132 | client_socket_factory_.SignalJob(0); |
| 4133 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 4134 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4135 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 4136 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 4137 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 4138 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroup(TestGroupId("a"))); |
| 4139 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4140 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4141 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4142 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4143 | } |
| 4144 | |
| 4145 | TEST_F(ClientSocketPoolBaseTest, PreconnectJobGetsAssignedToRequest) { |
| 4146 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4147 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4148 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4149 | pool_->RequestSockets(TestGroupId("a"), ¶ms_, 1, NetLogWithSource()); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4150 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4151 | ASSERT_TRUE(pool_->HasGroup(TestGroupId("a"))); |
| 4152 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 4153 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 4154 | EXPECT_EQ(1u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 4155 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4156 | |
| 4157 | ClientSocketHandle handle1; |
| 4158 | TestCompletionCallback callback1; |
| 4159 | EXPECT_EQ( |
| 4160 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4161 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4162 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4163 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4164 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4165 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4166 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 4167 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 4168 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 4169 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4170 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4171 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4172 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4173 | } |
| 4174 | |
| 4175 | TEST_F(ClientSocketPoolBaseTest, HigherPriorityRequestStealsJob) { |
| 4176 | CreatePool(kDefaultMaxSockets, 1); |
| 4177 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4178 | |
| 4179 | ClientSocketHandle handle1; |
| 4180 | TestCompletionCallback callback1; |
| 4181 | EXPECT_EQ( |
| 4182 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4183 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4184 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4185 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4186 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4187 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4188 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 4189 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 4190 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 4191 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4192 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4193 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4194 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4195 | |
| 4196 | // Insert a higher priority request |
| 4197 | ClientSocketHandle handle2; |
| 4198 | TestCompletionCallback callback2; |
| 4199 | EXPECT_EQ( |
| 4200 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4201 | handle2.Init(TestGroupId("a"), params_, HIGHEST, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4202 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4203 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4204 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4205 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4206 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 4207 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 4208 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 4209 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4210 | |
| 4211 | // The highest priority request should steal the job from the default priority |
| 4212 | // request. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4213 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4214 | &handle2)); |
| 4215 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4216 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4217 | } |
| 4218 | |
| 4219 | TEST_F(ClientSocketPoolBaseTest, RequestStealsJobFromLowestRequestWithJob) { |
| 4220 | CreatePool(3, 3); |
| 4221 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4222 | |
| 4223 | ClientSocketHandle handle_lowest; |
| 4224 | TestCompletionCallback callback_lowest; |
| 4225 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4226 | handle_lowest.Init(TestGroupId("a"), params_, LOWEST, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4227 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4228 | callback_lowest.callback(), |
| 4229 | ClientSocketPool::ProxyAuthCallback(), |
| 4230 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4231 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4232 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 4233 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 4234 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 4235 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4236 | |
| 4237 | ClientSocketHandle handle_highest; |
| 4238 | TestCompletionCallback callback_highest; |
| 4239 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4240 | handle_highest.Init(TestGroupId("a"), params_, HIGHEST, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4241 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4242 | callback_highest.callback(), |
| 4243 | ClientSocketPool::ProxyAuthCallback(), |
| 4244 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4245 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4246 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 4247 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 4248 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 4249 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4250 | |
| 4251 | ClientSocketHandle handle_low; |
| 4252 | TestCompletionCallback callback_low; |
| 4253 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4254 | handle_low.Init(TestGroupId("a"), params_, LOW, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4255 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4256 | callback_low.callback(), |
| 4257 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4258 | NetLogWithSource())); |
| 4259 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4260 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 4261 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 4262 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 4263 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4264 | |
| 4265 | ClientSocketHandle handle_lowest2; |
| 4266 | TestCompletionCallback callback_lowest2; |
| 4267 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4268 | handle_lowest2.Init(TestGroupId("a"), params_, LOWEST, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4269 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4270 | callback_lowest2.callback(), |
| 4271 | ClientSocketPool::ProxyAuthCallback(), |
| 4272 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4273 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4274 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 4275 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 4276 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 4277 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4278 | |
| 4279 | // The top three requests in the queue should have jobs. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4280 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4281 | &handle_highest)); |
| 4282 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4283 | &handle_low)); |
| 4284 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4285 | &handle_lowest)); |
| 4286 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting( |
| 4287 | TestGroupId("a"), &handle_lowest2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4288 | |
| 4289 | // Add another request with medium priority. It should steal the job from the |
| 4290 | // lowest priority request with a job. |
| 4291 | ClientSocketHandle handle_medium; |
| 4292 | TestCompletionCallback callback_medium; |
| 4293 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4294 | handle_medium.Init(TestGroupId("a"), params_, MEDIUM, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4295 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4296 | callback_medium.callback(), |
| 4297 | ClientSocketPool::ProxyAuthCallback(), |
| 4298 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4299 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4300 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 4301 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 4302 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 4303 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
| 4304 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4305 | &handle_highest)); |
| 4306 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4307 | &handle_medium)); |
| 4308 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4309 | &handle_low)); |
| 4310 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4311 | &handle_lowest)); |
| 4312 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting( |
| 4313 | TestGroupId("a"), &handle_lowest2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4314 | } |
| 4315 | |
| 4316 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeRequestStealsJob) { |
| 4317 | CreatePool(kDefaultMaxSockets, 1); |
| 4318 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4319 | |
| 4320 | ClientSocketHandle handle1; |
| 4321 | TestCompletionCallback callback1; |
| 4322 | EXPECT_EQ( |
| 4323 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4324 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4325 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4326 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4327 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4328 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4329 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 4330 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 4331 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 4332 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4333 | |
| 4334 | ClientSocketHandle handle2; |
| 4335 | TestCompletionCallback callback2; |
| 4336 | EXPECT_EQ( |
| 4337 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4338 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4339 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4340 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4341 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4342 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4343 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 4344 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 4345 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 4346 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4347 | |
| 4348 | // The second request doesn't get a job because we are at the limit. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4349 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4350 | &handle1)); |
| 4351 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4352 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4353 | |
| 4354 | // Reprioritizing the second request places it above the first, and it steals |
| 4355 | // the job from the first request. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4356 | pool_->SetPriority(TestGroupId("a"), &handle2, HIGHEST); |
| 4357 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4358 | &handle2)); |
| 4359 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4360 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4361 | } |
| 4362 | |
| 4363 | TEST_F(ClientSocketPoolBaseTest, CancelRequestReassignsJob) { |
| 4364 | CreatePool(kDefaultMaxSockets, 1); |
| 4365 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4366 | |
| 4367 | ClientSocketHandle handle1; |
| 4368 | TestCompletionCallback callback1; |
| 4369 | EXPECT_EQ( |
| 4370 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4371 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4372 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4373 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4374 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4375 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4376 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 4377 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 4378 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 4379 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4380 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4381 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4382 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4383 | |
| 4384 | ClientSocketHandle handle2; |
| 4385 | TestCompletionCallback callback2; |
| 4386 | EXPECT_EQ( |
| 4387 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4388 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4389 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4390 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4391 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4392 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4393 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 4394 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 4395 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 4396 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4397 | |
| 4398 | // The second request doesn't get a job because we are the limit. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4399 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4400 | &handle1)); |
| 4401 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4402 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4403 | |
| 4404 | // The second request should get a job upon cancelling the first request. |
| 4405 | handle1.Reset(); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4406 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 4407 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 4408 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 4409 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4410 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4411 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4412 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4413 | } |
| 4414 | |
| 4415 | TEST_F(ClientSocketPoolBaseTest, JobCompletionReassignsJob) { |
| 4416 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4417 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4418 | |
| 4419 | ClientSocketHandle handle1; |
| 4420 | TestCompletionCallback callback1; |
| 4421 | EXPECT_EQ( |
| 4422 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4423 | handle1.Init(TestGroupId("a"), params_, HIGHEST, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4424 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4425 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4426 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4427 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4428 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 4429 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 4430 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 4431 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4432 | |
| 4433 | ClientSocketHandle handle2; |
| 4434 | TestCompletionCallback callback2; |
| 4435 | EXPECT_EQ( |
| 4436 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4437 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4438 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4439 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4440 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4441 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4442 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 4443 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 4444 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 4445 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4446 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4447 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4448 | &handle1)); |
| 4449 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4450 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4451 | |
| 4452 | // The lower-priority job completes first. The higher-priority request should |
| 4453 | // get the socket, and the lower-priority request should get the remaining |
| 4454 | // job. |
| 4455 | client_socket_factory_.SignalJob(1); |
| 4456 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4457 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 4458 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 4459 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 4460 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroup(TestGroupId("a"))); |
| 4461 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4462 | EXPECT_TRUE(handle1.socket()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4463 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4464 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4465 | } |
| 4466 | |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 4467 | class MockLayeredPool : public HigherLayeredPool { |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4468 | public: |
| 4469 | MockLayeredPool(TestClientSocketPool* pool, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4470 | const ClientSocketPool::GroupId& group_id) |
| 4471 | : pool_(pool), group_id_(group_id), can_release_connection_(true) { |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 4472 | pool_->AddHigherLayeredPool(this); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4473 | } |
| 4474 | |
Daniel Cheng | 4496d082 | 2018-04-26 21:52:15 | [diff] [blame] | 4475 | ~MockLayeredPool() override { pool_->RemoveHigherLayeredPool(this); } |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4476 | |
| 4477 | int RequestSocket(TestClientSocketPool* pool) { |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4478 | scoped_refptr<TestSocketParams> params(new TestSocketParams()); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4479 | return handle_.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4480 | group_id_, params, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4481 | ClientSocketPool::RespectLimits::ENABLED, callback_.callback(), |
| 4482 | ClientSocketPool::ProxyAuthCallback(), pool, NetLogWithSource()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4483 | } |
| 4484 | |
| 4485 | int RequestSocketWithoutLimits(TestClientSocketPool* pool) { |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4486 | scoped_refptr<TestSocketParams> params(new TestSocketParams()); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4487 | return handle_.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4488 | group_id_, params, MAXIMUM_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4489 | ClientSocketPool::RespectLimits::DISABLED, callback_.callback(), |
| 4490 | ClientSocketPool::ProxyAuthCallback(), pool, NetLogWithSource()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4491 | } |
| 4492 | |
| 4493 | bool ReleaseOneConnection() { |
| 4494 | if (!handle_.is_initialized() || !can_release_connection_) { |
| 4495 | return false; |
| 4496 | } |
| 4497 | handle_.socket()->Disconnect(); |
| 4498 | handle_.Reset(); |
| 4499 | return true; |
| 4500 | } |
| 4501 | |
| 4502 | void set_can_release_connection(bool can_release_connection) { |
| 4503 | can_release_connection_ = can_release_connection; |
| 4504 | } |
| 4505 | |
| 4506 | MOCK_METHOD0(CloseOneIdleConnection, bool()); |
| 4507 | |
| 4508 | private: |
| 4509 | TestClientSocketPool* const pool_; |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4510 | ClientSocketHandle handle_; |
| 4511 | TestCompletionCallback callback_; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4512 | const ClientSocketPool::GroupId group_id_; |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4513 | bool can_release_connection_; |
| 4514 | }; |
| 4515 | |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4516 | // Tests the basic case of closing an idle socket in a higher layered pool when |
| 4517 | // a new request is issued and the lower layer pool is stalled. |
| 4518 | TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketsHeldByLayeredPoolWhenNeeded) { |
| 4519 | CreatePool(1, 1); |
| 4520 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 4521 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4522 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("foo")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4523 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4524 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4525 | .WillOnce(Invoke(&mock_layered_pool, |
| 4526 | &MockLayeredPool::ReleaseOneConnection)); |
| 4527 | ClientSocketHandle handle; |
| 4528 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4529 | EXPECT_EQ( |
| 4530 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4531 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4532 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 4533 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4534 | NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4535 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4536 | } |
| 4537 | |
Matt Menke | 83367864 | 2019-03-05 22:05:51 | [diff] [blame] | 4538 | // Tests the case that trying to close an idle socket in a higher layered pool |
| 4539 | // fails. |
| 4540 | TEST_F(ClientSocketPoolBaseTest, |
| 4541 | CloseIdleSocketsHeldByLayeredPoolWhenNeededFails) { |
| 4542 | CreatePool(1, 1); |
| 4543 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 4544 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4545 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("foo")); |
Matt Menke | 83367864 | 2019-03-05 22:05:51 | [diff] [blame] | 4546 | mock_layered_pool.set_can_release_connection(false); |
| 4547 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
| 4548 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4549 | .WillOnce(Invoke(&mock_layered_pool, |
| 4550 | &MockLayeredPool::ReleaseOneConnection)); |
| 4551 | ClientSocketHandle handle; |
| 4552 | TestCompletionCallback callback; |
| 4553 | EXPECT_EQ( |
| 4554 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4555 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 83367864 | 2019-03-05 22:05:51 | [diff] [blame] | 4556 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 4557 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4558 | NetLogWithSource())); |
| 4559 | base::RunLoop().RunUntilIdle(); |
| 4560 | EXPECT_FALSE(callback.have_result()); |
| 4561 | } |
| 4562 | |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4563 | // Same as above, but the idle socket is in the same group as the stalled |
| 4564 | // socket, and closes the only other request in its group when closing requests |
| 4565 | // in higher layered pools. This generally shouldn't happen, but it may be |
| 4566 | // possible if a higher level pool issues a request and the request is |
| 4567 | // subsequently cancelled. Even if it's not possible, best not to crash. |
| 4568 | TEST_F(ClientSocketPoolBaseTest, |
| 4569 | CloseIdleSocketsHeldByLayeredPoolWhenNeededSameGroup) { |
| 4570 | CreatePool(2, 2); |
| 4571 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 4572 | |
| 4573 | // Need a socket in another group for the pool to be stalled (If a group |
| 4574 | // has the maximum number of connections already, it's not stalled). |
| 4575 | ClientSocketHandle handle1; |
| 4576 | TestCompletionCallback callback1; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4577 | EXPECT_EQ(OK, handle1.Init( |
| 4578 | TestGroupId("group1"), params_, DEFAULT_PRIORITY, |
| 4579 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4580 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4581 | pool_.get(), NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4582 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4583 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("group2")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4584 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4585 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4586 | .WillOnce(Invoke(&mock_layered_pool, |
| 4587 | &MockLayeredPool::ReleaseOneConnection)); |
| 4588 | ClientSocketHandle handle; |
| 4589 | TestCompletionCallback callback2; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4590 | EXPECT_EQ( |
| 4591 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4592 | handle.Init(TestGroupId("group2"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4593 | ClientSocketPool::RespectLimits::ENABLED, |
| 4594 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4595 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4596 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4597 | } |
| 4598 | |
| 4599 | // Tests the case when an idle socket can be closed when a new request is |
| 4600 | // issued, and the new request belongs to a group that was previously stalled. |
| 4601 | TEST_F(ClientSocketPoolBaseTest, |
| 4602 | CloseIdleSocketsHeldByLayeredPoolInSameGroupWhenNeeded) { |
| 4603 | CreatePool(2, 2); |
| 4604 | std::list<TestConnectJob::JobType> job_types; |
| 4605 | job_types.push_back(TestConnectJob::kMockJob); |
| 4606 | job_types.push_back(TestConnectJob::kMockJob); |
| 4607 | job_types.push_back(TestConnectJob::kMockJob); |
| 4608 | job_types.push_back(TestConnectJob::kMockJob); |
| 4609 | connect_job_factory_->set_job_types(&job_types); |
| 4610 | |
| 4611 | ClientSocketHandle handle1; |
| 4612 | TestCompletionCallback callback1; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4613 | EXPECT_EQ(OK, handle1.Init( |
| 4614 | TestGroupId("group1"), params_, DEFAULT_PRIORITY, |
| 4615 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4616 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4617 | pool_.get(), NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4618 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4619 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("group2")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4620 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4621 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4622 | .WillRepeatedly(Invoke(&mock_layered_pool, |
| 4623 | &MockLayeredPool::ReleaseOneConnection)); |
| 4624 | mock_layered_pool.set_can_release_connection(false); |
| 4625 | |
| 4626 | // The third request is made when the socket pool is in a stalled state. |
| 4627 | ClientSocketHandle handle3; |
| 4628 | TestCompletionCallback callback3; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4629 | EXPECT_EQ( |
| 4630 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4631 | handle3.Init(TestGroupId("group3"), params_, DEFAULT_PRIORITY, |
| 4632 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4633 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4634 | pool_.get(), NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4635 | |
| 4636 | base::RunLoop().RunUntilIdle(); |
| 4637 | EXPECT_FALSE(callback3.have_result()); |
| 4638 | |
| 4639 | // The fourth request is made when the pool is no longer stalled. The third |
| 4640 | // request should be serviced first, since it was issued first and has the |
| 4641 | // same priority. |
| 4642 | mock_layered_pool.set_can_release_connection(true); |
| 4643 | ClientSocketHandle handle4; |
| 4644 | TestCompletionCallback callback4; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4645 | EXPECT_EQ( |
| 4646 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4647 | handle4.Init(TestGroupId("group3"), params_, DEFAULT_PRIORITY, |
| 4648 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4649 | callback4.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4650 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4651 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4652 | EXPECT_FALSE(callback4.have_result()); |
| 4653 | |
| 4654 | // Closing a handle should free up another socket slot. |
| 4655 | handle1.Reset(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4656 | EXPECT_THAT(callback4.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4657 | } |
| 4658 | |
| 4659 | // Tests the case when an idle socket can be closed when a new request is |
| 4660 | // issued, and the new request belongs to a group that was previously stalled. |
| 4661 | // |
| 4662 | // The two differences from the above test are that the stalled requests are not |
| 4663 | // in the same group as the layered pool's request, and the the fourth request |
| 4664 | // has a higher priority than the third one, so gets a socket first. |
| 4665 | TEST_F(ClientSocketPoolBaseTest, |
| 4666 | CloseIdleSocketsHeldByLayeredPoolInSameGroupWhenNeeded2) { |
| 4667 | CreatePool(2, 2); |
| 4668 | std::list<TestConnectJob::JobType> job_types; |
| 4669 | job_types.push_back(TestConnectJob::kMockJob); |
| 4670 | job_types.push_back(TestConnectJob::kMockJob); |
| 4671 | job_types.push_back(TestConnectJob::kMockJob); |
| 4672 | job_types.push_back(TestConnectJob::kMockJob); |
| 4673 | connect_job_factory_->set_job_types(&job_types); |
| 4674 | |
| 4675 | ClientSocketHandle handle1; |
| 4676 | TestCompletionCallback callback1; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4677 | EXPECT_EQ(OK, handle1.Init( |
| 4678 | TestGroupId("group1"), params_, DEFAULT_PRIORITY, |
| 4679 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4680 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4681 | pool_.get(), NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4682 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4683 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("group2")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4684 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4685 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4686 | .WillRepeatedly(Invoke(&mock_layered_pool, |
| 4687 | &MockLayeredPool::ReleaseOneConnection)); |
| 4688 | mock_layered_pool.set_can_release_connection(false); |
| 4689 | |
| 4690 | // The third request is made when the socket pool is in a stalled state. |
| 4691 | ClientSocketHandle handle3; |
| 4692 | TestCompletionCallback callback3; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4693 | EXPECT_EQ( |
| 4694 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4695 | handle3.Init(TestGroupId("group3"), params_, MEDIUM, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4696 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4697 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4698 | pool_.get(), NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4699 | |
| 4700 | base::RunLoop().RunUntilIdle(); |
| 4701 | EXPECT_FALSE(callback3.have_result()); |
| 4702 | |
| 4703 | // The fourth request is made when the pool is no longer stalled. This |
| 4704 | // request has a higher priority than the third request, so is serviced first. |
| 4705 | mock_layered_pool.set_can_release_connection(true); |
| 4706 | ClientSocketHandle handle4; |
| 4707 | TestCompletionCallback callback4; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4708 | EXPECT_EQ( |
| 4709 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4710 | handle4.Init(TestGroupId("group3"), params_, HIGHEST, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4711 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4712 | callback4.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4713 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4714 | EXPECT_THAT(callback4.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4715 | EXPECT_FALSE(callback3.have_result()); |
| 4716 | |
| 4717 | // Closing a handle should free up another socket slot. |
| 4718 | handle1.Reset(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4719 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4720 | } |
| 4721 | |
| 4722 | TEST_F(ClientSocketPoolBaseTest, |
| 4723 | CloseMultipleIdleSocketsHeldByLayeredPoolWhenNeeded) { |
| 4724 | CreatePool(1, 1); |
| 4725 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 4726 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4727 | MockLayeredPool mock_layered_pool1(pool_.get(), TestGroupId("foo")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4728 | EXPECT_THAT(mock_layered_pool1.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4729 | EXPECT_CALL(mock_layered_pool1, CloseOneIdleConnection()) |
| 4730 | .WillRepeatedly(Invoke(&mock_layered_pool1, |
| 4731 | &MockLayeredPool::ReleaseOneConnection)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4732 | MockLayeredPool mock_layered_pool2(pool_.get(), TestGroupId("bar")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4733 | EXPECT_THAT(mock_layered_pool2.RequestSocketWithoutLimits(pool_.get()), |
| 4734 | IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4735 | EXPECT_CALL(mock_layered_pool2, CloseOneIdleConnection()) |
| 4736 | .WillRepeatedly(Invoke(&mock_layered_pool2, |
| 4737 | &MockLayeredPool::ReleaseOneConnection)); |
| 4738 | ClientSocketHandle handle; |
| 4739 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4740 | EXPECT_EQ( |
| 4741 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4742 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4743 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 4744 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4745 | NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4746 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4747 | } |
| 4748 | |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4749 | // Test that when a socket pool and group are at their limits, a request |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4750 | // with RespectLimits::DISABLED triggers creation of a new socket, and gets the |
| 4751 | // socket instead of a request with the same priority that was issued earlier, |
| 4752 | // but has RespectLimits::ENABLED. |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4753 | TEST_F(ClientSocketPoolBaseTest, IgnoreLimits) { |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4754 | CreatePool(1, 1); |
| 4755 | |
| 4756 | // Issue a request to reach the socket pool limit. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4757 | EXPECT_EQ(OK, StartRequestWithIgnoreLimits( |
| 4758 | TestGroupId("a"), MAXIMUM_PRIORITY, |
| 4759 | ClientSocketPool::RespectLimits::ENABLED)); |
| 4760 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4761 | |
| 4762 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 4763 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4764 | EXPECT_EQ(ERR_IO_PENDING, StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4765 | TestGroupId("a"), MAXIMUM_PRIORITY, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4766 | ClientSocketPool::RespectLimits::ENABLED)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4767 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4768 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4769 | // Issue a request that ignores the limits, so a new ConnectJob is |
| 4770 | // created. |
| 4771 | EXPECT_EQ(ERR_IO_PENDING, StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4772 | TestGroupId("a"), MAXIMUM_PRIORITY, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4773 | ClientSocketPool::RespectLimits::DISABLED)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4774 | ASSERT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4775 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4776 | EXPECT_THAT(request(2)->WaitForResult(), IsOk()); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4777 | EXPECT_FALSE(request(1)->have_result()); |
| 4778 | } |
| 4779 | |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 4780 | // Test that when a socket pool and group are at their limits, a ConnectJob |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4781 | // issued for a request with RespectLimits::DISABLED is not cancelled when a |
| 4782 | // request with RespectLimits::ENABLED issued to the same group is cancelled. |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 4783 | TEST_F(ClientSocketPoolBaseTest, IgnoreLimitsCancelOtherJob) { |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 4784 | CreatePool(1, 1); |
| 4785 | |
| 4786 | // Issue a request to reach the socket pool limit. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4787 | EXPECT_EQ(OK, StartRequestWithIgnoreLimits( |
| 4788 | TestGroupId("a"), MAXIMUM_PRIORITY, |
| 4789 | ClientSocketPool::RespectLimits::ENABLED)); |
| 4790 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 4791 | |
| 4792 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 4793 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4794 | EXPECT_EQ(ERR_IO_PENDING, StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4795 | TestGroupId("a"), MAXIMUM_PRIORITY, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4796 | ClientSocketPool::RespectLimits::ENABLED)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4797 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 4798 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4799 | // Issue a request with RespectLimits::DISABLED, so a new ConnectJob is |
| 4800 | // created. |
| 4801 | EXPECT_EQ(ERR_IO_PENDING, StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4802 | TestGroupId("a"), MAXIMUM_PRIORITY, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4803 | ClientSocketPool::RespectLimits::DISABLED)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4804 | ASSERT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4805 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4806 | // Cancel the pending request with RespectLimits::ENABLED. The ConnectJob |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4807 | // should not be cancelled. |
| 4808 | request(1)->handle()->Reset(); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4809 | ASSERT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4810 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4811 | EXPECT_THAT(request(2)->WaitForResult(), IsOk()); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4812 | EXPECT_FALSE(request(1)->have_result()); |
| 4813 | } |
| 4814 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4815 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthNoAuthCallback) { |
| 4816 | CreatePool(1, 1); |
| 4817 | |
| 4818 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 4819 | |
| 4820 | ClientSocketHandle handle; |
| 4821 | TestCompletionCallback callback; |
| 4822 | EXPECT_EQ( |
| 4823 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4824 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4825 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 4826 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4827 | NetLogWithSource())); |
| 4828 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4829 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4830 | |
| 4831 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_PROXY_AUTH_REQUESTED)); |
| 4832 | EXPECT_FALSE(handle.is_initialized()); |
| 4833 | EXPECT_FALSE(handle.socket()); |
| 4834 | |
| 4835 | // The group should now be empty, and thus be deleted. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4836 | EXPECT_FALSE(pool_->HasGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4837 | } |
| 4838 | |
| 4839 | class TestAuthHelper { |
| 4840 | public: |
| 4841 | TestAuthHelper() = default; |
| 4842 | ~TestAuthHelper() = default; |
| 4843 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4844 | void InitHandle( |
| 4845 | scoped_refptr<TestSocketParams> params, |
| 4846 | TestClientSocketPool* pool, |
| 4847 | RequestPriority priority = DEFAULT_PRIORITY, |
| 4848 | ClientSocketPool::RespectLimits respect_limits = |
| 4849 | ClientSocketPool::RespectLimits::ENABLED, |
| 4850 | const ClientSocketPool::GroupId& group_id_in = TestGroupId("a")) { |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4851 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4852 | handle_.Init(group_id_in, params.get(), priority, SocketTag(), |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4853 | respect_limits, callback_.callback(), |
| 4854 | base::BindRepeating(&TestAuthHelper::AuthCallback, |
| 4855 | base::Unretained(this)), |
| 4856 | pool, NetLogWithSource())); |
| 4857 | } |
| 4858 | |
| 4859 | void WaitForAuth() { |
| 4860 | run_loop_ = std::make_unique<base::RunLoop>(); |
| 4861 | run_loop_->Run(); |
| 4862 | run_loop_.reset(); |
| 4863 | } |
| 4864 | |
| 4865 | void WaitForAuthAndRestartSync() { |
| 4866 | restart_sync_ = true; |
| 4867 | WaitForAuth(); |
| 4868 | restart_sync_ = false; |
| 4869 | } |
| 4870 | |
| 4871 | void WaitForAuthAndResetHandleSync() { |
| 4872 | reset_handle_sync_ = true; |
| 4873 | WaitForAuth(); |
| 4874 | reset_handle_sync_ = false; |
| 4875 | } |
| 4876 | |
| 4877 | void RestartWithAuth() { |
| 4878 | DCHECK(restart_with_auth_callback_); |
| 4879 | std::move(restart_with_auth_callback_).Run(); |
| 4880 | } |
| 4881 | |
| 4882 | int WaitForResult() { |
| 4883 | int result = callback_.WaitForResult(); |
| 4884 | // There shouldn't be any callback waiting to be invoked once the request is |
| 4885 | // complete. |
| 4886 | EXPECT_FALSE(restart_with_auth_callback_); |
| 4887 | // The socket should only be initialized on success. |
| 4888 | EXPECT_EQ(result == OK, handle_.is_initialized()); |
| 4889 | EXPECT_EQ(result == OK, handle_.socket() != nullptr); |
| 4890 | return result; |
| 4891 | } |
| 4892 | |
| 4893 | ClientSocketHandle* handle() { return &handle_; } |
| 4894 | int auth_count() const { return auth_count_; } |
| 4895 | int have_result() const { return callback_.have_result(); } |
| 4896 | |
| 4897 | private: |
| 4898 | void AuthCallback(const HttpResponseInfo& response, |
| 4899 | HttpAuthController* auth_controller, |
| 4900 | base::OnceClosure restart_with_auth_callback) { |
| 4901 | EXPECT_FALSE(restart_with_auth_callback_); |
| 4902 | EXPECT_TRUE(restart_with_auth_callback); |
| 4903 | |
| 4904 | // Once there's a result, this method shouldn't be invoked again. |
| 4905 | EXPECT_FALSE(callback_.have_result()); |
| 4906 | |
| 4907 | ++auth_count_; |
| 4908 | run_loop_->Quit(); |
| 4909 | if (restart_sync_) { |
| 4910 | std::move(restart_with_auth_callback).Run(); |
| 4911 | return; |
| 4912 | } |
| 4913 | |
| 4914 | restart_with_auth_callback_ = std::move(restart_with_auth_callback); |
| 4915 | |
| 4916 | if (reset_handle_sync_) { |
| 4917 | handle_.Reset(); |
| 4918 | return; |
| 4919 | } |
| 4920 | } |
| 4921 | |
| 4922 | std::unique_ptr<base::RunLoop> run_loop_; |
| 4923 | base::OnceClosure restart_with_auth_callback_; |
| 4924 | |
| 4925 | bool restart_sync_ = false; |
| 4926 | bool reset_handle_sync_ = false; |
| 4927 | |
| 4928 | ClientSocketHandle handle_; |
| 4929 | int auth_count_ = 0; |
| 4930 | TestCompletionCallback callback_; |
| 4931 | |
| 4932 | DISALLOW_COPY_AND_ASSIGN(TestAuthHelper); |
| 4933 | }; |
| 4934 | |
| 4935 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnce) { |
| 4936 | CreatePool(1, 1); |
| 4937 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 4938 | |
| 4939 | TestAuthHelper auth_helper; |
| 4940 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4941 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 4942 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4943 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4944 | |
| 4945 | auth_helper.WaitForAuth(); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4946 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 4947 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4948 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4949 | |
| 4950 | auth_helper.RestartWithAuth(); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4951 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 4952 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4953 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4954 | |
| 4955 | EXPECT_THAT(auth_helper.WaitForResult(), IsOk()); |
| 4956 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4957 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 4958 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroup(TestGroupId("a"))); |
| 4959 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4960 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 4961 | } |
| 4962 | |
| 4963 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceSync) { |
| 4964 | CreatePool(1, 1); |
| 4965 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 4966 | |
| 4967 | TestAuthHelper auth_helper; |
| 4968 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4969 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 4970 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4971 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4972 | |
| 4973 | auth_helper.WaitForAuthAndRestartSync(); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4974 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 4975 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4976 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4977 | |
| 4978 | EXPECT_THAT(auth_helper.WaitForResult(), IsOk()); |
| 4979 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4980 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 4981 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroup(TestGroupId("a"))); |
| 4982 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4983 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 4984 | } |
| 4985 | |
| 4986 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceFails) { |
| 4987 | CreatePool(1, 1); |
| 4988 | connect_job_factory_->set_job_type( |
| 4989 | TestConnectJob::kMockAuthChallengeOnceFailingJob); |
| 4990 | |
| 4991 | TestAuthHelper auth_helper; |
| 4992 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4993 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4994 | |
| 4995 | auth_helper.WaitForAuth(); |
| 4996 | auth_helper.RestartWithAuth(); |
| 4997 | EXPECT_THAT(auth_helper.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 4998 | |
| 4999 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5000 | EXPECT_FALSE(pool_->HasGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5001 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5002 | } |
| 5003 | |
| 5004 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceSyncFails) { |
| 5005 | CreatePool(1, 1); |
| 5006 | connect_job_factory_->set_job_type( |
| 5007 | TestConnectJob::kMockAuthChallengeOnceFailingJob); |
| 5008 | |
| 5009 | TestAuthHelper auth_helper; |
| 5010 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5011 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5012 | |
| 5013 | auth_helper.WaitForAuthAndRestartSync(); |
| 5014 | EXPECT_THAT(auth_helper.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 5015 | |
| 5016 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5017 | EXPECT_FALSE(pool_->HasGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5018 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5019 | } |
| 5020 | |
| 5021 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceDeleteHandle) { |
| 5022 | CreatePool(1, 1); |
| 5023 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5024 | |
| 5025 | TestAuthHelper auth_helper; |
| 5026 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5027 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5028 | |
| 5029 | auth_helper.WaitForAuth(); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5030 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5031 | |
| 5032 | auth_helper.handle()->Reset(); |
| 5033 | |
| 5034 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5035 | EXPECT_FALSE(pool_->HasGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5036 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5037 | EXPECT_FALSE(auth_helper.handle()->is_initialized()); |
| 5038 | EXPECT_FALSE(auth_helper.handle()->socket()); |
| 5039 | } |
| 5040 | |
| 5041 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceDeleteHandleSync) { |
| 5042 | CreatePool(1, 1); |
| 5043 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5044 | |
| 5045 | TestAuthHelper auth_helper; |
| 5046 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5047 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5048 | |
| 5049 | auth_helper.WaitForAuthAndResetHandleSync(); |
| 5050 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5051 | EXPECT_FALSE(pool_->HasGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5052 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5053 | EXPECT_FALSE(auth_helper.handle()->is_initialized()); |
| 5054 | EXPECT_FALSE(auth_helper.handle()->socket()); |
| 5055 | } |
| 5056 | |
| 5057 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceFlushWithError) { |
| 5058 | CreatePool(1, 1); |
| 5059 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5060 | |
| 5061 | TestAuthHelper auth_helper; |
| 5062 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5063 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5064 | |
| 5065 | auth_helper.WaitForAuth(); |
| 5066 | |
| 5067 | pool_->FlushWithError(ERR_FAILED); |
| 5068 | base::RunLoop().RunUntilIdle(); |
| 5069 | |
| 5070 | // When flushing the socket pool, bound sockets should delay returning the |
| 5071 | // error until completion. |
| 5072 | EXPECT_FALSE(auth_helper.have_result()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5073 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5074 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5075 | |
| 5076 | auth_helper.RestartWithAuth(); |
| 5077 | // The callback should be called asynchronously. |
| 5078 | EXPECT_FALSE(auth_helper.have_result()); |
| 5079 | |
| 5080 | EXPECT_THAT(auth_helper.WaitForResult(), IsError(ERR_FAILED)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5081 | EXPECT_FALSE(pool_->HasGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5082 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5083 | } |
| 5084 | |
| 5085 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthTwice) { |
| 5086 | CreatePool(1, 1); |
| 5087 | connect_job_factory_->set_job_type( |
| 5088 | TestConnectJob::kMockAuthChallengeTwiceJob); |
| 5089 | |
| 5090 | TestAuthHelper auth_helper; |
| 5091 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5092 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5093 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5094 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5095 | |
| 5096 | auth_helper.WaitForAuth(); |
| 5097 | auth_helper.RestartWithAuth(); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5098 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5099 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5100 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5101 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5102 | |
| 5103 | auth_helper.WaitForAuth(); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5104 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5105 | EXPECT_EQ(2, auth_helper.auth_count()); |
| 5106 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5107 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5108 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5109 | auth_helper.RestartWithAuth(); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5110 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5111 | EXPECT_EQ(2, auth_helper.auth_count()); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5112 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5113 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5114 | |
| 5115 | EXPECT_THAT(auth_helper.WaitForResult(), IsOk()); |
| 5116 | EXPECT_EQ(2, auth_helper.auth_count()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5117 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 5118 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroup(TestGroupId("a"))); |
| 5119 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5120 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5121 | } |
| 5122 | |
| 5123 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthTwiceFails) { |
| 5124 | CreatePool(1, 1); |
| 5125 | connect_job_factory_->set_job_type( |
| 5126 | TestConnectJob::kMockAuthChallengeTwiceFailingJob); |
| 5127 | |
| 5128 | TestAuthHelper auth_helper; |
| 5129 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5130 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5131 | |
| 5132 | auth_helper.WaitForAuth(); |
| 5133 | auth_helper.RestartWithAuth(); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5134 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5135 | EXPECT_EQ(1, auth_helper.auth_count()); |
| 5136 | |
| 5137 | auth_helper.WaitForAuth(); |
| 5138 | auth_helper.RestartWithAuth(); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5139 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5140 | EXPECT_EQ(2, auth_helper.auth_count()); |
| 5141 | |
| 5142 | EXPECT_THAT(auth_helper.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 5143 | EXPECT_EQ(2, auth_helper.auth_count()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5144 | EXPECT_FALSE(pool_->HasGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5145 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5146 | } |
| 5147 | |
| 5148 | // Makes sure that when a bound request is destroyed, a new ConnectJob is |
| 5149 | // created, if needed. |
| 5150 | TEST_F(ClientSocketPoolBaseTest, |
| 5151 | ProxyAuthCreateNewConnectJobOnDestroyBoundRequest) { |
| 5152 | CreatePool(1 /* max_sockets */, 1 /* max_sockets_per_group */); |
| 5153 | connect_job_factory_->set_job_type( |
| 5154 | TestConnectJob::kMockAuthChallengeOnceFailingJob); |
| 5155 | |
| 5156 | // First request creates a ConnectJob. |
| 5157 | TestAuthHelper auth_helper1; |
| 5158 | auth_helper1.InitHandle(params_, pool_.get()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5159 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5160 | |
| 5161 | // A second request come in, but no new ConnectJob is needed, since the limit |
| 5162 | // has been reached. |
| 5163 | TestAuthHelper auth_helper2; |
| 5164 | auth_helper2.InitHandle(params_, pool_.get()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5165 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5166 | |
| 5167 | // Run until the auth callback for the first request is invoked. |
| 5168 | auth_helper1.WaitForAuth(); |
| 5169 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5170 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 5171 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroup(TestGroupId("a"))); |
| 5172 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5173 | |
| 5174 | // Make connect jobs succeed, then cancel the first request, which should |
| 5175 | // destroy the bound ConnectJob, and cause a new ConnectJob to start. |
| 5176 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 5177 | auth_helper1.handle()->Reset(); |
| 5178 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5179 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5180 | |
| 5181 | // The second ConnectJob should succeed. |
| 5182 | EXPECT_THAT(auth_helper2.WaitForResult(), IsOk()); |
| 5183 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5184 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5185 | } |
| 5186 | |
| 5187 | // Makes sure that when a bound request is destroyed, a new ConnectJob is |
| 5188 | // created for another group, if needed. |
| 5189 | TEST_F(ClientSocketPoolBaseTest, |
| 5190 | ProxyAuthCreateNewConnectJobOnDestroyBoundRequestDifferentGroups) { |
| 5191 | CreatePool(1 /* max_sockets */, 1 /* max_sockets_per_group */); |
| 5192 | connect_job_factory_->set_job_type( |
| 5193 | TestConnectJob::kMockAuthChallengeOnceFailingJob); |
| 5194 | |
| 5195 | // First request creates a ConnectJob. |
| 5196 | TestAuthHelper auth_helper1; |
| 5197 | auth_helper1.InitHandle(params_, pool_.get(), DEFAULT_PRIORITY); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5198 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5199 | |
| 5200 | // A second request come in, but no new ConnectJob is needed, since the limit |
| 5201 | // has been reached. |
| 5202 | TestAuthHelper auth_helper2; |
| 5203 | auth_helper2.InitHandle(params_, pool_.get(), DEFAULT_PRIORITY, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5204 | ClientSocketPool::RespectLimits::ENABLED, |
| 5205 | TestGroupId("b")); |
| 5206 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 5207 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroup(TestGroupId("b"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5208 | |
| 5209 | // Run until the auth callback for the first request is invoked. |
| 5210 | auth_helper1.WaitForAuth(); |
| 5211 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5212 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 5213 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroup(TestGroupId("a"))); |
| 5214 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
| 5215 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroup(TestGroupId("b"))); |
| 5216 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroup(TestGroupId("b"))); |
| 5217 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5218 | |
| 5219 | // Make connect jobs succeed, then cancel the first request, which should |
| 5220 | // destroy the bound ConnectJob, and cause a new ConnectJob to start for the |
| 5221 | // other group. |
| 5222 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 5223 | auth_helper1.handle()->Reset(); |
| 5224 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5225 | EXPECT_FALSE(pool_->HasGroup(TestGroupId("a"))); |
| 5226 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("b"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5227 | |
| 5228 | // The second ConnectJob should succeed. |
| 5229 | EXPECT_THAT(auth_helper2.WaitForResult(), IsOk()); |
| 5230 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5231 | EXPECT_FALSE(pool_->HasGroup(TestGroupId("a"))); |
| 5232 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroup(TestGroupId("b"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5233 | } |
| 5234 | |
| 5235 | // Test that once an auth challenge is bound, that's the request that gets all |
| 5236 | // subsequent calls and the socket itself. |
| 5237 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthStaysBound) { |
| 5238 | CreatePool(1, 1); |
| 5239 | connect_job_factory_->set_job_type( |
| 5240 | TestConnectJob::kMockAuthChallengeTwiceJob); |
| 5241 | |
| 5242 | // First request creates a ConnectJob. |
| 5243 | TestAuthHelper auth_helper1; |
| 5244 | auth_helper1.InitHandle(params_, pool_.get(), LOWEST); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5245 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5246 | |
| 5247 | // A second, higher priority request is made. |
| 5248 | TestAuthHelper auth_helper2; |
| 5249 | auth_helper2.InitHandle(params_, pool_.get(), LOW); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5250 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5251 | |
| 5252 | // Run until the auth callback for the second request is invoked. |
| 5253 | auth_helper2.WaitForAuth(); |
| 5254 | EXPECT_EQ(0, auth_helper1.auth_count()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5255 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 5256 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroup(TestGroupId("a"))); |
| 5257 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5258 | |
| 5259 | // Start a higher priority job. It shouldn't be able to steal |auth_helper2|'s |
| 5260 | // ConnectJob. |
| 5261 | TestAuthHelper auth_helper3; |
| 5262 | auth_helper3.InitHandle(params_, pool_.get(), HIGHEST); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5263 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5264 | |
| 5265 | // Start a higher job that ignores limits, creating a hanging socket. It |
| 5266 | // shouldn't be able to steal |auth_helper2|'s ConnectJob. |
| 5267 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 5268 | TestAuthHelper auth_helper4; |
| 5269 | auth_helper4.InitHandle(params_, pool_.get(), HIGHEST, |
| 5270 | ClientSocketPool::RespectLimits::DISABLED); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5271 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5272 | |
| 5273 | // Restart with auth, and |auth_helper2|'s auth method should be invoked |
| 5274 | // again. |
| 5275 | auth_helper2.RestartWithAuth(); |
| 5276 | auth_helper2.WaitForAuth(); |
| 5277 | EXPECT_EQ(0, auth_helper1.auth_count()); |
| 5278 | EXPECT_FALSE(auth_helper1.have_result()); |
| 5279 | EXPECT_EQ(2, auth_helper2.auth_count()); |
| 5280 | EXPECT_FALSE(auth_helper2.have_result()); |
| 5281 | EXPECT_EQ(0, auth_helper3.auth_count()); |
| 5282 | EXPECT_FALSE(auth_helper3.have_result()); |
| 5283 | EXPECT_EQ(0, auth_helper4.auth_count()); |
| 5284 | EXPECT_FALSE(auth_helper4.have_result()); |
| 5285 | |
| 5286 | // Advance auth again, and |auth_helper2| should get the socket. |
| 5287 | auth_helper2.RestartWithAuth(); |
| 5288 | EXPECT_THAT(auth_helper2.WaitForResult(), IsOk()); |
| 5289 | // The hung ConnectJob for the RespectLimits::DISABLED request is still in the |
| 5290 | // socket pool. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5291 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 5292 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5293 | EXPECT_EQ(0, auth_helper1.auth_count()); |
| 5294 | EXPECT_FALSE(auth_helper1.have_result()); |
| 5295 | EXPECT_EQ(0, auth_helper3.auth_count()); |
| 5296 | EXPECT_FALSE(auth_helper3.have_result()); |
| 5297 | EXPECT_EQ(0, auth_helper4.auth_count()); |
| 5298 | EXPECT_FALSE(auth_helper4.have_result()); |
| 5299 | |
| 5300 | // If the socket is returned to the socket pool, the RespectLimits::DISABLED |
| 5301 | // socket request should be able to claim it. |
| 5302 | auth_helper2.handle()->Reset(); |
| 5303 | EXPECT_THAT(auth_helper4.WaitForResult(), IsOk()); |
| 5304 | EXPECT_EQ(0, auth_helper1.auth_count()); |
| 5305 | EXPECT_FALSE(auth_helper1.have_result()); |
| 5306 | EXPECT_EQ(0, auth_helper3.auth_count()); |
| 5307 | EXPECT_FALSE(auth_helper3.have_result()); |
| 5308 | EXPECT_EQ(0, auth_helper4.auth_count()); |
| 5309 | } |
| 5310 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 5311 | } // namespace |
| 5312 | |
| 5313 | } // namespace net |