[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 */, |
| 540 | nullptr /* proxy_delegate */, |
Matt Menke | d732ea4 | 2019-03-08 12:05:00 | [diff] [blame] | 541 | nullptr /* http_user_agent_settings */, |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 542 | SSLClientSocketContext(), |
| 543 | SSLClientSocketContext(), |
| 544 | nullptr /* socket_performance_watcher_factory */, |
| 545 | nullptr /* network_quality_estimator */, |
| 546 | net_log, |
| 547 | nullptr /* websocket_endpoint_lock_manager */), |
| 548 | job_type_(TestConnectJob::kMockJob), |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 549 | job_types_(nullptr), |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 550 | client_socket_factory_(client_socket_factory) {} |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 551 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 552 | ~TestConnectJobFactory() override = default; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 553 | |
| 554 | void set_job_type(TestConnectJob::JobType job_type) { job_type_ = job_type; } |
| 555 | |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 556 | void set_job_types(std::list<TestConnectJob::JobType>* job_types) { |
| 557 | job_types_ = job_types; |
| 558 | CHECK(!job_types_->empty()); |
| 559 | } |
| 560 | |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 561 | void set_timeout_duration(base::TimeDelta timeout_duration) { |
| 562 | timeout_duration_ = timeout_duration; |
| 563 | } |
| 564 | |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 565 | // ConnectJobFactory implementation. |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 566 | |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 567 | std::unique_ptr<ConnectJob> NewConnectJob( |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 568 | const TestClientSocketPoolBase::Request& request, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 569 | ConnectJob::Delegate* delegate) const override { |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 570 | EXPECT_TRUE(!job_types_ || !job_types_->empty()); |
| 571 | TestConnectJob::JobType job_type = job_type_; |
| 572 | if (job_types_ && !job_types_->empty()) { |
| 573 | job_type = job_types_->front(); |
| 574 | job_types_->pop_front(); |
| 575 | } |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 576 | return std::make_unique<TestConnectJob>( |
| 577 | job_type, request, timeout_duration_, &common_connect_job_params_, |
| 578 | delegate, client_socket_factory_); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | private: |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 582 | const CommonConnectJobParams common_connect_job_params_; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 583 | TestConnectJob::JobType job_type_; |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 584 | std::list<TestConnectJob::JobType>* job_types_; |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 585 | base::TimeDelta timeout_duration_; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 586 | MockClientSocketFactory* const client_socket_factory_; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 587 | |
| 588 | DISALLOW_COPY_AND_ASSIGN(TestConnectJobFactory); |
| 589 | }; |
| 590 | |
| 591 | class TestClientSocketPool : public ClientSocketPool { |
| 592 | public: |
[email protected] | 12322e7e | 2013-08-15 17:49:26 | [diff] [blame] | 593 | typedef TestSocketParams SocketParams; |
| 594 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 595 | TestClientSocketPool( |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 596 | int max_sockets, |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 597 | int max_sockets_per_group, |
[email protected] | 9bf28db | 2009-08-29 01:35:16 | [diff] [blame] | 598 | base::TimeDelta unused_idle_socket_timeout, |
| 599 | base::TimeDelta used_idle_socket_timeout, |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 600 | TestClientSocketPoolBase::ConnectJobFactory* connect_job_factory) |
Matt Menke | 83367864 | 2019-03-05 22:05:51 | [diff] [blame] | 601 | : base_(max_sockets, |
rkaplow | d90695c | 2015-03-25 22:12:41 | [diff] [blame] | 602 | max_sockets_per_group, |
| 603 | unused_idle_socket_timeout, |
| 604 | used_idle_socket_timeout, |
[email protected] | 66761b95 | 2010-06-25 21:30:38 | [diff] [blame] | 605 | connect_job_factory) {} |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 606 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 607 | ~TestClientSocketPool() override = default; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 608 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 609 | int RequestSocket(const std::string& group_name, |
| 610 | const void* params, |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 611 | RequestPriority priority, |
Paul Jensen | 8d6f87ec | 2018-01-13 00:46:54 | [diff] [blame] | 612 | const SocketTag& socket_tag, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 613 | RespectLimits respect_limits, |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 614 | ClientSocketHandle* handle, |
Bence Béky | 5a8662b | 2018-07-03 13:04:03 | [diff] [blame] | 615 | CompletionOnceCallback callback, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 616 | const ProxyAuthCallback& proxy_auth_callback, |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 617 | const NetLogWithSource& net_log) override { |
[email protected] | df4b4ef | 2010-07-12 18:25:21 | [diff] [blame] | 618 | const scoped_refptr<TestSocketParams>* casted_socket_params = |
| 619 | static_cast<const scoped_refptr<TestSocketParams>*>(params); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 620 | return base_.RequestSocket( |
| 621 | group_name, *casted_socket_params, priority, socket_tag, respect_limits, |
| 622 | handle, std::move(callback), proxy_auth_callback, net_log); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 623 | } |
| 624 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 625 | void RequestSockets(const std::string& group_name, |
| 626 | const void* params, |
| 627 | int num_sockets, |
Charlie Harrison | 55ce608 | 2018-05-14 02:25:57 | [diff] [blame] | 628 | const NetLogWithSource& net_log) override { |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 629 | const scoped_refptr<TestSocketParams>* casted_params = |
| 630 | static_cast<const scoped_refptr<TestSocketParams>*>(params); |
| 631 | |
Charlie Harrison | 55ce608 | 2018-05-14 02:25:57 | [diff] [blame] | 632 | base_.RequestSockets(group_name, *casted_params, num_sockets, net_log); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 633 | } |
| 634 | |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 635 | void SetPriority(const std::string& group_name, |
| 636 | ClientSocketHandle* handle, |
| 637 | RequestPriority priority) override { |
| 638 | base_.SetPriority(group_name, handle, priority); |
| 639 | } |
| 640 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 641 | void CancelRequest(const std::string& group_name, |
| 642 | ClientSocketHandle* handle) override { |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 643 | base_.CancelRequest(group_name, handle); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 644 | } |
| 645 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 646 | void ReleaseSocket(const std::string& group_name, |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 647 | std::unique_ptr<StreamSocket> socket, |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 648 | int id) override { |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 649 | base_.ReleaseSocket(group_name, std::move(socket), id); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 650 | } |
| 651 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 652 | void FlushWithError(int error) override { base_.FlushWithError(error); } |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 653 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 654 | bool IsStalled() const override { return base_.IsStalled(); } |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 655 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 656 | void CloseIdleSockets() override { base_.CloseIdleSockets(); } |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 657 | |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 658 | void CloseIdleSocketsInGroup(const std::string& group_name) override { |
| 659 | base_.CloseIdleSocketsInGroup(group_name); |
| 660 | } |
| 661 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 662 | int IdleSocketCount() const override { return base_.idle_socket_count(); } |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 663 | |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 664 | size_t IdleSocketCountInGroup(const std::string& group_name) const override { |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 665 | return base_.IdleSocketCountInGroup(group_name); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 666 | } |
| 667 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 668 | LoadState GetLoadState(const std::string& group_name, |
| 669 | const ClientSocketHandle* handle) const override { |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 670 | return base_.GetLoadState(group_name, handle); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 671 | } |
| 672 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 673 | void AddHigherLayeredPool(HigherLayeredPool* higher_pool) override { |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 674 | base_.AddHigherLayeredPool(higher_pool); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 675 | } |
| 676 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 677 | void RemoveHigherLayeredPool(HigherLayeredPool* higher_pool) override { |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 678 | base_.RemoveHigherLayeredPool(higher_pool); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 679 | } |
| 680 | |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 681 | std::unique_ptr<base::DictionaryValue> GetInfoAsValue( |
[email protected] | d4dfdab | 2011-12-07 16:56:59 | [diff] [blame] | 682 | const std::string& name, |
Matt Menke | 83367864 | 2019-03-05 22:05:51 | [diff] [blame] | 683 | const std::string& type) const override { |
[email protected] | 59d7a5a | 2010-08-30 16:44:27 | [diff] [blame] | 684 | return base_.GetInfoAsValue(name, type); |
| 685 | } |
| 686 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 687 | const TestClientSocketPoolBase* base() const { return &base_; } |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 688 | |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 689 | size_t NumNeverAssignedConnectJobsInGroup( |
| 690 | const std::string& group_name) const { |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 691 | return base_.NumNeverAssignedConnectJobsInGroup(group_name); |
| 692 | } |
| 693 | |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 694 | size_t NumUnassignedConnectJobsInGroup(const std::string& group_name) const { |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 695 | return base_.NumUnassignedConnectJobsInGroup(group_name); |
| 696 | } |
| 697 | |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 698 | size_t NumConnectJobsInGroup(const std::string& group_name) const { |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 699 | return base_.NumConnectJobsInGroup(group_name); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 700 | } |
| 701 | |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 702 | int NumActiveSocketsInGroup(const std::string& group_name) const { |
| 703 | return base_.NumActiveSocketsInGroup(group_name); |
| 704 | } |
| 705 | |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 706 | bool RequestInGroupWithHandleHasJobForTesting( |
| 707 | const std::string& group_name, |
| 708 | const ClientSocketHandle* handle) const { |
| 709 | return base_.RequestInGroupWithHandleHasJobForTesting(group_name, handle); |
| 710 | } |
| 711 | |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 712 | bool HasGroup(const std::string& group_name) const { |
| 713 | return base_.HasGroup(group_name); |
| 714 | } |
| 715 | |
[email protected] | 9bf28db | 2009-08-29 01:35:16 | [diff] [blame] | 716 | void CleanupTimedOutIdleSockets() { base_.CleanupIdleSockets(false); } |
| 717 | |
[email protected] | 06d9404 | 2010-08-25 01:45:22 | [diff] [blame] | 718 | void EnableConnectBackupJobs() { base_.EnableConnectBackupJobs(); } |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 719 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 720 | private: |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 721 | TestClientSocketPoolBase base_; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 722 | |
| 723 | DISALLOW_COPY_AND_ASSIGN(TestClientSocketPool); |
| 724 | }; |
| 725 | |
[email protected] | a937a06d | 2009-08-19 21:19:24 | [diff] [blame] | 726 | } // namespace |
| 727 | |
[email protected] | a937a06d | 2009-08-19 21:19:24 | [diff] [blame] | 728 | namespace { |
| 729 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 730 | void MockClientSocketFactory::SignalJobs() { |
jdoerrie | 22a91d8b9 | 2018-10-05 08:43:26 | [diff] [blame] | 731 | for (auto it = waiting_jobs_.begin(); it != waiting_jobs_.end(); ++it) { |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 732 | (*it)->Signal(); |
| 733 | } |
| 734 | waiting_jobs_.clear(); |
| 735 | } |
| 736 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 737 | void MockClientSocketFactory::SignalJob(size_t job) { |
| 738 | ASSERT_LT(job, waiting_jobs_.size()); |
| 739 | waiting_jobs_[job]->Signal(); |
| 740 | waiting_jobs_.erase(waiting_jobs_.begin() + job); |
| 741 | } |
| 742 | |
| 743 | void MockClientSocketFactory::SetJobLoadState(size_t job, |
| 744 | LoadState load_state) { |
| 745 | ASSERT_LT(job, waiting_jobs_.size()); |
| 746 | waiting_jobs_[job]->set_load_state(load_state); |
| 747 | } |
| 748 | |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 749 | void MockClientSocketFactory::SetJobHasEstablishedConnection(size_t job) { |
| 750 | ASSERT_LT(job, waiting_jobs_.size()); |
| 751 | waiting_jobs_[job]->set_has_established_connection(); |
| 752 | } |
| 753 | |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 754 | class ClientSocketPoolBaseTest : public TestWithScopedTaskEnvironment { |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 755 | protected: |
Alex Clarke | 0def209 | 2018-12-10 12:01:45 | [diff] [blame] | 756 | ClientSocketPoolBaseTest() |
| 757 | : TestWithScopedTaskEnvironment( |
| 758 | base::test::ScopedTaskEnvironment::MainThreadType::MOCK_TIME), |
| 759 | params_(new TestSocketParams()) { |
[email protected] | 636b825 | 2011-04-08 19:56:54 | [diff] [blame] | 760 | connect_backup_jobs_enabled_ = |
| 761 | internal::ClientSocketPoolBaseHelper::connect_backup_jobs_enabled(); |
| 762 | internal::ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(true); |
| 763 | } |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 764 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 765 | ~ClientSocketPoolBaseTest() override { |
[email protected] | 636b825 | 2011-04-08 19:56:54 | [diff] [blame] | 766 | internal::ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled( |
| 767 | connect_backup_jobs_enabled_); |
| 768 | } |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 769 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 770 | void CreatePool(int max_sockets, int max_sockets_per_group) { |
Tarun Bansal | a763509 | 2019-02-20 10:00:59 | [diff] [blame] | 771 | CreatePoolWithIdleTimeouts(max_sockets, max_sockets_per_group, |
| 772 | kUnusedIdleSocketTimeout, |
| 773 | ClientSocketPool::used_idle_socket_timeout()); |
[email protected] | 9bf28db | 2009-08-29 01:35:16 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | void CreatePoolWithIdleTimeouts( |
| 777 | int max_sockets, int max_sockets_per_group, |
| 778 | base::TimeDelta unused_idle_socket_timeout, |
| 779 | base::TimeDelta used_idle_socket_timeout) { |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 780 | DCHECK(!pool_.get()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 781 | connect_job_factory_ = new TestConnectJobFactory(&client_socket_factory_, |
| 782 | &net_log_); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 783 | pool_.reset(new TestClientSocketPool(max_sockets, |
| 784 | max_sockets_per_group, |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 785 | unused_idle_socket_timeout, |
| 786 | used_idle_socket_timeout, |
| 787 | connect_job_factory_)); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 788 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 789 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 790 | int StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 791 | const ClientSocketPool::GroupId& group_id, |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 792 | RequestPriority priority, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 793 | ClientSocketPool::RespectLimits respect_limits) { |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 794 | return test_base_.StartRequestUsingPool(pool_.get(), group_id, priority, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 795 | respect_limits, params_); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 796 | } |
| 797 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 798 | int StartRequest(const ClientSocketPool::GroupId& group_id, |
| 799 | RequestPriority priority) { |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 800 | return StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 801 | group_id, priority, ClientSocketPool::RespectLimits::ENABLED); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 802 | } |
| 803 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 804 | int GetOrderOfRequest(size_t index) const { |
| 805 | return test_base_.GetOrderOfRequest(index); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 806 | } |
| 807 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 808 | bool ReleaseOneConnection(ClientSocketPoolTest::KeepAlive keep_alive) { |
| 809 | return test_base_.ReleaseOneConnection(keep_alive); |
| 810 | } |
| 811 | |
| 812 | void ReleaseAllConnections(ClientSocketPoolTest::KeepAlive keep_alive) { |
| 813 | test_base_.ReleaseAllConnections(keep_alive); |
| 814 | } |
| 815 | |
| 816 | TestSocketRequest* request(int i) { return test_base_.request(i); } |
| 817 | size_t requests_size() const { return test_base_.requests_size(); } |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 818 | std::vector<std::unique_ptr<TestSocketRequest>>* requests() { |
olli.raula | 9d66b7d | 2015-11-23 08:30:42 | [diff] [blame] | 819 | return test_base_.requests(); |
| 820 | } |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 821 | // Only counts the requests that get sockets asynchronously; |
| 822 | // synchronous completions are not registered by this count. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 823 | size_t completion_count() const { return test_base_.completion_count(); } |
| 824 | |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 825 | TestNetLog net_log_; |
[email protected] | 636b825 | 2011-04-08 19:56:54 | [diff] [blame] | 826 | bool connect_backup_jobs_enabled_; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 827 | MockClientSocketFactory client_socket_factory_; |
[email protected] | 17a0c6c | 2009-08-04 00:07:04 | [diff] [blame] | 828 | TestConnectJobFactory* connect_job_factory_; |
[email protected] | df4b4ef | 2010-07-12 18:25:21 | [diff] [blame] | 829 | scoped_refptr<TestSocketParams> params_; |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 830 | std::unique_ptr<TestClientSocketPool> pool_; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 831 | ClientSocketPoolTest test_base_; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 832 | }; |
| 833 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 834 | TEST_F(ClientSocketPoolBaseTest, BasicSynchronous) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 835 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 836 | |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 837 | TestCompletionCallback callback; |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 838 | ClientSocketHandle handle; |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 839 | BoundTestNetLog log; |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 840 | TestLoadTimingInfoNotConnected(handle); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 841 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 842 | EXPECT_EQ( |
| 843 | OK, handle.Init( |
| 844 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
| 845 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 846 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound())); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 847 | EXPECT_TRUE(handle.is_initialized()); |
| 848 | EXPECT_TRUE(handle.socket()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 849 | TestLoadTimingInfoConnectedNotReused(handle); |
| 850 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 851 | handle.Reset(); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 852 | TestLoadTimingInfoNotConnected(handle); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 853 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 854 | TestNetLogEntry::List entries; |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 855 | log.GetEntries(&entries); |
| 856 | |
| 857 | EXPECT_EQ(4u, entries.size()); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 858 | EXPECT_TRUE(LogContainsBeginEvent(entries, 0, NetLogEventType::SOCKET_POOL)); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 859 | EXPECT_TRUE(LogContainsEvent( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 860 | entries, 1, NetLogEventType::SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
| 861 | NetLogEventPhase::NONE)); |
| 862 | EXPECT_TRUE(LogContainsEvent(entries, 2, |
| 863 | NetLogEventType::SOCKET_POOL_BOUND_TO_SOCKET, |
| 864 | NetLogEventPhase::NONE)); |
| 865 | EXPECT_TRUE(LogContainsEndEvent(entries, 3, NetLogEventType::SOCKET_POOL)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 866 | } |
| 867 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 868 | TEST_F(ClientSocketPoolBaseTest, InitConnectionFailure) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 869 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 870 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 871 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 872 | BoundTestNetLog log; |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 873 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 874 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 875 | TestCompletionCallback callback; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 876 | // Set the additional error state members to ensure that they get cleared. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 877 | handle.set_is_ssl_error(true); |
[email protected] | 8b49869 | 2010-07-16 17:11:43 | [diff] [blame] | 878 | HttpResponseInfo info; |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 879 | info.headers = new HttpResponseHeaders(std::string()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 880 | handle.set_ssl_error_response_info(info); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 881 | EXPECT_EQ( |
| 882 | ERR_CONNECTION_FAILED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 883 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 884 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 885 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 886 | log.bound())); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 887 | EXPECT_FALSE(handle.socket()); |
| 888 | EXPECT_FALSE(handle.is_ssl_error()); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 889 | EXPECT_TRUE(handle.ssl_error_response_info().headers.get() == nullptr); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 890 | TestLoadTimingInfoNotConnected(handle); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 891 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 892 | TestNetLogEntry::List entries; |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 893 | log.GetEntries(&entries); |
| 894 | |
| 895 | EXPECT_EQ(3u, entries.size()); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 896 | EXPECT_TRUE(LogContainsBeginEvent(entries, 0, NetLogEventType::SOCKET_POOL)); |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 897 | EXPECT_TRUE(LogContainsEvent( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 898 | entries, 1, NetLogEventType::SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
| 899 | NetLogEventPhase::NONE)); |
| 900 | EXPECT_TRUE(LogContainsEndEvent(entries, 2, NetLogEventType::SOCKET_POOL)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 901 | } |
| 902 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 903 | TEST_F(ClientSocketPoolBaseTest, TotalLimit) { |
| 904 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 905 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 906 | // TODO(eroman): Check that the NetLog contains this event. |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 907 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 908 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 909 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
| 910 | EXPECT_THAT(StartRequest(TestGroupId("c"), DEFAULT_PRIORITY), IsOk()); |
| 911 | EXPECT_THAT(StartRequest(TestGroupId("d"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 912 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 913 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 914 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 915 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 916 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 917 | EXPECT_THAT(StartRequest(TestGroupId("e"), DEFAULT_PRIORITY), |
| 918 | IsError(ERR_IO_PENDING)); |
| 919 | EXPECT_THAT(StartRequest(TestGroupId("f"), DEFAULT_PRIORITY), |
| 920 | IsError(ERR_IO_PENDING)); |
| 921 | EXPECT_THAT(StartRequest(TestGroupId("g"), DEFAULT_PRIORITY), |
| 922 | IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 923 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 924 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 925 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 926 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 927 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 928 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 929 | |
| 930 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 931 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 932 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 933 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 934 | EXPECT_EQ(5, GetOrderOfRequest(5)); |
| 935 | EXPECT_EQ(6, GetOrderOfRequest(6)); |
| 936 | EXPECT_EQ(7, GetOrderOfRequest(7)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 937 | |
| 938 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 939 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(8)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 940 | } |
| 941 | |
| 942 | TEST_F(ClientSocketPoolBaseTest, TotalLimitReachedNewGroup) { |
| 943 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 944 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 945 | // TODO(eroman): Check that the NetLog contains this event. |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 946 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 947 | // Reach all limits: max total sockets, and max sockets per group. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 948 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 949 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 950 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
| 951 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 952 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 953 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 954 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 955 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 956 | |
| 957 | // Now create a new group and verify that we don't starve it. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 958 | EXPECT_THAT(StartRequest(TestGroupId("c"), DEFAULT_PRIORITY), |
| 959 | IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 960 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 961 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 962 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 963 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 964 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 965 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 966 | |
| 967 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 968 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 969 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 970 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 971 | EXPECT_EQ(5, GetOrderOfRequest(5)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 972 | |
| 973 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 974 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(6)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 975 | } |
| 976 | |
| 977 | TEST_F(ClientSocketPoolBaseTest, TotalLimitRespectsPriority) { |
| 978 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 979 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 980 | EXPECT_THAT(StartRequest(TestGroupId("b"), LOWEST), IsOk()); |
| 981 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsOk()); |
| 982 | EXPECT_THAT(StartRequest(TestGroupId("b"), HIGHEST), IsOk()); |
| 983 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 984 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 985 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 986 | client_socket_factory_.allocation_count()); |
| 987 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 988 | EXPECT_THAT(StartRequest(TestGroupId("c"), LOWEST), IsError(ERR_IO_PENDING)); |
| 989 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 990 | EXPECT_THAT(StartRequest(TestGroupId("b"), HIGHEST), IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 991 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 992 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 993 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 994 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 995 | |
| 996 | // First 4 requests don't have to wait, and finish in order. |
| 997 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 998 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 999 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 1000 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 1001 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1002 | // Request ("b", HIGHEST) has the highest priority, then (TestGroupId("a"), |
| 1003 | // MEDIUM), and then ("c", LOWEST). |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1004 | EXPECT_EQ(7, GetOrderOfRequest(5)); |
| 1005 | EXPECT_EQ(6, GetOrderOfRequest(6)); |
| 1006 | EXPECT_EQ(5, GetOrderOfRequest(7)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1007 | |
| 1008 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1009 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(9)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1010 | } |
| 1011 | |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1012 | // Test reprioritizing a request before completion doesn't interfere with |
| 1013 | // its completion. |
| 1014 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeOne) { |
| 1015 | CreatePool(kDefaultMaxSockets, 1); |
| 1016 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1017 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1018 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1019 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1020 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1021 | |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 1022 | request(1)->handle()->SetPriority(HIGHEST); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1023 | |
| 1024 | ReleaseOneConnection(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1025 | |
| 1026 | EXPECT_TRUE(request(1)->handle()->socket()); |
| 1027 | } |
| 1028 | |
| 1029 | // Reprioritize a request up past another one and make sure that changes the |
| 1030 | // completion order. |
| 1031 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeUpReorder) { |
| 1032 | CreatePool(kDefaultMaxSockets, 1); |
| 1033 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1034 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1035 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1036 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1037 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1038 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1039 | EXPECT_FALSE(request(2)->handle()->socket()); |
| 1040 | |
| 1041 | request(2)->handle()->SetPriority(HIGHEST); |
| 1042 | |
| 1043 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1044 | |
| 1045 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1046 | EXPECT_EQ(3, GetOrderOfRequest(2)); |
| 1047 | EXPECT_EQ(2, GetOrderOfRequest(3)); |
| 1048 | } |
| 1049 | |
| 1050 | // Reprioritize a request without changing relative priorities and check |
| 1051 | // that the order doesn't change. |
| 1052 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeUpNoReorder) { |
| 1053 | CreatePool(kDefaultMaxSockets, 1); |
| 1054 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1055 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1056 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1057 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1058 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1059 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1060 | EXPECT_FALSE(request(2)->handle()->socket()); |
| 1061 | |
| 1062 | request(2)->handle()->SetPriority(MEDIUM); |
| 1063 | |
| 1064 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1065 | |
| 1066 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1067 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1068 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 1069 | } |
| 1070 | |
| 1071 | // Reprioritize a request past down another one and make sure that changes the |
| 1072 | // completion order. |
| 1073 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeDownReorder) { |
| 1074 | CreatePool(kDefaultMaxSockets, 1); |
| 1075 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1076 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1077 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1078 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1079 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1080 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1081 | EXPECT_FALSE(request(2)->handle()->socket()); |
| 1082 | |
| 1083 | request(1)->handle()->SetPriority(LOW); |
| 1084 | |
| 1085 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1086 | |
| 1087 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1088 | EXPECT_EQ(3, GetOrderOfRequest(2)); |
| 1089 | EXPECT_EQ(2, GetOrderOfRequest(3)); |
| 1090 | } |
| 1091 | |
| 1092 | // Reprioritize a request to the same level as another and confirm it is |
| 1093 | // put after the old request. |
| 1094 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeResetFIFO) { |
| 1095 | CreatePool(kDefaultMaxSockets, 1); |
| 1096 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1097 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1098 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1099 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1100 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1101 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1102 | EXPECT_FALSE(request(2)->handle()->socket()); |
| 1103 | |
| 1104 | request(1)->handle()->SetPriority(MEDIUM); |
| 1105 | |
| 1106 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1107 | |
| 1108 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1109 | EXPECT_EQ(3, GetOrderOfRequest(2)); |
| 1110 | EXPECT_EQ(2, GetOrderOfRequest(3)); |
| 1111 | } |
| 1112 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1113 | TEST_F(ClientSocketPoolBaseTest, TotalLimitRespectsGroupLimit) { |
| 1114 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1115 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1116 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsOk()); |
| 1117 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsOk()); |
| 1118 | EXPECT_THAT(StartRequest(TestGroupId("b"), HIGHEST), IsOk()); |
| 1119 | EXPECT_THAT(StartRequest(TestGroupId("b"), MEDIUM), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1120 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1121 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1122 | client_socket_factory_.allocation_count()); |
| 1123 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1124 | EXPECT_THAT(StartRequest(TestGroupId("c"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1125 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 1126 | EXPECT_THAT(StartRequest(TestGroupId("b"), HIGHEST), IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1127 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1128 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1129 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1130 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1131 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1132 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1133 | |
| 1134 | // First 4 requests don't have to wait, and finish in order. |
| 1135 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1136 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1137 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 1138 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 1139 | |
| 1140 | // Request ("b", 7) has the highest priority, but we can't make new socket for |
| 1141 | // group "b", because it has reached the per-group limit. Then we make |
| 1142 | // socket for ("c", 6), because it has higher priority than ("a", 4), |
| 1143 | // and we still can't make a socket for group "b". |
| 1144 | EXPECT_EQ(5, GetOrderOfRequest(5)); |
| 1145 | EXPECT_EQ(6, GetOrderOfRequest(6)); |
| 1146 | EXPECT_EQ(7, GetOrderOfRequest(7)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1147 | |
| 1148 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1149 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(8)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1150 | } |
| 1151 | |
| 1152 | // Make sure that we count connecting sockets against the total limit. |
| 1153 | TEST_F(ClientSocketPoolBaseTest, TotalLimitCountsConnectingSockets) { |
| 1154 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1155 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1156 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1157 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
| 1158 | EXPECT_THAT(StartRequest(TestGroupId("c"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1159 | |
| 1160 | // Create one asynchronous request. |
| 1161 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1162 | EXPECT_THAT(StartRequest(TestGroupId("d"), DEFAULT_PRIORITY), |
| 1163 | IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1164 | |
[email protected] | 6b17538 | 2009-10-13 06:47:47 | [diff] [blame] | 1165 | // We post all of our delayed tasks with a 2ms delay. I.e. they don't |
| 1166 | // actually become pending until 2ms after they have been created. In order |
| 1167 | // to flush all tasks, we need to wait so that we know there are no |
| 1168 | // soon-to-be-pending tasks waiting. |
Alex Clarke | 0def209 | 2018-12-10 12:01:45 | [diff] [blame] | 1169 | FastForwardBy(base::TimeDelta::FromMilliseconds(10)); |
[email protected] | 6b17538 | 2009-10-13 06:47:47 | [diff] [blame] | 1170 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1171 | // The next synchronous request should wait for its turn. |
| 1172 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1173 | EXPECT_THAT(StartRequest(TestGroupId("e"), DEFAULT_PRIORITY), |
| 1174 | IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1175 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1176 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1177 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1178 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1179 | client_socket_factory_.allocation_count()); |
| 1180 | |
| 1181 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1182 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1183 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 1184 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1185 | EXPECT_EQ(5, GetOrderOfRequest(5)); |
| 1186 | |
| 1187 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1188 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(6)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1189 | } |
| 1190 | |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1191 | TEST_F(ClientSocketPoolBaseTest, CorrectlyCountStalledGroups) { |
| 1192 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 1193 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 1194 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1195 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1196 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1197 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1198 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1199 | |
| 1200 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 1201 | |
| 1202 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1203 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1204 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), |
| 1205 | IsError(ERR_IO_PENDING)); |
| 1206 | EXPECT_THAT(StartRequest(TestGroupId("c"), DEFAULT_PRIORITY), |
| 1207 | IsError(ERR_IO_PENDING)); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1208 | |
| 1209 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1210 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1211 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1212 | EXPECT_EQ(kDefaultMaxSockets + 1, client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1213 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1214 | EXPECT_EQ(kDefaultMaxSockets + 2, client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1215 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
| 1216 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1217 | EXPECT_EQ(kDefaultMaxSockets + 2, client_socket_factory_.allocation_count()); |
| 1218 | } |
| 1219 | |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 1220 | TEST_F(ClientSocketPoolBaseTest, StallAndThenCancelAndTriggerAvailableSocket) { |
| 1221 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 1222 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1223 | |
| 1224 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1225 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1226 | EXPECT_EQ( |
| 1227 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1228 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1229 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1230 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 1231 | NetLogWithSource())); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 1232 | |
| 1233 | ClientSocketHandle handles[4]; |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 1234 | for (size_t i = 0; i < base::size(handles); ++i) { |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1235 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1236 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1237 | handles[i].Init( |
| 1238 | TestGroupId("b"), params_, DEFAULT_PRIORITY, SocketTag(), |
| 1239 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1240 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 1241 | NetLogWithSource())); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 1242 | } |
| 1243 | |
| 1244 | // One will be stalled, cancel all the handles now. |
| 1245 | // This should hit the OnAvailableSocketSlot() code where we previously had |
| 1246 | // stalled groups, but no longer have any. |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 1247 | for (size_t i = 0; i < base::size(handles); ++i) |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 1248 | handles[i].Reset(); |
| 1249 | } |
| 1250 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1251 | TEST_F(ClientSocketPoolBaseTest, CancelStalledSocketAtSocketLimit) { |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1252 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1253 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 1254 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1255 | { |
| 1256 | ClientSocketHandle handles[kDefaultMaxSockets]; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1257 | TestCompletionCallback callbacks[kDefaultMaxSockets]; |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1258 | for (int i = 0; i < kDefaultMaxSockets; ++i) { |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1259 | EXPECT_EQ(OK, handles[i].Init(TestGroupId(base::NumberToString(i)), |
| 1260 | params_, DEFAULT_PRIORITY, SocketTag(), |
Paul Jensen | 8d6f87ec | 2018-01-13 00:46:54 | [diff] [blame] | 1261 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1262 | callbacks[i].callback(), |
| 1263 | ClientSocketPool::ProxyAuthCallback(), |
| 1264 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1265 | } |
| 1266 | |
| 1267 | // Force a stalled group. |
| 1268 | ClientSocketHandle stalled_handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1269 | TestCompletionCallback callback; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1270 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1271 | stalled_handle.Init( |
| 1272 | TestGroupId("foo"), params_, DEFAULT_PRIORITY, SocketTag(), |
| 1273 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1274 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 1275 | NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1276 | |
| 1277 | // Cancel the stalled request. |
| 1278 | stalled_handle.Reset(); |
| 1279 | |
| 1280 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1281 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 1282 | |
| 1283 | // Dropping out of scope will close all handles and return them to idle. |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1284 | } |
| 1285 | |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1286 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1287 | EXPECT_EQ(kDefaultMaxSockets, pool_->IdleSocketCount()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1288 | } |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1289 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1290 | TEST_F(ClientSocketPoolBaseTest, CancelPendingSocketAtSocketLimit) { |
| 1291 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1292 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 1293 | |
| 1294 | { |
| 1295 | ClientSocketHandle handles[kDefaultMaxSockets]; |
| 1296 | for (int i = 0; i < kDefaultMaxSockets; ++i) { |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1297 | TestCompletionCallback callback; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1298 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1299 | handles[i].Init(TestGroupId(base::NumberToString(i)), params_, |
| 1300 | DEFAULT_PRIORITY, SocketTag(), |
| 1301 | ClientSocketPool::RespectLimits::ENABLED, |
| 1302 | callback.callback(), |
| 1303 | ClientSocketPool::ProxyAuthCallback(), |
| 1304 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1305 | } |
| 1306 | |
| 1307 | // Force a stalled group. |
| 1308 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1309 | ClientSocketHandle stalled_handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1310 | TestCompletionCallback callback; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1311 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1312 | stalled_handle.Init( |
| 1313 | TestGroupId("foo"), params_, DEFAULT_PRIORITY, SocketTag(), |
| 1314 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1315 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 1316 | NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1317 | |
| 1318 | // Since it is stalled, it should have no connect jobs. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1319 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroup(TestGroupId("foo"))); |
| 1320 | EXPECT_EQ(0u, |
| 1321 | pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("foo"))); |
| 1322 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("foo"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1323 | |
| 1324 | // Cancel the stalled request. |
| 1325 | handles[0].Reset(); |
| 1326 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1327 | // Now we should have a connect job. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1328 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("foo"))); |
| 1329 | EXPECT_EQ(0u, |
| 1330 | pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("foo"))); |
| 1331 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("foo"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1332 | |
| 1333 | // The stalled socket should connect. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1334 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1335 | |
| 1336 | EXPECT_EQ(kDefaultMaxSockets + 1, |
| 1337 | client_socket_factory_.allocation_count()); |
| 1338 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1339 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroup(TestGroupId("foo"))); |
| 1340 | EXPECT_EQ(0u, |
| 1341 | pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("foo"))); |
| 1342 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("foo"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1343 | |
| 1344 | // Dropping out of scope will close all handles and return them to idle. |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1345 | } |
| 1346 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1347 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 1348 | } |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1349 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1350 | TEST_F(ClientSocketPoolBaseTest, WaitForStalledSocketAtSocketLimit) { |
| 1351 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1352 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1353 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1354 | ClientSocketHandle stalled_handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1355 | TestCompletionCallback callback; |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1356 | { |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 1357 | EXPECT_FALSE(pool_->IsStalled()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1358 | ClientSocketHandle handles[kDefaultMaxSockets]; |
| 1359 | for (int i = 0; i < kDefaultMaxSockets; ++i) { |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1360 | TestCompletionCallback callback; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1361 | EXPECT_EQ( |
| 1362 | OK, handles[i].Init(TestGroupId(base::StringPrintf("Take 2: %d", i)), |
| 1363 | params_, DEFAULT_PRIORITY, SocketTag(), |
| 1364 | ClientSocketPool::RespectLimits::ENABLED, |
| 1365 | callback.callback(), |
| 1366 | ClientSocketPool::ProxyAuthCallback(), |
| 1367 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1368 | } |
| 1369 | |
| 1370 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1371 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 1372 | EXPECT_FALSE(pool_->IsStalled()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1373 | |
| 1374 | // Now we will hit the socket limit. |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1375 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1376 | stalled_handle.Init( |
| 1377 | TestGroupId("foo"), params_, DEFAULT_PRIORITY, SocketTag(), |
| 1378 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1379 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 1380 | NetLogWithSource())); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 1381 | EXPECT_TRUE(pool_->IsStalled()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1382 | |
| 1383 | // Dropping out of scope will close all handles and return them to idle. |
| 1384 | } |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1385 | |
| 1386 | // But if we wait for it, the released idle sockets will be closed in |
| 1387 | // preference of the waiting request. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1388 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1389 | |
| 1390 | EXPECT_EQ(kDefaultMaxSockets + 1, client_socket_factory_.allocation_count()); |
| 1391 | EXPECT_EQ(3, pool_->IdleSocketCount()); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1392 | } |
| 1393 | |
| 1394 | // Regression test for http://crbug.com/40952. |
| 1395 | TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketAtSocketLimitDeleteGroup) { |
| 1396 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 06d9404 | 2010-08-25 01:45:22 | [diff] [blame] | 1397 | pool_->EnableConnectBackupJobs(); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1398 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 1399 | |
| 1400 | for (int i = 0; i < kDefaultMaxSockets; ++i) { |
| 1401 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1402 | TestCompletionCallback callback; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1403 | EXPECT_EQ(OK, handle.Init(TestGroupId(base::NumberToString(i)), params_, |
| 1404 | DEFAULT_PRIORITY, SocketTag(), |
| 1405 | ClientSocketPool::RespectLimits::ENABLED, |
| 1406 | callback.callback(), |
| 1407 | ClientSocketPool::ProxyAuthCallback(), |
| 1408 | pool_.get(), NetLogWithSource())); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1409 | } |
| 1410 | |
| 1411 | // Flush all the DoReleaseSocket tasks. |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 1412 | base::RunLoop().RunUntilIdle(); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1413 | |
| 1414 | // Stall a group. Set a pending job so it'll trigger a backup job if we don't |
| 1415 | // reuse a socket. |
| 1416 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1417 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1418 | TestCompletionCallback callback; |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1419 | |
| 1420 | // "0" is special here, since it should be the first entry in the sorted map, |
| 1421 | // which is the one which we would close an idle socket for. We shouldn't |
| 1422 | // close an idle socket though, since we should reuse the idle socket. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1423 | EXPECT_EQ(OK, handle.Init( |
| 1424 | TestGroupId("0"), params_, DEFAULT_PRIORITY, SocketTag(), |
| 1425 | ClientSocketPool::RespectLimits::ENABLED, |
| 1426 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1427 | pool_.get(), NetLogWithSource())); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1428 | |
| 1429 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1430 | EXPECT_EQ(kDefaultMaxSockets - 1, pool_->IdleSocketCount()); |
| 1431 | } |
| 1432 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1433 | TEST_F(ClientSocketPoolBaseTest, PendingRequests) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1434 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1435 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1436 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1437 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1438 | EXPECT_THAT(StartRequest(TestGroupId("a"), IDLE), IsError(ERR_IO_PENDING)); |
| 1439 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
| 1440 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1441 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1442 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 1443 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1444 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1445 | ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1446 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
| 1447 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1448 | EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup, |
| 1449 | completion_count()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1450 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1451 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1452 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
[email protected] | c9c6f5c | 2010-07-31 01:30:03 | [diff] [blame] | 1453 | EXPECT_EQ(8, GetOrderOfRequest(3)); |
| 1454 | EXPECT_EQ(6, GetOrderOfRequest(4)); |
| 1455 | EXPECT_EQ(4, GetOrderOfRequest(5)); |
| 1456 | EXPECT_EQ(3, GetOrderOfRequest(6)); |
| 1457 | EXPECT_EQ(5, GetOrderOfRequest(7)); |
| 1458 | EXPECT_EQ(7, GetOrderOfRequest(8)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1459 | |
| 1460 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1461 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(9)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1462 | } |
| 1463 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1464 | TEST_F(ClientSocketPoolBaseTest, PendingRequests_NoKeepAlive) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1465 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1466 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1467 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1468 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1469 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
| 1470 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1471 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1472 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 1473 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1474 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1475 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1476 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1477 | for (size_t i = kDefaultMaxSocketsPerGroup; i < requests_size(); ++i) |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1478 | EXPECT_THAT(request(i)->WaitForResult(), IsOk()); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1479 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1480 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1481 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1482 | EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup, |
| 1483 | completion_count()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1484 | } |
| 1485 | |
| 1486 | // This test will start up a RequestSocket() and then immediately Cancel() it. |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1487 | // The pending connect job will be cancelled and should not call back into |
| 1488 | // ClientSocketPoolBase. |
| 1489 | TEST_F(ClientSocketPoolBaseTest, CancelRequestClearGroup) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1490 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1491 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1492 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1493 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1494 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1495 | EXPECT_EQ( |
| 1496 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1497 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1498 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1499 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 1500 | NetLogWithSource())); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1501 | handle.Reset(); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1502 | } |
| 1503 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1504 | TEST_F(ClientSocketPoolBaseTest, ConnectCancelConnect) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1505 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1506 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1507 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 1508 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1509 | TestCompletionCallback callback; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1510 | |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1511 | EXPECT_EQ( |
| 1512 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1513 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1514 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1515 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 1516 | NetLogWithSource())); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1517 | |
| 1518 | handle.Reset(); |
| 1519 | |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1520 | TestCompletionCallback callback2; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1521 | EXPECT_EQ( |
| 1522 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1523 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1524 | ClientSocketPool::RespectLimits::ENABLED, |
| 1525 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1526 | pool_.get(), NetLogWithSource())); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1527 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1528 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1529 | EXPECT_FALSE(callback.have_result()); |
| 1530 | |
| 1531 | handle.Reset(); |
| 1532 | } |
| 1533 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1534 | TEST_F(ClientSocketPoolBaseTest, CancelRequest) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1535 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1536 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1537 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1538 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1539 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
| 1540 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1541 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1542 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 1543 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1544 | |
| 1545 | // Cancel a request. |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1546 | size_t index_to_cancel = kDefaultMaxSocketsPerGroup + 2; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1547 | EXPECT_FALSE((*requests())[index_to_cancel]->handle()->is_initialized()); |
| 1548 | (*requests())[index_to_cancel]->handle()->Reset(); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1549 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1550 | ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1551 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1552 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
| 1553 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1554 | EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup - 1, |
| 1555 | completion_count()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1556 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1557 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1558 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1559 | EXPECT_EQ(5, GetOrderOfRequest(3)); |
| 1560 | EXPECT_EQ(3, GetOrderOfRequest(4)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1561 | EXPECT_EQ(ClientSocketPoolTest::kRequestNotFound, |
| 1562 | GetOrderOfRequest(5)); // Canceled request. |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1563 | EXPECT_EQ(4, GetOrderOfRequest(6)); |
| 1564 | EXPECT_EQ(6, GetOrderOfRequest(7)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1565 | |
| 1566 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1567 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(8)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1568 | } |
| 1569 | |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1570 | // Function to be used as a callback on socket request completion. It first |
| 1571 | // disconnects the successfully connected socket from the first request, and |
| 1572 | // then reuses the ClientSocketHandle to request another socket. |
| 1573 | // |
| 1574 | // |nested_callback| is called with the result of the second socket request. |
| 1575 | void RequestSocketOnComplete(ClientSocketHandle* handle, |
| 1576 | TestClientSocketPool* pool, |
| 1577 | TestConnectJobFactory* test_connect_job_factory, |
| 1578 | TestConnectJob::JobType next_job_type, |
Bence Béky | a4a5093 | 2018-08-10 13:39:41 | [diff] [blame] | 1579 | TestCompletionCallback* nested_callback, |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1580 | int first_request_result) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1581 | EXPECT_THAT(first_request_result, IsOk()); |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1582 | |
| 1583 | test_connect_job_factory->set_job_type(next_job_type); |
| 1584 | |
| 1585 | // Don't allow reuse of the socket. Disconnect it and then release it. |
| 1586 | if (handle->socket()) |
| 1587 | handle->socket()->Disconnect(); |
| 1588 | handle->Reset(); |
| 1589 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 1590 | scoped_refptr<TestSocketParams> params(new TestSocketParams()); |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1591 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1592 | int rv = handle->Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1593 | TestGroupId("a"), params, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1594 | ClientSocketPool::RespectLimits::ENABLED, nested_callback->callback(), |
| 1595 | ClientSocketPool::ProxyAuthCallback(), pool, NetLogWithSource()); |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1596 | if (rv != ERR_IO_PENDING) { |
| 1597 | DCHECK_EQ(TestConnectJob::kMockJob, next_job_type); |
Bence Béky | a4a5093 | 2018-08-10 13:39:41 | [diff] [blame] | 1598 | nested_callback->callback().Run(rv); |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1599 | } else { |
| 1600 | DCHECK_EQ(TestConnectJob::kMockPendingJob, next_job_type); |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1601 | } |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1602 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1603 | |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1604 | // Tests the case where a second socket is requested in a completion callback, |
| 1605 | // and the second socket connects asynchronously. Reuses the same |
| 1606 | // ClientSocketHandle for the second socket, after disconnecting the first. |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1607 | TEST_F(ClientSocketPoolBaseTest, RequestPendingJobTwice) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1608 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1609 | |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 1610 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 1611 | ClientSocketHandle handle; |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1612 | TestCompletionCallback second_result_callback; |
| 1613 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1614 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Paul Jensen | 8d6f87ec | 2018-01-13 00:46:54 | [diff] [blame] | 1615 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1616 | base::BindOnce(&RequestSocketOnComplete, &handle, pool_.get(), |
| 1617 | connect_job_factory_, TestConnectJob::kMockPendingJob, |
| 1618 | &second_result_callback), |
| 1619 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1620 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1621 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1622 | EXPECT_THAT(second_result_callback.WaitForResult(), IsOk()); |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1623 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1624 | |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1625 | // Tests the case where a second socket is requested in a completion callback, |
| 1626 | // and the second socket connects synchronously. Reuses the same |
| 1627 | // ClientSocketHandle for the second socket, after disconnecting the first. |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1628 | TEST_F(ClientSocketPoolBaseTest, RequestPendingJobThenSynchronous) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1629 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1630 | |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 1631 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 1632 | ClientSocketHandle handle; |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1633 | TestCompletionCallback second_result_callback; |
| 1634 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1635 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Paul Jensen | 8d6f87ec | 2018-01-13 00:46:54 | [diff] [blame] | 1636 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1637 | base::BindOnce(&RequestSocketOnComplete, &handle, pool_.get(), |
| 1638 | connect_job_factory_, TestConnectJob::kMockPendingJob, |
| 1639 | &second_result_callback), |
| 1640 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1641 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1642 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1643 | EXPECT_THAT(second_result_callback.WaitForResult(), IsOk()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1644 | } |
| 1645 | |
| 1646 | // Make sure that pending requests get serviced after active requests get |
| 1647 | // cancelled. |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1648 | TEST_F(ClientSocketPoolBaseTest, CancelActiveRequestWithPendingRequests) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1649 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1650 | |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 1651 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1652 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1653 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1654 | IsError(ERR_IO_PENDING)); |
| 1655 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1656 | IsError(ERR_IO_PENDING)); |
| 1657 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1658 | IsError(ERR_IO_PENDING)); |
| 1659 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1660 | IsError(ERR_IO_PENDING)); |
| 1661 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1662 | IsError(ERR_IO_PENDING)); |
| 1663 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1664 | IsError(ERR_IO_PENDING)); |
| 1665 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1666 | IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1667 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1668 | // Now, kDefaultMaxSocketsPerGroup requests should be active. |
| 1669 | // Let's cancel them. |
| 1670 | for (int i = 0; i < kDefaultMaxSocketsPerGroup; ++i) { |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1671 | ASSERT_FALSE(request(i)->handle()->is_initialized()); |
| 1672 | request(i)->handle()->Reset(); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1673 | } |
| 1674 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1675 | // Let's wait for the rest to complete now. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1676 | for (size_t i = kDefaultMaxSocketsPerGroup; i < requests_size(); ++i) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1677 | EXPECT_THAT(request(i)->WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1678 | request(i)->handle()->Reset(); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1679 | } |
| 1680 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1681 | EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup, |
| 1682 | completion_count()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1683 | } |
| 1684 | |
| 1685 | // Make sure that pending requests get serviced after active requests fail. |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1686 | TEST_F(ClientSocketPoolBaseTest, FailingActiveRequestWithPendingRequests) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1687 | const size_t kMaxSockets = 5; |
| 1688 | CreatePool(kMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1689 | |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 1690 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1691 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1692 | const size_t kNumberOfRequests = 2 * kDefaultMaxSocketsPerGroup + 1; |
| 1693 | ASSERT_LE(kNumberOfRequests, kMaxSockets); // Otherwise the test will hang. |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1694 | |
| 1695 | // Queue up all the requests |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1696 | for (size_t i = 0; i < kNumberOfRequests; ++i) |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1697 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1698 | IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1699 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1700 | for (size_t i = 0; i < kNumberOfRequests; ++i) |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1701 | EXPECT_THAT(request(i)->WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1702 | } |
| 1703 | |
mmenke | 9d72fe4 | 2017-05-18 22:36:07 | [diff] [blame] | 1704 | // Make sure that pending requests that complete synchronously get serviced |
| 1705 | // after active requests fail. See https://crbug.com/723748 |
| 1706 | TEST_F(ClientSocketPoolBaseTest, HandleMultipleSyncFailuresAfterAsyncFailure) { |
| 1707 | const size_t kNumberOfRequests = 10; |
| 1708 | const size_t kMaxSockets = 1; |
| 1709 | CreatePool(kMaxSockets, kMaxSockets); |
| 1710 | |
| 1711 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
| 1712 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1713 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1714 | IsError(ERR_IO_PENDING)); |
mmenke | 9d72fe4 | 2017-05-18 22:36:07 | [diff] [blame] | 1715 | |
| 1716 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
| 1717 | |
| 1718 | // Queue up all the other requests |
| 1719 | for (size_t i = 1; i < kNumberOfRequests; ++i) |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1720 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1721 | IsError(ERR_IO_PENDING)); |
mmenke | 9d72fe4 | 2017-05-18 22:36:07 | [diff] [blame] | 1722 | |
| 1723 | // Make sure all requests fail, instead of hanging. |
| 1724 | for (size_t i = 0; i < kNumberOfRequests; ++i) |
| 1725 | EXPECT_THAT(request(i)->WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 1726 | } |
| 1727 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1728 | TEST_F(ClientSocketPoolBaseTest, CancelActiveRequestThenRequestSocket) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1729 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1730 | |
| 1731 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1732 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1733 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1734 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1735 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1736 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1737 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1738 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1739 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1740 | |
| 1741 | // Cancel the active request. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1742 | handle.Reset(); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1743 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1744 | rv = handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 1745 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1746 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1747 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1748 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 1749 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1750 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1751 | EXPECT_FALSE(handle.is_reused()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 1752 | TestLoadTimingInfoConnectedNotReused(handle); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1753 | EXPECT_EQ(2, client_socket_factory_.allocation_count()); |
| 1754 | } |
| 1755 | |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1756 | TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketsForced) { |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1757 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1758 | ClientSocketHandle handle; |
| 1759 | TestCompletionCallback callback; |
| 1760 | BoundTestNetLog log; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1761 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1762 | TestGroupId("a"), params_, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1763 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1764 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1765 | EXPECT_THAT(rv, IsOk()); |
| 1766 | handle.Reset(); |
| 1767 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 1768 | pool_->CloseIdleSockets(); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1769 | } |
| 1770 | |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1771 | TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketsInGroupForced) { |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1772 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1773 | TestCompletionCallback callback; |
| 1774 | BoundTestNetLog log; |
| 1775 | ClientSocketHandle handle1; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1776 | int rv = handle1.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1777 | TestGroupId("a"), params_, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1778 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1779 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1780 | EXPECT_THAT(rv, IsOk()); |
| 1781 | ClientSocketHandle handle2; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1782 | rv = handle2.Init(TestGroupId("a"), params_, LOWEST, SocketTag(), |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1783 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1784 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1785 | pool_.get(), log.bound()); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1786 | ClientSocketHandle handle3; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1787 | rv = handle3.Init(TestGroupId("b"), params_, LOWEST, SocketTag(), |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1788 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1789 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1790 | pool_.get(), log.bound()); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1791 | EXPECT_THAT(rv, IsOk()); |
| 1792 | handle1.Reset(); |
| 1793 | handle2.Reset(); |
| 1794 | handle3.Reset(); |
| 1795 | EXPECT_EQ(3, pool_->IdleSocketCount()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1796 | pool_->CloseIdleSocketsInGroup(TestGroupId("a")); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1797 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1798 | } |
| 1799 | |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1800 | TEST_F(ClientSocketPoolBaseTest, CleanUpUnusableIdleSockets) { |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1801 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1802 | ClientSocketHandle handle; |
| 1803 | TestCompletionCallback callback; |
| 1804 | BoundTestNetLog log; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1805 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1806 | TestGroupId("a"), params_, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1807 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1808 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1809 | EXPECT_THAT(rv, IsOk()); |
| 1810 | StreamSocket* socket = handle.socket(); |
| 1811 | handle.Reset(); |
| 1812 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 1813 | |
| 1814 | // Disconnect socket now to make the socket unusable. |
| 1815 | socket->Disconnect(); |
| 1816 | ClientSocketHandle handle2; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1817 | rv = handle2.Init(TestGroupId("a"), params_, LOWEST, SocketTag(), |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1818 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1819 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1820 | pool_.get(), log.bound()); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1821 | EXPECT_THAT(rv, IsOk()); |
| 1822 | EXPECT_FALSE(handle2.is_reused()); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1823 | } |
| 1824 | |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1825 | // Regression test for http://crbug.com/17985. |
| 1826 | TEST_F(ClientSocketPoolBaseTest, GroupWithPendingRequestsIsNotEmpty) { |
| 1827 | const int kMaxSockets = 3; |
| 1828 | const int kMaxSocketsPerGroup = 2; |
| 1829 | CreatePool(kMaxSockets, kMaxSocketsPerGroup); |
| 1830 | |
[email protected] | ac790b4 | 2009-12-02 04:31:31 | [diff] [blame] | 1831 | const RequestPriority kHighPriority = HIGHEST; |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1832 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1833 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1834 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1835 | |
| 1836 | // This is going to be a pending request in an otherwise empty group. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1837 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1838 | IsError(ERR_IO_PENDING)); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1839 | |
| 1840 | // Reach the maximum socket limit. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1841 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1842 | |
| 1843 | // Create a stalled group with high priorities. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1844 | EXPECT_THAT(StartRequest(TestGroupId("c"), kHighPriority), |
| 1845 | IsError(ERR_IO_PENDING)); |
| 1846 | EXPECT_THAT(StartRequest(TestGroupId("c"), kHighPriority), |
| 1847 | IsError(ERR_IO_PENDING)); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1848 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1849 | // Release the first two sockets from TestGroupId("a"). Because this is a |
| 1850 | // keepalive, the first release will unblock the pending request for |
| 1851 | // TestGroupId("a"). The second release will unblock a request for "c", |
| 1852 | // because it is the next high priority socket. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1853 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
| 1854 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1855 | |
| 1856 | // Closing idle sockets should not get us into trouble, but in the bug |
| 1857 | // we were hitting a CHECK here. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1858 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1859 | pool_->CloseIdleSockets(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1860 | |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 1861 | // Run the released socket wakeups. |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 1862 | base::RunLoop().RunUntilIdle(); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1863 | } |
| 1864 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 1865 | TEST_F(ClientSocketPoolBaseTest, BasicAsynchronous) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1866 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1867 | |
| 1868 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1869 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1870 | TestCompletionCallback callback; |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 1871 | BoundTestNetLog log; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1872 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1873 | TestGroupId("a"), params_, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1874 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1875 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1876 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1877 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 1878 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 1879 | TestLoadTimingInfoNotConnected(handle); |
| 1880 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1881 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1882 | EXPECT_TRUE(handle.is_initialized()); |
| 1883 | EXPECT_TRUE(handle.socket()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 1884 | TestLoadTimingInfoConnectedNotReused(handle); |
| 1885 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1886 | handle.Reset(); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 1887 | TestLoadTimingInfoNotConnected(handle); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 1888 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 1889 | TestNetLogEntry::List entries; |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 1890 | log.GetEntries(&entries); |
| 1891 | |
| 1892 | EXPECT_EQ(4u, entries.size()); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 1893 | EXPECT_TRUE(LogContainsBeginEvent(entries, 0, NetLogEventType::SOCKET_POOL)); |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 1894 | EXPECT_TRUE(LogContainsEvent( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 1895 | entries, 1, NetLogEventType::SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
| 1896 | NetLogEventPhase::NONE)); |
| 1897 | EXPECT_TRUE(LogContainsEvent(entries, 2, |
| 1898 | NetLogEventType::SOCKET_POOL_BOUND_TO_SOCKET, |
| 1899 | NetLogEventPhase::NONE)); |
| 1900 | EXPECT_TRUE(LogContainsEndEvent(entries, 3, NetLogEventType::SOCKET_POOL)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1901 | } |
| 1902 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 1903 | TEST_F(ClientSocketPoolBaseTest, |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1904 | InitConnectionAsynchronousFailure) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1905 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1906 | |
| 1907 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1908 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1909 | TestCompletionCallback callback; |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 1910 | BoundTestNetLog log; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 1911 | // Set the additional error state members to ensure that they get cleared. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1912 | handle.set_is_ssl_error(true); |
[email protected] | 8b49869 | 2010-07-16 17:11:43 | [diff] [blame] | 1913 | HttpResponseInfo info; |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 1914 | info.headers = new HttpResponseHeaders(std::string()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1915 | handle.set_ssl_error_response_info(info); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1916 | EXPECT_EQ( |
| 1917 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1918 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1919 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1920 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 1921 | log.bound())); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1922 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 1923 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1924 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1925 | EXPECT_FALSE(handle.is_ssl_error()); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 1926 | EXPECT_TRUE(handle.ssl_error_response_info().headers.get() == nullptr); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 1927 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 1928 | TestNetLogEntry::List entries; |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 1929 | log.GetEntries(&entries); |
| 1930 | |
| 1931 | EXPECT_EQ(3u, entries.size()); |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 1932 | EXPECT_TRUE(LogContainsBeginEvent(entries, 0, NetLogEventType::SOCKET_POOL)); |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 1933 | EXPECT_TRUE(LogContainsEvent( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 1934 | entries, 1, NetLogEventType::SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
| 1935 | NetLogEventPhase::NONE)); |
| 1936 | EXPECT_TRUE(LogContainsEndEvent(entries, 2, NetLogEventType::SOCKET_POOL)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1937 | } |
| 1938 | |
mmenke | 6be122f | 2015-03-09 22:22:47 | [diff] [blame] | 1939 | // Check that an async ConnectJob failure does not result in creation of a new |
| 1940 | // ConnectJob when there's another pending request also waiting on its own |
| 1941 | // ConnectJob. See http://crbug.com/463960. |
| 1942 | TEST_F(ClientSocketPoolBaseTest, AsyncFailureWithPendingRequestWithJob) { |
| 1943 | CreatePool(2, 2); |
| 1944 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
| 1945 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1946 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1947 | IsError(ERR_IO_PENDING)); |
| 1948 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1949 | IsError(ERR_IO_PENDING)); |
mmenke | 6be122f | 2015-03-09 22:22:47 | [diff] [blame] | 1950 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1951 | EXPECT_THAT(request(0)->WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 1952 | EXPECT_THAT(request(1)->WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
mmenke | 6be122f | 2015-03-09 22:22:47 | [diff] [blame] | 1953 | |
| 1954 | EXPECT_EQ(2, client_socket_factory_.allocation_count()); |
| 1955 | } |
| 1956 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 1957 | TEST_F(ClientSocketPoolBaseTest, TwoRequestsCancelOne) { |
[email protected] | b22b516 | 2010-03-16 07:53:10 | [diff] [blame] | 1958 | // TODO(eroman): Add back the log expectations! Removed them because the |
| 1959 | // ordering is difficult, and some may fire during destructor. |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1960 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1961 | |
| 1962 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1963 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1964 | TestCompletionCallback callback; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1965 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1966 | TestCompletionCallback callback2; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1967 | |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1968 | EXPECT_EQ( |
| 1969 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1970 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1971 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1972 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 1973 | NetLogWithSource())); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 1974 | BoundTestNetLog log2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1975 | EXPECT_EQ( |
| 1976 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1977 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1978 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1979 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1980 | pool_.get(), NetLogWithSource())); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1981 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1982 | handle.Reset(); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1983 | |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 1984 | |
| 1985 | // 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] | 1986 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1987 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1988 | handle2.Reset(); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 1989 | |
| 1990 | // Now request 2 has actually finished. |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 1991 | // TODO(eroman): Add back log expectations. |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1992 | } |
| 1993 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 1994 | TEST_F(ClientSocketPoolBaseTest, CancelRequestLimitsJobs) { |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 1995 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1996 | |
[email protected] | 17a0c6c | 2009-08-04 00:07:04 | [diff] [blame] | 1997 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1998 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1999 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
| 2000 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 2001 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 2002 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 2003 | |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2004 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2005 | static_cast<int>(pool_->NumConnectJobsInGroup(TestGroupId("a")))); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2006 | (*requests())[2]->handle()->Reset(); |
| 2007 | (*requests())[3]->handle()->Reset(); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2008 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2009 | static_cast<int>(pool_->NumConnectJobsInGroup(TestGroupId("a")))); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 2010 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2011 | (*requests())[1]->handle()->Reset(); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2012 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2013 | static_cast<int>(pool_->NumConnectJobsInGroup(TestGroupId("a")))); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 2014 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2015 | (*requests())[0]->handle()->Reset(); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2016 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2017 | static_cast<int>(pool_->NumConnectJobsInGroup(TestGroupId("a")))); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 2018 | } |
| 2019 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2020 | // When requests and ConnectJobs are not coupled, the request will get serviced |
| 2021 | // by whatever comes first. |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2022 | TEST_F(ClientSocketPoolBaseTest, ReleaseSockets) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2023 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2024 | |
| 2025 | // Start job 1 (async OK) |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 2026 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2027 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2028 | std::vector<TestSocketRequest*> request_order; |
| 2029 | size_t completion_count; // unused |
| 2030 | TestSocketRequest req1(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2031 | int rv = req1.handle()->Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2032 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2033 | ClientSocketPool::RespectLimits::ENABLED, req1.callback(), |
| 2034 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2035 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 2036 | EXPECT_THAT(req1.WaitForResult(), IsOk()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2037 | |
| 2038 | // Job 1 finished OK. Start job 2 (also async OK). Request 3 is pending |
| 2039 | // without a job. |
| 2040 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2041 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2042 | TestSocketRequest req2(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2043 | rv = req2.handle()->Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2044 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2045 | ClientSocketPool::RespectLimits::ENABLED, req2.callback(), |
| 2046 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2047 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2048 | TestSocketRequest req3(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2049 | rv = req3.handle()->Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2050 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2051 | ClientSocketPool::RespectLimits::ENABLED, req3.callback(), |
| 2052 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2053 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2054 | |
| 2055 | // Both Requests 2 and 3 are pending. We release socket 1 which should |
| 2056 | // service request 2. Request 3 should still be waiting. |
[email protected] | a6c59f6 | 2009-07-29 16:33:33 | [diff] [blame] | 2057 | req1.handle()->Reset(); |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 2058 | // Run the released socket wakeups. |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 2059 | base::RunLoop().RunUntilIdle(); |
[email protected] | a6c59f6 | 2009-07-29 16:33:33 | [diff] [blame] | 2060 | ASSERT_TRUE(req2.handle()->socket()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2061 | EXPECT_THAT(req2.WaitForResult(), IsOk()); |
[email protected] | a6c59f6 | 2009-07-29 16:33:33 | [diff] [blame] | 2062 | EXPECT_FALSE(req3.handle()->socket()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2063 | |
| 2064 | // Signal job 2, which should service request 3. |
| 2065 | |
| 2066 | client_socket_factory_.SignalJobs(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2067 | EXPECT_THAT(req3.WaitForResult(), IsOk()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2068 | |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2069 | ASSERT_EQ(3u, request_order.size()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2070 | EXPECT_EQ(&req1, request_order[0]); |
| 2071 | EXPECT_EQ(&req2, request_order[1]); |
| 2072 | EXPECT_EQ(&req3, request_order[2]); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2073 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2074 | } |
| 2075 | |
| 2076 | // The requests are not coupled to the jobs. So, the requests should finish in |
| 2077 | // their priority / insertion order. |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2078 | TEST_F(ClientSocketPoolBaseTest, PendingJobCompletionOrder) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2079 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2080 | // First two jobs are async. |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 2081 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2082 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2083 | std::vector<TestSocketRequest*> request_order; |
| 2084 | size_t completion_count; // unused |
| 2085 | TestSocketRequest req1(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2086 | int rv = req1.handle()->Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2087 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2088 | ClientSocketPool::RespectLimits::ENABLED, req1.callback(), |
| 2089 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2090 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2091 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2092 | TestSocketRequest req2(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2093 | rv = req2.handle()->Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2094 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2095 | ClientSocketPool::RespectLimits::ENABLED, req2.callback(), |
| 2096 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2097 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2098 | |
| 2099 | // The pending job is sync. |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 2100 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2101 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2102 | TestSocketRequest req3(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2103 | rv = req3.handle()->Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2104 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2105 | ClientSocketPool::RespectLimits::ENABLED, req3.callback(), |
| 2106 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2107 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2108 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2109 | EXPECT_THAT(req1.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 2110 | EXPECT_THAT(req2.WaitForResult(), IsOk()); |
| 2111 | EXPECT_THAT(req3.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2112 | |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2113 | ASSERT_EQ(3u, request_order.size()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2114 | EXPECT_EQ(&req1, request_order[0]); |
| 2115 | EXPECT_EQ(&req2, request_order[1]); |
| 2116 | EXPECT_EQ(&req3, request_order[2]); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2117 | } |
| 2118 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2119 | // Test GetLoadState in the case there's only one socket request. |
| 2120 | TEST_F(ClientSocketPoolBaseTest, LoadStateOneRequest) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2121 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2122 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2123 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2124 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2125 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2126 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2127 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2128 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2129 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2130 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2131 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2132 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2133 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE); |
| 2134 | EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle.GetLoadState()); |
| 2135 | |
| 2136 | // No point in completing the connection, since ClientSocketHandles only |
| 2137 | // expect the LoadState to be checked while connecting. |
| 2138 | } |
| 2139 | |
| 2140 | // Test GetLoadState in the case there are two socket requests. |
| 2141 | TEST_F(ClientSocketPoolBaseTest, LoadStateTwoRequests) { |
| 2142 | CreatePool(2, 2); |
| 2143 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2144 | |
| 2145 | ClientSocketHandle handle; |
| 2146 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2147 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2148 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2149 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2150 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2151 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2152 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_RESOLVING_HOST); |
| 2153 | |
| 2154 | ClientSocketHandle handle2; |
| 2155 | TestCompletionCallback callback2; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2156 | rv = handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 2157 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2158 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2159 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2160 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2161 | client_socket_factory_.SetJobLoadState(1, LOAD_STATE_RESOLVING_HOST); |
| 2162 | |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 2163 | // Each handle should reflect the state of its own job. |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2164 | EXPECT_EQ(LOAD_STATE_RESOLVING_HOST, handle.GetLoadState()); |
| 2165 | EXPECT_EQ(LOAD_STATE_RESOLVING_HOST, handle2.GetLoadState()); |
| 2166 | |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 2167 | // Update the state of the first job. |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2168 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_CONNECTING); |
| 2169 | |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 2170 | // Only the state of the first request should have changed. |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2171 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2172 | EXPECT_EQ(LOAD_STATE_RESOLVING_HOST, handle2.GetLoadState()); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 2173 | |
| 2174 | // Update the state of the second job. |
| 2175 | client_socket_factory_.SetJobLoadState(1, LOAD_STATE_SSL_HANDSHAKE); |
| 2176 | |
| 2177 | // Only the state of the second request should have changed. |
| 2178 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
| 2179 | EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle2.GetLoadState()); |
| 2180 | |
| 2181 | // Second job connects and the first request gets the socket. The |
| 2182 | // second handle switches to the state of the remaining ConnectJob. |
| 2183 | client_socket_factory_.SignalJob(1); |
| 2184 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 2185 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle2.GetLoadState()); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2186 | } |
| 2187 | |
| 2188 | // Test GetLoadState in the case the per-group limit is reached. |
| 2189 | TEST_F(ClientSocketPoolBaseTest, LoadStateGroupLimit) { |
| 2190 | CreatePool(2, 1); |
| 2191 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2192 | |
| 2193 | ClientSocketHandle handle; |
| 2194 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2195 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2196 | TestGroupId("a"), params_, MEDIUM, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2197 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2198 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2199 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2200 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
| 2201 | |
| 2202 | // Request another socket from the same pool, buth with a higher priority. |
| 2203 | // The first request should now be stalled at the socket group limit. |
| 2204 | ClientSocketHandle handle2; |
| 2205 | TestCompletionCallback callback2; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2206 | rv = handle2.Init(TestGroupId("a"), params_, HIGHEST, SocketTag(), |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 2207 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2208 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2209 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2210 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2211 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET, handle.GetLoadState()); |
| 2212 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle2.GetLoadState()); |
| 2213 | |
| 2214 | // The first handle should remain stalled as the other socket goes through |
| 2215 | // the connect process. |
| 2216 | |
| 2217 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE); |
| 2218 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET, handle.GetLoadState()); |
| 2219 | EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle2.GetLoadState()); |
| 2220 | |
| 2221 | client_socket_factory_.SignalJob(0); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2222 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2223 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET, handle.GetLoadState()); |
| 2224 | |
| 2225 | // Closing the second socket should cause the stalled handle to finally get a |
| 2226 | // ConnectJob. |
| 2227 | handle2.socket()->Disconnect(); |
| 2228 | handle2.Reset(); |
| 2229 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
| 2230 | } |
| 2231 | |
| 2232 | // Test GetLoadState in the case the per-pool limit is reached. |
| 2233 | TEST_F(ClientSocketPoolBaseTest, LoadStatePoolLimit) { |
| 2234 | CreatePool(2, 2); |
| 2235 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2236 | |
| 2237 | ClientSocketHandle handle; |
| 2238 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2239 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2240 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2241 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2242 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2243 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2244 | |
| 2245 | // Request for socket from another pool. |
| 2246 | ClientSocketHandle handle2; |
| 2247 | TestCompletionCallback callback2; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2248 | rv = handle2.Init(TestGroupId("b"), params_, DEFAULT_PRIORITY, SocketTag(), |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 2249 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2250 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2251 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2252 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2253 | |
| 2254 | // Request another socket from the first pool. Request should stall at the |
| 2255 | // socket pool limit. |
| 2256 | ClientSocketHandle handle3; |
| 2257 | TestCompletionCallback callback3; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2258 | rv = handle3.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 2259 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2260 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2261 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2262 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2263 | |
| 2264 | // The third handle should remain stalled as the other sockets in its group |
| 2265 | // goes through the connect process. |
| 2266 | |
| 2267 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
| 2268 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL, handle3.GetLoadState()); |
| 2269 | |
| 2270 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE); |
| 2271 | EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle.GetLoadState()); |
| 2272 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL, handle3.GetLoadState()); |
| 2273 | |
| 2274 | client_socket_factory_.SignalJob(0); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2275 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2276 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL, handle3.GetLoadState()); |
| 2277 | |
| 2278 | // Closing a socket should allow the stalled handle to finally get a new |
| 2279 | // ConnectJob. |
| 2280 | handle.socket()->Disconnect(); |
| 2281 | handle.Reset(); |
| 2282 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle3.GetLoadState()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2283 | } |
| 2284 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2285 | TEST_F(ClientSocketPoolBaseTest, CertError) { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2286 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2287 | connect_job_factory_->set_job_type(TestConnectJob::kMockCertErrorJob); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2288 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2289 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2290 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2291 | EXPECT_EQ( |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2292 | ERR_CERT_COMMON_NAME_INVALID, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2293 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2294 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2295 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2296 | NetLogWithSource())); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2297 | EXPECT_TRUE(handle.is_initialized()); |
| 2298 | EXPECT_TRUE(handle.socket()); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2299 | } |
| 2300 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2301 | TEST_F(ClientSocketPoolBaseTest, AsyncCertError) { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2302 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2303 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2304 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingCertErrorJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2305 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2306 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2307 | EXPECT_EQ( |
| 2308 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2309 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2310 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2311 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2312 | NetLogWithSource())); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2313 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2314 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2315 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_CERT_COMMON_NAME_INVALID)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2316 | EXPECT_TRUE(handle.is_initialized()); |
| 2317 | EXPECT_TRUE(handle.socket()); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2318 | } |
| 2319 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2320 | TEST_F(ClientSocketPoolBaseTest, AdditionalErrorStateSynchronous) { |
| 2321 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2322 | connect_job_factory_->set_job_type( |
| 2323 | TestConnectJob::kMockAdditionalErrorStateJob); |
| 2324 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2325 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2326 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2327 | EXPECT_EQ( |
| 2328 | ERR_CONNECTION_FAILED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2329 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2330 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2331 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2332 | NetLogWithSource())); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2333 | EXPECT_FALSE(handle.is_initialized()); |
| 2334 | EXPECT_FALSE(handle.socket()); |
| 2335 | EXPECT_TRUE(handle.is_ssl_error()); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 2336 | EXPECT_FALSE(handle.ssl_error_response_info().headers.get() == nullptr); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2337 | } |
| 2338 | |
| 2339 | TEST_F(ClientSocketPoolBaseTest, AdditionalErrorStateAsynchronous) { |
| 2340 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2341 | |
| 2342 | connect_job_factory_->set_job_type( |
| 2343 | TestConnectJob::kMockPendingAdditionalErrorStateJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2344 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2345 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2346 | EXPECT_EQ( |
| 2347 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2348 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2349 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2350 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2351 | NetLogWithSource())); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2352 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2353 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2354 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2355 | EXPECT_FALSE(handle.is_initialized()); |
| 2356 | EXPECT_FALSE(handle.socket()); |
| 2357 | EXPECT_TRUE(handle.is_ssl_error()); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 2358 | EXPECT_FALSE(handle.ssl_error_response_info().headers.get() == nullptr); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2359 | } |
| 2360 | |
martijn | 003cd61 | 2016-05-19 22:24:38 | [diff] [blame] | 2361 | // Make sure we can reuse sockets. |
| 2362 | TEST_F(ClientSocketPoolBaseTest, CleanupTimedOutIdleSocketsReuse) { |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2363 | CreatePoolWithIdleTimeouts( |
| 2364 | kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2365 | base::TimeDelta(), // Time out unused sockets immediately. |
| 2366 | base::TimeDelta::FromDays(1)); // Don't time out used sockets. |
| 2367 | |
| 2368 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2369 | |
| 2370 | ClientSocketHandle handle; |
| 2371 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2372 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2373 | TestGroupId("a"), params_, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2374 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2375 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2376 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2377 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2378 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2379 | ASSERT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2380 | |
| 2381 | // Use and release the socket. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 2382 | EXPECT_EQ(1, handle.socket()->Write(nullptr, 1, CompletionOnceCallback(), |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 2383 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 2384 | TestLoadTimingInfoConnectedNotReused(handle); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2385 | handle.Reset(); |
| 2386 | |
| 2387 | // Should now have one idle socket. |
| 2388 | ASSERT_EQ(1, pool_->IdleSocketCount()); |
| 2389 | |
| 2390 | // Request a new socket. This should reuse the old socket and complete |
| 2391 | // synchronously. |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 2392 | BoundTestNetLog log; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2393 | rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2394 | TestGroupId("a"), params_, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2395 | ClientSocketPool::RespectLimits::ENABLED, CompletionOnceCallback(), |
| 2396 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2397 | ASSERT_THAT(rv, IsOk()); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2398 | EXPECT_TRUE(handle.is_reused()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 2399 | TestLoadTimingInfoConnectedReused(handle); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2400 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2401 | ASSERT_TRUE(pool_->HasGroup(TestGroupId("a"))); |
| 2402 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
| 2403 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroup(TestGroupId("a"))); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2404 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 2405 | TestNetLogEntry::List entries; |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2406 | log.GetEntries(&entries); |
| 2407 | EXPECT_TRUE(LogContainsEntryWithType( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 2408 | entries, 1, NetLogEventType::SOCKET_POOL_REUSED_AN_EXISTING_SOCKET)); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2409 | } |
| 2410 | |
martijn | 003cd61 | 2016-05-19 22:24:38 | [diff] [blame] | 2411 | // Make sure we cleanup old unused sockets. |
Eric Roman | b49715e | 2018-04-24 22:41:17 | [diff] [blame] | 2412 | TEST_F(ClientSocketPoolBaseTest, CleanupTimedOutIdleSocketsNoReuse) { |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2413 | CreatePoolWithIdleTimeouts( |
| 2414 | kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 2415 | base::TimeDelta(), // Time out unused sockets immediately |
| 2416 | base::TimeDelta()); // Time out used sockets immediately |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2417 | |
| 2418 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2419 | |
| 2420 | // Startup two mock pending connect jobs, which will sit in the MessageLoop. |
| 2421 | |
| 2422 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2423 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2424 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2425 | TestGroupId("a"), params_, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2426 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2427 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2428 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2429 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2430 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2431 | |
| 2432 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2433 | TestCompletionCallback callback2; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2434 | rv = handle2.Init(TestGroupId("a"), params_, LOWEST, SocketTag(), |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 2435 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2436 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2437 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2438 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2439 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2440 | pool_->GetLoadState(TestGroupId("a"), &handle2)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2441 | |
| 2442 | // Cancel one of the requests. Wait for the other, which will get the first |
| 2443 | // job. Release the socket. Run the loop again to make sure the second |
| 2444 | // socket is sitting idle and the first one is released (since ReleaseSocket() |
| 2445 | // just posts a DoReleaseSocket() task). |
| 2446 | |
| 2447 | handle.Reset(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2448 | ASSERT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2449 | // Use the socket. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 2450 | EXPECT_EQ(1, handle2.socket()->Write(nullptr, 1, CompletionOnceCallback(), |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 2451 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2452 | handle2.Reset(); |
| 2453 | |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2454 | // We post all of our delayed tasks with a 2ms delay. I.e. they don't |
| 2455 | // actually become pending until 2ms after they have been created. In order |
| 2456 | // to flush all tasks, we need to wait so that we know there are no |
| 2457 | // soon-to-be-pending tasks waiting. |
Alex Clarke | 0def209 | 2018-12-10 12:01:45 | [diff] [blame] | 2458 | FastForwardBy(base::TimeDelta::FromMilliseconds(10)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2459 | |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2460 | // Both sockets should now be idle. |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2461 | ASSERT_EQ(2, pool_->IdleSocketCount()); |
| 2462 | |
| 2463 | // Request a new socket. This should cleanup the unused and timed out ones. |
| 2464 | // A new socket will be created rather than reusing the idle one. |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 2465 | BoundTestNetLog log; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2466 | TestCompletionCallback callback3; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2467 | rv = handle.Init(TestGroupId("a"), params_, LOWEST, SocketTag(), |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 2468 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2469 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2470 | pool_.get(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2471 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 2472 | ASSERT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2473 | EXPECT_FALSE(handle.is_reused()); |
| 2474 | |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2475 | // Make sure the idle socket is closed. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2476 | ASSERT_TRUE(pool_->HasGroup(TestGroupId("a"))); |
| 2477 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
| 2478 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroup(TestGroupId("a"))); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2479 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 2480 | TestNetLogEntry::List entries; |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2481 | log.GetEntries(&entries); |
| 2482 | EXPECT_FALSE(LogContainsEntryWithType( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 2483 | entries, 1, NetLogEventType::SOCKET_POOL_REUSED_AN_EXISTING_SOCKET)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2484 | } |
| 2485 | |
[email protected] | 2041cf34 | 2010-02-19 03:15:59 | [diff] [blame] | 2486 | // Make sure that we process all pending requests even when we're stalling |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2487 | // because of multiple releasing disconnected sockets. |
| 2488 | TEST_F(ClientSocketPoolBaseTest, MultipleReleasingDisconnectedSockets) { |
| 2489 | CreatePoolWithIdleTimeouts( |
| 2490 | kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 2491 | base::TimeDelta(), // Time out unused sockets immediately. |
| 2492 | base::TimeDelta::FromDays(1)); // Don't time out used sockets. |
| 2493 | |
| 2494 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 2495 | |
| 2496 | // Startup 4 connect jobs. Two of them will be pending. |
| 2497 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2498 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2499 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2500 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2501 | TestGroupId("a"), params_, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2502 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2503 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2504 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2505 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2506 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2507 | TestCompletionCallback callback2; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2508 | rv = handle2.Init(TestGroupId("a"), params_, LOWEST, SocketTag(), |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 2509 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2510 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2511 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2512 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2513 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2514 | ClientSocketHandle handle3; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2515 | TestCompletionCallback callback3; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2516 | rv = handle3.Init(TestGroupId("a"), params_, LOWEST, SocketTag(), |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 2517 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2518 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2519 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2520 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2521 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2522 | ClientSocketHandle handle4; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2523 | TestCompletionCallback callback4; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2524 | rv = handle4.Init(TestGroupId("a"), params_, LOWEST, SocketTag(), |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 2525 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2526 | callback4.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2527 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2528 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2529 | |
| 2530 | // Release two disconnected sockets. |
| 2531 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2532 | handle.socket()->Disconnect(); |
| 2533 | handle.Reset(); |
| 2534 | handle2.socket()->Disconnect(); |
| 2535 | handle2.Reset(); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2536 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2537 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2538 | EXPECT_FALSE(handle3.is_reused()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2539 | EXPECT_THAT(callback4.WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2540 | EXPECT_FALSE(handle4.is_reused()); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2541 | } |
| 2542 | |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2543 | // Regression test for http://crbug.com/42267. |
| 2544 | // When DoReleaseSocket() is processed for one socket, it is blocked because the |
| 2545 | // other stalled groups all have releasing sockets, so no progress can be made. |
| 2546 | TEST_F(ClientSocketPoolBaseTest, SocketLimitReleasingSockets) { |
| 2547 | CreatePoolWithIdleTimeouts( |
| 2548 | 4 /* socket limit */, 4 /* socket limit per group */, |
| 2549 | base::TimeDelta(), // Time out unused sockets immediately. |
| 2550 | base::TimeDelta::FromDays(1)); // Don't time out used sockets. |
| 2551 | |
| 2552 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 2553 | |
| 2554 | // Max out the socket limit with 2 per group. |
| 2555 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2556 | ClientSocketHandle handle_a[4]; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2557 | TestCompletionCallback callback_a[4]; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2558 | ClientSocketHandle handle_b[4]; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2559 | TestCompletionCallback callback_b[4]; |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2560 | |
| 2561 | for (int i = 0; i < 2; ++i) { |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2562 | EXPECT_EQ( |
| 2563 | OK, handle_a[i].Init(TestGroupId("a"), params_, LOWEST, SocketTag(), |
| 2564 | ClientSocketPool::RespectLimits::ENABLED, |
| 2565 | callback_a[i].callback(), |
| 2566 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2567 | NetLogWithSource())); |
| 2568 | EXPECT_EQ( |
| 2569 | OK, handle_b[i].Init(TestGroupId("b"), params_, LOWEST, SocketTag(), |
| 2570 | ClientSocketPool::RespectLimits::ENABLED, |
| 2571 | callback_b[i].callback(), |
| 2572 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2573 | NetLogWithSource())); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2574 | } |
[email protected] | b89f7e4 | 2010-05-20 20:37:00 | [diff] [blame] | 2575 | |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2576 | // Make 4 pending requests, 2 per group. |
| 2577 | |
| 2578 | for (int i = 2; i < 4; ++i) { |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 2579 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2580 | handle_a[i].Init(TestGroupId("a"), params_, LOWEST, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 2581 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2582 | callback_a[i].callback(), |
| 2583 | ClientSocketPool::ProxyAuthCallback(), |
| 2584 | pool_.get(), NetLogWithSource())); |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 2585 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2586 | handle_b[i].Init(TestGroupId("b"), params_, LOWEST, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 2587 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2588 | callback_b[i].callback(), |
| 2589 | ClientSocketPool::ProxyAuthCallback(), |
| 2590 | pool_.get(), NetLogWithSource())); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2591 | } |
| 2592 | |
| 2593 | // Release b's socket first. The order is important, because in |
| 2594 | // DoReleaseSocket(), we'll process b's released socket, and since both b and |
| 2595 | // a are stalled, but 'a' is lower lexicographically, we'll process group 'a' |
| 2596 | // first, which has a releasing socket, so it refuses to start up another |
| 2597 | // ConnectJob. So, we used to infinite loop on this. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2598 | handle_b[0].socket()->Disconnect(); |
| 2599 | handle_b[0].Reset(); |
| 2600 | handle_a[0].socket()->Disconnect(); |
| 2601 | handle_a[0].Reset(); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2602 | |
| 2603 | // Used to get stuck here. |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 2604 | base::RunLoop().RunUntilIdle(); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2605 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2606 | handle_b[1].socket()->Disconnect(); |
| 2607 | handle_b[1].Reset(); |
| 2608 | handle_a[1].socket()->Disconnect(); |
| 2609 | handle_a[1].Reset(); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2610 | |
| 2611 | for (int i = 2; i < 4; ++i) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2612 | EXPECT_THAT(callback_b[i].WaitForResult(), IsOk()); |
| 2613 | EXPECT_THAT(callback_a[i].WaitForResult(), IsOk()); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2614 | } |
| 2615 | } |
| 2616 | |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2617 | TEST_F(ClientSocketPoolBaseTest, |
| 2618 | ReleasingDisconnectedSocketsMaintainsPriorityOrder) { |
| 2619 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2620 | |
| 2621 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2622 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2623 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2624 | IsError(ERR_IO_PENDING)); |
| 2625 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2626 | IsError(ERR_IO_PENDING)); |
| 2627 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2628 | IsError(ERR_IO_PENDING)); |
| 2629 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2630 | IsError(ERR_IO_PENDING)); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2631 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2632 | EXPECT_THAT((*requests())[0]->WaitForResult(), IsOk()); |
| 2633 | EXPECT_THAT((*requests())[1]->WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2634 | EXPECT_EQ(2u, completion_count()); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2635 | |
| 2636 | // Releases one connection. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2637 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::NO_KEEP_ALIVE)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2638 | EXPECT_THAT((*requests())[2]->WaitForResult(), IsOk()); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2639 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2640 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::NO_KEEP_ALIVE)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2641 | EXPECT_THAT((*requests())[3]->WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2642 | EXPECT_EQ(4u, completion_count()); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2643 | |
| 2644 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 2645 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 2646 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 2647 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 2648 | |
| 2649 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2650 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(5)); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2651 | } |
| 2652 | |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2653 | class TestReleasingSocketRequest : public TestCompletionCallbackBase { |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2654 | public: |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2655 | TestReleasingSocketRequest(TestClientSocketPool* pool, |
| 2656 | int expected_result, |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2657 | bool reset_releasing_handle) |
| 2658 | : pool_(pool), |
| 2659 | expected_result_(expected_result), |
Bence Béky | 8ddc249 | 2018-06-13 01:02:04 | [diff] [blame] | 2660 | reset_releasing_handle_(reset_releasing_handle) {} |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2661 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 2662 | ~TestReleasingSocketRequest() override = default; |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2663 | |
| 2664 | ClientSocketHandle* handle() { return &handle_; } |
| 2665 | |
Bence Béky | 8ddc249 | 2018-06-13 01:02:04 | [diff] [blame] | 2666 | CompletionOnceCallback callback() { |
| 2667 | return base::BindOnce(&TestReleasingSocketRequest::OnComplete, |
| 2668 | base::Unretained(this)); |
| 2669 | } |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2670 | |
| 2671 | private: |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2672 | void OnComplete(int result) { |
| 2673 | SetResult(result); |
| 2674 | if (reset_releasing_handle_) |
| 2675 | handle_.Reset(); |
| 2676 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 2677 | scoped_refptr<TestSocketParams> con_params(new TestSocketParams()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2678 | EXPECT_EQ( |
| 2679 | expected_result_, |
| 2680 | handle2_.Init( |
| 2681 | TestGroupId("a"), con_params, DEFAULT_PRIORITY, SocketTag(), |
| 2682 | ClientSocketPool::RespectLimits::ENABLED, CompletionOnceCallback(), |
| 2683 | ClientSocketPool::ProxyAuthCallback(), pool_, NetLogWithSource())); |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2684 | } |
| 2685 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2686 | TestClientSocketPool* const pool_; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2687 | int expected_result_; |
| 2688 | bool reset_releasing_handle_; |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2689 | ClientSocketHandle handle_; |
| 2690 | ClientSocketHandle handle2_; |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2691 | }; |
| 2692 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2693 | |
| 2694 | TEST_F(ClientSocketPoolBaseTest, AdditionalErrorSocketsDontUseSlot) { |
| 2695 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2696 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2697 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
| 2698 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 2699 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2700 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2701 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2702 | client_socket_factory_.allocation_count()); |
| 2703 | |
| 2704 | connect_job_factory_->set_job_type( |
| 2705 | TestConnectJob::kMockPendingAdditionalErrorStateJob); |
| 2706 | TestReleasingSocketRequest req(pool_.get(), OK, false); |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 2707 | EXPECT_EQ( |
| 2708 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2709 | req.handle()->Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, |
| 2710 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2711 | req.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2712 | pool_.get(), NetLogWithSource())); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2713 | // The next job should complete synchronously |
| 2714 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 2715 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2716 | EXPECT_THAT(req.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2717 | EXPECT_FALSE(req.handle()->is_initialized()); |
| 2718 | EXPECT_FALSE(req.handle()->socket()); |
| 2719 | EXPECT_TRUE(req.handle()->is_ssl_error()); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 2720 | EXPECT_FALSE(req.handle()->ssl_error_response_info().headers.get() == |
| 2721 | nullptr); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2722 | } |
| 2723 | |
[email protected] | b6501d3d | 2010-06-03 23:53:34 | [diff] [blame] | 2724 | // http://crbug.com/44724 regression test. |
| 2725 | // We start releasing the pool when we flush on network change. When that |
| 2726 | // happens, the only active references are in the ClientSocketHandles. When a |
| 2727 | // ConnectJob completes and calls back into the last ClientSocketHandle, that |
| 2728 | // callback can release the last reference and delete the pool. After the |
| 2729 | // callback finishes, we go back to the stack frame within the now-deleted pool. |
| 2730 | // Executing any code that refers to members of the now-deleted pool can cause |
| 2731 | // crashes. |
| 2732 | TEST_F(ClientSocketPoolBaseTest, CallbackThatReleasesPool) { |
| 2733 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2734 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
| 2735 | |
| 2736 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2737 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2738 | EXPECT_EQ( |
| 2739 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2740 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2741 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2742 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2743 | NetLogWithSource())); |
[email protected] | b6501d3d | 2010-06-03 23:53:34 | [diff] [blame] | 2744 | |
[email protected] | 7af985a | 2012-12-14 22:40:42 | [diff] [blame] | 2745 | pool_->FlushWithError(ERR_NETWORK_CHANGED); |
[email protected] | b6501d3d | 2010-06-03 23:53:34 | [diff] [blame] | 2746 | |
| 2747 | // We'll call back into this now. |
| 2748 | callback.WaitForResult(); |
| 2749 | } |
| 2750 | |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2751 | TEST_F(ClientSocketPoolBaseTest, DoNotReuseSocketAfterFlush) { |
| 2752 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2753 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2754 | |
| 2755 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2756 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2757 | EXPECT_EQ( |
| 2758 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2759 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2760 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2761 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2762 | NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2763 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2764 | EXPECT_EQ(ClientSocketHandle::UNUSED, handle.reuse_type()); |
| 2765 | |
[email protected] | 7af985a | 2012-12-14 22:40:42 | [diff] [blame] | 2766 | pool_->FlushWithError(ERR_NETWORK_CHANGED); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2767 | |
| 2768 | handle.Reset(); |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 2769 | base::RunLoop().RunUntilIdle(); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2770 | |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2771 | EXPECT_EQ( |
| 2772 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2773 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2774 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2775 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2776 | NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2777 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2778 | EXPECT_EQ(ClientSocketHandle::UNUSED, handle.reuse_type()); |
| 2779 | } |
| 2780 | |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2781 | class ConnectWithinCallback : public TestCompletionCallbackBase { |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2782 | public: |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2783 | ConnectWithinCallback(const ClientSocketPool::GroupId& group_id, |
Bence Béky | 8ddc249 | 2018-06-13 01:02:04 | [diff] [blame] | 2784 | const scoped_refptr<TestSocketParams>& params, |
| 2785 | TestClientSocketPool* pool) |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2786 | : group_id_(group_id), params_(params), pool_(pool) {} |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2787 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 2788 | ~ConnectWithinCallback() override = default; |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2789 | |
| 2790 | int WaitForNestedResult() { |
| 2791 | return nested_callback_.WaitForResult(); |
| 2792 | } |
| 2793 | |
Bence Béky | 8ddc249 | 2018-06-13 01:02:04 | [diff] [blame] | 2794 | CompletionOnceCallback callback() { |
| 2795 | return base::BindOnce(&ConnectWithinCallback::OnComplete, |
| 2796 | base::Unretained(this)); |
| 2797 | } |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2798 | |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2799 | private: |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2800 | void OnComplete(int result) { |
| 2801 | SetResult(result); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2802 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2803 | handle_.Init(group_id_, params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2804 | ClientSocketPool::RespectLimits::ENABLED, |
| 2805 | nested_callback_.callback(), |
| 2806 | ClientSocketPool::ProxyAuthCallback(), pool_, |
| 2807 | NetLogWithSource())); |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2808 | } |
| 2809 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2810 | const ClientSocketPool::GroupId group_id_; |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2811 | const scoped_refptr<TestSocketParams> params_; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2812 | TestClientSocketPool* const pool_; |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2813 | ClientSocketHandle handle_; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2814 | TestCompletionCallback nested_callback_; |
| 2815 | |
| 2816 | DISALLOW_COPY_AND_ASSIGN(ConnectWithinCallback); |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2817 | }; |
| 2818 | |
| 2819 | TEST_F(ClientSocketPoolBaseTest, AbortAllRequestsOnFlush) { |
| 2820 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2821 | |
| 2822 | // First job will be waiting until it gets aborted. |
| 2823 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2824 | |
| 2825 | ClientSocketHandle handle; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2826 | ConnectWithinCallback callback(TestGroupId("a"), params_, pool_.get()); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2827 | EXPECT_EQ( |
| 2828 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2829 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2830 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2831 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2832 | NetLogWithSource())); |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2833 | |
| 2834 | // Second job will be started during the first callback, and will |
| 2835 | // asynchronously complete with OK. |
| 2836 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 7af985a | 2012-12-14 22:40:42 | [diff] [blame] | 2837 | pool_->FlushWithError(ERR_NETWORK_CHANGED); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2838 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_NETWORK_CHANGED)); |
| 2839 | EXPECT_THAT(callback.WaitForNestedResult(), IsOk()); |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2840 | } |
| 2841 | |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 2842 | TEST_F(ClientSocketPoolBaseTest, BackupSocketWaitsForHostResolution) { |
| 2843 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 2844 | pool_->EnableConnectBackupJobs(); |
| 2845 | |
| 2846 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2847 | ClientSocketHandle handle; |
| 2848 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2849 | EXPECT_EQ( |
| 2850 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2851 | handle.Init(TestGroupId("bar"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2852 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2853 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2854 | NetLogWithSource())); |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 2855 | // The backup timer fires but doesn't start a new ConnectJob while resolving |
| 2856 | // the hostname. |
| 2857 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_RESOLVING_HOST); |
| 2858 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
| 2859 | ClientSocketPool::kMaxConnectRetryIntervalMs * 100)); |
| 2860 | EXPECT_EQ(1, client_socket_factory_.allocation_count()); |
| 2861 | |
| 2862 | // Once the ConnectJob has finished resolving the hostname, the backup timer |
| 2863 | // will create a ConnectJob when it fires. |
| 2864 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_CONNECTING); |
| 2865 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
| 2866 | ClientSocketPool::kMaxConnectRetryIntervalMs)); |
| 2867 | EXPECT_EQ(2, client_socket_factory_.allocation_count()); |
| 2868 | } |
| 2869 | |
| 2870 | // Test that no backup socket is created when a ConnectJob connects before it |
| 2871 | // completes. |
| 2872 | TEST_F(ClientSocketPoolBaseTest, NoBackupSocketWhenConnected) { |
| 2873 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 2874 | pool_->EnableConnectBackupJobs(); |
| 2875 | |
| 2876 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2877 | ClientSocketHandle handle; |
| 2878 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2879 | EXPECT_EQ( |
| 2880 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2881 | handle.Init(TestGroupId("bar"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2882 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2883 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2884 | NetLogWithSource())); |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 2885 | // The backup timer fires but doesn't start a new ConnectJob while resolving |
| 2886 | // the hostname. |
| 2887 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_RESOLVING_HOST); |
| 2888 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
| 2889 | ClientSocketPool::kMaxConnectRetryIntervalMs * 100)); |
| 2890 | EXPECT_EQ(1, client_socket_factory_.allocation_count()); |
| 2891 | |
| 2892 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE); |
| 2893 | client_socket_factory_.SetJobHasEstablishedConnection(0); |
| 2894 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
| 2895 | ClientSocketPool::kMaxConnectRetryIntervalMs * 100)); |
| 2896 | EXPECT_EQ(1, client_socket_factory_.allocation_count()); |
| 2897 | } |
| 2898 | |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 2899 | // Cancel a pending socket request while we're at max sockets, |
| 2900 | // and verify that the backup socket firing doesn't cause a crash. |
| 2901 | TEST_F(ClientSocketPoolBaseTest, BackupSocketCancelAtMaxSockets) { |
| 2902 | // Max 4 sockets globally, max 4 sockets per group. |
| 2903 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
[email protected] | 06d9404 | 2010-08-25 01:45:22 | [diff] [blame] | 2904 | pool_->EnableConnectBackupJobs(); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 2905 | |
[email protected] | 4baaf9d | 2010-08-31 15:15:44 | [diff] [blame] | 2906 | // Create the first socket and set to ERR_IO_PENDING. This starts the backup |
| 2907 | // timer. |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 2908 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2909 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2910 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2911 | EXPECT_EQ( |
| 2912 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2913 | handle.Init(TestGroupId("bar"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2914 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2915 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2916 | NetLogWithSource())); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 2917 | |
| 2918 | // Start (MaxSockets - 1) connected sockets to reach max sockets. |
| 2919 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 2920 | ClientSocketHandle handles[kDefaultMaxSockets]; |
| 2921 | for (int i = 1; i < kDefaultMaxSockets; ++i) { |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2922 | TestCompletionCallback callback; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2923 | EXPECT_EQ(OK, handles[i].Init(TestGroupId("bar"), params_, DEFAULT_PRIORITY, |
| 2924 | SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 2925 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2926 | callback.callback(), |
| 2927 | ClientSocketPool::ProxyAuthCallback(), |
| 2928 | pool_.get(), NetLogWithSource())); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 2929 | } |
| 2930 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 2931 | base::RunLoop().RunUntilIdle(); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 2932 | |
| 2933 | // Cancel the pending request. |
| 2934 | handle.Reset(); |
| 2935 | |
| 2936 | // 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] | 2937 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
[email protected] | 26b997396 | 2012-01-28 00:57:00 | [diff] [blame] | 2938 | ClientSocketPool::kMaxConnectRetryIntervalMs / 2 * 3)); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 2939 | |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 2940 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 2941 | } |
| 2942 | |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 2943 | TEST_F(ClientSocketPoolBaseTest, CancelBackupSocketAfterCancelingAllRequests) { |
[email protected] | 4baaf9d | 2010-08-31 15:15:44 | [diff] [blame] | 2944 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 2945 | pool_->EnableConnectBackupJobs(); |
| 2946 | |
| 2947 | // Create the first socket and set to ERR_IO_PENDING. This starts the backup |
| 2948 | // timer. |
| 2949 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2950 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2951 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2952 | EXPECT_EQ( |
| 2953 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2954 | handle.Init(TestGroupId("bar"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2955 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2956 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2957 | NetLogWithSource())); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2958 | ASSERT_TRUE(pool_->HasGroup(TestGroupId("bar"))); |
| 2959 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("bar"))); |
| 2960 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("bar"))); |
| 2961 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("bar"))); |
[email protected] | 4baaf9d | 2010-08-31 15:15:44 | [diff] [blame] | 2962 | |
| 2963 | // Cancel the socket request. This should cancel the backup timer. Wait for |
| 2964 | // the backup time to see if it indeed got canceled. |
| 2965 | handle.Reset(); |
| 2966 | // 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] | 2967 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
[email protected] | 26b997396 | 2012-01-28 00:57:00 | [diff] [blame] | 2968 | ClientSocketPool::kMaxConnectRetryIntervalMs / 2 * 3)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2969 | ASSERT_TRUE(pool_->HasGroup(TestGroupId("bar"))); |
| 2970 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("bar"))); |
[email protected] | 4baaf9d | 2010-08-31 15:15:44 | [diff] [blame] | 2971 | } |
| 2972 | |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 2973 | TEST_F(ClientSocketPoolBaseTest, CancelBackupSocketAfterFinishingAllRequests) { |
| 2974 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 2975 | pool_->EnableConnectBackupJobs(); |
| 2976 | |
| 2977 | // Create the first socket and set to ERR_IO_PENDING. This starts the backup |
| 2978 | // timer. |
| 2979 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2980 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2981 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2982 | EXPECT_EQ( |
| 2983 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2984 | handle.Init(TestGroupId("bar"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2985 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2986 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2987 | NetLogWithSource())); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 2988 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2989 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2990 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 2991 | EXPECT_EQ( |
| 2992 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2993 | handle2.Init(TestGroupId("bar"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 2994 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2995 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2996 | pool_.get(), NetLogWithSource())); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2997 | ASSERT_TRUE(pool_->HasGroup(TestGroupId("bar"))); |
| 2998 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroup(TestGroupId("bar"))); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 2999 | |
| 3000 | // Cancel request 1 and then complete request 2. With the requests finished, |
| 3001 | // the backup timer should be cancelled. |
| 3002 | handle.Reset(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3003 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3004 | // 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] | 3005 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
[email protected] | 26b997396 | 2012-01-28 00:57:00 | [diff] [blame] | 3006 | ClientSocketPool::kMaxConnectRetryIntervalMs / 2 * 3)); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3007 | } |
| 3008 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3009 | // Test delayed socket binding for the case where we have two connects, |
| 3010 | // and while one is waiting on a connect, the other frees up. |
| 3011 | // The socket waiting on a connect should switch immediately to the freed |
| 3012 | // up socket. |
| 3013 | TEST_F(ClientSocketPoolBaseTest, DelayedSocketBindingWaitingForConnect) { |
| 3014 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3015 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3016 | |
| 3017 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3018 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3019 | EXPECT_EQ( |
| 3020 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3021 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3022 | ClientSocketPool::RespectLimits::ENABLED, |
| 3023 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3024 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3025 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3026 | |
| 3027 | // No idle sockets, no pending jobs. |
| 3028 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3029 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3030 | |
| 3031 | // Create a second socket to the same host, but this one will wait. |
| 3032 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3033 | ClientSocketHandle handle2; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3034 | EXPECT_EQ( |
| 3035 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3036 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3037 | ClientSocketPool::RespectLimits::ENABLED, |
| 3038 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3039 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3040 | // No idle sockets, and one connecting job. |
| 3041 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3042 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3043 | |
| 3044 | // Return the first handle to the pool. This will initiate the delayed |
| 3045 | // binding. |
| 3046 | handle1.Reset(); |
| 3047 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3048 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3049 | |
| 3050 | // Still no idle sockets, still one pending connect job. |
| 3051 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3052 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3053 | |
| 3054 | // The second socket connected, even though it was a Waiting Job. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3055 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3056 | |
| 3057 | // And we can see there is still one job waiting. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3058 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3059 | |
| 3060 | // Finally, signal the waiting Connect. |
| 3061 | client_socket_factory_.SignalJobs(); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3062 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3063 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3064 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3065 | } |
| 3066 | |
| 3067 | // Test delayed socket binding when a group is at capacity and one |
| 3068 | // of the group's sockets frees up. |
| 3069 | TEST_F(ClientSocketPoolBaseTest, DelayedSocketBindingAtGroupCapacity) { |
| 3070 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3071 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3072 | |
| 3073 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3074 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3075 | EXPECT_EQ( |
| 3076 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3077 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3078 | ClientSocketPool::RespectLimits::ENABLED, |
| 3079 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3080 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3081 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3082 | |
| 3083 | // No idle sockets, no pending jobs. |
| 3084 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3085 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3086 | |
| 3087 | // Create a second socket to the same host, but this one will wait. |
| 3088 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3089 | ClientSocketHandle handle2; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3090 | EXPECT_EQ( |
| 3091 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3092 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3093 | ClientSocketPool::RespectLimits::ENABLED, |
| 3094 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3095 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3096 | // No idle sockets, and one connecting job. |
| 3097 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3098 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3099 | |
| 3100 | // Return the first handle to the pool. This will initiate the delayed |
| 3101 | // binding. |
| 3102 | handle1.Reset(); |
| 3103 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3104 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3105 | |
| 3106 | // Still no idle sockets, still one pending connect job. |
| 3107 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3108 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3109 | |
| 3110 | // The second socket connected, even though it was a Waiting Job. |
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 | // And we can see there is still one job waiting. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3114 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3115 | |
| 3116 | // Finally, signal the waiting Connect. |
| 3117 | client_socket_factory_.SignalJobs(); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3118 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3119 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3120 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3121 | } |
| 3122 | |
| 3123 | // Test out the case where we have one socket connected, one |
| 3124 | // connecting, when the first socket finishes and goes idle. |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3125 | // Although the second connection is pending, the second request |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3126 | // should complete, by taking the first socket's idle socket. |
| 3127 | TEST_F(ClientSocketPoolBaseTest, DelayedSocketBindingAtStall) { |
| 3128 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3129 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3130 | |
| 3131 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3132 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3133 | EXPECT_EQ( |
| 3134 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3135 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3136 | ClientSocketPool::RespectLimits::ENABLED, |
| 3137 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3138 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3139 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3140 | |
| 3141 | // No idle sockets, no pending jobs. |
| 3142 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3143 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3144 | |
| 3145 | // Create a second socket to the same host, but this one will wait. |
| 3146 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3147 | ClientSocketHandle handle2; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3148 | EXPECT_EQ( |
| 3149 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3150 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3151 | ClientSocketPool::RespectLimits::ENABLED, |
| 3152 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3153 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3154 | // No idle sockets, and one connecting job. |
| 3155 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3156 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3157 | |
| 3158 | // Return the first handle to the pool. This will initiate the delayed |
| 3159 | // binding. |
| 3160 | handle1.Reset(); |
| 3161 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3162 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3163 | |
| 3164 | // Still no idle sockets, still one pending connect job. |
| 3165 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3166 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3167 | |
| 3168 | // The second socket connected, even though it was a Waiting Job. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3169 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3170 | |
| 3171 | // And we can see there is still one job waiting. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3172 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3173 | |
| 3174 | // Finally, signal the waiting Connect. |
| 3175 | client_socket_factory_.SignalJobs(); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3176 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3177 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3178 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3179 | } |
| 3180 | |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3181 | // Cover the case where on an available socket slot, we have one pending |
| 3182 | // request that completes synchronously, thereby making the Group empty. |
| 3183 | TEST_F(ClientSocketPoolBaseTest, SynchronouslyProcessOnePendingRequest) { |
| 3184 | const int kUnlimitedSockets = 100; |
| 3185 | const int kOneSocketPerGroup = 1; |
| 3186 | CreatePool(kUnlimitedSockets, kOneSocketPerGroup); |
| 3187 | |
| 3188 | // Make the first request asynchronous fail. |
| 3189 | // This will free up a socket slot later. |
| 3190 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
| 3191 | |
| 3192 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3193 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3194 | EXPECT_EQ( |
| 3195 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3196 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3197 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3198 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3199 | pool_.get(), NetLogWithSource())); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3200 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3201 | |
| 3202 | // Make the second request synchronously fail. This should make the Group |
| 3203 | // empty. |
| 3204 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
| 3205 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3206 | TestCompletionCallback callback2; |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3207 | // It'll be ERR_IO_PENDING now, but the TestConnectJob will synchronously fail |
| 3208 | // when created. |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3209 | EXPECT_EQ( |
| 3210 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3211 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3212 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3213 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3214 | pool_.get(), NetLogWithSource())); |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3215 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3216 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3217 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3218 | EXPECT_THAT(callback1.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 3219 | EXPECT_THAT(callback2.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3220 | EXPECT_FALSE(pool_->HasGroup(TestGroupId("a"))); |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3221 | } |
| 3222 | |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3223 | TEST_F(ClientSocketPoolBaseTest, PreferUsedSocketToUnusedSocket) { |
| 3224 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 3225 | |
| 3226 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3227 | |
| 3228 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3229 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3230 | EXPECT_EQ( |
| 3231 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3232 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3233 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3234 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3235 | pool_.get(), NetLogWithSource())); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3236 | |
| 3237 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3238 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3239 | EXPECT_EQ( |
| 3240 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3241 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3242 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3243 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3244 | pool_.get(), NetLogWithSource())); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3245 | ClientSocketHandle handle3; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3246 | TestCompletionCallback callback3; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3247 | EXPECT_EQ( |
| 3248 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3249 | handle3.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3250 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3251 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3252 | pool_.get(), NetLogWithSource())); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3253 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3254 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3255 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
| 3256 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3257 | |
| 3258 | // Use the socket. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 3259 | EXPECT_EQ(1, handle1.socket()->Write(nullptr, 1, CompletionOnceCallback(), |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 3260 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 3261 | EXPECT_EQ(1, handle3.socket()->Write(nullptr, 1, CompletionOnceCallback(), |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 3262 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3263 | |
| 3264 | handle1.Reset(); |
| 3265 | handle2.Reset(); |
| 3266 | handle3.Reset(); |
| 3267 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3268 | EXPECT_EQ(OK, handle1.Init( |
| 3269 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
| 3270 | ClientSocketPool::RespectLimits::ENABLED, |
| 3271 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3272 | pool_.get(), NetLogWithSource())); |
| 3273 | EXPECT_EQ(OK, handle2.Init( |
| 3274 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
| 3275 | ClientSocketPool::RespectLimits::ENABLED, |
| 3276 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3277 | pool_.get(), NetLogWithSource())); |
| 3278 | EXPECT_EQ(OK, handle3.Init( |
| 3279 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
| 3280 | ClientSocketPool::RespectLimits::ENABLED, |
| 3281 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3282 | pool_.get(), NetLogWithSource())); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3283 | |
| 3284 | EXPECT_TRUE(handle1.socket()->WasEverUsed()); |
| 3285 | EXPECT_TRUE(handle2.socket()->WasEverUsed()); |
| 3286 | EXPECT_FALSE(handle3.socket()->WasEverUsed()); |
| 3287 | } |
| 3288 | |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3289 | TEST_F(ClientSocketPoolBaseTest, RequestSockets) { |
| 3290 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3291 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3292 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3293 | pool_->RequestSockets(TestGroupId("a"), ¶ms_, 2, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3294 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3295 | ASSERT_TRUE(pool_->HasGroup(TestGroupId("a"))); |
| 3296 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 3297 | EXPECT_EQ(2u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 3298 | EXPECT_EQ(2u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 3299 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3300 | |
| 3301 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3302 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3303 | EXPECT_EQ( |
| 3304 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3305 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3306 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3307 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3308 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3309 | |
| 3310 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3311 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3312 | EXPECT_EQ( |
| 3313 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3314 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3315 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3316 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3317 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3318 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3319 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 3320 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 3321 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 3322 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3323 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3324 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3325 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3326 | handle1.Reset(); |
| 3327 | handle2.Reset(); |
| 3328 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3329 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 3330 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 3331 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 3332 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3333 | } |
| 3334 | |
| 3335 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsWhenAlreadyHaveAConnectJob) { |
| 3336 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3337 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3338 | |
| 3339 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3340 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3341 | EXPECT_EQ( |
| 3342 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3343 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3344 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3345 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3346 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3347 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3348 | ASSERT_TRUE(pool_->HasGroup(TestGroupId("a"))); |
| 3349 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 3350 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 3351 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 3352 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3353 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3354 | pool_->RequestSockets(TestGroupId("a"), ¶ms_, 2, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3355 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3356 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 3357 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 3358 | EXPECT_EQ(1u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 3359 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3360 | |
| 3361 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3362 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3363 | EXPECT_EQ( |
| 3364 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3365 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3366 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3367 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3368 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3369 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3370 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 3371 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 3372 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 3373 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3374 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3375 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3376 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3377 | handle1.Reset(); |
| 3378 | handle2.Reset(); |
| 3379 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3380 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 3381 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 3382 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 3383 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3384 | } |
| 3385 | |
| 3386 | TEST_F(ClientSocketPoolBaseTest, |
| 3387 | RequestSocketsWhenAlreadyHaveMultipleConnectJob) { |
| 3388 | CreatePool(4, 4); |
| 3389 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3390 | |
| 3391 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3392 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3393 | EXPECT_EQ( |
| 3394 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3395 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3396 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3397 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3398 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3399 | |
| 3400 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3401 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3402 | EXPECT_EQ( |
| 3403 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3404 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3405 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3406 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3407 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3408 | |
| 3409 | ClientSocketHandle handle3; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3410 | TestCompletionCallback callback3; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3411 | EXPECT_EQ( |
| 3412 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3413 | handle3.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3414 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3415 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3416 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3417 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3418 | ASSERT_TRUE(pool_->HasGroup(TestGroupId("a"))); |
| 3419 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 3420 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 3421 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 3422 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3423 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3424 | pool_->RequestSockets(TestGroupId("a"), ¶ms_, 2, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3425 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3426 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 3427 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 3428 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 3429 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3430 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3431 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3432 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
| 3433 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3434 | handle1.Reset(); |
| 3435 | handle2.Reset(); |
| 3436 | handle3.Reset(); |
| 3437 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3438 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 3439 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 3440 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 3441 | EXPECT_EQ(3u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3442 | } |
| 3443 | |
| 3444 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsAtMaxSocketLimit) { |
| 3445 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 3446 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3447 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3448 | ASSERT_FALSE(pool_->HasGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3449 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3450 | pool_->RequestSockets(TestGroupId("a"), ¶ms_, kDefaultMaxSockets, |
| 3451 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3452 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3453 | ASSERT_TRUE(pool_->HasGroup(TestGroupId("a"))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3454 | EXPECT_EQ(kDefaultMaxSockets, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3455 | static_cast<int>(pool_->NumConnectJobsInGroup(TestGroupId("a")))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3456 | EXPECT_EQ(kDefaultMaxSockets, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3457 | static_cast<int>( |
| 3458 | pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a")))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3459 | EXPECT_EQ(kDefaultMaxSockets, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3460 | static_cast<int>( |
| 3461 | pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a")))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3462 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3463 | ASSERT_FALSE(pool_->HasGroup(TestGroupId("b"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3464 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3465 | pool_->RequestSockets(TestGroupId("b"), ¶ms_, kDefaultMaxSockets, |
| 3466 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3467 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3468 | ASSERT_FALSE(pool_->HasGroup(TestGroupId("b"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3469 | } |
| 3470 | |
| 3471 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsHitMaxSocketLimit) { |
| 3472 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 3473 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3474 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3475 | ASSERT_FALSE(pool_->HasGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3476 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3477 | pool_->RequestSockets(TestGroupId("a"), ¶ms_, kDefaultMaxSockets - 1, |
Charlie Harrison | 55ce608 | 2018-05-14 02:25:57 | [diff] [blame] | 3478 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3479 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3480 | ASSERT_TRUE(pool_->HasGroup(TestGroupId("a"))); |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3481 | EXPECT_EQ(kDefaultMaxSockets - 1, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3482 | static_cast<int>(pool_->NumConnectJobsInGroup(TestGroupId("a")))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3483 | EXPECT_EQ(kDefaultMaxSockets - 1, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3484 | static_cast<int>( |
| 3485 | pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a")))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3486 | EXPECT_EQ(kDefaultMaxSockets - 1, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3487 | static_cast<int>( |
| 3488 | pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a")))); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 3489 | EXPECT_FALSE(pool_->IsStalled()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3490 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3491 | ASSERT_FALSE(pool_->HasGroup(TestGroupId("b"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3492 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3493 | pool_->RequestSockets(TestGroupId("b"), ¶ms_, kDefaultMaxSockets, |
| 3494 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3495 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3496 | ASSERT_TRUE(pool_->HasGroup(TestGroupId("b"))); |
| 3497 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("b"))); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 3498 | EXPECT_FALSE(pool_->IsStalled()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3499 | } |
| 3500 | |
| 3501 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsCountIdleSockets) { |
| 3502 | CreatePool(4, 4); |
| 3503 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3504 | |
| 3505 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3506 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3507 | EXPECT_EQ( |
| 3508 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3509 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3510 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3511 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3512 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3513 | ASSERT_THAT(callback1.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3514 | handle1.Reset(); |
| 3515 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3516 | ASSERT_TRUE(pool_->HasGroup(TestGroupId("a"))); |
| 3517 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 3518 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 3519 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 3520 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3521 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3522 | pool_->RequestSockets(TestGroupId("a"), ¶ms_, 2, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3523 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3524 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 3525 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 3526 | EXPECT_EQ(1u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 3527 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3528 | } |
| 3529 | |
| 3530 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsCountActiveSockets) { |
| 3531 | CreatePool(4, 4); |
| 3532 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3533 | |
| 3534 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3535 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3536 | EXPECT_EQ( |
| 3537 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3538 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3539 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3540 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3541 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3542 | ASSERT_THAT(callback1.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3543 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3544 | ASSERT_TRUE(pool_->HasGroup(TestGroupId("a"))); |
| 3545 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 3546 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 3547 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 3548 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
| 3549 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroup(TestGroupId("a"))); |
[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("a"), ¶ms_, 2, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3552 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3553 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 3554 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 3555 | EXPECT_EQ(1u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 3556 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
| 3557 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3558 | } |
| 3559 | |
| 3560 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsSynchronous) { |
| 3561 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3562 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 3563 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3564 | pool_->RequestSockets(TestGroupId("a"), ¶ms_, kDefaultMaxSocketsPerGroup, |
Charlie Harrison | 55ce608 | 2018-05-14 02:25:57 | [diff] [blame] | 3565 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3566 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3567 | ASSERT_TRUE(pool_->HasGroup(TestGroupId("a"))); |
| 3568 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 3569 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 3570 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3571 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3572 | static_cast<int>(pool_->IdleSocketCountInGroup(TestGroupId("a")))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3573 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3574 | pool_->RequestSockets(TestGroupId("b"), ¶ms_, kDefaultMaxSocketsPerGroup, |
Charlie Harrison | 55ce608 | 2018-05-14 02:25:57 | [diff] [blame] | 3575 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3576 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3577 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroup(TestGroupId("b"))); |
| 3578 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("b"))); |
| 3579 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("b"))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3580 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3581 | static_cast<int>(pool_->IdleSocketCountInGroup(TestGroupId("b")))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3582 | } |
| 3583 | |
[email protected] | 3c819f52 | 2010-12-02 02:03:12 | [diff] [blame] | 3584 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsSynchronousError) { |
| 3585 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3586 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
| 3587 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3588 | pool_->RequestSockets(TestGroupId("a"), ¶ms_, kDefaultMaxSocketsPerGroup, |
Charlie Harrison | 55ce608 | 2018-05-14 02:25:57 | [diff] [blame] | 3589 | NetLogWithSource()); |
[email protected] | 3c819f52 | 2010-12-02 02:03:12 | [diff] [blame] | 3590 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3591 | ASSERT_FALSE(pool_->HasGroup(TestGroupId("a"))); |
[email protected] | fd2e53e | 2011-01-14 20:40:52 | [diff] [blame] | 3592 | |
| 3593 | connect_job_factory_->set_job_type( |
| 3594 | TestConnectJob::kMockAdditionalErrorStateJob); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3595 | pool_->RequestSockets(TestGroupId("a"), ¶ms_, kDefaultMaxSocketsPerGroup, |
Charlie Harrison | 55ce608 | 2018-05-14 02:25:57 | [diff] [blame] | 3596 | NetLogWithSource()); |
[email protected] | fd2e53e | 2011-01-14 20:40:52 | [diff] [blame] | 3597 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3598 | ASSERT_FALSE(pool_->HasGroup(TestGroupId("a"))); |
[email protected] | 3c819f52 | 2010-12-02 02:03:12 | [diff] [blame] | 3599 | } |
| 3600 | |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3601 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsMultipleTimesDoesNothing) { |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3602 | CreatePool(4, 4); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3603 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3604 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3605 | pool_->RequestSockets(TestGroupId("a"), ¶ms_, 2, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3606 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3607 | ASSERT_TRUE(pool_->HasGroup(TestGroupId("a"))); |
| 3608 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 3609 | EXPECT_EQ(2u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 3610 | EXPECT_EQ(2u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 3611 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroup(TestGroupId("a"))); |
| 3612 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3613 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3614 | pool_->RequestSockets(TestGroupId("a"), ¶ms_, 2, NetLogWithSource()); |
| 3615 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 3616 | EXPECT_EQ(2u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 3617 | EXPECT_EQ(2u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 3618 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroup(TestGroupId("a"))); |
| 3619 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3620 | |
| 3621 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3622 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3623 | EXPECT_EQ( |
| 3624 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3625 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3626 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3627 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3628 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3629 | |
| 3630 | client_socket_factory_.SignalJob(0); |
| 3631 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3632 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3633 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 3634 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 3635 | EXPECT_EQ(1u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 3636 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroup(TestGroupId("a"))); |
| 3637 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3638 | |
| 3639 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3640 | TestCompletionCallback callback2; |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3641 | EXPECT_EQ( |
| 3642 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3643 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3644 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3645 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3646 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3647 | client_socket_factory_.SignalJob(0); |
| 3648 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3649 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3650 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 3651 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 3652 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 3653 | EXPECT_EQ(2, pool_->NumActiveSocketsInGroup(TestGroupId("a"))); |
| 3654 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3655 | |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3656 | handle1.Reset(); |
| 3657 | handle2.Reset(); |
| 3658 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3659 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 3660 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 3661 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 3662 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroup(TestGroupId("a"))); |
| 3663 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3664 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3665 | pool_->RequestSockets(TestGroupId("a"), ¶ms_, 2, NetLogWithSource()); |
| 3666 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 3667 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 3668 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 3669 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroup(TestGroupId("a"))); |
| 3670 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3671 | } |
| 3672 | |
| 3673 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsDifferentNumSockets) { |
| 3674 | CreatePool(4, 4); |
| 3675 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3676 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3677 | pool_->RequestSockets(TestGroupId("a"), ¶ms_, 1, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3678 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3679 | ASSERT_TRUE(pool_->HasGroup(TestGroupId("a"))); |
| 3680 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 3681 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 3682 | EXPECT_EQ(1u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 3683 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3684 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3685 | pool_->RequestSockets(TestGroupId("a"), ¶ms_, 2, NetLogWithSource()); |
| 3686 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 3687 | EXPECT_EQ(2u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 3688 | EXPECT_EQ(2u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 3689 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[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_, 3, NetLogWithSource()); |
| 3692 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 3693 | EXPECT_EQ(3u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 3694 | EXPECT_EQ(3u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 3695 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3696 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3697 | pool_->RequestSockets(TestGroupId("a"), ¶ms_, 1, NetLogWithSource()); |
| 3698 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 3699 | EXPECT_EQ(3u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 3700 | EXPECT_EQ(3u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 3701 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3702 | } |
| 3703 | |
| 3704 | TEST_F(ClientSocketPoolBaseTest, PreconnectJobsTakenByNormalRequests) { |
| 3705 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3706 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3707 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3708 | pool_->RequestSockets(TestGroupId("a"), ¶ms_, 1, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3709 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3710 | ASSERT_TRUE(pool_->HasGroup(TestGroupId("a"))); |
| 3711 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 3712 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 3713 | EXPECT_EQ(1u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 3714 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3715 | |
| 3716 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3717 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3718 | EXPECT_EQ( |
| 3719 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3720 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3721 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3722 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3723 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3724 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3725 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 3726 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 3727 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 3728 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3729 | |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3730 | client_socket_factory_.SignalJobs(); |
| 3731 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3732 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3733 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 3734 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 3735 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 3736 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
| 3737 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3738 | |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 3739 | // Make sure if a preconnected socket is not fully connected when a request |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 3740 | // starts, it has a connect start time. |
| 3741 | TestLoadTimingInfoConnectedNotReused(handle1); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3742 | handle1.Reset(); |
| 3743 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3744 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3745 | } |
| 3746 | |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 3747 | // Checks that fully connected preconnect jobs have no connect times, and are |
| 3748 | // marked as reused. |
| 3749 | TEST_F(ClientSocketPoolBaseTest, ConnectedPreconnectJobsHaveNoConnectTimes) { |
| 3750 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3751 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3752 | pool_->RequestSockets(TestGroupId("a"), ¶ms_, 1, NetLogWithSource()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 3753 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3754 | ASSERT_TRUE(pool_->HasGroup(TestGroupId("a"))); |
| 3755 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 3756 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 3757 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 3758 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 3759 | |
| 3760 | ClientSocketHandle handle; |
| 3761 | TestCompletionCallback callback; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3762 | EXPECT_EQ(OK, handle.Init( |
| 3763 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
| 3764 | ClientSocketPool::RespectLimits::ENABLED, |
| 3765 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3766 | pool_.get(), NetLogWithSource())); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 3767 | |
| 3768 | // Make sure the idle socket was used. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3769 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 3770 | |
| 3771 | TestLoadTimingInfoConnectedReused(handle); |
| 3772 | handle.Reset(); |
| 3773 | TestLoadTimingInfoNotConnected(handle); |
| 3774 | } |
| 3775 | |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3776 | // http://crbug.com/64940 regression test. |
| 3777 | TEST_F(ClientSocketPoolBaseTest, PreconnectClosesIdleSocketRemovesGroup) { |
| 3778 | const int kMaxTotalSockets = 3; |
| 3779 | const int kMaxSocketsPerGroup = 2; |
| 3780 | CreatePool(kMaxTotalSockets, kMaxSocketsPerGroup); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3781 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3782 | |
| 3783 | // Note that group name ordering matters here. "a" comes before "b", so |
| 3784 | // CloseOneIdleSocket() will try to close "a"'s idle socket. |
| 3785 | |
| 3786 | // Set up one idle socket in "a". |
| 3787 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3788 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3789 | EXPECT_EQ( |
| 3790 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3791 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3792 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3793 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3794 | pool_.get(), NetLogWithSource())); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3795 | ASSERT_TRUE(pool_->HasGroup(TestGroupId("a"))); |
| 3796 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 3797 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 3798 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 3799 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3800 | |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3801 | client_socket_factory_.SignalJobs(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3802 | ASSERT_THAT(callback1.WaitForResult(), IsOk()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3803 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 3804 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 3805 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 3806 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3807 | |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3808 | handle1.Reset(); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3809 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3810 | |
| 3811 | // Set up two active sockets in "b". |
| 3812 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3813 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3814 | EXPECT_EQ( |
| 3815 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3816 | handle1.Init(TestGroupId("b"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3817 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3818 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3819 | pool_.get(), NetLogWithSource())); |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3820 | EXPECT_EQ( |
| 3821 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3822 | handle2.Init(TestGroupId("b"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3823 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3824 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3825 | pool_.get(), NetLogWithSource())); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3826 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3827 | ASSERT_TRUE(pool_->HasGroup(TestGroupId("b"))); |
| 3828 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroup(TestGroupId("b"))); |
| 3829 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("b"))); |
| 3830 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("b"))); |
| 3831 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3832 | |
| 3833 | client_socket_factory_.SignalJobs(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3834 | ASSERT_THAT(callback1.WaitForResult(), IsOk()); |
| 3835 | ASSERT_THAT(callback2.WaitForResult(), IsOk()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3836 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
| 3837 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("b"))); |
| 3838 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("b"))); |
| 3839 | EXPECT_EQ(2, pool_->NumActiveSocketsInGroup(TestGroupId("b"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3840 | |
| 3841 | // Now we have 1 idle socket in "a" and 2 active sockets in "b". This means |
| 3842 | // we've maxed out on sockets, since we set |kMaxTotalSockets| to 3. |
| 3843 | // Requesting 2 preconnected sockets for "a" should fail to allocate any more |
| 3844 | // sockets for "a", and "b" should still have 2 active sockets. |
| 3845 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3846 | pool_->RequestSockets(TestGroupId("a"), ¶ms_, 2, NetLogWithSource()); |
| 3847 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 3848 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 3849 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 3850 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
| 3851 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroup(TestGroupId("a"))); |
| 3852 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroup(TestGroupId("b"))); |
| 3853 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("b"))); |
| 3854 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("b"))); |
| 3855 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
| 3856 | EXPECT_EQ(2, pool_->NumActiveSocketsInGroup(TestGroupId("b"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3857 | |
| 3858 | // Now release the 2 active sockets for "b". This will give us 1 idle socket |
| 3859 | // in "a" and 2 idle sockets in "b". Requesting 2 preconnected sockets for |
| 3860 | // "a" should result in closing 1 for "b". |
| 3861 | handle1.Reset(); |
| 3862 | handle2.Reset(); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3863 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
| 3864 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroup(TestGroupId("b"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3865 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3866 | pool_->RequestSockets(TestGroupId("a"), ¶ms_, 2, NetLogWithSource()); |
| 3867 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 3868 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 3869 | EXPECT_EQ(1u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 3870 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
| 3871 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroup(TestGroupId("a"))); |
| 3872 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroup(TestGroupId("b"))); |
| 3873 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("b"))); |
| 3874 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("b"))); |
| 3875 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
| 3876 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroup(TestGroupId("b"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3877 | } |
| 3878 | |
[email protected] | b7b8be4 | 2011-07-12 12:46:41 | [diff] [blame] | 3879 | TEST_F(ClientSocketPoolBaseTest, PreconnectWithoutBackupJob) { |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 3880 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3881 | pool_->EnableConnectBackupJobs(); |
| 3882 | |
| 3883 | // Make the ConnectJob hang until it times out, shorten the timeout. |
| 3884 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3885 | connect_job_factory_->set_timeout_duration( |
| 3886 | base::TimeDelta::FromMilliseconds(500)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3887 | pool_->RequestSockets(TestGroupId("a"), ¶ms_, 1, NetLogWithSource()); |
| 3888 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 3889 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 3890 | EXPECT_EQ(1u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 3891 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 3892 | |
[email protected] | b7b8be4 | 2011-07-12 12:46:41 | [diff] [blame] | 3893 | // Verify the backup timer doesn't create a backup job, by making |
| 3894 | // the backup job a pending job instead of a waiting job, so it |
| 3895 | // *would* complete if it were created. |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 3896 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 3897 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
Gabriel Charette | ea91801 | 2018-05-16 11:53:44 | [diff] [blame] | 3898 | FROM_HERE, base::RunLoop::QuitCurrentWhenIdleClosureDeprecated(), |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 3899 | base::TimeDelta::FromSeconds(1)); |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3900 | base::RunLoop().Run(); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3901 | EXPECT_FALSE(pool_->HasGroup(TestGroupId("a"))); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 3902 | } |
| 3903 | |
[email protected] | b7b8be4 | 2011-07-12 12:46:41 | [diff] [blame] | 3904 | TEST_F(ClientSocketPoolBaseTest, PreconnectWithBackupJob) { |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 3905 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3906 | pool_->EnableConnectBackupJobs(); |
| 3907 | |
| 3908 | // Make the ConnectJob hang forever. |
| 3909 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3910 | pool_->RequestSockets(TestGroupId("a"), ¶ms_, 1, NetLogWithSource()); |
| 3911 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 3912 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 3913 | EXPECT_EQ(1u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 3914 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3915 | base::RunLoop().RunUntilIdle(); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 3916 | |
| 3917 | // Make the backup job be a pending job, so it completes normally. |
| 3918 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3919 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3920 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3921 | EXPECT_EQ( |
| 3922 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3923 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3924 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 3925 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 3926 | NetLogWithSource())); |
[email protected] | b7b8be4 | 2011-07-12 12:46:41 | [diff] [blame] | 3927 | // Timer has started, but the backup connect job shouldn't be created yet. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3928 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 3929 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 3930 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 3931 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
| 3932 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroup(TestGroupId("a"))); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3933 | ASSERT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 3934 | |
| 3935 | // The hung connect job should still be there, but everything else should be |
| 3936 | // complete. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3937 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 3938 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 3939 | EXPECT_EQ(1u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 3940 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
| 3941 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroup(TestGroupId("a"))); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 3942 | } |
| 3943 | |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 3944 | // Tests that a preconnect that starts out with unread data can still be used. |
| 3945 | // http://crbug.com/334467 |
| 3946 | TEST_F(ClientSocketPoolBaseTest, PreconnectWithUnreadData) { |
| 3947 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3948 | connect_job_factory_->set_job_type(TestConnectJob::kMockUnreadDataJob); |
| 3949 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3950 | pool_->RequestSockets(TestGroupId("a"), ¶ms_, 1, NetLogWithSource()); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 3951 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3952 | ASSERT_TRUE(pool_->HasGroup(TestGroupId("a"))); |
| 3953 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 3954 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 3955 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 3956 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 3957 | |
| 3958 | // Fail future jobs to be sure that handle receives the preconnected socket |
| 3959 | // rather than closing it and making a new one. |
| 3960 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
| 3961 | ClientSocketHandle handle; |
| 3962 | TestCompletionCallback callback; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3963 | EXPECT_EQ(OK, handle.Init( |
| 3964 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
| 3965 | ClientSocketPool::RespectLimits::ENABLED, |
| 3966 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3967 | pool_.get(), NetLogWithSource())); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 3968 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3969 | ASSERT_TRUE(pool_->HasGroup(TestGroupId("a"))); |
| 3970 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 3971 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 3972 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 3973 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
| 3974 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroup(TestGroupId("a"))); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 3975 | |
| 3976 | // Drain the pending read. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 3977 | EXPECT_EQ(1, handle.socket()->Read(nullptr, 1, CompletionOnceCallback())); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 3978 | |
| 3979 | TestLoadTimingInfoConnectedReused(handle); |
| 3980 | handle.Reset(); |
| 3981 | |
| 3982 | // The socket should be usable now that it's idle again. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3983 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 3984 | } |
| 3985 | |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3986 | TEST_F(ClientSocketPoolBaseTest, RequestGetsAssignedJob) { |
| 3987 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3988 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3989 | |
| 3990 | ClientSocketHandle handle1; |
| 3991 | TestCompletionCallback callback1; |
| 3992 | EXPECT_EQ( |
| 3993 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3994 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3995 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3996 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3997 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3998 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3999 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 4000 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 4001 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 4002 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4003 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4004 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4005 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4006 | } |
| 4007 | |
| 4008 | TEST_F(ClientSocketPoolBaseTest, MultipleRequestsGetAssignedJobs) { |
| 4009 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4010 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4011 | |
| 4012 | ClientSocketHandle handle1; |
| 4013 | TestCompletionCallback callback1; |
| 4014 | EXPECT_EQ( |
| 4015 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4016 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4017 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4018 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4019 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4020 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4021 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 4022 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 4023 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 4024 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4025 | |
| 4026 | ClientSocketHandle handle2; |
| 4027 | TestCompletionCallback callback2; |
| 4028 | EXPECT_EQ( |
| 4029 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4030 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4031 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4032 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4033 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4034 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4035 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 4036 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 4037 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 4038 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4039 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4040 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4041 | &handle1)); |
| 4042 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4043 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4044 | |
| 4045 | // One job completes. The other request should still have its job. |
| 4046 | client_socket_factory_.SignalJob(0); |
| 4047 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 4048 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4049 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 4050 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 4051 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 4052 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroup(TestGroupId("a"))); |
| 4053 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4054 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4055 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4056 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4057 | } |
| 4058 | |
| 4059 | TEST_F(ClientSocketPoolBaseTest, PreconnectJobGetsAssignedToRequest) { |
| 4060 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4061 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4062 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4063 | pool_->RequestSockets(TestGroupId("a"), ¶ms_, 1, NetLogWithSource()); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4064 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4065 | ASSERT_TRUE(pool_->HasGroup(TestGroupId("a"))); |
| 4066 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 4067 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 4068 | EXPECT_EQ(1u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 4069 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4070 | |
| 4071 | ClientSocketHandle handle1; |
| 4072 | TestCompletionCallback callback1; |
| 4073 | EXPECT_EQ( |
| 4074 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4075 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4076 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4077 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4078 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4079 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4080 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 4081 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 4082 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 4083 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
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_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4086 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4087 | } |
| 4088 | |
| 4089 | TEST_F(ClientSocketPoolBaseTest, HigherPriorityRequestStealsJob) { |
| 4090 | CreatePool(kDefaultMaxSockets, 1); |
| 4091 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4092 | |
| 4093 | ClientSocketHandle handle1; |
| 4094 | TestCompletionCallback callback1; |
| 4095 | EXPECT_EQ( |
| 4096 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4097 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4098 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4099 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4100 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4101 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4102 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 4103 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 4104 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 4105 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
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_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4108 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4109 | |
| 4110 | // Insert a higher priority request |
| 4111 | ClientSocketHandle handle2; |
| 4112 | TestCompletionCallback callback2; |
| 4113 | EXPECT_EQ( |
| 4114 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4115 | handle2.Init(TestGroupId("a"), params_, HIGHEST, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4116 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4117 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4118 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4119 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4120 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 4121 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 4122 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 4123 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4124 | |
| 4125 | // The highest priority request should steal the job from the default priority |
| 4126 | // request. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4127 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4128 | &handle2)); |
| 4129 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4130 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4131 | } |
| 4132 | |
| 4133 | TEST_F(ClientSocketPoolBaseTest, RequestStealsJobFromLowestRequestWithJob) { |
| 4134 | CreatePool(3, 3); |
| 4135 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4136 | |
| 4137 | ClientSocketHandle handle_lowest; |
| 4138 | TestCompletionCallback callback_lowest; |
| 4139 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4140 | handle_lowest.Init(TestGroupId("a"), params_, LOWEST, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4141 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4142 | callback_lowest.callback(), |
| 4143 | ClientSocketPool::ProxyAuthCallback(), |
| 4144 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4145 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4146 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 4147 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 4148 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 4149 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4150 | |
| 4151 | ClientSocketHandle handle_highest; |
| 4152 | TestCompletionCallback callback_highest; |
| 4153 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4154 | handle_highest.Init(TestGroupId("a"), params_, HIGHEST, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4155 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4156 | callback_highest.callback(), |
| 4157 | ClientSocketPool::ProxyAuthCallback(), |
| 4158 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4159 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4160 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 4161 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 4162 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 4163 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4164 | |
| 4165 | ClientSocketHandle handle_low; |
| 4166 | TestCompletionCallback callback_low; |
| 4167 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4168 | handle_low.Init(TestGroupId("a"), params_, LOW, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4169 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4170 | callback_low.callback(), |
| 4171 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4172 | NetLogWithSource())); |
| 4173 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4174 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 4175 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 4176 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 4177 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4178 | |
| 4179 | ClientSocketHandle handle_lowest2; |
| 4180 | TestCompletionCallback callback_lowest2; |
| 4181 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4182 | handle_lowest2.Init(TestGroupId("a"), params_, LOWEST, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4183 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4184 | callback_lowest2.callback(), |
| 4185 | 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(3u, 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 | |
| 4193 | // The top three requests in the queue should have jobs. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4194 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4195 | &handle_highest)); |
| 4196 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4197 | &handle_low)); |
| 4198 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4199 | &handle_lowest)); |
| 4200 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting( |
| 4201 | TestGroupId("a"), &handle_lowest2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4202 | |
| 4203 | // Add another request with medium priority. It should steal the job from the |
| 4204 | // lowest priority request with a job. |
| 4205 | ClientSocketHandle handle_medium; |
| 4206 | TestCompletionCallback callback_medium; |
| 4207 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4208 | handle_medium.Init(TestGroupId("a"), params_, MEDIUM, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4209 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4210 | callback_medium.callback(), |
| 4211 | ClientSocketPool::ProxyAuthCallback(), |
| 4212 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4213 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4214 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 4215 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 4216 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 4217 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
| 4218 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4219 | &handle_highest)); |
| 4220 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4221 | &handle_medium)); |
| 4222 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4223 | &handle_low)); |
| 4224 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4225 | &handle_lowest)); |
| 4226 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting( |
| 4227 | TestGroupId("a"), &handle_lowest2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4228 | } |
| 4229 | |
| 4230 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeRequestStealsJob) { |
| 4231 | CreatePool(kDefaultMaxSockets, 1); |
| 4232 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4233 | |
| 4234 | ClientSocketHandle handle1; |
| 4235 | TestCompletionCallback callback1; |
| 4236 | EXPECT_EQ( |
| 4237 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4238 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4239 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4240 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4241 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4242 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4243 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 4244 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 4245 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 4246 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4247 | |
| 4248 | ClientSocketHandle handle2; |
| 4249 | TestCompletionCallback callback2; |
| 4250 | EXPECT_EQ( |
| 4251 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4252 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4253 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4254 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4255 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4256 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4257 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 4258 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 4259 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 4260 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4261 | |
| 4262 | // 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] | 4263 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4264 | &handle1)); |
| 4265 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4266 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4267 | |
| 4268 | // Reprioritizing the second request places it above the first, and it steals |
| 4269 | // the job from the first request. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4270 | pool_->SetPriority(TestGroupId("a"), &handle2, HIGHEST); |
| 4271 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4272 | &handle2)); |
| 4273 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4274 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4275 | } |
| 4276 | |
| 4277 | TEST_F(ClientSocketPoolBaseTest, CancelRequestReassignsJob) { |
| 4278 | CreatePool(kDefaultMaxSockets, 1); |
| 4279 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4280 | |
| 4281 | ClientSocketHandle handle1; |
| 4282 | TestCompletionCallback callback1; |
| 4283 | EXPECT_EQ( |
| 4284 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4285 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4286 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4287 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4288 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4289 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4290 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 4291 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 4292 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 4293 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4294 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4295 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4296 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4297 | |
| 4298 | ClientSocketHandle handle2; |
| 4299 | TestCompletionCallback callback2; |
| 4300 | EXPECT_EQ( |
| 4301 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4302 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4303 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4304 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4305 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4306 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4307 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 4308 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 4309 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 4310 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4311 | |
| 4312 | // The second request doesn't get a job because we are the limit. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4313 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4314 | &handle1)); |
| 4315 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4316 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4317 | |
| 4318 | // The second request should get a job upon cancelling the first request. |
| 4319 | handle1.Reset(); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4320 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 4321 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 4322 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 4323 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4324 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4325 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4326 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4327 | } |
| 4328 | |
| 4329 | TEST_F(ClientSocketPoolBaseTest, JobCompletionReassignsJob) { |
| 4330 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4331 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4332 | |
| 4333 | ClientSocketHandle handle1; |
| 4334 | TestCompletionCallback callback1; |
| 4335 | EXPECT_EQ( |
| 4336 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4337 | handle1.Init(TestGroupId("a"), params_, HIGHEST, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4338 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4339 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4340 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4341 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4342 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 4343 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 4344 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 4345 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4346 | |
| 4347 | ClientSocketHandle handle2; |
| 4348 | TestCompletionCallback callback2; |
| 4349 | EXPECT_EQ( |
| 4350 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4351 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4352 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4353 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4354 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4355 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4356 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 4357 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 4358 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 4359 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4360 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4361 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4362 | &handle1)); |
| 4363 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4364 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4365 | |
| 4366 | // The lower-priority job completes first. The higher-priority request should |
| 4367 | // get the socket, and the lower-priority request should get the remaining |
| 4368 | // job. |
| 4369 | client_socket_factory_.SignalJob(1); |
| 4370 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4371 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 4372 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroup(TestGroupId("a"))); |
| 4373 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroup(TestGroupId("a"))); |
| 4374 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroup(TestGroupId("a"))); |
| 4375 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4376 | EXPECT_TRUE(handle1.socket()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4377 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4378 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4379 | } |
| 4380 | |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 4381 | class MockLayeredPool : public HigherLayeredPool { |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4382 | public: |
| 4383 | MockLayeredPool(TestClientSocketPool* pool, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4384 | const ClientSocketPool::GroupId& group_id) |
| 4385 | : pool_(pool), group_id_(group_id), can_release_connection_(true) { |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 4386 | pool_->AddHigherLayeredPool(this); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4387 | } |
| 4388 | |
Daniel Cheng | 4496d082 | 2018-04-26 21:52:15 | [diff] [blame] | 4389 | ~MockLayeredPool() override { pool_->RemoveHigherLayeredPool(this); } |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4390 | |
| 4391 | int RequestSocket(TestClientSocketPool* pool) { |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4392 | scoped_refptr<TestSocketParams> params(new TestSocketParams()); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4393 | return handle_.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4394 | group_id_, params, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4395 | ClientSocketPool::RespectLimits::ENABLED, callback_.callback(), |
| 4396 | ClientSocketPool::ProxyAuthCallback(), pool, NetLogWithSource()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4397 | } |
| 4398 | |
| 4399 | int RequestSocketWithoutLimits(TestClientSocketPool* pool) { |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4400 | scoped_refptr<TestSocketParams> params(new TestSocketParams()); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4401 | return handle_.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4402 | group_id_, params, MAXIMUM_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4403 | ClientSocketPool::RespectLimits::DISABLED, callback_.callback(), |
| 4404 | ClientSocketPool::ProxyAuthCallback(), pool, NetLogWithSource()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4405 | } |
| 4406 | |
| 4407 | bool ReleaseOneConnection() { |
| 4408 | if (!handle_.is_initialized() || !can_release_connection_) { |
| 4409 | return false; |
| 4410 | } |
| 4411 | handle_.socket()->Disconnect(); |
| 4412 | handle_.Reset(); |
| 4413 | return true; |
| 4414 | } |
| 4415 | |
| 4416 | void set_can_release_connection(bool can_release_connection) { |
| 4417 | can_release_connection_ = can_release_connection; |
| 4418 | } |
| 4419 | |
| 4420 | MOCK_METHOD0(CloseOneIdleConnection, bool()); |
| 4421 | |
| 4422 | private: |
| 4423 | TestClientSocketPool* const pool_; |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4424 | ClientSocketHandle handle_; |
| 4425 | TestCompletionCallback callback_; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4426 | const ClientSocketPool::GroupId group_id_; |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4427 | bool can_release_connection_; |
| 4428 | }; |
| 4429 | |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4430 | // Tests the basic case of closing an idle socket in a higher layered pool when |
| 4431 | // a new request is issued and the lower layer pool is stalled. |
| 4432 | TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketsHeldByLayeredPoolWhenNeeded) { |
| 4433 | CreatePool(1, 1); |
| 4434 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 4435 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4436 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("foo")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4437 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4438 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4439 | .WillOnce(Invoke(&mock_layered_pool, |
| 4440 | &MockLayeredPool::ReleaseOneConnection)); |
| 4441 | ClientSocketHandle handle; |
| 4442 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4443 | EXPECT_EQ( |
| 4444 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4445 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4446 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 4447 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4448 | NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4449 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4450 | } |
| 4451 | |
Matt Menke | 83367864 | 2019-03-05 22:05:51 | [diff] [blame] | 4452 | // Tests the case that trying to close an idle socket in a higher layered pool |
| 4453 | // fails. |
| 4454 | TEST_F(ClientSocketPoolBaseTest, |
| 4455 | CloseIdleSocketsHeldByLayeredPoolWhenNeededFails) { |
| 4456 | CreatePool(1, 1); |
| 4457 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 4458 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4459 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("foo")); |
Matt Menke | 83367864 | 2019-03-05 22:05:51 | [diff] [blame] | 4460 | mock_layered_pool.set_can_release_connection(false); |
| 4461 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
| 4462 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4463 | .WillOnce(Invoke(&mock_layered_pool, |
| 4464 | &MockLayeredPool::ReleaseOneConnection)); |
| 4465 | ClientSocketHandle handle; |
| 4466 | TestCompletionCallback callback; |
| 4467 | EXPECT_EQ( |
| 4468 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4469 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 83367864 | 2019-03-05 22:05:51 | [diff] [blame] | 4470 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 4471 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4472 | NetLogWithSource())); |
| 4473 | base::RunLoop().RunUntilIdle(); |
| 4474 | EXPECT_FALSE(callback.have_result()); |
| 4475 | } |
| 4476 | |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4477 | // Same as above, but the idle socket is in the same group as the stalled |
| 4478 | // socket, and closes the only other request in its group when closing requests |
| 4479 | // in higher layered pools. This generally shouldn't happen, but it may be |
| 4480 | // possible if a higher level pool issues a request and the request is |
| 4481 | // subsequently cancelled. Even if it's not possible, best not to crash. |
| 4482 | TEST_F(ClientSocketPoolBaseTest, |
| 4483 | CloseIdleSocketsHeldByLayeredPoolWhenNeededSameGroup) { |
| 4484 | CreatePool(2, 2); |
| 4485 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 4486 | |
| 4487 | // Need a socket in another group for the pool to be stalled (If a group |
| 4488 | // has the maximum number of connections already, it's not stalled). |
| 4489 | ClientSocketHandle handle1; |
| 4490 | TestCompletionCallback callback1; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4491 | EXPECT_EQ(OK, handle1.Init( |
| 4492 | TestGroupId("group1"), params_, DEFAULT_PRIORITY, |
| 4493 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4494 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4495 | pool_.get(), NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4496 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4497 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("group2")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4498 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4499 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4500 | .WillOnce(Invoke(&mock_layered_pool, |
| 4501 | &MockLayeredPool::ReleaseOneConnection)); |
| 4502 | ClientSocketHandle handle; |
| 4503 | TestCompletionCallback callback2; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4504 | EXPECT_EQ( |
| 4505 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4506 | handle.Init(TestGroupId("group2"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4507 | ClientSocketPool::RespectLimits::ENABLED, |
| 4508 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4509 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4510 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4511 | } |
| 4512 | |
| 4513 | // Tests the case when an idle socket can be closed when a new request is |
| 4514 | // issued, and the new request belongs to a group that was previously stalled. |
| 4515 | TEST_F(ClientSocketPoolBaseTest, |
| 4516 | CloseIdleSocketsHeldByLayeredPoolInSameGroupWhenNeeded) { |
| 4517 | CreatePool(2, 2); |
| 4518 | std::list<TestConnectJob::JobType> job_types; |
| 4519 | job_types.push_back(TestConnectJob::kMockJob); |
| 4520 | job_types.push_back(TestConnectJob::kMockJob); |
| 4521 | job_types.push_back(TestConnectJob::kMockJob); |
| 4522 | job_types.push_back(TestConnectJob::kMockJob); |
| 4523 | connect_job_factory_->set_job_types(&job_types); |
| 4524 | |
| 4525 | ClientSocketHandle handle1; |
| 4526 | TestCompletionCallback callback1; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4527 | EXPECT_EQ(OK, handle1.Init( |
| 4528 | TestGroupId("group1"), params_, DEFAULT_PRIORITY, |
| 4529 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4530 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4531 | pool_.get(), NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4532 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4533 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("group2")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4534 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4535 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4536 | .WillRepeatedly(Invoke(&mock_layered_pool, |
| 4537 | &MockLayeredPool::ReleaseOneConnection)); |
| 4538 | mock_layered_pool.set_can_release_connection(false); |
| 4539 | |
| 4540 | // The third request is made when the socket pool is in a stalled state. |
| 4541 | ClientSocketHandle handle3; |
| 4542 | TestCompletionCallback callback3; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4543 | EXPECT_EQ( |
| 4544 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4545 | handle3.Init(TestGroupId("group3"), params_, DEFAULT_PRIORITY, |
| 4546 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4547 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4548 | pool_.get(), NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4549 | |
| 4550 | base::RunLoop().RunUntilIdle(); |
| 4551 | EXPECT_FALSE(callback3.have_result()); |
| 4552 | |
| 4553 | // The fourth request is made when the pool is no longer stalled. The third |
| 4554 | // request should be serviced first, since it was issued first and has the |
| 4555 | // same priority. |
| 4556 | mock_layered_pool.set_can_release_connection(true); |
| 4557 | ClientSocketHandle handle4; |
| 4558 | TestCompletionCallback callback4; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4559 | EXPECT_EQ( |
| 4560 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4561 | handle4.Init(TestGroupId("group3"), params_, DEFAULT_PRIORITY, |
| 4562 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4563 | callback4.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4564 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4565 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4566 | EXPECT_FALSE(callback4.have_result()); |
| 4567 | |
| 4568 | // Closing a handle should free up another socket slot. |
| 4569 | handle1.Reset(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4570 | EXPECT_THAT(callback4.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4571 | } |
| 4572 | |
| 4573 | // Tests the case when an idle socket can be closed when a new request is |
| 4574 | // issued, and the new request belongs to a group that was previously stalled. |
| 4575 | // |
| 4576 | // The two differences from the above test are that the stalled requests are not |
| 4577 | // in the same group as the layered pool's request, and the the fourth request |
| 4578 | // has a higher priority than the third one, so gets a socket first. |
| 4579 | TEST_F(ClientSocketPoolBaseTest, |
| 4580 | CloseIdleSocketsHeldByLayeredPoolInSameGroupWhenNeeded2) { |
| 4581 | CreatePool(2, 2); |
| 4582 | std::list<TestConnectJob::JobType> job_types; |
| 4583 | job_types.push_back(TestConnectJob::kMockJob); |
| 4584 | job_types.push_back(TestConnectJob::kMockJob); |
| 4585 | job_types.push_back(TestConnectJob::kMockJob); |
| 4586 | job_types.push_back(TestConnectJob::kMockJob); |
| 4587 | connect_job_factory_->set_job_types(&job_types); |
| 4588 | |
| 4589 | ClientSocketHandle handle1; |
| 4590 | TestCompletionCallback callback1; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4591 | EXPECT_EQ(OK, handle1.Init( |
| 4592 | TestGroupId("group1"), params_, DEFAULT_PRIORITY, |
| 4593 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4594 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4595 | pool_.get(), NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4596 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4597 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("group2")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4598 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4599 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4600 | .WillRepeatedly(Invoke(&mock_layered_pool, |
| 4601 | &MockLayeredPool::ReleaseOneConnection)); |
| 4602 | mock_layered_pool.set_can_release_connection(false); |
| 4603 | |
| 4604 | // The third request is made when the socket pool is in a stalled state. |
| 4605 | ClientSocketHandle handle3; |
| 4606 | TestCompletionCallback callback3; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4607 | EXPECT_EQ( |
| 4608 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4609 | handle3.Init(TestGroupId("group3"), params_, MEDIUM, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4610 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4611 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4612 | pool_.get(), NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4613 | |
| 4614 | base::RunLoop().RunUntilIdle(); |
| 4615 | EXPECT_FALSE(callback3.have_result()); |
| 4616 | |
| 4617 | // The fourth request is made when the pool is no longer stalled. This |
| 4618 | // request has a higher priority than the third request, so is serviced first. |
| 4619 | mock_layered_pool.set_can_release_connection(true); |
| 4620 | ClientSocketHandle handle4; |
| 4621 | TestCompletionCallback callback4; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4622 | EXPECT_EQ( |
| 4623 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4624 | handle4.Init(TestGroupId("group3"), params_, HIGHEST, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4625 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4626 | callback4.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4627 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4628 | EXPECT_THAT(callback4.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4629 | EXPECT_FALSE(callback3.have_result()); |
| 4630 | |
| 4631 | // Closing a handle should free up another socket slot. |
| 4632 | handle1.Reset(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4633 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4634 | } |
| 4635 | |
| 4636 | TEST_F(ClientSocketPoolBaseTest, |
| 4637 | CloseMultipleIdleSocketsHeldByLayeredPoolWhenNeeded) { |
| 4638 | CreatePool(1, 1); |
| 4639 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 4640 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4641 | MockLayeredPool mock_layered_pool1(pool_.get(), TestGroupId("foo")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4642 | EXPECT_THAT(mock_layered_pool1.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4643 | EXPECT_CALL(mock_layered_pool1, CloseOneIdleConnection()) |
| 4644 | .WillRepeatedly(Invoke(&mock_layered_pool1, |
| 4645 | &MockLayeredPool::ReleaseOneConnection)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4646 | MockLayeredPool mock_layered_pool2(pool_.get(), TestGroupId("bar")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4647 | EXPECT_THAT(mock_layered_pool2.RequestSocketWithoutLimits(pool_.get()), |
| 4648 | IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4649 | EXPECT_CALL(mock_layered_pool2, CloseOneIdleConnection()) |
| 4650 | .WillRepeatedly(Invoke(&mock_layered_pool2, |
| 4651 | &MockLayeredPool::ReleaseOneConnection)); |
| 4652 | ClientSocketHandle handle; |
| 4653 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4654 | EXPECT_EQ( |
| 4655 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4656 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4657 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 4658 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4659 | NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4660 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4661 | } |
| 4662 | |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4663 | // Test that when a socket pool and group are at their limits, a request |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4664 | // with RespectLimits::DISABLED triggers creation of a new socket, and gets the |
| 4665 | // socket instead of a request with the same priority that was issued earlier, |
| 4666 | // but has RespectLimits::ENABLED. |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4667 | TEST_F(ClientSocketPoolBaseTest, IgnoreLimits) { |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4668 | CreatePool(1, 1); |
| 4669 | |
| 4670 | // Issue a request to reach the socket pool limit. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4671 | EXPECT_EQ(OK, StartRequestWithIgnoreLimits( |
| 4672 | TestGroupId("a"), MAXIMUM_PRIORITY, |
| 4673 | ClientSocketPool::RespectLimits::ENABLED)); |
| 4674 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4675 | |
| 4676 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 4677 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4678 | EXPECT_EQ(ERR_IO_PENDING, StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4679 | TestGroupId("a"), MAXIMUM_PRIORITY, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4680 | ClientSocketPool::RespectLimits::ENABLED)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4681 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4682 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4683 | // Issue a request that ignores the limits, so a new ConnectJob is |
| 4684 | // created. |
| 4685 | EXPECT_EQ(ERR_IO_PENDING, StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4686 | TestGroupId("a"), MAXIMUM_PRIORITY, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4687 | ClientSocketPool::RespectLimits::DISABLED)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4688 | ASSERT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4689 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4690 | EXPECT_THAT(request(2)->WaitForResult(), IsOk()); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4691 | EXPECT_FALSE(request(1)->have_result()); |
| 4692 | } |
| 4693 | |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 4694 | // Test that when a socket pool and group are at their limits, a ConnectJob |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4695 | // issued for a request with RespectLimits::DISABLED is not cancelled when a |
| 4696 | // request with RespectLimits::ENABLED issued to the same group is cancelled. |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 4697 | TEST_F(ClientSocketPoolBaseTest, IgnoreLimitsCancelOtherJob) { |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 4698 | CreatePool(1, 1); |
| 4699 | |
| 4700 | // Issue a request to reach the socket pool limit. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4701 | EXPECT_EQ(OK, StartRequestWithIgnoreLimits( |
| 4702 | TestGroupId("a"), MAXIMUM_PRIORITY, |
| 4703 | ClientSocketPool::RespectLimits::ENABLED)); |
| 4704 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 4705 | |
| 4706 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 4707 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4708 | EXPECT_EQ(ERR_IO_PENDING, StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4709 | TestGroupId("a"), MAXIMUM_PRIORITY, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4710 | ClientSocketPool::RespectLimits::ENABLED)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4711 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 4712 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4713 | // Issue a request with RespectLimits::DISABLED, so a new ConnectJob is |
| 4714 | // created. |
| 4715 | EXPECT_EQ(ERR_IO_PENDING, StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4716 | TestGroupId("a"), MAXIMUM_PRIORITY, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4717 | ClientSocketPool::RespectLimits::DISABLED)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4718 | ASSERT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4719 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4720 | // Cancel the pending request with RespectLimits::ENABLED. The ConnectJob |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4721 | // should not be cancelled. |
| 4722 | request(1)->handle()->Reset(); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4723 | ASSERT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4724 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4725 | EXPECT_THAT(request(2)->WaitForResult(), IsOk()); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4726 | EXPECT_FALSE(request(1)->have_result()); |
| 4727 | } |
| 4728 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4729 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthNoAuthCallback) { |
| 4730 | CreatePool(1, 1); |
| 4731 | |
| 4732 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 4733 | |
| 4734 | ClientSocketHandle handle; |
| 4735 | TestCompletionCallback callback; |
| 4736 | EXPECT_EQ( |
| 4737 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4738 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4739 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 4740 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4741 | NetLogWithSource())); |
| 4742 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4743 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4744 | |
| 4745 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_PROXY_AUTH_REQUESTED)); |
| 4746 | EXPECT_FALSE(handle.is_initialized()); |
| 4747 | EXPECT_FALSE(handle.socket()); |
| 4748 | |
| 4749 | // The group should now be empty, and thus be deleted. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4750 | EXPECT_FALSE(pool_->HasGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4751 | } |
| 4752 | |
| 4753 | class TestAuthHelper { |
| 4754 | public: |
| 4755 | TestAuthHelper() = default; |
| 4756 | ~TestAuthHelper() = default; |
| 4757 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4758 | void InitHandle( |
| 4759 | scoped_refptr<TestSocketParams> params, |
| 4760 | TestClientSocketPool* pool, |
| 4761 | RequestPriority priority = DEFAULT_PRIORITY, |
| 4762 | ClientSocketPool::RespectLimits respect_limits = |
| 4763 | ClientSocketPool::RespectLimits::ENABLED, |
| 4764 | const ClientSocketPool::GroupId& group_id_in = TestGroupId("a")) { |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4765 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4766 | handle_.Init(group_id_in, params.get(), priority, SocketTag(), |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4767 | respect_limits, callback_.callback(), |
| 4768 | base::BindRepeating(&TestAuthHelper::AuthCallback, |
| 4769 | base::Unretained(this)), |
| 4770 | pool, NetLogWithSource())); |
| 4771 | } |
| 4772 | |
| 4773 | void WaitForAuth() { |
| 4774 | run_loop_ = std::make_unique<base::RunLoop>(); |
| 4775 | run_loop_->Run(); |
| 4776 | run_loop_.reset(); |
| 4777 | } |
| 4778 | |
| 4779 | void WaitForAuthAndRestartSync() { |
| 4780 | restart_sync_ = true; |
| 4781 | WaitForAuth(); |
| 4782 | restart_sync_ = false; |
| 4783 | } |
| 4784 | |
| 4785 | void WaitForAuthAndResetHandleSync() { |
| 4786 | reset_handle_sync_ = true; |
| 4787 | WaitForAuth(); |
| 4788 | reset_handle_sync_ = false; |
| 4789 | } |
| 4790 | |
| 4791 | void RestartWithAuth() { |
| 4792 | DCHECK(restart_with_auth_callback_); |
| 4793 | std::move(restart_with_auth_callback_).Run(); |
| 4794 | } |
| 4795 | |
| 4796 | int WaitForResult() { |
| 4797 | int result = callback_.WaitForResult(); |
| 4798 | // There shouldn't be any callback waiting to be invoked once the request is |
| 4799 | // complete. |
| 4800 | EXPECT_FALSE(restart_with_auth_callback_); |
| 4801 | // The socket should only be initialized on success. |
| 4802 | EXPECT_EQ(result == OK, handle_.is_initialized()); |
| 4803 | EXPECT_EQ(result == OK, handle_.socket() != nullptr); |
| 4804 | return result; |
| 4805 | } |
| 4806 | |
| 4807 | ClientSocketHandle* handle() { return &handle_; } |
| 4808 | int auth_count() const { return auth_count_; } |
| 4809 | int have_result() const { return callback_.have_result(); } |
| 4810 | |
| 4811 | private: |
| 4812 | void AuthCallback(const HttpResponseInfo& response, |
| 4813 | HttpAuthController* auth_controller, |
| 4814 | base::OnceClosure restart_with_auth_callback) { |
| 4815 | EXPECT_FALSE(restart_with_auth_callback_); |
| 4816 | EXPECT_TRUE(restart_with_auth_callback); |
| 4817 | |
| 4818 | // Once there's a result, this method shouldn't be invoked again. |
| 4819 | EXPECT_FALSE(callback_.have_result()); |
| 4820 | |
| 4821 | ++auth_count_; |
| 4822 | run_loop_->Quit(); |
| 4823 | if (restart_sync_) { |
| 4824 | std::move(restart_with_auth_callback).Run(); |
| 4825 | return; |
| 4826 | } |
| 4827 | |
| 4828 | restart_with_auth_callback_ = std::move(restart_with_auth_callback); |
| 4829 | |
| 4830 | if (reset_handle_sync_) { |
| 4831 | handle_.Reset(); |
| 4832 | return; |
| 4833 | } |
| 4834 | } |
| 4835 | |
| 4836 | std::unique_ptr<base::RunLoop> run_loop_; |
| 4837 | base::OnceClosure restart_with_auth_callback_; |
| 4838 | |
| 4839 | bool restart_sync_ = false; |
| 4840 | bool reset_handle_sync_ = false; |
| 4841 | |
| 4842 | ClientSocketHandle handle_; |
| 4843 | int auth_count_ = 0; |
| 4844 | TestCompletionCallback callback_; |
| 4845 | |
| 4846 | DISALLOW_COPY_AND_ASSIGN(TestAuthHelper); |
| 4847 | }; |
| 4848 | |
| 4849 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnce) { |
| 4850 | CreatePool(1, 1); |
| 4851 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 4852 | |
| 4853 | TestAuthHelper auth_helper; |
| 4854 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4855 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 4856 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4857 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4858 | |
| 4859 | auth_helper.WaitForAuth(); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4860 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 4861 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4862 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4863 | |
| 4864 | auth_helper.RestartWithAuth(); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4865 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 4866 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4867 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4868 | |
| 4869 | EXPECT_THAT(auth_helper.WaitForResult(), IsOk()); |
| 4870 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4871 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 4872 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroup(TestGroupId("a"))); |
| 4873 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4874 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 4875 | } |
| 4876 | |
| 4877 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceSync) { |
| 4878 | CreatePool(1, 1); |
| 4879 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 4880 | |
| 4881 | TestAuthHelper auth_helper; |
| 4882 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4883 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 4884 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4885 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4886 | |
| 4887 | auth_helper.WaitForAuthAndRestartSync(); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4888 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 4889 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4890 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4891 | |
| 4892 | EXPECT_THAT(auth_helper.WaitForResult(), IsOk()); |
| 4893 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4894 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 4895 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroup(TestGroupId("a"))); |
| 4896 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4897 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 4898 | } |
| 4899 | |
| 4900 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceFails) { |
| 4901 | CreatePool(1, 1); |
| 4902 | connect_job_factory_->set_job_type( |
| 4903 | TestConnectJob::kMockAuthChallengeOnceFailingJob); |
| 4904 | |
| 4905 | TestAuthHelper auth_helper; |
| 4906 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4907 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4908 | |
| 4909 | auth_helper.WaitForAuth(); |
| 4910 | auth_helper.RestartWithAuth(); |
| 4911 | EXPECT_THAT(auth_helper.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 4912 | |
| 4913 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4914 | EXPECT_FALSE(pool_->HasGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4915 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 4916 | } |
| 4917 | |
| 4918 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceSyncFails) { |
| 4919 | CreatePool(1, 1); |
| 4920 | connect_job_factory_->set_job_type( |
| 4921 | TestConnectJob::kMockAuthChallengeOnceFailingJob); |
| 4922 | |
| 4923 | TestAuthHelper auth_helper; |
| 4924 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4925 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4926 | |
| 4927 | auth_helper.WaitForAuthAndRestartSync(); |
| 4928 | EXPECT_THAT(auth_helper.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 4929 | |
| 4930 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4931 | EXPECT_FALSE(pool_->HasGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4932 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 4933 | } |
| 4934 | |
| 4935 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceDeleteHandle) { |
| 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 | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4942 | |
| 4943 | auth_helper.WaitForAuth(); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4944 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4945 | |
| 4946 | auth_helper.handle()->Reset(); |
| 4947 | |
| 4948 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4949 | EXPECT_FALSE(pool_->HasGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4950 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 4951 | EXPECT_FALSE(auth_helper.handle()->is_initialized()); |
| 4952 | EXPECT_FALSE(auth_helper.handle()->socket()); |
| 4953 | } |
| 4954 | |
| 4955 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceDeleteHandleSync) { |
| 4956 | CreatePool(1, 1); |
| 4957 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 4958 | |
| 4959 | TestAuthHelper auth_helper; |
| 4960 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4961 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4962 | |
| 4963 | auth_helper.WaitForAuthAndResetHandleSync(); |
| 4964 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4965 | EXPECT_FALSE(pool_->HasGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4966 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 4967 | EXPECT_FALSE(auth_helper.handle()->is_initialized()); |
| 4968 | EXPECT_FALSE(auth_helper.handle()->socket()); |
| 4969 | } |
| 4970 | |
| 4971 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceFlushWithError) { |
| 4972 | CreatePool(1, 1); |
| 4973 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 4974 | |
| 4975 | TestAuthHelper auth_helper; |
| 4976 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4977 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4978 | |
| 4979 | auth_helper.WaitForAuth(); |
| 4980 | |
| 4981 | pool_->FlushWithError(ERR_FAILED); |
| 4982 | base::RunLoop().RunUntilIdle(); |
| 4983 | |
| 4984 | // When flushing the socket pool, bound sockets should delay returning the |
| 4985 | // error until completion. |
| 4986 | EXPECT_FALSE(auth_helper.have_result()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4987 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4988 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 4989 | |
| 4990 | auth_helper.RestartWithAuth(); |
| 4991 | // The callback should be called asynchronously. |
| 4992 | EXPECT_FALSE(auth_helper.have_result()); |
| 4993 | |
| 4994 | EXPECT_THAT(auth_helper.WaitForResult(), IsError(ERR_FAILED)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4995 | EXPECT_FALSE(pool_->HasGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4996 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 4997 | } |
| 4998 | |
| 4999 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthTwice) { |
| 5000 | CreatePool(1, 1); |
| 5001 | connect_job_factory_->set_job_type( |
| 5002 | TestConnectJob::kMockAuthChallengeTwiceJob); |
| 5003 | |
| 5004 | TestAuthHelper auth_helper; |
| 5005 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5006 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5007 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5008 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5009 | |
| 5010 | auth_helper.WaitForAuth(); |
| 5011 | auth_helper.RestartWithAuth(); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5012 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5013 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5014 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5015 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5016 | |
| 5017 | auth_helper.WaitForAuth(); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5018 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5019 | EXPECT_EQ(2, auth_helper.auth_count()); |
| 5020 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5021 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5022 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5023 | auth_helper.RestartWithAuth(); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5024 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5025 | EXPECT_EQ(2, auth_helper.auth_count()); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5026 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5027 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5028 | |
| 5029 | EXPECT_THAT(auth_helper.WaitForResult(), IsOk()); |
| 5030 | EXPECT_EQ(2, auth_helper.auth_count()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5031 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 5032 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroup(TestGroupId("a"))); |
| 5033 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5034 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5035 | } |
| 5036 | |
| 5037 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthTwiceFails) { |
| 5038 | CreatePool(1, 1); |
| 5039 | connect_job_factory_->set_job_type( |
| 5040 | TestConnectJob::kMockAuthChallengeTwiceFailingJob); |
| 5041 | |
| 5042 | TestAuthHelper auth_helper; |
| 5043 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5044 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5045 | |
| 5046 | auth_helper.WaitForAuth(); |
| 5047 | auth_helper.RestartWithAuth(); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5048 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5049 | EXPECT_EQ(1, auth_helper.auth_count()); |
| 5050 | |
| 5051 | auth_helper.WaitForAuth(); |
| 5052 | auth_helper.RestartWithAuth(); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5053 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5054 | EXPECT_EQ(2, auth_helper.auth_count()); |
| 5055 | |
| 5056 | EXPECT_THAT(auth_helper.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 5057 | EXPECT_EQ(2, auth_helper.auth_count()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5058 | EXPECT_FALSE(pool_->HasGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5059 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5060 | } |
| 5061 | |
| 5062 | // Makes sure that when a bound request is destroyed, a new ConnectJob is |
| 5063 | // created, if needed. |
| 5064 | TEST_F(ClientSocketPoolBaseTest, |
| 5065 | ProxyAuthCreateNewConnectJobOnDestroyBoundRequest) { |
| 5066 | CreatePool(1 /* max_sockets */, 1 /* max_sockets_per_group */); |
| 5067 | connect_job_factory_->set_job_type( |
| 5068 | TestConnectJob::kMockAuthChallengeOnceFailingJob); |
| 5069 | |
| 5070 | // First request creates a ConnectJob. |
| 5071 | TestAuthHelper auth_helper1; |
| 5072 | auth_helper1.InitHandle(params_, pool_.get()); |
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 | |
| 5075 | // A second request come in, but no new ConnectJob is needed, since the limit |
| 5076 | // has been reached. |
| 5077 | TestAuthHelper auth_helper2; |
| 5078 | auth_helper2.InitHandle(params_, pool_.get()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5079 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5080 | |
| 5081 | // Run until the auth callback for the first request is invoked. |
| 5082 | auth_helper1.WaitForAuth(); |
| 5083 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5084 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 5085 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroup(TestGroupId("a"))); |
| 5086 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5087 | |
| 5088 | // Make connect jobs succeed, then cancel the first request, which should |
| 5089 | // destroy the bound ConnectJob, and cause a new ConnectJob to start. |
| 5090 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 5091 | auth_helper1.handle()->Reset(); |
| 5092 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5093 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5094 | |
| 5095 | // The second ConnectJob should succeed. |
| 5096 | EXPECT_THAT(auth_helper2.WaitForResult(), IsOk()); |
| 5097 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5098 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5099 | } |
| 5100 | |
| 5101 | // Makes sure that when a bound request is destroyed, a new ConnectJob is |
| 5102 | // created for another group, if needed. |
| 5103 | TEST_F(ClientSocketPoolBaseTest, |
| 5104 | ProxyAuthCreateNewConnectJobOnDestroyBoundRequestDifferentGroups) { |
| 5105 | CreatePool(1 /* max_sockets */, 1 /* max_sockets_per_group */); |
| 5106 | connect_job_factory_->set_job_type( |
| 5107 | TestConnectJob::kMockAuthChallengeOnceFailingJob); |
| 5108 | |
| 5109 | // First request creates a ConnectJob. |
| 5110 | TestAuthHelper auth_helper1; |
| 5111 | auth_helper1.InitHandle(params_, pool_.get(), DEFAULT_PRIORITY); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5112 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5113 | |
| 5114 | // A second request come in, but no new ConnectJob is needed, since the limit |
| 5115 | // has been reached. |
| 5116 | TestAuthHelper auth_helper2; |
| 5117 | auth_helper2.InitHandle(params_, pool_.get(), DEFAULT_PRIORITY, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5118 | ClientSocketPool::RespectLimits::ENABLED, |
| 5119 | TestGroupId("b")); |
| 5120 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 5121 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroup(TestGroupId("b"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5122 | |
| 5123 | // Run until the auth callback for the first request is invoked. |
| 5124 | auth_helper1.WaitForAuth(); |
| 5125 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5126 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 5127 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroup(TestGroupId("a"))); |
| 5128 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
| 5129 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroup(TestGroupId("b"))); |
| 5130 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroup(TestGroupId("b"))); |
| 5131 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5132 | |
| 5133 | // Make connect jobs succeed, then cancel the first request, which should |
| 5134 | // destroy the bound ConnectJob, and cause a new ConnectJob to start for the |
| 5135 | // other group. |
| 5136 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 5137 | auth_helper1.handle()->Reset(); |
| 5138 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5139 | EXPECT_FALSE(pool_->HasGroup(TestGroupId("a"))); |
| 5140 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("b"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5141 | |
| 5142 | // The second ConnectJob should succeed. |
| 5143 | EXPECT_THAT(auth_helper2.WaitForResult(), IsOk()); |
| 5144 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5145 | EXPECT_FALSE(pool_->HasGroup(TestGroupId("a"))); |
| 5146 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroup(TestGroupId("b"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5147 | } |
| 5148 | |
| 5149 | // Test that once an auth challenge is bound, that's the request that gets all |
| 5150 | // subsequent calls and the socket itself. |
| 5151 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthStaysBound) { |
| 5152 | CreatePool(1, 1); |
| 5153 | connect_job_factory_->set_job_type( |
| 5154 | TestConnectJob::kMockAuthChallengeTwiceJob); |
| 5155 | |
| 5156 | // First request creates a ConnectJob. |
| 5157 | TestAuthHelper auth_helper1; |
| 5158 | auth_helper1.InitHandle(params_, pool_.get(), LOWEST); |
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, higher priority request is made. |
| 5162 | TestAuthHelper auth_helper2; |
| 5163 | auth_helper2.InitHandle(params_, pool_.get(), LOW); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5164 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5165 | |
| 5166 | // Run until the auth callback for the second request is invoked. |
| 5167 | auth_helper2.WaitForAuth(); |
| 5168 | EXPECT_EQ(0, auth_helper1.auth_count()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5169 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 5170 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroup(TestGroupId("a"))); |
| 5171 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5172 | |
| 5173 | // Start a higher priority job. It shouldn't be able to steal |auth_helper2|'s |
| 5174 | // ConnectJob. |
| 5175 | TestAuthHelper auth_helper3; |
| 5176 | auth_helper3.InitHandle(params_, pool_.get(), HIGHEST); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5177 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5178 | |
| 5179 | // Start a higher job that ignores limits, creating a hanging socket. It |
| 5180 | // shouldn't be able to steal |auth_helper2|'s ConnectJob. |
| 5181 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 5182 | TestAuthHelper auth_helper4; |
| 5183 | auth_helper4.InitHandle(params_, pool_.get(), HIGHEST, |
| 5184 | ClientSocketPool::RespectLimits::DISABLED); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5185 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5186 | |
| 5187 | // Restart with auth, and |auth_helper2|'s auth method should be invoked |
| 5188 | // again. |
| 5189 | auth_helper2.RestartWithAuth(); |
| 5190 | auth_helper2.WaitForAuth(); |
| 5191 | EXPECT_EQ(0, auth_helper1.auth_count()); |
| 5192 | EXPECT_FALSE(auth_helper1.have_result()); |
| 5193 | EXPECT_EQ(2, auth_helper2.auth_count()); |
| 5194 | EXPECT_FALSE(auth_helper2.have_result()); |
| 5195 | EXPECT_EQ(0, auth_helper3.auth_count()); |
| 5196 | EXPECT_FALSE(auth_helper3.have_result()); |
| 5197 | EXPECT_EQ(0, auth_helper4.auth_count()); |
| 5198 | EXPECT_FALSE(auth_helper4.have_result()); |
| 5199 | |
| 5200 | // Advance auth again, and |auth_helper2| should get the socket. |
| 5201 | auth_helper2.RestartWithAuth(); |
| 5202 | EXPECT_THAT(auth_helper2.WaitForResult(), IsOk()); |
| 5203 | // The hung ConnectJob for the RespectLimits::DISABLED request is still in the |
| 5204 | // socket pool. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5205 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroup(TestGroupId("a"))); |
| 5206 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5207 | EXPECT_EQ(0, auth_helper1.auth_count()); |
| 5208 | EXPECT_FALSE(auth_helper1.have_result()); |
| 5209 | EXPECT_EQ(0, auth_helper3.auth_count()); |
| 5210 | EXPECT_FALSE(auth_helper3.have_result()); |
| 5211 | EXPECT_EQ(0, auth_helper4.auth_count()); |
| 5212 | EXPECT_FALSE(auth_helper4.have_result()); |
| 5213 | |
| 5214 | // If the socket is returned to the socket pool, the RespectLimits::DISABLED |
| 5215 | // socket request should be able to claim it. |
| 5216 | auth_helper2.handle()->Reset(); |
| 5217 | EXPECT_THAT(auth_helper4.WaitForResult(), IsOk()); |
| 5218 | EXPECT_EQ(0, auth_helper1.auth_count()); |
| 5219 | EXPECT_FALSE(auth_helper1.have_result()); |
| 5220 | EXPECT_EQ(0, auth_helper3.auth_count()); |
| 5221 | EXPECT_FALSE(auth_helper3.have_result()); |
| 5222 | EXPECT_EQ(0, auth_helper4.auth_count()); |
| 5223 | } |
| 5224 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 5225 | } // namespace |
| 5226 | |
| 5227 | } // namespace net |