[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" |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 21 | #include "base/single_thread_task_runner.h" |
[email protected] | fc9be580 | 2013-06-11 10:56:51 | [diff] [blame] | 22 | #include "base/strings/string_number_conversions.h" |
[email protected] | 18b57741 | 2013-07-18 04:19:15 | [diff] [blame] | 23 | #include "base/strings/stringprintf.h" |
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" |
[email protected] | ac790b4 | 2009-12-02 04:31:31 | [diff] [blame] | 36 | #include "net/base/request_priority.h" |
Matt Menke | 4807a9a | 2020-11-21 00:14:41 | [diff] [blame] | 37 | #include "net/base/schemeful_site.h" |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 38 | #include "net/base/test_completion_callback.h" |
dalyk | edd30d98 | 2019-12-16 15:31:10 | [diff] [blame] | 39 | #include "net/dns/public/resolve_error_info.h" |
Ben Schwartz | 3ff4dc1e6 | 2021-04-27 21:15:23 | [diff] [blame] | 40 | #include "net/dns/public/secure_dns_policy.h" |
[email protected] | 277d594 | 2010-08-11 21:02:35 | [diff] [blame] | 41 | #include "net/http/http_response_headers.h" |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 42 | #include "net/http/http_response_info.h" |
eroman | 87c53d6 | 2015-04-02 06:51:07 | [diff] [blame] | 43 | #include "net/log/net_log.h" |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 44 | #include "net/log/net_log_event_type.h" |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 45 | #include "net/log/net_log_source.h" |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 46 | #include "net/log/net_log_source_type.h" |
mmenke | 16a7cbdd | 2015-04-24 23:00:56 | [diff] [blame] | 47 | #include "net/log/test_net_log.h" |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 48 | #include "net/log/test_net_log_util.h" |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 49 | #include "net/socket/client_socket_factory.h" |
| 50 | #include "net/socket/client_socket_handle.h" |
tfarina | 5dd13c2 | 2016-11-16 12:08:26 | [diff] [blame] | 51 | #include "net/socket/datagram_client_socket.h" |
tbansal | ca83c00 | 2016-04-28 20:56:28 | [diff] [blame] | 52 | #include "net/socket/socket_performance_watcher.h" |
Paul Jensen | 8d6f87ec | 2018-01-13 00:46:54 | [diff] [blame] | 53 | #include "net/socket/socket_tag.h" |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 54 | #include "net/socket/socket_test_util.h" |
[email protected] | 18ccfdb | 2013-08-15 00:13:44 | [diff] [blame] | 55 | #include "net/socket/ssl_client_socket.h" |
[email protected] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame] | 56 | #include "net/socket/stream_socket.h" |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 57 | #include "net/socket/transport_connect_job.h" |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 58 | #include "net/ssl/ssl_cert_request_info.h" |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 59 | #include "net/test/gtest_util.h" |
Gabriel Charette | c710874 | 2019-08-23 03:31:40 | [diff] [blame] | 60 | #include "net/test/test_with_task_environment.h" |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 61 | #include "net/traffic_annotation/network_traffic_annotation.h" |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 62 | #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 63 | #include "testing/gmock/include/gmock/gmock.h" |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 64 | #include "testing/gtest/include/gtest/gtest.h" |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 65 | #include "third_party/abseil-cpp/absl/types/optional.h" |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame^] | 66 | #include "url/scheme_host_port.h" |
| 67 | #include "url/url_constants.h" |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 68 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 69 | using net::test::IsError; |
| 70 | using net::test::IsOk; |
| 71 | |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 72 | using ::testing::Invoke; |
| 73 | using ::testing::Return; |
| 74 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 75 | namespace net { |
| 76 | |
| 77 | namespace { |
| 78 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 79 | const int kDefaultMaxSockets = 4; |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 80 | const int kDefaultMaxSocketsPerGroup = 2; |
Tarun Bansal | a763509 | 2019-02-20 10:00:59 | [diff] [blame] | 81 | constexpr base::TimeDelta kUnusedIdleSocketTimeout = |
| 82 | base::TimeDelta::FromSeconds(10); |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 83 | |
Matt Menke | bdf77780 | 2019-04-22 19:38:59 | [diff] [blame] | 84 | ClientSocketPool::GroupId TestGroupId( |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame^] | 85 | base::StringPiece host, |
Matt Menke | bdf77780 | 2019-04-22 19:38:59 | [diff] [blame] | 86 | int port = 80, |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame^] | 87 | base::StringPiece scheme = url::kHttpScheme, |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 88 | PrivacyMode privacy_mode = PrivacyMode::PRIVACY_MODE_DISABLED, |
| 89 | NetworkIsolationKey network_isolation_key = NetworkIsolationKey()) { |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame^] | 90 | return ClientSocketPool::GroupId(url::SchemeHostPort(scheme, host, port), |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 91 | privacy_mode, network_isolation_key, |
Ben Schwartz | 3ff4dc1e6 | 2021-04-27 21:15:23 | [diff] [blame] | 92 | SecureDnsPolicy::kAllow); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 93 | } |
| 94 | |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 95 | // Make sure |handle| sets load times correctly when it has been assigned a |
| 96 | // reused socket. |
| 97 | void TestLoadTimingInfoConnectedReused(const ClientSocketHandle& handle) { |
| 98 | LoadTimingInfo load_timing_info; |
| 99 | // Only pass true in as |is_reused|, as in general, HttpStream types should |
| 100 | // have stricter concepts of reuse than socket pools. |
| 101 | EXPECT_TRUE(handle.GetLoadTimingInfo(true, &load_timing_info)); |
| 102 | |
| 103 | EXPECT_EQ(true, load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 104 | EXPECT_NE(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 105 | |
[email protected] | b258e079 | 2013-01-12 07:11:59 | [diff] [blame] | 106 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 107 | ExpectLoadTimingHasOnlyConnectionTimes(load_timing_info); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | // Make sure |handle| sets load times correctly when it has been assigned a |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 111 | // fresh socket. Also runs TestLoadTimingInfoConnectedReused, since the owner |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 112 | // of a connection where |is_reused| is false may consider the connection |
| 113 | // reused. |
| 114 | void TestLoadTimingInfoConnectedNotReused(const ClientSocketHandle& handle) { |
| 115 | EXPECT_FALSE(handle.is_reused()); |
| 116 | |
| 117 | LoadTimingInfo load_timing_info; |
| 118 | EXPECT_TRUE(handle.GetLoadTimingInfo(false, &load_timing_info)); |
| 119 | |
| 120 | EXPECT_FALSE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 121 | EXPECT_NE(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 122 | |
[email protected] | b258e079 | 2013-01-12 07:11:59 | [diff] [blame] | 123 | ExpectConnectTimingHasTimes(load_timing_info.connect_timing, |
| 124 | CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY); |
| 125 | ExpectLoadTimingHasOnlyConnectionTimes(load_timing_info); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 126 | |
| 127 | TestLoadTimingInfoConnectedReused(handle); |
| 128 | } |
| 129 | |
| 130 | // Make sure |handle| sets load times correctly, in the case that it does not |
| 131 | // currently have a socket. |
| 132 | void TestLoadTimingInfoNotConnected(const ClientSocketHandle& handle) { |
| 133 | // Should only be set to true once a socket is assigned, if at all. |
| 134 | EXPECT_FALSE(handle.is_reused()); |
| 135 | |
| 136 | LoadTimingInfo load_timing_info; |
| 137 | EXPECT_FALSE(handle.GetLoadTimingInfo(false, &load_timing_info)); |
| 138 | |
| 139 | EXPECT_FALSE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 140 | EXPECT_EQ(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 141 | |
[email protected] | b258e079 | 2013-01-12 07:11:59 | [diff] [blame] | 142 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 143 | ExpectLoadTimingHasOnlyConnectionTimes(load_timing_info); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 144 | } |
| 145 | |
[email protected] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame] | 146 | class MockClientSocket : public StreamSocket { |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 147 | public: |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 148 | explicit MockClientSocket(net::NetLog* net_log) |
| 149 | : connected_(false), |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 150 | has_unread_data_(false), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 151 | net_log_(NetLogWithSource::Make(net_log, NetLogSourceType::SOCKET)), |
Charlie Harrison | 3e4c062 | 2018-05-13 15:44:30 | [diff] [blame] | 152 | was_used_to_convey_data_(false) {} |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 153 | |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 154 | // Sets whether the socket has unread data. If true, the next call to Read() |
| 155 | // will return 1 byte and IsConnectedAndIdle() will return false. |
| 156 | void set_has_unread_data(bool has_unread_data) { |
| 157 | has_unread_data_ = has_unread_data; |
| 158 | } |
| 159 | |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 160 | // Socket implementation. |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 161 | int Read(IOBuffer* /* buf */, |
| 162 | int len, |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 163 | CompletionOnceCallback /* callback */) override { |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 164 | if (has_unread_data_ && len > 0) { |
| 165 | has_unread_data_ = false; |
| 166 | was_used_to_convey_data_ = true; |
| 167 | return 1; |
| 168 | } |
[email protected] | e86df8dc | 2013-03-30 13:18:28 | [diff] [blame] | 169 | return ERR_UNEXPECTED; |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 170 | } |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 171 | |
[email protected] | a2b2cfc | 2017-12-06 09:06:08 | [diff] [blame] | 172 | int Write( |
| 173 | IOBuffer* /* buf */, |
| 174 | int len, |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 175 | CompletionOnceCallback /* callback */, |
[email protected] | a2b2cfc | 2017-12-06 09:06:08 | [diff] [blame] | 176 | const NetworkTrafficAnnotationTag& /*traffic_annotation*/) override { |
[email protected] | 0f873e8 | 2010-09-02 16:09:01 | [diff] [blame] | 177 | was_used_to_convey_data_ = true; |
| 178 | return len; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 179 | } |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 180 | int SetReceiveBufferSize(int32_t size) override { return OK; } |
| 181 | int SetSendBufferSize(int32_t size) override { return OK; } |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 182 | |
[email protected] | dbf036f | 2011-12-06 23:33:24 | [diff] [blame] | 183 | // StreamSocket implementation. |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 184 | int Connect(CompletionOnceCallback callback) override { |
[email protected] | dbf036f | 2011-12-06 23:33:24 | [diff] [blame] | 185 | connected_ = true; |
| 186 | return OK; |
| 187 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 188 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 189 | void Disconnect() override { connected_ = false; } |
| 190 | bool IsConnected() const override { return connected_; } |
| 191 | bool IsConnectedAndIdle() const override { |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 192 | return connected_ && !has_unread_data_; |
| 193 | } |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 194 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 195 | int GetPeerAddress(IPEndPoint* /* address */) const override { |
[email protected] | 9f864b3 | 2010-01-20 15:01:16 | [diff] [blame] | 196 | return ERR_UNEXPECTED; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 197 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 198 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 199 | int GetLocalAddress(IPEndPoint* /* address */) const override { |
[email protected] | e7f74da | 2011-04-19 23:49:35 | [diff] [blame] | 200 | return ERR_UNEXPECTED; |
| 201 | } |
| 202 | |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 203 | const NetLogWithSource& NetLog() const override { return net_log_; } |
[email protected] | a2006ece | 2010-04-23 16:44:02 | [diff] [blame] | 204 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 205 | bool WasEverUsed() const override { return was_used_to_convey_data_; } |
tfarina | 2846404c | 2016-12-25 14:31:37 | [diff] [blame] | 206 | bool WasAlpnNegotiated() const override { return false; } |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 207 | NextProto GetNegotiatedProtocol() const override { return kProtoUnknown; } |
| 208 | bool GetSSLInfo(SSLInfo* ssl_info) override { return false; } |
ttuttle | 23fdb7b | 2015-05-15 01:28:03 | [diff] [blame] | 209 | void GetConnectionAttempts(ConnectionAttempts* out) const override { |
| 210 | out->clear(); |
| 211 | } |
| 212 | void ClearConnectionAttempts() override {} |
| 213 | void AddConnectionAttempts(const ConnectionAttempts& attempts) override {} |
tbansal | f82cc8e | 2015-10-14 20:05:49 | [diff] [blame] | 214 | int64_t GetTotalReceivedBytes() const override { |
| 215 | NOTIMPLEMENTED(); |
| 216 | return 0; |
| 217 | } |
Paul Jensen | 0f49dec | 2017-12-12 23:39:58 | [diff] [blame] | 218 | void ApplySocketTag(const SocketTag& tag) override {} |
[email protected] | 9b5614a | 2010-08-25 20:29:45 | [diff] [blame] | 219 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 220 | private: |
| 221 | bool connected_; |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 222 | bool has_unread_data_; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 223 | NetLogWithSource net_log_; |
[email protected] | 0f873e8 | 2010-09-02 16:09:01 | [diff] [blame] | 224 | bool was_used_to_convey_data_; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 225 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 226 | DISALLOW_COPY_AND_ASSIGN(MockClientSocket); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 227 | }; |
| 228 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 229 | class TestConnectJob; |
| 230 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 231 | class MockClientSocketFactory : public ClientSocketFactory { |
| 232 | public: |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 233 | MockClientSocketFactory() : allocation_count_(0) {} |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 234 | |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 235 | std::unique_ptr<DatagramClientSocket> CreateDatagramClientSocket( |
[email protected] | 5370c01 | 2011-06-29 03:47:04 | [diff] [blame] | 236 | DatagramSocket::BindType bind_type, |
[email protected] | 98b0e58 | 2011-06-22 14:31:41 | [diff] [blame] | 237 | NetLog* net_log, |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 238 | const NetLogSource& source) override { |
[email protected] | 98b0e58 | 2011-06-22 14:31:41 | [diff] [blame] | 239 | NOTREACHED(); |
David Benjamin | 24725be | 2019-07-24 20:57:18 | [diff] [blame] | 240 | return nullptr; |
[email protected] | 98b0e58 | 2011-06-22 14:31:41 | [diff] [blame] | 241 | } |
| 242 | |
Helen Li | d5bb922 | 2018-04-12 15:33:09 | [diff] [blame] | 243 | std::unique_ptr<TransportClientSocket> CreateTransportClientSocket( |
[email protected] | 0a0b768 | 2010-08-25 17:08:07 | [diff] [blame] | 244 | const AddressList& addresses, |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 245 | std::unique_ptr< |
| 246 | SocketPerformanceWatcher> /* socket_performance_watcher */, |
Eric Roman | 2bc7716 | 2020-09-16 18:30:45 | [diff] [blame] | 247 | NetworkQualityEstimator* /* network_quality_estimator */, |
[email protected] | 0a0b768 | 2010-08-25 17:08:07 | [diff] [blame] | 248 | NetLog* /* net_log */, |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 249 | const NetLogSource& /*source*/) override { |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 250 | allocation_count_++; |
Helen Li | d5bb922 | 2018-04-12 15:33:09 | [diff] [blame] | 251 | return nullptr; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 252 | } |
| 253 | |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 254 | std::unique_ptr<SSLClientSocket> CreateSSLClientSocket( |
David Benjamin | 24725be | 2019-07-24 20:57:18 | [diff] [blame] | 255 | SSLClientContext* context, |
Matt Menke | 841fc41 | 2019-03-05 23:20:12 | [diff] [blame] | 256 | std::unique_ptr<StreamSocket> stream_socket, |
[email protected] | 4f4de7e6 | 2010-11-12 19:55:27 | [diff] [blame] | 257 | const HostPortPair& host_and_port, |
David Benjamin | 24725be | 2019-07-24 20:57:18 | [diff] [blame] | 258 | const SSLConfig& ssl_config) override { |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 259 | NOTIMPLEMENTED(); |
David Benjamin | 24725be | 2019-07-24 20:57:18 | [diff] [blame] | 260 | return nullptr; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 261 | } |
Matt Menke | fd95692 | 2019-02-04 23:44:03 | [diff] [blame] | 262 | |
Matt Menke | 52cd95a | 2019-02-08 06:16:27 | [diff] [blame] | 263 | std::unique_ptr<ProxyClientSocket> CreateProxyClientSocket( |
| 264 | std::unique_ptr<StreamSocket> stream_socket, |
| 265 | const std::string& user_agent, |
| 266 | const HostPortPair& endpoint, |
| 267 | const ProxyServer& proxy_server, |
| 268 | HttpAuthController* http_auth_controller, |
| 269 | bool tunnel, |
| 270 | bool using_spdy, |
| 271 | NextProto negotiated_protocol, |
| 272 | ProxyDelegate* proxy_delegate, |
Matt Menke | 52cd95a | 2019-02-08 06:16:27 | [diff] [blame] | 273 | const NetworkTrafficAnnotationTag& traffic_annotation) override { |
| 274 | NOTIMPLEMENTED(); |
| 275 | return nullptr; |
| 276 | } |
| 277 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 278 | void WaitForSignal(TestConnectJob* job) { waiting_jobs_.push_back(job); } |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 279 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 280 | void SignalJobs(); |
| 281 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 282 | void SignalJob(size_t job); |
| 283 | |
| 284 | void SetJobLoadState(size_t job, LoadState load_state); |
| 285 | |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 286 | // Sets the HasConnectionEstablished value of the specified job to true, |
| 287 | // without invoking the callback. |
| 288 | void SetJobHasEstablishedConnection(size_t job); |
| 289 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 290 | int allocation_count() const { return allocation_count_; } |
| 291 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 292 | private: |
| 293 | int allocation_count_; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 294 | std::vector<TestConnectJob*> waiting_jobs_; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 295 | }; |
| 296 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 297 | class TestConnectJob : public ConnectJob { |
| 298 | public: |
| 299 | enum JobType { |
| 300 | kMockJob, |
| 301 | kMockFailingJob, |
| 302 | kMockPendingJob, |
| 303 | kMockPendingFailingJob, |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 304 | kMockWaitingJob, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 305 | |
| 306 | // Certificate errors return a socket in addition to an error code. |
| 307 | kMockCertErrorJob, |
| 308 | kMockPendingCertErrorJob, |
| 309 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 310 | kMockAdditionalErrorStateJob, |
| 311 | kMockPendingAdditionalErrorStateJob, |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 312 | kMockUnreadDataJob, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 313 | |
| 314 | kMockAuthChallengeOnceJob, |
| 315 | kMockAuthChallengeTwiceJob, |
| 316 | kMockAuthChallengeOnceFailingJob, |
| 317 | kMockAuthChallengeTwiceFailingJob, |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 318 | }; |
| 319 | |
[email protected] | 994d493 | 2010-07-12 17:55:13 | [diff] [blame] | 320 | // The kMockPendingJob uses a slight delay before allowing the connect |
| 321 | // to complete. |
| 322 | static const int kPendingConnectDelay = 2; |
| 323 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 324 | TestConnectJob(JobType job_type, |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 325 | RequestPriority request_priority, |
| 326 | SocketTag socket_tag, |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 327 | base::TimeDelta timeout_duration, |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 328 | const CommonConnectJobParams* common_connect_job_params, |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 329 | ConnectJob::Delegate* delegate, |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 330 | MockClientSocketFactory* client_socket_factory) |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 331 | : ConnectJob(request_priority, |
| 332 | socket_tag, |
Matt Menke | 1a6c92d | 2019-02-23 00:25:38 | [diff] [blame] | 333 | timeout_duration, |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 334 | common_connect_job_params, |
Matt Menke | 1a6c92d | 2019-02-23 00:25:38 | [diff] [blame] | 335 | delegate, |
| 336 | nullptr /* net_log */, |
| 337 | NetLogSourceType::TRANSPORT_CONNECT_JOB, |
| 338 | NetLogEventType::TRANSPORT_CONNECT_JOB_CONNECT), |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 339 | job_type_(job_type), |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 340 | client_socket_factory_(client_socket_factory), |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 341 | load_state_(LOAD_STATE_IDLE), |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 342 | has_established_connection_(false), |
Jeremy Roman | d54000b2 | 2019-07-08 18:40:16 | [diff] [blame] | 343 | store_additional_error_state_(false) {} |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 344 | |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 345 | void Signal() { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 346 | DoConnect(waiting_success_, true /* async */, false /* recoverable */); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 347 | } |
| 348 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 349 | void set_load_state(LoadState load_state) { load_state_ = load_state; } |
| 350 | |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 351 | void set_has_established_connection() { |
| 352 | DCHECK(!has_established_connection_); |
| 353 | has_established_connection_ = true; |
| 354 | } |
| 355 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 356 | // From ConnectJob: |
| 357 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 358 | LoadState GetLoadState() const override { return load_state_; } |
[email protected] | 4645135 | 2009-09-01 14:54:21 | [diff] [blame] | 359 | |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 360 | bool HasEstablishedConnection() const override { |
| 361 | return has_established_connection_; |
| 362 | } |
| 363 | |
dalyk | edd30d98 | 2019-12-16 15:31:10 | [diff] [blame] | 364 | ResolveErrorInfo GetResolveErrorInfo() const override { |
| 365 | return ResolveErrorInfo(OK); |
| 366 | } |
| 367 | |
Matt Menke | 6f84d1f1 | 2019-04-11 19:26:47 | [diff] [blame] | 368 | bool IsSSLError() const override { return store_additional_error_state_; } |
| 369 | |
| 370 | scoped_refptr<SSLCertRequestInfo> GetCertRequestInfo() override { |
| 371 | if (store_additional_error_state_) |
| 372 | return base::MakeRefCounted<SSLCertRequestInfo>(); |
| 373 | return nullptr; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 374 | } |
| 375 | |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 376 | private: |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 377 | // From ConnectJob: |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 378 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 379 | int ConnectInternal() override { |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 380 | AddressList ignored; |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 381 | client_socket_factory_->CreateTransportClientSocket( |
Eric Roman | 2bc7716 | 2020-09-16 18:30:45 | [diff] [blame] | 382 | ignored, nullptr, nullptr, nullptr, NetLogSource()); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 383 | switch (job_type_) { |
| 384 | case kMockJob: |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 385 | return DoConnect(true /* successful */, false /* sync */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 386 | false /* cert_error */); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 387 | case kMockFailingJob: |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 388 | return DoConnect(false /* error */, false /* sync */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 389 | false /* cert_error */); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 390 | case kMockPendingJob: |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 391 | set_load_state(LOAD_STATE_CONNECTING); |
[email protected] | 6b17538 | 2009-10-13 06:47:47 | [diff] [blame] | 392 | |
| 393 | // Depending on execution timings, posting a delayed task can result |
| 394 | // in the task getting executed the at the earliest possible |
| 395 | // opportunity or only after returning once from the message loop and |
| 396 | // then a second call into the message loop. In order to make behavior |
| 397 | // more deterministic, we change the default delay to 2ms. This should |
| 398 | // always require us to wait for the second call into the message loop. |
| 399 | // |
| 400 | // N.B. The correct fix for this and similar timing problems is to |
| 401 | // abstract time for the purpose of unittests. Unfortunately, we have |
| 402 | // a lot of third-party components that directly call the various |
| 403 | // time functions, so this change would be rather invasive. |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 404 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 405 | FROM_HERE, |
kylechar | f4fe517 | 2019-02-15 18:53:49 | [diff] [blame] | 406 | base::BindOnce(base::IgnoreResult(&TestConnectJob::DoConnect), |
| 407 | weak_factory_.GetWeakPtr(), true /* successful */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 408 | true /* async */, false /* cert_error */), |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 409 | base::TimeDelta::FromMilliseconds(kPendingConnectDelay)); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 410 | return ERR_IO_PENDING; |
| 411 | case kMockPendingFailingJob: |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 412 | set_load_state(LOAD_STATE_CONNECTING); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 413 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 414 | FROM_HERE, |
kylechar | f4fe517 | 2019-02-15 18:53:49 | [diff] [blame] | 415 | base::BindOnce(base::IgnoreResult(&TestConnectJob::DoConnect), |
| 416 | weak_factory_.GetWeakPtr(), false /* error */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 417 | true /* async */, false /* cert_error */), |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 418 | base::TimeDelta::FromMilliseconds(2)); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 419 | return ERR_IO_PENDING; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 420 | case kMockWaitingJob: |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 421 | set_load_state(LOAD_STATE_CONNECTING); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 422 | client_socket_factory_->WaitForSignal(this); |
| 423 | waiting_success_ = true; |
| 424 | return ERR_IO_PENDING; |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 425 | case kMockCertErrorJob: |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 426 | return DoConnect(false /* error */, false /* sync */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 427 | true /* cert_error */); |
| 428 | case kMockPendingCertErrorJob: |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 429 | set_load_state(LOAD_STATE_CONNECTING); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 430 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 431 | FROM_HERE, |
kylechar | f4fe517 | 2019-02-15 18:53:49 | [diff] [blame] | 432 | base::BindOnce(base::IgnoreResult(&TestConnectJob::DoConnect), |
| 433 | weak_factory_.GetWeakPtr(), false /* error */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 434 | true /* async */, true /* cert_error */), |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 435 | base::TimeDelta::FromMilliseconds(2)); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 436 | return ERR_IO_PENDING; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 437 | case kMockAdditionalErrorStateJob: |
| 438 | store_additional_error_state_ = true; |
| 439 | return DoConnect(false /* error */, false /* sync */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 440 | false /* cert_error */); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 441 | case kMockPendingAdditionalErrorStateJob: |
| 442 | set_load_state(LOAD_STATE_CONNECTING); |
| 443 | store_additional_error_state_ = true; |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 444 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 445 | FROM_HERE, |
kylechar | f4fe517 | 2019-02-15 18:53:49 | [diff] [blame] | 446 | base::BindOnce(base::IgnoreResult(&TestConnectJob::DoConnect), |
| 447 | weak_factory_.GetWeakPtr(), false /* error */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 448 | true /* async */, false /* cert_error */), |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 449 | base::TimeDelta::FromMilliseconds(2)); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 450 | return ERR_IO_PENDING; |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 451 | case kMockUnreadDataJob: { |
| 452 | int ret = DoConnect(true /* successful */, false /* sync */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 453 | false /* cert_error */); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 454 | static_cast<MockClientSocket*>(socket())->set_has_unread_data(true); |
| 455 | return ret; |
| 456 | } |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 457 | case kMockAuthChallengeOnceJob: |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 458 | set_load_state(LOAD_STATE_CONNECTING); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 459 | DoAdvanceAuthChallenge(1, true /* succeed_after_last_challenge */); |
| 460 | return ERR_IO_PENDING; |
| 461 | case kMockAuthChallengeTwiceJob: |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 462 | set_load_state(LOAD_STATE_CONNECTING); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 463 | DoAdvanceAuthChallenge(2, true /* succeed_after_last_challenge */); |
| 464 | return ERR_IO_PENDING; |
| 465 | case kMockAuthChallengeOnceFailingJob: |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 466 | set_load_state(LOAD_STATE_CONNECTING); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 467 | DoAdvanceAuthChallenge(1, false /* succeed_after_last_challenge */); |
| 468 | return ERR_IO_PENDING; |
| 469 | case kMockAuthChallengeTwiceFailingJob: |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 470 | set_load_state(LOAD_STATE_CONNECTING); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 471 | DoAdvanceAuthChallenge(2, false /* succeed_after_last_challenge */); |
| 472 | return ERR_IO_PENDING; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 473 | default: |
| 474 | NOTREACHED(); |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 475 | SetSocket(std::unique_ptr<StreamSocket>(), absl::nullopt); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 476 | return ERR_FAILED; |
| 477 | } |
| 478 | } |
| 479 | |
Lily Chen | 02ef29a | 2018-11-30 16:31:43 | [diff] [blame] | 480 | void ChangePriorityInternal(RequestPriority priority) override {} |
| 481 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 482 | int DoConnect(bool succeed, bool was_async, bool cert_error) { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 483 | int result = OK; |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 484 | has_established_connection_ = true; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 485 | if (succeed) { |
Cammie Smith Barnes | aa2a8b5 | 2020-12-17 19:33:19 | [diff] [blame] | 486 | SetSocket(std::make_unique<MockClientSocket>(net_log().net_log()), |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 487 | absl::nullopt); |
Bence Béky | bdbb0e7 | 2018-08-07 21:42:59 | [diff] [blame] | 488 | socket()->Connect(CompletionOnceCallback()); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 489 | } else if (cert_error) { |
Cammie Smith Barnes | aa2a8b5 | 2020-12-17 19:33:19 | [diff] [blame] | 490 | SetSocket(std::make_unique<MockClientSocket>(net_log().net_log()), |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 491 | absl::nullopt); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 492 | result = ERR_CERT_COMMON_NAME_INVALID; |
[email protected] | 6e713f0 | 2009-08-06 02:56:40 | [diff] [blame] | 493 | } else { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 494 | result = ERR_CONNECTION_FAILED; |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 495 | SetSocket(std::unique_ptr<StreamSocket>(), absl::nullopt); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 496 | } |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 497 | |
| 498 | if (was_async) |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 499 | NotifyDelegateOfCompletion(result); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 500 | return result; |
| 501 | } |
| 502 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 503 | void DoAdvanceAuthChallenge(int remaining_challenges, |
| 504 | bool succeed_after_last_challenge) { |
| 505 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 506 | FROM_HERE, |
| 507 | base::BindOnce(&TestConnectJob::InvokeNextProxyAuthCallback, |
| 508 | weak_factory_.GetWeakPtr(), remaining_challenges, |
| 509 | succeed_after_last_challenge)); |
| 510 | } |
| 511 | |
| 512 | void InvokeNextProxyAuthCallback(int remaining_challenges, |
| 513 | bool succeed_after_last_challenge) { |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 514 | set_load_state(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 515 | if (remaining_challenges == 0) { |
| 516 | DoConnect(succeed_after_last_challenge, true /* was_async */, |
| 517 | false /* cert_error */); |
| 518 | return; |
| 519 | } |
| 520 | |
| 521 | // Integration tests make sure HttpResponseInfo and HttpAuthController work. |
| 522 | // The auth tests here are just focused on ConnectJob bookkeeping. |
| 523 | HttpResponseInfo info; |
| 524 | NotifyDelegateOfProxyAuth( |
| 525 | info, nullptr /* http_auth_controller */, |
| 526 | base::BindOnce(&TestConnectJob::DoAdvanceAuthChallenge, |
| 527 | weak_factory_.GetWeakPtr(), remaining_challenges - 1, |
| 528 | succeed_after_last_challenge)); |
| 529 | } |
| 530 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 531 | bool waiting_success_; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 532 | const JobType job_type_; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 533 | MockClientSocketFactory* const client_socket_factory_; |
[email protected] | 4645135 | 2009-09-01 14:54:21 | [diff] [blame] | 534 | LoadState load_state_; |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 535 | bool has_established_connection_; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 536 | bool store_additional_error_state_; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 537 | |
Jeremy Roman | d54000b2 | 2019-07-08 18:40:16 | [diff] [blame] | 538 | base::WeakPtrFactory<TestConnectJob> weak_factory_{this}; |
[email protected] | d5492c5 | 2013-11-10 20:44:39 | [diff] [blame] | 539 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 540 | DISALLOW_COPY_AND_ASSIGN(TestConnectJob); |
| 541 | }; |
| 542 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 543 | class TestConnectJobFactory |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 544 | : public TransportClientSocketPool::ConnectJobFactory { |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 545 | public: |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 546 | TestConnectJobFactory(MockClientSocketFactory* client_socket_factory, |
| 547 | NetLog* net_log) |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 548 | : common_connect_job_params_( |
| 549 | nullptr /* client_socket_factory */, |
| 550 | nullptr /* host_resolver */, |
Matt Menke | b88837e | 2019-03-20 11:50:40 | [diff] [blame] | 551 | nullptr /* http_auth_cache */, |
| 552 | nullptr /* http_auth_handler_factory */, |
| 553 | nullptr /* spdy_session_pool */, |
Matt Menke | b5fb42b | 2019-03-22 17:26:13 | [diff] [blame] | 554 | nullptr /* quic_supported_versions */, |
Matt Menke | b88837e | 2019-03-20 11:50:40 | [diff] [blame] | 555 | nullptr /* quic_stream_factory */, |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 556 | nullptr /* proxy_delegate */, |
Matt Menke | d732ea4 | 2019-03-08 12:05:00 | [diff] [blame] | 557 | nullptr /* http_user_agent_settings */, |
David Benjamin | 24725be | 2019-07-24 20:57:18 | [diff] [blame] | 558 | nullptr /* ssl_client_context */, |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 559 | nullptr /* socket_performance_watcher_factory */, |
| 560 | nullptr /* network_quality_estimator */, |
| 561 | net_log, |
| 562 | nullptr /* websocket_endpoint_lock_manager */), |
| 563 | job_type_(TestConnectJob::kMockJob), |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 564 | job_types_(nullptr), |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 565 | client_socket_factory_(client_socket_factory) {} |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 566 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 567 | ~TestConnectJobFactory() override = default; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 568 | |
| 569 | void set_job_type(TestConnectJob::JobType job_type) { job_type_ = job_type; } |
| 570 | |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 571 | void set_job_types(std::list<TestConnectJob::JobType>* job_types) { |
| 572 | job_types_ = job_types; |
| 573 | CHECK(!job_types_->empty()); |
| 574 | } |
| 575 | |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 576 | void set_timeout_duration(base::TimeDelta timeout_duration) { |
| 577 | timeout_duration_ = timeout_duration; |
| 578 | } |
| 579 | |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 580 | // ConnectJobFactory implementation. |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 581 | |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 582 | std::unique_ptr<ConnectJob> NewConnectJob( |
Matt Menke | aafff54 | 2019-04-22 22:09:36 | [diff] [blame] | 583 | ClientSocketPool::GroupId group_id, |
| 584 | scoped_refptr<ClientSocketPool::SocketParams> socket_params, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 585 | const absl::optional<NetworkTrafficAnnotationTag>& proxy_annotation_tag, |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 586 | RequestPriority request_priority, |
| 587 | SocketTag socket_tag, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 588 | ConnectJob::Delegate* delegate) const override { |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 589 | EXPECT_TRUE(!job_types_ || !job_types_->empty()); |
| 590 | TestConnectJob::JobType job_type = job_type_; |
| 591 | if (job_types_ && !job_types_->empty()) { |
| 592 | job_type = job_types_->front(); |
| 593 | job_types_->pop_front(); |
| 594 | } |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 595 | return std::make_unique<TestConnectJob>( |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 596 | job_type, request_priority, socket_tag, timeout_duration_, |
| 597 | &common_connect_job_params_, delegate, client_socket_factory_); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | private: |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 601 | const CommonConnectJobParams common_connect_job_params_; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 602 | TestConnectJob::JobType job_type_; |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 603 | std::list<TestConnectJob::JobType>* job_types_; |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 604 | base::TimeDelta timeout_duration_; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 605 | MockClientSocketFactory* const client_socket_factory_; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 606 | |
| 607 | DISALLOW_COPY_AND_ASSIGN(TestConnectJobFactory); |
| 608 | }; |
| 609 | |
[email protected] | a937a06d | 2009-08-19 21:19:24 | [diff] [blame] | 610 | } // namespace |
| 611 | |
[email protected] | a937a06d | 2009-08-19 21:19:24 | [diff] [blame] | 612 | namespace { |
| 613 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 614 | void MockClientSocketFactory::SignalJobs() { |
jdoerrie | 22a91d8b9 | 2018-10-05 08:43:26 | [diff] [blame] | 615 | for (auto it = waiting_jobs_.begin(); it != waiting_jobs_.end(); ++it) { |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 616 | (*it)->Signal(); |
| 617 | } |
| 618 | waiting_jobs_.clear(); |
| 619 | } |
| 620 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 621 | void MockClientSocketFactory::SignalJob(size_t job) { |
| 622 | ASSERT_LT(job, waiting_jobs_.size()); |
| 623 | waiting_jobs_[job]->Signal(); |
| 624 | waiting_jobs_.erase(waiting_jobs_.begin() + job); |
| 625 | } |
| 626 | |
| 627 | void MockClientSocketFactory::SetJobLoadState(size_t job, |
| 628 | LoadState load_state) { |
| 629 | ASSERT_LT(job, waiting_jobs_.size()); |
| 630 | waiting_jobs_[job]->set_load_state(load_state); |
| 631 | } |
| 632 | |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 633 | void MockClientSocketFactory::SetJobHasEstablishedConnection(size_t job) { |
| 634 | ASSERT_LT(job, waiting_jobs_.size()); |
| 635 | waiting_jobs_[job]->set_has_established_connection(); |
| 636 | } |
| 637 | |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 638 | class ClientSocketPoolBaseTest : public TestWithTaskEnvironment { |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 639 | protected: |
Alex Clarke | 0def209 | 2018-12-10 12:01:45 | [diff] [blame] | 640 | ClientSocketPoolBaseTest() |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 641 | : TestWithTaskEnvironment( |
| 642 | base::test::TaskEnvironment::TimeSource::MOCK_TIME), |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 643 | params_(ClientSocketPool::SocketParams::CreateForHttpForTesting()) { |
[email protected] | 636b825 | 2011-04-08 19:56:54 | [diff] [blame] | 644 | connect_backup_jobs_enabled_ = |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 645 | TransportClientSocketPool::connect_backup_jobs_enabled(); |
| 646 | TransportClientSocketPool::set_connect_backup_jobs_enabled(true); |
[email protected] | 636b825 | 2011-04-08 19:56:54 | [diff] [blame] | 647 | } |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 648 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 649 | ~ClientSocketPoolBaseTest() override { |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 650 | TransportClientSocketPool::set_connect_backup_jobs_enabled( |
[email protected] | 636b825 | 2011-04-08 19:56:54 | [diff] [blame] | 651 | connect_backup_jobs_enabled_); |
| 652 | } |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 653 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 654 | void CreatePool(int max_sockets, |
| 655 | int max_sockets_per_group, |
| 656 | bool enable_backup_connect_jobs = false) { |
Tarun Bansal | a763509 | 2019-02-20 10:00:59 | [diff] [blame] | 657 | CreatePoolWithIdleTimeouts(max_sockets, max_sockets_per_group, |
| 658 | kUnusedIdleSocketTimeout, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 659 | ClientSocketPool::used_idle_socket_timeout(), |
| 660 | enable_backup_connect_jobs); |
[email protected] | 9bf28db | 2009-08-29 01:35:16 | [diff] [blame] | 661 | } |
| 662 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 663 | void CreatePoolWithIdleTimeouts( |
| 664 | int max_sockets, |
| 665 | int max_sockets_per_group, |
| 666 | base::TimeDelta unused_idle_socket_timeout, |
| 667 | base::TimeDelta used_idle_socket_timeout, |
| 668 | bool enable_backup_connect_jobs = false, |
| 669 | ProxyServer proxy_server = ProxyServer::Direct()) { |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 670 | DCHECK(!pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 671 | std::unique_ptr<TestConnectJobFactory> connect_job_factory = |
| 672 | std::make_unique<TestConnectJobFactory>(&client_socket_factory_, |
| 673 | &net_log_); |
| 674 | connect_job_factory_ = connect_job_factory.get(); |
| 675 | pool_ = TransportClientSocketPool::CreateForTesting( |
| 676 | max_sockets, max_sockets_per_group, unused_idle_socket_timeout, |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 677 | used_idle_socket_timeout, proxy_server, std::move(connect_job_factory), |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 678 | nullptr /* ssl_config_service */, enable_backup_connect_jobs); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 679 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 680 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 681 | int StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 682 | const ClientSocketPool::GroupId& group_id, |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 683 | RequestPriority priority, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 684 | ClientSocketPool::RespectLimits respect_limits) { |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 685 | return test_base_.StartRequestUsingPool(pool_.get(), group_id, priority, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 686 | respect_limits, params_); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 687 | } |
| 688 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 689 | int StartRequest(const ClientSocketPool::GroupId& group_id, |
| 690 | RequestPriority priority) { |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 691 | return StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 692 | group_id, priority, ClientSocketPool::RespectLimits::ENABLED); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 693 | } |
| 694 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 695 | int GetOrderOfRequest(size_t index) const { |
| 696 | return test_base_.GetOrderOfRequest(index); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 697 | } |
| 698 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 699 | bool ReleaseOneConnection(ClientSocketPoolTest::KeepAlive keep_alive) { |
| 700 | return test_base_.ReleaseOneConnection(keep_alive); |
| 701 | } |
| 702 | |
| 703 | void ReleaseAllConnections(ClientSocketPoolTest::KeepAlive keep_alive) { |
| 704 | test_base_.ReleaseAllConnections(keep_alive); |
| 705 | } |
| 706 | |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 707 | // Expects a single NetLogEventType::SOCKET_POOL_CLOSING_SOCKET in |net_log_|. |
| 708 | // It should be logged for the provided source and have the indicated reason. |
| 709 | void ExpectSocketClosedWithReason(NetLogSource expected_source, |
| 710 | const char* expected_reason) { |
| 711 | auto entries = net_log_.GetEntriesForSourceWithType( |
| 712 | expected_source, NetLogEventType::SOCKET_POOL_CLOSING_SOCKET, |
| 713 | NetLogEventPhase::NONE); |
| 714 | ASSERT_EQ(1u, entries.size()); |
| 715 | ASSERT_TRUE(entries[0].HasParams()); |
| 716 | ASSERT_TRUE(entries[0].params.is_dict()); |
| 717 | const std::string* reason = entries[0].params.FindStringKey("reason"); |
| 718 | ASSERT_TRUE(reason); |
| 719 | EXPECT_EQ(expected_reason, *reason); |
| 720 | } |
| 721 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 722 | TestSocketRequest* request(int i) { return test_base_.request(i); } |
| 723 | size_t requests_size() const { return test_base_.requests_size(); } |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 724 | std::vector<std::unique_ptr<TestSocketRequest>>* requests() { |
olli.raula | 9d66b7d | 2015-11-23 08:30:42 | [diff] [blame] | 725 | return test_base_.requests(); |
| 726 | } |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 727 | // Only counts the requests that get sockets asynchronously; |
| 728 | // synchronous completions are not registered by this count. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 729 | size_t completion_count() const { return test_base_.completion_count(); } |
| 730 | |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 731 | RecordingTestNetLog net_log_; |
[email protected] | 636b825 | 2011-04-08 19:56:54 | [diff] [blame] | 732 | bool connect_backup_jobs_enabled_; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 733 | MockClientSocketFactory client_socket_factory_; |
[email protected] | 17a0c6c | 2009-08-04 00:07:04 | [diff] [blame] | 734 | TestConnectJobFactory* connect_job_factory_; |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 735 | // These parameters are never actually used to create a TransportConnectJob. |
Matt Menke | 84d11e56 | 2019-03-27 00:11:19 | [diff] [blame] | 736 | scoped_refptr<ClientSocketPool::SocketParams> params_; |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 737 | std::unique_ptr<TransportClientSocketPool> pool_; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 738 | ClientSocketPoolTest test_base_; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 739 | }; |
| 740 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 741 | TEST_F(ClientSocketPoolBaseTest, BasicSynchronous) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 742 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 743 | |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 744 | TestCompletionCallback callback; |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 745 | ClientSocketHandle handle; |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 746 | RecordingBoundTestNetLog log; |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 747 | TestLoadTimingInfoNotConnected(handle); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 748 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 749 | EXPECT_EQ(OK, handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 750 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 751 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 752 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 753 | pool_.get(), log.bound())); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 754 | EXPECT_TRUE(handle.is_initialized()); |
| 755 | EXPECT_TRUE(handle.socket()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 756 | TestLoadTimingInfoConnectedNotReused(handle); |
| 757 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 758 | handle.Reset(); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 759 | TestLoadTimingInfoNotConnected(handle); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 760 | |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 761 | auto entries = log.GetEntries(); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 762 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 763 | EXPECT_EQ(5u, entries.size()); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 764 | EXPECT_TRUE(LogContainsEvent( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 765 | entries, 0, NetLogEventType::TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 766 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 767 | EXPECT_TRUE(LogContainsBeginEvent(entries, 1, NetLogEventType::SOCKET_POOL)); |
| 768 | EXPECT_TRUE(LogContainsEvent( |
| 769 | entries, 2, NetLogEventType::SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
| 770 | NetLogEventPhase::NONE)); |
| 771 | EXPECT_TRUE(LogContainsEvent(entries, 3, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 772 | NetLogEventType::SOCKET_POOL_BOUND_TO_SOCKET, |
| 773 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 774 | EXPECT_TRUE(LogContainsEndEvent(entries, 4, NetLogEventType::SOCKET_POOL)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 775 | } |
| 776 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 777 | TEST_F(ClientSocketPoolBaseTest, InitConnectionFailure) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 778 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 779 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 780 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 781 | RecordingBoundTestNetLog log; |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 782 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 783 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 784 | TestCompletionCallback callback; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 785 | // Set the additional error state members to ensure that they get cleared. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 786 | handle.set_is_ssl_error(true); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 787 | handle.set_ssl_cert_request_info(base::MakeRefCounted<SSLCertRequestInfo>()); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 788 | EXPECT_EQ( |
| 789 | ERR_CONNECTION_FAILED, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 790 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 791 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 792 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 793 | pool_.get(), log.bound())); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 794 | EXPECT_FALSE(handle.socket()); |
| 795 | EXPECT_FALSE(handle.is_ssl_error()); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 796 | EXPECT_FALSE(handle.ssl_cert_request_info()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 797 | TestLoadTimingInfoNotConnected(handle); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 798 | |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 799 | auto entries = log.GetEntries(); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 800 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 801 | EXPECT_EQ(4u, entries.size()); |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 802 | EXPECT_TRUE(LogContainsEvent( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 803 | entries, 0, NetLogEventType::TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 804 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 805 | EXPECT_TRUE(LogContainsBeginEvent(entries, 1, NetLogEventType::SOCKET_POOL)); |
| 806 | EXPECT_TRUE(LogContainsEvent( |
| 807 | entries, 2, NetLogEventType::SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
| 808 | NetLogEventPhase::NONE)); |
| 809 | EXPECT_TRUE(LogContainsEndEvent(entries, 3, NetLogEventType::SOCKET_POOL)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 810 | } |
| 811 | |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 812 | // Test releasing an open socket into the socket pool, telling the socket pool |
| 813 | // to close the socket. |
| 814 | TEST_F(ClientSocketPoolBaseTest, ReleaseAndCloseConnection) { |
| 815 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 816 | |
| 817 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 818 | ASSERT_TRUE(request(0)->handle()->socket()); |
| 819 | net::NetLogSource source = request(0)->handle()->socket()->NetLog().source(); |
| 820 | ReleaseOneConnection(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 821 | |
| 822 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 823 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 824 | |
| 825 | ExpectSocketClosedWithReason( |
| 826 | source, TransportClientSocketPool::kClosedConnectionReturnedToPool); |
| 827 | } |
| 828 | |
| 829 | TEST_F(ClientSocketPoolBaseTest, SocketWithUnreadDataReturnedToPool) { |
| 830 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 831 | connect_job_factory_->set_job_type(TestConnectJob::kMockUnreadDataJob); |
| 832 | |
| 833 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 834 | ASSERT_TRUE(request(0)->handle()->socket()); |
| 835 | net::NetLogSource source = request(0)->handle()->socket()->NetLog().source(); |
| 836 | EXPECT_TRUE(request(0)->handle()->socket()->IsConnected()); |
| 837 | EXPECT_FALSE(request(0)->handle()->socket()->IsConnectedAndIdle()); |
| 838 | ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE); |
| 839 | |
| 840 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 841 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 842 | |
| 843 | ExpectSocketClosedWithReason( |
| 844 | source, TransportClientSocketPool::kDataReceivedUnexpectedly); |
| 845 | } |
| 846 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 847 | // Make sure different groups do not share sockets. |
| 848 | TEST_F(ClientSocketPoolBaseTest, GroupSeparation) { |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 849 | base::test::ScopedFeatureList feature_list; |
| 850 | feature_list.InitAndEnableFeature( |
| 851 | features::kPartitionConnectionsByNetworkIsolationKey); |
| 852 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 853 | CreatePool(1000 /* max_sockets */, 2 /* max_sockets_per_group */); |
| 854 | |
| 855 | const HostPortPair kHostPortPairs[] = { |
| 856 | {"a", 80}, |
| 857 | {"a", 443}, |
| 858 | {"b", 80}, |
| 859 | }; |
| 860 | |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame^] | 861 | const char* const kSchemes[] = { |
| 862 | url::kHttpScheme, |
| 863 | url::kHttpsScheme, |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 864 | }; |
| 865 | |
Matt Menke | bdf77780 | 2019-04-22 19:38:59 | [diff] [blame] | 866 | const PrivacyMode kPrivacyModes[] = {PrivacyMode::PRIVACY_MODE_DISABLED, |
| 867 | PrivacyMode::PRIVACY_MODE_ENABLED}; |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 868 | |
Matt Menke | 4807a9a | 2020-11-21 00:14:41 | [diff] [blame] | 869 | const SchemefulSite kSiteA(GURL("http://a.test/")); |
| 870 | const SchemefulSite kSiteB(GURL("http://b.test/")); |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 871 | const NetworkIsolationKey kNetworkIsolationKeys[] = { |
Matt Menke | 4807a9a | 2020-11-21 00:14:41 | [diff] [blame] | 872 | NetworkIsolationKey(kSiteA, kSiteA), |
| 873 | NetworkIsolationKey(kSiteB, kSiteB), |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 874 | }; |
| 875 | |
Ben Schwartz | 3ff4dc1e6 | 2021-04-27 21:15:23 | [diff] [blame] | 876 | const SecureDnsPolicy kSecureDnsPolicys[] = {SecureDnsPolicy::kAllow, |
| 877 | SecureDnsPolicy::kDisable}; |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 878 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 879 | int total_idle_sockets = 0; |
| 880 | |
| 881 | // Walk through each GroupId, making sure that requesting a socket for one |
| 882 | // group does not return a previously connected socket for another group. |
| 883 | for (const auto& host_port_pair : kHostPortPairs) { |
| 884 | SCOPED_TRACE(host_port_pair.ToString()); |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame^] | 885 | for (const char* scheme : kSchemes) { |
| 886 | SCOPED_TRACE(scheme); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 887 | for (const auto& privacy_mode : kPrivacyModes) { |
| 888 | SCOPED_TRACE(privacy_mode); |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 889 | for (const auto& network_isolation_key : kNetworkIsolationKeys) { |
| 890 | SCOPED_TRACE(network_isolation_key.ToString()); |
Ben Schwartz | 3ff4dc1e6 | 2021-04-27 21:15:23 | [diff] [blame] | 891 | for (const auto& secure_dns_policy : kSecureDnsPolicys) { |
| 892 | SCOPED_TRACE(static_cast<int>(secure_dns_policy)); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 893 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 894 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 895 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 896 | ClientSocketPool::GroupId group_id( |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame^] | 897 | url::SchemeHostPort(scheme, host_port_pair.host(), |
| 898 | host_port_pair.port()), |
| 899 | privacy_mode, network_isolation_key, secure_dns_policy); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 900 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 901 | EXPECT_FALSE(pool_->HasGroupForTesting(group_id)); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 902 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 903 | TestCompletionCallback callback; |
| 904 | ClientSocketHandle handle; |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 905 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 906 | // Since the group is empty, requesting a socket should not complete |
| 907 | // synchronously. |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 908 | EXPECT_THAT(handle.Init(group_id, params_, absl::nullopt, |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 909 | DEFAULT_PRIORITY, SocketTag(), |
| 910 | ClientSocketPool::RespectLimits::ENABLED, |
| 911 | callback.callback(), |
| 912 | ClientSocketPool::ProxyAuthCallback(), |
| 913 | pool_.get(), NetLogWithSource()), |
| 914 | IsError(ERR_IO_PENDING)); |
| 915 | EXPECT_TRUE(pool_->HasGroupForTesting(group_id)); |
| 916 | EXPECT_EQ(total_idle_sockets, pool_->IdleSocketCount()); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 917 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 918 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 919 | EXPECT_TRUE(handle.socket()); |
| 920 | EXPECT_TRUE(pool_->HasGroupForTesting(group_id)); |
| 921 | EXPECT_EQ(total_idle_sockets, pool_->IdleSocketCount()); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 922 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 923 | // Return socket to pool. |
| 924 | handle.Reset(); |
| 925 | EXPECT_EQ(total_idle_sockets + 1, pool_->IdleSocketCount()); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 926 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 927 | // Requesting a socket again should return the same socket as |
| 928 | // before, so should complete synchronously. |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 929 | EXPECT_THAT(handle.Init(group_id, params_, absl::nullopt, |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 930 | DEFAULT_PRIORITY, SocketTag(), |
| 931 | ClientSocketPool::RespectLimits::ENABLED, |
| 932 | callback.callback(), |
| 933 | ClientSocketPool::ProxyAuthCallback(), |
| 934 | pool_.get(), NetLogWithSource()), |
| 935 | IsOk()); |
| 936 | EXPECT_TRUE(handle.socket()); |
| 937 | EXPECT_EQ(total_idle_sockets, 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 | // Return socket to pool again. |
| 940 | handle.Reset(); |
| 941 | EXPECT_EQ(total_idle_sockets + 1, pool_->IdleSocketCount()); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 942 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 943 | ++total_idle_sockets; |
| 944 | } |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 945 | } |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 946 | } |
| 947 | } |
| 948 | } |
| 949 | } |
| 950 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 951 | TEST_F(ClientSocketPoolBaseTest, TotalLimit) { |
| 952 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 953 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 954 | // TODO(eroman): Check that the NetLog contains this event. |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 955 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 956 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 957 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
| 958 | EXPECT_THAT(StartRequest(TestGroupId("c"), DEFAULT_PRIORITY), IsOk()); |
| 959 | EXPECT_THAT(StartRequest(TestGroupId("d"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 960 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 961 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 962 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 963 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 964 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 965 | EXPECT_THAT(StartRequest(TestGroupId("e"), DEFAULT_PRIORITY), |
| 966 | IsError(ERR_IO_PENDING)); |
| 967 | EXPECT_THAT(StartRequest(TestGroupId("f"), DEFAULT_PRIORITY), |
| 968 | IsError(ERR_IO_PENDING)); |
| 969 | EXPECT_THAT(StartRequest(TestGroupId("g"), DEFAULT_PRIORITY), |
| 970 | IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 971 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 972 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 973 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 974 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 975 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 976 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 977 | |
| 978 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 979 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 980 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 981 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 982 | EXPECT_EQ(5, GetOrderOfRequest(5)); |
| 983 | EXPECT_EQ(6, GetOrderOfRequest(6)); |
| 984 | EXPECT_EQ(7, GetOrderOfRequest(7)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 985 | |
| 986 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 987 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(8)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 988 | } |
| 989 | |
| 990 | TEST_F(ClientSocketPoolBaseTest, TotalLimitReachedNewGroup) { |
| 991 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 992 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 993 | // TODO(eroman): Check that the NetLog contains this event. |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 994 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 995 | // Reach all limits: max total sockets, and max sockets per group. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 996 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 997 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 998 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
| 999 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1000 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1001 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1002 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1003 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1004 | |
| 1005 | // Now create a new group and verify that we don't starve it. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1006 | EXPECT_THAT(StartRequest(TestGroupId("c"), DEFAULT_PRIORITY), |
| 1007 | IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1008 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1009 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1010 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1011 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1012 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1013 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1014 | |
| 1015 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1016 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1017 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 1018 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 1019 | EXPECT_EQ(5, GetOrderOfRequest(5)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1020 | |
| 1021 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1022 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(6)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1023 | } |
| 1024 | |
| 1025 | TEST_F(ClientSocketPoolBaseTest, TotalLimitRespectsPriority) { |
| 1026 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1027 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1028 | EXPECT_THAT(StartRequest(TestGroupId("b"), LOWEST), IsOk()); |
| 1029 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsOk()); |
| 1030 | EXPECT_THAT(StartRequest(TestGroupId("b"), HIGHEST), IsOk()); |
| 1031 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1032 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1033 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1034 | client_socket_factory_.allocation_count()); |
| 1035 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1036 | EXPECT_THAT(StartRequest(TestGroupId("c"), LOWEST), IsError(ERR_IO_PENDING)); |
| 1037 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1038 | EXPECT_THAT(StartRequest(TestGroupId("b"), HIGHEST), IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1039 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1040 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1041 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1042 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1043 | |
| 1044 | // First 4 requests don't have to wait, and finish in order. |
| 1045 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1046 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1047 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 1048 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 1049 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1050 | // Request ("b", HIGHEST) has the highest priority, then (TestGroupId("a"), |
| 1051 | // MEDIUM), and then ("c", LOWEST). |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1052 | EXPECT_EQ(7, GetOrderOfRequest(5)); |
| 1053 | EXPECT_EQ(6, GetOrderOfRequest(6)); |
| 1054 | EXPECT_EQ(5, GetOrderOfRequest(7)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1055 | |
| 1056 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1057 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(9)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1058 | } |
| 1059 | |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1060 | // Test reprioritizing a request before completion doesn't interfere with |
| 1061 | // its completion. |
| 1062 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeOne) { |
| 1063 | CreatePool(kDefaultMaxSockets, 1); |
| 1064 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1065 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1066 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1067 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1068 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1069 | |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 1070 | request(1)->handle()->SetPriority(HIGHEST); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1071 | |
| 1072 | ReleaseOneConnection(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1073 | |
| 1074 | EXPECT_TRUE(request(1)->handle()->socket()); |
| 1075 | } |
| 1076 | |
| 1077 | // Reprioritize a request up past another one and make sure that changes the |
| 1078 | // completion order. |
| 1079 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeUpReorder) { |
| 1080 | CreatePool(kDefaultMaxSockets, 1); |
| 1081 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1082 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1083 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1084 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1085 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1086 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1087 | EXPECT_FALSE(request(2)->handle()->socket()); |
| 1088 | |
| 1089 | request(2)->handle()->SetPriority(HIGHEST); |
| 1090 | |
| 1091 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1092 | |
| 1093 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1094 | EXPECT_EQ(3, GetOrderOfRequest(2)); |
| 1095 | EXPECT_EQ(2, GetOrderOfRequest(3)); |
| 1096 | } |
| 1097 | |
| 1098 | // Reprioritize a request without changing relative priorities and check |
| 1099 | // that the order doesn't change. |
| 1100 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeUpNoReorder) { |
| 1101 | CreatePool(kDefaultMaxSockets, 1); |
| 1102 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1103 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1104 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1105 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1106 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1107 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1108 | EXPECT_FALSE(request(2)->handle()->socket()); |
| 1109 | |
| 1110 | request(2)->handle()->SetPriority(MEDIUM); |
| 1111 | |
| 1112 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1113 | |
| 1114 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1115 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1116 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 1117 | } |
| 1118 | |
| 1119 | // Reprioritize a request past down another one and make sure that changes the |
| 1120 | // completion order. |
| 1121 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeDownReorder) { |
| 1122 | CreatePool(kDefaultMaxSockets, 1); |
| 1123 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1124 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1125 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1126 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1127 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1128 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1129 | EXPECT_FALSE(request(2)->handle()->socket()); |
| 1130 | |
| 1131 | request(1)->handle()->SetPriority(LOW); |
| 1132 | |
| 1133 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1134 | |
| 1135 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1136 | EXPECT_EQ(3, GetOrderOfRequest(2)); |
| 1137 | EXPECT_EQ(2, GetOrderOfRequest(3)); |
| 1138 | } |
| 1139 | |
| 1140 | // Reprioritize a request to the same level as another and confirm it is |
| 1141 | // put after the old request. |
| 1142 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeResetFIFO) { |
| 1143 | CreatePool(kDefaultMaxSockets, 1); |
| 1144 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1145 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1146 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1147 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1148 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1149 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1150 | EXPECT_FALSE(request(2)->handle()->socket()); |
| 1151 | |
| 1152 | request(1)->handle()->SetPriority(MEDIUM); |
| 1153 | |
| 1154 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1155 | |
| 1156 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1157 | EXPECT_EQ(3, GetOrderOfRequest(2)); |
| 1158 | EXPECT_EQ(2, GetOrderOfRequest(3)); |
| 1159 | } |
| 1160 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1161 | TEST_F(ClientSocketPoolBaseTest, TotalLimitRespectsGroupLimit) { |
| 1162 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1163 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1164 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsOk()); |
| 1165 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsOk()); |
| 1166 | EXPECT_THAT(StartRequest(TestGroupId("b"), HIGHEST), IsOk()); |
| 1167 | EXPECT_THAT(StartRequest(TestGroupId("b"), MEDIUM), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1168 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1169 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1170 | client_socket_factory_.allocation_count()); |
| 1171 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1172 | EXPECT_THAT(StartRequest(TestGroupId("c"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1173 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 1174 | EXPECT_THAT(StartRequest(TestGroupId("b"), HIGHEST), IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1175 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1176 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1177 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1178 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1179 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1180 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1181 | |
| 1182 | // First 4 requests don't have to wait, and finish in order. |
| 1183 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1184 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1185 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 1186 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 1187 | |
| 1188 | // Request ("b", 7) has the highest priority, but we can't make new socket for |
| 1189 | // group "b", because it has reached the per-group limit. Then we make |
| 1190 | // socket for ("c", 6), because it has higher priority than ("a", 4), |
| 1191 | // and we still can't make a socket for group "b". |
| 1192 | EXPECT_EQ(5, GetOrderOfRequest(5)); |
| 1193 | EXPECT_EQ(6, GetOrderOfRequest(6)); |
| 1194 | EXPECT_EQ(7, GetOrderOfRequest(7)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1195 | |
| 1196 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1197 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(8)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1198 | } |
| 1199 | |
| 1200 | // Make sure that we count connecting sockets against the total limit. |
| 1201 | TEST_F(ClientSocketPoolBaseTest, TotalLimitCountsConnectingSockets) { |
| 1202 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1203 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1204 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1205 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
| 1206 | EXPECT_THAT(StartRequest(TestGroupId("c"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1207 | |
| 1208 | // Create one asynchronous request. |
| 1209 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1210 | EXPECT_THAT(StartRequest(TestGroupId("d"), DEFAULT_PRIORITY), |
| 1211 | IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1212 | |
[email protected] | 6b17538 | 2009-10-13 06:47:47 | [diff] [blame] | 1213 | // We post all of our delayed tasks with a 2ms delay. I.e. they don't |
| 1214 | // actually become pending until 2ms after they have been created. In order |
| 1215 | // to flush all tasks, we need to wait so that we know there are no |
| 1216 | // soon-to-be-pending tasks waiting. |
Alex Clarke | 0def209 | 2018-12-10 12:01:45 | [diff] [blame] | 1217 | FastForwardBy(base::TimeDelta::FromMilliseconds(10)); |
[email protected] | 6b17538 | 2009-10-13 06:47:47 | [diff] [blame] | 1218 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1219 | // The next synchronous request should wait for its turn. |
| 1220 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1221 | EXPECT_THAT(StartRequest(TestGroupId("e"), DEFAULT_PRIORITY), |
| 1222 | IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1223 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1224 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1225 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1226 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1227 | client_socket_factory_.allocation_count()); |
| 1228 | |
| 1229 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1230 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1231 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 1232 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1233 | EXPECT_EQ(5, GetOrderOfRequest(5)); |
| 1234 | |
| 1235 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1236 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(6)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1237 | } |
| 1238 | |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1239 | TEST_F(ClientSocketPoolBaseTest, CorrectlyCountStalledGroups) { |
| 1240 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 1241 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 1242 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1243 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1244 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1245 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1246 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1247 | |
| 1248 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 1249 | |
| 1250 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1251 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1252 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), |
| 1253 | IsError(ERR_IO_PENDING)); |
| 1254 | EXPECT_THAT(StartRequest(TestGroupId("c"), DEFAULT_PRIORITY), |
| 1255 | IsError(ERR_IO_PENDING)); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1256 | |
| 1257 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1258 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1259 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1260 | EXPECT_EQ(kDefaultMaxSockets + 1, client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1261 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1262 | EXPECT_EQ(kDefaultMaxSockets + 2, client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1263 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
| 1264 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1265 | EXPECT_EQ(kDefaultMaxSockets + 2, client_socket_factory_.allocation_count()); |
| 1266 | } |
| 1267 | |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 1268 | TEST_F(ClientSocketPoolBaseTest, StallAndThenCancelAndTriggerAvailableSocket) { |
| 1269 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 1270 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1271 | |
| 1272 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1273 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1274 | EXPECT_EQ( |
| 1275 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1276 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1277 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1278 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1279 | pool_.get(), NetLogWithSource())); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 1280 | |
| 1281 | ClientSocketHandle handles[4]; |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 1282 | for (size_t i = 0; i < base::size(handles); ++i) { |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1283 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1284 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1285 | handles[i].Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1286 | TestGroupId("b"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1287 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1288 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1289 | pool_.get(), NetLogWithSource())); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 1290 | } |
| 1291 | |
| 1292 | // One will be stalled, cancel all the handles now. |
| 1293 | // This should hit the OnAvailableSocketSlot() code where we previously had |
| 1294 | // stalled groups, but no longer have any. |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 1295 | for (size_t i = 0; i < base::size(handles); ++i) |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 1296 | handles[i].Reset(); |
| 1297 | } |
| 1298 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1299 | TEST_F(ClientSocketPoolBaseTest, CancelStalledSocketAtSocketLimit) { |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1300 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1301 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 1302 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1303 | { |
| 1304 | ClientSocketHandle handles[kDefaultMaxSockets]; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1305 | TestCompletionCallback callbacks[kDefaultMaxSockets]; |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1306 | for (int i = 0; i < kDefaultMaxSockets; ++i) { |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame^] | 1307 | EXPECT_EQ(OK, handles[i].Init(TestGroupId("a" + base::NumberToString(i)), |
| 1308 | params_, absl::nullopt, DEFAULT_PRIORITY, |
| 1309 | SocketTag(), |
| 1310 | ClientSocketPool::RespectLimits::ENABLED, |
| 1311 | callbacks[i].callback(), |
| 1312 | ClientSocketPool::ProxyAuthCallback(), |
| 1313 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1314 | } |
| 1315 | |
| 1316 | // Force a stalled group. |
| 1317 | ClientSocketHandle stalled_handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1318 | TestCompletionCallback callback; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1319 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1320 | stalled_handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1321 | TestGroupId("foo"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1322 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1323 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1324 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1325 | |
| 1326 | // Cancel the stalled request. |
| 1327 | stalled_handle.Reset(); |
| 1328 | |
| 1329 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1330 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 1331 | |
| 1332 | // Dropping out of scope will close all handles and return them to idle. |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1333 | } |
| 1334 | |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1335 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1336 | EXPECT_EQ(kDefaultMaxSockets, pool_->IdleSocketCount()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1337 | } |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1338 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1339 | TEST_F(ClientSocketPoolBaseTest, CancelPendingSocketAtSocketLimit) { |
| 1340 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1341 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 1342 | |
| 1343 | { |
| 1344 | ClientSocketHandle handles[kDefaultMaxSockets]; |
| 1345 | for (int i = 0; i < kDefaultMaxSockets; ++i) { |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1346 | TestCompletionCallback callback; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1347 | EXPECT_EQ(ERR_IO_PENDING, |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame^] | 1348 | handles[i].Init( |
| 1349 | TestGroupId("a" + base::NumberToString(i)), params_, |
| 1350 | absl::nullopt, DEFAULT_PRIORITY, SocketTag(), |
| 1351 | ClientSocketPool::RespectLimits::ENABLED, |
| 1352 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1353 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1354 | } |
| 1355 | |
| 1356 | // Force a stalled group. |
| 1357 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1358 | ClientSocketHandle stalled_handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1359 | TestCompletionCallback callback; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1360 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1361 | stalled_handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1362 | TestGroupId("foo"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1363 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1364 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1365 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1366 | |
| 1367 | // Since it is stalled, it should have no connect jobs. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1368 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("foo"))); |
| 1369 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 1370 | TestGroupId("foo"))); |
| 1371 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroupForTesting( |
| 1372 | TestGroupId("foo"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1373 | |
| 1374 | // Cancel the stalled request. |
| 1375 | handles[0].Reset(); |
| 1376 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1377 | // Now we should have a connect job. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1378 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("foo"))); |
| 1379 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 1380 | TestGroupId("foo"))); |
| 1381 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroupForTesting( |
| 1382 | TestGroupId("foo"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1383 | |
| 1384 | // The stalled socket should connect. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1385 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1386 | |
| 1387 | EXPECT_EQ(kDefaultMaxSockets + 1, |
| 1388 | client_socket_factory_.allocation_count()); |
| 1389 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1390 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("foo"))); |
| 1391 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 1392 | TestGroupId("foo"))); |
| 1393 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroupForTesting( |
| 1394 | TestGroupId("foo"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1395 | |
| 1396 | // Dropping out of scope will close all handles and return them to idle. |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1397 | } |
| 1398 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1399 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 1400 | } |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1401 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1402 | TEST_F(ClientSocketPoolBaseTest, WaitForStalledSocketAtSocketLimit) { |
| 1403 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1404 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1405 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1406 | ClientSocketHandle stalled_handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1407 | TestCompletionCallback callback; |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1408 | { |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 1409 | EXPECT_FALSE(pool_->IsStalled()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1410 | ClientSocketHandle handles[kDefaultMaxSockets]; |
| 1411 | for (int i = 0; i < kDefaultMaxSockets; ++i) { |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1412 | TestCompletionCallback callback; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1413 | EXPECT_EQ( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1414 | OK, handles[i].Init( |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame^] | 1415 | TestGroupId(base::StringPrintf("take-2-%d", i)), params_, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1416 | absl::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1417 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1418 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 1419 | NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1420 | } |
| 1421 | |
| 1422 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1423 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 1424 | EXPECT_FALSE(pool_->IsStalled()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1425 | |
| 1426 | // Now we will hit the socket limit. |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1427 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1428 | stalled_handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1429 | TestGroupId("foo"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1430 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1431 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1432 | pool_.get(), NetLogWithSource())); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 1433 | EXPECT_TRUE(pool_->IsStalled()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1434 | |
| 1435 | // Dropping out of scope will close all handles and return them to idle. |
| 1436 | } |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1437 | |
| 1438 | // But if we wait for it, the released idle sockets will be closed in |
| 1439 | // preference of the waiting request. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1440 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1441 | |
| 1442 | EXPECT_EQ(kDefaultMaxSockets + 1, client_socket_factory_.allocation_count()); |
| 1443 | EXPECT_EQ(3, pool_->IdleSocketCount()); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1444 | } |
| 1445 | |
| 1446 | // Regression test for http://crbug.com/40952. |
| 1447 | TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketAtSocketLimitDeleteGroup) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1448 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 1449 | true /* enable_backup_connect_jobs */); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1450 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 1451 | |
| 1452 | for (int i = 0; i < kDefaultMaxSockets; ++i) { |
| 1453 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1454 | TestCompletionCallback callback; |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame^] | 1455 | EXPECT_EQ( |
| 1456 | OK, |
| 1457 | handle.Init(TestGroupId("a" + base::NumberToString(i)), params_, |
| 1458 | absl::nullopt, DEFAULT_PRIORITY, SocketTag(), |
| 1459 | ClientSocketPool::RespectLimits::ENABLED, |
| 1460 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1461 | pool_.get(), NetLogWithSource())); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1462 | } |
| 1463 | |
| 1464 | // Flush all the DoReleaseSocket tasks. |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 1465 | base::RunLoop().RunUntilIdle(); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1466 | |
| 1467 | // Stall a group. Set a pending job so it'll trigger a backup job if we don't |
| 1468 | // reuse a socket. |
| 1469 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1470 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1471 | TestCompletionCallback callback; |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1472 | |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame^] | 1473 | // "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] | 1474 | // which is the one which we would close an idle socket for. We shouldn't |
| 1475 | // close an idle socket though, since we should reuse the idle socket. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1476 | EXPECT_EQ(OK, handle.Init( |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame^] | 1477 | TestGroupId("a0"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1478 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1479 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1480 | pool_.get(), NetLogWithSource())); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1481 | |
| 1482 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1483 | EXPECT_EQ(kDefaultMaxSockets - 1, pool_->IdleSocketCount()); |
| 1484 | } |
| 1485 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1486 | TEST_F(ClientSocketPoolBaseTest, PendingRequests) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1487 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1488 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1489 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1490 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1491 | EXPECT_THAT(StartRequest(TestGroupId("a"), IDLE), IsError(ERR_IO_PENDING)); |
| 1492 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
| 1493 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1494 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1495 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 1496 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1497 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1498 | ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1499 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
| 1500 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1501 | EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup, |
| 1502 | completion_count()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1503 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1504 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1505 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
[email protected] | c9c6f5c | 2010-07-31 01:30:03 | [diff] [blame] | 1506 | EXPECT_EQ(8, GetOrderOfRequest(3)); |
| 1507 | EXPECT_EQ(6, GetOrderOfRequest(4)); |
| 1508 | EXPECT_EQ(4, GetOrderOfRequest(5)); |
| 1509 | EXPECT_EQ(3, GetOrderOfRequest(6)); |
| 1510 | EXPECT_EQ(5, GetOrderOfRequest(7)); |
| 1511 | EXPECT_EQ(7, GetOrderOfRequest(8)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1512 | |
| 1513 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1514 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(9)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1515 | } |
| 1516 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1517 | TEST_F(ClientSocketPoolBaseTest, PendingRequests_NoKeepAlive) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1518 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1519 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1520 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1521 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1522 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
| 1523 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1524 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1525 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 1526 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1527 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1528 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1529 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1530 | for (size_t i = kDefaultMaxSocketsPerGroup; i < requests_size(); ++i) |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1531 | EXPECT_THAT(request(i)->WaitForResult(), IsOk()); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1532 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1533 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1534 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1535 | EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup, |
| 1536 | completion_count()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1537 | } |
| 1538 | |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1539 | TEST_F(ClientSocketPoolBaseTest, ResetAndCloseSocket) { |
| 1540 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1541 | |
| 1542 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1543 | ClientSocketHandle handle; |
| 1544 | TestCompletionCallback callback; |
| 1545 | EXPECT_EQ( |
| 1546 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1547 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1548 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1549 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1550 | pool_.get(), NetLogWithSource())); |
| 1551 | |
| 1552 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 1553 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1554 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1555 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
| 1556 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 1557 | |
| 1558 | handle.ResetAndCloseSocket(); |
| 1559 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1560 | } |
| 1561 | |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1562 | // This test will start up a socket request and then call Reset() on the handle. |
| 1563 | // The pending ConnectJob should not be destroyed. |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1564 | TEST_F(ClientSocketPoolBaseTest, CancelRequestKeepsConnectJob) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1565 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1566 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1567 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1568 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1569 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1570 | EXPECT_EQ( |
| 1571 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1572 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1573 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1574 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1575 | pool_.get(), NetLogWithSource())); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1576 | handle.Reset(); |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1577 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1578 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1579 | } |
| 1580 | |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1581 | // This test will start up a socket request and then call ResetAndCloseSocket() |
| 1582 | // on the handle. The pending ConnectJob or connected socket should be |
| 1583 | // destroyed. |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1584 | TEST_F(ClientSocketPoolBaseTest, CancelRequestAndCloseSocket) { |
| 1585 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1586 | |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1587 | // When true, the socket connects before it's canceled. |
| 1588 | for (bool cancel_when_callback_pending : {false, true}) { |
| 1589 | if (cancel_when_callback_pending) { |
| 1590 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 1591 | } else { |
| 1592 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1593 | } |
| 1594 | ClientSocketHandle handle; |
| 1595 | TestCompletionCallback callback; |
| 1596 | EXPECT_EQ( |
| 1597 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1598 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1599 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1600 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1601 | pool_.get(), NetLogWithSource())); |
| 1602 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1603 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1604 | |
| 1605 | if (cancel_when_callback_pending) { |
| 1606 | client_socket_factory_.SignalJobs(); |
| 1607 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1608 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 1609 | } |
| 1610 | |
| 1611 | handle.ResetAndCloseSocket(); |
| 1612 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1613 | } |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1614 | } |
| 1615 | |
| 1616 | TEST_F(ClientSocketPoolBaseTest, |
| 1617 | CancelRequestAndCloseSocketWhenMoreRequestsThanConnectJobs) { |
| 1618 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1619 | |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1620 | // When true, the sockets connect before they're canceled. |
| 1621 | for (bool cancel_when_callback_pending : {false, true}) { |
| 1622 | if (cancel_when_callback_pending) { |
| 1623 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 1624 | } else { |
| 1625 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1626 | } |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1627 | |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1628 | std::vector<std::unique_ptr<ClientSocketHandle>> handles; |
| 1629 | TestCompletionCallback callback; |
| 1630 | // Make |kDefaultMaxSockets + 1| socket requests. |
| 1631 | for (int i = 0; i < kDefaultMaxSocketsPerGroup + 1; ++i) { |
| 1632 | std::unique_ptr<ClientSocketHandle> handle = |
| 1633 | std::make_unique<ClientSocketHandle>(); |
| 1634 | EXPECT_EQ(ERR_IO_PENDING, |
| 1635 | handle->Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1636 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1637 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1638 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1639 | pool_.get(), NetLogWithSource())); |
| 1640 | handles.push_back(std::move(handle)); |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1641 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1642 | EXPECT_EQ( |
| 1643 | static_cast<size_t>(std::min(i + 1, kDefaultMaxSocketsPerGroup)), |
| 1644 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1645 | } |
| 1646 | |
| 1647 | if (cancel_when_callback_pending) { |
| 1648 | client_socket_factory_.SignalJobs(); |
| 1649 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1650 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
| 1651 | pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 1652 | } |
| 1653 | |
| 1654 | // Calling ResetAndCloseSocket() on a handle should not cancel a ConnectJob |
| 1655 | // or close a socket, since there are more requests than ConnectJobs or |
| 1656 | // sockets. |
| 1657 | handles[kDefaultMaxSocketsPerGroup]->ResetAndCloseSocket(); |
| 1658 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1659 | if (cancel_when_callback_pending) { |
| 1660 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
| 1661 | pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 1662 | } else { |
| 1663 | EXPECT_EQ(static_cast<size_t>(kDefaultMaxSocketsPerGroup), |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1664 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1665 | } |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1666 | |
| 1667 | // Calling ResetAndCloseSocket() on other handles should cancel a ConnectJob |
| 1668 | // or close a socket. |
| 1669 | for (int i = kDefaultMaxSocketsPerGroup - 1; i >= 0; --i) { |
| 1670 | handles[i]->ResetAndCloseSocket(); |
| 1671 | if (i > 0) { |
| 1672 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1673 | if (cancel_when_callback_pending) { |
| 1674 | EXPECT_EQ(i, |
| 1675 | pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 1676 | } else { |
| 1677 | EXPECT_EQ(static_cast<size_t>(i), |
| 1678 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1679 | } |
| 1680 | } else { |
| 1681 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1682 | } |
| 1683 | } |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1684 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1685 | } |
| 1686 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1687 | TEST_F(ClientSocketPoolBaseTest, ConnectCancelConnect) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1688 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1689 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1690 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 1691 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1692 | TestCompletionCallback callback; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1693 | |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1694 | EXPECT_EQ( |
| 1695 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1696 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1697 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1698 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1699 | pool_.get(), NetLogWithSource())); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1700 | |
| 1701 | handle.Reset(); |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1702 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1703 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1704 | |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1705 | // This will create a second ConnectJob, since the other ConnectJob was |
| 1706 | // previously assigned to a request. |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1707 | TestCompletionCallback callback2; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1708 | EXPECT_EQ( |
| 1709 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1710 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1711 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1712 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1713 | pool_.get(), NetLogWithSource())); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1714 | |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1715 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1716 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1717 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1718 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1719 | EXPECT_FALSE(callback.have_result()); |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1720 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1721 | // One ConnectJob completed, and its socket is now assigned to |handle|. |
| 1722 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 1723 | // The other ConnectJob should have either completed, or still be connecting. |
| 1724 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")) + |
| 1725 | pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1726 | |
| 1727 | handle.Reset(); |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1728 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1729 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")) + |
| 1730 | pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
| 1731 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1732 | } |
| 1733 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1734 | TEST_F(ClientSocketPoolBaseTest, CancelRequest) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1735 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1736 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1737 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1738 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1739 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
| 1740 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1741 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1742 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 1743 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1744 | |
| 1745 | // Cancel a request. |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1746 | size_t index_to_cancel = kDefaultMaxSocketsPerGroup + 2; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1747 | EXPECT_FALSE((*requests())[index_to_cancel]->handle()->is_initialized()); |
| 1748 | (*requests())[index_to_cancel]->handle()->Reset(); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1749 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1750 | ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1751 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1752 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
| 1753 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1754 | EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup - 1, |
| 1755 | completion_count()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1756 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1757 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1758 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1759 | EXPECT_EQ(5, GetOrderOfRequest(3)); |
| 1760 | EXPECT_EQ(3, GetOrderOfRequest(4)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1761 | EXPECT_EQ(ClientSocketPoolTest::kRequestNotFound, |
| 1762 | GetOrderOfRequest(5)); // Canceled request. |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1763 | EXPECT_EQ(4, GetOrderOfRequest(6)); |
| 1764 | EXPECT_EQ(6, GetOrderOfRequest(7)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1765 | |
| 1766 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1767 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(8)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1768 | } |
| 1769 | |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1770 | // Function to be used as a callback on socket request completion. It first |
| 1771 | // disconnects the successfully connected socket from the first request, and |
| 1772 | // then reuses the ClientSocketHandle to request another socket. |
| 1773 | // |
| 1774 | // |nested_callback| is called with the result of the second socket request. |
| 1775 | void RequestSocketOnComplete(ClientSocketHandle* handle, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1776 | TransportClientSocketPool* pool, |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1777 | TestConnectJobFactory* test_connect_job_factory, |
| 1778 | TestConnectJob::JobType next_job_type, |
Bence Béky | a4a5093 | 2018-08-10 13:39:41 | [diff] [blame] | 1779 | TestCompletionCallback* nested_callback, |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1780 | int first_request_result) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1781 | EXPECT_THAT(first_request_result, IsOk()); |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1782 | |
| 1783 | test_connect_job_factory->set_job_type(next_job_type); |
| 1784 | |
| 1785 | // Don't allow reuse of the socket. Disconnect it and then release it. |
| 1786 | if (handle->socket()) |
| 1787 | handle->socket()->Disconnect(); |
| 1788 | handle->Reset(); |
| 1789 | |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1790 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1791 | int rv = handle->Init( |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 1792 | TestGroupId("a"), |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1793 | ClientSocketPool::SocketParams::CreateForHttpForTesting(), absl::nullopt, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1794 | LOWEST, SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 1795 | nested_callback->callback(), ClientSocketPool::ProxyAuthCallback(), pool, |
| 1796 | NetLogWithSource()); |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1797 | if (rv != ERR_IO_PENDING) { |
| 1798 | DCHECK_EQ(TestConnectJob::kMockJob, next_job_type); |
Bence Béky | a4a5093 | 2018-08-10 13:39:41 | [diff] [blame] | 1799 | nested_callback->callback().Run(rv); |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1800 | } else { |
| 1801 | DCHECK_EQ(TestConnectJob::kMockPendingJob, next_job_type); |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1802 | } |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1803 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1804 | |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1805 | // Tests the case where a second socket is requested in a completion callback, |
| 1806 | // and the second socket connects asynchronously. Reuses the same |
| 1807 | // ClientSocketHandle for the second socket, after disconnecting the first. |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1808 | TEST_F(ClientSocketPoolBaseTest, RequestPendingJobTwice) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1809 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1810 | |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 1811 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 1812 | ClientSocketHandle handle; |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1813 | TestCompletionCallback second_result_callback; |
| 1814 | int rv = handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1815 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Paul Jensen | 8d6f87ec | 2018-01-13 00:46:54 | [diff] [blame] | 1816 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1817 | base::BindOnce(&RequestSocketOnComplete, &handle, pool_.get(), |
| 1818 | connect_job_factory_, TestConnectJob::kMockPendingJob, |
| 1819 | &second_result_callback), |
| 1820 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1821 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1822 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1823 | EXPECT_THAT(second_result_callback.WaitForResult(), IsOk()); |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1824 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1825 | |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1826 | // Tests the case where a second socket is requested in a completion callback, |
| 1827 | // and the second socket connects synchronously. Reuses the same |
| 1828 | // ClientSocketHandle for the second socket, after disconnecting the first. |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1829 | TEST_F(ClientSocketPoolBaseTest, RequestPendingJobThenSynchronous) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1830 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1831 | |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 1832 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 1833 | ClientSocketHandle handle; |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1834 | TestCompletionCallback second_result_callback; |
| 1835 | int rv = handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1836 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Paul Jensen | 8d6f87ec | 2018-01-13 00:46:54 | [diff] [blame] | 1837 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1838 | base::BindOnce(&RequestSocketOnComplete, &handle, pool_.get(), |
| 1839 | connect_job_factory_, TestConnectJob::kMockPendingJob, |
| 1840 | &second_result_callback), |
| 1841 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1842 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1843 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1844 | EXPECT_THAT(second_result_callback.WaitForResult(), IsOk()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1845 | } |
| 1846 | |
| 1847 | // Make sure that pending requests get serviced after active requests get |
| 1848 | // cancelled. |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1849 | TEST_F(ClientSocketPoolBaseTest, CancelActiveRequestWithPendingRequests) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1850 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1851 | |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 1852 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1853 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1854 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1855 | IsError(ERR_IO_PENDING)); |
| 1856 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1857 | IsError(ERR_IO_PENDING)); |
| 1858 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1859 | IsError(ERR_IO_PENDING)); |
| 1860 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1861 | IsError(ERR_IO_PENDING)); |
| 1862 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1863 | IsError(ERR_IO_PENDING)); |
| 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)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1868 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1869 | // Now, kDefaultMaxSocketsPerGroup requests should be active. |
| 1870 | // Let's cancel them. |
| 1871 | for (int i = 0; i < kDefaultMaxSocketsPerGroup; ++i) { |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1872 | ASSERT_FALSE(request(i)->handle()->is_initialized()); |
| 1873 | request(i)->handle()->Reset(); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1874 | } |
| 1875 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1876 | // Let's wait for the rest to complete now. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1877 | for (size_t i = kDefaultMaxSocketsPerGroup; i < requests_size(); ++i) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1878 | EXPECT_THAT(request(i)->WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1879 | request(i)->handle()->Reset(); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1880 | } |
| 1881 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1882 | EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup, |
| 1883 | completion_count()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1884 | } |
| 1885 | |
| 1886 | // Make sure that pending requests get serviced after active requests fail. |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1887 | TEST_F(ClientSocketPoolBaseTest, FailingActiveRequestWithPendingRequests) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1888 | const size_t kMaxSockets = 5; |
| 1889 | CreatePool(kMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1890 | |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 1891 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1892 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1893 | const size_t kNumberOfRequests = 2 * kDefaultMaxSocketsPerGroup + 1; |
| 1894 | ASSERT_LE(kNumberOfRequests, kMaxSockets); // Otherwise the test will hang. |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1895 | |
| 1896 | // Queue up all the requests |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1897 | for (size_t i = 0; i < kNumberOfRequests; ++i) |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1898 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1899 | IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1900 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1901 | for (size_t i = 0; i < kNumberOfRequests; ++i) |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1902 | EXPECT_THAT(request(i)->WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1903 | } |
| 1904 | |
mmenke | 9d72fe4 | 2017-05-18 22:36:07 | [diff] [blame] | 1905 | // Make sure that pending requests that complete synchronously get serviced |
| 1906 | // after active requests fail. See https://crbug.com/723748 |
| 1907 | TEST_F(ClientSocketPoolBaseTest, HandleMultipleSyncFailuresAfterAsyncFailure) { |
| 1908 | const size_t kNumberOfRequests = 10; |
| 1909 | const size_t kMaxSockets = 1; |
| 1910 | CreatePool(kMaxSockets, kMaxSockets); |
| 1911 | |
| 1912 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
| 1913 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1914 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1915 | IsError(ERR_IO_PENDING)); |
mmenke | 9d72fe4 | 2017-05-18 22:36:07 | [diff] [blame] | 1916 | |
| 1917 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
| 1918 | |
| 1919 | // Queue up all the other requests |
| 1920 | for (size_t i = 1; i < kNumberOfRequests; ++i) |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1921 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1922 | IsError(ERR_IO_PENDING)); |
mmenke | 9d72fe4 | 2017-05-18 22:36:07 | [diff] [blame] | 1923 | |
| 1924 | // Make sure all requests fail, instead of hanging. |
| 1925 | for (size_t i = 0; i < kNumberOfRequests; ++i) |
| 1926 | EXPECT_THAT(request(i)->WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 1927 | } |
| 1928 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1929 | TEST_F(ClientSocketPoolBaseTest, CancelActiveRequestThenRequestSocket) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1930 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1931 | |
| 1932 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1933 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1934 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1935 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1936 | int rv = handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1937 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1938 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1939 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1940 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1941 | |
| 1942 | // Cancel the active request. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1943 | handle.Reset(); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1944 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1945 | rv = handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1946 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1947 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1948 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1949 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 1950 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1951 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1952 | EXPECT_FALSE(handle.is_reused()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 1953 | TestLoadTimingInfoConnectedNotReused(handle); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1954 | EXPECT_EQ(2, client_socket_factory_.allocation_count()); |
| 1955 | } |
| 1956 | |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1957 | TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketsForced) { |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 1958 | const char kReason[] = "Really nifty reason"; |
| 1959 | |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1960 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1961 | ClientSocketHandle handle; |
| 1962 | TestCompletionCallback callback; |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 1963 | RecordingBoundTestNetLog log; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1964 | int rv = handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1965 | TestGroupId("a"), params_, absl::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1966 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1967 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1968 | EXPECT_THAT(rv, IsOk()); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 1969 | ASSERT_TRUE(handle.socket()); |
| 1970 | NetLogSource source = handle.socket()->NetLog().source(); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1971 | handle.Reset(); |
| 1972 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 1973 | pool_->CloseIdleSockets(kReason); |
| 1974 | ExpectSocketClosedWithReason(source, kReason); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1975 | } |
| 1976 | |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1977 | TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketsInGroupForced) { |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1978 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1979 | TestCompletionCallback callback; |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 1980 | RecordingBoundTestNetLog log; |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1981 | ClientSocketHandle handle1; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1982 | int rv = handle1.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1983 | TestGroupId("a"), params_, absl::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1984 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1985 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1986 | EXPECT_THAT(rv, IsOk()); |
| 1987 | ClientSocketHandle handle2; |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1988 | rv = handle2.Init(TestGroupId("a"), params_, absl::nullopt, LOWEST, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1989 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1990 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1991 | pool_.get(), log.bound()); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1992 | ClientSocketHandle handle3; |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1993 | rv = handle3.Init(TestGroupId("b"), params_, absl::nullopt, LOWEST, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1994 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1995 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1996 | pool_.get(), log.bound()); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1997 | EXPECT_THAT(rv, IsOk()); |
| 1998 | handle1.Reset(); |
| 1999 | handle2.Reset(); |
| 2000 | handle3.Reset(); |
| 2001 | EXPECT_EQ(3, pool_->IdleSocketCount()); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 2002 | pool_->CloseIdleSocketsInGroup(TestGroupId("a"), "Very good reason"); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 2003 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 2004 | } |
| 2005 | |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 2006 | TEST_F(ClientSocketPoolBaseTest, CleanUpUnusableIdleSockets) { |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 2007 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2008 | ClientSocketHandle handle; |
| 2009 | TestCompletionCallback callback; |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 2010 | RecordingBoundTestNetLog log; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2011 | int rv = handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2012 | TestGroupId("a"), params_, absl::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2013 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2014 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 2015 | EXPECT_THAT(rv, IsOk()); |
| 2016 | StreamSocket* socket = handle.socket(); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 2017 | ASSERT_TRUE(socket); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 2018 | handle.Reset(); |
| 2019 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 2020 | |
| 2021 | // Disconnect socket now to make the socket unusable. |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 2022 | NetLogSource source = socket->NetLog().source(); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 2023 | socket->Disconnect(); |
| 2024 | ClientSocketHandle handle2; |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2025 | rv = handle2.Init(TestGroupId("a"), params_, absl::nullopt, LOWEST, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2026 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2027 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2028 | pool_.get(), log.bound()); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 2029 | EXPECT_THAT(rv, IsOk()); |
| 2030 | EXPECT_FALSE(handle2.is_reused()); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 2031 | |
| 2032 | // This is admittedly not an accurate error in this case, but normally code |
| 2033 | // doesn't secretly keep a raw pointers to sockets returned to the socket pool |
| 2034 | // and close them out of band, so discovering an idle socket was closed when |
| 2035 | // trying to reuse it normally means it was closed by the remote side. |
| 2036 | ExpectSocketClosedWithReason( |
| 2037 | source, TransportClientSocketPool::kRemoteSideClosedConnection); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 2038 | } |
| 2039 | |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 2040 | // Regression test for http://crbug.com/17985. |
| 2041 | TEST_F(ClientSocketPoolBaseTest, GroupWithPendingRequestsIsNotEmpty) { |
| 2042 | const int kMaxSockets = 3; |
| 2043 | const int kMaxSocketsPerGroup = 2; |
| 2044 | CreatePool(kMaxSockets, kMaxSocketsPerGroup); |
| 2045 | |
[email protected] | ac790b4 | 2009-12-02 04:31:31 | [diff] [blame] | 2046 | const RequestPriority kHighPriority = HIGHEST; |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 2047 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2048 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 2049 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 2050 | |
| 2051 | // This is going to be a pending request in an otherwise empty group. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2052 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2053 | IsError(ERR_IO_PENDING)); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 2054 | |
| 2055 | // Reach the maximum socket limit. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2056 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 2057 | |
| 2058 | // Create a stalled group with high priorities. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2059 | EXPECT_THAT(StartRequest(TestGroupId("c"), kHighPriority), |
| 2060 | IsError(ERR_IO_PENDING)); |
| 2061 | EXPECT_THAT(StartRequest(TestGroupId("c"), kHighPriority), |
| 2062 | IsError(ERR_IO_PENDING)); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 2063 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2064 | // Release the first two sockets from TestGroupId("a"). Because this is a |
| 2065 | // keepalive, the first release will unblock the pending request for |
| 2066 | // TestGroupId("a"). The second release will unblock a request for "c", |
| 2067 | // because it is the next high priority socket. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2068 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
| 2069 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 2070 | |
| 2071 | // Closing idle sockets should not get us into trouble, but in the bug |
| 2072 | // we were hitting a CHECK here. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2073 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 2074 | pool_->CloseIdleSockets("Very good reason"); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 2075 | |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 2076 | // Run the released socket wakeups. |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 2077 | base::RunLoop().RunUntilIdle(); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 2078 | } |
| 2079 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2080 | TEST_F(ClientSocketPoolBaseTest, BasicAsynchronous) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2081 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2082 | |
| 2083 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2084 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2085 | TestCompletionCallback callback; |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 2086 | RecordingBoundTestNetLog log; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2087 | int rv = handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2088 | TestGroupId("a"), params_, absl::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2089 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2090 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2091 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2092 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2093 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 2094 | TestLoadTimingInfoNotConnected(handle); |
| 2095 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2096 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2097 | EXPECT_TRUE(handle.is_initialized()); |
| 2098 | EXPECT_TRUE(handle.socket()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 2099 | TestLoadTimingInfoConnectedNotReused(handle); |
| 2100 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2101 | handle.Reset(); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 2102 | TestLoadTimingInfoNotConnected(handle); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 2103 | |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 2104 | auto entries = log.GetEntries(); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 2105 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2106 | EXPECT_EQ(5u, entries.size()); |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 2107 | EXPECT_TRUE(LogContainsEvent( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2108 | entries, 0, NetLogEventType::TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 2109 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2110 | EXPECT_TRUE(LogContainsBeginEvent(entries, 1, NetLogEventType::SOCKET_POOL)); |
| 2111 | EXPECT_TRUE(LogContainsEvent( |
| 2112 | entries, 2, NetLogEventType::SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
| 2113 | NetLogEventPhase::NONE)); |
| 2114 | EXPECT_TRUE(LogContainsEvent(entries, 3, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 2115 | NetLogEventType::SOCKET_POOL_BOUND_TO_SOCKET, |
| 2116 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2117 | EXPECT_TRUE(LogContainsEndEvent(entries, 4, NetLogEventType::SOCKET_POOL)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2118 | } |
| 2119 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2120 | TEST_F(ClientSocketPoolBaseTest, |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2121 | InitConnectionAsynchronousFailure) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2122 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2123 | |
| 2124 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2125 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2126 | TestCompletionCallback callback; |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 2127 | RecordingBoundTestNetLog log; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2128 | // Set the additional error state members to ensure that they get cleared. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2129 | handle.set_is_ssl_error(true); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 2130 | handle.set_ssl_cert_request_info(base::MakeRefCounted<SSLCertRequestInfo>()); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2131 | EXPECT_EQ( |
| 2132 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2133 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2134 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2135 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2136 | pool_.get(), log.bound())); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2137 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2138 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2139 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2140 | EXPECT_FALSE(handle.is_ssl_error()); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 2141 | EXPECT_FALSE(handle.ssl_cert_request_info()); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 2142 | |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 2143 | auto entries = log.GetEntries(); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 2144 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2145 | EXPECT_EQ(4u, entries.size()); |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 2146 | EXPECT_TRUE(LogContainsEvent( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2147 | entries, 0, NetLogEventType::TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 2148 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2149 | EXPECT_TRUE(LogContainsBeginEvent(entries, 1, NetLogEventType::SOCKET_POOL)); |
| 2150 | EXPECT_TRUE(LogContainsEvent( |
| 2151 | entries, 2, NetLogEventType::SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
| 2152 | NetLogEventPhase::NONE)); |
| 2153 | EXPECT_TRUE(LogContainsEndEvent(entries, 3, NetLogEventType::SOCKET_POOL)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2154 | } |
| 2155 | |
mmenke | 6be122f | 2015-03-09 22:22:47 | [diff] [blame] | 2156 | // Check that an async ConnectJob failure does not result in creation of a new |
| 2157 | // ConnectJob when there's another pending request also waiting on its own |
| 2158 | // ConnectJob. See http://crbug.com/463960. |
| 2159 | TEST_F(ClientSocketPoolBaseTest, AsyncFailureWithPendingRequestWithJob) { |
| 2160 | CreatePool(2, 2); |
| 2161 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
| 2162 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2163 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2164 | IsError(ERR_IO_PENDING)); |
| 2165 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2166 | IsError(ERR_IO_PENDING)); |
mmenke | 6be122f | 2015-03-09 22:22:47 | [diff] [blame] | 2167 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2168 | EXPECT_THAT(request(0)->WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 2169 | EXPECT_THAT(request(1)->WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
mmenke | 6be122f | 2015-03-09 22:22:47 | [diff] [blame] | 2170 | |
| 2171 | EXPECT_EQ(2, client_socket_factory_.allocation_count()); |
| 2172 | } |
| 2173 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2174 | TEST_F(ClientSocketPoolBaseTest, TwoRequestsCancelOne) { |
[email protected] | b22b516 | 2010-03-16 07:53:10 | [diff] [blame] | 2175 | // TODO(eroman): Add back the log expectations! Removed them because the |
| 2176 | // ordering is difficult, and some may fire during destructor. |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2177 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2178 | |
| 2179 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2180 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2181 | TestCompletionCallback callback; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2182 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2183 | TestCompletionCallback callback2; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2184 | |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2185 | EXPECT_EQ( |
| 2186 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2187 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2188 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2189 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2190 | pool_.get(), NetLogWithSource())); |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 2191 | RecordingBoundTestNetLog log2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 2192 | EXPECT_EQ( |
| 2193 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2194 | handle2.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2195 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2196 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2197 | pool_.get(), NetLogWithSource())); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2198 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2199 | handle.Reset(); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2200 | |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 2201 | |
| 2202 | // 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] | 2203 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2204 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2205 | handle2.Reset(); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 2206 | |
| 2207 | // Now request 2 has actually finished. |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 2208 | // TODO(eroman): Add back log expectations. |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2209 | } |
| 2210 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2211 | TEST_F(ClientSocketPoolBaseTest, CancelRequestLimitsJobs) { |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 2212 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2213 | |
[email protected] | 17a0c6c | 2009-08-04 00:07:04 | [diff] [blame] | 2214 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2215 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2216 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
| 2217 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 2218 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 2219 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 2220 | |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2221 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2222 | static_cast<int>( |
| 2223 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2224 | (*requests())[2]->handle()->Reset(); |
| 2225 | (*requests())[3]->handle()->Reset(); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2226 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2227 | static_cast<int>( |
| 2228 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 2229 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2230 | (*requests())[1]->handle()->Reset(); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2231 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2232 | static_cast<int>( |
| 2233 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 2234 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2235 | (*requests())[0]->handle()->Reset(); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2236 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2237 | static_cast<int>( |
| 2238 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 2239 | } |
| 2240 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2241 | // When requests and ConnectJobs are not coupled, the request will get serviced |
| 2242 | // by whatever comes first. |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2243 | TEST_F(ClientSocketPoolBaseTest, ReleaseSockets) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2244 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2245 | |
| 2246 | // Start job 1 (async OK) |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 2247 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2248 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2249 | std::vector<TestSocketRequest*> request_order; |
| 2250 | size_t completion_count; // unused |
| 2251 | TestSocketRequest req1(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2252 | int rv = req1.handle()->Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2253 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2254 | ClientSocketPool::RespectLimits::ENABLED, req1.callback(), |
| 2255 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2256 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 2257 | EXPECT_THAT(req1.WaitForResult(), IsOk()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2258 | |
| 2259 | // Job 1 finished OK. Start job 2 (also async OK). Request 3 is pending |
| 2260 | // without a job. |
| 2261 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2262 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2263 | TestSocketRequest req2(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2264 | rv = req2.handle()->Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2265 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2266 | ClientSocketPool::RespectLimits::ENABLED, req2.callback(), |
| 2267 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2268 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2269 | TestSocketRequest req3(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2270 | rv = req3.handle()->Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2271 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2272 | ClientSocketPool::RespectLimits::ENABLED, req3.callback(), |
| 2273 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2274 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2275 | |
| 2276 | // Both Requests 2 and 3 are pending. We release socket 1 which should |
| 2277 | // service request 2. Request 3 should still be waiting. |
[email protected] | a6c59f6 | 2009-07-29 16:33:33 | [diff] [blame] | 2278 | req1.handle()->Reset(); |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 2279 | // Run the released socket wakeups. |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 2280 | base::RunLoop().RunUntilIdle(); |
[email protected] | a6c59f6 | 2009-07-29 16:33:33 | [diff] [blame] | 2281 | ASSERT_TRUE(req2.handle()->socket()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2282 | EXPECT_THAT(req2.WaitForResult(), IsOk()); |
[email protected] | a6c59f6 | 2009-07-29 16:33:33 | [diff] [blame] | 2283 | EXPECT_FALSE(req3.handle()->socket()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2284 | |
| 2285 | // Signal job 2, which should service request 3. |
| 2286 | |
| 2287 | client_socket_factory_.SignalJobs(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2288 | EXPECT_THAT(req3.WaitForResult(), IsOk()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2289 | |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2290 | ASSERT_EQ(3u, request_order.size()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2291 | EXPECT_EQ(&req1, request_order[0]); |
| 2292 | EXPECT_EQ(&req2, request_order[1]); |
| 2293 | EXPECT_EQ(&req3, request_order[2]); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2294 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2295 | } |
| 2296 | |
| 2297 | // The requests are not coupled to the jobs. So, the requests should finish in |
| 2298 | // their priority / insertion order. |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2299 | TEST_F(ClientSocketPoolBaseTest, PendingJobCompletionOrder) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2300 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2301 | // First two jobs are async. |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 2302 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2303 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2304 | std::vector<TestSocketRequest*> request_order; |
| 2305 | size_t completion_count; // unused |
| 2306 | TestSocketRequest req1(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2307 | int rv = req1.handle()->Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2308 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2309 | ClientSocketPool::RespectLimits::ENABLED, req1.callback(), |
| 2310 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2311 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2312 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2313 | TestSocketRequest req2(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2314 | rv = req2.handle()->Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2315 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2316 | ClientSocketPool::RespectLimits::ENABLED, req2.callback(), |
| 2317 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2318 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2319 | |
| 2320 | // The pending job is sync. |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 2321 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2322 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2323 | TestSocketRequest req3(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2324 | rv = req3.handle()->Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2325 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2326 | ClientSocketPool::RespectLimits::ENABLED, req3.callback(), |
| 2327 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2328 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2329 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2330 | EXPECT_THAT(req1.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 2331 | EXPECT_THAT(req2.WaitForResult(), IsOk()); |
| 2332 | EXPECT_THAT(req3.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2333 | |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2334 | ASSERT_EQ(3u, request_order.size()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2335 | EXPECT_EQ(&req1, request_order[0]); |
| 2336 | EXPECT_EQ(&req2, request_order[1]); |
| 2337 | EXPECT_EQ(&req3, request_order[2]); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2338 | } |
| 2339 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2340 | // Test GetLoadState in the case there's only one socket request. |
| 2341 | TEST_F(ClientSocketPoolBaseTest, LoadStateOneRequest) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2342 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2343 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2344 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2345 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2346 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2347 | int rv = handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2348 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2349 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2350 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2351 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2352 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2353 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2354 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE); |
| 2355 | EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle.GetLoadState()); |
| 2356 | |
| 2357 | // No point in completing the connection, since ClientSocketHandles only |
| 2358 | // expect the LoadState to be checked while connecting. |
| 2359 | } |
| 2360 | |
| 2361 | // Test GetLoadState in the case there are two socket requests. |
| 2362 | TEST_F(ClientSocketPoolBaseTest, LoadStateTwoRequests) { |
| 2363 | CreatePool(2, 2); |
| 2364 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2365 | |
| 2366 | ClientSocketHandle handle; |
| 2367 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2368 | int rv = handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2369 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2370 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2371 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2372 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2373 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_RESOLVING_HOST); |
| 2374 | |
| 2375 | ClientSocketHandle handle2; |
| 2376 | TestCompletionCallback callback2; |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2377 | rv = handle2.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2378 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2379 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2380 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2381 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2382 | client_socket_factory_.SetJobLoadState(1, LOAD_STATE_RESOLVING_HOST); |
| 2383 | |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 2384 | // Each handle should reflect the state of its own job. |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2385 | EXPECT_EQ(LOAD_STATE_RESOLVING_HOST, handle.GetLoadState()); |
| 2386 | EXPECT_EQ(LOAD_STATE_RESOLVING_HOST, handle2.GetLoadState()); |
| 2387 | |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 2388 | // Update the state of the first job. |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2389 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_CONNECTING); |
| 2390 | |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 2391 | // Only the state of the first request should have changed. |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2392 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2393 | EXPECT_EQ(LOAD_STATE_RESOLVING_HOST, handle2.GetLoadState()); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 2394 | |
| 2395 | // Update the state of the second job. |
| 2396 | client_socket_factory_.SetJobLoadState(1, LOAD_STATE_SSL_HANDSHAKE); |
| 2397 | |
| 2398 | // Only the state of the second request should have changed. |
| 2399 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
| 2400 | EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle2.GetLoadState()); |
| 2401 | |
| 2402 | // Second job connects and the first request gets the socket. The |
| 2403 | // second handle switches to the state of the remaining ConnectJob. |
| 2404 | client_socket_factory_.SignalJob(1); |
| 2405 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 2406 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle2.GetLoadState()); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2407 | } |
| 2408 | |
| 2409 | // Test GetLoadState in the case the per-group limit is reached. |
| 2410 | TEST_F(ClientSocketPoolBaseTest, LoadStateGroupLimit) { |
| 2411 | CreatePool(2, 1); |
| 2412 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2413 | |
| 2414 | ClientSocketHandle handle; |
| 2415 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2416 | int rv = handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2417 | TestGroupId("a"), params_, absl::nullopt, MEDIUM, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2418 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2419 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2420 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2421 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
| 2422 | |
| 2423 | // Request another socket from the same pool, buth with a higher priority. |
| 2424 | // The first request should now be stalled at the socket group limit. |
| 2425 | ClientSocketHandle handle2; |
| 2426 | TestCompletionCallback callback2; |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2427 | rv = handle2.Init(TestGroupId("a"), params_, absl::nullopt, HIGHEST, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2428 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2429 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2430 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2431 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2432 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET, handle.GetLoadState()); |
| 2433 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle2.GetLoadState()); |
| 2434 | |
| 2435 | // The first handle should remain stalled as the other socket goes through |
| 2436 | // the connect process. |
| 2437 | |
| 2438 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE); |
| 2439 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET, handle.GetLoadState()); |
| 2440 | EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle2.GetLoadState()); |
| 2441 | |
| 2442 | client_socket_factory_.SignalJob(0); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2443 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2444 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET, handle.GetLoadState()); |
| 2445 | |
| 2446 | // Closing the second socket should cause the stalled handle to finally get a |
| 2447 | // ConnectJob. |
| 2448 | handle2.socket()->Disconnect(); |
| 2449 | handle2.Reset(); |
| 2450 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
| 2451 | } |
| 2452 | |
| 2453 | // Test GetLoadState in the case the per-pool limit is reached. |
| 2454 | TEST_F(ClientSocketPoolBaseTest, LoadStatePoolLimit) { |
| 2455 | CreatePool(2, 2); |
| 2456 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2457 | |
| 2458 | ClientSocketHandle handle; |
| 2459 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2460 | int rv = handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2461 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2462 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2463 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2464 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2465 | |
| 2466 | // Request for socket from another pool. |
| 2467 | ClientSocketHandle handle2; |
| 2468 | TestCompletionCallback callback2; |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2469 | rv = handle2.Init(TestGroupId("b"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2470 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2471 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2472 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2473 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2474 | |
| 2475 | // Request another socket from the first pool. Request should stall at the |
| 2476 | // socket pool limit. |
| 2477 | ClientSocketHandle handle3; |
| 2478 | TestCompletionCallback callback3; |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2479 | rv = handle3.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2480 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2481 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2482 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2483 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2484 | |
| 2485 | // The third handle should remain stalled as the other sockets in its group |
| 2486 | // goes through the connect process. |
| 2487 | |
| 2488 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
| 2489 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL, handle3.GetLoadState()); |
| 2490 | |
| 2491 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE); |
| 2492 | EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle.GetLoadState()); |
| 2493 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL, handle3.GetLoadState()); |
| 2494 | |
| 2495 | client_socket_factory_.SignalJob(0); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2496 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2497 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL, handle3.GetLoadState()); |
| 2498 | |
| 2499 | // Closing a socket should allow the stalled handle to finally get a new |
| 2500 | // ConnectJob. |
| 2501 | handle.socket()->Disconnect(); |
| 2502 | handle.Reset(); |
| 2503 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle3.GetLoadState()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2504 | } |
| 2505 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2506 | TEST_F(ClientSocketPoolBaseTest, CertError) { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2507 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2508 | connect_job_factory_->set_job_type(TestConnectJob::kMockCertErrorJob); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2509 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2510 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2511 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2512 | EXPECT_EQ( |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2513 | ERR_CERT_COMMON_NAME_INVALID, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2514 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2515 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2516 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2517 | pool_.get(), NetLogWithSource())); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2518 | EXPECT_TRUE(handle.is_initialized()); |
| 2519 | EXPECT_TRUE(handle.socket()); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2520 | } |
| 2521 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2522 | TEST_F(ClientSocketPoolBaseTest, AsyncCertError) { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2523 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2524 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2525 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingCertErrorJob); |
[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( |
| 2529 | ERR_IO_PENDING, |
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())); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2534 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2535 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2536 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_CERT_COMMON_NAME_INVALID)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2537 | EXPECT_TRUE(handle.is_initialized()); |
| 2538 | EXPECT_TRUE(handle.socket()); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2539 | } |
| 2540 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2541 | TEST_F(ClientSocketPoolBaseTest, AdditionalErrorStateSynchronous) { |
| 2542 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2543 | connect_job_factory_->set_job_type( |
| 2544 | TestConnectJob::kMockAdditionalErrorStateJob); |
| 2545 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2546 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2547 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2548 | EXPECT_EQ( |
| 2549 | ERR_CONNECTION_FAILED, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2550 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2551 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2552 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2553 | pool_.get(), NetLogWithSource())); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2554 | EXPECT_FALSE(handle.is_initialized()); |
| 2555 | EXPECT_FALSE(handle.socket()); |
| 2556 | EXPECT_TRUE(handle.is_ssl_error()); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 2557 | EXPECT_TRUE(handle.ssl_cert_request_info()); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2558 | } |
| 2559 | |
| 2560 | TEST_F(ClientSocketPoolBaseTest, AdditionalErrorStateAsynchronous) { |
| 2561 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2562 | |
| 2563 | connect_job_factory_->set_job_type( |
| 2564 | TestConnectJob::kMockPendingAdditionalErrorStateJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2565 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2566 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2567 | EXPECT_EQ( |
| 2568 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2569 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2570 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2571 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2572 | pool_.get(), NetLogWithSource())); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2573 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2574 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2575 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2576 | EXPECT_FALSE(handle.is_initialized()); |
| 2577 | EXPECT_FALSE(handle.socket()); |
| 2578 | EXPECT_TRUE(handle.is_ssl_error()); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 2579 | EXPECT_TRUE(handle.ssl_cert_request_info()); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2580 | } |
| 2581 | |
martijn | 003cd61 | 2016-05-19 22:24:38 | [diff] [blame] | 2582 | // Make sure we can reuse sockets. |
| 2583 | TEST_F(ClientSocketPoolBaseTest, CleanupTimedOutIdleSocketsReuse) { |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2584 | CreatePoolWithIdleTimeouts( |
| 2585 | kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2586 | base::TimeDelta(), // Time out unused sockets immediately. |
| 2587 | base::TimeDelta::FromDays(1)); // Don't time out used sockets. |
| 2588 | |
| 2589 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2590 | |
| 2591 | ClientSocketHandle handle; |
| 2592 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2593 | int rv = handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2594 | TestGroupId("a"), params_, absl::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2595 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2596 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2597 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2598 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2599 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2600 | ASSERT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2601 | |
| 2602 | // Use and release the socket. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 2603 | EXPECT_EQ(1, handle.socket()->Write(nullptr, 1, CompletionOnceCallback(), |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 2604 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 2605 | TestLoadTimingInfoConnectedNotReused(handle); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2606 | handle.Reset(); |
| 2607 | |
| 2608 | // Should now have one idle socket. |
| 2609 | ASSERT_EQ(1, pool_->IdleSocketCount()); |
| 2610 | |
| 2611 | // Request a new socket. This should reuse the old socket and complete |
| 2612 | // synchronously. |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 2613 | RecordingBoundTestNetLog log; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2614 | rv = handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2615 | TestGroupId("a"), params_, absl::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2616 | ClientSocketPool::RespectLimits::ENABLED, CompletionOnceCallback(), |
| 2617 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2618 | ASSERT_THAT(rv, IsOk()); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2619 | EXPECT_TRUE(handle.is_reused()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 2620 | TestLoadTimingInfoConnectedReused(handle); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2621 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2622 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2623 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2624 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2625 | |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 2626 | auto entries = log.GetEntries(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2627 | EXPECT_TRUE(LogContainsEvent( |
| 2628 | entries, 0, NetLogEventType::TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET, |
| 2629 | NetLogEventPhase::NONE)); |
| 2630 | EXPECT_TRUE(LogContainsBeginEvent(entries, 1, NetLogEventType::SOCKET_POOL)); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2631 | EXPECT_TRUE(LogContainsEntryWithType( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2632 | entries, 2, NetLogEventType::SOCKET_POOL_REUSED_AN_EXISTING_SOCKET)); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2633 | } |
| 2634 | |
martijn | 003cd61 | 2016-05-19 22:24:38 | [diff] [blame] | 2635 | // Make sure we cleanup old unused sockets. |
Eric Roman | b49715e | 2018-04-24 22:41:17 | [diff] [blame] | 2636 | TEST_F(ClientSocketPoolBaseTest, CleanupTimedOutIdleSocketsNoReuse) { |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2637 | CreatePoolWithIdleTimeouts( |
| 2638 | kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 2639 | base::TimeDelta(), // Time out unused sockets immediately |
| 2640 | base::TimeDelta()); // Time out used sockets immediately |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2641 | |
| 2642 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2643 | |
| 2644 | // Startup two mock pending connect jobs, which will sit in the MessageLoop. |
| 2645 | |
| 2646 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2647 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2648 | int rv = handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2649 | TestGroupId("a"), params_, absl::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2650 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2651 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2652 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2653 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2654 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2655 | |
| 2656 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2657 | TestCompletionCallback callback2; |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2658 | rv = handle2.Init(TestGroupId("a"), params_, absl::nullopt, LOWEST, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2659 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2660 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2661 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2662 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2663 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2664 | pool_->GetLoadState(TestGroupId("a"), &handle2)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2665 | |
| 2666 | // Cancel one of the requests. Wait for the other, which will get the first |
| 2667 | // job. Release the socket. Run the loop again to make sure the second |
| 2668 | // socket is sitting idle and the first one is released (since ReleaseSocket() |
| 2669 | // just posts a DoReleaseSocket() task). |
| 2670 | |
| 2671 | handle.Reset(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2672 | ASSERT_THAT(callback2.WaitForResult(), IsOk()); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 2673 | // Get the NetLogSource for the socket, so the time out reason can be checked |
| 2674 | // at the end of the test. |
| 2675 | NetLogSource net_log_source2 = handle2.socket()->NetLog().source(); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2676 | // Use the socket. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 2677 | EXPECT_EQ(1, handle2.socket()->Write(nullptr, 1, CompletionOnceCallback(), |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 2678 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2679 | handle2.Reset(); |
| 2680 | |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2681 | // We post all of our delayed tasks with a 2ms delay. I.e. they don't |
| 2682 | // actually become pending until 2ms after they have been created. In order |
| 2683 | // to flush all tasks, we need to wait so that we know there are no |
| 2684 | // soon-to-be-pending tasks waiting. |
Alex Clarke | 0def209 | 2018-12-10 12:01:45 | [diff] [blame] | 2685 | FastForwardBy(base::TimeDelta::FromMilliseconds(10)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2686 | |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2687 | // Both sockets should now be idle. |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2688 | ASSERT_EQ(2, pool_->IdleSocketCount()); |
| 2689 | |
| 2690 | // Request a new socket. This should cleanup the unused and timed out ones. |
| 2691 | // A new socket will be created rather than reusing the idle one. |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 2692 | RecordingBoundTestNetLog log; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2693 | TestCompletionCallback callback3; |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2694 | rv = handle.Init(TestGroupId("a"), params_, absl::nullopt, LOWEST, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2695 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2696 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2697 | pool_.get(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2698 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 2699 | ASSERT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2700 | EXPECT_FALSE(handle.is_reused()); |
| 2701 | |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2702 | // Make sure the idle socket is closed. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2703 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2704 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2705 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2706 | |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 2707 | auto entries = log.GetEntries(); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2708 | EXPECT_FALSE(LogContainsEntryWithType( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 2709 | entries, 1, NetLogEventType::SOCKET_POOL_REUSED_AN_EXISTING_SOCKET)); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 2710 | ExpectSocketClosedWithReason( |
| 2711 | net_log_source2, TransportClientSocketPool::kIdleTimeLimitExpired); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2712 | } |
| 2713 | |
[email protected] | 2041cf34 | 2010-02-19 03:15:59 | [diff] [blame] | 2714 | // Make sure that we process all pending requests even when we're stalling |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2715 | // because of multiple releasing disconnected sockets. |
| 2716 | TEST_F(ClientSocketPoolBaseTest, MultipleReleasingDisconnectedSockets) { |
| 2717 | CreatePoolWithIdleTimeouts( |
| 2718 | kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 2719 | base::TimeDelta(), // Time out unused sockets immediately. |
| 2720 | base::TimeDelta::FromDays(1)); // Don't time out used sockets. |
| 2721 | |
| 2722 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 2723 | |
| 2724 | // Startup 4 connect jobs. Two of them will be pending. |
| 2725 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2726 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2727 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2728 | int rv = handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2729 | TestGroupId("a"), params_, absl::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2730 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2731 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2732 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2733 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2734 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2735 | TestCompletionCallback callback2; |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2736 | rv = handle2.Init(TestGroupId("a"), params_, absl::nullopt, LOWEST, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2737 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2738 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2739 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2740 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2741 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2742 | ClientSocketHandle handle3; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2743 | TestCompletionCallback callback3; |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2744 | rv = handle3.Init(TestGroupId("a"), params_, absl::nullopt, LOWEST, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2745 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2746 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2747 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2748 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2749 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2750 | ClientSocketHandle handle4; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2751 | TestCompletionCallback callback4; |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2752 | rv = handle4.Init(TestGroupId("a"), params_, absl::nullopt, LOWEST, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2753 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2754 | callback4.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2755 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2756 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2757 | |
| 2758 | // Release two disconnected sockets. |
| 2759 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2760 | handle.socket()->Disconnect(); |
| 2761 | handle.Reset(); |
| 2762 | handle2.socket()->Disconnect(); |
| 2763 | handle2.Reset(); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2764 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2765 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2766 | EXPECT_FALSE(handle3.is_reused()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2767 | EXPECT_THAT(callback4.WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2768 | EXPECT_FALSE(handle4.is_reused()); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2769 | } |
| 2770 | |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2771 | // Regression test for http://crbug.com/42267. |
| 2772 | // When DoReleaseSocket() is processed for one socket, it is blocked because the |
| 2773 | // other stalled groups all have releasing sockets, so no progress can be made. |
| 2774 | TEST_F(ClientSocketPoolBaseTest, SocketLimitReleasingSockets) { |
| 2775 | CreatePoolWithIdleTimeouts( |
| 2776 | 4 /* socket limit */, 4 /* socket limit per group */, |
| 2777 | base::TimeDelta(), // Time out unused sockets immediately. |
| 2778 | base::TimeDelta::FromDays(1)); // Don't time out used sockets. |
| 2779 | |
| 2780 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 2781 | |
| 2782 | // Max out the socket limit with 2 per group. |
| 2783 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2784 | ClientSocketHandle handle_a[4]; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2785 | TestCompletionCallback callback_a[4]; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2786 | ClientSocketHandle handle_b[4]; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2787 | TestCompletionCallback callback_b[4]; |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2788 | |
| 2789 | for (int i = 0; i < 2; ++i) { |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2790 | EXPECT_EQ(OK, handle_a[i].Init(TestGroupId("a"), params_, absl::nullopt, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2791 | LOWEST, SocketTag(), |
| 2792 | ClientSocketPool::RespectLimits::ENABLED, |
| 2793 | callback_a[i].callback(), |
| 2794 | ClientSocketPool::ProxyAuthCallback(), |
| 2795 | pool_.get(), NetLogWithSource())); |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2796 | EXPECT_EQ(OK, handle_b[i].Init(TestGroupId("b"), params_, absl::nullopt, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2797 | LOWEST, SocketTag(), |
| 2798 | ClientSocketPool::RespectLimits::ENABLED, |
| 2799 | callback_b[i].callback(), |
| 2800 | ClientSocketPool::ProxyAuthCallback(), |
| 2801 | pool_.get(), NetLogWithSource())); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2802 | } |
[email protected] | b89f7e4 | 2010-05-20 20:37:00 | [diff] [blame] | 2803 | |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2804 | // Make 4 pending requests, 2 per group. |
| 2805 | |
| 2806 | for (int i = 2; i < 4; ++i) { |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2807 | EXPECT_EQ( |
| 2808 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2809 | handle_a[i].Init(TestGroupId("a"), params_, absl::nullopt, LOWEST, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2810 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2811 | callback_a[i].callback(), |
| 2812 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2813 | NetLogWithSource())); |
| 2814 | EXPECT_EQ( |
| 2815 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2816 | handle_b[i].Init(TestGroupId("b"), params_, absl::nullopt, LOWEST, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2817 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2818 | callback_b[i].callback(), |
| 2819 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2820 | NetLogWithSource())); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2821 | } |
| 2822 | |
| 2823 | // Release b's socket first. The order is important, because in |
| 2824 | // DoReleaseSocket(), we'll process b's released socket, and since both b and |
| 2825 | // a are stalled, but 'a' is lower lexicographically, we'll process group 'a' |
| 2826 | // first, which has a releasing socket, so it refuses to start up another |
| 2827 | // ConnectJob. So, we used to infinite loop on this. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2828 | handle_b[0].socket()->Disconnect(); |
| 2829 | handle_b[0].Reset(); |
| 2830 | handle_a[0].socket()->Disconnect(); |
| 2831 | handle_a[0].Reset(); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2832 | |
| 2833 | // Used to get stuck here. |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 2834 | base::RunLoop().RunUntilIdle(); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2835 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2836 | handle_b[1].socket()->Disconnect(); |
| 2837 | handle_b[1].Reset(); |
| 2838 | handle_a[1].socket()->Disconnect(); |
| 2839 | handle_a[1].Reset(); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2840 | |
| 2841 | for (int i = 2; i < 4; ++i) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2842 | EXPECT_THAT(callback_b[i].WaitForResult(), IsOk()); |
| 2843 | EXPECT_THAT(callback_a[i].WaitForResult(), IsOk()); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2844 | } |
| 2845 | } |
| 2846 | |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2847 | TEST_F(ClientSocketPoolBaseTest, |
| 2848 | ReleasingDisconnectedSocketsMaintainsPriorityOrder) { |
| 2849 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2850 | |
| 2851 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2852 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2853 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2854 | IsError(ERR_IO_PENDING)); |
| 2855 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2856 | IsError(ERR_IO_PENDING)); |
| 2857 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2858 | IsError(ERR_IO_PENDING)); |
| 2859 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2860 | IsError(ERR_IO_PENDING)); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2861 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2862 | EXPECT_THAT((*requests())[0]->WaitForResult(), IsOk()); |
| 2863 | EXPECT_THAT((*requests())[1]->WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2864 | EXPECT_EQ(2u, completion_count()); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2865 | |
| 2866 | // Releases one connection. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2867 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::NO_KEEP_ALIVE)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2868 | EXPECT_THAT((*requests())[2]->WaitForResult(), IsOk()); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2869 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2870 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::NO_KEEP_ALIVE)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2871 | EXPECT_THAT((*requests())[3]->WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2872 | EXPECT_EQ(4u, completion_count()); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2873 | |
| 2874 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 2875 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 2876 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 2877 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 2878 | |
| 2879 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2880 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(5)); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2881 | } |
| 2882 | |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2883 | class TestReleasingSocketRequest : public TestCompletionCallbackBase { |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2884 | public: |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2885 | TestReleasingSocketRequest(TransportClientSocketPool* pool, |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2886 | int expected_result, |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2887 | bool reset_releasing_handle) |
| 2888 | : pool_(pool), |
| 2889 | expected_result_(expected_result), |
Bence Béky | 8ddc249 | 2018-06-13 01:02:04 | [diff] [blame] | 2890 | reset_releasing_handle_(reset_releasing_handle) {} |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2891 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 2892 | ~TestReleasingSocketRequest() override = default; |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2893 | |
| 2894 | ClientSocketHandle* handle() { return &handle_; } |
| 2895 | |
Bence Béky | 8ddc249 | 2018-06-13 01:02:04 | [diff] [blame] | 2896 | CompletionOnceCallback callback() { |
| 2897 | return base::BindOnce(&TestReleasingSocketRequest::OnComplete, |
| 2898 | base::Unretained(this)); |
| 2899 | } |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2900 | |
| 2901 | private: |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2902 | void OnComplete(int result) { |
| 2903 | SetResult(result); |
| 2904 | if (reset_releasing_handle_) |
| 2905 | handle_.Reset(); |
| 2906 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2907 | EXPECT_EQ( |
| 2908 | expected_result_, |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 2909 | handle2_.Init( |
| 2910 | TestGroupId("a"), |
| 2911 | ClientSocketPool::SocketParams::CreateForHttpForTesting(), |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2912 | absl::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 2913 | ClientSocketPool::RespectLimits::ENABLED, CompletionOnceCallback(), |
| 2914 | ClientSocketPool::ProxyAuthCallback(), pool_, NetLogWithSource())); |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2915 | } |
| 2916 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2917 | TransportClientSocketPool* const pool_; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2918 | int expected_result_; |
| 2919 | bool reset_releasing_handle_; |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2920 | ClientSocketHandle handle_; |
| 2921 | ClientSocketHandle handle2_; |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2922 | }; |
| 2923 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2924 | |
| 2925 | TEST_F(ClientSocketPoolBaseTest, AdditionalErrorSocketsDontUseSlot) { |
| 2926 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2927 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2928 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
| 2929 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 2930 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2931 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2932 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2933 | client_socket_factory_.allocation_count()); |
| 2934 | |
| 2935 | connect_job_factory_->set_job_type( |
| 2936 | TestConnectJob::kMockPendingAdditionalErrorStateJob); |
| 2937 | TestReleasingSocketRequest req(pool_.get(), OK, false); |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2938 | EXPECT_EQ(ERR_IO_PENDING, |
| 2939 | req.handle()->Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2940 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2941 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2942 | req.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2943 | pool_.get(), NetLogWithSource())); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2944 | // The next job should complete synchronously |
| 2945 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 2946 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2947 | EXPECT_THAT(req.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2948 | EXPECT_FALSE(req.handle()->is_initialized()); |
| 2949 | EXPECT_FALSE(req.handle()->socket()); |
| 2950 | EXPECT_TRUE(req.handle()->is_ssl_error()); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 2951 | EXPECT_TRUE(req.handle()->ssl_cert_request_info()); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2952 | } |
| 2953 | |
[email protected] | b6501d3d | 2010-06-03 23:53:34 | [diff] [blame] | 2954 | // http://crbug.com/44724 regression test. |
| 2955 | // We start releasing the pool when we flush on network change. When that |
| 2956 | // happens, the only active references are in the ClientSocketHandles. When a |
| 2957 | // ConnectJob completes and calls back into the last ClientSocketHandle, that |
| 2958 | // callback can release the last reference and delete the pool. After the |
| 2959 | // callback finishes, we go back to the stack frame within the now-deleted pool. |
| 2960 | // Executing any code that refers to members of the now-deleted pool can cause |
| 2961 | // crashes. |
| 2962 | TEST_F(ClientSocketPoolBaseTest, CallbackThatReleasesPool) { |
| 2963 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2964 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
| 2965 | |
| 2966 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2967 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2968 | EXPECT_EQ( |
| 2969 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2970 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2971 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2972 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2973 | pool_.get(), NetLogWithSource())); |
[email protected] | b6501d3d | 2010-06-03 23:53:34 | [diff] [blame] | 2974 | |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 2975 | pool_->FlushWithError(ERR_NETWORK_CHANGED, "Network changed"); |
[email protected] | b6501d3d | 2010-06-03 23:53:34 | [diff] [blame] | 2976 | |
| 2977 | // We'll call back into this now. |
| 2978 | callback.WaitForResult(); |
| 2979 | } |
| 2980 | |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2981 | TEST_F(ClientSocketPoolBaseTest, DoNotReuseSocketAfterFlush) { |
| 2982 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2983 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2984 | |
| 2985 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2986 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2987 | EXPECT_EQ( |
| 2988 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2989 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2990 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2991 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2992 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2993 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2994 | EXPECT_EQ(ClientSocketHandle::UNUSED, handle.reuse_type()); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 2995 | NetLogSource source = handle.socket()->NetLog().source(); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2996 | |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 2997 | pool_->FlushWithError(ERR_NETWORK_CHANGED, "Network changed"); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2998 | |
| 2999 | handle.Reset(); |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3000 | base::RunLoop().RunUntilIdle(); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 3001 | |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3002 | EXPECT_EQ( |
| 3003 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3004 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3005 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 3006 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3007 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3008 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 3009 | EXPECT_EQ(ClientSocketHandle::UNUSED, handle.reuse_type()); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 3010 | |
| 3011 | ExpectSocketClosedWithReason( |
| 3012 | source, TransportClientSocketPool::kSocketGenerationOutOfDate); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 3013 | } |
| 3014 | |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3015 | class ConnectWithinCallback : public TestCompletionCallbackBase { |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 3016 | public: |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3017 | ConnectWithinCallback( |
| 3018 | const ClientSocketPool::GroupId& group_id, |
Matt Menke | 84d11e56 | 2019-03-27 00:11:19 | [diff] [blame] | 3019 | const scoped_refptr<ClientSocketPool::SocketParams>& params, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3020 | TransportClientSocketPool* pool) |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3021 | : group_id_(group_id), params_(params), pool_(pool) {} |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 3022 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 3023 | ~ConnectWithinCallback() override = default; |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 3024 | |
| 3025 | int WaitForNestedResult() { |
| 3026 | return nested_callback_.WaitForResult(); |
| 3027 | } |
| 3028 | |
Bence Béky | 8ddc249 | 2018-06-13 01:02:04 | [diff] [blame] | 3029 | CompletionOnceCallback callback() { |
| 3030 | return base::BindOnce(&ConnectWithinCallback::OnComplete, |
| 3031 | base::Unretained(this)); |
| 3032 | } |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3033 | |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 3034 | private: |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3035 | void OnComplete(int result) { |
| 3036 | SetResult(result); |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3037 | EXPECT_EQ( |
| 3038 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3039 | handle_.Init(group_id_, params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3040 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 3041 | nested_callback_.callback(), |
| 3042 | ClientSocketPool::ProxyAuthCallback(), pool_, |
| 3043 | NetLogWithSource())); |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3044 | } |
| 3045 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3046 | const ClientSocketPool::GroupId group_id_; |
Matt Menke | 84d11e56 | 2019-03-27 00:11:19 | [diff] [blame] | 3047 | const scoped_refptr<ClientSocketPool::SocketParams> params_; |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3048 | TransportClientSocketPool* const pool_; |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 3049 | ClientSocketHandle handle_; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3050 | TestCompletionCallback nested_callback_; |
| 3051 | |
| 3052 | DISALLOW_COPY_AND_ASSIGN(ConnectWithinCallback); |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 3053 | }; |
| 3054 | |
| 3055 | TEST_F(ClientSocketPoolBaseTest, AbortAllRequestsOnFlush) { |
| 3056 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3057 | |
| 3058 | // First job will be waiting until it gets aborted. |
| 3059 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3060 | |
| 3061 | ClientSocketHandle handle; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3062 | ConnectWithinCallback callback(TestGroupId("a"), params_, pool_.get()); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3063 | EXPECT_EQ( |
| 3064 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3065 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3066 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 3067 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3068 | pool_.get(), NetLogWithSource())); |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 3069 | |
| 3070 | // Second job will be started during the first callback, and will |
| 3071 | // asynchronously complete with OK. |
| 3072 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 3073 | pool_->FlushWithError(ERR_NETWORK_CHANGED, "Network changed"); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3074 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_NETWORK_CHANGED)); |
| 3075 | EXPECT_THAT(callback.WaitForNestedResult(), IsOk()); |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 3076 | } |
| 3077 | |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 3078 | TEST_F(ClientSocketPoolBaseTest, BackupSocketWaitsForHostResolution) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3079 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets, |
| 3080 | true /* enable_backup_connect_jobs */); |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 3081 | |
| 3082 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3083 | ClientSocketHandle handle; |
| 3084 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3085 | EXPECT_EQ( |
| 3086 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3087 | handle.Init(TestGroupId("bar"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3088 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 3089 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3090 | pool_.get(), NetLogWithSource())); |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 3091 | // The backup timer fires but doesn't start a new ConnectJob while resolving |
| 3092 | // the hostname. |
| 3093 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_RESOLVING_HOST); |
| 3094 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
| 3095 | ClientSocketPool::kMaxConnectRetryIntervalMs * 100)); |
| 3096 | EXPECT_EQ(1, client_socket_factory_.allocation_count()); |
| 3097 | |
| 3098 | // Once the ConnectJob has finished resolving the hostname, the backup timer |
| 3099 | // will create a ConnectJob when it fires. |
| 3100 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_CONNECTING); |
| 3101 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
| 3102 | ClientSocketPool::kMaxConnectRetryIntervalMs)); |
| 3103 | EXPECT_EQ(2, client_socket_factory_.allocation_count()); |
| 3104 | } |
| 3105 | |
| 3106 | // Test that no backup socket is created when a ConnectJob connects before it |
| 3107 | // completes. |
| 3108 | TEST_F(ClientSocketPoolBaseTest, NoBackupSocketWhenConnected) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3109 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets, |
| 3110 | true /* enable_backup_connect_jobs */); |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 3111 | |
| 3112 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3113 | ClientSocketHandle handle; |
| 3114 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3115 | EXPECT_EQ( |
| 3116 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3117 | handle.Init(TestGroupId("bar"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3118 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 3119 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3120 | pool_.get(), NetLogWithSource())); |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 3121 | // The backup timer fires but doesn't start a new ConnectJob while resolving |
| 3122 | // the hostname. |
| 3123 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_RESOLVING_HOST); |
| 3124 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
| 3125 | ClientSocketPool::kMaxConnectRetryIntervalMs * 100)); |
| 3126 | EXPECT_EQ(1, client_socket_factory_.allocation_count()); |
| 3127 | |
| 3128 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE); |
| 3129 | client_socket_factory_.SetJobHasEstablishedConnection(0); |
| 3130 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
| 3131 | ClientSocketPool::kMaxConnectRetryIntervalMs * 100)); |
| 3132 | EXPECT_EQ(1, client_socket_factory_.allocation_count()); |
| 3133 | } |
| 3134 | |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3135 | // Cancel a pending socket request while we're at max sockets, |
| 3136 | // and verify that the backup socket firing doesn't cause a crash. |
| 3137 | TEST_F(ClientSocketPoolBaseTest, BackupSocketCancelAtMaxSockets) { |
| 3138 | // Max 4 sockets globally, max 4 sockets per group. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3139 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets, |
| 3140 | true /* enable_backup_connect_jobs */); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3141 | |
[email protected] | 4baaf9d | 2010-08-31 15:15:44 | [diff] [blame] | 3142 | // Create the first socket and set to ERR_IO_PENDING. This starts the backup |
| 3143 | // timer. |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3144 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3145 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3146 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3147 | EXPECT_EQ( |
| 3148 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3149 | handle.Init(TestGroupId("bar"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3150 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 3151 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3152 | pool_.get(), NetLogWithSource())); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3153 | |
| 3154 | // Start (MaxSockets - 1) connected sockets to reach max sockets. |
| 3155 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 3156 | ClientSocketHandle handles[kDefaultMaxSockets]; |
| 3157 | for (int i = 1; i < kDefaultMaxSockets; ++i) { |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3158 | TestCompletionCallback callback; |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3159 | EXPECT_EQ(OK, handles[i].Init(TestGroupId("bar"), params_, absl::nullopt, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3160 | DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3161 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3162 | callback.callback(), |
| 3163 | ClientSocketPool::ProxyAuthCallback(), |
| 3164 | pool_.get(), NetLogWithSource())); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3165 | } |
| 3166 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3167 | base::RunLoop().RunUntilIdle(); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3168 | |
| 3169 | // Cancel the pending request. |
| 3170 | handle.Reset(); |
| 3171 | |
| 3172 | // Wait for the backup timer to fire (add some slop to ensure it fires) |
Alex Clarke | 0def209 | 2018-12-10 12:01:45 | [diff] [blame] | 3173 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
[email protected] | 26b997396 | 2012-01-28 00:57:00 | [diff] [blame] | 3174 | ClientSocketPool::kMaxConnectRetryIntervalMs / 2 * 3)); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3175 | |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3176 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 3177 | } |
| 3178 | |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3179 | TEST_F(ClientSocketPoolBaseTest, CancelBackupSocketAfterCancelingAllRequests) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3180 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets, |
| 3181 | true /* enable_backup_connect_jobs */); |
[email protected] | 4baaf9d | 2010-08-31 15:15:44 | [diff] [blame] | 3182 | |
| 3183 | // Create the first socket and set to ERR_IO_PENDING. This starts the backup |
| 3184 | // timer. |
| 3185 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3186 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3187 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3188 | EXPECT_EQ( |
| 3189 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3190 | handle.Init(TestGroupId("bar"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3191 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 3192 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3193 | pool_.get(), NetLogWithSource())); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3194 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("bar"))); |
| 3195 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("bar"))); |
| 3196 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3197 | TestGroupId("bar"))); |
| 3198 | EXPECT_EQ( |
| 3199 | 0u, pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("bar"))); |
[email protected] | 4baaf9d | 2010-08-31 15:15:44 | [diff] [blame] | 3200 | |
| 3201 | // Cancel the socket request. This should cancel the backup timer. Wait for |
| 3202 | // the backup time to see if it indeed got canceled. |
| 3203 | handle.Reset(); |
| 3204 | // Wait for the backup timer to fire (add some slop to ensure it fires) |
Alex Clarke | 0def209 | 2018-12-10 12:01:45 | [diff] [blame] | 3205 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
[email protected] | 26b997396 | 2012-01-28 00:57:00 | [diff] [blame] | 3206 | ClientSocketPool::kMaxConnectRetryIntervalMs / 2 * 3)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3207 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("bar"))); |
| 3208 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("bar"))); |
[email protected] | 4baaf9d | 2010-08-31 15:15:44 | [diff] [blame] | 3209 | } |
| 3210 | |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3211 | TEST_F(ClientSocketPoolBaseTest, CancelBackupSocketAfterFinishingAllRequests) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3212 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets, |
| 3213 | true /* enable_backup_connect_jobs */); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3214 | |
| 3215 | // Create the first socket and set to ERR_IO_PENDING. This starts the backup |
| 3216 | // timer. |
| 3217 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3218 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3219 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3220 | EXPECT_EQ( |
| 3221 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3222 | handle.Init(TestGroupId("bar"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3223 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 3224 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3225 | pool_.get(), NetLogWithSource())); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3226 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3227 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3228 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3229 | EXPECT_EQ( |
| 3230 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3231 | handle2.Init(TestGroupId("bar"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3232 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3233 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3234 | pool_.get(), NetLogWithSource())); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3235 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("bar"))); |
| 3236 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("bar"))); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3237 | |
| 3238 | // Cancel request 1 and then complete request 2. With the requests finished, |
| 3239 | // the backup timer should be cancelled. |
| 3240 | handle.Reset(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3241 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3242 | // Wait for the backup timer to fire (add some slop to ensure it fires) |
Alex Clarke | 0def209 | 2018-12-10 12:01:45 | [diff] [blame] | 3243 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
[email protected] | 26b997396 | 2012-01-28 00:57:00 | [diff] [blame] | 3244 | ClientSocketPool::kMaxConnectRetryIntervalMs / 2 * 3)); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3245 | } |
| 3246 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3247 | // Test delayed socket binding for the case where we have two connects, |
| 3248 | // and while one is waiting on a connect, the other frees up. |
| 3249 | // The socket waiting on a connect should switch immediately to the freed |
| 3250 | // up socket. |
| 3251 | TEST_F(ClientSocketPoolBaseTest, DelayedSocketBindingWaitingForConnect) { |
| 3252 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3253 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3254 | |
| 3255 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3256 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3257 | EXPECT_EQ( |
| 3258 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3259 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3260 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3261 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3262 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3263 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3264 | |
| 3265 | // No idle sockets, no pending jobs. |
| 3266 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3267 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3268 | |
| 3269 | // Create a second socket to the same host, but this one will wait. |
| 3270 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3271 | ClientSocketHandle handle2; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3272 | EXPECT_EQ( |
| 3273 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3274 | handle2.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3275 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3276 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3277 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3278 | // No idle sockets, and one connecting job. |
| 3279 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3280 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3281 | |
| 3282 | // Return the first handle to the pool. This will initiate the delayed |
| 3283 | // binding. |
| 3284 | handle1.Reset(); |
| 3285 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3286 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3287 | |
| 3288 | // Still no idle sockets, still one pending connect job. |
| 3289 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3290 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3291 | |
| 3292 | // The second socket connected, even though it was a Waiting Job. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3293 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3294 | |
| 3295 | // And we can see there is still one job waiting. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3296 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3297 | |
| 3298 | // Finally, signal the waiting Connect. |
| 3299 | client_socket_factory_.SignalJobs(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3300 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3301 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3302 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3303 | } |
| 3304 | |
| 3305 | // Test delayed socket binding when a group is at capacity and one |
| 3306 | // of the group's sockets frees up. |
| 3307 | TEST_F(ClientSocketPoolBaseTest, DelayedSocketBindingAtGroupCapacity) { |
| 3308 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3309 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3310 | |
| 3311 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3312 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3313 | EXPECT_EQ( |
| 3314 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3315 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3316 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3317 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3318 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3319 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3320 | |
| 3321 | // No idle sockets, no pending jobs. |
| 3322 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3323 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3324 | |
| 3325 | // Create a second socket to the same host, but this one will wait. |
| 3326 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3327 | ClientSocketHandle handle2; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3328 | EXPECT_EQ( |
| 3329 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3330 | handle2.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3331 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3332 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3333 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3334 | // No idle sockets, and one connecting job. |
| 3335 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3336 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3337 | |
| 3338 | // Return the first handle to the pool. This will initiate the delayed |
| 3339 | // binding. |
| 3340 | handle1.Reset(); |
| 3341 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3342 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3343 | |
| 3344 | // Still no idle sockets, still one pending connect job. |
| 3345 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3346 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3347 | |
| 3348 | // The second socket connected, even though it was a Waiting Job. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3349 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3350 | |
| 3351 | // And we can see there is still one job waiting. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3352 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3353 | |
| 3354 | // Finally, signal the waiting Connect. |
| 3355 | client_socket_factory_.SignalJobs(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3356 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3357 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3358 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3359 | } |
| 3360 | |
| 3361 | // Test out the case where we have one socket connected, one |
| 3362 | // connecting, when the first socket finishes and goes idle. |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3363 | // Although the second connection is pending, the second request |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3364 | // should complete, by taking the first socket's idle socket. |
| 3365 | TEST_F(ClientSocketPoolBaseTest, DelayedSocketBindingAtStall) { |
| 3366 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3367 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3368 | |
| 3369 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3370 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3371 | EXPECT_EQ( |
| 3372 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3373 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3374 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3375 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3376 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3377 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3378 | |
| 3379 | // No idle sockets, no pending jobs. |
| 3380 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3381 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3382 | |
| 3383 | // Create a second socket to the same host, but this one will wait. |
| 3384 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3385 | ClientSocketHandle handle2; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3386 | EXPECT_EQ( |
| 3387 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3388 | handle2.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3389 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3390 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3391 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3392 | // No idle sockets, and one connecting job. |
| 3393 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3394 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3395 | |
| 3396 | // Return the first handle to the pool. This will initiate the delayed |
| 3397 | // binding. |
| 3398 | handle1.Reset(); |
| 3399 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3400 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3401 | |
| 3402 | // Still no idle sockets, still one pending connect job. |
| 3403 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3404 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3405 | |
| 3406 | // The second socket connected, even though it was a Waiting Job. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3407 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3408 | |
| 3409 | // And we can see there is still one job waiting. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3410 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3411 | |
| 3412 | // Finally, signal the waiting Connect. |
| 3413 | client_socket_factory_.SignalJobs(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3414 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3415 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3416 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3417 | } |
| 3418 | |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3419 | // Cover the case where on an available socket slot, we have one pending |
| 3420 | // request that completes synchronously, thereby making the Group empty. |
| 3421 | TEST_F(ClientSocketPoolBaseTest, SynchronouslyProcessOnePendingRequest) { |
| 3422 | const int kUnlimitedSockets = 100; |
| 3423 | const int kOneSocketPerGroup = 1; |
| 3424 | CreatePool(kUnlimitedSockets, kOneSocketPerGroup); |
| 3425 | |
| 3426 | // Make the first request asynchronous fail. |
| 3427 | // This will free up a socket slot later. |
| 3428 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
| 3429 | |
| 3430 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3431 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3432 | EXPECT_EQ( |
| 3433 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3434 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3435 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3436 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3437 | pool_.get(), NetLogWithSource())); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3438 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3439 | |
| 3440 | // Make the second request synchronously fail. This should make the Group |
| 3441 | // empty. |
| 3442 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
| 3443 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3444 | TestCompletionCallback callback2; |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3445 | // It'll be ERR_IO_PENDING now, but the TestConnectJob will synchronously fail |
| 3446 | // when created. |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3447 | EXPECT_EQ( |
| 3448 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3449 | handle2.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3450 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3451 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3452 | pool_.get(), NetLogWithSource())); |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3453 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3454 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3455 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3456 | EXPECT_THAT(callback1.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 3457 | EXPECT_THAT(callback2.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3458 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3459 | } |
| 3460 | |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3461 | TEST_F(ClientSocketPoolBaseTest, PreferUsedSocketToUnusedSocket) { |
| 3462 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 3463 | |
| 3464 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3465 | |
| 3466 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3467 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3468 | EXPECT_EQ( |
| 3469 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3470 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3471 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3472 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3473 | pool_.get(), NetLogWithSource())); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3474 | |
| 3475 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3476 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3477 | EXPECT_EQ( |
| 3478 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3479 | handle2.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3480 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3481 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3482 | pool_.get(), NetLogWithSource())); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3483 | ClientSocketHandle handle3; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3484 | TestCompletionCallback callback3; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3485 | EXPECT_EQ( |
| 3486 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3487 | handle3.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3488 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3489 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3490 | pool_.get(), NetLogWithSource())); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3491 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3492 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3493 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
| 3494 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3495 | |
| 3496 | // Use the socket. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 3497 | EXPECT_EQ(1, handle1.socket()->Write(nullptr, 1, CompletionOnceCallback(), |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 3498 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 3499 | EXPECT_EQ(1, handle3.socket()->Write(nullptr, 1, CompletionOnceCallback(), |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 3500 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3501 | |
| 3502 | handle1.Reset(); |
| 3503 | handle2.Reset(); |
| 3504 | handle3.Reset(); |
| 3505 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3506 | EXPECT_EQ(OK, handle1.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3507 | 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 | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3509 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3510 | pool_.get(), NetLogWithSource())); |
| 3511 | EXPECT_EQ(OK, handle2.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3512 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3513 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3514 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3515 | pool_.get(), NetLogWithSource())); |
| 3516 | EXPECT_EQ(OK, handle3.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3517 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3518 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3519 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3520 | pool_.get(), NetLogWithSource())); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3521 | |
| 3522 | EXPECT_TRUE(handle1.socket()->WasEverUsed()); |
| 3523 | EXPECT_TRUE(handle2.socket()->WasEverUsed()); |
| 3524 | EXPECT_FALSE(handle3.socket()->WasEverUsed()); |
| 3525 | } |
| 3526 | |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3527 | TEST_F(ClientSocketPoolBaseTest, RequestSockets) { |
| 3528 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3529 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3530 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3531 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 2, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3532 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3533 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3534 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3535 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3536 | EXPECT_EQ(2u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3537 | TestGroupId("a"))); |
| 3538 | EXPECT_EQ(2u, |
| 3539 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3540 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3541 | |
| 3542 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3543 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3544 | EXPECT_EQ( |
| 3545 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3546 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3547 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3548 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3549 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3550 | |
| 3551 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3552 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3553 | EXPECT_EQ( |
| 3554 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3555 | handle2.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3556 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3557 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3558 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3559 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3560 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3561 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3562 | TestGroupId("a"))); |
| 3563 | EXPECT_EQ(0u, |
| 3564 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3565 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3566 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3567 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3568 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3569 | handle1.Reset(); |
| 3570 | handle2.Reset(); |
| 3571 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3572 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3573 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3574 | TestGroupId("a"))); |
| 3575 | EXPECT_EQ(0u, |
| 3576 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3577 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3578 | } |
| 3579 | |
| 3580 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsWhenAlreadyHaveAConnectJob) { |
| 3581 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3582 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3583 | |
| 3584 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3585 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3586 | EXPECT_EQ( |
| 3587 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3588 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3589 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3590 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3591 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3592 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3593 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3594 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3595 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3596 | TestGroupId("a"))); |
| 3597 | EXPECT_EQ(0u, |
| 3598 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3599 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3600 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3601 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 2, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3602 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3603 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3604 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3605 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3606 | TestGroupId("a"))); |
| 3607 | EXPECT_EQ(1u, |
| 3608 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3609 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3610 | |
| 3611 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3612 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3613 | EXPECT_EQ( |
| 3614 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3615 | handle2.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3616 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3617 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3618 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3619 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3620 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3621 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3622 | TestGroupId("a"))); |
| 3623 | EXPECT_EQ(0u, |
| 3624 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3625 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3626 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3627 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3628 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3629 | handle1.Reset(); |
| 3630 | handle2.Reset(); |
| 3631 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3632 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3633 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3634 | TestGroupId("a"))); |
| 3635 | EXPECT_EQ(0u, |
| 3636 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3637 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3638 | } |
| 3639 | |
| 3640 | TEST_F(ClientSocketPoolBaseTest, |
| 3641 | RequestSocketsWhenAlreadyHaveMultipleConnectJob) { |
| 3642 | CreatePool(4, 4); |
| 3643 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3644 | |
| 3645 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3646 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3647 | EXPECT_EQ( |
| 3648 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3649 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3650 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3651 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3652 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3653 | |
| 3654 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3655 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3656 | EXPECT_EQ( |
| 3657 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3658 | handle2.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3659 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3660 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3661 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3662 | |
| 3663 | ClientSocketHandle handle3; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3664 | TestCompletionCallback callback3; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3665 | EXPECT_EQ( |
| 3666 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3667 | handle3.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3668 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3669 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3670 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3671 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3672 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3673 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3674 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3675 | TestGroupId("a"))); |
| 3676 | EXPECT_EQ(0u, |
| 3677 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3678 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3679 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3680 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 2, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3681 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3682 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3683 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3684 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3685 | TestGroupId("a"))); |
| 3686 | EXPECT_EQ(0u, |
| 3687 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3688 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3689 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3690 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3691 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
| 3692 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3693 | handle1.Reset(); |
| 3694 | handle2.Reset(); |
| 3695 | handle3.Reset(); |
| 3696 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3697 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3698 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3699 | TestGroupId("a"))); |
| 3700 | EXPECT_EQ(0u, |
| 3701 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3702 | EXPECT_EQ(3u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3703 | } |
| 3704 | |
| 3705 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsAtMaxSocketLimit) { |
| 3706 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 3707 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3708 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3709 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3710 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3711 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3712 | kDefaultMaxSockets, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3713 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3714 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3715 | EXPECT_EQ(kDefaultMaxSockets, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3716 | static_cast<int>( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3717 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
| 3718 | EXPECT_EQ( |
| 3719 | kDefaultMaxSockets, |
| 3720 | static_cast<int>(pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3721 | TestGroupId("a")))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3722 | EXPECT_EQ(kDefaultMaxSockets, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3723 | static_cast<int>(pool_->NumUnassignedConnectJobsInGroupForTesting( |
| 3724 | TestGroupId("a")))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3725 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3726 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("b"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3727 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3728 | pool_->RequestSockets(TestGroupId("b"), params_, absl::nullopt, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3729 | kDefaultMaxSockets, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3730 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3731 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("b"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3732 | } |
| 3733 | |
| 3734 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsHitMaxSocketLimit) { |
| 3735 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 3736 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3737 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3738 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3739 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3740 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3741 | kDefaultMaxSockets - 1, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3742 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3743 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3744 | EXPECT_EQ(kDefaultMaxSockets - 1, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3745 | static_cast<int>( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3746 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
| 3747 | EXPECT_EQ( |
| 3748 | kDefaultMaxSockets - 1, |
| 3749 | static_cast<int>(pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3750 | TestGroupId("a")))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3751 | EXPECT_EQ(kDefaultMaxSockets - 1, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3752 | static_cast<int>(pool_->NumUnassignedConnectJobsInGroupForTesting( |
| 3753 | TestGroupId("a")))); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 3754 | EXPECT_FALSE(pool_->IsStalled()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3755 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3756 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("b"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3757 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3758 | pool_->RequestSockets(TestGroupId("b"), params_, absl::nullopt, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3759 | kDefaultMaxSockets, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3760 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3761 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("b"))); |
| 3762 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 3763 | EXPECT_FALSE(pool_->IsStalled()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3764 | } |
| 3765 | |
| 3766 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsCountIdleSockets) { |
| 3767 | CreatePool(4, 4); |
| 3768 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3769 | |
| 3770 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3771 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3772 | EXPECT_EQ( |
| 3773 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3774 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3775 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3776 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3777 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3778 | ASSERT_THAT(callback1.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3779 | handle1.Reset(); |
| 3780 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3781 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3782 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3783 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3784 | TestGroupId("a"))); |
| 3785 | EXPECT_EQ(0u, |
| 3786 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3787 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3788 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3789 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 2, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3790 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3791 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3792 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3793 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3794 | TestGroupId("a"))); |
| 3795 | EXPECT_EQ(1u, |
| 3796 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3797 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3798 | } |
| 3799 | |
| 3800 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsCountActiveSockets) { |
| 3801 | CreatePool(4, 4); |
| 3802 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3803 | |
| 3804 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3805 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3806 | EXPECT_EQ( |
| 3807 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3808 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3809 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3810 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3811 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3812 | ASSERT_THAT(callback1.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3813 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3814 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3815 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3816 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3817 | TestGroupId("a"))); |
| 3818 | EXPECT_EQ(0u, |
| 3819 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3820 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3821 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3822 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3823 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 2, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3824 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3825 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3826 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3827 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3828 | TestGroupId("a"))); |
| 3829 | EXPECT_EQ(1u, |
| 3830 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3831 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3832 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3833 | } |
| 3834 | |
| 3835 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsSynchronous) { |
| 3836 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3837 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 3838 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3839 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3840 | kDefaultMaxSocketsPerGroup, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3841 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3842 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3843 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3844 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3845 | TestGroupId("a"))); |
| 3846 | EXPECT_EQ(0u, |
| 3847 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3848 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3849 | static_cast<int>(pool_->IdleSocketCountInGroup(TestGroupId("a")))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3850 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3851 | pool_->RequestSockets(TestGroupId("b"), params_, absl::nullopt, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3852 | kDefaultMaxSocketsPerGroup, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3853 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3854 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 3855 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3856 | TestGroupId("b"))); |
| 3857 | EXPECT_EQ(0u, |
| 3858 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3859 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3860 | static_cast<int>(pool_->IdleSocketCountInGroup(TestGroupId("b")))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3861 | } |
| 3862 | |
[email protected] | 3c819f52 | 2010-12-02 02:03:12 | [diff] [blame] | 3863 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsSynchronousError) { |
| 3864 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3865 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
| 3866 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3867 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3868 | kDefaultMaxSocketsPerGroup, NetLogWithSource()); |
[email protected] | 3c819f52 | 2010-12-02 02:03:12 | [diff] [blame] | 3869 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3870 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | fd2e53e | 2011-01-14 20:40:52 | [diff] [blame] | 3871 | |
| 3872 | connect_job_factory_->set_job_type( |
| 3873 | TestConnectJob::kMockAdditionalErrorStateJob); |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3874 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3875 | kDefaultMaxSocketsPerGroup, NetLogWithSource()); |
[email protected] | fd2e53e | 2011-01-14 20:40:52 | [diff] [blame] | 3876 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3877 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | 3c819f52 | 2010-12-02 02:03:12 | [diff] [blame] | 3878 | } |
| 3879 | |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3880 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsMultipleTimesDoesNothing) { |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3881 | CreatePool(4, 4); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3882 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3883 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3884 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 2, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3885 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3886 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3887 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3888 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3889 | EXPECT_EQ(2u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3890 | TestGroupId("a"))); |
| 3891 | EXPECT_EQ(2u, |
| 3892 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3893 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3894 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3895 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3896 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 2, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3897 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3898 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3899 | EXPECT_EQ(2u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3900 | TestGroupId("a"))); |
| 3901 | EXPECT_EQ(2u, |
| 3902 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3903 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3904 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3905 | |
| 3906 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3907 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3908 | EXPECT_EQ( |
| 3909 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3910 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3911 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3912 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3913 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3914 | |
| 3915 | client_socket_factory_.SignalJob(0); |
| 3916 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3917 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3918 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3919 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3920 | TestGroupId("a"))); |
| 3921 | EXPECT_EQ(1u, |
| 3922 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3923 | EXPECT_EQ(1, 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 handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3927 | TestCompletionCallback callback2; |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3928 | EXPECT_EQ( |
| 3929 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3930 | handle2.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 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3933 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3934 | client_socket_factory_.SignalJob(0); |
| 3935 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3936 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3937 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3938 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3939 | TestGroupId("a"))); |
| 3940 | EXPECT_EQ(0u, |
| 3941 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3942 | EXPECT_EQ(2, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3943 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3944 | |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3945 | handle1.Reset(); |
| 3946 | handle2.Reset(); |
| 3947 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3948 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3949 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3950 | TestGroupId("a"))); |
| 3951 | EXPECT_EQ(0u, |
| 3952 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3953 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3954 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3955 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3956 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 2, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3957 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3958 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3959 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3960 | TestGroupId("a"))); |
| 3961 | EXPECT_EQ(0u, |
| 3962 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3963 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3964 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3965 | } |
| 3966 | |
| 3967 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsDifferentNumSockets) { |
| 3968 | CreatePool(4, 4); |
| 3969 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3970 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3971 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 1, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3972 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3973 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3974 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3975 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3976 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3977 | TestGroupId("a"))); |
| 3978 | EXPECT_EQ(1u, |
| 3979 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3980 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3981 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3982 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 2, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3983 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3984 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3985 | EXPECT_EQ(2u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3986 | TestGroupId("a"))); |
| 3987 | EXPECT_EQ(2u, |
| 3988 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3989 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3990 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3991 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 3, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3992 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3993 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3994 | EXPECT_EQ(3u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3995 | TestGroupId("a"))); |
| 3996 | EXPECT_EQ(3u, |
| 3997 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3998 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3999 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4000 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 1, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4001 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4002 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4003 | EXPECT_EQ(3u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4004 | TestGroupId("a"))); |
| 4005 | EXPECT_EQ(3u, |
| 4006 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4007 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4008 | } |
| 4009 | |
| 4010 | TEST_F(ClientSocketPoolBaseTest, PreconnectJobsTakenByNormalRequests) { |
| 4011 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4012 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4013 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4014 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 1, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4015 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4016 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4017 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 4018 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4019 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4020 | TestGroupId("a"))); |
| 4021 | EXPECT_EQ(1u, |
| 4022 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4023 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4024 | |
| 4025 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 4026 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4027 | EXPECT_EQ( |
| 4028 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4029 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4030 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4031 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4032 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4033 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4034 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4035 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4036 | TestGroupId("a"))); |
| 4037 | EXPECT_EQ(0u, |
| 4038 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4039 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4040 | |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4041 | client_socket_factory_.SignalJobs(); |
| 4042 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 4043 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4044 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4045 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4046 | TestGroupId("a"))); |
| 4047 | EXPECT_EQ(0u, |
| 4048 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4049 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4050 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4051 | |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4052 | // Make sure if a preconnected socket is not fully connected when a request |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 4053 | // starts, it has a connect start time. |
| 4054 | TestLoadTimingInfoConnectedNotReused(handle1); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4055 | handle1.Reset(); |
| 4056 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4057 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4058 | } |
| 4059 | |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 4060 | // Checks that fully connected preconnect jobs have no connect times, and are |
| 4061 | // marked as reused. |
| 4062 | TEST_F(ClientSocketPoolBaseTest, ConnectedPreconnectJobsHaveNoConnectTimes) { |
| 4063 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4064 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4065 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 1, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4066 | NetLogWithSource()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 4067 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4068 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 4069 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4070 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4071 | TestGroupId("a"))); |
| 4072 | EXPECT_EQ(0u, |
| 4073 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4074 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 4075 | |
| 4076 | ClientSocketHandle handle; |
| 4077 | TestCompletionCallback callback; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4078 | EXPECT_EQ(OK, handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4079 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4080 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4081 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4082 | pool_.get(), NetLogWithSource())); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 4083 | |
| 4084 | // Make sure the idle socket was used. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4085 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 4086 | |
| 4087 | TestLoadTimingInfoConnectedReused(handle); |
| 4088 | handle.Reset(); |
| 4089 | TestLoadTimingInfoNotConnected(handle); |
| 4090 | } |
| 4091 | |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4092 | // http://crbug.com/64940 regression test. |
| 4093 | TEST_F(ClientSocketPoolBaseTest, PreconnectClosesIdleSocketRemovesGroup) { |
| 4094 | const int kMaxTotalSockets = 3; |
| 4095 | const int kMaxSocketsPerGroup = 2; |
| 4096 | CreatePool(kMaxTotalSockets, kMaxSocketsPerGroup); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4097 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4098 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 4099 | // Note that group id ordering matters here. "a" comes before "b", so |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4100 | // CloseOneIdleSocket() will try to close "a"'s idle socket. |
| 4101 | |
| 4102 | // Set up one idle socket in "a". |
| 4103 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 4104 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4105 | EXPECT_EQ( |
| 4106 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4107 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4108 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4109 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4110 | pool_.get(), NetLogWithSource())); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4111 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 4112 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4113 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4114 | TestGroupId("a"))); |
| 4115 | EXPECT_EQ(0u, |
| 4116 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4117 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4118 | |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4119 | client_socket_factory_.SignalJobs(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4120 | ASSERT_THAT(callback1.WaitForResult(), IsOk()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4121 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4122 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4123 | TestGroupId("a"))); |
| 4124 | EXPECT_EQ(0u, |
| 4125 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4126 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4127 | |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4128 | handle1.Reset(); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4129 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4130 | |
| 4131 | // Set up two active sockets in "b". |
| 4132 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 4133 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4134 | EXPECT_EQ( |
| 4135 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4136 | handle1.Init(TestGroupId("b"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4137 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4138 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4139 | pool_.get(), NetLogWithSource())); |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4140 | EXPECT_EQ( |
| 4141 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4142 | handle2.Init(TestGroupId("b"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4143 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4144 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4145 | pool_.get(), NetLogWithSource())); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4146 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4147 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("b"))); |
| 4148 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 4149 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4150 | TestGroupId("b"))); |
| 4151 | EXPECT_EQ(0u, |
| 4152 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4153 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4154 | |
| 4155 | client_socket_factory_.SignalJobs(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4156 | ASSERT_THAT(callback1.WaitForResult(), IsOk()); |
| 4157 | ASSERT_THAT(callback2.WaitForResult(), IsOk()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4158 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4159 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4160 | TestGroupId("b"))); |
| 4161 | EXPECT_EQ(0u, |
| 4162 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 4163 | EXPECT_EQ(2, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4164 | |
| 4165 | // Now we have 1 idle socket in "a" and 2 active sockets in "b". This means |
| 4166 | // we've maxed out on sockets, since we set |kMaxTotalSockets| to 3. |
| 4167 | // Requesting 2 preconnected sockets for "a" should fail to allocate any more |
| 4168 | // sockets for "a", and "b" should still have 2 active sockets. |
| 4169 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4170 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 2, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4171 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4172 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4173 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4174 | TestGroupId("a"))); |
| 4175 | EXPECT_EQ(0u, |
| 4176 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4177 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4178 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 4179 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 4180 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4181 | TestGroupId("b"))); |
| 4182 | EXPECT_EQ(0u, |
| 4183 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4184 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4185 | EXPECT_EQ(2, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4186 | |
| 4187 | // Now release the 2 active sockets for "b". This will give us 1 idle socket |
| 4188 | // in "a" and 2 idle sockets in "b". Requesting 2 preconnected sockets for |
| 4189 | // "a" should result in closing 1 for "b". |
| 4190 | handle1.Reset(); |
| 4191 | handle2.Reset(); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4192 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4193 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4194 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4195 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 2, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4196 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4197 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4198 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4199 | TestGroupId("a"))); |
| 4200 | EXPECT_EQ(1u, |
| 4201 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4202 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4203 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 4204 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 4205 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4206 | TestGroupId("b"))); |
| 4207 | EXPECT_EQ(0u, |
| 4208 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4209 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4210 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4211 | } |
| 4212 | |
[email protected] | b7b8be4 | 2011-07-12 12:46:41 | [diff] [blame] | 4213 | TEST_F(ClientSocketPoolBaseTest, PreconnectWithoutBackupJob) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4214 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 4215 | true /* enable_backup_connect_jobs */); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4216 | |
| 4217 | // Make the ConnectJob hang until it times out, shorten the timeout. |
| 4218 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4219 | connect_job_factory_->set_timeout_duration( |
| 4220 | base::TimeDelta::FromMilliseconds(500)); |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4221 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 1, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4222 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4223 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4224 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4225 | TestGroupId("a"))); |
| 4226 | EXPECT_EQ(1u, |
| 4227 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4228 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4229 | |
[email protected] | b7b8be4 | 2011-07-12 12:46:41 | [diff] [blame] | 4230 | // Verify the backup timer doesn't create a backup job, by making |
| 4231 | // the backup job a pending job instead of a waiting job, so it |
| 4232 | // *would* complete if it were created. |
Lukasz Krakowiak | 28dcf9d6 | 2020-06-04 09:46:59 | [diff] [blame] | 4233 | base::RunLoop loop; |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4234 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 4235 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
Lukasz Krakowiak | 85529208 | 2020-06-19 12:43:58 | [diff] [blame] | 4236 | FROM_HERE, loop.QuitClosure(), base::TimeDelta::FromSeconds(1)); |
Lukasz Krakowiak | 28dcf9d6 | 2020-06-04 09:46:59 | [diff] [blame] | 4237 | loop.Run(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4238 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4239 | } |
| 4240 | |
[email protected] | b7b8be4 | 2011-07-12 12:46:41 | [diff] [blame] | 4241 | TEST_F(ClientSocketPoolBaseTest, PreconnectWithBackupJob) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4242 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 4243 | true /* enable_backup_connect_jobs */); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4244 | |
| 4245 | // Make the ConnectJob hang forever. |
| 4246 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4247 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 1, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4248 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4249 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4250 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4251 | TestGroupId("a"))); |
| 4252 | EXPECT_EQ(1u, |
| 4253 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4254 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 4255 | base::RunLoop().RunUntilIdle(); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4256 | |
| 4257 | // Make the backup job be a pending job, so it completes normally. |
| 4258 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 4259 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 4260 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4261 | EXPECT_EQ( |
| 4262 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4263 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4264 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4265 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4266 | pool_.get(), NetLogWithSource())); |
[email protected] | b7b8be4 | 2011-07-12 12:46:41 | [diff] [blame] | 4267 | // Timer has started, but the backup connect job shouldn't be created yet. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4268 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4269 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4270 | TestGroupId("a"))); |
| 4271 | EXPECT_EQ(0u, |
| 4272 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4273 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4274 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4275 | ASSERT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4276 | |
| 4277 | // The hung connect job should still be there, but everything else should be |
| 4278 | // complete. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4279 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4280 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4281 | TestGroupId("a"))); |
| 4282 | EXPECT_EQ(1u, |
| 4283 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4284 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4285 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4286 | } |
| 4287 | |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4288 | // Tests that a preconnect that starts out with unread data can still be used. |
| 4289 | // http://crbug.com/334467 |
| 4290 | TEST_F(ClientSocketPoolBaseTest, PreconnectWithUnreadData) { |
| 4291 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4292 | connect_job_factory_->set_job_type(TestConnectJob::kMockUnreadDataJob); |
| 4293 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4294 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 1, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4295 | NetLogWithSource()); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4296 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4297 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 4298 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4299 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4300 | TestGroupId("a"))); |
| 4301 | EXPECT_EQ(0u, |
| 4302 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4303 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4304 | |
| 4305 | // Fail future jobs to be sure that handle receives the preconnected socket |
| 4306 | // rather than closing it and making a new one. |
| 4307 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
| 4308 | ClientSocketHandle handle; |
| 4309 | TestCompletionCallback callback; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4310 | EXPECT_EQ(OK, handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4311 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4312 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4313 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4314 | pool_.get(), 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(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4323 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4324 | |
| 4325 | // Drain the pending read. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 4326 | EXPECT_EQ(1, handle.socket()->Read(nullptr, 1, CompletionOnceCallback())); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4327 | |
| 4328 | TestLoadTimingInfoConnectedReused(handle); |
| 4329 | handle.Reset(); |
| 4330 | |
| 4331 | // The socket should be usable now that it's idle again. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4332 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4333 | } |
| 4334 | |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4335 | TEST_F(ClientSocketPoolBaseTest, RequestGetsAssignedJob) { |
| 4336 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4337 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4338 | |
| 4339 | ClientSocketHandle handle1; |
| 4340 | TestCompletionCallback callback1; |
| 4341 | EXPECT_EQ( |
| 4342 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4343 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4344 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4345 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4346 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4347 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4348 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4349 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4350 | TestGroupId("a"))); |
| 4351 | EXPECT_EQ(0u, |
| 4352 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4353 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4354 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4355 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4356 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4357 | } |
| 4358 | |
| 4359 | TEST_F(ClientSocketPoolBaseTest, MultipleRequestsGetAssignedJobs) { |
| 4360 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4361 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4362 | |
| 4363 | ClientSocketHandle handle1; |
| 4364 | TestCompletionCallback callback1; |
| 4365 | EXPECT_EQ( |
| 4366 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4367 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4368 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4369 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4370 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4371 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4372 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4373 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4374 | TestGroupId("a"))); |
| 4375 | EXPECT_EQ(0u, |
| 4376 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4377 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4378 | |
| 4379 | ClientSocketHandle handle2; |
| 4380 | TestCompletionCallback callback2; |
| 4381 | EXPECT_EQ( |
| 4382 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4383 | handle2.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4384 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4385 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4386 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4387 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4388 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4389 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4390 | TestGroupId("a"))); |
| 4391 | EXPECT_EQ(0u, |
| 4392 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4393 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4394 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4395 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4396 | &handle1)); |
| 4397 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4398 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4399 | |
| 4400 | // One job completes. The other request should still have its job. |
| 4401 | client_socket_factory_.SignalJob(0); |
| 4402 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 4403 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4404 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4405 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4406 | TestGroupId("a"))); |
| 4407 | EXPECT_EQ(0u, |
| 4408 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4409 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4410 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4411 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4412 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4413 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4414 | } |
| 4415 | |
| 4416 | TEST_F(ClientSocketPoolBaseTest, PreconnectJobGetsAssignedToRequest) { |
| 4417 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4418 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4419 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4420 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 1, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4421 | NetLogWithSource()); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4422 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4423 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 4424 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4425 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4426 | TestGroupId("a"))); |
| 4427 | EXPECT_EQ(1u, |
| 4428 | pool_->NumUnassignedConnectJobsInGroupForTesting(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 | |
| 4431 | ClientSocketHandle handle1; |
| 4432 | TestCompletionCallback callback1; |
| 4433 | EXPECT_EQ( |
| 4434 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4435 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4436 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4437 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4438 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4439 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4440 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4441 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4442 | TestGroupId("a"))); |
| 4443 | EXPECT_EQ(0u, |
| 4444 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4445 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4446 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4447 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4448 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4449 | } |
| 4450 | |
| 4451 | TEST_F(ClientSocketPoolBaseTest, HigherPriorityRequestStealsJob) { |
| 4452 | CreatePool(kDefaultMaxSockets, 1); |
| 4453 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4454 | |
| 4455 | ClientSocketHandle handle1; |
| 4456 | TestCompletionCallback callback1; |
| 4457 | EXPECT_EQ( |
| 4458 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4459 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4460 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4461 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4462 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4463 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4464 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4465 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4466 | TestGroupId("a"))); |
| 4467 | EXPECT_EQ(0u, |
| 4468 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4469 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4470 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4471 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4472 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4473 | |
| 4474 | // Insert a higher priority request |
| 4475 | ClientSocketHandle handle2; |
| 4476 | TestCompletionCallback callback2; |
| 4477 | EXPECT_EQ( |
| 4478 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4479 | handle2.Init(TestGroupId("a"), params_, absl::nullopt, HIGHEST, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4480 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4481 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4482 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4483 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4484 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4485 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4486 | TestGroupId("a"))); |
| 4487 | EXPECT_EQ(0u, |
| 4488 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4489 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4490 | |
| 4491 | // The highest priority request should steal the job from the default priority |
| 4492 | // request. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4493 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4494 | &handle2)); |
| 4495 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4496 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4497 | } |
| 4498 | |
| 4499 | TEST_F(ClientSocketPoolBaseTest, RequestStealsJobFromLowestRequestWithJob) { |
| 4500 | CreatePool(3, 3); |
| 4501 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4502 | |
| 4503 | ClientSocketHandle handle_lowest; |
| 4504 | TestCompletionCallback callback_lowest; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4505 | EXPECT_EQ( |
| 4506 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4507 | handle_lowest.Init(TestGroupId("a"), params_, absl::nullopt, LOWEST, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4508 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4509 | callback_lowest.callback(), |
| 4510 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4511 | NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4512 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4513 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4514 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4515 | TestGroupId("a"))); |
| 4516 | EXPECT_EQ(0u, |
| 4517 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4518 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4519 | |
| 4520 | ClientSocketHandle handle_highest; |
| 4521 | TestCompletionCallback callback_highest; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4522 | EXPECT_EQ( |
| 4523 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4524 | handle_highest.Init(TestGroupId("a"), params_, absl::nullopt, HIGHEST, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4525 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4526 | callback_highest.callback(), |
| 4527 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4528 | NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4529 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4530 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4531 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4532 | TestGroupId("a"))); |
| 4533 | EXPECT_EQ(0u, |
| 4534 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4535 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4536 | |
| 4537 | ClientSocketHandle handle_low; |
| 4538 | TestCompletionCallback callback_low; |
| 4539 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4540 | handle_low.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4541 | TestGroupId("a"), params_, absl::nullopt, LOW, SocketTag(), |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4542 | ClientSocketPool::RespectLimits::ENABLED, |
| 4543 | callback_low.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4544 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4545 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4546 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4547 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4548 | TestGroupId("a"))); |
| 4549 | EXPECT_EQ(0u, |
| 4550 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4551 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4552 | |
| 4553 | ClientSocketHandle handle_lowest2; |
| 4554 | TestCompletionCallback callback_lowest2; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4555 | EXPECT_EQ( |
| 4556 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4557 | handle_lowest2.Init(TestGroupId("a"), params_, absl::nullopt, LOWEST, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4558 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4559 | callback_lowest2.callback(), |
| 4560 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4561 | NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4562 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4563 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4564 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4565 | TestGroupId("a"))); |
| 4566 | EXPECT_EQ(0u, |
| 4567 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4568 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4569 | |
| 4570 | // The top three requests in the queue should have jobs. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4571 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4572 | &handle_highest)); |
| 4573 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4574 | &handle_low)); |
| 4575 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4576 | &handle_lowest)); |
| 4577 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting( |
| 4578 | TestGroupId("a"), &handle_lowest2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4579 | |
| 4580 | // Add another request with medium priority. It should steal the job from the |
| 4581 | // lowest priority request with a job. |
| 4582 | ClientSocketHandle handle_medium; |
| 4583 | TestCompletionCallback callback_medium; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4584 | EXPECT_EQ( |
| 4585 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4586 | handle_medium.Init(TestGroupId("a"), params_, absl::nullopt, MEDIUM, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4587 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4588 | callback_medium.callback(), |
| 4589 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4590 | NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4591 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4592 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4593 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4594 | TestGroupId("a"))); |
| 4595 | EXPECT_EQ(0u, |
| 4596 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4597 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
| 4598 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4599 | &handle_highest)); |
| 4600 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4601 | &handle_medium)); |
| 4602 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4603 | &handle_low)); |
| 4604 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4605 | &handle_lowest)); |
| 4606 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting( |
| 4607 | TestGroupId("a"), &handle_lowest2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4608 | } |
| 4609 | |
| 4610 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeRequestStealsJob) { |
| 4611 | CreatePool(kDefaultMaxSockets, 1); |
| 4612 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4613 | |
| 4614 | ClientSocketHandle handle1; |
| 4615 | TestCompletionCallback callback1; |
| 4616 | EXPECT_EQ( |
| 4617 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4618 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4619 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4620 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4621 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4622 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4623 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4624 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4625 | TestGroupId("a"))); |
| 4626 | EXPECT_EQ(0u, |
| 4627 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4628 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4629 | |
| 4630 | ClientSocketHandle handle2; |
| 4631 | TestCompletionCallback callback2; |
| 4632 | EXPECT_EQ( |
| 4633 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4634 | handle2.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4635 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4636 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4637 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4638 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4639 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4640 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4641 | TestGroupId("a"))); |
| 4642 | EXPECT_EQ(0u, |
| 4643 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4644 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4645 | |
| 4646 | // 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] | 4647 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4648 | &handle1)); |
| 4649 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4650 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4651 | |
| 4652 | // Reprioritizing the second request places it above the first, and it steals |
| 4653 | // the job from the first request. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4654 | pool_->SetPriority(TestGroupId("a"), &handle2, HIGHEST); |
| 4655 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4656 | &handle2)); |
| 4657 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4658 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4659 | } |
| 4660 | |
| 4661 | TEST_F(ClientSocketPoolBaseTest, CancelRequestReassignsJob) { |
| 4662 | CreatePool(kDefaultMaxSockets, 1); |
| 4663 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4664 | |
| 4665 | ClientSocketHandle handle1; |
| 4666 | TestCompletionCallback callback1; |
| 4667 | EXPECT_EQ( |
| 4668 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4669 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4670 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4671 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4672 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4673 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4674 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4675 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4676 | TestGroupId("a"))); |
| 4677 | EXPECT_EQ(0u, |
| 4678 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4679 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4680 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4681 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4682 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4683 | |
| 4684 | ClientSocketHandle handle2; |
| 4685 | TestCompletionCallback callback2; |
| 4686 | EXPECT_EQ( |
| 4687 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4688 | handle2.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 | callback2.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 | |
| 4700 | // The second request doesn't get a job because we are the limit. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4701 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4702 | &handle1)); |
| 4703 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4704 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4705 | |
| 4706 | // The second request should get a job upon cancelling the first request. |
| 4707 | handle1.Reset(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4708 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4709 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4710 | TestGroupId("a"))); |
| 4711 | EXPECT_EQ(0u, |
| 4712 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4713 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4714 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4715 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4716 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4717 | } |
| 4718 | |
| 4719 | TEST_F(ClientSocketPoolBaseTest, JobCompletionReassignsJob) { |
| 4720 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4721 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4722 | |
| 4723 | ClientSocketHandle handle1; |
| 4724 | TestCompletionCallback callback1; |
| 4725 | EXPECT_EQ( |
| 4726 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4727 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, HIGHEST, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4728 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4729 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4730 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4731 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4732 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4733 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4734 | TestGroupId("a"))); |
| 4735 | EXPECT_EQ(0u, |
| 4736 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4737 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4738 | |
| 4739 | ClientSocketHandle handle2; |
| 4740 | TestCompletionCallback callback2; |
| 4741 | EXPECT_EQ( |
| 4742 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4743 | handle2.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4744 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4745 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4746 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4747 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4748 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4749 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4750 | TestGroupId("a"))); |
| 4751 | EXPECT_EQ(0u, |
| 4752 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4753 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4754 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4755 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4756 | &handle1)); |
| 4757 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4758 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4759 | |
| 4760 | // The lower-priority job completes first. The higher-priority request should |
| 4761 | // get the socket, and the lower-priority request should get the remaining |
| 4762 | // job. |
| 4763 | client_socket_factory_.SignalJob(1); |
| 4764 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4765 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4766 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4767 | TestGroupId("a"))); |
| 4768 | EXPECT_EQ(0u, |
| 4769 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4770 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4771 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4772 | EXPECT_TRUE(handle1.socket()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4773 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4774 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4775 | } |
| 4776 | |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 4777 | class MockLayeredPool : public HigherLayeredPool { |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4778 | public: |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4779 | MockLayeredPool(TransportClientSocketPool* pool, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4780 | const ClientSocketPool::GroupId& group_id) |
| 4781 | : pool_(pool), group_id_(group_id), can_release_connection_(true) { |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 4782 | pool_->AddHigherLayeredPool(this); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4783 | } |
| 4784 | |
Daniel Cheng | 4496d082 | 2018-04-26 21:52:15 | [diff] [blame] | 4785 | ~MockLayeredPool() override { pool_->RemoveHigherLayeredPool(this); } |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4786 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4787 | int RequestSocket(TransportClientSocketPool* pool) { |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4788 | return handle_.Init( |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 4789 | group_id_, ClientSocketPool::SocketParams::CreateForHttpForTesting(), |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4790 | absl::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4791 | ClientSocketPool::RespectLimits::ENABLED, callback_.callback(), |
| 4792 | ClientSocketPool::ProxyAuthCallback(), pool, NetLogWithSource()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4793 | } |
| 4794 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4795 | int RequestSocketWithoutLimits(TransportClientSocketPool* pool) { |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4796 | return handle_.Init( |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 4797 | group_id_, ClientSocketPool::SocketParams::CreateForHttpForTesting(), |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4798 | absl::nullopt, MAXIMUM_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4799 | ClientSocketPool::RespectLimits::DISABLED, callback_.callback(), |
| 4800 | ClientSocketPool::ProxyAuthCallback(), pool, NetLogWithSource()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4801 | } |
| 4802 | |
| 4803 | bool ReleaseOneConnection() { |
| 4804 | if (!handle_.is_initialized() || !can_release_connection_) { |
| 4805 | return false; |
| 4806 | } |
| 4807 | handle_.socket()->Disconnect(); |
| 4808 | handle_.Reset(); |
| 4809 | return true; |
| 4810 | } |
| 4811 | |
| 4812 | void set_can_release_connection(bool can_release_connection) { |
| 4813 | can_release_connection_ = can_release_connection; |
| 4814 | } |
| 4815 | |
| 4816 | MOCK_METHOD0(CloseOneIdleConnection, bool()); |
| 4817 | |
| 4818 | private: |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4819 | TransportClientSocketPool* const pool_; |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4820 | ClientSocketHandle handle_; |
| 4821 | TestCompletionCallback callback_; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4822 | const ClientSocketPool::GroupId group_id_; |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4823 | bool can_release_connection_; |
| 4824 | }; |
| 4825 | |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4826 | // Tests the basic case of closing an idle socket in a higher layered pool when |
| 4827 | // a new request is issued and the lower layer pool is stalled. |
| 4828 | TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketsHeldByLayeredPoolWhenNeeded) { |
| 4829 | CreatePool(1, 1); |
| 4830 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 4831 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4832 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("foo")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4833 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4834 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4835 | .WillOnce(Invoke(&mock_layered_pool, |
| 4836 | &MockLayeredPool::ReleaseOneConnection)); |
| 4837 | ClientSocketHandle handle; |
| 4838 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4839 | EXPECT_EQ( |
| 4840 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4841 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4842 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4843 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4844 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4845 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4846 | } |
| 4847 | |
Matt Menke | 83367864 | 2019-03-05 22:05:51 | [diff] [blame] | 4848 | // Tests the case that trying to close an idle socket in a higher layered pool |
| 4849 | // fails. |
| 4850 | TEST_F(ClientSocketPoolBaseTest, |
| 4851 | CloseIdleSocketsHeldByLayeredPoolWhenNeededFails) { |
| 4852 | CreatePool(1, 1); |
| 4853 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 4854 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4855 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("foo")); |
Matt Menke | 83367864 | 2019-03-05 22:05:51 | [diff] [blame] | 4856 | mock_layered_pool.set_can_release_connection(false); |
| 4857 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
| 4858 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4859 | .WillOnce(Invoke(&mock_layered_pool, |
| 4860 | &MockLayeredPool::ReleaseOneConnection)); |
| 4861 | ClientSocketHandle handle; |
| 4862 | TestCompletionCallback callback; |
| 4863 | EXPECT_EQ( |
| 4864 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4865 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4866 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4867 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4868 | pool_.get(), NetLogWithSource())); |
Matt Menke | 83367864 | 2019-03-05 22:05:51 | [diff] [blame] | 4869 | base::RunLoop().RunUntilIdle(); |
| 4870 | EXPECT_FALSE(callback.have_result()); |
| 4871 | } |
| 4872 | |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4873 | // Same as above, but the idle socket is in the same group as the stalled |
| 4874 | // socket, and closes the only other request in its group when closing requests |
| 4875 | // in higher layered pools. This generally shouldn't happen, but it may be |
| 4876 | // possible if a higher level pool issues a request and the request is |
| 4877 | // subsequently cancelled. Even if it's not possible, best not to crash. |
| 4878 | TEST_F(ClientSocketPoolBaseTest, |
| 4879 | CloseIdleSocketsHeldByLayeredPoolWhenNeededSameGroup) { |
| 4880 | CreatePool(2, 2); |
| 4881 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 4882 | |
| 4883 | // Need a socket in another group for the pool to be stalled (If a group |
| 4884 | // has the maximum number of connections already, it's not stalled). |
| 4885 | ClientSocketHandle handle1; |
| 4886 | TestCompletionCallback callback1; |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4887 | EXPECT_EQ(OK, handle1.Init(TestGroupId("group1"), params_, absl::nullopt, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4888 | DEFAULT_PRIORITY, SocketTag(), |
| 4889 | ClientSocketPool::RespectLimits::ENABLED, |
| 4890 | callback1.callback(), |
| 4891 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4892 | NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4893 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4894 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("group2")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4895 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4896 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4897 | .WillOnce(Invoke(&mock_layered_pool, |
| 4898 | &MockLayeredPool::ReleaseOneConnection)); |
| 4899 | ClientSocketHandle handle; |
| 4900 | TestCompletionCallback callback2; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4901 | EXPECT_EQ(ERR_IO_PENDING, |
| 4902 | handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4903 | TestGroupId("group2"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4904 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4905 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4906 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4907 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4908 | } |
| 4909 | |
| 4910 | // Tests the case when an idle socket can be closed when a new request is |
| 4911 | // issued, and the new request belongs to a group that was previously stalled. |
| 4912 | TEST_F(ClientSocketPoolBaseTest, |
| 4913 | CloseIdleSocketsHeldByLayeredPoolInSameGroupWhenNeeded) { |
| 4914 | CreatePool(2, 2); |
| 4915 | std::list<TestConnectJob::JobType> job_types; |
| 4916 | job_types.push_back(TestConnectJob::kMockJob); |
| 4917 | job_types.push_back(TestConnectJob::kMockJob); |
| 4918 | job_types.push_back(TestConnectJob::kMockJob); |
| 4919 | job_types.push_back(TestConnectJob::kMockJob); |
| 4920 | connect_job_factory_->set_job_types(&job_types); |
| 4921 | |
| 4922 | ClientSocketHandle handle1; |
| 4923 | TestCompletionCallback callback1; |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4924 | EXPECT_EQ(OK, handle1.Init(TestGroupId("group1"), params_, absl::nullopt, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4925 | DEFAULT_PRIORITY, SocketTag(), |
| 4926 | ClientSocketPool::RespectLimits::ENABLED, |
| 4927 | callback1.callback(), |
| 4928 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4929 | NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4930 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4931 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("group2")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4932 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4933 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4934 | .WillRepeatedly(Invoke(&mock_layered_pool, |
| 4935 | &MockLayeredPool::ReleaseOneConnection)); |
| 4936 | mock_layered_pool.set_can_release_connection(false); |
| 4937 | |
| 4938 | // The third request is made when the socket pool is in a stalled state. |
| 4939 | ClientSocketHandle handle3; |
| 4940 | TestCompletionCallback callback3; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4941 | EXPECT_EQ(ERR_IO_PENDING, |
| 4942 | handle3.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4943 | TestGroupId("group3"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4944 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4945 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4946 | pool_.get(), NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4947 | |
| 4948 | base::RunLoop().RunUntilIdle(); |
| 4949 | EXPECT_FALSE(callback3.have_result()); |
| 4950 | |
| 4951 | // The fourth request is made when the pool is no longer stalled. The third |
| 4952 | // request should be serviced first, since it was issued first and has the |
| 4953 | // same priority. |
| 4954 | mock_layered_pool.set_can_release_connection(true); |
| 4955 | ClientSocketHandle handle4; |
| 4956 | TestCompletionCallback callback4; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4957 | EXPECT_EQ(ERR_IO_PENDING, |
| 4958 | handle4.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4959 | TestGroupId("group3"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4960 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4961 | callback4.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4962 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4963 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4964 | EXPECT_FALSE(callback4.have_result()); |
| 4965 | |
| 4966 | // Closing a handle should free up another socket slot. |
| 4967 | handle1.Reset(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4968 | EXPECT_THAT(callback4.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4969 | } |
| 4970 | |
| 4971 | // Tests the case when an idle socket can be closed when a new request is |
| 4972 | // issued, and the new request belongs to a group that was previously stalled. |
| 4973 | // |
| 4974 | // The two differences from the above test are that the stalled requests are not |
| 4975 | // in the same group as the layered pool's request, and the the fourth request |
| 4976 | // has a higher priority than the third one, so gets a socket first. |
| 4977 | TEST_F(ClientSocketPoolBaseTest, |
| 4978 | CloseIdleSocketsHeldByLayeredPoolInSameGroupWhenNeeded2) { |
| 4979 | CreatePool(2, 2); |
| 4980 | std::list<TestConnectJob::JobType> job_types; |
| 4981 | job_types.push_back(TestConnectJob::kMockJob); |
| 4982 | job_types.push_back(TestConnectJob::kMockJob); |
| 4983 | job_types.push_back(TestConnectJob::kMockJob); |
| 4984 | job_types.push_back(TestConnectJob::kMockJob); |
| 4985 | connect_job_factory_->set_job_types(&job_types); |
| 4986 | |
| 4987 | ClientSocketHandle handle1; |
| 4988 | TestCompletionCallback callback1; |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4989 | EXPECT_EQ(OK, handle1.Init(TestGroupId("group1"), params_, absl::nullopt, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4990 | DEFAULT_PRIORITY, SocketTag(), |
| 4991 | ClientSocketPool::RespectLimits::ENABLED, |
| 4992 | callback1.callback(), |
| 4993 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4994 | NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4995 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4996 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("group2")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4997 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4998 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4999 | .WillRepeatedly(Invoke(&mock_layered_pool, |
| 5000 | &MockLayeredPool::ReleaseOneConnection)); |
| 5001 | mock_layered_pool.set_can_release_connection(false); |
| 5002 | |
| 5003 | // The third request is made when the socket pool is in a stalled state. |
| 5004 | ClientSocketHandle handle3; |
| 5005 | TestCompletionCallback callback3; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 5006 | EXPECT_EQ( |
| 5007 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 5008 | handle3.Init(TestGroupId("group3"), params_, absl::nullopt, MEDIUM, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5009 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 5010 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5011 | pool_.get(), NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 5012 | |
| 5013 | base::RunLoop().RunUntilIdle(); |
| 5014 | EXPECT_FALSE(callback3.have_result()); |
| 5015 | |
| 5016 | // The fourth request is made when the pool is no longer stalled. This |
| 5017 | // request has a higher priority than the third request, so is serviced first. |
| 5018 | mock_layered_pool.set_can_release_connection(true); |
| 5019 | ClientSocketHandle handle4; |
| 5020 | TestCompletionCallback callback4; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 5021 | EXPECT_EQ( |
| 5022 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 5023 | handle4.Init(TestGroupId("group3"), params_, absl::nullopt, HIGHEST, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5024 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 5025 | callback4.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5026 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5027 | EXPECT_THAT(callback4.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 5028 | EXPECT_FALSE(callback3.have_result()); |
| 5029 | |
| 5030 | // Closing a handle should free up another socket slot. |
| 5031 | handle1.Reset(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5032 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 5033 | } |
| 5034 | |
| 5035 | TEST_F(ClientSocketPoolBaseTest, |
| 5036 | CloseMultipleIdleSocketsHeldByLayeredPoolWhenNeeded) { |
| 5037 | CreatePool(1, 1); |
| 5038 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 5039 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5040 | MockLayeredPool mock_layered_pool1(pool_.get(), TestGroupId("foo")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5041 | EXPECT_THAT(mock_layered_pool1.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 5042 | EXPECT_CALL(mock_layered_pool1, CloseOneIdleConnection()) |
| 5043 | .WillRepeatedly(Invoke(&mock_layered_pool1, |
| 5044 | &MockLayeredPool::ReleaseOneConnection)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5045 | MockLayeredPool mock_layered_pool2(pool_.get(), TestGroupId("bar")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5046 | EXPECT_THAT(mock_layered_pool2.RequestSocketWithoutLimits(pool_.get()), |
| 5047 | IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 5048 | EXPECT_CALL(mock_layered_pool2, CloseOneIdleConnection()) |
| 5049 | .WillRepeatedly(Invoke(&mock_layered_pool2, |
| 5050 | &MockLayeredPool::ReleaseOneConnection)); |
| 5051 | ClientSocketHandle handle; |
| 5052 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 5053 | EXPECT_EQ( |
| 5054 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 5055 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5056 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5057 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5058 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5059 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 5060 | } |
| 5061 | |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5062 | // Test that when a socket pool and group are at their limits, a request |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5063 | // with RespectLimits::DISABLED triggers creation of a new socket, and gets the |
| 5064 | // socket instead of a request with the same priority that was issued earlier, |
| 5065 | // but has RespectLimits::ENABLED. |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5066 | TEST_F(ClientSocketPoolBaseTest, IgnoreLimits) { |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5067 | CreatePool(1, 1); |
| 5068 | |
| 5069 | // Issue a request to reach the socket pool limit. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5070 | EXPECT_EQ(OK, StartRequestWithIgnoreLimits( |
| 5071 | TestGroupId("a"), MAXIMUM_PRIORITY, |
| 5072 | ClientSocketPool::RespectLimits::ENABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5073 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5074 | |
| 5075 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 5076 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5077 | EXPECT_EQ(ERR_IO_PENDING, StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5078 | TestGroupId("a"), MAXIMUM_PRIORITY, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5079 | ClientSocketPool::RespectLimits::ENABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5080 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5081 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5082 | // Issue a request that ignores the limits, so a new ConnectJob is |
| 5083 | // created. |
| 5084 | EXPECT_EQ(ERR_IO_PENDING, StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5085 | TestGroupId("a"), MAXIMUM_PRIORITY, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5086 | ClientSocketPool::RespectLimits::DISABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5087 | ASSERT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5088 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5089 | EXPECT_THAT(request(2)->WaitForResult(), IsOk()); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5090 | EXPECT_FALSE(request(1)->have_result()); |
| 5091 | } |
| 5092 | |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 5093 | // Test that when a socket pool and group are at their limits, a ConnectJob |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5094 | // issued for a request with RespectLimits::DISABLED is not cancelled when a |
| 5095 | // request with RespectLimits::ENABLED issued to the same group is cancelled. |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 5096 | TEST_F(ClientSocketPoolBaseTest, IgnoreLimitsCancelOtherJob) { |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 5097 | CreatePool(1, 1); |
| 5098 | |
| 5099 | // Issue a request to reach the socket pool limit. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5100 | EXPECT_EQ(OK, StartRequestWithIgnoreLimits( |
| 5101 | TestGroupId("a"), MAXIMUM_PRIORITY, |
| 5102 | ClientSocketPool::RespectLimits::ENABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5103 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 5104 | |
| 5105 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 5106 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5107 | EXPECT_EQ(ERR_IO_PENDING, StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5108 | TestGroupId("a"), MAXIMUM_PRIORITY, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5109 | ClientSocketPool::RespectLimits::ENABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5110 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 5111 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5112 | // Issue a request with RespectLimits::DISABLED, so a new ConnectJob is |
| 5113 | // created. |
| 5114 | EXPECT_EQ(ERR_IO_PENDING, StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5115 | TestGroupId("a"), MAXIMUM_PRIORITY, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5116 | ClientSocketPool::RespectLimits::DISABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5117 | ASSERT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5118 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5119 | // Cancel the pending request with RespectLimits::ENABLED. The ConnectJob |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5120 | // should not be cancelled. |
| 5121 | request(1)->handle()->Reset(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5122 | ASSERT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5123 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5124 | EXPECT_THAT(request(2)->WaitForResult(), IsOk()); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5125 | EXPECT_FALSE(request(1)->have_result()); |
| 5126 | } |
| 5127 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5128 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthNoAuthCallback) { |
| 5129 | CreatePool(1, 1); |
| 5130 | |
| 5131 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5132 | |
| 5133 | ClientSocketHandle handle; |
| 5134 | TestCompletionCallback callback; |
| 5135 | EXPECT_EQ( |
| 5136 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 5137 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5138 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5139 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5140 | pool_.get(), NetLogWithSource())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5141 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5142 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5143 | |
| 5144 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_PROXY_AUTH_REQUESTED)); |
| 5145 | EXPECT_FALSE(handle.is_initialized()); |
| 5146 | EXPECT_FALSE(handle.socket()); |
| 5147 | |
| 5148 | // The group should now be empty, and thus be deleted. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5149 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5150 | } |
| 5151 | |
| 5152 | class TestAuthHelper { |
| 5153 | public: |
| 5154 | TestAuthHelper() = default; |
| 5155 | ~TestAuthHelper() = default; |
| 5156 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5157 | void InitHandle( |
Matt Menke | 84d11e56 | 2019-03-27 00:11:19 | [diff] [blame] | 5158 | scoped_refptr<ClientSocketPool::SocketParams> params, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5159 | TransportClientSocketPool* pool, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5160 | RequestPriority priority = DEFAULT_PRIORITY, |
| 5161 | ClientSocketPool::RespectLimits respect_limits = |
| 5162 | ClientSocketPool::RespectLimits::ENABLED, |
| 5163 | const ClientSocketPool::GroupId& group_id_in = TestGroupId("a")) { |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5164 | EXPECT_EQ(ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 5165 | handle_.Init(group_id_in, params, absl::nullopt, priority, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5166 | SocketTag(), respect_limits, callback_.callback(), |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5167 | base::BindRepeating(&TestAuthHelper::AuthCallback, |
| 5168 | base::Unretained(this)), |
| 5169 | pool, NetLogWithSource())); |
| 5170 | } |
| 5171 | |
| 5172 | void WaitForAuth() { |
| 5173 | run_loop_ = std::make_unique<base::RunLoop>(); |
| 5174 | run_loop_->Run(); |
| 5175 | run_loop_.reset(); |
| 5176 | } |
| 5177 | |
| 5178 | void WaitForAuthAndRestartSync() { |
| 5179 | restart_sync_ = true; |
| 5180 | WaitForAuth(); |
| 5181 | restart_sync_ = false; |
| 5182 | } |
| 5183 | |
| 5184 | void WaitForAuthAndResetHandleSync() { |
| 5185 | reset_handle_sync_ = true; |
| 5186 | WaitForAuth(); |
| 5187 | reset_handle_sync_ = false; |
| 5188 | } |
| 5189 | |
| 5190 | void RestartWithAuth() { |
| 5191 | DCHECK(restart_with_auth_callback_); |
| 5192 | std::move(restart_with_auth_callback_).Run(); |
| 5193 | } |
| 5194 | |
| 5195 | int WaitForResult() { |
| 5196 | int result = callback_.WaitForResult(); |
| 5197 | // There shouldn't be any callback waiting to be invoked once the request is |
| 5198 | // complete. |
| 5199 | EXPECT_FALSE(restart_with_auth_callback_); |
| 5200 | // The socket should only be initialized on success. |
| 5201 | EXPECT_EQ(result == OK, handle_.is_initialized()); |
| 5202 | EXPECT_EQ(result == OK, handle_.socket() != nullptr); |
| 5203 | return result; |
| 5204 | } |
| 5205 | |
| 5206 | ClientSocketHandle* handle() { return &handle_; } |
| 5207 | int auth_count() const { return auth_count_; } |
| 5208 | int have_result() const { return callback_.have_result(); } |
| 5209 | |
| 5210 | private: |
| 5211 | void AuthCallback(const HttpResponseInfo& response, |
| 5212 | HttpAuthController* auth_controller, |
| 5213 | base::OnceClosure restart_with_auth_callback) { |
| 5214 | EXPECT_FALSE(restart_with_auth_callback_); |
| 5215 | EXPECT_TRUE(restart_with_auth_callback); |
| 5216 | |
| 5217 | // Once there's a result, this method shouldn't be invoked again. |
| 5218 | EXPECT_FALSE(callback_.have_result()); |
| 5219 | |
| 5220 | ++auth_count_; |
| 5221 | run_loop_->Quit(); |
| 5222 | if (restart_sync_) { |
| 5223 | std::move(restart_with_auth_callback).Run(); |
| 5224 | return; |
| 5225 | } |
| 5226 | |
| 5227 | restart_with_auth_callback_ = std::move(restart_with_auth_callback); |
| 5228 | |
| 5229 | if (reset_handle_sync_) { |
| 5230 | handle_.Reset(); |
| 5231 | return; |
| 5232 | } |
| 5233 | } |
| 5234 | |
| 5235 | std::unique_ptr<base::RunLoop> run_loop_; |
| 5236 | base::OnceClosure restart_with_auth_callback_; |
| 5237 | |
| 5238 | bool restart_sync_ = false; |
| 5239 | bool reset_handle_sync_ = false; |
| 5240 | |
| 5241 | ClientSocketHandle handle_; |
| 5242 | int auth_count_ = 0; |
| 5243 | TestCompletionCallback callback_; |
| 5244 | |
| 5245 | DISALLOW_COPY_AND_ASSIGN(TestAuthHelper); |
| 5246 | }; |
| 5247 | |
| 5248 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnce) { |
| 5249 | CreatePool(1, 1); |
| 5250 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5251 | |
| 5252 | TestAuthHelper auth_helper; |
| 5253 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5254 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5255 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5256 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5257 | |
| 5258 | auth_helper.WaitForAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5259 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5260 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5261 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5262 | |
| 5263 | auth_helper.RestartWithAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5264 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5265 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5266 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5267 | |
| 5268 | EXPECT_THAT(auth_helper.WaitForResult(), IsOk()); |
| 5269 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5270 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5271 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5272 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5273 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5274 | } |
| 5275 | |
| 5276 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceSync) { |
| 5277 | CreatePool(1, 1); |
| 5278 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5279 | |
| 5280 | TestAuthHelper auth_helper; |
| 5281 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5282 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5283 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5284 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5285 | |
| 5286 | auth_helper.WaitForAuthAndRestartSync(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5287 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5288 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5289 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5290 | |
| 5291 | EXPECT_THAT(auth_helper.WaitForResult(), IsOk()); |
| 5292 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5293 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5294 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5295 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5296 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5297 | } |
| 5298 | |
| 5299 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceFails) { |
| 5300 | CreatePool(1, 1); |
| 5301 | connect_job_factory_->set_job_type( |
| 5302 | TestConnectJob::kMockAuthChallengeOnceFailingJob); |
| 5303 | |
| 5304 | TestAuthHelper auth_helper; |
| 5305 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5306 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5307 | |
| 5308 | auth_helper.WaitForAuth(); |
| 5309 | auth_helper.RestartWithAuth(); |
| 5310 | EXPECT_THAT(auth_helper.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 5311 | |
| 5312 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5313 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5314 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5315 | } |
| 5316 | |
| 5317 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceSyncFails) { |
| 5318 | CreatePool(1, 1); |
| 5319 | connect_job_factory_->set_job_type( |
| 5320 | TestConnectJob::kMockAuthChallengeOnceFailingJob); |
| 5321 | |
| 5322 | TestAuthHelper auth_helper; |
| 5323 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5324 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5325 | |
| 5326 | auth_helper.WaitForAuthAndRestartSync(); |
| 5327 | EXPECT_THAT(auth_helper.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 5328 | |
| 5329 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5330 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5331 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5332 | } |
| 5333 | |
| 5334 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceDeleteHandle) { |
| 5335 | CreatePool(1, 1); |
| 5336 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5337 | |
| 5338 | TestAuthHelper auth_helper; |
| 5339 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5340 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5341 | |
| 5342 | auth_helper.WaitForAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5343 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5344 | |
| 5345 | auth_helper.handle()->Reset(); |
| 5346 | |
| 5347 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5348 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5349 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5350 | EXPECT_FALSE(auth_helper.handle()->is_initialized()); |
| 5351 | EXPECT_FALSE(auth_helper.handle()->socket()); |
| 5352 | } |
| 5353 | |
| 5354 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceDeleteHandleSync) { |
| 5355 | CreatePool(1, 1); |
| 5356 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5357 | |
| 5358 | TestAuthHelper auth_helper; |
| 5359 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5360 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5361 | |
| 5362 | auth_helper.WaitForAuthAndResetHandleSync(); |
| 5363 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5364 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5365 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5366 | EXPECT_FALSE(auth_helper.handle()->is_initialized()); |
| 5367 | EXPECT_FALSE(auth_helper.handle()->socket()); |
| 5368 | } |
| 5369 | |
| 5370 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceFlushWithError) { |
| 5371 | CreatePool(1, 1); |
| 5372 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5373 | |
| 5374 | TestAuthHelper auth_helper; |
| 5375 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5376 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5377 | |
| 5378 | auth_helper.WaitForAuth(); |
| 5379 | |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 5380 | pool_->FlushWithError(ERR_FAILED, "Network changed"); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5381 | base::RunLoop().RunUntilIdle(); |
| 5382 | |
| 5383 | // When flushing the socket pool, bound sockets should delay returning the |
| 5384 | // error until completion. |
| 5385 | EXPECT_FALSE(auth_helper.have_result()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5386 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5387 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5388 | |
| 5389 | auth_helper.RestartWithAuth(); |
| 5390 | // The callback should be called asynchronously. |
| 5391 | EXPECT_FALSE(auth_helper.have_result()); |
| 5392 | |
| 5393 | EXPECT_THAT(auth_helper.WaitForResult(), IsError(ERR_FAILED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5394 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5395 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5396 | } |
| 5397 | |
| 5398 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthTwice) { |
| 5399 | CreatePool(1, 1); |
| 5400 | connect_job_factory_->set_job_type( |
| 5401 | TestConnectJob::kMockAuthChallengeTwiceJob); |
| 5402 | |
| 5403 | TestAuthHelper auth_helper; |
| 5404 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5405 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5406 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5407 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5408 | |
| 5409 | auth_helper.WaitForAuth(); |
| 5410 | auth_helper.RestartWithAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5411 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5412 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5413 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5414 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5415 | |
| 5416 | auth_helper.WaitForAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5417 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5418 | EXPECT_EQ(2, auth_helper.auth_count()); |
| 5419 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5420 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5421 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5422 | auth_helper.RestartWithAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5423 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5424 | EXPECT_EQ(2, auth_helper.auth_count()); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5425 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5426 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5427 | |
| 5428 | EXPECT_THAT(auth_helper.WaitForResult(), IsOk()); |
| 5429 | EXPECT_EQ(2, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5430 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5431 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5432 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5433 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5434 | } |
| 5435 | |
| 5436 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthTwiceFails) { |
| 5437 | CreatePool(1, 1); |
| 5438 | connect_job_factory_->set_job_type( |
| 5439 | TestConnectJob::kMockAuthChallengeTwiceFailingJob); |
| 5440 | |
| 5441 | TestAuthHelper auth_helper; |
| 5442 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5443 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5444 | |
| 5445 | auth_helper.WaitForAuth(); |
| 5446 | auth_helper.RestartWithAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5447 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5448 | EXPECT_EQ(1, auth_helper.auth_count()); |
| 5449 | |
| 5450 | auth_helper.WaitForAuth(); |
| 5451 | auth_helper.RestartWithAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5452 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5453 | EXPECT_EQ(2, auth_helper.auth_count()); |
| 5454 | |
| 5455 | EXPECT_THAT(auth_helper.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 5456 | EXPECT_EQ(2, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5457 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5458 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5459 | } |
| 5460 | |
| 5461 | // Makes sure that when a bound request is destroyed, a new ConnectJob is |
| 5462 | // created, if needed. |
| 5463 | TEST_F(ClientSocketPoolBaseTest, |
| 5464 | ProxyAuthCreateNewConnectJobOnDestroyBoundRequest) { |
| 5465 | CreatePool(1 /* max_sockets */, 1 /* max_sockets_per_group */); |
| 5466 | connect_job_factory_->set_job_type( |
| 5467 | TestConnectJob::kMockAuthChallengeOnceFailingJob); |
| 5468 | |
| 5469 | // First request creates a ConnectJob. |
| 5470 | TestAuthHelper auth_helper1; |
| 5471 | auth_helper1.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5472 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5473 | |
| 5474 | // A second request come in, but no new ConnectJob is needed, since the limit |
| 5475 | // has been reached. |
| 5476 | TestAuthHelper auth_helper2; |
| 5477 | auth_helper2.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5478 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5479 | |
| 5480 | // Run until the auth callback for the first request is invoked. |
| 5481 | auth_helper1.WaitForAuth(); |
| 5482 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5483 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5484 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5485 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5486 | |
| 5487 | // Make connect jobs succeed, then cancel the first request, which should |
| 5488 | // destroy the bound ConnectJob, and cause a new ConnectJob to start. |
| 5489 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 5490 | auth_helper1.handle()->Reset(); |
| 5491 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5492 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5493 | |
| 5494 | // The second ConnectJob should succeed. |
| 5495 | EXPECT_THAT(auth_helper2.WaitForResult(), IsOk()); |
| 5496 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5497 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5498 | } |
| 5499 | |
| 5500 | // Makes sure that when a bound request is destroyed, a new ConnectJob is |
| 5501 | // created for another group, if needed. |
| 5502 | TEST_F(ClientSocketPoolBaseTest, |
| 5503 | ProxyAuthCreateNewConnectJobOnDestroyBoundRequestDifferentGroups) { |
| 5504 | CreatePool(1 /* max_sockets */, 1 /* max_sockets_per_group */); |
| 5505 | connect_job_factory_->set_job_type( |
| 5506 | TestConnectJob::kMockAuthChallengeOnceFailingJob); |
| 5507 | |
| 5508 | // First request creates a ConnectJob. |
| 5509 | TestAuthHelper auth_helper1; |
| 5510 | auth_helper1.InitHandle(params_, pool_.get(), DEFAULT_PRIORITY); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5511 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5512 | |
| 5513 | // A second request come in, but no new ConnectJob is needed, since the limit |
| 5514 | // has been reached. |
| 5515 | TestAuthHelper auth_helper2; |
| 5516 | auth_helper2.InitHandle(params_, pool_.get(), DEFAULT_PRIORITY, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5517 | ClientSocketPool::RespectLimits::ENABLED, |
| 5518 | TestGroupId("b")); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5519 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5520 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5521 | |
| 5522 | // Run until the auth callback for the first request is invoked. |
| 5523 | auth_helper1.WaitForAuth(); |
| 5524 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5525 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5526 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5527 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5528 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 5529 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5530 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5531 | |
| 5532 | // Make connect jobs succeed, then cancel the first request, which should |
| 5533 | // destroy the bound ConnectJob, and cause a new ConnectJob to start for the |
| 5534 | // other group. |
| 5535 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 5536 | auth_helper1.handle()->Reset(); |
| 5537 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5538 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 5539 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5540 | |
| 5541 | // The second ConnectJob should succeed. |
| 5542 | EXPECT_THAT(auth_helper2.WaitForResult(), IsOk()); |
| 5543 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5544 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 5545 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5546 | } |
| 5547 | |
| 5548 | // Test that once an auth challenge is bound, that's the request that gets all |
| 5549 | // subsequent calls and the socket itself. |
| 5550 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthStaysBound) { |
| 5551 | CreatePool(1, 1); |
| 5552 | connect_job_factory_->set_job_type( |
| 5553 | TestConnectJob::kMockAuthChallengeTwiceJob); |
| 5554 | |
| 5555 | // First request creates a ConnectJob. |
| 5556 | TestAuthHelper auth_helper1; |
| 5557 | auth_helper1.InitHandle(params_, pool_.get(), LOWEST); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5558 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5559 | |
| 5560 | // A second, higher priority request is made. |
| 5561 | TestAuthHelper auth_helper2; |
| 5562 | auth_helper2.InitHandle(params_, pool_.get(), LOW); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5563 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5564 | |
| 5565 | // Run until the auth callback for the second request is invoked. |
| 5566 | auth_helper2.WaitForAuth(); |
| 5567 | EXPECT_EQ(0, auth_helper1.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5568 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5569 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5570 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5571 | |
| 5572 | // Start a higher priority job. It shouldn't be able to steal |auth_helper2|'s |
| 5573 | // ConnectJob. |
| 5574 | TestAuthHelper auth_helper3; |
| 5575 | auth_helper3.InitHandle(params_, pool_.get(), HIGHEST); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5576 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5577 | |
| 5578 | // Start a higher job that ignores limits, creating a hanging socket. It |
| 5579 | // shouldn't be able to steal |auth_helper2|'s ConnectJob. |
| 5580 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 5581 | TestAuthHelper auth_helper4; |
| 5582 | auth_helper4.InitHandle(params_, pool_.get(), HIGHEST, |
| 5583 | ClientSocketPool::RespectLimits::DISABLED); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5584 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5585 | |
| 5586 | // Restart with auth, and |auth_helper2|'s auth method should be invoked |
| 5587 | // again. |
| 5588 | auth_helper2.RestartWithAuth(); |
| 5589 | auth_helper2.WaitForAuth(); |
| 5590 | EXPECT_EQ(0, auth_helper1.auth_count()); |
| 5591 | EXPECT_FALSE(auth_helper1.have_result()); |
| 5592 | EXPECT_EQ(2, auth_helper2.auth_count()); |
| 5593 | EXPECT_FALSE(auth_helper2.have_result()); |
| 5594 | EXPECT_EQ(0, auth_helper3.auth_count()); |
| 5595 | EXPECT_FALSE(auth_helper3.have_result()); |
| 5596 | EXPECT_EQ(0, auth_helper4.auth_count()); |
| 5597 | EXPECT_FALSE(auth_helper4.have_result()); |
| 5598 | |
| 5599 | // Advance auth again, and |auth_helper2| should get the socket. |
| 5600 | auth_helper2.RestartWithAuth(); |
| 5601 | EXPECT_THAT(auth_helper2.WaitForResult(), IsOk()); |
| 5602 | // The hung ConnectJob for the RespectLimits::DISABLED request is still in the |
| 5603 | // socket pool. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5604 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5605 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5606 | EXPECT_EQ(0, auth_helper1.auth_count()); |
| 5607 | EXPECT_FALSE(auth_helper1.have_result()); |
| 5608 | EXPECT_EQ(0, auth_helper3.auth_count()); |
| 5609 | EXPECT_FALSE(auth_helper3.have_result()); |
| 5610 | EXPECT_EQ(0, auth_helper4.auth_count()); |
| 5611 | EXPECT_FALSE(auth_helper4.have_result()); |
| 5612 | |
| 5613 | // If the socket is returned to the socket pool, the RespectLimits::DISABLED |
| 5614 | // socket request should be able to claim it. |
| 5615 | auth_helper2.handle()->Reset(); |
| 5616 | EXPECT_THAT(auth_helper4.WaitForResult(), IsOk()); |
| 5617 | EXPECT_EQ(0, auth_helper1.auth_count()); |
| 5618 | EXPECT_FALSE(auth_helper1.have_result()); |
| 5619 | EXPECT_EQ(0, auth_helper3.auth_count()); |
| 5620 | EXPECT_FALSE(auth_helper3.have_result()); |
| 5621 | EXPECT_EQ(0, auth_helper4.auth_count()); |
| 5622 | } |
| 5623 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5624 | enum class RefreshType { |
| 5625 | kServer, |
| 5626 | kProxy, |
| 5627 | }; |
| 5628 | |
| 5629 | // Common base class to test RefreshGroup() when called from either |
| 5630 | // OnSSLConfigForServerChanged() matching a specific group or the pool's proxy. |
| 5631 | // |
| 5632 | // Tests which test behavior specific to one or the other case should use |
| 5633 | // ClientSocketPoolBaseTest directly. In particular, there is no "other group" |
| 5634 | // when the pool's proxy matches. |
| 5635 | class ClientSocketPoolBaseRefreshTest |
| 5636 | : public ClientSocketPoolBaseTest, |
| 5637 | public testing::WithParamInterface<RefreshType> { |
| 5638 | public: |
| 5639 | void CreatePoolForRefresh(int max_sockets, |
| 5640 | int max_sockets_per_group, |
| 5641 | bool enable_backup_connect_jobs = false) { |
| 5642 | switch (GetParam()) { |
| 5643 | case RefreshType::kServer: |
| 5644 | CreatePool(max_sockets, max_sockets_per_group, |
| 5645 | enable_backup_connect_jobs); |
| 5646 | break; |
| 5647 | case RefreshType::kProxy: |
| 5648 | CreatePoolWithIdleTimeouts( |
| 5649 | max_sockets, max_sockets_per_group, kUnusedIdleSocketTimeout, |
| 5650 | ClientSocketPool::used_idle_socket_timeout(), |
| 5651 | enable_backup_connect_jobs, |
| 5652 | ProxyServer::FromPacString("HTTPS myproxy:70")); |
| 5653 | break; |
| 5654 | } |
| 5655 | } |
| 5656 | |
| 5657 | static ClientSocketPool::GroupId GetGroupId() { |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame^] | 5658 | return TestGroupId("a", 443, url::kHttpsScheme); |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5659 | } |
| 5660 | |
David Benjamin | 6dd7e88 | 2019-10-10 02:35:23 | [diff] [blame] | 5661 | static ClientSocketPool::GroupId GetGroupIdInPartition() { |
| 5662 | // Note this GroupId will match GetGroupId() unless |
| 5663 | // kPartitionConnectionsByNetworkIsolationKey is enabled. |
Matt Menke | 4807a9a | 2020-11-21 00:14:41 | [diff] [blame] | 5664 | const SchemefulSite kSite(GURL("https://b/")); |
| 5665 | const NetworkIsolationKey kNetworkIsolationKey(kSite, kSite); |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame^] | 5666 | return TestGroupId("a", 443, url::kHttpsScheme, |
David Benjamin | 6dd7e88 | 2019-10-10 02:35:23 | [diff] [blame] | 5667 | PrivacyMode::PRIVACY_MODE_DISABLED, |
| 5668 | kNetworkIsolationKey); |
| 5669 | } |
| 5670 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5671 | void OnSSLConfigForServerChanged() { |
| 5672 | switch (GetParam()) { |
| 5673 | case RefreshType::kServer: |
| 5674 | pool_->OnSSLConfigForServerChanged(HostPortPair("a", 443)); |
| 5675 | break; |
| 5676 | case RefreshType::kProxy: |
| 5677 | pool_->OnSSLConfigForServerChanged(HostPortPair("myproxy", 70)); |
| 5678 | break; |
| 5679 | } |
| 5680 | } |
| 5681 | }; |
| 5682 | |
| 5683 | INSTANTIATE_TEST_SUITE_P(RefreshType, |
| 5684 | ClientSocketPoolBaseRefreshTest, |
| 5685 | ::testing::Values(RefreshType::kServer, |
| 5686 | RefreshType::kProxy)); |
| 5687 | |
| 5688 | TEST_P(ClientSocketPoolBaseRefreshTest, RefreshGroupCreatesNewConnectJobs) { |
| 5689 | CreatePoolForRefresh(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 5690 | const ClientSocketPool::GroupId kGroupId = GetGroupId(); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5691 | |
| 5692 | // First job will be waiting until it gets aborted. |
| 5693 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 5694 | |
| 5695 | ClientSocketHandle handle; |
| 5696 | TestCompletionCallback callback; |
| 5697 | EXPECT_THAT( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 5698 | handle.Init(kGroupId, params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5699 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5700 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5701 | pool_.get(), NetLogWithSource()), |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5702 | IsError(ERR_IO_PENDING)); |
| 5703 | |
| 5704 | // Switch connect job types, so creating a new ConnectJob will result in |
| 5705 | // success. |
| 5706 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 5707 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5708 | OnSSLConfigForServerChanged(); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5709 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 5710 | ASSERT_TRUE(handle.socket()); |
| 5711 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5712 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId)); |
| 5713 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(kGroupId)); |
| 5714 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(kGroupId)); |
| 5715 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId)); |
| 5716 | } |
| 5717 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5718 | TEST_P(ClientSocketPoolBaseRefreshTest, RefreshGroupClosesIdleConnectJobs) { |
David Benjamin | 6dd7e88 | 2019-10-10 02:35:23 | [diff] [blame] | 5719 | base::test::ScopedFeatureList feature_list; |
| 5720 | feature_list.InitAndEnableFeature( |
| 5721 | features::kPartitionConnectionsByNetworkIsolationKey); |
| 5722 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5723 | CreatePoolForRefresh(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 5724 | const ClientSocketPool::GroupId kGroupId = GetGroupId(); |
David Benjamin | 6dd7e88 | 2019-10-10 02:35:23 | [diff] [blame] | 5725 | const ClientSocketPool::GroupId kGroupIdInPartition = GetGroupIdInPartition(); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5726 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 5727 | pool_->RequestSockets(kGroupId, params_, absl::nullopt, 2, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5728 | NetLogWithSource()); |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 5729 | pool_->RequestSockets(kGroupIdInPartition, params_, absl::nullopt, 2, |
David Benjamin | 6dd7e88 | 2019-10-10 02:35:23 | [diff] [blame] | 5730 | NetLogWithSource()); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5731 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId)); |
David Benjamin | 6dd7e88 | 2019-10-10 02:35:23 | [diff] [blame] | 5732 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupIdInPartition)); |
| 5733 | EXPECT_EQ(4, pool_->IdleSocketCount()); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5734 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(kGroupId)); |
David Benjamin | 6dd7e88 | 2019-10-10 02:35:23 | [diff] [blame] | 5735 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(kGroupIdInPartition)); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5736 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5737 | OnSSLConfigForServerChanged(); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5738 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5739 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupId)); |
David Benjamin | 6dd7e88 | 2019-10-10 02:35:23 | [diff] [blame] | 5740 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupIdInPartition)); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5741 | } |
| 5742 | |
| 5743 | TEST_F(ClientSocketPoolBaseTest, |
| 5744 | RefreshGroupDoesNotCloseIdleConnectJobsInOtherGroup) { |
| 5745 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5746 | const ClientSocketPool::GroupId kGroupId = |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame^] | 5747 | TestGroupId("a", 443, url::kHttpsScheme); |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5748 | const ClientSocketPool::GroupId kOtherGroupId = |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame^] | 5749 | TestGroupId("b", 443, url::kHttpsScheme); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5750 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 5751 | pool_->RequestSockets(kOtherGroupId, params_, absl::nullopt, 2, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5752 | NetLogWithSource()); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5753 | ASSERT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 5754 | EXPECT_EQ(2, pool_->IdleSocketCount()); |
| 5755 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(kOtherGroupId)); |
| 5756 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5757 | pool_->OnSSLConfigForServerChanged(HostPortPair("a", 443)); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5758 | ASSERT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 5759 | EXPECT_EQ(2, pool_->IdleSocketCount()); |
| 5760 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(kOtherGroupId)); |
| 5761 | } |
| 5762 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5763 | TEST_P(ClientSocketPoolBaseRefreshTest, RefreshGroupPreventsSocketReuse) { |
| 5764 | CreatePoolForRefresh(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 5765 | const ClientSocketPool::GroupId kGroupId = GetGroupId(); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5766 | |
| 5767 | ClientSocketHandle handle; |
| 5768 | TestCompletionCallback callback; |
| 5769 | EXPECT_THAT( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 5770 | handle.Init(kGroupId, params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5771 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5772 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5773 | pool_.get(), NetLogWithSource()), |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5774 | IsOk()); |
| 5775 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId)); |
| 5776 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId)); |
| 5777 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5778 | OnSSLConfigForServerChanged(); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5779 | |
| 5780 | handle.Reset(); |
| 5781 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5782 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupId)); |
| 5783 | } |
| 5784 | |
| 5785 | TEST_F(ClientSocketPoolBaseTest, |
| 5786 | RefreshGroupDoesNotPreventSocketReuseInOtherGroup) { |
| 5787 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5788 | const ClientSocketPool::GroupId kGroupId = |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame^] | 5789 | TestGroupId("a", 443, url::kHttpsScheme); |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5790 | const ClientSocketPool::GroupId kOtherGroupId = |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame^] | 5791 | TestGroupId("b", 443, url::kHttpsScheme); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5792 | |
| 5793 | ClientSocketHandle handle; |
| 5794 | TestCompletionCallback callback; |
| 5795 | EXPECT_THAT( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 5796 | handle.Init(kOtherGroupId, params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5797 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5798 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5799 | pool_.get(), NetLogWithSource()), |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5800 | IsOk()); |
| 5801 | ASSERT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 5802 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kOtherGroupId)); |
| 5803 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5804 | pool_->OnSSLConfigForServerChanged(HostPortPair("a", 443)); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5805 | |
| 5806 | handle.Reset(); |
| 5807 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 5808 | ASSERT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 5809 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(kOtherGroupId)); |
| 5810 | } |
| 5811 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5812 | TEST_P(ClientSocketPoolBaseRefreshTest, |
| 5813 | RefreshGroupReplacesBoundConnectJobOnConnect) { |
| 5814 | CreatePoolForRefresh(1, 1); |
| 5815 | const ClientSocketPool::GroupId kGroupId = GetGroupId(); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5816 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5817 | |
| 5818 | TestAuthHelper auth_helper; |
| 5819 | auth_helper.InitHandle(params_, pool_.get(), DEFAULT_PRIORITY, |
| 5820 | ClientSocketPool::RespectLimits::ENABLED, kGroupId); |
| 5821 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(kGroupId)); |
| 5822 | |
| 5823 | auth_helper.WaitForAuth(); |
| 5824 | |
| 5825 | // This should update the generation, but not cancel the old ConnectJob - it's |
| 5826 | // not safe to do anything while waiting on the original ConnectJob. |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5827 | OnSSLConfigForServerChanged(); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5828 | |
| 5829 | // Providing auth credentials and restarting the request with them will cause |
| 5830 | // the ConnectJob to complete successfully, but the result will be discarded |
| 5831 | // because of the generation mismatch. |
| 5832 | auth_helper.RestartWithAuth(); |
| 5833 | |
| 5834 | // Despite using ConnectJobs that simulate a single challenge, a second |
| 5835 | // challenge will be seen, due to using a new ConnectJob. |
| 5836 | auth_helper.WaitForAuth(); |
| 5837 | auth_helper.RestartWithAuth(); |
| 5838 | |
| 5839 | EXPECT_THAT(auth_helper.WaitForResult(), IsOk()); |
| 5840 | EXPECT_TRUE(auth_helper.handle()->socket()); |
| 5841 | EXPECT_EQ(2, auth_helper.auth_count()); |
| 5842 | |
| 5843 | // When released, the socket will be returned to the socket pool, and |
| 5844 | // available for reuse. |
| 5845 | auth_helper.handle()->Reset(); |
| 5846 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 5847 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId)); |
| 5848 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(kGroupId)); |
| 5849 | } |
| 5850 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5851 | TEST_F(ClientSocketPoolBaseTest, RefreshProxyRefreshesAllGroups) { |
| 5852 | CreatePoolWithIdleTimeouts(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 5853 | kUnusedIdleSocketTimeout, |
| 5854 | ClientSocketPool::used_idle_socket_timeout(), |
| 5855 | false /* no backup connect jobs */, |
| 5856 | ProxyServer::FromPacString("HTTPS myproxy:70")); |
| 5857 | |
| 5858 | const ClientSocketPool::GroupId kGroupId1 = |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame^] | 5859 | TestGroupId("a", 443, url::kHttpsScheme); |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5860 | const ClientSocketPool::GroupId kGroupId2 = |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame^] | 5861 | TestGroupId("b", 443, url::kHttpsScheme); |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5862 | const ClientSocketPool::GroupId kGroupId3 = |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame^] | 5863 | TestGroupId("c", 443, url::kHttpsScheme); |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5864 | |
| 5865 | // Make three sockets in three different groups. The third socket is released |
| 5866 | // to the pool as idle. |
| 5867 | ClientSocketHandle handle1, handle2, handle3; |
| 5868 | TestCompletionCallback callback; |
| 5869 | EXPECT_THAT( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 5870 | handle1.Init(kGroupId1, params_, absl::nullopt, DEFAULT_PRIORITY, |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5871 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5872 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5873 | pool_.get(), NetLogWithSource()), |
| 5874 | IsOk()); |
| 5875 | EXPECT_THAT( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 5876 | handle2.Init(kGroupId2, params_, absl::nullopt, DEFAULT_PRIORITY, |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5877 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5878 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5879 | pool_.get(), NetLogWithSource()), |
| 5880 | IsOk()); |
| 5881 | EXPECT_THAT( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 5882 | handle3.Init(kGroupId3, params_, absl::nullopt, DEFAULT_PRIORITY, |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5883 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5884 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5885 | pool_.get(), NetLogWithSource()), |
| 5886 | IsOk()); |
| 5887 | handle3.Reset(); |
| 5888 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId1)); |
| 5889 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId1)); |
| 5890 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId2)); |
| 5891 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId2)); |
| 5892 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId3)); |
| 5893 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(kGroupId3)); |
| 5894 | |
| 5895 | // Changes to some other proxy do not affect the pool. The idle socket remains |
| 5896 | // alive and closing |handle2| makes the socket available for the pool. |
| 5897 | pool_->OnSSLConfigForServerChanged(HostPortPair("someotherproxy", 70)); |
| 5898 | |
| 5899 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId1)); |
| 5900 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId1)); |
| 5901 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId2)); |
| 5902 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId2)); |
| 5903 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId3)); |
| 5904 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(kGroupId3)); |
| 5905 | |
| 5906 | handle2.Reset(); |
| 5907 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId2)); |
| 5908 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(kGroupId2)); |
| 5909 | |
| 5910 | // Changes to the matching proxy refreshes all groups. |
| 5911 | pool_->OnSSLConfigForServerChanged(HostPortPair("myproxy", 70)); |
| 5912 | |
| 5913 | // Idle sockets are closed. |
| 5914 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5915 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupId2)); |
| 5916 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupId3)); |
| 5917 | |
| 5918 | // The active socket, however, continues to be active. |
| 5919 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId1)); |
| 5920 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId1)); |
| 5921 | |
| 5922 | // Closing it does not make it available for the pool. |
| 5923 | handle1.Reset(); |
| 5924 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5925 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupId1)); |
| 5926 | } |
| 5927 | |
| 5928 | TEST_F(ClientSocketPoolBaseTest, RefreshBothPrivacyAndNormalSockets) { |
| 5929 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 5930 | |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame^] | 5931 | const ClientSocketPool::GroupId kGroupId = TestGroupId( |
| 5932 | "a", 443, url::kHttpsScheme, PrivacyMode::PRIVACY_MODE_DISABLED); |
| 5933 | const ClientSocketPool::GroupId kGroupIdPrivacy = TestGroupId( |
| 5934 | "a", 443, url::kHttpsScheme, PrivacyMode::PRIVACY_MODE_ENABLED); |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5935 | const ClientSocketPool::GroupId kOtherGroupId = |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame^] | 5936 | TestGroupId("b", 443, url::kHttpsScheme); |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5937 | |
| 5938 | // Make a socket in each groups. |
| 5939 | ClientSocketHandle handle1, handle2, handle3; |
| 5940 | TestCompletionCallback callback; |
| 5941 | EXPECT_THAT( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 5942 | handle1.Init(kGroupId, params_, absl::nullopt, DEFAULT_PRIORITY, |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5943 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5944 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5945 | pool_.get(), NetLogWithSource()), |
| 5946 | IsOk()); |
| 5947 | EXPECT_THAT( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 5948 | handle2.Init(kGroupIdPrivacy, params_, absl::nullopt, DEFAULT_PRIORITY, |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5949 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5950 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5951 | pool_.get(), NetLogWithSource()), |
| 5952 | IsOk()); |
| 5953 | EXPECT_THAT( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 5954 | handle3.Init(kOtherGroupId, params_, absl::nullopt, DEFAULT_PRIORITY, |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5955 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5956 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5957 | pool_.get(), NetLogWithSource()), |
| 5958 | IsOk()); |
| 5959 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId)); |
| 5960 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId)); |
| 5961 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupIdPrivacy)); |
| 5962 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupIdPrivacy)); |
| 5963 | ASSERT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 5964 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kOtherGroupId)); |
| 5965 | |
| 5966 | pool_->OnSSLConfigForServerChanged(HostPortPair("a", 443)); |
| 5967 | |
| 5968 | // Active sockets continue to be active. |
| 5969 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId)); |
| 5970 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId)); |
| 5971 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupIdPrivacy)); |
| 5972 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupIdPrivacy)); |
| 5973 | ASSERT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 5974 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kOtherGroupId)); |
| 5975 | |
| 5976 | // Closing them leaves kOtherGroupId alone, but kGroupId and kGroupIdPrivacy |
| 5977 | // are unusable. |
| 5978 | handle1.Reset(); |
| 5979 | handle2.Reset(); |
| 5980 | handle3.Reset(); |
| 5981 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 5982 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupId)); |
| 5983 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupIdPrivacy)); |
| 5984 | EXPECT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 5985 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(kOtherGroupId)); |
| 5986 | } |
| 5987 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 5988 | } // namespace |
| 5989 | |
| 5990 | } // namespace net |