[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 | 0049a0b7 | 2021-10-20 20:44:26 | [diff] [blame^] | 703 | auto entries = net_log_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 | 0049a0b7 | 2021-10-20 20:44:26 | [diff] [blame^] | 741 | RecordingNetLogObserver net_log_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 Reichhoff | 0049a0b7 | 2021-10-20 20:44:26 | [diff] [blame^] | 753 | NetLogWithSource net_log_with_source = |
| 754 | NetLogWithSource::Make(NetLogSourceType::NONE); |
| 755 | |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 756 | TestLoadTimingInfoNotConnected(handle); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 757 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 758 | EXPECT_EQ(OK, handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 759 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 760 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 761 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
Matt Reichhoff | 0049a0b7 | 2021-10-20 20:44:26 | [diff] [blame^] | 762 | pool_.get(), net_log_with_source)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 763 | EXPECT_TRUE(handle.is_initialized()); |
| 764 | EXPECT_TRUE(handle.socket()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 765 | TestLoadTimingInfoConnectedNotReused(handle); |
| 766 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 767 | handle.Reset(); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 768 | TestLoadTimingInfoNotConnected(handle); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 769 | |
Matt Reichhoff | 0049a0b7 | 2021-10-20 20:44:26 | [diff] [blame^] | 770 | auto entries = |
| 771 | net_log_observer_.GetEntriesForSource(net_log_with_source.source()); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 772 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 773 | EXPECT_EQ(5u, entries.size()); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 774 | EXPECT_TRUE(LogContainsEvent( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 775 | entries, 0, NetLogEventType::TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 776 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 777 | EXPECT_TRUE(LogContainsBeginEvent(entries, 1, NetLogEventType::SOCKET_POOL)); |
| 778 | EXPECT_TRUE(LogContainsEvent( |
| 779 | entries, 2, NetLogEventType::SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
| 780 | NetLogEventPhase::NONE)); |
| 781 | EXPECT_TRUE(LogContainsEvent(entries, 3, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 782 | NetLogEventType::SOCKET_POOL_BOUND_TO_SOCKET, |
| 783 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 784 | EXPECT_TRUE(LogContainsEndEvent(entries, 4, NetLogEventType::SOCKET_POOL)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 785 | } |
| 786 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 787 | TEST_F(ClientSocketPoolBaseTest, InitConnectionFailure) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 788 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 789 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 790 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
Matt Reichhoff | 0049a0b7 | 2021-10-20 20:44:26 | [diff] [blame^] | 791 | NetLogWithSource net_log_with_source = |
| 792 | NetLogWithSource::Make(NetLogSourceType::NONE); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 793 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 794 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 795 | TestCompletionCallback callback; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 796 | // Set the additional error state members to ensure that they get cleared. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 797 | handle.set_is_ssl_error(true); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 798 | handle.set_ssl_cert_request_info(base::MakeRefCounted<SSLCertRequestInfo>()); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 799 | EXPECT_EQ( |
| 800 | ERR_CONNECTION_FAILED, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 801 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 802 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 803 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
Matt Reichhoff | 0049a0b7 | 2021-10-20 20:44:26 | [diff] [blame^] | 804 | pool_.get(), net_log_with_source)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 805 | EXPECT_FALSE(handle.socket()); |
| 806 | EXPECT_FALSE(handle.is_ssl_error()); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 807 | EXPECT_FALSE(handle.ssl_cert_request_info()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 808 | TestLoadTimingInfoNotConnected(handle); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 809 | |
Matt Reichhoff | 0049a0b7 | 2021-10-20 20:44:26 | [diff] [blame^] | 810 | auto entries = |
| 811 | net_log_observer_.GetEntriesForSource(net_log_with_source.source()); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 812 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 813 | EXPECT_EQ(4u, entries.size()); |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 814 | EXPECT_TRUE(LogContainsEvent( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 815 | entries, 0, NetLogEventType::TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 816 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 817 | EXPECT_TRUE(LogContainsBeginEvent(entries, 1, NetLogEventType::SOCKET_POOL)); |
| 818 | EXPECT_TRUE(LogContainsEvent( |
| 819 | entries, 2, NetLogEventType::SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
| 820 | NetLogEventPhase::NONE)); |
| 821 | EXPECT_TRUE(LogContainsEndEvent(entries, 3, NetLogEventType::SOCKET_POOL)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 822 | } |
| 823 | |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 824 | // Test releasing an open socket into the socket pool, telling the socket pool |
| 825 | // to close the socket. |
| 826 | TEST_F(ClientSocketPoolBaseTest, ReleaseAndCloseConnection) { |
| 827 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 828 | |
| 829 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 830 | ASSERT_TRUE(request(0)->handle()->socket()); |
| 831 | net::NetLogSource source = request(0)->handle()->socket()->NetLog().source(); |
| 832 | ReleaseOneConnection(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 833 | |
| 834 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 835 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 836 | |
| 837 | ExpectSocketClosedWithReason( |
| 838 | source, TransportClientSocketPool::kClosedConnectionReturnedToPool); |
| 839 | } |
| 840 | |
| 841 | TEST_F(ClientSocketPoolBaseTest, SocketWithUnreadDataReturnedToPool) { |
| 842 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 843 | connect_job_factory_->set_job_type(TestConnectJob::kMockUnreadDataJob); |
| 844 | |
| 845 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 846 | ASSERT_TRUE(request(0)->handle()->socket()); |
| 847 | net::NetLogSource source = request(0)->handle()->socket()->NetLog().source(); |
| 848 | EXPECT_TRUE(request(0)->handle()->socket()->IsConnected()); |
| 849 | EXPECT_FALSE(request(0)->handle()->socket()->IsConnectedAndIdle()); |
| 850 | ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE); |
| 851 | |
| 852 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 853 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 854 | |
| 855 | ExpectSocketClosedWithReason( |
| 856 | source, TransportClientSocketPool::kDataReceivedUnexpectedly); |
| 857 | } |
| 858 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 859 | // Make sure different groups do not share sockets. |
| 860 | TEST_F(ClientSocketPoolBaseTest, GroupSeparation) { |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 861 | base::test::ScopedFeatureList feature_list; |
| 862 | feature_list.InitAndEnableFeature( |
| 863 | features::kPartitionConnectionsByNetworkIsolationKey); |
| 864 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 865 | CreatePool(1000 /* max_sockets */, 2 /* max_sockets_per_group */); |
| 866 | |
| 867 | const HostPortPair kHostPortPairs[] = { |
| 868 | {"a", 80}, |
| 869 | {"a", 443}, |
| 870 | {"b", 80}, |
| 871 | }; |
| 872 | |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame] | 873 | const char* const kSchemes[] = { |
| 874 | url::kHttpScheme, |
| 875 | url::kHttpsScheme, |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 876 | }; |
| 877 | |
Matt Menke | bdf77780 | 2019-04-22 19:38:59 | [diff] [blame] | 878 | const PrivacyMode kPrivacyModes[] = {PrivacyMode::PRIVACY_MODE_DISABLED, |
| 879 | PrivacyMode::PRIVACY_MODE_ENABLED}; |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 880 | |
Matt Menke | 4807a9a | 2020-11-21 00:14:41 | [diff] [blame] | 881 | const SchemefulSite kSiteA(GURL("http://a.test/")); |
| 882 | const SchemefulSite kSiteB(GURL("http://b.test/")); |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 883 | const NetworkIsolationKey kNetworkIsolationKeys[] = { |
Matt Menke | 4807a9a | 2020-11-21 00:14:41 | [diff] [blame] | 884 | NetworkIsolationKey(kSiteA, kSiteA), |
| 885 | NetworkIsolationKey(kSiteB, kSiteB), |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 886 | }; |
| 887 | |
Ben Schwartz | 3ff4dc1e6 | 2021-04-27 21:15:23 | [diff] [blame] | 888 | const SecureDnsPolicy kSecureDnsPolicys[] = {SecureDnsPolicy::kAllow, |
| 889 | SecureDnsPolicy::kDisable}; |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 890 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 891 | int total_idle_sockets = 0; |
| 892 | |
| 893 | // Walk through each GroupId, making sure that requesting a socket for one |
| 894 | // group does not return a previously connected socket for another group. |
| 895 | for (const auto& host_port_pair : kHostPortPairs) { |
| 896 | SCOPED_TRACE(host_port_pair.ToString()); |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame] | 897 | for (const char* scheme : kSchemes) { |
| 898 | SCOPED_TRACE(scheme); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 899 | for (const auto& privacy_mode : kPrivacyModes) { |
| 900 | SCOPED_TRACE(privacy_mode); |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 901 | for (const auto& network_isolation_key : kNetworkIsolationKeys) { |
| 902 | SCOPED_TRACE(network_isolation_key.ToString()); |
Ben Schwartz | 3ff4dc1e6 | 2021-04-27 21:15:23 | [diff] [blame] | 903 | for (const auto& secure_dns_policy : kSecureDnsPolicys) { |
| 904 | SCOPED_TRACE(static_cast<int>(secure_dns_policy)); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 905 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 906 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 907 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 908 | ClientSocketPool::GroupId group_id( |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame] | 909 | url::SchemeHostPort(scheme, host_port_pair.host(), |
| 910 | host_port_pair.port()), |
| 911 | privacy_mode, network_isolation_key, secure_dns_policy); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 912 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 913 | EXPECT_FALSE(pool_->HasGroupForTesting(group_id)); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 914 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 915 | TestCompletionCallback callback; |
| 916 | ClientSocketHandle handle; |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 917 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 918 | // Since the group is empty, requesting a socket should not complete |
| 919 | // synchronously. |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 920 | EXPECT_THAT(handle.Init(group_id, params_, absl::nullopt, |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 921 | DEFAULT_PRIORITY, SocketTag(), |
| 922 | ClientSocketPool::RespectLimits::ENABLED, |
| 923 | callback.callback(), |
| 924 | ClientSocketPool::ProxyAuthCallback(), |
| 925 | pool_.get(), NetLogWithSource()), |
| 926 | IsError(ERR_IO_PENDING)); |
| 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 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 931 | EXPECT_TRUE(handle.socket()); |
| 932 | EXPECT_TRUE(pool_->HasGroupForTesting(group_id)); |
| 933 | EXPECT_EQ(total_idle_sockets, pool_->IdleSocketCount()); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 934 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 935 | // Return socket to pool. |
| 936 | handle.Reset(); |
| 937 | EXPECT_EQ(total_idle_sockets + 1, pool_->IdleSocketCount()); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 938 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 939 | // Requesting a socket again should return the same socket as |
| 940 | // before, so should complete synchronously. |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 941 | EXPECT_THAT(handle.Init(group_id, params_, absl::nullopt, |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 942 | DEFAULT_PRIORITY, SocketTag(), |
| 943 | ClientSocketPool::RespectLimits::ENABLED, |
| 944 | callback.callback(), |
| 945 | ClientSocketPool::ProxyAuthCallback(), |
| 946 | pool_.get(), NetLogWithSource()), |
| 947 | IsOk()); |
| 948 | EXPECT_TRUE(handle.socket()); |
| 949 | EXPECT_EQ(total_idle_sockets, pool_->IdleSocketCount()); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 950 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 951 | // Return socket to pool again. |
| 952 | handle.Reset(); |
| 953 | EXPECT_EQ(total_idle_sockets + 1, pool_->IdleSocketCount()); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 954 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 955 | ++total_idle_sockets; |
| 956 | } |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 957 | } |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 958 | } |
| 959 | } |
| 960 | } |
| 961 | } |
| 962 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 963 | TEST_F(ClientSocketPoolBaseTest, TotalLimit) { |
| 964 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 965 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 966 | // TODO(eroman): Check that the NetLog contains this event. |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 967 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 968 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 969 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
| 970 | EXPECT_THAT(StartRequest(TestGroupId("c"), DEFAULT_PRIORITY), IsOk()); |
| 971 | EXPECT_THAT(StartRequest(TestGroupId("d"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 972 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 973 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 974 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 975 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 976 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 977 | EXPECT_THAT(StartRequest(TestGroupId("e"), DEFAULT_PRIORITY), |
| 978 | IsError(ERR_IO_PENDING)); |
| 979 | EXPECT_THAT(StartRequest(TestGroupId("f"), DEFAULT_PRIORITY), |
| 980 | IsError(ERR_IO_PENDING)); |
| 981 | EXPECT_THAT(StartRequest(TestGroupId("g"), DEFAULT_PRIORITY), |
| 982 | IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 983 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 984 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 985 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 986 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 987 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 988 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 989 | |
| 990 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 991 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 992 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 993 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 994 | EXPECT_EQ(5, GetOrderOfRequest(5)); |
| 995 | EXPECT_EQ(6, GetOrderOfRequest(6)); |
| 996 | EXPECT_EQ(7, GetOrderOfRequest(7)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 997 | |
| 998 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 999 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(8)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1000 | } |
| 1001 | |
| 1002 | TEST_F(ClientSocketPoolBaseTest, TotalLimitReachedNewGroup) { |
| 1003 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1004 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 1005 | // TODO(eroman): Check that the NetLog contains this event. |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 1006 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1007 | // Reach all limits: max total sockets, and max sockets per group. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1008 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1009 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1010 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
| 1011 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1012 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1013 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1014 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1015 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1016 | |
| 1017 | // Now create a new group and verify that we don't starve it. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1018 | EXPECT_THAT(StartRequest(TestGroupId("c"), DEFAULT_PRIORITY), |
| 1019 | IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1020 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1021 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1022 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1023 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1024 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1025 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1026 | |
| 1027 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1028 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1029 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 1030 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 1031 | EXPECT_EQ(5, GetOrderOfRequest(5)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1032 | |
| 1033 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1034 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(6)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1035 | } |
| 1036 | |
| 1037 | TEST_F(ClientSocketPoolBaseTest, TotalLimitRespectsPriority) { |
| 1038 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1039 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1040 | EXPECT_THAT(StartRequest(TestGroupId("b"), LOWEST), IsOk()); |
| 1041 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsOk()); |
| 1042 | EXPECT_THAT(StartRequest(TestGroupId("b"), HIGHEST), IsOk()); |
| 1043 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1044 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1045 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1046 | client_socket_factory_.allocation_count()); |
| 1047 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1048 | EXPECT_THAT(StartRequest(TestGroupId("c"), LOWEST), IsError(ERR_IO_PENDING)); |
| 1049 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1050 | EXPECT_THAT(StartRequest(TestGroupId("b"), HIGHEST), IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1051 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1052 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1053 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1054 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1055 | |
| 1056 | // First 4 requests don't have to wait, and finish in order. |
| 1057 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1058 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1059 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 1060 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 1061 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1062 | // Request ("b", HIGHEST) has the highest priority, then (TestGroupId("a"), |
| 1063 | // MEDIUM), and then ("c", LOWEST). |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1064 | EXPECT_EQ(7, GetOrderOfRequest(5)); |
| 1065 | EXPECT_EQ(6, GetOrderOfRequest(6)); |
| 1066 | EXPECT_EQ(5, GetOrderOfRequest(7)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1067 | |
| 1068 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1069 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(9)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1070 | } |
| 1071 | |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1072 | // Test reprioritizing a request before completion doesn't interfere with |
| 1073 | // its completion. |
| 1074 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeOne) { |
| 1075 | CreatePool(kDefaultMaxSockets, 1); |
| 1076 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1077 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1078 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1079 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1080 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1081 | |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 1082 | request(1)->handle()->SetPriority(HIGHEST); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1083 | |
| 1084 | ReleaseOneConnection(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1085 | |
| 1086 | EXPECT_TRUE(request(1)->handle()->socket()); |
| 1087 | } |
| 1088 | |
| 1089 | // Reprioritize a request up past another one and make sure that changes the |
| 1090 | // completion order. |
| 1091 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeUpReorder) { |
| 1092 | CreatePool(kDefaultMaxSockets, 1); |
| 1093 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1094 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1095 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1096 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1097 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1098 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1099 | EXPECT_FALSE(request(2)->handle()->socket()); |
| 1100 | |
| 1101 | request(2)->handle()->SetPriority(HIGHEST); |
| 1102 | |
| 1103 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1104 | |
| 1105 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1106 | EXPECT_EQ(3, GetOrderOfRequest(2)); |
| 1107 | EXPECT_EQ(2, GetOrderOfRequest(3)); |
| 1108 | } |
| 1109 | |
| 1110 | // Reprioritize a request without changing relative priorities and check |
| 1111 | // that the order doesn't change. |
| 1112 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeUpNoReorder) { |
| 1113 | CreatePool(kDefaultMaxSockets, 1); |
| 1114 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1115 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1116 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1117 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1118 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1119 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1120 | EXPECT_FALSE(request(2)->handle()->socket()); |
| 1121 | |
| 1122 | request(2)->handle()->SetPriority(MEDIUM); |
| 1123 | |
| 1124 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1125 | |
| 1126 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1127 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1128 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 1129 | } |
| 1130 | |
| 1131 | // Reprioritize a request past down another one and make sure that changes the |
| 1132 | // completion order. |
| 1133 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeDownReorder) { |
| 1134 | CreatePool(kDefaultMaxSockets, 1); |
| 1135 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1136 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1137 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1138 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1139 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1140 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1141 | EXPECT_FALSE(request(2)->handle()->socket()); |
| 1142 | |
| 1143 | request(1)->handle()->SetPriority(LOW); |
| 1144 | |
| 1145 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1146 | |
| 1147 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1148 | EXPECT_EQ(3, GetOrderOfRequest(2)); |
| 1149 | EXPECT_EQ(2, GetOrderOfRequest(3)); |
| 1150 | } |
| 1151 | |
| 1152 | // Reprioritize a request to the same level as another and confirm it is |
| 1153 | // put after the old request. |
| 1154 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeResetFIFO) { |
| 1155 | CreatePool(kDefaultMaxSockets, 1); |
| 1156 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1157 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1158 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1159 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1160 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1161 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1162 | EXPECT_FALSE(request(2)->handle()->socket()); |
| 1163 | |
| 1164 | request(1)->handle()->SetPriority(MEDIUM); |
| 1165 | |
| 1166 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1167 | |
| 1168 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1169 | EXPECT_EQ(3, GetOrderOfRequest(2)); |
| 1170 | EXPECT_EQ(2, GetOrderOfRequest(3)); |
| 1171 | } |
| 1172 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1173 | TEST_F(ClientSocketPoolBaseTest, TotalLimitRespectsGroupLimit) { |
| 1174 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1175 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1176 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsOk()); |
| 1177 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsOk()); |
| 1178 | EXPECT_THAT(StartRequest(TestGroupId("b"), HIGHEST), IsOk()); |
| 1179 | EXPECT_THAT(StartRequest(TestGroupId("b"), MEDIUM), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1180 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1181 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1182 | client_socket_factory_.allocation_count()); |
| 1183 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1184 | EXPECT_THAT(StartRequest(TestGroupId("c"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1185 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 1186 | EXPECT_THAT(StartRequest(TestGroupId("b"), HIGHEST), IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1187 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1188 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1189 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1190 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1191 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1192 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1193 | |
| 1194 | // First 4 requests don't have to wait, and finish in order. |
| 1195 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1196 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1197 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 1198 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 1199 | |
| 1200 | // Request ("b", 7) has the highest priority, but we can't make new socket for |
| 1201 | // group "b", because it has reached the per-group limit. Then we make |
| 1202 | // socket for ("c", 6), because it has higher priority than ("a", 4), |
| 1203 | // and we still can't make a socket for group "b". |
| 1204 | EXPECT_EQ(5, GetOrderOfRequest(5)); |
| 1205 | EXPECT_EQ(6, GetOrderOfRequest(6)); |
| 1206 | EXPECT_EQ(7, GetOrderOfRequest(7)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1207 | |
| 1208 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1209 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(8)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1210 | } |
| 1211 | |
| 1212 | // Make sure that we count connecting sockets against the total limit. |
| 1213 | TEST_F(ClientSocketPoolBaseTest, TotalLimitCountsConnectingSockets) { |
| 1214 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1215 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1216 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1217 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
| 1218 | EXPECT_THAT(StartRequest(TestGroupId("c"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1219 | |
| 1220 | // Create one asynchronous request. |
| 1221 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1222 | EXPECT_THAT(StartRequest(TestGroupId("d"), DEFAULT_PRIORITY), |
| 1223 | IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1224 | |
[email protected] | 6b17538 | 2009-10-13 06:47:47 | [diff] [blame] | 1225 | // We post all of our delayed tasks with a 2ms delay. I.e. they don't |
| 1226 | // actually become pending until 2ms after they have been created. In order |
| 1227 | // to flush all tasks, we need to wait so that we know there are no |
| 1228 | // soon-to-be-pending tasks waiting. |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 1229 | FastForwardBy(base::Milliseconds(10)); |
[email protected] | 6b17538 | 2009-10-13 06:47:47 | [diff] [blame] | 1230 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1231 | // The next synchronous request should wait for its turn. |
| 1232 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1233 | EXPECT_THAT(StartRequest(TestGroupId("e"), DEFAULT_PRIORITY), |
| 1234 | IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1235 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1236 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1237 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1238 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1239 | client_socket_factory_.allocation_count()); |
| 1240 | |
| 1241 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1242 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1243 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 1244 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1245 | EXPECT_EQ(5, GetOrderOfRequest(5)); |
| 1246 | |
| 1247 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1248 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(6)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1249 | } |
| 1250 | |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1251 | TEST_F(ClientSocketPoolBaseTest, CorrectlyCountStalledGroups) { |
| 1252 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 1253 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 1254 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1255 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1256 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1257 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1258 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1259 | |
| 1260 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 1261 | |
| 1262 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1263 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1264 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), |
| 1265 | IsError(ERR_IO_PENDING)); |
| 1266 | EXPECT_THAT(StartRequest(TestGroupId("c"), DEFAULT_PRIORITY), |
| 1267 | IsError(ERR_IO_PENDING)); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1268 | |
| 1269 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1270 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1271 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1272 | EXPECT_EQ(kDefaultMaxSockets + 1, client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1273 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1274 | EXPECT_EQ(kDefaultMaxSockets + 2, client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1275 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
| 1276 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1277 | EXPECT_EQ(kDefaultMaxSockets + 2, client_socket_factory_.allocation_count()); |
| 1278 | } |
| 1279 | |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 1280 | TEST_F(ClientSocketPoolBaseTest, StallAndThenCancelAndTriggerAvailableSocket) { |
| 1281 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 1282 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1283 | |
| 1284 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1285 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1286 | EXPECT_EQ( |
| 1287 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1288 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1289 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1290 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1291 | pool_.get(), NetLogWithSource())); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 1292 | |
| 1293 | ClientSocketHandle handles[4]; |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 1294 | for (size_t i = 0; i < base::size(handles); ++i) { |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1295 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1296 | handles[i].Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1297 | TestGroupId("b"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1298 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1299 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1300 | pool_.get(), NetLogWithSource())); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 1301 | } |
| 1302 | |
| 1303 | // One will be stalled, cancel all the handles now. |
| 1304 | // This should hit the OnAvailableSocketSlot() code where we previously had |
| 1305 | // stalled groups, but no longer have any. |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 1306 | for (size_t i = 0; i < base::size(handles); ++i) |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 1307 | handles[i].Reset(); |
| 1308 | } |
| 1309 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1310 | TEST_F(ClientSocketPoolBaseTest, CancelStalledSocketAtSocketLimit) { |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1311 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1312 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 1313 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1314 | { |
| 1315 | ClientSocketHandle handles[kDefaultMaxSockets]; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1316 | TestCompletionCallback callbacks[kDefaultMaxSockets]; |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1317 | for (int i = 0; i < kDefaultMaxSockets; ++i) { |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame] | 1318 | EXPECT_EQ(OK, handles[i].Init(TestGroupId("a" + base::NumberToString(i)), |
| 1319 | params_, absl::nullopt, DEFAULT_PRIORITY, |
| 1320 | SocketTag(), |
| 1321 | ClientSocketPool::RespectLimits::ENABLED, |
| 1322 | callbacks[i].callback(), |
| 1323 | ClientSocketPool::ProxyAuthCallback(), |
| 1324 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1325 | } |
| 1326 | |
| 1327 | // Force a stalled group. |
| 1328 | ClientSocketHandle stalled_handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1329 | TestCompletionCallback callback; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1330 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1331 | stalled_handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1332 | TestGroupId("foo"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1333 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1334 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1335 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1336 | |
| 1337 | // Cancel the stalled request. |
| 1338 | stalled_handle.Reset(); |
| 1339 | |
| 1340 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1341 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 1342 | |
| 1343 | // Dropping out of scope will close all handles and return them to idle. |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1344 | } |
| 1345 | |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1346 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1347 | EXPECT_EQ(kDefaultMaxSockets, pool_->IdleSocketCount()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1348 | } |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1349 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1350 | TEST_F(ClientSocketPoolBaseTest, CancelPendingSocketAtSocketLimit) { |
| 1351 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1352 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 1353 | |
| 1354 | { |
| 1355 | ClientSocketHandle handles[kDefaultMaxSockets]; |
| 1356 | for (int i = 0; i < kDefaultMaxSockets; ++i) { |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1357 | TestCompletionCallback callback; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1358 | EXPECT_EQ(ERR_IO_PENDING, |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame] | 1359 | handles[i].Init( |
| 1360 | TestGroupId("a" + base::NumberToString(i)), params_, |
| 1361 | absl::nullopt, DEFAULT_PRIORITY, SocketTag(), |
| 1362 | ClientSocketPool::RespectLimits::ENABLED, |
| 1363 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1364 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1365 | } |
| 1366 | |
| 1367 | // Force a stalled group. |
| 1368 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1369 | ClientSocketHandle stalled_handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1370 | TestCompletionCallback callback; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1371 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1372 | stalled_handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1373 | TestGroupId("foo"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1374 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1375 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1376 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1377 | |
| 1378 | // Since it is stalled, it should have no connect jobs. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1379 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("foo"))); |
| 1380 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 1381 | TestGroupId("foo"))); |
| 1382 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroupForTesting( |
| 1383 | TestGroupId("foo"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1384 | |
| 1385 | // Cancel the stalled request. |
| 1386 | handles[0].Reset(); |
| 1387 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1388 | // Now we should have a connect job. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1389 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("foo"))); |
| 1390 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 1391 | TestGroupId("foo"))); |
| 1392 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroupForTesting( |
| 1393 | TestGroupId("foo"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1394 | |
| 1395 | // The stalled socket should connect. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1396 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1397 | |
| 1398 | EXPECT_EQ(kDefaultMaxSockets + 1, |
| 1399 | client_socket_factory_.allocation_count()); |
| 1400 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1401 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("foo"))); |
| 1402 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 1403 | TestGroupId("foo"))); |
| 1404 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroupForTesting( |
| 1405 | TestGroupId("foo"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1406 | |
| 1407 | // Dropping out of scope will close all handles and return them to idle. |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1408 | } |
| 1409 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1410 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 1411 | } |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1412 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1413 | TEST_F(ClientSocketPoolBaseTest, WaitForStalledSocketAtSocketLimit) { |
| 1414 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1415 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1416 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1417 | ClientSocketHandle stalled_handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1418 | TestCompletionCallback callback; |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1419 | { |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 1420 | EXPECT_FALSE(pool_->IsStalled()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1421 | ClientSocketHandle handles[kDefaultMaxSockets]; |
| 1422 | for (int i = 0; i < kDefaultMaxSockets; ++i) { |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1423 | EXPECT_EQ( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1424 | OK, handles[i].Init( |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame] | 1425 | TestGroupId(base::StringPrintf("take-2-%d", i)), params_, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1426 | absl::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1427 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1428 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 1429 | NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1430 | } |
| 1431 | |
| 1432 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1433 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 1434 | EXPECT_FALSE(pool_->IsStalled()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1435 | |
| 1436 | // Now we will hit the socket limit. |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1437 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1438 | stalled_handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1439 | TestGroupId("foo"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1440 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1441 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1442 | pool_.get(), NetLogWithSource())); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 1443 | EXPECT_TRUE(pool_->IsStalled()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1444 | |
| 1445 | // Dropping out of scope will close all handles and return them to idle. |
| 1446 | } |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1447 | |
| 1448 | // But if we wait for it, the released idle sockets will be closed in |
| 1449 | // preference of the waiting request. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1450 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1451 | |
| 1452 | EXPECT_EQ(kDefaultMaxSockets + 1, client_socket_factory_.allocation_count()); |
| 1453 | EXPECT_EQ(3, pool_->IdleSocketCount()); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1454 | } |
| 1455 | |
| 1456 | // Regression test for http://crbug.com/40952. |
| 1457 | TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketAtSocketLimitDeleteGroup) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1458 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 1459 | true /* enable_backup_connect_jobs */); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1460 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 1461 | |
| 1462 | for (int i = 0; i < kDefaultMaxSockets; ++i) { |
| 1463 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1464 | TestCompletionCallback callback; |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame] | 1465 | EXPECT_EQ( |
| 1466 | OK, |
| 1467 | handle.Init(TestGroupId("a" + base::NumberToString(i)), params_, |
| 1468 | absl::nullopt, DEFAULT_PRIORITY, SocketTag(), |
| 1469 | ClientSocketPool::RespectLimits::ENABLED, |
| 1470 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1471 | pool_.get(), NetLogWithSource())); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1472 | } |
| 1473 | |
| 1474 | // Flush all the DoReleaseSocket tasks. |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 1475 | base::RunLoop().RunUntilIdle(); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1476 | |
| 1477 | // Stall a group. Set a pending job so it'll trigger a backup job if we don't |
| 1478 | // reuse a socket. |
| 1479 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1480 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1481 | TestCompletionCallback callback; |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1482 | |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame] | 1483 | // "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] | 1484 | // which is the one which we would close an idle socket for. We shouldn't |
| 1485 | // close an idle socket though, since we should reuse the idle socket. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1486 | EXPECT_EQ(OK, handle.Init( |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame] | 1487 | TestGroupId("a0"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1488 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1489 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1490 | pool_.get(), NetLogWithSource())); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1491 | |
| 1492 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1493 | EXPECT_EQ(kDefaultMaxSockets - 1, pool_->IdleSocketCount()); |
| 1494 | } |
| 1495 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1496 | TEST_F(ClientSocketPoolBaseTest, PendingRequests) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1497 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1498 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1499 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1500 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1501 | EXPECT_THAT(StartRequest(TestGroupId("a"), IDLE), IsError(ERR_IO_PENDING)); |
| 1502 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
| 1503 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1504 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1505 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 1506 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1507 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1508 | ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1509 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
| 1510 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1511 | EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup, |
| 1512 | completion_count()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1513 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1514 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1515 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
[email protected] | c9c6f5c | 2010-07-31 01:30:03 | [diff] [blame] | 1516 | EXPECT_EQ(8, GetOrderOfRequest(3)); |
| 1517 | EXPECT_EQ(6, GetOrderOfRequest(4)); |
| 1518 | EXPECT_EQ(4, GetOrderOfRequest(5)); |
| 1519 | EXPECT_EQ(3, GetOrderOfRequest(6)); |
| 1520 | EXPECT_EQ(5, GetOrderOfRequest(7)); |
| 1521 | EXPECT_EQ(7, GetOrderOfRequest(8)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1522 | |
| 1523 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1524 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(9)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1525 | } |
| 1526 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1527 | TEST_F(ClientSocketPoolBaseTest, PendingRequests_NoKeepAlive) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1528 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1529 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1530 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1531 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1532 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
| 1533 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1534 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1535 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 1536 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1537 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1538 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1539 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1540 | for (size_t i = kDefaultMaxSocketsPerGroup; i < requests_size(); ++i) |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1541 | EXPECT_THAT(request(i)->WaitForResult(), IsOk()); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1542 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1543 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1544 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1545 | EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup, |
| 1546 | completion_count()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1547 | } |
| 1548 | |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1549 | TEST_F(ClientSocketPoolBaseTest, ResetAndCloseSocket) { |
| 1550 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1551 | |
| 1552 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1553 | ClientSocketHandle handle; |
| 1554 | TestCompletionCallback callback; |
| 1555 | EXPECT_EQ( |
| 1556 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1557 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1558 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1559 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1560 | pool_.get(), NetLogWithSource())); |
| 1561 | |
| 1562 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 1563 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1564 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1565 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
| 1566 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 1567 | |
| 1568 | handle.ResetAndCloseSocket(); |
| 1569 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1570 | } |
| 1571 | |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1572 | // This test will start up a socket request and then call Reset() on the handle. |
| 1573 | // The pending ConnectJob should not be destroyed. |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1574 | TEST_F(ClientSocketPoolBaseTest, CancelRequestKeepsConnectJob) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1575 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1576 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1577 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1578 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1579 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1580 | EXPECT_EQ( |
| 1581 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1582 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1583 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1584 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1585 | pool_.get(), NetLogWithSource())); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1586 | handle.Reset(); |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1587 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1588 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1589 | } |
| 1590 | |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1591 | // This test will start up a socket request and then call ResetAndCloseSocket() |
| 1592 | // on the handle. The pending ConnectJob or connected socket should be |
| 1593 | // destroyed. |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1594 | TEST_F(ClientSocketPoolBaseTest, CancelRequestAndCloseSocket) { |
| 1595 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1596 | |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1597 | // When true, the socket connects before it's canceled. |
| 1598 | for (bool cancel_when_callback_pending : {false, true}) { |
| 1599 | if (cancel_when_callback_pending) { |
| 1600 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 1601 | } else { |
| 1602 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1603 | } |
| 1604 | ClientSocketHandle handle; |
| 1605 | TestCompletionCallback callback; |
| 1606 | EXPECT_EQ( |
| 1607 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1608 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1609 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1610 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1611 | pool_.get(), NetLogWithSource())); |
| 1612 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1613 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1614 | |
| 1615 | if (cancel_when_callback_pending) { |
| 1616 | client_socket_factory_.SignalJobs(); |
| 1617 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1618 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 1619 | } |
| 1620 | |
| 1621 | handle.ResetAndCloseSocket(); |
| 1622 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1623 | } |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1624 | } |
| 1625 | |
| 1626 | TEST_F(ClientSocketPoolBaseTest, |
| 1627 | CancelRequestAndCloseSocketWhenMoreRequestsThanConnectJobs) { |
| 1628 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1629 | |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1630 | // When true, the sockets connect before they're canceled. |
| 1631 | for (bool cancel_when_callback_pending : {false, true}) { |
| 1632 | if (cancel_when_callback_pending) { |
| 1633 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 1634 | } else { |
| 1635 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1636 | } |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1637 | |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1638 | std::vector<std::unique_ptr<ClientSocketHandle>> handles; |
| 1639 | TestCompletionCallback callback; |
| 1640 | // Make |kDefaultMaxSockets + 1| socket requests. |
| 1641 | for (int i = 0; i < kDefaultMaxSocketsPerGroup + 1; ++i) { |
| 1642 | std::unique_ptr<ClientSocketHandle> handle = |
| 1643 | std::make_unique<ClientSocketHandle>(); |
| 1644 | EXPECT_EQ(ERR_IO_PENDING, |
| 1645 | handle->Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1646 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1647 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1648 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1649 | pool_.get(), NetLogWithSource())); |
| 1650 | handles.push_back(std::move(handle)); |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1651 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1652 | EXPECT_EQ( |
| 1653 | static_cast<size_t>(std::min(i + 1, kDefaultMaxSocketsPerGroup)), |
| 1654 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1655 | } |
| 1656 | |
| 1657 | if (cancel_when_callback_pending) { |
| 1658 | client_socket_factory_.SignalJobs(); |
| 1659 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1660 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
| 1661 | pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 1662 | } |
| 1663 | |
| 1664 | // Calling ResetAndCloseSocket() on a handle should not cancel a ConnectJob |
| 1665 | // or close a socket, since there are more requests than ConnectJobs or |
| 1666 | // sockets. |
| 1667 | handles[kDefaultMaxSocketsPerGroup]->ResetAndCloseSocket(); |
| 1668 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1669 | if (cancel_when_callback_pending) { |
| 1670 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
| 1671 | pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 1672 | } else { |
| 1673 | EXPECT_EQ(static_cast<size_t>(kDefaultMaxSocketsPerGroup), |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1674 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1675 | } |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1676 | |
| 1677 | // Calling ResetAndCloseSocket() on other handles should cancel a ConnectJob |
| 1678 | // or close a socket. |
| 1679 | for (int i = kDefaultMaxSocketsPerGroup - 1; i >= 0; --i) { |
| 1680 | handles[i]->ResetAndCloseSocket(); |
| 1681 | if (i > 0) { |
| 1682 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1683 | if (cancel_when_callback_pending) { |
| 1684 | EXPECT_EQ(i, |
| 1685 | pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 1686 | } else { |
| 1687 | EXPECT_EQ(static_cast<size_t>(i), |
| 1688 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1689 | } |
| 1690 | } else { |
| 1691 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1692 | } |
| 1693 | } |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1694 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1695 | } |
| 1696 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1697 | TEST_F(ClientSocketPoolBaseTest, ConnectCancelConnect) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1698 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1699 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1700 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 1701 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1702 | TestCompletionCallback callback; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1703 | |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1704 | EXPECT_EQ( |
| 1705 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1706 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1707 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1708 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1709 | pool_.get(), NetLogWithSource())); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1710 | |
| 1711 | handle.Reset(); |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1712 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1713 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1714 | |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1715 | // This will create a second ConnectJob, since the other ConnectJob was |
| 1716 | // previously assigned to a request. |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1717 | TestCompletionCallback callback2; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1718 | EXPECT_EQ( |
| 1719 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1720 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1721 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1722 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1723 | pool_.get(), NetLogWithSource())); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1724 | |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1725 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1726 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1727 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1728 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1729 | EXPECT_FALSE(callback.have_result()); |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1730 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1731 | // One ConnectJob completed, and its socket is now assigned to |handle|. |
| 1732 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 1733 | // The other ConnectJob should have either completed, or still be connecting. |
| 1734 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")) + |
| 1735 | pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1736 | |
| 1737 | handle.Reset(); |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1738 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1739 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")) + |
| 1740 | pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
| 1741 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1742 | } |
| 1743 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1744 | TEST_F(ClientSocketPoolBaseTest, CancelRequest) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1745 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1746 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1747 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1748 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1749 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
| 1750 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1751 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1752 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 1753 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1754 | |
| 1755 | // Cancel a request. |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1756 | size_t index_to_cancel = kDefaultMaxSocketsPerGroup + 2; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1757 | EXPECT_FALSE((*requests())[index_to_cancel]->handle()->is_initialized()); |
| 1758 | (*requests())[index_to_cancel]->handle()->Reset(); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1759 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1760 | ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE); |
[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(kDefaultMaxSocketsPerGroup, |
| 1763 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1764 | EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup - 1, |
| 1765 | completion_count()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1766 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1767 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1768 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1769 | EXPECT_EQ(5, GetOrderOfRequest(3)); |
| 1770 | EXPECT_EQ(3, GetOrderOfRequest(4)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1771 | EXPECT_EQ(ClientSocketPoolTest::kRequestNotFound, |
| 1772 | GetOrderOfRequest(5)); // Canceled request. |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1773 | EXPECT_EQ(4, GetOrderOfRequest(6)); |
| 1774 | EXPECT_EQ(6, GetOrderOfRequest(7)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1775 | |
| 1776 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1777 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(8)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1778 | } |
| 1779 | |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1780 | // Function to be used as a callback on socket request completion. It first |
| 1781 | // disconnects the successfully connected socket from the first request, and |
| 1782 | // then reuses the ClientSocketHandle to request another socket. |
| 1783 | // |
| 1784 | // |nested_callback| is called with the result of the second socket request. |
| 1785 | void RequestSocketOnComplete(ClientSocketHandle* handle, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1786 | TransportClientSocketPool* pool, |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1787 | TestConnectJobFactory* test_connect_job_factory, |
| 1788 | TestConnectJob::JobType next_job_type, |
Bence Béky | a4a5093 | 2018-08-10 13:39:41 | [diff] [blame] | 1789 | TestCompletionCallback* nested_callback, |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1790 | int first_request_result) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1791 | EXPECT_THAT(first_request_result, IsOk()); |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1792 | |
| 1793 | test_connect_job_factory->set_job_type(next_job_type); |
| 1794 | |
| 1795 | // Don't allow reuse of the socket. Disconnect it and then release it. |
| 1796 | if (handle->socket()) |
| 1797 | handle->socket()->Disconnect(); |
| 1798 | handle->Reset(); |
| 1799 | |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1800 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1801 | int rv = handle->Init( |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 1802 | TestGroupId("a"), |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1803 | ClientSocketPool::SocketParams::CreateForHttpForTesting(), absl::nullopt, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1804 | LOWEST, SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 1805 | nested_callback->callback(), ClientSocketPool::ProxyAuthCallback(), pool, |
| 1806 | NetLogWithSource()); |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1807 | if (rv != ERR_IO_PENDING) { |
| 1808 | DCHECK_EQ(TestConnectJob::kMockJob, next_job_type); |
Bence Béky | a4a5093 | 2018-08-10 13:39:41 | [diff] [blame] | 1809 | nested_callback->callback().Run(rv); |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1810 | } else { |
| 1811 | DCHECK_EQ(TestConnectJob::kMockPendingJob, next_job_type); |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1812 | } |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1813 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1814 | |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1815 | // Tests the case where a second socket is requested in a completion callback, |
| 1816 | // and the second socket connects asynchronously. Reuses the same |
| 1817 | // ClientSocketHandle for the second socket, after disconnecting the first. |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1818 | TEST_F(ClientSocketPoolBaseTest, RequestPendingJobTwice) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1819 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1820 | |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 1821 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 1822 | ClientSocketHandle handle; |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1823 | TestCompletionCallback second_result_callback; |
| 1824 | int rv = handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1825 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Paul Jensen | 8d6f87ec | 2018-01-13 00:46:54 | [diff] [blame] | 1826 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1827 | base::BindOnce(&RequestSocketOnComplete, &handle, pool_.get(), |
| 1828 | connect_job_factory_, TestConnectJob::kMockPendingJob, |
| 1829 | &second_result_callback), |
| 1830 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1831 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1832 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1833 | EXPECT_THAT(second_result_callback.WaitForResult(), IsOk()); |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1834 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1835 | |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1836 | // Tests the case where a second socket is requested in a completion callback, |
| 1837 | // and the second socket connects synchronously. Reuses the same |
| 1838 | // ClientSocketHandle for the second socket, after disconnecting the first. |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1839 | TEST_F(ClientSocketPoolBaseTest, RequestPendingJobThenSynchronous) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1840 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1841 | |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 1842 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 1843 | ClientSocketHandle handle; |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1844 | TestCompletionCallback second_result_callback; |
| 1845 | int rv = handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1846 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Paul Jensen | 8d6f87ec | 2018-01-13 00:46:54 | [diff] [blame] | 1847 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1848 | base::BindOnce(&RequestSocketOnComplete, &handle, pool_.get(), |
| 1849 | connect_job_factory_, TestConnectJob::kMockPendingJob, |
| 1850 | &second_result_callback), |
| 1851 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1852 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1853 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1854 | EXPECT_THAT(second_result_callback.WaitForResult(), IsOk()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1855 | } |
| 1856 | |
| 1857 | // Make sure that pending requests get serviced after active requests get |
| 1858 | // cancelled. |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1859 | TEST_F(ClientSocketPoolBaseTest, CancelActiveRequestWithPendingRequests) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1860 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1861 | |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 1862 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1863 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1864 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1865 | IsError(ERR_IO_PENDING)); |
| 1866 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1867 | IsError(ERR_IO_PENDING)); |
| 1868 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1869 | IsError(ERR_IO_PENDING)); |
| 1870 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1871 | IsError(ERR_IO_PENDING)); |
| 1872 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1873 | IsError(ERR_IO_PENDING)); |
| 1874 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1875 | IsError(ERR_IO_PENDING)); |
| 1876 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1877 | IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1878 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1879 | // Now, kDefaultMaxSocketsPerGroup requests should be active. |
| 1880 | // Let's cancel them. |
| 1881 | for (int i = 0; i < kDefaultMaxSocketsPerGroup; ++i) { |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1882 | ASSERT_FALSE(request(i)->handle()->is_initialized()); |
| 1883 | request(i)->handle()->Reset(); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1884 | } |
| 1885 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1886 | // Let's wait for the rest to complete now. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1887 | for (size_t i = kDefaultMaxSocketsPerGroup; i < requests_size(); ++i) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1888 | EXPECT_THAT(request(i)->WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1889 | request(i)->handle()->Reset(); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1890 | } |
| 1891 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1892 | EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup, |
| 1893 | completion_count()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1894 | } |
| 1895 | |
| 1896 | // Make sure that pending requests get serviced after active requests fail. |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1897 | TEST_F(ClientSocketPoolBaseTest, FailingActiveRequestWithPendingRequests) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1898 | const size_t kMaxSockets = 5; |
| 1899 | CreatePool(kMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1900 | |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 1901 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1902 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1903 | const size_t kNumberOfRequests = 2 * kDefaultMaxSocketsPerGroup + 1; |
| 1904 | ASSERT_LE(kNumberOfRequests, kMaxSockets); // Otherwise the test will hang. |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1905 | |
| 1906 | // Queue up all the requests |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1907 | for (size_t i = 0; i < kNumberOfRequests; ++i) |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1908 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1909 | IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1910 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1911 | for (size_t i = 0; i < kNumberOfRequests; ++i) |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1912 | EXPECT_THAT(request(i)->WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1913 | } |
| 1914 | |
mmenke | 9d72fe4 | 2017-05-18 22:36:07 | [diff] [blame] | 1915 | // Make sure that pending requests that complete synchronously get serviced |
| 1916 | // after active requests fail. See https://crbug.com/723748 |
| 1917 | TEST_F(ClientSocketPoolBaseTest, HandleMultipleSyncFailuresAfterAsyncFailure) { |
| 1918 | const size_t kNumberOfRequests = 10; |
| 1919 | const size_t kMaxSockets = 1; |
| 1920 | CreatePool(kMaxSockets, kMaxSockets); |
| 1921 | |
| 1922 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
| 1923 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1924 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1925 | IsError(ERR_IO_PENDING)); |
mmenke | 9d72fe4 | 2017-05-18 22:36:07 | [diff] [blame] | 1926 | |
| 1927 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
| 1928 | |
| 1929 | // Queue up all the other requests |
| 1930 | for (size_t i = 1; i < kNumberOfRequests; ++i) |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1931 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1932 | IsError(ERR_IO_PENDING)); |
mmenke | 9d72fe4 | 2017-05-18 22:36:07 | [diff] [blame] | 1933 | |
| 1934 | // Make sure all requests fail, instead of hanging. |
| 1935 | for (size_t i = 0; i < kNumberOfRequests; ++i) |
| 1936 | EXPECT_THAT(request(i)->WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 1937 | } |
| 1938 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1939 | TEST_F(ClientSocketPoolBaseTest, CancelActiveRequestThenRequestSocket) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1940 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1941 | |
| 1942 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1943 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1944 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1945 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1946 | int rv = handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1947 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1948 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1949 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1950 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1951 | |
| 1952 | // Cancel the active request. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1953 | handle.Reset(); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1954 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1955 | rv = handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1956 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1957 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1958 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1959 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 1960 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1961 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1962 | EXPECT_FALSE(handle.is_reused()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 1963 | TestLoadTimingInfoConnectedNotReused(handle); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1964 | EXPECT_EQ(2, client_socket_factory_.allocation_count()); |
| 1965 | } |
| 1966 | |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1967 | TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketsForced) { |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 1968 | const char kReason[] = "Really nifty reason"; |
| 1969 | |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1970 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1971 | ClientSocketHandle handle; |
| 1972 | TestCompletionCallback callback; |
Matt Reichhoff | 0049a0b7 | 2021-10-20 20:44:26 | [diff] [blame^] | 1973 | int rv = |
| 1974 | handle.Init(TestGroupId("a"), params_, absl::nullopt, LOWEST, SocketTag(), |
| 1975 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1976 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 1977 | NetLogWithSource::Make(NetLogSourceType::NONE)); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1978 | EXPECT_THAT(rv, IsOk()); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 1979 | ASSERT_TRUE(handle.socket()); |
| 1980 | NetLogSource source = handle.socket()->NetLog().source(); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1981 | handle.Reset(); |
| 1982 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 1983 | pool_->CloseIdleSockets(kReason); |
| 1984 | ExpectSocketClosedWithReason(source, kReason); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1985 | } |
| 1986 | |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1987 | TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketsInGroupForced) { |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1988 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1989 | TestCompletionCallback callback; |
Matt Reichhoff | 0049a0b7 | 2021-10-20 20:44:26 | [diff] [blame^] | 1990 | NetLogWithSource net_log_with_source = |
| 1991 | NetLogWithSource::Make(NetLogSourceType::NONE); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1992 | ClientSocketHandle handle1; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1993 | int rv = handle1.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1994 | TestGroupId("a"), params_, absl::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1995 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
Matt Reichhoff | 0049a0b7 | 2021-10-20 20:44:26 | [diff] [blame^] | 1996 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), net_log_with_source); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1997 | EXPECT_THAT(rv, IsOk()); |
| 1998 | ClientSocketHandle handle2; |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1999 | rv = handle2.Init(TestGroupId("a"), params_, absl::nullopt, LOWEST, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2000 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2001 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
Matt Reichhoff | 0049a0b7 | 2021-10-20 20:44:26 | [diff] [blame^] | 2002 | pool_.get(), net_log_with_source); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 2003 | ClientSocketHandle handle3; |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2004 | rv = handle3.Init(TestGroupId("b"), params_, absl::nullopt, LOWEST, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2005 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2006 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
Matt Reichhoff | 0049a0b7 | 2021-10-20 20:44:26 | [diff] [blame^] | 2007 | pool_.get(), net_log_with_source); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 2008 | EXPECT_THAT(rv, IsOk()); |
| 2009 | handle1.Reset(); |
| 2010 | handle2.Reset(); |
| 2011 | handle3.Reset(); |
| 2012 | EXPECT_EQ(3, pool_->IdleSocketCount()); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 2013 | pool_->CloseIdleSocketsInGroup(TestGroupId("a"), "Very good reason"); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 2014 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 2015 | } |
| 2016 | |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 2017 | TEST_F(ClientSocketPoolBaseTest, CleanUpUnusableIdleSockets) { |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 2018 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2019 | ClientSocketHandle handle; |
| 2020 | TestCompletionCallback callback; |
Matt Reichhoff | 0049a0b7 | 2021-10-20 20:44:26 | [diff] [blame^] | 2021 | NetLogWithSource net_log_with_source = |
| 2022 | NetLogWithSource::Make(NetLogSourceType::NONE); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2023 | int rv = handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2024 | TestGroupId("a"), params_, absl::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2025 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
Matt Reichhoff | 0049a0b7 | 2021-10-20 20:44:26 | [diff] [blame^] | 2026 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), net_log_with_source); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 2027 | EXPECT_THAT(rv, IsOk()); |
| 2028 | StreamSocket* socket = handle.socket(); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 2029 | ASSERT_TRUE(socket); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 2030 | handle.Reset(); |
| 2031 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 2032 | |
| 2033 | // Disconnect socket now to make the socket unusable. |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 2034 | NetLogSource source = socket->NetLog().source(); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 2035 | socket->Disconnect(); |
| 2036 | ClientSocketHandle handle2; |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2037 | rv = handle2.Init(TestGroupId("a"), params_, absl::nullopt, LOWEST, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2038 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2039 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
Matt Reichhoff | 0049a0b7 | 2021-10-20 20:44:26 | [diff] [blame^] | 2040 | pool_.get(), net_log_with_source); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 2041 | EXPECT_THAT(rv, IsOk()); |
| 2042 | EXPECT_FALSE(handle2.is_reused()); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 2043 | |
| 2044 | // This is admittedly not an accurate error in this case, but normally code |
| 2045 | // doesn't secretly keep a raw pointers to sockets returned to the socket pool |
| 2046 | // and close them out of band, so discovering an idle socket was closed when |
| 2047 | // trying to reuse it normally means it was closed by the remote side. |
| 2048 | ExpectSocketClosedWithReason( |
| 2049 | source, TransportClientSocketPool::kRemoteSideClosedConnection); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 2050 | } |
| 2051 | |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 2052 | // Regression test for http://crbug.com/17985. |
| 2053 | TEST_F(ClientSocketPoolBaseTest, GroupWithPendingRequestsIsNotEmpty) { |
| 2054 | const int kMaxSockets = 3; |
| 2055 | const int kMaxSocketsPerGroup = 2; |
| 2056 | CreatePool(kMaxSockets, kMaxSocketsPerGroup); |
| 2057 | |
[email protected] | ac790b4 | 2009-12-02 04:31:31 | [diff] [blame] | 2058 | const RequestPriority kHighPriority = HIGHEST; |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 2059 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2060 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 2061 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 2062 | |
| 2063 | // This is going to be a pending request in an otherwise empty group. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2064 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2065 | IsError(ERR_IO_PENDING)); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 2066 | |
| 2067 | // Reach the maximum socket limit. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2068 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 2069 | |
| 2070 | // Create a stalled group with high priorities. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2071 | EXPECT_THAT(StartRequest(TestGroupId("c"), kHighPriority), |
| 2072 | IsError(ERR_IO_PENDING)); |
| 2073 | EXPECT_THAT(StartRequest(TestGroupId("c"), kHighPriority), |
| 2074 | IsError(ERR_IO_PENDING)); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 2075 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2076 | // Release the first two sockets from TestGroupId("a"). Because this is a |
| 2077 | // keepalive, the first release will unblock the pending request for |
| 2078 | // TestGroupId("a"). The second release will unblock a request for "c", |
| 2079 | // because it is the next high priority socket. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2080 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
| 2081 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 2082 | |
| 2083 | // Closing idle sockets should not get us into trouble, but in the bug |
| 2084 | // we were hitting a CHECK here. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2085 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 2086 | pool_->CloseIdleSockets("Very good reason"); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 2087 | |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 2088 | // Run the released socket wakeups. |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 2089 | base::RunLoop().RunUntilIdle(); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 2090 | } |
| 2091 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2092 | TEST_F(ClientSocketPoolBaseTest, BasicAsynchronous) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2093 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2094 | |
| 2095 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2096 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2097 | TestCompletionCallback callback; |
Matt Reichhoff | 0049a0b7 | 2021-10-20 20:44:26 | [diff] [blame^] | 2098 | NetLogWithSource net_log_with_source = |
| 2099 | NetLogWithSource::Make(NetLogSourceType::NONE); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2100 | int rv = handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2101 | TestGroupId("a"), params_, absl::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2102 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
Matt Reichhoff | 0049a0b7 | 2021-10-20 20:44:26 | [diff] [blame^] | 2103 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), net_log_with_source); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2104 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2105 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2106 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 2107 | TestLoadTimingInfoNotConnected(handle); |
| 2108 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2109 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2110 | EXPECT_TRUE(handle.is_initialized()); |
| 2111 | EXPECT_TRUE(handle.socket()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 2112 | TestLoadTimingInfoConnectedNotReused(handle); |
| 2113 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2114 | handle.Reset(); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 2115 | TestLoadTimingInfoNotConnected(handle); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 2116 | |
Matt Reichhoff | 0049a0b7 | 2021-10-20 20:44:26 | [diff] [blame^] | 2117 | auto entries = |
| 2118 | net_log_observer_.GetEntriesForSource(net_log_with_source.source()); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 2119 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2120 | EXPECT_EQ(5u, entries.size()); |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 2121 | EXPECT_TRUE(LogContainsEvent( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2122 | entries, 0, NetLogEventType::TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 2123 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2124 | EXPECT_TRUE(LogContainsBeginEvent(entries, 1, NetLogEventType::SOCKET_POOL)); |
| 2125 | EXPECT_TRUE(LogContainsEvent( |
| 2126 | entries, 2, NetLogEventType::SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
| 2127 | NetLogEventPhase::NONE)); |
| 2128 | EXPECT_TRUE(LogContainsEvent(entries, 3, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 2129 | NetLogEventType::SOCKET_POOL_BOUND_TO_SOCKET, |
| 2130 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2131 | EXPECT_TRUE(LogContainsEndEvent(entries, 4, NetLogEventType::SOCKET_POOL)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2132 | } |
| 2133 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2134 | TEST_F(ClientSocketPoolBaseTest, |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2135 | InitConnectionAsynchronousFailure) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2136 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2137 | |
| 2138 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2139 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2140 | TestCompletionCallback callback; |
Matt Reichhoff | 0049a0b7 | 2021-10-20 20:44:26 | [diff] [blame^] | 2141 | NetLogWithSource net_log_with_source = |
| 2142 | NetLogWithSource::Make(NetLogSourceType::NONE); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2143 | // Set the additional error state members to ensure that they get cleared. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2144 | handle.set_is_ssl_error(true); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 2145 | handle.set_ssl_cert_request_info(base::MakeRefCounted<SSLCertRequestInfo>()); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2146 | EXPECT_EQ( |
| 2147 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2148 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2149 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2150 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
Matt Reichhoff | 0049a0b7 | 2021-10-20 20:44:26 | [diff] [blame^] | 2151 | pool_.get(), net_log_with_source)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2152 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2153 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2154 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2155 | EXPECT_FALSE(handle.is_ssl_error()); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 2156 | EXPECT_FALSE(handle.ssl_cert_request_info()); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 2157 | |
Matt Reichhoff | 0049a0b7 | 2021-10-20 20:44:26 | [diff] [blame^] | 2158 | auto entries = |
| 2159 | net_log_observer_.GetEntriesForSource(net_log_with_source.source()); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 2160 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2161 | EXPECT_EQ(4u, entries.size()); |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 2162 | EXPECT_TRUE(LogContainsEvent( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2163 | entries, 0, NetLogEventType::TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 2164 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2165 | EXPECT_TRUE(LogContainsBeginEvent(entries, 1, NetLogEventType::SOCKET_POOL)); |
| 2166 | EXPECT_TRUE(LogContainsEvent( |
| 2167 | entries, 2, NetLogEventType::SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
| 2168 | NetLogEventPhase::NONE)); |
| 2169 | EXPECT_TRUE(LogContainsEndEvent(entries, 3, NetLogEventType::SOCKET_POOL)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2170 | } |
| 2171 | |
mmenke | 6be122f | 2015-03-09 22:22:47 | [diff] [blame] | 2172 | // Check that an async ConnectJob failure does not result in creation of a new |
| 2173 | // ConnectJob when there's another pending request also waiting on its own |
| 2174 | // ConnectJob. See http://crbug.com/463960. |
| 2175 | TEST_F(ClientSocketPoolBaseTest, AsyncFailureWithPendingRequestWithJob) { |
| 2176 | CreatePool(2, 2); |
| 2177 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
| 2178 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2179 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2180 | IsError(ERR_IO_PENDING)); |
| 2181 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2182 | IsError(ERR_IO_PENDING)); |
mmenke | 6be122f | 2015-03-09 22:22:47 | [diff] [blame] | 2183 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2184 | EXPECT_THAT(request(0)->WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 2185 | EXPECT_THAT(request(1)->WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
mmenke | 6be122f | 2015-03-09 22:22:47 | [diff] [blame] | 2186 | |
| 2187 | EXPECT_EQ(2, client_socket_factory_.allocation_count()); |
| 2188 | } |
| 2189 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2190 | TEST_F(ClientSocketPoolBaseTest, TwoRequestsCancelOne) { |
[email protected] | b22b516 | 2010-03-16 07:53:10 | [diff] [blame] | 2191 | // TODO(eroman): Add back the log expectations! Removed them because the |
| 2192 | // ordering is difficult, and some may fire during destructor. |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2193 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2194 | |
| 2195 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2196 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2197 | TestCompletionCallback callback; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2198 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2199 | TestCompletionCallback callback2; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2200 | |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2201 | EXPECT_EQ( |
| 2202 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2203 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2204 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2205 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2206 | pool_.get(), NetLogWithSource())); |
Matt Reichhoff | 0049a0b7 | 2021-10-20 20:44:26 | [diff] [blame^] | 2207 | RecordingNetLogObserver log2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 2208 | EXPECT_EQ( |
| 2209 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2210 | handle2.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2211 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2212 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2213 | pool_.get(), NetLogWithSource())); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2214 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2215 | handle.Reset(); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2216 | |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 2217 | |
| 2218 | // 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] | 2219 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2220 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2221 | handle2.Reset(); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 2222 | |
| 2223 | // Now request 2 has actually finished. |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 2224 | // TODO(eroman): Add back log expectations. |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2225 | } |
| 2226 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2227 | TEST_F(ClientSocketPoolBaseTest, CancelRequestLimitsJobs) { |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 2228 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2229 | |
[email protected] | 17a0c6c | 2009-08-04 00:07:04 | [diff] [blame] | 2230 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2231 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2232 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
| 2233 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 2234 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 2235 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 2236 | |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2237 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2238 | static_cast<int>( |
| 2239 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2240 | (*requests())[2]->handle()->Reset(); |
| 2241 | (*requests())[3]->handle()->Reset(); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2242 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2243 | static_cast<int>( |
| 2244 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 2245 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2246 | (*requests())[1]->handle()->Reset(); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2247 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2248 | static_cast<int>( |
| 2249 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 2250 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2251 | (*requests())[0]->handle()->Reset(); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2252 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2253 | static_cast<int>( |
| 2254 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 2255 | } |
| 2256 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2257 | // When requests and ConnectJobs are not coupled, the request will get serviced |
| 2258 | // by whatever comes first. |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2259 | TEST_F(ClientSocketPoolBaseTest, ReleaseSockets) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2260 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2261 | |
| 2262 | // Start job 1 (async OK) |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 2263 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2264 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2265 | std::vector<TestSocketRequest*> request_order; |
| 2266 | size_t completion_count; // unused |
| 2267 | TestSocketRequest req1(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2268 | int rv = req1.handle()->Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2269 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2270 | ClientSocketPool::RespectLimits::ENABLED, req1.callback(), |
| 2271 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2272 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 2273 | EXPECT_THAT(req1.WaitForResult(), IsOk()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2274 | |
| 2275 | // Job 1 finished OK. Start job 2 (also async OK). Request 3 is pending |
| 2276 | // without a job. |
| 2277 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2278 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2279 | TestSocketRequest req2(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2280 | rv = req2.handle()->Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2281 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2282 | ClientSocketPool::RespectLimits::ENABLED, req2.callback(), |
| 2283 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2284 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2285 | TestSocketRequest req3(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2286 | rv = req3.handle()->Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2287 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2288 | ClientSocketPool::RespectLimits::ENABLED, req3.callback(), |
| 2289 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2290 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2291 | |
| 2292 | // Both Requests 2 and 3 are pending. We release socket 1 which should |
| 2293 | // service request 2. Request 3 should still be waiting. |
[email protected] | a6c59f6 | 2009-07-29 16:33:33 | [diff] [blame] | 2294 | req1.handle()->Reset(); |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 2295 | // Run the released socket wakeups. |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 2296 | base::RunLoop().RunUntilIdle(); |
[email protected] | a6c59f6 | 2009-07-29 16:33:33 | [diff] [blame] | 2297 | ASSERT_TRUE(req2.handle()->socket()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2298 | EXPECT_THAT(req2.WaitForResult(), IsOk()); |
[email protected] | a6c59f6 | 2009-07-29 16:33:33 | [diff] [blame] | 2299 | EXPECT_FALSE(req3.handle()->socket()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2300 | |
| 2301 | // Signal job 2, which should service request 3. |
| 2302 | |
| 2303 | client_socket_factory_.SignalJobs(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2304 | EXPECT_THAT(req3.WaitForResult(), IsOk()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2305 | |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2306 | ASSERT_EQ(3u, request_order.size()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2307 | EXPECT_EQ(&req1, request_order[0]); |
| 2308 | EXPECT_EQ(&req2, request_order[1]); |
| 2309 | EXPECT_EQ(&req3, request_order[2]); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2310 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2311 | } |
| 2312 | |
| 2313 | // The requests are not coupled to the jobs. So, the requests should finish in |
| 2314 | // their priority / insertion order. |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2315 | TEST_F(ClientSocketPoolBaseTest, PendingJobCompletionOrder) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2316 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2317 | // First two jobs are async. |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 2318 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2319 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2320 | std::vector<TestSocketRequest*> request_order; |
| 2321 | size_t completion_count; // unused |
| 2322 | TestSocketRequest req1(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2323 | int rv = req1.handle()->Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2324 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2325 | ClientSocketPool::RespectLimits::ENABLED, req1.callback(), |
| 2326 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2327 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2328 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2329 | TestSocketRequest req2(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2330 | rv = req2.handle()->Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2331 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2332 | ClientSocketPool::RespectLimits::ENABLED, req2.callback(), |
| 2333 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2334 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2335 | |
| 2336 | // The pending job is sync. |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 2337 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2338 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2339 | TestSocketRequest req3(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2340 | rv = req3.handle()->Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2341 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2342 | ClientSocketPool::RespectLimits::ENABLED, req3.callback(), |
| 2343 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2344 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2345 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2346 | EXPECT_THAT(req1.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 2347 | EXPECT_THAT(req2.WaitForResult(), IsOk()); |
| 2348 | EXPECT_THAT(req3.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2349 | |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2350 | ASSERT_EQ(3u, request_order.size()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2351 | EXPECT_EQ(&req1, request_order[0]); |
| 2352 | EXPECT_EQ(&req2, request_order[1]); |
| 2353 | EXPECT_EQ(&req3, request_order[2]); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2354 | } |
| 2355 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2356 | // Test GetLoadState in the case there's only one socket request. |
| 2357 | TEST_F(ClientSocketPoolBaseTest, LoadStateOneRequest) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2358 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2359 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2360 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2361 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2362 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2363 | int rv = handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2364 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2365 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2366 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2367 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2368 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2369 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2370 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE); |
| 2371 | EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle.GetLoadState()); |
| 2372 | |
| 2373 | // No point in completing the connection, since ClientSocketHandles only |
| 2374 | // expect the LoadState to be checked while connecting. |
| 2375 | } |
| 2376 | |
| 2377 | // Test GetLoadState in the case there are two socket requests. |
| 2378 | TEST_F(ClientSocketPoolBaseTest, LoadStateTwoRequests) { |
| 2379 | CreatePool(2, 2); |
| 2380 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2381 | |
| 2382 | ClientSocketHandle handle; |
| 2383 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2384 | int rv = handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2385 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2386 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2387 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2388 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2389 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_RESOLVING_HOST); |
| 2390 | |
| 2391 | ClientSocketHandle handle2; |
| 2392 | TestCompletionCallback callback2; |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2393 | rv = handle2.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2394 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2395 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2396 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2397 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2398 | client_socket_factory_.SetJobLoadState(1, LOAD_STATE_RESOLVING_HOST); |
| 2399 | |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 2400 | // Each handle should reflect the state of its own job. |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2401 | EXPECT_EQ(LOAD_STATE_RESOLVING_HOST, handle.GetLoadState()); |
| 2402 | EXPECT_EQ(LOAD_STATE_RESOLVING_HOST, handle2.GetLoadState()); |
| 2403 | |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 2404 | // Update the state of the first job. |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2405 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_CONNECTING); |
| 2406 | |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 2407 | // Only the state of the first request should have changed. |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2408 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2409 | EXPECT_EQ(LOAD_STATE_RESOLVING_HOST, handle2.GetLoadState()); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 2410 | |
| 2411 | // Update the state of the second job. |
| 2412 | client_socket_factory_.SetJobLoadState(1, LOAD_STATE_SSL_HANDSHAKE); |
| 2413 | |
| 2414 | // Only the state of the second request should have changed. |
| 2415 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
| 2416 | EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle2.GetLoadState()); |
| 2417 | |
| 2418 | // Second job connects and the first request gets the socket. The |
| 2419 | // second handle switches to the state of the remaining ConnectJob. |
| 2420 | client_socket_factory_.SignalJob(1); |
| 2421 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 2422 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle2.GetLoadState()); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2423 | } |
| 2424 | |
| 2425 | // Test GetLoadState in the case the per-group limit is reached. |
| 2426 | TEST_F(ClientSocketPoolBaseTest, LoadStateGroupLimit) { |
| 2427 | CreatePool(2, 1); |
| 2428 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2429 | |
| 2430 | ClientSocketHandle handle; |
| 2431 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2432 | int rv = handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2433 | TestGroupId("a"), params_, absl::nullopt, MEDIUM, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2434 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2435 | ClientSocketPool::ProxyAuthCallback(), 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_CONNECTING, handle.GetLoadState()); |
| 2438 | |
| 2439 | // Request another socket from the same pool, buth with a higher priority. |
| 2440 | // The first request should now be stalled at the socket group limit. |
| 2441 | ClientSocketHandle handle2; |
| 2442 | TestCompletionCallback callback2; |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2443 | rv = handle2.Init(TestGroupId("a"), params_, absl::nullopt, HIGHEST, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2444 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2445 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2446 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2447 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2448 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET, handle.GetLoadState()); |
| 2449 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle2.GetLoadState()); |
| 2450 | |
| 2451 | // The first handle should remain stalled as the other socket goes through |
| 2452 | // the connect process. |
| 2453 | |
| 2454 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE); |
| 2455 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET, handle.GetLoadState()); |
| 2456 | EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle2.GetLoadState()); |
| 2457 | |
| 2458 | client_socket_factory_.SignalJob(0); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2459 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2460 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET, handle.GetLoadState()); |
| 2461 | |
| 2462 | // Closing the second socket should cause the stalled handle to finally get a |
| 2463 | // ConnectJob. |
| 2464 | handle2.socket()->Disconnect(); |
| 2465 | handle2.Reset(); |
| 2466 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
| 2467 | } |
| 2468 | |
| 2469 | // Test GetLoadState in the case the per-pool limit is reached. |
| 2470 | TEST_F(ClientSocketPoolBaseTest, LoadStatePoolLimit) { |
| 2471 | CreatePool(2, 2); |
| 2472 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2473 | |
| 2474 | ClientSocketHandle handle; |
| 2475 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2476 | int rv = handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2477 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2478 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2479 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2480 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2481 | |
| 2482 | // Request for socket from another pool. |
| 2483 | ClientSocketHandle handle2; |
| 2484 | TestCompletionCallback callback2; |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2485 | rv = handle2.Init(TestGroupId("b"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2486 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2487 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2488 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2489 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2490 | |
| 2491 | // Request another socket from the first pool. Request should stall at the |
| 2492 | // socket pool limit. |
| 2493 | ClientSocketHandle handle3; |
| 2494 | TestCompletionCallback callback3; |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2495 | rv = handle3.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2496 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2497 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2498 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2499 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2500 | |
| 2501 | // The third handle should remain stalled as the other sockets in its group |
| 2502 | // goes through the connect process. |
| 2503 | |
| 2504 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
| 2505 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL, handle3.GetLoadState()); |
| 2506 | |
| 2507 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE); |
| 2508 | EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle.GetLoadState()); |
| 2509 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL, handle3.GetLoadState()); |
| 2510 | |
| 2511 | client_socket_factory_.SignalJob(0); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2512 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2513 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL, handle3.GetLoadState()); |
| 2514 | |
| 2515 | // Closing a socket should allow the stalled handle to finally get a new |
| 2516 | // ConnectJob. |
| 2517 | handle.socket()->Disconnect(); |
| 2518 | handle.Reset(); |
| 2519 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle3.GetLoadState()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2520 | } |
| 2521 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2522 | TEST_F(ClientSocketPoolBaseTest, CertError) { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2523 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2524 | connect_job_factory_->set_job_type(TestConnectJob::kMockCertErrorJob); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2525 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2526 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2527 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2528 | EXPECT_EQ( |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2529 | ERR_CERT_COMMON_NAME_INVALID, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2530 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2531 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2532 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2533 | pool_.get(), NetLogWithSource())); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2534 | EXPECT_TRUE(handle.is_initialized()); |
| 2535 | EXPECT_TRUE(handle.socket()); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2536 | } |
| 2537 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2538 | TEST_F(ClientSocketPoolBaseTest, AsyncCertError) { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2539 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2540 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2541 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingCertErrorJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2542 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2543 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2544 | EXPECT_EQ( |
| 2545 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2546 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2547 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2548 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2549 | pool_.get(), NetLogWithSource())); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2550 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2551 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2552 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_CERT_COMMON_NAME_INVALID)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2553 | EXPECT_TRUE(handle.is_initialized()); |
| 2554 | EXPECT_TRUE(handle.socket()); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2555 | } |
| 2556 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2557 | TEST_F(ClientSocketPoolBaseTest, AdditionalErrorStateSynchronous) { |
| 2558 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2559 | connect_job_factory_->set_job_type( |
| 2560 | TestConnectJob::kMockAdditionalErrorStateJob); |
| 2561 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2562 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2563 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2564 | EXPECT_EQ( |
| 2565 | ERR_CONNECTION_FAILED, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2566 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2567 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2568 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2569 | pool_.get(), NetLogWithSource())); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2570 | EXPECT_FALSE(handle.is_initialized()); |
| 2571 | EXPECT_FALSE(handle.socket()); |
| 2572 | EXPECT_TRUE(handle.is_ssl_error()); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 2573 | EXPECT_TRUE(handle.ssl_cert_request_info()); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2574 | } |
| 2575 | |
| 2576 | TEST_F(ClientSocketPoolBaseTest, AdditionalErrorStateAsynchronous) { |
| 2577 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2578 | |
| 2579 | connect_job_factory_->set_job_type( |
| 2580 | TestConnectJob::kMockPendingAdditionalErrorStateJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2581 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2582 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2583 | EXPECT_EQ( |
| 2584 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2585 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2586 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2587 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2588 | pool_.get(), NetLogWithSource())); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2589 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2590 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2591 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2592 | EXPECT_FALSE(handle.is_initialized()); |
| 2593 | EXPECT_FALSE(handle.socket()); |
| 2594 | EXPECT_TRUE(handle.is_ssl_error()); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 2595 | EXPECT_TRUE(handle.ssl_cert_request_info()); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2596 | } |
| 2597 | |
martijn | 003cd61 | 2016-05-19 22:24:38 | [diff] [blame] | 2598 | // Make sure we can reuse sockets. |
| 2599 | TEST_F(ClientSocketPoolBaseTest, CleanupTimedOutIdleSocketsReuse) { |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2600 | CreatePoolWithIdleTimeouts( |
| 2601 | kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2602 | base::TimeDelta(), // Time out unused sockets immediately. |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 2603 | base::Days(1)); // Don't time out used sockets. |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2604 | |
| 2605 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2606 | |
| 2607 | ClientSocketHandle handle; |
| 2608 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2609 | int rv = handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2610 | TestGroupId("a"), params_, absl::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2611 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2612 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2613 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2614 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2615 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2616 | ASSERT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2617 | |
| 2618 | // Use and release the socket. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 2619 | EXPECT_EQ(1, handle.socket()->Write(nullptr, 1, CompletionOnceCallback(), |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 2620 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 2621 | TestLoadTimingInfoConnectedNotReused(handle); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2622 | handle.Reset(); |
| 2623 | |
| 2624 | // Should now have one idle socket. |
| 2625 | ASSERT_EQ(1, pool_->IdleSocketCount()); |
| 2626 | |
| 2627 | // Request a new socket. This should reuse the old socket and complete |
| 2628 | // synchronously. |
Matt Reichhoff | 0049a0b7 | 2021-10-20 20:44:26 | [diff] [blame^] | 2629 | NetLogWithSource net_log_with_source = |
| 2630 | NetLogWithSource::Make(NetLogSourceType::NONE); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2631 | rv = handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2632 | TestGroupId("a"), params_, absl::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2633 | ClientSocketPool::RespectLimits::ENABLED, CompletionOnceCallback(), |
Matt Reichhoff | 0049a0b7 | 2021-10-20 20:44:26 | [diff] [blame^] | 2634 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), net_log_with_source); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2635 | ASSERT_THAT(rv, IsOk()); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2636 | EXPECT_TRUE(handle.is_reused()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 2637 | TestLoadTimingInfoConnectedReused(handle); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2638 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2639 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2640 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2641 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2642 | |
Matt Reichhoff | 0049a0b7 | 2021-10-20 20:44:26 | [diff] [blame^] | 2643 | auto entries = |
| 2644 | net_log_observer_.GetEntriesForSource(net_log_with_source.source()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2645 | EXPECT_TRUE(LogContainsEvent( |
| 2646 | entries, 0, NetLogEventType::TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET, |
| 2647 | NetLogEventPhase::NONE)); |
| 2648 | EXPECT_TRUE(LogContainsBeginEvent(entries, 1, NetLogEventType::SOCKET_POOL)); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2649 | EXPECT_TRUE(LogContainsEntryWithType( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2650 | entries, 2, NetLogEventType::SOCKET_POOL_REUSED_AN_EXISTING_SOCKET)); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2651 | } |
| 2652 | |
martijn | 003cd61 | 2016-05-19 22:24:38 | [diff] [blame] | 2653 | // Make sure we cleanup old unused sockets. |
Eric Roman | b49715e | 2018-04-24 22:41:17 | [diff] [blame] | 2654 | TEST_F(ClientSocketPoolBaseTest, CleanupTimedOutIdleSocketsNoReuse) { |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2655 | CreatePoolWithIdleTimeouts( |
| 2656 | kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 2657 | base::TimeDelta(), // Time out unused sockets immediately |
| 2658 | base::TimeDelta()); // Time out used sockets immediately |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2659 | |
| 2660 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2661 | |
| 2662 | // Startup two mock pending connect jobs, which will sit in the MessageLoop. |
| 2663 | |
| 2664 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2665 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2666 | int rv = handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2667 | TestGroupId("a"), params_, absl::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2668 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2669 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2670 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2671 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2672 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2673 | |
| 2674 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2675 | TestCompletionCallback callback2; |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2676 | rv = handle2.Init(TestGroupId("a"), params_, absl::nullopt, LOWEST, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2677 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2678 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2679 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2680 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2681 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2682 | pool_->GetLoadState(TestGroupId("a"), &handle2)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2683 | |
| 2684 | // Cancel one of the requests. Wait for the other, which will get the first |
| 2685 | // job. Release the socket. Run the loop again to make sure the second |
| 2686 | // socket is sitting idle and the first one is released (since ReleaseSocket() |
| 2687 | // just posts a DoReleaseSocket() task). |
| 2688 | |
| 2689 | handle.Reset(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2690 | ASSERT_THAT(callback2.WaitForResult(), IsOk()); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 2691 | // Get the NetLogSource for the socket, so the time out reason can be checked |
| 2692 | // at the end of the test. |
| 2693 | NetLogSource net_log_source2 = handle2.socket()->NetLog().source(); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2694 | // Use the socket. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 2695 | EXPECT_EQ(1, handle2.socket()->Write(nullptr, 1, CompletionOnceCallback(), |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 2696 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2697 | handle2.Reset(); |
| 2698 | |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2699 | // We post all of our delayed tasks with a 2ms delay. I.e. they don't |
| 2700 | // actually become pending until 2ms after they have been created. In order |
| 2701 | // to flush all tasks, we need to wait so that we know there are no |
| 2702 | // soon-to-be-pending tasks waiting. |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 2703 | FastForwardBy(base::Milliseconds(10)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2704 | |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2705 | // Both sockets should now be idle. |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2706 | ASSERT_EQ(2, pool_->IdleSocketCount()); |
| 2707 | |
| 2708 | // Request a new socket. This should cleanup the unused and timed out ones. |
| 2709 | // A new socket will be created rather than reusing the idle one. |
Matt Reichhoff | 0049a0b7 | 2021-10-20 20:44:26 | [diff] [blame^] | 2710 | NetLogWithSource net_log_with_source = |
| 2711 | NetLogWithSource::Make(NetLogSourceType::NONE); |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2712 | TestCompletionCallback callback3; |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2713 | rv = handle.Init(TestGroupId("a"), params_, absl::nullopt, LOWEST, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2714 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2715 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
Matt Reichhoff | 0049a0b7 | 2021-10-20 20:44:26 | [diff] [blame^] | 2716 | pool_.get(), net_log_with_source); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2717 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 2718 | ASSERT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2719 | EXPECT_FALSE(handle.is_reused()); |
| 2720 | |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2721 | // Make sure the idle socket is closed. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2722 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2723 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2724 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2725 | |
Matt Reichhoff | 0049a0b7 | 2021-10-20 20:44:26 | [diff] [blame^] | 2726 | auto entries = |
| 2727 | net_log_observer_.GetEntriesForSource(net_log_with_source.source()); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2728 | EXPECT_FALSE(LogContainsEntryWithType( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 2729 | entries, 1, NetLogEventType::SOCKET_POOL_REUSED_AN_EXISTING_SOCKET)); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 2730 | ExpectSocketClosedWithReason( |
| 2731 | net_log_source2, TransportClientSocketPool::kIdleTimeLimitExpired); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2732 | } |
| 2733 | |
[email protected] | 2041cf34 | 2010-02-19 03:15:59 | [diff] [blame] | 2734 | // Make sure that we process all pending requests even when we're stalling |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2735 | // because of multiple releasing disconnected sockets. |
| 2736 | TEST_F(ClientSocketPoolBaseTest, MultipleReleasingDisconnectedSockets) { |
| 2737 | CreatePoolWithIdleTimeouts( |
| 2738 | kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 2739 | base::TimeDelta(), // Time out unused sockets immediately. |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 2740 | base::Days(1)); // Don't time out used sockets. |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2741 | |
| 2742 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 2743 | |
| 2744 | // Startup 4 connect jobs. Two of them will be pending. |
| 2745 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2746 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2747 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2748 | int rv = handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2749 | TestGroupId("a"), params_, absl::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2750 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2751 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2752 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2753 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2754 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2755 | TestCompletionCallback callback2; |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2756 | rv = handle2.Init(TestGroupId("a"), params_, absl::nullopt, LOWEST, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2757 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2758 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2759 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2760 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2761 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2762 | ClientSocketHandle handle3; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2763 | TestCompletionCallback callback3; |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2764 | rv = handle3.Init(TestGroupId("a"), params_, absl::nullopt, LOWEST, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2765 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2766 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2767 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2768 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2769 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2770 | ClientSocketHandle handle4; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2771 | TestCompletionCallback callback4; |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2772 | rv = handle4.Init(TestGroupId("a"), params_, absl::nullopt, LOWEST, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2773 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2774 | callback4.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2775 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2776 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2777 | |
| 2778 | // Release two disconnected sockets. |
| 2779 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2780 | handle.socket()->Disconnect(); |
| 2781 | handle.Reset(); |
| 2782 | handle2.socket()->Disconnect(); |
| 2783 | handle2.Reset(); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2784 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2785 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2786 | EXPECT_FALSE(handle3.is_reused()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2787 | EXPECT_THAT(callback4.WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2788 | EXPECT_FALSE(handle4.is_reused()); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2789 | } |
| 2790 | |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2791 | // Regression test for http://crbug.com/42267. |
| 2792 | // When DoReleaseSocket() is processed for one socket, it is blocked because the |
| 2793 | // other stalled groups all have releasing sockets, so no progress can be made. |
| 2794 | TEST_F(ClientSocketPoolBaseTest, SocketLimitReleasingSockets) { |
| 2795 | CreatePoolWithIdleTimeouts( |
| 2796 | 4 /* socket limit */, 4 /* socket limit per group */, |
| 2797 | base::TimeDelta(), // Time out unused sockets immediately. |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 2798 | base::Days(1)); // Don't time out used sockets. |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2799 | |
| 2800 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 2801 | |
| 2802 | // Max out the socket limit with 2 per group. |
| 2803 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2804 | ClientSocketHandle handle_a[4]; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2805 | TestCompletionCallback callback_a[4]; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2806 | ClientSocketHandle handle_b[4]; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2807 | TestCompletionCallback callback_b[4]; |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2808 | |
| 2809 | for (int i = 0; i < 2; ++i) { |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2810 | EXPECT_EQ(OK, handle_a[i].Init(TestGroupId("a"), params_, absl::nullopt, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2811 | LOWEST, SocketTag(), |
| 2812 | ClientSocketPool::RespectLimits::ENABLED, |
| 2813 | callback_a[i].callback(), |
| 2814 | ClientSocketPool::ProxyAuthCallback(), |
| 2815 | pool_.get(), NetLogWithSource())); |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2816 | EXPECT_EQ(OK, handle_b[i].Init(TestGroupId("b"), params_, absl::nullopt, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2817 | LOWEST, SocketTag(), |
| 2818 | ClientSocketPool::RespectLimits::ENABLED, |
| 2819 | callback_b[i].callback(), |
| 2820 | ClientSocketPool::ProxyAuthCallback(), |
| 2821 | pool_.get(), NetLogWithSource())); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2822 | } |
[email protected] | b89f7e4 | 2010-05-20 20:37:00 | [diff] [blame] | 2823 | |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2824 | // Make 4 pending requests, 2 per group. |
| 2825 | |
| 2826 | for (int i = 2; i < 4; ++i) { |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2827 | EXPECT_EQ( |
| 2828 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2829 | handle_a[i].Init(TestGroupId("a"), params_, absl::nullopt, LOWEST, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2830 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2831 | callback_a[i].callback(), |
| 2832 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2833 | NetLogWithSource())); |
| 2834 | EXPECT_EQ( |
| 2835 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2836 | handle_b[i].Init(TestGroupId("b"), params_, absl::nullopt, LOWEST, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2837 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2838 | callback_b[i].callback(), |
| 2839 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2840 | NetLogWithSource())); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2841 | } |
| 2842 | |
| 2843 | // Release b's socket first. The order is important, because in |
| 2844 | // DoReleaseSocket(), we'll process b's released socket, and since both b and |
| 2845 | // a are stalled, but 'a' is lower lexicographically, we'll process group 'a' |
| 2846 | // first, which has a releasing socket, so it refuses to start up another |
| 2847 | // ConnectJob. So, we used to infinite loop on this. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2848 | handle_b[0].socket()->Disconnect(); |
| 2849 | handle_b[0].Reset(); |
| 2850 | handle_a[0].socket()->Disconnect(); |
| 2851 | handle_a[0].Reset(); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2852 | |
| 2853 | // Used to get stuck here. |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 2854 | base::RunLoop().RunUntilIdle(); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2855 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2856 | handle_b[1].socket()->Disconnect(); |
| 2857 | handle_b[1].Reset(); |
| 2858 | handle_a[1].socket()->Disconnect(); |
| 2859 | handle_a[1].Reset(); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2860 | |
| 2861 | for (int i = 2; i < 4; ++i) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2862 | EXPECT_THAT(callback_b[i].WaitForResult(), IsOk()); |
| 2863 | EXPECT_THAT(callback_a[i].WaitForResult(), IsOk()); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2864 | } |
| 2865 | } |
| 2866 | |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2867 | TEST_F(ClientSocketPoolBaseTest, |
| 2868 | ReleasingDisconnectedSocketsMaintainsPriorityOrder) { |
| 2869 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2870 | |
| 2871 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2872 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2873 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2874 | IsError(ERR_IO_PENDING)); |
| 2875 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2876 | IsError(ERR_IO_PENDING)); |
| 2877 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2878 | IsError(ERR_IO_PENDING)); |
| 2879 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2880 | IsError(ERR_IO_PENDING)); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2881 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2882 | EXPECT_THAT((*requests())[0]->WaitForResult(), IsOk()); |
| 2883 | EXPECT_THAT((*requests())[1]->WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2884 | EXPECT_EQ(2u, completion_count()); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2885 | |
| 2886 | // Releases one connection. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2887 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::NO_KEEP_ALIVE)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2888 | EXPECT_THAT((*requests())[2]->WaitForResult(), IsOk()); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2889 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2890 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::NO_KEEP_ALIVE)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2891 | EXPECT_THAT((*requests())[3]->WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2892 | EXPECT_EQ(4u, completion_count()); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2893 | |
| 2894 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 2895 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 2896 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 2897 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 2898 | |
| 2899 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2900 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(5)); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2901 | } |
| 2902 | |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2903 | class TestReleasingSocketRequest : public TestCompletionCallbackBase { |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2904 | public: |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2905 | TestReleasingSocketRequest(TransportClientSocketPool* pool, |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2906 | int expected_result, |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2907 | bool reset_releasing_handle) |
| 2908 | : pool_(pool), |
| 2909 | expected_result_(expected_result), |
Bence Béky | 8ddc249 | 2018-06-13 01:02:04 | [diff] [blame] | 2910 | reset_releasing_handle_(reset_releasing_handle) {} |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2911 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 2912 | ~TestReleasingSocketRequest() override = default; |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2913 | |
| 2914 | ClientSocketHandle* handle() { return &handle_; } |
| 2915 | |
Bence Béky | 8ddc249 | 2018-06-13 01:02:04 | [diff] [blame] | 2916 | CompletionOnceCallback callback() { |
| 2917 | return base::BindOnce(&TestReleasingSocketRequest::OnComplete, |
| 2918 | base::Unretained(this)); |
| 2919 | } |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2920 | |
| 2921 | private: |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2922 | void OnComplete(int result) { |
| 2923 | SetResult(result); |
| 2924 | if (reset_releasing_handle_) |
| 2925 | handle_.Reset(); |
| 2926 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2927 | EXPECT_EQ( |
| 2928 | expected_result_, |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 2929 | handle2_.Init( |
| 2930 | TestGroupId("a"), |
| 2931 | ClientSocketPool::SocketParams::CreateForHttpForTesting(), |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2932 | absl::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 2933 | ClientSocketPool::RespectLimits::ENABLED, CompletionOnceCallback(), |
| 2934 | ClientSocketPool::ProxyAuthCallback(), pool_, NetLogWithSource())); |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2935 | } |
| 2936 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2937 | TransportClientSocketPool* const pool_; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2938 | int expected_result_; |
| 2939 | bool reset_releasing_handle_; |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2940 | ClientSocketHandle handle_; |
| 2941 | ClientSocketHandle handle2_; |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2942 | }; |
| 2943 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2944 | |
| 2945 | TEST_F(ClientSocketPoolBaseTest, AdditionalErrorSocketsDontUseSlot) { |
| 2946 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2947 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2948 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
| 2949 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 2950 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2951 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2952 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2953 | client_socket_factory_.allocation_count()); |
| 2954 | |
| 2955 | connect_job_factory_->set_job_type( |
| 2956 | TestConnectJob::kMockPendingAdditionalErrorStateJob); |
| 2957 | TestReleasingSocketRequest req(pool_.get(), OK, false); |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2958 | EXPECT_EQ(ERR_IO_PENDING, |
| 2959 | req.handle()->Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2960 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2961 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2962 | req.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2963 | pool_.get(), NetLogWithSource())); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2964 | // The next job should complete synchronously |
| 2965 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 2966 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2967 | EXPECT_THAT(req.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2968 | EXPECT_FALSE(req.handle()->is_initialized()); |
| 2969 | EXPECT_FALSE(req.handle()->socket()); |
| 2970 | EXPECT_TRUE(req.handle()->is_ssl_error()); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 2971 | EXPECT_TRUE(req.handle()->ssl_cert_request_info()); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2972 | } |
| 2973 | |
[email protected] | b6501d3d | 2010-06-03 23:53:34 | [diff] [blame] | 2974 | // http://crbug.com/44724 regression test. |
| 2975 | // We start releasing the pool when we flush on network change. When that |
| 2976 | // happens, the only active references are in the ClientSocketHandles. When a |
| 2977 | // ConnectJob completes and calls back into the last ClientSocketHandle, that |
| 2978 | // callback can release the last reference and delete the pool. After the |
| 2979 | // callback finishes, we go back to the stack frame within the now-deleted pool. |
| 2980 | // Executing any code that refers to members of the now-deleted pool can cause |
| 2981 | // crashes. |
| 2982 | TEST_F(ClientSocketPoolBaseTest, CallbackThatReleasesPool) { |
| 2983 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2984 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
| 2985 | |
| 2986 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2987 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2988 | EXPECT_EQ( |
| 2989 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2990 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2991 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2992 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2993 | pool_.get(), NetLogWithSource())); |
[email protected] | b6501d3d | 2010-06-03 23:53:34 | [diff] [blame] | 2994 | |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 2995 | pool_->FlushWithError(ERR_NETWORK_CHANGED, "Network changed"); |
[email protected] | b6501d3d | 2010-06-03 23:53:34 | [diff] [blame] | 2996 | |
| 2997 | // We'll call back into this now. |
| 2998 | callback.WaitForResult(); |
| 2999 | } |
| 3000 | |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 3001 | TEST_F(ClientSocketPoolBaseTest, DoNotReuseSocketAfterFlush) { |
| 3002 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3003 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3004 | |
| 3005 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3006 | TestCompletionCallback callback; |
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 | NetLogSource source = handle.socket()->NetLog().source(); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 3016 | |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 3017 | pool_->FlushWithError(ERR_NETWORK_CHANGED, "Network changed"); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 3018 | |
| 3019 | handle.Reset(); |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3020 | base::RunLoop().RunUntilIdle(); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 3021 | |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3022 | EXPECT_EQ( |
| 3023 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3024 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3025 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 3026 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3027 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3028 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 3029 | EXPECT_EQ(ClientSocketHandle::UNUSED, handle.reuse_type()); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 3030 | |
| 3031 | ExpectSocketClosedWithReason( |
| 3032 | source, TransportClientSocketPool::kSocketGenerationOutOfDate); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 3033 | } |
| 3034 | |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3035 | class ConnectWithinCallback : public TestCompletionCallbackBase { |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 3036 | public: |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3037 | ConnectWithinCallback( |
| 3038 | const ClientSocketPool::GroupId& group_id, |
Matt Menke | 84d11e56 | 2019-03-27 00:11:19 | [diff] [blame] | 3039 | const scoped_refptr<ClientSocketPool::SocketParams>& params, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3040 | TransportClientSocketPool* pool) |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3041 | : group_id_(group_id), params_(params), pool_(pool) {} |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 3042 | |
Peter Boström | 293b134 | 2021-09-22 17:31:43 | [diff] [blame] | 3043 | ConnectWithinCallback(const ConnectWithinCallback&) = delete; |
| 3044 | ConnectWithinCallback& operator=(const ConnectWithinCallback&) = delete; |
| 3045 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 3046 | ~ConnectWithinCallback() override = default; |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 3047 | |
| 3048 | int WaitForNestedResult() { |
| 3049 | return nested_callback_.WaitForResult(); |
| 3050 | } |
| 3051 | |
Bence Béky | 8ddc249 | 2018-06-13 01:02:04 | [diff] [blame] | 3052 | CompletionOnceCallback callback() { |
| 3053 | return base::BindOnce(&ConnectWithinCallback::OnComplete, |
| 3054 | base::Unretained(this)); |
| 3055 | } |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3056 | |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 3057 | private: |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3058 | void OnComplete(int result) { |
| 3059 | SetResult(result); |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3060 | EXPECT_EQ( |
| 3061 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3062 | handle_.Init(group_id_, params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3063 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 3064 | nested_callback_.callback(), |
| 3065 | ClientSocketPool::ProxyAuthCallback(), pool_, |
| 3066 | NetLogWithSource())); |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3067 | } |
| 3068 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3069 | const ClientSocketPool::GroupId group_id_; |
Matt Menke | 84d11e56 | 2019-03-27 00:11:19 | [diff] [blame] | 3070 | const scoped_refptr<ClientSocketPool::SocketParams> params_; |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3071 | TransportClientSocketPool* const pool_; |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 3072 | ClientSocketHandle handle_; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3073 | TestCompletionCallback nested_callback_; |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 3074 | }; |
| 3075 | |
| 3076 | TEST_F(ClientSocketPoolBaseTest, AbortAllRequestsOnFlush) { |
| 3077 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3078 | |
| 3079 | // First job will be waiting until it gets aborted. |
| 3080 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3081 | |
| 3082 | ClientSocketHandle handle; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3083 | ConnectWithinCallback callback(TestGroupId("a"), params_, pool_.get()); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3084 | EXPECT_EQ( |
| 3085 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3086 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3087 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 3088 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3089 | pool_.get(), NetLogWithSource())); |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 3090 | |
| 3091 | // Second job will be started during the first callback, and will |
| 3092 | // asynchronously complete with OK. |
| 3093 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 3094 | pool_->FlushWithError(ERR_NETWORK_CHANGED, "Network changed"); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3095 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_NETWORK_CHANGED)); |
| 3096 | EXPECT_THAT(callback.WaitForNestedResult(), IsOk()); |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 3097 | } |
| 3098 | |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 3099 | TEST_F(ClientSocketPoolBaseTest, BackupSocketWaitsForHostResolution) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3100 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets, |
| 3101 | true /* enable_backup_connect_jobs */); |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 3102 | |
| 3103 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3104 | ClientSocketHandle handle; |
| 3105 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3106 | EXPECT_EQ( |
| 3107 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3108 | handle.Init(TestGroupId("bar"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3109 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 3110 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3111 | pool_.get(), NetLogWithSource())); |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 3112 | // The backup timer fires but doesn't start a new ConnectJob while resolving |
| 3113 | // the hostname. |
| 3114 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_RESOLVING_HOST); |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 3115 | FastForwardBy( |
| 3116 | base::Milliseconds(ClientSocketPool::kMaxConnectRetryIntervalMs * 100)); |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 3117 | EXPECT_EQ(1, client_socket_factory_.allocation_count()); |
| 3118 | |
| 3119 | // Once the ConnectJob has finished resolving the hostname, the backup timer |
| 3120 | // will create a ConnectJob when it fires. |
| 3121 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_CONNECTING); |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 3122 | FastForwardBy( |
| 3123 | base::Milliseconds(ClientSocketPool::kMaxConnectRetryIntervalMs)); |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 3124 | EXPECT_EQ(2, client_socket_factory_.allocation_count()); |
| 3125 | } |
| 3126 | |
| 3127 | // Test that no backup socket is created when a ConnectJob connects before it |
| 3128 | // completes. |
| 3129 | TEST_F(ClientSocketPoolBaseTest, NoBackupSocketWhenConnected) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3130 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets, |
| 3131 | true /* enable_backup_connect_jobs */); |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 3132 | |
| 3133 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3134 | ClientSocketHandle handle; |
| 3135 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3136 | EXPECT_EQ( |
| 3137 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3138 | handle.Init(TestGroupId("bar"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3139 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 3140 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3141 | pool_.get(), NetLogWithSource())); |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 3142 | // The backup timer fires but doesn't start a new ConnectJob while resolving |
| 3143 | // the hostname. |
| 3144 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_RESOLVING_HOST); |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 3145 | FastForwardBy( |
| 3146 | base::Milliseconds(ClientSocketPool::kMaxConnectRetryIntervalMs * 100)); |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 3147 | EXPECT_EQ(1, client_socket_factory_.allocation_count()); |
| 3148 | |
| 3149 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE); |
| 3150 | client_socket_factory_.SetJobHasEstablishedConnection(0); |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 3151 | FastForwardBy( |
| 3152 | base::Milliseconds(ClientSocketPool::kMaxConnectRetryIntervalMs * 100)); |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 3153 | EXPECT_EQ(1, client_socket_factory_.allocation_count()); |
| 3154 | } |
| 3155 | |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3156 | // Cancel a pending socket request while we're at max sockets, |
| 3157 | // and verify that the backup socket firing doesn't cause a crash. |
| 3158 | TEST_F(ClientSocketPoolBaseTest, BackupSocketCancelAtMaxSockets) { |
| 3159 | // Max 4 sockets globally, max 4 sockets per group. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3160 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets, |
| 3161 | true /* enable_backup_connect_jobs */); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3162 | |
[email protected] | 4baaf9d | 2010-08-31 15:15:44 | [diff] [blame] | 3163 | // Create the first socket and set to ERR_IO_PENDING. This starts the backup |
| 3164 | // timer. |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3165 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3166 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3167 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3168 | EXPECT_EQ( |
| 3169 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3170 | handle.Init(TestGroupId("bar"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3171 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 3172 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3173 | pool_.get(), NetLogWithSource())); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3174 | |
| 3175 | // Start (MaxSockets - 1) connected sockets to reach max sockets. |
| 3176 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 3177 | ClientSocketHandle handles[kDefaultMaxSockets]; |
| 3178 | for (int i = 1; i < kDefaultMaxSockets; ++i) { |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3179 | EXPECT_EQ(OK, handles[i].Init(TestGroupId("bar"), params_, absl::nullopt, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3180 | DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3181 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3182 | callback.callback(), |
| 3183 | ClientSocketPool::ProxyAuthCallback(), |
| 3184 | pool_.get(), NetLogWithSource())); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3185 | } |
| 3186 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3187 | base::RunLoop().RunUntilIdle(); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3188 | |
| 3189 | // Cancel the pending request. |
| 3190 | handle.Reset(); |
| 3191 | |
| 3192 | // 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] | 3193 | FastForwardBy( |
| 3194 | base::Milliseconds(ClientSocketPool::kMaxConnectRetryIntervalMs / 2 * 3)); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3195 | |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3196 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 3197 | } |
| 3198 | |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3199 | TEST_F(ClientSocketPoolBaseTest, CancelBackupSocketAfterCancelingAllRequests) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3200 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets, |
| 3201 | true /* enable_backup_connect_jobs */); |
[email protected] | 4baaf9d | 2010-08-31 15:15:44 | [diff] [blame] | 3202 | |
| 3203 | // Create the first socket and set to ERR_IO_PENDING. This starts the backup |
| 3204 | // timer. |
| 3205 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3206 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3207 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3208 | EXPECT_EQ( |
| 3209 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3210 | handle.Init(TestGroupId("bar"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3211 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 3212 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3213 | pool_.get(), NetLogWithSource())); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3214 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("bar"))); |
| 3215 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("bar"))); |
| 3216 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3217 | TestGroupId("bar"))); |
| 3218 | EXPECT_EQ( |
| 3219 | 0u, pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("bar"))); |
[email protected] | 4baaf9d | 2010-08-31 15:15:44 | [diff] [blame] | 3220 | |
| 3221 | // Cancel the socket request. This should cancel the backup timer. Wait for |
| 3222 | // the backup time to see if it indeed got canceled. |
| 3223 | handle.Reset(); |
| 3224 | // 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] | 3225 | FastForwardBy( |
| 3226 | base::Milliseconds(ClientSocketPool::kMaxConnectRetryIntervalMs / 2 * 3)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3227 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("bar"))); |
| 3228 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("bar"))); |
[email protected] | 4baaf9d | 2010-08-31 15:15:44 | [diff] [blame] | 3229 | } |
| 3230 | |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3231 | TEST_F(ClientSocketPoolBaseTest, CancelBackupSocketAfterFinishingAllRequests) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3232 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets, |
| 3233 | true /* enable_backup_connect_jobs */); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3234 | |
| 3235 | // Create the first socket and set to ERR_IO_PENDING. This starts the backup |
| 3236 | // timer. |
| 3237 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3238 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3239 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3240 | EXPECT_EQ( |
| 3241 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3242 | handle.Init(TestGroupId("bar"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3243 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 3244 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3245 | pool_.get(), NetLogWithSource())); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3246 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3247 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3248 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3249 | EXPECT_EQ( |
| 3250 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3251 | handle2.Init(TestGroupId("bar"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3252 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3253 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3254 | pool_.get(), NetLogWithSource())); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3255 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("bar"))); |
| 3256 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("bar"))); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3257 | |
| 3258 | // Cancel request 1 and then complete request 2. With the requests finished, |
| 3259 | // the backup timer should be cancelled. |
| 3260 | handle.Reset(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3261 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3262 | // 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] | 3263 | FastForwardBy( |
| 3264 | base::Milliseconds(ClientSocketPool::kMaxConnectRetryIntervalMs / 2 * 3)); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3265 | } |
| 3266 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3267 | // Test delayed socket binding for the case where we have two connects, |
| 3268 | // and while one is waiting on a connect, the other frees up. |
| 3269 | // The socket waiting on a connect should switch immediately to the freed |
| 3270 | // up socket. |
| 3271 | TEST_F(ClientSocketPoolBaseTest, DelayedSocketBindingWaitingForConnect) { |
| 3272 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3273 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3274 | |
| 3275 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3276 | TestCompletionCallback callback; |
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 | handle1.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())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3283 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3284 | |
| 3285 | // No idle sockets, no pending jobs. |
| 3286 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3287 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3288 | |
| 3289 | // Create a second socket to the same host, but this one will wait. |
| 3290 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3291 | ClientSocketHandle handle2; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3292 | EXPECT_EQ( |
| 3293 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3294 | handle2.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3295 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3296 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3297 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3298 | // No idle sockets, and one connecting job. |
| 3299 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3300 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3301 | |
| 3302 | // Return the first handle to the pool. This will initiate the delayed |
| 3303 | // binding. |
| 3304 | handle1.Reset(); |
| 3305 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3306 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3307 | |
| 3308 | // Still no idle sockets, still one pending connect job. |
| 3309 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3310 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3311 | |
| 3312 | // The second socket connected, even though it was a Waiting Job. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3313 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3314 | |
| 3315 | // And we can see there is still one job waiting. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3316 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3317 | |
| 3318 | // Finally, signal the waiting Connect. |
| 3319 | client_socket_factory_.SignalJobs(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3320 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3321 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3322 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3323 | } |
| 3324 | |
| 3325 | // Test delayed socket binding when a group is at capacity and one |
| 3326 | // of the group's sockets frees up. |
| 3327 | TEST_F(ClientSocketPoolBaseTest, DelayedSocketBindingAtGroupCapacity) { |
| 3328 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3329 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3330 | |
| 3331 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3332 | TestCompletionCallback callback; |
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 | handle1.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())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3339 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3340 | |
| 3341 | // No idle sockets, no pending jobs. |
| 3342 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3343 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3344 | |
| 3345 | // Create a second socket to the same host, but this one will wait. |
| 3346 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3347 | ClientSocketHandle handle2; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3348 | EXPECT_EQ( |
| 3349 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3350 | handle2.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3351 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3352 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3353 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3354 | // No idle sockets, and one connecting job. |
| 3355 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3356 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3357 | |
| 3358 | // Return the first handle to the pool. This will initiate the delayed |
| 3359 | // binding. |
| 3360 | handle1.Reset(); |
| 3361 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3362 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3363 | |
| 3364 | // Still no idle sockets, still one pending connect job. |
| 3365 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3366 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3367 | |
| 3368 | // The second socket connected, even though it was a Waiting Job. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3369 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3370 | |
| 3371 | // And we can see there is still one job waiting. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3372 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3373 | |
| 3374 | // Finally, signal the waiting Connect. |
| 3375 | client_socket_factory_.SignalJobs(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3376 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3377 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3378 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3379 | } |
| 3380 | |
| 3381 | // Test out the case where we have one socket connected, one |
| 3382 | // connecting, when the first socket finishes and goes idle. |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3383 | // Although the second connection is pending, the second request |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3384 | // should complete, by taking the first socket's idle socket. |
| 3385 | TEST_F(ClientSocketPoolBaseTest, DelayedSocketBindingAtStall) { |
| 3386 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3387 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3388 | |
| 3389 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3390 | TestCompletionCallback callback; |
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 | handle1.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())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3397 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3398 | |
| 3399 | // No idle sockets, no pending jobs. |
| 3400 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3401 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3402 | |
| 3403 | // Create a second socket to the same host, but this one will wait. |
| 3404 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3405 | ClientSocketHandle handle2; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3406 | EXPECT_EQ( |
| 3407 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3408 | handle2.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3409 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3410 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3411 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3412 | // No idle sockets, and one connecting job. |
| 3413 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3414 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3415 | |
| 3416 | // Return the first handle to the pool. This will initiate the delayed |
| 3417 | // binding. |
| 3418 | handle1.Reset(); |
| 3419 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3420 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3421 | |
| 3422 | // Still no idle sockets, still one pending connect job. |
| 3423 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3424 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3425 | |
| 3426 | // The second socket connected, even though it was a Waiting Job. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3427 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3428 | |
| 3429 | // And we can see there is still one job waiting. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3430 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3431 | |
| 3432 | // Finally, signal the waiting Connect. |
| 3433 | client_socket_factory_.SignalJobs(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3434 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3435 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3436 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3437 | } |
| 3438 | |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3439 | // Cover the case where on an available socket slot, we have one pending |
| 3440 | // request that completes synchronously, thereby making the Group empty. |
| 3441 | TEST_F(ClientSocketPoolBaseTest, SynchronouslyProcessOnePendingRequest) { |
| 3442 | const int kUnlimitedSockets = 100; |
| 3443 | const int kOneSocketPerGroup = 1; |
| 3444 | CreatePool(kUnlimitedSockets, kOneSocketPerGroup); |
| 3445 | |
| 3446 | // Make the first request asynchronous fail. |
| 3447 | // This will free up a socket slot later. |
| 3448 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
| 3449 | |
| 3450 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3451 | TestCompletionCallback callback1; |
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 | handle1.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 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3457 | pool_.get(), NetLogWithSource())); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3458 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3459 | |
| 3460 | // Make the second request synchronously fail. This should make the Group |
| 3461 | // empty. |
| 3462 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
| 3463 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3464 | TestCompletionCallback callback2; |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3465 | // It'll be ERR_IO_PENDING now, but the TestConnectJob will synchronously fail |
| 3466 | // when created. |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3467 | EXPECT_EQ( |
| 3468 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3469 | handle2.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3470 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3471 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3472 | pool_.get(), NetLogWithSource())); |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3473 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3474 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3475 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3476 | EXPECT_THAT(callback1.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 3477 | EXPECT_THAT(callback2.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3478 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3479 | } |
| 3480 | |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3481 | TEST_F(ClientSocketPoolBaseTest, PreferUsedSocketToUnusedSocket) { |
| 3482 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 3483 | |
| 3484 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3485 | |
| 3486 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3487 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3488 | EXPECT_EQ( |
| 3489 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3490 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3491 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3492 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3493 | pool_.get(), NetLogWithSource())); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3494 | |
| 3495 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3496 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3497 | EXPECT_EQ( |
| 3498 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3499 | handle2.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3500 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3501 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3502 | pool_.get(), NetLogWithSource())); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3503 | ClientSocketHandle handle3; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3504 | TestCompletionCallback callback3; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3505 | EXPECT_EQ( |
| 3506 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3507 | handle3.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3508 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3509 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3510 | pool_.get(), NetLogWithSource())); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3511 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3512 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3513 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
| 3514 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3515 | |
| 3516 | // Use the socket. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 3517 | EXPECT_EQ(1, handle1.socket()->Write(nullptr, 1, CompletionOnceCallback(), |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 3518 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 3519 | EXPECT_EQ(1, handle3.socket()->Write(nullptr, 1, CompletionOnceCallback(), |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 3520 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3521 | |
| 3522 | handle1.Reset(); |
| 3523 | handle2.Reset(); |
| 3524 | handle3.Reset(); |
| 3525 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3526 | EXPECT_EQ(OK, handle1.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3527 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3528 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3529 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3530 | pool_.get(), NetLogWithSource())); |
| 3531 | EXPECT_EQ(OK, handle2.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3532 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3533 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3534 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3535 | pool_.get(), NetLogWithSource())); |
| 3536 | EXPECT_EQ(OK, handle3.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3537 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3538 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3539 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3540 | pool_.get(), NetLogWithSource())); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3541 | |
| 3542 | EXPECT_TRUE(handle1.socket()->WasEverUsed()); |
| 3543 | EXPECT_TRUE(handle2.socket()->WasEverUsed()); |
| 3544 | EXPECT_FALSE(handle3.socket()->WasEverUsed()); |
| 3545 | } |
| 3546 | |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3547 | TEST_F(ClientSocketPoolBaseTest, RequestSockets) { |
| 3548 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3549 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3550 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3551 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 2, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3552 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3553 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3554 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3555 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3556 | EXPECT_EQ(2u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3557 | TestGroupId("a"))); |
| 3558 | EXPECT_EQ(2u, |
| 3559 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3560 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3561 | |
| 3562 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3563 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3564 | EXPECT_EQ( |
| 3565 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3566 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3567 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3568 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3569 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3570 | |
| 3571 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3572 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3573 | EXPECT_EQ( |
| 3574 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3575 | handle2.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3576 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3577 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3578 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3579 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3580 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3581 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3582 | TestGroupId("a"))); |
| 3583 | EXPECT_EQ(0u, |
| 3584 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3585 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3586 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3587 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3588 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3589 | handle1.Reset(); |
| 3590 | handle2.Reset(); |
| 3591 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3592 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3593 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3594 | TestGroupId("a"))); |
| 3595 | EXPECT_EQ(0u, |
| 3596 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3597 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3598 | } |
| 3599 | |
| 3600 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsWhenAlreadyHaveAConnectJob) { |
| 3601 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3602 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3603 | |
| 3604 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3605 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3606 | EXPECT_EQ( |
| 3607 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3608 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3609 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3610 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3611 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3612 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3613 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3614 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3615 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3616 | TestGroupId("a"))); |
| 3617 | EXPECT_EQ(0u, |
| 3618 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3619 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3620 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3621 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 2, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3622 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3623 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3624 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3625 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3626 | TestGroupId("a"))); |
| 3627 | EXPECT_EQ(1u, |
| 3628 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3629 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3630 | |
| 3631 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3632 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3633 | EXPECT_EQ( |
| 3634 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3635 | handle2.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3636 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3637 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3638 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3639 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3640 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3641 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3642 | TestGroupId("a"))); |
| 3643 | EXPECT_EQ(0u, |
| 3644 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3645 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3646 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3647 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3648 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3649 | handle1.Reset(); |
| 3650 | handle2.Reset(); |
| 3651 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3652 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3653 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3654 | TestGroupId("a"))); |
| 3655 | EXPECT_EQ(0u, |
| 3656 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3657 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3658 | } |
| 3659 | |
| 3660 | TEST_F(ClientSocketPoolBaseTest, |
| 3661 | RequestSocketsWhenAlreadyHaveMultipleConnectJob) { |
| 3662 | CreatePool(4, 4); |
| 3663 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3664 | |
| 3665 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3666 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3667 | EXPECT_EQ( |
| 3668 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3669 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3670 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3671 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3672 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3673 | |
| 3674 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3675 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3676 | EXPECT_EQ( |
| 3677 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3678 | handle2.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3679 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3680 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3681 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3682 | |
| 3683 | ClientSocketHandle handle3; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3684 | TestCompletionCallback callback3; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3685 | EXPECT_EQ( |
| 3686 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3687 | handle3.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3688 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3689 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3690 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3691 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3692 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3693 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3694 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3695 | TestGroupId("a"))); |
| 3696 | EXPECT_EQ(0u, |
| 3697 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3698 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3699 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3700 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 2, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3701 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3702 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3703 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3704 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3705 | TestGroupId("a"))); |
| 3706 | EXPECT_EQ(0u, |
| 3707 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3708 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3709 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3710 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3711 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
| 3712 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3713 | handle1.Reset(); |
| 3714 | handle2.Reset(); |
| 3715 | handle3.Reset(); |
| 3716 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3717 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3718 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3719 | TestGroupId("a"))); |
| 3720 | EXPECT_EQ(0u, |
| 3721 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3722 | EXPECT_EQ(3u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3723 | } |
| 3724 | |
| 3725 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsAtMaxSocketLimit) { |
| 3726 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 3727 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3728 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3729 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3730 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3731 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3732 | kDefaultMaxSockets, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3733 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3734 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3735 | EXPECT_EQ(kDefaultMaxSockets, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3736 | static_cast<int>( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3737 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
| 3738 | EXPECT_EQ( |
| 3739 | kDefaultMaxSockets, |
| 3740 | static_cast<int>(pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3741 | TestGroupId("a")))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3742 | EXPECT_EQ(kDefaultMaxSockets, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3743 | static_cast<int>(pool_->NumUnassignedConnectJobsInGroupForTesting( |
| 3744 | TestGroupId("a")))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3745 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3746 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("b"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3747 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3748 | pool_->RequestSockets(TestGroupId("b"), params_, absl::nullopt, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3749 | kDefaultMaxSockets, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3750 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3751 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("b"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3752 | } |
| 3753 | |
| 3754 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsHitMaxSocketLimit) { |
| 3755 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 3756 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3757 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3758 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3759 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3760 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3761 | kDefaultMaxSockets - 1, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3762 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3763 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3764 | EXPECT_EQ(kDefaultMaxSockets - 1, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3765 | static_cast<int>( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3766 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
| 3767 | EXPECT_EQ( |
| 3768 | kDefaultMaxSockets - 1, |
| 3769 | static_cast<int>(pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3770 | TestGroupId("a")))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3771 | EXPECT_EQ(kDefaultMaxSockets - 1, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3772 | static_cast<int>(pool_->NumUnassignedConnectJobsInGroupForTesting( |
| 3773 | TestGroupId("a")))); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 3774 | EXPECT_FALSE(pool_->IsStalled()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3775 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3776 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("b"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3777 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3778 | pool_->RequestSockets(TestGroupId("b"), params_, absl::nullopt, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3779 | kDefaultMaxSockets, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3780 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3781 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("b"))); |
| 3782 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 3783 | EXPECT_FALSE(pool_->IsStalled()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3784 | } |
| 3785 | |
| 3786 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsCountIdleSockets) { |
| 3787 | CreatePool(4, 4); |
| 3788 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3789 | |
| 3790 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3791 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3792 | EXPECT_EQ( |
| 3793 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3794 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3795 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3796 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3797 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3798 | ASSERT_THAT(callback1.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3799 | handle1.Reset(); |
| 3800 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3801 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3802 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3803 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3804 | TestGroupId("a"))); |
| 3805 | EXPECT_EQ(0u, |
| 3806 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3807 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3808 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3809 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 2, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3810 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3811 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3812 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3813 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3814 | TestGroupId("a"))); |
| 3815 | EXPECT_EQ(1u, |
| 3816 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3817 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3818 | } |
| 3819 | |
| 3820 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsCountActiveSockets) { |
| 3821 | CreatePool(4, 4); |
| 3822 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3823 | |
| 3824 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3825 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3826 | EXPECT_EQ( |
| 3827 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3828 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3829 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3830 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3831 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3832 | ASSERT_THAT(callback1.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3833 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3834 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3835 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3836 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3837 | TestGroupId("a"))); |
| 3838 | EXPECT_EQ(0u, |
| 3839 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3840 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3841 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3842 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3843 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 2, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3844 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3845 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3846 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3847 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3848 | TestGroupId("a"))); |
| 3849 | EXPECT_EQ(1u, |
| 3850 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3851 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3852 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3853 | } |
| 3854 | |
| 3855 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsSynchronous) { |
| 3856 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3857 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 3858 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3859 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3860 | kDefaultMaxSocketsPerGroup, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3861 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3862 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3863 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3864 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3865 | TestGroupId("a"))); |
| 3866 | EXPECT_EQ(0u, |
| 3867 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3868 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3869 | static_cast<int>(pool_->IdleSocketCountInGroup(TestGroupId("a")))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3870 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3871 | pool_->RequestSockets(TestGroupId("b"), params_, absl::nullopt, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3872 | kDefaultMaxSocketsPerGroup, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3873 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3874 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 3875 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3876 | TestGroupId("b"))); |
| 3877 | EXPECT_EQ(0u, |
| 3878 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3879 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3880 | static_cast<int>(pool_->IdleSocketCountInGroup(TestGroupId("b")))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3881 | } |
| 3882 | |
[email protected] | 3c819f52 | 2010-12-02 02:03:12 | [diff] [blame] | 3883 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsSynchronousError) { |
| 3884 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3885 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
| 3886 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3887 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3888 | kDefaultMaxSocketsPerGroup, NetLogWithSource()); |
[email protected] | 3c819f52 | 2010-12-02 02:03:12 | [diff] [blame] | 3889 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3890 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | fd2e53e | 2011-01-14 20:40:52 | [diff] [blame] | 3891 | |
| 3892 | connect_job_factory_->set_job_type( |
| 3893 | TestConnectJob::kMockAdditionalErrorStateJob); |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3894 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3895 | kDefaultMaxSocketsPerGroup, NetLogWithSource()); |
[email protected] | fd2e53e | 2011-01-14 20:40:52 | [diff] [blame] | 3896 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3897 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | 3c819f52 | 2010-12-02 02:03:12 | [diff] [blame] | 3898 | } |
| 3899 | |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3900 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsMultipleTimesDoesNothing) { |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3901 | CreatePool(4, 4); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3902 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3903 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3904 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 2, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3905 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3906 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3907 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3908 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3909 | EXPECT_EQ(2u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3910 | TestGroupId("a"))); |
| 3911 | EXPECT_EQ(2u, |
| 3912 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3913 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3914 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3915 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3916 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 2, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3917 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3918 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3919 | EXPECT_EQ(2u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3920 | TestGroupId("a"))); |
| 3921 | EXPECT_EQ(2u, |
| 3922 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3923 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3924 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3925 | |
| 3926 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3927 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3928 | EXPECT_EQ( |
| 3929 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3930 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3931 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3932 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3933 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3934 | |
| 3935 | client_socket_factory_.SignalJob(0); |
| 3936 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3937 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3938 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3939 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3940 | TestGroupId("a"))); |
| 3941 | EXPECT_EQ(1u, |
| 3942 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3943 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3944 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3945 | |
| 3946 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3947 | TestCompletionCallback callback2; |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3948 | EXPECT_EQ( |
| 3949 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3950 | handle2.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3951 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3952 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3953 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3954 | client_socket_factory_.SignalJob(0); |
| 3955 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3956 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3957 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3958 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3959 | TestGroupId("a"))); |
| 3960 | EXPECT_EQ(0u, |
| 3961 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3962 | EXPECT_EQ(2, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3963 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3964 | |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3965 | handle1.Reset(); |
| 3966 | handle2.Reset(); |
| 3967 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3968 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3969 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3970 | TestGroupId("a"))); |
| 3971 | EXPECT_EQ(0u, |
| 3972 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3973 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3974 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3975 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3976 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 2, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3977 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3978 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3979 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3980 | TestGroupId("a"))); |
| 3981 | EXPECT_EQ(0u, |
| 3982 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3983 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3984 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3985 | } |
| 3986 | |
| 3987 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsDifferentNumSockets) { |
| 3988 | CreatePool(4, 4); |
| 3989 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3990 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3991 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 1, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3992 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3993 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3994 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3995 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3996 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3997 | TestGroupId("a"))); |
| 3998 | EXPECT_EQ(1u, |
| 3999 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4000 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4001 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4002 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 2, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4003 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4004 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4005 | EXPECT_EQ(2u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4006 | TestGroupId("a"))); |
| 4007 | EXPECT_EQ(2u, |
| 4008 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4009 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4010 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4011 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 3, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4012 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4013 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4014 | EXPECT_EQ(3u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4015 | TestGroupId("a"))); |
| 4016 | EXPECT_EQ(3u, |
| 4017 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4018 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4019 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4020 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 1, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4021 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4022 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4023 | EXPECT_EQ(3u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4024 | TestGroupId("a"))); |
| 4025 | EXPECT_EQ(3u, |
| 4026 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4027 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4028 | } |
| 4029 | |
| 4030 | TEST_F(ClientSocketPoolBaseTest, PreconnectJobsTakenByNormalRequests) { |
| 4031 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4032 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4033 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4034 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 1, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4035 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4036 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4037 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 4038 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4039 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4040 | TestGroupId("a"))); |
| 4041 | EXPECT_EQ(1u, |
| 4042 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4043 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4044 | |
| 4045 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 4046 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4047 | EXPECT_EQ( |
| 4048 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4049 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4050 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4051 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4052 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4053 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4054 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4055 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4056 | TestGroupId("a"))); |
| 4057 | EXPECT_EQ(0u, |
| 4058 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4059 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4060 | |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4061 | client_socket_factory_.SignalJobs(); |
| 4062 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 4063 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4064 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4065 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4066 | TestGroupId("a"))); |
| 4067 | EXPECT_EQ(0u, |
| 4068 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4069 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4070 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4071 | |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4072 | // Make sure if a preconnected socket is not fully connected when a request |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 4073 | // starts, it has a connect start time. |
| 4074 | TestLoadTimingInfoConnectedNotReused(handle1); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4075 | handle1.Reset(); |
| 4076 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4077 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4078 | } |
| 4079 | |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 4080 | // Checks that fully connected preconnect jobs have no connect times, and are |
| 4081 | // marked as reused. |
| 4082 | TEST_F(ClientSocketPoolBaseTest, ConnectedPreconnectJobsHaveNoConnectTimes) { |
| 4083 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4084 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4085 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 1, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4086 | NetLogWithSource()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 4087 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4088 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 4089 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4090 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4091 | TestGroupId("a"))); |
| 4092 | EXPECT_EQ(0u, |
| 4093 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4094 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 4095 | |
| 4096 | ClientSocketHandle handle; |
| 4097 | TestCompletionCallback callback; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4098 | EXPECT_EQ(OK, handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4099 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4100 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4101 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4102 | pool_.get(), NetLogWithSource())); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 4103 | |
| 4104 | // Make sure the idle socket was used. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4105 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 4106 | |
| 4107 | TestLoadTimingInfoConnectedReused(handle); |
| 4108 | handle.Reset(); |
| 4109 | TestLoadTimingInfoNotConnected(handle); |
| 4110 | } |
| 4111 | |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4112 | // http://crbug.com/64940 regression test. |
| 4113 | TEST_F(ClientSocketPoolBaseTest, PreconnectClosesIdleSocketRemovesGroup) { |
| 4114 | const int kMaxTotalSockets = 3; |
| 4115 | const int kMaxSocketsPerGroup = 2; |
| 4116 | CreatePool(kMaxTotalSockets, kMaxSocketsPerGroup); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4117 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4118 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 4119 | // Note that group id ordering matters here. "a" comes before "b", so |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4120 | // CloseOneIdleSocket() will try to close "a"'s idle socket. |
| 4121 | |
| 4122 | // Set up one idle socket in "a". |
| 4123 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 4124 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4125 | EXPECT_EQ( |
| 4126 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4127 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4128 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4129 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4130 | pool_.get(), NetLogWithSource())); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4131 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 4132 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4133 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4134 | TestGroupId("a"))); |
| 4135 | EXPECT_EQ(0u, |
| 4136 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4137 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4138 | |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4139 | client_socket_factory_.SignalJobs(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4140 | ASSERT_THAT(callback1.WaitForResult(), IsOk()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4141 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4142 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4143 | TestGroupId("a"))); |
| 4144 | EXPECT_EQ(0u, |
| 4145 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4146 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4147 | |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4148 | handle1.Reset(); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4149 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4150 | |
| 4151 | // Set up two active sockets in "b". |
| 4152 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 4153 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4154 | EXPECT_EQ( |
| 4155 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4156 | handle1.Init(TestGroupId("b"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4157 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4158 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4159 | pool_.get(), NetLogWithSource())); |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4160 | EXPECT_EQ( |
| 4161 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4162 | handle2.Init(TestGroupId("b"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4163 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4164 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4165 | pool_.get(), NetLogWithSource())); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4166 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4167 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("b"))); |
| 4168 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 4169 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4170 | TestGroupId("b"))); |
| 4171 | EXPECT_EQ(0u, |
| 4172 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4173 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4174 | |
| 4175 | client_socket_factory_.SignalJobs(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4176 | ASSERT_THAT(callback1.WaitForResult(), IsOk()); |
| 4177 | ASSERT_THAT(callback2.WaitForResult(), IsOk()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4178 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4179 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4180 | TestGroupId("b"))); |
| 4181 | EXPECT_EQ(0u, |
| 4182 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 4183 | EXPECT_EQ(2, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4184 | |
| 4185 | // Now we have 1 idle socket in "a" and 2 active sockets in "b". This means |
| 4186 | // we've maxed out on sockets, since we set |kMaxTotalSockets| to 3. |
| 4187 | // Requesting 2 preconnected sockets for "a" should fail to allocate any more |
| 4188 | // sockets for "a", and "b" should still have 2 active sockets. |
| 4189 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4190 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 2, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4191 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4192 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4193 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4194 | TestGroupId("a"))); |
| 4195 | EXPECT_EQ(0u, |
| 4196 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4197 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4198 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 4199 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 4200 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4201 | TestGroupId("b"))); |
| 4202 | EXPECT_EQ(0u, |
| 4203 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4204 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4205 | EXPECT_EQ(2, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4206 | |
| 4207 | // Now release the 2 active sockets for "b". This will give us 1 idle socket |
| 4208 | // in "a" and 2 idle sockets in "b". Requesting 2 preconnected sockets for |
| 4209 | // "a" should result in closing 1 for "b". |
| 4210 | handle1.Reset(); |
| 4211 | handle2.Reset(); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4212 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4213 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4214 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4215 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 2, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4216 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4217 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4218 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4219 | TestGroupId("a"))); |
| 4220 | EXPECT_EQ(1u, |
| 4221 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4222 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4223 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 4224 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 4225 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4226 | TestGroupId("b"))); |
| 4227 | EXPECT_EQ(0u, |
| 4228 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4229 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4230 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4231 | } |
| 4232 | |
[email protected] | b7b8be4 | 2011-07-12 12:46:41 | [diff] [blame] | 4233 | TEST_F(ClientSocketPoolBaseTest, PreconnectWithoutBackupJob) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4234 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 4235 | true /* enable_backup_connect_jobs */); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4236 | |
| 4237 | // Make the ConnectJob hang until it times out, shorten the timeout. |
| 4238 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 4239 | connect_job_factory_->set_timeout_duration(base::Milliseconds(500)); |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4240 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 1, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4241 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4242 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4243 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4244 | TestGroupId("a"))); |
| 4245 | EXPECT_EQ(1u, |
| 4246 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4247 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4248 | |
[email protected] | b7b8be4 | 2011-07-12 12:46:41 | [diff] [blame] | 4249 | // Verify the backup timer doesn't create a backup job, by making |
| 4250 | // the backup job a pending job instead of a waiting job, so it |
| 4251 | // *would* complete if it were created. |
Lukasz Krakowiak | 28dcf9d6 | 2020-06-04 09:46:59 | [diff] [blame] | 4252 | base::RunLoop loop; |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4253 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 4254 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 4255 | FROM_HERE, loop.QuitClosure(), base::Seconds(1)); |
Lukasz Krakowiak | 28dcf9d6 | 2020-06-04 09:46:59 | [diff] [blame] | 4256 | loop.Run(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4257 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4258 | } |
| 4259 | |
[email protected] | b7b8be4 | 2011-07-12 12:46:41 | [diff] [blame] | 4260 | TEST_F(ClientSocketPoolBaseTest, PreconnectWithBackupJob) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4261 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 4262 | true /* enable_backup_connect_jobs */); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4263 | |
| 4264 | // Make the ConnectJob hang forever. |
| 4265 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4266 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 1, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4267 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4268 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4269 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4270 | TestGroupId("a"))); |
| 4271 | EXPECT_EQ(1u, |
| 4272 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4273 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 4274 | base::RunLoop().RunUntilIdle(); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4275 | |
| 4276 | // Make the backup job be a pending job, so it completes normally. |
| 4277 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 4278 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 4279 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4280 | EXPECT_EQ( |
| 4281 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4282 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4283 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4284 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4285 | pool_.get(), NetLogWithSource())); |
[email protected] | b7b8be4 | 2011-07-12 12:46:41 | [diff] [blame] | 4286 | // Timer has started, but the backup connect job shouldn't be created yet. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4287 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4288 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4289 | TestGroupId("a"))); |
| 4290 | EXPECT_EQ(0u, |
| 4291 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4292 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4293 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4294 | ASSERT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4295 | |
| 4296 | // The hung connect job should still be there, but everything else should be |
| 4297 | // complete. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4298 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4299 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4300 | TestGroupId("a"))); |
| 4301 | EXPECT_EQ(1u, |
| 4302 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4303 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4304 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4305 | } |
| 4306 | |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4307 | // Tests that a preconnect that starts out with unread data can still be used. |
| 4308 | // http://crbug.com/334467 |
| 4309 | TEST_F(ClientSocketPoolBaseTest, PreconnectWithUnreadData) { |
| 4310 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4311 | connect_job_factory_->set_job_type(TestConnectJob::kMockUnreadDataJob); |
| 4312 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4313 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 1, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4314 | NetLogWithSource()); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4315 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4316 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 4317 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4318 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4319 | TestGroupId("a"))); |
| 4320 | EXPECT_EQ(0u, |
| 4321 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4322 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4323 | |
| 4324 | // Fail future jobs to be sure that handle receives the preconnected socket |
| 4325 | // rather than closing it and making a new one. |
| 4326 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
| 4327 | ClientSocketHandle handle; |
| 4328 | TestCompletionCallback callback; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4329 | EXPECT_EQ(OK, handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4330 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4331 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4332 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4333 | pool_.get(), NetLogWithSource())); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4334 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4335 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 4336 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4337 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4338 | TestGroupId("a"))); |
| 4339 | EXPECT_EQ(0u, |
| 4340 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4341 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4342 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4343 | |
| 4344 | // Drain the pending read. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 4345 | EXPECT_EQ(1, handle.socket()->Read(nullptr, 1, CompletionOnceCallback())); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4346 | |
| 4347 | TestLoadTimingInfoConnectedReused(handle); |
| 4348 | handle.Reset(); |
| 4349 | |
| 4350 | // The socket should be usable now that it's idle again. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4351 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4352 | } |
| 4353 | |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4354 | TEST_F(ClientSocketPoolBaseTest, RequestGetsAssignedJob) { |
| 4355 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4356 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4357 | |
| 4358 | ClientSocketHandle handle1; |
| 4359 | TestCompletionCallback callback1; |
| 4360 | EXPECT_EQ( |
| 4361 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4362 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4363 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4364 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4365 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4366 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4367 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4368 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4369 | TestGroupId("a"))); |
| 4370 | EXPECT_EQ(0u, |
| 4371 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4372 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4373 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4374 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4375 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4376 | } |
| 4377 | |
| 4378 | TEST_F(ClientSocketPoolBaseTest, MultipleRequestsGetAssignedJobs) { |
| 4379 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4380 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4381 | |
| 4382 | ClientSocketHandle handle1; |
| 4383 | TestCompletionCallback callback1; |
| 4384 | EXPECT_EQ( |
| 4385 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4386 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4387 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4388 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4389 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4390 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4391 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4392 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4393 | TestGroupId("a"))); |
| 4394 | EXPECT_EQ(0u, |
| 4395 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4396 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4397 | |
| 4398 | ClientSocketHandle handle2; |
| 4399 | TestCompletionCallback callback2; |
| 4400 | EXPECT_EQ( |
| 4401 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4402 | handle2.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4403 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4404 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4405 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4406 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4407 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4408 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4409 | TestGroupId("a"))); |
| 4410 | EXPECT_EQ(0u, |
| 4411 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4412 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4413 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4414 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4415 | &handle1)); |
| 4416 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4417 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4418 | |
| 4419 | // One job completes. The other request should still have its job. |
| 4420 | client_socket_factory_.SignalJob(0); |
| 4421 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 4422 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4423 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4424 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4425 | TestGroupId("a"))); |
| 4426 | EXPECT_EQ(0u, |
| 4427 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4428 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4429 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4430 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4431 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4432 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4433 | } |
| 4434 | |
| 4435 | TEST_F(ClientSocketPoolBaseTest, PreconnectJobGetsAssignedToRequest) { |
| 4436 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4437 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4438 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4439 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 1, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4440 | NetLogWithSource()); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4441 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4442 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 4443 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4444 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4445 | TestGroupId("a"))); |
| 4446 | EXPECT_EQ(1u, |
| 4447 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4448 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4449 | |
| 4450 | ClientSocketHandle handle1; |
| 4451 | TestCompletionCallback callback1; |
| 4452 | EXPECT_EQ( |
| 4453 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4454 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4455 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4456 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4457 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4458 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4459 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4460 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4461 | TestGroupId("a"))); |
| 4462 | EXPECT_EQ(0u, |
| 4463 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4464 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4465 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4466 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4467 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4468 | } |
| 4469 | |
| 4470 | TEST_F(ClientSocketPoolBaseTest, HigherPriorityRequestStealsJob) { |
| 4471 | CreatePool(kDefaultMaxSockets, 1); |
| 4472 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4473 | |
| 4474 | ClientSocketHandle handle1; |
| 4475 | TestCompletionCallback callback1; |
| 4476 | EXPECT_EQ( |
| 4477 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4478 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4479 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4480 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4481 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4482 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4483 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4484 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4485 | TestGroupId("a"))); |
| 4486 | EXPECT_EQ(0u, |
| 4487 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4488 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4489 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4490 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4491 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4492 | |
| 4493 | // Insert a higher priority request |
| 4494 | ClientSocketHandle handle2; |
| 4495 | TestCompletionCallback callback2; |
| 4496 | EXPECT_EQ( |
| 4497 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4498 | handle2.Init(TestGroupId("a"), params_, absl::nullopt, HIGHEST, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4499 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4500 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4501 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4502 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4503 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4504 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4505 | TestGroupId("a"))); |
| 4506 | EXPECT_EQ(0u, |
| 4507 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4508 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4509 | |
| 4510 | // The highest priority request should steal the job from the default priority |
| 4511 | // request. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4512 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4513 | &handle2)); |
| 4514 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4515 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4516 | } |
| 4517 | |
| 4518 | TEST_F(ClientSocketPoolBaseTest, RequestStealsJobFromLowestRequestWithJob) { |
| 4519 | CreatePool(3, 3); |
| 4520 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4521 | |
| 4522 | ClientSocketHandle handle_lowest; |
| 4523 | TestCompletionCallback callback_lowest; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4524 | EXPECT_EQ( |
| 4525 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4526 | handle_lowest.Init(TestGroupId("a"), params_, absl::nullopt, LOWEST, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4527 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4528 | callback_lowest.callback(), |
| 4529 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4530 | NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4531 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4532 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4533 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4534 | TestGroupId("a"))); |
| 4535 | EXPECT_EQ(0u, |
| 4536 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4537 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4538 | |
| 4539 | ClientSocketHandle handle_highest; |
| 4540 | TestCompletionCallback callback_highest; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4541 | EXPECT_EQ( |
| 4542 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4543 | handle_highest.Init(TestGroupId("a"), params_, absl::nullopt, HIGHEST, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4544 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4545 | callback_highest.callback(), |
| 4546 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4547 | NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4548 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4549 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4550 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4551 | TestGroupId("a"))); |
| 4552 | EXPECT_EQ(0u, |
| 4553 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4554 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4555 | |
| 4556 | ClientSocketHandle handle_low; |
| 4557 | TestCompletionCallback callback_low; |
| 4558 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4559 | handle_low.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4560 | TestGroupId("a"), params_, absl::nullopt, LOW, SocketTag(), |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4561 | ClientSocketPool::RespectLimits::ENABLED, |
| 4562 | callback_low.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4563 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4564 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4565 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4566 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4567 | TestGroupId("a"))); |
| 4568 | EXPECT_EQ(0u, |
| 4569 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4570 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4571 | |
| 4572 | ClientSocketHandle handle_lowest2; |
| 4573 | TestCompletionCallback callback_lowest2; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4574 | EXPECT_EQ( |
| 4575 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4576 | handle_lowest2.Init(TestGroupId("a"), params_, absl::nullopt, LOWEST, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4577 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4578 | callback_lowest2.callback(), |
| 4579 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4580 | NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4581 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4582 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4583 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4584 | TestGroupId("a"))); |
| 4585 | EXPECT_EQ(0u, |
| 4586 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4587 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4588 | |
| 4589 | // The top three requests in the queue should have jobs. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4590 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4591 | &handle_highest)); |
| 4592 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4593 | &handle_low)); |
| 4594 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4595 | &handle_lowest)); |
| 4596 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting( |
| 4597 | TestGroupId("a"), &handle_lowest2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4598 | |
| 4599 | // Add another request with medium priority. It should steal the job from the |
| 4600 | // lowest priority request with a job. |
| 4601 | ClientSocketHandle handle_medium; |
| 4602 | TestCompletionCallback callback_medium; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4603 | EXPECT_EQ( |
| 4604 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4605 | handle_medium.Init(TestGroupId("a"), params_, absl::nullopt, MEDIUM, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4606 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4607 | callback_medium.callback(), |
| 4608 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4609 | NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4610 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4611 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4612 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4613 | TestGroupId("a"))); |
| 4614 | EXPECT_EQ(0u, |
| 4615 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4616 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
| 4617 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4618 | &handle_highest)); |
| 4619 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4620 | &handle_medium)); |
| 4621 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4622 | &handle_low)); |
| 4623 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4624 | &handle_lowest)); |
| 4625 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting( |
| 4626 | TestGroupId("a"), &handle_lowest2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4627 | } |
| 4628 | |
| 4629 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeRequestStealsJob) { |
| 4630 | CreatePool(kDefaultMaxSockets, 1); |
| 4631 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4632 | |
| 4633 | ClientSocketHandle handle1; |
| 4634 | TestCompletionCallback callback1; |
| 4635 | EXPECT_EQ( |
| 4636 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4637 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4638 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4639 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4640 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4641 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4642 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4643 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4644 | TestGroupId("a"))); |
| 4645 | EXPECT_EQ(0u, |
| 4646 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4647 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4648 | |
| 4649 | ClientSocketHandle handle2; |
| 4650 | TestCompletionCallback callback2; |
| 4651 | EXPECT_EQ( |
| 4652 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4653 | handle2.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4654 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4655 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4656 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4657 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4658 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4659 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4660 | TestGroupId("a"))); |
| 4661 | EXPECT_EQ(0u, |
| 4662 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4663 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4664 | |
| 4665 | // 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] | 4666 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4667 | &handle1)); |
| 4668 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4669 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4670 | |
| 4671 | // Reprioritizing the second request places it above the first, and it steals |
| 4672 | // the job from the first request. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4673 | pool_->SetPriority(TestGroupId("a"), &handle2, HIGHEST); |
| 4674 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4675 | &handle2)); |
| 4676 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4677 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4678 | } |
| 4679 | |
| 4680 | TEST_F(ClientSocketPoolBaseTest, CancelRequestReassignsJob) { |
| 4681 | CreatePool(kDefaultMaxSockets, 1); |
| 4682 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4683 | |
| 4684 | ClientSocketHandle handle1; |
| 4685 | TestCompletionCallback callback1; |
| 4686 | EXPECT_EQ( |
| 4687 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4688 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4689 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4690 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4691 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4692 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4693 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4694 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4695 | TestGroupId("a"))); |
| 4696 | EXPECT_EQ(0u, |
| 4697 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4698 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4699 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4700 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4701 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4702 | |
| 4703 | ClientSocketHandle handle2; |
| 4704 | TestCompletionCallback callback2; |
| 4705 | EXPECT_EQ( |
| 4706 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4707 | handle2.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4708 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4709 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4710 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4711 | |
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 | |
| 4719 | // The second request doesn't get a job because we are the limit. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4720 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4721 | &handle1)); |
| 4722 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4723 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4724 | |
| 4725 | // The second request should get a job upon cancelling the first request. |
| 4726 | handle1.Reset(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4727 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4728 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4729 | TestGroupId("a"))); |
| 4730 | EXPECT_EQ(0u, |
| 4731 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4732 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4733 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4734 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4735 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4736 | } |
| 4737 | |
| 4738 | TEST_F(ClientSocketPoolBaseTest, JobCompletionReassignsJob) { |
| 4739 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4740 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4741 | |
| 4742 | ClientSocketHandle handle1; |
| 4743 | TestCompletionCallback callback1; |
| 4744 | EXPECT_EQ( |
| 4745 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4746 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, HIGHEST, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4747 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4748 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4749 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4750 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4751 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4752 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4753 | TestGroupId("a"))); |
| 4754 | EXPECT_EQ(0u, |
| 4755 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4756 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4757 | |
| 4758 | ClientSocketHandle handle2; |
| 4759 | TestCompletionCallback callback2; |
| 4760 | EXPECT_EQ( |
| 4761 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4762 | handle2.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4763 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4764 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4765 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4766 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4767 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4768 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4769 | TestGroupId("a"))); |
| 4770 | EXPECT_EQ(0u, |
| 4771 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4772 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4773 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4774 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4775 | &handle1)); |
| 4776 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4777 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4778 | |
| 4779 | // The lower-priority job completes first. The higher-priority request should |
| 4780 | // get the socket, and the lower-priority request should get the remaining |
| 4781 | // job. |
| 4782 | client_socket_factory_.SignalJob(1); |
| 4783 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4784 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4785 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4786 | TestGroupId("a"))); |
| 4787 | EXPECT_EQ(0u, |
| 4788 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4789 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4790 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4791 | EXPECT_TRUE(handle1.socket()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4792 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4793 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4794 | } |
| 4795 | |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 4796 | class MockLayeredPool : public HigherLayeredPool { |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4797 | public: |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4798 | MockLayeredPool(TransportClientSocketPool* pool, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4799 | const ClientSocketPool::GroupId& group_id) |
| 4800 | : pool_(pool), group_id_(group_id), can_release_connection_(true) { |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 4801 | pool_->AddHigherLayeredPool(this); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4802 | } |
| 4803 | |
Daniel Cheng | 4496d082 | 2018-04-26 21:52:15 | [diff] [blame] | 4804 | ~MockLayeredPool() override { pool_->RemoveHigherLayeredPool(this); } |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4805 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4806 | int RequestSocket(TransportClientSocketPool* pool) { |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4807 | return handle_.Init( |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 4808 | group_id_, ClientSocketPool::SocketParams::CreateForHttpForTesting(), |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4809 | absl::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4810 | ClientSocketPool::RespectLimits::ENABLED, callback_.callback(), |
| 4811 | ClientSocketPool::ProxyAuthCallback(), pool, NetLogWithSource()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4812 | } |
| 4813 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4814 | int RequestSocketWithoutLimits(TransportClientSocketPool* pool) { |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4815 | return handle_.Init( |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 4816 | group_id_, ClientSocketPool::SocketParams::CreateForHttpForTesting(), |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4817 | absl::nullopt, MAXIMUM_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4818 | ClientSocketPool::RespectLimits::DISABLED, callback_.callback(), |
| 4819 | ClientSocketPool::ProxyAuthCallback(), pool, NetLogWithSource()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4820 | } |
| 4821 | |
| 4822 | bool ReleaseOneConnection() { |
| 4823 | if (!handle_.is_initialized() || !can_release_connection_) { |
| 4824 | return false; |
| 4825 | } |
| 4826 | handle_.socket()->Disconnect(); |
| 4827 | handle_.Reset(); |
| 4828 | return true; |
| 4829 | } |
| 4830 | |
| 4831 | void set_can_release_connection(bool can_release_connection) { |
| 4832 | can_release_connection_ = can_release_connection; |
| 4833 | } |
| 4834 | |
| 4835 | MOCK_METHOD0(CloseOneIdleConnection, bool()); |
| 4836 | |
| 4837 | private: |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4838 | TransportClientSocketPool* const pool_; |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4839 | ClientSocketHandle handle_; |
| 4840 | TestCompletionCallback callback_; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4841 | const ClientSocketPool::GroupId group_id_; |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4842 | bool can_release_connection_; |
| 4843 | }; |
| 4844 | |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4845 | // Tests the basic case of closing an idle socket in a higher layered pool when |
| 4846 | // a new request is issued and the lower layer pool is stalled. |
| 4847 | TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketsHeldByLayeredPoolWhenNeeded) { |
| 4848 | CreatePool(1, 1); |
| 4849 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 4850 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4851 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("foo")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4852 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4853 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4854 | .WillOnce(Invoke(&mock_layered_pool, |
| 4855 | &MockLayeredPool::ReleaseOneConnection)); |
| 4856 | ClientSocketHandle handle; |
| 4857 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4858 | EXPECT_EQ( |
| 4859 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4860 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4861 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4862 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4863 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4864 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4865 | } |
| 4866 | |
Matt Menke | 83367864 | 2019-03-05 22:05:51 | [diff] [blame] | 4867 | // Tests the case that trying to close an idle socket in a higher layered pool |
| 4868 | // fails. |
| 4869 | TEST_F(ClientSocketPoolBaseTest, |
| 4870 | CloseIdleSocketsHeldByLayeredPoolWhenNeededFails) { |
| 4871 | CreatePool(1, 1); |
| 4872 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 4873 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4874 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("foo")); |
Matt Menke | 83367864 | 2019-03-05 22:05:51 | [diff] [blame] | 4875 | mock_layered_pool.set_can_release_connection(false); |
| 4876 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
| 4877 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4878 | .WillOnce(Invoke(&mock_layered_pool, |
| 4879 | &MockLayeredPool::ReleaseOneConnection)); |
| 4880 | ClientSocketHandle handle; |
| 4881 | TestCompletionCallback callback; |
| 4882 | EXPECT_EQ( |
| 4883 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4884 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4885 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4886 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4887 | pool_.get(), NetLogWithSource())); |
Matt Menke | 83367864 | 2019-03-05 22:05:51 | [diff] [blame] | 4888 | base::RunLoop().RunUntilIdle(); |
| 4889 | EXPECT_FALSE(callback.have_result()); |
| 4890 | } |
| 4891 | |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4892 | // Same as above, but the idle socket is in the same group as the stalled |
| 4893 | // socket, and closes the only other request in its group when closing requests |
| 4894 | // in higher layered pools. This generally shouldn't happen, but it may be |
| 4895 | // possible if a higher level pool issues a request and the request is |
| 4896 | // subsequently cancelled. Even if it's not possible, best not to crash. |
| 4897 | TEST_F(ClientSocketPoolBaseTest, |
| 4898 | CloseIdleSocketsHeldByLayeredPoolWhenNeededSameGroup) { |
| 4899 | CreatePool(2, 2); |
| 4900 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 4901 | |
| 4902 | // Need a socket in another group for the pool to be stalled (If a group |
| 4903 | // has the maximum number of connections already, it's not stalled). |
| 4904 | ClientSocketHandle handle1; |
| 4905 | TestCompletionCallback callback1; |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4906 | EXPECT_EQ(OK, handle1.Init(TestGroupId("group1"), params_, absl::nullopt, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4907 | DEFAULT_PRIORITY, SocketTag(), |
| 4908 | ClientSocketPool::RespectLimits::ENABLED, |
| 4909 | callback1.callback(), |
| 4910 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4911 | NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4912 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4913 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("group2")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4914 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4915 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4916 | .WillOnce(Invoke(&mock_layered_pool, |
| 4917 | &MockLayeredPool::ReleaseOneConnection)); |
| 4918 | ClientSocketHandle handle; |
| 4919 | TestCompletionCallback callback2; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4920 | EXPECT_EQ(ERR_IO_PENDING, |
| 4921 | handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4922 | TestGroupId("group2"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4923 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4924 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4925 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4926 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4927 | } |
| 4928 | |
| 4929 | // Tests the case when an idle socket can be closed when a new request is |
| 4930 | // issued, and the new request belongs to a group that was previously stalled. |
| 4931 | TEST_F(ClientSocketPoolBaseTest, |
| 4932 | CloseIdleSocketsHeldByLayeredPoolInSameGroupWhenNeeded) { |
| 4933 | CreatePool(2, 2); |
| 4934 | std::list<TestConnectJob::JobType> job_types; |
| 4935 | job_types.push_back(TestConnectJob::kMockJob); |
| 4936 | job_types.push_back(TestConnectJob::kMockJob); |
| 4937 | job_types.push_back(TestConnectJob::kMockJob); |
| 4938 | job_types.push_back(TestConnectJob::kMockJob); |
| 4939 | connect_job_factory_->set_job_types(&job_types); |
| 4940 | |
| 4941 | ClientSocketHandle handle1; |
| 4942 | TestCompletionCallback callback1; |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4943 | EXPECT_EQ(OK, handle1.Init(TestGroupId("group1"), params_, absl::nullopt, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4944 | DEFAULT_PRIORITY, SocketTag(), |
| 4945 | ClientSocketPool::RespectLimits::ENABLED, |
| 4946 | callback1.callback(), |
| 4947 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4948 | NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4949 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4950 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("group2")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4951 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4952 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4953 | .WillRepeatedly(Invoke(&mock_layered_pool, |
| 4954 | &MockLayeredPool::ReleaseOneConnection)); |
| 4955 | mock_layered_pool.set_can_release_connection(false); |
| 4956 | |
| 4957 | // The third request is made when the socket pool is in a stalled state. |
| 4958 | ClientSocketHandle handle3; |
| 4959 | TestCompletionCallback callback3; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4960 | EXPECT_EQ(ERR_IO_PENDING, |
| 4961 | handle3.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4962 | TestGroupId("group3"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4963 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4964 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4965 | pool_.get(), NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4966 | |
| 4967 | base::RunLoop().RunUntilIdle(); |
| 4968 | EXPECT_FALSE(callback3.have_result()); |
| 4969 | |
| 4970 | // The fourth request is made when the pool is no longer stalled. The third |
| 4971 | // request should be serviced first, since it was issued first and has the |
| 4972 | // same priority. |
| 4973 | mock_layered_pool.set_can_release_connection(true); |
| 4974 | ClientSocketHandle handle4; |
| 4975 | TestCompletionCallback callback4; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4976 | EXPECT_EQ(ERR_IO_PENDING, |
| 4977 | handle4.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4978 | TestGroupId("group3"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4979 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4980 | callback4.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4981 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4982 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4983 | EXPECT_FALSE(callback4.have_result()); |
| 4984 | |
| 4985 | // Closing a handle should free up another socket slot. |
| 4986 | handle1.Reset(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4987 | EXPECT_THAT(callback4.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4988 | } |
| 4989 | |
| 4990 | // Tests the case when an idle socket can be closed when a new request is |
| 4991 | // issued, and the new request belongs to a group that was previously stalled. |
| 4992 | // |
| 4993 | // The two differences from the above test are that the stalled requests are not |
| 4994 | // in the same group as the layered pool's request, and the the fourth request |
| 4995 | // has a higher priority than the third one, so gets a socket first. |
| 4996 | TEST_F(ClientSocketPoolBaseTest, |
| 4997 | CloseIdleSocketsHeldByLayeredPoolInSameGroupWhenNeeded2) { |
| 4998 | CreatePool(2, 2); |
| 4999 | std::list<TestConnectJob::JobType> job_types; |
| 5000 | job_types.push_back(TestConnectJob::kMockJob); |
| 5001 | job_types.push_back(TestConnectJob::kMockJob); |
| 5002 | job_types.push_back(TestConnectJob::kMockJob); |
| 5003 | job_types.push_back(TestConnectJob::kMockJob); |
| 5004 | connect_job_factory_->set_job_types(&job_types); |
| 5005 | |
| 5006 | ClientSocketHandle handle1; |
| 5007 | TestCompletionCallback callback1; |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 5008 | EXPECT_EQ(OK, handle1.Init(TestGroupId("group1"), params_, absl::nullopt, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5009 | DEFAULT_PRIORITY, SocketTag(), |
| 5010 | ClientSocketPool::RespectLimits::ENABLED, |
| 5011 | callback1.callback(), |
| 5012 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 5013 | NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 5014 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5015 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("group2")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5016 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 5017 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 5018 | .WillRepeatedly(Invoke(&mock_layered_pool, |
| 5019 | &MockLayeredPool::ReleaseOneConnection)); |
| 5020 | mock_layered_pool.set_can_release_connection(false); |
| 5021 | |
| 5022 | // The third request is made when the socket pool is in a stalled state. |
| 5023 | ClientSocketHandle handle3; |
| 5024 | TestCompletionCallback callback3; |
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 | handle3.Init(TestGroupId("group3"), params_, absl::nullopt, MEDIUM, |
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 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5030 | pool_.get(), NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 5031 | |
| 5032 | base::RunLoop().RunUntilIdle(); |
| 5033 | EXPECT_FALSE(callback3.have_result()); |
| 5034 | |
| 5035 | // The fourth request is made when the pool is no longer stalled. This |
| 5036 | // request has a higher priority than the third request, so is serviced first. |
| 5037 | mock_layered_pool.set_can_release_connection(true); |
| 5038 | ClientSocketHandle handle4; |
| 5039 | TestCompletionCallback callback4; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 5040 | EXPECT_EQ( |
| 5041 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 5042 | handle4.Init(TestGroupId("group3"), params_, absl::nullopt, HIGHEST, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5043 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 5044 | callback4.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5045 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5046 | EXPECT_THAT(callback4.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 5047 | EXPECT_FALSE(callback3.have_result()); |
| 5048 | |
| 5049 | // Closing a handle should free up another socket slot. |
| 5050 | handle1.Reset(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5051 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 5052 | } |
| 5053 | |
| 5054 | TEST_F(ClientSocketPoolBaseTest, |
| 5055 | CloseMultipleIdleSocketsHeldByLayeredPoolWhenNeeded) { |
| 5056 | CreatePool(1, 1); |
| 5057 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 5058 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5059 | MockLayeredPool mock_layered_pool1(pool_.get(), TestGroupId("foo")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5060 | EXPECT_THAT(mock_layered_pool1.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 5061 | EXPECT_CALL(mock_layered_pool1, CloseOneIdleConnection()) |
| 5062 | .WillRepeatedly(Invoke(&mock_layered_pool1, |
| 5063 | &MockLayeredPool::ReleaseOneConnection)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5064 | MockLayeredPool mock_layered_pool2(pool_.get(), TestGroupId("bar")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5065 | EXPECT_THAT(mock_layered_pool2.RequestSocketWithoutLimits(pool_.get()), |
| 5066 | IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 5067 | EXPECT_CALL(mock_layered_pool2, CloseOneIdleConnection()) |
| 5068 | .WillRepeatedly(Invoke(&mock_layered_pool2, |
| 5069 | &MockLayeredPool::ReleaseOneConnection)); |
| 5070 | ClientSocketHandle handle; |
| 5071 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 5072 | EXPECT_EQ( |
| 5073 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 5074 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5075 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5076 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5077 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5078 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 5079 | } |
| 5080 | |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5081 | // Test that when a socket pool and group are at their limits, a request |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5082 | // with RespectLimits::DISABLED triggers creation of a new socket, and gets the |
| 5083 | // socket instead of a request with the same priority that was issued earlier, |
| 5084 | // but has RespectLimits::ENABLED. |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5085 | TEST_F(ClientSocketPoolBaseTest, IgnoreLimits) { |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5086 | CreatePool(1, 1); |
| 5087 | |
| 5088 | // Issue a request to reach the socket pool limit. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5089 | EXPECT_EQ(OK, StartRequestWithIgnoreLimits( |
| 5090 | TestGroupId("a"), MAXIMUM_PRIORITY, |
| 5091 | ClientSocketPool::RespectLimits::ENABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5092 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5093 | |
| 5094 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 5095 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5096 | EXPECT_EQ(ERR_IO_PENDING, StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5097 | TestGroupId("a"), MAXIMUM_PRIORITY, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5098 | ClientSocketPool::RespectLimits::ENABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5099 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5100 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5101 | // Issue a request that ignores the limits, so a new ConnectJob is |
| 5102 | // created. |
| 5103 | EXPECT_EQ(ERR_IO_PENDING, StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5104 | TestGroupId("a"), MAXIMUM_PRIORITY, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5105 | ClientSocketPool::RespectLimits::DISABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5106 | ASSERT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5107 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5108 | EXPECT_THAT(request(2)->WaitForResult(), IsOk()); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5109 | EXPECT_FALSE(request(1)->have_result()); |
| 5110 | } |
| 5111 | |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 5112 | // Test that when a socket pool and group are at their limits, a ConnectJob |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5113 | // issued for a request with RespectLimits::DISABLED is not cancelled when a |
| 5114 | // request with RespectLimits::ENABLED issued to the same group is cancelled. |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 5115 | TEST_F(ClientSocketPoolBaseTest, IgnoreLimitsCancelOtherJob) { |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 5116 | CreatePool(1, 1); |
| 5117 | |
| 5118 | // Issue a request to reach the socket pool limit. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5119 | EXPECT_EQ(OK, StartRequestWithIgnoreLimits( |
| 5120 | TestGroupId("a"), MAXIMUM_PRIORITY, |
| 5121 | ClientSocketPool::RespectLimits::ENABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5122 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 5123 | |
| 5124 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 5125 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5126 | EXPECT_EQ(ERR_IO_PENDING, StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5127 | TestGroupId("a"), MAXIMUM_PRIORITY, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5128 | ClientSocketPool::RespectLimits::ENABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5129 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 5130 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5131 | // Issue a request with RespectLimits::DISABLED, so a new ConnectJob is |
| 5132 | // created. |
| 5133 | EXPECT_EQ(ERR_IO_PENDING, StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5134 | TestGroupId("a"), MAXIMUM_PRIORITY, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5135 | ClientSocketPool::RespectLimits::DISABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5136 | ASSERT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5137 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5138 | // Cancel the pending request with RespectLimits::ENABLED. The ConnectJob |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5139 | // should not be cancelled. |
| 5140 | request(1)->handle()->Reset(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5141 | ASSERT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5142 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5143 | EXPECT_THAT(request(2)->WaitForResult(), IsOk()); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5144 | EXPECT_FALSE(request(1)->have_result()); |
| 5145 | } |
| 5146 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5147 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthNoAuthCallback) { |
| 5148 | CreatePool(1, 1); |
| 5149 | |
| 5150 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5151 | |
| 5152 | ClientSocketHandle handle; |
| 5153 | TestCompletionCallback callback; |
| 5154 | EXPECT_EQ( |
| 5155 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 5156 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5157 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5158 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5159 | pool_.get(), NetLogWithSource())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5160 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5161 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5162 | |
| 5163 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_PROXY_AUTH_REQUESTED)); |
| 5164 | EXPECT_FALSE(handle.is_initialized()); |
| 5165 | EXPECT_FALSE(handle.socket()); |
| 5166 | |
| 5167 | // The group should now be empty, and thus be deleted. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5168 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5169 | } |
| 5170 | |
| 5171 | class TestAuthHelper { |
| 5172 | public: |
| 5173 | TestAuthHelper() = default; |
Peter Boström | 293b134 | 2021-09-22 17:31:43 | [diff] [blame] | 5174 | |
| 5175 | TestAuthHelper(const TestAuthHelper&) = delete; |
| 5176 | TestAuthHelper& operator=(const TestAuthHelper&) = delete; |
| 5177 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5178 | ~TestAuthHelper() = default; |
| 5179 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5180 | void InitHandle( |
Matt Menke | 84d11e56 | 2019-03-27 00:11:19 | [diff] [blame] | 5181 | scoped_refptr<ClientSocketPool::SocketParams> params, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5182 | TransportClientSocketPool* pool, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5183 | RequestPriority priority = DEFAULT_PRIORITY, |
| 5184 | ClientSocketPool::RespectLimits respect_limits = |
| 5185 | ClientSocketPool::RespectLimits::ENABLED, |
| 5186 | const ClientSocketPool::GroupId& group_id_in = TestGroupId("a")) { |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5187 | EXPECT_EQ(ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 5188 | handle_.Init(group_id_in, params, absl::nullopt, priority, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5189 | SocketTag(), respect_limits, callback_.callback(), |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5190 | base::BindRepeating(&TestAuthHelper::AuthCallback, |
| 5191 | base::Unretained(this)), |
| 5192 | pool, NetLogWithSource())); |
| 5193 | } |
| 5194 | |
| 5195 | void WaitForAuth() { |
| 5196 | run_loop_ = std::make_unique<base::RunLoop>(); |
| 5197 | run_loop_->Run(); |
| 5198 | run_loop_.reset(); |
| 5199 | } |
| 5200 | |
| 5201 | void WaitForAuthAndRestartSync() { |
| 5202 | restart_sync_ = true; |
| 5203 | WaitForAuth(); |
| 5204 | restart_sync_ = false; |
| 5205 | } |
| 5206 | |
| 5207 | void WaitForAuthAndResetHandleSync() { |
| 5208 | reset_handle_sync_ = true; |
| 5209 | WaitForAuth(); |
| 5210 | reset_handle_sync_ = false; |
| 5211 | } |
| 5212 | |
| 5213 | void RestartWithAuth() { |
| 5214 | DCHECK(restart_with_auth_callback_); |
| 5215 | std::move(restart_with_auth_callback_).Run(); |
| 5216 | } |
| 5217 | |
| 5218 | int WaitForResult() { |
| 5219 | int result = callback_.WaitForResult(); |
| 5220 | // There shouldn't be any callback waiting to be invoked once the request is |
| 5221 | // complete. |
| 5222 | EXPECT_FALSE(restart_with_auth_callback_); |
| 5223 | // The socket should only be initialized on success. |
| 5224 | EXPECT_EQ(result == OK, handle_.is_initialized()); |
| 5225 | EXPECT_EQ(result == OK, handle_.socket() != nullptr); |
| 5226 | return result; |
| 5227 | } |
| 5228 | |
| 5229 | ClientSocketHandle* handle() { return &handle_; } |
| 5230 | int auth_count() const { return auth_count_; } |
| 5231 | int have_result() const { return callback_.have_result(); } |
| 5232 | |
| 5233 | private: |
| 5234 | void AuthCallback(const HttpResponseInfo& response, |
| 5235 | HttpAuthController* auth_controller, |
| 5236 | base::OnceClosure restart_with_auth_callback) { |
| 5237 | EXPECT_FALSE(restart_with_auth_callback_); |
| 5238 | EXPECT_TRUE(restart_with_auth_callback); |
| 5239 | |
| 5240 | // Once there's a result, this method shouldn't be invoked again. |
| 5241 | EXPECT_FALSE(callback_.have_result()); |
| 5242 | |
| 5243 | ++auth_count_; |
| 5244 | run_loop_->Quit(); |
| 5245 | if (restart_sync_) { |
| 5246 | std::move(restart_with_auth_callback).Run(); |
| 5247 | return; |
| 5248 | } |
| 5249 | |
| 5250 | restart_with_auth_callback_ = std::move(restart_with_auth_callback); |
| 5251 | |
| 5252 | if (reset_handle_sync_) { |
| 5253 | handle_.Reset(); |
| 5254 | return; |
| 5255 | } |
| 5256 | } |
| 5257 | |
| 5258 | std::unique_ptr<base::RunLoop> run_loop_; |
| 5259 | base::OnceClosure restart_with_auth_callback_; |
| 5260 | |
| 5261 | bool restart_sync_ = false; |
| 5262 | bool reset_handle_sync_ = false; |
| 5263 | |
| 5264 | ClientSocketHandle handle_; |
| 5265 | int auth_count_ = 0; |
| 5266 | TestCompletionCallback callback_; |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5267 | }; |
| 5268 | |
| 5269 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnce) { |
| 5270 | CreatePool(1, 1); |
| 5271 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5272 | |
| 5273 | TestAuthHelper auth_helper; |
| 5274 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5275 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5276 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5277 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5278 | |
| 5279 | auth_helper.WaitForAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5280 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5281 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5282 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5283 | |
| 5284 | auth_helper.RestartWithAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5285 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5286 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5287 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5288 | |
| 5289 | EXPECT_THAT(auth_helper.WaitForResult(), IsOk()); |
| 5290 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5291 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5292 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5293 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5294 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5295 | } |
| 5296 | |
| 5297 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceSync) { |
| 5298 | CreatePool(1, 1); |
| 5299 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5300 | |
| 5301 | TestAuthHelper auth_helper; |
| 5302 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5303 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5304 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5305 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5306 | |
| 5307 | auth_helper.WaitForAuthAndRestartSync(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5308 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5309 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5310 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5311 | |
| 5312 | EXPECT_THAT(auth_helper.WaitForResult(), IsOk()); |
| 5313 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5314 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5315 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5316 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5317 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5318 | } |
| 5319 | |
| 5320 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceFails) { |
| 5321 | CreatePool(1, 1); |
| 5322 | connect_job_factory_->set_job_type( |
| 5323 | TestConnectJob::kMockAuthChallengeOnceFailingJob); |
| 5324 | |
| 5325 | TestAuthHelper auth_helper; |
| 5326 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5327 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5328 | |
| 5329 | auth_helper.WaitForAuth(); |
| 5330 | auth_helper.RestartWithAuth(); |
| 5331 | EXPECT_THAT(auth_helper.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 5332 | |
| 5333 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5334 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5335 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5336 | } |
| 5337 | |
| 5338 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceSyncFails) { |
| 5339 | CreatePool(1, 1); |
| 5340 | connect_job_factory_->set_job_type( |
| 5341 | TestConnectJob::kMockAuthChallengeOnceFailingJob); |
| 5342 | |
| 5343 | TestAuthHelper auth_helper; |
| 5344 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5345 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5346 | |
| 5347 | auth_helper.WaitForAuthAndRestartSync(); |
| 5348 | EXPECT_THAT(auth_helper.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 5349 | |
| 5350 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5351 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5352 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5353 | } |
| 5354 | |
| 5355 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceDeleteHandle) { |
| 5356 | CreatePool(1, 1); |
| 5357 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5358 | |
| 5359 | TestAuthHelper auth_helper; |
| 5360 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5361 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5362 | |
| 5363 | auth_helper.WaitForAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5364 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5365 | |
| 5366 | auth_helper.handle()->Reset(); |
| 5367 | |
| 5368 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5369 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5370 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5371 | EXPECT_FALSE(auth_helper.handle()->is_initialized()); |
| 5372 | EXPECT_FALSE(auth_helper.handle()->socket()); |
| 5373 | } |
| 5374 | |
| 5375 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceDeleteHandleSync) { |
| 5376 | CreatePool(1, 1); |
| 5377 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5378 | |
| 5379 | TestAuthHelper auth_helper; |
| 5380 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5381 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5382 | |
| 5383 | auth_helper.WaitForAuthAndResetHandleSync(); |
| 5384 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5385 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5386 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5387 | EXPECT_FALSE(auth_helper.handle()->is_initialized()); |
| 5388 | EXPECT_FALSE(auth_helper.handle()->socket()); |
| 5389 | } |
| 5390 | |
| 5391 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceFlushWithError) { |
| 5392 | CreatePool(1, 1); |
| 5393 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5394 | |
| 5395 | TestAuthHelper auth_helper; |
| 5396 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5397 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5398 | |
| 5399 | auth_helper.WaitForAuth(); |
| 5400 | |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 5401 | pool_->FlushWithError(ERR_FAILED, "Network changed"); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5402 | base::RunLoop().RunUntilIdle(); |
| 5403 | |
| 5404 | // When flushing the socket pool, bound sockets should delay returning the |
| 5405 | // error until completion. |
| 5406 | EXPECT_FALSE(auth_helper.have_result()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5407 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5408 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5409 | |
| 5410 | auth_helper.RestartWithAuth(); |
| 5411 | // The callback should be called asynchronously. |
| 5412 | EXPECT_FALSE(auth_helper.have_result()); |
| 5413 | |
| 5414 | EXPECT_THAT(auth_helper.WaitForResult(), IsError(ERR_FAILED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5415 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5416 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5417 | } |
| 5418 | |
| 5419 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthTwice) { |
| 5420 | CreatePool(1, 1); |
| 5421 | connect_job_factory_->set_job_type( |
| 5422 | TestConnectJob::kMockAuthChallengeTwiceJob); |
| 5423 | |
| 5424 | TestAuthHelper auth_helper; |
| 5425 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5426 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5427 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5428 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5429 | |
| 5430 | auth_helper.WaitForAuth(); |
| 5431 | auth_helper.RestartWithAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5432 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5433 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5434 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5435 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5436 | |
| 5437 | auth_helper.WaitForAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5438 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5439 | EXPECT_EQ(2, auth_helper.auth_count()); |
| 5440 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5441 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5442 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5443 | auth_helper.RestartWithAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5444 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5445 | EXPECT_EQ(2, auth_helper.auth_count()); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5446 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5447 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5448 | |
| 5449 | EXPECT_THAT(auth_helper.WaitForResult(), IsOk()); |
| 5450 | EXPECT_EQ(2, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5451 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5452 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5453 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5454 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5455 | } |
| 5456 | |
| 5457 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthTwiceFails) { |
| 5458 | CreatePool(1, 1); |
| 5459 | connect_job_factory_->set_job_type( |
| 5460 | TestConnectJob::kMockAuthChallengeTwiceFailingJob); |
| 5461 | |
| 5462 | TestAuthHelper auth_helper; |
| 5463 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5464 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5465 | |
| 5466 | auth_helper.WaitForAuth(); |
| 5467 | auth_helper.RestartWithAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5468 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5469 | EXPECT_EQ(1, auth_helper.auth_count()); |
| 5470 | |
| 5471 | auth_helper.WaitForAuth(); |
| 5472 | auth_helper.RestartWithAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5473 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5474 | EXPECT_EQ(2, auth_helper.auth_count()); |
| 5475 | |
| 5476 | EXPECT_THAT(auth_helper.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 5477 | EXPECT_EQ(2, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5478 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5479 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5480 | } |
| 5481 | |
| 5482 | // Makes sure that when a bound request is destroyed, a new ConnectJob is |
| 5483 | // created, if needed. |
| 5484 | TEST_F(ClientSocketPoolBaseTest, |
| 5485 | ProxyAuthCreateNewConnectJobOnDestroyBoundRequest) { |
| 5486 | CreatePool(1 /* max_sockets */, 1 /* max_sockets_per_group */); |
| 5487 | connect_job_factory_->set_job_type( |
| 5488 | TestConnectJob::kMockAuthChallengeOnceFailingJob); |
| 5489 | |
| 5490 | // First request creates a ConnectJob. |
| 5491 | TestAuthHelper auth_helper1; |
| 5492 | auth_helper1.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5493 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5494 | |
| 5495 | // A second request come in, but no new ConnectJob is needed, since the limit |
| 5496 | // has been reached. |
| 5497 | TestAuthHelper auth_helper2; |
| 5498 | auth_helper2.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5499 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5500 | |
| 5501 | // Run until the auth callback for the first request is invoked. |
| 5502 | auth_helper1.WaitForAuth(); |
| 5503 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5504 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5505 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5506 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5507 | |
| 5508 | // Make connect jobs succeed, then cancel the first request, which should |
| 5509 | // destroy the bound ConnectJob, and cause a new ConnectJob to start. |
| 5510 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 5511 | auth_helper1.handle()->Reset(); |
| 5512 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5513 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5514 | |
| 5515 | // The second ConnectJob should succeed. |
| 5516 | EXPECT_THAT(auth_helper2.WaitForResult(), IsOk()); |
| 5517 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5518 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5519 | } |
| 5520 | |
| 5521 | // Makes sure that when a bound request is destroyed, a new ConnectJob is |
| 5522 | // created for another group, if needed. |
| 5523 | TEST_F(ClientSocketPoolBaseTest, |
| 5524 | ProxyAuthCreateNewConnectJobOnDestroyBoundRequestDifferentGroups) { |
| 5525 | CreatePool(1 /* max_sockets */, 1 /* max_sockets_per_group */); |
| 5526 | connect_job_factory_->set_job_type( |
| 5527 | TestConnectJob::kMockAuthChallengeOnceFailingJob); |
| 5528 | |
| 5529 | // First request creates a ConnectJob. |
| 5530 | TestAuthHelper auth_helper1; |
| 5531 | auth_helper1.InitHandle(params_, pool_.get(), DEFAULT_PRIORITY); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5532 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5533 | |
| 5534 | // A second request come in, but no new ConnectJob is needed, since the limit |
| 5535 | // has been reached. |
| 5536 | TestAuthHelper auth_helper2; |
| 5537 | auth_helper2.InitHandle(params_, pool_.get(), DEFAULT_PRIORITY, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5538 | ClientSocketPool::RespectLimits::ENABLED, |
| 5539 | TestGroupId("b")); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5540 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5541 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5542 | |
| 5543 | // Run until the auth callback for the first request is invoked. |
| 5544 | auth_helper1.WaitForAuth(); |
| 5545 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5546 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5547 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5548 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5549 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 5550 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5551 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5552 | |
| 5553 | // Make connect jobs succeed, then cancel the first request, which should |
| 5554 | // destroy the bound ConnectJob, and cause a new ConnectJob to start for the |
| 5555 | // other group. |
| 5556 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 5557 | auth_helper1.handle()->Reset(); |
| 5558 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5559 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 5560 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5561 | |
| 5562 | // The second ConnectJob should succeed. |
| 5563 | EXPECT_THAT(auth_helper2.WaitForResult(), IsOk()); |
| 5564 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5565 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 5566 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5567 | } |
| 5568 | |
| 5569 | // Test that once an auth challenge is bound, that's the request that gets all |
| 5570 | // subsequent calls and the socket itself. |
| 5571 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthStaysBound) { |
| 5572 | CreatePool(1, 1); |
| 5573 | connect_job_factory_->set_job_type( |
| 5574 | TestConnectJob::kMockAuthChallengeTwiceJob); |
| 5575 | |
| 5576 | // First request creates a ConnectJob. |
| 5577 | TestAuthHelper auth_helper1; |
| 5578 | auth_helper1.InitHandle(params_, pool_.get(), LOWEST); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5579 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5580 | |
| 5581 | // A second, higher priority request is made. |
| 5582 | TestAuthHelper auth_helper2; |
| 5583 | auth_helper2.InitHandle(params_, pool_.get(), LOW); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5584 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5585 | |
| 5586 | // Run until the auth callback for the second request is invoked. |
| 5587 | auth_helper2.WaitForAuth(); |
| 5588 | EXPECT_EQ(0, auth_helper1.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5589 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5590 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5591 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5592 | |
| 5593 | // Start a higher priority job. It shouldn't be able to steal |auth_helper2|'s |
| 5594 | // ConnectJob. |
| 5595 | TestAuthHelper auth_helper3; |
| 5596 | auth_helper3.InitHandle(params_, pool_.get(), HIGHEST); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5597 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5598 | |
| 5599 | // Start a higher job that ignores limits, creating a hanging socket. It |
| 5600 | // shouldn't be able to steal |auth_helper2|'s ConnectJob. |
| 5601 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 5602 | TestAuthHelper auth_helper4; |
| 5603 | auth_helper4.InitHandle(params_, pool_.get(), HIGHEST, |
| 5604 | ClientSocketPool::RespectLimits::DISABLED); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5605 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5606 | |
| 5607 | // Restart with auth, and |auth_helper2|'s auth method should be invoked |
| 5608 | // again. |
| 5609 | auth_helper2.RestartWithAuth(); |
| 5610 | auth_helper2.WaitForAuth(); |
| 5611 | EXPECT_EQ(0, auth_helper1.auth_count()); |
| 5612 | EXPECT_FALSE(auth_helper1.have_result()); |
| 5613 | EXPECT_EQ(2, auth_helper2.auth_count()); |
| 5614 | EXPECT_FALSE(auth_helper2.have_result()); |
| 5615 | EXPECT_EQ(0, auth_helper3.auth_count()); |
| 5616 | EXPECT_FALSE(auth_helper3.have_result()); |
| 5617 | EXPECT_EQ(0, auth_helper4.auth_count()); |
| 5618 | EXPECT_FALSE(auth_helper4.have_result()); |
| 5619 | |
| 5620 | // Advance auth again, and |auth_helper2| should get the socket. |
| 5621 | auth_helper2.RestartWithAuth(); |
| 5622 | EXPECT_THAT(auth_helper2.WaitForResult(), IsOk()); |
| 5623 | // The hung ConnectJob for the RespectLimits::DISABLED request is still in the |
| 5624 | // socket pool. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5625 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5626 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5627 | EXPECT_EQ(0, auth_helper1.auth_count()); |
| 5628 | EXPECT_FALSE(auth_helper1.have_result()); |
| 5629 | EXPECT_EQ(0, auth_helper3.auth_count()); |
| 5630 | EXPECT_FALSE(auth_helper3.have_result()); |
| 5631 | EXPECT_EQ(0, auth_helper4.auth_count()); |
| 5632 | EXPECT_FALSE(auth_helper4.have_result()); |
| 5633 | |
| 5634 | // If the socket is returned to the socket pool, the RespectLimits::DISABLED |
| 5635 | // socket request should be able to claim it. |
| 5636 | auth_helper2.handle()->Reset(); |
| 5637 | EXPECT_THAT(auth_helper4.WaitForResult(), IsOk()); |
| 5638 | EXPECT_EQ(0, auth_helper1.auth_count()); |
| 5639 | EXPECT_FALSE(auth_helper1.have_result()); |
| 5640 | EXPECT_EQ(0, auth_helper3.auth_count()); |
| 5641 | EXPECT_FALSE(auth_helper3.have_result()); |
| 5642 | EXPECT_EQ(0, auth_helper4.auth_count()); |
| 5643 | } |
| 5644 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5645 | enum class RefreshType { |
| 5646 | kServer, |
| 5647 | kProxy, |
| 5648 | }; |
| 5649 | |
| 5650 | // Common base class to test RefreshGroup() when called from either |
| 5651 | // OnSSLConfigForServerChanged() matching a specific group or the pool's proxy. |
| 5652 | // |
| 5653 | // Tests which test behavior specific to one or the other case should use |
| 5654 | // ClientSocketPoolBaseTest directly. In particular, there is no "other group" |
| 5655 | // when the pool's proxy matches. |
| 5656 | class ClientSocketPoolBaseRefreshTest |
| 5657 | : public ClientSocketPoolBaseTest, |
| 5658 | public testing::WithParamInterface<RefreshType> { |
| 5659 | public: |
| 5660 | void CreatePoolForRefresh(int max_sockets, |
| 5661 | int max_sockets_per_group, |
| 5662 | bool enable_backup_connect_jobs = false) { |
| 5663 | switch (GetParam()) { |
| 5664 | case RefreshType::kServer: |
| 5665 | CreatePool(max_sockets, max_sockets_per_group, |
| 5666 | enable_backup_connect_jobs); |
| 5667 | break; |
| 5668 | case RefreshType::kProxy: |
| 5669 | CreatePoolWithIdleTimeouts( |
| 5670 | max_sockets, max_sockets_per_group, kUnusedIdleSocketTimeout, |
| 5671 | ClientSocketPool::used_idle_socket_timeout(), |
| 5672 | enable_backup_connect_jobs, |
Eric Orth | 5ccc3f0 | 2021-09-23 00:01:57 | [diff] [blame] | 5673 | PacResultElementToProxyServer("HTTPS myproxy:70")); |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5674 | break; |
| 5675 | } |
| 5676 | } |
| 5677 | |
| 5678 | static ClientSocketPool::GroupId GetGroupId() { |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame] | 5679 | return TestGroupId("a", 443, url::kHttpsScheme); |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5680 | } |
| 5681 | |
David Benjamin | 6dd7e88 | 2019-10-10 02:35:23 | [diff] [blame] | 5682 | static ClientSocketPool::GroupId GetGroupIdInPartition() { |
| 5683 | // Note this GroupId will match GetGroupId() unless |
| 5684 | // kPartitionConnectionsByNetworkIsolationKey is enabled. |
Matt Menke | 4807a9a | 2020-11-21 00:14:41 | [diff] [blame] | 5685 | const SchemefulSite kSite(GURL("https://b/")); |
| 5686 | const NetworkIsolationKey kNetworkIsolationKey(kSite, kSite); |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame] | 5687 | return TestGroupId("a", 443, url::kHttpsScheme, |
David Benjamin | 6dd7e88 | 2019-10-10 02:35:23 | [diff] [blame] | 5688 | PrivacyMode::PRIVACY_MODE_DISABLED, |
| 5689 | kNetworkIsolationKey); |
| 5690 | } |
| 5691 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5692 | void OnSSLConfigForServerChanged() { |
| 5693 | switch (GetParam()) { |
| 5694 | case RefreshType::kServer: |
| 5695 | pool_->OnSSLConfigForServerChanged(HostPortPair("a", 443)); |
| 5696 | break; |
| 5697 | case RefreshType::kProxy: |
| 5698 | pool_->OnSSLConfigForServerChanged(HostPortPair("myproxy", 70)); |
| 5699 | break; |
| 5700 | } |
| 5701 | } |
| 5702 | }; |
| 5703 | |
| 5704 | INSTANTIATE_TEST_SUITE_P(RefreshType, |
| 5705 | ClientSocketPoolBaseRefreshTest, |
| 5706 | ::testing::Values(RefreshType::kServer, |
| 5707 | RefreshType::kProxy)); |
| 5708 | |
| 5709 | TEST_P(ClientSocketPoolBaseRefreshTest, RefreshGroupCreatesNewConnectJobs) { |
| 5710 | CreatePoolForRefresh(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 5711 | const ClientSocketPool::GroupId kGroupId = GetGroupId(); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5712 | |
| 5713 | // First job will be waiting until it gets aborted. |
| 5714 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 5715 | |
| 5716 | ClientSocketHandle handle; |
| 5717 | TestCompletionCallback callback; |
| 5718 | EXPECT_THAT( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 5719 | handle.Init(kGroupId, params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5720 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5721 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5722 | pool_.get(), NetLogWithSource()), |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5723 | IsError(ERR_IO_PENDING)); |
| 5724 | |
| 5725 | // Switch connect job types, so creating a new ConnectJob will result in |
| 5726 | // success. |
| 5727 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 5728 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5729 | OnSSLConfigForServerChanged(); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5730 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 5731 | ASSERT_TRUE(handle.socket()); |
| 5732 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5733 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId)); |
| 5734 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(kGroupId)); |
| 5735 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(kGroupId)); |
| 5736 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId)); |
| 5737 | } |
| 5738 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5739 | TEST_P(ClientSocketPoolBaseRefreshTest, RefreshGroupClosesIdleConnectJobs) { |
David Benjamin | 6dd7e88 | 2019-10-10 02:35:23 | [diff] [blame] | 5740 | base::test::ScopedFeatureList feature_list; |
| 5741 | feature_list.InitAndEnableFeature( |
| 5742 | features::kPartitionConnectionsByNetworkIsolationKey); |
| 5743 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5744 | CreatePoolForRefresh(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 5745 | const ClientSocketPool::GroupId kGroupId = GetGroupId(); |
David Benjamin | 6dd7e88 | 2019-10-10 02:35:23 | [diff] [blame] | 5746 | const ClientSocketPool::GroupId kGroupIdInPartition = GetGroupIdInPartition(); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5747 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 5748 | pool_->RequestSockets(kGroupId, params_, absl::nullopt, 2, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5749 | NetLogWithSource()); |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 5750 | pool_->RequestSockets(kGroupIdInPartition, params_, absl::nullopt, 2, |
David Benjamin | 6dd7e88 | 2019-10-10 02:35:23 | [diff] [blame] | 5751 | NetLogWithSource()); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5752 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId)); |
David Benjamin | 6dd7e88 | 2019-10-10 02:35:23 | [diff] [blame] | 5753 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupIdInPartition)); |
| 5754 | EXPECT_EQ(4, pool_->IdleSocketCount()); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5755 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(kGroupId)); |
David Benjamin | 6dd7e88 | 2019-10-10 02:35:23 | [diff] [blame] | 5756 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(kGroupIdInPartition)); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5757 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5758 | OnSSLConfigForServerChanged(); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5759 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5760 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupId)); |
David Benjamin | 6dd7e88 | 2019-10-10 02:35:23 | [diff] [blame] | 5761 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupIdInPartition)); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5762 | } |
| 5763 | |
| 5764 | TEST_F(ClientSocketPoolBaseTest, |
| 5765 | RefreshGroupDoesNotCloseIdleConnectJobsInOtherGroup) { |
| 5766 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5767 | const ClientSocketPool::GroupId kGroupId = |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame] | 5768 | TestGroupId("a", 443, url::kHttpsScheme); |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5769 | const ClientSocketPool::GroupId kOtherGroupId = |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame] | 5770 | TestGroupId("b", 443, url::kHttpsScheme); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5771 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 5772 | pool_->RequestSockets(kOtherGroupId, params_, absl::nullopt, 2, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5773 | NetLogWithSource()); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5774 | ASSERT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 5775 | EXPECT_EQ(2, pool_->IdleSocketCount()); |
| 5776 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(kOtherGroupId)); |
| 5777 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5778 | pool_->OnSSLConfigForServerChanged(HostPortPair("a", 443)); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5779 | ASSERT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 5780 | EXPECT_EQ(2, pool_->IdleSocketCount()); |
| 5781 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(kOtherGroupId)); |
| 5782 | } |
| 5783 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5784 | TEST_P(ClientSocketPoolBaseRefreshTest, RefreshGroupPreventsSocketReuse) { |
| 5785 | CreatePoolForRefresh(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 5786 | const ClientSocketPool::GroupId kGroupId = GetGroupId(); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5787 | |
| 5788 | ClientSocketHandle handle; |
| 5789 | TestCompletionCallback callback; |
| 5790 | EXPECT_THAT( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 5791 | handle.Init(kGroupId, params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5792 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5793 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5794 | pool_.get(), NetLogWithSource()), |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5795 | IsOk()); |
| 5796 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId)); |
| 5797 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId)); |
| 5798 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5799 | OnSSLConfigForServerChanged(); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5800 | |
| 5801 | handle.Reset(); |
| 5802 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5803 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupId)); |
| 5804 | } |
| 5805 | |
| 5806 | TEST_F(ClientSocketPoolBaseTest, |
| 5807 | RefreshGroupDoesNotPreventSocketReuseInOtherGroup) { |
| 5808 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5809 | const ClientSocketPool::GroupId kGroupId = |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame] | 5810 | TestGroupId("a", 443, url::kHttpsScheme); |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5811 | const ClientSocketPool::GroupId kOtherGroupId = |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame] | 5812 | TestGroupId("b", 443, url::kHttpsScheme); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5813 | |
| 5814 | ClientSocketHandle handle; |
| 5815 | TestCompletionCallback callback; |
| 5816 | EXPECT_THAT( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 5817 | handle.Init(kOtherGroupId, params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5818 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5819 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5820 | pool_.get(), NetLogWithSource()), |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5821 | IsOk()); |
| 5822 | ASSERT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 5823 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kOtherGroupId)); |
| 5824 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5825 | pool_->OnSSLConfigForServerChanged(HostPortPair("a", 443)); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5826 | |
| 5827 | handle.Reset(); |
| 5828 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 5829 | ASSERT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 5830 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(kOtherGroupId)); |
| 5831 | } |
| 5832 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5833 | TEST_P(ClientSocketPoolBaseRefreshTest, |
| 5834 | RefreshGroupReplacesBoundConnectJobOnConnect) { |
| 5835 | CreatePoolForRefresh(1, 1); |
| 5836 | const ClientSocketPool::GroupId kGroupId = GetGroupId(); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5837 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5838 | |
| 5839 | TestAuthHelper auth_helper; |
| 5840 | auth_helper.InitHandle(params_, pool_.get(), DEFAULT_PRIORITY, |
| 5841 | ClientSocketPool::RespectLimits::ENABLED, kGroupId); |
| 5842 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(kGroupId)); |
| 5843 | |
| 5844 | auth_helper.WaitForAuth(); |
| 5845 | |
| 5846 | // This should update the generation, but not cancel the old ConnectJob - it's |
| 5847 | // not safe to do anything while waiting on the original ConnectJob. |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5848 | OnSSLConfigForServerChanged(); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5849 | |
| 5850 | // Providing auth credentials and restarting the request with them will cause |
| 5851 | // the ConnectJob to complete successfully, but the result will be discarded |
| 5852 | // because of the generation mismatch. |
| 5853 | auth_helper.RestartWithAuth(); |
| 5854 | |
| 5855 | // Despite using ConnectJobs that simulate a single challenge, a second |
| 5856 | // challenge will be seen, due to using a new ConnectJob. |
| 5857 | auth_helper.WaitForAuth(); |
| 5858 | auth_helper.RestartWithAuth(); |
| 5859 | |
| 5860 | EXPECT_THAT(auth_helper.WaitForResult(), IsOk()); |
| 5861 | EXPECT_TRUE(auth_helper.handle()->socket()); |
| 5862 | EXPECT_EQ(2, auth_helper.auth_count()); |
| 5863 | |
| 5864 | // When released, the socket will be returned to the socket pool, and |
| 5865 | // available for reuse. |
| 5866 | auth_helper.handle()->Reset(); |
| 5867 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 5868 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId)); |
| 5869 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(kGroupId)); |
| 5870 | } |
| 5871 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5872 | TEST_F(ClientSocketPoolBaseTest, RefreshProxyRefreshesAllGroups) { |
| 5873 | CreatePoolWithIdleTimeouts(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 5874 | kUnusedIdleSocketTimeout, |
| 5875 | ClientSocketPool::used_idle_socket_timeout(), |
| 5876 | false /* no backup connect jobs */, |
Eric Orth | 5ccc3f0 | 2021-09-23 00:01:57 | [diff] [blame] | 5877 | PacResultElementToProxyServer("HTTPS myproxy:70")); |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5878 | |
| 5879 | const ClientSocketPool::GroupId kGroupId1 = |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame] | 5880 | TestGroupId("a", 443, url::kHttpsScheme); |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5881 | const ClientSocketPool::GroupId kGroupId2 = |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame] | 5882 | TestGroupId("b", 443, url::kHttpsScheme); |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5883 | const ClientSocketPool::GroupId kGroupId3 = |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame] | 5884 | TestGroupId("c", 443, url::kHttpsScheme); |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5885 | |
| 5886 | // Make three sockets in three different groups. The third socket is released |
| 5887 | // to the pool as idle. |
| 5888 | ClientSocketHandle handle1, handle2, handle3; |
| 5889 | TestCompletionCallback callback; |
| 5890 | EXPECT_THAT( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 5891 | handle1.Init(kGroupId1, params_, absl::nullopt, DEFAULT_PRIORITY, |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5892 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5893 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5894 | pool_.get(), NetLogWithSource()), |
| 5895 | IsOk()); |
| 5896 | EXPECT_THAT( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 5897 | handle2.Init(kGroupId2, params_, absl::nullopt, DEFAULT_PRIORITY, |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5898 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5899 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5900 | pool_.get(), NetLogWithSource()), |
| 5901 | IsOk()); |
| 5902 | EXPECT_THAT( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 5903 | handle3.Init(kGroupId3, params_, absl::nullopt, DEFAULT_PRIORITY, |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5904 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5905 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5906 | pool_.get(), NetLogWithSource()), |
| 5907 | IsOk()); |
| 5908 | handle3.Reset(); |
| 5909 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId1)); |
| 5910 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId1)); |
| 5911 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId2)); |
| 5912 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId2)); |
| 5913 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId3)); |
| 5914 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(kGroupId3)); |
| 5915 | |
| 5916 | // Changes to some other proxy do not affect the pool. The idle socket remains |
| 5917 | // alive and closing |handle2| makes the socket available for the pool. |
| 5918 | pool_->OnSSLConfigForServerChanged(HostPortPair("someotherproxy", 70)); |
| 5919 | |
| 5920 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId1)); |
| 5921 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId1)); |
| 5922 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId2)); |
| 5923 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId2)); |
| 5924 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId3)); |
| 5925 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(kGroupId3)); |
| 5926 | |
| 5927 | handle2.Reset(); |
| 5928 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId2)); |
| 5929 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(kGroupId2)); |
| 5930 | |
| 5931 | // Changes to the matching proxy refreshes all groups. |
| 5932 | pool_->OnSSLConfigForServerChanged(HostPortPair("myproxy", 70)); |
| 5933 | |
| 5934 | // Idle sockets are closed. |
| 5935 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5936 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupId2)); |
| 5937 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupId3)); |
| 5938 | |
| 5939 | // The active socket, however, continues to be active. |
| 5940 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId1)); |
| 5941 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId1)); |
| 5942 | |
| 5943 | // Closing it does not make it available for the pool. |
| 5944 | handle1.Reset(); |
| 5945 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5946 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupId1)); |
| 5947 | } |
| 5948 | |
| 5949 | TEST_F(ClientSocketPoolBaseTest, RefreshBothPrivacyAndNormalSockets) { |
| 5950 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 5951 | |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame] | 5952 | const ClientSocketPool::GroupId kGroupId = TestGroupId( |
| 5953 | "a", 443, url::kHttpsScheme, PrivacyMode::PRIVACY_MODE_DISABLED); |
| 5954 | const ClientSocketPool::GroupId kGroupIdPrivacy = TestGroupId( |
| 5955 | "a", 443, url::kHttpsScheme, PrivacyMode::PRIVACY_MODE_ENABLED); |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5956 | const ClientSocketPool::GroupId kOtherGroupId = |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame] | 5957 | TestGroupId("b", 443, url::kHttpsScheme); |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5958 | |
| 5959 | // Make a socket in each groups. |
| 5960 | ClientSocketHandle handle1, handle2, handle3; |
| 5961 | TestCompletionCallback callback; |
| 5962 | EXPECT_THAT( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 5963 | handle1.Init(kGroupId, params_, absl::nullopt, DEFAULT_PRIORITY, |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5964 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5965 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5966 | pool_.get(), NetLogWithSource()), |
| 5967 | IsOk()); |
| 5968 | EXPECT_THAT( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 5969 | handle2.Init(kGroupIdPrivacy, params_, absl::nullopt, DEFAULT_PRIORITY, |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5970 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5971 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5972 | pool_.get(), NetLogWithSource()), |
| 5973 | IsOk()); |
| 5974 | EXPECT_THAT( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 5975 | handle3.Init(kOtherGroupId, params_, absl::nullopt, DEFAULT_PRIORITY, |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5976 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5977 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5978 | pool_.get(), NetLogWithSource()), |
| 5979 | IsOk()); |
| 5980 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId)); |
| 5981 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId)); |
| 5982 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupIdPrivacy)); |
| 5983 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupIdPrivacy)); |
| 5984 | ASSERT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 5985 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kOtherGroupId)); |
| 5986 | |
| 5987 | pool_->OnSSLConfigForServerChanged(HostPortPair("a", 443)); |
| 5988 | |
| 5989 | // Active sockets continue to be active. |
| 5990 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId)); |
| 5991 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId)); |
| 5992 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupIdPrivacy)); |
| 5993 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupIdPrivacy)); |
| 5994 | ASSERT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 5995 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kOtherGroupId)); |
| 5996 | |
| 5997 | // Closing them leaves kOtherGroupId alone, but kGroupId and kGroupIdPrivacy |
| 5998 | // are unusable. |
| 5999 | handle1.Reset(); |
| 6000 | handle2.Reset(); |
| 6001 | handle3.Reset(); |
| 6002 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 6003 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupId)); |
| 6004 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupIdPrivacy)); |
| 6005 | EXPECT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 6006 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(kOtherGroupId)); |
| 6007 | } |
| 6008 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 6009 | } // namespace |
| 6010 | |
| 6011 | } // namespace net |