[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 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5 | #include "net/socket/transport_client_socket_pool.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" |
[email protected] | 2041cf34 | 2010-02-19 03:15:59 | [diff] [blame] | 12 | #include "base/callback.h" |
danakj | db9ae794 | 2020-11-11 16:01:35 | [diff] [blame] | 13 | #include "base/callback_helpers.h" |
Hans Wennborg | 0924470b | 2020-04-27 21:08:05 | [diff] [blame] | 14 | #include "base/check_op.h" |
Lei Zhang | 305c015e1 | 2021-06-04 21:07:02 | [diff] [blame] | 15 | #include "base/cxx17_backports.h" |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 16 | #include "base/location.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 17 | #include "base/memory/ref_counted.h" |
[email protected] | 6ea7b15 | 2011-12-21 21:21:13 | [diff] [blame] | 18 | #include "base/memory/weak_ptr.h" |
Hans Wennborg | 0924470b | 2020-04-27 21:08:05 | [diff] [blame] | 19 | #include "base/notreached.h" |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 20 | #include "base/run_loop.h" |
[email protected] | fc9be580 | 2013-06-11 10:56:51 | [diff] [blame] | 21 | #include "base/strings/string_number_conversions.h" |
[email protected] | 18b57741 | 2013-07-18 04:19:15 | [diff] [blame] | 22 | #include "base/strings/stringprintf.h" |
Patrick Monette | 643cdf6 | 2021-10-15 19:13:42 | [diff] [blame] | 23 | #include "base/task/single_thread_task_runner.h" |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 24 | #include "base/test/scoped_feature_list.h" |
[email protected] | f214f879 | 2011-01-01 02:17:08 | [diff] [blame] | 25 | #include "base/threading/platform_thread.h" |
gab | f767595f | 2016-05-11 18:50:35 | [diff] [blame] | 26 | #include "base/threading/thread_task_runner_handle.h" |
[email protected] | f3a1c64 | 2011-07-12 19:15:03 | [diff] [blame] | 27 | #include "base/values.h" |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 28 | #include "net/base/features.h" |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame] | 29 | #include "net/base/host_port_pair.h" |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 30 | #include "net/base/load_timing_info.h" |
[email protected] | b258e079 | 2013-01-12 07:11:59 | [diff] [blame] | 31 | #include "net/base/load_timing_info_test_util.h" |
[email protected] | d8eb8424 | 2010-09-25 02:25:06 | [diff] [blame] | 32 | #include "net/base/net_errors.h" |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 33 | #include "net/base/network_isolation_key.h" |
Matt Menke | bdf77780 | 2019-04-22 19:38:59 | [diff] [blame] | 34 | #include "net/base/privacy_mode.h" |
Matt Menke | aafff54 | 2019-04-22 22:09:36 | [diff] [blame] | 35 | #include "net/base/proxy_server.h" |
Eric Orth | 5ccc3f0 | 2021-09-23 00:01:57 | [diff] [blame] | 36 | #include "net/base/proxy_string_util.h" |
[email protected] | ac790b4 | 2009-12-02 04:31:31 | [diff] [blame] | 37 | #include "net/base/request_priority.h" |
Matt Menke | 4807a9a | 2020-11-21 00:14:41 | [diff] [blame] | 38 | #include "net/base/schemeful_site.h" |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 39 | #include "net/base/test_completion_callback.h" |
dalyk | edd30d98 | 2019-12-16 15:31:10 | [diff] [blame] | 40 | #include "net/dns/public/resolve_error_info.h" |
Ben Schwartz | 3ff4dc1e6 | 2021-04-27 21:15:23 | [diff] [blame] | 41 | #include "net/dns/public/secure_dns_policy.h" |
[email protected] | 277d594 | 2010-08-11 21:02:35 | [diff] [blame] | 42 | #include "net/http/http_response_headers.h" |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 43 | #include "net/http/http_response_info.h" |
eroman | 87c53d6 | 2015-04-02 06:51:07 | [diff] [blame] | 44 | #include "net/log/net_log.h" |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 45 | #include "net/log/net_log_event_type.h" |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 46 | #include "net/log/net_log_source.h" |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 47 | #include "net/log/net_log_source_type.h" |
mmenke | 16a7cbdd | 2015-04-24 23:00:56 | [diff] [blame] | 48 | #include "net/log/test_net_log.h" |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 49 | #include "net/log/test_net_log_util.h" |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 50 | #include "net/socket/client_socket_factory.h" |
| 51 | #include "net/socket/client_socket_handle.h" |
Eric Orth | a9b8be0 | 2021-06-29 23:09:08 | [diff] [blame] | 52 | #include "net/socket/connect_job_factory.h" |
tfarina | 5dd13c2 | 2016-11-16 12:08:26 | [diff] [blame] | 53 | #include "net/socket/datagram_client_socket.h" |
tbansal | ca83c00 | 2016-04-28 20:56:28 | [diff] [blame] | 54 | #include "net/socket/socket_performance_watcher.h" |
Paul Jensen | 8d6f87ec | 2018-01-13 00:46:54 | [diff] [blame] | 55 | #include "net/socket/socket_tag.h" |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 56 | #include "net/socket/socket_test_util.h" |
[email protected] | 18ccfdb | 2013-08-15 00:13:44 | [diff] [blame] | 57 | #include "net/socket/ssl_client_socket.h" |
[email protected] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame] | 58 | #include "net/socket/stream_socket.h" |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 59 | #include "net/socket/transport_connect_job.h" |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 60 | #include "net/ssl/ssl_cert_request_info.h" |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 61 | #include "net/test/gtest_util.h" |
Gabriel Charette | c710874 | 2019-08-23 03:31:40 | [diff] [blame] | 62 | #include "net/test/test_with_task_environment.h" |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 63 | #include "net/traffic_annotation/network_traffic_annotation.h" |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 64 | #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 65 | #include "testing/gmock/include/gmock/gmock.h" |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 66 | #include "testing/gtest/include/gtest/gtest.h" |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 67 | #include "third_party/abseil-cpp/absl/types/optional.h" |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame] | 68 | #include "url/scheme_host_port.h" |
| 69 | #include "url/url_constants.h" |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 70 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 71 | using net::test::IsError; |
| 72 | using net::test::IsOk; |
| 73 | |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 74 | using ::testing::Invoke; |
| 75 | using ::testing::Return; |
| 76 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 77 | namespace net { |
| 78 | |
| 79 | namespace { |
| 80 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 81 | const int kDefaultMaxSockets = 4; |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 82 | const int kDefaultMaxSocketsPerGroup = 2; |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 83 | constexpr base::TimeDelta kUnusedIdleSocketTimeout = base::Seconds(10); |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 84 | |
Matt Menke | bdf77780 | 2019-04-22 19:38:59 | [diff] [blame] | 85 | ClientSocketPool::GroupId TestGroupId( |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame] | 86 | base::StringPiece host, |
Matt Menke | bdf77780 | 2019-04-22 19:38:59 | [diff] [blame] | 87 | int port = 80, |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame] | 88 | base::StringPiece scheme = url::kHttpScheme, |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 89 | PrivacyMode privacy_mode = PrivacyMode::PRIVACY_MODE_DISABLED, |
| 90 | NetworkIsolationKey network_isolation_key = NetworkIsolationKey()) { |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame] | 91 | return ClientSocketPool::GroupId(url::SchemeHostPort(scheme, host, port), |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 92 | privacy_mode, network_isolation_key, |
Ben Schwartz | 3ff4dc1e6 | 2021-04-27 21:15:23 | [diff] [blame] | 93 | SecureDnsPolicy::kAllow); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 94 | } |
| 95 | |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 96 | // Make sure |handle| sets load times correctly when it has been assigned a |
| 97 | // reused socket. |
| 98 | void TestLoadTimingInfoConnectedReused(const ClientSocketHandle& handle) { |
| 99 | LoadTimingInfo load_timing_info; |
| 100 | // Only pass true in as |is_reused|, as in general, HttpStream types should |
| 101 | // have stricter concepts of reuse than socket pools. |
| 102 | EXPECT_TRUE(handle.GetLoadTimingInfo(true, &load_timing_info)); |
| 103 | |
| 104 | EXPECT_EQ(true, load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 105 | EXPECT_NE(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 106 | |
[email protected] | b258e079 | 2013-01-12 07:11:59 | [diff] [blame] | 107 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 108 | ExpectLoadTimingHasOnlyConnectionTimes(load_timing_info); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | // Make sure |handle| sets load times correctly when it has been assigned a |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 112 | // fresh socket. Also runs TestLoadTimingInfoConnectedReused, since the owner |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 113 | // of a connection where |is_reused| is false may consider the connection |
| 114 | // reused. |
| 115 | void TestLoadTimingInfoConnectedNotReused(const ClientSocketHandle& handle) { |
| 116 | EXPECT_FALSE(handle.is_reused()); |
| 117 | |
| 118 | LoadTimingInfo load_timing_info; |
| 119 | EXPECT_TRUE(handle.GetLoadTimingInfo(false, &load_timing_info)); |
| 120 | |
| 121 | EXPECT_FALSE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 122 | EXPECT_NE(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 123 | |
[email protected] | b258e079 | 2013-01-12 07:11:59 | [diff] [blame] | 124 | ExpectConnectTimingHasTimes(load_timing_info.connect_timing, |
| 125 | CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY); |
| 126 | ExpectLoadTimingHasOnlyConnectionTimes(load_timing_info); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 127 | |
| 128 | TestLoadTimingInfoConnectedReused(handle); |
| 129 | } |
| 130 | |
| 131 | // Make sure |handle| sets load times correctly, in the case that it does not |
| 132 | // currently have a socket. |
| 133 | void TestLoadTimingInfoNotConnected(const ClientSocketHandle& handle) { |
| 134 | // Should only be set to true once a socket is assigned, if at all. |
| 135 | EXPECT_FALSE(handle.is_reused()); |
| 136 | |
| 137 | LoadTimingInfo load_timing_info; |
| 138 | EXPECT_FALSE(handle.GetLoadTimingInfo(false, &load_timing_info)); |
| 139 | |
| 140 | EXPECT_FALSE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 141 | EXPECT_EQ(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 142 | |
[email protected] | b258e079 | 2013-01-12 07:11:59 | [diff] [blame] | 143 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 144 | ExpectLoadTimingHasOnlyConnectionTimes(load_timing_info); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 145 | } |
| 146 | |
[email protected] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame] | 147 | class MockClientSocket : public StreamSocket { |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 148 | public: |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 149 | explicit MockClientSocket(net::NetLog* net_log) |
| 150 | : connected_(false), |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 151 | has_unread_data_(false), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 152 | net_log_(NetLogWithSource::Make(net_log, NetLogSourceType::SOCKET)), |
Charlie Harrison | 3e4c062 | 2018-05-13 15:44:30 | [diff] [blame] | 153 | was_used_to_convey_data_(false) {} |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 154 | |
Peter Boström | 407869b | 2021-10-07 04:42:48 | [diff] [blame] | 155 | MockClientSocket(const MockClientSocket&) = delete; |
| 156 | MockClientSocket& operator=(const MockClientSocket&) = delete; |
| 157 | |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 158 | // Sets whether the socket has unread data. If true, the next call to Read() |
| 159 | // will return 1 byte and IsConnectedAndIdle() will return false. |
| 160 | void set_has_unread_data(bool has_unread_data) { |
| 161 | has_unread_data_ = has_unread_data; |
| 162 | } |
| 163 | |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 164 | // Socket implementation. |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 165 | int Read(IOBuffer* /* buf */, |
| 166 | int len, |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 167 | CompletionOnceCallback /* callback */) override { |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 168 | if (has_unread_data_ && len > 0) { |
| 169 | has_unread_data_ = false; |
| 170 | was_used_to_convey_data_ = true; |
| 171 | return 1; |
| 172 | } |
[email protected] | e86df8dc | 2013-03-30 13:18:28 | [diff] [blame] | 173 | return ERR_UNEXPECTED; |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 174 | } |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 175 | |
[email protected] | a2b2cfc | 2017-12-06 09:06:08 | [diff] [blame] | 176 | int Write( |
| 177 | IOBuffer* /* buf */, |
| 178 | int len, |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 179 | CompletionOnceCallback /* callback */, |
[email protected] | a2b2cfc | 2017-12-06 09:06:08 | [diff] [blame] | 180 | const NetworkTrafficAnnotationTag& /*traffic_annotation*/) override { |
[email protected] | 0f873e8 | 2010-09-02 16:09:01 | [diff] [blame] | 181 | was_used_to_convey_data_ = true; |
| 182 | return len; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 183 | } |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 184 | int SetReceiveBufferSize(int32_t size) override { return OK; } |
| 185 | int SetSendBufferSize(int32_t size) override { return OK; } |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 186 | |
[email protected] | dbf036f | 2011-12-06 23:33:24 | [diff] [blame] | 187 | // StreamSocket implementation. |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 188 | int Connect(CompletionOnceCallback callback) override { |
[email protected] | dbf036f | 2011-12-06 23:33:24 | [diff] [blame] | 189 | connected_ = true; |
| 190 | return OK; |
| 191 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 192 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 193 | void Disconnect() override { connected_ = false; } |
| 194 | bool IsConnected() const override { return connected_; } |
| 195 | bool IsConnectedAndIdle() const override { |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 196 | return connected_ && !has_unread_data_; |
| 197 | } |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 198 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 199 | int GetPeerAddress(IPEndPoint* /* address */) const override { |
[email protected] | 9f864b3 | 2010-01-20 15:01:16 | [diff] [blame] | 200 | return ERR_UNEXPECTED; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 201 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 202 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 203 | int GetLocalAddress(IPEndPoint* /* address */) const override { |
[email protected] | e7f74da | 2011-04-19 23:49:35 | [diff] [blame] | 204 | return ERR_UNEXPECTED; |
| 205 | } |
| 206 | |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 207 | const NetLogWithSource& NetLog() const override { return net_log_; } |
[email protected] | a2006ece | 2010-04-23 16:44:02 | [diff] [blame] | 208 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 209 | bool WasEverUsed() const override { return was_used_to_convey_data_; } |
tfarina | 2846404c | 2016-12-25 14:31:37 | [diff] [blame] | 210 | bool WasAlpnNegotiated() const override { return false; } |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 211 | NextProto GetNegotiatedProtocol() const override { return kProtoUnknown; } |
| 212 | bool GetSSLInfo(SSLInfo* ssl_info) override { return false; } |
ttuttle | 23fdb7b | 2015-05-15 01:28:03 | [diff] [blame] | 213 | void GetConnectionAttempts(ConnectionAttempts* out) const override { |
| 214 | out->clear(); |
| 215 | } |
| 216 | void ClearConnectionAttempts() override {} |
| 217 | void AddConnectionAttempts(const ConnectionAttempts& attempts) override {} |
tbansal | f82cc8e | 2015-10-14 20:05:49 | [diff] [blame] | 218 | int64_t GetTotalReceivedBytes() const override { |
| 219 | NOTIMPLEMENTED(); |
| 220 | return 0; |
| 221 | } |
Paul Jensen | 0f49dec | 2017-12-12 23:39:58 | [diff] [blame] | 222 | void ApplySocketTag(const SocketTag& tag) override {} |
[email protected] | 9b5614a | 2010-08-25 20:29:45 | [diff] [blame] | 223 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 224 | private: |
| 225 | bool connected_; |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 226 | bool has_unread_data_; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 227 | NetLogWithSource net_log_; |
[email protected] | 0f873e8 | 2010-09-02 16:09:01 | [diff] [blame] | 228 | bool was_used_to_convey_data_; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 229 | }; |
| 230 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 231 | class TestConnectJob; |
| 232 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 233 | class MockClientSocketFactory : public ClientSocketFactory { |
| 234 | public: |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 235 | MockClientSocketFactory() : allocation_count_(0) {} |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 236 | |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 237 | std::unique_ptr<DatagramClientSocket> CreateDatagramClientSocket( |
[email protected] | 5370c01 | 2011-06-29 03:47:04 | [diff] [blame] | 238 | DatagramSocket::BindType bind_type, |
[email protected] | 98b0e58 | 2011-06-22 14:31:41 | [diff] [blame] | 239 | NetLog* net_log, |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 240 | const NetLogSource& source) override { |
[email protected] | 98b0e58 | 2011-06-22 14:31:41 | [diff] [blame] | 241 | NOTREACHED(); |
David Benjamin | 24725be | 2019-07-24 20:57:18 | [diff] [blame] | 242 | return nullptr; |
[email protected] | 98b0e58 | 2011-06-22 14:31:41 | [diff] [blame] | 243 | } |
| 244 | |
Helen Li | d5bb922 | 2018-04-12 15:33:09 | [diff] [blame] | 245 | std::unique_ptr<TransportClientSocket> CreateTransportClientSocket( |
[email protected] | 0a0b768 | 2010-08-25 17:08:07 | [diff] [blame] | 246 | const AddressList& addresses, |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 247 | std::unique_ptr< |
| 248 | SocketPerformanceWatcher> /* socket_performance_watcher */, |
Eric Roman | 2bc7716 | 2020-09-16 18:30:45 | [diff] [blame] | 249 | NetworkQualityEstimator* /* network_quality_estimator */, |
[email protected] | 0a0b768 | 2010-08-25 17:08:07 | [diff] [blame] | 250 | NetLog* /* net_log */, |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 251 | const NetLogSource& /*source*/) override { |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 252 | allocation_count_++; |
Helen Li | d5bb922 | 2018-04-12 15:33:09 | [diff] [blame] | 253 | return nullptr; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 254 | } |
| 255 | |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 256 | std::unique_ptr<SSLClientSocket> CreateSSLClientSocket( |
David Benjamin | 24725be | 2019-07-24 20:57:18 | [diff] [blame] | 257 | SSLClientContext* context, |
Matt Menke | 841fc41 | 2019-03-05 23:20:12 | [diff] [blame] | 258 | std::unique_ptr<StreamSocket> stream_socket, |
[email protected] | 4f4de7e6 | 2010-11-12 19:55:27 | [diff] [blame] | 259 | const HostPortPair& host_and_port, |
David Benjamin | 24725be | 2019-07-24 20:57:18 | [diff] [blame] | 260 | const SSLConfig& ssl_config) override { |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 261 | NOTIMPLEMENTED(); |
David Benjamin | 24725be | 2019-07-24 20:57:18 | [diff] [blame] | 262 | return nullptr; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 263 | } |
Matt Menke | fd95692 | 2019-02-04 23:44:03 | [diff] [blame] | 264 | |
Matt Menke | 52cd95a | 2019-02-08 06:16:27 | [diff] [blame] | 265 | std::unique_ptr<ProxyClientSocket> CreateProxyClientSocket( |
| 266 | std::unique_ptr<StreamSocket> stream_socket, |
| 267 | const std::string& user_agent, |
| 268 | const HostPortPair& endpoint, |
| 269 | const ProxyServer& proxy_server, |
| 270 | HttpAuthController* http_auth_controller, |
| 271 | bool tunnel, |
| 272 | bool using_spdy, |
| 273 | NextProto negotiated_protocol, |
| 274 | ProxyDelegate* proxy_delegate, |
Matt Menke | 52cd95a | 2019-02-08 06:16:27 | [diff] [blame] | 275 | const NetworkTrafficAnnotationTag& traffic_annotation) override { |
| 276 | NOTIMPLEMENTED(); |
| 277 | return nullptr; |
| 278 | } |
| 279 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 280 | void WaitForSignal(TestConnectJob* job) { waiting_jobs_.push_back(job); } |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 281 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 282 | void SignalJobs(); |
| 283 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 284 | void SignalJob(size_t job); |
| 285 | |
| 286 | void SetJobLoadState(size_t job, LoadState load_state); |
| 287 | |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 288 | // Sets the HasConnectionEstablished value of the specified job to true, |
| 289 | // without invoking the callback. |
| 290 | void SetJobHasEstablishedConnection(size_t job); |
| 291 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 292 | int allocation_count() const { return allocation_count_; } |
| 293 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 294 | private: |
| 295 | int allocation_count_; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 296 | std::vector<TestConnectJob*> waiting_jobs_; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 297 | }; |
| 298 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 299 | class TestConnectJob : public ConnectJob { |
| 300 | public: |
| 301 | enum JobType { |
| 302 | kMockJob, |
| 303 | kMockFailingJob, |
| 304 | kMockPendingJob, |
| 305 | kMockPendingFailingJob, |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 306 | kMockWaitingJob, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 307 | |
| 308 | // Certificate errors return a socket in addition to an error code. |
| 309 | kMockCertErrorJob, |
| 310 | kMockPendingCertErrorJob, |
| 311 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 312 | kMockAdditionalErrorStateJob, |
| 313 | kMockPendingAdditionalErrorStateJob, |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 314 | kMockUnreadDataJob, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 315 | |
| 316 | kMockAuthChallengeOnceJob, |
| 317 | kMockAuthChallengeTwiceJob, |
| 318 | kMockAuthChallengeOnceFailingJob, |
| 319 | kMockAuthChallengeTwiceFailingJob, |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 320 | }; |
| 321 | |
[email protected] | 994d493 | 2010-07-12 17:55:13 | [diff] [blame] | 322 | // The kMockPendingJob uses a slight delay before allowing the connect |
| 323 | // to complete. |
| 324 | static const int kPendingConnectDelay = 2; |
| 325 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 326 | TestConnectJob(JobType job_type, |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 327 | RequestPriority request_priority, |
| 328 | SocketTag socket_tag, |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 329 | base::TimeDelta timeout_duration, |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 330 | const CommonConnectJobParams* common_connect_job_params, |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 331 | ConnectJob::Delegate* delegate, |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 332 | MockClientSocketFactory* client_socket_factory) |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 333 | : ConnectJob(request_priority, |
| 334 | socket_tag, |
Matt Menke | 1a6c92d | 2019-02-23 00:25:38 | [diff] [blame] | 335 | timeout_duration, |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 336 | common_connect_job_params, |
Matt Menke | 1a6c92d | 2019-02-23 00:25:38 | [diff] [blame] | 337 | delegate, |
| 338 | nullptr /* net_log */, |
| 339 | NetLogSourceType::TRANSPORT_CONNECT_JOB, |
| 340 | NetLogEventType::TRANSPORT_CONNECT_JOB_CONNECT), |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 341 | job_type_(job_type), |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 342 | client_socket_factory_(client_socket_factory), |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 343 | load_state_(LOAD_STATE_IDLE), |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 344 | has_established_connection_(false), |
Jeremy Roman | d54000b2 | 2019-07-08 18:40:16 | [diff] [blame] | 345 | store_additional_error_state_(false) {} |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 346 | |
Peter Boström | 407869b | 2021-10-07 04:42:48 | [diff] [blame] | 347 | TestConnectJob(const TestConnectJob&) = delete; |
| 348 | TestConnectJob& operator=(const TestConnectJob&) = delete; |
| 349 | |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 350 | void Signal() { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 351 | DoConnect(waiting_success_, true /* async */, false /* recoverable */); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 352 | } |
| 353 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 354 | void set_load_state(LoadState load_state) { load_state_ = load_state; } |
| 355 | |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 356 | void set_has_established_connection() { |
| 357 | DCHECK(!has_established_connection_); |
| 358 | has_established_connection_ = true; |
| 359 | } |
| 360 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 361 | // From ConnectJob: |
| 362 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 363 | LoadState GetLoadState() const override { return load_state_; } |
[email protected] | 4645135 | 2009-09-01 14:54:21 | [diff] [blame] | 364 | |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 365 | bool HasEstablishedConnection() const override { |
| 366 | return has_established_connection_; |
| 367 | } |
| 368 | |
dalyk | edd30d98 | 2019-12-16 15:31:10 | [diff] [blame] | 369 | ResolveErrorInfo GetResolveErrorInfo() const override { |
| 370 | return ResolveErrorInfo(OK); |
| 371 | } |
| 372 | |
Matt Menke | 6f84d1f1 | 2019-04-11 19:26:47 | [diff] [blame] | 373 | bool IsSSLError() const override { return store_additional_error_state_; } |
| 374 | |
| 375 | scoped_refptr<SSLCertRequestInfo> GetCertRequestInfo() override { |
| 376 | if (store_additional_error_state_) |
| 377 | return base::MakeRefCounted<SSLCertRequestInfo>(); |
| 378 | return nullptr; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 379 | } |
| 380 | |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 381 | private: |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 382 | // From ConnectJob: |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 383 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 384 | int ConnectInternal() override { |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 385 | AddressList ignored; |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 386 | client_socket_factory_->CreateTransportClientSocket( |
Eric Roman | 2bc7716 | 2020-09-16 18:30:45 | [diff] [blame] | 387 | ignored, nullptr, nullptr, nullptr, NetLogSource()); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 388 | switch (job_type_) { |
| 389 | case kMockJob: |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 390 | return DoConnect(true /* successful */, false /* sync */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 391 | false /* cert_error */); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 392 | case kMockFailingJob: |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 393 | return DoConnect(false /* error */, false /* sync */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 394 | false /* cert_error */); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 395 | case kMockPendingJob: |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 396 | set_load_state(LOAD_STATE_CONNECTING); |
[email protected] | 6b17538 | 2009-10-13 06:47:47 | [diff] [blame] | 397 | |
| 398 | // Depending on execution timings, posting a delayed task can result |
| 399 | // in the task getting executed the at the earliest possible |
| 400 | // opportunity or only after returning once from the message loop and |
| 401 | // then a second call into the message loop. In order to make behavior |
| 402 | // more deterministic, we change the default delay to 2ms. This should |
| 403 | // always require us to wait for the second call into the message loop. |
| 404 | // |
| 405 | // N.B. The correct fix for this and similar timing problems is to |
| 406 | // abstract time for the purpose of unittests. Unfortunately, we have |
| 407 | // a lot of third-party components that directly call the various |
| 408 | // time functions, so this change would be rather invasive. |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 409 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 410 | FROM_HERE, |
kylechar | f4fe517 | 2019-02-15 18:53:49 | [diff] [blame] | 411 | base::BindOnce(base::IgnoreResult(&TestConnectJob::DoConnect), |
| 412 | weak_factory_.GetWeakPtr(), true /* successful */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 413 | true /* async */, false /* cert_error */), |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 414 | base::Milliseconds(kPendingConnectDelay)); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 415 | return ERR_IO_PENDING; |
| 416 | case kMockPendingFailingJob: |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 417 | set_load_state(LOAD_STATE_CONNECTING); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 418 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 419 | FROM_HERE, |
kylechar | f4fe517 | 2019-02-15 18:53:49 | [diff] [blame] | 420 | base::BindOnce(base::IgnoreResult(&TestConnectJob::DoConnect), |
| 421 | weak_factory_.GetWeakPtr(), false /* error */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 422 | true /* async */, false /* cert_error */), |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 423 | base::Milliseconds(2)); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 424 | return ERR_IO_PENDING; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 425 | case kMockWaitingJob: |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 426 | set_load_state(LOAD_STATE_CONNECTING); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 427 | client_socket_factory_->WaitForSignal(this); |
| 428 | waiting_success_ = true; |
| 429 | return ERR_IO_PENDING; |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 430 | case kMockCertErrorJob: |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 431 | return DoConnect(false /* error */, false /* sync */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 432 | true /* cert_error */); |
| 433 | case kMockPendingCertErrorJob: |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 434 | set_load_state(LOAD_STATE_CONNECTING); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 435 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [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 */, true /* cert_error */), |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 440 | base::Milliseconds(2)); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 441 | return ERR_IO_PENDING; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 442 | case kMockAdditionalErrorStateJob: |
| 443 | store_additional_error_state_ = true; |
| 444 | return DoConnect(false /* error */, false /* sync */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 445 | false /* cert_error */); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 446 | case kMockPendingAdditionalErrorStateJob: |
| 447 | set_load_state(LOAD_STATE_CONNECTING); |
| 448 | store_additional_error_state_ = true; |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 449 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 450 | FROM_HERE, |
kylechar | f4fe517 | 2019-02-15 18:53:49 | [diff] [blame] | 451 | base::BindOnce(base::IgnoreResult(&TestConnectJob::DoConnect), |
| 452 | weak_factory_.GetWeakPtr(), false /* error */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 453 | true /* async */, false /* cert_error */), |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 454 | base::Milliseconds(2)); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 455 | return ERR_IO_PENDING; |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 456 | case kMockUnreadDataJob: { |
| 457 | int ret = DoConnect(true /* successful */, false /* sync */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 458 | false /* cert_error */); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 459 | static_cast<MockClientSocket*>(socket())->set_has_unread_data(true); |
| 460 | return ret; |
| 461 | } |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 462 | case kMockAuthChallengeOnceJob: |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 463 | set_load_state(LOAD_STATE_CONNECTING); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 464 | DoAdvanceAuthChallenge(1, true /* succeed_after_last_challenge */); |
| 465 | return ERR_IO_PENDING; |
| 466 | case kMockAuthChallengeTwiceJob: |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 467 | set_load_state(LOAD_STATE_CONNECTING); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 468 | DoAdvanceAuthChallenge(2, true /* succeed_after_last_challenge */); |
| 469 | return ERR_IO_PENDING; |
| 470 | case kMockAuthChallengeOnceFailingJob: |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 471 | set_load_state(LOAD_STATE_CONNECTING); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 472 | DoAdvanceAuthChallenge(1, false /* succeed_after_last_challenge */); |
| 473 | return ERR_IO_PENDING; |
| 474 | case kMockAuthChallengeTwiceFailingJob: |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 475 | set_load_state(LOAD_STATE_CONNECTING); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 476 | DoAdvanceAuthChallenge(2, false /* succeed_after_last_challenge */); |
| 477 | return ERR_IO_PENDING; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 478 | default: |
| 479 | NOTREACHED(); |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 480 | SetSocket(std::unique_ptr<StreamSocket>(), absl::nullopt); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 481 | return ERR_FAILED; |
| 482 | } |
| 483 | } |
| 484 | |
Lily Chen | 02ef29a | 2018-11-30 16:31:43 | [diff] [blame] | 485 | void ChangePriorityInternal(RequestPriority priority) override {} |
| 486 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 487 | int DoConnect(bool succeed, bool was_async, bool cert_error) { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 488 | int result = OK; |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 489 | has_established_connection_ = true; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 490 | if (succeed) { |
Cammie Smith Barnes | aa2a8b5 | 2020-12-17 19:33:19 | [diff] [blame] | 491 | SetSocket(std::make_unique<MockClientSocket>(net_log().net_log()), |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 492 | absl::nullopt); |
Bence Béky | bdbb0e7 | 2018-08-07 21:42:59 | [diff] [blame] | 493 | socket()->Connect(CompletionOnceCallback()); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 494 | } else if (cert_error) { |
Cammie Smith Barnes | aa2a8b5 | 2020-12-17 19:33:19 | [diff] [blame] | 495 | SetSocket(std::make_unique<MockClientSocket>(net_log().net_log()), |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 496 | absl::nullopt); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 497 | result = ERR_CERT_COMMON_NAME_INVALID; |
[email protected] | 6e713f0 | 2009-08-06 02:56:40 | [diff] [blame] | 498 | } else { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 499 | result = ERR_CONNECTION_FAILED; |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 500 | SetSocket(std::unique_ptr<StreamSocket>(), absl::nullopt); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 501 | } |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 502 | |
| 503 | if (was_async) |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 504 | NotifyDelegateOfCompletion(result); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 505 | return result; |
| 506 | } |
| 507 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 508 | void DoAdvanceAuthChallenge(int remaining_challenges, |
| 509 | bool succeed_after_last_challenge) { |
| 510 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 511 | FROM_HERE, |
| 512 | base::BindOnce(&TestConnectJob::InvokeNextProxyAuthCallback, |
| 513 | weak_factory_.GetWeakPtr(), remaining_challenges, |
| 514 | succeed_after_last_challenge)); |
| 515 | } |
| 516 | |
| 517 | void InvokeNextProxyAuthCallback(int remaining_challenges, |
| 518 | bool succeed_after_last_challenge) { |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 519 | set_load_state(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 520 | if (remaining_challenges == 0) { |
| 521 | DoConnect(succeed_after_last_challenge, true /* was_async */, |
| 522 | false /* cert_error */); |
| 523 | return; |
| 524 | } |
| 525 | |
| 526 | // Integration tests make sure HttpResponseInfo and HttpAuthController work. |
| 527 | // The auth tests here are just focused on ConnectJob bookkeeping. |
| 528 | HttpResponseInfo info; |
| 529 | NotifyDelegateOfProxyAuth( |
| 530 | info, nullptr /* http_auth_controller */, |
| 531 | base::BindOnce(&TestConnectJob::DoAdvanceAuthChallenge, |
| 532 | weak_factory_.GetWeakPtr(), remaining_challenges - 1, |
| 533 | succeed_after_last_challenge)); |
| 534 | } |
| 535 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 536 | bool waiting_success_; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 537 | const JobType job_type_; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 538 | MockClientSocketFactory* const client_socket_factory_; |
[email protected] | 4645135 | 2009-09-01 14:54:21 | [diff] [blame] | 539 | LoadState load_state_; |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 540 | bool has_established_connection_; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 541 | bool store_additional_error_state_; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 542 | |
Jeremy Roman | d54000b2 | 2019-07-08 18:40:16 | [diff] [blame] | 543 | base::WeakPtrFactory<TestConnectJob> weak_factory_{this}; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 544 | }; |
| 545 | |
Eric Orth | a9b8be0 | 2021-06-29 23:09:08 | [diff] [blame] | 546 | class TestConnectJobFactory : public ConnectJobFactory { |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 547 | public: |
Eric Orth | a9b8be0 | 2021-06-29 23:09:08 | [diff] [blame] | 548 | explicit TestConnectJobFactory(MockClientSocketFactory* client_socket_factory) |
| 549 | : client_socket_factory_(client_socket_factory) {} |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 550 | |
Peter Boström | 293b134 | 2021-09-22 17:31:43 | [diff] [blame] | 551 | TestConnectJobFactory(const TestConnectJobFactory&) = delete; |
| 552 | TestConnectJobFactory& operator=(const TestConnectJobFactory&) = delete; |
| 553 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 554 | ~TestConnectJobFactory() override = default; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 555 | |
| 556 | void set_job_type(TestConnectJob::JobType job_type) { job_type_ = job_type; } |
| 557 | |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 558 | void set_job_types(std::list<TestConnectJob::JobType>* job_types) { |
| 559 | job_types_ = job_types; |
| 560 | CHECK(!job_types_->empty()); |
| 561 | } |
| 562 | |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 563 | void set_timeout_duration(base::TimeDelta timeout_duration) { |
| 564 | timeout_duration_ = timeout_duration; |
| 565 | } |
| 566 | |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 567 | // ConnectJobFactory implementation. |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 568 | |
Eric Orth | a9b8be0 | 2021-06-29 23:09:08 | [diff] [blame] | 569 | std::unique_ptr<ConnectJob> CreateConnectJob( |
Eric Orth | c98a3e6 | 2021-07-02 17:46:37 | [diff] [blame] | 570 | Endpoint endpoint, |
Eric Orth | a9b8be0 | 2021-06-29 23:09:08 | [diff] [blame] | 571 | const ProxyServer& proxy_server, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 572 | const absl::optional<NetworkTrafficAnnotationTag>& proxy_annotation_tag, |
Eric Orth | a9b8be0 | 2021-06-29 23:09:08 | [diff] [blame] | 573 | const SSLConfig* ssl_config_for_origin, |
| 574 | const SSLConfig* ssl_config_for_proxy, |
| 575 | bool force_tunnel, |
| 576 | PrivacyMode privacy_mode, |
| 577 | const OnHostResolutionCallback& resolution_callback, |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 578 | RequestPriority request_priority, |
| 579 | SocketTag socket_tag, |
Eric Orth | a9b8be0 | 2021-06-29 23:09:08 | [diff] [blame] | 580 | const NetworkIsolationKey& network_isolation_key, |
| 581 | SecureDnsPolicy secure_dns_policy, |
| 582 | const CommonConnectJobParams* common_connect_job_params, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 583 | ConnectJob::Delegate* delegate) const override { |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 584 | EXPECT_TRUE(!job_types_ || !job_types_->empty()); |
| 585 | TestConnectJob::JobType job_type = job_type_; |
| 586 | if (job_types_ && !job_types_->empty()) { |
| 587 | job_type = job_types_->front(); |
| 588 | job_types_->pop_front(); |
| 589 | } |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 590 | return std::make_unique<TestConnectJob>( |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 591 | job_type, request_priority, socket_tag, timeout_duration_, |
Eric Orth | a9b8be0 | 2021-06-29 23:09:08 | [diff] [blame] | 592 | common_connect_job_params, delegate, client_socket_factory_); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | private: |
Eric Orth | a9b8be0 | 2021-06-29 23:09:08 | [diff] [blame] | 596 | TestConnectJob::JobType job_type_ = TestConnectJob::kMockJob; |
| 597 | std::list<TestConnectJob::JobType>* job_types_ = nullptr; |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 598 | base::TimeDelta timeout_duration_; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 599 | MockClientSocketFactory* const client_socket_factory_; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 600 | }; |
| 601 | |
[email protected] | a937a06d | 2009-08-19 21:19:24 | [diff] [blame] | 602 | } // namespace |
| 603 | |
[email protected] | a937a06d | 2009-08-19 21:19:24 | [diff] [blame] | 604 | namespace { |
| 605 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 606 | void MockClientSocketFactory::SignalJobs() { |
jdoerrie | 22a91d8b9 | 2018-10-05 08:43:26 | [diff] [blame] | 607 | for (auto it = waiting_jobs_.begin(); it != waiting_jobs_.end(); ++it) { |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 608 | (*it)->Signal(); |
| 609 | } |
| 610 | waiting_jobs_.clear(); |
| 611 | } |
| 612 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 613 | void MockClientSocketFactory::SignalJob(size_t job) { |
| 614 | ASSERT_LT(job, waiting_jobs_.size()); |
| 615 | waiting_jobs_[job]->Signal(); |
| 616 | waiting_jobs_.erase(waiting_jobs_.begin() + job); |
| 617 | } |
| 618 | |
| 619 | void MockClientSocketFactory::SetJobLoadState(size_t job, |
| 620 | LoadState load_state) { |
| 621 | ASSERT_LT(job, waiting_jobs_.size()); |
| 622 | waiting_jobs_[job]->set_load_state(load_state); |
| 623 | } |
| 624 | |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 625 | void MockClientSocketFactory::SetJobHasEstablishedConnection(size_t job) { |
| 626 | ASSERT_LT(job, waiting_jobs_.size()); |
| 627 | waiting_jobs_[job]->set_has_established_connection(); |
| 628 | } |
| 629 | |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 630 | class ClientSocketPoolBaseTest : public TestWithTaskEnvironment { |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 631 | protected: |
Alex Clarke | 0def209 | 2018-12-10 12:01:45 | [diff] [blame] | 632 | ClientSocketPoolBaseTest() |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 633 | : TestWithTaskEnvironment( |
| 634 | base::test::TaskEnvironment::TimeSource::MOCK_TIME), |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 635 | params_(ClientSocketPool::SocketParams::CreateForHttpForTesting()) { |
[email protected] | 636b825 | 2011-04-08 19:56:54 | [diff] [blame] | 636 | connect_backup_jobs_enabled_ = |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 637 | TransportClientSocketPool::connect_backup_jobs_enabled(); |
| 638 | TransportClientSocketPool::set_connect_backup_jobs_enabled(true); |
[email protected] | 636b825 | 2011-04-08 19:56:54 | [diff] [blame] | 639 | } |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 640 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 641 | ~ClientSocketPoolBaseTest() override { |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 642 | TransportClientSocketPool::set_connect_backup_jobs_enabled( |
[email protected] | 636b825 | 2011-04-08 19:56:54 | [diff] [blame] | 643 | connect_backup_jobs_enabled_); |
| 644 | } |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 645 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 646 | void CreatePool(int max_sockets, |
| 647 | int max_sockets_per_group, |
| 648 | bool enable_backup_connect_jobs = false) { |
Tarun Bansal | a763509 | 2019-02-20 10:00:59 | [diff] [blame] | 649 | CreatePoolWithIdleTimeouts(max_sockets, max_sockets_per_group, |
| 650 | kUnusedIdleSocketTimeout, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 651 | ClientSocketPool::used_idle_socket_timeout(), |
| 652 | enable_backup_connect_jobs); |
[email protected] | 9bf28db | 2009-08-29 01:35:16 | [diff] [blame] | 653 | } |
| 654 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 655 | void CreatePoolWithIdleTimeouts( |
| 656 | int max_sockets, |
| 657 | int max_sockets_per_group, |
| 658 | base::TimeDelta unused_idle_socket_timeout, |
| 659 | base::TimeDelta used_idle_socket_timeout, |
| 660 | bool enable_backup_connect_jobs = false, |
| 661 | ProxyServer proxy_server = ProxyServer::Direct()) { |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 662 | DCHECK(!pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 663 | std::unique_ptr<TestConnectJobFactory> connect_job_factory = |
Eric Orth | a9b8be0 | 2021-06-29 23:09:08 | [diff] [blame] | 664 | std::make_unique<TestConnectJobFactory>(&client_socket_factory_); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 665 | connect_job_factory_ = connect_job_factory.get(); |
| 666 | pool_ = TransportClientSocketPool::CreateForTesting( |
| 667 | max_sockets, max_sockets_per_group, unused_idle_socket_timeout, |
Eric Orth | a9b8be0 | 2021-06-29 23:09:08 | [diff] [blame] | 668 | used_idle_socket_timeout, proxy_server, /*is_for_websockets=*/false, |
| 669 | &common_connect_job_params_, std::move(connect_job_factory), |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 670 | nullptr /* ssl_config_service */, enable_backup_connect_jobs); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 671 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 672 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 673 | int StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 674 | const ClientSocketPool::GroupId& group_id, |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 675 | RequestPriority priority, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 676 | ClientSocketPool::RespectLimits respect_limits) { |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 677 | return test_base_.StartRequestUsingPool(pool_.get(), group_id, priority, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 678 | respect_limits, params_); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 679 | } |
| 680 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 681 | int StartRequest(const ClientSocketPool::GroupId& group_id, |
| 682 | RequestPriority priority) { |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 683 | return StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 684 | group_id, priority, ClientSocketPool::RespectLimits::ENABLED); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 685 | } |
| 686 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 687 | int GetOrderOfRequest(size_t index) const { |
| 688 | return test_base_.GetOrderOfRequest(index); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 689 | } |
| 690 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 691 | bool ReleaseOneConnection(ClientSocketPoolTest::KeepAlive keep_alive) { |
| 692 | return test_base_.ReleaseOneConnection(keep_alive); |
| 693 | } |
| 694 | |
| 695 | void ReleaseAllConnections(ClientSocketPoolTest::KeepAlive keep_alive) { |
| 696 | test_base_.ReleaseAllConnections(keep_alive); |
| 697 | } |
| 698 | |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 699 | // Expects a single NetLogEventType::SOCKET_POOL_CLOSING_SOCKET in |net_log_|. |
| 700 | // It should be logged for the provided source and have the indicated reason. |
| 701 | void ExpectSocketClosedWithReason(NetLogSource expected_source, |
| 702 | const char* expected_reason) { |
Matt Reichhoff | 36a1fd6 | 2021-10-12 22:52:20 | [diff] [blame] | 703 | auto entries = observer_.GetEntriesForSourceWithType( |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 704 | expected_source, NetLogEventType::SOCKET_POOL_CLOSING_SOCKET, |
| 705 | NetLogEventPhase::NONE); |
| 706 | ASSERT_EQ(1u, entries.size()); |
| 707 | ASSERT_TRUE(entries[0].HasParams()); |
| 708 | ASSERT_TRUE(entries[0].params.is_dict()); |
| 709 | const std::string* reason = entries[0].params.FindStringKey("reason"); |
| 710 | ASSERT_TRUE(reason); |
| 711 | EXPECT_EQ(expected_reason, *reason); |
| 712 | } |
| 713 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 714 | TestSocketRequest* request(int i) { return test_base_.request(i); } |
| 715 | size_t requests_size() const { return test_base_.requests_size(); } |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 716 | std::vector<std::unique_ptr<TestSocketRequest>>* requests() { |
olli.raula | 9d66b7d | 2015-11-23 08:30:42 | [diff] [blame] | 717 | return test_base_.requests(); |
| 718 | } |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 719 | // Only counts the requests that get sockets asynchronously; |
| 720 | // synchronous completions are not registered by this count. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 721 | size_t completion_count() const { return test_base_.completion_count(); } |
| 722 | |
Eric Orth | a9b8be0 | 2021-06-29 23:09:08 | [diff] [blame] | 723 | const CommonConnectJobParams common_connect_job_params_{ |
| 724 | nullptr /* client_socket_factory */, |
| 725 | nullptr /* host_resolver */, |
| 726 | nullptr /* http_auth_cache */, |
| 727 | nullptr /* http_auth_handler_factory */, |
| 728 | nullptr /* spdy_session_pool */, |
| 729 | nullptr /* quic_supported_versions */, |
| 730 | nullptr /* quic_stream_factory */, |
| 731 | nullptr /* proxy_delegate */, |
| 732 | nullptr /* http_user_agent_settings */, |
| 733 | nullptr /* ssl_client_context */, |
| 734 | nullptr /* socket_performance_watcher_factory */, |
| 735 | nullptr /* network_quality_estimator */, |
Matt Reichhoff | 36a1fd6 | 2021-10-12 22:52:20 | [diff] [blame] | 736 | NetLog::Get(), |
Eric Orth | a9b8be0 | 2021-06-29 23:09:08 | [diff] [blame] | 737 | nullptr /* websocket_endpoint_lock_manager */}; |
[email protected] | 636b825 | 2011-04-08 19:56:54 | [diff] [blame] | 738 | bool connect_backup_jobs_enabled_; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 739 | MockClientSocketFactory client_socket_factory_; |
[email protected] | 17a0c6c | 2009-08-04 00:07:04 | [diff] [blame] | 740 | TestConnectJobFactory* connect_job_factory_; |
Matt Reichhoff | 36a1fd6 | 2021-10-12 22:52:20 | [diff] [blame] | 741 | RecordingNetLogObserver observer_; |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 742 | // These parameters are never actually used to create a TransportConnectJob. |
Matt Menke | 84d11e56 | 2019-03-27 00:11:19 | [diff] [blame] | 743 | scoped_refptr<ClientSocketPool::SocketParams> params_; |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 744 | std::unique_ptr<TransportClientSocketPool> pool_; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 745 | ClientSocketPoolTest test_base_; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 746 | }; |
| 747 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 748 | TEST_F(ClientSocketPoolBaseTest, BasicSynchronous) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 749 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 750 | |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 751 | TestCompletionCallback callback; |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 752 | ClientSocketHandle handle; |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 753 | RecordingBoundTestNetLog log; |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 754 | TestLoadTimingInfoNotConnected(handle); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 755 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 756 | EXPECT_EQ(OK, handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 757 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 758 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 759 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 760 | pool_.get(), log.bound())); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 761 | EXPECT_TRUE(handle.is_initialized()); |
| 762 | EXPECT_TRUE(handle.socket()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 763 | TestLoadTimingInfoConnectedNotReused(handle); |
| 764 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 765 | handle.Reset(); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 766 | TestLoadTimingInfoNotConnected(handle); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 767 | |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 768 | auto entries = log.GetEntries(); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 769 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 770 | EXPECT_EQ(5u, entries.size()); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 771 | EXPECT_TRUE(LogContainsEvent( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 772 | entries, 0, NetLogEventType::TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 773 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 774 | EXPECT_TRUE(LogContainsBeginEvent(entries, 1, NetLogEventType::SOCKET_POOL)); |
| 775 | EXPECT_TRUE(LogContainsEvent( |
| 776 | entries, 2, NetLogEventType::SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
| 777 | NetLogEventPhase::NONE)); |
| 778 | EXPECT_TRUE(LogContainsEvent(entries, 3, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 779 | NetLogEventType::SOCKET_POOL_BOUND_TO_SOCKET, |
| 780 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 781 | EXPECT_TRUE(LogContainsEndEvent(entries, 4, NetLogEventType::SOCKET_POOL)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 782 | } |
| 783 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 784 | TEST_F(ClientSocketPoolBaseTest, InitConnectionFailure) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 785 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 786 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 787 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 788 | RecordingBoundTestNetLog log; |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 789 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 790 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 791 | TestCompletionCallback callback; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 792 | // Set the additional error state members to ensure that they get cleared. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 793 | handle.set_is_ssl_error(true); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 794 | handle.set_ssl_cert_request_info(base::MakeRefCounted<SSLCertRequestInfo>()); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 795 | EXPECT_EQ( |
| 796 | ERR_CONNECTION_FAILED, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 797 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 798 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 799 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 800 | pool_.get(), log.bound())); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 801 | EXPECT_FALSE(handle.socket()); |
| 802 | EXPECT_FALSE(handle.is_ssl_error()); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 803 | EXPECT_FALSE(handle.ssl_cert_request_info()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 804 | TestLoadTimingInfoNotConnected(handle); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 805 | |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 806 | auto entries = log.GetEntries(); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 807 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 808 | EXPECT_EQ(4u, entries.size()); |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 809 | EXPECT_TRUE(LogContainsEvent( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 810 | entries, 0, NetLogEventType::TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 811 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 812 | EXPECT_TRUE(LogContainsBeginEvent(entries, 1, NetLogEventType::SOCKET_POOL)); |
| 813 | EXPECT_TRUE(LogContainsEvent( |
| 814 | entries, 2, NetLogEventType::SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
| 815 | NetLogEventPhase::NONE)); |
| 816 | EXPECT_TRUE(LogContainsEndEvent(entries, 3, NetLogEventType::SOCKET_POOL)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 817 | } |
| 818 | |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 819 | // Test releasing an open socket into the socket pool, telling the socket pool |
| 820 | // to close the socket. |
| 821 | TEST_F(ClientSocketPoolBaseTest, ReleaseAndCloseConnection) { |
| 822 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 823 | |
| 824 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 825 | ASSERT_TRUE(request(0)->handle()->socket()); |
| 826 | net::NetLogSource source = request(0)->handle()->socket()->NetLog().source(); |
| 827 | ReleaseOneConnection(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 828 | |
| 829 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 830 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 831 | |
| 832 | ExpectSocketClosedWithReason( |
| 833 | source, TransportClientSocketPool::kClosedConnectionReturnedToPool); |
| 834 | } |
| 835 | |
| 836 | TEST_F(ClientSocketPoolBaseTest, SocketWithUnreadDataReturnedToPool) { |
| 837 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 838 | connect_job_factory_->set_job_type(TestConnectJob::kMockUnreadDataJob); |
| 839 | |
| 840 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 841 | ASSERT_TRUE(request(0)->handle()->socket()); |
| 842 | net::NetLogSource source = request(0)->handle()->socket()->NetLog().source(); |
| 843 | EXPECT_TRUE(request(0)->handle()->socket()->IsConnected()); |
| 844 | EXPECT_FALSE(request(0)->handle()->socket()->IsConnectedAndIdle()); |
| 845 | ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE); |
| 846 | |
| 847 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 848 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 849 | |
| 850 | ExpectSocketClosedWithReason( |
| 851 | source, TransportClientSocketPool::kDataReceivedUnexpectedly); |
| 852 | } |
| 853 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 854 | // Make sure different groups do not share sockets. |
| 855 | TEST_F(ClientSocketPoolBaseTest, GroupSeparation) { |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 856 | base::test::ScopedFeatureList feature_list; |
| 857 | feature_list.InitAndEnableFeature( |
| 858 | features::kPartitionConnectionsByNetworkIsolationKey); |
| 859 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 860 | CreatePool(1000 /* max_sockets */, 2 /* max_sockets_per_group */); |
| 861 | |
| 862 | const HostPortPair kHostPortPairs[] = { |
| 863 | {"a", 80}, |
| 864 | {"a", 443}, |
| 865 | {"b", 80}, |
| 866 | }; |
| 867 | |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame] | 868 | const char* const kSchemes[] = { |
| 869 | url::kHttpScheme, |
| 870 | url::kHttpsScheme, |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 871 | }; |
| 872 | |
Matt Menke | bdf77780 | 2019-04-22 19:38:59 | [diff] [blame] | 873 | const PrivacyMode kPrivacyModes[] = {PrivacyMode::PRIVACY_MODE_DISABLED, |
| 874 | PrivacyMode::PRIVACY_MODE_ENABLED}; |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 875 | |
Matt Menke | 4807a9a | 2020-11-21 00:14:41 | [diff] [blame] | 876 | const SchemefulSite kSiteA(GURL("http://a.test/")); |
| 877 | const SchemefulSite kSiteB(GURL("http://b.test/")); |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 878 | const NetworkIsolationKey kNetworkIsolationKeys[] = { |
Matt Menke | 4807a9a | 2020-11-21 00:14:41 | [diff] [blame] | 879 | NetworkIsolationKey(kSiteA, kSiteA), |
| 880 | NetworkIsolationKey(kSiteB, kSiteB), |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 881 | }; |
| 882 | |
Ben Schwartz | 3ff4dc1e6 | 2021-04-27 21:15:23 | [diff] [blame] | 883 | const SecureDnsPolicy kSecureDnsPolicys[] = {SecureDnsPolicy::kAllow, |
| 884 | SecureDnsPolicy::kDisable}; |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 885 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 886 | int total_idle_sockets = 0; |
| 887 | |
| 888 | // Walk through each GroupId, making sure that requesting a socket for one |
| 889 | // group does not return a previously connected socket for another group. |
| 890 | for (const auto& host_port_pair : kHostPortPairs) { |
| 891 | SCOPED_TRACE(host_port_pair.ToString()); |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame] | 892 | for (const char* scheme : kSchemes) { |
| 893 | SCOPED_TRACE(scheme); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 894 | for (const auto& privacy_mode : kPrivacyModes) { |
| 895 | SCOPED_TRACE(privacy_mode); |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 896 | for (const auto& network_isolation_key : kNetworkIsolationKeys) { |
| 897 | SCOPED_TRACE(network_isolation_key.ToString()); |
Ben Schwartz | 3ff4dc1e6 | 2021-04-27 21:15:23 | [diff] [blame] | 898 | for (const auto& secure_dns_policy : kSecureDnsPolicys) { |
| 899 | SCOPED_TRACE(static_cast<int>(secure_dns_policy)); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 900 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 901 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 902 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 903 | ClientSocketPool::GroupId group_id( |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame] | 904 | url::SchemeHostPort(scheme, host_port_pair.host(), |
| 905 | host_port_pair.port()), |
| 906 | privacy_mode, network_isolation_key, secure_dns_policy); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 907 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 908 | EXPECT_FALSE(pool_->HasGroupForTesting(group_id)); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 909 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 910 | TestCompletionCallback callback; |
| 911 | ClientSocketHandle handle; |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 912 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 913 | // Since the group is empty, requesting a socket should not complete |
| 914 | // synchronously. |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 915 | EXPECT_THAT(handle.Init(group_id, params_, absl::nullopt, |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 916 | DEFAULT_PRIORITY, SocketTag(), |
| 917 | ClientSocketPool::RespectLimits::ENABLED, |
| 918 | callback.callback(), |
| 919 | ClientSocketPool::ProxyAuthCallback(), |
| 920 | pool_.get(), NetLogWithSource()), |
| 921 | IsError(ERR_IO_PENDING)); |
| 922 | EXPECT_TRUE(pool_->HasGroupForTesting(group_id)); |
| 923 | EXPECT_EQ(total_idle_sockets, pool_->IdleSocketCount()); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 924 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 925 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 926 | EXPECT_TRUE(handle.socket()); |
| 927 | EXPECT_TRUE(pool_->HasGroupForTesting(group_id)); |
| 928 | EXPECT_EQ(total_idle_sockets, pool_->IdleSocketCount()); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 929 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 930 | // Return socket to pool. |
| 931 | handle.Reset(); |
| 932 | EXPECT_EQ(total_idle_sockets + 1, pool_->IdleSocketCount()); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 933 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 934 | // Requesting a socket again should return the same socket as |
| 935 | // before, so should complete synchronously. |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 936 | EXPECT_THAT(handle.Init(group_id, params_, absl::nullopt, |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 937 | DEFAULT_PRIORITY, SocketTag(), |
| 938 | ClientSocketPool::RespectLimits::ENABLED, |
| 939 | callback.callback(), |
| 940 | ClientSocketPool::ProxyAuthCallback(), |
| 941 | pool_.get(), NetLogWithSource()), |
| 942 | IsOk()); |
| 943 | EXPECT_TRUE(handle.socket()); |
| 944 | EXPECT_EQ(total_idle_sockets, pool_->IdleSocketCount()); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 945 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 946 | // Return socket to pool again. |
| 947 | handle.Reset(); |
| 948 | EXPECT_EQ(total_idle_sockets + 1, pool_->IdleSocketCount()); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 949 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 950 | ++total_idle_sockets; |
| 951 | } |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 952 | } |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 953 | } |
| 954 | } |
| 955 | } |
| 956 | } |
| 957 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 958 | TEST_F(ClientSocketPoolBaseTest, TotalLimit) { |
| 959 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 960 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 961 | // TODO(eroman): Check that the NetLog contains this event. |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 962 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 963 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 964 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
| 965 | EXPECT_THAT(StartRequest(TestGroupId("c"), DEFAULT_PRIORITY), IsOk()); |
| 966 | EXPECT_THAT(StartRequest(TestGroupId("d"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 967 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 968 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 969 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 970 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 971 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 972 | EXPECT_THAT(StartRequest(TestGroupId("e"), DEFAULT_PRIORITY), |
| 973 | IsError(ERR_IO_PENDING)); |
| 974 | EXPECT_THAT(StartRequest(TestGroupId("f"), DEFAULT_PRIORITY), |
| 975 | IsError(ERR_IO_PENDING)); |
| 976 | EXPECT_THAT(StartRequest(TestGroupId("g"), DEFAULT_PRIORITY), |
| 977 | IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 978 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 979 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 980 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 981 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 982 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 983 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 984 | |
| 985 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 986 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 987 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 988 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 989 | EXPECT_EQ(5, GetOrderOfRequest(5)); |
| 990 | EXPECT_EQ(6, GetOrderOfRequest(6)); |
| 991 | EXPECT_EQ(7, GetOrderOfRequest(7)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 992 | |
| 993 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 994 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(8)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 995 | } |
| 996 | |
| 997 | TEST_F(ClientSocketPoolBaseTest, TotalLimitReachedNewGroup) { |
| 998 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 999 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 1000 | // TODO(eroman): Check that the NetLog contains this event. |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 1001 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1002 | // Reach all limits: max total sockets, and max sockets per group. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1003 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1004 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1005 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
| 1006 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1007 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1008 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1009 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1010 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1011 | |
| 1012 | // Now create a new group and verify that we don't starve it. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1013 | EXPECT_THAT(StartRequest(TestGroupId("c"), DEFAULT_PRIORITY), |
| 1014 | IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1015 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1016 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1017 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1018 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1019 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1020 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1021 | |
| 1022 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1023 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1024 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 1025 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 1026 | EXPECT_EQ(5, GetOrderOfRequest(5)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1027 | |
| 1028 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1029 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(6)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1030 | } |
| 1031 | |
| 1032 | TEST_F(ClientSocketPoolBaseTest, TotalLimitRespectsPriority) { |
| 1033 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1034 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1035 | EXPECT_THAT(StartRequest(TestGroupId("b"), LOWEST), IsOk()); |
| 1036 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsOk()); |
| 1037 | EXPECT_THAT(StartRequest(TestGroupId("b"), HIGHEST), IsOk()); |
| 1038 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1039 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1040 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1041 | client_socket_factory_.allocation_count()); |
| 1042 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1043 | EXPECT_THAT(StartRequest(TestGroupId("c"), LOWEST), IsError(ERR_IO_PENDING)); |
| 1044 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1045 | EXPECT_THAT(StartRequest(TestGroupId("b"), HIGHEST), IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1046 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1047 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1048 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1049 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1050 | |
| 1051 | // First 4 requests don't have to wait, and finish in order. |
| 1052 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1053 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1054 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 1055 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 1056 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1057 | // Request ("b", HIGHEST) has the highest priority, then (TestGroupId("a"), |
| 1058 | // MEDIUM), and then ("c", LOWEST). |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1059 | EXPECT_EQ(7, GetOrderOfRequest(5)); |
| 1060 | EXPECT_EQ(6, GetOrderOfRequest(6)); |
| 1061 | EXPECT_EQ(5, GetOrderOfRequest(7)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1062 | |
| 1063 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1064 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(9)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1065 | } |
| 1066 | |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1067 | // Test reprioritizing a request before completion doesn't interfere with |
| 1068 | // its completion. |
| 1069 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeOne) { |
| 1070 | CreatePool(kDefaultMaxSockets, 1); |
| 1071 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1072 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1073 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1074 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1075 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1076 | |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 1077 | request(1)->handle()->SetPriority(HIGHEST); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1078 | |
| 1079 | ReleaseOneConnection(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1080 | |
| 1081 | EXPECT_TRUE(request(1)->handle()->socket()); |
| 1082 | } |
| 1083 | |
| 1084 | // Reprioritize a request up past another one and make sure that changes the |
| 1085 | // completion order. |
| 1086 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeUpReorder) { |
| 1087 | CreatePool(kDefaultMaxSockets, 1); |
| 1088 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1089 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1090 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1091 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1092 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1093 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1094 | EXPECT_FALSE(request(2)->handle()->socket()); |
| 1095 | |
| 1096 | request(2)->handle()->SetPriority(HIGHEST); |
| 1097 | |
| 1098 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1099 | |
| 1100 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1101 | EXPECT_EQ(3, GetOrderOfRequest(2)); |
| 1102 | EXPECT_EQ(2, GetOrderOfRequest(3)); |
| 1103 | } |
| 1104 | |
| 1105 | // Reprioritize a request without changing relative priorities and check |
| 1106 | // that the order doesn't change. |
| 1107 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeUpNoReorder) { |
| 1108 | CreatePool(kDefaultMaxSockets, 1); |
| 1109 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1110 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1111 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1112 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1113 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1114 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1115 | EXPECT_FALSE(request(2)->handle()->socket()); |
| 1116 | |
| 1117 | request(2)->handle()->SetPriority(MEDIUM); |
| 1118 | |
| 1119 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1120 | |
| 1121 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1122 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1123 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 1124 | } |
| 1125 | |
| 1126 | // Reprioritize a request past down another one and make sure that changes the |
| 1127 | // completion order. |
| 1128 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeDownReorder) { |
| 1129 | CreatePool(kDefaultMaxSockets, 1); |
| 1130 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1131 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1132 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1133 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1134 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1135 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1136 | EXPECT_FALSE(request(2)->handle()->socket()); |
| 1137 | |
| 1138 | request(1)->handle()->SetPriority(LOW); |
| 1139 | |
| 1140 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1141 | |
| 1142 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1143 | EXPECT_EQ(3, GetOrderOfRequest(2)); |
| 1144 | EXPECT_EQ(2, GetOrderOfRequest(3)); |
| 1145 | } |
| 1146 | |
| 1147 | // Reprioritize a request to the same level as another and confirm it is |
| 1148 | // put after the old request. |
| 1149 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeResetFIFO) { |
| 1150 | CreatePool(kDefaultMaxSockets, 1); |
| 1151 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1152 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1153 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1154 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1155 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1156 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1157 | EXPECT_FALSE(request(2)->handle()->socket()); |
| 1158 | |
| 1159 | request(1)->handle()->SetPriority(MEDIUM); |
| 1160 | |
| 1161 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1162 | |
| 1163 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1164 | EXPECT_EQ(3, GetOrderOfRequest(2)); |
| 1165 | EXPECT_EQ(2, GetOrderOfRequest(3)); |
| 1166 | } |
| 1167 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1168 | TEST_F(ClientSocketPoolBaseTest, TotalLimitRespectsGroupLimit) { |
| 1169 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1170 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1171 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsOk()); |
| 1172 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsOk()); |
| 1173 | EXPECT_THAT(StartRequest(TestGroupId("b"), HIGHEST), IsOk()); |
| 1174 | EXPECT_THAT(StartRequest(TestGroupId("b"), MEDIUM), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1175 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1176 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1177 | client_socket_factory_.allocation_count()); |
| 1178 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1179 | EXPECT_THAT(StartRequest(TestGroupId("c"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1180 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 1181 | EXPECT_THAT(StartRequest(TestGroupId("b"), HIGHEST), IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1182 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1183 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1184 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1185 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1186 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1187 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1188 | |
| 1189 | // First 4 requests don't have to wait, and finish in order. |
| 1190 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1191 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1192 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 1193 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 1194 | |
| 1195 | // Request ("b", 7) has the highest priority, but we can't make new socket for |
| 1196 | // group "b", because it has reached the per-group limit. Then we make |
| 1197 | // socket for ("c", 6), because it has higher priority than ("a", 4), |
| 1198 | // and we still can't make a socket for group "b". |
| 1199 | EXPECT_EQ(5, GetOrderOfRequest(5)); |
| 1200 | EXPECT_EQ(6, GetOrderOfRequest(6)); |
| 1201 | EXPECT_EQ(7, GetOrderOfRequest(7)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1202 | |
| 1203 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1204 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(8)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1205 | } |
| 1206 | |
| 1207 | // Make sure that we count connecting sockets against the total limit. |
| 1208 | TEST_F(ClientSocketPoolBaseTest, TotalLimitCountsConnectingSockets) { |
| 1209 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1210 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1211 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1212 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
| 1213 | EXPECT_THAT(StartRequest(TestGroupId("c"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1214 | |
| 1215 | // Create one asynchronous request. |
| 1216 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1217 | EXPECT_THAT(StartRequest(TestGroupId("d"), DEFAULT_PRIORITY), |
| 1218 | IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1219 | |
[email protected] | 6b17538 | 2009-10-13 06:47:47 | [diff] [blame] | 1220 | // We post all of our delayed tasks with a 2ms delay. I.e. they don't |
| 1221 | // actually become pending until 2ms after they have been created. In order |
| 1222 | // to flush all tasks, we need to wait so that we know there are no |
| 1223 | // soon-to-be-pending tasks waiting. |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 1224 | FastForwardBy(base::Milliseconds(10)); |
[email protected] | 6b17538 | 2009-10-13 06:47:47 | [diff] [blame] | 1225 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1226 | // The next synchronous request should wait for its turn. |
| 1227 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1228 | EXPECT_THAT(StartRequest(TestGroupId("e"), DEFAULT_PRIORITY), |
| 1229 | IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1230 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1231 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1232 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1233 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1234 | client_socket_factory_.allocation_count()); |
| 1235 | |
| 1236 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1237 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1238 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 1239 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1240 | EXPECT_EQ(5, GetOrderOfRequest(5)); |
| 1241 | |
| 1242 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1243 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(6)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1244 | } |
| 1245 | |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1246 | TEST_F(ClientSocketPoolBaseTest, CorrectlyCountStalledGroups) { |
| 1247 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 1248 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 1249 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1250 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1251 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1252 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1253 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1254 | |
| 1255 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 1256 | |
| 1257 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1258 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1259 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), |
| 1260 | IsError(ERR_IO_PENDING)); |
| 1261 | EXPECT_THAT(StartRequest(TestGroupId("c"), DEFAULT_PRIORITY), |
| 1262 | IsError(ERR_IO_PENDING)); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1263 | |
| 1264 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1265 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1266 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1267 | EXPECT_EQ(kDefaultMaxSockets + 1, client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1268 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1269 | EXPECT_EQ(kDefaultMaxSockets + 2, client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1270 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
| 1271 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1272 | EXPECT_EQ(kDefaultMaxSockets + 2, client_socket_factory_.allocation_count()); |
| 1273 | } |
| 1274 | |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 1275 | TEST_F(ClientSocketPoolBaseTest, StallAndThenCancelAndTriggerAvailableSocket) { |
| 1276 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 1277 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1278 | |
| 1279 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1280 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1281 | EXPECT_EQ( |
| 1282 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1283 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1284 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1285 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1286 | pool_.get(), NetLogWithSource())); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 1287 | |
| 1288 | ClientSocketHandle handles[4]; |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 1289 | for (size_t i = 0; i < base::size(handles); ++i) { |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1290 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1291 | handles[i].Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1292 | TestGroupId("b"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1293 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1294 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1295 | pool_.get(), NetLogWithSource())); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 1296 | } |
| 1297 | |
| 1298 | // One will be stalled, cancel all the handles now. |
| 1299 | // This should hit the OnAvailableSocketSlot() code where we previously had |
| 1300 | // stalled groups, but no longer have any. |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 1301 | for (size_t i = 0; i < base::size(handles); ++i) |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 1302 | handles[i].Reset(); |
| 1303 | } |
| 1304 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1305 | TEST_F(ClientSocketPoolBaseTest, CancelStalledSocketAtSocketLimit) { |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1306 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1307 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 1308 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1309 | { |
| 1310 | ClientSocketHandle handles[kDefaultMaxSockets]; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1311 | TestCompletionCallback callbacks[kDefaultMaxSockets]; |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1312 | for (int i = 0; i < kDefaultMaxSockets; ++i) { |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame] | 1313 | EXPECT_EQ(OK, handles[i].Init(TestGroupId("a" + base::NumberToString(i)), |
| 1314 | params_, absl::nullopt, DEFAULT_PRIORITY, |
| 1315 | SocketTag(), |
| 1316 | ClientSocketPool::RespectLimits::ENABLED, |
| 1317 | callbacks[i].callback(), |
| 1318 | ClientSocketPool::ProxyAuthCallback(), |
| 1319 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1320 | } |
| 1321 | |
| 1322 | // Force a stalled group. |
| 1323 | ClientSocketHandle stalled_handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1324 | TestCompletionCallback callback; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1325 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1326 | stalled_handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1327 | TestGroupId("foo"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1328 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1329 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1330 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1331 | |
| 1332 | // Cancel the stalled request. |
| 1333 | stalled_handle.Reset(); |
| 1334 | |
| 1335 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1336 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 1337 | |
| 1338 | // Dropping out of scope will close all handles and return them to idle. |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1339 | } |
| 1340 | |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1341 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1342 | EXPECT_EQ(kDefaultMaxSockets, pool_->IdleSocketCount()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1343 | } |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1344 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1345 | TEST_F(ClientSocketPoolBaseTest, CancelPendingSocketAtSocketLimit) { |
| 1346 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1347 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 1348 | |
| 1349 | { |
| 1350 | ClientSocketHandle handles[kDefaultMaxSockets]; |
| 1351 | for (int i = 0; i < kDefaultMaxSockets; ++i) { |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1352 | TestCompletionCallback callback; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1353 | EXPECT_EQ(ERR_IO_PENDING, |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame] | 1354 | handles[i].Init( |
| 1355 | TestGroupId("a" + base::NumberToString(i)), params_, |
| 1356 | absl::nullopt, DEFAULT_PRIORITY, SocketTag(), |
| 1357 | ClientSocketPool::RespectLimits::ENABLED, |
| 1358 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1359 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1360 | } |
| 1361 | |
| 1362 | // Force a stalled group. |
| 1363 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1364 | ClientSocketHandle stalled_handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1365 | TestCompletionCallback callback; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1366 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1367 | stalled_handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1368 | TestGroupId("foo"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1369 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1370 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1371 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1372 | |
| 1373 | // Since it is stalled, it should have no connect jobs. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1374 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("foo"))); |
| 1375 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 1376 | TestGroupId("foo"))); |
| 1377 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroupForTesting( |
| 1378 | TestGroupId("foo"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1379 | |
| 1380 | // Cancel the stalled request. |
| 1381 | handles[0].Reset(); |
| 1382 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1383 | // Now we should have a connect job. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1384 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("foo"))); |
| 1385 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 1386 | TestGroupId("foo"))); |
| 1387 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroupForTesting( |
| 1388 | TestGroupId("foo"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1389 | |
| 1390 | // The stalled socket should connect. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1391 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1392 | |
| 1393 | EXPECT_EQ(kDefaultMaxSockets + 1, |
| 1394 | client_socket_factory_.allocation_count()); |
| 1395 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1396 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("foo"))); |
| 1397 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 1398 | TestGroupId("foo"))); |
| 1399 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroupForTesting( |
| 1400 | TestGroupId("foo"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1401 | |
| 1402 | // Dropping out of scope will close all handles and return them to idle. |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1403 | } |
| 1404 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1405 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 1406 | } |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1407 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1408 | TEST_F(ClientSocketPoolBaseTest, WaitForStalledSocketAtSocketLimit) { |
| 1409 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1410 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1411 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1412 | ClientSocketHandle stalled_handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1413 | TestCompletionCallback callback; |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1414 | { |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 1415 | EXPECT_FALSE(pool_->IsStalled()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1416 | ClientSocketHandle handles[kDefaultMaxSockets]; |
| 1417 | for (int i = 0; i < kDefaultMaxSockets; ++i) { |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1418 | EXPECT_EQ( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1419 | OK, handles[i].Init( |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame] | 1420 | TestGroupId(base::StringPrintf("take-2-%d", i)), params_, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1421 | absl::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1422 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1423 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 1424 | NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1425 | } |
| 1426 | |
| 1427 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1428 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 1429 | EXPECT_FALSE(pool_->IsStalled()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1430 | |
| 1431 | // Now we will hit the socket limit. |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1432 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1433 | stalled_handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1434 | TestGroupId("foo"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1435 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1436 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1437 | pool_.get(), NetLogWithSource())); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 1438 | EXPECT_TRUE(pool_->IsStalled()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1439 | |
| 1440 | // Dropping out of scope will close all handles and return them to idle. |
| 1441 | } |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1442 | |
| 1443 | // But if we wait for it, the released idle sockets will be closed in |
| 1444 | // preference of the waiting request. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1445 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1446 | |
| 1447 | EXPECT_EQ(kDefaultMaxSockets + 1, client_socket_factory_.allocation_count()); |
| 1448 | EXPECT_EQ(3, pool_->IdleSocketCount()); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1449 | } |
| 1450 | |
| 1451 | // Regression test for http://crbug.com/40952. |
| 1452 | TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketAtSocketLimitDeleteGroup) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1453 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 1454 | true /* enable_backup_connect_jobs */); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1455 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 1456 | |
| 1457 | for (int i = 0; i < kDefaultMaxSockets; ++i) { |
| 1458 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1459 | TestCompletionCallback callback; |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame] | 1460 | EXPECT_EQ( |
| 1461 | OK, |
| 1462 | handle.Init(TestGroupId("a" + base::NumberToString(i)), params_, |
| 1463 | absl::nullopt, DEFAULT_PRIORITY, SocketTag(), |
| 1464 | ClientSocketPool::RespectLimits::ENABLED, |
| 1465 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1466 | pool_.get(), NetLogWithSource())); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1467 | } |
| 1468 | |
| 1469 | // Flush all the DoReleaseSocket tasks. |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 1470 | base::RunLoop().RunUntilIdle(); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1471 | |
| 1472 | // Stall a group. Set a pending job so it'll trigger a backup job if we don't |
| 1473 | // reuse a socket. |
| 1474 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1475 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1476 | TestCompletionCallback callback; |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1477 | |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame] | 1478 | // "a0" is special here, since it should be the first entry in the sorted map, |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1479 | // which is the one which we would close an idle socket for. We shouldn't |
| 1480 | // close an idle socket though, since we should reuse the idle socket. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1481 | EXPECT_EQ(OK, handle.Init( |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame] | 1482 | TestGroupId("a0"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1483 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1484 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1485 | pool_.get(), NetLogWithSource())); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1486 | |
| 1487 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1488 | EXPECT_EQ(kDefaultMaxSockets - 1, pool_->IdleSocketCount()); |
| 1489 | } |
| 1490 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1491 | TEST_F(ClientSocketPoolBaseTest, PendingRequests) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1492 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1493 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1494 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1495 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1496 | EXPECT_THAT(StartRequest(TestGroupId("a"), IDLE), IsError(ERR_IO_PENDING)); |
| 1497 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
| 1498 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1499 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1500 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 1501 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1502 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1503 | ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1504 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
| 1505 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1506 | EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup, |
| 1507 | completion_count()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1508 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1509 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1510 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
[email protected] | c9c6f5c | 2010-07-31 01:30:03 | [diff] [blame] | 1511 | EXPECT_EQ(8, GetOrderOfRequest(3)); |
| 1512 | EXPECT_EQ(6, GetOrderOfRequest(4)); |
| 1513 | EXPECT_EQ(4, GetOrderOfRequest(5)); |
| 1514 | EXPECT_EQ(3, GetOrderOfRequest(6)); |
| 1515 | EXPECT_EQ(5, GetOrderOfRequest(7)); |
| 1516 | EXPECT_EQ(7, GetOrderOfRequest(8)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1517 | |
| 1518 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1519 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(9)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1520 | } |
| 1521 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1522 | TEST_F(ClientSocketPoolBaseTest, PendingRequests_NoKeepAlive) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1523 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1524 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1525 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1526 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1527 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
| 1528 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1529 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1530 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 1531 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1532 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1533 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1534 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1535 | for (size_t i = kDefaultMaxSocketsPerGroup; i < requests_size(); ++i) |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1536 | EXPECT_THAT(request(i)->WaitForResult(), IsOk()); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1537 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1538 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1539 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1540 | EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup, |
| 1541 | completion_count()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1542 | } |
| 1543 | |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1544 | TEST_F(ClientSocketPoolBaseTest, ResetAndCloseSocket) { |
| 1545 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1546 | |
| 1547 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1548 | ClientSocketHandle handle; |
| 1549 | TestCompletionCallback callback; |
| 1550 | EXPECT_EQ( |
| 1551 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1552 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1553 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1554 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1555 | pool_.get(), NetLogWithSource())); |
| 1556 | |
| 1557 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 1558 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1559 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1560 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
| 1561 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 1562 | |
| 1563 | handle.ResetAndCloseSocket(); |
| 1564 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1565 | } |
| 1566 | |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1567 | // This test will start up a socket request and then call Reset() on the handle. |
| 1568 | // The pending ConnectJob should not be destroyed. |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1569 | TEST_F(ClientSocketPoolBaseTest, CancelRequestKeepsConnectJob) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1570 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1571 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1572 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1573 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1574 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1575 | EXPECT_EQ( |
| 1576 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1577 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1578 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1579 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1580 | pool_.get(), NetLogWithSource())); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1581 | handle.Reset(); |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1582 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1583 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1584 | } |
| 1585 | |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1586 | // This test will start up a socket request and then call ResetAndCloseSocket() |
| 1587 | // on the handle. The pending ConnectJob or connected socket should be |
| 1588 | // destroyed. |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1589 | TEST_F(ClientSocketPoolBaseTest, CancelRequestAndCloseSocket) { |
| 1590 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1591 | |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1592 | // When true, the socket connects before it's canceled. |
| 1593 | for (bool cancel_when_callback_pending : {false, true}) { |
| 1594 | if (cancel_when_callback_pending) { |
| 1595 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 1596 | } else { |
| 1597 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1598 | } |
| 1599 | ClientSocketHandle handle; |
| 1600 | TestCompletionCallback callback; |
| 1601 | EXPECT_EQ( |
| 1602 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1603 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1604 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1605 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1606 | pool_.get(), NetLogWithSource())); |
| 1607 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1608 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1609 | |
| 1610 | if (cancel_when_callback_pending) { |
| 1611 | client_socket_factory_.SignalJobs(); |
| 1612 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1613 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 1614 | } |
| 1615 | |
| 1616 | handle.ResetAndCloseSocket(); |
| 1617 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1618 | } |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1619 | } |
| 1620 | |
| 1621 | TEST_F(ClientSocketPoolBaseTest, |
| 1622 | CancelRequestAndCloseSocketWhenMoreRequestsThanConnectJobs) { |
| 1623 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1624 | |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1625 | // When true, the sockets connect before they're canceled. |
| 1626 | for (bool cancel_when_callback_pending : {false, true}) { |
| 1627 | if (cancel_when_callback_pending) { |
| 1628 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 1629 | } else { |
| 1630 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1631 | } |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1632 | |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1633 | std::vector<std::unique_ptr<ClientSocketHandle>> handles; |
| 1634 | TestCompletionCallback callback; |
| 1635 | // Make |kDefaultMaxSockets + 1| socket requests. |
| 1636 | for (int i = 0; i < kDefaultMaxSocketsPerGroup + 1; ++i) { |
| 1637 | std::unique_ptr<ClientSocketHandle> handle = |
| 1638 | std::make_unique<ClientSocketHandle>(); |
| 1639 | EXPECT_EQ(ERR_IO_PENDING, |
| 1640 | handle->Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1641 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1642 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1643 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1644 | pool_.get(), NetLogWithSource())); |
| 1645 | handles.push_back(std::move(handle)); |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1646 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1647 | EXPECT_EQ( |
| 1648 | static_cast<size_t>(std::min(i + 1, kDefaultMaxSocketsPerGroup)), |
| 1649 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1650 | } |
| 1651 | |
| 1652 | if (cancel_when_callback_pending) { |
| 1653 | client_socket_factory_.SignalJobs(); |
| 1654 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1655 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
| 1656 | pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 1657 | } |
| 1658 | |
| 1659 | // Calling ResetAndCloseSocket() on a handle should not cancel a ConnectJob |
| 1660 | // or close a socket, since there are more requests than ConnectJobs or |
| 1661 | // sockets. |
| 1662 | handles[kDefaultMaxSocketsPerGroup]->ResetAndCloseSocket(); |
| 1663 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1664 | if (cancel_when_callback_pending) { |
| 1665 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
| 1666 | pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 1667 | } else { |
| 1668 | EXPECT_EQ(static_cast<size_t>(kDefaultMaxSocketsPerGroup), |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1669 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1670 | } |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1671 | |
| 1672 | // Calling ResetAndCloseSocket() on other handles should cancel a ConnectJob |
| 1673 | // or close a socket. |
| 1674 | for (int i = kDefaultMaxSocketsPerGroup - 1; i >= 0; --i) { |
| 1675 | handles[i]->ResetAndCloseSocket(); |
| 1676 | if (i > 0) { |
| 1677 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1678 | if (cancel_when_callback_pending) { |
| 1679 | EXPECT_EQ(i, |
| 1680 | pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 1681 | } else { |
| 1682 | EXPECT_EQ(static_cast<size_t>(i), |
| 1683 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1684 | } |
| 1685 | } else { |
| 1686 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1687 | } |
| 1688 | } |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1689 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1690 | } |
| 1691 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1692 | TEST_F(ClientSocketPoolBaseTest, ConnectCancelConnect) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1693 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1694 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1695 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 1696 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1697 | TestCompletionCallback callback; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1698 | |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1699 | EXPECT_EQ( |
| 1700 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1701 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1702 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1703 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1704 | pool_.get(), NetLogWithSource())); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1705 | |
| 1706 | handle.Reset(); |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1707 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1708 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1709 | |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1710 | // This will create a second ConnectJob, since the other ConnectJob was |
| 1711 | // previously assigned to a request. |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1712 | TestCompletionCallback callback2; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1713 | EXPECT_EQ( |
| 1714 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1715 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1716 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1717 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1718 | pool_.get(), NetLogWithSource())); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1719 | |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1720 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1721 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1722 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1723 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1724 | EXPECT_FALSE(callback.have_result()); |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1725 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1726 | // One ConnectJob completed, and its socket is now assigned to |handle|. |
| 1727 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 1728 | // The other ConnectJob should have either completed, or still be connecting. |
| 1729 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")) + |
| 1730 | pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1731 | |
| 1732 | handle.Reset(); |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1733 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1734 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")) + |
| 1735 | pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
| 1736 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1737 | } |
| 1738 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1739 | TEST_F(ClientSocketPoolBaseTest, CancelRequest) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1740 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1741 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1742 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1743 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1744 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
| 1745 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1746 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1747 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 1748 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1749 | |
| 1750 | // Cancel a request. |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1751 | size_t index_to_cancel = kDefaultMaxSocketsPerGroup + 2; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1752 | EXPECT_FALSE((*requests())[index_to_cancel]->handle()->is_initialized()); |
| 1753 | (*requests())[index_to_cancel]->handle()->Reset(); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1754 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1755 | ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1756 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1757 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
| 1758 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1759 | EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup - 1, |
| 1760 | completion_count()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1761 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1762 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1763 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1764 | EXPECT_EQ(5, GetOrderOfRequest(3)); |
| 1765 | EXPECT_EQ(3, GetOrderOfRequest(4)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1766 | EXPECT_EQ(ClientSocketPoolTest::kRequestNotFound, |
| 1767 | GetOrderOfRequest(5)); // Canceled request. |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1768 | EXPECT_EQ(4, GetOrderOfRequest(6)); |
| 1769 | EXPECT_EQ(6, GetOrderOfRequest(7)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1770 | |
| 1771 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1772 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(8)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1773 | } |
| 1774 | |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1775 | // Function to be used as a callback on socket request completion. It first |
| 1776 | // disconnects the successfully connected socket from the first request, and |
| 1777 | // then reuses the ClientSocketHandle to request another socket. |
| 1778 | // |
| 1779 | // |nested_callback| is called with the result of the second socket request. |
| 1780 | void RequestSocketOnComplete(ClientSocketHandle* handle, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1781 | TransportClientSocketPool* pool, |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1782 | TestConnectJobFactory* test_connect_job_factory, |
| 1783 | TestConnectJob::JobType next_job_type, |
Bence Béky | a4a5093 | 2018-08-10 13:39:41 | [diff] [blame] | 1784 | TestCompletionCallback* nested_callback, |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1785 | int first_request_result) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1786 | EXPECT_THAT(first_request_result, IsOk()); |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1787 | |
| 1788 | test_connect_job_factory->set_job_type(next_job_type); |
| 1789 | |
| 1790 | // Don't allow reuse of the socket. Disconnect it and then release it. |
| 1791 | if (handle->socket()) |
| 1792 | handle->socket()->Disconnect(); |
| 1793 | handle->Reset(); |
| 1794 | |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1795 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1796 | int rv = handle->Init( |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 1797 | TestGroupId("a"), |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1798 | ClientSocketPool::SocketParams::CreateForHttpForTesting(), absl::nullopt, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1799 | LOWEST, SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 1800 | nested_callback->callback(), ClientSocketPool::ProxyAuthCallback(), pool, |
| 1801 | NetLogWithSource()); |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1802 | if (rv != ERR_IO_PENDING) { |
| 1803 | DCHECK_EQ(TestConnectJob::kMockJob, next_job_type); |
Bence Béky | a4a5093 | 2018-08-10 13:39:41 | [diff] [blame] | 1804 | nested_callback->callback().Run(rv); |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1805 | } else { |
| 1806 | DCHECK_EQ(TestConnectJob::kMockPendingJob, next_job_type); |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1807 | } |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1808 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1809 | |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1810 | // Tests the case where a second socket is requested in a completion callback, |
| 1811 | // and the second socket connects asynchronously. Reuses the same |
| 1812 | // ClientSocketHandle for the second socket, after disconnecting the first. |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1813 | TEST_F(ClientSocketPoolBaseTest, RequestPendingJobTwice) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1814 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1815 | |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 1816 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 1817 | ClientSocketHandle handle; |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1818 | TestCompletionCallback second_result_callback; |
| 1819 | int rv = handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1820 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Paul Jensen | 8d6f87ec | 2018-01-13 00:46:54 | [diff] [blame] | 1821 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1822 | base::BindOnce(&RequestSocketOnComplete, &handle, pool_.get(), |
| 1823 | connect_job_factory_, TestConnectJob::kMockPendingJob, |
| 1824 | &second_result_callback), |
| 1825 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1826 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1827 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1828 | EXPECT_THAT(second_result_callback.WaitForResult(), IsOk()); |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1829 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1830 | |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1831 | // Tests the case where a second socket is requested in a completion callback, |
| 1832 | // and the second socket connects synchronously. Reuses the same |
| 1833 | // ClientSocketHandle for the second socket, after disconnecting the first. |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1834 | TEST_F(ClientSocketPoolBaseTest, RequestPendingJobThenSynchronous) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1835 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1836 | |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 1837 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 1838 | ClientSocketHandle handle; |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1839 | TestCompletionCallback second_result_callback; |
| 1840 | int rv = handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1841 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Paul Jensen | 8d6f87ec | 2018-01-13 00:46:54 | [diff] [blame] | 1842 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1843 | base::BindOnce(&RequestSocketOnComplete, &handle, pool_.get(), |
| 1844 | connect_job_factory_, TestConnectJob::kMockPendingJob, |
| 1845 | &second_result_callback), |
| 1846 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1847 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1848 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1849 | EXPECT_THAT(second_result_callback.WaitForResult(), IsOk()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1850 | } |
| 1851 | |
| 1852 | // Make sure that pending requests get serviced after active requests get |
| 1853 | // cancelled. |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1854 | TEST_F(ClientSocketPoolBaseTest, CancelActiveRequestWithPendingRequests) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1855 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1856 | |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 1857 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1858 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1859 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1860 | IsError(ERR_IO_PENDING)); |
| 1861 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1862 | IsError(ERR_IO_PENDING)); |
| 1863 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1864 | IsError(ERR_IO_PENDING)); |
| 1865 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1866 | IsError(ERR_IO_PENDING)); |
| 1867 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1868 | IsError(ERR_IO_PENDING)); |
| 1869 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1870 | IsError(ERR_IO_PENDING)); |
| 1871 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1872 | IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1873 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1874 | // Now, kDefaultMaxSocketsPerGroup requests should be active. |
| 1875 | // Let's cancel them. |
| 1876 | for (int i = 0; i < kDefaultMaxSocketsPerGroup; ++i) { |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1877 | ASSERT_FALSE(request(i)->handle()->is_initialized()); |
| 1878 | request(i)->handle()->Reset(); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1879 | } |
| 1880 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1881 | // Let's wait for the rest to complete now. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1882 | for (size_t i = kDefaultMaxSocketsPerGroup; i < requests_size(); ++i) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1883 | EXPECT_THAT(request(i)->WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1884 | request(i)->handle()->Reset(); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1885 | } |
| 1886 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1887 | EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup, |
| 1888 | completion_count()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1889 | } |
| 1890 | |
| 1891 | // Make sure that pending requests get serviced after active requests fail. |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1892 | TEST_F(ClientSocketPoolBaseTest, FailingActiveRequestWithPendingRequests) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1893 | const size_t kMaxSockets = 5; |
| 1894 | CreatePool(kMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1895 | |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 1896 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1897 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1898 | const size_t kNumberOfRequests = 2 * kDefaultMaxSocketsPerGroup + 1; |
| 1899 | ASSERT_LE(kNumberOfRequests, kMaxSockets); // Otherwise the test will hang. |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1900 | |
| 1901 | // Queue up all the requests |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1902 | for (size_t i = 0; i < kNumberOfRequests; ++i) |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1903 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1904 | IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1905 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1906 | for (size_t i = 0; i < kNumberOfRequests; ++i) |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1907 | EXPECT_THAT(request(i)->WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1908 | } |
| 1909 | |
mmenke | 9d72fe4 | 2017-05-18 22:36:07 | [diff] [blame] | 1910 | // Make sure that pending requests that complete synchronously get serviced |
| 1911 | // after active requests fail. See https://crbug.com/723748 |
| 1912 | TEST_F(ClientSocketPoolBaseTest, HandleMultipleSyncFailuresAfterAsyncFailure) { |
| 1913 | const size_t kNumberOfRequests = 10; |
| 1914 | const size_t kMaxSockets = 1; |
| 1915 | CreatePool(kMaxSockets, kMaxSockets); |
| 1916 | |
| 1917 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
| 1918 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1919 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1920 | IsError(ERR_IO_PENDING)); |
mmenke | 9d72fe4 | 2017-05-18 22:36:07 | [diff] [blame] | 1921 | |
| 1922 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
| 1923 | |
| 1924 | // Queue up all the other requests |
| 1925 | for (size_t i = 1; i < kNumberOfRequests; ++i) |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1926 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1927 | IsError(ERR_IO_PENDING)); |
mmenke | 9d72fe4 | 2017-05-18 22:36:07 | [diff] [blame] | 1928 | |
| 1929 | // Make sure all requests fail, instead of hanging. |
| 1930 | for (size_t i = 0; i < kNumberOfRequests; ++i) |
| 1931 | EXPECT_THAT(request(i)->WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 1932 | } |
| 1933 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1934 | TEST_F(ClientSocketPoolBaseTest, CancelActiveRequestThenRequestSocket) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1935 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1936 | |
| 1937 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1938 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1939 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1940 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1941 | int rv = handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1942 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1943 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1944 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1945 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1946 | |
| 1947 | // Cancel the active request. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1948 | handle.Reset(); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1949 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1950 | rv = handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1951 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1952 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1953 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1954 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 1955 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1956 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1957 | EXPECT_FALSE(handle.is_reused()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 1958 | TestLoadTimingInfoConnectedNotReused(handle); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1959 | EXPECT_EQ(2, client_socket_factory_.allocation_count()); |
| 1960 | } |
| 1961 | |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1962 | TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketsForced) { |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 1963 | const char kReason[] = "Really nifty reason"; |
| 1964 | |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1965 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1966 | ClientSocketHandle handle; |
| 1967 | TestCompletionCallback callback; |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 1968 | RecordingBoundTestNetLog log; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1969 | int rv = handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1970 | TestGroupId("a"), params_, absl::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1971 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1972 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1973 | EXPECT_THAT(rv, IsOk()); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 1974 | ASSERT_TRUE(handle.socket()); |
| 1975 | NetLogSource source = handle.socket()->NetLog().source(); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1976 | handle.Reset(); |
| 1977 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 1978 | pool_->CloseIdleSockets(kReason); |
| 1979 | ExpectSocketClosedWithReason(source, kReason); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1980 | } |
| 1981 | |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1982 | TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketsInGroupForced) { |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1983 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1984 | TestCompletionCallback callback; |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 1985 | RecordingBoundTestNetLog log; |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1986 | ClientSocketHandle handle1; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1987 | int rv = handle1.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1988 | TestGroupId("a"), params_, absl::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1989 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1990 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1991 | EXPECT_THAT(rv, IsOk()); |
| 1992 | ClientSocketHandle handle2; |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1993 | rv = handle2.Init(TestGroupId("a"), params_, absl::nullopt, LOWEST, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1994 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1995 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1996 | pool_.get(), log.bound()); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1997 | ClientSocketHandle handle3; |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1998 | rv = handle3.Init(TestGroupId("b"), params_, absl::nullopt, LOWEST, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1999 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2000 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2001 | pool_.get(), log.bound()); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 2002 | EXPECT_THAT(rv, IsOk()); |
| 2003 | handle1.Reset(); |
| 2004 | handle2.Reset(); |
| 2005 | handle3.Reset(); |
| 2006 | EXPECT_EQ(3, pool_->IdleSocketCount()); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 2007 | pool_->CloseIdleSocketsInGroup(TestGroupId("a"), "Very good reason"); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 2008 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 2009 | } |
| 2010 | |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 2011 | TEST_F(ClientSocketPoolBaseTest, CleanUpUnusableIdleSockets) { |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 2012 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2013 | ClientSocketHandle handle; |
| 2014 | TestCompletionCallback callback; |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 2015 | RecordingBoundTestNetLog log; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2016 | int rv = handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2017 | TestGroupId("a"), params_, absl::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2018 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2019 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 2020 | EXPECT_THAT(rv, IsOk()); |
| 2021 | StreamSocket* socket = handle.socket(); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 2022 | ASSERT_TRUE(socket); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 2023 | handle.Reset(); |
| 2024 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 2025 | |
| 2026 | // Disconnect socket now to make the socket unusable. |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 2027 | NetLogSource source = socket->NetLog().source(); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 2028 | socket->Disconnect(); |
| 2029 | ClientSocketHandle handle2; |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2030 | rv = handle2.Init(TestGroupId("a"), params_, absl::nullopt, LOWEST, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2031 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2032 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2033 | pool_.get(), log.bound()); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 2034 | EXPECT_THAT(rv, IsOk()); |
| 2035 | EXPECT_FALSE(handle2.is_reused()); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 2036 | |
| 2037 | // This is admittedly not an accurate error in this case, but normally code |
| 2038 | // doesn't secretly keep a raw pointers to sockets returned to the socket pool |
| 2039 | // and close them out of band, so discovering an idle socket was closed when |
| 2040 | // trying to reuse it normally means it was closed by the remote side. |
| 2041 | ExpectSocketClosedWithReason( |
| 2042 | source, TransportClientSocketPool::kRemoteSideClosedConnection); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 2043 | } |
| 2044 | |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 2045 | // Regression test for http://crbug.com/17985. |
| 2046 | TEST_F(ClientSocketPoolBaseTest, GroupWithPendingRequestsIsNotEmpty) { |
| 2047 | const int kMaxSockets = 3; |
| 2048 | const int kMaxSocketsPerGroup = 2; |
| 2049 | CreatePool(kMaxSockets, kMaxSocketsPerGroup); |
| 2050 | |
[email protected] | ac790b4 | 2009-12-02 04:31:31 | [diff] [blame] | 2051 | const RequestPriority kHighPriority = HIGHEST; |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 2052 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2053 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 2054 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 2055 | |
| 2056 | // This is going to be a pending request in an otherwise empty group. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2057 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2058 | IsError(ERR_IO_PENDING)); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 2059 | |
| 2060 | // Reach the maximum socket limit. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2061 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 2062 | |
| 2063 | // Create a stalled group with high priorities. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2064 | EXPECT_THAT(StartRequest(TestGroupId("c"), kHighPriority), |
| 2065 | IsError(ERR_IO_PENDING)); |
| 2066 | EXPECT_THAT(StartRequest(TestGroupId("c"), kHighPriority), |
| 2067 | IsError(ERR_IO_PENDING)); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 2068 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2069 | // Release the first two sockets from TestGroupId("a"). Because this is a |
| 2070 | // keepalive, the first release will unblock the pending request for |
| 2071 | // TestGroupId("a"). The second release will unblock a request for "c", |
| 2072 | // because it is the next high priority socket. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2073 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
| 2074 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 2075 | |
| 2076 | // Closing idle sockets should not get us into trouble, but in the bug |
| 2077 | // we were hitting a CHECK here. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2078 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 2079 | pool_->CloseIdleSockets("Very good reason"); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 2080 | |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 2081 | // Run the released socket wakeups. |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 2082 | base::RunLoop().RunUntilIdle(); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 2083 | } |
| 2084 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2085 | TEST_F(ClientSocketPoolBaseTest, BasicAsynchronous) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2086 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2087 | |
| 2088 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2089 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2090 | TestCompletionCallback callback; |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 2091 | RecordingBoundTestNetLog log; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2092 | int rv = handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2093 | TestGroupId("a"), params_, absl::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2094 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2095 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2096 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2097 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2098 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 2099 | TestLoadTimingInfoNotConnected(handle); |
| 2100 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2101 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2102 | EXPECT_TRUE(handle.is_initialized()); |
| 2103 | EXPECT_TRUE(handle.socket()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 2104 | TestLoadTimingInfoConnectedNotReused(handle); |
| 2105 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2106 | handle.Reset(); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 2107 | TestLoadTimingInfoNotConnected(handle); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 2108 | |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 2109 | auto entries = log.GetEntries(); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 2110 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2111 | EXPECT_EQ(5u, entries.size()); |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 2112 | EXPECT_TRUE(LogContainsEvent( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2113 | entries, 0, NetLogEventType::TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 2114 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2115 | EXPECT_TRUE(LogContainsBeginEvent(entries, 1, NetLogEventType::SOCKET_POOL)); |
| 2116 | EXPECT_TRUE(LogContainsEvent( |
| 2117 | entries, 2, NetLogEventType::SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
| 2118 | NetLogEventPhase::NONE)); |
| 2119 | EXPECT_TRUE(LogContainsEvent(entries, 3, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 2120 | NetLogEventType::SOCKET_POOL_BOUND_TO_SOCKET, |
| 2121 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2122 | EXPECT_TRUE(LogContainsEndEvent(entries, 4, NetLogEventType::SOCKET_POOL)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2123 | } |
| 2124 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2125 | TEST_F(ClientSocketPoolBaseTest, |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2126 | InitConnectionAsynchronousFailure) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2127 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2128 | |
| 2129 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2130 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2131 | TestCompletionCallback callback; |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 2132 | RecordingBoundTestNetLog log; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2133 | // Set the additional error state members to ensure that they get cleared. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2134 | handle.set_is_ssl_error(true); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 2135 | handle.set_ssl_cert_request_info(base::MakeRefCounted<SSLCertRequestInfo>()); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2136 | EXPECT_EQ( |
| 2137 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2138 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2139 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2140 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2141 | pool_.get(), log.bound())); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2142 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2143 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2144 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2145 | EXPECT_FALSE(handle.is_ssl_error()); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 2146 | EXPECT_FALSE(handle.ssl_cert_request_info()); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 2147 | |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 2148 | auto entries = log.GetEntries(); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 2149 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2150 | EXPECT_EQ(4u, entries.size()); |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 2151 | EXPECT_TRUE(LogContainsEvent( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2152 | entries, 0, NetLogEventType::TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 2153 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2154 | EXPECT_TRUE(LogContainsBeginEvent(entries, 1, NetLogEventType::SOCKET_POOL)); |
| 2155 | EXPECT_TRUE(LogContainsEvent( |
| 2156 | entries, 2, NetLogEventType::SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
| 2157 | NetLogEventPhase::NONE)); |
| 2158 | EXPECT_TRUE(LogContainsEndEvent(entries, 3, NetLogEventType::SOCKET_POOL)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2159 | } |
| 2160 | |
mmenke | 6be122f | 2015-03-09 22:22:47 | [diff] [blame] | 2161 | // Check that an async ConnectJob failure does not result in creation of a new |
| 2162 | // ConnectJob when there's another pending request also waiting on its own |
| 2163 | // ConnectJob. See http://crbug.com/463960. |
| 2164 | TEST_F(ClientSocketPoolBaseTest, AsyncFailureWithPendingRequestWithJob) { |
| 2165 | CreatePool(2, 2); |
| 2166 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
| 2167 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2168 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2169 | IsError(ERR_IO_PENDING)); |
| 2170 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2171 | IsError(ERR_IO_PENDING)); |
mmenke | 6be122f | 2015-03-09 22:22:47 | [diff] [blame] | 2172 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2173 | EXPECT_THAT(request(0)->WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 2174 | EXPECT_THAT(request(1)->WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
mmenke | 6be122f | 2015-03-09 22:22:47 | [diff] [blame] | 2175 | |
| 2176 | EXPECT_EQ(2, client_socket_factory_.allocation_count()); |
| 2177 | } |
| 2178 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2179 | TEST_F(ClientSocketPoolBaseTest, TwoRequestsCancelOne) { |
[email protected] | b22b516 | 2010-03-16 07:53:10 | [diff] [blame] | 2180 | // TODO(eroman): Add back the log expectations! Removed them because the |
| 2181 | // ordering is difficult, and some may fire during destructor. |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2182 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2183 | |
| 2184 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2185 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2186 | TestCompletionCallback callback; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2187 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2188 | TestCompletionCallback callback2; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2189 | |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2190 | EXPECT_EQ( |
| 2191 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2192 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2193 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2194 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2195 | pool_.get(), NetLogWithSource())); |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 2196 | RecordingBoundTestNetLog log2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 2197 | EXPECT_EQ( |
| 2198 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2199 | handle2.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2200 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2201 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2202 | pool_.get(), NetLogWithSource())); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2203 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2204 | handle.Reset(); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2205 | |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 2206 | |
| 2207 | // 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] | 2208 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2209 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2210 | handle2.Reset(); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 2211 | |
| 2212 | // Now request 2 has actually finished. |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 2213 | // TODO(eroman): Add back log expectations. |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2214 | } |
| 2215 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2216 | TEST_F(ClientSocketPoolBaseTest, CancelRequestLimitsJobs) { |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 2217 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2218 | |
[email protected] | 17a0c6c | 2009-08-04 00:07:04 | [diff] [blame] | 2219 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2220 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2221 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
| 2222 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 2223 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 2224 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 2225 | |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2226 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2227 | static_cast<int>( |
| 2228 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2229 | (*requests())[2]->handle()->Reset(); |
| 2230 | (*requests())[3]->handle()->Reset(); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2231 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2232 | static_cast<int>( |
| 2233 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 2234 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2235 | (*requests())[1]->handle()->Reset(); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2236 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2237 | static_cast<int>( |
| 2238 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 2239 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2240 | (*requests())[0]->handle()->Reset(); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2241 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2242 | static_cast<int>( |
| 2243 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 2244 | } |
| 2245 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2246 | // When requests and ConnectJobs are not coupled, the request will get serviced |
| 2247 | // by whatever comes first. |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2248 | TEST_F(ClientSocketPoolBaseTest, ReleaseSockets) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2249 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2250 | |
| 2251 | // Start job 1 (async OK) |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 2252 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2253 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2254 | std::vector<TestSocketRequest*> request_order; |
| 2255 | size_t completion_count; // unused |
| 2256 | TestSocketRequest req1(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2257 | int rv = req1.handle()->Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2258 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2259 | ClientSocketPool::RespectLimits::ENABLED, req1.callback(), |
| 2260 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2261 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 2262 | EXPECT_THAT(req1.WaitForResult(), IsOk()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2263 | |
| 2264 | // Job 1 finished OK. Start job 2 (also async OK). Request 3 is pending |
| 2265 | // without a job. |
| 2266 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2267 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2268 | TestSocketRequest req2(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2269 | rv = req2.handle()->Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2270 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2271 | ClientSocketPool::RespectLimits::ENABLED, req2.callback(), |
| 2272 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2273 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2274 | TestSocketRequest req3(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2275 | rv = req3.handle()->Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2276 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2277 | ClientSocketPool::RespectLimits::ENABLED, req3.callback(), |
| 2278 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2279 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2280 | |
| 2281 | // Both Requests 2 and 3 are pending. We release socket 1 which should |
| 2282 | // service request 2. Request 3 should still be waiting. |
[email protected] | a6c59f6 | 2009-07-29 16:33:33 | [diff] [blame] | 2283 | req1.handle()->Reset(); |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 2284 | // Run the released socket wakeups. |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 2285 | base::RunLoop().RunUntilIdle(); |
[email protected] | a6c59f6 | 2009-07-29 16:33:33 | [diff] [blame] | 2286 | ASSERT_TRUE(req2.handle()->socket()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2287 | EXPECT_THAT(req2.WaitForResult(), IsOk()); |
[email protected] | a6c59f6 | 2009-07-29 16:33:33 | [diff] [blame] | 2288 | EXPECT_FALSE(req3.handle()->socket()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2289 | |
| 2290 | // Signal job 2, which should service request 3. |
| 2291 | |
| 2292 | client_socket_factory_.SignalJobs(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2293 | EXPECT_THAT(req3.WaitForResult(), IsOk()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2294 | |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2295 | ASSERT_EQ(3u, request_order.size()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2296 | EXPECT_EQ(&req1, request_order[0]); |
| 2297 | EXPECT_EQ(&req2, request_order[1]); |
| 2298 | EXPECT_EQ(&req3, request_order[2]); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2299 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2300 | } |
| 2301 | |
| 2302 | // The requests are not coupled to the jobs. So, the requests should finish in |
| 2303 | // their priority / insertion order. |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2304 | TEST_F(ClientSocketPoolBaseTest, PendingJobCompletionOrder) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2305 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2306 | // First two jobs are async. |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 2307 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2308 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2309 | std::vector<TestSocketRequest*> request_order; |
| 2310 | size_t completion_count; // unused |
| 2311 | TestSocketRequest req1(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2312 | int rv = req1.handle()->Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2313 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2314 | ClientSocketPool::RespectLimits::ENABLED, req1.callback(), |
| 2315 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2316 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2317 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2318 | TestSocketRequest req2(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2319 | rv = req2.handle()->Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2320 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2321 | ClientSocketPool::RespectLimits::ENABLED, req2.callback(), |
| 2322 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2323 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2324 | |
| 2325 | // The pending job is sync. |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 2326 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2327 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2328 | TestSocketRequest req3(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2329 | rv = req3.handle()->Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2330 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2331 | ClientSocketPool::RespectLimits::ENABLED, req3.callback(), |
| 2332 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2333 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2334 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2335 | EXPECT_THAT(req1.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 2336 | EXPECT_THAT(req2.WaitForResult(), IsOk()); |
| 2337 | EXPECT_THAT(req3.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2338 | |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2339 | ASSERT_EQ(3u, request_order.size()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2340 | EXPECT_EQ(&req1, request_order[0]); |
| 2341 | EXPECT_EQ(&req2, request_order[1]); |
| 2342 | EXPECT_EQ(&req3, request_order[2]); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2343 | } |
| 2344 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2345 | // Test GetLoadState in the case there's only one socket request. |
| 2346 | TEST_F(ClientSocketPoolBaseTest, LoadStateOneRequest) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2347 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2348 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2349 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2350 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2351 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2352 | int rv = handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2353 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2354 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2355 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2356 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2357 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2358 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2359 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE); |
| 2360 | EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle.GetLoadState()); |
| 2361 | |
| 2362 | // No point in completing the connection, since ClientSocketHandles only |
| 2363 | // expect the LoadState to be checked while connecting. |
| 2364 | } |
| 2365 | |
| 2366 | // Test GetLoadState in the case there are two socket requests. |
| 2367 | TEST_F(ClientSocketPoolBaseTest, LoadStateTwoRequests) { |
| 2368 | CreatePool(2, 2); |
| 2369 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2370 | |
| 2371 | ClientSocketHandle handle; |
| 2372 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2373 | int rv = handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2374 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2375 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2376 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2377 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2378 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_RESOLVING_HOST); |
| 2379 | |
| 2380 | ClientSocketHandle handle2; |
| 2381 | TestCompletionCallback callback2; |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2382 | rv = handle2.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2383 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2384 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2385 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2386 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2387 | client_socket_factory_.SetJobLoadState(1, LOAD_STATE_RESOLVING_HOST); |
| 2388 | |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 2389 | // Each handle should reflect the state of its own job. |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2390 | EXPECT_EQ(LOAD_STATE_RESOLVING_HOST, handle.GetLoadState()); |
| 2391 | EXPECT_EQ(LOAD_STATE_RESOLVING_HOST, handle2.GetLoadState()); |
| 2392 | |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 2393 | // Update the state of the first job. |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2394 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_CONNECTING); |
| 2395 | |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 2396 | // Only the state of the first request should have changed. |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2397 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2398 | EXPECT_EQ(LOAD_STATE_RESOLVING_HOST, handle2.GetLoadState()); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 2399 | |
| 2400 | // Update the state of the second job. |
| 2401 | client_socket_factory_.SetJobLoadState(1, LOAD_STATE_SSL_HANDSHAKE); |
| 2402 | |
| 2403 | // Only the state of the second request should have changed. |
| 2404 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
| 2405 | EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle2.GetLoadState()); |
| 2406 | |
| 2407 | // Second job connects and the first request gets the socket. The |
| 2408 | // second handle switches to the state of the remaining ConnectJob. |
| 2409 | client_socket_factory_.SignalJob(1); |
| 2410 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 2411 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle2.GetLoadState()); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2412 | } |
| 2413 | |
| 2414 | // Test GetLoadState in the case the per-group limit is reached. |
| 2415 | TEST_F(ClientSocketPoolBaseTest, LoadStateGroupLimit) { |
| 2416 | CreatePool(2, 1); |
| 2417 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2418 | |
| 2419 | ClientSocketHandle handle; |
| 2420 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2421 | int rv = handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2422 | TestGroupId("a"), params_, absl::nullopt, MEDIUM, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2423 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2424 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2425 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2426 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
| 2427 | |
| 2428 | // Request another socket from the same pool, buth with a higher priority. |
| 2429 | // The first request should now be stalled at the socket group limit. |
| 2430 | ClientSocketHandle handle2; |
| 2431 | TestCompletionCallback callback2; |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2432 | rv = handle2.Init(TestGroupId("a"), params_, absl::nullopt, HIGHEST, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2433 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2434 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2435 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2436 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2437 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET, handle.GetLoadState()); |
| 2438 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle2.GetLoadState()); |
| 2439 | |
| 2440 | // The first handle should remain stalled as the other socket goes through |
| 2441 | // the connect process. |
| 2442 | |
| 2443 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE); |
| 2444 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET, handle.GetLoadState()); |
| 2445 | EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle2.GetLoadState()); |
| 2446 | |
| 2447 | client_socket_factory_.SignalJob(0); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2448 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2449 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET, handle.GetLoadState()); |
| 2450 | |
| 2451 | // Closing the second socket should cause the stalled handle to finally get a |
| 2452 | // ConnectJob. |
| 2453 | handle2.socket()->Disconnect(); |
| 2454 | handle2.Reset(); |
| 2455 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
| 2456 | } |
| 2457 | |
| 2458 | // Test GetLoadState in the case the per-pool limit is reached. |
| 2459 | TEST_F(ClientSocketPoolBaseTest, LoadStatePoolLimit) { |
| 2460 | CreatePool(2, 2); |
| 2461 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2462 | |
| 2463 | ClientSocketHandle handle; |
| 2464 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2465 | int rv = handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2466 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2467 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2468 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2469 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2470 | |
| 2471 | // Request for socket from another pool. |
| 2472 | ClientSocketHandle handle2; |
| 2473 | TestCompletionCallback callback2; |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2474 | rv = handle2.Init(TestGroupId("b"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2475 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2476 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2477 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2478 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2479 | |
| 2480 | // Request another socket from the first pool. Request should stall at the |
| 2481 | // socket pool limit. |
| 2482 | ClientSocketHandle handle3; |
| 2483 | TestCompletionCallback callback3; |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2484 | rv = handle3.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2485 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2486 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2487 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2488 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2489 | |
| 2490 | // The third handle should remain stalled as the other sockets in its group |
| 2491 | // goes through the connect process. |
| 2492 | |
| 2493 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
| 2494 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL, handle3.GetLoadState()); |
| 2495 | |
| 2496 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE); |
| 2497 | EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle.GetLoadState()); |
| 2498 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL, handle3.GetLoadState()); |
| 2499 | |
| 2500 | client_socket_factory_.SignalJob(0); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2501 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2502 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL, handle3.GetLoadState()); |
| 2503 | |
| 2504 | // Closing a socket should allow the stalled handle to finally get a new |
| 2505 | // ConnectJob. |
| 2506 | handle.socket()->Disconnect(); |
| 2507 | handle.Reset(); |
| 2508 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle3.GetLoadState()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2509 | } |
| 2510 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2511 | TEST_F(ClientSocketPoolBaseTest, CertError) { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2512 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2513 | connect_job_factory_->set_job_type(TestConnectJob::kMockCertErrorJob); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2514 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2515 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2516 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2517 | EXPECT_EQ( |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2518 | ERR_CERT_COMMON_NAME_INVALID, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2519 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2520 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2521 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2522 | pool_.get(), NetLogWithSource())); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2523 | EXPECT_TRUE(handle.is_initialized()); |
| 2524 | EXPECT_TRUE(handle.socket()); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2525 | } |
| 2526 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2527 | TEST_F(ClientSocketPoolBaseTest, AsyncCertError) { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2528 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2529 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2530 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingCertErrorJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2531 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2532 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2533 | EXPECT_EQ( |
| 2534 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2535 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2536 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2537 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2538 | pool_.get(), NetLogWithSource())); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2539 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2540 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2541 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_CERT_COMMON_NAME_INVALID)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2542 | EXPECT_TRUE(handle.is_initialized()); |
| 2543 | EXPECT_TRUE(handle.socket()); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2544 | } |
| 2545 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2546 | TEST_F(ClientSocketPoolBaseTest, AdditionalErrorStateSynchronous) { |
| 2547 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2548 | connect_job_factory_->set_job_type( |
| 2549 | TestConnectJob::kMockAdditionalErrorStateJob); |
| 2550 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2551 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2552 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2553 | EXPECT_EQ( |
| 2554 | ERR_CONNECTION_FAILED, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2555 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2556 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2557 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2558 | pool_.get(), NetLogWithSource())); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2559 | EXPECT_FALSE(handle.is_initialized()); |
| 2560 | EXPECT_FALSE(handle.socket()); |
| 2561 | EXPECT_TRUE(handle.is_ssl_error()); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 2562 | EXPECT_TRUE(handle.ssl_cert_request_info()); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2563 | } |
| 2564 | |
| 2565 | TEST_F(ClientSocketPoolBaseTest, AdditionalErrorStateAsynchronous) { |
| 2566 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2567 | |
| 2568 | connect_job_factory_->set_job_type( |
| 2569 | TestConnectJob::kMockPendingAdditionalErrorStateJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2570 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2571 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2572 | EXPECT_EQ( |
| 2573 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2574 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2575 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2576 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2577 | pool_.get(), NetLogWithSource())); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2578 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2579 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2580 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2581 | EXPECT_FALSE(handle.is_initialized()); |
| 2582 | EXPECT_FALSE(handle.socket()); |
| 2583 | EXPECT_TRUE(handle.is_ssl_error()); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 2584 | EXPECT_TRUE(handle.ssl_cert_request_info()); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2585 | } |
| 2586 | |
martijn | 003cd61 | 2016-05-19 22:24:38 | [diff] [blame] | 2587 | // Make sure we can reuse sockets. |
| 2588 | TEST_F(ClientSocketPoolBaseTest, CleanupTimedOutIdleSocketsReuse) { |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2589 | CreatePoolWithIdleTimeouts( |
| 2590 | kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2591 | base::TimeDelta(), // Time out unused sockets immediately. |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 2592 | base::Days(1)); // Don't time out used sockets. |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2593 | |
| 2594 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2595 | |
| 2596 | ClientSocketHandle handle; |
| 2597 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2598 | int rv = handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2599 | TestGroupId("a"), params_, absl::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2600 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2601 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2602 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2603 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2604 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2605 | ASSERT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2606 | |
| 2607 | // Use and release the socket. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 2608 | EXPECT_EQ(1, handle.socket()->Write(nullptr, 1, CompletionOnceCallback(), |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 2609 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 2610 | TestLoadTimingInfoConnectedNotReused(handle); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2611 | handle.Reset(); |
| 2612 | |
| 2613 | // Should now have one idle socket. |
| 2614 | ASSERT_EQ(1, pool_->IdleSocketCount()); |
| 2615 | |
| 2616 | // Request a new socket. This should reuse the old socket and complete |
| 2617 | // synchronously. |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 2618 | RecordingBoundTestNetLog log; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2619 | rv = handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2620 | TestGroupId("a"), params_, absl::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2621 | ClientSocketPool::RespectLimits::ENABLED, CompletionOnceCallback(), |
| 2622 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2623 | ASSERT_THAT(rv, IsOk()); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2624 | EXPECT_TRUE(handle.is_reused()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 2625 | TestLoadTimingInfoConnectedReused(handle); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2626 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2627 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2628 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2629 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2630 | |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 2631 | auto entries = log.GetEntries(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2632 | EXPECT_TRUE(LogContainsEvent( |
| 2633 | entries, 0, NetLogEventType::TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET, |
| 2634 | NetLogEventPhase::NONE)); |
| 2635 | EXPECT_TRUE(LogContainsBeginEvent(entries, 1, NetLogEventType::SOCKET_POOL)); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2636 | EXPECT_TRUE(LogContainsEntryWithType( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2637 | entries, 2, NetLogEventType::SOCKET_POOL_REUSED_AN_EXISTING_SOCKET)); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2638 | } |
| 2639 | |
martijn | 003cd61 | 2016-05-19 22:24:38 | [diff] [blame] | 2640 | // Make sure we cleanup old unused sockets. |
Eric Roman | b49715e | 2018-04-24 22:41:17 | [diff] [blame] | 2641 | TEST_F(ClientSocketPoolBaseTest, CleanupTimedOutIdleSocketsNoReuse) { |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2642 | CreatePoolWithIdleTimeouts( |
| 2643 | kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 2644 | base::TimeDelta(), // Time out unused sockets immediately |
| 2645 | base::TimeDelta()); // Time out used sockets immediately |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2646 | |
| 2647 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2648 | |
| 2649 | // Startup two mock pending connect jobs, which will sit in the MessageLoop. |
| 2650 | |
| 2651 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2652 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2653 | int rv = handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2654 | TestGroupId("a"), params_, absl::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2655 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2656 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2657 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2658 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2659 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2660 | |
| 2661 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2662 | TestCompletionCallback callback2; |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2663 | rv = handle2.Init(TestGroupId("a"), params_, absl::nullopt, LOWEST, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2664 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2665 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2666 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2667 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2668 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2669 | pool_->GetLoadState(TestGroupId("a"), &handle2)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2670 | |
| 2671 | // Cancel one of the requests. Wait for the other, which will get the first |
| 2672 | // job. Release the socket. Run the loop again to make sure the second |
| 2673 | // socket is sitting idle and the first one is released (since ReleaseSocket() |
| 2674 | // just posts a DoReleaseSocket() task). |
| 2675 | |
| 2676 | handle.Reset(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2677 | ASSERT_THAT(callback2.WaitForResult(), IsOk()); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 2678 | // Get the NetLogSource for the socket, so the time out reason can be checked |
| 2679 | // at the end of the test. |
| 2680 | NetLogSource net_log_source2 = handle2.socket()->NetLog().source(); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2681 | // Use the socket. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 2682 | EXPECT_EQ(1, handle2.socket()->Write(nullptr, 1, CompletionOnceCallback(), |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 2683 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2684 | handle2.Reset(); |
| 2685 | |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2686 | // We post all of our delayed tasks with a 2ms delay. I.e. they don't |
| 2687 | // actually become pending until 2ms after they have been created. In order |
| 2688 | // to flush all tasks, we need to wait so that we know there are no |
| 2689 | // soon-to-be-pending tasks waiting. |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 2690 | FastForwardBy(base::Milliseconds(10)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2691 | |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2692 | // Both sockets should now be idle. |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2693 | ASSERT_EQ(2, pool_->IdleSocketCount()); |
| 2694 | |
| 2695 | // Request a new socket. This should cleanup the unused and timed out ones. |
| 2696 | // A new socket will be created rather than reusing the idle one. |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 2697 | RecordingBoundTestNetLog log; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2698 | TestCompletionCallback callback3; |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2699 | rv = handle.Init(TestGroupId("a"), params_, absl::nullopt, LOWEST, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2700 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2701 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2702 | pool_.get(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2703 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 2704 | ASSERT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2705 | EXPECT_FALSE(handle.is_reused()); |
| 2706 | |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2707 | // Make sure the idle socket is closed. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2708 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2709 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2710 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2711 | |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 2712 | auto entries = log.GetEntries(); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2713 | EXPECT_FALSE(LogContainsEntryWithType( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 2714 | entries, 1, NetLogEventType::SOCKET_POOL_REUSED_AN_EXISTING_SOCKET)); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 2715 | ExpectSocketClosedWithReason( |
| 2716 | net_log_source2, TransportClientSocketPool::kIdleTimeLimitExpired); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2717 | } |
| 2718 | |
[email protected] | 2041cf34 | 2010-02-19 03:15:59 | [diff] [blame] | 2719 | // Make sure that we process all pending requests even when we're stalling |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2720 | // because of multiple releasing disconnected sockets. |
| 2721 | TEST_F(ClientSocketPoolBaseTest, MultipleReleasingDisconnectedSockets) { |
| 2722 | CreatePoolWithIdleTimeouts( |
| 2723 | kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 2724 | base::TimeDelta(), // Time out unused sockets immediately. |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 2725 | base::Days(1)); // Don't time out used sockets. |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2726 | |
| 2727 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 2728 | |
| 2729 | // Startup 4 connect jobs. Two of them will be pending. |
| 2730 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2731 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2732 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2733 | int rv = handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2734 | TestGroupId("a"), params_, absl::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2735 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2736 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2737 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2738 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2739 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2740 | TestCompletionCallback callback2; |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2741 | rv = handle2.Init(TestGroupId("a"), params_, absl::nullopt, LOWEST, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2742 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2743 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2744 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2745 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2746 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2747 | ClientSocketHandle handle3; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2748 | TestCompletionCallback callback3; |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2749 | rv = handle3.Init(TestGroupId("a"), params_, absl::nullopt, LOWEST, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2750 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2751 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2752 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2753 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2754 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2755 | ClientSocketHandle handle4; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2756 | TestCompletionCallback callback4; |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2757 | rv = handle4.Init(TestGroupId("a"), params_, absl::nullopt, LOWEST, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2758 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2759 | callback4.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2760 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2761 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2762 | |
| 2763 | // Release two disconnected sockets. |
| 2764 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2765 | handle.socket()->Disconnect(); |
| 2766 | handle.Reset(); |
| 2767 | handle2.socket()->Disconnect(); |
| 2768 | handle2.Reset(); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2769 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2770 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2771 | EXPECT_FALSE(handle3.is_reused()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2772 | EXPECT_THAT(callback4.WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2773 | EXPECT_FALSE(handle4.is_reused()); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2774 | } |
| 2775 | |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2776 | // Regression test for http://crbug.com/42267. |
| 2777 | // When DoReleaseSocket() is processed for one socket, it is blocked because the |
| 2778 | // other stalled groups all have releasing sockets, so no progress can be made. |
| 2779 | TEST_F(ClientSocketPoolBaseTest, SocketLimitReleasingSockets) { |
| 2780 | CreatePoolWithIdleTimeouts( |
| 2781 | 4 /* socket limit */, 4 /* socket limit per group */, |
| 2782 | base::TimeDelta(), // Time out unused sockets immediately. |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 2783 | base::Days(1)); // Don't time out used sockets. |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2784 | |
| 2785 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 2786 | |
| 2787 | // Max out the socket limit with 2 per group. |
| 2788 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2789 | ClientSocketHandle handle_a[4]; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2790 | TestCompletionCallback callback_a[4]; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2791 | ClientSocketHandle handle_b[4]; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2792 | TestCompletionCallback callback_b[4]; |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2793 | |
| 2794 | for (int i = 0; i < 2; ++i) { |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2795 | EXPECT_EQ(OK, handle_a[i].Init(TestGroupId("a"), params_, absl::nullopt, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2796 | LOWEST, SocketTag(), |
| 2797 | ClientSocketPool::RespectLimits::ENABLED, |
| 2798 | callback_a[i].callback(), |
| 2799 | ClientSocketPool::ProxyAuthCallback(), |
| 2800 | pool_.get(), NetLogWithSource())); |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2801 | EXPECT_EQ(OK, handle_b[i].Init(TestGroupId("b"), params_, absl::nullopt, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2802 | LOWEST, SocketTag(), |
| 2803 | ClientSocketPool::RespectLimits::ENABLED, |
| 2804 | callback_b[i].callback(), |
| 2805 | ClientSocketPool::ProxyAuthCallback(), |
| 2806 | pool_.get(), NetLogWithSource())); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2807 | } |
[email protected] | b89f7e4 | 2010-05-20 20:37:00 | [diff] [blame] | 2808 | |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2809 | // Make 4 pending requests, 2 per group. |
| 2810 | |
| 2811 | for (int i = 2; i < 4; ++i) { |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2812 | EXPECT_EQ( |
| 2813 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2814 | handle_a[i].Init(TestGroupId("a"), params_, absl::nullopt, LOWEST, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2815 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2816 | callback_a[i].callback(), |
| 2817 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2818 | NetLogWithSource())); |
| 2819 | EXPECT_EQ( |
| 2820 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2821 | handle_b[i].Init(TestGroupId("b"), params_, absl::nullopt, LOWEST, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2822 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2823 | callback_b[i].callback(), |
| 2824 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2825 | NetLogWithSource())); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2826 | } |
| 2827 | |
| 2828 | // Release b's socket first. The order is important, because in |
| 2829 | // DoReleaseSocket(), we'll process b's released socket, and since both b and |
| 2830 | // a are stalled, but 'a' is lower lexicographically, we'll process group 'a' |
| 2831 | // first, which has a releasing socket, so it refuses to start up another |
| 2832 | // ConnectJob. So, we used to infinite loop on this. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2833 | handle_b[0].socket()->Disconnect(); |
| 2834 | handle_b[0].Reset(); |
| 2835 | handle_a[0].socket()->Disconnect(); |
| 2836 | handle_a[0].Reset(); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2837 | |
| 2838 | // Used to get stuck here. |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 2839 | base::RunLoop().RunUntilIdle(); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2840 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2841 | handle_b[1].socket()->Disconnect(); |
| 2842 | handle_b[1].Reset(); |
| 2843 | handle_a[1].socket()->Disconnect(); |
| 2844 | handle_a[1].Reset(); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2845 | |
| 2846 | for (int i = 2; i < 4; ++i) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2847 | EXPECT_THAT(callback_b[i].WaitForResult(), IsOk()); |
| 2848 | EXPECT_THAT(callback_a[i].WaitForResult(), IsOk()); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2849 | } |
| 2850 | } |
| 2851 | |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2852 | TEST_F(ClientSocketPoolBaseTest, |
| 2853 | ReleasingDisconnectedSocketsMaintainsPriorityOrder) { |
| 2854 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2855 | |
| 2856 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2857 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2858 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2859 | IsError(ERR_IO_PENDING)); |
| 2860 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2861 | IsError(ERR_IO_PENDING)); |
| 2862 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2863 | IsError(ERR_IO_PENDING)); |
| 2864 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2865 | IsError(ERR_IO_PENDING)); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2866 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2867 | EXPECT_THAT((*requests())[0]->WaitForResult(), IsOk()); |
| 2868 | EXPECT_THAT((*requests())[1]->WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2869 | EXPECT_EQ(2u, completion_count()); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2870 | |
| 2871 | // Releases one connection. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2872 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::NO_KEEP_ALIVE)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2873 | EXPECT_THAT((*requests())[2]->WaitForResult(), IsOk()); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2874 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2875 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::NO_KEEP_ALIVE)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2876 | EXPECT_THAT((*requests())[3]->WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2877 | EXPECT_EQ(4u, completion_count()); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2878 | |
| 2879 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 2880 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 2881 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 2882 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 2883 | |
| 2884 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2885 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(5)); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2886 | } |
| 2887 | |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2888 | class TestReleasingSocketRequest : public TestCompletionCallbackBase { |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2889 | public: |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2890 | TestReleasingSocketRequest(TransportClientSocketPool* pool, |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2891 | int expected_result, |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2892 | bool reset_releasing_handle) |
| 2893 | : pool_(pool), |
| 2894 | expected_result_(expected_result), |
Bence Béky | 8ddc249 | 2018-06-13 01:02:04 | [diff] [blame] | 2895 | reset_releasing_handle_(reset_releasing_handle) {} |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2896 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 2897 | ~TestReleasingSocketRequest() override = default; |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2898 | |
| 2899 | ClientSocketHandle* handle() { return &handle_; } |
| 2900 | |
Bence Béky | 8ddc249 | 2018-06-13 01:02:04 | [diff] [blame] | 2901 | CompletionOnceCallback callback() { |
| 2902 | return base::BindOnce(&TestReleasingSocketRequest::OnComplete, |
| 2903 | base::Unretained(this)); |
| 2904 | } |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2905 | |
| 2906 | private: |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2907 | void OnComplete(int result) { |
| 2908 | SetResult(result); |
| 2909 | if (reset_releasing_handle_) |
| 2910 | handle_.Reset(); |
| 2911 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2912 | EXPECT_EQ( |
| 2913 | expected_result_, |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 2914 | handle2_.Init( |
| 2915 | TestGroupId("a"), |
| 2916 | ClientSocketPool::SocketParams::CreateForHttpForTesting(), |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2917 | absl::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 2918 | ClientSocketPool::RespectLimits::ENABLED, CompletionOnceCallback(), |
| 2919 | ClientSocketPool::ProxyAuthCallback(), pool_, NetLogWithSource())); |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2920 | } |
| 2921 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2922 | TransportClientSocketPool* const pool_; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2923 | int expected_result_; |
| 2924 | bool reset_releasing_handle_; |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2925 | ClientSocketHandle handle_; |
| 2926 | ClientSocketHandle handle2_; |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2927 | }; |
| 2928 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2929 | |
| 2930 | TEST_F(ClientSocketPoolBaseTest, AdditionalErrorSocketsDontUseSlot) { |
| 2931 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2932 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2933 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
| 2934 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 2935 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2936 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2937 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2938 | client_socket_factory_.allocation_count()); |
| 2939 | |
| 2940 | connect_job_factory_->set_job_type( |
| 2941 | TestConnectJob::kMockPendingAdditionalErrorStateJob); |
| 2942 | TestReleasingSocketRequest req(pool_.get(), OK, false); |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2943 | EXPECT_EQ(ERR_IO_PENDING, |
| 2944 | req.handle()->Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2945 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2946 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2947 | req.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2948 | pool_.get(), NetLogWithSource())); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2949 | // The next job should complete synchronously |
| 2950 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 2951 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2952 | EXPECT_THAT(req.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2953 | EXPECT_FALSE(req.handle()->is_initialized()); |
| 2954 | EXPECT_FALSE(req.handle()->socket()); |
| 2955 | EXPECT_TRUE(req.handle()->is_ssl_error()); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 2956 | EXPECT_TRUE(req.handle()->ssl_cert_request_info()); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2957 | } |
| 2958 | |
[email protected] | b6501d3d | 2010-06-03 23:53:34 | [diff] [blame] | 2959 | // http://crbug.com/44724 regression test. |
| 2960 | // We start releasing the pool when we flush on network change. When that |
| 2961 | // happens, the only active references are in the ClientSocketHandles. When a |
| 2962 | // ConnectJob completes and calls back into the last ClientSocketHandle, that |
| 2963 | // callback can release the last reference and delete the pool. After the |
| 2964 | // callback finishes, we go back to the stack frame within the now-deleted pool. |
| 2965 | // Executing any code that refers to members of the now-deleted pool can cause |
| 2966 | // crashes. |
| 2967 | TEST_F(ClientSocketPoolBaseTest, CallbackThatReleasesPool) { |
| 2968 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2969 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
| 2970 | |
| 2971 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2972 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2973 | EXPECT_EQ( |
| 2974 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2975 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2976 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2977 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2978 | pool_.get(), NetLogWithSource())); |
[email protected] | b6501d3d | 2010-06-03 23:53:34 | [diff] [blame] | 2979 | |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 2980 | pool_->FlushWithError(ERR_NETWORK_CHANGED, "Network changed"); |
[email protected] | b6501d3d | 2010-06-03 23:53:34 | [diff] [blame] | 2981 | |
| 2982 | // We'll call back into this now. |
| 2983 | callback.WaitForResult(); |
| 2984 | } |
| 2985 | |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2986 | TEST_F(ClientSocketPoolBaseTest, DoNotReuseSocketAfterFlush) { |
| 2987 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2988 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2989 | |
| 2990 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2991 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2992 | EXPECT_EQ( |
| 2993 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2994 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2995 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2996 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2997 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2998 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2999 | EXPECT_EQ(ClientSocketHandle::UNUSED, handle.reuse_type()); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 3000 | NetLogSource source = handle.socket()->NetLog().source(); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 3001 | |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 3002 | pool_->FlushWithError(ERR_NETWORK_CHANGED, "Network changed"); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 3003 | |
| 3004 | handle.Reset(); |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3005 | base::RunLoop().RunUntilIdle(); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 3006 | |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3007 | EXPECT_EQ( |
| 3008 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3009 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3010 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 3011 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3012 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3013 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 3014 | EXPECT_EQ(ClientSocketHandle::UNUSED, handle.reuse_type()); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 3015 | |
| 3016 | ExpectSocketClosedWithReason( |
| 3017 | source, TransportClientSocketPool::kSocketGenerationOutOfDate); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 3018 | } |
| 3019 | |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3020 | class ConnectWithinCallback : public TestCompletionCallbackBase { |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 3021 | public: |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3022 | ConnectWithinCallback( |
| 3023 | const ClientSocketPool::GroupId& group_id, |
Matt Menke | 84d11e56 | 2019-03-27 00:11:19 | [diff] [blame] | 3024 | const scoped_refptr<ClientSocketPool::SocketParams>& params, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3025 | TransportClientSocketPool* pool) |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3026 | : group_id_(group_id), params_(params), pool_(pool) {} |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 3027 | |
Peter Boström | 293b134 | 2021-09-22 17:31:43 | [diff] [blame] | 3028 | ConnectWithinCallback(const ConnectWithinCallback&) = delete; |
| 3029 | ConnectWithinCallback& operator=(const ConnectWithinCallback&) = delete; |
| 3030 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 3031 | ~ConnectWithinCallback() override = default; |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 3032 | |
| 3033 | int WaitForNestedResult() { |
| 3034 | return nested_callback_.WaitForResult(); |
| 3035 | } |
| 3036 | |
Bence Béky | 8ddc249 | 2018-06-13 01:02:04 | [diff] [blame] | 3037 | CompletionOnceCallback callback() { |
| 3038 | return base::BindOnce(&ConnectWithinCallback::OnComplete, |
| 3039 | base::Unretained(this)); |
| 3040 | } |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3041 | |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 3042 | private: |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3043 | void OnComplete(int result) { |
| 3044 | SetResult(result); |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3045 | EXPECT_EQ( |
| 3046 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3047 | handle_.Init(group_id_, params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3048 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 3049 | nested_callback_.callback(), |
| 3050 | ClientSocketPool::ProxyAuthCallback(), pool_, |
| 3051 | NetLogWithSource())); |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3052 | } |
| 3053 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3054 | const ClientSocketPool::GroupId group_id_; |
Matt Menke | 84d11e56 | 2019-03-27 00:11:19 | [diff] [blame] | 3055 | const scoped_refptr<ClientSocketPool::SocketParams> params_; |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3056 | TransportClientSocketPool* const pool_; |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 3057 | ClientSocketHandle handle_; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3058 | TestCompletionCallback nested_callback_; |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 3059 | }; |
| 3060 | |
| 3061 | TEST_F(ClientSocketPoolBaseTest, AbortAllRequestsOnFlush) { |
| 3062 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3063 | |
| 3064 | // First job will be waiting until it gets aborted. |
| 3065 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3066 | |
| 3067 | ClientSocketHandle handle; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3068 | ConnectWithinCallback callback(TestGroupId("a"), params_, pool_.get()); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3069 | EXPECT_EQ( |
| 3070 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3071 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3072 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 3073 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3074 | pool_.get(), NetLogWithSource())); |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 3075 | |
| 3076 | // Second job will be started during the first callback, and will |
| 3077 | // asynchronously complete with OK. |
| 3078 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 3079 | pool_->FlushWithError(ERR_NETWORK_CHANGED, "Network changed"); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3080 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_NETWORK_CHANGED)); |
| 3081 | EXPECT_THAT(callback.WaitForNestedResult(), IsOk()); |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 3082 | } |
| 3083 | |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 3084 | TEST_F(ClientSocketPoolBaseTest, BackupSocketWaitsForHostResolution) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3085 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets, |
| 3086 | true /* enable_backup_connect_jobs */); |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 3087 | |
| 3088 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3089 | ClientSocketHandle handle; |
| 3090 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3091 | EXPECT_EQ( |
| 3092 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3093 | handle.Init(TestGroupId("bar"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3094 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 3095 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3096 | pool_.get(), NetLogWithSource())); |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 3097 | // The backup timer fires but doesn't start a new ConnectJob while resolving |
| 3098 | // the hostname. |
| 3099 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_RESOLVING_HOST); |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 3100 | FastForwardBy( |
| 3101 | base::Milliseconds(ClientSocketPool::kMaxConnectRetryIntervalMs * 100)); |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 3102 | EXPECT_EQ(1, client_socket_factory_.allocation_count()); |
| 3103 | |
| 3104 | // Once the ConnectJob has finished resolving the hostname, the backup timer |
| 3105 | // will create a ConnectJob when it fires. |
| 3106 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_CONNECTING); |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 3107 | FastForwardBy( |
| 3108 | base::Milliseconds(ClientSocketPool::kMaxConnectRetryIntervalMs)); |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 3109 | EXPECT_EQ(2, client_socket_factory_.allocation_count()); |
| 3110 | } |
| 3111 | |
| 3112 | // Test that no backup socket is created when a ConnectJob connects before it |
| 3113 | // completes. |
| 3114 | TEST_F(ClientSocketPoolBaseTest, NoBackupSocketWhenConnected) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3115 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets, |
| 3116 | true /* enable_backup_connect_jobs */); |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 3117 | |
| 3118 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3119 | ClientSocketHandle handle; |
| 3120 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3121 | EXPECT_EQ( |
| 3122 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3123 | handle.Init(TestGroupId("bar"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3124 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 3125 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3126 | pool_.get(), NetLogWithSource())); |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 3127 | // The backup timer fires but doesn't start a new ConnectJob while resolving |
| 3128 | // the hostname. |
| 3129 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_RESOLVING_HOST); |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 3130 | FastForwardBy( |
| 3131 | base::Milliseconds(ClientSocketPool::kMaxConnectRetryIntervalMs * 100)); |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 3132 | EXPECT_EQ(1, client_socket_factory_.allocation_count()); |
| 3133 | |
| 3134 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE); |
| 3135 | client_socket_factory_.SetJobHasEstablishedConnection(0); |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 3136 | FastForwardBy( |
| 3137 | base::Milliseconds(ClientSocketPool::kMaxConnectRetryIntervalMs * 100)); |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 3138 | EXPECT_EQ(1, client_socket_factory_.allocation_count()); |
| 3139 | } |
| 3140 | |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3141 | // Cancel a pending socket request while we're at max sockets, |
| 3142 | // and verify that the backup socket firing doesn't cause a crash. |
| 3143 | TEST_F(ClientSocketPoolBaseTest, BackupSocketCancelAtMaxSockets) { |
| 3144 | // Max 4 sockets globally, max 4 sockets per group. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3145 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets, |
| 3146 | true /* enable_backup_connect_jobs */); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3147 | |
[email protected] | 4baaf9d | 2010-08-31 15:15:44 | [diff] [blame] | 3148 | // Create the first socket and set to ERR_IO_PENDING. This starts the backup |
| 3149 | // timer. |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3150 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3151 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3152 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3153 | EXPECT_EQ( |
| 3154 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3155 | handle.Init(TestGroupId("bar"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3156 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 3157 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3158 | pool_.get(), NetLogWithSource())); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3159 | |
| 3160 | // Start (MaxSockets - 1) connected sockets to reach max sockets. |
| 3161 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 3162 | ClientSocketHandle handles[kDefaultMaxSockets]; |
| 3163 | for (int i = 1; i < kDefaultMaxSockets; ++i) { |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3164 | EXPECT_EQ(OK, handles[i].Init(TestGroupId("bar"), params_, absl::nullopt, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3165 | DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3166 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3167 | callback.callback(), |
| 3168 | ClientSocketPool::ProxyAuthCallback(), |
| 3169 | pool_.get(), NetLogWithSource())); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3170 | } |
| 3171 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3172 | base::RunLoop().RunUntilIdle(); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3173 | |
| 3174 | // Cancel the pending request. |
| 3175 | handle.Reset(); |
| 3176 | |
| 3177 | // Wait for the backup timer to fire (add some slop to ensure it fires) |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 3178 | FastForwardBy( |
| 3179 | base::Milliseconds(ClientSocketPool::kMaxConnectRetryIntervalMs / 2 * 3)); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3180 | |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3181 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 3182 | } |
| 3183 | |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3184 | TEST_F(ClientSocketPoolBaseTest, CancelBackupSocketAfterCancelingAllRequests) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3185 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets, |
| 3186 | true /* enable_backup_connect_jobs */); |
[email protected] | 4baaf9d | 2010-08-31 15:15:44 | [diff] [blame] | 3187 | |
| 3188 | // Create the first socket and set to ERR_IO_PENDING. This starts the backup |
| 3189 | // timer. |
| 3190 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3191 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3192 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3193 | EXPECT_EQ( |
| 3194 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3195 | handle.Init(TestGroupId("bar"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3196 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 3197 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3198 | pool_.get(), NetLogWithSource())); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3199 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("bar"))); |
| 3200 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("bar"))); |
| 3201 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3202 | TestGroupId("bar"))); |
| 3203 | EXPECT_EQ( |
| 3204 | 0u, pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("bar"))); |
[email protected] | 4baaf9d | 2010-08-31 15:15:44 | [diff] [blame] | 3205 | |
| 3206 | // Cancel the socket request. This should cancel the backup timer. Wait for |
| 3207 | // the backup time to see if it indeed got canceled. |
| 3208 | handle.Reset(); |
| 3209 | // Wait for the backup timer to fire (add some slop to ensure it fires) |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 3210 | FastForwardBy( |
| 3211 | base::Milliseconds(ClientSocketPool::kMaxConnectRetryIntervalMs / 2 * 3)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3212 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("bar"))); |
| 3213 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("bar"))); |
[email protected] | 4baaf9d | 2010-08-31 15:15:44 | [diff] [blame] | 3214 | } |
| 3215 | |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3216 | TEST_F(ClientSocketPoolBaseTest, CancelBackupSocketAfterFinishingAllRequests) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3217 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets, |
| 3218 | true /* enable_backup_connect_jobs */); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3219 | |
| 3220 | // Create the first socket and set to ERR_IO_PENDING. This starts the backup |
| 3221 | // timer. |
| 3222 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3223 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3224 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3225 | EXPECT_EQ( |
| 3226 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3227 | handle.Init(TestGroupId("bar"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3228 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 3229 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3230 | pool_.get(), NetLogWithSource())); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3231 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3232 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3233 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3234 | EXPECT_EQ( |
| 3235 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3236 | handle2.Init(TestGroupId("bar"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3237 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3238 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3239 | pool_.get(), NetLogWithSource())); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3240 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("bar"))); |
| 3241 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("bar"))); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3242 | |
| 3243 | // Cancel request 1 and then complete request 2. With the requests finished, |
| 3244 | // the backup timer should be cancelled. |
| 3245 | handle.Reset(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3246 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3247 | // Wait for the backup timer to fire (add some slop to ensure it fires) |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 3248 | FastForwardBy( |
| 3249 | base::Milliseconds(ClientSocketPool::kMaxConnectRetryIntervalMs / 2 * 3)); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3250 | } |
| 3251 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3252 | // Test delayed socket binding for the case where we have two connects, |
| 3253 | // and while one is waiting on a connect, the other frees up. |
| 3254 | // The socket waiting on a connect should switch immediately to the freed |
| 3255 | // up socket. |
| 3256 | TEST_F(ClientSocketPoolBaseTest, DelayedSocketBindingWaitingForConnect) { |
| 3257 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3258 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3259 | |
| 3260 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3261 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3262 | EXPECT_EQ( |
| 3263 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3264 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3265 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3266 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3267 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3268 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3269 | |
| 3270 | // No idle sockets, no pending jobs. |
| 3271 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3272 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3273 | |
| 3274 | // Create a second socket to the same host, but this one will wait. |
| 3275 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3276 | ClientSocketHandle handle2; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3277 | EXPECT_EQ( |
| 3278 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3279 | handle2.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3280 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3281 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3282 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3283 | // No idle sockets, and one connecting job. |
| 3284 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3285 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3286 | |
| 3287 | // Return the first handle to the pool. This will initiate the delayed |
| 3288 | // binding. |
| 3289 | handle1.Reset(); |
| 3290 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3291 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3292 | |
| 3293 | // Still no idle sockets, still one pending connect job. |
| 3294 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3295 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3296 | |
| 3297 | // The second socket connected, even though it was a Waiting Job. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3298 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3299 | |
| 3300 | // And we can see there is still one job waiting. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3301 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3302 | |
| 3303 | // Finally, signal the waiting Connect. |
| 3304 | client_socket_factory_.SignalJobs(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3305 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3306 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3307 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3308 | } |
| 3309 | |
| 3310 | // Test delayed socket binding when a group is at capacity and one |
| 3311 | // of the group's sockets frees up. |
| 3312 | TEST_F(ClientSocketPoolBaseTest, DelayedSocketBindingAtGroupCapacity) { |
| 3313 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3314 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3315 | |
| 3316 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3317 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3318 | EXPECT_EQ( |
| 3319 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3320 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3321 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3322 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3323 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3324 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3325 | |
| 3326 | // No idle sockets, no pending jobs. |
| 3327 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3328 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3329 | |
| 3330 | // Create a second socket to the same host, but this one will wait. |
| 3331 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3332 | ClientSocketHandle handle2; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3333 | EXPECT_EQ( |
| 3334 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3335 | handle2.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3336 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3337 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3338 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3339 | // No idle sockets, and one connecting job. |
| 3340 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3341 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3342 | |
| 3343 | // Return the first handle to the pool. This will initiate the delayed |
| 3344 | // binding. |
| 3345 | handle1.Reset(); |
| 3346 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3347 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3348 | |
| 3349 | // Still no idle sockets, still one pending connect job. |
| 3350 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3351 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3352 | |
| 3353 | // The second socket connected, even though it was a Waiting Job. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3354 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3355 | |
| 3356 | // And we can see there is still one job waiting. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3357 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3358 | |
| 3359 | // Finally, signal the waiting Connect. |
| 3360 | client_socket_factory_.SignalJobs(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3361 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3362 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3363 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3364 | } |
| 3365 | |
| 3366 | // Test out the case where we have one socket connected, one |
| 3367 | // connecting, when the first socket finishes and goes idle. |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3368 | // Although the second connection is pending, the second request |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3369 | // should complete, by taking the first socket's idle socket. |
| 3370 | TEST_F(ClientSocketPoolBaseTest, DelayedSocketBindingAtStall) { |
| 3371 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3372 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3373 | |
| 3374 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3375 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3376 | EXPECT_EQ( |
| 3377 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3378 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3379 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3380 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3381 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3382 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3383 | |
| 3384 | // No idle sockets, no pending jobs. |
| 3385 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3386 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3387 | |
| 3388 | // Create a second socket to the same host, but this one will wait. |
| 3389 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3390 | ClientSocketHandle handle2; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3391 | EXPECT_EQ( |
| 3392 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3393 | handle2.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3394 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3395 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3396 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3397 | // No idle sockets, and one connecting job. |
| 3398 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3399 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3400 | |
| 3401 | // Return the first handle to the pool. This will initiate the delayed |
| 3402 | // binding. |
| 3403 | handle1.Reset(); |
| 3404 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3405 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3406 | |
| 3407 | // Still no idle sockets, still one pending connect job. |
| 3408 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3409 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3410 | |
| 3411 | // The second socket connected, even though it was a Waiting Job. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3412 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3413 | |
| 3414 | // And we can see there is still one job waiting. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3415 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3416 | |
| 3417 | // Finally, signal the waiting Connect. |
| 3418 | client_socket_factory_.SignalJobs(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3419 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3420 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3421 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3422 | } |
| 3423 | |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3424 | // Cover the case where on an available socket slot, we have one pending |
| 3425 | // request that completes synchronously, thereby making the Group empty. |
| 3426 | TEST_F(ClientSocketPoolBaseTest, SynchronouslyProcessOnePendingRequest) { |
| 3427 | const int kUnlimitedSockets = 100; |
| 3428 | const int kOneSocketPerGroup = 1; |
| 3429 | CreatePool(kUnlimitedSockets, kOneSocketPerGroup); |
| 3430 | |
| 3431 | // Make the first request asynchronous fail. |
| 3432 | // This will free up a socket slot later. |
| 3433 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
| 3434 | |
| 3435 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3436 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3437 | EXPECT_EQ( |
| 3438 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3439 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3440 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3441 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3442 | pool_.get(), NetLogWithSource())); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3443 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3444 | |
| 3445 | // Make the second request synchronously fail. This should make the Group |
| 3446 | // empty. |
| 3447 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
| 3448 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3449 | TestCompletionCallback callback2; |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3450 | // It'll be ERR_IO_PENDING now, but the TestConnectJob will synchronously fail |
| 3451 | // when created. |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3452 | EXPECT_EQ( |
| 3453 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3454 | handle2.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3455 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3456 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3457 | pool_.get(), NetLogWithSource())); |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3458 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3459 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3460 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3461 | EXPECT_THAT(callback1.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 3462 | EXPECT_THAT(callback2.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3463 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3464 | } |
| 3465 | |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3466 | TEST_F(ClientSocketPoolBaseTest, PreferUsedSocketToUnusedSocket) { |
| 3467 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 3468 | |
| 3469 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3470 | |
| 3471 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3472 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3473 | EXPECT_EQ( |
| 3474 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3475 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3476 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3477 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3478 | pool_.get(), NetLogWithSource())); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3479 | |
| 3480 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3481 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3482 | EXPECT_EQ( |
| 3483 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3484 | handle2.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3485 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3486 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3487 | pool_.get(), NetLogWithSource())); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3488 | ClientSocketHandle handle3; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3489 | TestCompletionCallback callback3; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3490 | EXPECT_EQ( |
| 3491 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3492 | handle3.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3493 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3494 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3495 | pool_.get(), NetLogWithSource())); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3496 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3497 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3498 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
| 3499 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3500 | |
| 3501 | // Use the socket. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 3502 | EXPECT_EQ(1, handle1.socket()->Write(nullptr, 1, CompletionOnceCallback(), |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 3503 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 3504 | EXPECT_EQ(1, handle3.socket()->Write(nullptr, 1, CompletionOnceCallback(), |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 3505 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3506 | |
| 3507 | handle1.Reset(); |
| 3508 | handle2.Reset(); |
| 3509 | handle3.Reset(); |
| 3510 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3511 | EXPECT_EQ(OK, handle1.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3512 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3513 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3514 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3515 | pool_.get(), NetLogWithSource())); |
| 3516 | EXPECT_EQ(OK, handle2.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3517 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3518 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3519 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3520 | pool_.get(), NetLogWithSource())); |
| 3521 | EXPECT_EQ(OK, handle3.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3522 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3523 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3524 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3525 | pool_.get(), NetLogWithSource())); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3526 | |
| 3527 | EXPECT_TRUE(handle1.socket()->WasEverUsed()); |
| 3528 | EXPECT_TRUE(handle2.socket()->WasEverUsed()); |
| 3529 | EXPECT_FALSE(handle3.socket()->WasEverUsed()); |
| 3530 | } |
| 3531 | |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3532 | TEST_F(ClientSocketPoolBaseTest, RequestSockets) { |
| 3533 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3534 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3535 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3536 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 2, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3537 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3538 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3539 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3540 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3541 | EXPECT_EQ(2u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3542 | TestGroupId("a"))); |
| 3543 | EXPECT_EQ(2u, |
| 3544 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3545 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3546 | |
| 3547 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3548 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3549 | EXPECT_EQ( |
| 3550 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3551 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3552 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3553 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3554 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3555 | |
| 3556 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3557 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3558 | EXPECT_EQ( |
| 3559 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3560 | handle2.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3561 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3562 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3563 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3564 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3565 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3566 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3567 | TestGroupId("a"))); |
| 3568 | EXPECT_EQ(0u, |
| 3569 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3570 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3571 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3572 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3573 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3574 | handle1.Reset(); |
| 3575 | handle2.Reset(); |
| 3576 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3577 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3578 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3579 | TestGroupId("a"))); |
| 3580 | EXPECT_EQ(0u, |
| 3581 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3582 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3583 | } |
| 3584 | |
| 3585 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsWhenAlreadyHaveAConnectJob) { |
| 3586 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3587 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3588 | |
| 3589 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3590 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3591 | EXPECT_EQ( |
| 3592 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3593 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3594 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3595 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3596 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3597 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3598 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3599 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3600 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3601 | TestGroupId("a"))); |
| 3602 | EXPECT_EQ(0u, |
| 3603 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3604 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3605 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3606 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 2, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3607 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3608 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3609 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3610 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3611 | TestGroupId("a"))); |
| 3612 | EXPECT_EQ(1u, |
| 3613 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3614 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3615 | |
| 3616 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3617 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3618 | EXPECT_EQ( |
| 3619 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3620 | handle2.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3621 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3622 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3623 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3624 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3625 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3626 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3627 | TestGroupId("a"))); |
| 3628 | EXPECT_EQ(0u, |
| 3629 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3630 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3631 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3632 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3633 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3634 | handle1.Reset(); |
| 3635 | handle2.Reset(); |
| 3636 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3637 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3638 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3639 | TestGroupId("a"))); |
| 3640 | EXPECT_EQ(0u, |
| 3641 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3642 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3643 | } |
| 3644 | |
| 3645 | TEST_F(ClientSocketPoolBaseTest, |
| 3646 | RequestSocketsWhenAlreadyHaveMultipleConnectJob) { |
| 3647 | CreatePool(4, 4); |
| 3648 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3649 | |
| 3650 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3651 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3652 | EXPECT_EQ( |
| 3653 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3654 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3655 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3656 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3657 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3658 | |
| 3659 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3660 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3661 | EXPECT_EQ( |
| 3662 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3663 | handle2.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3664 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3665 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3666 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3667 | |
| 3668 | ClientSocketHandle handle3; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3669 | TestCompletionCallback callback3; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3670 | EXPECT_EQ( |
| 3671 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3672 | handle3.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3673 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3674 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3675 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3676 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3677 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3678 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3679 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3680 | TestGroupId("a"))); |
| 3681 | EXPECT_EQ(0u, |
| 3682 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3683 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3684 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3685 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 2, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3686 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3687 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3688 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3689 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3690 | TestGroupId("a"))); |
| 3691 | EXPECT_EQ(0u, |
| 3692 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3693 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3694 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3695 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3696 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
| 3697 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3698 | handle1.Reset(); |
| 3699 | handle2.Reset(); |
| 3700 | handle3.Reset(); |
| 3701 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3702 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3703 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3704 | TestGroupId("a"))); |
| 3705 | EXPECT_EQ(0u, |
| 3706 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3707 | EXPECT_EQ(3u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3708 | } |
| 3709 | |
| 3710 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsAtMaxSocketLimit) { |
| 3711 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 3712 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3713 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3714 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3715 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3716 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3717 | kDefaultMaxSockets, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3718 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3719 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3720 | EXPECT_EQ(kDefaultMaxSockets, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3721 | static_cast<int>( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3722 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
| 3723 | EXPECT_EQ( |
| 3724 | kDefaultMaxSockets, |
| 3725 | static_cast<int>(pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3726 | TestGroupId("a")))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3727 | EXPECT_EQ(kDefaultMaxSockets, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3728 | static_cast<int>(pool_->NumUnassignedConnectJobsInGroupForTesting( |
| 3729 | TestGroupId("a")))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3730 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3731 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("b"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3732 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3733 | pool_->RequestSockets(TestGroupId("b"), params_, absl::nullopt, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3734 | kDefaultMaxSockets, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3735 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3736 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("b"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3737 | } |
| 3738 | |
| 3739 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsHitMaxSocketLimit) { |
| 3740 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 3741 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3742 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3743 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3744 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3745 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3746 | kDefaultMaxSockets - 1, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3747 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3748 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3749 | EXPECT_EQ(kDefaultMaxSockets - 1, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3750 | static_cast<int>( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3751 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
| 3752 | EXPECT_EQ( |
| 3753 | kDefaultMaxSockets - 1, |
| 3754 | static_cast<int>(pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3755 | TestGroupId("a")))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3756 | EXPECT_EQ(kDefaultMaxSockets - 1, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3757 | static_cast<int>(pool_->NumUnassignedConnectJobsInGroupForTesting( |
| 3758 | TestGroupId("a")))); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 3759 | EXPECT_FALSE(pool_->IsStalled()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3760 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3761 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("b"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3762 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3763 | pool_->RequestSockets(TestGroupId("b"), params_, absl::nullopt, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3764 | kDefaultMaxSockets, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3765 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3766 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("b"))); |
| 3767 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 3768 | EXPECT_FALSE(pool_->IsStalled()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3769 | } |
| 3770 | |
| 3771 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsCountIdleSockets) { |
| 3772 | CreatePool(4, 4); |
| 3773 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3774 | |
| 3775 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3776 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3777 | EXPECT_EQ( |
| 3778 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3779 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3780 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3781 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3782 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3783 | ASSERT_THAT(callback1.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3784 | handle1.Reset(); |
| 3785 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3786 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3787 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3788 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3789 | TestGroupId("a"))); |
| 3790 | EXPECT_EQ(0u, |
| 3791 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3792 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3793 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3794 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 2, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3795 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3796 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3797 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3798 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3799 | TestGroupId("a"))); |
| 3800 | EXPECT_EQ(1u, |
| 3801 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3802 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3803 | } |
| 3804 | |
| 3805 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsCountActiveSockets) { |
| 3806 | CreatePool(4, 4); |
| 3807 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3808 | |
| 3809 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3810 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3811 | EXPECT_EQ( |
| 3812 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3813 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3814 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3815 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3816 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3817 | ASSERT_THAT(callback1.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3818 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3819 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3820 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3821 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3822 | TestGroupId("a"))); |
| 3823 | EXPECT_EQ(0u, |
| 3824 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3825 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3826 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3827 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3828 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 2, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3829 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3830 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3831 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3832 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3833 | TestGroupId("a"))); |
| 3834 | EXPECT_EQ(1u, |
| 3835 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3836 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3837 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3838 | } |
| 3839 | |
| 3840 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsSynchronous) { |
| 3841 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3842 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 3843 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3844 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3845 | kDefaultMaxSocketsPerGroup, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3846 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3847 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3848 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3849 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3850 | TestGroupId("a"))); |
| 3851 | EXPECT_EQ(0u, |
| 3852 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3853 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3854 | static_cast<int>(pool_->IdleSocketCountInGroup(TestGroupId("a")))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3855 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3856 | pool_->RequestSockets(TestGroupId("b"), params_, absl::nullopt, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3857 | kDefaultMaxSocketsPerGroup, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3858 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3859 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 3860 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3861 | TestGroupId("b"))); |
| 3862 | EXPECT_EQ(0u, |
| 3863 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3864 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3865 | static_cast<int>(pool_->IdleSocketCountInGroup(TestGroupId("b")))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3866 | } |
| 3867 | |
[email protected] | 3c819f52 | 2010-12-02 02:03:12 | [diff] [blame] | 3868 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsSynchronousError) { |
| 3869 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3870 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
| 3871 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3872 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3873 | kDefaultMaxSocketsPerGroup, NetLogWithSource()); |
[email protected] | 3c819f52 | 2010-12-02 02:03:12 | [diff] [blame] | 3874 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3875 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | fd2e53e | 2011-01-14 20:40:52 | [diff] [blame] | 3876 | |
| 3877 | connect_job_factory_->set_job_type( |
| 3878 | TestConnectJob::kMockAdditionalErrorStateJob); |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3879 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3880 | kDefaultMaxSocketsPerGroup, NetLogWithSource()); |
[email protected] | fd2e53e | 2011-01-14 20:40:52 | [diff] [blame] | 3881 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3882 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | 3c819f52 | 2010-12-02 02:03:12 | [diff] [blame] | 3883 | } |
| 3884 | |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3885 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsMultipleTimesDoesNothing) { |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3886 | CreatePool(4, 4); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3887 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3888 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3889 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 2, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3890 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3891 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3892 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3893 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3894 | EXPECT_EQ(2u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3895 | TestGroupId("a"))); |
| 3896 | EXPECT_EQ(2u, |
| 3897 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3898 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3899 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3900 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3901 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 2, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3902 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3903 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3904 | EXPECT_EQ(2u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3905 | TestGroupId("a"))); |
| 3906 | EXPECT_EQ(2u, |
| 3907 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3908 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3909 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3910 | |
| 3911 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3912 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3913 | EXPECT_EQ( |
| 3914 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3915 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3916 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3917 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3918 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3919 | |
| 3920 | client_socket_factory_.SignalJob(0); |
| 3921 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3922 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3923 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3924 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3925 | TestGroupId("a"))); |
| 3926 | EXPECT_EQ(1u, |
| 3927 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3928 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3929 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3930 | |
| 3931 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3932 | TestCompletionCallback callback2; |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3933 | EXPECT_EQ( |
| 3934 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3935 | handle2.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3936 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3937 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3938 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3939 | client_socket_factory_.SignalJob(0); |
| 3940 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3941 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3942 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3943 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3944 | TestGroupId("a"))); |
| 3945 | EXPECT_EQ(0u, |
| 3946 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3947 | EXPECT_EQ(2, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3948 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3949 | |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3950 | handle1.Reset(); |
| 3951 | handle2.Reset(); |
| 3952 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3953 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3954 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3955 | TestGroupId("a"))); |
| 3956 | EXPECT_EQ(0u, |
| 3957 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3958 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3959 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3960 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3961 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 2, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3962 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3963 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3964 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3965 | TestGroupId("a"))); |
| 3966 | EXPECT_EQ(0u, |
| 3967 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3968 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3969 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3970 | } |
| 3971 | |
| 3972 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsDifferentNumSockets) { |
| 3973 | CreatePool(4, 4); |
| 3974 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3975 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3976 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 1, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3977 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3978 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3979 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3980 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3981 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3982 | TestGroupId("a"))); |
| 3983 | EXPECT_EQ(1u, |
| 3984 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3985 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3986 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3987 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 2, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3988 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3989 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3990 | EXPECT_EQ(2u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3991 | TestGroupId("a"))); |
| 3992 | EXPECT_EQ(2u, |
| 3993 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3994 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3995 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3996 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 3, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3997 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3998 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3999 | EXPECT_EQ(3u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4000 | TestGroupId("a"))); |
| 4001 | EXPECT_EQ(3u, |
| 4002 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4003 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4004 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4005 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 1, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4006 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4007 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4008 | EXPECT_EQ(3u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4009 | TestGroupId("a"))); |
| 4010 | EXPECT_EQ(3u, |
| 4011 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4012 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4013 | } |
| 4014 | |
| 4015 | TEST_F(ClientSocketPoolBaseTest, PreconnectJobsTakenByNormalRequests) { |
| 4016 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4017 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4018 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4019 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 1, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4020 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4021 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4022 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 4023 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4024 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4025 | TestGroupId("a"))); |
| 4026 | EXPECT_EQ(1u, |
| 4027 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4028 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4029 | |
| 4030 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 4031 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4032 | EXPECT_EQ( |
| 4033 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4034 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4035 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4036 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4037 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4038 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4039 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4040 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4041 | TestGroupId("a"))); |
| 4042 | EXPECT_EQ(0u, |
| 4043 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4044 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4045 | |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4046 | client_socket_factory_.SignalJobs(); |
| 4047 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 4048 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4049 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4050 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4051 | TestGroupId("a"))); |
| 4052 | EXPECT_EQ(0u, |
| 4053 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4054 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4055 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4056 | |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4057 | // Make sure if a preconnected socket is not fully connected when a request |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 4058 | // starts, it has a connect start time. |
| 4059 | TestLoadTimingInfoConnectedNotReused(handle1); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4060 | handle1.Reset(); |
| 4061 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4062 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4063 | } |
| 4064 | |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 4065 | // Checks that fully connected preconnect jobs have no connect times, and are |
| 4066 | // marked as reused. |
| 4067 | TEST_F(ClientSocketPoolBaseTest, ConnectedPreconnectJobsHaveNoConnectTimes) { |
| 4068 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4069 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4070 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 1, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4071 | NetLogWithSource()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 4072 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4073 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 4074 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4075 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4076 | TestGroupId("a"))); |
| 4077 | EXPECT_EQ(0u, |
| 4078 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4079 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 4080 | |
| 4081 | ClientSocketHandle handle; |
| 4082 | TestCompletionCallback callback; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4083 | EXPECT_EQ(OK, handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4084 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4085 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4086 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4087 | pool_.get(), NetLogWithSource())); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 4088 | |
| 4089 | // Make sure the idle socket was used. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4090 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 4091 | |
| 4092 | TestLoadTimingInfoConnectedReused(handle); |
| 4093 | handle.Reset(); |
| 4094 | TestLoadTimingInfoNotConnected(handle); |
| 4095 | } |
| 4096 | |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4097 | // http://crbug.com/64940 regression test. |
| 4098 | TEST_F(ClientSocketPoolBaseTest, PreconnectClosesIdleSocketRemovesGroup) { |
| 4099 | const int kMaxTotalSockets = 3; |
| 4100 | const int kMaxSocketsPerGroup = 2; |
| 4101 | CreatePool(kMaxTotalSockets, kMaxSocketsPerGroup); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4102 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4103 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 4104 | // Note that group id ordering matters here. "a" comes before "b", so |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4105 | // CloseOneIdleSocket() will try to close "a"'s idle socket. |
| 4106 | |
| 4107 | // Set up one idle socket in "a". |
| 4108 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 4109 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4110 | EXPECT_EQ( |
| 4111 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4112 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4113 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4114 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4115 | pool_.get(), NetLogWithSource())); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4116 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 4117 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4118 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4119 | TestGroupId("a"))); |
| 4120 | EXPECT_EQ(0u, |
| 4121 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4122 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4123 | |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4124 | client_socket_factory_.SignalJobs(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4125 | ASSERT_THAT(callback1.WaitForResult(), IsOk()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4126 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4127 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4128 | TestGroupId("a"))); |
| 4129 | EXPECT_EQ(0u, |
| 4130 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4131 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4132 | |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4133 | handle1.Reset(); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4134 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4135 | |
| 4136 | // Set up two active sockets in "b". |
| 4137 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 4138 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4139 | EXPECT_EQ( |
| 4140 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4141 | handle1.Init(TestGroupId("b"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4142 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4143 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4144 | pool_.get(), NetLogWithSource())); |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4145 | EXPECT_EQ( |
| 4146 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4147 | handle2.Init(TestGroupId("b"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4148 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4149 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4150 | pool_.get(), NetLogWithSource())); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4151 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4152 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("b"))); |
| 4153 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 4154 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4155 | TestGroupId("b"))); |
| 4156 | EXPECT_EQ(0u, |
| 4157 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4158 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4159 | |
| 4160 | client_socket_factory_.SignalJobs(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4161 | ASSERT_THAT(callback1.WaitForResult(), IsOk()); |
| 4162 | ASSERT_THAT(callback2.WaitForResult(), IsOk()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4163 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4164 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4165 | TestGroupId("b"))); |
| 4166 | EXPECT_EQ(0u, |
| 4167 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 4168 | EXPECT_EQ(2, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4169 | |
| 4170 | // Now we have 1 idle socket in "a" and 2 active sockets in "b". This means |
| 4171 | // we've maxed out on sockets, since we set |kMaxTotalSockets| to 3. |
| 4172 | // Requesting 2 preconnected sockets for "a" should fail to allocate any more |
| 4173 | // sockets for "a", and "b" should still have 2 active sockets. |
| 4174 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4175 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 2, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4176 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4177 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4178 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4179 | TestGroupId("a"))); |
| 4180 | EXPECT_EQ(0u, |
| 4181 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4182 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4183 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 4184 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 4185 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4186 | TestGroupId("b"))); |
| 4187 | EXPECT_EQ(0u, |
| 4188 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4189 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4190 | EXPECT_EQ(2, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4191 | |
| 4192 | // Now release the 2 active sockets for "b". This will give us 1 idle socket |
| 4193 | // in "a" and 2 idle sockets in "b". Requesting 2 preconnected sockets for |
| 4194 | // "a" should result in closing 1 for "b". |
| 4195 | handle1.Reset(); |
| 4196 | handle2.Reset(); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4197 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4198 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4199 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4200 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 2, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4201 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4202 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4203 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4204 | TestGroupId("a"))); |
| 4205 | EXPECT_EQ(1u, |
| 4206 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4207 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4208 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 4209 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 4210 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4211 | TestGroupId("b"))); |
| 4212 | EXPECT_EQ(0u, |
| 4213 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4214 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4215 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4216 | } |
| 4217 | |
[email protected] | b7b8be4 | 2011-07-12 12:46:41 | [diff] [blame] | 4218 | TEST_F(ClientSocketPoolBaseTest, PreconnectWithoutBackupJob) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4219 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 4220 | true /* enable_backup_connect_jobs */); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4221 | |
| 4222 | // Make the ConnectJob hang until it times out, shorten the timeout. |
| 4223 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 4224 | connect_job_factory_->set_timeout_duration(base::Milliseconds(500)); |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4225 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 1, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4226 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4227 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4228 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4229 | TestGroupId("a"))); |
| 4230 | EXPECT_EQ(1u, |
| 4231 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4232 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4233 | |
[email protected] | b7b8be4 | 2011-07-12 12:46:41 | [diff] [blame] | 4234 | // Verify the backup timer doesn't create a backup job, by making |
| 4235 | // the backup job a pending job instead of a waiting job, so it |
| 4236 | // *would* complete if it were created. |
Lukasz Krakowiak | 28dcf9d6 | 2020-06-04 09:46:59 | [diff] [blame] | 4237 | base::RunLoop loop; |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4238 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 4239 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 4240 | FROM_HERE, loop.QuitClosure(), base::Seconds(1)); |
Lukasz Krakowiak | 28dcf9d6 | 2020-06-04 09:46:59 | [diff] [blame] | 4241 | loop.Run(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4242 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4243 | } |
| 4244 | |
[email protected] | b7b8be4 | 2011-07-12 12:46:41 | [diff] [blame] | 4245 | TEST_F(ClientSocketPoolBaseTest, PreconnectWithBackupJob) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4246 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 4247 | true /* enable_backup_connect_jobs */); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4248 | |
| 4249 | // Make the ConnectJob hang forever. |
| 4250 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4251 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 1, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4252 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4253 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4254 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4255 | TestGroupId("a"))); |
| 4256 | EXPECT_EQ(1u, |
| 4257 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4258 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 4259 | base::RunLoop().RunUntilIdle(); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4260 | |
| 4261 | // Make the backup job be a pending job, so it completes normally. |
| 4262 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 4263 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 4264 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4265 | EXPECT_EQ( |
| 4266 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4267 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4268 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4269 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4270 | pool_.get(), NetLogWithSource())); |
[email protected] | b7b8be4 | 2011-07-12 12:46:41 | [diff] [blame] | 4271 | // Timer has started, but the backup connect job shouldn't be created yet. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4272 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4273 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4274 | TestGroupId("a"))); |
| 4275 | EXPECT_EQ(0u, |
| 4276 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4277 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4278 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4279 | ASSERT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4280 | |
| 4281 | // The hung connect job should still be there, but everything else should be |
| 4282 | // complete. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4283 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4284 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4285 | TestGroupId("a"))); |
| 4286 | EXPECT_EQ(1u, |
| 4287 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4288 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4289 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4290 | } |
| 4291 | |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4292 | // Tests that a preconnect that starts out with unread data can still be used. |
| 4293 | // http://crbug.com/334467 |
| 4294 | TEST_F(ClientSocketPoolBaseTest, PreconnectWithUnreadData) { |
| 4295 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4296 | connect_job_factory_->set_job_type(TestConnectJob::kMockUnreadDataJob); |
| 4297 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4298 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 1, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4299 | NetLogWithSource()); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4300 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4301 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 4302 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4303 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4304 | TestGroupId("a"))); |
| 4305 | EXPECT_EQ(0u, |
| 4306 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4307 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4308 | |
| 4309 | // Fail future jobs to be sure that handle receives the preconnected socket |
| 4310 | // rather than closing it and making a new one. |
| 4311 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
| 4312 | ClientSocketHandle handle; |
| 4313 | TestCompletionCallback callback; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4314 | EXPECT_EQ(OK, handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4315 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4316 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4317 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4318 | pool_.get(), NetLogWithSource())); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4319 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4320 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 4321 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4322 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4323 | TestGroupId("a"))); |
| 4324 | EXPECT_EQ(0u, |
| 4325 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4326 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4327 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4328 | |
| 4329 | // Drain the pending read. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 4330 | EXPECT_EQ(1, handle.socket()->Read(nullptr, 1, CompletionOnceCallback())); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4331 | |
| 4332 | TestLoadTimingInfoConnectedReused(handle); |
| 4333 | handle.Reset(); |
| 4334 | |
| 4335 | // The socket should be usable now that it's idle again. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4336 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4337 | } |
| 4338 | |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4339 | TEST_F(ClientSocketPoolBaseTest, RequestGetsAssignedJob) { |
| 4340 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4341 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4342 | |
| 4343 | ClientSocketHandle handle1; |
| 4344 | TestCompletionCallback callback1; |
| 4345 | EXPECT_EQ( |
| 4346 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4347 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4348 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4349 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4350 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4351 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4352 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4353 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4354 | TestGroupId("a"))); |
| 4355 | EXPECT_EQ(0u, |
| 4356 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4357 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4358 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4359 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4360 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4361 | } |
| 4362 | |
| 4363 | TEST_F(ClientSocketPoolBaseTest, MultipleRequestsGetAssignedJobs) { |
| 4364 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4365 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4366 | |
| 4367 | ClientSocketHandle handle1; |
| 4368 | TestCompletionCallback callback1; |
| 4369 | EXPECT_EQ( |
| 4370 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4371 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4372 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4373 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4374 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4375 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4376 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4377 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4378 | TestGroupId("a"))); |
| 4379 | EXPECT_EQ(0u, |
| 4380 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4381 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4382 | |
| 4383 | ClientSocketHandle handle2; |
| 4384 | TestCompletionCallback callback2; |
| 4385 | EXPECT_EQ( |
| 4386 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4387 | handle2.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4388 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4389 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4390 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4391 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4392 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4393 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4394 | TestGroupId("a"))); |
| 4395 | EXPECT_EQ(0u, |
| 4396 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4397 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4398 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4399 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4400 | &handle1)); |
| 4401 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4402 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4403 | |
| 4404 | // One job completes. The other request should still have its job. |
| 4405 | client_socket_factory_.SignalJob(0); |
| 4406 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 4407 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4408 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4409 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4410 | TestGroupId("a"))); |
| 4411 | EXPECT_EQ(0u, |
| 4412 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4413 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4414 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4415 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4416 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4417 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4418 | } |
| 4419 | |
| 4420 | TEST_F(ClientSocketPoolBaseTest, PreconnectJobGetsAssignedToRequest) { |
| 4421 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4422 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4423 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4424 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 1, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4425 | NetLogWithSource()); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4426 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4427 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 4428 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4429 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4430 | TestGroupId("a"))); |
| 4431 | EXPECT_EQ(1u, |
| 4432 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4433 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4434 | |
| 4435 | ClientSocketHandle handle1; |
| 4436 | TestCompletionCallback callback1; |
| 4437 | EXPECT_EQ( |
| 4438 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4439 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4440 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4441 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4442 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4443 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4444 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4445 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4446 | TestGroupId("a"))); |
| 4447 | EXPECT_EQ(0u, |
| 4448 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4449 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4450 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4451 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4452 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4453 | } |
| 4454 | |
| 4455 | TEST_F(ClientSocketPoolBaseTest, HigherPriorityRequestStealsJob) { |
| 4456 | CreatePool(kDefaultMaxSockets, 1); |
| 4457 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4458 | |
| 4459 | ClientSocketHandle handle1; |
| 4460 | TestCompletionCallback callback1; |
| 4461 | EXPECT_EQ( |
| 4462 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4463 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4464 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4465 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4466 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4467 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4468 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4469 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4470 | TestGroupId("a"))); |
| 4471 | EXPECT_EQ(0u, |
| 4472 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4473 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4474 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4475 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4476 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4477 | |
| 4478 | // Insert a higher priority request |
| 4479 | ClientSocketHandle handle2; |
| 4480 | TestCompletionCallback callback2; |
| 4481 | EXPECT_EQ( |
| 4482 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4483 | handle2.Init(TestGroupId("a"), params_, absl::nullopt, HIGHEST, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4484 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4485 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4486 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4487 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4488 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4489 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4490 | TestGroupId("a"))); |
| 4491 | EXPECT_EQ(0u, |
| 4492 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4493 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4494 | |
| 4495 | // The highest priority request should steal the job from the default priority |
| 4496 | // request. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4497 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4498 | &handle2)); |
| 4499 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4500 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4501 | } |
| 4502 | |
| 4503 | TEST_F(ClientSocketPoolBaseTest, RequestStealsJobFromLowestRequestWithJob) { |
| 4504 | CreatePool(3, 3); |
| 4505 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4506 | |
| 4507 | ClientSocketHandle handle_lowest; |
| 4508 | TestCompletionCallback callback_lowest; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4509 | EXPECT_EQ( |
| 4510 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4511 | handle_lowest.Init(TestGroupId("a"), params_, absl::nullopt, LOWEST, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4512 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4513 | callback_lowest.callback(), |
| 4514 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4515 | NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4516 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4517 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4518 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4519 | TestGroupId("a"))); |
| 4520 | EXPECT_EQ(0u, |
| 4521 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4522 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4523 | |
| 4524 | ClientSocketHandle handle_highest; |
| 4525 | TestCompletionCallback callback_highest; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4526 | EXPECT_EQ( |
| 4527 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4528 | handle_highest.Init(TestGroupId("a"), params_, absl::nullopt, HIGHEST, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4529 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4530 | callback_highest.callback(), |
| 4531 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4532 | NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4533 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4534 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4535 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4536 | TestGroupId("a"))); |
| 4537 | EXPECT_EQ(0u, |
| 4538 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4539 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4540 | |
| 4541 | ClientSocketHandle handle_low; |
| 4542 | TestCompletionCallback callback_low; |
| 4543 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4544 | handle_low.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4545 | TestGroupId("a"), params_, absl::nullopt, LOW, SocketTag(), |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4546 | ClientSocketPool::RespectLimits::ENABLED, |
| 4547 | callback_low.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4548 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4549 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4550 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4551 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4552 | TestGroupId("a"))); |
| 4553 | EXPECT_EQ(0u, |
| 4554 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4555 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4556 | |
| 4557 | ClientSocketHandle handle_lowest2; |
| 4558 | TestCompletionCallback callback_lowest2; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4559 | EXPECT_EQ( |
| 4560 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4561 | handle_lowest2.Init(TestGroupId("a"), params_, absl::nullopt, LOWEST, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4562 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4563 | callback_lowest2.callback(), |
| 4564 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4565 | NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4566 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4567 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4568 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4569 | TestGroupId("a"))); |
| 4570 | EXPECT_EQ(0u, |
| 4571 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4572 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4573 | |
| 4574 | // The top three requests in the queue should have jobs. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4575 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4576 | &handle_highest)); |
| 4577 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4578 | &handle_low)); |
| 4579 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4580 | &handle_lowest)); |
| 4581 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting( |
| 4582 | TestGroupId("a"), &handle_lowest2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4583 | |
| 4584 | // Add another request with medium priority. It should steal the job from the |
| 4585 | // lowest priority request with a job. |
| 4586 | ClientSocketHandle handle_medium; |
| 4587 | TestCompletionCallback callback_medium; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4588 | EXPECT_EQ( |
| 4589 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4590 | handle_medium.Init(TestGroupId("a"), params_, absl::nullopt, MEDIUM, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4591 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4592 | callback_medium.callback(), |
| 4593 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4594 | NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4595 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4596 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4597 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4598 | TestGroupId("a"))); |
| 4599 | EXPECT_EQ(0u, |
| 4600 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4601 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
| 4602 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4603 | &handle_highest)); |
| 4604 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4605 | &handle_medium)); |
| 4606 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4607 | &handle_low)); |
| 4608 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4609 | &handle_lowest)); |
| 4610 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting( |
| 4611 | TestGroupId("a"), &handle_lowest2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4612 | } |
| 4613 | |
| 4614 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeRequestStealsJob) { |
| 4615 | CreatePool(kDefaultMaxSockets, 1); |
| 4616 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4617 | |
| 4618 | ClientSocketHandle handle1; |
| 4619 | TestCompletionCallback callback1; |
| 4620 | EXPECT_EQ( |
| 4621 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4622 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4623 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4624 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4625 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4626 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4627 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4628 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4629 | TestGroupId("a"))); |
| 4630 | EXPECT_EQ(0u, |
| 4631 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4632 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4633 | |
| 4634 | ClientSocketHandle handle2; |
| 4635 | TestCompletionCallback callback2; |
| 4636 | EXPECT_EQ( |
| 4637 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4638 | handle2.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4639 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4640 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4641 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4642 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4643 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4644 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4645 | TestGroupId("a"))); |
| 4646 | EXPECT_EQ(0u, |
| 4647 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4648 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4649 | |
| 4650 | // 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] | 4651 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4652 | &handle1)); |
| 4653 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4654 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4655 | |
| 4656 | // Reprioritizing the second request places it above the first, and it steals |
| 4657 | // the job from the first request. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4658 | pool_->SetPriority(TestGroupId("a"), &handle2, HIGHEST); |
| 4659 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4660 | &handle2)); |
| 4661 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4662 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4663 | } |
| 4664 | |
| 4665 | TEST_F(ClientSocketPoolBaseTest, CancelRequestReassignsJob) { |
| 4666 | CreatePool(kDefaultMaxSockets, 1); |
| 4667 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4668 | |
| 4669 | ClientSocketHandle handle1; |
| 4670 | TestCompletionCallback callback1; |
| 4671 | EXPECT_EQ( |
| 4672 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4673 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4674 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4675 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4676 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4677 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4678 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4679 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4680 | TestGroupId("a"))); |
| 4681 | EXPECT_EQ(0u, |
| 4682 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4683 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4684 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4685 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4686 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4687 | |
| 4688 | ClientSocketHandle handle2; |
| 4689 | TestCompletionCallback callback2; |
| 4690 | EXPECT_EQ( |
| 4691 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4692 | handle2.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4693 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4694 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4695 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4696 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4697 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4698 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4699 | TestGroupId("a"))); |
| 4700 | EXPECT_EQ(0u, |
| 4701 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4702 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4703 | |
| 4704 | // The second request doesn't get a job because we are the limit. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4705 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4706 | &handle1)); |
| 4707 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4708 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4709 | |
| 4710 | // The second request should get a job upon cancelling the first request. |
| 4711 | handle1.Reset(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4712 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4713 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4714 | TestGroupId("a"))); |
| 4715 | EXPECT_EQ(0u, |
| 4716 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4717 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4718 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4719 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4720 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4721 | } |
| 4722 | |
| 4723 | TEST_F(ClientSocketPoolBaseTest, JobCompletionReassignsJob) { |
| 4724 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4725 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4726 | |
| 4727 | ClientSocketHandle handle1; |
| 4728 | TestCompletionCallback callback1; |
| 4729 | EXPECT_EQ( |
| 4730 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4731 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, HIGHEST, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4732 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4733 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4734 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4735 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4736 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4737 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4738 | TestGroupId("a"))); |
| 4739 | EXPECT_EQ(0u, |
| 4740 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4741 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4742 | |
| 4743 | ClientSocketHandle handle2; |
| 4744 | TestCompletionCallback callback2; |
| 4745 | EXPECT_EQ( |
| 4746 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4747 | handle2.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4748 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4749 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4750 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4751 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4752 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4753 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4754 | TestGroupId("a"))); |
| 4755 | EXPECT_EQ(0u, |
| 4756 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4757 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4758 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4759 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4760 | &handle1)); |
| 4761 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4762 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4763 | |
| 4764 | // The lower-priority job completes first. The higher-priority request should |
| 4765 | // get the socket, and the lower-priority request should get the remaining |
| 4766 | // job. |
| 4767 | client_socket_factory_.SignalJob(1); |
| 4768 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4769 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4770 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4771 | TestGroupId("a"))); |
| 4772 | EXPECT_EQ(0u, |
| 4773 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4774 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4775 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4776 | EXPECT_TRUE(handle1.socket()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4777 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4778 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4779 | } |
| 4780 | |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 4781 | class MockLayeredPool : public HigherLayeredPool { |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4782 | public: |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4783 | MockLayeredPool(TransportClientSocketPool* pool, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4784 | const ClientSocketPool::GroupId& group_id) |
| 4785 | : pool_(pool), group_id_(group_id), can_release_connection_(true) { |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 4786 | pool_->AddHigherLayeredPool(this); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4787 | } |
| 4788 | |
Daniel Cheng | 4496d082 | 2018-04-26 21:52:15 | [diff] [blame] | 4789 | ~MockLayeredPool() override { pool_->RemoveHigherLayeredPool(this); } |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4790 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4791 | int RequestSocket(TransportClientSocketPool* pool) { |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4792 | return handle_.Init( |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 4793 | group_id_, ClientSocketPool::SocketParams::CreateForHttpForTesting(), |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4794 | absl::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4795 | ClientSocketPool::RespectLimits::ENABLED, callback_.callback(), |
| 4796 | ClientSocketPool::ProxyAuthCallback(), pool, NetLogWithSource()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4797 | } |
| 4798 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4799 | int RequestSocketWithoutLimits(TransportClientSocketPool* pool) { |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4800 | return handle_.Init( |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 4801 | group_id_, ClientSocketPool::SocketParams::CreateForHttpForTesting(), |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4802 | absl::nullopt, MAXIMUM_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4803 | ClientSocketPool::RespectLimits::DISABLED, callback_.callback(), |
| 4804 | ClientSocketPool::ProxyAuthCallback(), pool, NetLogWithSource()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4805 | } |
| 4806 | |
| 4807 | bool ReleaseOneConnection() { |
| 4808 | if (!handle_.is_initialized() || !can_release_connection_) { |
| 4809 | return false; |
| 4810 | } |
| 4811 | handle_.socket()->Disconnect(); |
| 4812 | handle_.Reset(); |
| 4813 | return true; |
| 4814 | } |
| 4815 | |
| 4816 | void set_can_release_connection(bool can_release_connection) { |
| 4817 | can_release_connection_ = can_release_connection; |
| 4818 | } |
| 4819 | |
| 4820 | MOCK_METHOD0(CloseOneIdleConnection, bool()); |
| 4821 | |
| 4822 | private: |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4823 | TransportClientSocketPool* const pool_; |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4824 | ClientSocketHandle handle_; |
| 4825 | TestCompletionCallback callback_; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4826 | const ClientSocketPool::GroupId group_id_; |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4827 | bool can_release_connection_; |
| 4828 | }; |
| 4829 | |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4830 | // Tests the basic case of closing an idle socket in a higher layered pool when |
| 4831 | // a new request is issued and the lower layer pool is stalled. |
| 4832 | TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketsHeldByLayeredPoolWhenNeeded) { |
| 4833 | CreatePool(1, 1); |
| 4834 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 4835 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4836 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("foo")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4837 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4838 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4839 | .WillOnce(Invoke(&mock_layered_pool, |
| 4840 | &MockLayeredPool::ReleaseOneConnection)); |
| 4841 | ClientSocketHandle handle; |
| 4842 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4843 | EXPECT_EQ( |
| 4844 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4845 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4846 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4847 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4848 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4849 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4850 | } |
| 4851 | |
Matt Menke | 83367864 | 2019-03-05 22:05:51 | [diff] [blame] | 4852 | // Tests the case that trying to close an idle socket in a higher layered pool |
| 4853 | // fails. |
| 4854 | TEST_F(ClientSocketPoolBaseTest, |
| 4855 | CloseIdleSocketsHeldByLayeredPoolWhenNeededFails) { |
| 4856 | CreatePool(1, 1); |
| 4857 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 4858 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4859 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("foo")); |
Matt Menke | 83367864 | 2019-03-05 22:05:51 | [diff] [blame] | 4860 | mock_layered_pool.set_can_release_connection(false); |
| 4861 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
| 4862 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4863 | .WillOnce(Invoke(&mock_layered_pool, |
| 4864 | &MockLayeredPool::ReleaseOneConnection)); |
| 4865 | ClientSocketHandle handle; |
| 4866 | TestCompletionCallback callback; |
| 4867 | EXPECT_EQ( |
| 4868 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4869 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4870 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4871 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4872 | pool_.get(), NetLogWithSource())); |
Matt Menke | 83367864 | 2019-03-05 22:05:51 | [diff] [blame] | 4873 | base::RunLoop().RunUntilIdle(); |
| 4874 | EXPECT_FALSE(callback.have_result()); |
| 4875 | } |
| 4876 | |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4877 | // Same as above, but the idle socket is in the same group as the stalled |
| 4878 | // socket, and closes the only other request in its group when closing requests |
| 4879 | // in higher layered pools. This generally shouldn't happen, but it may be |
| 4880 | // possible if a higher level pool issues a request and the request is |
| 4881 | // subsequently cancelled. Even if it's not possible, best not to crash. |
| 4882 | TEST_F(ClientSocketPoolBaseTest, |
| 4883 | CloseIdleSocketsHeldByLayeredPoolWhenNeededSameGroup) { |
| 4884 | CreatePool(2, 2); |
| 4885 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 4886 | |
| 4887 | // Need a socket in another group for the pool to be stalled (If a group |
| 4888 | // has the maximum number of connections already, it's not stalled). |
| 4889 | ClientSocketHandle handle1; |
| 4890 | TestCompletionCallback callback1; |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4891 | EXPECT_EQ(OK, handle1.Init(TestGroupId("group1"), params_, absl::nullopt, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4892 | DEFAULT_PRIORITY, SocketTag(), |
| 4893 | ClientSocketPool::RespectLimits::ENABLED, |
| 4894 | callback1.callback(), |
| 4895 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4896 | NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4897 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4898 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("group2")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4899 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4900 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4901 | .WillOnce(Invoke(&mock_layered_pool, |
| 4902 | &MockLayeredPool::ReleaseOneConnection)); |
| 4903 | ClientSocketHandle handle; |
| 4904 | TestCompletionCallback callback2; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4905 | EXPECT_EQ(ERR_IO_PENDING, |
| 4906 | handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4907 | TestGroupId("group2"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4908 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4909 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4910 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4911 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4912 | } |
| 4913 | |
| 4914 | // Tests the case when an idle socket can be closed when a new request is |
| 4915 | // issued, and the new request belongs to a group that was previously stalled. |
| 4916 | TEST_F(ClientSocketPoolBaseTest, |
| 4917 | CloseIdleSocketsHeldByLayeredPoolInSameGroupWhenNeeded) { |
| 4918 | CreatePool(2, 2); |
| 4919 | std::list<TestConnectJob::JobType> job_types; |
| 4920 | job_types.push_back(TestConnectJob::kMockJob); |
| 4921 | job_types.push_back(TestConnectJob::kMockJob); |
| 4922 | job_types.push_back(TestConnectJob::kMockJob); |
| 4923 | job_types.push_back(TestConnectJob::kMockJob); |
| 4924 | connect_job_factory_->set_job_types(&job_types); |
| 4925 | |
| 4926 | ClientSocketHandle handle1; |
| 4927 | TestCompletionCallback callback1; |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4928 | EXPECT_EQ(OK, handle1.Init(TestGroupId("group1"), params_, absl::nullopt, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4929 | DEFAULT_PRIORITY, SocketTag(), |
| 4930 | ClientSocketPool::RespectLimits::ENABLED, |
| 4931 | callback1.callback(), |
| 4932 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4933 | NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4934 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4935 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("group2")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4936 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4937 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4938 | .WillRepeatedly(Invoke(&mock_layered_pool, |
| 4939 | &MockLayeredPool::ReleaseOneConnection)); |
| 4940 | mock_layered_pool.set_can_release_connection(false); |
| 4941 | |
| 4942 | // The third request is made when the socket pool is in a stalled state. |
| 4943 | ClientSocketHandle handle3; |
| 4944 | TestCompletionCallback callback3; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4945 | EXPECT_EQ(ERR_IO_PENDING, |
| 4946 | handle3.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4947 | TestGroupId("group3"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4948 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4949 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4950 | pool_.get(), NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4951 | |
| 4952 | base::RunLoop().RunUntilIdle(); |
| 4953 | EXPECT_FALSE(callback3.have_result()); |
| 4954 | |
| 4955 | // The fourth request is made when the pool is no longer stalled. The third |
| 4956 | // request should be serviced first, since it was issued first and has the |
| 4957 | // same priority. |
| 4958 | mock_layered_pool.set_can_release_connection(true); |
| 4959 | ClientSocketHandle handle4; |
| 4960 | TestCompletionCallback callback4; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4961 | EXPECT_EQ(ERR_IO_PENDING, |
| 4962 | handle4.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4963 | TestGroupId("group3"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4964 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4965 | callback4.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4966 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4967 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4968 | EXPECT_FALSE(callback4.have_result()); |
| 4969 | |
| 4970 | // Closing a handle should free up another socket slot. |
| 4971 | handle1.Reset(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4972 | EXPECT_THAT(callback4.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4973 | } |
| 4974 | |
| 4975 | // Tests the case when an idle socket can be closed when a new request is |
| 4976 | // issued, and the new request belongs to a group that was previously stalled. |
| 4977 | // |
| 4978 | // The two differences from the above test are that the stalled requests are not |
| 4979 | // in the same group as the layered pool's request, and the the fourth request |
| 4980 | // has a higher priority than the third one, so gets a socket first. |
| 4981 | TEST_F(ClientSocketPoolBaseTest, |
| 4982 | CloseIdleSocketsHeldByLayeredPoolInSameGroupWhenNeeded2) { |
| 4983 | CreatePool(2, 2); |
| 4984 | std::list<TestConnectJob::JobType> job_types; |
| 4985 | job_types.push_back(TestConnectJob::kMockJob); |
| 4986 | job_types.push_back(TestConnectJob::kMockJob); |
| 4987 | job_types.push_back(TestConnectJob::kMockJob); |
| 4988 | job_types.push_back(TestConnectJob::kMockJob); |
| 4989 | connect_job_factory_->set_job_types(&job_types); |
| 4990 | |
| 4991 | ClientSocketHandle handle1; |
| 4992 | TestCompletionCallback callback1; |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4993 | EXPECT_EQ(OK, handle1.Init(TestGroupId("group1"), params_, absl::nullopt, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4994 | DEFAULT_PRIORITY, SocketTag(), |
| 4995 | ClientSocketPool::RespectLimits::ENABLED, |
| 4996 | callback1.callback(), |
| 4997 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4998 | NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4999 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5000 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("group2")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5001 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 5002 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 5003 | .WillRepeatedly(Invoke(&mock_layered_pool, |
| 5004 | &MockLayeredPool::ReleaseOneConnection)); |
| 5005 | mock_layered_pool.set_can_release_connection(false); |
| 5006 | |
| 5007 | // The third request is made when the socket pool is in a stalled state. |
| 5008 | ClientSocketHandle handle3; |
| 5009 | TestCompletionCallback callback3; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 5010 | EXPECT_EQ( |
| 5011 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 5012 | handle3.Init(TestGroupId("group3"), params_, absl::nullopt, MEDIUM, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5013 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 5014 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5015 | pool_.get(), NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 5016 | |
| 5017 | base::RunLoop().RunUntilIdle(); |
| 5018 | EXPECT_FALSE(callback3.have_result()); |
| 5019 | |
| 5020 | // The fourth request is made when the pool is no longer stalled. This |
| 5021 | // request has a higher priority than the third request, so is serviced first. |
| 5022 | mock_layered_pool.set_can_release_connection(true); |
| 5023 | ClientSocketHandle handle4; |
| 5024 | TestCompletionCallback callback4; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 5025 | EXPECT_EQ( |
| 5026 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 5027 | handle4.Init(TestGroupId("group3"), params_, absl::nullopt, HIGHEST, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5028 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 5029 | callback4.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5030 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5031 | EXPECT_THAT(callback4.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 5032 | EXPECT_FALSE(callback3.have_result()); |
| 5033 | |
| 5034 | // Closing a handle should free up another socket slot. |
| 5035 | handle1.Reset(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5036 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 5037 | } |
| 5038 | |
| 5039 | TEST_F(ClientSocketPoolBaseTest, |
| 5040 | CloseMultipleIdleSocketsHeldByLayeredPoolWhenNeeded) { |
| 5041 | CreatePool(1, 1); |
| 5042 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 5043 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5044 | MockLayeredPool mock_layered_pool1(pool_.get(), TestGroupId("foo")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5045 | EXPECT_THAT(mock_layered_pool1.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 5046 | EXPECT_CALL(mock_layered_pool1, CloseOneIdleConnection()) |
| 5047 | .WillRepeatedly(Invoke(&mock_layered_pool1, |
| 5048 | &MockLayeredPool::ReleaseOneConnection)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5049 | MockLayeredPool mock_layered_pool2(pool_.get(), TestGroupId("bar")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5050 | EXPECT_THAT(mock_layered_pool2.RequestSocketWithoutLimits(pool_.get()), |
| 5051 | IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 5052 | EXPECT_CALL(mock_layered_pool2, CloseOneIdleConnection()) |
| 5053 | .WillRepeatedly(Invoke(&mock_layered_pool2, |
| 5054 | &MockLayeredPool::ReleaseOneConnection)); |
| 5055 | ClientSocketHandle handle; |
| 5056 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 5057 | EXPECT_EQ( |
| 5058 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 5059 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5060 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5061 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5062 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5063 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 5064 | } |
| 5065 | |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5066 | // Test that when a socket pool and group are at their limits, a request |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5067 | // with RespectLimits::DISABLED triggers creation of a new socket, and gets the |
| 5068 | // socket instead of a request with the same priority that was issued earlier, |
| 5069 | // but has RespectLimits::ENABLED. |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5070 | TEST_F(ClientSocketPoolBaseTest, IgnoreLimits) { |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5071 | CreatePool(1, 1); |
| 5072 | |
| 5073 | // Issue a request to reach the socket pool limit. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5074 | EXPECT_EQ(OK, StartRequestWithIgnoreLimits( |
| 5075 | TestGroupId("a"), MAXIMUM_PRIORITY, |
| 5076 | ClientSocketPool::RespectLimits::ENABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5077 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5078 | |
| 5079 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 5080 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5081 | EXPECT_EQ(ERR_IO_PENDING, StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5082 | TestGroupId("a"), MAXIMUM_PRIORITY, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5083 | ClientSocketPool::RespectLimits::ENABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5084 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5085 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5086 | // Issue a request that ignores the limits, so a new ConnectJob is |
| 5087 | // created. |
| 5088 | EXPECT_EQ(ERR_IO_PENDING, StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5089 | TestGroupId("a"), MAXIMUM_PRIORITY, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5090 | ClientSocketPool::RespectLimits::DISABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5091 | ASSERT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5092 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5093 | EXPECT_THAT(request(2)->WaitForResult(), IsOk()); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5094 | EXPECT_FALSE(request(1)->have_result()); |
| 5095 | } |
| 5096 | |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 5097 | // Test that when a socket pool and group are at their limits, a ConnectJob |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5098 | // issued for a request with RespectLimits::DISABLED is not cancelled when a |
| 5099 | // request with RespectLimits::ENABLED issued to the same group is cancelled. |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 5100 | TEST_F(ClientSocketPoolBaseTest, IgnoreLimitsCancelOtherJob) { |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 5101 | CreatePool(1, 1); |
| 5102 | |
| 5103 | // Issue a request to reach the socket pool limit. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5104 | EXPECT_EQ(OK, StartRequestWithIgnoreLimits( |
| 5105 | TestGroupId("a"), MAXIMUM_PRIORITY, |
| 5106 | ClientSocketPool::RespectLimits::ENABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5107 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 5108 | |
| 5109 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 5110 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5111 | EXPECT_EQ(ERR_IO_PENDING, StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5112 | TestGroupId("a"), MAXIMUM_PRIORITY, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5113 | ClientSocketPool::RespectLimits::ENABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5114 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 5115 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5116 | // Issue a request with RespectLimits::DISABLED, so a new ConnectJob is |
| 5117 | // created. |
| 5118 | EXPECT_EQ(ERR_IO_PENDING, StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5119 | TestGroupId("a"), MAXIMUM_PRIORITY, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5120 | ClientSocketPool::RespectLimits::DISABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5121 | ASSERT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5122 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5123 | // Cancel the pending request with RespectLimits::ENABLED. The ConnectJob |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5124 | // should not be cancelled. |
| 5125 | request(1)->handle()->Reset(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5126 | ASSERT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5127 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5128 | EXPECT_THAT(request(2)->WaitForResult(), IsOk()); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5129 | EXPECT_FALSE(request(1)->have_result()); |
| 5130 | } |
| 5131 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5132 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthNoAuthCallback) { |
| 5133 | CreatePool(1, 1); |
| 5134 | |
| 5135 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5136 | |
| 5137 | ClientSocketHandle handle; |
| 5138 | TestCompletionCallback callback; |
| 5139 | EXPECT_EQ( |
| 5140 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 5141 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5142 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5143 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5144 | pool_.get(), NetLogWithSource())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5145 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5146 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5147 | |
| 5148 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_PROXY_AUTH_REQUESTED)); |
| 5149 | EXPECT_FALSE(handle.is_initialized()); |
| 5150 | EXPECT_FALSE(handle.socket()); |
| 5151 | |
| 5152 | // The group should now be empty, and thus be deleted. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5153 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5154 | } |
| 5155 | |
| 5156 | class TestAuthHelper { |
| 5157 | public: |
| 5158 | TestAuthHelper() = default; |
Peter Boström | 293b134 | 2021-09-22 17:31:43 | [diff] [blame] | 5159 | |
| 5160 | TestAuthHelper(const TestAuthHelper&) = delete; |
| 5161 | TestAuthHelper& operator=(const TestAuthHelper&) = delete; |
| 5162 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5163 | ~TestAuthHelper() = default; |
| 5164 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5165 | void InitHandle( |
Matt Menke | 84d11e56 | 2019-03-27 00:11:19 | [diff] [blame] | 5166 | scoped_refptr<ClientSocketPool::SocketParams> params, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5167 | TransportClientSocketPool* pool, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5168 | RequestPriority priority = DEFAULT_PRIORITY, |
| 5169 | ClientSocketPool::RespectLimits respect_limits = |
| 5170 | ClientSocketPool::RespectLimits::ENABLED, |
| 5171 | const ClientSocketPool::GroupId& group_id_in = TestGroupId("a")) { |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5172 | EXPECT_EQ(ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 5173 | handle_.Init(group_id_in, params, absl::nullopt, priority, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5174 | SocketTag(), respect_limits, callback_.callback(), |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5175 | base::BindRepeating(&TestAuthHelper::AuthCallback, |
| 5176 | base::Unretained(this)), |
| 5177 | pool, NetLogWithSource())); |
| 5178 | } |
| 5179 | |
| 5180 | void WaitForAuth() { |
| 5181 | run_loop_ = std::make_unique<base::RunLoop>(); |
| 5182 | run_loop_->Run(); |
| 5183 | run_loop_.reset(); |
| 5184 | } |
| 5185 | |
| 5186 | void WaitForAuthAndRestartSync() { |
| 5187 | restart_sync_ = true; |
| 5188 | WaitForAuth(); |
| 5189 | restart_sync_ = false; |
| 5190 | } |
| 5191 | |
| 5192 | void WaitForAuthAndResetHandleSync() { |
| 5193 | reset_handle_sync_ = true; |
| 5194 | WaitForAuth(); |
| 5195 | reset_handle_sync_ = false; |
| 5196 | } |
| 5197 | |
| 5198 | void RestartWithAuth() { |
| 5199 | DCHECK(restart_with_auth_callback_); |
| 5200 | std::move(restart_with_auth_callback_).Run(); |
| 5201 | } |
| 5202 | |
| 5203 | int WaitForResult() { |
| 5204 | int result = callback_.WaitForResult(); |
| 5205 | // There shouldn't be any callback waiting to be invoked once the request is |
| 5206 | // complete. |
| 5207 | EXPECT_FALSE(restart_with_auth_callback_); |
| 5208 | // The socket should only be initialized on success. |
| 5209 | EXPECT_EQ(result == OK, handle_.is_initialized()); |
| 5210 | EXPECT_EQ(result == OK, handle_.socket() != nullptr); |
| 5211 | return result; |
| 5212 | } |
| 5213 | |
| 5214 | ClientSocketHandle* handle() { return &handle_; } |
| 5215 | int auth_count() const { return auth_count_; } |
| 5216 | int have_result() const { return callback_.have_result(); } |
| 5217 | |
| 5218 | private: |
| 5219 | void AuthCallback(const HttpResponseInfo& response, |
| 5220 | HttpAuthController* auth_controller, |
| 5221 | base::OnceClosure restart_with_auth_callback) { |
| 5222 | EXPECT_FALSE(restart_with_auth_callback_); |
| 5223 | EXPECT_TRUE(restart_with_auth_callback); |
| 5224 | |
| 5225 | // Once there's a result, this method shouldn't be invoked again. |
| 5226 | EXPECT_FALSE(callback_.have_result()); |
| 5227 | |
| 5228 | ++auth_count_; |
| 5229 | run_loop_->Quit(); |
| 5230 | if (restart_sync_) { |
| 5231 | std::move(restart_with_auth_callback).Run(); |
| 5232 | return; |
| 5233 | } |
| 5234 | |
| 5235 | restart_with_auth_callback_ = std::move(restart_with_auth_callback); |
| 5236 | |
| 5237 | if (reset_handle_sync_) { |
| 5238 | handle_.Reset(); |
| 5239 | return; |
| 5240 | } |
| 5241 | } |
| 5242 | |
| 5243 | std::unique_ptr<base::RunLoop> run_loop_; |
| 5244 | base::OnceClosure restart_with_auth_callback_; |
| 5245 | |
| 5246 | bool restart_sync_ = false; |
| 5247 | bool reset_handle_sync_ = false; |
| 5248 | |
| 5249 | ClientSocketHandle handle_; |
| 5250 | int auth_count_ = 0; |
| 5251 | TestCompletionCallback callback_; |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5252 | }; |
| 5253 | |
| 5254 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnce) { |
| 5255 | CreatePool(1, 1); |
| 5256 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5257 | |
| 5258 | TestAuthHelper auth_helper; |
| 5259 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5260 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5261 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5262 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5263 | |
| 5264 | auth_helper.WaitForAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5265 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5266 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5267 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5268 | |
| 5269 | auth_helper.RestartWithAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5270 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5271 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5272 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5273 | |
| 5274 | EXPECT_THAT(auth_helper.WaitForResult(), IsOk()); |
| 5275 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5276 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5277 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5278 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5279 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5280 | } |
| 5281 | |
| 5282 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceSync) { |
| 5283 | CreatePool(1, 1); |
| 5284 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5285 | |
| 5286 | TestAuthHelper auth_helper; |
| 5287 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5288 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5289 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5290 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5291 | |
| 5292 | auth_helper.WaitForAuthAndRestartSync(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5293 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5294 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5295 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5296 | |
| 5297 | EXPECT_THAT(auth_helper.WaitForResult(), IsOk()); |
| 5298 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5299 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5300 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5301 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5302 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5303 | } |
| 5304 | |
| 5305 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceFails) { |
| 5306 | CreatePool(1, 1); |
| 5307 | connect_job_factory_->set_job_type( |
| 5308 | TestConnectJob::kMockAuthChallengeOnceFailingJob); |
| 5309 | |
| 5310 | TestAuthHelper auth_helper; |
| 5311 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5312 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5313 | |
| 5314 | auth_helper.WaitForAuth(); |
| 5315 | auth_helper.RestartWithAuth(); |
| 5316 | EXPECT_THAT(auth_helper.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 5317 | |
| 5318 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5319 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5320 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5321 | } |
| 5322 | |
| 5323 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceSyncFails) { |
| 5324 | CreatePool(1, 1); |
| 5325 | connect_job_factory_->set_job_type( |
| 5326 | TestConnectJob::kMockAuthChallengeOnceFailingJob); |
| 5327 | |
| 5328 | TestAuthHelper auth_helper; |
| 5329 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5330 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5331 | |
| 5332 | auth_helper.WaitForAuthAndRestartSync(); |
| 5333 | EXPECT_THAT(auth_helper.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 5334 | |
| 5335 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5336 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5337 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5338 | } |
| 5339 | |
| 5340 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceDeleteHandle) { |
| 5341 | CreatePool(1, 1); |
| 5342 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5343 | |
| 5344 | TestAuthHelper auth_helper; |
| 5345 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5346 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5347 | |
| 5348 | auth_helper.WaitForAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5349 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5350 | |
| 5351 | auth_helper.handle()->Reset(); |
| 5352 | |
| 5353 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5354 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5355 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5356 | EXPECT_FALSE(auth_helper.handle()->is_initialized()); |
| 5357 | EXPECT_FALSE(auth_helper.handle()->socket()); |
| 5358 | } |
| 5359 | |
| 5360 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceDeleteHandleSync) { |
| 5361 | CreatePool(1, 1); |
| 5362 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5363 | |
| 5364 | TestAuthHelper auth_helper; |
| 5365 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5366 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5367 | |
| 5368 | auth_helper.WaitForAuthAndResetHandleSync(); |
| 5369 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5370 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5371 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5372 | EXPECT_FALSE(auth_helper.handle()->is_initialized()); |
| 5373 | EXPECT_FALSE(auth_helper.handle()->socket()); |
| 5374 | } |
| 5375 | |
| 5376 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceFlushWithError) { |
| 5377 | CreatePool(1, 1); |
| 5378 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5379 | |
| 5380 | TestAuthHelper auth_helper; |
| 5381 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5382 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5383 | |
| 5384 | auth_helper.WaitForAuth(); |
| 5385 | |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 5386 | pool_->FlushWithError(ERR_FAILED, "Network changed"); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5387 | base::RunLoop().RunUntilIdle(); |
| 5388 | |
| 5389 | // When flushing the socket pool, bound sockets should delay returning the |
| 5390 | // error until completion. |
| 5391 | EXPECT_FALSE(auth_helper.have_result()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5392 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5393 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5394 | |
| 5395 | auth_helper.RestartWithAuth(); |
| 5396 | // The callback should be called asynchronously. |
| 5397 | EXPECT_FALSE(auth_helper.have_result()); |
| 5398 | |
| 5399 | EXPECT_THAT(auth_helper.WaitForResult(), IsError(ERR_FAILED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5400 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5401 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5402 | } |
| 5403 | |
| 5404 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthTwice) { |
| 5405 | CreatePool(1, 1); |
| 5406 | connect_job_factory_->set_job_type( |
| 5407 | TestConnectJob::kMockAuthChallengeTwiceJob); |
| 5408 | |
| 5409 | TestAuthHelper auth_helper; |
| 5410 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5411 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5412 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5413 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5414 | |
| 5415 | auth_helper.WaitForAuth(); |
| 5416 | auth_helper.RestartWithAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5417 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5418 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5419 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5420 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5421 | |
| 5422 | auth_helper.WaitForAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5423 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5424 | EXPECT_EQ(2, auth_helper.auth_count()); |
| 5425 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5426 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5427 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5428 | auth_helper.RestartWithAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5429 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5430 | EXPECT_EQ(2, auth_helper.auth_count()); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5431 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5432 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5433 | |
| 5434 | EXPECT_THAT(auth_helper.WaitForResult(), IsOk()); |
| 5435 | EXPECT_EQ(2, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5436 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5437 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5438 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5439 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5440 | } |
| 5441 | |
| 5442 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthTwiceFails) { |
| 5443 | CreatePool(1, 1); |
| 5444 | connect_job_factory_->set_job_type( |
| 5445 | TestConnectJob::kMockAuthChallengeTwiceFailingJob); |
| 5446 | |
| 5447 | TestAuthHelper auth_helper; |
| 5448 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5449 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5450 | |
| 5451 | auth_helper.WaitForAuth(); |
| 5452 | auth_helper.RestartWithAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5453 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5454 | EXPECT_EQ(1, auth_helper.auth_count()); |
| 5455 | |
| 5456 | auth_helper.WaitForAuth(); |
| 5457 | auth_helper.RestartWithAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5458 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5459 | EXPECT_EQ(2, auth_helper.auth_count()); |
| 5460 | |
| 5461 | EXPECT_THAT(auth_helper.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 5462 | EXPECT_EQ(2, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5463 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5464 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5465 | } |
| 5466 | |
| 5467 | // Makes sure that when a bound request is destroyed, a new ConnectJob is |
| 5468 | // created, if needed. |
| 5469 | TEST_F(ClientSocketPoolBaseTest, |
| 5470 | ProxyAuthCreateNewConnectJobOnDestroyBoundRequest) { |
| 5471 | CreatePool(1 /* max_sockets */, 1 /* max_sockets_per_group */); |
| 5472 | connect_job_factory_->set_job_type( |
| 5473 | TestConnectJob::kMockAuthChallengeOnceFailingJob); |
| 5474 | |
| 5475 | // First request creates a ConnectJob. |
| 5476 | TestAuthHelper auth_helper1; |
| 5477 | auth_helper1.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5478 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5479 | |
| 5480 | // A second request come in, but no new ConnectJob is needed, since the limit |
| 5481 | // has been reached. |
| 5482 | TestAuthHelper auth_helper2; |
| 5483 | auth_helper2.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5484 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5485 | |
| 5486 | // Run until the auth callback for the first request is invoked. |
| 5487 | auth_helper1.WaitForAuth(); |
| 5488 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5489 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5490 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5491 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5492 | |
| 5493 | // Make connect jobs succeed, then cancel the first request, which should |
| 5494 | // destroy the bound ConnectJob, and cause a new ConnectJob to start. |
| 5495 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 5496 | auth_helper1.handle()->Reset(); |
| 5497 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5498 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5499 | |
| 5500 | // The second ConnectJob should succeed. |
| 5501 | EXPECT_THAT(auth_helper2.WaitForResult(), IsOk()); |
| 5502 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5503 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5504 | } |
| 5505 | |
| 5506 | // Makes sure that when a bound request is destroyed, a new ConnectJob is |
| 5507 | // created for another group, if needed. |
| 5508 | TEST_F(ClientSocketPoolBaseTest, |
| 5509 | ProxyAuthCreateNewConnectJobOnDestroyBoundRequestDifferentGroups) { |
| 5510 | CreatePool(1 /* max_sockets */, 1 /* max_sockets_per_group */); |
| 5511 | connect_job_factory_->set_job_type( |
| 5512 | TestConnectJob::kMockAuthChallengeOnceFailingJob); |
| 5513 | |
| 5514 | // First request creates a ConnectJob. |
| 5515 | TestAuthHelper auth_helper1; |
| 5516 | auth_helper1.InitHandle(params_, pool_.get(), DEFAULT_PRIORITY); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5517 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5518 | |
| 5519 | // A second request come in, but no new ConnectJob is needed, since the limit |
| 5520 | // has been reached. |
| 5521 | TestAuthHelper auth_helper2; |
| 5522 | auth_helper2.InitHandle(params_, pool_.get(), DEFAULT_PRIORITY, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5523 | ClientSocketPool::RespectLimits::ENABLED, |
| 5524 | TestGroupId("b")); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5525 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5526 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5527 | |
| 5528 | // Run until the auth callback for the first request is invoked. |
| 5529 | auth_helper1.WaitForAuth(); |
| 5530 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5531 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5532 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5533 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5534 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 5535 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5536 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5537 | |
| 5538 | // Make connect jobs succeed, then cancel the first request, which should |
| 5539 | // destroy the bound ConnectJob, and cause a new ConnectJob to start for the |
| 5540 | // other group. |
| 5541 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 5542 | auth_helper1.handle()->Reset(); |
| 5543 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5544 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 5545 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5546 | |
| 5547 | // The second ConnectJob should succeed. |
| 5548 | EXPECT_THAT(auth_helper2.WaitForResult(), IsOk()); |
| 5549 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5550 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 5551 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5552 | } |
| 5553 | |
| 5554 | // Test that once an auth challenge is bound, that's the request that gets all |
| 5555 | // subsequent calls and the socket itself. |
| 5556 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthStaysBound) { |
| 5557 | CreatePool(1, 1); |
| 5558 | connect_job_factory_->set_job_type( |
| 5559 | TestConnectJob::kMockAuthChallengeTwiceJob); |
| 5560 | |
| 5561 | // First request creates a ConnectJob. |
| 5562 | TestAuthHelper auth_helper1; |
| 5563 | auth_helper1.InitHandle(params_, pool_.get(), LOWEST); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5564 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5565 | |
| 5566 | // A second, higher priority request is made. |
| 5567 | TestAuthHelper auth_helper2; |
| 5568 | auth_helper2.InitHandle(params_, pool_.get(), LOW); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5569 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5570 | |
| 5571 | // Run until the auth callback for the second request is invoked. |
| 5572 | auth_helper2.WaitForAuth(); |
| 5573 | EXPECT_EQ(0, auth_helper1.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5574 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5575 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5576 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5577 | |
| 5578 | // Start a higher priority job. It shouldn't be able to steal |auth_helper2|'s |
| 5579 | // ConnectJob. |
| 5580 | TestAuthHelper auth_helper3; |
| 5581 | auth_helper3.InitHandle(params_, pool_.get(), HIGHEST); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5582 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5583 | |
| 5584 | // Start a higher job that ignores limits, creating a hanging socket. It |
| 5585 | // shouldn't be able to steal |auth_helper2|'s ConnectJob. |
| 5586 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 5587 | TestAuthHelper auth_helper4; |
| 5588 | auth_helper4.InitHandle(params_, pool_.get(), HIGHEST, |
| 5589 | ClientSocketPool::RespectLimits::DISABLED); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5590 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5591 | |
| 5592 | // Restart with auth, and |auth_helper2|'s auth method should be invoked |
| 5593 | // again. |
| 5594 | auth_helper2.RestartWithAuth(); |
| 5595 | auth_helper2.WaitForAuth(); |
| 5596 | EXPECT_EQ(0, auth_helper1.auth_count()); |
| 5597 | EXPECT_FALSE(auth_helper1.have_result()); |
| 5598 | EXPECT_EQ(2, auth_helper2.auth_count()); |
| 5599 | EXPECT_FALSE(auth_helper2.have_result()); |
| 5600 | EXPECT_EQ(0, auth_helper3.auth_count()); |
| 5601 | EXPECT_FALSE(auth_helper3.have_result()); |
| 5602 | EXPECT_EQ(0, auth_helper4.auth_count()); |
| 5603 | EXPECT_FALSE(auth_helper4.have_result()); |
| 5604 | |
| 5605 | // Advance auth again, and |auth_helper2| should get the socket. |
| 5606 | auth_helper2.RestartWithAuth(); |
| 5607 | EXPECT_THAT(auth_helper2.WaitForResult(), IsOk()); |
| 5608 | // The hung ConnectJob for the RespectLimits::DISABLED request is still in the |
| 5609 | // socket pool. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5610 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5611 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5612 | EXPECT_EQ(0, auth_helper1.auth_count()); |
| 5613 | EXPECT_FALSE(auth_helper1.have_result()); |
| 5614 | EXPECT_EQ(0, auth_helper3.auth_count()); |
| 5615 | EXPECT_FALSE(auth_helper3.have_result()); |
| 5616 | EXPECT_EQ(0, auth_helper4.auth_count()); |
| 5617 | EXPECT_FALSE(auth_helper4.have_result()); |
| 5618 | |
| 5619 | // If the socket is returned to the socket pool, the RespectLimits::DISABLED |
| 5620 | // socket request should be able to claim it. |
| 5621 | auth_helper2.handle()->Reset(); |
| 5622 | EXPECT_THAT(auth_helper4.WaitForResult(), IsOk()); |
| 5623 | EXPECT_EQ(0, auth_helper1.auth_count()); |
| 5624 | EXPECT_FALSE(auth_helper1.have_result()); |
| 5625 | EXPECT_EQ(0, auth_helper3.auth_count()); |
| 5626 | EXPECT_FALSE(auth_helper3.have_result()); |
| 5627 | EXPECT_EQ(0, auth_helper4.auth_count()); |
| 5628 | } |
| 5629 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5630 | enum class RefreshType { |
| 5631 | kServer, |
| 5632 | kProxy, |
| 5633 | }; |
| 5634 | |
| 5635 | // Common base class to test RefreshGroup() when called from either |
| 5636 | // OnSSLConfigForServerChanged() matching a specific group or the pool's proxy. |
| 5637 | // |
| 5638 | // Tests which test behavior specific to one or the other case should use |
| 5639 | // ClientSocketPoolBaseTest directly. In particular, there is no "other group" |
| 5640 | // when the pool's proxy matches. |
| 5641 | class ClientSocketPoolBaseRefreshTest |
| 5642 | : public ClientSocketPoolBaseTest, |
| 5643 | public testing::WithParamInterface<RefreshType> { |
| 5644 | public: |
| 5645 | void CreatePoolForRefresh(int max_sockets, |
| 5646 | int max_sockets_per_group, |
| 5647 | bool enable_backup_connect_jobs = false) { |
| 5648 | switch (GetParam()) { |
| 5649 | case RefreshType::kServer: |
| 5650 | CreatePool(max_sockets, max_sockets_per_group, |
| 5651 | enable_backup_connect_jobs); |
| 5652 | break; |
| 5653 | case RefreshType::kProxy: |
| 5654 | CreatePoolWithIdleTimeouts( |
| 5655 | max_sockets, max_sockets_per_group, kUnusedIdleSocketTimeout, |
| 5656 | ClientSocketPool::used_idle_socket_timeout(), |
| 5657 | enable_backup_connect_jobs, |
Eric Orth | 5ccc3f0 | 2021-09-23 00:01:57 | [diff] [blame] | 5658 | PacResultElementToProxyServer("HTTPS myproxy:70")); |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5659 | break; |
| 5660 | } |
| 5661 | } |
| 5662 | |
| 5663 | static ClientSocketPool::GroupId GetGroupId() { |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame] | 5664 | return TestGroupId("a", 443, url::kHttpsScheme); |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5665 | } |
| 5666 | |
David Benjamin | 6dd7e88 | 2019-10-10 02:35:23 | [diff] [blame] | 5667 | static ClientSocketPool::GroupId GetGroupIdInPartition() { |
| 5668 | // Note this GroupId will match GetGroupId() unless |
| 5669 | // kPartitionConnectionsByNetworkIsolationKey is enabled. |
Matt Menke | 4807a9a | 2020-11-21 00:14:41 | [diff] [blame] | 5670 | const SchemefulSite kSite(GURL("https://b/")); |
| 5671 | const NetworkIsolationKey kNetworkIsolationKey(kSite, kSite); |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame] | 5672 | return TestGroupId("a", 443, url::kHttpsScheme, |
David Benjamin | 6dd7e88 | 2019-10-10 02:35:23 | [diff] [blame] | 5673 | PrivacyMode::PRIVACY_MODE_DISABLED, |
| 5674 | kNetworkIsolationKey); |
| 5675 | } |
| 5676 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5677 | void OnSSLConfigForServerChanged() { |
| 5678 | switch (GetParam()) { |
| 5679 | case RefreshType::kServer: |
| 5680 | pool_->OnSSLConfigForServerChanged(HostPortPair("a", 443)); |
| 5681 | break; |
| 5682 | case RefreshType::kProxy: |
| 5683 | pool_->OnSSLConfigForServerChanged(HostPortPair("myproxy", 70)); |
| 5684 | break; |
| 5685 | } |
| 5686 | } |
| 5687 | }; |
| 5688 | |
| 5689 | INSTANTIATE_TEST_SUITE_P(RefreshType, |
| 5690 | ClientSocketPoolBaseRefreshTest, |
| 5691 | ::testing::Values(RefreshType::kServer, |
| 5692 | RefreshType::kProxy)); |
| 5693 | |
| 5694 | TEST_P(ClientSocketPoolBaseRefreshTest, RefreshGroupCreatesNewConnectJobs) { |
| 5695 | CreatePoolForRefresh(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 5696 | const ClientSocketPool::GroupId kGroupId = GetGroupId(); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5697 | |
| 5698 | // First job will be waiting until it gets aborted. |
| 5699 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 5700 | |
| 5701 | ClientSocketHandle handle; |
| 5702 | TestCompletionCallback callback; |
| 5703 | EXPECT_THAT( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 5704 | handle.Init(kGroupId, params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5705 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5706 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5707 | pool_.get(), NetLogWithSource()), |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5708 | IsError(ERR_IO_PENDING)); |
| 5709 | |
| 5710 | // Switch connect job types, so creating a new ConnectJob will result in |
| 5711 | // success. |
| 5712 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 5713 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5714 | OnSSLConfigForServerChanged(); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5715 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 5716 | ASSERT_TRUE(handle.socket()); |
| 5717 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5718 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId)); |
| 5719 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(kGroupId)); |
| 5720 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(kGroupId)); |
| 5721 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId)); |
| 5722 | } |
| 5723 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5724 | TEST_P(ClientSocketPoolBaseRefreshTest, RefreshGroupClosesIdleConnectJobs) { |
David Benjamin | 6dd7e88 | 2019-10-10 02:35:23 | [diff] [blame] | 5725 | base::test::ScopedFeatureList feature_list; |
| 5726 | feature_list.InitAndEnableFeature( |
| 5727 | features::kPartitionConnectionsByNetworkIsolationKey); |
| 5728 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5729 | CreatePoolForRefresh(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 5730 | const ClientSocketPool::GroupId kGroupId = GetGroupId(); |
David Benjamin | 6dd7e88 | 2019-10-10 02:35:23 | [diff] [blame] | 5731 | const ClientSocketPool::GroupId kGroupIdInPartition = GetGroupIdInPartition(); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5732 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 5733 | pool_->RequestSockets(kGroupId, params_, absl::nullopt, 2, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5734 | NetLogWithSource()); |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 5735 | pool_->RequestSockets(kGroupIdInPartition, params_, absl::nullopt, 2, |
David Benjamin | 6dd7e88 | 2019-10-10 02:35:23 | [diff] [blame] | 5736 | NetLogWithSource()); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5737 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId)); |
David Benjamin | 6dd7e88 | 2019-10-10 02:35:23 | [diff] [blame] | 5738 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupIdInPartition)); |
| 5739 | EXPECT_EQ(4, pool_->IdleSocketCount()); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5740 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(kGroupId)); |
David Benjamin | 6dd7e88 | 2019-10-10 02:35:23 | [diff] [blame] | 5741 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(kGroupIdInPartition)); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5742 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5743 | OnSSLConfigForServerChanged(); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5744 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5745 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupId)); |
David Benjamin | 6dd7e88 | 2019-10-10 02:35:23 | [diff] [blame] | 5746 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupIdInPartition)); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5747 | } |
| 5748 | |
| 5749 | TEST_F(ClientSocketPoolBaseTest, |
| 5750 | RefreshGroupDoesNotCloseIdleConnectJobsInOtherGroup) { |
| 5751 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5752 | const ClientSocketPool::GroupId kGroupId = |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame] | 5753 | TestGroupId("a", 443, url::kHttpsScheme); |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5754 | const ClientSocketPool::GroupId kOtherGroupId = |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame] | 5755 | TestGroupId("b", 443, url::kHttpsScheme); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5756 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 5757 | pool_->RequestSockets(kOtherGroupId, params_, absl::nullopt, 2, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5758 | NetLogWithSource()); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5759 | ASSERT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 5760 | EXPECT_EQ(2, pool_->IdleSocketCount()); |
| 5761 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(kOtherGroupId)); |
| 5762 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5763 | pool_->OnSSLConfigForServerChanged(HostPortPair("a", 443)); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5764 | ASSERT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 5765 | EXPECT_EQ(2, pool_->IdleSocketCount()); |
| 5766 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(kOtherGroupId)); |
| 5767 | } |
| 5768 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5769 | TEST_P(ClientSocketPoolBaseRefreshTest, RefreshGroupPreventsSocketReuse) { |
| 5770 | CreatePoolForRefresh(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 5771 | const ClientSocketPool::GroupId kGroupId = GetGroupId(); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5772 | |
| 5773 | ClientSocketHandle handle; |
| 5774 | TestCompletionCallback callback; |
| 5775 | EXPECT_THAT( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 5776 | handle.Init(kGroupId, params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5777 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5778 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5779 | pool_.get(), NetLogWithSource()), |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5780 | IsOk()); |
| 5781 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId)); |
| 5782 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId)); |
| 5783 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5784 | OnSSLConfigForServerChanged(); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5785 | |
| 5786 | handle.Reset(); |
| 5787 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5788 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupId)); |
| 5789 | } |
| 5790 | |
| 5791 | TEST_F(ClientSocketPoolBaseTest, |
| 5792 | RefreshGroupDoesNotPreventSocketReuseInOtherGroup) { |
| 5793 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5794 | const ClientSocketPool::GroupId kGroupId = |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame] | 5795 | TestGroupId("a", 443, url::kHttpsScheme); |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5796 | const ClientSocketPool::GroupId kOtherGroupId = |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame] | 5797 | TestGroupId("b", 443, url::kHttpsScheme); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5798 | |
| 5799 | ClientSocketHandle handle; |
| 5800 | TestCompletionCallback callback; |
| 5801 | EXPECT_THAT( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 5802 | handle.Init(kOtherGroupId, params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5803 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5804 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5805 | pool_.get(), NetLogWithSource()), |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5806 | IsOk()); |
| 5807 | ASSERT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 5808 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kOtherGroupId)); |
| 5809 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5810 | pool_->OnSSLConfigForServerChanged(HostPortPair("a", 443)); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5811 | |
| 5812 | handle.Reset(); |
| 5813 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 5814 | ASSERT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 5815 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(kOtherGroupId)); |
| 5816 | } |
| 5817 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5818 | TEST_P(ClientSocketPoolBaseRefreshTest, |
| 5819 | RefreshGroupReplacesBoundConnectJobOnConnect) { |
| 5820 | CreatePoolForRefresh(1, 1); |
| 5821 | const ClientSocketPool::GroupId kGroupId = GetGroupId(); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5822 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5823 | |
| 5824 | TestAuthHelper auth_helper; |
| 5825 | auth_helper.InitHandle(params_, pool_.get(), DEFAULT_PRIORITY, |
| 5826 | ClientSocketPool::RespectLimits::ENABLED, kGroupId); |
| 5827 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(kGroupId)); |
| 5828 | |
| 5829 | auth_helper.WaitForAuth(); |
| 5830 | |
| 5831 | // This should update the generation, but not cancel the old ConnectJob - it's |
| 5832 | // not safe to do anything while waiting on the original ConnectJob. |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5833 | OnSSLConfigForServerChanged(); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5834 | |
| 5835 | // Providing auth credentials and restarting the request with them will cause |
| 5836 | // the ConnectJob to complete successfully, but the result will be discarded |
| 5837 | // because of the generation mismatch. |
| 5838 | auth_helper.RestartWithAuth(); |
| 5839 | |
| 5840 | // Despite using ConnectJobs that simulate a single challenge, a second |
| 5841 | // challenge will be seen, due to using a new ConnectJob. |
| 5842 | auth_helper.WaitForAuth(); |
| 5843 | auth_helper.RestartWithAuth(); |
| 5844 | |
| 5845 | EXPECT_THAT(auth_helper.WaitForResult(), IsOk()); |
| 5846 | EXPECT_TRUE(auth_helper.handle()->socket()); |
| 5847 | EXPECT_EQ(2, auth_helper.auth_count()); |
| 5848 | |
| 5849 | // When released, the socket will be returned to the socket pool, and |
| 5850 | // available for reuse. |
| 5851 | auth_helper.handle()->Reset(); |
| 5852 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 5853 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId)); |
| 5854 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(kGroupId)); |
| 5855 | } |
| 5856 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5857 | TEST_F(ClientSocketPoolBaseTest, RefreshProxyRefreshesAllGroups) { |
| 5858 | CreatePoolWithIdleTimeouts(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 5859 | kUnusedIdleSocketTimeout, |
| 5860 | ClientSocketPool::used_idle_socket_timeout(), |
| 5861 | false /* no backup connect jobs */, |
Eric Orth | 5ccc3f0 | 2021-09-23 00:01:57 | [diff] [blame] | 5862 | PacResultElementToProxyServer("HTTPS myproxy:70")); |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5863 | |
| 5864 | const ClientSocketPool::GroupId kGroupId1 = |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame] | 5865 | TestGroupId("a", 443, url::kHttpsScheme); |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5866 | const ClientSocketPool::GroupId kGroupId2 = |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame] | 5867 | TestGroupId("b", 443, url::kHttpsScheme); |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5868 | const ClientSocketPool::GroupId kGroupId3 = |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame] | 5869 | TestGroupId("c", 443, url::kHttpsScheme); |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5870 | |
| 5871 | // Make three sockets in three different groups. The third socket is released |
| 5872 | // to the pool as idle. |
| 5873 | ClientSocketHandle handle1, handle2, handle3; |
| 5874 | TestCompletionCallback callback; |
| 5875 | EXPECT_THAT( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 5876 | handle1.Init(kGroupId1, params_, absl::nullopt, DEFAULT_PRIORITY, |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5877 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5878 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5879 | pool_.get(), NetLogWithSource()), |
| 5880 | IsOk()); |
| 5881 | EXPECT_THAT( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 5882 | handle2.Init(kGroupId2, params_, absl::nullopt, DEFAULT_PRIORITY, |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5883 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5884 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5885 | pool_.get(), NetLogWithSource()), |
| 5886 | IsOk()); |
| 5887 | EXPECT_THAT( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 5888 | handle3.Init(kGroupId3, params_, absl::nullopt, DEFAULT_PRIORITY, |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5889 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5890 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5891 | pool_.get(), NetLogWithSource()), |
| 5892 | IsOk()); |
| 5893 | handle3.Reset(); |
| 5894 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId1)); |
| 5895 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId1)); |
| 5896 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId2)); |
| 5897 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId2)); |
| 5898 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId3)); |
| 5899 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(kGroupId3)); |
| 5900 | |
| 5901 | // Changes to some other proxy do not affect the pool. The idle socket remains |
| 5902 | // alive and closing |handle2| makes the socket available for the pool. |
| 5903 | pool_->OnSSLConfigForServerChanged(HostPortPair("someotherproxy", 70)); |
| 5904 | |
| 5905 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId1)); |
| 5906 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId1)); |
| 5907 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId2)); |
| 5908 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId2)); |
| 5909 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId3)); |
| 5910 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(kGroupId3)); |
| 5911 | |
| 5912 | handle2.Reset(); |
| 5913 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId2)); |
| 5914 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(kGroupId2)); |
| 5915 | |
| 5916 | // Changes to the matching proxy refreshes all groups. |
| 5917 | pool_->OnSSLConfigForServerChanged(HostPortPair("myproxy", 70)); |
| 5918 | |
| 5919 | // Idle sockets are closed. |
| 5920 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5921 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupId2)); |
| 5922 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupId3)); |
| 5923 | |
| 5924 | // The active socket, however, continues to be active. |
| 5925 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId1)); |
| 5926 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId1)); |
| 5927 | |
| 5928 | // Closing it does not make it available for the pool. |
| 5929 | handle1.Reset(); |
| 5930 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5931 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupId1)); |
| 5932 | } |
| 5933 | |
| 5934 | TEST_F(ClientSocketPoolBaseTest, RefreshBothPrivacyAndNormalSockets) { |
| 5935 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 5936 | |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame] | 5937 | const ClientSocketPool::GroupId kGroupId = TestGroupId( |
| 5938 | "a", 443, url::kHttpsScheme, PrivacyMode::PRIVACY_MODE_DISABLED); |
| 5939 | const ClientSocketPool::GroupId kGroupIdPrivacy = TestGroupId( |
| 5940 | "a", 443, url::kHttpsScheme, PrivacyMode::PRIVACY_MODE_ENABLED); |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5941 | const ClientSocketPool::GroupId kOtherGroupId = |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame] | 5942 | TestGroupId("b", 443, url::kHttpsScheme); |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5943 | |
| 5944 | // Make a socket in each groups. |
| 5945 | ClientSocketHandle handle1, handle2, handle3; |
| 5946 | TestCompletionCallback callback; |
| 5947 | EXPECT_THAT( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 5948 | handle1.Init(kGroupId, params_, absl::nullopt, DEFAULT_PRIORITY, |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5949 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5950 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5951 | pool_.get(), NetLogWithSource()), |
| 5952 | IsOk()); |
| 5953 | EXPECT_THAT( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 5954 | handle2.Init(kGroupIdPrivacy, params_, absl::nullopt, DEFAULT_PRIORITY, |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5955 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5956 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5957 | pool_.get(), NetLogWithSource()), |
| 5958 | IsOk()); |
| 5959 | EXPECT_THAT( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 5960 | handle3.Init(kOtherGroupId, params_, absl::nullopt, DEFAULT_PRIORITY, |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5961 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5962 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5963 | pool_.get(), NetLogWithSource()), |
| 5964 | IsOk()); |
| 5965 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId)); |
| 5966 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId)); |
| 5967 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupIdPrivacy)); |
| 5968 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupIdPrivacy)); |
| 5969 | ASSERT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 5970 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kOtherGroupId)); |
| 5971 | |
| 5972 | pool_->OnSSLConfigForServerChanged(HostPortPair("a", 443)); |
| 5973 | |
| 5974 | // Active sockets continue to be active. |
| 5975 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId)); |
| 5976 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId)); |
| 5977 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupIdPrivacy)); |
| 5978 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupIdPrivacy)); |
| 5979 | ASSERT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 5980 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kOtherGroupId)); |
| 5981 | |
| 5982 | // Closing them leaves kOtherGroupId alone, but kGroupId and kGroupIdPrivacy |
| 5983 | // are unusable. |
| 5984 | handle1.Reset(); |
| 5985 | handle2.Reset(); |
| 5986 | handle3.Reset(); |
| 5987 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 5988 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupId)); |
| 5989 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupIdPrivacy)); |
| 5990 | EXPECT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 5991 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(kOtherGroupId)); |
| 5992 | } |
| 5993 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 5994 | } // namespace |
| 5995 | |
| 5996 | } // namespace net |