[email protected] | e34400c3 | 2012-01-24 02:49:33 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5 | #include "net/socket/transport_client_socket_pool.h" |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 6 | |
tbansal | f82cc8e | 2015-10-14 20:05:49 | [diff] [blame] | 7 | #include <stdint.h> |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 8 | #include <utility> |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 9 | #include <vector> |
| 10 | |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 11 | #include "base/bind.h" |
[email protected] | 2041cf34 | 2010-02-19 03:15:59 | [diff] [blame] | 12 | #include "base/callback.h" |
danakj | db9ae794 | 2020-11-11 16:01:35 | [diff] [blame] | 13 | #include "base/callback_helpers.h" |
Hans Wennborg | 0924470b | 2020-04-27 21:08:05 | [diff] [blame] | 14 | #include "base/check_op.h" |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 15 | #include "base/location.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" |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 19 | #include "base/optional.h" |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 20 | #include "base/run_loop.h" |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 21 | #include "base/single_thread_task_runner.h" |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 22 | #include "base/stl_util.h" |
[email protected] | fc9be580 | 2013-06-11 10:56:51 | [diff] [blame] | 23 | #include "base/strings/string_number_conversions.h" |
[email protected] | 18b57741 | 2013-07-18 04:19:15 | [diff] [blame] | 24 | #include "base/strings/stringprintf.h" |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 25 | #include "base/test/scoped_feature_list.h" |
[email protected] | f214f879 | 2011-01-01 02:17:08 | [diff] [blame] | 26 | #include "base/threading/platform_thread.h" |
gab | f767595f | 2016-05-11 18:50:35 | [diff] [blame] | 27 | #include "base/threading/thread_task_runner_handle.h" |
[email protected] | f3a1c64 | 2011-07-12 19:15:03 | [diff] [blame] | 28 | #include "base/values.h" |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 29 | #include "net/base/features.h" |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 30 | #include "net/base/load_timing_info.h" |
[email protected] | b258e079 | 2013-01-12 07:11:59 | [diff] [blame] | 31 | #include "net/base/load_timing_info_test_util.h" |
[email protected] | d8eb8424 | 2010-09-25 02:25:06 | [diff] [blame] | 32 | #include "net/base/net_errors.h" |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 33 | #include "net/base/network_isolation_key.h" |
Matt Menke | bdf77780 | 2019-04-22 19:38:59 | [diff] [blame] | 34 | #include "net/base/privacy_mode.h" |
Matt Menke | aafff54 | 2019-04-22 22:09:36 | [diff] [blame] | 35 | #include "net/base/proxy_server.h" |
[email protected] | ac790b4 | 2009-12-02 04:31:31 | [diff] [blame] | 36 | #include "net/base/request_priority.h" |
Matt Menke | 4807a9a | 2020-11-21 00:14:41 | [diff] [blame] | 37 | #include "net/base/schemeful_site.h" |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 38 | #include "net/base/test_completion_callback.h" |
dalyk | edd30d98 | 2019-12-16 15:31:10 | [diff] [blame] | 39 | #include "net/dns/public/resolve_error_info.h" |
Ben Schwartz | 3ff4dc1e6 | 2021-04-27 21:15:23 | [diff] [blame^] | 40 | #include "net/dns/public/secure_dns_policy.h" |
[email protected] | 277d594 | 2010-08-11 21:02:35 | [diff] [blame] | 41 | #include "net/http/http_response_headers.h" |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 42 | #include "net/http/http_response_info.h" |
eroman | 87c53d6 | 2015-04-02 06:51:07 | [diff] [blame] | 43 | #include "net/log/net_log.h" |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 44 | #include "net/log/net_log_event_type.h" |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 45 | #include "net/log/net_log_source.h" |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 46 | #include "net/log/net_log_source_type.h" |
mmenke | 16a7cbdd | 2015-04-24 23:00:56 | [diff] [blame] | 47 | #include "net/log/test_net_log.h" |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 48 | #include "net/log/test_net_log_util.h" |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 49 | #include "net/socket/client_socket_factory.h" |
| 50 | #include "net/socket/client_socket_handle.h" |
tfarina | 5dd13c2 | 2016-11-16 12:08:26 | [diff] [blame] | 51 | #include "net/socket/datagram_client_socket.h" |
tbansal | ca83c00 | 2016-04-28 20:56:28 | [diff] [blame] | 52 | #include "net/socket/socket_performance_watcher.h" |
Paul Jensen | 8d6f87ec | 2018-01-13 00:46:54 | [diff] [blame] | 53 | #include "net/socket/socket_tag.h" |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 54 | #include "net/socket/socket_test_util.h" |
[email protected] | 18ccfdb | 2013-08-15 00:13:44 | [diff] [blame] | 55 | #include "net/socket/ssl_client_socket.h" |
[email protected] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame] | 56 | #include "net/socket/stream_socket.h" |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 57 | #include "net/socket/transport_connect_job.h" |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 58 | #include "net/ssl/ssl_cert_request_info.h" |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 59 | #include "net/test/gtest_util.h" |
Gabriel Charette | c710874 | 2019-08-23 03:31:40 | [diff] [blame] | 60 | #include "net/test/test_with_task_environment.h" |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 61 | #include "net/traffic_annotation/network_traffic_annotation.h" |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 62 | #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 63 | #include "testing/gmock/include/gmock/gmock.h" |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 64 | #include "testing/gtest/include/gtest/gtest.h" |
| 65 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 66 | using net::test::IsError; |
| 67 | using net::test::IsOk; |
| 68 | |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 69 | using ::testing::Invoke; |
| 70 | using ::testing::Return; |
| 71 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 72 | namespace net { |
| 73 | |
| 74 | namespace { |
| 75 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 76 | const int kDefaultMaxSockets = 4; |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 77 | const int kDefaultMaxSocketsPerGroup = 2; |
Tarun Bansal | a763509 | 2019-02-20 10:00:59 | [diff] [blame] | 78 | constexpr base::TimeDelta kUnusedIdleSocketTimeout = |
| 79 | base::TimeDelta::FromSeconds(10); |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 80 | |
Matt Menke | bdf77780 | 2019-04-22 19:38:59 | [diff] [blame] | 81 | ClientSocketPool::GroupId TestGroupId( |
| 82 | const std::string& host, |
| 83 | int port = 80, |
| 84 | ClientSocketPool::SocketType socket_type = |
| 85 | ClientSocketPool::SocketType::kHttp, |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 86 | PrivacyMode privacy_mode = PrivacyMode::PRIVACY_MODE_DISABLED, |
| 87 | NetworkIsolationKey network_isolation_key = NetworkIsolationKey()) { |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 88 | return ClientSocketPool::GroupId(HostPortPair(host, port), socket_type, |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 89 | privacy_mode, network_isolation_key, |
Ben Schwartz | 3ff4dc1e6 | 2021-04-27 21:15:23 | [diff] [blame^] | 90 | SecureDnsPolicy::kAllow); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 91 | } |
| 92 | |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 93 | // Make sure |handle| sets load times correctly when it has been assigned a |
| 94 | // reused socket. |
| 95 | void TestLoadTimingInfoConnectedReused(const ClientSocketHandle& handle) { |
| 96 | LoadTimingInfo load_timing_info; |
| 97 | // Only pass true in as |is_reused|, as in general, HttpStream types should |
| 98 | // have stricter concepts of reuse than socket pools. |
| 99 | EXPECT_TRUE(handle.GetLoadTimingInfo(true, &load_timing_info)); |
| 100 | |
| 101 | EXPECT_EQ(true, load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 102 | EXPECT_NE(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 103 | |
[email protected] | b258e079 | 2013-01-12 07:11:59 | [diff] [blame] | 104 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 105 | ExpectLoadTimingHasOnlyConnectionTimes(load_timing_info); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | // Make sure |handle| sets load times correctly when it has been assigned a |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 109 | // fresh socket. Also runs TestLoadTimingInfoConnectedReused, since the owner |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 110 | // of a connection where |is_reused| is false may consider the connection |
| 111 | // reused. |
| 112 | void TestLoadTimingInfoConnectedNotReused(const ClientSocketHandle& handle) { |
| 113 | EXPECT_FALSE(handle.is_reused()); |
| 114 | |
| 115 | LoadTimingInfo load_timing_info; |
| 116 | EXPECT_TRUE(handle.GetLoadTimingInfo(false, &load_timing_info)); |
| 117 | |
| 118 | EXPECT_FALSE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 119 | EXPECT_NE(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 120 | |
[email protected] | b258e079 | 2013-01-12 07:11:59 | [diff] [blame] | 121 | ExpectConnectTimingHasTimes(load_timing_info.connect_timing, |
| 122 | CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY); |
| 123 | ExpectLoadTimingHasOnlyConnectionTimes(load_timing_info); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 124 | |
| 125 | TestLoadTimingInfoConnectedReused(handle); |
| 126 | } |
| 127 | |
| 128 | // Make sure |handle| sets load times correctly, in the case that it does not |
| 129 | // currently have a socket. |
| 130 | void TestLoadTimingInfoNotConnected(const ClientSocketHandle& handle) { |
| 131 | // Should only be set to true once a socket is assigned, if at all. |
| 132 | EXPECT_FALSE(handle.is_reused()); |
| 133 | |
| 134 | LoadTimingInfo load_timing_info; |
| 135 | EXPECT_FALSE(handle.GetLoadTimingInfo(false, &load_timing_info)); |
| 136 | |
| 137 | EXPECT_FALSE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 138 | EXPECT_EQ(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 139 | |
[email protected] | b258e079 | 2013-01-12 07:11:59 | [diff] [blame] | 140 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 141 | ExpectLoadTimingHasOnlyConnectionTimes(load_timing_info); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 142 | } |
| 143 | |
[email protected] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame] | 144 | class MockClientSocket : public StreamSocket { |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 145 | public: |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 146 | explicit MockClientSocket(net::NetLog* net_log) |
| 147 | : connected_(false), |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 148 | has_unread_data_(false), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 149 | net_log_(NetLogWithSource::Make(net_log, NetLogSourceType::SOCKET)), |
Charlie Harrison | 3e4c062 | 2018-05-13 15:44:30 | [diff] [blame] | 150 | was_used_to_convey_data_(false) {} |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 151 | |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 152 | // Sets whether the socket has unread data. If true, the next call to Read() |
| 153 | // will return 1 byte and IsConnectedAndIdle() will return false. |
| 154 | void set_has_unread_data(bool has_unread_data) { |
| 155 | has_unread_data_ = has_unread_data; |
| 156 | } |
| 157 | |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 158 | // Socket implementation. |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 159 | int Read(IOBuffer* /* buf */, |
| 160 | int len, |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 161 | CompletionOnceCallback /* callback */) override { |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 162 | if (has_unread_data_ && len > 0) { |
| 163 | has_unread_data_ = false; |
| 164 | was_used_to_convey_data_ = true; |
| 165 | return 1; |
| 166 | } |
[email protected] | e86df8dc | 2013-03-30 13:18:28 | [diff] [blame] | 167 | return ERR_UNEXPECTED; |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 168 | } |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 169 | |
[email protected] | a2b2cfc | 2017-12-06 09:06:08 | [diff] [blame] | 170 | int Write( |
| 171 | IOBuffer* /* buf */, |
| 172 | int len, |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 173 | CompletionOnceCallback /* callback */, |
[email protected] | a2b2cfc | 2017-12-06 09:06:08 | [diff] [blame] | 174 | const NetworkTrafficAnnotationTag& /*traffic_annotation*/) override { |
[email protected] | 0f873e8 | 2010-09-02 16:09:01 | [diff] [blame] | 175 | was_used_to_convey_data_ = true; |
| 176 | return len; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 177 | } |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 178 | int SetReceiveBufferSize(int32_t size) override { return OK; } |
| 179 | int SetSendBufferSize(int32_t size) override { return OK; } |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 180 | |
[email protected] | dbf036f | 2011-12-06 23:33:24 | [diff] [blame] | 181 | // StreamSocket implementation. |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 182 | int Connect(CompletionOnceCallback callback) override { |
[email protected] | dbf036f | 2011-12-06 23:33:24 | [diff] [blame] | 183 | connected_ = true; |
| 184 | return OK; |
| 185 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 186 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 187 | void Disconnect() override { connected_ = false; } |
| 188 | bool IsConnected() const override { return connected_; } |
| 189 | bool IsConnectedAndIdle() const override { |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 190 | return connected_ && !has_unread_data_; |
| 191 | } |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 192 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 193 | int GetPeerAddress(IPEndPoint* /* address */) const override { |
[email protected] | 9f864b3 | 2010-01-20 15:01:16 | [diff] [blame] | 194 | return ERR_UNEXPECTED; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 195 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 196 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 197 | int GetLocalAddress(IPEndPoint* /* address */) const override { |
[email protected] | e7f74da | 2011-04-19 23:49:35 | [diff] [blame] | 198 | return ERR_UNEXPECTED; |
| 199 | } |
| 200 | |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 201 | const NetLogWithSource& NetLog() const override { return net_log_; } |
[email protected] | a2006ece | 2010-04-23 16:44:02 | [diff] [blame] | 202 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 203 | bool WasEverUsed() const override { return was_used_to_convey_data_; } |
tfarina | 2846404c | 2016-12-25 14:31:37 | [diff] [blame] | 204 | bool WasAlpnNegotiated() const override { return false; } |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 205 | NextProto GetNegotiatedProtocol() const override { return kProtoUnknown; } |
| 206 | bool GetSSLInfo(SSLInfo* ssl_info) override { return false; } |
ttuttle | 23fdb7b | 2015-05-15 01:28:03 | [diff] [blame] | 207 | void GetConnectionAttempts(ConnectionAttempts* out) const override { |
| 208 | out->clear(); |
| 209 | } |
| 210 | void ClearConnectionAttempts() override {} |
| 211 | void AddConnectionAttempts(const ConnectionAttempts& attempts) override {} |
tbansal | f82cc8e | 2015-10-14 20:05:49 | [diff] [blame] | 212 | int64_t GetTotalReceivedBytes() const override { |
| 213 | NOTIMPLEMENTED(); |
| 214 | return 0; |
| 215 | } |
Paul Jensen | 0f49dec | 2017-12-12 23:39:58 | [diff] [blame] | 216 | void ApplySocketTag(const SocketTag& tag) override {} |
[email protected] | 9b5614a | 2010-08-25 20:29:45 | [diff] [blame] | 217 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 218 | private: |
| 219 | bool connected_; |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 220 | bool has_unread_data_; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 221 | NetLogWithSource net_log_; |
[email protected] | 0f873e8 | 2010-09-02 16:09:01 | [diff] [blame] | 222 | bool was_used_to_convey_data_; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 223 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 224 | DISALLOW_COPY_AND_ASSIGN(MockClientSocket); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 225 | }; |
| 226 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 227 | class TestConnectJob; |
| 228 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 229 | class MockClientSocketFactory : public ClientSocketFactory { |
| 230 | public: |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 231 | MockClientSocketFactory() : allocation_count_(0) {} |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 232 | |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 233 | std::unique_ptr<DatagramClientSocket> CreateDatagramClientSocket( |
[email protected] | 5370c01 | 2011-06-29 03:47:04 | [diff] [blame] | 234 | DatagramSocket::BindType bind_type, |
[email protected] | 98b0e58 | 2011-06-22 14:31:41 | [diff] [blame] | 235 | NetLog* net_log, |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 236 | const NetLogSource& source) override { |
[email protected] | 98b0e58 | 2011-06-22 14:31:41 | [diff] [blame] | 237 | NOTREACHED(); |
David Benjamin | 24725be | 2019-07-24 20:57:18 | [diff] [blame] | 238 | return nullptr; |
[email protected] | 98b0e58 | 2011-06-22 14:31:41 | [diff] [blame] | 239 | } |
| 240 | |
Helen Li | d5bb922 | 2018-04-12 15:33:09 | [diff] [blame] | 241 | std::unique_ptr<TransportClientSocket> CreateTransportClientSocket( |
[email protected] | 0a0b768 | 2010-08-25 17:08:07 | [diff] [blame] | 242 | const AddressList& addresses, |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 243 | std::unique_ptr< |
| 244 | SocketPerformanceWatcher> /* socket_performance_watcher */, |
Eric Roman | 2bc7716 | 2020-09-16 18:30:45 | [diff] [blame] | 245 | NetworkQualityEstimator* /* network_quality_estimator */, |
[email protected] | 0a0b768 | 2010-08-25 17:08:07 | [diff] [blame] | 246 | NetLog* /* net_log */, |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 247 | const NetLogSource& /*source*/) override { |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 248 | allocation_count_++; |
Helen Li | d5bb922 | 2018-04-12 15:33:09 | [diff] [blame] | 249 | return nullptr; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 250 | } |
| 251 | |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 252 | std::unique_ptr<SSLClientSocket> CreateSSLClientSocket( |
David Benjamin | 24725be | 2019-07-24 20:57:18 | [diff] [blame] | 253 | SSLClientContext* context, |
Matt Menke | 841fc41 | 2019-03-05 23:20:12 | [diff] [blame] | 254 | std::unique_ptr<StreamSocket> stream_socket, |
[email protected] | 4f4de7e6 | 2010-11-12 19:55:27 | [diff] [blame] | 255 | const HostPortPair& host_and_port, |
David Benjamin | 24725be | 2019-07-24 20:57:18 | [diff] [blame] | 256 | const SSLConfig& ssl_config) override { |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 257 | NOTIMPLEMENTED(); |
David Benjamin | 24725be | 2019-07-24 20:57:18 | [diff] [blame] | 258 | return nullptr; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 259 | } |
Matt Menke | fd95692 | 2019-02-04 23:44:03 | [diff] [blame] | 260 | |
Matt Menke | 52cd95a | 2019-02-08 06:16:27 | [diff] [blame] | 261 | std::unique_ptr<ProxyClientSocket> CreateProxyClientSocket( |
| 262 | std::unique_ptr<StreamSocket> stream_socket, |
| 263 | const std::string& user_agent, |
| 264 | const HostPortPair& endpoint, |
| 265 | const ProxyServer& proxy_server, |
| 266 | HttpAuthController* http_auth_controller, |
| 267 | bool tunnel, |
| 268 | bool using_spdy, |
| 269 | NextProto negotiated_protocol, |
| 270 | ProxyDelegate* proxy_delegate, |
Matt Menke | 52cd95a | 2019-02-08 06:16:27 | [diff] [blame] | 271 | const NetworkTrafficAnnotationTag& traffic_annotation) override { |
| 272 | NOTIMPLEMENTED(); |
| 273 | return nullptr; |
| 274 | } |
| 275 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 276 | void WaitForSignal(TestConnectJob* job) { waiting_jobs_.push_back(job); } |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 277 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 278 | void SignalJobs(); |
| 279 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 280 | void SignalJob(size_t job); |
| 281 | |
| 282 | void SetJobLoadState(size_t job, LoadState load_state); |
| 283 | |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 284 | // Sets the HasConnectionEstablished value of the specified job to true, |
| 285 | // without invoking the callback. |
| 286 | void SetJobHasEstablishedConnection(size_t job); |
| 287 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 288 | int allocation_count() const { return allocation_count_; } |
| 289 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 290 | private: |
| 291 | int allocation_count_; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 292 | std::vector<TestConnectJob*> waiting_jobs_; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 293 | }; |
| 294 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 295 | class TestConnectJob : public ConnectJob { |
| 296 | public: |
| 297 | enum JobType { |
| 298 | kMockJob, |
| 299 | kMockFailingJob, |
| 300 | kMockPendingJob, |
| 301 | kMockPendingFailingJob, |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 302 | kMockWaitingJob, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 303 | |
| 304 | // Certificate errors return a socket in addition to an error code. |
| 305 | kMockCertErrorJob, |
| 306 | kMockPendingCertErrorJob, |
| 307 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 308 | kMockAdditionalErrorStateJob, |
| 309 | kMockPendingAdditionalErrorStateJob, |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 310 | kMockUnreadDataJob, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 311 | |
| 312 | kMockAuthChallengeOnceJob, |
| 313 | kMockAuthChallengeTwiceJob, |
| 314 | kMockAuthChallengeOnceFailingJob, |
| 315 | kMockAuthChallengeTwiceFailingJob, |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 316 | }; |
| 317 | |
[email protected] | 994d493 | 2010-07-12 17:55:13 | [diff] [blame] | 318 | // The kMockPendingJob uses a slight delay before allowing the connect |
| 319 | // to complete. |
| 320 | static const int kPendingConnectDelay = 2; |
| 321 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 322 | TestConnectJob(JobType job_type, |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 323 | RequestPriority request_priority, |
| 324 | SocketTag socket_tag, |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 325 | base::TimeDelta timeout_duration, |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 326 | const CommonConnectJobParams* common_connect_job_params, |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 327 | ConnectJob::Delegate* delegate, |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 328 | MockClientSocketFactory* client_socket_factory) |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 329 | : ConnectJob(request_priority, |
| 330 | socket_tag, |
Matt Menke | 1a6c92d | 2019-02-23 00:25:38 | [diff] [blame] | 331 | timeout_duration, |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 332 | common_connect_job_params, |
Matt Menke | 1a6c92d | 2019-02-23 00:25:38 | [diff] [blame] | 333 | delegate, |
| 334 | nullptr /* net_log */, |
| 335 | NetLogSourceType::TRANSPORT_CONNECT_JOB, |
| 336 | NetLogEventType::TRANSPORT_CONNECT_JOB_CONNECT), |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 337 | job_type_(job_type), |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 338 | client_socket_factory_(client_socket_factory), |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 339 | load_state_(LOAD_STATE_IDLE), |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 340 | has_established_connection_(false), |
Jeremy Roman | d54000b2 | 2019-07-08 18:40:16 | [diff] [blame] | 341 | store_additional_error_state_(false) {} |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 342 | |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 343 | void Signal() { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 344 | DoConnect(waiting_success_, true /* async */, false /* recoverable */); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 345 | } |
| 346 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 347 | void set_load_state(LoadState load_state) { load_state_ = load_state; } |
| 348 | |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 349 | void set_has_established_connection() { |
| 350 | DCHECK(!has_established_connection_); |
| 351 | has_established_connection_ = true; |
| 352 | } |
| 353 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 354 | // From ConnectJob: |
| 355 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 356 | LoadState GetLoadState() const override { return load_state_; } |
[email protected] | 4645135 | 2009-09-01 14:54:21 | [diff] [blame] | 357 | |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 358 | bool HasEstablishedConnection() const override { |
| 359 | return has_established_connection_; |
| 360 | } |
| 361 | |
dalyk | edd30d98 | 2019-12-16 15:31:10 | [diff] [blame] | 362 | ResolveErrorInfo GetResolveErrorInfo() const override { |
| 363 | return ResolveErrorInfo(OK); |
| 364 | } |
| 365 | |
Matt Menke | 6f84d1f1 | 2019-04-11 19:26:47 | [diff] [blame] | 366 | bool IsSSLError() const override { return store_additional_error_state_; } |
| 367 | |
| 368 | scoped_refptr<SSLCertRequestInfo> GetCertRequestInfo() override { |
| 369 | if (store_additional_error_state_) |
| 370 | return base::MakeRefCounted<SSLCertRequestInfo>(); |
| 371 | return nullptr; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 372 | } |
| 373 | |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 374 | private: |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 375 | // From ConnectJob: |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 376 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 377 | int ConnectInternal() override { |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 378 | AddressList ignored; |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 379 | client_socket_factory_->CreateTransportClientSocket( |
Eric Roman | 2bc7716 | 2020-09-16 18:30:45 | [diff] [blame] | 380 | ignored, nullptr, nullptr, nullptr, NetLogSource()); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 381 | switch (job_type_) { |
| 382 | case kMockJob: |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 383 | return DoConnect(true /* successful */, false /* sync */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 384 | false /* cert_error */); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 385 | case kMockFailingJob: |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 386 | return DoConnect(false /* error */, false /* sync */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 387 | false /* cert_error */); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 388 | case kMockPendingJob: |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 389 | set_load_state(LOAD_STATE_CONNECTING); |
[email protected] | 6b17538 | 2009-10-13 06:47:47 | [diff] [blame] | 390 | |
| 391 | // Depending on execution timings, posting a delayed task can result |
| 392 | // in the task getting executed the at the earliest possible |
| 393 | // opportunity or only after returning once from the message loop and |
| 394 | // then a second call into the message loop. In order to make behavior |
| 395 | // more deterministic, we change the default delay to 2ms. This should |
| 396 | // always require us to wait for the second call into the message loop. |
| 397 | // |
| 398 | // N.B. The correct fix for this and similar timing problems is to |
| 399 | // abstract time for the purpose of unittests. Unfortunately, we have |
| 400 | // a lot of third-party components that directly call the various |
| 401 | // time functions, so this change would be rather invasive. |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 402 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 403 | FROM_HERE, |
kylechar | f4fe517 | 2019-02-15 18:53:49 | [diff] [blame] | 404 | base::BindOnce(base::IgnoreResult(&TestConnectJob::DoConnect), |
| 405 | weak_factory_.GetWeakPtr(), true /* successful */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 406 | true /* async */, false /* cert_error */), |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 407 | base::TimeDelta::FromMilliseconds(kPendingConnectDelay)); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 408 | return ERR_IO_PENDING; |
| 409 | case kMockPendingFailingJob: |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 410 | set_load_state(LOAD_STATE_CONNECTING); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 411 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 412 | FROM_HERE, |
kylechar | f4fe517 | 2019-02-15 18:53:49 | [diff] [blame] | 413 | base::BindOnce(base::IgnoreResult(&TestConnectJob::DoConnect), |
| 414 | weak_factory_.GetWeakPtr(), false /* error */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 415 | true /* async */, false /* cert_error */), |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 416 | base::TimeDelta::FromMilliseconds(2)); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 417 | return ERR_IO_PENDING; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 418 | case kMockWaitingJob: |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 419 | set_load_state(LOAD_STATE_CONNECTING); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 420 | client_socket_factory_->WaitForSignal(this); |
| 421 | waiting_success_ = true; |
| 422 | return ERR_IO_PENDING; |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 423 | case kMockCertErrorJob: |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 424 | return DoConnect(false /* error */, false /* sync */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 425 | true /* cert_error */); |
| 426 | case kMockPendingCertErrorJob: |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 427 | set_load_state(LOAD_STATE_CONNECTING); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 428 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 429 | FROM_HERE, |
kylechar | f4fe517 | 2019-02-15 18:53:49 | [diff] [blame] | 430 | base::BindOnce(base::IgnoreResult(&TestConnectJob::DoConnect), |
| 431 | weak_factory_.GetWeakPtr(), false /* error */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 432 | true /* async */, true /* cert_error */), |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 433 | base::TimeDelta::FromMilliseconds(2)); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 434 | return ERR_IO_PENDING; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 435 | case kMockAdditionalErrorStateJob: |
| 436 | store_additional_error_state_ = true; |
| 437 | return DoConnect(false /* error */, false /* sync */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 438 | false /* cert_error */); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 439 | case kMockPendingAdditionalErrorStateJob: |
| 440 | set_load_state(LOAD_STATE_CONNECTING); |
| 441 | store_additional_error_state_ = true; |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 442 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 443 | FROM_HERE, |
kylechar | f4fe517 | 2019-02-15 18:53:49 | [diff] [blame] | 444 | base::BindOnce(base::IgnoreResult(&TestConnectJob::DoConnect), |
| 445 | weak_factory_.GetWeakPtr(), false /* error */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 446 | true /* async */, false /* cert_error */), |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 447 | base::TimeDelta::FromMilliseconds(2)); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 448 | return ERR_IO_PENDING; |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 449 | case kMockUnreadDataJob: { |
| 450 | int ret = DoConnect(true /* successful */, false /* sync */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 451 | false /* cert_error */); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 452 | static_cast<MockClientSocket*>(socket())->set_has_unread_data(true); |
| 453 | return ret; |
| 454 | } |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 455 | case kMockAuthChallengeOnceJob: |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 456 | set_load_state(LOAD_STATE_CONNECTING); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 457 | DoAdvanceAuthChallenge(1, true /* succeed_after_last_challenge */); |
| 458 | return ERR_IO_PENDING; |
| 459 | case kMockAuthChallengeTwiceJob: |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 460 | set_load_state(LOAD_STATE_CONNECTING); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 461 | DoAdvanceAuthChallenge(2, true /* succeed_after_last_challenge */); |
| 462 | return ERR_IO_PENDING; |
| 463 | case kMockAuthChallengeOnceFailingJob: |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 464 | set_load_state(LOAD_STATE_CONNECTING); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 465 | DoAdvanceAuthChallenge(1, false /* succeed_after_last_challenge */); |
| 466 | return ERR_IO_PENDING; |
| 467 | case kMockAuthChallengeTwiceFailingJob: |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 468 | set_load_state(LOAD_STATE_CONNECTING); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 469 | DoAdvanceAuthChallenge(2, false /* succeed_after_last_challenge */); |
| 470 | return ERR_IO_PENDING; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 471 | default: |
| 472 | NOTREACHED(); |
Cammie Smith Barnes | aa2a8b5 | 2020-12-17 19:33:19 | [diff] [blame] | 473 | SetSocket(std::unique_ptr<StreamSocket>(), base::nullopt); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 474 | return ERR_FAILED; |
| 475 | } |
| 476 | } |
| 477 | |
Lily Chen | 02ef29a | 2018-11-30 16:31:43 | [diff] [blame] | 478 | void ChangePriorityInternal(RequestPriority priority) override {} |
| 479 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 480 | int DoConnect(bool succeed, bool was_async, bool cert_error) { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 481 | int result = OK; |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 482 | has_established_connection_ = true; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 483 | if (succeed) { |
Cammie Smith Barnes | aa2a8b5 | 2020-12-17 19:33:19 | [diff] [blame] | 484 | SetSocket(std::make_unique<MockClientSocket>(net_log().net_log()), |
| 485 | base::nullopt); |
Bence Béky | bdbb0e7 | 2018-08-07 21:42:59 | [diff] [blame] | 486 | socket()->Connect(CompletionOnceCallback()); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 487 | } else if (cert_error) { |
Cammie Smith Barnes | aa2a8b5 | 2020-12-17 19:33:19 | [diff] [blame] | 488 | SetSocket(std::make_unique<MockClientSocket>(net_log().net_log()), |
| 489 | base::nullopt); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 490 | result = ERR_CERT_COMMON_NAME_INVALID; |
[email protected] | 6e713f0 | 2009-08-06 02:56:40 | [diff] [blame] | 491 | } else { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 492 | result = ERR_CONNECTION_FAILED; |
Cammie Smith Barnes | aa2a8b5 | 2020-12-17 19:33:19 | [diff] [blame] | 493 | SetSocket(std::unique_ptr<StreamSocket>(), base::nullopt); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 494 | } |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 495 | |
| 496 | if (was_async) |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 497 | NotifyDelegateOfCompletion(result); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 498 | return result; |
| 499 | } |
| 500 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 501 | void DoAdvanceAuthChallenge(int remaining_challenges, |
| 502 | bool succeed_after_last_challenge) { |
| 503 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 504 | FROM_HERE, |
| 505 | base::BindOnce(&TestConnectJob::InvokeNextProxyAuthCallback, |
| 506 | weak_factory_.GetWeakPtr(), remaining_challenges, |
| 507 | succeed_after_last_challenge)); |
| 508 | } |
| 509 | |
| 510 | void InvokeNextProxyAuthCallback(int remaining_challenges, |
| 511 | bool succeed_after_last_challenge) { |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 512 | set_load_state(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 513 | if (remaining_challenges == 0) { |
| 514 | DoConnect(succeed_after_last_challenge, true /* was_async */, |
| 515 | false /* cert_error */); |
| 516 | return; |
| 517 | } |
| 518 | |
| 519 | // Integration tests make sure HttpResponseInfo and HttpAuthController work. |
| 520 | // The auth tests here are just focused on ConnectJob bookkeeping. |
| 521 | HttpResponseInfo info; |
| 522 | NotifyDelegateOfProxyAuth( |
| 523 | info, nullptr /* http_auth_controller */, |
| 524 | base::BindOnce(&TestConnectJob::DoAdvanceAuthChallenge, |
| 525 | weak_factory_.GetWeakPtr(), remaining_challenges - 1, |
| 526 | succeed_after_last_challenge)); |
| 527 | } |
| 528 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 529 | bool waiting_success_; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 530 | const JobType job_type_; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 531 | MockClientSocketFactory* const client_socket_factory_; |
[email protected] | 4645135 | 2009-09-01 14:54:21 | [diff] [blame] | 532 | LoadState load_state_; |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 533 | bool has_established_connection_; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 534 | bool store_additional_error_state_; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 535 | |
Jeremy Roman | d54000b2 | 2019-07-08 18:40:16 | [diff] [blame] | 536 | base::WeakPtrFactory<TestConnectJob> weak_factory_{this}; |
[email protected] | d5492c5 | 2013-11-10 20:44:39 | [diff] [blame] | 537 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 538 | DISALLOW_COPY_AND_ASSIGN(TestConnectJob); |
| 539 | }; |
| 540 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 541 | class TestConnectJobFactory |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 542 | : public TransportClientSocketPool::ConnectJobFactory { |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 543 | public: |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 544 | TestConnectJobFactory(MockClientSocketFactory* client_socket_factory, |
| 545 | NetLog* net_log) |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 546 | : common_connect_job_params_( |
| 547 | nullptr /* client_socket_factory */, |
| 548 | nullptr /* host_resolver */, |
Matt Menke | b88837e | 2019-03-20 11:50:40 | [diff] [blame] | 549 | nullptr /* http_auth_cache */, |
| 550 | nullptr /* http_auth_handler_factory */, |
| 551 | nullptr /* spdy_session_pool */, |
Matt Menke | b5fb42b | 2019-03-22 17:26:13 | [diff] [blame] | 552 | nullptr /* quic_supported_versions */, |
Matt Menke | b88837e | 2019-03-20 11:50:40 | [diff] [blame] | 553 | nullptr /* quic_stream_factory */, |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 554 | nullptr /* proxy_delegate */, |
Matt Menke | d732ea4 | 2019-03-08 12:05:00 | [diff] [blame] | 555 | nullptr /* http_user_agent_settings */, |
David Benjamin | 24725be | 2019-07-24 20:57:18 | [diff] [blame] | 556 | nullptr /* ssl_client_context */, |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 557 | nullptr /* socket_performance_watcher_factory */, |
| 558 | nullptr /* network_quality_estimator */, |
| 559 | net_log, |
| 560 | nullptr /* websocket_endpoint_lock_manager */), |
| 561 | job_type_(TestConnectJob::kMockJob), |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 562 | job_types_(nullptr), |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 563 | client_socket_factory_(client_socket_factory) {} |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 564 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 565 | ~TestConnectJobFactory() override = default; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 566 | |
| 567 | void set_job_type(TestConnectJob::JobType job_type) { job_type_ = job_type; } |
| 568 | |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 569 | void set_job_types(std::list<TestConnectJob::JobType>* job_types) { |
| 570 | job_types_ = job_types; |
| 571 | CHECK(!job_types_->empty()); |
| 572 | } |
| 573 | |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 574 | void set_timeout_duration(base::TimeDelta timeout_duration) { |
| 575 | timeout_duration_ = timeout_duration; |
| 576 | } |
| 577 | |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 578 | // ConnectJobFactory implementation. |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 579 | |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 580 | std::unique_ptr<ConnectJob> NewConnectJob( |
Matt Menke | aafff54 | 2019-04-22 22:09:36 | [diff] [blame] | 581 | ClientSocketPool::GroupId group_id, |
| 582 | scoped_refptr<ClientSocketPool::SocketParams> socket_params, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 583 | const base::Optional<NetworkTrafficAnnotationTag>& proxy_annotation_tag, |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 584 | RequestPriority request_priority, |
| 585 | SocketTag socket_tag, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 586 | ConnectJob::Delegate* delegate) const override { |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 587 | EXPECT_TRUE(!job_types_ || !job_types_->empty()); |
| 588 | TestConnectJob::JobType job_type = job_type_; |
| 589 | if (job_types_ && !job_types_->empty()) { |
| 590 | job_type = job_types_->front(); |
| 591 | job_types_->pop_front(); |
| 592 | } |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 593 | return std::make_unique<TestConnectJob>( |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 594 | job_type, request_priority, socket_tag, timeout_duration_, |
| 595 | &common_connect_job_params_, delegate, client_socket_factory_); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | private: |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 599 | const CommonConnectJobParams common_connect_job_params_; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 600 | TestConnectJob::JobType job_type_; |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 601 | std::list<TestConnectJob::JobType>* job_types_; |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 602 | base::TimeDelta timeout_duration_; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 603 | MockClientSocketFactory* const client_socket_factory_; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 604 | |
| 605 | DISALLOW_COPY_AND_ASSIGN(TestConnectJobFactory); |
| 606 | }; |
| 607 | |
[email protected] | a937a06d | 2009-08-19 21:19:24 | [diff] [blame] | 608 | } // namespace |
| 609 | |
[email protected] | a937a06d | 2009-08-19 21:19:24 | [diff] [blame] | 610 | namespace { |
| 611 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 612 | void MockClientSocketFactory::SignalJobs() { |
jdoerrie | 22a91d8b9 | 2018-10-05 08:43:26 | [diff] [blame] | 613 | for (auto it = waiting_jobs_.begin(); it != waiting_jobs_.end(); ++it) { |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 614 | (*it)->Signal(); |
| 615 | } |
| 616 | waiting_jobs_.clear(); |
| 617 | } |
| 618 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 619 | void MockClientSocketFactory::SignalJob(size_t job) { |
| 620 | ASSERT_LT(job, waiting_jobs_.size()); |
| 621 | waiting_jobs_[job]->Signal(); |
| 622 | waiting_jobs_.erase(waiting_jobs_.begin() + job); |
| 623 | } |
| 624 | |
| 625 | void MockClientSocketFactory::SetJobLoadState(size_t job, |
| 626 | LoadState load_state) { |
| 627 | ASSERT_LT(job, waiting_jobs_.size()); |
| 628 | waiting_jobs_[job]->set_load_state(load_state); |
| 629 | } |
| 630 | |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 631 | void MockClientSocketFactory::SetJobHasEstablishedConnection(size_t job) { |
| 632 | ASSERT_LT(job, waiting_jobs_.size()); |
| 633 | waiting_jobs_[job]->set_has_established_connection(); |
| 634 | } |
| 635 | |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 636 | class ClientSocketPoolBaseTest : public TestWithTaskEnvironment { |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 637 | protected: |
Alex Clarke | 0def209 | 2018-12-10 12:01:45 | [diff] [blame] | 638 | ClientSocketPoolBaseTest() |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 639 | : TestWithTaskEnvironment( |
| 640 | base::test::TaskEnvironment::TimeSource::MOCK_TIME), |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 641 | params_(ClientSocketPool::SocketParams::CreateForHttpForTesting()) { |
[email protected] | 636b825 | 2011-04-08 19:56:54 | [diff] [blame] | 642 | connect_backup_jobs_enabled_ = |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 643 | TransportClientSocketPool::connect_backup_jobs_enabled(); |
| 644 | TransportClientSocketPool::set_connect_backup_jobs_enabled(true); |
[email protected] | 636b825 | 2011-04-08 19:56:54 | [diff] [blame] | 645 | } |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 646 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 647 | ~ClientSocketPoolBaseTest() override { |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 648 | TransportClientSocketPool::set_connect_backup_jobs_enabled( |
[email protected] | 636b825 | 2011-04-08 19:56:54 | [diff] [blame] | 649 | connect_backup_jobs_enabled_); |
| 650 | } |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 651 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 652 | void CreatePool(int max_sockets, |
| 653 | int max_sockets_per_group, |
| 654 | bool enable_backup_connect_jobs = false) { |
Tarun Bansal | a763509 | 2019-02-20 10:00:59 | [diff] [blame] | 655 | CreatePoolWithIdleTimeouts(max_sockets, max_sockets_per_group, |
| 656 | kUnusedIdleSocketTimeout, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 657 | ClientSocketPool::used_idle_socket_timeout(), |
| 658 | enable_backup_connect_jobs); |
[email protected] | 9bf28db | 2009-08-29 01:35:16 | [diff] [blame] | 659 | } |
| 660 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 661 | void CreatePoolWithIdleTimeouts( |
| 662 | int max_sockets, |
| 663 | int max_sockets_per_group, |
| 664 | base::TimeDelta unused_idle_socket_timeout, |
| 665 | base::TimeDelta used_idle_socket_timeout, |
| 666 | bool enable_backup_connect_jobs = false, |
| 667 | ProxyServer proxy_server = ProxyServer::Direct()) { |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 668 | DCHECK(!pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 669 | std::unique_ptr<TestConnectJobFactory> connect_job_factory = |
| 670 | std::make_unique<TestConnectJobFactory>(&client_socket_factory_, |
| 671 | &net_log_); |
| 672 | connect_job_factory_ = connect_job_factory.get(); |
| 673 | pool_ = TransportClientSocketPool::CreateForTesting( |
| 674 | max_sockets, max_sockets_per_group, unused_idle_socket_timeout, |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 675 | used_idle_socket_timeout, proxy_server, std::move(connect_job_factory), |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 676 | nullptr /* ssl_config_service */, enable_backup_connect_jobs); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 677 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 678 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 679 | int StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 680 | const ClientSocketPool::GroupId& group_id, |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 681 | RequestPriority priority, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 682 | ClientSocketPool::RespectLimits respect_limits) { |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 683 | return test_base_.StartRequestUsingPool(pool_.get(), group_id, priority, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 684 | respect_limits, params_); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 685 | } |
| 686 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 687 | int StartRequest(const ClientSocketPool::GroupId& group_id, |
| 688 | RequestPriority priority) { |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 689 | return StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 690 | group_id, priority, ClientSocketPool::RespectLimits::ENABLED); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 691 | } |
| 692 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 693 | int GetOrderOfRequest(size_t index) const { |
| 694 | return test_base_.GetOrderOfRequest(index); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 695 | } |
| 696 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 697 | bool ReleaseOneConnection(ClientSocketPoolTest::KeepAlive keep_alive) { |
| 698 | return test_base_.ReleaseOneConnection(keep_alive); |
| 699 | } |
| 700 | |
| 701 | void ReleaseAllConnections(ClientSocketPoolTest::KeepAlive keep_alive) { |
| 702 | test_base_.ReleaseAllConnections(keep_alive); |
| 703 | } |
| 704 | |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 705 | // Expects a single NetLogEventType::SOCKET_POOL_CLOSING_SOCKET in |net_log_|. |
| 706 | // It should be logged for the provided source and have the indicated reason. |
| 707 | void ExpectSocketClosedWithReason(NetLogSource expected_source, |
| 708 | const char* expected_reason) { |
| 709 | auto entries = net_log_.GetEntriesForSourceWithType( |
| 710 | expected_source, NetLogEventType::SOCKET_POOL_CLOSING_SOCKET, |
| 711 | NetLogEventPhase::NONE); |
| 712 | ASSERT_EQ(1u, entries.size()); |
| 713 | ASSERT_TRUE(entries[0].HasParams()); |
| 714 | ASSERT_TRUE(entries[0].params.is_dict()); |
| 715 | const std::string* reason = entries[0].params.FindStringKey("reason"); |
| 716 | ASSERT_TRUE(reason); |
| 717 | EXPECT_EQ(expected_reason, *reason); |
| 718 | } |
| 719 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 720 | TestSocketRequest* request(int i) { return test_base_.request(i); } |
| 721 | size_t requests_size() const { return test_base_.requests_size(); } |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 722 | std::vector<std::unique_ptr<TestSocketRequest>>* requests() { |
olli.raula | 9d66b7d | 2015-11-23 08:30:42 | [diff] [blame] | 723 | return test_base_.requests(); |
| 724 | } |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 725 | // Only counts the requests that get sockets asynchronously; |
| 726 | // synchronous completions are not registered by this count. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 727 | size_t completion_count() const { return test_base_.completion_count(); } |
| 728 | |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 729 | RecordingTestNetLog net_log_; |
[email protected] | 636b825 | 2011-04-08 19:56:54 | [diff] [blame] | 730 | bool connect_backup_jobs_enabled_; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 731 | MockClientSocketFactory client_socket_factory_; |
[email protected] | 17a0c6c | 2009-08-04 00:07:04 | [diff] [blame] | 732 | TestConnectJobFactory* connect_job_factory_; |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 733 | // These parameters are never actually used to create a TransportConnectJob. |
Matt Menke | 84d11e56 | 2019-03-27 00:11:19 | [diff] [blame] | 734 | scoped_refptr<ClientSocketPool::SocketParams> params_; |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 735 | std::unique_ptr<TransportClientSocketPool> pool_; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 736 | ClientSocketPoolTest test_base_; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 737 | }; |
| 738 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 739 | TEST_F(ClientSocketPoolBaseTest, BasicSynchronous) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 740 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 741 | |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 742 | TestCompletionCallback callback; |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 743 | ClientSocketHandle handle; |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 744 | RecordingBoundTestNetLog log; |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 745 | TestLoadTimingInfoNotConnected(handle); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 746 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 747 | EXPECT_EQ(OK, handle.Init( |
| 748 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 749 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 750 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 751 | pool_.get(), log.bound())); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 752 | EXPECT_TRUE(handle.is_initialized()); |
| 753 | EXPECT_TRUE(handle.socket()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 754 | TestLoadTimingInfoConnectedNotReused(handle); |
| 755 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 756 | handle.Reset(); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 757 | TestLoadTimingInfoNotConnected(handle); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 758 | |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 759 | auto entries = log.GetEntries(); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 760 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 761 | EXPECT_EQ(5u, entries.size()); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 762 | EXPECT_TRUE(LogContainsEvent( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 763 | entries, 0, NetLogEventType::TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 764 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 765 | EXPECT_TRUE(LogContainsBeginEvent(entries, 1, NetLogEventType::SOCKET_POOL)); |
| 766 | EXPECT_TRUE(LogContainsEvent( |
| 767 | entries, 2, NetLogEventType::SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
| 768 | NetLogEventPhase::NONE)); |
| 769 | EXPECT_TRUE(LogContainsEvent(entries, 3, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 770 | NetLogEventType::SOCKET_POOL_BOUND_TO_SOCKET, |
| 771 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 772 | EXPECT_TRUE(LogContainsEndEvent(entries, 4, NetLogEventType::SOCKET_POOL)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 773 | } |
| 774 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 775 | TEST_F(ClientSocketPoolBaseTest, InitConnectionFailure) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 776 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 777 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 778 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 779 | RecordingBoundTestNetLog log; |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 780 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 781 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 782 | TestCompletionCallback callback; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 783 | // Set the additional error state members to ensure that they get cleared. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 784 | handle.set_is_ssl_error(true); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 785 | handle.set_ssl_cert_request_info(base::MakeRefCounted<SSLCertRequestInfo>()); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 786 | EXPECT_EQ( |
| 787 | ERR_CONNECTION_FAILED, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 788 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 789 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 790 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 791 | pool_.get(), log.bound())); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 792 | EXPECT_FALSE(handle.socket()); |
| 793 | EXPECT_FALSE(handle.is_ssl_error()); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 794 | EXPECT_FALSE(handle.ssl_cert_request_info()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 795 | TestLoadTimingInfoNotConnected(handle); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 796 | |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 797 | auto entries = log.GetEntries(); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 798 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 799 | EXPECT_EQ(4u, entries.size()); |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 800 | EXPECT_TRUE(LogContainsEvent( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 801 | entries, 0, NetLogEventType::TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 802 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 803 | EXPECT_TRUE(LogContainsBeginEvent(entries, 1, NetLogEventType::SOCKET_POOL)); |
| 804 | EXPECT_TRUE(LogContainsEvent( |
| 805 | entries, 2, NetLogEventType::SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
| 806 | NetLogEventPhase::NONE)); |
| 807 | EXPECT_TRUE(LogContainsEndEvent(entries, 3, NetLogEventType::SOCKET_POOL)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 808 | } |
| 809 | |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 810 | // Test releasing an open socket into the socket pool, telling the socket pool |
| 811 | // to close the socket. |
| 812 | TEST_F(ClientSocketPoolBaseTest, ReleaseAndCloseConnection) { |
| 813 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 814 | |
| 815 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 816 | ASSERT_TRUE(request(0)->handle()->socket()); |
| 817 | net::NetLogSource source = request(0)->handle()->socket()->NetLog().source(); |
| 818 | ReleaseOneConnection(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 819 | |
| 820 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 821 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 822 | |
| 823 | ExpectSocketClosedWithReason( |
| 824 | source, TransportClientSocketPool::kClosedConnectionReturnedToPool); |
| 825 | } |
| 826 | |
| 827 | TEST_F(ClientSocketPoolBaseTest, SocketWithUnreadDataReturnedToPool) { |
| 828 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 829 | connect_job_factory_->set_job_type(TestConnectJob::kMockUnreadDataJob); |
| 830 | |
| 831 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 832 | ASSERT_TRUE(request(0)->handle()->socket()); |
| 833 | net::NetLogSource source = request(0)->handle()->socket()->NetLog().source(); |
| 834 | EXPECT_TRUE(request(0)->handle()->socket()->IsConnected()); |
| 835 | EXPECT_FALSE(request(0)->handle()->socket()->IsConnectedAndIdle()); |
| 836 | ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE); |
| 837 | |
| 838 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 839 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 840 | |
| 841 | ExpectSocketClosedWithReason( |
| 842 | source, TransportClientSocketPool::kDataReceivedUnexpectedly); |
| 843 | } |
| 844 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 845 | // Make sure different groups do not share sockets. |
| 846 | TEST_F(ClientSocketPoolBaseTest, GroupSeparation) { |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 847 | base::test::ScopedFeatureList feature_list; |
| 848 | feature_list.InitAndEnableFeature( |
| 849 | features::kPartitionConnectionsByNetworkIsolationKey); |
| 850 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 851 | CreatePool(1000 /* max_sockets */, 2 /* max_sockets_per_group */); |
| 852 | |
| 853 | const HostPortPair kHostPortPairs[] = { |
| 854 | {"a", 80}, |
| 855 | {"a", 443}, |
| 856 | {"b", 80}, |
| 857 | }; |
| 858 | |
| 859 | const ClientSocketPool::SocketType kSocketTypes[] = { |
| 860 | ClientSocketPool::SocketType::kHttp, |
| 861 | ClientSocketPool::SocketType::kSsl, |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 862 | }; |
| 863 | |
Matt Menke | bdf77780 | 2019-04-22 19:38:59 | [diff] [blame] | 864 | const PrivacyMode kPrivacyModes[] = {PrivacyMode::PRIVACY_MODE_DISABLED, |
| 865 | PrivacyMode::PRIVACY_MODE_ENABLED}; |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 866 | |
Matt Menke | 4807a9a | 2020-11-21 00:14:41 | [diff] [blame] | 867 | const SchemefulSite kSiteA(GURL("http://a.test/")); |
| 868 | const SchemefulSite kSiteB(GURL("http://b.test/")); |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 869 | const NetworkIsolationKey kNetworkIsolationKeys[] = { |
Matt Menke | 4807a9a | 2020-11-21 00:14:41 | [diff] [blame] | 870 | NetworkIsolationKey(kSiteA, kSiteA), |
| 871 | NetworkIsolationKey(kSiteB, kSiteB), |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 872 | }; |
| 873 | |
Ben Schwartz | 3ff4dc1e6 | 2021-04-27 21:15:23 | [diff] [blame^] | 874 | const SecureDnsPolicy kSecureDnsPolicys[] = {SecureDnsPolicy::kAllow, |
| 875 | SecureDnsPolicy::kDisable}; |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 876 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 877 | int total_idle_sockets = 0; |
| 878 | |
| 879 | // Walk through each GroupId, making sure that requesting a socket for one |
| 880 | // group does not return a previously connected socket for another group. |
| 881 | for (const auto& host_port_pair : kHostPortPairs) { |
| 882 | SCOPED_TRACE(host_port_pair.ToString()); |
| 883 | for (const auto& socket_type : kSocketTypes) { |
| 884 | SCOPED_TRACE(static_cast<int>(socket_type)); |
| 885 | for (const auto& privacy_mode : kPrivacyModes) { |
| 886 | SCOPED_TRACE(privacy_mode); |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 887 | for (const auto& network_isolation_key : kNetworkIsolationKeys) { |
| 888 | SCOPED_TRACE(network_isolation_key.ToString()); |
Ben Schwartz | 3ff4dc1e6 | 2021-04-27 21:15:23 | [diff] [blame^] | 889 | for (const auto& secure_dns_policy : kSecureDnsPolicys) { |
| 890 | SCOPED_TRACE(static_cast<int>(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 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 893 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 894 | ClientSocketPool::GroupId group_id( |
| 895 | host_port_pair, socket_type, privacy_mode, |
Ben Schwartz | 3ff4dc1e6 | 2021-04-27 21:15:23 | [diff] [blame^] | 896 | network_isolation_key, secure_dns_policy); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 897 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 898 | EXPECT_FALSE(pool_->HasGroupForTesting(group_id)); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 899 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 900 | TestCompletionCallback callback; |
| 901 | ClientSocketHandle handle; |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 902 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 903 | // Since the group is empty, requesting a socket should not complete |
| 904 | // synchronously. |
| 905 | EXPECT_THAT(handle.Init(group_id, params_, base::nullopt, |
| 906 | DEFAULT_PRIORITY, SocketTag(), |
| 907 | ClientSocketPool::RespectLimits::ENABLED, |
| 908 | callback.callback(), |
| 909 | ClientSocketPool::ProxyAuthCallback(), |
| 910 | pool_.get(), NetLogWithSource()), |
| 911 | IsError(ERR_IO_PENDING)); |
| 912 | EXPECT_TRUE(pool_->HasGroupForTesting(group_id)); |
| 913 | EXPECT_EQ(total_idle_sockets, pool_->IdleSocketCount()); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 914 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 915 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 916 | EXPECT_TRUE(handle.socket()); |
| 917 | EXPECT_TRUE(pool_->HasGroupForTesting(group_id)); |
| 918 | EXPECT_EQ(total_idle_sockets, pool_->IdleSocketCount()); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 919 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 920 | // Return socket to pool. |
| 921 | handle.Reset(); |
| 922 | EXPECT_EQ(total_idle_sockets + 1, pool_->IdleSocketCount()); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 923 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 924 | // Requesting a socket again should return the same socket as |
| 925 | // before, so should complete synchronously. |
| 926 | EXPECT_THAT(handle.Init(group_id, params_, base::nullopt, |
| 927 | DEFAULT_PRIORITY, SocketTag(), |
| 928 | ClientSocketPool::RespectLimits::ENABLED, |
| 929 | callback.callback(), |
| 930 | ClientSocketPool::ProxyAuthCallback(), |
| 931 | pool_.get(), NetLogWithSource()), |
| 932 | IsOk()); |
| 933 | EXPECT_TRUE(handle.socket()); |
| 934 | EXPECT_EQ(total_idle_sockets, pool_->IdleSocketCount()); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 935 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 936 | // Return socket to pool again. |
| 937 | handle.Reset(); |
| 938 | EXPECT_EQ(total_idle_sockets + 1, pool_->IdleSocketCount()); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 939 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 940 | ++total_idle_sockets; |
| 941 | } |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 942 | } |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 943 | } |
| 944 | } |
| 945 | } |
| 946 | } |
| 947 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 948 | TEST_F(ClientSocketPoolBaseTest, TotalLimit) { |
| 949 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 950 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 951 | // TODO(eroman): Check that the NetLog contains this event. |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 952 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 953 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 954 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
| 955 | EXPECT_THAT(StartRequest(TestGroupId("c"), DEFAULT_PRIORITY), IsOk()); |
| 956 | EXPECT_THAT(StartRequest(TestGroupId("d"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 957 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 958 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 959 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 960 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 961 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 962 | EXPECT_THAT(StartRequest(TestGroupId("e"), DEFAULT_PRIORITY), |
| 963 | IsError(ERR_IO_PENDING)); |
| 964 | EXPECT_THAT(StartRequest(TestGroupId("f"), DEFAULT_PRIORITY), |
| 965 | IsError(ERR_IO_PENDING)); |
| 966 | EXPECT_THAT(StartRequest(TestGroupId("g"), DEFAULT_PRIORITY), |
| 967 | IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 968 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 969 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 970 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 971 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 972 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 973 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 974 | |
| 975 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 976 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 977 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 978 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 979 | EXPECT_EQ(5, GetOrderOfRequest(5)); |
| 980 | EXPECT_EQ(6, GetOrderOfRequest(6)); |
| 981 | EXPECT_EQ(7, GetOrderOfRequest(7)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 982 | |
| 983 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 984 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(8)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 985 | } |
| 986 | |
| 987 | TEST_F(ClientSocketPoolBaseTest, TotalLimitReachedNewGroup) { |
| 988 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 989 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 990 | // TODO(eroman): Check that the NetLog contains this event. |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 991 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 992 | // Reach all limits: max total sockets, and max sockets per group. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 993 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 994 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 995 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
| 996 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 997 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 998 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 999 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1000 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1001 | |
| 1002 | // Now create a new group and verify that we don't starve it. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1003 | EXPECT_THAT(StartRequest(TestGroupId("c"), DEFAULT_PRIORITY), |
| 1004 | IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1005 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1006 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1007 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1008 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1009 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1010 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1011 | |
| 1012 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1013 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1014 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 1015 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 1016 | EXPECT_EQ(5, GetOrderOfRequest(5)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1017 | |
| 1018 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1019 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(6)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1020 | } |
| 1021 | |
| 1022 | TEST_F(ClientSocketPoolBaseTest, TotalLimitRespectsPriority) { |
| 1023 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1024 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1025 | EXPECT_THAT(StartRequest(TestGroupId("b"), LOWEST), IsOk()); |
| 1026 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsOk()); |
| 1027 | EXPECT_THAT(StartRequest(TestGroupId("b"), HIGHEST), IsOk()); |
| 1028 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1029 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1030 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1031 | client_socket_factory_.allocation_count()); |
| 1032 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1033 | EXPECT_THAT(StartRequest(TestGroupId("c"), LOWEST), IsError(ERR_IO_PENDING)); |
| 1034 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1035 | EXPECT_THAT(StartRequest(TestGroupId("b"), HIGHEST), IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1036 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1037 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1038 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1039 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1040 | |
| 1041 | // First 4 requests don't have to wait, and finish in order. |
| 1042 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1043 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1044 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 1045 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 1046 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1047 | // Request ("b", HIGHEST) has the highest priority, then (TestGroupId("a"), |
| 1048 | // MEDIUM), and then ("c", LOWEST). |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1049 | EXPECT_EQ(7, GetOrderOfRequest(5)); |
| 1050 | EXPECT_EQ(6, GetOrderOfRequest(6)); |
| 1051 | EXPECT_EQ(5, GetOrderOfRequest(7)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1052 | |
| 1053 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1054 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(9)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1055 | } |
| 1056 | |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1057 | // Test reprioritizing a request before completion doesn't interfere with |
| 1058 | // its completion. |
| 1059 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeOne) { |
| 1060 | CreatePool(kDefaultMaxSockets, 1); |
| 1061 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1062 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1063 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1064 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1065 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1066 | |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 1067 | request(1)->handle()->SetPriority(HIGHEST); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1068 | |
| 1069 | ReleaseOneConnection(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1070 | |
| 1071 | EXPECT_TRUE(request(1)->handle()->socket()); |
| 1072 | } |
| 1073 | |
| 1074 | // Reprioritize a request up past another one and make sure that changes the |
| 1075 | // completion order. |
| 1076 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeUpReorder) { |
| 1077 | CreatePool(kDefaultMaxSockets, 1); |
| 1078 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1079 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1080 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1081 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1082 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1083 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1084 | EXPECT_FALSE(request(2)->handle()->socket()); |
| 1085 | |
| 1086 | request(2)->handle()->SetPriority(HIGHEST); |
| 1087 | |
| 1088 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1089 | |
| 1090 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1091 | EXPECT_EQ(3, GetOrderOfRequest(2)); |
| 1092 | EXPECT_EQ(2, GetOrderOfRequest(3)); |
| 1093 | } |
| 1094 | |
| 1095 | // Reprioritize a request without changing relative priorities and check |
| 1096 | // that the order doesn't change. |
| 1097 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeUpNoReorder) { |
| 1098 | CreatePool(kDefaultMaxSockets, 1); |
| 1099 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1100 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1101 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1102 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1103 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1104 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1105 | EXPECT_FALSE(request(2)->handle()->socket()); |
| 1106 | |
| 1107 | request(2)->handle()->SetPriority(MEDIUM); |
| 1108 | |
| 1109 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1110 | |
| 1111 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1112 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1113 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 1114 | } |
| 1115 | |
| 1116 | // Reprioritize a request past down another one and make sure that changes the |
| 1117 | // completion order. |
| 1118 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeDownReorder) { |
| 1119 | CreatePool(kDefaultMaxSockets, 1); |
| 1120 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1121 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1122 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1123 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1124 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1125 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1126 | EXPECT_FALSE(request(2)->handle()->socket()); |
| 1127 | |
| 1128 | request(1)->handle()->SetPriority(LOW); |
| 1129 | |
| 1130 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1131 | |
| 1132 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1133 | EXPECT_EQ(3, GetOrderOfRequest(2)); |
| 1134 | EXPECT_EQ(2, GetOrderOfRequest(3)); |
| 1135 | } |
| 1136 | |
| 1137 | // Reprioritize a request to the same level as another and confirm it is |
| 1138 | // put after the old request. |
| 1139 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeResetFIFO) { |
| 1140 | CreatePool(kDefaultMaxSockets, 1); |
| 1141 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1142 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1143 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1144 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1145 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1146 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1147 | EXPECT_FALSE(request(2)->handle()->socket()); |
| 1148 | |
| 1149 | request(1)->handle()->SetPriority(MEDIUM); |
| 1150 | |
| 1151 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1152 | |
| 1153 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1154 | EXPECT_EQ(3, GetOrderOfRequest(2)); |
| 1155 | EXPECT_EQ(2, GetOrderOfRequest(3)); |
| 1156 | } |
| 1157 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1158 | TEST_F(ClientSocketPoolBaseTest, TotalLimitRespectsGroupLimit) { |
| 1159 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1160 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1161 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsOk()); |
| 1162 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsOk()); |
| 1163 | EXPECT_THAT(StartRequest(TestGroupId("b"), HIGHEST), IsOk()); |
| 1164 | EXPECT_THAT(StartRequest(TestGroupId("b"), MEDIUM), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1165 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1166 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1167 | client_socket_factory_.allocation_count()); |
| 1168 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1169 | EXPECT_THAT(StartRequest(TestGroupId("c"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1170 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 1171 | EXPECT_THAT(StartRequest(TestGroupId("b"), HIGHEST), IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1172 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1173 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1174 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1175 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1176 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1177 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1178 | |
| 1179 | // First 4 requests don't have to wait, and finish in order. |
| 1180 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1181 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1182 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 1183 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 1184 | |
| 1185 | // Request ("b", 7) has the highest priority, but we can't make new socket for |
| 1186 | // group "b", because it has reached the per-group limit. Then we make |
| 1187 | // socket for ("c", 6), because it has higher priority than ("a", 4), |
| 1188 | // and we still can't make a socket for group "b". |
| 1189 | EXPECT_EQ(5, GetOrderOfRequest(5)); |
| 1190 | EXPECT_EQ(6, GetOrderOfRequest(6)); |
| 1191 | EXPECT_EQ(7, GetOrderOfRequest(7)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1192 | |
| 1193 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1194 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(8)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1195 | } |
| 1196 | |
| 1197 | // Make sure that we count connecting sockets against the total limit. |
| 1198 | TEST_F(ClientSocketPoolBaseTest, TotalLimitCountsConnectingSockets) { |
| 1199 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1200 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1201 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1202 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
| 1203 | EXPECT_THAT(StartRequest(TestGroupId("c"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1204 | |
| 1205 | // Create one asynchronous request. |
| 1206 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1207 | EXPECT_THAT(StartRequest(TestGroupId("d"), DEFAULT_PRIORITY), |
| 1208 | IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1209 | |
[email protected] | 6b17538 | 2009-10-13 06:47:47 | [diff] [blame] | 1210 | // We post all of our delayed tasks with a 2ms delay. I.e. they don't |
| 1211 | // actually become pending until 2ms after they have been created. In order |
| 1212 | // to flush all tasks, we need to wait so that we know there are no |
| 1213 | // soon-to-be-pending tasks waiting. |
Alex Clarke | 0def209 | 2018-12-10 12:01:45 | [diff] [blame] | 1214 | FastForwardBy(base::TimeDelta::FromMilliseconds(10)); |
[email protected] | 6b17538 | 2009-10-13 06:47:47 | [diff] [blame] | 1215 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1216 | // The next synchronous request should wait for its turn. |
| 1217 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1218 | EXPECT_THAT(StartRequest(TestGroupId("e"), DEFAULT_PRIORITY), |
| 1219 | IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1220 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1221 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1222 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1223 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1224 | client_socket_factory_.allocation_count()); |
| 1225 | |
| 1226 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1227 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1228 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 1229 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1230 | EXPECT_EQ(5, GetOrderOfRequest(5)); |
| 1231 | |
| 1232 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1233 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(6)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1234 | } |
| 1235 | |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1236 | TEST_F(ClientSocketPoolBaseTest, CorrectlyCountStalledGroups) { |
| 1237 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 1238 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 1239 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1240 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1241 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1242 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1243 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1244 | |
| 1245 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 1246 | |
| 1247 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1248 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1249 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), |
| 1250 | IsError(ERR_IO_PENDING)); |
| 1251 | EXPECT_THAT(StartRequest(TestGroupId("c"), DEFAULT_PRIORITY), |
| 1252 | IsError(ERR_IO_PENDING)); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1253 | |
| 1254 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1255 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1256 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1257 | EXPECT_EQ(kDefaultMaxSockets + 1, client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1258 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1259 | EXPECT_EQ(kDefaultMaxSockets + 2, client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1260 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
| 1261 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1262 | EXPECT_EQ(kDefaultMaxSockets + 2, client_socket_factory_.allocation_count()); |
| 1263 | } |
| 1264 | |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 1265 | TEST_F(ClientSocketPoolBaseTest, StallAndThenCancelAndTriggerAvailableSocket) { |
| 1266 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 1267 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1268 | |
| 1269 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1270 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1271 | EXPECT_EQ( |
| 1272 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1273 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1274 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1275 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1276 | pool_.get(), NetLogWithSource())); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 1277 | |
| 1278 | ClientSocketHandle handles[4]; |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 1279 | for (size_t i = 0; i < base::size(handles); ++i) { |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1280 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1281 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1282 | handles[i].Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1283 | TestGroupId("b"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1284 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1285 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1286 | pool_.get(), NetLogWithSource())); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 1287 | } |
| 1288 | |
| 1289 | // One will be stalled, cancel all the handles now. |
| 1290 | // This should hit the OnAvailableSocketSlot() code where we previously had |
| 1291 | // stalled groups, but no longer have any. |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 1292 | for (size_t i = 0; i < base::size(handles); ++i) |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 1293 | handles[i].Reset(); |
| 1294 | } |
| 1295 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1296 | TEST_F(ClientSocketPoolBaseTest, CancelStalledSocketAtSocketLimit) { |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1297 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1298 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 1299 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1300 | { |
| 1301 | ClientSocketHandle handles[kDefaultMaxSockets]; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1302 | TestCompletionCallback callbacks[kDefaultMaxSockets]; |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1303 | for (int i = 0; i < kDefaultMaxSockets; ++i) { |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1304 | EXPECT_EQ(OK, |
| 1305 | handles[i].Init(TestGroupId(base::NumberToString(i)), params_, |
| 1306 | base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
| 1307 | ClientSocketPool::RespectLimits::ENABLED, |
| 1308 | callbacks[i].callback(), |
| 1309 | ClientSocketPool::ProxyAuthCallback(), |
| 1310 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1311 | } |
| 1312 | |
| 1313 | // Force a stalled group. |
| 1314 | ClientSocketHandle stalled_handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1315 | TestCompletionCallback callback; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1316 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1317 | stalled_handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1318 | TestGroupId("foo"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1319 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1320 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1321 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1322 | |
| 1323 | // Cancel the stalled request. |
| 1324 | stalled_handle.Reset(); |
| 1325 | |
| 1326 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1327 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 1328 | |
| 1329 | // Dropping out of scope will close all handles and return them to idle. |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1330 | } |
| 1331 | |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1332 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1333 | EXPECT_EQ(kDefaultMaxSockets, pool_->IdleSocketCount()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1334 | } |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1335 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1336 | TEST_F(ClientSocketPoolBaseTest, CancelPendingSocketAtSocketLimit) { |
| 1337 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1338 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 1339 | |
| 1340 | { |
| 1341 | ClientSocketHandle handles[kDefaultMaxSockets]; |
| 1342 | for (int i = 0; i < kDefaultMaxSockets; ++i) { |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1343 | TestCompletionCallback callback; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1344 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1345 | handles[i].Init(TestGroupId(base::NumberToString(i)), params_, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1346 | base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1347 | ClientSocketPool::RespectLimits::ENABLED, |
| 1348 | callback.callback(), |
| 1349 | ClientSocketPool::ProxyAuthCallback(), |
| 1350 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1351 | } |
| 1352 | |
| 1353 | // Force a stalled group. |
| 1354 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1355 | ClientSocketHandle stalled_handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1356 | TestCompletionCallback callback; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1357 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1358 | stalled_handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1359 | TestGroupId("foo"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1360 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1361 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1362 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1363 | |
| 1364 | // Since it is stalled, it should have no connect jobs. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1365 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("foo"))); |
| 1366 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 1367 | TestGroupId("foo"))); |
| 1368 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroupForTesting( |
| 1369 | TestGroupId("foo"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1370 | |
| 1371 | // Cancel the stalled request. |
| 1372 | handles[0].Reset(); |
| 1373 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1374 | // Now we should have a connect job. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1375 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("foo"))); |
| 1376 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 1377 | TestGroupId("foo"))); |
| 1378 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroupForTesting( |
| 1379 | TestGroupId("foo"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1380 | |
| 1381 | // The stalled socket should connect. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1382 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1383 | |
| 1384 | EXPECT_EQ(kDefaultMaxSockets + 1, |
| 1385 | client_socket_factory_.allocation_count()); |
| 1386 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1387 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("foo"))); |
| 1388 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 1389 | TestGroupId("foo"))); |
| 1390 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroupForTesting( |
| 1391 | TestGroupId("foo"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1392 | |
| 1393 | // Dropping out of scope will close all handles and return them to idle. |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1394 | } |
| 1395 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1396 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 1397 | } |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1398 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1399 | TEST_F(ClientSocketPoolBaseTest, WaitForStalledSocketAtSocketLimit) { |
| 1400 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1401 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1402 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1403 | ClientSocketHandle stalled_handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1404 | TestCompletionCallback callback; |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1405 | { |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 1406 | EXPECT_FALSE(pool_->IsStalled()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1407 | ClientSocketHandle handles[kDefaultMaxSockets]; |
| 1408 | for (int i = 0; i < kDefaultMaxSockets; ++i) { |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1409 | TestCompletionCallback callback; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1410 | EXPECT_EQ( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1411 | OK, handles[i].Init( |
| 1412 | TestGroupId(base::StringPrintf("Take 2: %d", i)), params_, |
| 1413 | base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
| 1414 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1415 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 1416 | NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1417 | } |
| 1418 | |
| 1419 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1420 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 1421 | EXPECT_FALSE(pool_->IsStalled()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1422 | |
| 1423 | // Now we will hit the socket limit. |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1424 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1425 | stalled_handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1426 | TestGroupId("foo"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1427 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1428 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1429 | pool_.get(), NetLogWithSource())); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 1430 | EXPECT_TRUE(pool_->IsStalled()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1431 | |
| 1432 | // Dropping out of scope will close all handles and return them to idle. |
| 1433 | } |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1434 | |
| 1435 | // But if we wait for it, the released idle sockets will be closed in |
| 1436 | // preference of the waiting request. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1437 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1438 | |
| 1439 | EXPECT_EQ(kDefaultMaxSockets + 1, client_socket_factory_.allocation_count()); |
| 1440 | EXPECT_EQ(3, pool_->IdleSocketCount()); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1441 | } |
| 1442 | |
| 1443 | // Regression test for http://crbug.com/40952. |
| 1444 | TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketAtSocketLimitDeleteGroup) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1445 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 1446 | true /* enable_backup_connect_jobs */); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1447 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 1448 | |
| 1449 | for (int i = 0; i < kDefaultMaxSockets; ++i) { |
| 1450 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1451 | TestCompletionCallback callback; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1452 | EXPECT_EQ(OK, handle.Init(TestGroupId(base::NumberToString(i)), params_, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1453 | base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1454 | ClientSocketPool::RespectLimits::ENABLED, |
| 1455 | callback.callback(), |
| 1456 | ClientSocketPool::ProxyAuthCallback(), |
| 1457 | pool_.get(), NetLogWithSource())); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1458 | } |
| 1459 | |
| 1460 | // Flush all the DoReleaseSocket tasks. |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 1461 | base::RunLoop().RunUntilIdle(); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1462 | |
| 1463 | // Stall a group. Set a pending job so it'll trigger a backup job if we don't |
| 1464 | // reuse a socket. |
| 1465 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1466 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1467 | TestCompletionCallback callback; |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1468 | |
| 1469 | // "0" is special here, since it should be the first entry in the sorted map, |
| 1470 | // which is the one which we would close an idle socket for. We shouldn't |
| 1471 | // close an idle socket though, since we should reuse the idle socket. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1472 | EXPECT_EQ(OK, handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1473 | TestGroupId("0"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1474 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1475 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1476 | pool_.get(), NetLogWithSource())); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1477 | |
| 1478 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1479 | EXPECT_EQ(kDefaultMaxSockets - 1, pool_->IdleSocketCount()); |
| 1480 | } |
| 1481 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1482 | TEST_F(ClientSocketPoolBaseTest, PendingRequests) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1483 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1484 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1485 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1486 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1487 | EXPECT_THAT(StartRequest(TestGroupId("a"), IDLE), IsError(ERR_IO_PENDING)); |
| 1488 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
| 1489 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1490 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1491 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 1492 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1493 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1494 | ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1495 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
| 1496 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1497 | EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup, |
| 1498 | completion_count()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1499 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1500 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1501 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
[email protected] | c9c6f5c | 2010-07-31 01:30:03 | [diff] [blame] | 1502 | EXPECT_EQ(8, GetOrderOfRequest(3)); |
| 1503 | EXPECT_EQ(6, GetOrderOfRequest(4)); |
| 1504 | EXPECT_EQ(4, GetOrderOfRequest(5)); |
| 1505 | EXPECT_EQ(3, GetOrderOfRequest(6)); |
| 1506 | EXPECT_EQ(5, GetOrderOfRequest(7)); |
| 1507 | EXPECT_EQ(7, GetOrderOfRequest(8)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1508 | |
| 1509 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1510 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(9)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1511 | } |
| 1512 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1513 | TEST_F(ClientSocketPoolBaseTest, PendingRequests_NoKeepAlive) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1514 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1515 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1516 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1517 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1518 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
| 1519 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1520 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1521 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 1522 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1523 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1524 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1525 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1526 | for (size_t i = kDefaultMaxSocketsPerGroup; i < requests_size(); ++i) |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1527 | EXPECT_THAT(request(i)->WaitForResult(), IsOk()); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1528 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1529 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1530 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1531 | EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup, |
| 1532 | completion_count()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1533 | } |
| 1534 | |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1535 | TEST_F(ClientSocketPoolBaseTest, ResetAndCloseSocket) { |
| 1536 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1537 | |
| 1538 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1539 | ClientSocketHandle handle; |
| 1540 | TestCompletionCallback callback; |
| 1541 | EXPECT_EQ( |
| 1542 | ERR_IO_PENDING, |
| 1543 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1544 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1545 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1546 | pool_.get(), NetLogWithSource())); |
| 1547 | |
| 1548 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 1549 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1550 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1551 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
| 1552 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 1553 | |
| 1554 | handle.ResetAndCloseSocket(); |
| 1555 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1556 | } |
| 1557 | |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1558 | // This test will start up a socket request and then call Reset() on the handle. |
| 1559 | // The pending ConnectJob should not be destroyed. |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1560 | TEST_F(ClientSocketPoolBaseTest, CancelRequestKeepsConnectJob) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1561 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1562 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1563 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1564 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1565 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1566 | EXPECT_EQ( |
| 1567 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1568 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1569 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1570 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1571 | pool_.get(), NetLogWithSource())); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1572 | handle.Reset(); |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1573 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1574 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1575 | } |
| 1576 | |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1577 | // This test will start up a socket request and then call ResetAndCloseSocket() |
| 1578 | // on the handle. The pending ConnectJob or connected socket should be |
| 1579 | // destroyed. |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1580 | TEST_F(ClientSocketPoolBaseTest, CancelRequestAndCloseSocket) { |
| 1581 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1582 | |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1583 | // When true, the socket connects before it's canceled. |
| 1584 | for (bool cancel_when_callback_pending : {false, true}) { |
| 1585 | if (cancel_when_callback_pending) { |
| 1586 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 1587 | } else { |
| 1588 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1589 | } |
| 1590 | ClientSocketHandle handle; |
| 1591 | TestCompletionCallback callback; |
| 1592 | EXPECT_EQ( |
| 1593 | ERR_IO_PENDING, |
| 1594 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1595 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1596 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1597 | pool_.get(), NetLogWithSource())); |
| 1598 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1599 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1600 | |
| 1601 | if (cancel_when_callback_pending) { |
| 1602 | client_socket_factory_.SignalJobs(); |
| 1603 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1604 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 1605 | } |
| 1606 | |
| 1607 | handle.ResetAndCloseSocket(); |
| 1608 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1609 | } |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1610 | } |
| 1611 | |
| 1612 | TEST_F(ClientSocketPoolBaseTest, |
| 1613 | CancelRequestAndCloseSocketWhenMoreRequestsThanConnectJobs) { |
| 1614 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1615 | |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1616 | // When true, the sockets connect before they're canceled. |
| 1617 | for (bool cancel_when_callback_pending : {false, true}) { |
| 1618 | if (cancel_when_callback_pending) { |
| 1619 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 1620 | } else { |
| 1621 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1622 | } |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1623 | |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1624 | std::vector<std::unique_ptr<ClientSocketHandle>> handles; |
| 1625 | TestCompletionCallback callback; |
| 1626 | // Make |kDefaultMaxSockets + 1| socket requests. |
| 1627 | for (int i = 0; i < kDefaultMaxSocketsPerGroup + 1; ++i) { |
| 1628 | std::unique_ptr<ClientSocketHandle> handle = |
| 1629 | std::make_unique<ClientSocketHandle>(); |
| 1630 | EXPECT_EQ(ERR_IO_PENDING, |
| 1631 | handle->Init( |
| 1632 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1633 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1634 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1635 | pool_.get(), NetLogWithSource())); |
| 1636 | handles.push_back(std::move(handle)); |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1637 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1638 | EXPECT_EQ( |
| 1639 | static_cast<size_t>(std::min(i + 1, kDefaultMaxSocketsPerGroup)), |
| 1640 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1641 | } |
| 1642 | |
| 1643 | if (cancel_when_callback_pending) { |
| 1644 | client_socket_factory_.SignalJobs(); |
| 1645 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1646 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
| 1647 | pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 1648 | } |
| 1649 | |
| 1650 | // Calling ResetAndCloseSocket() on a handle should not cancel a ConnectJob |
| 1651 | // or close a socket, since there are more requests than ConnectJobs or |
| 1652 | // sockets. |
| 1653 | handles[kDefaultMaxSocketsPerGroup]->ResetAndCloseSocket(); |
| 1654 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1655 | if (cancel_when_callback_pending) { |
| 1656 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
| 1657 | pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 1658 | } else { |
| 1659 | EXPECT_EQ(static_cast<size_t>(kDefaultMaxSocketsPerGroup), |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1660 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1661 | } |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1662 | |
| 1663 | // Calling ResetAndCloseSocket() on other handles should cancel a ConnectJob |
| 1664 | // or close a socket. |
| 1665 | for (int i = kDefaultMaxSocketsPerGroup - 1; i >= 0; --i) { |
| 1666 | handles[i]->ResetAndCloseSocket(); |
| 1667 | if (i > 0) { |
| 1668 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1669 | if (cancel_when_callback_pending) { |
| 1670 | EXPECT_EQ(i, |
| 1671 | pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 1672 | } else { |
| 1673 | EXPECT_EQ(static_cast<size_t>(i), |
| 1674 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1675 | } |
| 1676 | } else { |
| 1677 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1678 | } |
| 1679 | } |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1680 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1681 | } |
| 1682 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1683 | TEST_F(ClientSocketPoolBaseTest, ConnectCancelConnect) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1684 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1685 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1686 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 1687 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1688 | TestCompletionCallback callback; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1689 | |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1690 | EXPECT_EQ( |
| 1691 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1692 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1693 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1694 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1695 | pool_.get(), NetLogWithSource())); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1696 | |
| 1697 | handle.Reset(); |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1698 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1699 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1700 | |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1701 | // This will create a second ConnectJob, since the other ConnectJob was |
| 1702 | // previously assigned to a request. |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1703 | TestCompletionCallback callback2; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1704 | EXPECT_EQ( |
| 1705 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1706 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1707 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1708 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1709 | pool_.get(), NetLogWithSource())); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1710 | |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1711 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1712 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1713 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1714 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1715 | EXPECT_FALSE(callback.have_result()); |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1716 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1717 | // One ConnectJob completed, and its socket is now assigned to |handle|. |
| 1718 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 1719 | // The other ConnectJob should have either completed, or still be connecting. |
| 1720 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")) + |
| 1721 | pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1722 | |
| 1723 | handle.Reset(); |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1724 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1725 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")) + |
| 1726 | pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
| 1727 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1728 | } |
| 1729 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1730 | TEST_F(ClientSocketPoolBaseTest, CancelRequest) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1731 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1732 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1733 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1734 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1735 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
| 1736 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1737 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1738 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 1739 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1740 | |
| 1741 | // Cancel a request. |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1742 | size_t index_to_cancel = kDefaultMaxSocketsPerGroup + 2; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1743 | EXPECT_FALSE((*requests())[index_to_cancel]->handle()->is_initialized()); |
| 1744 | (*requests())[index_to_cancel]->handle()->Reset(); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1745 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1746 | ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1747 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1748 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
| 1749 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1750 | EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup - 1, |
| 1751 | completion_count()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1752 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1753 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1754 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1755 | EXPECT_EQ(5, GetOrderOfRequest(3)); |
| 1756 | EXPECT_EQ(3, GetOrderOfRequest(4)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1757 | EXPECT_EQ(ClientSocketPoolTest::kRequestNotFound, |
| 1758 | GetOrderOfRequest(5)); // Canceled request. |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1759 | EXPECT_EQ(4, GetOrderOfRequest(6)); |
| 1760 | EXPECT_EQ(6, GetOrderOfRequest(7)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1761 | |
| 1762 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1763 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(8)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1764 | } |
| 1765 | |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1766 | // Function to be used as a callback on socket request completion. It first |
| 1767 | // disconnects the successfully connected socket from the first request, and |
| 1768 | // then reuses the ClientSocketHandle to request another socket. |
| 1769 | // |
| 1770 | // |nested_callback| is called with the result of the second socket request. |
| 1771 | void RequestSocketOnComplete(ClientSocketHandle* handle, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1772 | TransportClientSocketPool* pool, |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1773 | TestConnectJobFactory* test_connect_job_factory, |
| 1774 | TestConnectJob::JobType next_job_type, |
Bence Béky | a4a5093 | 2018-08-10 13:39:41 | [diff] [blame] | 1775 | TestCompletionCallback* nested_callback, |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1776 | int first_request_result) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1777 | EXPECT_THAT(first_request_result, IsOk()); |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1778 | |
| 1779 | test_connect_job_factory->set_job_type(next_job_type); |
| 1780 | |
| 1781 | // Don't allow reuse of the socket. Disconnect it and then release it. |
| 1782 | if (handle->socket()) |
| 1783 | handle->socket()->Disconnect(); |
| 1784 | handle->Reset(); |
| 1785 | |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1786 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1787 | int rv = handle->Init( |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 1788 | TestGroupId("a"), |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1789 | ClientSocketPool::SocketParams::CreateForHttpForTesting(), base::nullopt, |
| 1790 | LOWEST, SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 1791 | nested_callback->callback(), ClientSocketPool::ProxyAuthCallback(), pool, |
| 1792 | NetLogWithSource()); |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1793 | if (rv != ERR_IO_PENDING) { |
| 1794 | DCHECK_EQ(TestConnectJob::kMockJob, next_job_type); |
Bence Béky | a4a5093 | 2018-08-10 13:39:41 | [diff] [blame] | 1795 | nested_callback->callback().Run(rv); |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1796 | } else { |
| 1797 | DCHECK_EQ(TestConnectJob::kMockPendingJob, next_job_type); |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1798 | } |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1799 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1800 | |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1801 | // Tests the case where a second socket is requested in a completion callback, |
| 1802 | // and the second socket connects asynchronously. Reuses the same |
| 1803 | // ClientSocketHandle for the second socket, after disconnecting the first. |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1804 | TEST_F(ClientSocketPoolBaseTest, RequestPendingJobTwice) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1805 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1806 | |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 1807 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 1808 | ClientSocketHandle handle; |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1809 | TestCompletionCallback second_result_callback; |
| 1810 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1811 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Paul Jensen | 8d6f87ec | 2018-01-13 00:46:54 | [diff] [blame] | 1812 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1813 | base::BindOnce(&RequestSocketOnComplete, &handle, pool_.get(), |
| 1814 | connect_job_factory_, TestConnectJob::kMockPendingJob, |
| 1815 | &second_result_callback), |
| 1816 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1817 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1818 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1819 | EXPECT_THAT(second_result_callback.WaitForResult(), IsOk()); |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1820 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1821 | |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1822 | // Tests the case where a second socket is requested in a completion callback, |
| 1823 | // and the second socket connects synchronously. Reuses the same |
| 1824 | // ClientSocketHandle for the second socket, after disconnecting the first. |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1825 | TEST_F(ClientSocketPoolBaseTest, RequestPendingJobThenSynchronous) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1826 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1827 | |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 1828 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 1829 | ClientSocketHandle handle; |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1830 | TestCompletionCallback second_result_callback; |
| 1831 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1832 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Paul Jensen | 8d6f87ec | 2018-01-13 00:46:54 | [diff] [blame] | 1833 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1834 | base::BindOnce(&RequestSocketOnComplete, &handle, pool_.get(), |
| 1835 | connect_job_factory_, TestConnectJob::kMockPendingJob, |
| 1836 | &second_result_callback), |
| 1837 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1838 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1839 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1840 | EXPECT_THAT(second_result_callback.WaitForResult(), IsOk()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1841 | } |
| 1842 | |
| 1843 | // Make sure that pending requests get serviced after active requests get |
| 1844 | // cancelled. |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1845 | TEST_F(ClientSocketPoolBaseTest, CancelActiveRequestWithPendingRequests) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1846 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1847 | |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 1848 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1849 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1850 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1851 | IsError(ERR_IO_PENDING)); |
| 1852 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1853 | IsError(ERR_IO_PENDING)); |
| 1854 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1855 | IsError(ERR_IO_PENDING)); |
| 1856 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1857 | IsError(ERR_IO_PENDING)); |
| 1858 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1859 | IsError(ERR_IO_PENDING)); |
| 1860 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1861 | IsError(ERR_IO_PENDING)); |
| 1862 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1863 | IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1864 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1865 | // Now, kDefaultMaxSocketsPerGroup requests should be active. |
| 1866 | // Let's cancel them. |
| 1867 | for (int i = 0; i < kDefaultMaxSocketsPerGroup; ++i) { |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1868 | ASSERT_FALSE(request(i)->handle()->is_initialized()); |
| 1869 | request(i)->handle()->Reset(); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1870 | } |
| 1871 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1872 | // Let's wait for the rest to complete now. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1873 | for (size_t i = kDefaultMaxSocketsPerGroup; i < requests_size(); ++i) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1874 | EXPECT_THAT(request(i)->WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1875 | request(i)->handle()->Reset(); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1876 | } |
| 1877 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1878 | EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup, |
| 1879 | completion_count()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1880 | } |
| 1881 | |
| 1882 | // Make sure that pending requests get serviced after active requests fail. |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1883 | TEST_F(ClientSocketPoolBaseTest, FailingActiveRequestWithPendingRequests) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1884 | const size_t kMaxSockets = 5; |
| 1885 | CreatePool(kMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1886 | |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 1887 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1888 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1889 | const size_t kNumberOfRequests = 2 * kDefaultMaxSocketsPerGroup + 1; |
| 1890 | ASSERT_LE(kNumberOfRequests, kMaxSockets); // Otherwise the test will hang. |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1891 | |
| 1892 | // Queue up all the requests |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1893 | for (size_t i = 0; i < kNumberOfRequests; ++i) |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1894 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1895 | IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1896 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1897 | for (size_t i = 0; i < kNumberOfRequests; ++i) |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1898 | EXPECT_THAT(request(i)->WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1899 | } |
| 1900 | |
mmenke | 9d72fe4 | 2017-05-18 22:36:07 | [diff] [blame] | 1901 | // Make sure that pending requests that complete synchronously get serviced |
| 1902 | // after active requests fail. See https://crbug.com/723748 |
| 1903 | TEST_F(ClientSocketPoolBaseTest, HandleMultipleSyncFailuresAfterAsyncFailure) { |
| 1904 | const size_t kNumberOfRequests = 10; |
| 1905 | const size_t kMaxSockets = 1; |
| 1906 | CreatePool(kMaxSockets, kMaxSockets); |
| 1907 | |
| 1908 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
| 1909 | |
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 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
| 1914 | |
| 1915 | // Queue up all the other requests |
| 1916 | for (size_t i = 1; i < kNumberOfRequests; ++i) |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1917 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1918 | IsError(ERR_IO_PENDING)); |
mmenke | 9d72fe4 | 2017-05-18 22:36:07 | [diff] [blame] | 1919 | |
| 1920 | // Make sure all requests fail, instead of hanging. |
| 1921 | for (size_t i = 0; i < kNumberOfRequests; ++i) |
| 1922 | EXPECT_THAT(request(i)->WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 1923 | } |
| 1924 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1925 | TEST_F(ClientSocketPoolBaseTest, CancelActiveRequestThenRequestSocket) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1926 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1927 | |
| 1928 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1929 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1930 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1931 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1932 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1933 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1934 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1935 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1936 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1937 | |
| 1938 | // Cancel the active request. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1939 | handle.Reset(); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1940 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1941 | rv = handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1942 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1943 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1944 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1945 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 1946 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1947 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1948 | EXPECT_FALSE(handle.is_reused()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 1949 | TestLoadTimingInfoConnectedNotReused(handle); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1950 | EXPECT_EQ(2, client_socket_factory_.allocation_count()); |
| 1951 | } |
| 1952 | |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1953 | TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketsForced) { |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 1954 | const char kReason[] = "Really nifty reason"; |
| 1955 | |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1956 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1957 | ClientSocketHandle handle; |
| 1958 | TestCompletionCallback callback; |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 1959 | RecordingBoundTestNetLog log; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1960 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1961 | TestGroupId("a"), params_, base::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1962 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1963 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1964 | EXPECT_THAT(rv, IsOk()); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 1965 | ASSERT_TRUE(handle.socket()); |
| 1966 | NetLogSource source = handle.socket()->NetLog().source(); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1967 | handle.Reset(); |
| 1968 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 1969 | pool_->CloseIdleSockets(kReason); |
| 1970 | ExpectSocketClosedWithReason(source, kReason); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1971 | } |
| 1972 | |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1973 | TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketsInGroupForced) { |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1974 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1975 | TestCompletionCallback callback; |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 1976 | RecordingBoundTestNetLog log; |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1977 | ClientSocketHandle handle1; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1978 | int rv = handle1.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1979 | TestGroupId("a"), params_, base::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1980 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1981 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1982 | EXPECT_THAT(rv, IsOk()); |
| 1983 | ClientSocketHandle handle2; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1984 | rv = handle2.Init(TestGroupId("a"), params_, base::nullopt, LOWEST, |
| 1985 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1986 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1987 | pool_.get(), log.bound()); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1988 | ClientSocketHandle handle3; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1989 | rv = handle3.Init(TestGroupId("b"), params_, base::nullopt, LOWEST, |
| 1990 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1991 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1992 | pool_.get(), log.bound()); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1993 | EXPECT_THAT(rv, IsOk()); |
| 1994 | handle1.Reset(); |
| 1995 | handle2.Reset(); |
| 1996 | handle3.Reset(); |
| 1997 | EXPECT_EQ(3, pool_->IdleSocketCount()); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 1998 | pool_->CloseIdleSocketsInGroup(TestGroupId("a"), "Very good reason"); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1999 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 2000 | } |
| 2001 | |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 2002 | TEST_F(ClientSocketPoolBaseTest, CleanUpUnusableIdleSockets) { |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 2003 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2004 | ClientSocketHandle handle; |
| 2005 | TestCompletionCallback callback; |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 2006 | RecordingBoundTestNetLog log; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2007 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2008 | TestGroupId("a"), params_, base::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2009 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2010 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 2011 | EXPECT_THAT(rv, IsOk()); |
| 2012 | StreamSocket* socket = handle.socket(); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 2013 | ASSERT_TRUE(socket); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 2014 | handle.Reset(); |
| 2015 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 2016 | |
| 2017 | // Disconnect socket now to make the socket unusable. |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 2018 | NetLogSource source = socket->NetLog().source(); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 2019 | socket->Disconnect(); |
| 2020 | ClientSocketHandle handle2; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2021 | rv = handle2.Init(TestGroupId("a"), params_, base::nullopt, LOWEST, |
| 2022 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2023 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2024 | pool_.get(), log.bound()); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 2025 | EXPECT_THAT(rv, IsOk()); |
| 2026 | EXPECT_FALSE(handle2.is_reused()); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 2027 | |
| 2028 | // This is admittedly not an accurate error in this case, but normally code |
| 2029 | // doesn't secretly keep a raw pointers to sockets returned to the socket pool |
| 2030 | // and close them out of band, so discovering an idle socket was closed when |
| 2031 | // trying to reuse it normally means it was closed by the remote side. |
| 2032 | ExpectSocketClosedWithReason( |
| 2033 | source, TransportClientSocketPool::kRemoteSideClosedConnection); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 2034 | } |
| 2035 | |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 2036 | // Regression test for http://crbug.com/17985. |
| 2037 | TEST_F(ClientSocketPoolBaseTest, GroupWithPendingRequestsIsNotEmpty) { |
| 2038 | const int kMaxSockets = 3; |
| 2039 | const int kMaxSocketsPerGroup = 2; |
| 2040 | CreatePool(kMaxSockets, kMaxSocketsPerGroup); |
| 2041 | |
[email protected] | ac790b4 | 2009-12-02 04:31:31 | [diff] [blame] | 2042 | const RequestPriority kHighPriority = HIGHEST; |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 2043 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2044 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 2045 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 2046 | |
| 2047 | // This is going to be a pending request in an otherwise empty group. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2048 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2049 | IsError(ERR_IO_PENDING)); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 2050 | |
| 2051 | // Reach the maximum socket limit. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2052 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 2053 | |
| 2054 | // Create a stalled group with high priorities. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2055 | EXPECT_THAT(StartRequest(TestGroupId("c"), kHighPriority), |
| 2056 | IsError(ERR_IO_PENDING)); |
| 2057 | EXPECT_THAT(StartRequest(TestGroupId("c"), kHighPriority), |
| 2058 | IsError(ERR_IO_PENDING)); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 2059 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2060 | // Release the first two sockets from TestGroupId("a"). Because this is a |
| 2061 | // keepalive, the first release will unblock the pending request for |
| 2062 | // TestGroupId("a"). The second release will unblock a request for "c", |
| 2063 | // because it is the next high priority socket. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2064 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
| 2065 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 2066 | |
| 2067 | // Closing idle sockets should not get us into trouble, but in the bug |
| 2068 | // we were hitting a CHECK here. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2069 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 2070 | pool_->CloseIdleSockets("Very good reason"); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 2071 | |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 2072 | // Run the released socket wakeups. |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 2073 | base::RunLoop().RunUntilIdle(); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 2074 | } |
| 2075 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2076 | TEST_F(ClientSocketPoolBaseTest, BasicAsynchronous) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2077 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2078 | |
| 2079 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2080 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2081 | TestCompletionCallback callback; |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 2082 | RecordingBoundTestNetLog log; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2083 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2084 | TestGroupId("a"), params_, base::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2085 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2086 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2087 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2088 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2089 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 2090 | TestLoadTimingInfoNotConnected(handle); |
| 2091 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2092 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2093 | EXPECT_TRUE(handle.is_initialized()); |
| 2094 | EXPECT_TRUE(handle.socket()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 2095 | TestLoadTimingInfoConnectedNotReused(handle); |
| 2096 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2097 | handle.Reset(); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 2098 | TestLoadTimingInfoNotConnected(handle); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 2099 | |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 2100 | auto entries = log.GetEntries(); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 2101 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2102 | EXPECT_EQ(5u, entries.size()); |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 2103 | EXPECT_TRUE(LogContainsEvent( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2104 | entries, 0, NetLogEventType::TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 2105 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2106 | EXPECT_TRUE(LogContainsBeginEvent(entries, 1, NetLogEventType::SOCKET_POOL)); |
| 2107 | EXPECT_TRUE(LogContainsEvent( |
| 2108 | entries, 2, NetLogEventType::SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
| 2109 | NetLogEventPhase::NONE)); |
| 2110 | EXPECT_TRUE(LogContainsEvent(entries, 3, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 2111 | NetLogEventType::SOCKET_POOL_BOUND_TO_SOCKET, |
| 2112 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2113 | EXPECT_TRUE(LogContainsEndEvent(entries, 4, NetLogEventType::SOCKET_POOL)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2114 | } |
| 2115 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2116 | TEST_F(ClientSocketPoolBaseTest, |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2117 | InitConnectionAsynchronousFailure) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2118 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2119 | |
| 2120 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2121 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2122 | TestCompletionCallback callback; |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 2123 | RecordingBoundTestNetLog log; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2124 | // Set the additional error state members to ensure that they get cleared. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2125 | handle.set_is_ssl_error(true); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 2126 | handle.set_ssl_cert_request_info(base::MakeRefCounted<SSLCertRequestInfo>()); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2127 | EXPECT_EQ( |
| 2128 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2129 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2130 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2131 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2132 | pool_.get(), log.bound())); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2133 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2134 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2135 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2136 | EXPECT_FALSE(handle.is_ssl_error()); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 2137 | EXPECT_FALSE(handle.ssl_cert_request_info()); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 2138 | |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 2139 | auto entries = log.GetEntries(); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 2140 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2141 | EXPECT_EQ(4u, entries.size()); |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 2142 | EXPECT_TRUE(LogContainsEvent( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2143 | entries, 0, NetLogEventType::TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 2144 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2145 | EXPECT_TRUE(LogContainsBeginEvent(entries, 1, NetLogEventType::SOCKET_POOL)); |
| 2146 | EXPECT_TRUE(LogContainsEvent( |
| 2147 | entries, 2, NetLogEventType::SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
| 2148 | NetLogEventPhase::NONE)); |
| 2149 | EXPECT_TRUE(LogContainsEndEvent(entries, 3, NetLogEventType::SOCKET_POOL)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2150 | } |
| 2151 | |
mmenke | 6be122f | 2015-03-09 22:22:47 | [diff] [blame] | 2152 | // Check that an async ConnectJob failure does not result in creation of a new |
| 2153 | // ConnectJob when there's another pending request also waiting on its own |
| 2154 | // ConnectJob. See http://crbug.com/463960. |
| 2155 | TEST_F(ClientSocketPoolBaseTest, AsyncFailureWithPendingRequestWithJob) { |
| 2156 | CreatePool(2, 2); |
| 2157 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
| 2158 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2159 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2160 | IsError(ERR_IO_PENDING)); |
| 2161 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2162 | IsError(ERR_IO_PENDING)); |
mmenke | 6be122f | 2015-03-09 22:22:47 | [diff] [blame] | 2163 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2164 | EXPECT_THAT(request(0)->WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 2165 | EXPECT_THAT(request(1)->WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
mmenke | 6be122f | 2015-03-09 22:22:47 | [diff] [blame] | 2166 | |
| 2167 | EXPECT_EQ(2, client_socket_factory_.allocation_count()); |
| 2168 | } |
| 2169 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2170 | TEST_F(ClientSocketPoolBaseTest, TwoRequestsCancelOne) { |
[email protected] | b22b516 | 2010-03-16 07:53:10 | [diff] [blame] | 2171 | // TODO(eroman): Add back the log expectations! Removed them because the |
| 2172 | // ordering is difficult, and some may fire during destructor. |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2173 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2174 | |
| 2175 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2176 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2177 | TestCompletionCallback callback; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2178 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2179 | TestCompletionCallback callback2; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2180 | |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2181 | EXPECT_EQ( |
| 2182 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2183 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2184 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2185 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2186 | pool_.get(), NetLogWithSource())); |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 2187 | RecordingBoundTestNetLog log2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 2188 | EXPECT_EQ( |
| 2189 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2190 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2191 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2192 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2193 | pool_.get(), NetLogWithSource())); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2194 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2195 | handle.Reset(); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2196 | |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 2197 | |
| 2198 | // 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] | 2199 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2200 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2201 | handle2.Reset(); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 2202 | |
| 2203 | // Now request 2 has actually finished. |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 2204 | // TODO(eroman): Add back log expectations. |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2205 | } |
| 2206 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2207 | TEST_F(ClientSocketPoolBaseTest, CancelRequestLimitsJobs) { |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 2208 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2209 | |
[email protected] | 17a0c6c | 2009-08-04 00:07:04 | [diff] [blame] | 2210 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2211 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2212 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
| 2213 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 2214 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 2215 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 2216 | |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2217 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2218 | static_cast<int>( |
| 2219 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2220 | (*requests())[2]->handle()->Reset(); |
| 2221 | (*requests())[3]->handle()->Reset(); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2222 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2223 | static_cast<int>( |
| 2224 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 2225 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2226 | (*requests())[1]->handle()->Reset(); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2227 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2228 | static_cast<int>( |
| 2229 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 2230 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2231 | (*requests())[0]->handle()->Reset(); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2232 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2233 | static_cast<int>( |
| 2234 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 2235 | } |
| 2236 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2237 | // When requests and ConnectJobs are not coupled, the request will get serviced |
| 2238 | // by whatever comes first. |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2239 | TEST_F(ClientSocketPoolBaseTest, ReleaseSockets) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2240 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2241 | |
| 2242 | // Start job 1 (async OK) |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 2243 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2244 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2245 | std::vector<TestSocketRequest*> request_order; |
| 2246 | size_t completion_count; // unused |
| 2247 | TestSocketRequest req1(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2248 | int rv = req1.handle()->Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2249 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2250 | ClientSocketPool::RespectLimits::ENABLED, req1.callback(), |
| 2251 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2252 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 2253 | EXPECT_THAT(req1.WaitForResult(), IsOk()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2254 | |
| 2255 | // Job 1 finished OK. Start job 2 (also async OK). Request 3 is pending |
| 2256 | // without a job. |
| 2257 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2258 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2259 | TestSocketRequest req2(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2260 | rv = req2.handle()->Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2261 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2262 | ClientSocketPool::RespectLimits::ENABLED, req2.callback(), |
| 2263 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2264 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2265 | TestSocketRequest req3(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2266 | rv = req3.handle()->Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2267 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2268 | ClientSocketPool::RespectLimits::ENABLED, req3.callback(), |
| 2269 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2270 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2271 | |
| 2272 | // Both Requests 2 and 3 are pending. We release socket 1 which should |
| 2273 | // service request 2. Request 3 should still be waiting. |
[email protected] | a6c59f6 | 2009-07-29 16:33:33 | [diff] [blame] | 2274 | req1.handle()->Reset(); |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 2275 | // Run the released socket wakeups. |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 2276 | base::RunLoop().RunUntilIdle(); |
[email protected] | a6c59f6 | 2009-07-29 16:33:33 | [diff] [blame] | 2277 | ASSERT_TRUE(req2.handle()->socket()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2278 | EXPECT_THAT(req2.WaitForResult(), IsOk()); |
[email protected] | a6c59f6 | 2009-07-29 16:33:33 | [diff] [blame] | 2279 | EXPECT_FALSE(req3.handle()->socket()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2280 | |
| 2281 | // Signal job 2, which should service request 3. |
| 2282 | |
| 2283 | client_socket_factory_.SignalJobs(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2284 | EXPECT_THAT(req3.WaitForResult(), IsOk()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2285 | |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2286 | ASSERT_EQ(3u, request_order.size()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2287 | EXPECT_EQ(&req1, request_order[0]); |
| 2288 | EXPECT_EQ(&req2, request_order[1]); |
| 2289 | EXPECT_EQ(&req3, request_order[2]); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2290 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2291 | } |
| 2292 | |
| 2293 | // The requests are not coupled to the jobs. So, the requests should finish in |
| 2294 | // their priority / insertion order. |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2295 | TEST_F(ClientSocketPoolBaseTest, PendingJobCompletionOrder) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2296 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2297 | // First two jobs are async. |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 2298 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2299 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2300 | std::vector<TestSocketRequest*> request_order; |
| 2301 | size_t completion_count; // unused |
| 2302 | TestSocketRequest req1(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2303 | int rv = req1.handle()->Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2304 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2305 | ClientSocketPool::RespectLimits::ENABLED, req1.callback(), |
| 2306 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2307 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2308 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2309 | TestSocketRequest req2(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2310 | rv = req2.handle()->Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2311 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2312 | ClientSocketPool::RespectLimits::ENABLED, req2.callback(), |
| 2313 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2314 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2315 | |
| 2316 | // The pending job is sync. |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 2317 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2318 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2319 | TestSocketRequest req3(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2320 | rv = req3.handle()->Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2321 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2322 | ClientSocketPool::RespectLimits::ENABLED, req3.callback(), |
| 2323 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2324 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2325 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2326 | EXPECT_THAT(req1.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 2327 | EXPECT_THAT(req2.WaitForResult(), IsOk()); |
| 2328 | EXPECT_THAT(req3.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2329 | |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2330 | ASSERT_EQ(3u, request_order.size()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2331 | EXPECT_EQ(&req1, request_order[0]); |
| 2332 | EXPECT_EQ(&req2, request_order[1]); |
| 2333 | EXPECT_EQ(&req3, request_order[2]); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2334 | } |
| 2335 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2336 | // Test GetLoadState in the case there's only one socket request. |
| 2337 | TEST_F(ClientSocketPoolBaseTest, LoadStateOneRequest) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2338 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2339 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2340 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2341 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2342 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2343 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2344 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2345 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2346 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2347 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2348 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2349 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2350 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE); |
| 2351 | EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle.GetLoadState()); |
| 2352 | |
| 2353 | // No point in completing the connection, since ClientSocketHandles only |
| 2354 | // expect the LoadState to be checked while connecting. |
| 2355 | } |
| 2356 | |
| 2357 | // Test GetLoadState in the case there are two socket requests. |
| 2358 | TEST_F(ClientSocketPoolBaseTest, LoadStateTwoRequests) { |
| 2359 | CreatePool(2, 2); |
| 2360 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2361 | |
| 2362 | ClientSocketHandle handle; |
| 2363 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2364 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2365 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2366 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2367 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2368 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2369 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_RESOLVING_HOST); |
| 2370 | |
| 2371 | ClientSocketHandle handle2; |
| 2372 | TestCompletionCallback callback2; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2373 | rv = handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2374 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2375 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2376 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2377 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2378 | client_socket_factory_.SetJobLoadState(1, LOAD_STATE_RESOLVING_HOST); |
| 2379 | |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 2380 | // Each handle should reflect the state of its own job. |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2381 | EXPECT_EQ(LOAD_STATE_RESOLVING_HOST, handle.GetLoadState()); |
| 2382 | EXPECT_EQ(LOAD_STATE_RESOLVING_HOST, handle2.GetLoadState()); |
| 2383 | |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 2384 | // Update the state of the first job. |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2385 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_CONNECTING); |
| 2386 | |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 2387 | // Only the state of the first request should have changed. |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2388 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2389 | EXPECT_EQ(LOAD_STATE_RESOLVING_HOST, handle2.GetLoadState()); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 2390 | |
| 2391 | // Update the state of the second job. |
| 2392 | client_socket_factory_.SetJobLoadState(1, LOAD_STATE_SSL_HANDSHAKE); |
| 2393 | |
| 2394 | // Only the state of the second request should have changed. |
| 2395 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
| 2396 | EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle2.GetLoadState()); |
| 2397 | |
| 2398 | // Second job connects and the first request gets the socket. The |
| 2399 | // second handle switches to the state of the remaining ConnectJob. |
| 2400 | client_socket_factory_.SignalJob(1); |
| 2401 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 2402 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle2.GetLoadState()); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2403 | } |
| 2404 | |
| 2405 | // Test GetLoadState in the case the per-group limit is reached. |
| 2406 | TEST_F(ClientSocketPoolBaseTest, LoadStateGroupLimit) { |
| 2407 | CreatePool(2, 1); |
| 2408 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2409 | |
| 2410 | ClientSocketHandle handle; |
| 2411 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2412 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2413 | TestGroupId("a"), params_, base::nullopt, MEDIUM, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2414 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2415 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2416 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2417 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
| 2418 | |
| 2419 | // Request another socket from the same pool, buth with a higher priority. |
| 2420 | // The first request should now be stalled at the socket group limit. |
| 2421 | ClientSocketHandle handle2; |
| 2422 | TestCompletionCallback callback2; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2423 | rv = handle2.Init(TestGroupId("a"), params_, base::nullopt, HIGHEST, |
| 2424 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2425 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2426 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2427 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2428 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET, handle.GetLoadState()); |
| 2429 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle2.GetLoadState()); |
| 2430 | |
| 2431 | // The first handle should remain stalled as the other socket goes through |
| 2432 | // the connect process. |
| 2433 | |
| 2434 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE); |
| 2435 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET, handle.GetLoadState()); |
| 2436 | EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle2.GetLoadState()); |
| 2437 | |
| 2438 | client_socket_factory_.SignalJob(0); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2439 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2440 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET, handle.GetLoadState()); |
| 2441 | |
| 2442 | // Closing the second socket should cause the stalled handle to finally get a |
| 2443 | // ConnectJob. |
| 2444 | handle2.socket()->Disconnect(); |
| 2445 | handle2.Reset(); |
| 2446 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
| 2447 | } |
| 2448 | |
| 2449 | // Test GetLoadState in the case the per-pool limit is reached. |
| 2450 | TEST_F(ClientSocketPoolBaseTest, LoadStatePoolLimit) { |
| 2451 | CreatePool(2, 2); |
| 2452 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2453 | |
| 2454 | ClientSocketHandle handle; |
| 2455 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2456 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2457 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2458 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2459 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2460 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2461 | |
| 2462 | // Request for socket from another pool. |
| 2463 | ClientSocketHandle handle2; |
| 2464 | TestCompletionCallback callback2; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2465 | rv = handle2.Init(TestGroupId("b"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2466 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2467 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2468 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2469 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2470 | |
| 2471 | // Request another socket from the first pool. Request should stall at the |
| 2472 | // socket pool limit. |
| 2473 | ClientSocketHandle handle3; |
| 2474 | TestCompletionCallback callback3; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2475 | rv = handle3.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2476 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2477 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2478 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2479 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2480 | |
| 2481 | // The third handle should remain stalled as the other sockets in its group |
| 2482 | // goes through the connect process. |
| 2483 | |
| 2484 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
| 2485 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL, handle3.GetLoadState()); |
| 2486 | |
| 2487 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE); |
| 2488 | EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle.GetLoadState()); |
| 2489 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL, handle3.GetLoadState()); |
| 2490 | |
| 2491 | client_socket_factory_.SignalJob(0); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2492 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2493 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL, handle3.GetLoadState()); |
| 2494 | |
| 2495 | // Closing a socket should allow the stalled handle to finally get a new |
| 2496 | // ConnectJob. |
| 2497 | handle.socket()->Disconnect(); |
| 2498 | handle.Reset(); |
| 2499 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle3.GetLoadState()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2500 | } |
| 2501 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2502 | TEST_F(ClientSocketPoolBaseTest, CertError) { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2503 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2504 | connect_job_factory_->set_job_type(TestConnectJob::kMockCertErrorJob); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2505 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2506 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2507 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2508 | EXPECT_EQ( |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2509 | ERR_CERT_COMMON_NAME_INVALID, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2510 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2511 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2512 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2513 | pool_.get(), NetLogWithSource())); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2514 | EXPECT_TRUE(handle.is_initialized()); |
| 2515 | EXPECT_TRUE(handle.socket()); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2516 | } |
| 2517 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2518 | TEST_F(ClientSocketPoolBaseTest, AsyncCertError) { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2519 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2520 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2521 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingCertErrorJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2522 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2523 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2524 | EXPECT_EQ( |
| 2525 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2526 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2527 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2528 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2529 | pool_.get(), NetLogWithSource())); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2530 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2531 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2532 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_CERT_COMMON_NAME_INVALID)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2533 | EXPECT_TRUE(handle.is_initialized()); |
| 2534 | EXPECT_TRUE(handle.socket()); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2535 | } |
| 2536 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2537 | TEST_F(ClientSocketPoolBaseTest, AdditionalErrorStateSynchronous) { |
| 2538 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2539 | connect_job_factory_->set_job_type( |
| 2540 | TestConnectJob::kMockAdditionalErrorStateJob); |
| 2541 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2542 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2543 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2544 | EXPECT_EQ( |
| 2545 | ERR_CONNECTION_FAILED, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2546 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2547 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2548 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2549 | pool_.get(), NetLogWithSource())); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2550 | EXPECT_FALSE(handle.is_initialized()); |
| 2551 | EXPECT_FALSE(handle.socket()); |
| 2552 | EXPECT_TRUE(handle.is_ssl_error()); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 2553 | EXPECT_TRUE(handle.ssl_cert_request_info()); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2554 | } |
| 2555 | |
| 2556 | TEST_F(ClientSocketPoolBaseTest, AdditionalErrorStateAsynchronous) { |
| 2557 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2558 | |
| 2559 | connect_job_factory_->set_job_type( |
| 2560 | TestConnectJob::kMockPendingAdditionalErrorStateJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2561 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2562 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2563 | EXPECT_EQ( |
| 2564 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2565 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2566 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2567 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2568 | pool_.get(), NetLogWithSource())); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2569 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2570 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2571 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2572 | EXPECT_FALSE(handle.is_initialized()); |
| 2573 | EXPECT_FALSE(handle.socket()); |
| 2574 | EXPECT_TRUE(handle.is_ssl_error()); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 2575 | EXPECT_TRUE(handle.ssl_cert_request_info()); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2576 | } |
| 2577 | |
martijn | 003cd61 | 2016-05-19 22:24:38 | [diff] [blame] | 2578 | // Make sure we can reuse sockets. |
| 2579 | TEST_F(ClientSocketPoolBaseTest, CleanupTimedOutIdleSocketsReuse) { |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2580 | CreatePoolWithIdleTimeouts( |
| 2581 | kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2582 | base::TimeDelta(), // Time out unused sockets immediately. |
| 2583 | base::TimeDelta::FromDays(1)); // Don't time out used sockets. |
| 2584 | |
| 2585 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2586 | |
| 2587 | ClientSocketHandle handle; |
| 2588 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2589 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2590 | TestGroupId("a"), params_, base::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2591 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2592 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2593 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2594 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2595 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2596 | ASSERT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2597 | |
| 2598 | // Use and release the socket. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 2599 | EXPECT_EQ(1, handle.socket()->Write(nullptr, 1, CompletionOnceCallback(), |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 2600 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 2601 | TestLoadTimingInfoConnectedNotReused(handle); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2602 | handle.Reset(); |
| 2603 | |
| 2604 | // Should now have one idle socket. |
| 2605 | ASSERT_EQ(1, pool_->IdleSocketCount()); |
| 2606 | |
| 2607 | // Request a new socket. This should reuse the old socket and complete |
| 2608 | // synchronously. |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 2609 | RecordingBoundTestNetLog log; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2610 | rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2611 | TestGroupId("a"), params_, base::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2612 | ClientSocketPool::RespectLimits::ENABLED, CompletionOnceCallback(), |
| 2613 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
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 | |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 2622 | auto entries = log.GetEntries(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2623 | EXPECT_TRUE(LogContainsEvent( |
| 2624 | entries, 0, NetLogEventType::TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET, |
| 2625 | NetLogEventPhase::NONE)); |
| 2626 | EXPECT_TRUE(LogContainsBeginEvent(entries, 1, NetLogEventType::SOCKET_POOL)); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2627 | EXPECT_TRUE(LogContainsEntryWithType( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2628 | entries, 2, NetLogEventType::SOCKET_POOL_REUSED_AN_EXISTING_SOCKET)); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2629 | } |
| 2630 | |
martijn | 003cd61 | 2016-05-19 22:24:38 | [diff] [blame] | 2631 | // Make sure we cleanup old unused sockets. |
Eric Roman | b49715e | 2018-04-24 22:41:17 | [diff] [blame] | 2632 | TEST_F(ClientSocketPoolBaseTest, CleanupTimedOutIdleSocketsNoReuse) { |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2633 | CreatePoolWithIdleTimeouts( |
| 2634 | kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 2635 | base::TimeDelta(), // Time out unused sockets immediately |
| 2636 | base::TimeDelta()); // Time out used sockets immediately |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2637 | |
| 2638 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2639 | |
| 2640 | // Startup two mock pending connect jobs, which will sit in the MessageLoop. |
| 2641 | |
| 2642 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2643 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2644 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2645 | TestGroupId("a"), params_, base::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2646 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2647 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2648 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2649 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2650 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2651 | |
| 2652 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2653 | TestCompletionCallback callback2; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2654 | rv = handle2.Init(TestGroupId("a"), params_, base::nullopt, LOWEST, |
| 2655 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2656 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2657 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2658 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2659 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2660 | pool_->GetLoadState(TestGroupId("a"), &handle2)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2661 | |
| 2662 | // Cancel one of the requests. Wait for the other, which will get the first |
| 2663 | // job. Release the socket. Run the loop again to make sure the second |
| 2664 | // socket is sitting idle and the first one is released (since ReleaseSocket() |
| 2665 | // just posts a DoReleaseSocket() task). |
| 2666 | |
| 2667 | handle.Reset(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2668 | ASSERT_THAT(callback2.WaitForResult(), IsOk()); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 2669 | // Get the NetLogSource for the socket, so the time out reason can be checked |
| 2670 | // at the end of the test. |
| 2671 | NetLogSource net_log_source2 = handle2.socket()->NetLog().source(); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2672 | // Use the socket. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 2673 | EXPECT_EQ(1, handle2.socket()->Write(nullptr, 1, CompletionOnceCallback(), |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 2674 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2675 | handle2.Reset(); |
| 2676 | |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2677 | // We post all of our delayed tasks with a 2ms delay. I.e. they don't |
| 2678 | // actually become pending until 2ms after they have been created. In order |
| 2679 | // to flush all tasks, we need to wait so that we know there are no |
| 2680 | // soon-to-be-pending tasks waiting. |
Alex Clarke | 0def209 | 2018-12-10 12:01:45 | [diff] [blame] | 2681 | FastForwardBy(base::TimeDelta::FromMilliseconds(10)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2682 | |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2683 | // Both sockets should now be idle. |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2684 | ASSERT_EQ(2, pool_->IdleSocketCount()); |
| 2685 | |
| 2686 | // Request a new socket. This should cleanup the unused and timed out ones. |
| 2687 | // A new socket will be created rather than reusing the idle one. |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 2688 | RecordingBoundTestNetLog log; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2689 | TestCompletionCallback callback3; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2690 | rv = handle.Init(TestGroupId("a"), params_, base::nullopt, LOWEST, |
| 2691 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2692 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2693 | pool_.get(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2694 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 2695 | ASSERT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2696 | EXPECT_FALSE(handle.is_reused()); |
| 2697 | |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2698 | // Make sure the idle socket is closed. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2699 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2700 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2701 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2702 | |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 2703 | auto entries = log.GetEntries(); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2704 | EXPECT_FALSE(LogContainsEntryWithType( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 2705 | entries, 1, NetLogEventType::SOCKET_POOL_REUSED_AN_EXISTING_SOCKET)); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 2706 | ExpectSocketClosedWithReason( |
| 2707 | net_log_source2, TransportClientSocketPool::kIdleTimeLimitExpired); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2708 | } |
| 2709 | |
[email protected] | 2041cf34 | 2010-02-19 03:15:59 | [diff] [blame] | 2710 | // Make sure that we process all pending requests even when we're stalling |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2711 | // because of multiple releasing disconnected sockets. |
| 2712 | TEST_F(ClientSocketPoolBaseTest, MultipleReleasingDisconnectedSockets) { |
| 2713 | CreatePoolWithIdleTimeouts( |
| 2714 | kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 2715 | base::TimeDelta(), // Time out unused sockets immediately. |
| 2716 | base::TimeDelta::FromDays(1)); // Don't time out used sockets. |
| 2717 | |
| 2718 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 2719 | |
| 2720 | // Startup 4 connect jobs. Two of them will be pending. |
| 2721 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2722 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2723 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2724 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2725 | TestGroupId("a"), params_, base::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2726 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2727 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2728 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2729 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2730 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2731 | TestCompletionCallback callback2; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2732 | rv = handle2.Init(TestGroupId("a"), params_, base::nullopt, LOWEST, |
| 2733 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2734 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2735 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2736 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2737 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2738 | ClientSocketHandle handle3; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2739 | TestCompletionCallback callback3; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2740 | rv = handle3.Init(TestGroupId("a"), params_, base::nullopt, LOWEST, |
| 2741 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2742 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2743 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2744 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2745 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2746 | ClientSocketHandle handle4; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2747 | TestCompletionCallback callback4; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2748 | rv = handle4.Init(TestGroupId("a"), params_, base::nullopt, LOWEST, |
| 2749 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2750 | callback4.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2751 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2752 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2753 | |
| 2754 | // Release two disconnected sockets. |
| 2755 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2756 | handle.socket()->Disconnect(); |
| 2757 | handle.Reset(); |
| 2758 | handle2.socket()->Disconnect(); |
| 2759 | handle2.Reset(); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2760 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2761 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2762 | EXPECT_FALSE(handle3.is_reused()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2763 | EXPECT_THAT(callback4.WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2764 | EXPECT_FALSE(handle4.is_reused()); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2765 | } |
| 2766 | |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2767 | // Regression test for http://crbug.com/42267. |
| 2768 | // When DoReleaseSocket() is processed for one socket, it is blocked because the |
| 2769 | // other stalled groups all have releasing sockets, so no progress can be made. |
| 2770 | TEST_F(ClientSocketPoolBaseTest, SocketLimitReleasingSockets) { |
| 2771 | CreatePoolWithIdleTimeouts( |
| 2772 | 4 /* socket limit */, 4 /* socket limit per group */, |
| 2773 | base::TimeDelta(), // Time out unused sockets immediately. |
| 2774 | base::TimeDelta::FromDays(1)); // Don't time out used sockets. |
| 2775 | |
| 2776 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 2777 | |
| 2778 | // Max out the socket limit with 2 per group. |
| 2779 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2780 | ClientSocketHandle handle_a[4]; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2781 | TestCompletionCallback callback_a[4]; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2782 | ClientSocketHandle handle_b[4]; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2783 | TestCompletionCallback callback_b[4]; |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2784 | |
| 2785 | for (int i = 0; i < 2; ++i) { |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2786 | EXPECT_EQ(OK, handle_a[i].Init(TestGroupId("a"), params_, base::nullopt, |
| 2787 | LOWEST, SocketTag(), |
| 2788 | ClientSocketPool::RespectLimits::ENABLED, |
| 2789 | callback_a[i].callback(), |
| 2790 | ClientSocketPool::ProxyAuthCallback(), |
| 2791 | pool_.get(), NetLogWithSource())); |
| 2792 | EXPECT_EQ(OK, handle_b[i].Init(TestGroupId("b"), params_, base::nullopt, |
| 2793 | LOWEST, SocketTag(), |
| 2794 | ClientSocketPool::RespectLimits::ENABLED, |
| 2795 | callback_b[i].callback(), |
| 2796 | ClientSocketPool::ProxyAuthCallback(), |
| 2797 | pool_.get(), NetLogWithSource())); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2798 | } |
[email protected] | b89f7e4 | 2010-05-20 20:37:00 | [diff] [blame] | 2799 | |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2800 | // Make 4 pending requests, 2 per group. |
| 2801 | |
| 2802 | for (int i = 2; i < 4; ++i) { |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2803 | EXPECT_EQ( |
| 2804 | ERR_IO_PENDING, |
| 2805 | handle_a[i].Init(TestGroupId("a"), params_, base::nullopt, LOWEST, |
| 2806 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2807 | callback_a[i].callback(), |
| 2808 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2809 | NetLogWithSource())); |
| 2810 | EXPECT_EQ( |
| 2811 | ERR_IO_PENDING, |
| 2812 | handle_b[i].Init(TestGroupId("b"), params_, base::nullopt, LOWEST, |
| 2813 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2814 | callback_b[i].callback(), |
| 2815 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2816 | NetLogWithSource())); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2817 | } |
| 2818 | |
| 2819 | // Release b's socket first. The order is important, because in |
| 2820 | // DoReleaseSocket(), we'll process b's released socket, and since both b and |
| 2821 | // a are stalled, but 'a' is lower lexicographically, we'll process group 'a' |
| 2822 | // first, which has a releasing socket, so it refuses to start up another |
| 2823 | // ConnectJob. So, we used to infinite loop on this. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2824 | handle_b[0].socket()->Disconnect(); |
| 2825 | handle_b[0].Reset(); |
| 2826 | handle_a[0].socket()->Disconnect(); |
| 2827 | handle_a[0].Reset(); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2828 | |
| 2829 | // Used to get stuck here. |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 2830 | base::RunLoop().RunUntilIdle(); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2831 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2832 | handle_b[1].socket()->Disconnect(); |
| 2833 | handle_b[1].Reset(); |
| 2834 | handle_a[1].socket()->Disconnect(); |
| 2835 | handle_a[1].Reset(); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2836 | |
| 2837 | for (int i = 2; i < 4; ++i) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2838 | EXPECT_THAT(callback_b[i].WaitForResult(), IsOk()); |
| 2839 | EXPECT_THAT(callback_a[i].WaitForResult(), IsOk()); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2840 | } |
| 2841 | } |
| 2842 | |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2843 | TEST_F(ClientSocketPoolBaseTest, |
| 2844 | ReleasingDisconnectedSocketsMaintainsPriorityOrder) { |
| 2845 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2846 | |
| 2847 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2848 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2849 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2850 | IsError(ERR_IO_PENDING)); |
| 2851 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2852 | IsError(ERR_IO_PENDING)); |
| 2853 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2854 | IsError(ERR_IO_PENDING)); |
| 2855 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2856 | IsError(ERR_IO_PENDING)); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2857 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2858 | EXPECT_THAT((*requests())[0]->WaitForResult(), IsOk()); |
| 2859 | EXPECT_THAT((*requests())[1]->WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2860 | EXPECT_EQ(2u, completion_count()); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2861 | |
| 2862 | // Releases one connection. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2863 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::NO_KEEP_ALIVE)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2864 | EXPECT_THAT((*requests())[2]->WaitForResult(), IsOk()); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2865 | |
[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())[3]->WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2868 | EXPECT_EQ(4u, completion_count()); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2869 | |
| 2870 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 2871 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 2872 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 2873 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 2874 | |
| 2875 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2876 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(5)); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2877 | } |
| 2878 | |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2879 | class TestReleasingSocketRequest : public TestCompletionCallbackBase { |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2880 | public: |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2881 | TestReleasingSocketRequest(TransportClientSocketPool* pool, |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2882 | int expected_result, |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2883 | bool reset_releasing_handle) |
| 2884 | : pool_(pool), |
| 2885 | expected_result_(expected_result), |
Bence Béky | 8ddc249 | 2018-06-13 01:02:04 | [diff] [blame] | 2886 | reset_releasing_handle_(reset_releasing_handle) {} |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2887 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 2888 | ~TestReleasingSocketRequest() override = default; |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2889 | |
| 2890 | ClientSocketHandle* handle() { return &handle_; } |
| 2891 | |
Bence Béky | 8ddc249 | 2018-06-13 01:02:04 | [diff] [blame] | 2892 | CompletionOnceCallback callback() { |
| 2893 | return base::BindOnce(&TestReleasingSocketRequest::OnComplete, |
| 2894 | base::Unretained(this)); |
| 2895 | } |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2896 | |
| 2897 | private: |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2898 | void OnComplete(int result) { |
| 2899 | SetResult(result); |
| 2900 | if (reset_releasing_handle_) |
| 2901 | handle_.Reset(); |
| 2902 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2903 | EXPECT_EQ( |
| 2904 | expected_result_, |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 2905 | handle2_.Init( |
| 2906 | TestGroupId("a"), |
| 2907 | ClientSocketPool::SocketParams::CreateForHttpForTesting(), |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2908 | base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 2909 | ClientSocketPool::RespectLimits::ENABLED, CompletionOnceCallback(), |
| 2910 | ClientSocketPool::ProxyAuthCallback(), pool_, NetLogWithSource())); |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2911 | } |
| 2912 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2913 | TransportClientSocketPool* const pool_; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2914 | int expected_result_; |
| 2915 | bool reset_releasing_handle_; |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2916 | ClientSocketHandle handle_; |
| 2917 | ClientSocketHandle handle2_; |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2918 | }; |
| 2919 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2920 | |
| 2921 | TEST_F(ClientSocketPoolBaseTest, AdditionalErrorSocketsDontUseSlot) { |
| 2922 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2923 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2924 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
| 2925 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 2926 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2927 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2928 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2929 | client_socket_factory_.allocation_count()); |
| 2930 | |
| 2931 | connect_job_factory_->set_job_type( |
| 2932 | TestConnectJob::kMockPendingAdditionalErrorStateJob); |
| 2933 | TestReleasingSocketRequest req(pool_.get(), OK, false); |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2934 | EXPECT_EQ(ERR_IO_PENDING, |
| 2935 | req.handle()->Init( |
| 2936 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2937 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2938 | req.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2939 | pool_.get(), NetLogWithSource())); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2940 | // The next job should complete synchronously |
| 2941 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 2942 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2943 | EXPECT_THAT(req.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2944 | EXPECT_FALSE(req.handle()->is_initialized()); |
| 2945 | EXPECT_FALSE(req.handle()->socket()); |
| 2946 | EXPECT_TRUE(req.handle()->is_ssl_error()); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 2947 | EXPECT_TRUE(req.handle()->ssl_cert_request_info()); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2948 | } |
| 2949 | |
[email protected] | b6501d3d | 2010-06-03 23:53:34 | [diff] [blame] | 2950 | // http://crbug.com/44724 regression test. |
| 2951 | // We start releasing the pool when we flush on network change. When that |
| 2952 | // happens, the only active references are in the ClientSocketHandles. When a |
| 2953 | // ConnectJob completes and calls back into the last ClientSocketHandle, that |
| 2954 | // callback can release the last reference and delete the pool. After the |
| 2955 | // callback finishes, we go back to the stack frame within the now-deleted pool. |
| 2956 | // Executing any code that refers to members of the now-deleted pool can cause |
| 2957 | // crashes. |
| 2958 | TEST_F(ClientSocketPoolBaseTest, CallbackThatReleasesPool) { |
| 2959 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2960 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
| 2961 | |
| 2962 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2963 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2964 | EXPECT_EQ( |
| 2965 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2966 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2967 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2968 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2969 | pool_.get(), NetLogWithSource())); |
[email protected] | b6501d3d | 2010-06-03 23:53:34 | [diff] [blame] | 2970 | |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 2971 | pool_->FlushWithError(ERR_NETWORK_CHANGED, "Network changed"); |
[email protected] | b6501d3d | 2010-06-03 23:53:34 | [diff] [blame] | 2972 | |
| 2973 | // We'll call back into this now. |
| 2974 | callback.WaitForResult(); |
| 2975 | } |
| 2976 | |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2977 | TEST_F(ClientSocketPoolBaseTest, DoNotReuseSocketAfterFlush) { |
| 2978 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2979 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2980 | |
| 2981 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2982 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2983 | EXPECT_EQ( |
| 2984 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2985 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2986 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2987 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2988 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2989 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2990 | EXPECT_EQ(ClientSocketHandle::UNUSED, handle.reuse_type()); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 2991 | NetLogSource source = handle.socket()->NetLog().source(); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2992 | |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 2993 | pool_->FlushWithError(ERR_NETWORK_CHANGED, "Network changed"); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2994 | |
| 2995 | handle.Reset(); |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 2996 | base::RunLoop().RunUntilIdle(); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2997 | |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2998 | EXPECT_EQ( |
| 2999 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3000 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3001 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 3002 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3003 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3004 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 3005 | EXPECT_EQ(ClientSocketHandle::UNUSED, handle.reuse_type()); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 3006 | |
| 3007 | ExpectSocketClosedWithReason( |
| 3008 | source, TransportClientSocketPool::kSocketGenerationOutOfDate); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 3009 | } |
| 3010 | |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3011 | class ConnectWithinCallback : public TestCompletionCallbackBase { |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 3012 | public: |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3013 | ConnectWithinCallback( |
| 3014 | const ClientSocketPool::GroupId& group_id, |
Matt Menke | 84d11e56 | 2019-03-27 00:11:19 | [diff] [blame] | 3015 | const scoped_refptr<ClientSocketPool::SocketParams>& params, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3016 | TransportClientSocketPool* pool) |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3017 | : group_id_(group_id), params_(params), pool_(pool) {} |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 3018 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 3019 | ~ConnectWithinCallback() override = default; |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 3020 | |
| 3021 | int WaitForNestedResult() { |
| 3022 | return nested_callback_.WaitForResult(); |
| 3023 | } |
| 3024 | |
Bence Béky | 8ddc249 | 2018-06-13 01:02:04 | [diff] [blame] | 3025 | CompletionOnceCallback callback() { |
| 3026 | return base::BindOnce(&ConnectWithinCallback::OnComplete, |
| 3027 | base::Unretained(this)); |
| 3028 | } |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3029 | |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 3030 | private: |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3031 | void OnComplete(int result) { |
| 3032 | SetResult(result); |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3033 | EXPECT_EQ( |
| 3034 | ERR_IO_PENDING, |
| 3035 | handle_.Init(group_id_, params_, base::nullopt, DEFAULT_PRIORITY, |
| 3036 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 3037 | nested_callback_.callback(), |
| 3038 | ClientSocketPool::ProxyAuthCallback(), pool_, |
| 3039 | NetLogWithSource())); |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3040 | } |
| 3041 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3042 | const ClientSocketPool::GroupId group_id_; |
Matt Menke | 84d11e56 | 2019-03-27 00:11:19 | [diff] [blame] | 3043 | const scoped_refptr<ClientSocketPool::SocketParams> params_; |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3044 | TransportClientSocketPool* const pool_; |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 3045 | ClientSocketHandle handle_; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3046 | TestCompletionCallback nested_callback_; |
| 3047 | |
| 3048 | DISALLOW_COPY_AND_ASSIGN(ConnectWithinCallback); |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 3049 | }; |
| 3050 | |
| 3051 | TEST_F(ClientSocketPoolBaseTest, AbortAllRequestsOnFlush) { |
| 3052 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3053 | |
| 3054 | // First job will be waiting until it gets aborted. |
| 3055 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3056 | |
| 3057 | ClientSocketHandle handle; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3058 | ConnectWithinCallback callback(TestGroupId("a"), params_, pool_.get()); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3059 | EXPECT_EQ( |
| 3060 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3061 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3062 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 3063 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3064 | pool_.get(), NetLogWithSource())); |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 3065 | |
| 3066 | // Second job will be started during the first callback, and will |
| 3067 | // asynchronously complete with OK. |
| 3068 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 3069 | pool_->FlushWithError(ERR_NETWORK_CHANGED, "Network changed"); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3070 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_NETWORK_CHANGED)); |
| 3071 | EXPECT_THAT(callback.WaitForNestedResult(), IsOk()); |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 3072 | } |
| 3073 | |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 3074 | TEST_F(ClientSocketPoolBaseTest, BackupSocketWaitsForHostResolution) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3075 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets, |
| 3076 | true /* enable_backup_connect_jobs */); |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 3077 | |
| 3078 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3079 | ClientSocketHandle handle; |
| 3080 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3081 | EXPECT_EQ( |
| 3082 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3083 | handle.Init(TestGroupId("bar"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3084 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 3085 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3086 | pool_.get(), NetLogWithSource())); |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 3087 | // The backup timer fires but doesn't start a new ConnectJob while resolving |
| 3088 | // the hostname. |
| 3089 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_RESOLVING_HOST); |
| 3090 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
| 3091 | ClientSocketPool::kMaxConnectRetryIntervalMs * 100)); |
| 3092 | EXPECT_EQ(1, client_socket_factory_.allocation_count()); |
| 3093 | |
| 3094 | // Once the ConnectJob has finished resolving the hostname, the backup timer |
| 3095 | // will create a ConnectJob when it fires. |
| 3096 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_CONNECTING); |
| 3097 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
| 3098 | ClientSocketPool::kMaxConnectRetryIntervalMs)); |
| 3099 | EXPECT_EQ(2, client_socket_factory_.allocation_count()); |
| 3100 | } |
| 3101 | |
| 3102 | // Test that no backup socket is created when a ConnectJob connects before it |
| 3103 | // completes. |
| 3104 | TEST_F(ClientSocketPoolBaseTest, NoBackupSocketWhenConnected) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3105 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets, |
| 3106 | true /* enable_backup_connect_jobs */); |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 3107 | |
| 3108 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3109 | ClientSocketHandle handle; |
| 3110 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3111 | EXPECT_EQ( |
| 3112 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3113 | handle.Init(TestGroupId("bar"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3114 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 3115 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3116 | pool_.get(), NetLogWithSource())); |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 3117 | // The backup timer fires but doesn't start a new ConnectJob while resolving |
| 3118 | // the hostname. |
| 3119 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_RESOLVING_HOST); |
| 3120 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
| 3121 | ClientSocketPool::kMaxConnectRetryIntervalMs * 100)); |
| 3122 | EXPECT_EQ(1, client_socket_factory_.allocation_count()); |
| 3123 | |
| 3124 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE); |
| 3125 | client_socket_factory_.SetJobHasEstablishedConnection(0); |
| 3126 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
| 3127 | ClientSocketPool::kMaxConnectRetryIntervalMs * 100)); |
| 3128 | EXPECT_EQ(1, client_socket_factory_.allocation_count()); |
| 3129 | } |
| 3130 | |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3131 | // Cancel a pending socket request while we're at max sockets, |
| 3132 | // and verify that the backup socket firing doesn't cause a crash. |
| 3133 | TEST_F(ClientSocketPoolBaseTest, BackupSocketCancelAtMaxSockets) { |
| 3134 | // Max 4 sockets globally, max 4 sockets per group. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3135 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets, |
| 3136 | true /* enable_backup_connect_jobs */); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3137 | |
[email protected] | 4baaf9d | 2010-08-31 15:15:44 | [diff] [blame] | 3138 | // Create the first socket and set to ERR_IO_PENDING. This starts the backup |
| 3139 | // timer. |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3140 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3141 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3142 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3143 | EXPECT_EQ( |
| 3144 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3145 | handle.Init(TestGroupId("bar"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3146 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 3147 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3148 | pool_.get(), NetLogWithSource())); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3149 | |
| 3150 | // Start (MaxSockets - 1) connected sockets to reach max sockets. |
| 3151 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 3152 | ClientSocketHandle handles[kDefaultMaxSockets]; |
| 3153 | for (int i = 1; i < kDefaultMaxSockets; ++i) { |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3154 | TestCompletionCallback callback; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3155 | EXPECT_EQ(OK, handles[i].Init(TestGroupId("bar"), params_, base::nullopt, |
| 3156 | DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3157 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3158 | callback.callback(), |
| 3159 | ClientSocketPool::ProxyAuthCallback(), |
| 3160 | pool_.get(), NetLogWithSource())); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3161 | } |
| 3162 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3163 | base::RunLoop().RunUntilIdle(); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3164 | |
| 3165 | // Cancel the pending request. |
| 3166 | handle.Reset(); |
| 3167 | |
| 3168 | // Wait for the backup timer to fire (add some slop to ensure it fires) |
Alex Clarke | 0def209 | 2018-12-10 12:01:45 | [diff] [blame] | 3169 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
[email protected] | 26b997396 | 2012-01-28 00:57:00 | [diff] [blame] | 3170 | ClientSocketPool::kMaxConnectRetryIntervalMs / 2 * 3)); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3171 | |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3172 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 3173 | } |
| 3174 | |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3175 | TEST_F(ClientSocketPoolBaseTest, CancelBackupSocketAfterCancelingAllRequests) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3176 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets, |
| 3177 | true /* enable_backup_connect_jobs */); |
[email protected] | 4baaf9d | 2010-08-31 15:15:44 | [diff] [blame] | 3178 | |
| 3179 | // Create the first socket and set to ERR_IO_PENDING. This starts the backup |
| 3180 | // timer. |
| 3181 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3182 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3183 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3184 | EXPECT_EQ( |
| 3185 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3186 | handle.Init(TestGroupId("bar"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3187 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 3188 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3189 | pool_.get(), NetLogWithSource())); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3190 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("bar"))); |
| 3191 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("bar"))); |
| 3192 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3193 | TestGroupId("bar"))); |
| 3194 | EXPECT_EQ( |
| 3195 | 0u, pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("bar"))); |
[email protected] | 4baaf9d | 2010-08-31 15:15:44 | [diff] [blame] | 3196 | |
| 3197 | // Cancel the socket request. This should cancel the backup timer. Wait for |
| 3198 | // the backup time to see if it indeed got canceled. |
| 3199 | handle.Reset(); |
| 3200 | // Wait for the backup timer to fire (add some slop to ensure it fires) |
Alex Clarke | 0def209 | 2018-12-10 12:01:45 | [diff] [blame] | 3201 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
[email protected] | 26b997396 | 2012-01-28 00:57:00 | [diff] [blame] | 3202 | ClientSocketPool::kMaxConnectRetryIntervalMs / 2 * 3)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3203 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("bar"))); |
| 3204 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("bar"))); |
[email protected] | 4baaf9d | 2010-08-31 15:15:44 | [diff] [blame] | 3205 | } |
| 3206 | |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3207 | TEST_F(ClientSocketPoolBaseTest, CancelBackupSocketAfterFinishingAllRequests) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3208 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets, |
| 3209 | true /* enable_backup_connect_jobs */); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3210 | |
| 3211 | // Create the first socket and set to ERR_IO_PENDING. This starts the backup |
| 3212 | // timer. |
| 3213 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3214 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3215 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3216 | EXPECT_EQ( |
| 3217 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3218 | handle.Init(TestGroupId("bar"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3219 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 3220 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3221 | pool_.get(), NetLogWithSource())); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3222 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3223 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3224 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3225 | EXPECT_EQ( |
| 3226 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3227 | handle2.Init(TestGroupId("bar"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3228 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3229 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3230 | pool_.get(), NetLogWithSource())); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3231 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("bar"))); |
| 3232 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("bar"))); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3233 | |
| 3234 | // Cancel request 1 and then complete request 2. With the requests finished, |
| 3235 | // the backup timer should be cancelled. |
| 3236 | handle.Reset(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3237 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3238 | // Wait for the backup timer to fire (add some slop to ensure it fires) |
Alex Clarke | 0def209 | 2018-12-10 12:01:45 | [diff] [blame] | 3239 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
[email protected] | 26b997396 | 2012-01-28 00:57:00 | [diff] [blame] | 3240 | ClientSocketPool::kMaxConnectRetryIntervalMs / 2 * 3)); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3241 | } |
| 3242 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3243 | // Test delayed socket binding for the case where we have two connects, |
| 3244 | // and while one is waiting on a connect, the other frees up. |
| 3245 | // The socket waiting on a connect should switch immediately to the freed |
| 3246 | // up socket. |
| 3247 | TEST_F(ClientSocketPoolBaseTest, DelayedSocketBindingWaitingForConnect) { |
| 3248 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3249 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3250 | |
| 3251 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3252 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3253 | EXPECT_EQ( |
| 3254 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3255 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3256 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3257 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3258 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3259 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3260 | |
| 3261 | // No idle sockets, no pending jobs. |
| 3262 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3263 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3264 | |
| 3265 | // Create a second socket to the same host, but this one will wait. |
| 3266 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3267 | ClientSocketHandle handle2; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3268 | EXPECT_EQ( |
| 3269 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3270 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3271 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3272 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3273 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3274 | // No idle sockets, and one connecting job. |
| 3275 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3276 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3277 | |
| 3278 | // Return the first handle to the pool. This will initiate the delayed |
| 3279 | // binding. |
| 3280 | handle1.Reset(); |
| 3281 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3282 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3283 | |
| 3284 | // Still no idle sockets, still one pending connect job. |
| 3285 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3286 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3287 | |
| 3288 | // The second socket connected, even though it was a Waiting Job. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3289 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3290 | |
| 3291 | // And we can see there is still one job waiting. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3292 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3293 | |
| 3294 | // Finally, signal the waiting Connect. |
| 3295 | client_socket_factory_.SignalJobs(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3296 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3297 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3298 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3299 | } |
| 3300 | |
| 3301 | // Test delayed socket binding when a group is at capacity and one |
| 3302 | // of the group's sockets frees up. |
| 3303 | TEST_F(ClientSocketPoolBaseTest, DelayedSocketBindingAtGroupCapacity) { |
| 3304 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3305 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3306 | |
| 3307 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3308 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3309 | EXPECT_EQ( |
| 3310 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3311 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3312 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3313 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3314 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3315 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3316 | |
| 3317 | // No idle sockets, no pending jobs. |
| 3318 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3319 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3320 | |
| 3321 | // Create a second socket to the same host, but this one will wait. |
| 3322 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3323 | ClientSocketHandle handle2; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3324 | EXPECT_EQ( |
| 3325 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3326 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3327 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3328 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3329 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3330 | // No idle sockets, and one connecting job. |
| 3331 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3332 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3333 | |
| 3334 | // Return the first handle to the pool. This will initiate the delayed |
| 3335 | // binding. |
| 3336 | handle1.Reset(); |
| 3337 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3338 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3339 | |
| 3340 | // Still no idle sockets, still one pending connect job. |
| 3341 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3342 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3343 | |
| 3344 | // The second socket connected, even though it was a Waiting Job. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3345 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3346 | |
| 3347 | // And we can see there is still one job waiting. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3348 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3349 | |
| 3350 | // Finally, signal the waiting Connect. |
| 3351 | client_socket_factory_.SignalJobs(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3352 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3353 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3354 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3355 | } |
| 3356 | |
| 3357 | // Test out the case where we have one socket connected, one |
| 3358 | // connecting, when the first socket finishes and goes idle. |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3359 | // Although the second connection is pending, the second request |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3360 | // should complete, by taking the first socket's idle socket. |
| 3361 | TEST_F(ClientSocketPoolBaseTest, DelayedSocketBindingAtStall) { |
| 3362 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3363 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3364 | |
| 3365 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3366 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3367 | EXPECT_EQ( |
| 3368 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3369 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3370 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3371 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3372 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3373 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3374 | |
| 3375 | // No idle sockets, no pending jobs. |
| 3376 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3377 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3378 | |
| 3379 | // Create a second socket to the same host, but this one will wait. |
| 3380 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3381 | ClientSocketHandle handle2; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3382 | EXPECT_EQ( |
| 3383 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3384 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3385 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3386 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3387 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3388 | // No idle sockets, and one connecting job. |
| 3389 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3390 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3391 | |
| 3392 | // Return the first handle to the pool. This will initiate the delayed |
| 3393 | // binding. |
| 3394 | handle1.Reset(); |
| 3395 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3396 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3397 | |
| 3398 | // Still no idle sockets, still one pending connect job. |
| 3399 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3400 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3401 | |
| 3402 | // The second socket connected, even though it was a Waiting Job. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3403 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3404 | |
| 3405 | // And we can see there is still one job waiting. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3406 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3407 | |
| 3408 | // Finally, signal the waiting Connect. |
| 3409 | client_socket_factory_.SignalJobs(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3410 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3411 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3412 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3413 | } |
| 3414 | |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3415 | // Cover the case where on an available socket slot, we have one pending |
| 3416 | // request that completes synchronously, thereby making the Group empty. |
| 3417 | TEST_F(ClientSocketPoolBaseTest, SynchronouslyProcessOnePendingRequest) { |
| 3418 | const int kUnlimitedSockets = 100; |
| 3419 | const int kOneSocketPerGroup = 1; |
| 3420 | CreatePool(kUnlimitedSockets, kOneSocketPerGroup); |
| 3421 | |
| 3422 | // Make the first request asynchronous fail. |
| 3423 | // This will free up a socket slot later. |
| 3424 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
| 3425 | |
| 3426 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3427 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3428 | EXPECT_EQ( |
| 3429 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3430 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3431 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3432 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3433 | pool_.get(), NetLogWithSource())); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3434 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3435 | |
| 3436 | // Make the second request synchronously fail. This should make the Group |
| 3437 | // empty. |
| 3438 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
| 3439 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3440 | TestCompletionCallback callback2; |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3441 | // It'll be ERR_IO_PENDING now, but the TestConnectJob will synchronously fail |
| 3442 | // when created. |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3443 | EXPECT_EQ( |
| 3444 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3445 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3446 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3447 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3448 | pool_.get(), NetLogWithSource())); |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3449 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3450 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3451 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3452 | EXPECT_THAT(callback1.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 3453 | EXPECT_THAT(callback2.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3454 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3455 | } |
| 3456 | |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3457 | TEST_F(ClientSocketPoolBaseTest, PreferUsedSocketToUnusedSocket) { |
| 3458 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 3459 | |
| 3460 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3461 | |
| 3462 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3463 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3464 | EXPECT_EQ( |
| 3465 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3466 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3467 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3468 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3469 | pool_.get(), NetLogWithSource())); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3470 | |
| 3471 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3472 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3473 | EXPECT_EQ( |
| 3474 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3475 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3476 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3477 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3478 | pool_.get(), NetLogWithSource())); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3479 | ClientSocketHandle handle3; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3480 | TestCompletionCallback callback3; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3481 | EXPECT_EQ( |
| 3482 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3483 | handle3.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3484 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3485 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3486 | pool_.get(), NetLogWithSource())); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3487 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3488 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3489 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
| 3490 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3491 | |
| 3492 | // Use the socket. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 3493 | EXPECT_EQ(1, handle1.socket()->Write(nullptr, 1, CompletionOnceCallback(), |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 3494 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 3495 | EXPECT_EQ(1, handle3.socket()->Write(nullptr, 1, CompletionOnceCallback(), |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 3496 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3497 | |
| 3498 | handle1.Reset(); |
| 3499 | handle2.Reset(); |
| 3500 | handle3.Reset(); |
| 3501 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3502 | EXPECT_EQ(OK, handle1.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3503 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3504 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3505 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3506 | pool_.get(), NetLogWithSource())); |
| 3507 | EXPECT_EQ(OK, handle2.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3508 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3509 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3510 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3511 | pool_.get(), NetLogWithSource())); |
| 3512 | EXPECT_EQ(OK, handle3.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3513 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3514 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3515 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3516 | pool_.get(), NetLogWithSource())); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3517 | |
| 3518 | EXPECT_TRUE(handle1.socket()->WasEverUsed()); |
| 3519 | EXPECT_TRUE(handle2.socket()->WasEverUsed()); |
| 3520 | EXPECT_FALSE(handle3.socket()->WasEverUsed()); |
| 3521 | } |
| 3522 | |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3523 | TEST_F(ClientSocketPoolBaseTest, RequestSockets) { |
| 3524 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3525 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3526 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3527 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 3528 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3529 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3530 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3531 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3532 | EXPECT_EQ(2u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3533 | TestGroupId("a"))); |
| 3534 | EXPECT_EQ(2u, |
| 3535 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3536 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3537 | |
| 3538 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3539 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3540 | EXPECT_EQ( |
| 3541 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3542 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3543 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3544 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3545 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3546 | |
| 3547 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3548 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3549 | EXPECT_EQ( |
| 3550 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3551 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3552 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3553 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3554 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3555 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3556 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3557 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3558 | TestGroupId("a"))); |
| 3559 | EXPECT_EQ(0u, |
| 3560 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3561 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3562 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3563 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3564 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3565 | handle1.Reset(); |
| 3566 | handle2.Reset(); |
| 3567 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3568 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3569 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3570 | TestGroupId("a"))); |
| 3571 | EXPECT_EQ(0u, |
| 3572 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3573 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3574 | } |
| 3575 | |
| 3576 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsWhenAlreadyHaveAConnectJob) { |
| 3577 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3578 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3579 | |
| 3580 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3581 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3582 | EXPECT_EQ( |
| 3583 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3584 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3585 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3586 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3587 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3588 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3589 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3590 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3591 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3592 | TestGroupId("a"))); |
| 3593 | EXPECT_EQ(0u, |
| 3594 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3595 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3596 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3597 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 3598 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3599 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3600 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3601 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3602 | TestGroupId("a"))); |
| 3603 | EXPECT_EQ(1u, |
| 3604 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3605 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3606 | |
| 3607 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3608 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3609 | EXPECT_EQ( |
| 3610 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3611 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3612 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3613 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3614 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3615 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3616 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3617 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3618 | TestGroupId("a"))); |
| 3619 | EXPECT_EQ(0u, |
| 3620 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3621 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3622 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3623 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3624 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3625 | handle1.Reset(); |
| 3626 | handle2.Reset(); |
| 3627 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3628 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3629 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3630 | TestGroupId("a"))); |
| 3631 | EXPECT_EQ(0u, |
| 3632 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3633 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3634 | } |
| 3635 | |
| 3636 | TEST_F(ClientSocketPoolBaseTest, |
| 3637 | RequestSocketsWhenAlreadyHaveMultipleConnectJob) { |
| 3638 | CreatePool(4, 4); |
| 3639 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3640 | |
| 3641 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3642 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3643 | EXPECT_EQ( |
| 3644 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3645 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3646 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3647 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3648 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3649 | |
| 3650 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3651 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3652 | EXPECT_EQ( |
| 3653 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3654 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3655 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3656 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3657 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3658 | |
| 3659 | ClientSocketHandle handle3; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3660 | TestCompletionCallback callback3; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3661 | EXPECT_EQ( |
| 3662 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3663 | handle3.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3664 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3665 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3666 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3667 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3668 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3669 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3670 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3671 | TestGroupId("a"))); |
| 3672 | EXPECT_EQ(0u, |
| 3673 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3674 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3675 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3676 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 3677 | 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 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3680 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3681 | TestGroupId("a"))); |
| 3682 | EXPECT_EQ(0u, |
| 3683 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3684 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3685 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3686 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3687 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
| 3688 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3689 | handle1.Reset(); |
| 3690 | handle2.Reset(); |
| 3691 | handle3.Reset(); |
| 3692 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3693 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3694 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3695 | TestGroupId("a"))); |
| 3696 | EXPECT_EQ(0u, |
| 3697 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3698 | EXPECT_EQ(3u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3699 | } |
| 3700 | |
| 3701 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsAtMaxSocketLimit) { |
| 3702 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 3703 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3704 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3705 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3706 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3707 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, |
| 3708 | kDefaultMaxSockets, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3709 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3710 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3711 | EXPECT_EQ(kDefaultMaxSockets, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3712 | static_cast<int>( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3713 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
| 3714 | EXPECT_EQ( |
| 3715 | kDefaultMaxSockets, |
| 3716 | static_cast<int>(pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3717 | TestGroupId("a")))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3718 | EXPECT_EQ(kDefaultMaxSockets, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3719 | static_cast<int>(pool_->NumUnassignedConnectJobsInGroupForTesting( |
| 3720 | TestGroupId("a")))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3721 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3722 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("b"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3723 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3724 | pool_->RequestSockets(TestGroupId("b"), params_, base::nullopt, |
| 3725 | kDefaultMaxSockets, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3726 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3727 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("b"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3728 | } |
| 3729 | |
| 3730 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsHitMaxSocketLimit) { |
| 3731 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 3732 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3733 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3734 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3735 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3736 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, |
| 3737 | kDefaultMaxSockets - 1, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3738 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3739 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3740 | EXPECT_EQ(kDefaultMaxSockets - 1, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3741 | static_cast<int>( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3742 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
| 3743 | EXPECT_EQ( |
| 3744 | kDefaultMaxSockets - 1, |
| 3745 | static_cast<int>(pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3746 | TestGroupId("a")))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3747 | EXPECT_EQ(kDefaultMaxSockets - 1, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3748 | static_cast<int>(pool_->NumUnassignedConnectJobsInGroupForTesting( |
| 3749 | TestGroupId("a")))); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 3750 | EXPECT_FALSE(pool_->IsStalled()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3751 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3752 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("b"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3753 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3754 | pool_->RequestSockets(TestGroupId("b"), params_, base::nullopt, |
| 3755 | kDefaultMaxSockets, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3756 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3757 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("b"))); |
| 3758 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 3759 | EXPECT_FALSE(pool_->IsStalled()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3760 | } |
| 3761 | |
| 3762 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsCountIdleSockets) { |
| 3763 | CreatePool(4, 4); |
| 3764 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3765 | |
| 3766 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3767 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3768 | EXPECT_EQ( |
| 3769 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3770 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3771 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3772 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3773 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3774 | ASSERT_THAT(callback1.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3775 | handle1.Reset(); |
| 3776 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3777 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3778 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3779 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3780 | TestGroupId("a"))); |
| 3781 | EXPECT_EQ(0u, |
| 3782 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3783 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3784 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3785 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 3786 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3787 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3788 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3789 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3790 | TestGroupId("a"))); |
| 3791 | EXPECT_EQ(1u, |
| 3792 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3793 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3794 | } |
| 3795 | |
| 3796 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsCountActiveSockets) { |
| 3797 | CreatePool(4, 4); |
| 3798 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3799 | |
| 3800 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3801 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3802 | EXPECT_EQ( |
| 3803 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3804 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3805 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3806 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3807 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3808 | ASSERT_THAT(callback1.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3809 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3810 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3811 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3812 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3813 | TestGroupId("a"))); |
| 3814 | EXPECT_EQ(0u, |
| 3815 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3816 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3817 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3818 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3819 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 3820 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3821 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3822 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3823 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3824 | TestGroupId("a"))); |
| 3825 | EXPECT_EQ(1u, |
| 3826 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3827 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3828 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3829 | } |
| 3830 | |
| 3831 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsSynchronous) { |
| 3832 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3833 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 3834 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3835 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, |
| 3836 | kDefaultMaxSocketsPerGroup, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3837 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3838 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3839 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3840 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3841 | TestGroupId("a"))); |
| 3842 | EXPECT_EQ(0u, |
| 3843 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3844 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3845 | static_cast<int>(pool_->IdleSocketCountInGroup(TestGroupId("a")))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3846 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3847 | pool_->RequestSockets(TestGroupId("b"), params_, base::nullopt, |
| 3848 | kDefaultMaxSocketsPerGroup, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3849 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3850 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 3851 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3852 | TestGroupId("b"))); |
| 3853 | EXPECT_EQ(0u, |
| 3854 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3855 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3856 | static_cast<int>(pool_->IdleSocketCountInGroup(TestGroupId("b")))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3857 | } |
| 3858 | |
[email protected] | 3c819f52 | 2010-12-02 02:03:12 | [diff] [blame] | 3859 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsSynchronousError) { |
| 3860 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3861 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
| 3862 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3863 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, |
| 3864 | kDefaultMaxSocketsPerGroup, NetLogWithSource()); |
[email protected] | 3c819f52 | 2010-12-02 02:03:12 | [diff] [blame] | 3865 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3866 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | fd2e53e | 2011-01-14 20:40:52 | [diff] [blame] | 3867 | |
| 3868 | connect_job_factory_->set_job_type( |
| 3869 | TestConnectJob::kMockAdditionalErrorStateJob); |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3870 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, |
| 3871 | kDefaultMaxSocketsPerGroup, NetLogWithSource()); |
[email protected] | fd2e53e | 2011-01-14 20:40:52 | [diff] [blame] | 3872 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3873 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | 3c819f52 | 2010-12-02 02:03:12 | [diff] [blame] | 3874 | } |
| 3875 | |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3876 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsMultipleTimesDoesNothing) { |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3877 | CreatePool(4, 4); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3878 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3879 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3880 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 3881 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3882 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3883 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3884 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3885 | EXPECT_EQ(2u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3886 | TestGroupId("a"))); |
| 3887 | EXPECT_EQ(2u, |
| 3888 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3889 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3890 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3891 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3892 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 3893 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3894 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3895 | EXPECT_EQ(2u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3896 | TestGroupId("a"))); |
| 3897 | EXPECT_EQ(2u, |
| 3898 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3899 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3900 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3901 | |
| 3902 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3903 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3904 | EXPECT_EQ( |
| 3905 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3906 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3907 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3908 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3909 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3910 | |
| 3911 | client_socket_factory_.SignalJob(0); |
| 3912 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3913 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3914 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3915 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3916 | TestGroupId("a"))); |
| 3917 | EXPECT_EQ(1u, |
| 3918 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3919 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3920 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3921 | |
| 3922 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3923 | TestCompletionCallback callback2; |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3924 | EXPECT_EQ( |
| 3925 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3926 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3927 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3928 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3929 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3930 | client_socket_factory_.SignalJob(0); |
| 3931 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3932 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3933 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3934 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3935 | TestGroupId("a"))); |
| 3936 | EXPECT_EQ(0u, |
| 3937 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3938 | EXPECT_EQ(2, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3939 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3940 | |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3941 | handle1.Reset(); |
| 3942 | handle2.Reset(); |
| 3943 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3944 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3945 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3946 | TestGroupId("a"))); |
| 3947 | EXPECT_EQ(0u, |
| 3948 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3949 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3950 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3951 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3952 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 3953 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3954 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3955 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3956 | TestGroupId("a"))); |
| 3957 | EXPECT_EQ(0u, |
| 3958 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3959 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3960 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3961 | } |
| 3962 | |
| 3963 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsDifferentNumSockets) { |
| 3964 | CreatePool(4, 4); |
| 3965 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3966 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3967 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 1, |
| 3968 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3969 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3970 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3971 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3972 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3973 | TestGroupId("a"))); |
| 3974 | EXPECT_EQ(1u, |
| 3975 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3976 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3977 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3978 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 3979 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3980 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3981 | EXPECT_EQ(2u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3982 | TestGroupId("a"))); |
| 3983 | EXPECT_EQ(2u, |
| 3984 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3985 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3986 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3987 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 3, |
| 3988 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3989 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3990 | EXPECT_EQ(3u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3991 | TestGroupId("a"))); |
| 3992 | EXPECT_EQ(3u, |
| 3993 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3994 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3995 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3996 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 1, |
| 3997 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3998 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3999 | EXPECT_EQ(3u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4000 | TestGroupId("a"))); |
| 4001 | EXPECT_EQ(3u, |
| 4002 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4003 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4004 | } |
| 4005 | |
| 4006 | TEST_F(ClientSocketPoolBaseTest, PreconnectJobsTakenByNormalRequests) { |
| 4007 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4008 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4009 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4010 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 1, |
| 4011 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4012 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4013 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 4014 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4015 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4016 | TestGroupId("a"))); |
| 4017 | EXPECT_EQ(1u, |
| 4018 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4019 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4020 | |
| 4021 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 4022 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4023 | EXPECT_EQ( |
| 4024 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4025 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4026 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4027 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4028 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4029 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4030 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4031 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4032 | TestGroupId("a"))); |
| 4033 | EXPECT_EQ(0u, |
| 4034 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4035 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4036 | |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4037 | client_socket_factory_.SignalJobs(); |
| 4038 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 4039 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4040 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4041 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4042 | TestGroupId("a"))); |
| 4043 | EXPECT_EQ(0u, |
| 4044 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4045 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4046 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4047 | |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4048 | // Make sure if a preconnected socket is not fully connected when a request |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 4049 | // starts, it has a connect start time. |
| 4050 | TestLoadTimingInfoConnectedNotReused(handle1); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4051 | handle1.Reset(); |
| 4052 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4053 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4054 | } |
| 4055 | |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 4056 | // Checks that fully connected preconnect jobs have no connect times, and are |
| 4057 | // marked as reused. |
| 4058 | TEST_F(ClientSocketPoolBaseTest, ConnectedPreconnectJobsHaveNoConnectTimes) { |
| 4059 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4060 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4061 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 1, |
| 4062 | NetLogWithSource()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 4063 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4064 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 4065 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4066 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4067 | TestGroupId("a"))); |
| 4068 | EXPECT_EQ(0u, |
| 4069 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4070 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 4071 | |
| 4072 | ClientSocketHandle handle; |
| 4073 | TestCompletionCallback callback; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4074 | EXPECT_EQ(OK, handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4075 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4076 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4077 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4078 | pool_.get(), NetLogWithSource())); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 4079 | |
| 4080 | // Make sure the idle socket was used. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4081 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 4082 | |
| 4083 | TestLoadTimingInfoConnectedReused(handle); |
| 4084 | handle.Reset(); |
| 4085 | TestLoadTimingInfoNotConnected(handle); |
| 4086 | } |
| 4087 | |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4088 | // http://crbug.com/64940 regression test. |
| 4089 | TEST_F(ClientSocketPoolBaseTest, PreconnectClosesIdleSocketRemovesGroup) { |
| 4090 | const int kMaxTotalSockets = 3; |
| 4091 | const int kMaxSocketsPerGroup = 2; |
| 4092 | CreatePool(kMaxTotalSockets, kMaxSocketsPerGroup); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4093 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4094 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 4095 | // Note that group id ordering matters here. "a" comes before "b", so |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4096 | // CloseOneIdleSocket() will try to close "a"'s idle socket. |
| 4097 | |
| 4098 | // Set up one idle socket in "a". |
| 4099 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 4100 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4101 | EXPECT_EQ( |
| 4102 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4103 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4104 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4105 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4106 | pool_.get(), NetLogWithSource())); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4107 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 4108 | EXPECT_EQ(1u, 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"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4114 | |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4115 | client_socket_factory_.SignalJobs(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4116 | ASSERT_THAT(callback1.WaitForResult(), IsOk()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4117 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4118 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4119 | TestGroupId("a"))); |
| 4120 | EXPECT_EQ(0u, |
| 4121 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4122 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4123 | |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4124 | handle1.Reset(); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4125 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4126 | |
| 4127 | // Set up two active sockets in "b". |
| 4128 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 4129 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4130 | EXPECT_EQ( |
| 4131 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4132 | handle1.Init(TestGroupId("b"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4133 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4134 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4135 | pool_.get(), NetLogWithSource())); |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4136 | EXPECT_EQ( |
| 4137 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4138 | handle2.Init(TestGroupId("b"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4139 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4140 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4141 | pool_.get(), NetLogWithSource())); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4142 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4143 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("b"))); |
| 4144 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 4145 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4146 | TestGroupId("b"))); |
| 4147 | EXPECT_EQ(0u, |
| 4148 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4149 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4150 | |
| 4151 | client_socket_factory_.SignalJobs(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4152 | ASSERT_THAT(callback1.WaitForResult(), IsOk()); |
| 4153 | ASSERT_THAT(callback2.WaitForResult(), IsOk()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4154 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4155 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4156 | TestGroupId("b"))); |
| 4157 | EXPECT_EQ(0u, |
| 4158 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 4159 | EXPECT_EQ(2, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4160 | |
| 4161 | // Now we have 1 idle socket in "a" and 2 active sockets in "b". This means |
| 4162 | // we've maxed out on sockets, since we set |kMaxTotalSockets| to 3. |
| 4163 | // Requesting 2 preconnected sockets for "a" should fail to allocate any more |
| 4164 | // sockets for "a", and "b" should still have 2 active sockets. |
| 4165 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4166 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 4167 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4168 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4169 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4170 | TestGroupId("a"))); |
| 4171 | EXPECT_EQ(0u, |
| 4172 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4173 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4174 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 4175 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 4176 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4177 | TestGroupId("b"))); |
| 4178 | EXPECT_EQ(0u, |
| 4179 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4180 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4181 | EXPECT_EQ(2, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4182 | |
| 4183 | // Now release the 2 active sockets for "b". This will give us 1 idle socket |
| 4184 | // in "a" and 2 idle sockets in "b". Requesting 2 preconnected sockets for |
| 4185 | // "a" should result in closing 1 for "b". |
| 4186 | handle1.Reset(); |
| 4187 | handle2.Reset(); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4188 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4189 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4190 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4191 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 4192 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4193 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4194 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4195 | TestGroupId("a"))); |
| 4196 | EXPECT_EQ(1u, |
| 4197 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4198 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4199 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 4200 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 4201 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4202 | TestGroupId("b"))); |
| 4203 | EXPECT_EQ(0u, |
| 4204 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4205 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4206 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4207 | } |
| 4208 | |
[email protected] | b7b8be4 | 2011-07-12 12:46:41 | [diff] [blame] | 4209 | TEST_F(ClientSocketPoolBaseTest, PreconnectWithoutBackupJob) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4210 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 4211 | true /* enable_backup_connect_jobs */); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4212 | |
| 4213 | // Make the ConnectJob hang until it times out, shorten the timeout. |
| 4214 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4215 | connect_job_factory_->set_timeout_duration( |
| 4216 | base::TimeDelta::FromMilliseconds(500)); |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4217 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 1, |
| 4218 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4219 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4220 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4221 | TestGroupId("a"))); |
| 4222 | EXPECT_EQ(1u, |
| 4223 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4224 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4225 | |
[email protected] | b7b8be4 | 2011-07-12 12:46:41 | [diff] [blame] | 4226 | // Verify the backup timer doesn't create a backup job, by making |
| 4227 | // the backup job a pending job instead of a waiting job, so it |
| 4228 | // *would* complete if it were created. |
Lukasz Krakowiak | 28dcf9d6 | 2020-06-04 09:46:59 | [diff] [blame] | 4229 | base::RunLoop loop; |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4230 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 4231 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
Lukasz Krakowiak | 85529208 | 2020-06-19 12:43:58 | [diff] [blame] | 4232 | FROM_HERE, loop.QuitClosure(), base::TimeDelta::FromSeconds(1)); |
Lukasz Krakowiak | 28dcf9d6 | 2020-06-04 09:46:59 | [diff] [blame] | 4233 | loop.Run(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4234 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4235 | } |
| 4236 | |
[email protected] | b7b8be4 | 2011-07-12 12:46:41 | [diff] [blame] | 4237 | TEST_F(ClientSocketPoolBaseTest, PreconnectWithBackupJob) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4238 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 4239 | true /* enable_backup_connect_jobs */); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4240 | |
| 4241 | // Make the ConnectJob hang forever. |
| 4242 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4243 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 1, |
| 4244 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4245 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4246 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4247 | TestGroupId("a"))); |
| 4248 | EXPECT_EQ(1u, |
| 4249 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4250 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 4251 | base::RunLoop().RunUntilIdle(); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4252 | |
| 4253 | // Make the backup job be a pending job, so it completes normally. |
| 4254 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 4255 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 4256 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4257 | EXPECT_EQ( |
| 4258 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4259 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4260 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4261 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4262 | pool_.get(), NetLogWithSource())); |
[email protected] | b7b8be4 | 2011-07-12 12:46:41 | [diff] [blame] | 4263 | // Timer has started, but the backup connect job shouldn't be created yet. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4264 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4265 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4266 | TestGroupId("a"))); |
| 4267 | EXPECT_EQ(0u, |
| 4268 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4269 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4270 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4271 | ASSERT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4272 | |
| 4273 | // The hung connect job should still be there, but everything else should be |
| 4274 | // complete. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4275 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4276 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4277 | TestGroupId("a"))); |
| 4278 | EXPECT_EQ(1u, |
| 4279 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4280 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4281 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4282 | } |
| 4283 | |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4284 | // Tests that a preconnect that starts out with unread data can still be used. |
| 4285 | // http://crbug.com/334467 |
| 4286 | TEST_F(ClientSocketPoolBaseTest, PreconnectWithUnreadData) { |
| 4287 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4288 | connect_job_factory_->set_job_type(TestConnectJob::kMockUnreadDataJob); |
| 4289 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4290 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 1, |
| 4291 | NetLogWithSource()); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4292 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4293 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 4294 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4295 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4296 | TestGroupId("a"))); |
| 4297 | EXPECT_EQ(0u, |
| 4298 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4299 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4300 | |
| 4301 | // Fail future jobs to be sure that handle receives the preconnected socket |
| 4302 | // rather than closing it and making a new one. |
| 4303 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
| 4304 | ClientSocketHandle handle; |
| 4305 | TestCompletionCallback callback; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4306 | EXPECT_EQ(OK, handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4307 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4308 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4309 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4310 | pool_.get(), NetLogWithSource())); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4311 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4312 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 4313 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4314 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4315 | TestGroupId("a"))); |
| 4316 | EXPECT_EQ(0u, |
| 4317 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4318 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4319 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4320 | |
| 4321 | // Drain the pending read. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 4322 | EXPECT_EQ(1, handle.socket()->Read(nullptr, 1, CompletionOnceCallback())); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4323 | |
| 4324 | TestLoadTimingInfoConnectedReused(handle); |
| 4325 | handle.Reset(); |
| 4326 | |
| 4327 | // The socket should be usable now that it's idle again. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4328 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4329 | } |
| 4330 | |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4331 | TEST_F(ClientSocketPoolBaseTest, RequestGetsAssignedJob) { |
| 4332 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4333 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4334 | |
| 4335 | ClientSocketHandle handle1; |
| 4336 | TestCompletionCallback callback1; |
| 4337 | EXPECT_EQ( |
| 4338 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4339 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4340 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4341 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4342 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4343 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4344 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4345 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4346 | TestGroupId("a"))); |
| 4347 | EXPECT_EQ(0u, |
| 4348 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4349 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4350 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4351 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4352 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4353 | } |
| 4354 | |
| 4355 | TEST_F(ClientSocketPoolBaseTest, MultipleRequestsGetAssignedJobs) { |
| 4356 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4357 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4358 | |
| 4359 | ClientSocketHandle handle1; |
| 4360 | TestCompletionCallback callback1; |
| 4361 | EXPECT_EQ( |
| 4362 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4363 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4364 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4365 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4366 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4367 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4368 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4369 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4370 | TestGroupId("a"))); |
| 4371 | EXPECT_EQ(0u, |
| 4372 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4373 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4374 | |
| 4375 | ClientSocketHandle handle2; |
| 4376 | TestCompletionCallback callback2; |
| 4377 | EXPECT_EQ( |
| 4378 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4379 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4380 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4381 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4382 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4383 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4384 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4385 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4386 | TestGroupId("a"))); |
| 4387 | EXPECT_EQ(0u, |
| 4388 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4389 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4390 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4391 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4392 | &handle1)); |
| 4393 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4394 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4395 | |
| 4396 | // One job completes. The other request should still have its job. |
| 4397 | client_socket_factory_.SignalJob(0); |
| 4398 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 4399 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4400 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4401 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4402 | TestGroupId("a"))); |
| 4403 | EXPECT_EQ(0u, |
| 4404 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4405 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4406 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4407 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4408 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4409 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4410 | } |
| 4411 | |
| 4412 | TEST_F(ClientSocketPoolBaseTest, PreconnectJobGetsAssignedToRequest) { |
| 4413 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4414 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4415 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4416 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 1, |
| 4417 | NetLogWithSource()); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4418 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4419 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 4420 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4421 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4422 | TestGroupId("a"))); |
| 4423 | EXPECT_EQ(1u, |
| 4424 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4425 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4426 | |
| 4427 | ClientSocketHandle handle1; |
| 4428 | TestCompletionCallback callback1; |
| 4429 | EXPECT_EQ( |
| 4430 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4431 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4432 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4433 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4434 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4435 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4436 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4437 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4438 | TestGroupId("a"))); |
| 4439 | EXPECT_EQ(0u, |
| 4440 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4441 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4442 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4443 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4444 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4445 | } |
| 4446 | |
| 4447 | TEST_F(ClientSocketPoolBaseTest, HigherPriorityRequestStealsJob) { |
| 4448 | CreatePool(kDefaultMaxSockets, 1); |
| 4449 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4450 | |
| 4451 | ClientSocketHandle handle1; |
| 4452 | TestCompletionCallback callback1; |
| 4453 | EXPECT_EQ( |
| 4454 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4455 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4456 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4457 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4458 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4459 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4460 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4461 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4462 | TestGroupId("a"))); |
| 4463 | EXPECT_EQ(0u, |
| 4464 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4465 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4466 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4467 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4468 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4469 | |
| 4470 | // Insert a higher priority request |
| 4471 | ClientSocketHandle handle2; |
| 4472 | TestCompletionCallback callback2; |
| 4473 | EXPECT_EQ( |
| 4474 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4475 | handle2.Init(TestGroupId("a"), params_, base::nullopt, HIGHEST, |
| 4476 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4477 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4478 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 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"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4485 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4486 | |
| 4487 | // The highest priority request should steal the job from the default priority |
| 4488 | // request. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4489 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4490 | &handle2)); |
| 4491 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4492 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4493 | } |
| 4494 | |
| 4495 | TEST_F(ClientSocketPoolBaseTest, RequestStealsJobFromLowestRequestWithJob) { |
| 4496 | CreatePool(3, 3); |
| 4497 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4498 | |
| 4499 | ClientSocketHandle handle_lowest; |
| 4500 | TestCompletionCallback callback_lowest; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4501 | EXPECT_EQ( |
| 4502 | ERR_IO_PENDING, |
| 4503 | handle_lowest.Init(TestGroupId("a"), params_, base::nullopt, LOWEST, |
| 4504 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4505 | callback_lowest.callback(), |
| 4506 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4507 | NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4508 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4509 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4510 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4511 | TestGroupId("a"))); |
| 4512 | EXPECT_EQ(0u, |
| 4513 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4514 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4515 | |
| 4516 | ClientSocketHandle handle_highest; |
| 4517 | TestCompletionCallback callback_highest; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4518 | EXPECT_EQ( |
| 4519 | ERR_IO_PENDING, |
| 4520 | handle_highest.Init(TestGroupId("a"), params_, base::nullopt, HIGHEST, |
| 4521 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4522 | callback_highest.callback(), |
| 4523 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4524 | NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4525 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4526 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4527 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4528 | TestGroupId("a"))); |
| 4529 | EXPECT_EQ(0u, |
| 4530 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4531 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4532 | |
| 4533 | ClientSocketHandle handle_low; |
| 4534 | TestCompletionCallback callback_low; |
| 4535 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4536 | handle_low.Init( |
| 4537 | TestGroupId("a"), params_, base::nullopt, LOW, SocketTag(), |
| 4538 | ClientSocketPool::RespectLimits::ENABLED, |
| 4539 | callback_low.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4540 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4541 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4542 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4543 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4544 | TestGroupId("a"))); |
| 4545 | EXPECT_EQ(0u, |
| 4546 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4547 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4548 | |
| 4549 | ClientSocketHandle handle_lowest2; |
| 4550 | TestCompletionCallback callback_lowest2; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4551 | EXPECT_EQ( |
| 4552 | ERR_IO_PENDING, |
| 4553 | handle_lowest2.Init(TestGroupId("a"), params_, base::nullopt, LOWEST, |
| 4554 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4555 | callback_lowest2.callback(), |
| 4556 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4557 | NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4558 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4559 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4560 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4561 | TestGroupId("a"))); |
| 4562 | EXPECT_EQ(0u, |
| 4563 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4564 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4565 | |
| 4566 | // The top three requests in the queue should have jobs. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4567 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4568 | &handle_highest)); |
| 4569 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4570 | &handle_low)); |
| 4571 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4572 | &handle_lowest)); |
| 4573 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting( |
| 4574 | TestGroupId("a"), &handle_lowest2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4575 | |
| 4576 | // Add another request with medium priority. It should steal the job from the |
| 4577 | // lowest priority request with a job. |
| 4578 | ClientSocketHandle handle_medium; |
| 4579 | TestCompletionCallback callback_medium; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4580 | EXPECT_EQ( |
| 4581 | ERR_IO_PENDING, |
| 4582 | handle_medium.Init(TestGroupId("a"), params_, base::nullopt, MEDIUM, |
| 4583 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4584 | callback_medium.callback(), |
| 4585 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4586 | NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4587 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4588 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4589 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4590 | TestGroupId("a"))); |
| 4591 | EXPECT_EQ(0u, |
| 4592 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4593 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
| 4594 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4595 | &handle_highest)); |
| 4596 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4597 | &handle_medium)); |
| 4598 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4599 | &handle_low)); |
| 4600 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4601 | &handle_lowest)); |
| 4602 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting( |
| 4603 | TestGroupId("a"), &handle_lowest2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4604 | } |
| 4605 | |
| 4606 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeRequestStealsJob) { |
| 4607 | CreatePool(kDefaultMaxSockets, 1); |
| 4608 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4609 | |
| 4610 | ClientSocketHandle handle1; |
| 4611 | TestCompletionCallback callback1; |
| 4612 | EXPECT_EQ( |
| 4613 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4614 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4615 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4616 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4617 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4618 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4619 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4620 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4621 | TestGroupId("a"))); |
| 4622 | EXPECT_EQ(0u, |
| 4623 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4624 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4625 | |
| 4626 | ClientSocketHandle handle2; |
| 4627 | TestCompletionCallback callback2; |
| 4628 | EXPECT_EQ( |
| 4629 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4630 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4631 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4632 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4633 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4634 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4635 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4636 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4637 | TestGroupId("a"))); |
| 4638 | EXPECT_EQ(0u, |
| 4639 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4640 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4641 | |
| 4642 | // 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] | 4643 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4644 | &handle1)); |
| 4645 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4646 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4647 | |
| 4648 | // Reprioritizing the second request places it above the first, and it steals |
| 4649 | // the job from the first request. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4650 | pool_->SetPriority(TestGroupId("a"), &handle2, HIGHEST); |
| 4651 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4652 | &handle2)); |
| 4653 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4654 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4655 | } |
| 4656 | |
| 4657 | TEST_F(ClientSocketPoolBaseTest, CancelRequestReassignsJob) { |
| 4658 | CreatePool(kDefaultMaxSockets, 1); |
| 4659 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4660 | |
| 4661 | ClientSocketHandle handle1; |
| 4662 | TestCompletionCallback callback1; |
| 4663 | EXPECT_EQ( |
| 4664 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4665 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4666 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4667 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4668 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4669 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4670 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4671 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4672 | TestGroupId("a"))); |
| 4673 | EXPECT_EQ(0u, |
| 4674 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4675 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4676 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4677 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4678 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4679 | |
| 4680 | ClientSocketHandle handle2; |
| 4681 | TestCompletionCallback callback2; |
| 4682 | EXPECT_EQ( |
| 4683 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4684 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4685 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4686 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4687 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4688 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4689 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4690 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4691 | TestGroupId("a"))); |
| 4692 | EXPECT_EQ(0u, |
| 4693 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4694 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4695 | |
| 4696 | // The second request doesn't get a job because we are the limit. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4697 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4698 | &handle1)); |
| 4699 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4700 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4701 | |
| 4702 | // The second request should get a job upon cancelling the first request. |
| 4703 | handle1.Reset(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4704 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4705 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4706 | TestGroupId("a"))); |
| 4707 | EXPECT_EQ(0u, |
| 4708 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4709 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4710 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4711 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4712 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4713 | } |
| 4714 | |
| 4715 | TEST_F(ClientSocketPoolBaseTest, JobCompletionReassignsJob) { |
| 4716 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4717 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4718 | |
| 4719 | ClientSocketHandle handle1; |
| 4720 | TestCompletionCallback callback1; |
| 4721 | EXPECT_EQ( |
| 4722 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4723 | handle1.Init(TestGroupId("a"), params_, base::nullopt, HIGHEST, |
| 4724 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4725 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4726 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4727 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4728 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4729 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4730 | TestGroupId("a"))); |
| 4731 | EXPECT_EQ(0u, |
| 4732 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4733 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4734 | |
| 4735 | ClientSocketHandle handle2; |
| 4736 | TestCompletionCallback callback2; |
| 4737 | EXPECT_EQ( |
| 4738 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4739 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4740 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4741 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4742 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4743 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4744 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4745 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4746 | TestGroupId("a"))); |
| 4747 | EXPECT_EQ(0u, |
| 4748 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4749 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4750 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4751 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4752 | &handle1)); |
| 4753 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4754 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4755 | |
| 4756 | // The lower-priority job completes first. The higher-priority request should |
| 4757 | // get the socket, and the lower-priority request should get the remaining |
| 4758 | // job. |
| 4759 | client_socket_factory_.SignalJob(1); |
| 4760 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4761 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4762 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4763 | TestGroupId("a"))); |
| 4764 | EXPECT_EQ(0u, |
| 4765 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4766 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4767 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4768 | EXPECT_TRUE(handle1.socket()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4769 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4770 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4771 | } |
| 4772 | |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 4773 | class MockLayeredPool : public HigherLayeredPool { |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4774 | public: |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4775 | MockLayeredPool(TransportClientSocketPool* pool, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4776 | const ClientSocketPool::GroupId& group_id) |
| 4777 | : pool_(pool), group_id_(group_id), can_release_connection_(true) { |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 4778 | pool_->AddHigherLayeredPool(this); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4779 | } |
| 4780 | |
Daniel Cheng | 4496d082 | 2018-04-26 21:52:15 | [diff] [blame] | 4781 | ~MockLayeredPool() override { pool_->RemoveHigherLayeredPool(this); } |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4782 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4783 | int RequestSocket(TransportClientSocketPool* pool) { |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4784 | return handle_.Init( |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 4785 | group_id_, ClientSocketPool::SocketParams::CreateForHttpForTesting(), |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4786 | base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
| 4787 | ClientSocketPool::RespectLimits::ENABLED, callback_.callback(), |
| 4788 | ClientSocketPool::ProxyAuthCallback(), pool, NetLogWithSource()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4789 | } |
| 4790 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4791 | int RequestSocketWithoutLimits(TransportClientSocketPool* pool) { |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4792 | return handle_.Init( |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 4793 | group_id_, ClientSocketPool::SocketParams::CreateForHttpForTesting(), |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4794 | base::nullopt, MAXIMUM_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4795 | ClientSocketPool::RespectLimits::DISABLED, callback_.callback(), |
| 4796 | ClientSocketPool::ProxyAuthCallback(), pool, NetLogWithSource()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4797 | } |
| 4798 | |
| 4799 | bool ReleaseOneConnection() { |
| 4800 | if (!handle_.is_initialized() || !can_release_connection_) { |
| 4801 | return false; |
| 4802 | } |
| 4803 | handle_.socket()->Disconnect(); |
| 4804 | handle_.Reset(); |
| 4805 | return true; |
| 4806 | } |
| 4807 | |
| 4808 | void set_can_release_connection(bool can_release_connection) { |
| 4809 | can_release_connection_ = can_release_connection; |
| 4810 | } |
| 4811 | |
| 4812 | MOCK_METHOD0(CloseOneIdleConnection, bool()); |
| 4813 | |
| 4814 | private: |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4815 | TransportClientSocketPool* const pool_; |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4816 | ClientSocketHandle handle_; |
| 4817 | TestCompletionCallback callback_; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4818 | const ClientSocketPool::GroupId group_id_; |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4819 | bool can_release_connection_; |
| 4820 | }; |
| 4821 | |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4822 | // Tests the basic case of closing an idle socket in a higher layered pool when |
| 4823 | // a new request is issued and the lower layer pool is stalled. |
| 4824 | TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketsHeldByLayeredPoolWhenNeeded) { |
| 4825 | CreatePool(1, 1); |
| 4826 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 4827 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4828 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("foo")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4829 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4830 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4831 | .WillOnce(Invoke(&mock_layered_pool, |
| 4832 | &MockLayeredPool::ReleaseOneConnection)); |
| 4833 | ClientSocketHandle handle; |
| 4834 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4835 | EXPECT_EQ( |
| 4836 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4837 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4838 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4839 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4840 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4841 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4842 | } |
| 4843 | |
Matt Menke | 83367864 | 2019-03-05 22:05:51 | [diff] [blame] | 4844 | // Tests the case that trying to close an idle socket in a higher layered pool |
| 4845 | // fails. |
| 4846 | TEST_F(ClientSocketPoolBaseTest, |
| 4847 | CloseIdleSocketsHeldByLayeredPoolWhenNeededFails) { |
| 4848 | CreatePool(1, 1); |
| 4849 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 4850 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4851 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("foo")); |
Matt Menke | 83367864 | 2019-03-05 22:05:51 | [diff] [blame] | 4852 | mock_layered_pool.set_can_release_connection(false); |
| 4853 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
| 4854 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4855 | .WillOnce(Invoke(&mock_layered_pool, |
| 4856 | &MockLayeredPool::ReleaseOneConnection)); |
| 4857 | ClientSocketHandle handle; |
| 4858 | TestCompletionCallback callback; |
| 4859 | EXPECT_EQ( |
| 4860 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4861 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4862 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4863 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4864 | pool_.get(), NetLogWithSource())); |
Matt Menke | 83367864 | 2019-03-05 22:05:51 | [diff] [blame] | 4865 | base::RunLoop().RunUntilIdle(); |
| 4866 | EXPECT_FALSE(callback.have_result()); |
| 4867 | } |
| 4868 | |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4869 | // Same as above, but the idle socket is in the same group as the stalled |
| 4870 | // socket, and closes the only other request in its group when closing requests |
| 4871 | // in higher layered pools. This generally shouldn't happen, but it may be |
| 4872 | // possible if a higher level pool issues a request and the request is |
| 4873 | // subsequently cancelled. Even if it's not possible, best not to crash. |
| 4874 | TEST_F(ClientSocketPoolBaseTest, |
| 4875 | CloseIdleSocketsHeldByLayeredPoolWhenNeededSameGroup) { |
| 4876 | CreatePool(2, 2); |
| 4877 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 4878 | |
| 4879 | // Need a socket in another group for the pool to be stalled (If a group |
| 4880 | // has the maximum number of connections already, it's not stalled). |
| 4881 | ClientSocketHandle handle1; |
| 4882 | TestCompletionCallback callback1; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4883 | EXPECT_EQ(OK, handle1.Init(TestGroupId("group1"), params_, base::nullopt, |
| 4884 | DEFAULT_PRIORITY, SocketTag(), |
| 4885 | ClientSocketPool::RespectLimits::ENABLED, |
| 4886 | callback1.callback(), |
| 4887 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4888 | NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4889 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4890 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("group2")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4891 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4892 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4893 | .WillOnce(Invoke(&mock_layered_pool, |
| 4894 | &MockLayeredPool::ReleaseOneConnection)); |
| 4895 | ClientSocketHandle handle; |
| 4896 | TestCompletionCallback callback2; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4897 | EXPECT_EQ(ERR_IO_PENDING, |
| 4898 | handle.Init( |
| 4899 | TestGroupId("group2"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4900 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4901 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4902 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4903 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4904 | } |
| 4905 | |
| 4906 | // Tests the case when an idle socket can be closed when a new request is |
| 4907 | // issued, and the new request belongs to a group that was previously stalled. |
| 4908 | TEST_F(ClientSocketPoolBaseTest, |
| 4909 | CloseIdleSocketsHeldByLayeredPoolInSameGroupWhenNeeded) { |
| 4910 | CreatePool(2, 2); |
| 4911 | std::list<TestConnectJob::JobType> job_types; |
| 4912 | job_types.push_back(TestConnectJob::kMockJob); |
| 4913 | job_types.push_back(TestConnectJob::kMockJob); |
| 4914 | job_types.push_back(TestConnectJob::kMockJob); |
| 4915 | job_types.push_back(TestConnectJob::kMockJob); |
| 4916 | connect_job_factory_->set_job_types(&job_types); |
| 4917 | |
| 4918 | ClientSocketHandle handle1; |
| 4919 | TestCompletionCallback callback1; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4920 | EXPECT_EQ(OK, handle1.Init(TestGroupId("group1"), params_, base::nullopt, |
| 4921 | DEFAULT_PRIORITY, SocketTag(), |
| 4922 | ClientSocketPool::RespectLimits::ENABLED, |
| 4923 | callback1.callback(), |
| 4924 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4925 | NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4926 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4927 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("group2")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4928 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4929 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4930 | .WillRepeatedly(Invoke(&mock_layered_pool, |
| 4931 | &MockLayeredPool::ReleaseOneConnection)); |
| 4932 | mock_layered_pool.set_can_release_connection(false); |
| 4933 | |
| 4934 | // The third request is made when the socket pool is in a stalled state. |
| 4935 | ClientSocketHandle handle3; |
| 4936 | TestCompletionCallback callback3; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4937 | EXPECT_EQ(ERR_IO_PENDING, |
| 4938 | handle3.Init( |
| 4939 | TestGroupId("group3"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4940 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4941 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4942 | pool_.get(), NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4943 | |
| 4944 | base::RunLoop().RunUntilIdle(); |
| 4945 | EXPECT_FALSE(callback3.have_result()); |
| 4946 | |
| 4947 | // The fourth request is made when the pool is no longer stalled. The third |
| 4948 | // request should be serviced first, since it was issued first and has the |
| 4949 | // same priority. |
| 4950 | mock_layered_pool.set_can_release_connection(true); |
| 4951 | ClientSocketHandle handle4; |
| 4952 | TestCompletionCallback callback4; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4953 | EXPECT_EQ(ERR_IO_PENDING, |
| 4954 | handle4.Init( |
| 4955 | TestGroupId("group3"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4956 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4957 | callback4.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4958 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4959 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4960 | EXPECT_FALSE(callback4.have_result()); |
| 4961 | |
| 4962 | // Closing a handle should free up another socket slot. |
| 4963 | handle1.Reset(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4964 | EXPECT_THAT(callback4.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4965 | } |
| 4966 | |
| 4967 | // Tests the case when an idle socket can be closed when a new request is |
| 4968 | // issued, and the new request belongs to a group that was previously stalled. |
| 4969 | // |
| 4970 | // The two differences from the above test are that the stalled requests are not |
| 4971 | // in the same group as the layered pool's request, and the the fourth request |
| 4972 | // has a higher priority than the third one, so gets a socket first. |
| 4973 | TEST_F(ClientSocketPoolBaseTest, |
| 4974 | CloseIdleSocketsHeldByLayeredPoolInSameGroupWhenNeeded2) { |
| 4975 | CreatePool(2, 2); |
| 4976 | std::list<TestConnectJob::JobType> job_types; |
| 4977 | job_types.push_back(TestConnectJob::kMockJob); |
| 4978 | job_types.push_back(TestConnectJob::kMockJob); |
| 4979 | job_types.push_back(TestConnectJob::kMockJob); |
| 4980 | job_types.push_back(TestConnectJob::kMockJob); |
| 4981 | connect_job_factory_->set_job_types(&job_types); |
| 4982 | |
| 4983 | ClientSocketHandle handle1; |
| 4984 | TestCompletionCallback callback1; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4985 | EXPECT_EQ(OK, handle1.Init(TestGroupId("group1"), params_, base::nullopt, |
| 4986 | DEFAULT_PRIORITY, SocketTag(), |
| 4987 | ClientSocketPool::RespectLimits::ENABLED, |
| 4988 | callback1.callback(), |
| 4989 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4990 | NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4991 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4992 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("group2")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4993 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4994 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4995 | .WillRepeatedly(Invoke(&mock_layered_pool, |
| 4996 | &MockLayeredPool::ReleaseOneConnection)); |
| 4997 | mock_layered_pool.set_can_release_connection(false); |
| 4998 | |
| 4999 | // The third request is made when the socket pool is in a stalled state. |
| 5000 | ClientSocketHandle handle3; |
| 5001 | TestCompletionCallback callback3; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 5002 | EXPECT_EQ( |
| 5003 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5004 | handle3.Init(TestGroupId("group3"), params_, base::nullopt, MEDIUM, |
| 5005 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 5006 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5007 | pool_.get(), NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 5008 | |
| 5009 | base::RunLoop().RunUntilIdle(); |
| 5010 | EXPECT_FALSE(callback3.have_result()); |
| 5011 | |
| 5012 | // The fourth request is made when the pool is no longer stalled. This |
| 5013 | // request has a higher priority than the third request, so is serviced first. |
| 5014 | mock_layered_pool.set_can_release_connection(true); |
| 5015 | ClientSocketHandle handle4; |
| 5016 | TestCompletionCallback callback4; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 5017 | EXPECT_EQ( |
| 5018 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5019 | handle4.Init(TestGroupId("group3"), params_, base::nullopt, HIGHEST, |
| 5020 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 5021 | callback4.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5022 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5023 | EXPECT_THAT(callback4.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 5024 | EXPECT_FALSE(callback3.have_result()); |
| 5025 | |
| 5026 | // Closing a handle should free up another socket slot. |
| 5027 | handle1.Reset(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5028 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 5029 | } |
| 5030 | |
| 5031 | TEST_F(ClientSocketPoolBaseTest, |
| 5032 | CloseMultipleIdleSocketsHeldByLayeredPoolWhenNeeded) { |
| 5033 | CreatePool(1, 1); |
| 5034 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 5035 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5036 | MockLayeredPool mock_layered_pool1(pool_.get(), TestGroupId("foo")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5037 | EXPECT_THAT(mock_layered_pool1.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 5038 | EXPECT_CALL(mock_layered_pool1, CloseOneIdleConnection()) |
| 5039 | .WillRepeatedly(Invoke(&mock_layered_pool1, |
| 5040 | &MockLayeredPool::ReleaseOneConnection)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5041 | MockLayeredPool mock_layered_pool2(pool_.get(), TestGroupId("bar")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5042 | EXPECT_THAT(mock_layered_pool2.RequestSocketWithoutLimits(pool_.get()), |
| 5043 | IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 5044 | EXPECT_CALL(mock_layered_pool2, CloseOneIdleConnection()) |
| 5045 | .WillRepeatedly(Invoke(&mock_layered_pool2, |
| 5046 | &MockLayeredPool::ReleaseOneConnection)); |
| 5047 | ClientSocketHandle handle; |
| 5048 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 5049 | EXPECT_EQ( |
| 5050 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5051 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 5052 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5053 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5054 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5055 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 5056 | } |
| 5057 | |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5058 | // Test that when a socket pool and group are at their limits, a request |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5059 | // with RespectLimits::DISABLED triggers creation of a new socket, and gets the |
| 5060 | // socket instead of a request with the same priority that was issued earlier, |
| 5061 | // but has RespectLimits::ENABLED. |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5062 | TEST_F(ClientSocketPoolBaseTest, IgnoreLimits) { |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5063 | CreatePool(1, 1); |
| 5064 | |
| 5065 | // Issue a request to reach the socket pool limit. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5066 | EXPECT_EQ(OK, StartRequestWithIgnoreLimits( |
| 5067 | TestGroupId("a"), MAXIMUM_PRIORITY, |
| 5068 | ClientSocketPool::RespectLimits::ENABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5069 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5070 | |
| 5071 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 5072 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5073 | EXPECT_EQ(ERR_IO_PENDING, StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5074 | TestGroupId("a"), MAXIMUM_PRIORITY, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5075 | ClientSocketPool::RespectLimits::ENABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5076 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5077 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5078 | // Issue a request that ignores the limits, so a new ConnectJob is |
| 5079 | // created. |
| 5080 | EXPECT_EQ(ERR_IO_PENDING, StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5081 | TestGroupId("a"), MAXIMUM_PRIORITY, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5082 | ClientSocketPool::RespectLimits::DISABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5083 | ASSERT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5084 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5085 | EXPECT_THAT(request(2)->WaitForResult(), IsOk()); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5086 | EXPECT_FALSE(request(1)->have_result()); |
| 5087 | } |
| 5088 | |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 5089 | // Test that when a socket pool and group are at their limits, a ConnectJob |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5090 | // issued for a request with RespectLimits::DISABLED is not cancelled when a |
| 5091 | // request with RespectLimits::ENABLED issued to the same group is cancelled. |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 5092 | TEST_F(ClientSocketPoolBaseTest, IgnoreLimitsCancelOtherJob) { |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 5093 | CreatePool(1, 1); |
| 5094 | |
| 5095 | // Issue a request to reach the socket pool limit. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5096 | EXPECT_EQ(OK, StartRequestWithIgnoreLimits( |
| 5097 | TestGroupId("a"), MAXIMUM_PRIORITY, |
| 5098 | ClientSocketPool::RespectLimits::ENABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5099 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 5100 | |
| 5101 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 5102 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5103 | EXPECT_EQ(ERR_IO_PENDING, StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5104 | TestGroupId("a"), MAXIMUM_PRIORITY, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5105 | ClientSocketPool::RespectLimits::ENABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5106 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 5107 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5108 | // Issue a request with RespectLimits::DISABLED, so a new ConnectJob is |
| 5109 | // created. |
| 5110 | EXPECT_EQ(ERR_IO_PENDING, StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5111 | TestGroupId("a"), MAXIMUM_PRIORITY, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5112 | ClientSocketPool::RespectLimits::DISABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5113 | ASSERT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5114 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5115 | // Cancel the pending request with RespectLimits::ENABLED. The ConnectJob |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5116 | // should not be cancelled. |
| 5117 | request(1)->handle()->Reset(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5118 | ASSERT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5119 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5120 | EXPECT_THAT(request(2)->WaitForResult(), IsOk()); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5121 | EXPECT_FALSE(request(1)->have_result()); |
| 5122 | } |
| 5123 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5124 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthNoAuthCallback) { |
| 5125 | CreatePool(1, 1); |
| 5126 | |
| 5127 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5128 | |
| 5129 | ClientSocketHandle handle; |
| 5130 | TestCompletionCallback callback; |
| 5131 | EXPECT_EQ( |
| 5132 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5133 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 5134 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5135 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5136 | pool_.get(), NetLogWithSource())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5137 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5138 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5139 | |
| 5140 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_PROXY_AUTH_REQUESTED)); |
| 5141 | EXPECT_FALSE(handle.is_initialized()); |
| 5142 | EXPECT_FALSE(handle.socket()); |
| 5143 | |
| 5144 | // The group should now be empty, and thus be deleted. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5145 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5146 | } |
| 5147 | |
| 5148 | class TestAuthHelper { |
| 5149 | public: |
| 5150 | TestAuthHelper() = default; |
| 5151 | ~TestAuthHelper() = default; |
| 5152 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5153 | void InitHandle( |
Matt Menke | 84d11e56 | 2019-03-27 00:11:19 | [diff] [blame] | 5154 | scoped_refptr<ClientSocketPool::SocketParams> params, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5155 | TransportClientSocketPool* pool, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5156 | RequestPriority priority = DEFAULT_PRIORITY, |
| 5157 | ClientSocketPool::RespectLimits respect_limits = |
| 5158 | ClientSocketPool::RespectLimits::ENABLED, |
| 5159 | const ClientSocketPool::GroupId& group_id_in = TestGroupId("a")) { |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5160 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5161 | handle_.Init(group_id_in, params, base::nullopt, priority, |
| 5162 | SocketTag(), respect_limits, callback_.callback(), |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5163 | base::BindRepeating(&TestAuthHelper::AuthCallback, |
| 5164 | base::Unretained(this)), |
| 5165 | pool, NetLogWithSource())); |
| 5166 | } |
| 5167 | |
| 5168 | void WaitForAuth() { |
| 5169 | run_loop_ = std::make_unique<base::RunLoop>(); |
| 5170 | run_loop_->Run(); |
| 5171 | run_loop_.reset(); |
| 5172 | } |
| 5173 | |
| 5174 | void WaitForAuthAndRestartSync() { |
| 5175 | restart_sync_ = true; |
| 5176 | WaitForAuth(); |
| 5177 | restart_sync_ = false; |
| 5178 | } |
| 5179 | |
| 5180 | void WaitForAuthAndResetHandleSync() { |
| 5181 | reset_handle_sync_ = true; |
| 5182 | WaitForAuth(); |
| 5183 | reset_handle_sync_ = false; |
| 5184 | } |
| 5185 | |
| 5186 | void RestartWithAuth() { |
| 5187 | DCHECK(restart_with_auth_callback_); |
| 5188 | std::move(restart_with_auth_callback_).Run(); |
| 5189 | } |
| 5190 | |
| 5191 | int WaitForResult() { |
| 5192 | int result = callback_.WaitForResult(); |
| 5193 | // There shouldn't be any callback waiting to be invoked once the request is |
| 5194 | // complete. |
| 5195 | EXPECT_FALSE(restart_with_auth_callback_); |
| 5196 | // The socket should only be initialized on success. |
| 5197 | EXPECT_EQ(result == OK, handle_.is_initialized()); |
| 5198 | EXPECT_EQ(result == OK, handle_.socket() != nullptr); |
| 5199 | return result; |
| 5200 | } |
| 5201 | |
| 5202 | ClientSocketHandle* handle() { return &handle_; } |
| 5203 | int auth_count() const { return auth_count_; } |
| 5204 | int have_result() const { return callback_.have_result(); } |
| 5205 | |
| 5206 | private: |
| 5207 | void AuthCallback(const HttpResponseInfo& response, |
| 5208 | HttpAuthController* auth_controller, |
| 5209 | base::OnceClosure restart_with_auth_callback) { |
| 5210 | EXPECT_FALSE(restart_with_auth_callback_); |
| 5211 | EXPECT_TRUE(restart_with_auth_callback); |
| 5212 | |
| 5213 | // Once there's a result, this method shouldn't be invoked again. |
| 5214 | EXPECT_FALSE(callback_.have_result()); |
| 5215 | |
| 5216 | ++auth_count_; |
| 5217 | run_loop_->Quit(); |
| 5218 | if (restart_sync_) { |
| 5219 | std::move(restart_with_auth_callback).Run(); |
| 5220 | return; |
| 5221 | } |
| 5222 | |
| 5223 | restart_with_auth_callback_ = std::move(restart_with_auth_callback); |
| 5224 | |
| 5225 | if (reset_handle_sync_) { |
| 5226 | handle_.Reset(); |
| 5227 | return; |
| 5228 | } |
| 5229 | } |
| 5230 | |
| 5231 | std::unique_ptr<base::RunLoop> run_loop_; |
| 5232 | base::OnceClosure restart_with_auth_callback_; |
| 5233 | |
| 5234 | bool restart_sync_ = false; |
| 5235 | bool reset_handle_sync_ = false; |
| 5236 | |
| 5237 | ClientSocketHandle handle_; |
| 5238 | int auth_count_ = 0; |
| 5239 | TestCompletionCallback callback_; |
| 5240 | |
| 5241 | DISALLOW_COPY_AND_ASSIGN(TestAuthHelper); |
| 5242 | }; |
| 5243 | |
| 5244 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnce) { |
| 5245 | CreatePool(1, 1); |
| 5246 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5247 | |
| 5248 | TestAuthHelper auth_helper; |
| 5249 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5250 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5251 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5252 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5253 | |
| 5254 | auth_helper.WaitForAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5255 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5256 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5257 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5258 | |
| 5259 | auth_helper.RestartWithAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5260 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5261 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5262 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5263 | |
| 5264 | EXPECT_THAT(auth_helper.WaitForResult(), IsOk()); |
| 5265 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5266 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5267 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5268 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5269 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5270 | } |
| 5271 | |
| 5272 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceSync) { |
| 5273 | CreatePool(1, 1); |
| 5274 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5275 | |
| 5276 | TestAuthHelper auth_helper; |
| 5277 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5278 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5279 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5280 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5281 | |
| 5282 | auth_helper.WaitForAuthAndRestartSync(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5283 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5284 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5285 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5286 | |
| 5287 | EXPECT_THAT(auth_helper.WaitForResult(), IsOk()); |
| 5288 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5289 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5290 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5291 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5292 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5293 | } |
| 5294 | |
| 5295 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceFails) { |
| 5296 | CreatePool(1, 1); |
| 5297 | connect_job_factory_->set_job_type( |
| 5298 | TestConnectJob::kMockAuthChallengeOnceFailingJob); |
| 5299 | |
| 5300 | TestAuthHelper auth_helper; |
| 5301 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5302 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5303 | |
| 5304 | auth_helper.WaitForAuth(); |
| 5305 | auth_helper.RestartWithAuth(); |
| 5306 | EXPECT_THAT(auth_helper.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 5307 | |
| 5308 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5309 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5310 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5311 | } |
| 5312 | |
| 5313 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceSyncFails) { |
| 5314 | CreatePool(1, 1); |
| 5315 | connect_job_factory_->set_job_type( |
| 5316 | TestConnectJob::kMockAuthChallengeOnceFailingJob); |
| 5317 | |
| 5318 | TestAuthHelper auth_helper; |
| 5319 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5320 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5321 | |
| 5322 | auth_helper.WaitForAuthAndRestartSync(); |
| 5323 | EXPECT_THAT(auth_helper.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 5324 | |
| 5325 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5326 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5327 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5328 | } |
| 5329 | |
| 5330 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceDeleteHandle) { |
| 5331 | CreatePool(1, 1); |
| 5332 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5333 | |
| 5334 | TestAuthHelper auth_helper; |
| 5335 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5336 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5337 | |
| 5338 | auth_helper.WaitForAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5339 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5340 | |
| 5341 | auth_helper.handle()->Reset(); |
| 5342 | |
| 5343 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5344 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5345 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5346 | EXPECT_FALSE(auth_helper.handle()->is_initialized()); |
| 5347 | EXPECT_FALSE(auth_helper.handle()->socket()); |
| 5348 | } |
| 5349 | |
| 5350 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceDeleteHandleSync) { |
| 5351 | CreatePool(1, 1); |
| 5352 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5353 | |
| 5354 | TestAuthHelper auth_helper; |
| 5355 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5356 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5357 | |
| 5358 | auth_helper.WaitForAuthAndResetHandleSync(); |
| 5359 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5360 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5361 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5362 | EXPECT_FALSE(auth_helper.handle()->is_initialized()); |
| 5363 | EXPECT_FALSE(auth_helper.handle()->socket()); |
| 5364 | } |
| 5365 | |
| 5366 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceFlushWithError) { |
| 5367 | CreatePool(1, 1); |
| 5368 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5369 | |
| 5370 | TestAuthHelper auth_helper; |
| 5371 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5372 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5373 | |
| 5374 | auth_helper.WaitForAuth(); |
| 5375 | |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 5376 | pool_->FlushWithError(ERR_FAILED, "Network changed"); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5377 | base::RunLoop().RunUntilIdle(); |
| 5378 | |
| 5379 | // When flushing the socket pool, bound sockets should delay returning the |
| 5380 | // error until completion. |
| 5381 | EXPECT_FALSE(auth_helper.have_result()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5382 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5383 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5384 | |
| 5385 | auth_helper.RestartWithAuth(); |
| 5386 | // The callback should be called asynchronously. |
| 5387 | EXPECT_FALSE(auth_helper.have_result()); |
| 5388 | |
| 5389 | EXPECT_THAT(auth_helper.WaitForResult(), IsError(ERR_FAILED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5390 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5391 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5392 | } |
| 5393 | |
| 5394 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthTwice) { |
| 5395 | CreatePool(1, 1); |
| 5396 | connect_job_factory_->set_job_type( |
| 5397 | TestConnectJob::kMockAuthChallengeTwiceJob); |
| 5398 | |
| 5399 | TestAuthHelper auth_helper; |
| 5400 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5401 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5402 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5403 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5404 | |
| 5405 | auth_helper.WaitForAuth(); |
| 5406 | auth_helper.RestartWithAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5407 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5408 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5409 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5410 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5411 | |
| 5412 | auth_helper.WaitForAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5413 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5414 | EXPECT_EQ(2, auth_helper.auth_count()); |
| 5415 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5416 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5417 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5418 | auth_helper.RestartWithAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5419 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5420 | EXPECT_EQ(2, auth_helper.auth_count()); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5421 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5422 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5423 | |
| 5424 | EXPECT_THAT(auth_helper.WaitForResult(), IsOk()); |
| 5425 | EXPECT_EQ(2, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5426 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5427 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5428 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5429 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5430 | } |
| 5431 | |
| 5432 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthTwiceFails) { |
| 5433 | CreatePool(1, 1); |
| 5434 | connect_job_factory_->set_job_type( |
| 5435 | TestConnectJob::kMockAuthChallengeTwiceFailingJob); |
| 5436 | |
| 5437 | TestAuthHelper auth_helper; |
| 5438 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5439 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5440 | |
| 5441 | auth_helper.WaitForAuth(); |
| 5442 | auth_helper.RestartWithAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5443 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5444 | EXPECT_EQ(1, auth_helper.auth_count()); |
| 5445 | |
| 5446 | auth_helper.WaitForAuth(); |
| 5447 | auth_helper.RestartWithAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5448 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5449 | EXPECT_EQ(2, auth_helper.auth_count()); |
| 5450 | |
| 5451 | EXPECT_THAT(auth_helper.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 5452 | EXPECT_EQ(2, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5453 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5454 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5455 | } |
| 5456 | |
| 5457 | // Makes sure that when a bound request is destroyed, a new ConnectJob is |
| 5458 | // created, if needed. |
| 5459 | TEST_F(ClientSocketPoolBaseTest, |
| 5460 | ProxyAuthCreateNewConnectJobOnDestroyBoundRequest) { |
| 5461 | CreatePool(1 /* max_sockets */, 1 /* max_sockets_per_group */); |
| 5462 | connect_job_factory_->set_job_type( |
| 5463 | TestConnectJob::kMockAuthChallengeOnceFailingJob); |
| 5464 | |
| 5465 | // First request creates a ConnectJob. |
| 5466 | TestAuthHelper auth_helper1; |
| 5467 | auth_helper1.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5468 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5469 | |
| 5470 | // A second request come in, but no new ConnectJob is needed, since the limit |
| 5471 | // has been reached. |
| 5472 | TestAuthHelper auth_helper2; |
| 5473 | auth_helper2.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5474 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5475 | |
| 5476 | // Run until the auth callback for the first request is invoked. |
| 5477 | auth_helper1.WaitForAuth(); |
| 5478 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5479 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5480 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5481 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5482 | |
| 5483 | // Make connect jobs succeed, then cancel the first request, which should |
| 5484 | // destroy the bound ConnectJob, and cause a new ConnectJob to start. |
| 5485 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 5486 | auth_helper1.handle()->Reset(); |
| 5487 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5488 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5489 | |
| 5490 | // The second ConnectJob should succeed. |
| 5491 | EXPECT_THAT(auth_helper2.WaitForResult(), IsOk()); |
| 5492 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5493 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5494 | } |
| 5495 | |
| 5496 | // Makes sure that when a bound request is destroyed, a new ConnectJob is |
| 5497 | // created for another group, if needed. |
| 5498 | TEST_F(ClientSocketPoolBaseTest, |
| 5499 | ProxyAuthCreateNewConnectJobOnDestroyBoundRequestDifferentGroups) { |
| 5500 | CreatePool(1 /* max_sockets */, 1 /* max_sockets_per_group */); |
| 5501 | connect_job_factory_->set_job_type( |
| 5502 | TestConnectJob::kMockAuthChallengeOnceFailingJob); |
| 5503 | |
| 5504 | // First request creates a ConnectJob. |
| 5505 | TestAuthHelper auth_helper1; |
| 5506 | auth_helper1.InitHandle(params_, pool_.get(), DEFAULT_PRIORITY); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5507 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5508 | |
| 5509 | // A second request come in, but no new ConnectJob is needed, since the limit |
| 5510 | // has been reached. |
| 5511 | TestAuthHelper auth_helper2; |
| 5512 | auth_helper2.InitHandle(params_, pool_.get(), DEFAULT_PRIORITY, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5513 | ClientSocketPool::RespectLimits::ENABLED, |
| 5514 | TestGroupId("b")); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5515 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5516 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5517 | |
| 5518 | // Run until the auth callback for the first request is invoked. |
| 5519 | auth_helper1.WaitForAuth(); |
| 5520 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5521 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5522 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5523 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5524 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 5525 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5526 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5527 | |
| 5528 | // Make connect jobs succeed, then cancel the first request, which should |
| 5529 | // destroy the bound ConnectJob, and cause a new ConnectJob to start for the |
| 5530 | // other group. |
| 5531 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 5532 | auth_helper1.handle()->Reset(); |
| 5533 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5534 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 5535 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5536 | |
| 5537 | // The second ConnectJob should succeed. |
| 5538 | EXPECT_THAT(auth_helper2.WaitForResult(), IsOk()); |
| 5539 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5540 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 5541 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5542 | } |
| 5543 | |
| 5544 | // Test that once an auth challenge is bound, that's the request that gets all |
| 5545 | // subsequent calls and the socket itself. |
| 5546 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthStaysBound) { |
| 5547 | CreatePool(1, 1); |
| 5548 | connect_job_factory_->set_job_type( |
| 5549 | TestConnectJob::kMockAuthChallengeTwiceJob); |
| 5550 | |
| 5551 | // First request creates a ConnectJob. |
| 5552 | TestAuthHelper auth_helper1; |
| 5553 | auth_helper1.InitHandle(params_, pool_.get(), LOWEST); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5554 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5555 | |
| 5556 | // A second, higher priority request is made. |
| 5557 | TestAuthHelper auth_helper2; |
| 5558 | auth_helper2.InitHandle(params_, pool_.get(), LOW); |
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 second request is invoked. |
| 5562 | auth_helper2.WaitForAuth(); |
| 5563 | EXPECT_EQ(0, auth_helper1.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 | // Start a higher priority job. It shouldn't be able to steal |auth_helper2|'s |
| 5569 | // ConnectJob. |
| 5570 | TestAuthHelper auth_helper3; |
| 5571 | auth_helper3.InitHandle(params_, pool_.get(), HIGHEST); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5572 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5573 | |
| 5574 | // Start a higher job that ignores limits, creating a hanging socket. It |
| 5575 | // shouldn't be able to steal |auth_helper2|'s ConnectJob. |
| 5576 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 5577 | TestAuthHelper auth_helper4; |
| 5578 | auth_helper4.InitHandle(params_, pool_.get(), HIGHEST, |
| 5579 | ClientSocketPool::RespectLimits::DISABLED); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5580 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5581 | |
| 5582 | // Restart with auth, and |auth_helper2|'s auth method should be invoked |
| 5583 | // again. |
| 5584 | auth_helper2.RestartWithAuth(); |
| 5585 | auth_helper2.WaitForAuth(); |
| 5586 | EXPECT_EQ(0, auth_helper1.auth_count()); |
| 5587 | EXPECT_FALSE(auth_helper1.have_result()); |
| 5588 | EXPECT_EQ(2, auth_helper2.auth_count()); |
| 5589 | EXPECT_FALSE(auth_helper2.have_result()); |
| 5590 | EXPECT_EQ(0, auth_helper3.auth_count()); |
| 5591 | EXPECT_FALSE(auth_helper3.have_result()); |
| 5592 | EXPECT_EQ(0, auth_helper4.auth_count()); |
| 5593 | EXPECT_FALSE(auth_helper4.have_result()); |
| 5594 | |
| 5595 | // Advance auth again, and |auth_helper2| should get the socket. |
| 5596 | auth_helper2.RestartWithAuth(); |
| 5597 | EXPECT_THAT(auth_helper2.WaitForResult(), IsOk()); |
| 5598 | // The hung ConnectJob for the RespectLimits::DISABLED request is still in the |
| 5599 | // socket pool. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5600 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5601 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5602 | EXPECT_EQ(0, auth_helper1.auth_count()); |
| 5603 | EXPECT_FALSE(auth_helper1.have_result()); |
| 5604 | EXPECT_EQ(0, auth_helper3.auth_count()); |
| 5605 | EXPECT_FALSE(auth_helper3.have_result()); |
| 5606 | EXPECT_EQ(0, auth_helper4.auth_count()); |
| 5607 | EXPECT_FALSE(auth_helper4.have_result()); |
| 5608 | |
| 5609 | // If the socket is returned to the socket pool, the RespectLimits::DISABLED |
| 5610 | // socket request should be able to claim it. |
| 5611 | auth_helper2.handle()->Reset(); |
| 5612 | EXPECT_THAT(auth_helper4.WaitForResult(), IsOk()); |
| 5613 | EXPECT_EQ(0, auth_helper1.auth_count()); |
| 5614 | EXPECT_FALSE(auth_helper1.have_result()); |
| 5615 | EXPECT_EQ(0, auth_helper3.auth_count()); |
| 5616 | EXPECT_FALSE(auth_helper3.have_result()); |
| 5617 | EXPECT_EQ(0, auth_helper4.auth_count()); |
| 5618 | } |
| 5619 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5620 | enum class RefreshType { |
| 5621 | kServer, |
| 5622 | kProxy, |
| 5623 | }; |
| 5624 | |
| 5625 | // Common base class to test RefreshGroup() when called from either |
| 5626 | // OnSSLConfigForServerChanged() matching a specific group or the pool's proxy. |
| 5627 | // |
| 5628 | // Tests which test behavior specific to one or the other case should use |
| 5629 | // ClientSocketPoolBaseTest directly. In particular, there is no "other group" |
| 5630 | // when the pool's proxy matches. |
| 5631 | class ClientSocketPoolBaseRefreshTest |
| 5632 | : public ClientSocketPoolBaseTest, |
| 5633 | public testing::WithParamInterface<RefreshType> { |
| 5634 | public: |
| 5635 | void CreatePoolForRefresh(int max_sockets, |
| 5636 | int max_sockets_per_group, |
| 5637 | bool enable_backup_connect_jobs = false) { |
| 5638 | switch (GetParam()) { |
| 5639 | case RefreshType::kServer: |
| 5640 | CreatePool(max_sockets, max_sockets_per_group, |
| 5641 | enable_backup_connect_jobs); |
| 5642 | break; |
| 5643 | case RefreshType::kProxy: |
| 5644 | CreatePoolWithIdleTimeouts( |
| 5645 | max_sockets, max_sockets_per_group, kUnusedIdleSocketTimeout, |
| 5646 | ClientSocketPool::used_idle_socket_timeout(), |
| 5647 | enable_backup_connect_jobs, |
| 5648 | ProxyServer::FromPacString("HTTPS myproxy:70")); |
| 5649 | break; |
| 5650 | } |
| 5651 | } |
| 5652 | |
| 5653 | static ClientSocketPool::GroupId GetGroupId() { |
| 5654 | return TestGroupId("a", 443, ClientSocketPool::SocketType::kSsl); |
| 5655 | } |
| 5656 | |
David Benjamin | 6dd7e88 | 2019-10-10 02:35:23 | [diff] [blame] | 5657 | static ClientSocketPool::GroupId GetGroupIdInPartition() { |
| 5658 | // Note this GroupId will match GetGroupId() unless |
| 5659 | // kPartitionConnectionsByNetworkIsolationKey is enabled. |
Matt Menke | 4807a9a | 2020-11-21 00:14:41 | [diff] [blame] | 5660 | const SchemefulSite kSite(GURL("https://b/")); |
| 5661 | const NetworkIsolationKey kNetworkIsolationKey(kSite, kSite); |
David Benjamin | 6dd7e88 | 2019-10-10 02:35:23 | [diff] [blame] | 5662 | return TestGroupId("a", 443, ClientSocketPool::SocketType::kSsl, |
| 5663 | PrivacyMode::PRIVACY_MODE_DISABLED, |
| 5664 | kNetworkIsolationKey); |
| 5665 | } |
| 5666 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5667 | void OnSSLConfigForServerChanged() { |
| 5668 | switch (GetParam()) { |
| 5669 | case RefreshType::kServer: |
| 5670 | pool_->OnSSLConfigForServerChanged(HostPortPair("a", 443)); |
| 5671 | break; |
| 5672 | case RefreshType::kProxy: |
| 5673 | pool_->OnSSLConfigForServerChanged(HostPortPair("myproxy", 70)); |
| 5674 | break; |
| 5675 | } |
| 5676 | } |
| 5677 | }; |
| 5678 | |
| 5679 | INSTANTIATE_TEST_SUITE_P(RefreshType, |
| 5680 | ClientSocketPoolBaseRefreshTest, |
| 5681 | ::testing::Values(RefreshType::kServer, |
| 5682 | RefreshType::kProxy)); |
| 5683 | |
| 5684 | TEST_P(ClientSocketPoolBaseRefreshTest, RefreshGroupCreatesNewConnectJobs) { |
| 5685 | CreatePoolForRefresh(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 5686 | const ClientSocketPool::GroupId kGroupId = GetGroupId(); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5687 | |
| 5688 | // First job will be waiting until it gets aborted. |
| 5689 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 5690 | |
| 5691 | ClientSocketHandle handle; |
| 5692 | TestCompletionCallback callback; |
| 5693 | EXPECT_THAT( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5694 | handle.Init(kGroupId, params_, base::nullopt, DEFAULT_PRIORITY, |
| 5695 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5696 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5697 | pool_.get(), NetLogWithSource()), |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5698 | IsError(ERR_IO_PENDING)); |
| 5699 | |
| 5700 | // Switch connect job types, so creating a new ConnectJob will result in |
| 5701 | // success. |
| 5702 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 5703 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5704 | OnSSLConfigForServerChanged(); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5705 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 5706 | ASSERT_TRUE(handle.socket()); |
| 5707 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5708 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId)); |
| 5709 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(kGroupId)); |
| 5710 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(kGroupId)); |
| 5711 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId)); |
| 5712 | } |
| 5713 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5714 | TEST_P(ClientSocketPoolBaseRefreshTest, RefreshGroupClosesIdleConnectJobs) { |
David Benjamin | 6dd7e88 | 2019-10-10 02:35:23 | [diff] [blame] | 5715 | base::test::ScopedFeatureList feature_list; |
| 5716 | feature_list.InitAndEnableFeature( |
| 5717 | features::kPartitionConnectionsByNetworkIsolationKey); |
| 5718 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5719 | CreatePoolForRefresh(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 5720 | const ClientSocketPool::GroupId kGroupId = GetGroupId(); |
David Benjamin | 6dd7e88 | 2019-10-10 02:35:23 | [diff] [blame] | 5721 | const ClientSocketPool::GroupId kGroupIdInPartition = GetGroupIdInPartition(); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5722 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5723 | pool_->RequestSockets(kGroupId, params_, base::nullopt, 2, |
| 5724 | NetLogWithSource()); |
David Benjamin | 6dd7e88 | 2019-10-10 02:35:23 | [diff] [blame] | 5725 | pool_->RequestSockets(kGroupIdInPartition, params_, base::nullopt, 2, |
| 5726 | NetLogWithSource()); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5727 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId)); |
David Benjamin | 6dd7e88 | 2019-10-10 02:35:23 | [diff] [blame] | 5728 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupIdInPartition)); |
| 5729 | EXPECT_EQ(4, pool_->IdleSocketCount()); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5730 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(kGroupId)); |
David Benjamin | 6dd7e88 | 2019-10-10 02:35:23 | [diff] [blame] | 5731 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(kGroupIdInPartition)); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5732 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5733 | OnSSLConfigForServerChanged(); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5734 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5735 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupId)); |
David Benjamin | 6dd7e88 | 2019-10-10 02:35:23 | [diff] [blame] | 5736 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupIdInPartition)); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5737 | } |
| 5738 | |
| 5739 | TEST_F(ClientSocketPoolBaseTest, |
| 5740 | RefreshGroupDoesNotCloseIdleConnectJobsInOtherGroup) { |
| 5741 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5742 | const ClientSocketPool::GroupId kGroupId = |
| 5743 | TestGroupId("a", 443, ClientSocketPool::SocketType::kSsl); |
| 5744 | const ClientSocketPool::GroupId kOtherGroupId = |
| 5745 | TestGroupId("b", 443, ClientSocketPool::SocketType::kSsl); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5746 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5747 | pool_->RequestSockets(kOtherGroupId, params_, base::nullopt, 2, |
| 5748 | NetLogWithSource()); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5749 | ASSERT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 5750 | EXPECT_EQ(2, pool_->IdleSocketCount()); |
| 5751 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(kOtherGroupId)); |
| 5752 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5753 | pool_->OnSSLConfigForServerChanged(HostPortPair("a", 443)); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5754 | ASSERT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 5755 | EXPECT_EQ(2, pool_->IdleSocketCount()); |
| 5756 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(kOtherGroupId)); |
| 5757 | } |
| 5758 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5759 | TEST_P(ClientSocketPoolBaseRefreshTest, RefreshGroupPreventsSocketReuse) { |
| 5760 | CreatePoolForRefresh(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 5761 | const ClientSocketPool::GroupId kGroupId = GetGroupId(); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5762 | |
| 5763 | ClientSocketHandle handle; |
| 5764 | TestCompletionCallback callback; |
| 5765 | EXPECT_THAT( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5766 | handle.Init(kGroupId, params_, base::nullopt, DEFAULT_PRIORITY, |
| 5767 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5768 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5769 | pool_.get(), NetLogWithSource()), |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5770 | IsOk()); |
| 5771 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId)); |
| 5772 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId)); |
| 5773 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5774 | OnSSLConfigForServerChanged(); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5775 | |
| 5776 | handle.Reset(); |
| 5777 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5778 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupId)); |
| 5779 | } |
| 5780 | |
| 5781 | TEST_F(ClientSocketPoolBaseTest, |
| 5782 | RefreshGroupDoesNotPreventSocketReuseInOtherGroup) { |
| 5783 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5784 | const ClientSocketPool::GroupId kGroupId = |
| 5785 | TestGroupId("a", 443, ClientSocketPool::SocketType::kSsl); |
| 5786 | const ClientSocketPool::GroupId kOtherGroupId = |
| 5787 | TestGroupId("b", 443, ClientSocketPool::SocketType::kSsl); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5788 | |
| 5789 | ClientSocketHandle handle; |
| 5790 | TestCompletionCallback callback; |
| 5791 | EXPECT_THAT( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5792 | handle.Init(kOtherGroupId, params_, base::nullopt, DEFAULT_PRIORITY, |
| 5793 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5794 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5795 | pool_.get(), NetLogWithSource()), |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5796 | IsOk()); |
| 5797 | ASSERT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 5798 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kOtherGroupId)); |
| 5799 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5800 | pool_->OnSSLConfigForServerChanged(HostPortPair("a", 443)); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5801 | |
| 5802 | handle.Reset(); |
| 5803 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 5804 | ASSERT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 5805 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(kOtherGroupId)); |
| 5806 | } |
| 5807 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5808 | TEST_P(ClientSocketPoolBaseRefreshTest, |
| 5809 | RefreshGroupReplacesBoundConnectJobOnConnect) { |
| 5810 | CreatePoolForRefresh(1, 1); |
| 5811 | const ClientSocketPool::GroupId kGroupId = GetGroupId(); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5812 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5813 | |
| 5814 | TestAuthHelper auth_helper; |
| 5815 | auth_helper.InitHandle(params_, pool_.get(), DEFAULT_PRIORITY, |
| 5816 | ClientSocketPool::RespectLimits::ENABLED, kGroupId); |
| 5817 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(kGroupId)); |
| 5818 | |
| 5819 | auth_helper.WaitForAuth(); |
| 5820 | |
| 5821 | // This should update the generation, but not cancel the old ConnectJob - it's |
| 5822 | // not safe to do anything while waiting on the original ConnectJob. |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5823 | OnSSLConfigForServerChanged(); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5824 | |
| 5825 | // Providing auth credentials and restarting the request with them will cause |
| 5826 | // the ConnectJob to complete successfully, but the result will be discarded |
| 5827 | // because of the generation mismatch. |
| 5828 | auth_helper.RestartWithAuth(); |
| 5829 | |
| 5830 | // Despite using ConnectJobs that simulate a single challenge, a second |
| 5831 | // challenge will be seen, due to using a new ConnectJob. |
| 5832 | auth_helper.WaitForAuth(); |
| 5833 | auth_helper.RestartWithAuth(); |
| 5834 | |
| 5835 | EXPECT_THAT(auth_helper.WaitForResult(), IsOk()); |
| 5836 | EXPECT_TRUE(auth_helper.handle()->socket()); |
| 5837 | EXPECT_EQ(2, auth_helper.auth_count()); |
| 5838 | |
| 5839 | // When released, the socket will be returned to the socket pool, and |
| 5840 | // available for reuse. |
| 5841 | auth_helper.handle()->Reset(); |
| 5842 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 5843 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId)); |
| 5844 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(kGroupId)); |
| 5845 | } |
| 5846 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5847 | TEST_F(ClientSocketPoolBaseTest, RefreshProxyRefreshesAllGroups) { |
| 5848 | CreatePoolWithIdleTimeouts(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 5849 | kUnusedIdleSocketTimeout, |
| 5850 | ClientSocketPool::used_idle_socket_timeout(), |
| 5851 | false /* no backup connect jobs */, |
| 5852 | ProxyServer::FromPacString("HTTPS myproxy:70")); |
| 5853 | |
| 5854 | const ClientSocketPool::GroupId kGroupId1 = |
| 5855 | TestGroupId("a", 443, ClientSocketPool::SocketType::kSsl); |
| 5856 | const ClientSocketPool::GroupId kGroupId2 = |
| 5857 | TestGroupId("b", 443, ClientSocketPool::SocketType::kSsl); |
| 5858 | const ClientSocketPool::GroupId kGroupId3 = |
| 5859 | TestGroupId("c", 443, ClientSocketPool::SocketType::kSsl); |
| 5860 | |
| 5861 | // Make three sockets in three different groups. The third socket is released |
| 5862 | // to the pool as idle. |
| 5863 | ClientSocketHandle handle1, handle2, handle3; |
| 5864 | TestCompletionCallback callback; |
| 5865 | EXPECT_THAT( |
| 5866 | handle1.Init(kGroupId1, params_, base::nullopt, DEFAULT_PRIORITY, |
| 5867 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5868 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5869 | pool_.get(), NetLogWithSource()), |
| 5870 | IsOk()); |
| 5871 | EXPECT_THAT( |
| 5872 | handle2.Init(kGroupId2, params_, base::nullopt, DEFAULT_PRIORITY, |
| 5873 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5874 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5875 | pool_.get(), NetLogWithSource()), |
| 5876 | IsOk()); |
| 5877 | EXPECT_THAT( |
| 5878 | handle3.Init(kGroupId3, params_, base::nullopt, DEFAULT_PRIORITY, |
| 5879 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5880 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5881 | pool_.get(), NetLogWithSource()), |
| 5882 | IsOk()); |
| 5883 | handle3.Reset(); |
| 5884 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId1)); |
| 5885 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId1)); |
| 5886 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId2)); |
| 5887 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId2)); |
| 5888 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId3)); |
| 5889 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(kGroupId3)); |
| 5890 | |
| 5891 | // Changes to some other proxy do not affect the pool. The idle socket remains |
| 5892 | // alive and closing |handle2| makes the socket available for the pool. |
| 5893 | pool_->OnSSLConfigForServerChanged(HostPortPair("someotherproxy", 70)); |
| 5894 | |
| 5895 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId1)); |
| 5896 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId1)); |
| 5897 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId2)); |
| 5898 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId2)); |
| 5899 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId3)); |
| 5900 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(kGroupId3)); |
| 5901 | |
| 5902 | handle2.Reset(); |
| 5903 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId2)); |
| 5904 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(kGroupId2)); |
| 5905 | |
| 5906 | // Changes to the matching proxy refreshes all groups. |
| 5907 | pool_->OnSSLConfigForServerChanged(HostPortPair("myproxy", 70)); |
| 5908 | |
| 5909 | // Idle sockets are closed. |
| 5910 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5911 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupId2)); |
| 5912 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupId3)); |
| 5913 | |
| 5914 | // The active socket, however, continues to be active. |
| 5915 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId1)); |
| 5916 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId1)); |
| 5917 | |
| 5918 | // Closing it does not make it available for the pool. |
| 5919 | handle1.Reset(); |
| 5920 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5921 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupId1)); |
| 5922 | } |
| 5923 | |
| 5924 | TEST_F(ClientSocketPoolBaseTest, RefreshBothPrivacyAndNormalSockets) { |
| 5925 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 5926 | |
| 5927 | const ClientSocketPool::GroupId kGroupId = |
| 5928 | TestGroupId("a", 443, ClientSocketPool::SocketType::kSsl, |
| 5929 | PrivacyMode::PRIVACY_MODE_DISABLED); |
| 5930 | const ClientSocketPool::GroupId kGroupIdPrivacy = |
| 5931 | TestGroupId("a", 443, ClientSocketPool::SocketType::kSsl, |
| 5932 | PrivacyMode::PRIVACY_MODE_ENABLED); |
| 5933 | const ClientSocketPool::GroupId kOtherGroupId = |
| 5934 | TestGroupId("b", 443, ClientSocketPool::SocketType::kSsl); |
| 5935 | |
| 5936 | // Make a socket in each groups. |
| 5937 | ClientSocketHandle handle1, handle2, handle3; |
| 5938 | TestCompletionCallback callback; |
| 5939 | EXPECT_THAT( |
| 5940 | handle1.Init(kGroupId, params_, base::nullopt, DEFAULT_PRIORITY, |
| 5941 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5942 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5943 | pool_.get(), NetLogWithSource()), |
| 5944 | IsOk()); |
| 5945 | EXPECT_THAT( |
| 5946 | handle2.Init(kGroupIdPrivacy, params_, base::nullopt, DEFAULT_PRIORITY, |
| 5947 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5948 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5949 | pool_.get(), NetLogWithSource()), |
| 5950 | IsOk()); |
| 5951 | EXPECT_THAT( |
| 5952 | handle3.Init(kOtherGroupId, params_, base::nullopt, DEFAULT_PRIORITY, |
| 5953 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5954 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5955 | pool_.get(), NetLogWithSource()), |
| 5956 | IsOk()); |
| 5957 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId)); |
| 5958 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId)); |
| 5959 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupIdPrivacy)); |
| 5960 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupIdPrivacy)); |
| 5961 | ASSERT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 5962 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kOtherGroupId)); |
| 5963 | |
| 5964 | pool_->OnSSLConfigForServerChanged(HostPortPair("a", 443)); |
| 5965 | |
| 5966 | // Active sockets continue to be active. |
| 5967 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId)); |
| 5968 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId)); |
| 5969 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupIdPrivacy)); |
| 5970 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupIdPrivacy)); |
| 5971 | ASSERT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 5972 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kOtherGroupId)); |
| 5973 | |
| 5974 | // Closing them leaves kOtherGroupId alone, but kGroupId and kGroupIdPrivacy |
| 5975 | // are unusable. |
| 5976 | handle1.Reset(); |
| 5977 | handle2.Reset(); |
| 5978 | handle3.Reset(); |
| 5979 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 5980 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupId)); |
| 5981 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupIdPrivacy)); |
| 5982 | EXPECT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 5983 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(kOtherGroupId)); |
| 5984 | } |
| 5985 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 5986 | } // namespace |
| 5987 | |
| 5988 | } // namespace net |