Avi Drissman | 6459548 | 2022-09-14 20:52:29 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[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 | |
tbansal | f82cc8e | 2015-10-14 20:05:49 | [diff] [blame] | 5 | #include <stdint.h> |
Daniel Cheng | 5feb16f | 2022-02-28 06:52:07 | [diff] [blame] | 6 | |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 7 | #include <utility> |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 8 | #include <vector> |
| 9 | |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 10 | #include "base/bind.h" |
[email protected] | 2041cf34 | 2010-02-19 03:15:59 | [diff] [blame] | 11 | #include "base/callback.h" |
danakj | db9ae794 | 2020-11-11 16:01:35 | [diff] [blame] | 12 | #include "base/callback_helpers.h" |
Hans Wennborg | 0924470b | 2020-04-27 21:08:05 | [diff] [blame] | 13 | #include "base/check_op.h" |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 14 | #include "base/location.h" |
Daniel Cheng | 5feb16f | 2022-02-28 06:52:07 | [diff] [blame] | 15 | #include "base/memory/raw_ptr.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 16 | #include "base/memory/ref_counted.h" |
[email protected] | 6ea7b15 | 2011-12-21 21:21:13 | [diff] [blame] | 17 | #include "base/memory/weak_ptr.h" |
Hans Wennborg | 0924470b | 2020-04-27 21:08:05 | [diff] [blame] | 18 | #include "base/notreached.h" |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 19 | #include "base/run_loop.h" |
[email protected] | fc9be580 | 2013-06-11 10:56:51 | [diff] [blame] | 20 | #include "base/strings/string_number_conversions.h" |
[email protected] | 18b57741 | 2013-07-18 04:19:15 | [diff] [blame] | 21 | #include "base/strings/stringprintf.h" |
Patrick Monette | 643cdf6 | 2021-10-15 19:13:42 | [diff] [blame] | 22 | #include "base/task/single_thread_task_runner.h" |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 23 | #include "base/test/scoped_feature_list.h" |
[email protected] | f214f879 | 2011-01-01 02:17:08 | [diff] [blame] | 24 | #include "base/threading/platform_thread.h" |
gab | f767595f | 2016-05-11 18:50:35 | [diff] [blame] | 25 | #include "base/threading/thread_task_runner_handle.h" |
Gabriel Charette | d87f10f | 2022-03-31 00:44:22 | [diff] [blame] | 26 | #include "base/time/time.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" |
Brianna Goldstein | a7593fca | 2022-09-28 02:59:32 | [diff] [blame^] | 33 | #include "net/base/network_anonymization_key.h" |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 34 | #include "net/base/network_isolation_key.h" |
Matt Menke | bdf77780 | 2019-04-22 19:38:59 | [diff] [blame] | 35 | #include "net/base/privacy_mode.h" |
Matt Menke | aafff54 | 2019-04-22 22:09:36 | [diff] [blame] | 36 | #include "net/base/proxy_server.h" |
Eric Orth | 5ccc3f0 | 2021-09-23 00:01:57 | [diff] [blame] | 37 | #include "net/base/proxy_string_util.h" |
[email protected] | ac790b4 | 2009-12-02 04:31:31 | [diff] [blame] | 38 | #include "net/base/request_priority.h" |
Matt Menke | 4807a9a | 2020-11-21 00:14:41 | [diff] [blame] | 39 | #include "net/base/schemeful_site.h" |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 40 | #include "net/base/test_completion_callback.h" |
dalyk | edd30d98 | 2019-12-16 15:31:10 | [diff] [blame] | 41 | #include "net/dns/public/resolve_error_info.h" |
Ben Schwartz | 3ff4dc1e6 | 2021-04-27 21:15:23 | [diff] [blame] | 42 | #include "net/dns/public/secure_dns_policy.h" |
[email protected] | 277d594 | 2010-08-11 21:02:35 | [diff] [blame] | 43 | #include "net/http/http_response_headers.h" |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 44 | #include "net/http/http_response_info.h" |
eroman | 87c53d6 | 2015-04-02 06:51:07 | [diff] [blame] | 45 | #include "net/log/net_log.h" |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 46 | #include "net/log/net_log_event_type.h" |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 47 | #include "net/log/net_log_source.h" |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 48 | #include "net/log/net_log_source_type.h" |
mmenke | 16a7cbdd | 2015-04-24 23:00:56 | [diff] [blame] | 49 | #include "net/log/test_net_log.h" |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 50 | #include "net/log/test_net_log_util.h" |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 51 | #include "net/socket/client_socket_factory.h" |
| 52 | #include "net/socket/client_socket_handle.h" |
Eric Orth | a9b8be0 | 2021-06-29 23:09:08 | [diff] [blame] | 53 | #include "net/socket/connect_job_factory.h" |
tfarina | 5dd13c2 | 2016-11-16 12:08:26 | [diff] [blame] | 54 | #include "net/socket/datagram_client_socket.h" |
tbansal | ca83c00 | 2016-04-28 20:56:28 | [diff] [blame] | 55 | #include "net/socket/socket_performance_watcher.h" |
Paul Jensen | 8d6f87ec | 2018-01-13 00:46:54 | [diff] [blame] | 56 | #include "net/socket/socket_tag.h" |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 57 | #include "net/socket/socket_test_util.h" |
[email protected] | 18ccfdb | 2013-08-15 00:13:44 | [diff] [blame] | 58 | #include "net/socket/ssl_client_socket.h" |
[email protected] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame] | 59 | #include "net/socket/stream_socket.h" |
Daniel Cheng | 5feb16f | 2022-02-28 06:52:07 | [diff] [blame] | 60 | #include "net/socket/transport_client_socket_pool.h" |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 61 | #include "net/socket/transport_connect_job.h" |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 62 | #include "net/ssl/ssl_cert_request_info.h" |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 63 | #include "net/test/gtest_util.h" |
Gabriel Charette | c710874 | 2019-08-23 03:31:40 | [diff] [blame] | 64 | #include "net/test/test_with_task_environment.h" |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 65 | #include "net/traffic_annotation/network_traffic_annotation.h" |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 66 | #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 67 | #include "testing/gmock/include/gmock/gmock.h" |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 68 | #include "testing/gtest/include/gtest/gtest.h" |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 69 | #include "third_party/abseil-cpp/absl/types/optional.h" |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame] | 70 | #include "url/scheme_host_port.h" |
| 71 | #include "url/url_constants.h" |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 72 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 73 | using net::test::IsError; |
| 74 | using net::test::IsOk; |
| 75 | |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 76 | using ::testing::Invoke; |
| 77 | using ::testing::Return; |
| 78 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 79 | namespace net { |
| 80 | |
| 81 | namespace { |
| 82 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 83 | const int kDefaultMaxSockets = 4; |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 84 | const int kDefaultMaxSocketsPerGroup = 2; |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 85 | constexpr base::TimeDelta kUnusedIdleSocketTimeout = base::Seconds(10); |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 86 | |
Matt Menke | bdf77780 | 2019-04-22 19:38:59 | [diff] [blame] | 87 | ClientSocketPool::GroupId TestGroupId( |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame] | 88 | base::StringPiece host, |
Matt Menke | bdf77780 | 2019-04-22 19:38:59 | [diff] [blame] | 89 | int port = 80, |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame] | 90 | base::StringPiece scheme = url::kHttpScheme, |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 91 | PrivacyMode privacy_mode = PrivacyMode::PRIVACY_MODE_DISABLED, |
Brianna Goldstein | a7593fca | 2022-09-28 02:59:32 | [diff] [blame^] | 92 | NetworkAnonymizationKey network_anonymization_key = |
| 93 | NetworkAnonymizationKey()) { |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame] | 94 | return ClientSocketPool::GroupId(url::SchemeHostPort(scheme, host, port), |
Brianna Goldstein | a7593fca | 2022-09-28 02:59:32 | [diff] [blame^] | 95 | privacy_mode, network_anonymization_key, |
Ben Schwartz | 3ff4dc1e6 | 2021-04-27 21:15:23 | [diff] [blame] | 96 | SecureDnsPolicy::kAllow); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 97 | } |
| 98 | |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 99 | // Make sure |handle| sets load times correctly when it has been assigned a |
| 100 | // reused socket. |
| 101 | void TestLoadTimingInfoConnectedReused(const ClientSocketHandle& handle) { |
| 102 | LoadTimingInfo load_timing_info; |
| 103 | // Only pass true in as |is_reused|, as in general, HttpStream types should |
| 104 | // have stricter concepts of reuse than socket pools. |
| 105 | EXPECT_TRUE(handle.GetLoadTimingInfo(true, &load_timing_info)); |
| 106 | |
| 107 | EXPECT_EQ(true, load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 108 | EXPECT_NE(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 109 | |
[email protected] | b258e079 | 2013-01-12 07:11:59 | [diff] [blame] | 110 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 111 | ExpectLoadTimingHasOnlyConnectionTimes(load_timing_info); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | // Make sure |handle| sets load times correctly when it has been assigned a |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 115 | // fresh socket. Also runs TestLoadTimingInfoConnectedReused, since the owner |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 116 | // of a connection where |is_reused| is false may consider the connection |
| 117 | // reused. |
| 118 | void TestLoadTimingInfoConnectedNotReused(const ClientSocketHandle& handle) { |
| 119 | EXPECT_FALSE(handle.is_reused()); |
| 120 | |
| 121 | LoadTimingInfo load_timing_info; |
| 122 | EXPECT_TRUE(handle.GetLoadTimingInfo(false, &load_timing_info)); |
| 123 | |
| 124 | EXPECT_FALSE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 125 | EXPECT_NE(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 126 | |
[email protected] | b258e079 | 2013-01-12 07:11:59 | [diff] [blame] | 127 | ExpectConnectTimingHasTimes(load_timing_info.connect_timing, |
| 128 | CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY); |
| 129 | ExpectLoadTimingHasOnlyConnectionTimes(load_timing_info); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 130 | |
| 131 | TestLoadTimingInfoConnectedReused(handle); |
| 132 | } |
| 133 | |
| 134 | // Make sure |handle| sets load times correctly, in the case that it does not |
| 135 | // currently have a socket. |
| 136 | void TestLoadTimingInfoNotConnected(const ClientSocketHandle& handle) { |
| 137 | // Should only be set to true once a socket is assigned, if at all. |
| 138 | EXPECT_FALSE(handle.is_reused()); |
| 139 | |
| 140 | LoadTimingInfo load_timing_info; |
| 141 | EXPECT_FALSE(handle.GetLoadTimingInfo(false, &load_timing_info)); |
| 142 | |
| 143 | EXPECT_FALSE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 144 | EXPECT_EQ(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 145 | |
[email protected] | b258e079 | 2013-01-12 07:11:59 | [diff] [blame] | 146 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 147 | ExpectLoadTimingHasOnlyConnectionTimes(load_timing_info); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 148 | } |
| 149 | |
[email protected] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame] | 150 | class MockClientSocket : public StreamSocket { |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 151 | public: |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 152 | explicit MockClientSocket(net::NetLog* net_log) |
Tsuyoshi Horo | 2ec06e00 | 2022-06-09 01:38:59 | [diff] [blame] | 153 | : net_log_(NetLogWithSource::Make(net_log, NetLogSourceType::SOCKET)) {} |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 154 | |
Peter Boström | 407869b | 2021-10-07 04:42:48 | [diff] [blame] | 155 | MockClientSocket(const MockClientSocket&) = delete; |
| 156 | MockClientSocket& operator=(const MockClientSocket&) = delete; |
| 157 | |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 158 | // Sets whether the socket has unread data. If true, the next call to Read() |
| 159 | // will return 1 byte and IsConnectedAndIdle() will return false. |
| 160 | void set_has_unread_data(bool has_unread_data) { |
| 161 | has_unread_data_ = has_unread_data; |
| 162 | } |
| 163 | |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 164 | // Socket implementation. |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 165 | int Read(IOBuffer* /* buf */, |
| 166 | int len, |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 167 | CompletionOnceCallback /* callback */) override { |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 168 | if (has_unread_data_ && len > 0) { |
| 169 | has_unread_data_ = false; |
| 170 | was_used_to_convey_data_ = true; |
| 171 | return 1; |
| 172 | } |
[email protected] | e86df8dc | 2013-03-30 13:18:28 | [diff] [blame] | 173 | return ERR_UNEXPECTED; |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 174 | } |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 175 | |
[email protected] | a2b2cfc | 2017-12-06 09:06:08 | [diff] [blame] | 176 | int Write( |
| 177 | IOBuffer* /* buf */, |
| 178 | int len, |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 179 | CompletionOnceCallback /* callback */, |
[email protected] | a2b2cfc | 2017-12-06 09:06:08 | [diff] [blame] | 180 | const NetworkTrafficAnnotationTag& /*traffic_annotation*/) override { |
[email protected] | 0f873e8 | 2010-09-02 16:09:01 | [diff] [blame] | 181 | was_used_to_convey_data_ = true; |
| 182 | return len; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 183 | } |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 184 | int SetReceiveBufferSize(int32_t size) override { return OK; } |
| 185 | int SetSendBufferSize(int32_t size) override { return OK; } |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 186 | |
[email protected] | dbf036f | 2011-12-06 23:33:24 | [diff] [blame] | 187 | // StreamSocket implementation. |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 188 | int Connect(CompletionOnceCallback callback) override { |
[email protected] | dbf036f | 2011-12-06 23:33:24 | [diff] [blame] | 189 | connected_ = true; |
| 190 | return OK; |
| 191 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 192 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 193 | void Disconnect() override { connected_ = false; } |
| 194 | bool IsConnected() const override { return connected_; } |
| 195 | bool IsConnectedAndIdle() const override { |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 196 | return connected_ && !has_unread_data_; |
| 197 | } |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 198 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 199 | int GetPeerAddress(IPEndPoint* /* address */) const override { |
[email protected] | 9f864b3 | 2010-01-20 15:01:16 | [diff] [blame] | 200 | return ERR_UNEXPECTED; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 201 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 202 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 203 | int GetLocalAddress(IPEndPoint* /* address */) const override { |
[email protected] | e7f74da | 2011-04-19 23:49:35 | [diff] [blame] | 204 | return ERR_UNEXPECTED; |
| 205 | } |
| 206 | |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 207 | const NetLogWithSource& NetLog() const override { return net_log_; } |
[email protected] | a2006ece | 2010-04-23 16:44:02 | [diff] [blame] | 208 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 209 | bool WasEverUsed() const override { return was_used_to_convey_data_; } |
tfarina | 2846404c | 2016-12-25 14:31:37 | [diff] [blame] | 210 | bool WasAlpnNegotiated() const override { return false; } |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 211 | NextProto GetNegotiatedProtocol() const override { return kProtoUnknown; } |
| 212 | bool GetSSLInfo(SSLInfo* ssl_info) override { return false; } |
tbansal | f82cc8e | 2015-10-14 20:05:49 | [diff] [blame] | 213 | int64_t GetTotalReceivedBytes() const override { |
| 214 | NOTIMPLEMENTED(); |
| 215 | return 0; |
| 216 | } |
Paul Jensen | 0f49dec | 2017-12-12 23:39:58 | [diff] [blame] | 217 | void ApplySocketTag(const SocketTag& tag) override {} |
[email protected] | 9b5614a | 2010-08-25 20:29:45 | [diff] [blame] | 218 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 219 | private: |
Tsuyoshi Horo | 2ec06e00 | 2022-06-09 01:38:59 | [diff] [blame] | 220 | bool connected_ = false; |
| 221 | bool has_unread_data_ = false; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 222 | NetLogWithSource net_log_; |
Tsuyoshi Horo | 2ec06e00 | 2022-06-09 01:38:59 | [diff] [blame] | 223 | bool was_used_to_convey_data_ = false; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 224 | }; |
| 225 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 226 | class TestConnectJob; |
| 227 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 228 | class MockClientSocketFactory : public ClientSocketFactory { |
| 229 | public: |
Tsuyoshi Horo | 2ec06e00 | 2022-06-09 01:38:59 | [diff] [blame] | 230 | MockClientSocketFactory() = default; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 231 | |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 232 | std::unique_ptr<DatagramClientSocket> CreateDatagramClientSocket( |
[email protected] | 5370c01 | 2011-06-29 03:47:04 | [diff] [blame] | 233 | DatagramSocket::BindType bind_type, |
[email protected] | 98b0e58 | 2011-06-22 14:31:41 | [diff] [blame] | 234 | NetLog* net_log, |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 235 | const NetLogSource& source) override { |
[email protected] | 98b0e58 | 2011-06-22 14:31:41 | [diff] [blame] | 236 | NOTREACHED(); |
David Benjamin | 24725be | 2019-07-24 20:57:18 | [diff] [blame] | 237 | return nullptr; |
[email protected] | 98b0e58 | 2011-06-22 14:31:41 | [diff] [blame] | 238 | } |
| 239 | |
Helen Li | d5bb922 | 2018-04-12 15:33:09 | [diff] [blame] | 240 | std::unique_ptr<TransportClientSocket> CreateTransportClientSocket( |
[email protected] | 0a0b768 | 2010-08-25 17:08:07 | [diff] [blame] | 241 | const AddressList& addresses, |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 242 | std::unique_ptr< |
| 243 | SocketPerformanceWatcher> /* socket_performance_watcher */, |
Eric Roman | 2bc7716 | 2020-09-16 18:30:45 | [diff] [blame] | 244 | NetworkQualityEstimator* /* network_quality_estimator */, |
[email protected] | 0a0b768 | 2010-08-25 17:08:07 | [diff] [blame] | 245 | NetLog* /* net_log */, |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 246 | const NetLogSource& /*source*/) override { |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 247 | allocation_count_++; |
Helen Li | d5bb922 | 2018-04-12 15:33:09 | [diff] [blame] | 248 | return nullptr; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 249 | } |
| 250 | |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 251 | std::unique_ptr<SSLClientSocket> CreateSSLClientSocket( |
David Benjamin | 24725be | 2019-07-24 20:57:18 | [diff] [blame] | 252 | SSLClientContext* context, |
Matt Menke | 841fc41 | 2019-03-05 23:20:12 | [diff] [blame] | 253 | std::unique_ptr<StreamSocket> stream_socket, |
[email protected] | 4f4de7e6 | 2010-11-12 19:55:27 | [diff] [blame] | 254 | const HostPortPair& host_and_port, |
David Benjamin | 24725be | 2019-07-24 20:57:18 | [diff] [blame] | 255 | const SSLConfig& ssl_config) override { |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 256 | NOTIMPLEMENTED(); |
David Benjamin | 24725be | 2019-07-24 20:57:18 | [diff] [blame] | 257 | return nullptr; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 258 | } |
Matt Menke | fd95692 | 2019-02-04 23:44:03 | [diff] [blame] | 259 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 260 | void WaitForSignal(TestConnectJob* job) { waiting_jobs_.push_back(job); } |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 261 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 262 | void SignalJobs(); |
| 263 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 264 | void SignalJob(size_t job); |
| 265 | |
| 266 | void SetJobLoadState(size_t job, LoadState load_state); |
| 267 | |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 268 | // Sets the HasConnectionEstablished value of the specified job to true, |
| 269 | // without invoking the callback. |
| 270 | void SetJobHasEstablishedConnection(size_t job); |
| 271 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 272 | int allocation_count() const { return allocation_count_; } |
| 273 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 274 | private: |
Tsuyoshi Horo | 2ec06e00 | 2022-06-09 01:38:59 | [diff] [blame] | 275 | int allocation_count_ = 0; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 276 | std::vector<TestConnectJob*> waiting_jobs_; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 277 | }; |
| 278 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 279 | class TestConnectJob : public ConnectJob { |
| 280 | public: |
| 281 | enum JobType { |
| 282 | kMockJob, |
| 283 | kMockFailingJob, |
| 284 | kMockPendingJob, |
| 285 | kMockPendingFailingJob, |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 286 | kMockWaitingJob, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 287 | |
| 288 | // Certificate errors return a socket in addition to an error code. |
| 289 | kMockCertErrorJob, |
| 290 | kMockPendingCertErrorJob, |
| 291 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 292 | kMockAdditionalErrorStateJob, |
| 293 | kMockPendingAdditionalErrorStateJob, |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 294 | kMockUnreadDataJob, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 295 | |
| 296 | kMockAuthChallengeOnceJob, |
| 297 | kMockAuthChallengeTwiceJob, |
| 298 | kMockAuthChallengeOnceFailingJob, |
| 299 | kMockAuthChallengeTwiceFailingJob, |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 300 | }; |
| 301 | |
[email protected] | 994d493 | 2010-07-12 17:55:13 | [diff] [blame] | 302 | // The kMockPendingJob uses a slight delay before allowing the connect |
| 303 | // to complete. |
| 304 | static const int kPendingConnectDelay = 2; |
| 305 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 306 | TestConnectJob(JobType job_type, |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 307 | RequestPriority request_priority, |
| 308 | SocketTag socket_tag, |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 309 | base::TimeDelta timeout_duration, |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 310 | const CommonConnectJobParams* common_connect_job_params, |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 311 | ConnectJob::Delegate* delegate, |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 312 | MockClientSocketFactory* client_socket_factory) |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 313 | : ConnectJob(request_priority, |
| 314 | socket_tag, |
Matt Menke | 1a6c92d | 2019-02-23 00:25:38 | [diff] [blame] | 315 | timeout_duration, |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 316 | common_connect_job_params, |
Matt Menke | 1a6c92d | 2019-02-23 00:25:38 | [diff] [blame] | 317 | delegate, |
| 318 | nullptr /* net_log */, |
| 319 | NetLogSourceType::TRANSPORT_CONNECT_JOB, |
| 320 | NetLogEventType::TRANSPORT_CONNECT_JOB_CONNECT), |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 321 | job_type_(job_type), |
Tsuyoshi Horo | 2ec06e00 | 2022-06-09 01:38:59 | [diff] [blame] | 322 | client_socket_factory_(client_socket_factory) {} |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 323 | |
Peter Boström | 407869b | 2021-10-07 04:42:48 | [diff] [blame] | 324 | TestConnectJob(const TestConnectJob&) = delete; |
| 325 | TestConnectJob& operator=(const TestConnectJob&) = delete; |
| 326 | |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 327 | void Signal() { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 328 | DoConnect(waiting_success_, true /* async */, false /* recoverable */); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 329 | } |
| 330 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 331 | void set_load_state(LoadState load_state) { load_state_ = load_state; } |
| 332 | |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 333 | void set_has_established_connection() { |
| 334 | DCHECK(!has_established_connection_); |
| 335 | has_established_connection_ = true; |
| 336 | } |
| 337 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 338 | // From ConnectJob: |
| 339 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 340 | LoadState GetLoadState() const override { return load_state_; } |
[email protected] | 4645135 | 2009-09-01 14:54:21 | [diff] [blame] | 341 | |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 342 | bool HasEstablishedConnection() const override { |
| 343 | return has_established_connection_; |
| 344 | } |
| 345 | |
dalyk | edd30d98 | 2019-12-16 15:31:10 | [diff] [blame] | 346 | ResolveErrorInfo GetResolveErrorInfo() const override { |
| 347 | return ResolveErrorInfo(OK); |
| 348 | } |
| 349 | |
Matt Menke | 6f84d1f1 | 2019-04-11 19:26:47 | [diff] [blame] | 350 | bool IsSSLError() const override { return store_additional_error_state_; } |
| 351 | |
| 352 | scoped_refptr<SSLCertRequestInfo> GetCertRequestInfo() override { |
| 353 | if (store_additional_error_state_) |
| 354 | return base::MakeRefCounted<SSLCertRequestInfo>(); |
| 355 | return nullptr; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 356 | } |
| 357 | |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 358 | private: |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 359 | // From ConnectJob: |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 360 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 361 | int ConnectInternal() override { |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 362 | AddressList ignored; |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 363 | client_socket_factory_->CreateTransportClientSocket( |
Eric Roman | 2bc7716 | 2020-09-16 18:30:45 | [diff] [blame] | 364 | ignored, nullptr, nullptr, nullptr, NetLogSource()); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 365 | switch (job_type_) { |
| 366 | case kMockJob: |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 367 | return DoConnect(true /* successful */, false /* sync */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 368 | false /* cert_error */); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 369 | case kMockFailingJob: |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 370 | return DoConnect(false /* error */, false /* sync */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 371 | false /* cert_error */); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 372 | case kMockPendingJob: |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 373 | set_load_state(LOAD_STATE_CONNECTING); |
[email protected] | 6b17538 | 2009-10-13 06:47:47 | [diff] [blame] | 374 | |
| 375 | // Depending on execution timings, posting a delayed task can result |
| 376 | // in the task getting executed the at the earliest possible |
| 377 | // opportunity or only after returning once from the message loop and |
| 378 | // then a second call into the message loop. In order to make behavior |
| 379 | // more deterministic, we change the default delay to 2ms. This should |
| 380 | // always require us to wait for the second call into the message loop. |
| 381 | // |
| 382 | // N.B. The correct fix for this and similar timing problems is to |
| 383 | // abstract time for the purpose of unittests. Unfortunately, we have |
| 384 | // a lot of third-party components that directly call the various |
| 385 | // time functions, so this change would be rather invasive. |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 386 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 387 | FROM_HERE, |
kylechar | f4fe517 | 2019-02-15 18:53:49 | [diff] [blame] | 388 | base::BindOnce(base::IgnoreResult(&TestConnectJob::DoConnect), |
| 389 | weak_factory_.GetWeakPtr(), true /* successful */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 390 | true /* async */, false /* cert_error */), |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 391 | base::Milliseconds(kPendingConnectDelay)); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 392 | return ERR_IO_PENDING; |
| 393 | case kMockPendingFailingJob: |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 394 | set_load_state(LOAD_STATE_CONNECTING); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 395 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 396 | FROM_HERE, |
kylechar | f4fe517 | 2019-02-15 18:53:49 | [diff] [blame] | 397 | base::BindOnce(base::IgnoreResult(&TestConnectJob::DoConnect), |
| 398 | weak_factory_.GetWeakPtr(), false /* error */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 399 | true /* async */, false /* cert_error */), |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 400 | base::Milliseconds(2)); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 401 | return ERR_IO_PENDING; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 402 | case kMockWaitingJob: |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 403 | set_load_state(LOAD_STATE_CONNECTING); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 404 | client_socket_factory_->WaitForSignal(this); |
| 405 | waiting_success_ = true; |
| 406 | return ERR_IO_PENDING; |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 407 | case kMockCertErrorJob: |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 408 | return DoConnect(false /* error */, false /* sync */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 409 | true /* cert_error */); |
| 410 | case kMockPendingCertErrorJob: |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 411 | set_load_state(LOAD_STATE_CONNECTING); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 412 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 413 | FROM_HERE, |
kylechar | f4fe517 | 2019-02-15 18:53:49 | [diff] [blame] | 414 | base::BindOnce(base::IgnoreResult(&TestConnectJob::DoConnect), |
| 415 | weak_factory_.GetWeakPtr(), false /* error */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 416 | true /* async */, true /* cert_error */), |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 417 | base::Milliseconds(2)); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 418 | return ERR_IO_PENDING; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 419 | case kMockAdditionalErrorStateJob: |
| 420 | store_additional_error_state_ = true; |
| 421 | return DoConnect(false /* error */, false /* sync */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 422 | false /* cert_error */); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 423 | case kMockPendingAdditionalErrorStateJob: |
| 424 | set_load_state(LOAD_STATE_CONNECTING); |
| 425 | store_additional_error_state_ = true; |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 426 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 427 | FROM_HERE, |
kylechar | f4fe517 | 2019-02-15 18:53:49 | [diff] [blame] | 428 | base::BindOnce(base::IgnoreResult(&TestConnectJob::DoConnect), |
| 429 | weak_factory_.GetWeakPtr(), false /* error */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 430 | true /* async */, false /* cert_error */), |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 431 | base::Milliseconds(2)); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 432 | return ERR_IO_PENDING; |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 433 | case kMockUnreadDataJob: { |
| 434 | int ret = DoConnect(true /* successful */, false /* sync */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 435 | false /* cert_error */); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 436 | static_cast<MockClientSocket*>(socket())->set_has_unread_data(true); |
| 437 | return ret; |
| 438 | } |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 439 | case kMockAuthChallengeOnceJob: |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 440 | set_load_state(LOAD_STATE_CONNECTING); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 441 | DoAdvanceAuthChallenge(1, true /* succeed_after_last_challenge */); |
| 442 | return ERR_IO_PENDING; |
| 443 | case kMockAuthChallengeTwiceJob: |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 444 | set_load_state(LOAD_STATE_CONNECTING); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 445 | DoAdvanceAuthChallenge(2, true /* succeed_after_last_challenge */); |
| 446 | return ERR_IO_PENDING; |
| 447 | case kMockAuthChallengeOnceFailingJob: |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 448 | set_load_state(LOAD_STATE_CONNECTING); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 449 | DoAdvanceAuthChallenge(1, false /* succeed_after_last_challenge */); |
| 450 | return ERR_IO_PENDING; |
| 451 | case kMockAuthChallengeTwiceFailingJob: |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 452 | set_load_state(LOAD_STATE_CONNECTING); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 453 | DoAdvanceAuthChallenge(2, false /* succeed_after_last_challenge */); |
| 454 | return ERR_IO_PENDING; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 455 | default: |
| 456 | NOTREACHED(); |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 457 | SetSocket(std::unique_ptr<StreamSocket>(), absl::nullopt); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 458 | return ERR_FAILED; |
| 459 | } |
| 460 | } |
| 461 | |
Lily Chen | 02ef29a | 2018-11-30 16:31:43 | [diff] [blame] | 462 | void ChangePriorityInternal(RequestPriority priority) override {} |
| 463 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 464 | int DoConnect(bool succeed, bool was_async, bool cert_error) { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 465 | int result = OK; |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 466 | has_established_connection_ = true; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 467 | if (succeed) { |
Cammie Smith Barnes | aa2a8b5 | 2020-12-17 19:33:19 | [diff] [blame] | 468 | SetSocket(std::make_unique<MockClientSocket>(net_log().net_log()), |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 469 | absl::nullopt); |
Bence Béky | bdbb0e7 | 2018-08-07 21:42:59 | [diff] [blame] | 470 | socket()->Connect(CompletionOnceCallback()); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 471 | } else if (cert_error) { |
Cammie Smith Barnes | aa2a8b5 | 2020-12-17 19:33:19 | [diff] [blame] | 472 | SetSocket(std::make_unique<MockClientSocket>(net_log().net_log()), |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 473 | absl::nullopt); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 474 | result = ERR_CERT_COMMON_NAME_INVALID; |
[email protected] | 6e713f0 | 2009-08-06 02:56:40 | [diff] [blame] | 475 | } else { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 476 | result = ERR_CONNECTION_FAILED; |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 477 | SetSocket(std::unique_ptr<StreamSocket>(), absl::nullopt); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 478 | } |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 479 | |
| 480 | if (was_async) |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 481 | NotifyDelegateOfCompletion(result); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 482 | return result; |
| 483 | } |
| 484 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 485 | void DoAdvanceAuthChallenge(int remaining_challenges, |
| 486 | bool succeed_after_last_challenge) { |
| 487 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 488 | FROM_HERE, |
| 489 | base::BindOnce(&TestConnectJob::InvokeNextProxyAuthCallback, |
| 490 | weak_factory_.GetWeakPtr(), remaining_challenges, |
| 491 | succeed_after_last_challenge)); |
| 492 | } |
| 493 | |
| 494 | void InvokeNextProxyAuthCallback(int remaining_challenges, |
| 495 | bool succeed_after_last_challenge) { |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 496 | set_load_state(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 497 | if (remaining_challenges == 0) { |
| 498 | DoConnect(succeed_after_last_challenge, true /* was_async */, |
| 499 | false /* cert_error */); |
| 500 | return; |
| 501 | } |
| 502 | |
| 503 | // Integration tests make sure HttpResponseInfo and HttpAuthController work. |
| 504 | // The auth tests here are just focused on ConnectJob bookkeeping. |
| 505 | HttpResponseInfo info; |
| 506 | NotifyDelegateOfProxyAuth( |
| 507 | info, nullptr /* http_auth_controller */, |
| 508 | base::BindOnce(&TestConnectJob::DoAdvanceAuthChallenge, |
| 509 | weak_factory_.GetWeakPtr(), remaining_challenges - 1, |
| 510 | succeed_after_last_challenge)); |
| 511 | } |
| 512 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 513 | bool waiting_success_; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 514 | const JobType job_type_; |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 515 | const raw_ptr<MockClientSocketFactory> client_socket_factory_; |
Tsuyoshi Horo | 2ec06e00 | 2022-06-09 01:38:59 | [diff] [blame] | 516 | LoadState load_state_ = LOAD_STATE_IDLE; |
| 517 | bool has_established_connection_ = false; |
| 518 | bool store_additional_error_state_ = false; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 519 | |
Jeremy Roman | d54000b2 | 2019-07-08 18:40:16 | [diff] [blame] | 520 | base::WeakPtrFactory<TestConnectJob> weak_factory_{this}; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 521 | }; |
| 522 | |
Eric Orth | a9b8be0 | 2021-06-29 23:09:08 | [diff] [blame] | 523 | class TestConnectJobFactory : public ConnectJobFactory { |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 524 | public: |
Eric Orth | a9b8be0 | 2021-06-29 23:09:08 | [diff] [blame] | 525 | explicit TestConnectJobFactory(MockClientSocketFactory* client_socket_factory) |
| 526 | : client_socket_factory_(client_socket_factory) {} |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 527 | |
Peter Boström | 293b134 | 2021-09-22 17:31:43 | [diff] [blame] | 528 | TestConnectJobFactory(const TestConnectJobFactory&) = delete; |
| 529 | TestConnectJobFactory& operator=(const TestConnectJobFactory&) = delete; |
| 530 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 531 | ~TestConnectJobFactory() override = default; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 532 | |
| 533 | void set_job_type(TestConnectJob::JobType job_type) { job_type_ = job_type; } |
| 534 | |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 535 | void set_job_types(std::list<TestConnectJob::JobType>* job_types) { |
| 536 | job_types_ = job_types; |
| 537 | CHECK(!job_types_->empty()); |
| 538 | } |
| 539 | |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 540 | void set_timeout_duration(base::TimeDelta timeout_duration) { |
| 541 | timeout_duration_ = timeout_duration; |
| 542 | } |
| 543 | |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 544 | // ConnectJobFactory implementation. |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 545 | |
Eric Orth | a9b8be0 | 2021-06-29 23:09:08 | [diff] [blame] | 546 | std::unique_ptr<ConnectJob> CreateConnectJob( |
Eric Orth | c98a3e6 | 2021-07-02 17:46:37 | [diff] [blame] | 547 | Endpoint endpoint, |
Eric Orth | a9b8be0 | 2021-06-29 23:09:08 | [diff] [blame] | 548 | const ProxyServer& proxy_server, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 549 | const absl::optional<NetworkTrafficAnnotationTag>& proxy_annotation_tag, |
Eric Orth | a9b8be0 | 2021-06-29 23:09:08 | [diff] [blame] | 550 | const SSLConfig* ssl_config_for_origin, |
| 551 | const SSLConfig* ssl_config_for_proxy, |
| 552 | bool force_tunnel, |
| 553 | PrivacyMode privacy_mode, |
| 554 | const OnHostResolutionCallback& resolution_callback, |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 555 | RequestPriority request_priority, |
| 556 | SocketTag socket_tag, |
Eric Orth | a9b8be0 | 2021-06-29 23:09:08 | [diff] [blame] | 557 | const NetworkIsolationKey& network_isolation_key, |
| 558 | SecureDnsPolicy secure_dns_policy, |
| 559 | const CommonConnectJobParams* common_connect_job_params, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 560 | ConnectJob::Delegate* delegate) const override { |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 561 | EXPECT_TRUE(!job_types_ || !job_types_->empty()); |
| 562 | TestConnectJob::JobType job_type = job_type_; |
| 563 | if (job_types_ && !job_types_->empty()) { |
| 564 | job_type = job_types_->front(); |
| 565 | job_types_->pop_front(); |
| 566 | } |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 567 | return std::make_unique<TestConnectJob>( |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 568 | job_type, request_priority, socket_tag, timeout_duration_, |
Eric Orth | a9b8be0 | 2021-06-29 23:09:08 | [diff] [blame] | 569 | common_connect_job_params, delegate, client_socket_factory_); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | private: |
Eric Orth | a9b8be0 | 2021-06-29 23:09:08 | [diff] [blame] | 573 | TestConnectJob::JobType job_type_ = TestConnectJob::kMockJob; |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 574 | raw_ptr<std::list<TestConnectJob::JobType>> job_types_ = nullptr; |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 575 | base::TimeDelta timeout_duration_; |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 576 | const raw_ptr<MockClientSocketFactory> client_socket_factory_; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 577 | }; |
| 578 | |
[email protected] | a937a06d | 2009-08-19 21:19:24 | [diff] [blame] | 579 | } // namespace |
| 580 | |
[email protected] | a937a06d | 2009-08-19 21:19:24 | [diff] [blame] | 581 | namespace { |
| 582 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 583 | void MockClientSocketFactory::SignalJobs() { |
Tsuyoshi Horo | 17ef47d0 | 2022-06-30 10:58:27 | [diff] [blame] | 584 | for (auto* waiting_job : waiting_jobs_) { |
| 585 | waiting_job->Signal(); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 586 | } |
| 587 | waiting_jobs_.clear(); |
| 588 | } |
| 589 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 590 | void MockClientSocketFactory::SignalJob(size_t job) { |
| 591 | ASSERT_LT(job, waiting_jobs_.size()); |
| 592 | waiting_jobs_[job]->Signal(); |
| 593 | waiting_jobs_.erase(waiting_jobs_.begin() + job); |
| 594 | } |
| 595 | |
| 596 | void MockClientSocketFactory::SetJobLoadState(size_t job, |
| 597 | LoadState load_state) { |
| 598 | ASSERT_LT(job, waiting_jobs_.size()); |
| 599 | waiting_jobs_[job]->set_load_state(load_state); |
| 600 | } |
| 601 | |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 602 | void MockClientSocketFactory::SetJobHasEstablishedConnection(size_t job) { |
| 603 | ASSERT_LT(job, waiting_jobs_.size()); |
| 604 | waiting_jobs_[job]->set_has_established_connection(); |
| 605 | } |
| 606 | |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 607 | class ClientSocketPoolBaseTest : public TestWithTaskEnvironment { |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 608 | protected: |
Alex Clarke | 0def209 | 2018-12-10 12:01:45 | [diff] [blame] | 609 | ClientSocketPoolBaseTest() |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 610 | : TestWithTaskEnvironment( |
| 611 | base::test::TaskEnvironment::TimeSource::MOCK_TIME), |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 612 | params_(ClientSocketPool::SocketParams::CreateForHttpForTesting()) { |
[email protected] | 636b825 | 2011-04-08 19:56:54 | [diff] [blame] | 613 | connect_backup_jobs_enabled_ = |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 614 | TransportClientSocketPool::connect_backup_jobs_enabled(); |
| 615 | TransportClientSocketPool::set_connect_backup_jobs_enabled(true); |
[email protected] | 636b825 | 2011-04-08 19:56:54 | [diff] [blame] | 616 | } |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 617 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 618 | ~ClientSocketPoolBaseTest() override { |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 619 | TransportClientSocketPool::set_connect_backup_jobs_enabled( |
[email protected] | 636b825 | 2011-04-08 19:56:54 | [diff] [blame] | 620 | connect_backup_jobs_enabled_); |
| 621 | } |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 622 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 623 | void CreatePool(int max_sockets, |
| 624 | int max_sockets_per_group, |
| 625 | bool enable_backup_connect_jobs = false) { |
Tarun Bansal | a763509 | 2019-02-20 10:00:59 | [diff] [blame] | 626 | CreatePoolWithIdleTimeouts(max_sockets, max_sockets_per_group, |
| 627 | kUnusedIdleSocketTimeout, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 628 | ClientSocketPool::used_idle_socket_timeout(), |
| 629 | enable_backup_connect_jobs); |
[email protected] | 9bf28db | 2009-08-29 01:35:16 | [diff] [blame] | 630 | } |
| 631 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 632 | void CreatePoolWithIdleTimeouts( |
| 633 | int max_sockets, |
| 634 | int max_sockets_per_group, |
| 635 | base::TimeDelta unused_idle_socket_timeout, |
| 636 | base::TimeDelta used_idle_socket_timeout, |
| 637 | bool enable_backup_connect_jobs = false, |
| 638 | ProxyServer proxy_server = ProxyServer::Direct()) { |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 639 | DCHECK(!pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 640 | std::unique_ptr<TestConnectJobFactory> connect_job_factory = |
Eric Orth | a9b8be0 | 2021-06-29 23:09:08 | [diff] [blame] | 641 | std::make_unique<TestConnectJobFactory>(&client_socket_factory_); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 642 | connect_job_factory_ = connect_job_factory.get(); |
| 643 | pool_ = TransportClientSocketPool::CreateForTesting( |
| 644 | max_sockets, max_sockets_per_group, unused_idle_socket_timeout, |
Eric Orth | a9b8be0 | 2021-06-29 23:09:08 | [diff] [blame] | 645 | used_idle_socket_timeout, proxy_server, /*is_for_websockets=*/false, |
| 646 | &common_connect_job_params_, std::move(connect_job_factory), |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 647 | nullptr /* ssl_config_service */, enable_backup_connect_jobs); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 648 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 649 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 650 | int StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 651 | const ClientSocketPool::GroupId& group_id, |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 652 | RequestPriority priority, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 653 | ClientSocketPool::RespectLimits respect_limits) { |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 654 | return test_base_.StartRequestUsingPool(pool_.get(), group_id, priority, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 655 | respect_limits, params_); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 656 | } |
| 657 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 658 | int StartRequest(const ClientSocketPool::GroupId& group_id, |
| 659 | RequestPriority priority) { |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 660 | return StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 661 | group_id, priority, ClientSocketPool::RespectLimits::ENABLED); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 662 | } |
| 663 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 664 | int GetOrderOfRequest(size_t index) const { |
| 665 | return test_base_.GetOrderOfRequest(index); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 666 | } |
| 667 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 668 | bool ReleaseOneConnection(ClientSocketPoolTest::KeepAlive keep_alive) { |
| 669 | return test_base_.ReleaseOneConnection(keep_alive); |
| 670 | } |
| 671 | |
| 672 | void ReleaseAllConnections(ClientSocketPoolTest::KeepAlive keep_alive) { |
| 673 | test_base_.ReleaseAllConnections(keep_alive); |
| 674 | } |
| 675 | |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 676 | // Expects a single NetLogEventType::SOCKET_POOL_CLOSING_SOCKET in |net_log_|. |
| 677 | // It should be logged for the provided source and have the indicated reason. |
| 678 | void ExpectSocketClosedWithReason(NetLogSource expected_source, |
| 679 | const char* expected_reason) { |
Matt Reichhoff | 0049a0b7 | 2021-10-20 20:44:26 | [diff] [blame] | 680 | auto entries = net_log_observer_.GetEntriesForSourceWithType( |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 681 | expected_source, NetLogEventType::SOCKET_POOL_CLOSING_SOCKET, |
| 682 | NetLogEventPhase::NONE); |
| 683 | ASSERT_EQ(1u, entries.size()); |
| 684 | ASSERT_TRUE(entries[0].HasParams()); |
| 685 | ASSERT_TRUE(entries[0].params.is_dict()); |
Matt Menke | ca721da | 2022-06-01 04:47:29 | [diff] [blame] | 686 | const std::string* reason = |
| 687 | entries[0].params.GetDict().FindString("reason"); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 688 | ASSERT_TRUE(reason); |
| 689 | EXPECT_EQ(expected_reason, *reason); |
| 690 | } |
| 691 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 692 | TestSocketRequest* request(int i) { return test_base_.request(i); } |
| 693 | size_t requests_size() const { return test_base_.requests_size(); } |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 694 | std::vector<std::unique_ptr<TestSocketRequest>>* requests() { |
olli.raula | 9d66b7d | 2015-11-23 08:30:42 | [diff] [blame] | 695 | return test_base_.requests(); |
| 696 | } |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 697 | // Only counts the requests that get sockets asynchronously; |
| 698 | // synchronous completions are not registered by this count. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 699 | size_t completion_count() const { return test_base_.completion_count(); } |
| 700 | |
Eric Orth | a9b8be0 | 2021-06-29 23:09:08 | [diff] [blame] | 701 | const CommonConnectJobParams common_connect_job_params_{ |
| 702 | nullptr /* client_socket_factory */, |
| 703 | nullptr /* host_resolver */, |
| 704 | nullptr /* http_auth_cache */, |
| 705 | nullptr /* http_auth_handler_factory */, |
| 706 | nullptr /* spdy_session_pool */, |
| 707 | nullptr /* quic_supported_versions */, |
| 708 | nullptr /* quic_stream_factory */, |
| 709 | nullptr /* proxy_delegate */, |
| 710 | nullptr /* http_user_agent_settings */, |
| 711 | nullptr /* ssl_client_context */, |
| 712 | nullptr /* socket_performance_watcher_factory */, |
| 713 | nullptr /* network_quality_estimator */, |
Matt Reichhoff | 36a1fd6 | 2021-10-12 22:52:20 | [diff] [blame] | 714 | NetLog::Get(), |
Eric Orth | a9b8be0 | 2021-06-29 23:09:08 | [diff] [blame] | 715 | nullptr /* websocket_endpoint_lock_manager */}; |
[email protected] | 636b825 | 2011-04-08 19:56:54 | [diff] [blame] | 716 | bool connect_backup_jobs_enabled_; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 717 | MockClientSocketFactory client_socket_factory_; |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 718 | raw_ptr<TestConnectJobFactory> connect_job_factory_; |
Matt Reichhoff | 0049a0b7 | 2021-10-20 20:44:26 | [diff] [blame] | 719 | RecordingNetLogObserver net_log_observer_; |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 720 | // These parameters are never actually used to create a TransportConnectJob. |
Matt Menke | 84d11e56 | 2019-03-27 00:11:19 | [diff] [blame] | 721 | scoped_refptr<ClientSocketPool::SocketParams> params_; |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 722 | std::unique_ptr<TransportClientSocketPool> pool_; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 723 | ClientSocketPoolTest test_base_; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 724 | }; |
| 725 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 726 | TEST_F(ClientSocketPoolBaseTest, BasicSynchronous) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 727 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 728 | |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 729 | TestCompletionCallback callback; |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 730 | ClientSocketHandle handle; |
Matt Reichhoff | 0049a0b7 | 2021-10-20 20:44:26 | [diff] [blame] | 731 | NetLogWithSource net_log_with_source = |
| 732 | NetLogWithSource::Make(NetLogSourceType::NONE); |
| 733 | |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 734 | TestLoadTimingInfoNotConnected(handle); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 735 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 736 | EXPECT_EQ(OK, handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 737 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 738 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 739 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
Matt Reichhoff | 0049a0b7 | 2021-10-20 20:44:26 | [diff] [blame] | 740 | pool_.get(), net_log_with_source)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 741 | EXPECT_TRUE(handle.is_initialized()); |
| 742 | EXPECT_TRUE(handle.socket()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 743 | TestLoadTimingInfoConnectedNotReused(handle); |
| 744 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 745 | handle.Reset(); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 746 | TestLoadTimingInfoNotConnected(handle); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 747 | |
Matt Reichhoff | 0049a0b7 | 2021-10-20 20:44:26 | [diff] [blame] | 748 | auto entries = |
| 749 | net_log_observer_.GetEntriesForSource(net_log_with_source.source()); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 750 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 751 | EXPECT_EQ(5u, entries.size()); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 752 | EXPECT_TRUE(LogContainsEvent( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 753 | entries, 0, NetLogEventType::TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 754 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 755 | EXPECT_TRUE(LogContainsBeginEvent(entries, 1, NetLogEventType::SOCKET_POOL)); |
| 756 | EXPECT_TRUE(LogContainsEvent( |
| 757 | entries, 2, NetLogEventType::SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
| 758 | NetLogEventPhase::NONE)); |
| 759 | EXPECT_TRUE(LogContainsEvent(entries, 3, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 760 | NetLogEventType::SOCKET_POOL_BOUND_TO_SOCKET, |
| 761 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 762 | EXPECT_TRUE(LogContainsEndEvent(entries, 4, NetLogEventType::SOCKET_POOL)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 763 | } |
| 764 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 765 | TEST_F(ClientSocketPoolBaseTest, InitConnectionFailure) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 766 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 767 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 768 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
Matt Reichhoff | 0049a0b7 | 2021-10-20 20:44:26 | [diff] [blame] | 769 | NetLogWithSource net_log_with_source = |
| 770 | NetLogWithSource::Make(NetLogSourceType::NONE); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 771 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 772 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 773 | TestCompletionCallback callback; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 774 | // Set the additional error state members to ensure that they get cleared. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 775 | handle.set_is_ssl_error(true); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 776 | handle.set_ssl_cert_request_info(base::MakeRefCounted<SSLCertRequestInfo>()); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 777 | EXPECT_EQ( |
| 778 | ERR_CONNECTION_FAILED, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 779 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 780 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 781 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
Matt Reichhoff | 0049a0b7 | 2021-10-20 20:44:26 | [diff] [blame] | 782 | pool_.get(), net_log_with_source)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 783 | EXPECT_FALSE(handle.socket()); |
| 784 | EXPECT_FALSE(handle.is_ssl_error()); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 785 | EXPECT_FALSE(handle.ssl_cert_request_info()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 786 | TestLoadTimingInfoNotConnected(handle); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 787 | |
Matt Reichhoff | 0049a0b7 | 2021-10-20 20:44:26 | [diff] [blame] | 788 | auto entries = |
| 789 | net_log_observer_.GetEntriesForSource(net_log_with_source.source()); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 790 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 791 | EXPECT_EQ(4u, entries.size()); |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 792 | EXPECT_TRUE(LogContainsEvent( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 793 | entries, 0, NetLogEventType::TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 794 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 795 | EXPECT_TRUE(LogContainsBeginEvent(entries, 1, NetLogEventType::SOCKET_POOL)); |
| 796 | EXPECT_TRUE(LogContainsEvent( |
| 797 | entries, 2, NetLogEventType::SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
| 798 | NetLogEventPhase::NONE)); |
| 799 | EXPECT_TRUE(LogContainsEndEvent(entries, 3, NetLogEventType::SOCKET_POOL)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 800 | } |
| 801 | |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 802 | // Test releasing an open socket into the socket pool, telling the socket pool |
| 803 | // to close the socket. |
| 804 | TEST_F(ClientSocketPoolBaseTest, ReleaseAndCloseConnection) { |
| 805 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 806 | |
| 807 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 808 | ASSERT_TRUE(request(0)->handle()->socket()); |
| 809 | net::NetLogSource source = request(0)->handle()->socket()->NetLog().source(); |
| 810 | ReleaseOneConnection(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 811 | |
| 812 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 813 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 814 | |
| 815 | ExpectSocketClosedWithReason( |
| 816 | source, TransportClientSocketPool::kClosedConnectionReturnedToPool); |
| 817 | } |
| 818 | |
| 819 | TEST_F(ClientSocketPoolBaseTest, SocketWithUnreadDataReturnedToPool) { |
| 820 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 821 | connect_job_factory_->set_job_type(TestConnectJob::kMockUnreadDataJob); |
| 822 | |
| 823 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 824 | ASSERT_TRUE(request(0)->handle()->socket()); |
| 825 | net::NetLogSource source = request(0)->handle()->socket()->NetLog().source(); |
| 826 | EXPECT_TRUE(request(0)->handle()->socket()->IsConnected()); |
| 827 | EXPECT_FALSE(request(0)->handle()->socket()->IsConnectedAndIdle()); |
| 828 | ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE); |
| 829 | |
| 830 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 831 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 832 | |
| 833 | ExpectSocketClosedWithReason( |
| 834 | source, TransportClientSocketPool::kDataReceivedUnexpectedly); |
| 835 | } |
| 836 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 837 | // Make sure different groups do not share sockets. |
| 838 | TEST_F(ClientSocketPoolBaseTest, GroupSeparation) { |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 839 | base::test::ScopedFeatureList feature_list; |
| 840 | feature_list.InitAndEnableFeature( |
| 841 | features::kPartitionConnectionsByNetworkIsolationKey); |
| 842 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 843 | CreatePool(1000 /* max_sockets */, 2 /* max_sockets_per_group */); |
| 844 | |
| 845 | const HostPortPair kHostPortPairs[] = { |
| 846 | {"a", 80}, |
| 847 | {"a", 443}, |
| 848 | {"b", 80}, |
| 849 | }; |
| 850 | |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame] | 851 | const char* const kSchemes[] = { |
| 852 | url::kHttpScheme, |
| 853 | url::kHttpsScheme, |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 854 | }; |
| 855 | |
Matt Menke | bdf77780 | 2019-04-22 19:38:59 | [diff] [blame] | 856 | const PrivacyMode kPrivacyModes[] = {PrivacyMode::PRIVACY_MODE_DISABLED, |
| 857 | PrivacyMode::PRIVACY_MODE_ENABLED}; |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 858 | |
Matt Menke | 4807a9a | 2020-11-21 00:14:41 | [diff] [blame] | 859 | const SchemefulSite kSiteA(GURL("http://a.test/")); |
| 860 | const SchemefulSite kSiteB(GURL("http://b.test/")); |
Brianna Goldstein | a7593fca | 2022-09-28 02:59:32 | [diff] [blame^] | 861 | const NetworkAnonymizationKey kNetworkAnonymizationKeys[] = { |
| 862 | NetworkAnonymizationKey(kSiteA, kSiteA, /*is_cross_site=*/false), |
| 863 | NetworkAnonymizationKey(kSiteB, kSiteB, /*is_cross_site=*/false), |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 864 | }; |
| 865 | |
Ben Schwartz | 3ff4dc1e6 | 2021-04-27 21:15:23 | [diff] [blame] | 866 | const SecureDnsPolicy kSecureDnsPolicys[] = {SecureDnsPolicy::kAllow, |
| 867 | SecureDnsPolicy::kDisable}; |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 868 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 869 | int total_idle_sockets = 0; |
| 870 | |
| 871 | // Walk through each GroupId, making sure that requesting a socket for one |
| 872 | // group does not return a previously connected socket for another group. |
| 873 | for (const auto& host_port_pair : kHostPortPairs) { |
| 874 | SCOPED_TRACE(host_port_pair.ToString()); |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame] | 875 | for (const char* scheme : kSchemes) { |
| 876 | SCOPED_TRACE(scheme); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 877 | for (const auto& privacy_mode : kPrivacyModes) { |
| 878 | SCOPED_TRACE(privacy_mode); |
Brianna Goldstein | a7593fca | 2022-09-28 02:59:32 | [diff] [blame^] | 879 | for (const auto& network_anonymization_key : |
| 880 | kNetworkAnonymizationKeys) { |
| 881 | SCOPED_TRACE(network_anonymization_key.ToDebugString()); |
Ben Schwartz | 3ff4dc1e6 | 2021-04-27 21:15:23 | [diff] [blame] | 882 | for (const auto& secure_dns_policy : kSecureDnsPolicys) { |
| 883 | SCOPED_TRACE(static_cast<int>(secure_dns_policy)); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 884 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 885 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 886 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 887 | ClientSocketPool::GroupId group_id( |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame] | 888 | url::SchemeHostPort(scheme, host_port_pair.host(), |
| 889 | host_port_pair.port()), |
Brianna Goldstein | a7593fca | 2022-09-28 02:59:32 | [diff] [blame^] | 890 | privacy_mode, network_anonymization_key, secure_dns_policy); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 891 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 892 | EXPECT_FALSE(pool_->HasGroupForTesting(group_id)); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 893 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 894 | TestCompletionCallback callback; |
| 895 | ClientSocketHandle handle; |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 896 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 897 | // Since the group is empty, requesting a socket should not complete |
| 898 | // synchronously. |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 899 | EXPECT_THAT(handle.Init(group_id, params_, absl::nullopt, |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 900 | DEFAULT_PRIORITY, SocketTag(), |
| 901 | ClientSocketPool::RespectLimits::ENABLED, |
| 902 | callback.callback(), |
| 903 | ClientSocketPool::ProxyAuthCallback(), |
| 904 | pool_.get(), NetLogWithSource()), |
| 905 | IsError(ERR_IO_PENDING)); |
| 906 | EXPECT_TRUE(pool_->HasGroupForTesting(group_id)); |
| 907 | EXPECT_EQ(total_idle_sockets, pool_->IdleSocketCount()); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 908 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 909 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 910 | EXPECT_TRUE(handle.socket()); |
| 911 | EXPECT_TRUE(pool_->HasGroupForTesting(group_id)); |
| 912 | EXPECT_EQ(total_idle_sockets, pool_->IdleSocketCount()); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 913 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 914 | // Return socket to pool. |
| 915 | handle.Reset(); |
| 916 | EXPECT_EQ(total_idle_sockets + 1, 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 | // Requesting a socket again should return the same socket as |
| 919 | // before, so should complete synchronously. |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 920 | EXPECT_THAT(handle.Init(group_id, params_, absl::nullopt, |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 921 | DEFAULT_PRIORITY, SocketTag(), |
| 922 | ClientSocketPool::RespectLimits::ENABLED, |
| 923 | callback.callback(), |
| 924 | ClientSocketPool::ProxyAuthCallback(), |
| 925 | pool_.get(), NetLogWithSource()), |
| 926 | IsOk()); |
| 927 | EXPECT_TRUE(handle.socket()); |
| 928 | EXPECT_EQ(total_idle_sockets, pool_->IdleSocketCount()); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 929 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 930 | // Return socket to pool again. |
| 931 | handle.Reset(); |
| 932 | EXPECT_EQ(total_idle_sockets + 1, pool_->IdleSocketCount()); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 933 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 934 | ++total_idle_sockets; |
| 935 | } |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 936 | } |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 937 | } |
| 938 | } |
| 939 | } |
| 940 | } |
| 941 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 942 | TEST_F(ClientSocketPoolBaseTest, TotalLimit) { |
| 943 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 944 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 945 | // TODO(eroman): Check that the NetLog contains this event. |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 946 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 947 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 948 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
| 949 | EXPECT_THAT(StartRequest(TestGroupId("c"), DEFAULT_PRIORITY), IsOk()); |
| 950 | EXPECT_THAT(StartRequest(TestGroupId("d"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 951 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 952 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 953 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 954 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 955 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 956 | EXPECT_THAT(StartRequest(TestGroupId("e"), DEFAULT_PRIORITY), |
| 957 | IsError(ERR_IO_PENDING)); |
| 958 | EXPECT_THAT(StartRequest(TestGroupId("f"), DEFAULT_PRIORITY), |
| 959 | IsError(ERR_IO_PENDING)); |
| 960 | EXPECT_THAT(StartRequest(TestGroupId("g"), DEFAULT_PRIORITY), |
| 961 | IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 962 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 963 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 964 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 965 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 966 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 967 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 968 | |
| 969 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 970 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 971 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 972 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 973 | EXPECT_EQ(5, GetOrderOfRequest(5)); |
| 974 | EXPECT_EQ(6, GetOrderOfRequest(6)); |
| 975 | EXPECT_EQ(7, GetOrderOfRequest(7)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 976 | |
| 977 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 978 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(8)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 979 | } |
| 980 | |
| 981 | TEST_F(ClientSocketPoolBaseTest, TotalLimitReachedNewGroup) { |
| 982 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 983 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 984 | // TODO(eroman): Check that the NetLog contains this event. |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 985 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 986 | // Reach all limits: max total sockets, and max sockets per group. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 987 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 988 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 989 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
| 990 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 991 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 992 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 993 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 994 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 995 | |
| 996 | // Now create a new group and verify that we don't starve it. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 997 | EXPECT_THAT(StartRequest(TestGroupId("c"), DEFAULT_PRIORITY), |
| 998 | IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 999 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1000 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1001 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1002 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1003 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1004 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1005 | |
| 1006 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1007 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1008 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 1009 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 1010 | EXPECT_EQ(5, GetOrderOfRequest(5)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1011 | |
| 1012 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1013 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(6)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1014 | } |
| 1015 | |
| 1016 | TEST_F(ClientSocketPoolBaseTest, TotalLimitRespectsPriority) { |
| 1017 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1018 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1019 | EXPECT_THAT(StartRequest(TestGroupId("b"), LOWEST), IsOk()); |
| 1020 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsOk()); |
| 1021 | EXPECT_THAT(StartRequest(TestGroupId("b"), HIGHEST), IsOk()); |
| 1022 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1023 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1024 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1025 | client_socket_factory_.allocation_count()); |
| 1026 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1027 | EXPECT_THAT(StartRequest(TestGroupId("c"), LOWEST), IsError(ERR_IO_PENDING)); |
| 1028 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1029 | EXPECT_THAT(StartRequest(TestGroupId("b"), HIGHEST), IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1030 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1031 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[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(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1034 | |
| 1035 | // First 4 requests don't have to wait, and finish in order. |
| 1036 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1037 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1038 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 1039 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 1040 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1041 | // Request ("b", HIGHEST) has the highest priority, then (TestGroupId("a"), |
| 1042 | // MEDIUM), and then ("c", LOWEST). |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1043 | EXPECT_EQ(7, GetOrderOfRequest(5)); |
| 1044 | EXPECT_EQ(6, GetOrderOfRequest(6)); |
| 1045 | EXPECT_EQ(5, GetOrderOfRequest(7)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1046 | |
| 1047 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1048 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(9)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1049 | } |
| 1050 | |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1051 | // Test reprioritizing a request before completion doesn't interfere with |
| 1052 | // its completion. |
| 1053 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeOne) { |
| 1054 | CreatePool(kDefaultMaxSockets, 1); |
| 1055 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1056 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1057 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1058 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1059 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1060 | |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 1061 | request(1)->handle()->SetPriority(HIGHEST); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1062 | |
| 1063 | ReleaseOneConnection(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1064 | |
| 1065 | EXPECT_TRUE(request(1)->handle()->socket()); |
| 1066 | } |
| 1067 | |
| 1068 | // Reprioritize a request up past another one and make sure that changes the |
| 1069 | // completion order. |
| 1070 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeUpReorder) { |
| 1071 | CreatePool(kDefaultMaxSockets, 1); |
| 1072 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1073 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1074 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1075 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1076 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1077 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1078 | EXPECT_FALSE(request(2)->handle()->socket()); |
| 1079 | |
| 1080 | request(2)->handle()->SetPriority(HIGHEST); |
| 1081 | |
| 1082 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1083 | |
| 1084 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1085 | EXPECT_EQ(3, GetOrderOfRequest(2)); |
| 1086 | EXPECT_EQ(2, GetOrderOfRequest(3)); |
| 1087 | } |
| 1088 | |
| 1089 | // Reprioritize a request without changing relative priorities and check |
| 1090 | // that the order doesn't change. |
| 1091 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeUpNoReorder) { |
| 1092 | CreatePool(kDefaultMaxSockets, 1); |
| 1093 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1094 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1095 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1096 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1097 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1098 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1099 | EXPECT_FALSE(request(2)->handle()->socket()); |
| 1100 | |
| 1101 | request(2)->handle()->SetPriority(MEDIUM); |
| 1102 | |
| 1103 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1104 | |
| 1105 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1106 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1107 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 1108 | } |
| 1109 | |
| 1110 | // Reprioritize a request past down another one and make sure that changes the |
| 1111 | // completion order. |
| 1112 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeDownReorder) { |
| 1113 | CreatePool(kDefaultMaxSockets, 1); |
| 1114 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1115 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1116 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1117 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1118 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1119 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1120 | EXPECT_FALSE(request(2)->handle()->socket()); |
| 1121 | |
| 1122 | request(1)->handle()->SetPriority(LOW); |
| 1123 | |
| 1124 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1125 | |
| 1126 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1127 | EXPECT_EQ(3, GetOrderOfRequest(2)); |
| 1128 | EXPECT_EQ(2, GetOrderOfRequest(3)); |
| 1129 | } |
| 1130 | |
| 1131 | // Reprioritize a request to the same level as another and confirm it is |
| 1132 | // put after the old request. |
| 1133 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeResetFIFO) { |
| 1134 | CreatePool(kDefaultMaxSockets, 1); |
| 1135 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1136 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1137 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1138 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1139 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1140 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1141 | EXPECT_FALSE(request(2)->handle()->socket()); |
| 1142 | |
| 1143 | request(1)->handle()->SetPriority(MEDIUM); |
| 1144 | |
| 1145 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1146 | |
| 1147 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1148 | EXPECT_EQ(3, GetOrderOfRequest(2)); |
| 1149 | EXPECT_EQ(2, GetOrderOfRequest(3)); |
| 1150 | } |
| 1151 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1152 | TEST_F(ClientSocketPoolBaseTest, TotalLimitRespectsGroupLimit) { |
| 1153 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1154 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1155 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsOk()); |
| 1156 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsOk()); |
| 1157 | EXPECT_THAT(StartRequest(TestGroupId("b"), HIGHEST), IsOk()); |
| 1158 | EXPECT_THAT(StartRequest(TestGroupId("b"), MEDIUM), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1159 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1160 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1161 | client_socket_factory_.allocation_count()); |
| 1162 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1163 | EXPECT_THAT(StartRequest(TestGroupId("c"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1164 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 1165 | EXPECT_THAT(StartRequest(TestGroupId("b"), HIGHEST), IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1166 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1167 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[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()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1171 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1172 | |
| 1173 | // First 4 requests don't have to wait, and finish in order. |
| 1174 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1175 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1176 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 1177 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 1178 | |
| 1179 | // Request ("b", 7) has the highest priority, but we can't make new socket for |
| 1180 | // group "b", because it has reached the per-group limit. Then we make |
| 1181 | // socket for ("c", 6), because it has higher priority than ("a", 4), |
| 1182 | // and we still can't make a socket for group "b". |
| 1183 | EXPECT_EQ(5, GetOrderOfRequest(5)); |
| 1184 | EXPECT_EQ(6, GetOrderOfRequest(6)); |
| 1185 | EXPECT_EQ(7, GetOrderOfRequest(7)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1186 | |
| 1187 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1188 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(8)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1189 | } |
| 1190 | |
| 1191 | // Make sure that we count connecting sockets against the total limit. |
| 1192 | TEST_F(ClientSocketPoolBaseTest, TotalLimitCountsConnectingSockets) { |
| 1193 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1194 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1195 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1196 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
| 1197 | EXPECT_THAT(StartRequest(TestGroupId("c"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1198 | |
| 1199 | // Create one asynchronous request. |
| 1200 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1201 | EXPECT_THAT(StartRequest(TestGroupId("d"), DEFAULT_PRIORITY), |
| 1202 | IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1203 | |
[email protected] | 6b17538 | 2009-10-13 06:47:47 | [diff] [blame] | 1204 | // We post all of our delayed tasks with a 2ms delay. I.e. they don't |
| 1205 | // actually become pending until 2ms after they have been created. In order |
| 1206 | // to flush all tasks, we need to wait so that we know there are no |
| 1207 | // soon-to-be-pending tasks waiting. |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 1208 | FastForwardBy(base::Milliseconds(10)); |
[email protected] | 6b17538 | 2009-10-13 06:47:47 | [diff] [blame] | 1209 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1210 | // The next synchronous request should wait for its turn. |
| 1211 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1212 | EXPECT_THAT(StartRequest(TestGroupId("e"), DEFAULT_PRIORITY), |
| 1213 | IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1214 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1215 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1216 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1217 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1218 | client_socket_factory_.allocation_count()); |
| 1219 | |
| 1220 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1221 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1222 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 1223 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1224 | EXPECT_EQ(5, GetOrderOfRequest(5)); |
| 1225 | |
| 1226 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1227 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(6)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1228 | } |
| 1229 | |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1230 | TEST_F(ClientSocketPoolBaseTest, CorrectlyCountStalledGroups) { |
| 1231 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 1232 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 1233 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1234 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1235 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1236 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1237 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1238 | |
| 1239 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 1240 | |
| 1241 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1242 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1243 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), |
| 1244 | IsError(ERR_IO_PENDING)); |
| 1245 | EXPECT_THAT(StartRequest(TestGroupId("c"), DEFAULT_PRIORITY), |
| 1246 | IsError(ERR_IO_PENDING)); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1247 | |
| 1248 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1249 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1250 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1251 | EXPECT_EQ(kDefaultMaxSockets + 1, client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1252 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1253 | EXPECT_EQ(kDefaultMaxSockets + 2, client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1254 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
| 1255 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1256 | EXPECT_EQ(kDefaultMaxSockets + 2, client_socket_factory_.allocation_count()); |
| 1257 | } |
| 1258 | |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 1259 | TEST_F(ClientSocketPoolBaseTest, StallAndThenCancelAndTriggerAvailableSocket) { |
| 1260 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 1261 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1262 | |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1263 | TestCompletionCallback callback; |
Peter Kasting | e20797db | 2022-08-18 19:44:14 | [diff] [blame] | 1264 | ClientSocketHandle stalled_handle; |
| 1265 | EXPECT_EQ(ERR_IO_PENDING, |
| 1266 | stalled_handle.Init( |
| 1267 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
| 1268 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1269 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1270 | pool_.get(), NetLogWithSource())); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 1271 | |
| 1272 | ClientSocketHandle handles[4]; |
Tsuyoshi Horo | 17ef47d0 | 2022-06-30 10:58:27 | [diff] [blame] | 1273 | for (auto& handle : handles) { |
| 1274 | EXPECT_EQ( |
| 1275 | ERR_IO_PENDING, |
| 1276 | handle.Init(TestGroupId("b"), params_, absl::nullopt, DEFAULT_PRIORITY, |
| 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 | |
| 1282 | // One will be stalled, cancel all the handles now. |
| 1283 | // This should hit the OnAvailableSocketSlot() code where we previously had |
| 1284 | // stalled groups, but no longer have any. |
Tsuyoshi Horo | 17ef47d0 | 2022-06-30 10:58:27 | [diff] [blame] | 1285 | for (auto& handle : handles) |
| 1286 | handle.Reset(); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 1287 | } |
| 1288 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1289 | TEST_F(ClientSocketPoolBaseTest, CancelStalledSocketAtSocketLimit) { |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1290 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1291 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 1292 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1293 | { |
| 1294 | ClientSocketHandle handles[kDefaultMaxSockets]; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1295 | TestCompletionCallback callbacks[kDefaultMaxSockets]; |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1296 | for (int i = 0; i < kDefaultMaxSockets; ++i) { |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame] | 1297 | EXPECT_EQ(OK, handles[i].Init(TestGroupId("a" + base::NumberToString(i)), |
| 1298 | params_, absl::nullopt, DEFAULT_PRIORITY, |
| 1299 | SocketTag(), |
| 1300 | ClientSocketPool::RespectLimits::ENABLED, |
| 1301 | callbacks[i].callback(), |
| 1302 | ClientSocketPool::ProxyAuthCallback(), |
| 1303 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1304 | } |
| 1305 | |
| 1306 | // Force a stalled group. |
| 1307 | ClientSocketHandle stalled_handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1308 | TestCompletionCallback callback; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1309 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1310 | stalled_handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1311 | TestGroupId("foo"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1312 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1313 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1314 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1315 | |
| 1316 | // Cancel the stalled request. |
| 1317 | stalled_handle.Reset(); |
| 1318 | |
| 1319 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1320 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 1321 | |
| 1322 | // Dropping out of scope will close all handles and return them to idle. |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1323 | } |
| 1324 | |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1325 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1326 | EXPECT_EQ(kDefaultMaxSockets, pool_->IdleSocketCount()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1327 | } |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1328 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1329 | TEST_F(ClientSocketPoolBaseTest, CancelPendingSocketAtSocketLimit) { |
| 1330 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1331 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 1332 | |
| 1333 | { |
| 1334 | ClientSocketHandle handles[kDefaultMaxSockets]; |
| 1335 | for (int i = 0; i < kDefaultMaxSockets; ++i) { |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1336 | TestCompletionCallback callback; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1337 | EXPECT_EQ(ERR_IO_PENDING, |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame] | 1338 | handles[i].Init( |
| 1339 | TestGroupId("a" + base::NumberToString(i)), params_, |
| 1340 | absl::nullopt, DEFAULT_PRIORITY, SocketTag(), |
| 1341 | ClientSocketPool::RespectLimits::ENABLED, |
| 1342 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1343 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1344 | } |
| 1345 | |
| 1346 | // Force a stalled group. |
| 1347 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1348 | ClientSocketHandle stalled_handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1349 | TestCompletionCallback callback; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1350 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1351 | stalled_handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1352 | TestGroupId("foo"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1353 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1354 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1355 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1356 | |
| 1357 | // Since it is stalled, it should have no connect jobs. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1358 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("foo"))); |
| 1359 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 1360 | TestGroupId("foo"))); |
| 1361 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroupForTesting( |
| 1362 | TestGroupId("foo"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1363 | |
| 1364 | // Cancel the stalled request. |
| 1365 | handles[0].Reset(); |
| 1366 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1367 | // Now we should have a connect job. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1368 | EXPECT_EQ(1u, 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 | // The stalled socket should connect. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1375 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1376 | |
| 1377 | EXPECT_EQ(kDefaultMaxSockets + 1, |
| 1378 | client_socket_factory_.allocation_count()); |
| 1379 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1380 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("foo"))); |
| 1381 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 1382 | TestGroupId("foo"))); |
| 1383 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroupForTesting( |
| 1384 | TestGroupId("foo"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1385 | |
| 1386 | // Dropping out of scope will close all handles and return them to idle. |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1387 | } |
| 1388 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1389 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 1390 | } |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1391 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1392 | TEST_F(ClientSocketPoolBaseTest, WaitForStalledSocketAtSocketLimit) { |
| 1393 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1394 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1395 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1396 | ClientSocketHandle stalled_handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1397 | TestCompletionCallback callback; |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1398 | { |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 1399 | EXPECT_FALSE(pool_->IsStalled()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1400 | ClientSocketHandle handles[kDefaultMaxSockets]; |
| 1401 | for (int i = 0; i < kDefaultMaxSockets; ++i) { |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1402 | EXPECT_EQ( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1403 | OK, handles[i].Init( |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame] | 1404 | TestGroupId(base::StringPrintf("take-2-%d", i)), params_, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1405 | absl::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1406 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1407 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 1408 | NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1409 | } |
| 1410 | |
| 1411 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1412 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 1413 | EXPECT_FALSE(pool_->IsStalled()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1414 | |
| 1415 | // Now we will hit the socket limit. |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1416 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1417 | stalled_handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1418 | TestGroupId("foo"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1419 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1420 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1421 | pool_.get(), NetLogWithSource())); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 1422 | EXPECT_TRUE(pool_->IsStalled()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1423 | |
| 1424 | // Dropping out of scope will close all handles and return them to idle. |
| 1425 | } |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1426 | |
| 1427 | // But if we wait for it, the released idle sockets will be closed in |
| 1428 | // preference of the waiting request. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1429 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1430 | |
| 1431 | EXPECT_EQ(kDefaultMaxSockets + 1, client_socket_factory_.allocation_count()); |
| 1432 | EXPECT_EQ(3, pool_->IdleSocketCount()); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1433 | } |
| 1434 | |
| 1435 | // Regression test for http://crbug.com/40952. |
| 1436 | TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketAtSocketLimitDeleteGroup) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1437 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 1438 | true /* enable_backup_connect_jobs */); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1439 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 1440 | |
| 1441 | for (int i = 0; i < kDefaultMaxSockets; ++i) { |
| 1442 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1443 | TestCompletionCallback callback; |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame] | 1444 | EXPECT_EQ( |
| 1445 | OK, |
| 1446 | handle.Init(TestGroupId("a" + base::NumberToString(i)), params_, |
| 1447 | absl::nullopt, DEFAULT_PRIORITY, SocketTag(), |
| 1448 | ClientSocketPool::RespectLimits::ENABLED, |
| 1449 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1450 | pool_.get(), NetLogWithSource())); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1451 | } |
| 1452 | |
| 1453 | // Flush all the DoReleaseSocket tasks. |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 1454 | base::RunLoop().RunUntilIdle(); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1455 | |
| 1456 | // Stall a group. Set a pending job so it'll trigger a backup job if we don't |
| 1457 | // reuse a socket. |
| 1458 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1459 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1460 | TestCompletionCallback callback; |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1461 | |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame] | 1462 | // "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] | 1463 | // which is the one which we would close an idle socket for. We shouldn't |
| 1464 | // close an idle socket though, since we should reuse the idle socket. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1465 | EXPECT_EQ(OK, handle.Init( |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame] | 1466 | TestGroupId("a0"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1467 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1468 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1469 | pool_.get(), NetLogWithSource())); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1470 | |
| 1471 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1472 | EXPECT_EQ(kDefaultMaxSockets - 1, pool_->IdleSocketCount()); |
| 1473 | } |
| 1474 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1475 | TEST_F(ClientSocketPoolBaseTest, PendingRequests) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1476 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1477 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1478 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1479 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1480 | EXPECT_THAT(StartRequest(TestGroupId("a"), IDLE), IsError(ERR_IO_PENDING)); |
| 1481 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
| 1482 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1483 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1484 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 1485 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1486 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1487 | ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1488 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
| 1489 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1490 | EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup, |
| 1491 | completion_count()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1492 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1493 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1494 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
[email protected] | c9c6f5c | 2010-07-31 01:30:03 | [diff] [blame] | 1495 | EXPECT_EQ(8, GetOrderOfRequest(3)); |
| 1496 | EXPECT_EQ(6, GetOrderOfRequest(4)); |
| 1497 | EXPECT_EQ(4, GetOrderOfRequest(5)); |
| 1498 | EXPECT_EQ(3, GetOrderOfRequest(6)); |
| 1499 | EXPECT_EQ(5, GetOrderOfRequest(7)); |
| 1500 | EXPECT_EQ(7, GetOrderOfRequest(8)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1501 | |
| 1502 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1503 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(9)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1504 | } |
| 1505 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1506 | TEST_F(ClientSocketPoolBaseTest, PendingRequests_NoKeepAlive) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1507 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1508 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1509 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1510 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1511 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
| 1512 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1513 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1514 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 1515 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1516 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1517 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1518 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1519 | for (size_t i = kDefaultMaxSocketsPerGroup; i < requests_size(); ++i) |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1520 | EXPECT_THAT(request(i)->WaitForResult(), IsOk()); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1521 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1522 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1523 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1524 | EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup, |
| 1525 | completion_count()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1526 | } |
| 1527 | |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1528 | TEST_F(ClientSocketPoolBaseTest, ResetAndCloseSocket) { |
| 1529 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1530 | |
| 1531 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1532 | ClientSocketHandle handle; |
| 1533 | TestCompletionCallback callback; |
| 1534 | EXPECT_EQ( |
| 1535 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1536 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1537 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1538 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1539 | pool_.get(), NetLogWithSource())); |
| 1540 | |
| 1541 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 1542 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1543 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1544 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
| 1545 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 1546 | |
| 1547 | handle.ResetAndCloseSocket(); |
| 1548 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1549 | } |
| 1550 | |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1551 | // This test will start up a socket request and then call Reset() on the handle. |
| 1552 | // The pending ConnectJob should not be destroyed. |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1553 | TEST_F(ClientSocketPoolBaseTest, CancelRequestKeepsConnectJob) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1554 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1555 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1556 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1557 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1558 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1559 | EXPECT_EQ( |
| 1560 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1561 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1562 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1563 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1564 | pool_.get(), NetLogWithSource())); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1565 | handle.Reset(); |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1566 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1567 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1568 | } |
| 1569 | |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1570 | // This test will start up a socket request and then call ResetAndCloseSocket() |
| 1571 | // on the handle. The pending ConnectJob or connected socket should be |
| 1572 | // destroyed. |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1573 | TEST_F(ClientSocketPoolBaseTest, CancelRequestAndCloseSocket) { |
| 1574 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1575 | |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1576 | // When true, the socket connects before it's canceled. |
| 1577 | for (bool cancel_when_callback_pending : {false, true}) { |
| 1578 | if (cancel_when_callback_pending) { |
| 1579 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 1580 | } else { |
| 1581 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1582 | } |
| 1583 | ClientSocketHandle handle; |
| 1584 | TestCompletionCallback callback; |
| 1585 | EXPECT_EQ( |
| 1586 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1587 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1588 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1589 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1590 | pool_.get(), NetLogWithSource())); |
| 1591 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1592 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1593 | |
| 1594 | if (cancel_when_callback_pending) { |
| 1595 | client_socket_factory_.SignalJobs(); |
| 1596 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1597 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 1598 | } |
| 1599 | |
| 1600 | handle.ResetAndCloseSocket(); |
| 1601 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1602 | } |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1603 | } |
| 1604 | |
| 1605 | TEST_F(ClientSocketPoolBaseTest, |
| 1606 | CancelRequestAndCloseSocketWhenMoreRequestsThanConnectJobs) { |
| 1607 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1608 | |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1609 | // When true, the sockets connect before they're canceled. |
| 1610 | for (bool cancel_when_callback_pending : {false, true}) { |
| 1611 | if (cancel_when_callback_pending) { |
| 1612 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 1613 | } else { |
| 1614 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1615 | } |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1616 | |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1617 | std::vector<std::unique_ptr<ClientSocketHandle>> handles; |
| 1618 | TestCompletionCallback callback; |
| 1619 | // Make |kDefaultMaxSockets + 1| socket requests. |
| 1620 | for (int i = 0; i < kDefaultMaxSocketsPerGroup + 1; ++i) { |
| 1621 | std::unique_ptr<ClientSocketHandle> handle = |
| 1622 | std::make_unique<ClientSocketHandle>(); |
| 1623 | EXPECT_EQ(ERR_IO_PENDING, |
| 1624 | handle->Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1625 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1626 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1627 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1628 | pool_.get(), NetLogWithSource())); |
| 1629 | handles.push_back(std::move(handle)); |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1630 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1631 | EXPECT_EQ( |
| 1632 | static_cast<size_t>(std::min(i + 1, kDefaultMaxSocketsPerGroup)), |
| 1633 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1634 | } |
| 1635 | |
| 1636 | if (cancel_when_callback_pending) { |
| 1637 | client_socket_factory_.SignalJobs(); |
| 1638 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1639 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
| 1640 | pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 1641 | } |
| 1642 | |
| 1643 | // Calling ResetAndCloseSocket() on a handle should not cancel a ConnectJob |
| 1644 | // or close a socket, since there are more requests than ConnectJobs or |
| 1645 | // sockets. |
| 1646 | handles[kDefaultMaxSocketsPerGroup]->ResetAndCloseSocket(); |
| 1647 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1648 | if (cancel_when_callback_pending) { |
| 1649 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
| 1650 | pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 1651 | } else { |
| 1652 | EXPECT_EQ(static_cast<size_t>(kDefaultMaxSocketsPerGroup), |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1653 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1654 | } |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1655 | |
| 1656 | // Calling ResetAndCloseSocket() on other handles should cancel a ConnectJob |
| 1657 | // or close a socket. |
| 1658 | for (int i = kDefaultMaxSocketsPerGroup - 1; i >= 0; --i) { |
| 1659 | handles[i]->ResetAndCloseSocket(); |
| 1660 | if (i > 0) { |
| 1661 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1662 | if (cancel_when_callback_pending) { |
| 1663 | EXPECT_EQ(i, |
| 1664 | pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 1665 | } else { |
| 1666 | EXPECT_EQ(static_cast<size_t>(i), |
| 1667 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1668 | } |
| 1669 | } else { |
| 1670 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1671 | } |
| 1672 | } |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1673 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1674 | } |
| 1675 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1676 | TEST_F(ClientSocketPoolBaseTest, ConnectCancelConnect) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1677 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1678 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1679 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 1680 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1681 | TestCompletionCallback callback; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1682 | |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1683 | EXPECT_EQ( |
| 1684 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1685 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1686 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1687 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1688 | pool_.get(), NetLogWithSource())); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1689 | |
| 1690 | handle.Reset(); |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1691 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1692 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1693 | |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1694 | // This will create a second ConnectJob, since the other ConnectJob was |
| 1695 | // previously assigned to a request. |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1696 | TestCompletionCallback callback2; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1697 | EXPECT_EQ( |
| 1698 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1699 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1700 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1701 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1702 | pool_.get(), NetLogWithSource())); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1703 | |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1704 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1705 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1706 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1707 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1708 | EXPECT_FALSE(callback.have_result()); |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1709 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1710 | // One ConnectJob completed, and its socket is now assigned to |handle|. |
| 1711 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 1712 | // The other ConnectJob should have either completed, or still be connecting. |
| 1713 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")) + |
| 1714 | pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1715 | |
| 1716 | handle.Reset(); |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1717 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1718 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")) + |
| 1719 | pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
| 1720 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1721 | } |
| 1722 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1723 | TEST_F(ClientSocketPoolBaseTest, CancelRequest) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1724 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1725 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1726 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1727 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1728 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
| 1729 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1730 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1731 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 1732 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1733 | |
| 1734 | // Cancel a request. |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1735 | size_t index_to_cancel = kDefaultMaxSocketsPerGroup + 2; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1736 | EXPECT_FALSE((*requests())[index_to_cancel]->handle()->is_initialized()); |
| 1737 | (*requests())[index_to_cancel]->handle()->Reset(); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1738 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1739 | ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1740 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1741 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
| 1742 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1743 | EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup - 1, |
| 1744 | completion_count()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1745 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1746 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1747 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1748 | EXPECT_EQ(5, GetOrderOfRequest(3)); |
| 1749 | EXPECT_EQ(3, GetOrderOfRequest(4)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1750 | EXPECT_EQ(ClientSocketPoolTest::kRequestNotFound, |
| 1751 | GetOrderOfRequest(5)); // Canceled request. |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1752 | EXPECT_EQ(4, GetOrderOfRequest(6)); |
| 1753 | EXPECT_EQ(6, GetOrderOfRequest(7)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1754 | |
| 1755 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1756 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(8)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1757 | } |
| 1758 | |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1759 | // Function to be used as a callback on socket request completion. It first |
| 1760 | // disconnects the successfully connected socket from the first request, and |
| 1761 | // then reuses the ClientSocketHandle to request another socket. |
| 1762 | // |
| 1763 | // |nested_callback| is called with the result of the second socket request. |
| 1764 | void RequestSocketOnComplete(ClientSocketHandle* handle, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1765 | TransportClientSocketPool* pool, |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1766 | TestConnectJobFactory* test_connect_job_factory, |
| 1767 | TestConnectJob::JobType next_job_type, |
Bence Béky | a4a5093 | 2018-08-10 13:39:41 | [diff] [blame] | 1768 | TestCompletionCallback* nested_callback, |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1769 | int first_request_result) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1770 | EXPECT_THAT(first_request_result, IsOk()); |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1771 | |
| 1772 | test_connect_job_factory->set_job_type(next_job_type); |
| 1773 | |
| 1774 | // Don't allow reuse of the socket. Disconnect it and then release it. |
| 1775 | if (handle->socket()) |
| 1776 | handle->socket()->Disconnect(); |
| 1777 | handle->Reset(); |
| 1778 | |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1779 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1780 | int rv = handle->Init( |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 1781 | TestGroupId("a"), |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1782 | ClientSocketPool::SocketParams::CreateForHttpForTesting(), absl::nullopt, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1783 | LOWEST, SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 1784 | nested_callback->callback(), ClientSocketPool::ProxyAuthCallback(), pool, |
| 1785 | NetLogWithSource()); |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1786 | if (rv != ERR_IO_PENDING) { |
| 1787 | DCHECK_EQ(TestConnectJob::kMockJob, next_job_type); |
Bence Béky | a4a5093 | 2018-08-10 13:39:41 | [diff] [blame] | 1788 | nested_callback->callback().Run(rv); |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1789 | } else { |
| 1790 | DCHECK_EQ(TestConnectJob::kMockPendingJob, next_job_type); |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1791 | } |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1792 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1793 | |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1794 | // Tests the case where a second socket is requested in a completion callback, |
| 1795 | // and the second socket connects asynchronously. Reuses the same |
| 1796 | // ClientSocketHandle for the second socket, after disconnecting the first. |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1797 | TEST_F(ClientSocketPoolBaseTest, RequestPendingJobTwice) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1798 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1799 | |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 1800 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 1801 | ClientSocketHandle handle; |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1802 | TestCompletionCallback second_result_callback; |
| 1803 | int rv = handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1804 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Paul Jensen | 8d6f87ec | 2018-01-13 00:46:54 | [diff] [blame] | 1805 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1806 | base::BindOnce(&RequestSocketOnComplete, &handle, pool_.get(), |
| 1807 | connect_job_factory_, TestConnectJob::kMockPendingJob, |
| 1808 | &second_result_callback), |
| 1809 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1810 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1811 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1812 | EXPECT_THAT(second_result_callback.WaitForResult(), IsOk()); |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1813 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1814 | |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1815 | // Tests the case where a second socket is requested in a completion callback, |
| 1816 | // and the second socket connects synchronously. Reuses the same |
| 1817 | // ClientSocketHandle for the second socket, after disconnecting the first. |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1818 | TEST_F(ClientSocketPoolBaseTest, RequestPendingJobThenSynchronous) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1819 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1820 | |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 1821 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 1822 | ClientSocketHandle handle; |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1823 | TestCompletionCallback second_result_callback; |
| 1824 | int rv = handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1825 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Paul Jensen | 8d6f87ec | 2018-01-13 00:46:54 | [diff] [blame] | 1826 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1827 | base::BindOnce(&RequestSocketOnComplete, &handle, pool_.get(), |
| 1828 | connect_job_factory_, TestConnectJob::kMockPendingJob, |
| 1829 | &second_result_callback), |
| 1830 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1831 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1832 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1833 | EXPECT_THAT(second_result_callback.WaitForResult(), IsOk()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1834 | } |
| 1835 | |
| 1836 | // Make sure that pending requests get serviced after active requests get |
| 1837 | // cancelled. |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1838 | TEST_F(ClientSocketPoolBaseTest, CancelActiveRequestWithPendingRequests) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1839 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1840 | |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 1841 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1842 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1843 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1844 | IsError(ERR_IO_PENDING)); |
| 1845 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1846 | IsError(ERR_IO_PENDING)); |
| 1847 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1848 | IsError(ERR_IO_PENDING)); |
| 1849 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1850 | IsError(ERR_IO_PENDING)); |
| 1851 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1852 | IsError(ERR_IO_PENDING)); |
| 1853 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1854 | IsError(ERR_IO_PENDING)); |
| 1855 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1856 | IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1857 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1858 | // Now, kDefaultMaxSocketsPerGroup requests should be active. |
| 1859 | // Let's cancel them. |
| 1860 | for (int i = 0; i < kDefaultMaxSocketsPerGroup; ++i) { |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1861 | ASSERT_FALSE(request(i)->handle()->is_initialized()); |
| 1862 | request(i)->handle()->Reset(); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1863 | } |
| 1864 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1865 | // Let's wait for the rest to complete now. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1866 | for (size_t i = kDefaultMaxSocketsPerGroup; i < requests_size(); ++i) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1867 | EXPECT_THAT(request(i)->WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1868 | request(i)->handle()->Reset(); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1869 | } |
| 1870 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1871 | EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup, |
| 1872 | completion_count()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1873 | } |
| 1874 | |
| 1875 | // Make sure that pending requests get serviced after active requests fail. |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1876 | TEST_F(ClientSocketPoolBaseTest, FailingActiveRequestWithPendingRequests) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1877 | const size_t kMaxSockets = 5; |
| 1878 | CreatePool(kMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1879 | |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 1880 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1881 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1882 | const size_t kNumberOfRequests = 2 * kDefaultMaxSocketsPerGroup + 1; |
| 1883 | ASSERT_LE(kNumberOfRequests, kMaxSockets); // Otherwise the test will hang. |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1884 | |
| 1885 | // Queue up all the requests |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1886 | for (size_t i = 0; i < kNumberOfRequests; ++i) |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1887 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1888 | IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1889 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1890 | for (size_t i = 0; i < kNumberOfRequests; ++i) |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1891 | EXPECT_THAT(request(i)->WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1892 | } |
| 1893 | |
mmenke | 9d72fe4 | 2017-05-18 22:36:07 | [diff] [blame] | 1894 | // Make sure that pending requests that complete synchronously get serviced |
| 1895 | // after active requests fail. See https://crbug.com/723748 |
| 1896 | TEST_F(ClientSocketPoolBaseTest, HandleMultipleSyncFailuresAfterAsyncFailure) { |
| 1897 | const size_t kNumberOfRequests = 10; |
| 1898 | const size_t kMaxSockets = 1; |
| 1899 | CreatePool(kMaxSockets, kMaxSockets); |
| 1900 | |
| 1901 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
| 1902 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1903 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1904 | IsError(ERR_IO_PENDING)); |
mmenke | 9d72fe4 | 2017-05-18 22:36:07 | [diff] [blame] | 1905 | |
| 1906 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
| 1907 | |
| 1908 | // Queue up all the other requests |
| 1909 | for (size_t i = 1; i < kNumberOfRequests; ++i) |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1910 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1911 | IsError(ERR_IO_PENDING)); |
mmenke | 9d72fe4 | 2017-05-18 22:36:07 | [diff] [blame] | 1912 | |
| 1913 | // Make sure all requests fail, instead of hanging. |
| 1914 | for (size_t i = 0; i < kNumberOfRequests; ++i) |
| 1915 | EXPECT_THAT(request(i)->WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 1916 | } |
| 1917 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1918 | TEST_F(ClientSocketPoolBaseTest, CancelActiveRequestThenRequestSocket) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1919 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1920 | |
| 1921 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1922 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1923 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1924 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1925 | int rv = handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1926 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1927 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1928 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1929 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1930 | |
| 1931 | // Cancel the active request. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1932 | handle.Reset(); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1933 | |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1934 | rv = handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1935 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1936 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1937 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1938 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 1939 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1940 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1941 | EXPECT_FALSE(handle.is_reused()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 1942 | TestLoadTimingInfoConnectedNotReused(handle); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1943 | EXPECT_EQ(2, client_socket_factory_.allocation_count()); |
| 1944 | } |
| 1945 | |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1946 | TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketsForced) { |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 1947 | const char kReason[] = "Really nifty reason"; |
| 1948 | |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1949 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1950 | ClientSocketHandle handle; |
| 1951 | TestCompletionCallback callback; |
Matt Reichhoff | 0049a0b7 | 2021-10-20 20:44:26 | [diff] [blame] | 1952 | int rv = |
| 1953 | handle.Init(TestGroupId("a"), params_, absl::nullopt, LOWEST, SocketTag(), |
| 1954 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1955 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 1956 | NetLogWithSource::Make(NetLogSourceType::NONE)); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1957 | EXPECT_THAT(rv, IsOk()); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 1958 | ASSERT_TRUE(handle.socket()); |
| 1959 | NetLogSource source = handle.socket()->NetLog().source(); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1960 | handle.Reset(); |
| 1961 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 1962 | pool_->CloseIdleSockets(kReason); |
| 1963 | ExpectSocketClosedWithReason(source, kReason); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1964 | } |
| 1965 | |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1966 | TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketsInGroupForced) { |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1967 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1968 | TestCompletionCallback callback; |
Matt Reichhoff | 0049a0b7 | 2021-10-20 20:44:26 | [diff] [blame] | 1969 | NetLogWithSource net_log_with_source = |
| 1970 | NetLogWithSource::Make(NetLogSourceType::NONE); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1971 | ClientSocketHandle handle1; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1972 | int rv = handle1.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1973 | TestGroupId("a"), params_, absl::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1974 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
Matt Reichhoff | 0049a0b7 | 2021-10-20 20:44:26 | [diff] [blame] | 1975 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), net_log_with_source); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1976 | EXPECT_THAT(rv, IsOk()); |
| 1977 | ClientSocketHandle handle2; |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1978 | rv = handle2.Init(TestGroupId("a"), params_, absl::nullopt, LOWEST, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1979 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1980 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
Matt Reichhoff | 0049a0b7 | 2021-10-20 20:44:26 | [diff] [blame] | 1981 | pool_.get(), net_log_with_source); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1982 | ClientSocketHandle handle3; |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 1983 | rv = handle3.Init(TestGroupId("b"), params_, absl::nullopt, LOWEST, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1984 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1985 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
Matt Reichhoff | 0049a0b7 | 2021-10-20 20:44:26 | [diff] [blame] | 1986 | pool_.get(), net_log_with_source); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1987 | EXPECT_THAT(rv, IsOk()); |
| 1988 | handle1.Reset(); |
| 1989 | handle2.Reset(); |
| 1990 | handle3.Reset(); |
| 1991 | EXPECT_EQ(3, pool_->IdleSocketCount()); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 1992 | pool_->CloseIdleSocketsInGroup(TestGroupId("a"), "Very good reason"); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1993 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1994 | } |
| 1995 | |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1996 | TEST_F(ClientSocketPoolBaseTest, CleanUpUnusableIdleSockets) { |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1997 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1998 | ClientSocketHandle handle; |
| 1999 | TestCompletionCallback callback; |
Matt Reichhoff | 0049a0b7 | 2021-10-20 20:44:26 | [diff] [blame] | 2000 | NetLogWithSource net_log_with_source = |
| 2001 | NetLogWithSource::Make(NetLogSourceType::NONE); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2002 | int rv = handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2003 | TestGroupId("a"), params_, absl::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2004 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
Matt Reichhoff | 0049a0b7 | 2021-10-20 20:44:26 | [diff] [blame] | 2005 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), net_log_with_source); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 2006 | EXPECT_THAT(rv, IsOk()); |
| 2007 | StreamSocket* socket = handle.socket(); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 2008 | ASSERT_TRUE(socket); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 2009 | handle.Reset(); |
| 2010 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 2011 | |
| 2012 | // Disconnect socket now to make the socket unusable. |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 2013 | NetLogSource source = socket->NetLog().source(); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 2014 | socket->Disconnect(); |
| 2015 | ClientSocketHandle handle2; |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2016 | rv = handle2.Init(TestGroupId("a"), params_, absl::nullopt, LOWEST, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2017 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2018 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
Matt Reichhoff | 0049a0b7 | 2021-10-20 20:44:26 | [diff] [blame] | 2019 | pool_.get(), net_log_with_source); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 2020 | EXPECT_THAT(rv, IsOk()); |
| 2021 | EXPECT_FALSE(handle2.is_reused()); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 2022 | |
| 2023 | // This is admittedly not an accurate error in this case, but normally code |
| 2024 | // doesn't secretly keep a raw pointers to sockets returned to the socket pool |
| 2025 | // and close them out of band, so discovering an idle socket was closed when |
| 2026 | // trying to reuse it normally means it was closed by the remote side. |
| 2027 | ExpectSocketClosedWithReason( |
| 2028 | source, TransportClientSocketPool::kRemoteSideClosedConnection); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 2029 | } |
| 2030 | |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 2031 | // Regression test for http://crbug.com/17985. |
| 2032 | TEST_F(ClientSocketPoolBaseTest, GroupWithPendingRequestsIsNotEmpty) { |
| 2033 | const int kMaxSockets = 3; |
| 2034 | const int kMaxSocketsPerGroup = 2; |
| 2035 | CreatePool(kMaxSockets, kMaxSocketsPerGroup); |
| 2036 | |
[email protected] | ac790b4 | 2009-12-02 04:31:31 | [diff] [blame] | 2037 | const RequestPriority kHighPriority = HIGHEST; |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 2038 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2039 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 2040 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 2041 | |
| 2042 | // This is going to be a pending request in an otherwise empty group. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2043 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2044 | IsError(ERR_IO_PENDING)); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 2045 | |
| 2046 | // Reach the maximum socket limit. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2047 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 2048 | |
| 2049 | // Create a stalled group with high priorities. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2050 | EXPECT_THAT(StartRequest(TestGroupId("c"), kHighPriority), |
| 2051 | IsError(ERR_IO_PENDING)); |
| 2052 | EXPECT_THAT(StartRequest(TestGroupId("c"), kHighPriority), |
| 2053 | IsError(ERR_IO_PENDING)); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 2054 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2055 | // Release the first two sockets from TestGroupId("a"). Because this is a |
| 2056 | // keepalive, the first release will unblock the pending request for |
| 2057 | // TestGroupId("a"). The second release will unblock a request for "c", |
| 2058 | // because it is the next high priority socket. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2059 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
| 2060 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 2061 | |
| 2062 | // Closing idle sockets should not get us into trouble, but in the bug |
| 2063 | // we were hitting a CHECK here. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2064 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 2065 | pool_->CloseIdleSockets("Very good reason"); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 2066 | |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 2067 | // Run the released socket wakeups. |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 2068 | base::RunLoop().RunUntilIdle(); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 2069 | } |
| 2070 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2071 | TEST_F(ClientSocketPoolBaseTest, BasicAsynchronous) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2072 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2073 | |
| 2074 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2075 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2076 | TestCompletionCallback callback; |
Matt Reichhoff | 0049a0b7 | 2021-10-20 20:44:26 | [diff] [blame] | 2077 | NetLogWithSource net_log_with_source = |
| 2078 | NetLogWithSource::Make(NetLogSourceType::NONE); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2079 | int rv = handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2080 | TestGroupId("a"), params_, absl::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2081 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
Matt Reichhoff | 0049a0b7 | 2021-10-20 20:44:26 | [diff] [blame] | 2082 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), net_log_with_source); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2083 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2084 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2085 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 2086 | TestLoadTimingInfoNotConnected(handle); |
| 2087 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2088 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2089 | EXPECT_TRUE(handle.is_initialized()); |
| 2090 | EXPECT_TRUE(handle.socket()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 2091 | TestLoadTimingInfoConnectedNotReused(handle); |
| 2092 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2093 | handle.Reset(); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 2094 | TestLoadTimingInfoNotConnected(handle); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 2095 | |
Matt Reichhoff | 0049a0b7 | 2021-10-20 20:44:26 | [diff] [blame] | 2096 | auto entries = |
| 2097 | net_log_observer_.GetEntriesForSource(net_log_with_source.source()); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 2098 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2099 | EXPECT_EQ(5u, entries.size()); |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 2100 | EXPECT_TRUE(LogContainsEvent( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2101 | entries, 0, NetLogEventType::TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 2102 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2103 | EXPECT_TRUE(LogContainsBeginEvent(entries, 1, NetLogEventType::SOCKET_POOL)); |
| 2104 | EXPECT_TRUE(LogContainsEvent( |
| 2105 | entries, 2, NetLogEventType::SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
| 2106 | NetLogEventPhase::NONE)); |
| 2107 | EXPECT_TRUE(LogContainsEvent(entries, 3, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 2108 | NetLogEventType::SOCKET_POOL_BOUND_TO_SOCKET, |
| 2109 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2110 | EXPECT_TRUE(LogContainsEndEvent(entries, 4, NetLogEventType::SOCKET_POOL)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2111 | } |
| 2112 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2113 | TEST_F(ClientSocketPoolBaseTest, |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2114 | InitConnectionAsynchronousFailure) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2115 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2116 | |
| 2117 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2118 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2119 | TestCompletionCallback callback; |
Matt Reichhoff | 0049a0b7 | 2021-10-20 20:44:26 | [diff] [blame] | 2120 | NetLogWithSource net_log_with_source = |
| 2121 | NetLogWithSource::Make(NetLogSourceType::NONE); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2122 | // Set the additional error state members to ensure that they get cleared. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2123 | handle.set_is_ssl_error(true); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 2124 | handle.set_ssl_cert_request_info(base::MakeRefCounted<SSLCertRequestInfo>()); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2125 | EXPECT_EQ( |
| 2126 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2127 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2128 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2129 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
Matt Reichhoff | 0049a0b7 | 2021-10-20 20:44:26 | [diff] [blame] | 2130 | pool_.get(), net_log_with_source)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2131 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2132 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2133 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2134 | EXPECT_FALSE(handle.is_ssl_error()); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 2135 | EXPECT_FALSE(handle.ssl_cert_request_info()); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 2136 | |
Matt Reichhoff | 0049a0b7 | 2021-10-20 20:44:26 | [diff] [blame] | 2137 | auto entries = |
| 2138 | net_log_observer_.GetEntriesForSource(net_log_with_source.source()); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 2139 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2140 | EXPECT_EQ(4u, entries.size()); |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 2141 | EXPECT_TRUE(LogContainsEvent( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2142 | entries, 0, NetLogEventType::TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 2143 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2144 | EXPECT_TRUE(LogContainsBeginEvent(entries, 1, NetLogEventType::SOCKET_POOL)); |
| 2145 | EXPECT_TRUE(LogContainsEvent( |
| 2146 | entries, 2, NetLogEventType::SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
| 2147 | NetLogEventPhase::NONE)); |
| 2148 | EXPECT_TRUE(LogContainsEndEvent(entries, 3, NetLogEventType::SOCKET_POOL)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2149 | } |
| 2150 | |
mmenke | 6be122f | 2015-03-09 22:22:47 | [diff] [blame] | 2151 | // Check that an async ConnectJob failure does not result in creation of a new |
| 2152 | // ConnectJob when there's another pending request also waiting on its own |
| 2153 | // ConnectJob. See http://crbug.com/463960. |
| 2154 | TEST_F(ClientSocketPoolBaseTest, AsyncFailureWithPendingRequestWithJob) { |
| 2155 | CreatePool(2, 2); |
| 2156 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
| 2157 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2158 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2159 | IsError(ERR_IO_PENDING)); |
| 2160 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2161 | IsError(ERR_IO_PENDING)); |
mmenke | 6be122f | 2015-03-09 22:22:47 | [diff] [blame] | 2162 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2163 | EXPECT_THAT(request(0)->WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 2164 | EXPECT_THAT(request(1)->WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
mmenke | 6be122f | 2015-03-09 22:22:47 | [diff] [blame] | 2165 | |
| 2166 | EXPECT_EQ(2, client_socket_factory_.allocation_count()); |
| 2167 | } |
| 2168 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2169 | TEST_F(ClientSocketPoolBaseTest, TwoRequestsCancelOne) { |
[email protected] | b22b516 | 2010-03-16 07:53:10 | [diff] [blame] | 2170 | // TODO(eroman): Add back the log expectations! Removed them because the |
| 2171 | // ordering is difficult, and some may fire during destructor. |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2172 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2173 | |
| 2174 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2175 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2176 | TestCompletionCallback callback; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2177 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2178 | TestCompletionCallback callback2; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2179 | |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2180 | EXPECT_EQ( |
| 2181 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2182 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2183 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2184 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2185 | pool_.get(), NetLogWithSource())); |
Matt Reichhoff | 0049a0b7 | 2021-10-20 20:44:26 | [diff] [blame] | 2186 | RecordingNetLogObserver log2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 2187 | EXPECT_EQ( |
| 2188 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2189 | handle2.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2190 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2191 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2192 | pool_.get(), NetLogWithSource())); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2193 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2194 | handle.Reset(); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2195 | |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 2196 | |
| 2197 | // 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] | 2198 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2199 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2200 | handle2.Reset(); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 2201 | |
| 2202 | // Now request 2 has actually finished. |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 2203 | // TODO(eroman): Add back log expectations. |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2204 | } |
| 2205 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2206 | TEST_F(ClientSocketPoolBaseTest, CancelRequestLimitsJobs) { |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 2207 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2208 | |
[email protected] | 17a0c6c | 2009-08-04 00:07:04 | [diff] [blame] | 2209 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2210 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2211 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
| 2212 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 2213 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 2214 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 2215 | |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2216 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2217 | static_cast<int>( |
| 2218 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2219 | (*requests())[2]->handle()->Reset(); |
| 2220 | (*requests())[3]->handle()->Reset(); |
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] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 2224 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2225 | (*requests())[1]->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())[0]->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 | } |
| 2235 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2236 | // When requests and ConnectJobs are not coupled, the request will get serviced |
| 2237 | // by whatever comes first. |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2238 | TEST_F(ClientSocketPoolBaseTest, ReleaseSockets) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2239 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2240 | |
| 2241 | // Start job 1 (async OK) |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 2242 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2243 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2244 | std::vector<TestSocketRequest*> request_order; |
| 2245 | size_t completion_count; // unused |
| 2246 | TestSocketRequest req1(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2247 | int rv = req1.handle()->Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2248 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2249 | ClientSocketPool::RespectLimits::ENABLED, req1.callback(), |
| 2250 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2251 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 2252 | EXPECT_THAT(req1.WaitForResult(), IsOk()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2253 | |
| 2254 | // Job 1 finished OK. Start job 2 (also async OK). Request 3 is pending |
| 2255 | // without a job. |
| 2256 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2257 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2258 | TestSocketRequest req2(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2259 | rv = req2.handle()->Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2260 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2261 | ClientSocketPool::RespectLimits::ENABLED, req2.callback(), |
| 2262 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2263 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2264 | TestSocketRequest req3(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2265 | rv = req3.handle()->Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2266 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2267 | ClientSocketPool::RespectLimits::ENABLED, req3.callback(), |
| 2268 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2269 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2270 | |
| 2271 | // Both Requests 2 and 3 are pending. We release socket 1 which should |
| 2272 | // service request 2. Request 3 should still be waiting. |
[email protected] | a6c59f6 | 2009-07-29 16:33:33 | [diff] [blame] | 2273 | req1.handle()->Reset(); |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 2274 | // Run the released socket wakeups. |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 2275 | base::RunLoop().RunUntilIdle(); |
[email protected] | a6c59f6 | 2009-07-29 16:33:33 | [diff] [blame] | 2276 | ASSERT_TRUE(req2.handle()->socket()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2277 | EXPECT_THAT(req2.WaitForResult(), IsOk()); |
[email protected] | a6c59f6 | 2009-07-29 16:33:33 | [diff] [blame] | 2278 | EXPECT_FALSE(req3.handle()->socket()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2279 | |
| 2280 | // Signal job 2, which should service request 3. |
| 2281 | |
| 2282 | client_socket_factory_.SignalJobs(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2283 | EXPECT_THAT(req3.WaitForResult(), IsOk()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2284 | |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2285 | ASSERT_EQ(3u, request_order.size()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2286 | EXPECT_EQ(&req1, request_order[0]); |
| 2287 | EXPECT_EQ(&req2, request_order[1]); |
| 2288 | EXPECT_EQ(&req3, request_order[2]); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2289 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2290 | } |
| 2291 | |
| 2292 | // The requests are not coupled to the jobs. So, the requests should finish in |
| 2293 | // their priority / insertion order. |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2294 | TEST_F(ClientSocketPoolBaseTest, PendingJobCompletionOrder) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2295 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2296 | // First two jobs are async. |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 2297 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2298 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2299 | std::vector<TestSocketRequest*> request_order; |
| 2300 | size_t completion_count; // unused |
| 2301 | TestSocketRequest req1(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2302 | int rv = req1.handle()->Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2303 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2304 | ClientSocketPool::RespectLimits::ENABLED, req1.callback(), |
| 2305 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2306 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2307 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2308 | TestSocketRequest req2(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2309 | rv = req2.handle()->Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2310 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2311 | ClientSocketPool::RespectLimits::ENABLED, req2.callback(), |
| 2312 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2313 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2314 | |
| 2315 | // The pending job is sync. |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 2316 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2317 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2318 | TestSocketRequest req3(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2319 | rv = req3.handle()->Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2320 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2321 | ClientSocketPool::RespectLimits::ENABLED, req3.callback(), |
| 2322 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2323 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2324 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2325 | EXPECT_THAT(req1.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 2326 | EXPECT_THAT(req2.WaitForResult(), IsOk()); |
| 2327 | EXPECT_THAT(req3.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2328 | |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2329 | ASSERT_EQ(3u, request_order.size()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2330 | EXPECT_EQ(&req1, request_order[0]); |
| 2331 | EXPECT_EQ(&req2, request_order[1]); |
| 2332 | EXPECT_EQ(&req3, request_order[2]); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2333 | } |
| 2334 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2335 | // Test GetLoadState in the case there's only one socket request. |
| 2336 | TEST_F(ClientSocketPoolBaseTest, LoadStateOneRequest) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2337 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2338 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2339 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2340 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2341 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2342 | int rv = handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2343 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2344 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2345 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2346 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2347 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2348 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2349 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE); |
| 2350 | EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle.GetLoadState()); |
| 2351 | |
| 2352 | // No point in completing the connection, since ClientSocketHandles only |
| 2353 | // expect the LoadState to be checked while connecting. |
| 2354 | } |
| 2355 | |
| 2356 | // Test GetLoadState in the case there are two socket requests. |
| 2357 | TEST_F(ClientSocketPoolBaseTest, LoadStateTwoRequests) { |
| 2358 | CreatePool(2, 2); |
| 2359 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2360 | |
| 2361 | ClientSocketHandle handle; |
| 2362 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2363 | int rv = handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2364 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2365 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2366 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2367 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2368 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_RESOLVING_HOST); |
| 2369 | |
| 2370 | ClientSocketHandle handle2; |
| 2371 | TestCompletionCallback callback2; |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2372 | rv = handle2.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2373 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2374 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2375 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2376 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2377 | client_socket_factory_.SetJobLoadState(1, LOAD_STATE_RESOLVING_HOST); |
| 2378 | |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 2379 | // Each handle should reflect the state of its own job. |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2380 | EXPECT_EQ(LOAD_STATE_RESOLVING_HOST, handle.GetLoadState()); |
| 2381 | EXPECT_EQ(LOAD_STATE_RESOLVING_HOST, handle2.GetLoadState()); |
| 2382 | |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 2383 | // Update the state of the first job. |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2384 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_CONNECTING); |
| 2385 | |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 2386 | // Only the state of the first request should have changed. |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2387 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2388 | EXPECT_EQ(LOAD_STATE_RESOLVING_HOST, handle2.GetLoadState()); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 2389 | |
| 2390 | // Update the state of the second job. |
| 2391 | client_socket_factory_.SetJobLoadState(1, LOAD_STATE_SSL_HANDSHAKE); |
| 2392 | |
| 2393 | // Only the state of the second request should have changed. |
| 2394 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
| 2395 | EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle2.GetLoadState()); |
| 2396 | |
| 2397 | // Second job connects and the first request gets the socket. The |
| 2398 | // second handle switches to the state of the remaining ConnectJob. |
| 2399 | client_socket_factory_.SignalJob(1); |
| 2400 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 2401 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle2.GetLoadState()); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2402 | } |
| 2403 | |
| 2404 | // Test GetLoadState in the case the per-group limit is reached. |
| 2405 | TEST_F(ClientSocketPoolBaseTest, LoadStateGroupLimit) { |
| 2406 | CreatePool(2, 1); |
| 2407 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2408 | |
| 2409 | ClientSocketHandle handle; |
| 2410 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2411 | int rv = handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2412 | TestGroupId("a"), params_, absl::nullopt, MEDIUM, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2413 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2414 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2415 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2416 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
| 2417 | |
| 2418 | // Request another socket from the same pool, buth with a higher priority. |
| 2419 | // The first request should now be stalled at the socket group limit. |
| 2420 | ClientSocketHandle handle2; |
| 2421 | TestCompletionCallback callback2; |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2422 | rv = handle2.Init(TestGroupId("a"), params_, absl::nullopt, HIGHEST, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2423 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2424 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2425 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2426 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2427 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET, handle.GetLoadState()); |
| 2428 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle2.GetLoadState()); |
| 2429 | |
| 2430 | // The first handle should remain stalled as the other socket goes through |
| 2431 | // the connect process. |
| 2432 | |
| 2433 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE); |
| 2434 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET, handle.GetLoadState()); |
| 2435 | EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle2.GetLoadState()); |
| 2436 | |
| 2437 | client_socket_factory_.SignalJob(0); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2438 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2439 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET, handle.GetLoadState()); |
| 2440 | |
| 2441 | // Closing the second socket should cause the stalled handle to finally get a |
| 2442 | // ConnectJob. |
| 2443 | handle2.socket()->Disconnect(); |
| 2444 | handle2.Reset(); |
| 2445 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
| 2446 | } |
| 2447 | |
| 2448 | // Test GetLoadState in the case the per-pool limit is reached. |
| 2449 | TEST_F(ClientSocketPoolBaseTest, LoadStatePoolLimit) { |
| 2450 | CreatePool(2, 2); |
| 2451 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2452 | |
| 2453 | ClientSocketHandle handle; |
| 2454 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2455 | int rv = handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2456 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2457 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2458 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2459 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2460 | |
| 2461 | // Request for socket from another pool. |
| 2462 | ClientSocketHandle handle2; |
| 2463 | TestCompletionCallback callback2; |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2464 | rv = handle2.Init(TestGroupId("b"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2465 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2466 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2467 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2468 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2469 | |
| 2470 | // Request another socket from the first pool. Request should stall at the |
| 2471 | // socket pool limit. |
| 2472 | ClientSocketHandle handle3; |
| 2473 | TestCompletionCallback callback3; |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2474 | rv = handle3.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2475 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2476 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2477 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2478 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2479 | |
| 2480 | // The third handle should remain stalled as the other sockets in its group |
| 2481 | // goes through the connect process. |
| 2482 | |
| 2483 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
| 2484 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL, handle3.GetLoadState()); |
| 2485 | |
| 2486 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE); |
| 2487 | EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle.GetLoadState()); |
| 2488 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL, handle3.GetLoadState()); |
| 2489 | |
| 2490 | client_socket_factory_.SignalJob(0); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2491 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2492 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL, handle3.GetLoadState()); |
| 2493 | |
| 2494 | // Closing a socket should allow the stalled handle to finally get a new |
| 2495 | // ConnectJob. |
| 2496 | handle.socket()->Disconnect(); |
| 2497 | handle.Reset(); |
| 2498 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle3.GetLoadState()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2499 | } |
| 2500 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2501 | TEST_F(ClientSocketPoolBaseTest, CertError) { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2502 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2503 | connect_job_factory_->set_job_type(TestConnectJob::kMockCertErrorJob); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2504 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2505 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2506 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2507 | EXPECT_EQ( |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2508 | ERR_CERT_COMMON_NAME_INVALID, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2509 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2510 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2511 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2512 | pool_.get(), NetLogWithSource())); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2513 | EXPECT_TRUE(handle.is_initialized()); |
| 2514 | EXPECT_TRUE(handle.socket()); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2515 | } |
| 2516 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2517 | TEST_F(ClientSocketPoolBaseTest, AsyncCertError) { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2518 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2519 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2520 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingCertErrorJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2521 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2522 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2523 | EXPECT_EQ( |
| 2524 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2525 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2526 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2527 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2528 | pool_.get(), NetLogWithSource())); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2529 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2530 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2531 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_CERT_COMMON_NAME_INVALID)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2532 | EXPECT_TRUE(handle.is_initialized()); |
| 2533 | EXPECT_TRUE(handle.socket()); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2534 | } |
| 2535 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2536 | TEST_F(ClientSocketPoolBaseTest, AdditionalErrorStateSynchronous) { |
| 2537 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2538 | connect_job_factory_->set_job_type( |
| 2539 | TestConnectJob::kMockAdditionalErrorStateJob); |
| 2540 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2541 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2542 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2543 | EXPECT_EQ( |
| 2544 | ERR_CONNECTION_FAILED, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2545 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2546 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2547 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2548 | pool_.get(), NetLogWithSource())); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2549 | EXPECT_FALSE(handle.is_initialized()); |
| 2550 | EXPECT_FALSE(handle.socket()); |
| 2551 | EXPECT_TRUE(handle.is_ssl_error()); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 2552 | EXPECT_TRUE(handle.ssl_cert_request_info()); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2553 | } |
| 2554 | |
| 2555 | TEST_F(ClientSocketPoolBaseTest, AdditionalErrorStateAsynchronous) { |
| 2556 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2557 | |
| 2558 | connect_job_factory_->set_job_type( |
| 2559 | TestConnectJob::kMockPendingAdditionalErrorStateJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2560 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2561 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2562 | EXPECT_EQ( |
| 2563 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2564 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2565 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2566 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2567 | pool_.get(), NetLogWithSource())); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2568 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2569 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2570 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2571 | EXPECT_FALSE(handle.is_initialized()); |
| 2572 | EXPECT_FALSE(handle.socket()); |
| 2573 | EXPECT_TRUE(handle.is_ssl_error()); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 2574 | EXPECT_TRUE(handle.ssl_cert_request_info()); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2575 | } |
| 2576 | |
martijn | 003cd61 | 2016-05-19 22:24:38 | [diff] [blame] | 2577 | // Make sure we can reuse sockets. |
| 2578 | TEST_F(ClientSocketPoolBaseTest, CleanupTimedOutIdleSocketsReuse) { |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2579 | CreatePoolWithIdleTimeouts( |
| 2580 | kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2581 | base::TimeDelta(), // Time out unused sockets immediately. |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 2582 | base::Days(1)); // Don't time out used sockets. |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2583 | |
| 2584 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2585 | |
| 2586 | ClientSocketHandle handle; |
| 2587 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2588 | int rv = handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2589 | TestGroupId("a"), params_, absl::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2590 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2591 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2592 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2593 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2594 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2595 | ASSERT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2596 | |
| 2597 | // Use and release the socket. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 2598 | EXPECT_EQ(1, handle.socket()->Write(nullptr, 1, CompletionOnceCallback(), |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 2599 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 2600 | TestLoadTimingInfoConnectedNotReused(handle); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2601 | handle.Reset(); |
| 2602 | |
| 2603 | // Should now have one idle socket. |
| 2604 | ASSERT_EQ(1, pool_->IdleSocketCount()); |
| 2605 | |
| 2606 | // Request a new socket. This should reuse the old socket and complete |
| 2607 | // synchronously. |
Matt Reichhoff | 0049a0b7 | 2021-10-20 20:44:26 | [diff] [blame] | 2608 | NetLogWithSource net_log_with_source = |
| 2609 | NetLogWithSource::Make(NetLogSourceType::NONE); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2610 | rv = handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2611 | TestGroupId("a"), params_, absl::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2612 | ClientSocketPool::RespectLimits::ENABLED, CompletionOnceCallback(), |
Matt Reichhoff | 0049a0b7 | 2021-10-20 20:44:26 | [diff] [blame] | 2613 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), net_log_with_source); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2614 | ASSERT_THAT(rv, IsOk()); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2615 | EXPECT_TRUE(handle.is_reused()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 2616 | TestLoadTimingInfoConnectedReused(handle); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2617 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2618 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2619 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2620 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2621 | |
Matt Reichhoff | 0049a0b7 | 2021-10-20 20:44:26 | [diff] [blame] | 2622 | auto entries = |
| 2623 | net_log_observer_.GetEntriesForSource(net_log_with_source.source()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2624 | EXPECT_TRUE(LogContainsEvent( |
| 2625 | entries, 0, NetLogEventType::TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET, |
| 2626 | NetLogEventPhase::NONE)); |
| 2627 | EXPECT_TRUE(LogContainsBeginEvent(entries, 1, NetLogEventType::SOCKET_POOL)); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2628 | EXPECT_TRUE(LogContainsEntryWithType( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2629 | entries, 2, NetLogEventType::SOCKET_POOL_REUSED_AN_EXISTING_SOCKET)); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2630 | } |
| 2631 | |
martijn | 003cd61 | 2016-05-19 22:24:38 | [diff] [blame] | 2632 | // Make sure we cleanup old unused sockets. |
Eric Roman | b49715e | 2018-04-24 22:41:17 | [diff] [blame] | 2633 | TEST_F(ClientSocketPoolBaseTest, CleanupTimedOutIdleSocketsNoReuse) { |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2634 | CreatePoolWithIdleTimeouts( |
| 2635 | kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 2636 | base::TimeDelta(), // Time out unused sockets immediately |
| 2637 | base::TimeDelta()); // Time out used sockets immediately |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2638 | |
| 2639 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2640 | |
| 2641 | // Startup two mock pending connect jobs, which will sit in the MessageLoop. |
| 2642 | |
| 2643 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2644 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2645 | int rv = handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2646 | TestGroupId("a"), params_, absl::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2647 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2648 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2649 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2650 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2651 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2652 | |
| 2653 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2654 | TestCompletionCallback callback2; |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2655 | rv = handle2.Init(TestGroupId("a"), params_, absl::nullopt, LOWEST, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2656 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2657 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2658 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2659 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2660 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2661 | pool_->GetLoadState(TestGroupId("a"), &handle2)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2662 | |
| 2663 | // Cancel one of the requests. Wait for the other, which will get the first |
| 2664 | // job. Release the socket. Run the loop again to make sure the second |
| 2665 | // socket is sitting idle and the first one is released (since ReleaseSocket() |
| 2666 | // just posts a DoReleaseSocket() task). |
| 2667 | |
| 2668 | handle.Reset(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2669 | ASSERT_THAT(callback2.WaitForResult(), IsOk()); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 2670 | // Get the NetLogSource for the socket, so the time out reason can be checked |
| 2671 | // at the end of the test. |
| 2672 | NetLogSource net_log_source2 = handle2.socket()->NetLog().source(); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2673 | // Use the socket. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 2674 | EXPECT_EQ(1, handle2.socket()->Write(nullptr, 1, CompletionOnceCallback(), |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 2675 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2676 | handle2.Reset(); |
| 2677 | |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2678 | // We post all of our delayed tasks with a 2ms delay. I.e. they don't |
| 2679 | // actually become pending until 2ms after they have been created. In order |
| 2680 | // to flush all tasks, we need to wait so that we know there are no |
| 2681 | // soon-to-be-pending tasks waiting. |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 2682 | FastForwardBy(base::Milliseconds(10)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2683 | |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2684 | // Both sockets should now be idle. |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2685 | ASSERT_EQ(2, pool_->IdleSocketCount()); |
| 2686 | |
| 2687 | // Request a new socket. This should cleanup the unused and timed out ones. |
| 2688 | // A new socket will be created rather than reusing the idle one. |
Matt Reichhoff | 0049a0b7 | 2021-10-20 20:44:26 | [diff] [blame] | 2689 | NetLogWithSource net_log_with_source = |
| 2690 | NetLogWithSource::Make(NetLogSourceType::NONE); |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2691 | TestCompletionCallback callback3; |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2692 | rv = handle.Init(TestGroupId("a"), params_, absl::nullopt, LOWEST, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2693 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2694 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
Matt Reichhoff | 0049a0b7 | 2021-10-20 20:44:26 | [diff] [blame] | 2695 | pool_.get(), net_log_with_source); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2696 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 2697 | ASSERT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2698 | EXPECT_FALSE(handle.is_reused()); |
| 2699 | |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2700 | // Make sure the idle socket is closed. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2701 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2702 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2703 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2704 | |
Matt Reichhoff | 0049a0b7 | 2021-10-20 20:44:26 | [diff] [blame] | 2705 | auto entries = |
| 2706 | net_log_observer_.GetEntriesForSource(net_log_with_source.source()); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2707 | EXPECT_FALSE(LogContainsEntryWithType( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 2708 | entries, 1, NetLogEventType::SOCKET_POOL_REUSED_AN_EXISTING_SOCKET)); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 2709 | ExpectSocketClosedWithReason( |
| 2710 | net_log_source2, TransportClientSocketPool::kIdleTimeLimitExpired); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2711 | } |
| 2712 | |
[email protected] | 2041cf34 | 2010-02-19 03:15:59 | [diff] [blame] | 2713 | // Make sure that we process all pending requests even when we're stalling |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2714 | // because of multiple releasing disconnected sockets. |
| 2715 | TEST_F(ClientSocketPoolBaseTest, MultipleReleasingDisconnectedSockets) { |
| 2716 | CreatePoolWithIdleTimeouts( |
| 2717 | kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 2718 | base::TimeDelta(), // Time out unused sockets immediately. |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 2719 | base::Days(1)); // Don't time out used sockets. |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2720 | |
| 2721 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 2722 | |
| 2723 | // Startup 4 connect jobs. Two of them will be pending. |
| 2724 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2725 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2726 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2727 | int rv = handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2728 | TestGroupId("a"), params_, absl::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2729 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2730 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2731 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2732 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2733 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2734 | TestCompletionCallback callback2; |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2735 | rv = handle2.Init(TestGroupId("a"), params_, absl::nullopt, LOWEST, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2736 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2737 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2738 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2739 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2740 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2741 | ClientSocketHandle handle3; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2742 | TestCompletionCallback callback3; |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2743 | rv = handle3.Init(TestGroupId("a"), params_, absl::nullopt, LOWEST, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2744 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2745 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2746 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2747 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2748 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2749 | ClientSocketHandle handle4; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2750 | TestCompletionCallback callback4; |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2751 | rv = handle4.Init(TestGroupId("a"), params_, absl::nullopt, LOWEST, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2752 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2753 | callback4.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2754 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2755 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2756 | |
| 2757 | // Release two disconnected sockets. |
| 2758 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2759 | handle.socket()->Disconnect(); |
| 2760 | handle.Reset(); |
| 2761 | handle2.socket()->Disconnect(); |
| 2762 | handle2.Reset(); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2763 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2764 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2765 | EXPECT_FALSE(handle3.is_reused()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2766 | EXPECT_THAT(callback4.WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2767 | EXPECT_FALSE(handle4.is_reused()); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2768 | } |
| 2769 | |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2770 | // Regression test for http://crbug.com/42267. |
| 2771 | // When DoReleaseSocket() is processed for one socket, it is blocked because the |
| 2772 | // other stalled groups all have releasing sockets, so no progress can be made. |
| 2773 | TEST_F(ClientSocketPoolBaseTest, SocketLimitReleasingSockets) { |
| 2774 | CreatePoolWithIdleTimeouts( |
| 2775 | 4 /* socket limit */, 4 /* socket limit per group */, |
| 2776 | base::TimeDelta(), // Time out unused sockets immediately. |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 2777 | base::Days(1)); // Don't time out used sockets. |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2778 | |
| 2779 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 2780 | |
| 2781 | // Max out the socket limit with 2 per group. |
| 2782 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2783 | ClientSocketHandle handle_a[4]; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2784 | TestCompletionCallback callback_a[4]; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2785 | ClientSocketHandle handle_b[4]; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2786 | TestCompletionCallback callback_b[4]; |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2787 | |
| 2788 | for (int i = 0; i < 2; ++i) { |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2789 | EXPECT_EQ(OK, handle_a[i].Init(TestGroupId("a"), params_, absl::nullopt, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2790 | LOWEST, SocketTag(), |
| 2791 | ClientSocketPool::RespectLimits::ENABLED, |
| 2792 | callback_a[i].callback(), |
| 2793 | ClientSocketPool::ProxyAuthCallback(), |
| 2794 | pool_.get(), NetLogWithSource())); |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2795 | EXPECT_EQ(OK, handle_b[i].Init(TestGroupId("b"), params_, absl::nullopt, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2796 | LOWEST, SocketTag(), |
| 2797 | ClientSocketPool::RespectLimits::ENABLED, |
| 2798 | callback_b[i].callback(), |
| 2799 | ClientSocketPool::ProxyAuthCallback(), |
| 2800 | pool_.get(), NetLogWithSource())); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2801 | } |
[email protected] | b89f7e4 | 2010-05-20 20:37:00 | [diff] [blame] | 2802 | |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2803 | // Make 4 pending requests, 2 per group. |
| 2804 | |
| 2805 | for (int i = 2; i < 4; ++i) { |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2806 | EXPECT_EQ( |
| 2807 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2808 | handle_a[i].Init(TestGroupId("a"), params_, absl::nullopt, LOWEST, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2809 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2810 | callback_a[i].callback(), |
| 2811 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2812 | NetLogWithSource())); |
| 2813 | EXPECT_EQ( |
| 2814 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2815 | handle_b[i].Init(TestGroupId("b"), params_, absl::nullopt, LOWEST, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2816 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2817 | callback_b[i].callback(), |
| 2818 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2819 | NetLogWithSource())); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2820 | } |
| 2821 | |
| 2822 | // Release b's socket first. The order is important, because in |
| 2823 | // DoReleaseSocket(), we'll process b's released socket, and since both b and |
| 2824 | // a are stalled, but 'a' is lower lexicographically, we'll process group 'a' |
| 2825 | // first, which has a releasing socket, so it refuses to start up another |
| 2826 | // ConnectJob. So, we used to infinite loop on this. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2827 | handle_b[0].socket()->Disconnect(); |
| 2828 | handle_b[0].Reset(); |
| 2829 | handle_a[0].socket()->Disconnect(); |
| 2830 | handle_a[0].Reset(); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2831 | |
| 2832 | // Used to get stuck here. |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 2833 | base::RunLoop().RunUntilIdle(); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2834 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2835 | handle_b[1].socket()->Disconnect(); |
| 2836 | handle_b[1].Reset(); |
| 2837 | handle_a[1].socket()->Disconnect(); |
| 2838 | handle_a[1].Reset(); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2839 | |
| 2840 | for (int i = 2; i < 4; ++i) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2841 | EXPECT_THAT(callback_b[i].WaitForResult(), IsOk()); |
| 2842 | EXPECT_THAT(callback_a[i].WaitForResult(), IsOk()); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2843 | } |
| 2844 | } |
| 2845 | |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2846 | TEST_F(ClientSocketPoolBaseTest, |
| 2847 | ReleasingDisconnectedSocketsMaintainsPriorityOrder) { |
| 2848 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2849 | |
| 2850 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2851 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2852 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2853 | IsError(ERR_IO_PENDING)); |
| 2854 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2855 | IsError(ERR_IO_PENDING)); |
| 2856 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2857 | IsError(ERR_IO_PENDING)); |
| 2858 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2859 | IsError(ERR_IO_PENDING)); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2860 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2861 | EXPECT_THAT((*requests())[0]->WaitForResult(), IsOk()); |
| 2862 | EXPECT_THAT((*requests())[1]->WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2863 | EXPECT_EQ(2u, completion_count()); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2864 | |
| 2865 | // Releases one connection. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2866 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::NO_KEEP_ALIVE)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2867 | EXPECT_THAT((*requests())[2]->WaitForResult(), IsOk()); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2868 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2869 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::NO_KEEP_ALIVE)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2870 | EXPECT_THAT((*requests())[3]->WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2871 | EXPECT_EQ(4u, completion_count()); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2872 | |
| 2873 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 2874 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 2875 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 2876 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 2877 | |
| 2878 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2879 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(5)); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2880 | } |
| 2881 | |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2882 | class TestReleasingSocketRequest : public TestCompletionCallbackBase { |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2883 | public: |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2884 | TestReleasingSocketRequest(TransportClientSocketPool* pool, |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2885 | int expected_result, |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2886 | bool reset_releasing_handle) |
| 2887 | : pool_(pool), |
| 2888 | expected_result_(expected_result), |
Bence Béky | 8ddc249 | 2018-06-13 01:02:04 | [diff] [blame] | 2889 | reset_releasing_handle_(reset_releasing_handle) {} |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2890 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 2891 | ~TestReleasingSocketRequest() override = default; |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2892 | |
| 2893 | ClientSocketHandle* handle() { return &handle_; } |
| 2894 | |
Bence Béky | 8ddc249 | 2018-06-13 01:02:04 | [diff] [blame] | 2895 | CompletionOnceCallback callback() { |
| 2896 | return base::BindOnce(&TestReleasingSocketRequest::OnComplete, |
| 2897 | base::Unretained(this)); |
| 2898 | } |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2899 | |
| 2900 | private: |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2901 | void OnComplete(int result) { |
| 2902 | SetResult(result); |
| 2903 | if (reset_releasing_handle_) |
| 2904 | handle_.Reset(); |
| 2905 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2906 | EXPECT_EQ( |
| 2907 | expected_result_, |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 2908 | handle2_.Init( |
| 2909 | TestGroupId("a"), |
| 2910 | ClientSocketPool::SocketParams::CreateForHttpForTesting(), |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2911 | absl::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 2912 | ClientSocketPool::RespectLimits::ENABLED, CompletionOnceCallback(), |
| 2913 | ClientSocketPool::ProxyAuthCallback(), pool_, NetLogWithSource())); |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2914 | } |
| 2915 | |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 2916 | const raw_ptr<TransportClientSocketPool> pool_; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2917 | int expected_result_; |
| 2918 | bool reset_releasing_handle_; |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2919 | ClientSocketHandle handle_; |
| 2920 | ClientSocketHandle handle2_; |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2921 | }; |
| 2922 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2923 | |
| 2924 | TEST_F(ClientSocketPoolBaseTest, AdditionalErrorSocketsDontUseSlot) { |
| 2925 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2926 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2927 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
| 2928 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 2929 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2930 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2931 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2932 | client_socket_factory_.allocation_count()); |
| 2933 | |
| 2934 | connect_job_factory_->set_job_type( |
| 2935 | TestConnectJob::kMockPendingAdditionalErrorStateJob); |
| 2936 | TestReleasingSocketRequest req(pool_.get(), OK, false); |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2937 | EXPECT_EQ(ERR_IO_PENDING, |
| 2938 | req.handle()->Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2939 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2940 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2941 | req.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2942 | pool_.get(), NetLogWithSource())); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2943 | // The next job should complete synchronously |
| 2944 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 2945 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2946 | EXPECT_THAT(req.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2947 | EXPECT_FALSE(req.handle()->is_initialized()); |
| 2948 | EXPECT_FALSE(req.handle()->socket()); |
| 2949 | EXPECT_TRUE(req.handle()->is_ssl_error()); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 2950 | EXPECT_TRUE(req.handle()->ssl_cert_request_info()); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2951 | } |
| 2952 | |
[email protected] | b6501d3d | 2010-06-03 23:53:34 | [diff] [blame] | 2953 | // http://crbug.com/44724 regression test. |
| 2954 | // We start releasing the pool when we flush on network change. When that |
| 2955 | // happens, the only active references are in the ClientSocketHandles. When a |
| 2956 | // ConnectJob completes and calls back into the last ClientSocketHandle, that |
| 2957 | // callback can release the last reference and delete the pool. After the |
| 2958 | // callback finishes, we go back to the stack frame within the now-deleted pool. |
| 2959 | // Executing any code that refers to members of the now-deleted pool can cause |
| 2960 | // crashes. |
| 2961 | TEST_F(ClientSocketPoolBaseTest, CallbackThatReleasesPool) { |
| 2962 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2963 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
| 2964 | |
| 2965 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2966 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2967 | EXPECT_EQ( |
| 2968 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2969 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2970 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2971 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2972 | pool_.get(), NetLogWithSource())); |
[email protected] | b6501d3d | 2010-06-03 23:53:34 | [diff] [blame] | 2973 | |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 2974 | pool_->FlushWithError(ERR_NETWORK_CHANGED, "Network changed"); |
[email protected] | b6501d3d | 2010-06-03 23:53:34 | [diff] [blame] | 2975 | |
| 2976 | // We'll call back into this now. |
| 2977 | callback.WaitForResult(); |
| 2978 | } |
| 2979 | |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2980 | TEST_F(ClientSocketPoolBaseTest, DoNotReuseSocketAfterFlush) { |
| 2981 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2982 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2983 | |
| 2984 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2985 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2986 | EXPECT_EQ( |
| 2987 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 2988 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2989 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2990 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2991 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2992 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2993 | EXPECT_EQ(ClientSocketHandle::UNUSED, handle.reuse_type()); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 2994 | NetLogSource source = handle.socket()->NetLog().source(); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2995 | |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 2996 | pool_->FlushWithError(ERR_NETWORK_CHANGED, "Network changed"); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2997 | |
| 2998 | handle.Reset(); |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 2999 | base::RunLoop().RunUntilIdle(); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 3000 | |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3001 | EXPECT_EQ( |
| 3002 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3003 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3004 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 3005 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3006 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3007 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 3008 | EXPECT_EQ(ClientSocketHandle::UNUSED, handle.reuse_type()); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 3009 | |
| 3010 | ExpectSocketClosedWithReason( |
| 3011 | source, TransportClientSocketPool::kSocketGenerationOutOfDate); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 3012 | } |
| 3013 | |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3014 | class ConnectWithinCallback : public TestCompletionCallbackBase { |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 3015 | public: |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3016 | ConnectWithinCallback( |
| 3017 | const ClientSocketPool::GroupId& group_id, |
Matt Menke | 84d11e56 | 2019-03-27 00:11:19 | [diff] [blame] | 3018 | const scoped_refptr<ClientSocketPool::SocketParams>& params, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3019 | TransportClientSocketPool* pool) |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3020 | : group_id_(group_id), params_(params), pool_(pool) {} |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 3021 | |
Peter Boström | 293b134 | 2021-09-22 17:31:43 | [diff] [blame] | 3022 | ConnectWithinCallback(const ConnectWithinCallback&) = delete; |
| 3023 | ConnectWithinCallback& operator=(const ConnectWithinCallback&) = delete; |
| 3024 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 3025 | ~ConnectWithinCallback() override = default; |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 3026 | |
| 3027 | int WaitForNestedResult() { |
| 3028 | return nested_callback_.WaitForResult(); |
| 3029 | } |
| 3030 | |
Bence Béky | 8ddc249 | 2018-06-13 01:02:04 | [diff] [blame] | 3031 | CompletionOnceCallback callback() { |
| 3032 | return base::BindOnce(&ConnectWithinCallback::OnComplete, |
| 3033 | base::Unretained(this)); |
| 3034 | } |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3035 | |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 3036 | private: |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3037 | void OnComplete(int result) { |
| 3038 | SetResult(result); |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3039 | EXPECT_EQ( |
| 3040 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3041 | handle_.Init(group_id_, params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3042 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 3043 | nested_callback_.callback(), |
| 3044 | ClientSocketPool::ProxyAuthCallback(), pool_, |
| 3045 | NetLogWithSource())); |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3046 | } |
| 3047 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3048 | const ClientSocketPool::GroupId group_id_; |
Matt Menke | 84d11e56 | 2019-03-27 00:11:19 | [diff] [blame] | 3049 | const scoped_refptr<ClientSocketPool::SocketParams> params_; |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 3050 | const raw_ptr<TransportClientSocketPool> pool_; |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 3051 | ClientSocketHandle handle_; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3052 | TestCompletionCallback nested_callback_; |
[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); |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 3094 | FastForwardBy( |
| 3095 | base::Milliseconds(ClientSocketPool::kMaxConnectRetryIntervalMs * 100)); |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 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); |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 3101 | FastForwardBy( |
| 3102 | base::Milliseconds(ClientSocketPool::kMaxConnectRetryIntervalMs)); |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 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); |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 3124 | FastForwardBy( |
| 3125 | base::Milliseconds(ClientSocketPool::kMaxConnectRetryIntervalMs * 100)); |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 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); |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 3130 | FastForwardBy( |
| 3131 | base::Milliseconds(ClientSocketPool::kMaxConnectRetryIntervalMs * 100)); |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 3132 | EXPECT_EQ(1, client_socket_factory_.allocation_count()); |
| 3133 | } |
| 3134 | |
[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) { |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3158 | EXPECT_EQ(OK, handles[i].Init(TestGroupId("bar"), params_, absl::nullopt, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3159 | DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3160 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3161 | callback.callback(), |
| 3162 | ClientSocketPool::ProxyAuthCallback(), |
| 3163 | pool_.get(), NetLogWithSource())); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3164 | } |
| 3165 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3166 | base::RunLoop().RunUntilIdle(); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3167 | |
| 3168 | // Cancel the pending request. |
| 3169 | handle.Reset(); |
| 3170 | |
| 3171 | // Wait for the backup timer to fire (add some slop to ensure it fires) |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 3172 | FastForwardBy( |
| 3173 | base::Milliseconds(ClientSocketPool::kMaxConnectRetryIntervalMs / 2 * 3)); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3174 | |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3175 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 3176 | } |
| 3177 | |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3178 | TEST_F(ClientSocketPoolBaseTest, CancelBackupSocketAfterCancelingAllRequests) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3179 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets, |
| 3180 | true /* enable_backup_connect_jobs */); |
[email protected] | 4baaf9d | 2010-08-31 15:15:44 | [diff] [blame] | 3181 | |
| 3182 | // Create the first socket and set to ERR_IO_PENDING. This starts the backup |
| 3183 | // timer. |
| 3184 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3185 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3186 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3187 | EXPECT_EQ( |
| 3188 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3189 | handle.Init(TestGroupId("bar"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3190 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 3191 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3192 | pool_.get(), NetLogWithSource())); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3193 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("bar"))); |
| 3194 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("bar"))); |
| 3195 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3196 | TestGroupId("bar"))); |
| 3197 | EXPECT_EQ( |
| 3198 | 0u, pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("bar"))); |
[email protected] | 4baaf9d | 2010-08-31 15:15:44 | [diff] [blame] | 3199 | |
| 3200 | // Cancel the socket request. This should cancel the backup timer. Wait for |
| 3201 | // the backup time to see if it indeed got canceled. |
| 3202 | handle.Reset(); |
| 3203 | // Wait for the backup timer to fire (add some slop to ensure it fires) |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 3204 | FastForwardBy( |
| 3205 | base::Milliseconds(ClientSocketPool::kMaxConnectRetryIntervalMs / 2 * 3)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3206 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("bar"))); |
| 3207 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("bar"))); |
[email protected] | 4baaf9d | 2010-08-31 15:15:44 | [diff] [blame] | 3208 | } |
| 3209 | |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3210 | TEST_F(ClientSocketPoolBaseTest, CancelBackupSocketAfterFinishingAllRequests) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3211 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets, |
| 3212 | true /* enable_backup_connect_jobs */); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3213 | |
| 3214 | // Create the first socket and set to ERR_IO_PENDING. This starts the backup |
| 3215 | // timer. |
| 3216 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3217 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3218 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3219 | EXPECT_EQ( |
| 3220 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3221 | handle.Init(TestGroupId("bar"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3222 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 3223 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3224 | pool_.get(), NetLogWithSource())); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3225 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3226 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3227 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3228 | EXPECT_EQ( |
| 3229 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3230 | handle2.Init(TestGroupId("bar"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3231 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3232 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3233 | pool_.get(), NetLogWithSource())); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3234 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("bar"))); |
| 3235 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("bar"))); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3236 | |
| 3237 | // Cancel request 1 and then complete request 2. With the requests finished, |
| 3238 | // the backup timer should be cancelled. |
| 3239 | handle.Reset(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3240 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3241 | // Wait for the backup timer to fire (add some slop to ensure it fires) |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 3242 | FastForwardBy( |
| 3243 | base::Milliseconds(ClientSocketPool::kMaxConnectRetryIntervalMs / 2 * 3)); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3244 | } |
| 3245 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3246 | // Test delayed socket binding for the case where we have two connects, |
| 3247 | // and while one is waiting on a connect, the other frees up. |
| 3248 | // The socket waiting on a connect should switch immediately to the freed |
| 3249 | // up socket. |
| 3250 | TEST_F(ClientSocketPoolBaseTest, DelayedSocketBindingWaitingForConnect) { |
| 3251 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3252 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3253 | |
| 3254 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3255 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3256 | EXPECT_EQ( |
| 3257 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3258 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3259 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3260 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3261 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3262 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3263 | |
| 3264 | // No idle sockets, no pending jobs. |
| 3265 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3266 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3267 | |
| 3268 | // Create a second socket to the same host, but this one will wait. |
| 3269 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3270 | ClientSocketHandle handle2; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3271 | EXPECT_EQ( |
| 3272 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3273 | handle2.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3274 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3275 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3276 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3277 | // No idle sockets, and one connecting job. |
| 3278 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3279 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3280 | |
| 3281 | // Return the first handle to the pool. This will initiate the delayed |
| 3282 | // binding. |
| 3283 | handle1.Reset(); |
| 3284 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3285 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3286 | |
| 3287 | // Still no idle sockets, still one pending connect job. |
| 3288 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3289 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3290 | |
| 3291 | // The second socket connected, even though it was a Waiting Job. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3292 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3293 | |
| 3294 | // And we can see there is still one job waiting. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3295 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3296 | |
| 3297 | // Finally, signal the waiting Connect. |
| 3298 | client_socket_factory_.SignalJobs(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3299 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3300 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3301 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3302 | } |
| 3303 | |
| 3304 | // Test delayed socket binding when a group is at capacity and one |
| 3305 | // of the group's sockets frees up. |
| 3306 | TEST_F(ClientSocketPoolBaseTest, DelayedSocketBindingAtGroupCapacity) { |
| 3307 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3308 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3309 | |
| 3310 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3311 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3312 | EXPECT_EQ( |
| 3313 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3314 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3315 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3316 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3317 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3318 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3319 | |
| 3320 | // No idle sockets, no pending jobs. |
| 3321 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3322 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3323 | |
| 3324 | // Create a second socket to the same host, but this one will wait. |
| 3325 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3326 | ClientSocketHandle handle2; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3327 | EXPECT_EQ( |
| 3328 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3329 | handle2.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3330 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3331 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3332 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3333 | // No idle sockets, and one connecting job. |
| 3334 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3335 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3336 | |
| 3337 | // Return the first handle to the pool. This will initiate the delayed |
| 3338 | // binding. |
| 3339 | handle1.Reset(); |
| 3340 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3341 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3342 | |
| 3343 | // Still no idle sockets, still one pending connect job. |
| 3344 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3345 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3346 | |
| 3347 | // The second socket connected, even though it was a Waiting Job. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3348 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3349 | |
| 3350 | // And we can see there is still one job waiting. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3351 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3352 | |
| 3353 | // Finally, signal the waiting Connect. |
| 3354 | client_socket_factory_.SignalJobs(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3355 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3356 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3357 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3358 | } |
| 3359 | |
| 3360 | // Test out the case where we have one socket connected, one |
| 3361 | // connecting, when the first socket finishes and goes idle. |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3362 | // Although the second connection is pending, the second request |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3363 | // should complete, by taking the first socket's idle socket. |
| 3364 | TEST_F(ClientSocketPoolBaseTest, DelayedSocketBindingAtStall) { |
| 3365 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3366 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3367 | |
| 3368 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3369 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3370 | EXPECT_EQ( |
| 3371 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3372 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3373 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3374 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3375 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3376 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3377 | |
| 3378 | // No idle sockets, no pending jobs. |
| 3379 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3380 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3381 | |
| 3382 | // Create a second socket to the same host, but this one will wait. |
| 3383 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3384 | ClientSocketHandle handle2; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3385 | EXPECT_EQ( |
| 3386 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3387 | handle2.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3388 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3389 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3390 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3391 | // No idle sockets, and one connecting job. |
| 3392 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3393 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3394 | |
| 3395 | // Return the first handle to the pool. This will initiate the delayed |
| 3396 | // binding. |
| 3397 | handle1.Reset(); |
| 3398 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3399 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3400 | |
| 3401 | // Still no idle sockets, still one pending connect job. |
| 3402 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3403 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3404 | |
| 3405 | // The second socket connected, even though it was a Waiting Job. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3406 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3407 | |
| 3408 | // And we can see there is still one job waiting. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3409 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3410 | |
| 3411 | // Finally, signal the waiting Connect. |
| 3412 | client_socket_factory_.SignalJobs(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3413 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3414 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3415 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3416 | } |
| 3417 | |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3418 | // Cover the case where on an available socket slot, we have one pending |
| 3419 | // request that completes synchronously, thereby making the Group empty. |
| 3420 | TEST_F(ClientSocketPoolBaseTest, SynchronouslyProcessOnePendingRequest) { |
| 3421 | const int kUnlimitedSockets = 100; |
| 3422 | const int kOneSocketPerGroup = 1; |
| 3423 | CreatePool(kUnlimitedSockets, kOneSocketPerGroup); |
| 3424 | |
| 3425 | // Make the first request asynchronous fail. |
| 3426 | // This will free up a socket slot later. |
| 3427 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
| 3428 | |
| 3429 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3430 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3431 | EXPECT_EQ( |
| 3432 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3433 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3434 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3435 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3436 | pool_.get(), NetLogWithSource())); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3437 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3438 | |
| 3439 | // Make the second request synchronously fail. This should make the Group |
| 3440 | // empty. |
| 3441 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
| 3442 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3443 | TestCompletionCallback callback2; |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3444 | // It'll be ERR_IO_PENDING now, but the TestConnectJob will synchronously fail |
| 3445 | // when created. |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3446 | EXPECT_EQ( |
| 3447 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3448 | handle2.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3449 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3450 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3451 | pool_.get(), NetLogWithSource())); |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3452 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3453 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3454 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3455 | EXPECT_THAT(callback1.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 3456 | EXPECT_THAT(callback2.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3457 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3458 | } |
| 3459 | |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3460 | TEST_F(ClientSocketPoolBaseTest, PreferUsedSocketToUnusedSocket) { |
| 3461 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 3462 | |
| 3463 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3464 | |
| 3465 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3466 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3467 | EXPECT_EQ( |
| 3468 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3469 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3470 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3471 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3472 | pool_.get(), NetLogWithSource())); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3473 | |
| 3474 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3475 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3476 | EXPECT_EQ( |
| 3477 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3478 | handle2.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3479 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3480 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3481 | pool_.get(), NetLogWithSource())); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3482 | ClientSocketHandle handle3; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3483 | TestCompletionCallback callback3; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3484 | EXPECT_EQ( |
| 3485 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3486 | handle3.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3487 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3488 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3489 | pool_.get(), NetLogWithSource())); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3490 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3491 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3492 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
| 3493 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3494 | |
| 3495 | // Use the socket. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 3496 | EXPECT_EQ(1, handle1.socket()->Write(nullptr, 1, CompletionOnceCallback(), |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 3497 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 3498 | EXPECT_EQ(1, handle3.socket()->Write(nullptr, 1, CompletionOnceCallback(), |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 3499 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3500 | |
| 3501 | handle1.Reset(); |
| 3502 | handle2.Reset(); |
| 3503 | handle3.Reset(); |
| 3504 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3505 | EXPECT_EQ(OK, handle1.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3506 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3507 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3508 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3509 | pool_.get(), NetLogWithSource())); |
| 3510 | EXPECT_EQ(OK, handle2.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3511 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3512 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3513 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3514 | pool_.get(), NetLogWithSource())); |
| 3515 | EXPECT_EQ(OK, handle3.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3516 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3517 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3518 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3519 | pool_.get(), NetLogWithSource())); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3520 | |
| 3521 | EXPECT_TRUE(handle1.socket()->WasEverUsed()); |
| 3522 | EXPECT_TRUE(handle2.socket()->WasEverUsed()); |
| 3523 | EXPECT_FALSE(handle3.socket()->WasEverUsed()); |
| 3524 | } |
| 3525 | |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3526 | TEST_F(ClientSocketPoolBaseTest, RequestSockets) { |
| 3527 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3528 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3529 | |
Tsuyoshi Horo | 1d8eb53f | 2022-06-02 07:12:21 | [diff] [blame] | 3530 | TestCompletionCallback preconnect_callback; |
| 3531 | EXPECT_EQ(ERR_IO_PENDING, |
| 3532 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 2, |
| 3533 | preconnect_callback.callback(), |
| 3534 | NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3535 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3536 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3537 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3538 | EXPECT_EQ(2u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3539 | TestGroupId("a"))); |
| 3540 | EXPECT_EQ(2u, |
| 3541 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3542 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3543 | |
| 3544 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3545 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3546 | EXPECT_EQ( |
| 3547 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3548 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3549 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3550 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3551 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3552 | |
| 3553 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3554 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3555 | EXPECT_EQ( |
| 3556 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3557 | handle2.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3558 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3559 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3560 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3561 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3562 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3563 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3564 | TestGroupId("a"))); |
| 3565 | EXPECT_EQ(0u, |
| 3566 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3567 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3568 | |
Tsuyoshi Horo | 1d8eb53f | 2022-06-02 07:12:21 | [diff] [blame] | 3569 | EXPECT_THAT(preconnect_callback.WaitForResult(), IsOk()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3570 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3571 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3572 | handle1.Reset(); |
| 3573 | handle2.Reset(); |
| 3574 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3575 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3576 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3577 | TestGroupId("a"))); |
| 3578 | EXPECT_EQ(0u, |
| 3579 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3580 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3581 | } |
| 3582 | |
| 3583 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsWhenAlreadyHaveAConnectJob) { |
| 3584 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3585 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3586 | |
| 3587 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3588 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3589 | EXPECT_EQ( |
| 3590 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3591 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3592 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3593 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3594 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3595 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3596 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3597 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3598 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3599 | TestGroupId("a"))); |
| 3600 | EXPECT_EQ(0u, |
| 3601 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3602 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3603 | |
Tsuyoshi Horo | 1d8eb53f | 2022-06-02 07:12:21 | [diff] [blame] | 3604 | TestCompletionCallback preconnect_callback; |
| 3605 | EXPECT_EQ(ERR_IO_PENDING, |
| 3606 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 2, |
| 3607 | preconnect_callback.callback(), |
| 3608 | NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3609 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3610 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3611 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3612 | TestGroupId("a"))); |
| 3613 | EXPECT_EQ(1u, |
| 3614 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3615 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3616 | |
| 3617 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3618 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3619 | EXPECT_EQ( |
| 3620 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3621 | handle2.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3622 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3623 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3624 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3625 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3626 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3627 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3628 | TestGroupId("a"))); |
| 3629 | EXPECT_EQ(0u, |
| 3630 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3631 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3632 | |
Tsuyoshi Horo | 1d8eb53f | 2022-06-02 07:12:21 | [diff] [blame] | 3633 | EXPECT_THAT(preconnect_callback.WaitForResult(), IsOk()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3634 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3635 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3636 | handle1.Reset(); |
| 3637 | handle2.Reset(); |
| 3638 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3639 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3640 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3641 | TestGroupId("a"))); |
| 3642 | EXPECT_EQ(0u, |
| 3643 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3644 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3645 | } |
| 3646 | |
| 3647 | TEST_F(ClientSocketPoolBaseTest, |
| 3648 | RequestSocketsWhenAlreadyHaveMultipleConnectJob) { |
| 3649 | CreatePool(4, 4); |
| 3650 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3651 | |
| 3652 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3653 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3654 | EXPECT_EQ( |
| 3655 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3656 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3657 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3658 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3659 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3660 | |
| 3661 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3662 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3663 | EXPECT_EQ( |
| 3664 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3665 | handle2.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3666 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3667 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3668 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3669 | |
| 3670 | ClientSocketHandle handle3; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3671 | TestCompletionCallback callback3; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3672 | EXPECT_EQ( |
| 3673 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3674 | handle3.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3675 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3676 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3677 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3678 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3679 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3680 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3681 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3682 | TestGroupId("a"))); |
| 3683 | EXPECT_EQ(0u, |
| 3684 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3685 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3686 | |
Tsuyoshi Horo | 1d8eb53f | 2022-06-02 07:12:21 | [diff] [blame] | 3687 | EXPECT_EQ( |
| 3688 | OK, pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 2, |
| 3689 | CompletionOnceCallback(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3690 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3691 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3692 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3693 | TestGroupId("a"))); |
| 3694 | EXPECT_EQ(0u, |
| 3695 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3696 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3697 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3698 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3699 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
| 3700 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3701 | handle1.Reset(); |
| 3702 | handle2.Reset(); |
| 3703 | handle3.Reset(); |
| 3704 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3705 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3706 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3707 | TestGroupId("a"))); |
| 3708 | EXPECT_EQ(0u, |
| 3709 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3710 | EXPECT_EQ(3u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3711 | } |
| 3712 | |
| 3713 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsAtMaxSocketLimit) { |
| 3714 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 3715 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3716 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3717 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3718 | |
Tsuyoshi Horo | 1d8eb53f | 2022-06-02 07:12:21 | [diff] [blame] | 3719 | TestCompletionCallback preconnect_callback; |
| 3720 | EXPECT_EQ(ERR_IO_PENDING, |
| 3721 | pool_->RequestSockets( |
| 3722 | TestGroupId("a"), params_, absl::nullopt, kDefaultMaxSockets, |
| 3723 | preconnect_callback.callback(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3724 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3725 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3726 | EXPECT_EQ(kDefaultMaxSockets, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3727 | static_cast<int>( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3728 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
| 3729 | EXPECT_EQ( |
| 3730 | kDefaultMaxSockets, |
| 3731 | static_cast<int>(pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3732 | TestGroupId("a")))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3733 | EXPECT_EQ(kDefaultMaxSockets, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3734 | static_cast<int>(pool_->NumUnassignedConnectJobsInGroupForTesting( |
| 3735 | TestGroupId("a")))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3736 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3737 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("b"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3738 | |
Tsuyoshi Horo | 1d8eb53f | 2022-06-02 07:12:21 | [diff] [blame] | 3739 | EXPECT_EQ(OK, |
| 3740 | pool_->RequestSockets(TestGroupId("b"), params_, absl::nullopt, |
| 3741 | kDefaultMaxSockets, CompletionOnceCallback(), |
| 3742 | NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3743 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3744 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("b"))); |
Tsuyoshi Horo | 1d8eb53f | 2022-06-02 07:12:21 | [diff] [blame] | 3745 | |
| 3746 | EXPECT_THAT(preconnect_callback.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3747 | } |
| 3748 | |
| 3749 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsHitMaxSocketLimit) { |
| 3750 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 3751 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3752 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3753 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3754 | |
Tsuyoshi Horo | 1d8eb53f | 2022-06-02 07:12:21 | [diff] [blame] | 3755 | TestCompletionCallback preconnect_callback1; |
| 3756 | EXPECT_EQ(ERR_IO_PENDING, |
| 3757 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, |
| 3758 | kDefaultMaxSockets - 1, |
| 3759 | preconnect_callback1.callback(), |
| 3760 | NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3761 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3762 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3763 | EXPECT_EQ(kDefaultMaxSockets - 1, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3764 | static_cast<int>( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3765 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
| 3766 | EXPECT_EQ( |
| 3767 | kDefaultMaxSockets - 1, |
| 3768 | static_cast<int>(pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3769 | TestGroupId("a")))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3770 | EXPECT_EQ(kDefaultMaxSockets - 1, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3771 | static_cast<int>(pool_->NumUnassignedConnectJobsInGroupForTesting( |
| 3772 | TestGroupId("a")))); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 3773 | EXPECT_FALSE(pool_->IsStalled()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3774 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3775 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("b"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3776 | |
Tsuyoshi Horo | 1d8eb53f | 2022-06-02 07:12:21 | [diff] [blame] | 3777 | TestCompletionCallback preconnect_callback2; |
| 3778 | EXPECT_EQ(ERR_IO_PENDING, |
| 3779 | pool_->RequestSockets( |
| 3780 | TestGroupId("b"), params_, absl::nullopt, kDefaultMaxSockets, |
| 3781 | preconnect_callback2.callback(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3782 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3783 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("b"))); |
| 3784 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 3785 | EXPECT_FALSE(pool_->IsStalled()); |
Tsuyoshi Horo | 1d8eb53f | 2022-06-02 07:12:21 | [diff] [blame] | 3786 | |
| 3787 | EXPECT_THAT(preconnect_callback1.WaitForResult(), IsOk()); |
| 3788 | EXPECT_THAT(preconnect_callback2.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3789 | } |
| 3790 | |
| 3791 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsCountIdleSockets) { |
| 3792 | CreatePool(4, 4); |
| 3793 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3794 | |
| 3795 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3796 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3797 | EXPECT_EQ( |
| 3798 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3799 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3800 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3801 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3802 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3803 | ASSERT_THAT(callback1.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3804 | handle1.Reset(); |
| 3805 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3806 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3807 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3808 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3809 | TestGroupId("a"))); |
| 3810 | EXPECT_EQ(0u, |
| 3811 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3812 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3813 | |
Tsuyoshi Horo | 1d8eb53f | 2022-06-02 07:12:21 | [diff] [blame] | 3814 | TestCompletionCallback preconnect_callback; |
| 3815 | EXPECT_EQ(ERR_IO_PENDING, |
| 3816 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 2, |
| 3817 | preconnect_callback.callback(), |
| 3818 | NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3819 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3820 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3821 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3822 | TestGroupId("a"))); |
| 3823 | EXPECT_EQ(1u, |
| 3824 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3825 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Tsuyoshi Horo | 1d8eb53f | 2022-06-02 07:12:21 | [diff] [blame] | 3826 | |
| 3827 | EXPECT_THAT(preconnect_callback.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3828 | } |
| 3829 | |
| 3830 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsCountActiveSockets) { |
| 3831 | CreatePool(4, 4); |
| 3832 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3833 | |
| 3834 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3835 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3836 | EXPECT_EQ( |
| 3837 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3838 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3839 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3840 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3841 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3842 | ASSERT_THAT(callback1.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3843 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3844 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3845 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3846 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3847 | TestGroupId("a"))); |
| 3848 | EXPECT_EQ(0u, |
| 3849 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3850 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3851 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3852 | |
Tsuyoshi Horo | 1d8eb53f | 2022-06-02 07:12:21 | [diff] [blame] | 3853 | TestCompletionCallback preconnect_callback; |
| 3854 | EXPECT_EQ(ERR_IO_PENDING, |
| 3855 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 2, |
| 3856 | preconnect_callback.callback(), |
| 3857 | NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3858 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3859 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3860 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3861 | TestGroupId("a"))); |
| 3862 | EXPECT_EQ(1u, |
| 3863 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3864 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3865 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Tsuyoshi Horo | 1d8eb53f | 2022-06-02 07:12:21 | [diff] [blame] | 3866 | |
| 3867 | EXPECT_THAT(preconnect_callback.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3868 | } |
| 3869 | |
| 3870 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsSynchronous) { |
| 3871 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3872 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 3873 | |
Tsuyoshi Horo | 1d8eb53f | 2022-06-02 07:12:21 | [diff] [blame] | 3874 | EXPECT_EQ( |
| 3875 | OK, pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, |
| 3876 | kDefaultMaxSocketsPerGroup, |
| 3877 | CompletionOnceCallback(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3878 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3879 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3880 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3881 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3882 | TestGroupId("a"))); |
| 3883 | EXPECT_EQ(0u, |
| 3884 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3885 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3886 | static_cast<int>(pool_->IdleSocketCountInGroup(TestGroupId("a")))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3887 | |
Tsuyoshi Horo | 1d8eb53f | 2022-06-02 07:12:21 | [diff] [blame] | 3888 | EXPECT_EQ( |
| 3889 | OK, pool_->RequestSockets(TestGroupId("b"), params_, absl::nullopt, |
| 3890 | kDefaultMaxSocketsPerGroup, |
| 3891 | CompletionOnceCallback(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3892 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3893 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 3894 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3895 | TestGroupId("b"))); |
| 3896 | EXPECT_EQ(0u, |
| 3897 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3898 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3899 | static_cast<int>(pool_->IdleSocketCountInGroup(TestGroupId("b")))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3900 | } |
| 3901 | |
[email protected] | 3c819f52 | 2010-12-02 02:03:12 | [diff] [blame] | 3902 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsSynchronousError) { |
| 3903 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3904 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
| 3905 | |
Tsuyoshi Horo | 1d8eb53f | 2022-06-02 07:12:21 | [diff] [blame] | 3906 | EXPECT_EQ( |
| 3907 | OK, pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, |
| 3908 | kDefaultMaxSocketsPerGroup, |
| 3909 | CompletionOnceCallback(), NetLogWithSource())); |
[email protected] | 3c819f52 | 2010-12-02 02:03:12 | [diff] [blame] | 3910 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3911 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | fd2e53e | 2011-01-14 20:40:52 | [diff] [blame] | 3912 | |
| 3913 | connect_job_factory_->set_job_type( |
| 3914 | TestConnectJob::kMockAdditionalErrorStateJob); |
Tsuyoshi Horo | 1d8eb53f | 2022-06-02 07:12:21 | [diff] [blame] | 3915 | |
| 3916 | EXPECT_EQ( |
| 3917 | OK, pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, |
| 3918 | kDefaultMaxSocketsPerGroup, |
| 3919 | CompletionOnceCallback(), NetLogWithSource())); |
[email protected] | fd2e53e | 2011-01-14 20:40:52 | [diff] [blame] | 3920 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3921 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | 3c819f52 | 2010-12-02 02:03:12 | [diff] [blame] | 3922 | } |
| 3923 | |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3924 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsMultipleTimesDoesNothing) { |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3925 | CreatePool(4, 4); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3926 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3927 | |
Tsuyoshi Horo | 1d8eb53f | 2022-06-02 07:12:21 | [diff] [blame] | 3928 | TestCompletionCallback preconnect_callback; |
| 3929 | EXPECT_EQ(ERR_IO_PENDING, |
| 3930 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 2, |
| 3931 | preconnect_callback.callback(), |
| 3932 | NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3933 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3934 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3935 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3936 | EXPECT_EQ(2u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3937 | TestGroupId("a"))); |
| 3938 | EXPECT_EQ(2u, |
| 3939 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3940 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3941 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3942 | |
Tsuyoshi Horo | 1d8eb53f | 2022-06-02 07:12:21 | [diff] [blame] | 3943 | EXPECT_EQ( |
| 3944 | OK, pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 2, |
| 3945 | CompletionOnceCallback(), NetLogWithSource())); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3946 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3947 | EXPECT_EQ(2u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3948 | TestGroupId("a"))); |
| 3949 | EXPECT_EQ(2u, |
| 3950 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3951 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3952 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3953 | |
| 3954 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3955 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3956 | EXPECT_EQ( |
| 3957 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3958 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3959 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3960 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3961 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3962 | |
| 3963 | client_socket_factory_.SignalJob(0); |
| 3964 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3965 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3966 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3967 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3968 | TestGroupId("a"))); |
| 3969 | EXPECT_EQ(1u, |
| 3970 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3971 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3972 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3973 | |
| 3974 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3975 | TestCompletionCallback callback2; |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3976 | EXPECT_EQ( |
| 3977 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 3978 | handle2.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3979 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3980 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3981 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3982 | client_socket_factory_.SignalJob(0); |
| 3983 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
Tsuyoshi Horo | 1d8eb53f | 2022-06-02 07:12:21 | [diff] [blame] | 3984 | EXPECT_THAT(preconnect_callback.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3985 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3986 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3987 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3988 | TestGroupId("a"))); |
| 3989 | EXPECT_EQ(0u, |
| 3990 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3991 | EXPECT_EQ(2, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3992 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3993 | |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3994 | handle1.Reset(); |
| 3995 | handle2.Reset(); |
| 3996 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3997 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3998 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3999 | TestGroupId("a"))); |
| 4000 | EXPECT_EQ(0u, |
| 4001 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4002 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4003 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4004 | |
Tsuyoshi Horo | 1d8eb53f | 2022-06-02 07:12:21 | [diff] [blame] | 4005 | EXPECT_EQ( |
| 4006 | OK, pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 2, |
| 4007 | CompletionOnceCallback(), NetLogWithSource())); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4008 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4009 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4010 | TestGroupId("a"))); |
| 4011 | EXPECT_EQ(0u, |
| 4012 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4013 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4014 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4015 | } |
| 4016 | |
| 4017 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsDifferentNumSockets) { |
| 4018 | CreatePool(4, 4); |
| 4019 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 4020 | |
Tsuyoshi Horo | 1d8eb53f | 2022-06-02 07:12:21 | [diff] [blame] | 4021 | TestCompletionCallback preconnect_callback1; |
| 4022 | EXPECT_EQ(ERR_IO_PENDING, |
| 4023 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 1, |
| 4024 | preconnect_callback1.callback(), |
| 4025 | NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4026 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4027 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 4028 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4029 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4030 | TestGroupId("a"))); |
| 4031 | EXPECT_EQ(1u, |
| 4032 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4033 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4034 | |
Tsuyoshi Horo | 1d8eb53f | 2022-06-02 07:12:21 | [diff] [blame] | 4035 | TestCompletionCallback preconnect_callback2; |
| 4036 | EXPECT_EQ(ERR_IO_PENDING, |
| 4037 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 2, |
| 4038 | preconnect_callback2.callback(), |
| 4039 | NetLogWithSource())); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4040 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4041 | EXPECT_EQ(2u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4042 | TestGroupId("a"))); |
| 4043 | EXPECT_EQ(2u, |
| 4044 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4045 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4046 | |
Tsuyoshi Horo | 1d8eb53f | 2022-06-02 07:12:21 | [diff] [blame] | 4047 | TestCompletionCallback preconnect_callback3; |
| 4048 | EXPECT_EQ(ERR_IO_PENDING, |
| 4049 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 3, |
| 4050 | preconnect_callback3.callback(), |
| 4051 | NetLogWithSource())); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4052 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4053 | EXPECT_EQ(3u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4054 | TestGroupId("a"))); |
| 4055 | EXPECT_EQ(3u, |
| 4056 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4057 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4058 | |
Tsuyoshi Horo | 1d8eb53f | 2022-06-02 07:12:21 | [diff] [blame] | 4059 | EXPECT_EQ( |
| 4060 | OK, pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 1, |
| 4061 | CompletionOnceCallback(), NetLogWithSource())); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4062 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4063 | EXPECT_EQ(3u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4064 | TestGroupId("a"))); |
| 4065 | EXPECT_EQ(3u, |
| 4066 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4067 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4068 | } |
| 4069 | |
| 4070 | TEST_F(ClientSocketPoolBaseTest, PreconnectJobsTakenByNormalRequests) { |
| 4071 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4072 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4073 | |
Tsuyoshi Horo | 1d8eb53f | 2022-06-02 07:12:21 | [diff] [blame] | 4074 | TestCompletionCallback preconnect_callback; |
| 4075 | EXPECT_EQ(ERR_IO_PENDING, |
| 4076 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 1, |
| 4077 | preconnect_callback.callback(), |
| 4078 | NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4079 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4080 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 4081 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4082 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4083 | TestGroupId("a"))); |
| 4084 | EXPECT_EQ(1u, |
| 4085 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4086 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4087 | |
| 4088 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 4089 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4090 | EXPECT_EQ( |
| 4091 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4092 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4093 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4094 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4095 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4096 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4097 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4098 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4099 | TestGroupId("a"))); |
| 4100 | EXPECT_EQ(0u, |
| 4101 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4102 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4103 | |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4104 | client_socket_factory_.SignalJobs(); |
| 4105 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
Tsuyoshi Horo | 1d8eb53f | 2022-06-02 07:12:21 | [diff] [blame] | 4106 | EXPECT_THAT(preconnect_callback.WaitForResult(), IsOk()); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4107 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4108 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4109 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4110 | TestGroupId("a"))); |
| 4111 | EXPECT_EQ(0u, |
| 4112 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4113 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4114 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4115 | |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4116 | // Make sure if a preconnected socket is not fully connected when a request |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 4117 | // starts, it has a connect start time. |
| 4118 | TestLoadTimingInfoConnectedNotReused(handle1); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4119 | handle1.Reset(); |
| 4120 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4121 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4122 | } |
| 4123 | |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 4124 | // Checks that fully connected preconnect jobs have no connect times, and are |
| 4125 | // marked as reused. |
| 4126 | TEST_F(ClientSocketPoolBaseTest, ConnectedPreconnectJobsHaveNoConnectTimes) { |
| 4127 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4128 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
Tsuyoshi Horo | 1d8eb53f | 2022-06-02 07:12:21 | [diff] [blame] | 4129 | |
| 4130 | EXPECT_EQ( |
| 4131 | OK, pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 1, |
| 4132 | CompletionOnceCallback(), NetLogWithSource())); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 4133 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4134 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 4135 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4136 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4137 | TestGroupId("a"))); |
| 4138 | EXPECT_EQ(0u, |
| 4139 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4140 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 4141 | |
| 4142 | ClientSocketHandle handle; |
| 4143 | TestCompletionCallback callback; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4144 | EXPECT_EQ(OK, handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4145 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4146 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4147 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4148 | pool_.get(), NetLogWithSource())); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 4149 | |
| 4150 | // Make sure the idle socket was used. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4151 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 4152 | |
| 4153 | TestLoadTimingInfoConnectedReused(handle); |
| 4154 | handle.Reset(); |
| 4155 | TestLoadTimingInfoNotConnected(handle); |
| 4156 | } |
| 4157 | |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4158 | // http://crbug.com/64940 regression test. |
| 4159 | TEST_F(ClientSocketPoolBaseTest, PreconnectClosesIdleSocketRemovesGroup) { |
| 4160 | const int kMaxTotalSockets = 3; |
| 4161 | const int kMaxSocketsPerGroup = 2; |
| 4162 | CreatePool(kMaxTotalSockets, kMaxSocketsPerGroup); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4163 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4164 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 4165 | // Note that group id ordering matters here. "a" comes before "b", so |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4166 | // CloseOneIdleSocket() will try to close "a"'s idle socket. |
| 4167 | |
| 4168 | // Set up one idle socket in "a". |
| 4169 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 4170 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4171 | EXPECT_EQ( |
| 4172 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4173 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4174 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4175 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4176 | pool_.get(), NetLogWithSource())); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4177 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 4178 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4179 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4180 | TestGroupId("a"))); |
| 4181 | EXPECT_EQ(0u, |
| 4182 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4183 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4184 | |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4185 | client_socket_factory_.SignalJobs(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4186 | ASSERT_THAT(callback1.WaitForResult(), IsOk()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4187 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4188 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4189 | TestGroupId("a"))); |
| 4190 | EXPECT_EQ(0u, |
| 4191 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4192 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4193 | |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4194 | handle1.Reset(); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4195 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4196 | |
| 4197 | // Set up two active sockets in "b". |
| 4198 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 4199 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4200 | EXPECT_EQ( |
| 4201 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4202 | handle1.Init(TestGroupId("b"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4203 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4204 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4205 | pool_.get(), NetLogWithSource())); |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4206 | EXPECT_EQ( |
| 4207 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4208 | handle2.Init(TestGroupId("b"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4209 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4210 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4211 | pool_.get(), NetLogWithSource())); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4212 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4213 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("b"))); |
| 4214 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 4215 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4216 | TestGroupId("b"))); |
| 4217 | EXPECT_EQ(0u, |
| 4218 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4219 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4220 | |
| 4221 | client_socket_factory_.SignalJobs(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4222 | ASSERT_THAT(callback1.WaitForResult(), IsOk()); |
| 4223 | ASSERT_THAT(callback2.WaitForResult(), IsOk()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4224 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4225 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4226 | TestGroupId("b"))); |
| 4227 | EXPECT_EQ(0u, |
| 4228 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 4229 | EXPECT_EQ(2, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4230 | |
| 4231 | // Now we have 1 idle socket in "a" and 2 active sockets in "b". This means |
| 4232 | // we've maxed out on sockets, since we set |kMaxTotalSockets| to 3. |
| 4233 | // Requesting 2 preconnected sockets for "a" should fail to allocate any more |
| 4234 | // sockets for "a", and "b" should still have 2 active sockets. |
| 4235 | |
Tsuyoshi Horo | 1d8eb53f | 2022-06-02 07:12:21 | [diff] [blame] | 4236 | EXPECT_EQ( |
| 4237 | OK, pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 2, |
| 4238 | CompletionOnceCallback(), NetLogWithSource())); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4239 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4240 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4241 | TestGroupId("a"))); |
| 4242 | EXPECT_EQ(0u, |
| 4243 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4244 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4245 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 4246 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 4247 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4248 | TestGroupId("b"))); |
| 4249 | EXPECT_EQ(0u, |
| 4250 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4251 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4252 | EXPECT_EQ(2, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4253 | |
| 4254 | // Now release the 2 active sockets for "b". This will give us 1 idle socket |
| 4255 | // in "a" and 2 idle sockets in "b". Requesting 2 preconnected sockets for |
| 4256 | // "a" should result in closing 1 for "b". |
| 4257 | handle1.Reset(); |
| 4258 | handle2.Reset(); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4259 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4260 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4261 | |
Tsuyoshi Horo | 1d8eb53f | 2022-06-02 07:12:21 | [diff] [blame] | 4262 | TestCompletionCallback preconnect_callback; |
| 4263 | EXPECT_EQ(ERR_IO_PENDING, |
| 4264 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 2, |
| 4265 | preconnect_callback.callback(), |
| 4266 | NetLogWithSource())); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4267 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4268 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4269 | TestGroupId("a"))); |
| 4270 | EXPECT_EQ(1u, |
| 4271 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4272 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4273 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 4274 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 4275 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4276 | TestGroupId("b"))); |
| 4277 | EXPECT_EQ(0u, |
| 4278 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4279 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4280 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4281 | } |
| 4282 | |
[email protected] | b7b8be4 | 2011-07-12 12:46:41 | [diff] [blame] | 4283 | TEST_F(ClientSocketPoolBaseTest, PreconnectWithoutBackupJob) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4284 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 4285 | true /* enable_backup_connect_jobs */); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4286 | |
| 4287 | // Make the ConnectJob hang until it times out, shorten the timeout. |
| 4288 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 4289 | connect_job_factory_->set_timeout_duration(base::Milliseconds(500)); |
Tsuyoshi Horo | 1d8eb53f | 2022-06-02 07:12:21 | [diff] [blame] | 4290 | TestCompletionCallback preconnect_callback; |
| 4291 | EXPECT_EQ(ERR_IO_PENDING, |
| 4292 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 1, |
| 4293 | preconnect_callback.callback(), |
| 4294 | NetLogWithSource())); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4295 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4296 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4297 | TestGroupId("a"))); |
| 4298 | EXPECT_EQ(1u, |
| 4299 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4300 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4301 | |
[email protected] | b7b8be4 | 2011-07-12 12:46:41 | [diff] [blame] | 4302 | // Verify the backup timer doesn't create a backup job, by making |
| 4303 | // the backup job a pending job instead of a waiting job, so it |
| 4304 | // *would* complete if it were created. |
Lukasz Krakowiak | 28dcf9d6 | 2020-06-04 09:46:59 | [diff] [blame] | 4305 | base::RunLoop loop; |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4306 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 4307 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 4308 | FROM_HERE, loop.QuitClosure(), base::Seconds(1)); |
Lukasz Krakowiak | 28dcf9d6 | 2020-06-04 09:46:59 | [diff] [blame] | 4309 | loop.Run(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4310 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4311 | } |
| 4312 | |
[email protected] | b7b8be4 | 2011-07-12 12:46:41 | [diff] [blame] | 4313 | TEST_F(ClientSocketPoolBaseTest, PreconnectWithBackupJob) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4314 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 4315 | true /* enable_backup_connect_jobs */); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4316 | |
| 4317 | // Make the ConnectJob hang forever. |
| 4318 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
Tsuyoshi Horo | 1d8eb53f | 2022-06-02 07:12:21 | [diff] [blame] | 4319 | TestCompletionCallback preconnect_callback; |
| 4320 | EXPECT_EQ(ERR_IO_PENDING, |
| 4321 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 1, |
| 4322 | preconnect_callback.callback(), |
| 4323 | NetLogWithSource())); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4324 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4325 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4326 | TestGroupId("a"))); |
| 4327 | EXPECT_EQ(1u, |
| 4328 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4329 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 4330 | base::RunLoop().RunUntilIdle(); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4331 | |
| 4332 | // Make the backup job be a pending job, so it completes normally. |
| 4333 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 4334 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 4335 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4336 | EXPECT_EQ( |
| 4337 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4338 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4339 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4340 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4341 | pool_.get(), NetLogWithSource())); |
[email protected] | b7b8be4 | 2011-07-12 12:46:41 | [diff] [blame] | 4342 | // Timer has started, but the backup connect job shouldn't be created yet. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4343 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4344 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4345 | TestGroupId("a"))); |
| 4346 | EXPECT_EQ(0u, |
| 4347 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4348 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4349 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4350 | ASSERT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4351 | |
| 4352 | // The hung connect job should still be there, but everything else should be |
| 4353 | // complete. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4354 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4355 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4356 | TestGroupId("a"))); |
| 4357 | EXPECT_EQ(1u, |
| 4358 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4359 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4360 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4361 | } |
| 4362 | |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4363 | // Tests that a preconnect that starts out with unread data can still be used. |
| 4364 | // http://crbug.com/334467 |
| 4365 | TEST_F(ClientSocketPoolBaseTest, PreconnectWithUnreadData) { |
| 4366 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4367 | connect_job_factory_->set_job_type(TestConnectJob::kMockUnreadDataJob); |
| 4368 | |
Tsuyoshi Horo | 1d8eb53f | 2022-06-02 07:12:21 | [diff] [blame] | 4369 | EXPECT_EQ( |
| 4370 | OK, pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 1, |
| 4371 | CompletionOnceCallback(), NetLogWithSource())); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4372 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4373 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 4374 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4375 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4376 | TestGroupId("a"))); |
| 4377 | EXPECT_EQ(0u, |
| 4378 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4379 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4380 | |
| 4381 | // Fail future jobs to be sure that handle receives the preconnected socket |
| 4382 | // rather than closing it and making a new one. |
| 4383 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
| 4384 | ClientSocketHandle handle; |
| 4385 | TestCompletionCallback callback; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4386 | EXPECT_EQ(OK, handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4387 | TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4388 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4389 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4390 | pool_.get(), NetLogWithSource())); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4391 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4392 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 4393 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4394 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4395 | TestGroupId("a"))); |
| 4396 | EXPECT_EQ(0u, |
| 4397 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4398 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4399 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4400 | |
| 4401 | // Drain the pending read. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 4402 | EXPECT_EQ(1, handle.socket()->Read(nullptr, 1, CompletionOnceCallback())); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4403 | |
| 4404 | TestLoadTimingInfoConnectedReused(handle); |
| 4405 | handle.Reset(); |
| 4406 | |
| 4407 | // The socket should be usable now that it's idle again. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4408 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4409 | } |
| 4410 | |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4411 | TEST_F(ClientSocketPoolBaseTest, RequestGetsAssignedJob) { |
| 4412 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4413 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4414 | |
| 4415 | ClientSocketHandle handle1; |
| 4416 | TestCompletionCallback callback1; |
| 4417 | EXPECT_EQ( |
| 4418 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4419 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4420 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4421 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4422 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4423 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4424 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4425 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4426 | TestGroupId("a"))); |
| 4427 | EXPECT_EQ(0u, |
| 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 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4431 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4432 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4433 | } |
| 4434 | |
| 4435 | TEST_F(ClientSocketPoolBaseTest, MultipleRequestsGetAssignedJobs) { |
| 4436 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4437 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4438 | |
| 4439 | ClientSocketHandle handle1; |
| 4440 | TestCompletionCallback callback1; |
| 4441 | EXPECT_EQ( |
| 4442 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4443 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4444 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4445 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4446 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4447 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4448 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4449 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4450 | TestGroupId("a"))); |
| 4451 | EXPECT_EQ(0u, |
| 4452 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4453 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4454 | |
| 4455 | ClientSocketHandle handle2; |
| 4456 | TestCompletionCallback callback2; |
| 4457 | EXPECT_EQ( |
| 4458 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4459 | handle2.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 | callback2.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(2u, 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)); |
| 4473 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4474 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4475 | |
| 4476 | // One job completes. The other request should still have its job. |
| 4477 | client_socket_factory_.SignalJob(0); |
| 4478 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 4479 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4480 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4481 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4482 | TestGroupId("a"))); |
| 4483 | EXPECT_EQ(0u, |
| 4484 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4485 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4486 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4487 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4488 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4489 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4490 | } |
| 4491 | |
| 4492 | TEST_F(ClientSocketPoolBaseTest, PreconnectJobGetsAssignedToRequest) { |
| 4493 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4494 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4495 | |
Tsuyoshi Horo | 1d8eb53f | 2022-06-02 07:12:21 | [diff] [blame] | 4496 | TestCompletionCallback preconnect_callback; |
| 4497 | EXPECT_EQ(ERR_IO_PENDING, |
| 4498 | pool_->RequestSockets(TestGroupId("a"), params_, absl::nullopt, 1, |
| 4499 | preconnect_callback.callback(), |
| 4500 | NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4501 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4502 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 4503 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4504 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4505 | TestGroupId("a"))); |
| 4506 | EXPECT_EQ(1u, |
| 4507 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4508 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4509 | |
| 4510 | ClientSocketHandle handle1; |
| 4511 | TestCompletionCallback callback1; |
| 4512 | EXPECT_EQ( |
| 4513 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4514 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4515 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4516 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4517 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4518 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4519 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4520 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4521 | TestGroupId("a"))); |
| 4522 | EXPECT_EQ(0u, |
| 4523 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4524 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4525 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4526 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4527 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4528 | } |
| 4529 | |
| 4530 | TEST_F(ClientSocketPoolBaseTest, HigherPriorityRequestStealsJob) { |
| 4531 | CreatePool(kDefaultMaxSockets, 1); |
| 4532 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4533 | |
| 4534 | ClientSocketHandle handle1; |
| 4535 | TestCompletionCallback callback1; |
| 4536 | EXPECT_EQ( |
| 4537 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4538 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4539 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4540 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4541 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4542 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4543 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4544 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4545 | TestGroupId("a"))); |
| 4546 | EXPECT_EQ(0u, |
| 4547 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4548 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4549 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4550 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4551 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4552 | |
| 4553 | // Insert a higher priority request |
| 4554 | ClientSocketHandle handle2; |
| 4555 | TestCompletionCallback callback2; |
| 4556 | EXPECT_EQ( |
| 4557 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4558 | handle2.Init(TestGroupId("a"), params_, absl::nullopt, HIGHEST, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4559 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4560 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4561 | pool_.get(), 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(1u, 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 highest priority request should steal the job from the default priority |
| 4571 | // request. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4572 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4573 | &handle2)); |
| 4574 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4575 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4576 | } |
| 4577 | |
| 4578 | TEST_F(ClientSocketPoolBaseTest, RequestStealsJobFromLowestRequestWithJob) { |
| 4579 | CreatePool(3, 3); |
| 4580 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4581 | |
| 4582 | ClientSocketHandle handle_lowest; |
| 4583 | TestCompletionCallback callback_lowest; |
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_lowest.Init(TestGroupId("a"), params_, absl::nullopt, LOWEST, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4587 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4588 | callback_lowest.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(1u, 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"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4598 | |
| 4599 | ClientSocketHandle handle_highest; |
| 4600 | TestCompletionCallback callback_highest; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4601 | EXPECT_EQ( |
| 4602 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4603 | handle_highest.Init(TestGroupId("a"), params_, absl::nullopt, HIGHEST, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4604 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4605 | callback_highest.callback(), |
| 4606 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4607 | NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4608 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4609 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4610 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4611 | TestGroupId("a"))); |
| 4612 | EXPECT_EQ(0u, |
| 4613 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4614 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4615 | |
| 4616 | ClientSocketHandle handle_low; |
| 4617 | TestCompletionCallback callback_low; |
| 4618 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4619 | handle_low.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4620 | TestGroupId("a"), params_, absl::nullopt, LOW, SocketTag(), |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4621 | ClientSocketPool::RespectLimits::ENABLED, |
| 4622 | callback_low.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4623 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4624 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4625 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4626 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4627 | TestGroupId("a"))); |
| 4628 | EXPECT_EQ(0u, |
| 4629 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4630 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4631 | |
| 4632 | ClientSocketHandle handle_lowest2; |
| 4633 | TestCompletionCallback callback_lowest2; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4634 | EXPECT_EQ( |
| 4635 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4636 | handle_lowest2.Init(TestGroupId("a"), params_, absl::nullopt, LOWEST, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4637 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4638 | callback_lowest2.callback(), |
| 4639 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4640 | NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4641 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4642 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4643 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4644 | TestGroupId("a"))); |
| 4645 | EXPECT_EQ(0u, |
| 4646 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4647 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4648 | |
| 4649 | // The top three requests in the queue should have jobs. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4650 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4651 | &handle_highest)); |
| 4652 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4653 | &handle_low)); |
| 4654 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4655 | &handle_lowest)); |
| 4656 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting( |
| 4657 | TestGroupId("a"), &handle_lowest2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4658 | |
| 4659 | // Add another request with medium priority. It should steal the job from the |
| 4660 | // lowest priority request with a job. |
| 4661 | ClientSocketHandle handle_medium; |
| 4662 | TestCompletionCallback callback_medium; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4663 | EXPECT_EQ( |
| 4664 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4665 | handle_medium.Init(TestGroupId("a"), params_, absl::nullopt, MEDIUM, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4666 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4667 | callback_medium.callback(), |
| 4668 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4669 | NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4670 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4671 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4672 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4673 | TestGroupId("a"))); |
| 4674 | EXPECT_EQ(0u, |
| 4675 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4676 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
| 4677 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4678 | &handle_highest)); |
| 4679 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4680 | &handle_medium)); |
| 4681 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4682 | &handle_low)); |
| 4683 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4684 | &handle_lowest)); |
| 4685 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting( |
| 4686 | TestGroupId("a"), &handle_lowest2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4687 | } |
| 4688 | |
| 4689 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeRequestStealsJob) { |
| 4690 | CreatePool(kDefaultMaxSockets, 1); |
| 4691 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4692 | |
| 4693 | ClientSocketHandle handle1; |
| 4694 | TestCompletionCallback callback1; |
| 4695 | EXPECT_EQ( |
| 4696 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4697 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4698 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4699 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4700 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4701 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4702 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4703 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4704 | TestGroupId("a"))); |
| 4705 | EXPECT_EQ(0u, |
| 4706 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4707 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4708 | |
| 4709 | ClientSocketHandle handle2; |
| 4710 | TestCompletionCallback callback2; |
| 4711 | EXPECT_EQ( |
| 4712 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4713 | handle2.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4714 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4715 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4716 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4717 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4718 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4719 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4720 | TestGroupId("a"))); |
| 4721 | EXPECT_EQ(0u, |
| 4722 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4723 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4724 | |
| 4725 | // 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] | 4726 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4727 | &handle1)); |
| 4728 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4729 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4730 | |
| 4731 | // Reprioritizing the second request places it above the first, and it steals |
| 4732 | // the job from the first request. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4733 | pool_->SetPriority(TestGroupId("a"), &handle2, HIGHEST); |
| 4734 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4735 | &handle2)); |
| 4736 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4737 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4738 | } |
| 4739 | |
| 4740 | TEST_F(ClientSocketPoolBaseTest, CancelRequestReassignsJob) { |
| 4741 | CreatePool(kDefaultMaxSockets, 1); |
| 4742 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4743 | |
| 4744 | ClientSocketHandle handle1; |
| 4745 | TestCompletionCallback callback1; |
| 4746 | EXPECT_EQ( |
| 4747 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4748 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4749 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4750 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4751 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4752 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4753 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4754 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4755 | TestGroupId("a"))); |
| 4756 | EXPECT_EQ(0u, |
| 4757 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4758 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4759 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4760 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4761 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4762 | |
| 4763 | ClientSocketHandle handle2; |
| 4764 | TestCompletionCallback callback2; |
| 4765 | EXPECT_EQ( |
| 4766 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4767 | handle2.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4768 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4769 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4770 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4771 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4772 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4773 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4774 | TestGroupId("a"))); |
| 4775 | EXPECT_EQ(0u, |
| 4776 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4777 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4778 | |
| 4779 | // The second request doesn't get a job because we are the limit. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4780 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4781 | &handle1)); |
| 4782 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4783 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4784 | |
| 4785 | // The second request should get a job upon cancelling the first request. |
| 4786 | handle1.Reset(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4787 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4788 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4789 | TestGroupId("a"))); |
| 4790 | EXPECT_EQ(0u, |
| 4791 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4792 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4793 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4794 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4795 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4796 | } |
| 4797 | |
| 4798 | TEST_F(ClientSocketPoolBaseTest, JobCompletionReassignsJob) { |
| 4799 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4800 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4801 | |
| 4802 | ClientSocketHandle handle1; |
| 4803 | TestCompletionCallback callback1; |
| 4804 | EXPECT_EQ( |
| 4805 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4806 | handle1.Init(TestGroupId("a"), params_, absl::nullopt, HIGHEST, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4807 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4808 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4809 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4810 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4811 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4812 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4813 | TestGroupId("a"))); |
| 4814 | EXPECT_EQ(0u, |
| 4815 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4816 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4817 | |
| 4818 | ClientSocketHandle handle2; |
| 4819 | TestCompletionCallback callback2; |
| 4820 | EXPECT_EQ( |
| 4821 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4822 | handle2.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4823 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4824 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4825 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4826 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4827 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4828 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4829 | TestGroupId("a"))); |
| 4830 | EXPECT_EQ(0u, |
| 4831 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4832 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4833 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4834 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4835 | &handle1)); |
| 4836 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4837 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4838 | |
| 4839 | // The lower-priority job completes first. The higher-priority request should |
| 4840 | // get the socket, and the lower-priority request should get the remaining |
| 4841 | // job. |
| 4842 | client_socket_factory_.SignalJob(1); |
| 4843 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4844 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4845 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4846 | TestGroupId("a"))); |
| 4847 | EXPECT_EQ(0u, |
| 4848 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4849 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4850 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4851 | EXPECT_TRUE(handle1.socket()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4852 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4853 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4854 | } |
| 4855 | |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 4856 | class MockLayeredPool : public HigherLayeredPool { |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4857 | public: |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4858 | MockLayeredPool(TransportClientSocketPool* pool, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4859 | const ClientSocketPool::GroupId& group_id) |
Tsuyoshi Horo | 2ec06e00 | 2022-06-09 01:38:59 | [diff] [blame] | 4860 | : pool_(pool), group_id_(group_id) { |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 4861 | pool_->AddHigherLayeredPool(this); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4862 | } |
| 4863 | |
Daniel Cheng | 4496d082 | 2018-04-26 21:52:15 | [diff] [blame] | 4864 | ~MockLayeredPool() override { pool_->RemoveHigherLayeredPool(this); } |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4865 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4866 | int RequestSocket(TransportClientSocketPool* pool) { |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4867 | return handle_.Init( |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 4868 | group_id_, ClientSocketPool::SocketParams::CreateForHttpForTesting(), |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4869 | absl::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4870 | ClientSocketPool::RespectLimits::ENABLED, callback_.callback(), |
| 4871 | ClientSocketPool::ProxyAuthCallback(), pool, NetLogWithSource()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4872 | } |
| 4873 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4874 | int RequestSocketWithoutLimits(TransportClientSocketPool* pool) { |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4875 | return handle_.Init( |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 4876 | group_id_, ClientSocketPool::SocketParams::CreateForHttpForTesting(), |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4877 | absl::nullopt, MAXIMUM_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4878 | ClientSocketPool::RespectLimits::DISABLED, callback_.callback(), |
| 4879 | ClientSocketPool::ProxyAuthCallback(), pool, NetLogWithSource()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4880 | } |
| 4881 | |
| 4882 | bool ReleaseOneConnection() { |
| 4883 | if (!handle_.is_initialized() || !can_release_connection_) { |
| 4884 | return false; |
| 4885 | } |
| 4886 | handle_.socket()->Disconnect(); |
| 4887 | handle_.Reset(); |
| 4888 | return true; |
| 4889 | } |
| 4890 | |
| 4891 | void set_can_release_connection(bool can_release_connection) { |
| 4892 | can_release_connection_ = can_release_connection; |
| 4893 | } |
| 4894 | |
| 4895 | MOCK_METHOD0(CloseOneIdleConnection, bool()); |
| 4896 | |
| 4897 | private: |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 4898 | const raw_ptr<TransportClientSocketPool> pool_; |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4899 | ClientSocketHandle handle_; |
| 4900 | TestCompletionCallback callback_; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4901 | const ClientSocketPool::GroupId group_id_; |
Tsuyoshi Horo | 2ec06e00 | 2022-06-09 01:38:59 | [diff] [blame] | 4902 | bool can_release_connection_ = true; |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4903 | }; |
| 4904 | |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4905 | // Tests the basic case of closing an idle socket in a higher layered pool when |
| 4906 | // a new request is issued and the lower layer pool is stalled. |
| 4907 | TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketsHeldByLayeredPoolWhenNeeded) { |
| 4908 | CreatePool(1, 1); |
| 4909 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 4910 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4911 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("foo")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4912 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4913 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4914 | .WillOnce(Invoke(&mock_layered_pool, |
| 4915 | &MockLayeredPool::ReleaseOneConnection)); |
| 4916 | ClientSocketHandle handle; |
| 4917 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4918 | EXPECT_EQ( |
| 4919 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4920 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4921 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4922 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4923 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4924 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4925 | } |
| 4926 | |
Matt Menke | 83367864 | 2019-03-05 22:05:51 | [diff] [blame] | 4927 | // Tests the case that trying to close an idle socket in a higher layered pool |
| 4928 | // fails. |
| 4929 | TEST_F(ClientSocketPoolBaseTest, |
| 4930 | CloseIdleSocketsHeldByLayeredPoolWhenNeededFails) { |
| 4931 | CreatePool(1, 1); |
| 4932 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 4933 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4934 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("foo")); |
Matt Menke | 83367864 | 2019-03-05 22:05:51 | [diff] [blame] | 4935 | mock_layered_pool.set_can_release_connection(false); |
| 4936 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
| 4937 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4938 | .WillOnce(Invoke(&mock_layered_pool, |
| 4939 | &MockLayeredPool::ReleaseOneConnection)); |
| 4940 | ClientSocketHandle handle; |
| 4941 | TestCompletionCallback callback; |
| 4942 | EXPECT_EQ( |
| 4943 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4944 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4945 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4946 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4947 | pool_.get(), NetLogWithSource())); |
Matt Menke | 83367864 | 2019-03-05 22:05:51 | [diff] [blame] | 4948 | base::RunLoop().RunUntilIdle(); |
| 4949 | EXPECT_FALSE(callback.have_result()); |
| 4950 | } |
| 4951 | |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4952 | // Same as above, but the idle socket is in the same group as the stalled |
| 4953 | // socket, and closes the only other request in its group when closing requests |
| 4954 | // in higher layered pools. This generally shouldn't happen, but it may be |
| 4955 | // possible if a higher level pool issues a request and the request is |
| 4956 | // subsequently cancelled. Even if it's not possible, best not to crash. |
| 4957 | TEST_F(ClientSocketPoolBaseTest, |
| 4958 | CloseIdleSocketsHeldByLayeredPoolWhenNeededSameGroup) { |
| 4959 | CreatePool(2, 2); |
| 4960 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 4961 | |
| 4962 | // Need a socket in another group for the pool to be stalled (If a group |
| 4963 | // has the maximum number of connections already, it's not stalled). |
| 4964 | ClientSocketHandle handle1; |
| 4965 | TestCompletionCallback callback1; |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4966 | EXPECT_EQ(OK, handle1.Init(TestGroupId("group1"), params_, absl::nullopt, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4967 | DEFAULT_PRIORITY, SocketTag(), |
| 4968 | ClientSocketPool::RespectLimits::ENABLED, |
| 4969 | callback1.callback(), |
| 4970 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4971 | NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4972 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4973 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("group2")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4974 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4975 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4976 | .WillOnce(Invoke(&mock_layered_pool, |
| 4977 | &MockLayeredPool::ReleaseOneConnection)); |
| 4978 | ClientSocketHandle handle; |
| 4979 | TestCompletionCallback callback2; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4980 | EXPECT_EQ(ERR_IO_PENDING, |
| 4981 | handle.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 4982 | TestGroupId("group2"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4983 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4984 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4985 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4986 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4987 | } |
| 4988 | |
| 4989 | // Tests the case when an idle socket can be closed when a new request is |
| 4990 | // issued, and the new request belongs to a group that was previously stalled. |
| 4991 | TEST_F(ClientSocketPoolBaseTest, |
| 4992 | CloseIdleSocketsHeldByLayeredPoolInSameGroupWhenNeeded) { |
| 4993 | CreatePool(2, 2); |
| 4994 | std::list<TestConnectJob::JobType> job_types; |
| 4995 | job_types.push_back(TestConnectJob::kMockJob); |
| 4996 | job_types.push_back(TestConnectJob::kMockJob); |
| 4997 | job_types.push_back(TestConnectJob::kMockJob); |
| 4998 | job_types.push_back(TestConnectJob::kMockJob); |
| 4999 | connect_job_factory_->set_job_types(&job_types); |
| 5000 | |
| 5001 | ClientSocketHandle handle1; |
| 5002 | TestCompletionCallback callback1; |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 5003 | EXPECT_EQ(OK, handle1.Init(TestGroupId("group1"), params_, absl::nullopt, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5004 | DEFAULT_PRIORITY, SocketTag(), |
| 5005 | ClientSocketPool::RespectLimits::ENABLED, |
| 5006 | callback1.callback(), |
| 5007 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 5008 | NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 5009 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5010 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("group2")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5011 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 5012 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 5013 | .WillRepeatedly(Invoke(&mock_layered_pool, |
| 5014 | &MockLayeredPool::ReleaseOneConnection)); |
| 5015 | mock_layered_pool.set_can_release_connection(false); |
| 5016 | |
| 5017 | // The third request is made when the socket pool is in a stalled state. |
| 5018 | ClientSocketHandle handle3; |
| 5019 | TestCompletionCallback callback3; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5020 | EXPECT_EQ(ERR_IO_PENDING, |
| 5021 | handle3.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 5022 | TestGroupId("group3"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5023 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5024 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5025 | pool_.get(), NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 5026 | |
| 5027 | base::RunLoop().RunUntilIdle(); |
| 5028 | EXPECT_FALSE(callback3.have_result()); |
| 5029 | |
| 5030 | // The fourth request is made when the pool is no longer stalled. The third |
| 5031 | // request should be serviced first, since it was issued first and has the |
| 5032 | // same priority. |
| 5033 | mock_layered_pool.set_can_release_connection(true); |
| 5034 | ClientSocketHandle handle4; |
| 5035 | TestCompletionCallback callback4; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5036 | EXPECT_EQ(ERR_IO_PENDING, |
| 5037 | handle4.Init( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 5038 | TestGroupId("group3"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5039 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5040 | callback4.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5041 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5042 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 5043 | EXPECT_FALSE(callback4.have_result()); |
| 5044 | |
| 5045 | // Closing a handle should free up another socket slot. |
| 5046 | handle1.Reset(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5047 | EXPECT_THAT(callback4.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 5048 | } |
| 5049 | |
| 5050 | // Tests the case when an idle socket can be closed when a new request is |
| 5051 | // issued, and the new request belongs to a group that was previously stalled. |
| 5052 | // |
| 5053 | // The two differences from the above test are that the stalled requests are not |
| 5054 | // in the same group as the layered pool's request, and the the fourth request |
| 5055 | // has a higher priority than the third one, so gets a socket first. |
| 5056 | TEST_F(ClientSocketPoolBaseTest, |
| 5057 | CloseIdleSocketsHeldByLayeredPoolInSameGroupWhenNeeded2) { |
| 5058 | CreatePool(2, 2); |
| 5059 | std::list<TestConnectJob::JobType> job_types; |
| 5060 | job_types.push_back(TestConnectJob::kMockJob); |
| 5061 | job_types.push_back(TestConnectJob::kMockJob); |
| 5062 | job_types.push_back(TestConnectJob::kMockJob); |
| 5063 | job_types.push_back(TestConnectJob::kMockJob); |
| 5064 | connect_job_factory_->set_job_types(&job_types); |
| 5065 | |
| 5066 | ClientSocketHandle handle1; |
| 5067 | TestCompletionCallback callback1; |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 5068 | EXPECT_EQ(OK, handle1.Init(TestGroupId("group1"), params_, absl::nullopt, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5069 | DEFAULT_PRIORITY, SocketTag(), |
| 5070 | ClientSocketPool::RespectLimits::ENABLED, |
| 5071 | callback1.callback(), |
| 5072 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 5073 | NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 5074 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5075 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("group2")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5076 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 5077 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 5078 | .WillRepeatedly(Invoke(&mock_layered_pool, |
| 5079 | &MockLayeredPool::ReleaseOneConnection)); |
| 5080 | mock_layered_pool.set_can_release_connection(false); |
| 5081 | |
| 5082 | // The third request is made when the socket pool is in a stalled state. |
| 5083 | ClientSocketHandle handle3; |
| 5084 | TestCompletionCallback callback3; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 5085 | EXPECT_EQ( |
| 5086 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 5087 | handle3.Init(TestGroupId("group3"), params_, absl::nullopt, MEDIUM, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5088 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 5089 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5090 | pool_.get(), NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 5091 | |
| 5092 | base::RunLoop().RunUntilIdle(); |
| 5093 | EXPECT_FALSE(callback3.have_result()); |
| 5094 | |
| 5095 | // The fourth request is made when the pool is no longer stalled. This |
| 5096 | // request has a higher priority than the third request, so is serviced first. |
| 5097 | mock_layered_pool.set_can_release_connection(true); |
| 5098 | ClientSocketHandle handle4; |
| 5099 | TestCompletionCallback callback4; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 5100 | EXPECT_EQ( |
| 5101 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 5102 | handle4.Init(TestGroupId("group3"), params_, absl::nullopt, HIGHEST, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5103 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 5104 | callback4.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5105 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5106 | EXPECT_THAT(callback4.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 5107 | EXPECT_FALSE(callback3.have_result()); |
| 5108 | |
| 5109 | // Closing a handle should free up another socket slot. |
| 5110 | handle1.Reset(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5111 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 5112 | } |
| 5113 | |
| 5114 | TEST_F(ClientSocketPoolBaseTest, |
| 5115 | CloseMultipleIdleSocketsHeldByLayeredPoolWhenNeeded) { |
| 5116 | CreatePool(1, 1); |
| 5117 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 5118 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5119 | MockLayeredPool mock_layered_pool1(pool_.get(), TestGroupId("foo")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5120 | EXPECT_THAT(mock_layered_pool1.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 5121 | EXPECT_CALL(mock_layered_pool1, CloseOneIdleConnection()) |
| 5122 | .WillRepeatedly(Invoke(&mock_layered_pool1, |
| 5123 | &MockLayeredPool::ReleaseOneConnection)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5124 | MockLayeredPool mock_layered_pool2(pool_.get(), TestGroupId("bar")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5125 | EXPECT_THAT(mock_layered_pool2.RequestSocketWithoutLimits(pool_.get()), |
| 5126 | IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 5127 | EXPECT_CALL(mock_layered_pool2, CloseOneIdleConnection()) |
| 5128 | .WillRepeatedly(Invoke(&mock_layered_pool2, |
| 5129 | &MockLayeredPool::ReleaseOneConnection)); |
| 5130 | ClientSocketHandle handle; |
| 5131 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 5132 | EXPECT_EQ( |
| 5133 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 5134 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5135 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5136 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5137 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5138 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 5139 | } |
| 5140 | |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5141 | // Test that when a socket pool and group are at their limits, a request |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5142 | // with RespectLimits::DISABLED triggers creation of a new socket, and gets the |
| 5143 | // socket instead of a request with the same priority that was issued earlier, |
| 5144 | // but has RespectLimits::ENABLED. |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5145 | TEST_F(ClientSocketPoolBaseTest, IgnoreLimits) { |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5146 | CreatePool(1, 1); |
| 5147 | |
| 5148 | // Issue a request to reach the socket pool limit. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5149 | EXPECT_EQ(OK, StartRequestWithIgnoreLimits( |
| 5150 | TestGroupId("a"), MAXIMUM_PRIORITY, |
| 5151 | ClientSocketPool::RespectLimits::ENABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5152 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5153 | |
| 5154 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 5155 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5156 | EXPECT_EQ(ERR_IO_PENDING, StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5157 | TestGroupId("a"), MAXIMUM_PRIORITY, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5158 | ClientSocketPool::RespectLimits::ENABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5159 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5160 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5161 | // Issue a request that ignores the limits, so a new ConnectJob is |
| 5162 | // created. |
| 5163 | EXPECT_EQ(ERR_IO_PENDING, StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5164 | TestGroupId("a"), MAXIMUM_PRIORITY, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5165 | ClientSocketPool::RespectLimits::DISABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5166 | ASSERT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5167 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5168 | EXPECT_THAT(request(2)->WaitForResult(), IsOk()); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5169 | EXPECT_FALSE(request(1)->have_result()); |
| 5170 | } |
| 5171 | |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 5172 | // Test that when a socket pool and group are at their limits, a ConnectJob |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5173 | // issued for a request with RespectLimits::DISABLED is not cancelled when a |
| 5174 | // request with RespectLimits::ENABLED issued to the same group is cancelled. |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 5175 | TEST_F(ClientSocketPoolBaseTest, IgnoreLimitsCancelOtherJob) { |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 5176 | CreatePool(1, 1); |
| 5177 | |
| 5178 | // Issue a request to reach the socket pool limit. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5179 | EXPECT_EQ(OK, StartRequestWithIgnoreLimits( |
| 5180 | TestGroupId("a"), MAXIMUM_PRIORITY, |
| 5181 | ClientSocketPool::RespectLimits::ENABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5182 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 5183 | |
| 5184 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 5185 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5186 | EXPECT_EQ(ERR_IO_PENDING, StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5187 | TestGroupId("a"), MAXIMUM_PRIORITY, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5188 | ClientSocketPool::RespectLimits::ENABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5189 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 5190 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5191 | // Issue a request with RespectLimits::DISABLED, so a new ConnectJob is |
| 5192 | // created. |
| 5193 | EXPECT_EQ(ERR_IO_PENDING, StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5194 | TestGroupId("a"), MAXIMUM_PRIORITY, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5195 | ClientSocketPool::RespectLimits::DISABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5196 | ASSERT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5197 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5198 | // Cancel the pending request with RespectLimits::ENABLED. The ConnectJob |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5199 | // should not be cancelled. |
| 5200 | request(1)->handle()->Reset(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5201 | ASSERT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5202 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5203 | EXPECT_THAT(request(2)->WaitForResult(), IsOk()); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5204 | EXPECT_FALSE(request(1)->have_result()); |
| 5205 | } |
| 5206 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5207 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthNoAuthCallback) { |
| 5208 | CreatePool(1, 1); |
| 5209 | |
| 5210 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5211 | |
| 5212 | ClientSocketHandle handle; |
| 5213 | TestCompletionCallback callback; |
| 5214 | EXPECT_EQ( |
| 5215 | ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 5216 | handle.Init(TestGroupId("a"), params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5217 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5218 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5219 | pool_.get(), NetLogWithSource())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5220 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5221 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5222 | |
| 5223 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_PROXY_AUTH_REQUESTED)); |
| 5224 | EXPECT_FALSE(handle.is_initialized()); |
| 5225 | EXPECT_FALSE(handle.socket()); |
| 5226 | |
| 5227 | // The group should now be empty, and thus be deleted. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5228 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5229 | } |
| 5230 | |
| 5231 | class TestAuthHelper { |
| 5232 | public: |
| 5233 | TestAuthHelper() = default; |
Peter Boström | 293b134 | 2021-09-22 17:31:43 | [diff] [blame] | 5234 | |
| 5235 | TestAuthHelper(const TestAuthHelper&) = delete; |
| 5236 | TestAuthHelper& operator=(const TestAuthHelper&) = delete; |
| 5237 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5238 | ~TestAuthHelper() = default; |
| 5239 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5240 | void InitHandle( |
Matt Menke | 84d11e56 | 2019-03-27 00:11:19 | [diff] [blame] | 5241 | scoped_refptr<ClientSocketPool::SocketParams> params, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5242 | TransportClientSocketPool* pool, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5243 | RequestPriority priority = DEFAULT_PRIORITY, |
| 5244 | ClientSocketPool::RespectLimits respect_limits = |
| 5245 | ClientSocketPool::RespectLimits::ENABLED, |
| 5246 | const ClientSocketPool::GroupId& group_id_in = TestGroupId("a")) { |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5247 | EXPECT_EQ(ERR_IO_PENDING, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 5248 | handle_.Init(group_id_in, params, absl::nullopt, priority, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5249 | SocketTag(), respect_limits, callback_.callback(), |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5250 | base::BindRepeating(&TestAuthHelper::AuthCallback, |
| 5251 | base::Unretained(this)), |
| 5252 | pool, NetLogWithSource())); |
| 5253 | } |
| 5254 | |
| 5255 | void WaitForAuth() { |
| 5256 | run_loop_ = std::make_unique<base::RunLoop>(); |
| 5257 | run_loop_->Run(); |
| 5258 | run_loop_.reset(); |
| 5259 | } |
| 5260 | |
| 5261 | void WaitForAuthAndRestartSync() { |
| 5262 | restart_sync_ = true; |
| 5263 | WaitForAuth(); |
| 5264 | restart_sync_ = false; |
| 5265 | } |
| 5266 | |
| 5267 | void WaitForAuthAndResetHandleSync() { |
| 5268 | reset_handle_sync_ = true; |
| 5269 | WaitForAuth(); |
| 5270 | reset_handle_sync_ = false; |
| 5271 | } |
| 5272 | |
| 5273 | void RestartWithAuth() { |
| 5274 | DCHECK(restart_with_auth_callback_); |
| 5275 | std::move(restart_with_auth_callback_).Run(); |
| 5276 | } |
| 5277 | |
| 5278 | int WaitForResult() { |
| 5279 | int result = callback_.WaitForResult(); |
| 5280 | // There shouldn't be any callback waiting to be invoked once the request is |
| 5281 | // complete. |
| 5282 | EXPECT_FALSE(restart_with_auth_callback_); |
| 5283 | // The socket should only be initialized on success. |
| 5284 | EXPECT_EQ(result == OK, handle_.is_initialized()); |
| 5285 | EXPECT_EQ(result == OK, handle_.socket() != nullptr); |
| 5286 | return result; |
| 5287 | } |
| 5288 | |
| 5289 | ClientSocketHandle* handle() { return &handle_; } |
| 5290 | int auth_count() const { return auth_count_; } |
| 5291 | int have_result() const { return callback_.have_result(); } |
| 5292 | |
| 5293 | private: |
| 5294 | void AuthCallback(const HttpResponseInfo& response, |
| 5295 | HttpAuthController* auth_controller, |
| 5296 | base::OnceClosure restart_with_auth_callback) { |
| 5297 | EXPECT_FALSE(restart_with_auth_callback_); |
| 5298 | EXPECT_TRUE(restart_with_auth_callback); |
| 5299 | |
| 5300 | // Once there's a result, this method shouldn't be invoked again. |
| 5301 | EXPECT_FALSE(callback_.have_result()); |
| 5302 | |
| 5303 | ++auth_count_; |
| 5304 | run_loop_->Quit(); |
| 5305 | if (restart_sync_) { |
| 5306 | std::move(restart_with_auth_callback).Run(); |
| 5307 | return; |
| 5308 | } |
| 5309 | |
| 5310 | restart_with_auth_callback_ = std::move(restart_with_auth_callback); |
| 5311 | |
| 5312 | if (reset_handle_sync_) { |
| 5313 | handle_.Reset(); |
| 5314 | return; |
| 5315 | } |
| 5316 | } |
| 5317 | |
| 5318 | std::unique_ptr<base::RunLoop> run_loop_; |
| 5319 | base::OnceClosure restart_with_auth_callback_; |
| 5320 | |
| 5321 | bool restart_sync_ = false; |
| 5322 | bool reset_handle_sync_ = false; |
| 5323 | |
| 5324 | ClientSocketHandle handle_; |
| 5325 | int auth_count_ = 0; |
| 5326 | TestCompletionCallback callback_; |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5327 | }; |
| 5328 | |
| 5329 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnce) { |
| 5330 | CreatePool(1, 1); |
| 5331 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5332 | |
| 5333 | TestAuthHelper auth_helper; |
| 5334 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5335 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5336 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5337 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5338 | |
| 5339 | auth_helper.WaitForAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5340 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5341 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5342 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5343 | |
| 5344 | auth_helper.RestartWithAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5345 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5346 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5347 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5348 | |
| 5349 | EXPECT_THAT(auth_helper.WaitForResult(), IsOk()); |
| 5350 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5351 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5352 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5353 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5354 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5355 | } |
| 5356 | |
| 5357 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceSync) { |
| 5358 | CreatePool(1, 1); |
| 5359 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5360 | |
| 5361 | TestAuthHelper auth_helper; |
| 5362 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5363 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5364 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5365 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5366 | |
| 5367 | auth_helper.WaitForAuthAndRestartSync(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5368 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5369 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5370 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5371 | |
| 5372 | EXPECT_THAT(auth_helper.WaitForResult(), IsOk()); |
| 5373 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5374 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5375 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5376 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5377 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5378 | } |
| 5379 | |
| 5380 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceFails) { |
| 5381 | CreatePool(1, 1); |
| 5382 | connect_job_factory_->set_job_type( |
| 5383 | TestConnectJob::kMockAuthChallengeOnceFailingJob); |
| 5384 | |
| 5385 | TestAuthHelper auth_helper; |
| 5386 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5387 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5388 | |
| 5389 | auth_helper.WaitForAuth(); |
| 5390 | auth_helper.RestartWithAuth(); |
| 5391 | EXPECT_THAT(auth_helper.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 5392 | |
| 5393 | EXPECT_EQ(1, auth_helper.auth_count()); |
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, ProxyAuthOnceSyncFails) { |
| 5399 | CreatePool(1, 1); |
| 5400 | connect_job_factory_->set_job_type( |
| 5401 | TestConnectJob::kMockAuthChallengeOnceFailingJob); |
| 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 | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5406 | |
| 5407 | auth_helper.WaitForAuthAndRestartSync(); |
| 5408 | EXPECT_THAT(auth_helper.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 5409 | |
| 5410 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5411 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5412 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5413 | } |
| 5414 | |
| 5415 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceDeleteHandle) { |
| 5416 | CreatePool(1, 1); |
| 5417 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5418 | |
| 5419 | TestAuthHelper auth_helper; |
| 5420 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5421 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5422 | |
| 5423 | auth_helper.WaitForAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5424 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5425 | |
| 5426 | auth_helper.handle()->Reset(); |
| 5427 | |
| 5428 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5429 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5430 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5431 | EXPECT_FALSE(auth_helper.handle()->is_initialized()); |
| 5432 | EXPECT_FALSE(auth_helper.handle()->socket()); |
| 5433 | } |
| 5434 | |
| 5435 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceDeleteHandleSync) { |
| 5436 | CreatePool(1, 1); |
| 5437 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5438 | |
| 5439 | TestAuthHelper auth_helper; |
| 5440 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5441 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5442 | |
| 5443 | auth_helper.WaitForAuthAndResetHandleSync(); |
| 5444 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5445 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5446 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5447 | EXPECT_FALSE(auth_helper.handle()->is_initialized()); |
| 5448 | EXPECT_FALSE(auth_helper.handle()->socket()); |
| 5449 | } |
| 5450 | |
| 5451 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceFlushWithError) { |
| 5452 | CreatePool(1, 1); |
| 5453 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5454 | |
| 5455 | TestAuthHelper auth_helper; |
| 5456 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5457 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5458 | |
| 5459 | auth_helper.WaitForAuth(); |
| 5460 | |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 5461 | pool_->FlushWithError(ERR_FAILED, "Network changed"); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5462 | base::RunLoop().RunUntilIdle(); |
| 5463 | |
| 5464 | // When flushing the socket pool, bound sockets should delay returning the |
| 5465 | // error until completion. |
| 5466 | EXPECT_FALSE(auth_helper.have_result()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5467 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5468 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5469 | |
| 5470 | auth_helper.RestartWithAuth(); |
| 5471 | // The callback should be called asynchronously. |
| 5472 | EXPECT_FALSE(auth_helper.have_result()); |
| 5473 | |
| 5474 | EXPECT_THAT(auth_helper.WaitForResult(), IsError(ERR_FAILED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5475 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5476 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5477 | } |
| 5478 | |
| 5479 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthTwice) { |
| 5480 | CreatePool(1, 1); |
| 5481 | connect_job_factory_->set_job_type( |
| 5482 | TestConnectJob::kMockAuthChallengeTwiceJob); |
| 5483 | |
| 5484 | TestAuthHelper auth_helper; |
| 5485 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5486 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5487 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5488 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5489 | |
| 5490 | auth_helper.WaitForAuth(); |
| 5491 | auth_helper.RestartWithAuth(); |
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 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5494 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5495 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5496 | |
| 5497 | auth_helper.WaitForAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5498 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5499 | EXPECT_EQ(2, auth_helper.auth_count()); |
| 5500 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5501 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5502 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5503 | auth_helper.RestartWithAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5504 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5505 | EXPECT_EQ(2, auth_helper.auth_count()); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5506 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5507 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5508 | |
| 5509 | EXPECT_THAT(auth_helper.WaitForResult(), IsOk()); |
| 5510 | EXPECT_EQ(2, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5511 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5512 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5513 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5514 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5515 | } |
| 5516 | |
| 5517 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthTwiceFails) { |
| 5518 | CreatePool(1, 1); |
| 5519 | connect_job_factory_->set_job_type( |
| 5520 | TestConnectJob::kMockAuthChallengeTwiceFailingJob); |
| 5521 | |
| 5522 | TestAuthHelper auth_helper; |
| 5523 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5524 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5525 | |
| 5526 | auth_helper.WaitForAuth(); |
| 5527 | auth_helper.RestartWithAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5528 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5529 | EXPECT_EQ(1, auth_helper.auth_count()); |
| 5530 | |
| 5531 | auth_helper.WaitForAuth(); |
| 5532 | auth_helper.RestartWithAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5533 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5534 | EXPECT_EQ(2, auth_helper.auth_count()); |
| 5535 | |
| 5536 | EXPECT_THAT(auth_helper.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 5537 | EXPECT_EQ(2, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5538 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5539 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5540 | } |
| 5541 | |
| 5542 | // Makes sure that when a bound request is destroyed, a new ConnectJob is |
| 5543 | // created, if needed. |
| 5544 | TEST_F(ClientSocketPoolBaseTest, |
| 5545 | ProxyAuthCreateNewConnectJobOnDestroyBoundRequest) { |
| 5546 | CreatePool(1 /* max_sockets */, 1 /* max_sockets_per_group */); |
| 5547 | connect_job_factory_->set_job_type( |
| 5548 | TestConnectJob::kMockAuthChallengeOnceFailingJob); |
| 5549 | |
| 5550 | // First request creates a ConnectJob. |
| 5551 | TestAuthHelper auth_helper1; |
| 5552 | auth_helper1.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5553 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5554 | |
| 5555 | // A second request come in, but no new ConnectJob is needed, since the limit |
| 5556 | // has been reached. |
| 5557 | TestAuthHelper auth_helper2; |
| 5558 | auth_helper2.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5559 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5560 | |
| 5561 | // Run until the auth callback for the first request is invoked. |
| 5562 | auth_helper1.WaitForAuth(); |
| 5563 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5564 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5565 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5566 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5567 | |
| 5568 | // Make connect jobs succeed, then cancel the first request, which should |
| 5569 | // destroy the bound ConnectJob, and cause a new ConnectJob to start. |
| 5570 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 5571 | auth_helper1.handle()->Reset(); |
| 5572 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5573 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5574 | |
| 5575 | // The second ConnectJob should succeed. |
| 5576 | EXPECT_THAT(auth_helper2.WaitForResult(), IsOk()); |
| 5577 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5578 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5579 | } |
| 5580 | |
| 5581 | // Makes sure that when a bound request is destroyed, a new ConnectJob is |
| 5582 | // created for another group, if needed. |
| 5583 | TEST_F(ClientSocketPoolBaseTest, |
| 5584 | ProxyAuthCreateNewConnectJobOnDestroyBoundRequestDifferentGroups) { |
| 5585 | CreatePool(1 /* max_sockets */, 1 /* max_sockets_per_group */); |
| 5586 | connect_job_factory_->set_job_type( |
| 5587 | TestConnectJob::kMockAuthChallengeOnceFailingJob); |
| 5588 | |
| 5589 | // First request creates a ConnectJob. |
| 5590 | TestAuthHelper auth_helper1; |
| 5591 | auth_helper1.InitHandle(params_, pool_.get(), DEFAULT_PRIORITY); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5592 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5593 | |
| 5594 | // A second request come in, but no new ConnectJob is needed, since the limit |
| 5595 | // has been reached. |
| 5596 | TestAuthHelper auth_helper2; |
| 5597 | auth_helper2.InitHandle(params_, pool_.get(), DEFAULT_PRIORITY, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5598 | ClientSocketPool::RespectLimits::ENABLED, |
| 5599 | TestGroupId("b")); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5600 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5601 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5602 | |
| 5603 | // Run until the auth callback for the first request is invoked. |
| 5604 | auth_helper1.WaitForAuth(); |
| 5605 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5606 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5607 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5608 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5609 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 5610 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5611 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5612 | |
| 5613 | // Make connect jobs succeed, then cancel the first request, which should |
| 5614 | // destroy the bound ConnectJob, and cause a new ConnectJob to start for the |
| 5615 | // other group. |
| 5616 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 5617 | auth_helper1.handle()->Reset(); |
| 5618 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5619 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 5620 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5621 | |
| 5622 | // The second ConnectJob should succeed. |
| 5623 | EXPECT_THAT(auth_helper2.WaitForResult(), IsOk()); |
| 5624 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5625 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 5626 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5627 | } |
| 5628 | |
| 5629 | // Test that once an auth challenge is bound, that's the request that gets all |
| 5630 | // subsequent calls and the socket itself. |
| 5631 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthStaysBound) { |
| 5632 | CreatePool(1, 1); |
| 5633 | connect_job_factory_->set_job_type( |
| 5634 | TestConnectJob::kMockAuthChallengeTwiceJob); |
| 5635 | |
| 5636 | // First request creates a ConnectJob. |
| 5637 | TestAuthHelper auth_helper1; |
| 5638 | auth_helper1.InitHandle(params_, pool_.get(), LOWEST); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5639 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5640 | |
| 5641 | // A second, higher priority request is made. |
| 5642 | TestAuthHelper auth_helper2; |
| 5643 | auth_helper2.InitHandle(params_, pool_.get(), LOW); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5644 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5645 | |
| 5646 | // Run until the auth callback for the second request is invoked. |
| 5647 | auth_helper2.WaitForAuth(); |
| 5648 | EXPECT_EQ(0, auth_helper1.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5649 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5650 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5651 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5652 | |
| 5653 | // Start a higher priority job. It shouldn't be able to steal |auth_helper2|'s |
| 5654 | // ConnectJob. |
| 5655 | TestAuthHelper auth_helper3; |
| 5656 | auth_helper3.InitHandle(params_, pool_.get(), HIGHEST); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5657 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5658 | |
| 5659 | // Start a higher job that ignores limits, creating a hanging socket. It |
| 5660 | // shouldn't be able to steal |auth_helper2|'s ConnectJob. |
| 5661 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 5662 | TestAuthHelper auth_helper4; |
| 5663 | auth_helper4.InitHandle(params_, pool_.get(), HIGHEST, |
| 5664 | ClientSocketPool::RespectLimits::DISABLED); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5665 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5666 | |
| 5667 | // Restart with auth, and |auth_helper2|'s auth method should be invoked |
| 5668 | // again. |
| 5669 | auth_helper2.RestartWithAuth(); |
| 5670 | auth_helper2.WaitForAuth(); |
| 5671 | EXPECT_EQ(0, auth_helper1.auth_count()); |
| 5672 | EXPECT_FALSE(auth_helper1.have_result()); |
| 5673 | EXPECT_EQ(2, auth_helper2.auth_count()); |
| 5674 | EXPECT_FALSE(auth_helper2.have_result()); |
| 5675 | EXPECT_EQ(0, auth_helper3.auth_count()); |
| 5676 | EXPECT_FALSE(auth_helper3.have_result()); |
| 5677 | EXPECT_EQ(0, auth_helper4.auth_count()); |
| 5678 | EXPECT_FALSE(auth_helper4.have_result()); |
| 5679 | |
| 5680 | // Advance auth again, and |auth_helper2| should get the socket. |
| 5681 | auth_helper2.RestartWithAuth(); |
| 5682 | EXPECT_THAT(auth_helper2.WaitForResult(), IsOk()); |
| 5683 | // The hung ConnectJob for the RespectLimits::DISABLED request is still in the |
| 5684 | // socket pool. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5685 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5686 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5687 | EXPECT_EQ(0, auth_helper1.auth_count()); |
| 5688 | EXPECT_FALSE(auth_helper1.have_result()); |
| 5689 | EXPECT_EQ(0, auth_helper3.auth_count()); |
| 5690 | EXPECT_FALSE(auth_helper3.have_result()); |
| 5691 | EXPECT_EQ(0, auth_helper4.auth_count()); |
| 5692 | EXPECT_FALSE(auth_helper4.have_result()); |
| 5693 | |
| 5694 | // If the socket is returned to the socket pool, the RespectLimits::DISABLED |
| 5695 | // socket request should be able to claim it. |
| 5696 | auth_helper2.handle()->Reset(); |
| 5697 | EXPECT_THAT(auth_helper4.WaitForResult(), IsOk()); |
| 5698 | EXPECT_EQ(0, auth_helper1.auth_count()); |
| 5699 | EXPECT_FALSE(auth_helper1.have_result()); |
| 5700 | EXPECT_EQ(0, auth_helper3.auth_count()); |
| 5701 | EXPECT_FALSE(auth_helper3.have_result()); |
| 5702 | EXPECT_EQ(0, auth_helper4.auth_count()); |
| 5703 | } |
| 5704 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5705 | enum class RefreshType { |
| 5706 | kServer, |
| 5707 | kProxy, |
| 5708 | }; |
| 5709 | |
| 5710 | // Common base class to test RefreshGroup() when called from either |
| 5711 | // OnSSLConfigForServerChanged() matching a specific group or the pool's proxy. |
| 5712 | // |
| 5713 | // Tests which test behavior specific to one or the other case should use |
| 5714 | // ClientSocketPoolBaseTest directly. In particular, there is no "other group" |
| 5715 | // when the pool's proxy matches. |
| 5716 | class ClientSocketPoolBaseRefreshTest |
| 5717 | : public ClientSocketPoolBaseTest, |
| 5718 | public testing::WithParamInterface<RefreshType> { |
| 5719 | public: |
| 5720 | void CreatePoolForRefresh(int max_sockets, |
| 5721 | int max_sockets_per_group, |
| 5722 | bool enable_backup_connect_jobs = false) { |
| 5723 | switch (GetParam()) { |
| 5724 | case RefreshType::kServer: |
| 5725 | CreatePool(max_sockets, max_sockets_per_group, |
| 5726 | enable_backup_connect_jobs); |
| 5727 | break; |
| 5728 | case RefreshType::kProxy: |
| 5729 | CreatePoolWithIdleTimeouts( |
| 5730 | max_sockets, max_sockets_per_group, kUnusedIdleSocketTimeout, |
| 5731 | ClientSocketPool::used_idle_socket_timeout(), |
| 5732 | enable_backup_connect_jobs, |
Eric Orth | 5ccc3f0 | 2021-09-23 00:01:57 | [diff] [blame] | 5733 | PacResultElementToProxyServer("HTTPS myproxy:70")); |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5734 | break; |
| 5735 | } |
| 5736 | } |
| 5737 | |
| 5738 | static ClientSocketPool::GroupId GetGroupId() { |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame] | 5739 | return TestGroupId("a", 443, url::kHttpsScheme); |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5740 | } |
| 5741 | |
David Benjamin | 6dd7e88 | 2019-10-10 02:35:23 | [diff] [blame] | 5742 | static ClientSocketPool::GroupId GetGroupIdInPartition() { |
| 5743 | // Note this GroupId will match GetGroupId() unless |
| 5744 | // kPartitionConnectionsByNetworkIsolationKey is enabled. |
Matt Menke | 4807a9a | 2020-11-21 00:14:41 | [diff] [blame] | 5745 | const SchemefulSite kSite(GURL("https://b/")); |
Brianna Goldstein | a7593fca | 2022-09-28 02:59:32 | [diff] [blame^] | 5746 | const NetworkAnonymizationKey kNetworkAnonymizationKey( |
| 5747 | kSite, kSite, /*is_cross_site=*/false); |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame] | 5748 | return TestGroupId("a", 443, url::kHttpsScheme, |
David Benjamin | 6dd7e88 | 2019-10-10 02:35:23 | [diff] [blame] | 5749 | PrivacyMode::PRIVACY_MODE_DISABLED, |
Brianna Goldstein | a7593fca | 2022-09-28 02:59:32 | [diff] [blame^] | 5750 | kNetworkAnonymizationKey); |
David Benjamin | 6dd7e88 | 2019-10-10 02:35:23 | [diff] [blame] | 5751 | } |
| 5752 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5753 | void OnSSLConfigForServerChanged() { |
| 5754 | switch (GetParam()) { |
| 5755 | case RefreshType::kServer: |
| 5756 | pool_->OnSSLConfigForServerChanged(HostPortPair("a", 443)); |
| 5757 | break; |
| 5758 | case RefreshType::kProxy: |
| 5759 | pool_->OnSSLConfigForServerChanged(HostPortPair("myproxy", 70)); |
| 5760 | break; |
| 5761 | } |
| 5762 | } |
| 5763 | }; |
| 5764 | |
| 5765 | INSTANTIATE_TEST_SUITE_P(RefreshType, |
| 5766 | ClientSocketPoolBaseRefreshTest, |
| 5767 | ::testing::Values(RefreshType::kServer, |
| 5768 | RefreshType::kProxy)); |
| 5769 | |
| 5770 | TEST_P(ClientSocketPoolBaseRefreshTest, RefreshGroupCreatesNewConnectJobs) { |
| 5771 | CreatePoolForRefresh(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 5772 | const ClientSocketPool::GroupId kGroupId = GetGroupId(); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5773 | |
| 5774 | // First job will be waiting until it gets aborted. |
| 5775 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 5776 | |
| 5777 | ClientSocketHandle handle; |
| 5778 | TestCompletionCallback callback; |
| 5779 | EXPECT_THAT( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 5780 | handle.Init(kGroupId, params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5781 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5782 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5783 | pool_.get(), NetLogWithSource()), |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5784 | IsError(ERR_IO_PENDING)); |
| 5785 | |
| 5786 | // Switch connect job types, so creating a new ConnectJob will result in |
| 5787 | // success. |
| 5788 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 5789 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5790 | OnSSLConfigForServerChanged(); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5791 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 5792 | ASSERT_TRUE(handle.socket()); |
| 5793 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5794 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId)); |
| 5795 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(kGroupId)); |
| 5796 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(kGroupId)); |
| 5797 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId)); |
| 5798 | } |
| 5799 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5800 | TEST_P(ClientSocketPoolBaseRefreshTest, RefreshGroupClosesIdleConnectJobs) { |
David Benjamin | 6dd7e88 | 2019-10-10 02:35:23 | [diff] [blame] | 5801 | base::test::ScopedFeatureList feature_list; |
| 5802 | feature_list.InitAndEnableFeature( |
| 5803 | features::kPartitionConnectionsByNetworkIsolationKey); |
| 5804 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5805 | CreatePoolForRefresh(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 5806 | const ClientSocketPool::GroupId kGroupId = GetGroupId(); |
David Benjamin | 6dd7e88 | 2019-10-10 02:35:23 | [diff] [blame] | 5807 | const ClientSocketPool::GroupId kGroupIdInPartition = GetGroupIdInPartition(); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5808 | |
Tsuyoshi Horo | 1d8eb53f | 2022-06-02 07:12:21 | [diff] [blame] | 5809 | EXPECT_EQ( |
| 5810 | OK, pool_->RequestSockets(kGroupId, params_, absl::nullopt, 2, |
| 5811 | CompletionOnceCallback(), NetLogWithSource())); |
| 5812 | |
| 5813 | EXPECT_EQ( |
| 5814 | OK, pool_->RequestSockets(kGroupIdInPartition, params_, absl::nullopt, 2, |
| 5815 | CompletionOnceCallback(), NetLogWithSource())); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5816 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId)); |
David Benjamin | 6dd7e88 | 2019-10-10 02:35:23 | [diff] [blame] | 5817 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupIdInPartition)); |
| 5818 | EXPECT_EQ(4, pool_->IdleSocketCount()); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5819 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(kGroupId)); |
David Benjamin | 6dd7e88 | 2019-10-10 02:35:23 | [diff] [blame] | 5820 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(kGroupIdInPartition)); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5821 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5822 | OnSSLConfigForServerChanged(); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5823 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5824 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupId)); |
David Benjamin | 6dd7e88 | 2019-10-10 02:35:23 | [diff] [blame] | 5825 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupIdInPartition)); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5826 | } |
| 5827 | |
| 5828 | TEST_F(ClientSocketPoolBaseTest, |
| 5829 | RefreshGroupDoesNotCloseIdleConnectJobsInOtherGroup) { |
| 5830 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5831 | const ClientSocketPool::GroupId kGroupId = |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame] | 5832 | TestGroupId("a", 443, url::kHttpsScheme); |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5833 | const ClientSocketPool::GroupId kOtherGroupId = |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame] | 5834 | TestGroupId("b", 443, url::kHttpsScheme); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5835 | |
Tsuyoshi Horo | 1d8eb53f | 2022-06-02 07:12:21 | [diff] [blame] | 5836 | EXPECT_EQ( |
| 5837 | OK, pool_->RequestSockets(kOtherGroupId, params_, absl::nullopt, 2, |
| 5838 | CompletionOnceCallback(), NetLogWithSource())); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5839 | ASSERT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 5840 | EXPECT_EQ(2, pool_->IdleSocketCount()); |
| 5841 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(kOtherGroupId)); |
| 5842 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5843 | pool_->OnSSLConfigForServerChanged(HostPortPair("a", 443)); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5844 | ASSERT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 5845 | EXPECT_EQ(2, pool_->IdleSocketCount()); |
| 5846 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(kOtherGroupId)); |
| 5847 | } |
| 5848 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5849 | TEST_P(ClientSocketPoolBaseRefreshTest, RefreshGroupPreventsSocketReuse) { |
| 5850 | CreatePoolForRefresh(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 5851 | const ClientSocketPool::GroupId kGroupId = GetGroupId(); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5852 | |
| 5853 | ClientSocketHandle handle; |
| 5854 | TestCompletionCallback callback; |
| 5855 | EXPECT_THAT( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 5856 | handle.Init(kGroupId, params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5857 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5858 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5859 | pool_.get(), NetLogWithSource()), |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5860 | IsOk()); |
| 5861 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId)); |
| 5862 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId)); |
| 5863 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5864 | OnSSLConfigForServerChanged(); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5865 | |
| 5866 | handle.Reset(); |
| 5867 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5868 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupId)); |
| 5869 | } |
| 5870 | |
| 5871 | TEST_F(ClientSocketPoolBaseTest, |
| 5872 | RefreshGroupDoesNotPreventSocketReuseInOtherGroup) { |
| 5873 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5874 | const ClientSocketPool::GroupId kGroupId = |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame] | 5875 | TestGroupId("a", 443, url::kHttpsScheme); |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5876 | const ClientSocketPool::GroupId kOtherGroupId = |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame] | 5877 | TestGroupId("b", 443, url::kHttpsScheme); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5878 | |
| 5879 | ClientSocketHandle handle; |
| 5880 | TestCompletionCallback callback; |
| 5881 | EXPECT_THAT( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 5882 | handle.Init(kOtherGroupId, params_, absl::nullopt, DEFAULT_PRIORITY, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5883 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5884 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5885 | pool_.get(), NetLogWithSource()), |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5886 | IsOk()); |
| 5887 | ASSERT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 5888 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kOtherGroupId)); |
| 5889 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5890 | pool_->OnSSLConfigForServerChanged(HostPortPair("a", 443)); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5891 | |
| 5892 | handle.Reset(); |
| 5893 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 5894 | ASSERT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 5895 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(kOtherGroupId)); |
| 5896 | } |
| 5897 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5898 | TEST_P(ClientSocketPoolBaseRefreshTest, |
| 5899 | RefreshGroupReplacesBoundConnectJobOnConnect) { |
| 5900 | CreatePoolForRefresh(1, 1); |
| 5901 | const ClientSocketPool::GroupId kGroupId = GetGroupId(); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5902 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5903 | |
| 5904 | TestAuthHelper auth_helper; |
| 5905 | auth_helper.InitHandle(params_, pool_.get(), DEFAULT_PRIORITY, |
| 5906 | ClientSocketPool::RespectLimits::ENABLED, kGroupId); |
| 5907 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(kGroupId)); |
| 5908 | |
| 5909 | auth_helper.WaitForAuth(); |
| 5910 | |
| 5911 | // This should update the generation, but not cancel the old ConnectJob - it's |
| 5912 | // not safe to do anything while waiting on the original ConnectJob. |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5913 | OnSSLConfigForServerChanged(); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5914 | |
| 5915 | // Providing auth credentials and restarting the request with them will cause |
| 5916 | // the ConnectJob to complete successfully, but the result will be discarded |
| 5917 | // because of the generation mismatch. |
| 5918 | auth_helper.RestartWithAuth(); |
| 5919 | |
| 5920 | // Despite using ConnectJobs that simulate a single challenge, a second |
| 5921 | // challenge will be seen, due to using a new ConnectJob. |
| 5922 | auth_helper.WaitForAuth(); |
| 5923 | auth_helper.RestartWithAuth(); |
| 5924 | |
| 5925 | EXPECT_THAT(auth_helper.WaitForResult(), IsOk()); |
| 5926 | EXPECT_TRUE(auth_helper.handle()->socket()); |
| 5927 | EXPECT_EQ(2, auth_helper.auth_count()); |
| 5928 | |
| 5929 | // When released, the socket will be returned to the socket pool, and |
| 5930 | // available for reuse. |
| 5931 | auth_helper.handle()->Reset(); |
| 5932 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 5933 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId)); |
| 5934 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(kGroupId)); |
| 5935 | } |
| 5936 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5937 | TEST_F(ClientSocketPoolBaseTest, RefreshProxyRefreshesAllGroups) { |
| 5938 | CreatePoolWithIdleTimeouts(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 5939 | kUnusedIdleSocketTimeout, |
| 5940 | ClientSocketPool::used_idle_socket_timeout(), |
| 5941 | false /* no backup connect jobs */, |
Eric Orth | 5ccc3f0 | 2021-09-23 00:01:57 | [diff] [blame] | 5942 | PacResultElementToProxyServer("HTTPS myproxy:70")); |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5943 | |
| 5944 | const ClientSocketPool::GroupId kGroupId1 = |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame] | 5945 | TestGroupId("a", 443, url::kHttpsScheme); |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5946 | const ClientSocketPool::GroupId kGroupId2 = |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame] | 5947 | TestGroupId("b", 443, url::kHttpsScheme); |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5948 | const ClientSocketPool::GroupId kGroupId3 = |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame] | 5949 | TestGroupId("c", 443, url::kHttpsScheme); |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5950 | |
| 5951 | // Make three sockets in three different groups. The third socket is released |
| 5952 | // to the pool as idle. |
| 5953 | ClientSocketHandle handle1, handle2, handle3; |
| 5954 | TestCompletionCallback callback; |
| 5955 | EXPECT_THAT( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 5956 | handle1.Init(kGroupId1, params_, absl::nullopt, DEFAULT_PRIORITY, |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5957 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5958 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5959 | pool_.get(), NetLogWithSource()), |
| 5960 | IsOk()); |
| 5961 | EXPECT_THAT( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 5962 | handle2.Init(kGroupId2, params_, absl::nullopt, DEFAULT_PRIORITY, |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5963 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5964 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5965 | pool_.get(), NetLogWithSource()), |
| 5966 | IsOk()); |
| 5967 | EXPECT_THAT( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 5968 | handle3.Init(kGroupId3, params_, absl::nullopt, DEFAULT_PRIORITY, |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5969 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5970 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5971 | pool_.get(), NetLogWithSource()), |
| 5972 | IsOk()); |
| 5973 | handle3.Reset(); |
| 5974 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId1)); |
| 5975 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId1)); |
| 5976 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId2)); |
| 5977 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId2)); |
| 5978 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId3)); |
| 5979 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(kGroupId3)); |
| 5980 | |
| 5981 | // Changes to some other proxy do not affect the pool. The idle socket remains |
| 5982 | // alive and closing |handle2| makes the socket available for the pool. |
| 5983 | pool_->OnSSLConfigForServerChanged(HostPortPair("someotherproxy", 70)); |
| 5984 | |
| 5985 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId1)); |
| 5986 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId1)); |
| 5987 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId2)); |
| 5988 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId2)); |
| 5989 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId3)); |
| 5990 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(kGroupId3)); |
| 5991 | |
| 5992 | handle2.Reset(); |
| 5993 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId2)); |
| 5994 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(kGroupId2)); |
| 5995 | |
| 5996 | // Changes to the matching proxy refreshes all groups. |
| 5997 | pool_->OnSSLConfigForServerChanged(HostPortPair("myproxy", 70)); |
| 5998 | |
| 5999 | // Idle sockets are closed. |
| 6000 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 6001 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupId2)); |
| 6002 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupId3)); |
| 6003 | |
| 6004 | // The active socket, however, continues to be active. |
| 6005 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId1)); |
| 6006 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId1)); |
| 6007 | |
| 6008 | // Closing it does not make it available for the pool. |
| 6009 | handle1.Reset(); |
| 6010 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 6011 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupId1)); |
| 6012 | } |
| 6013 | |
| 6014 | TEST_F(ClientSocketPoolBaseTest, RefreshBothPrivacyAndNormalSockets) { |
| 6015 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 6016 | |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame] | 6017 | const ClientSocketPool::GroupId kGroupId = TestGroupId( |
| 6018 | "a", 443, url::kHttpsScheme, PrivacyMode::PRIVACY_MODE_DISABLED); |
| 6019 | const ClientSocketPool::GroupId kGroupIdPrivacy = TestGroupId( |
| 6020 | "a", 443, url::kHttpsScheme, PrivacyMode::PRIVACY_MODE_ENABLED); |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 6021 | const ClientSocketPool::GroupId kOtherGroupId = |
Eric Orth | a2e777321 | 2021-06-22 21:49:55 | [diff] [blame] | 6022 | TestGroupId("b", 443, url::kHttpsScheme); |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 6023 | |
| 6024 | // Make a socket in each groups. |
| 6025 | ClientSocketHandle handle1, handle2, handle3; |
| 6026 | TestCompletionCallback callback; |
| 6027 | EXPECT_THAT( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 6028 | handle1.Init(kGroupId, params_, absl::nullopt, DEFAULT_PRIORITY, |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 6029 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 6030 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 6031 | pool_.get(), NetLogWithSource()), |
| 6032 | IsOk()); |
| 6033 | EXPECT_THAT( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 6034 | handle2.Init(kGroupIdPrivacy, params_, absl::nullopt, DEFAULT_PRIORITY, |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 6035 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 6036 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 6037 | pool_.get(), NetLogWithSource()), |
| 6038 | IsOk()); |
| 6039 | EXPECT_THAT( |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 6040 | handle3.Init(kOtherGroupId, params_, absl::nullopt, DEFAULT_PRIORITY, |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 6041 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 6042 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 6043 | pool_.get(), NetLogWithSource()), |
| 6044 | IsOk()); |
| 6045 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId)); |
| 6046 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId)); |
| 6047 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupIdPrivacy)); |
| 6048 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupIdPrivacy)); |
| 6049 | ASSERT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 6050 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kOtherGroupId)); |
| 6051 | |
| 6052 | pool_->OnSSLConfigForServerChanged(HostPortPair("a", 443)); |
| 6053 | |
| 6054 | // Active sockets continue to be active. |
| 6055 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId)); |
| 6056 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId)); |
| 6057 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupIdPrivacy)); |
| 6058 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupIdPrivacy)); |
| 6059 | ASSERT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 6060 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kOtherGroupId)); |
| 6061 | |
| 6062 | // Closing them leaves kOtherGroupId alone, but kGroupId and kGroupIdPrivacy |
| 6063 | // are unusable. |
| 6064 | handle1.Reset(); |
| 6065 | handle2.Reset(); |
| 6066 | handle3.Reset(); |
| 6067 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 6068 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupId)); |
| 6069 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupIdPrivacy)); |
| 6070 | EXPECT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 6071 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(kOtherGroupId)); |
| 6072 | } |
| 6073 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 6074 | } // namespace |
| 6075 | |
| 6076 | } // namespace net |