[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" |
[email protected] | 277d594 | 2010-08-11 21:02:35 | [diff] [blame] | 40 | #include "net/http/http_response_headers.h" |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 41 | #include "net/http/http_response_info.h" |
eroman | 87c53d6 | 2015-04-02 06:51:07 | [diff] [blame] | 42 | #include "net/log/net_log.h" |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 43 | #include "net/log/net_log_event_type.h" |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 44 | #include "net/log/net_log_source.h" |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 45 | #include "net/log/net_log_source_type.h" |
mmenke | 16a7cbdd | 2015-04-24 23:00:56 | [diff] [blame] | 46 | #include "net/log/test_net_log.h" |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 47 | #include "net/log/test_net_log_util.h" |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 48 | #include "net/socket/client_socket_factory.h" |
| 49 | #include "net/socket/client_socket_handle.h" |
tfarina | 5dd13c2 | 2016-11-16 12:08:26 | [diff] [blame] | 50 | #include "net/socket/datagram_client_socket.h" |
tbansal | ca83c00 | 2016-04-28 20:56:28 | [diff] [blame] | 51 | #include "net/socket/socket_performance_watcher.h" |
Paul Jensen | 8d6f87ec | 2018-01-13 00:46:54 | [diff] [blame] | 52 | #include "net/socket/socket_tag.h" |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 53 | #include "net/socket/socket_test_util.h" |
[email protected] | 18ccfdb | 2013-08-15 00:13:44 | [diff] [blame] | 54 | #include "net/socket/ssl_client_socket.h" |
[email protected] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame] | 55 | #include "net/socket/stream_socket.h" |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 56 | #include "net/socket/transport_connect_job.h" |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 57 | #include "net/ssl/ssl_cert_request_info.h" |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 58 | #include "net/test/gtest_util.h" |
Gabriel Charette | c710874 | 2019-08-23 03:31:40 | [diff] [blame] | 59 | #include "net/test/test_with_task_environment.h" |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 60 | #include "net/traffic_annotation/network_traffic_annotation.h" |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 61 | #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 62 | #include "testing/gmock/include/gmock/gmock.h" |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 63 | #include "testing/gtest/include/gtest/gtest.h" |
| 64 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 65 | using net::test::IsError; |
| 66 | using net::test::IsOk; |
| 67 | |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 68 | using ::testing::Invoke; |
| 69 | using ::testing::Return; |
| 70 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 71 | namespace net { |
| 72 | |
| 73 | namespace { |
| 74 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 75 | const int kDefaultMaxSockets = 4; |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 76 | const int kDefaultMaxSocketsPerGroup = 2; |
Tarun Bansal | a763509 | 2019-02-20 10:00:59 | [diff] [blame] | 77 | constexpr base::TimeDelta kUnusedIdleSocketTimeout = |
| 78 | base::TimeDelta::FromSeconds(10); |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 79 | |
Matt Menke | bdf77780 | 2019-04-22 19:38:59 | [diff] [blame] | 80 | ClientSocketPool::GroupId TestGroupId( |
| 81 | const std::string& host, |
| 82 | int port = 80, |
| 83 | ClientSocketPool::SocketType socket_type = |
| 84 | ClientSocketPool::SocketType::kHttp, |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 85 | PrivacyMode privacy_mode = PrivacyMode::PRIVACY_MODE_DISABLED, |
| 86 | NetworkIsolationKey network_isolation_key = NetworkIsolationKey()) { |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 87 | bool disable_secure_dns = false; |
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, |
| 90 | disable_secure_dns); |
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(); |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 473 | SetSocket(std::unique_ptr<StreamSocket>()); |
[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) { |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 484 | SetSocket(std::make_unique<MockClientSocket>(net_log().net_log())); |
Bence Béky | bdbb0e7 | 2018-08-07 21:42:59 | [diff] [blame] | 485 | socket()->Connect(CompletionOnceCallback()); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 486 | } else if (cert_error) { |
| 487 | SetSocket(std::make_unique<MockClientSocket>(net_log().net_log())); |
| 488 | result = ERR_CERT_COMMON_NAME_INVALID; |
[email protected] | 6e713f0 | 2009-08-06 02:56:40 | [diff] [blame] | 489 | } else { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 490 | result = ERR_CONNECTION_FAILED; |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 491 | SetSocket(std::unique_ptr<StreamSocket>()); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 492 | } |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 493 | |
| 494 | if (was_async) |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 495 | NotifyDelegateOfCompletion(result); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 496 | return result; |
| 497 | } |
| 498 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 499 | void DoAdvanceAuthChallenge(int remaining_challenges, |
| 500 | bool succeed_after_last_challenge) { |
| 501 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 502 | FROM_HERE, |
| 503 | base::BindOnce(&TestConnectJob::InvokeNextProxyAuthCallback, |
| 504 | weak_factory_.GetWeakPtr(), remaining_challenges, |
| 505 | succeed_after_last_challenge)); |
| 506 | } |
| 507 | |
| 508 | void InvokeNextProxyAuthCallback(int remaining_challenges, |
| 509 | bool succeed_after_last_challenge) { |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 510 | set_load_state(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 511 | if (remaining_challenges == 0) { |
| 512 | DoConnect(succeed_after_last_challenge, true /* was_async */, |
| 513 | false /* cert_error */); |
| 514 | return; |
| 515 | } |
| 516 | |
| 517 | // Integration tests make sure HttpResponseInfo and HttpAuthController work. |
| 518 | // The auth tests here are just focused on ConnectJob bookkeeping. |
| 519 | HttpResponseInfo info; |
| 520 | NotifyDelegateOfProxyAuth( |
| 521 | info, nullptr /* http_auth_controller */, |
| 522 | base::BindOnce(&TestConnectJob::DoAdvanceAuthChallenge, |
| 523 | weak_factory_.GetWeakPtr(), remaining_challenges - 1, |
| 524 | succeed_after_last_challenge)); |
| 525 | } |
| 526 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 527 | bool waiting_success_; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 528 | const JobType job_type_; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 529 | MockClientSocketFactory* const client_socket_factory_; |
[email protected] | 4645135 | 2009-09-01 14:54:21 | [diff] [blame] | 530 | LoadState load_state_; |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 531 | bool has_established_connection_; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 532 | bool store_additional_error_state_; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 533 | |
Jeremy Roman | d54000b2 | 2019-07-08 18:40:16 | [diff] [blame] | 534 | base::WeakPtrFactory<TestConnectJob> weak_factory_{this}; |
[email protected] | d5492c5 | 2013-11-10 20:44:39 | [diff] [blame] | 535 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 536 | DISALLOW_COPY_AND_ASSIGN(TestConnectJob); |
| 537 | }; |
| 538 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 539 | class TestConnectJobFactory |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 540 | : public TransportClientSocketPool::ConnectJobFactory { |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 541 | public: |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 542 | TestConnectJobFactory(MockClientSocketFactory* client_socket_factory, |
| 543 | NetLog* net_log) |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 544 | : common_connect_job_params_( |
| 545 | nullptr /* client_socket_factory */, |
| 546 | nullptr /* host_resolver */, |
Matt Menke | b88837e | 2019-03-20 11:50:40 | [diff] [blame] | 547 | nullptr /* http_auth_cache */, |
| 548 | nullptr /* http_auth_handler_factory */, |
| 549 | nullptr /* spdy_session_pool */, |
Matt Menke | b5fb42b | 2019-03-22 17:26:13 | [diff] [blame] | 550 | nullptr /* quic_supported_versions */, |
Matt Menke | b88837e | 2019-03-20 11:50:40 | [diff] [blame] | 551 | nullptr /* quic_stream_factory */, |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 552 | nullptr /* proxy_delegate */, |
Matt Menke | d732ea4 | 2019-03-08 12:05:00 | [diff] [blame] | 553 | nullptr /* http_user_agent_settings */, |
David Benjamin | 24725be | 2019-07-24 20:57:18 | [diff] [blame] | 554 | nullptr /* ssl_client_context */, |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 555 | nullptr /* socket_performance_watcher_factory */, |
| 556 | nullptr /* network_quality_estimator */, |
| 557 | net_log, |
| 558 | nullptr /* websocket_endpoint_lock_manager */), |
| 559 | job_type_(TestConnectJob::kMockJob), |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 560 | job_types_(nullptr), |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 561 | client_socket_factory_(client_socket_factory) {} |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 562 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 563 | ~TestConnectJobFactory() override = default; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 564 | |
| 565 | void set_job_type(TestConnectJob::JobType job_type) { job_type_ = job_type; } |
| 566 | |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 567 | void set_job_types(std::list<TestConnectJob::JobType>* job_types) { |
| 568 | job_types_ = job_types; |
| 569 | CHECK(!job_types_->empty()); |
| 570 | } |
| 571 | |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 572 | void set_timeout_duration(base::TimeDelta timeout_duration) { |
| 573 | timeout_duration_ = timeout_duration; |
| 574 | } |
| 575 | |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 576 | // ConnectJobFactory implementation. |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 577 | |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 578 | std::unique_ptr<ConnectJob> NewConnectJob( |
Matt Menke | aafff54 | 2019-04-22 22:09:36 | [diff] [blame] | 579 | ClientSocketPool::GroupId group_id, |
| 580 | scoped_refptr<ClientSocketPool::SocketParams> socket_params, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 581 | const base::Optional<NetworkTrafficAnnotationTag>& proxy_annotation_tag, |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 582 | RequestPriority request_priority, |
| 583 | SocketTag socket_tag, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 584 | ConnectJob::Delegate* delegate) const override { |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 585 | EXPECT_TRUE(!job_types_ || !job_types_->empty()); |
| 586 | TestConnectJob::JobType job_type = job_type_; |
| 587 | if (job_types_ && !job_types_->empty()) { |
| 588 | job_type = job_types_->front(); |
| 589 | job_types_->pop_front(); |
| 590 | } |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 591 | return std::make_unique<TestConnectJob>( |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 592 | job_type, request_priority, socket_tag, timeout_duration_, |
| 593 | &common_connect_job_params_, delegate, client_socket_factory_); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | private: |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 597 | const CommonConnectJobParams common_connect_job_params_; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 598 | TestConnectJob::JobType job_type_; |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 599 | std::list<TestConnectJob::JobType>* job_types_; |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 600 | base::TimeDelta timeout_duration_; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 601 | MockClientSocketFactory* const client_socket_factory_; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 602 | |
| 603 | DISALLOW_COPY_AND_ASSIGN(TestConnectJobFactory); |
| 604 | }; |
| 605 | |
[email protected] | a937a06d | 2009-08-19 21:19:24 | [diff] [blame] | 606 | } // namespace |
| 607 | |
[email protected] | a937a06d | 2009-08-19 21:19:24 | [diff] [blame] | 608 | namespace { |
| 609 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 610 | void MockClientSocketFactory::SignalJobs() { |
jdoerrie | 22a91d8b9 | 2018-10-05 08:43:26 | [diff] [blame] | 611 | for (auto it = waiting_jobs_.begin(); it != waiting_jobs_.end(); ++it) { |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 612 | (*it)->Signal(); |
| 613 | } |
| 614 | waiting_jobs_.clear(); |
| 615 | } |
| 616 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 617 | void MockClientSocketFactory::SignalJob(size_t job) { |
| 618 | ASSERT_LT(job, waiting_jobs_.size()); |
| 619 | waiting_jobs_[job]->Signal(); |
| 620 | waiting_jobs_.erase(waiting_jobs_.begin() + job); |
| 621 | } |
| 622 | |
| 623 | void MockClientSocketFactory::SetJobLoadState(size_t job, |
| 624 | LoadState load_state) { |
| 625 | ASSERT_LT(job, waiting_jobs_.size()); |
| 626 | waiting_jobs_[job]->set_load_state(load_state); |
| 627 | } |
| 628 | |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 629 | void MockClientSocketFactory::SetJobHasEstablishedConnection(size_t job) { |
| 630 | ASSERT_LT(job, waiting_jobs_.size()); |
| 631 | waiting_jobs_[job]->set_has_established_connection(); |
| 632 | } |
| 633 | |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 634 | class ClientSocketPoolBaseTest : public TestWithTaskEnvironment { |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 635 | protected: |
Alex Clarke | 0def209 | 2018-12-10 12:01:45 | [diff] [blame] | 636 | ClientSocketPoolBaseTest() |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 637 | : TestWithTaskEnvironment( |
| 638 | base::test::TaskEnvironment::TimeSource::MOCK_TIME), |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 639 | params_(ClientSocketPool::SocketParams::CreateForHttpForTesting()) { |
[email protected] | 636b825 | 2011-04-08 19:56:54 | [diff] [blame] | 640 | connect_backup_jobs_enabled_ = |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 641 | TransportClientSocketPool::connect_backup_jobs_enabled(); |
| 642 | TransportClientSocketPool::set_connect_backup_jobs_enabled(true); |
[email protected] | 636b825 | 2011-04-08 19:56:54 | [diff] [blame] | 643 | } |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 644 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 645 | ~ClientSocketPoolBaseTest() override { |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 646 | TransportClientSocketPool::set_connect_backup_jobs_enabled( |
[email protected] | 636b825 | 2011-04-08 19:56:54 | [diff] [blame] | 647 | connect_backup_jobs_enabled_); |
| 648 | } |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 649 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 650 | void CreatePool(int max_sockets, |
| 651 | int max_sockets_per_group, |
| 652 | bool enable_backup_connect_jobs = false) { |
Tarun Bansal | a763509 | 2019-02-20 10:00:59 | [diff] [blame] | 653 | CreatePoolWithIdleTimeouts(max_sockets, max_sockets_per_group, |
| 654 | kUnusedIdleSocketTimeout, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 655 | ClientSocketPool::used_idle_socket_timeout(), |
| 656 | enable_backup_connect_jobs); |
[email protected] | 9bf28db | 2009-08-29 01:35:16 | [diff] [blame] | 657 | } |
| 658 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 659 | void CreatePoolWithIdleTimeouts( |
| 660 | int max_sockets, |
| 661 | int max_sockets_per_group, |
| 662 | base::TimeDelta unused_idle_socket_timeout, |
| 663 | base::TimeDelta used_idle_socket_timeout, |
| 664 | bool enable_backup_connect_jobs = false, |
| 665 | ProxyServer proxy_server = ProxyServer::Direct()) { |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 666 | DCHECK(!pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 667 | std::unique_ptr<TestConnectJobFactory> connect_job_factory = |
| 668 | std::make_unique<TestConnectJobFactory>(&client_socket_factory_, |
| 669 | &net_log_); |
| 670 | connect_job_factory_ = connect_job_factory.get(); |
| 671 | pool_ = TransportClientSocketPool::CreateForTesting( |
| 672 | max_sockets, max_sockets_per_group, unused_idle_socket_timeout, |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 673 | used_idle_socket_timeout, proxy_server, std::move(connect_job_factory), |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 674 | nullptr /* ssl_config_service */, enable_backup_connect_jobs); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 675 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 676 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 677 | int StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 678 | const ClientSocketPool::GroupId& group_id, |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 679 | RequestPriority priority, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 680 | ClientSocketPool::RespectLimits respect_limits) { |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 681 | return test_base_.StartRequestUsingPool(pool_.get(), group_id, priority, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 682 | respect_limits, params_); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 683 | } |
| 684 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 685 | int StartRequest(const ClientSocketPool::GroupId& group_id, |
| 686 | RequestPriority priority) { |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 687 | return StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 688 | group_id, priority, ClientSocketPool::RespectLimits::ENABLED); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 689 | } |
| 690 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 691 | int GetOrderOfRequest(size_t index) const { |
| 692 | return test_base_.GetOrderOfRequest(index); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 693 | } |
| 694 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 695 | bool ReleaseOneConnection(ClientSocketPoolTest::KeepAlive keep_alive) { |
| 696 | return test_base_.ReleaseOneConnection(keep_alive); |
| 697 | } |
| 698 | |
| 699 | void ReleaseAllConnections(ClientSocketPoolTest::KeepAlive keep_alive) { |
| 700 | test_base_.ReleaseAllConnections(keep_alive); |
| 701 | } |
| 702 | |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 703 | // Expects a single NetLogEventType::SOCKET_POOL_CLOSING_SOCKET in |net_log_|. |
| 704 | // It should be logged for the provided source and have the indicated reason. |
| 705 | void ExpectSocketClosedWithReason(NetLogSource expected_source, |
| 706 | const char* expected_reason) { |
| 707 | auto entries = net_log_.GetEntriesForSourceWithType( |
| 708 | expected_source, NetLogEventType::SOCKET_POOL_CLOSING_SOCKET, |
| 709 | NetLogEventPhase::NONE); |
| 710 | ASSERT_EQ(1u, entries.size()); |
| 711 | ASSERT_TRUE(entries[0].HasParams()); |
| 712 | ASSERT_TRUE(entries[0].params.is_dict()); |
| 713 | const std::string* reason = entries[0].params.FindStringKey("reason"); |
| 714 | ASSERT_TRUE(reason); |
| 715 | EXPECT_EQ(expected_reason, *reason); |
| 716 | } |
| 717 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 718 | TestSocketRequest* request(int i) { return test_base_.request(i); } |
| 719 | size_t requests_size() const { return test_base_.requests_size(); } |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 720 | std::vector<std::unique_ptr<TestSocketRequest>>* requests() { |
olli.raula | 9d66b7d | 2015-11-23 08:30:42 | [diff] [blame] | 721 | return test_base_.requests(); |
| 722 | } |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 723 | // Only counts the requests that get sockets asynchronously; |
| 724 | // synchronous completions are not registered by this count. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 725 | size_t completion_count() const { return test_base_.completion_count(); } |
| 726 | |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 727 | RecordingTestNetLog net_log_; |
[email protected] | 636b825 | 2011-04-08 19:56:54 | [diff] [blame] | 728 | bool connect_backup_jobs_enabled_; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 729 | MockClientSocketFactory client_socket_factory_; |
[email protected] | 17a0c6c | 2009-08-04 00:07:04 | [diff] [blame] | 730 | TestConnectJobFactory* connect_job_factory_; |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 731 | // These parameters are never actually used to create a TransportConnectJob. |
Matt Menke | 84d11e56 | 2019-03-27 00:11:19 | [diff] [blame] | 732 | scoped_refptr<ClientSocketPool::SocketParams> params_; |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 733 | std::unique_ptr<TransportClientSocketPool> pool_; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 734 | ClientSocketPoolTest test_base_; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 735 | }; |
| 736 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 737 | TEST_F(ClientSocketPoolBaseTest, BasicSynchronous) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 738 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 739 | |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 740 | TestCompletionCallback callback; |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 741 | ClientSocketHandle handle; |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 742 | RecordingBoundTestNetLog log; |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 743 | TestLoadTimingInfoNotConnected(handle); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 744 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 745 | EXPECT_EQ(OK, handle.Init( |
| 746 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 747 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 748 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 749 | pool_.get(), log.bound())); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 750 | EXPECT_TRUE(handle.is_initialized()); |
| 751 | EXPECT_TRUE(handle.socket()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 752 | TestLoadTimingInfoConnectedNotReused(handle); |
| 753 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 754 | handle.Reset(); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 755 | TestLoadTimingInfoNotConnected(handle); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 756 | |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 757 | auto entries = log.GetEntries(); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 758 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 759 | EXPECT_EQ(5u, entries.size()); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 760 | EXPECT_TRUE(LogContainsEvent( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 761 | entries, 0, NetLogEventType::TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 762 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 763 | EXPECT_TRUE(LogContainsBeginEvent(entries, 1, NetLogEventType::SOCKET_POOL)); |
| 764 | EXPECT_TRUE(LogContainsEvent( |
| 765 | entries, 2, NetLogEventType::SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
| 766 | NetLogEventPhase::NONE)); |
| 767 | EXPECT_TRUE(LogContainsEvent(entries, 3, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 768 | NetLogEventType::SOCKET_POOL_BOUND_TO_SOCKET, |
| 769 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 770 | EXPECT_TRUE(LogContainsEndEvent(entries, 4, NetLogEventType::SOCKET_POOL)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 771 | } |
| 772 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 773 | TEST_F(ClientSocketPoolBaseTest, InitConnectionFailure) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 774 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 775 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 776 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 777 | RecordingBoundTestNetLog log; |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 778 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 779 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 780 | TestCompletionCallback callback; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 781 | // Set the additional error state members to ensure that they get cleared. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 782 | handle.set_is_ssl_error(true); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 783 | handle.set_ssl_cert_request_info(base::MakeRefCounted<SSLCertRequestInfo>()); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 784 | EXPECT_EQ( |
| 785 | ERR_CONNECTION_FAILED, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 786 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 787 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 788 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 789 | pool_.get(), log.bound())); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 790 | EXPECT_FALSE(handle.socket()); |
| 791 | EXPECT_FALSE(handle.is_ssl_error()); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 792 | EXPECT_FALSE(handle.ssl_cert_request_info()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 793 | TestLoadTimingInfoNotConnected(handle); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 794 | |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 795 | auto entries = log.GetEntries(); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 796 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 797 | EXPECT_EQ(4u, entries.size()); |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 798 | EXPECT_TRUE(LogContainsEvent( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 799 | entries, 0, NetLogEventType::TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 800 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 801 | EXPECT_TRUE(LogContainsBeginEvent(entries, 1, NetLogEventType::SOCKET_POOL)); |
| 802 | EXPECT_TRUE(LogContainsEvent( |
| 803 | entries, 2, NetLogEventType::SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
| 804 | NetLogEventPhase::NONE)); |
| 805 | EXPECT_TRUE(LogContainsEndEvent(entries, 3, NetLogEventType::SOCKET_POOL)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 806 | } |
| 807 | |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 808 | // Test releasing an open socket into the socket pool, telling the socket pool |
| 809 | // to close the socket. |
| 810 | TEST_F(ClientSocketPoolBaseTest, ReleaseAndCloseConnection) { |
| 811 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 812 | |
| 813 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 814 | ASSERT_TRUE(request(0)->handle()->socket()); |
| 815 | net::NetLogSource source = request(0)->handle()->socket()->NetLog().source(); |
| 816 | ReleaseOneConnection(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 817 | |
| 818 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 819 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 820 | |
| 821 | ExpectSocketClosedWithReason( |
| 822 | source, TransportClientSocketPool::kClosedConnectionReturnedToPool); |
| 823 | } |
| 824 | |
| 825 | TEST_F(ClientSocketPoolBaseTest, SocketWithUnreadDataReturnedToPool) { |
| 826 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 827 | connect_job_factory_->set_job_type(TestConnectJob::kMockUnreadDataJob); |
| 828 | |
| 829 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 830 | ASSERT_TRUE(request(0)->handle()->socket()); |
| 831 | net::NetLogSource source = request(0)->handle()->socket()->NetLog().source(); |
| 832 | EXPECT_TRUE(request(0)->handle()->socket()->IsConnected()); |
| 833 | EXPECT_FALSE(request(0)->handle()->socket()->IsConnectedAndIdle()); |
| 834 | ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE); |
| 835 | |
| 836 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 837 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 838 | |
| 839 | ExpectSocketClosedWithReason( |
| 840 | source, TransportClientSocketPool::kDataReceivedUnexpectedly); |
| 841 | } |
| 842 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 843 | // Make sure different groups do not share sockets. |
| 844 | TEST_F(ClientSocketPoolBaseTest, GroupSeparation) { |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 845 | base::test::ScopedFeatureList feature_list; |
| 846 | feature_list.InitAndEnableFeature( |
| 847 | features::kPartitionConnectionsByNetworkIsolationKey); |
| 848 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 849 | CreatePool(1000 /* max_sockets */, 2 /* max_sockets_per_group */); |
| 850 | |
| 851 | const HostPortPair kHostPortPairs[] = { |
| 852 | {"a", 80}, |
| 853 | {"a", 443}, |
| 854 | {"b", 80}, |
| 855 | }; |
| 856 | |
| 857 | const ClientSocketPool::SocketType kSocketTypes[] = { |
| 858 | ClientSocketPool::SocketType::kHttp, |
| 859 | ClientSocketPool::SocketType::kSsl, |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 860 | }; |
| 861 | |
Matt Menke | bdf77780 | 2019-04-22 19:38:59 | [diff] [blame] | 862 | const PrivacyMode kPrivacyModes[] = {PrivacyMode::PRIVACY_MODE_DISABLED, |
| 863 | PrivacyMode::PRIVACY_MODE_ENABLED}; |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 864 | |
Matt Menke | 4807a9a | 2020-11-21 00:14:41 | [diff] [blame] | 865 | const SchemefulSite kSiteA(GURL("http://a.test/")); |
| 866 | const SchemefulSite kSiteB(GURL("http://b.test/")); |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 867 | const NetworkIsolationKey kNetworkIsolationKeys[] = { |
Matt Menke | 4807a9a | 2020-11-21 00:14:41 | [diff] [blame] | 868 | NetworkIsolationKey(kSiteA, kSiteA), |
| 869 | NetworkIsolationKey(kSiteB, kSiteB), |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 870 | }; |
| 871 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 872 | const bool kDisableSecureDnsValues[] = {false, true}; |
| 873 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 874 | int total_idle_sockets = 0; |
| 875 | |
| 876 | // Walk through each GroupId, making sure that requesting a socket for one |
| 877 | // group does not return a previously connected socket for another group. |
| 878 | for (const auto& host_port_pair : kHostPortPairs) { |
| 879 | SCOPED_TRACE(host_port_pair.ToString()); |
| 880 | for (const auto& socket_type : kSocketTypes) { |
| 881 | SCOPED_TRACE(static_cast<int>(socket_type)); |
| 882 | for (const auto& privacy_mode : kPrivacyModes) { |
| 883 | SCOPED_TRACE(privacy_mode); |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 884 | for (const auto& network_isolation_key : kNetworkIsolationKeys) { |
| 885 | SCOPED_TRACE(network_isolation_key.ToString()); |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 886 | for (const auto& disable_secure_dns : kDisableSecureDnsValues) { |
| 887 | SCOPED_TRACE(disable_secure_dns); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 888 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 889 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 890 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 891 | ClientSocketPool::GroupId group_id( |
| 892 | host_port_pair, socket_type, privacy_mode, |
| 893 | network_isolation_key, disable_secure_dns); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 894 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 895 | EXPECT_FALSE(pool_->HasGroupForTesting(group_id)); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 896 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 897 | TestCompletionCallback callback; |
| 898 | ClientSocketHandle handle; |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 899 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 900 | // Since the group is empty, requesting a socket should not complete |
| 901 | // synchronously. |
| 902 | EXPECT_THAT(handle.Init(group_id, params_, base::nullopt, |
| 903 | DEFAULT_PRIORITY, SocketTag(), |
| 904 | ClientSocketPool::RespectLimits::ENABLED, |
| 905 | callback.callback(), |
| 906 | ClientSocketPool::ProxyAuthCallback(), |
| 907 | pool_.get(), NetLogWithSource()), |
| 908 | IsError(ERR_IO_PENDING)); |
| 909 | EXPECT_TRUE(pool_->HasGroupForTesting(group_id)); |
| 910 | EXPECT_EQ(total_idle_sockets, pool_->IdleSocketCount()); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 911 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 912 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 913 | EXPECT_TRUE(handle.socket()); |
| 914 | EXPECT_TRUE(pool_->HasGroupForTesting(group_id)); |
| 915 | EXPECT_EQ(total_idle_sockets, pool_->IdleSocketCount()); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 916 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 917 | // Return socket to pool. |
| 918 | handle.Reset(); |
| 919 | EXPECT_EQ(total_idle_sockets + 1, pool_->IdleSocketCount()); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 920 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 921 | // Requesting a socket again should return the same socket as |
| 922 | // before, so should complete synchronously. |
| 923 | EXPECT_THAT(handle.Init(group_id, params_, base::nullopt, |
| 924 | DEFAULT_PRIORITY, SocketTag(), |
| 925 | ClientSocketPool::RespectLimits::ENABLED, |
| 926 | callback.callback(), |
| 927 | ClientSocketPool::ProxyAuthCallback(), |
| 928 | pool_.get(), NetLogWithSource()), |
| 929 | IsOk()); |
| 930 | EXPECT_TRUE(handle.socket()); |
| 931 | EXPECT_EQ(total_idle_sockets, pool_->IdleSocketCount()); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 932 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 933 | // Return socket to pool again. |
| 934 | handle.Reset(); |
| 935 | EXPECT_EQ(total_idle_sockets + 1, pool_->IdleSocketCount()); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 936 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 937 | ++total_idle_sockets; |
| 938 | } |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 939 | } |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 940 | } |
| 941 | } |
| 942 | } |
| 943 | } |
| 944 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 945 | TEST_F(ClientSocketPoolBaseTest, TotalLimit) { |
| 946 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 947 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 948 | // TODO(eroman): Check that the NetLog contains this event. |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 949 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 950 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 951 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
| 952 | EXPECT_THAT(StartRequest(TestGroupId("c"), DEFAULT_PRIORITY), IsOk()); |
| 953 | EXPECT_THAT(StartRequest(TestGroupId("d"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 954 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 955 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 956 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 957 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 958 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 959 | EXPECT_THAT(StartRequest(TestGroupId("e"), DEFAULT_PRIORITY), |
| 960 | IsError(ERR_IO_PENDING)); |
| 961 | EXPECT_THAT(StartRequest(TestGroupId("f"), DEFAULT_PRIORITY), |
| 962 | IsError(ERR_IO_PENDING)); |
| 963 | EXPECT_THAT(StartRequest(TestGroupId("g"), DEFAULT_PRIORITY), |
| 964 | IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 965 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 966 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 967 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 968 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 969 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 970 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 971 | |
| 972 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 973 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 974 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 975 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 976 | EXPECT_EQ(5, GetOrderOfRequest(5)); |
| 977 | EXPECT_EQ(6, GetOrderOfRequest(6)); |
| 978 | EXPECT_EQ(7, GetOrderOfRequest(7)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 979 | |
| 980 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 981 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(8)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 982 | } |
| 983 | |
| 984 | TEST_F(ClientSocketPoolBaseTest, TotalLimitReachedNewGroup) { |
| 985 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 986 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 987 | // TODO(eroman): Check that the NetLog contains this event. |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 988 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 989 | // Reach all limits: max total sockets, and max sockets per group. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 990 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 991 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 992 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
| 993 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 994 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 995 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 996 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 997 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 998 | |
| 999 | // Now create a new group and verify that we don't starve it. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1000 | EXPECT_THAT(StartRequest(TestGroupId("c"), DEFAULT_PRIORITY), |
| 1001 | IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1002 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1003 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1004 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1005 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1006 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1007 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1008 | |
| 1009 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1010 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1011 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 1012 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 1013 | EXPECT_EQ(5, GetOrderOfRequest(5)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1014 | |
| 1015 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1016 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(6)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1017 | } |
| 1018 | |
| 1019 | TEST_F(ClientSocketPoolBaseTest, TotalLimitRespectsPriority) { |
| 1020 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1021 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1022 | EXPECT_THAT(StartRequest(TestGroupId("b"), LOWEST), IsOk()); |
| 1023 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsOk()); |
| 1024 | EXPECT_THAT(StartRequest(TestGroupId("b"), HIGHEST), IsOk()); |
| 1025 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1026 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1027 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1028 | client_socket_factory_.allocation_count()); |
| 1029 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1030 | EXPECT_THAT(StartRequest(TestGroupId("c"), LOWEST), IsError(ERR_IO_PENDING)); |
| 1031 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1032 | EXPECT_THAT(StartRequest(TestGroupId("b"), HIGHEST), IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1033 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1034 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1035 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1036 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1037 | |
| 1038 | // First 4 requests don't have to wait, and finish in order. |
| 1039 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1040 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1041 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 1042 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 1043 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1044 | // Request ("b", HIGHEST) has the highest priority, then (TestGroupId("a"), |
| 1045 | // MEDIUM), and then ("c", LOWEST). |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1046 | EXPECT_EQ(7, GetOrderOfRequest(5)); |
| 1047 | EXPECT_EQ(6, GetOrderOfRequest(6)); |
| 1048 | EXPECT_EQ(5, GetOrderOfRequest(7)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1049 | |
| 1050 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1051 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(9)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1052 | } |
| 1053 | |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1054 | // Test reprioritizing a request before completion doesn't interfere with |
| 1055 | // its completion. |
| 1056 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeOne) { |
| 1057 | CreatePool(kDefaultMaxSockets, 1); |
| 1058 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1059 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1060 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1061 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1062 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1063 | |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 1064 | request(1)->handle()->SetPriority(HIGHEST); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1065 | |
| 1066 | ReleaseOneConnection(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1067 | |
| 1068 | EXPECT_TRUE(request(1)->handle()->socket()); |
| 1069 | } |
| 1070 | |
| 1071 | // Reprioritize a request up past another one and make sure that changes the |
| 1072 | // completion order. |
| 1073 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeUpReorder) { |
| 1074 | CreatePool(kDefaultMaxSockets, 1); |
| 1075 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1076 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1077 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1078 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1079 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1080 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1081 | EXPECT_FALSE(request(2)->handle()->socket()); |
| 1082 | |
| 1083 | request(2)->handle()->SetPriority(HIGHEST); |
| 1084 | |
| 1085 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1086 | |
| 1087 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1088 | EXPECT_EQ(3, GetOrderOfRequest(2)); |
| 1089 | EXPECT_EQ(2, GetOrderOfRequest(3)); |
| 1090 | } |
| 1091 | |
| 1092 | // Reprioritize a request without changing relative priorities and check |
| 1093 | // that the order doesn't change. |
| 1094 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeUpNoReorder) { |
| 1095 | CreatePool(kDefaultMaxSockets, 1); |
| 1096 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1097 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1098 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1099 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1100 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1101 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1102 | EXPECT_FALSE(request(2)->handle()->socket()); |
| 1103 | |
| 1104 | request(2)->handle()->SetPriority(MEDIUM); |
| 1105 | |
| 1106 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1107 | |
| 1108 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1109 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1110 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 1111 | } |
| 1112 | |
| 1113 | // Reprioritize a request past down another one and make sure that changes the |
| 1114 | // completion order. |
| 1115 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeDownReorder) { |
| 1116 | CreatePool(kDefaultMaxSockets, 1); |
| 1117 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1118 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1119 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1120 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1121 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1122 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1123 | EXPECT_FALSE(request(2)->handle()->socket()); |
| 1124 | |
| 1125 | request(1)->handle()->SetPriority(LOW); |
| 1126 | |
| 1127 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1128 | |
| 1129 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1130 | EXPECT_EQ(3, GetOrderOfRequest(2)); |
| 1131 | EXPECT_EQ(2, GetOrderOfRequest(3)); |
| 1132 | } |
| 1133 | |
| 1134 | // Reprioritize a request to the same level as another and confirm it is |
| 1135 | // put after the old request. |
| 1136 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeResetFIFO) { |
| 1137 | CreatePool(kDefaultMaxSockets, 1); |
| 1138 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1139 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1140 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1141 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1142 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1143 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1144 | EXPECT_FALSE(request(2)->handle()->socket()); |
| 1145 | |
| 1146 | request(1)->handle()->SetPriority(MEDIUM); |
| 1147 | |
| 1148 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1149 | |
| 1150 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1151 | EXPECT_EQ(3, GetOrderOfRequest(2)); |
| 1152 | EXPECT_EQ(2, GetOrderOfRequest(3)); |
| 1153 | } |
| 1154 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1155 | TEST_F(ClientSocketPoolBaseTest, TotalLimitRespectsGroupLimit) { |
| 1156 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1157 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1158 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsOk()); |
| 1159 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsOk()); |
| 1160 | EXPECT_THAT(StartRequest(TestGroupId("b"), HIGHEST), IsOk()); |
| 1161 | EXPECT_THAT(StartRequest(TestGroupId("b"), MEDIUM), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1162 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1163 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1164 | client_socket_factory_.allocation_count()); |
| 1165 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1166 | EXPECT_THAT(StartRequest(TestGroupId("c"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1167 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 1168 | EXPECT_THAT(StartRequest(TestGroupId("b"), HIGHEST), IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1169 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1170 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1171 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1172 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1173 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1174 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1175 | |
| 1176 | // First 4 requests don't have to wait, and finish in order. |
| 1177 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1178 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1179 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 1180 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 1181 | |
| 1182 | // Request ("b", 7) has the highest priority, but we can't make new socket for |
| 1183 | // group "b", because it has reached the per-group limit. Then we make |
| 1184 | // socket for ("c", 6), because it has higher priority than ("a", 4), |
| 1185 | // and we still can't make a socket for group "b". |
| 1186 | EXPECT_EQ(5, GetOrderOfRequest(5)); |
| 1187 | EXPECT_EQ(6, GetOrderOfRequest(6)); |
| 1188 | EXPECT_EQ(7, GetOrderOfRequest(7)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1189 | |
| 1190 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1191 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(8)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1192 | } |
| 1193 | |
| 1194 | // Make sure that we count connecting sockets against the total limit. |
| 1195 | TEST_F(ClientSocketPoolBaseTest, TotalLimitCountsConnectingSockets) { |
| 1196 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1197 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1198 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1199 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
| 1200 | EXPECT_THAT(StartRequest(TestGroupId("c"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1201 | |
| 1202 | // Create one asynchronous request. |
| 1203 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1204 | EXPECT_THAT(StartRequest(TestGroupId("d"), DEFAULT_PRIORITY), |
| 1205 | IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1206 | |
[email protected] | 6b17538 | 2009-10-13 06:47:47 | [diff] [blame] | 1207 | // We post all of our delayed tasks with a 2ms delay. I.e. they don't |
| 1208 | // actually become pending until 2ms after they have been created. In order |
| 1209 | // to flush all tasks, we need to wait so that we know there are no |
| 1210 | // soon-to-be-pending tasks waiting. |
Alex Clarke | 0def209 | 2018-12-10 12:01:45 | [diff] [blame] | 1211 | FastForwardBy(base::TimeDelta::FromMilliseconds(10)); |
[email protected] | 6b17538 | 2009-10-13 06:47:47 | [diff] [blame] | 1212 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1213 | // The next synchronous request should wait for its turn. |
| 1214 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1215 | EXPECT_THAT(StartRequest(TestGroupId("e"), DEFAULT_PRIORITY), |
| 1216 | IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1217 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1218 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1219 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1220 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1221 | client_socket_factory_.allocation_count()); |
| 1222 | |
| 1223 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1224 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1225 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 1226 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1227 | EXPECT_EQ(5, GetOrderOfRequest(5)); |
| 1228 | |
| 1229 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1230 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(6)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1231 | } |
| 1232 | |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1233 | TEST_F(ClientSocketPoolBaseTest, CorrectlyCountStalledGroups) { |
| 1234 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 1235 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 1236 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1237 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1238 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1239 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1240 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1241 | |
| 1242 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 1243 | |
| 1244 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1245 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1246 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), |
| 1247 | IsError(ERR_IO_PENDING)); |
| 1248 | EXPECT_THAT(StartRequest(TestGroupId("c"), DEFAULT_PRIORITY), |
| 1249 | IsError(ERR_IO_PENDING)); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1250 | |
| 1251 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1252 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1253 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1254 | EXPECT_EQ(kDefaultMaxSockets + 1, client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1255 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1256 | EXPECT_EQ(kDefaultMaxSockets + 2, client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1257 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
| 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()); |
| 1260 | } |
| 1261 | |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 1262 | TEST_F(ClientSocketPoolBaseTest, StallAndThenCancelAndTriggerAvailableSocket) { |
| 1263 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 1264 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1265 | |
| 1266 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1267 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1268 | EXPECT_EQ( |
| 1269 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1270 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1271 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1272 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1273 | pool_.get(), NetLogWithSource())); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 1274 | |
| 1275 | ClientSocketHandle handles[4]; |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 1276 | for (size_t i = 0; i < base::size(handles); ++i) { |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1277 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1278 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1279 | handles[i].Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1280 | TestGroupId("b"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1281 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1282 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1283 | pool_.get(), NetLogWithSource())); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 1284 | } |
| 1285 | |
| 1286 | // One will be stalled, cancel all the handles now. |
| 1287 | // This should hit the OnAvailableSocketSlot() code where we previously had |
| 1288 | // stalled groups, but no longer have any. |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 1289 | for (size_t i = 0; i < base::size(handles); ++i) |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 1290 | handles[i].Reset(); |
| 1291 | } |
| 1292 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1293 | TEST_F(ClientSocketPoolBaseTest, CancelStalledSocketAtSocketLimit) { |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1294 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1295 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 1296 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1297 | { |
| 1298 | ClientSocketHandle handles[kDefaultMaxSockets]; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1299 | TestCompletionCallback callbacks[kDefaultMaxSockets]; |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1300 | for (int i = 0; i < kDefaultMaxSockets; ++i) { |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1301 | EXPECT_EQ(OK, |
| 1302 | handles[i].Init(TestGroupId(base::NumberToString(i)), params_, |
| 1303 | base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
| 1304 | ClientSocketPool::RespectLimits::ENABLED, |
| 1305 | callbacks[i].callback(), |
| 1306 | ClientSocketPool::ProxyAuthCallback(), |
| 1307 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1308 | } |
| 1309 | |
| 1310 | // Force a stalled group. |
| 1311 | ClientSocketHandle stalled_handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1312 | TestCompletionCallback callback; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1313 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1314 | stalled_handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1315 | TestGroupId("foo"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1316 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1317 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1318 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1319 | |
| 1320 | // Cancel the stalled request. |
| 1321 | stalled_handle.Reset(); |
| 1322 | |
| 1323 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1324 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 1325 | |
| 1326 | // Dropping out of scope will close all handles and return them to idle. |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1327 | } |
| 1328 | |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1329 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1330 | EXPECT_EQ(kDefaultMaxSockets, pool_->IdleSocketCount()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1331 | } |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1332 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1333 | TEST_F(ClientSocketPoolBaseTest, CancelPendingSocketAtSocketLimit) { |
| 1334 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1335 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 1336 | |
| 1337 | { |
| 1338 | ClientSocketHandle handles[kDefaultMaxSockets]; |
| 1339 | for (int i = 0; i < kDefaultMaxSockets; ++i) { |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1340 | TestCompletionCallback callback; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1341 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1342 | handles[i].Init(TestGroupId(base::NumberToString(i)), params_, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1343 | base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1344 | ClientSocketPool::RespectLimits::ENABLED, |
| 1345 | callback.callback(), |
| 1346 | ClientSocketPool::ProxyAuthCallback(), |
| 1347 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1348 | } |
| 1349 | |
| 1350 | // Force a stalled group. |
| 1351 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1352 | ClientSocketHandle stalled_handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1353 | TestCompletionCallback callback; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1354 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1355 | stalled_handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1356 | TestGroupId("foo"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1357 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1358 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1359 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1360 | |
| 1361 | // Since it is stalled, it should have no connect jobs. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1362 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("foo"))); |
| 1363 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 1364 | TestGroupId("foo"))); |
| 1365 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroupForTesting( |
| 1366 | TestGroupId("foo"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1367 | |
| 1368 | // Cancel the stalled request. |
| 1369 | handles[0].Reset(); |
| 1370 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1371 | // Now we should have a connect job. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1372 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("foo"))); |
| 1373 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 1374 | TestGroupId("foo"))); |
| 1375 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroupForTesting( |
| 1376 | TestGroupId("foo"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1377 | |
| 1378 | // The stalled socket should connect. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1379 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1380 | |
| 1381 | EXPECT_EQ(kDefaultMaxSockets + 1, |
| 1382 | client_socket_factory_.allocation_count()); |
| 1383 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1384 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("foo"))); |
| 1385 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 1386 | TestGroupId("foo"))); |
| 1387 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroupForTesting( |
| 1388 | TestGroupId("foo"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1389 | |
| 1390 | // Dropping out of scope will close all handles and return them to idle. |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1391 | } |
| 1392 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1393 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 1394 | } |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1395 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1396 | TEST_F(ClientSocketPoolBaseTest, WaitForStalledSocketAtSocketLimit) { |
| 1397 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1398 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1399 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1400 | ClientSocketHandle stalled_handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1401 | TestCompletionCallback callback; |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1402 | { |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 1403 | EXPECT_FALSE(pool_->IsStalled()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1404 | ClientSocketHandle handles[kDefaultMaxSockets]; |
| 1405 | for (int i = 0; i < kDefaultMaxSockets; ++i) { |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1406 | TestCompletionCallback callback; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1407 | EXPECT_EQ( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1408 | OK, handles[i].Init( |
| 1409 | TestGroupId(base::StringPrintf("Take 2: %d", i)), params_, |
| 1410 | base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
| 1411 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1412 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 1413 | NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1414 | } |
| 1415 | |
| 1416 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1417 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 1418 | EXPECT_FALSE(pool_->IsStalled()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1419 | |
| 1420 | // Now we will hit the socket limit. |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1421 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1422 | stalled_handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1423 | TestGroupId("foo"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1424 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1425 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1426 | pool_.get(), NetLogWithSource())); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 1427 | EXPECT_TRUE(pool_->IsStalled()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1428 | |
| 1429 | // Dropping out of scope will close all handles and return them to idle. |
| 1430 | } |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1431 | |
| 1432 | // But if we wait for it, the released idle sockets will be closed in |
| 1433 | // preference of the waiting request. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1434 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1435 | |
| 1436 | EXPECT_EQ(kDefaultMaxSockets + 1, client_socket_factory_.allocation_count()); |
| 1437 | EXPECT_EQ(3, pool_->IdleSocketCount()); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1438 | } |
| 1439 | |
| 1440 | // Regression test for http://crbug.com/40952. |
| 1441 | TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketAtSocketLimitDeleteGroup) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1442 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 1443 | true /* enable_backup_connect_jobs */); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1444 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 1445 | |
| 1446 | for (int i = 0; i < kDefaultMaxSockets; ++i) { |
| 1447 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1448 | TestCompletionCallback callback; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1449 | EXPECT_EQ(OK, handle.Init(TestGroupId(base::NumberToString(i)), params_, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1450 | base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1451 | ClientSocketPool::RespectLimits::ENABLED, |
| 1452 | callback.callback(), |
| 1453 | ClientSocketPool::ProxyAuthCallback(), |
| 1454 | pool_.get(), NetLogWithSource())); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1455 | } |
| 1456 | |
| 1457 | // Flush all the DoReleaseSocket tasks. |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 1458 | base::RunLoop().RunUntilIdle(); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1459 | |
| 1460 | // Stall a group. Set a pending job so it'll trigger a backup job if we don't |
| 1461 | // reuse a socket. |
| 1462 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1463 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1464 | TestCompletionCallback callback; |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1465 | |
| 1466 | // "0" is special here, since it should be the first entry in the sorted map, |
| 1467 | // which is the one which we would close an idle socket for. We shouldn't |
| 1468 | // close an idle socket though, since we should reuse the idle socket. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1469 | EXPECT_EQ(OK, handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1470 | TestGroupId("0"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1471 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1472 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1473 | pool_.get(), NetLogWithSource())); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1474 | |
| 1475 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1476 | EXPECT_EQ(kDefaultMaxSockets - 1, pool_->IdleSocketCount()); |
| 1477 | } |
| 1478 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1479 | TEST_F(ClientSocketPoolBaseTest, PendingRequests) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1480 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1481 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1482 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1483 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1484 | EXPECT_THAT(StartRequest(TestGroupId("a"), IDLE), IsError(ERR_IO_PENDING)); |
| 1485 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
| 1486 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1487 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1488 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 1489 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1490 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1491 | ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1492 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
| 1493 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1494 | EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup, |
| 1495 | completion_count()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1496 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1497 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1498 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
[email protected] | c9c6f5c | 2010-07-31 01:30:03 | [diff] [blame] | 1499 | EXPECT_EQ(8, GetOrderOfRequest(3)); |
| 1500 | EXPECT_EQ(6, GetOrderOfRequest(4)); |
| 1501 | EXPECT_EQ(4, GetOrderOfRequest(5)); |
| 1502 | EXPECT_EQ(3, GetOrderOfRequest(6)); |
| 1503 | EXPECT_EQ(5, GetOrderOfRequest(7)); |
| 1504 | EXPECT_EQ(7, GetOrderOfRequest(8)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1505 | |
| 1506 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1507 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(9)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1508 | } |
| 1509 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1510 | TEST_F(ClientSocketPoolBaseTest, PendingRequests_NoKeepAlive) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1511 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1512 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1513 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1514 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1515 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
| 1516 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1517 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1518 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 1519 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1520 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1521 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1522 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1523 | for (size_t i = kDefaultMaxSocketsPerGroup; i < requests_size(); ++i) |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1524 | EXPECT_THAT(request(i)->WaitForResult(), IsOk()); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1525 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1526 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1527 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1528 | EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup, |
| 1529 | completion_count()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1530 | } |
| 1531 | |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1532 | TEST_F(ClientSocketPoolBaseTest, ResetAndCloseSocket) { |
| 1533 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1534 | |
| 1535 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1536 | ClientSocketHandle handle; |
| 1537 | TestCompletionCallback callback; |
| 1538 | EXPECT_EQ( |
| 1539 | ERR_IO_PENDING, |
| 1540 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1541 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1542 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1543 | pool_.get(), NetLogWithSource())); |
| 1544 | |
| 1545 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 1546 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1547 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1548 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
| 1549 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 1550 | |
| 1551 | handle.ResetAndCloseSocket(); |
| 1552 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1553 | } |
| 1554 | |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1555 | // This test will start up a socket request and then call Reset() on the handle. |
| 1556 | // The pending ConnectJob should not be destroyed. |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1557 | TEST_F(ClientSocketPoolBaseTest, CancelRequestKeepsConnectJob) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1558 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1559 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1560 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1561 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1562 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1563 | EXPECT_EQ( |
| 1564 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1565 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1566 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1567 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1568 | pool_.get(), NetLogWithSource())); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1569 | handle.Reset(); |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1570 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1571 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1572 | } |
| 1573 | |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1574 | // This test will start up a socket request and then call ResetAndCloseSocket() |
| 1575 | // on the handle. The pending ConnectJob or connected socket should be |
| 1576 | // destroyed. |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1577 | TEST_F(ClientSocketPoolBaseTest, CancelRequestAndCloseSocket) { |
| 1578 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1579 | |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1580 | // When true, the socket connects before it's canceled. |
| 1581 | for (bool cancel_when_callback_pending : {false, true}) { |
| 1582 | if (cancel_when_callback_pending) { |
| 1583 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 1584 | } else { |
| 1585 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1586 | } |
| 1587 | ClientSocketHandle handle; |
| 1588 | TestCompletionCallback callback; |
| 1589 | EXPECT_EQ( |
| 1590 | ERR_IO_PENDING, |
| 1591 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1592 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1593 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1594 | pool_.get(), NetLogWithSource())); |
| 1595 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1596 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1597 | |
| 1598 | if (cancel_when_callback_pending) { |
| 1599 | client_socket_factory_.SignalJobs(); |
| 1600 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1601 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 1602 | } |
| 1603 | |
| 1604 | handle.ResetAndCloseSocket(); |
| 1605 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1606 | } |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1607 | } |
| 1608 | |
| 1609 | TEST_F(ClientSocketPoolBaseTest, |
| 1610 | CancelRequestAndCloseSocketWhenMoreRequestsThanConnectJobs) { |
| 1611 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1612 | |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1613 | // When true, the sockets connect before they're canceled. |
| 1614 | for (bool cancel_when_callback_pending : {false, true}) { |
| 1615 | if (cancel_when_callback_pending) { |
| 1616 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 1617 | } else { |
| 1618 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1619 | } |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1620 | |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1621 | std::vector<std::unique_ptr<ClientSocketHandle>> handles; |
| 1622 | TestCompletionCallback callback; |
| 1623 | // Make |kDefaultMaxSockets + 1| socket requests. |
| 1624 | for (int i = 0; i < kDefaultMaxSocketsPerGroup + 1; ++i) { |
| 1625 | std::unique_ptr<ClientSocketHandle> handle = |
| 1626 | std::make_unique<ClientSocketHandle>(); |
| 1627 | EXPECT_EQ(ERR_IO_PENDING, |
| 1628 | handle->Init( |
| 1629 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1630 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1631 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1632 | pool_.get(), NetLogWithSource())); |
| 1633 | handles.push_back(std::move(handle)); |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1634 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1635 | EXPECT_EQ( |
| 1636 | static_cast<size_t>(std::min(i + 1, kDefaultMaxSocketsPerGroup)), |
| 1637 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1638 | } |
| 1639 | |
| 1640 | if (cancel_when_callback_pending) { |
| 1641 | client_socket_factory_.SignalJobs(); |
| 1642 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1643 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
| 1644 | pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 1645 | } |
| 1646 | |
| 1647 | // Calling ResetAndCloseSocket() on a handle should not cancel a ConnectJob |
| 1648 | // or close a socket, since there are more requests than ConnectJobs or |
| 1649 | // sockets. |
| 1650 | handles[kDefaultMaxSocketsPerGroup]->ResetAndCloseSocket(); |
| 1651 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1652 | if (cancel_when_callback_pending) { |
| 1653 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
| 1654 | pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 1655 | } else { |
| 1656 | EXPECT_EQ(static_cast<size_t>(kDefaultMaxSocketsPerGroup), |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1657 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1658 | } |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1659 | |
| 1660 | // Calling ResetAndCloseSocket() on other handles should cancel a ConnectJob |
| 1661 | // or close a socket. |
| 1662 | for (int i = kDefaultMaxSocketsPerGroup - 1; i >= 0; --i) { |
| 1663 | handles[i]->ResetAndCloseSocket(); |
| 1664 | if (i > 0) { |
| 1665 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1666 | if (cancel_when_callback_pending) { |
| 1667 | EXPECT_EQ(i, |
| 1668 | pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 1669 | } else { |
| 1670 | EXPECT_EQ(static_cast<size_t>(i), |
| 1671 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1672 | } |
| 1673 | } else { |
| 1674 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1675 | } |
| 1676 | } |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1677 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1678 | } |
| 1679 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1680 | TEST_F(ClientSocketPoolBaseTest, ConnectCancelConnect) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1681 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1682 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1683 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 1684 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1685 | TestCompletionCallback callback; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1686 | |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1687 | EXPECT_EQ( |
| 1688 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1689 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1690 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1691 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1692 | pool_.get(), NetLogWithSource())); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1693 | |
| 1694 | handle.Reset(); |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1695 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1696 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1697 | |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1698 | // This will create a second ConnectJob, since the other ConnectJob was |
| 1699 | // previously assigned to a request. |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1700 | TestCompletionCallback callback2; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1701 | EXPECT_EQ( |
| 1702 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1703 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1704 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1705 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1706 | pool_.get(), NetLogWithSource())); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1707 | |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1708 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1709 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1710 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1711 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1712 | EXPECT_FALSE(callback.have_result()); |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1713 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1714 | // One ConnectJob completed, and its socket is now assigned to |handle|. |
| 1715 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 1716 | // The other ConnectJob should have either completed, or still be connecting. |
| 1717 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")) + |
| 1718 | pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1719 | |
| 1720 | handle.Reset(); |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1721 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1722 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")) + |
| 1723 | pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
| 1724 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1725 | } |
| 1726 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1727 | TEST_F(ClientSocketPoolBaseTest, CancelRequest) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1728 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1729 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1730 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1731 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1732 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
| 1733 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1734 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1735 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 1736 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1737 | |
| 1738 | // Cancel a request. |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1739 | size_t index_to_cancel = kDefaultMaxSocketsPerGroup + 2; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1740 | EXPECT_FALSE((*requests())[index_to_cancel]->handle()->is_initialized()); |
| 1741 | (*requests())[index_to_cancel]->handle()->Reset(); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1742 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1743 | ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1744 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1745 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
| 1746 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1747 | EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup - 1, |
| 1748 | completion_count()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1749 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1750 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1751 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1752 | EXPECT_EQ(5, GetOrderOfRequest(3)); |
| 1753 | EXPECT_EQ(3, GetOrderOfRequest(4)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1754 | EXPECT_EQ(ClientSocketPoolTest::kRequestNotFound, |
| 1755 | GetOrderOfRequest(5)); // Canceled request. |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1756 | EXPECT_EQ(4, GetOrderOfRequest(6)); |
| 1757 | EXPECT_EQ(6, GetOrderOfRequest(7)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1758 | |
| 1759 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1760 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(8)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1761 | } |
| 1762 | |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1763 | // Function to be used as a callback on socket request completion. It first |
| 1764 | // disconnects the successfully connected socket from the first request, and |
| 1765 | // then reuses the ClientSocketHandle to request another socket. |
| 1766 | // |
| 1767 | // |nested_callback| is called with the result of the second socket request. |
| 1768 | void RequestSocketOnComplete(ClientSocketHandle* handle, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1769 | TransportClientSocketPool* pool, |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1770 | TestConnectJobFactory* test_connect_job_factory, |
| 1771 | TestConnectJob::JobType next_job_type, |
Bence Béky | a4a5093 | 2018-08-10 13:39:41 | [diff] [blame] | 1772 | TestCompletionCallback* nested_callback, |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1773 | int first_request_result) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1774 | EXPECT_THAT(first_request_result, IsOk()); |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1775 | |
| 1776 | test_connect_job_factory->set_job_type(next_job_type); |
| 1777 | |
| 1778 | // Don't allow reuse of the socket. Disconnect it and then release it. |
| 1779 | if (handle->socket()) |
| 1780 | handle->socket()->Disconnect(); |
| 1781 | handle->Reset(); |
| 1782 | |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1783 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1784 | int rv = handle->Init( |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 1785 | TestGroupId("a"), |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1786 | ClientSocketPool::SocketParams::CreateForHttpForTesting(), base::nullopt, |
| 1787 | LOWEST, SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 1788 | nested_callback->callback(), ClientSocketPool::ProxyAuthCallback(), pool, |
| 1789 | NetLogWithSource()); |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1790 | if (rv != ERR_IO_PENDING) { |
| 1791 | DCHECK_EQ(TestConnectJob::kMockJob, next_job_type); |
Bence Béky | a4a5093 | 2018-08-10 13:39:41 | [diff] [blame] | 1792 | nested_callback->callback().Run(rv); |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1793 | } else { |
| 1794 | DCHECK_EQ(TestConnectJob::kMockPendingJob, next_job_type); |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1795 | } |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1796 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1797 | |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1798 | // Tests the case where a second socket is requested in a completion callback, |
| 1799 | // and the second socket connects asynchronously. Reuses the same |
| 1800 | // ClientSocketHandle for the second socket, after disconnecting the first. |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1801 | TEST_F(ClientSocketPoolBaseTest, RequestPendingJobTwice) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1802 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1803 | |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 1804 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 1805 | ClientSocketHandle handle; |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1806 | TestCompletionCallback second_result_callback; |
| 1807 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1808 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Paul Jensen | 8d6f87ec | 2018-01-13 00:46:54 | [diff] [blame] | 1809 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1810 | base::BindOnce(&RequestSocketOnComplete, &handle, pool_.get(), |
| 1811 | connect_job_factory_, TestConnectJob::kMockPendingJob, |
| 1812 | &second_result_callback), |
| 1813 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1814 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1815 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1816 | EXPECT_THAT(second_result_callback.WaitForResult(), IsOk()); |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1817 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1818 | |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1819 | // Tests the case where a second socket is requested in a completion callback, |
| 1820 | // and the second socket connects synchronously. Reuses the same |
| 1821 | // ClientSocketHandle for the second socket, after disconnecting the first. |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1822 | TEST_F(ClientSocketPoolBaseTest, RequestPendingJobThenSynchronous) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1823 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1824 | |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 1825 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 1826 | ClientSocketHandle handle; |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1827 | TestCompletionCallback second_result_callback; |
| 1828 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1829 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Paul Jensen | 8d6f87ec | 2018-01-13 00:46:54 | [diff] [blame] | 1830 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1831 | base::BindOnce(&RequestSocketOnComplete, &handle, pool_.get(), |
| 1832 | connect_job_factory_, TestConnectJob::kMockPendingJob, |
| 1833 | &second_result_callback), |
| 1834 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1835 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1836 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1837 | EXPECT_THAT(second_result_callback.WaitForResult(), IsOk()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1838 | } |
| 1839 | |
| 1840 | // Make sure that pending requests get serviced after active requests get |
| 1841 | // cancelled. |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1842 | TEST_F(ClientSocketPoolBaseTest, CancelActiveRequestWithPendingRequests) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1843 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1844 | |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 1845 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1846 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1847 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1848 | IsError(ERR_IO_PENDING)); |
| 1849 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1850 | IsError(ERR_IO_PENDING)); |
| 1851 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1852 | IsError(ERR_IO_PENDING)); |
| 1853 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1854 | IsError(ERR_IO_PENDING)); |
| 1855 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1856 | IsError(ERR_IO_PENDING)); |
| 1857 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1858 | IsError(ERR_IO_PENDING)); |
| 1859 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1860 | IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1861 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1862 | // Now, kDefaultMaxSocketsPerGroup requests should be active. |
| 1863 | // Let's cancel them. |
| 1864 | for (int i = 0; i < kDefaultMaxSocketsPerGroup; ++i) { |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1865 | ASSERT_FALSE(request(i)->handle()->is_initialized()); |
| 1866 | request(i)->handle()->Reset(); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1867 | } |
| 1868 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1869 | // Let's wait for the rest to complete now. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1870 | for (size_t i = kDefaultMaxSocketsPerGroup; i < requests_size(); ++i) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1871 | EXPECT_THAT(request(i)->WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1872 | request(i)->handle()->Reset(); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1873 | } |
| 1874 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1875 | EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup, |
| 1876 | completion_count()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1877 | } |
| 1878 | |
| 1879 | // Make sure that pending requests get serviced after active requests fail. |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1880 | TEST_F(ClientSocketPoolBaseTest, FailingActiveRequestWithPendingRequests) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1881 | const size_t kMaxSockets = 5; |
| 1882 | CreatePool(kMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1883 | |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 1884 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1885 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1886 | const size_t kNumberOfRequests = 2 * kDefaultMaxSocketsPerGroup + 1; |
| 1887 | ASSERT_LE(kNumberOfRequests, kMaxSockets); // Otherwise the test will hang. |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1888 | |
| 1889 | // Queue up all the requests |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1890 | for (size_t i = 0; i < kNumberOfRequests; ++i) |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1891 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1892 | IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1893 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1894 | for (size_t i = 0; i < kNumberOfRequests; ++i) |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1895 | EXPECT_THAT(request(i)->WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1896 | } |
| 1897 | |
mmenke | 9d72fe4 | 2017-05-18 22:36:07 | [diff] [blame] | 1898 | // Make sure that pending requests that complete synchronously get serviced |
| 1899 | // after active requests fail. See https://crbug.com/723748 |
| 1900 | TEST_F(ClientSocketPoolBaseTest, HandleMultipleSyncFailuresAfterAsyncFailure) { |
| 1901 | const size_t kNumberOfRequests = 10; |
| 1902 | const size_t kMaxSockets = 1; |
| 1903 | CreatePool(kMaxSockets, kMaxSockets); |
| 1904 | |
| 1905 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
| 1906 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1907 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1908 | IsError(ERR_IO_PENDING)); |
mmenke | 9d72fe4 | 2017-05-18 22:36:07 | [diff] [blame] | 1909 | |
| 1910 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
| 1911 | |
| 1912 | // Queue up all the other requests |
| 1913 | for (size_t i = 1; i < kNumberOfRequests; ++i) |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1914 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1915 | IsError(ERR_IO_PENDING)); |
mmenke | 9d72fe4 | 2017-05-18 22:36:07 | [diff] [blame] | 1916 | |
| 1917 | // Make sure all requests fail, instead of hanging. |
| 1918 | for (size_t i = 0; i < kNumberOfRequests; ++i) |
| 1919 | EXPECT_THAT(request(i)->WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 1920 | } |
| 1921 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1922 | TEST_F(ClientSocketPoolBaseTest, CancelActiveRequestThenRequestSocket) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1923 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1924 | |
| 1925 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1926 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1927 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1928 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1929 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1930 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1931 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1932 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1933 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1934 | |
| 1935 | // Cancel the active request. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1936 | handle.Reset(); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1937 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1938 | rv = handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1939 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1940 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1941 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1942 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 1943 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1944 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1945 | EXPECT_FALSE(handle.is_reused()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 1946 | TestLoadTimingInfoConnectedNotReused(handle); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1947 | EXPECT_EQ(2, client_socket_factory_.allocation_count()); |
| 1948 | } |
| 1949 | |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1950 | TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketsForced) { |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 1951 | const char kReason[] = "Really nifty reason"; |
| 1952 | |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1953 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1954 | ClientSocketHandle handle; |
| 1955 | TestCompletionCallback callback; |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 1956 | RecordingBoundTestNetLog log; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1957 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1958 | TestGroupId("a"), params_, base::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1959 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1960 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1961 | EXPECT_THAT(rv, IsOk()); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 1962 | ASSERT_TRUE(handle.socket()); |
| 1963 | NetLogSource source = handle.socket()->NetLog().source(); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1964 | handle.Reset(); |
| 1965 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 1966 | pool_->CloseIdleSockets(kReason); |
| 1967 | ExpectSocketClosedWithReason(source, kReason); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1968 | } |
| 1969 | |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1970 | TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketsInGroupForced) { |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1971 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1972 | TestCompletionCallback callback; |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 1973 | RecordingBoundTestNetLog log; |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1974 | ClientSocketHandle handle1; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1975 | int rv = handle1.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1976 | TestGroupId("a"), params_, base::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1977 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1978 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1979 | EXPECT_THAT(rv, IsOk()); |
| 1980 | ClientSocketHandle handle2; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1981 | rv = handle2.Init(TestGroupId("a"), params_, base::nullopt, LOWEST, |
| 1982 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1983 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1984 | pool_.get(), log.bound()); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1985 | ClientSocketHandle handle3; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1986 | rv = handle3.Init(TestGroupId("b"), params_, base::nullopt, LOWEST, |
| 1987 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1988 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1989 | pool_.get(), log.bound()); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1990 | EXPECT_THAT(rv, IsOk()); |
| 1991 | handle1.Reset(); |
| 1992 | handle2.Reset(); |
| 1993 | handle3.Reset(); |
| 1994 | EXPECT_EQ(3, pool_->IdleSocketCount()); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 1995 | pool_->CloseIdleSocketsInGroup(TestGroupId("a"), "Very good reason"); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1996 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1997 | } |
| 1998 | |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1999 | TEST_F(ClientSocketPoolBaseTest, CleanUpUnusableIdleSockets) { |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 2000 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2001 | ClientSocketHandle handle; |
| 2002 | TestCompletionCallback callback; |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 2003 | RecordingBoundTestNetLog log; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2004 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2005 | TestGroupId("a"), params_, base::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2006 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2007 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 2008 | EXPECT_THAT(rv, IsOk()); |
| 2009 | StreamSocket* socket = handle.socket(); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 2010 | ASSERT_TRUE(socket); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 2011 | handle.Reset(); |
| 2012 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 2013 | |
| 2014 | // Disconnect socket now to make the socket unusable. |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 2015 | NetLogSource source = socket->NetLog().source(); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 2016 | socket->Disconnect(); |
| 2017 | ClientSocketHandle handle2; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2018 | rv = handle2.Init(TestGroupId("a"), params_, base::nullopt, LOWEST, |
| 2019 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2020 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2021 | pool_.get(), log.bound()); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 2022 | EXPECT_THAT(rv, IsOk()); |
| 2023 | EXPECT_FALSE(handle2.is_reused()); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 2024 | |
| 2025 | // This is admittedly not an accurate error in this case, but normally code |
| 2026 | // doesn't secretly keep a raw pointers to sockets returned to the socket pool |
| 2027 | // and close them out of band, so discovering an idle socket was closed when |
| 2028 | // trying to reuse it normally means it was closed by the remote side. |
| 2029 | ExpectSocketClosedWithReason( |
| 2030 | source, TransportClientSocketPool::kRemoteSideClosedConnection); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 2031 | } |
| 2032 | |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 2033 | // Regression test for http://crbug.com/17985. |
| 2034 | TEST_F(ClientSocketPoolBaseTest, GroupWithPendingRequestsIsNotEmpty) { |
| 2035 | const int kMaxSockets = 3; |
| 2036 | const int kMaxSocketsPerGroup = 2; |
| 2037 | CreatePool(kMaxSockets, kMaxSocketsPerGroup); |
| 2038 | |
[email protected] | ac790b4 | 2009-12-02 04:31:31 | [diff] [blame] | 2039 | const RequestPriority kHighPriority = HIGHEST; |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 2040 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2041 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 2042 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 2043 | |
| 2044 | // This is going to be a pending request in an otherwise empty group. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2045 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2046 | IsError(ERR_IO_PENDING)); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 2047 | |
| 2048 | // Reach the maximum socket limit. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2049 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 2050 | |
| 2051 | // Create a stalled group with high priorities. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2052 | EXPECT_THAT(StartRequest(TestGroupId("c"), kHighPriority), |
| 2053 | IsError(ERR_IO_PENDING)); |
| 2054 | EXPECT_THAT(StartRequest(TestGroupId("c"), kHighPriority), |
| 2055 | IsError(ERR_IO_PENDING)); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 2056 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2057 | // Release the first two sockets from TestGroupId("a"). Because this is a |
| 2058 | // keepalive, the first release will unblock the pending request for |
| 2059 | // TestGroupId("a"). The second release will unblock a request for "c", |
| 2060 | // because it is the next high priority socket. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2061 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
| 2062 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 2063 | |
| 2064 | // Closing idle sockets should not get us into trouble, but in the bug |
| 2065 | // we were hitting a CHECK here. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2066 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 2067 | pool_->CloseIdleSockets("Very good reason"); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 2068 | |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 2069 | // Run the released socket wakeups. |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 2070 | base::RunLoop().RunUntilIdle(); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 2071 | } |
| 2072 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2073 | TEST_F(ClientSocketPoolBaseTest, BasicAsynchronous) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2074 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2075 | |
| 2076 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2077 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2078 | TestCompletionCallback callback; |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 2079 | RecordingBoundTestNetLog log; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2080 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2081 | TestGroupId("a"), params_, base::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2082 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2083 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2084 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2085 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2086 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 2087 | TestLoadTimingInfoNotConnected(handle); |
| 2088 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2089 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2090 | EXPECT_TRUE(handle.is_initialized()); |
| 2091 | EXPECT_TRUE(handle.socket()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 2092 | TestLoadTimingInfoConnectedNotReused(handle); |
| 2093 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2094 | handle.Reset(); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 2095 | TestLoadTimingInfoNotConnected(handle); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 2096 | |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 2097 | auto entries = log.GetEntries(); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 2098 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2099 | EXPECT_EQ(5u, entries.size()); |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 2100 | EXPECT_TRUE(LogContainsEvent( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2101 | entries, 0, NetLogEventType::TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 2102 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2103 | EXPECT_TRUE(LogContainsBeginEvent(entries, 1, NetLogEventType::SOCKET_POOL)); |
| 2104 | EXPECT_TRUE(LogContainsEvent( |
| 2105 | entries, 2, NetLogEventType::SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
| 2106 | NetLogEventPhase::NONE)); |
| 2107 | EXPECT_TRUE(LogContainsEvent(entries, 3, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 2108 | NetLogEventType::SOCKET_POOL_BOUND_TO_SOCKET, |
| 2109 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2110 | EXPECT_TRUE(LogContainsEndEvent(entries, 4, NetLogEventType::SOCKET_POOL)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2111 | } |
| 2112 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2113 | TEST_F(ClientSocketPoolBaseTest, |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2114 | InitConnectionAsynchronousFailure) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2115 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2116 | |
| 2117 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2118 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2119 | TestCompletionCallback callback; |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 2120 | RecordingBoundTestNetLog log; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2121 | // Set the additional error state members to ensure that they get cleared. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2122 | handle.set_is_ssl_error(true); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 2123 | handle.set_ssl_cert_request_info(base::MakeRefCounted<SSLCertRequestInfo>()); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2124 | EXPECT_EQ( |
| 2125 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2126 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2127 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2128 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2129 | pool_.get(), log.bound())); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2130 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2131 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2132 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2133 | EXPECT_FALSE(handle.is_ssl_error()); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 2134 | EXPECT_FALSE(handle.ssl_cert_request_info()); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 2135 | |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 2136 | auto entries = log.GetEntries(); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 2137 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2138 | EXPECT_EQ(4u, entries.size()); |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 2139 | EXPECT_TRUE(LogContainsEvent( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2140 | entries, 0, NetLogEventType::TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 2141 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2142 | EXPECT_TRUE(LogContainsBeginEvent(entries, 1, NetLogEventType::SOCKET_POOL)); |
| 2143 | EXPECT_TRUE(LogContainsEvent( |
| 2144 | entries, 2, NetLogEventType::SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
| 2145 | NetLogEventPhase::NONE)); |
| 2146 | EXPECT_TRUE(LogContainsEndEvent(entries, 3, NetLogEventType::SOCKET_POOL)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2147 | } |
| 2148 | |
mmenke | 6be122f | 2015-03-09 22:22:47 | [diff] [blame] | 2149 | // Check that an async ConnectJob failure does not result in creation of a new |
| 2150 | // ConnectJob when there's another pending request also waiting on its own |
| 2151 | // ConnectJob. See http://crbug.com/463960. |
| 2152 | TEST_F(ClientSocketPoolBaseTest, AsyncFailureWithPendingRequestWithJob) { |
| 2153 | CreatePool(2, 2); |
| 2154 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
| 2155 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2156 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2157 | IsError(ERR_IO_PENDING)); |
| 2158 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2159 | IsError(ERR_IO_PENDING)); |
mmenke | 6be122f | 2015-03-09 22:22:47 | [diff] [blame] | 2160 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2161 | EXPECT_THAT(request(0)->WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 2162 | EXPECT_THAT(request(1)->WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
mmenke | 6be122f | 2015-03-09 22:22:47 | [diff] [blame] | 2163 | |
| 2164 | EXPECT_EQ(2, client_socket_factory_.allocation_count()); |
| 2165 | } |
| 2166 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2167 | TEST_F(ClientSocketPoolBaseTest, TwoRequestsCancelOne) { |
[email protected] | b22b516 | 2010-03-16 07:53:10 | [diff] [blame] | 2168 | // TODO(eroman): Add back the log expectations! Removed them because the |
| 2169 | // ordering is difficult, and some may fire during destructor. |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2170 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2171 | |
| 2172 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2173 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2174 | TestCompletionCallback callback; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2175 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2176 | TestCompletionCallback callback2; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2177 | |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2178 | EXPECT_EQ( |
| 2179 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2180 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2181 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2182 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2183 | pool_.get(), NetLogWithSource())); |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 2184 | RecordingBoundTestNetLog log2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 2185 | EXPECT_EQ( |
| 2186 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2187 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2188 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2189 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2190 | pool_.get(), NetLogWithSource())); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2191 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2192 | handle.Reset(); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2193 | |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 2194 | |
| 2195 | // 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] | 2196 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2197 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2198 | handle2.Reset(); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 2199 | |
| 2200 | // Now request 2 has actually finished. |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 2201 | // TODO(eroman): Add back log expectations. |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2202 | } |
| 2203 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2204 | TEST_F(ClientSocketPoolBaseTest, CancelRequestLimitsJobs) { |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 2205 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2206 | |
[email protected] | 17a0c6c | 2009-08-04 00:07:04 | [diff] [blame] | 2207 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2208 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2209 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
| 2210 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 2211 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 2212 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 2213 | |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2214 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2215 | static_cast<int>( |
| 2216 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2217 | (*requests())[2]->handle()->Reset(); |
| 2218 | (*requests())[3]->handle()->Reset(); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2219 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2220 | static_cast<int>( |
| 2221 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 2222 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2223 | (*requests())[1]->handle()->Reset(); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2224 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2225 | static_cast<int>( |
| 2226 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 2227 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2228 | (*requests())[0]->handle()->Reset(); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2229 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2230 | static_cast<int>( |
| 2231 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 2232 | } |
| 2233 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2234 | // When requests and ConnectJobs are not coupled, the request will get serviced |
| 2235 | // by whatever comes first. |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2236 | TEST_F(ClientSocketPoolBaseTest, ReleaseSockets) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2237 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2238 | |
| 2239 | // Start job 1 (async OK) |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 2240 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2241 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2242 | std::vector<TestSocketRequest*> request_order; |
| 2243 | size_t completion_count; // unused |
| 2244 | TestSocketRequest req1(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2245 | int rv = req1.handle()->Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2246 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2247 | ClientSocketPool::RespectLimits::ENABLED, req1.callback(), |
| 2248 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2249 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 2250 | EXPECT_THAT(req1.WaitForResult(), IsOk()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2251 | |
| 2252 | // Job 1 finished OK. Start job 2 (also async OK). Request 3 is pending |
| 2253 | // without a job. |
| 2254 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2255 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2256 | TestSocketRequest req2(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2257 | rv = req2.handle()->Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2258 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2259 | ClientSocketPool::RespectLimits::ENABLED, req2.callback(), |
| 2260 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2261 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2262 | TestSocketRequest req3(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2263 | rv = req3.handle()->Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2264 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2265 | ClientSocketPool::RespectLimits::ENABLED, req3.callback(), |
| 2266 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2267 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2268 | |
| 2269 | // Both Requests 2 and 3 are pending. We release socket 1 which should |
| 2270 | // service request 2. Request 3 should still be waiting. |
[email protected] | a6c59f6 | 2009-07-29 16:33:33 | [diff] [blame] | 2271 | req1.handle()->Reset(); |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 2272 | // Run the released socket wakeups. |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 2273 | base::RunLoop().RunUntilIdle(); |
[email protected] | a6c59f6 | 2009-07-29 16:33:33 | [diff] [blame] | 2274 | ASSERT_TRUE(req2.handle()->socket()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2275 | EXPECT_THAT(req2.WaitForResult(), IsOk()); |
[email protected] | a6c59f6 | 2009-07-29 16:33:33 | [diff] [blame] | 2276 | EXPECT_FALSE(req3.handle()->socket()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2277 | |
| 2278 | // Signal job 2, which should service request 3. |
| 2279 | |
| 2280 | client_socket_factory_.SignalJobs(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2281 | EXPECT_THAT(req3.WaitForResult(), IsOk()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2282 | |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2283 | ASSERT_EQ(3u, request_order.size()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2284 | EXPECT_EQ(&req1, request_order[0]); |
| 2285 | EXPECT_EQ(&req2, request_order[1]); |
| 2286 | EXPECT_EQ(&req3, request_order[2]); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2287 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2288 | } |
| 2289 | |
| 2290 | // The requests are not coupled to the jobs. So, the requests should finish in |
| 2291 | // their priority / insertion order. |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2292 | TEST_F(ClientSocketPoolBaseTest, PendingJobCompletionOrder) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2293 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2294 | // First two jobs are async. |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 2295 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2296 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2297 | std::vector<TestSocketRequest*> request_order; |
| 2298 | size_t completion_count; // unused |
| 2299 | TestSocketRequest req1(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2300 | int rv = req1.handle()->Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2301 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2302 | ClientSocketPool::RespectLimits::ENABLED, req1.callback(), |
| 2303 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2304 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2305 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2306 | TestSocketRequest req2(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2307 | rv = req2.handle()->Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2308 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2309 | ClientSocketPool::RespectLimits::ENABLED, req2.callback(), |
| 2310 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2311 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2312 | |
| 2313 | // The pending job is sync. |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 2314 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2315 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2316 | TestSocketRequest req3(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2317 | rv = req3.handle()->Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2318 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2319 | ClientSocketPool::RespectLimits::ENABLED, req3.callback(), |
| 2320 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2321 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2322 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2323 | EXPECT_THAT(req1.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 2324 | EXPECT_THAT(req2.WaitForResult(), IsOk()); |
| 2325 | EXPECT_THAT(req3.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2326 | |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2327 | ASSERT_EQ(3u, request_order.size()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2328 | EXPECT_EQ(&req1, request_order[0]); |
| 2329 | EXPECT_EQ(&req2, request_order[1]); |
| 2330 | EXPECT_EQ(&req3, request_order[2]); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2331 | } |
| 2332 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2333 | // Test GetLoadState in the case there's only one socket request. |
| 2334 | TEST_F(ClientSocketPoolBaseTest, LoadStateOneRequest) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2335 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2336 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2337 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2338 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2339 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2340 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2341 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2342 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2343 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2344 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2345 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2346 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2347 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE); |
| 2348 | EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle.GetLoadState()); |
| 2349 | |
| 2350 | // No point in completing the connection, since ClientSocketHandles only |
| 2351 | // expect the LoadState to be checked while connecting. |
| 2352 | } |
| 2353 | |
| 2354 | // Test GetLoadState in the case there are two socket requests. |
| 2355 | TEST_F(ClientSocketPoolBaseTest, LoadStateTwoRequests) { |
| 2356 | CreatePool(2, 2); |
| 2357 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2358 | |
| 2359 | ClientSocketHandle handle; |
| 2360 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2361 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2362 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2363 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2364 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2365 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2366 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_RESOLVING_HOST); |
| 2367 | |
| 2368 | ClientSocketHandle handle2; |
| 2369 | TestCompletionCallback callback2; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2370 | rv = handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2371 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2372 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2373 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2374 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2375 | client_socket_factory_.SetJobLoadState(1, LOAD_STATE_RESOLVING_HOST); |
| 2376 | |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 2377 | // Each handle should reflect the state of its own job. |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2378 | EXPECT_EQ(LOAD_STATE_RESOLVING_HOST, handle.GetLoadState()); |
| 2379 | EXPECT_EQ(LOAD_STATE_RESOLVING_HOST, handle2.GetLoadState()); |
| 2380 | |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 2381 | // Update the state of the first job. |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2382 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_CONNECTING); |
| 2383 | |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 2384 | // Only the state of the first request should have changed. |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2385 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2386 | EXPECT_EQ(LOAD_STATE_RESOLVING_HOST, handle2.GetLoadState()); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 2387 | |
| 2388 | // Update the state of the second job. |
| 2389 | client_socket_factory_.SetJobLoadState(1, LOAD_STATE_SSL_HANDSHAKE); |
| 2390 | |
| 2391 | // Only the state of the second request should have changed. |
| 2392 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
| 2393 | EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle2.GetLoadState()); |
| 2394 | |
| 2395 | // Second job connects and the first request gets the socket. The |
| 2396 | // second handle switches to the state of the remaining ConnectJob. |
| 2397 | client_socket_factory_.SignalJob(1); |
| 2398 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 2399 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle2.GetLoadState()); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2400 | } |
| 2401 | |
| 2402 | // Test GetLoadState in the case the per-group limit is reached. |
| 2403 | TEST_F(ClientSocketPoolBaseTest, LoadStateGroupLimit) { |
| 2404 | CreatePool(2, 1); |
| 2405 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2406 | |
| 2407 | ClientSocketHandle handle; |
| 2408 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2409 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2410 | TestGroupId("a"), params_, base::nullopt, MEDIUM, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2411 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2412 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2413 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2414 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
| 2415 | |
| 2416 | // Request another socket from the same pool, buth with a higher priority. |
| 2417 | // The first request should now be stalled at the socket group limit. |
| 2418 | ClientSocketHandle handle2; |
| 2419 | TestCompletionCallback callback2; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2420 | rv = handle2.Init(TestGroupId("a"), params_, base::nullopt, HIGHEST, |
| 2421 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2422 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2423 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2424 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2425 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET, handle.GetLoadState()); |
| 2426 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle2.GetLoadState()); |
| 2427 | |
| 2428 | // The first handle should remain stalled as the other socket goes through |
| 2429 | // the connect process. |
| 2430 | |
| 2431 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE); |
| 2432 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET, handle.GetLoadState()); |
| 2433 | EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle2.GetLoadState()); |
| 2434 | |
| 2435 | client_socket_factory_.SignalJob(0); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2436 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2437 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET, handle.GetLoadState()); |
| 2438 | |
| 2439 | // Closing the second socket should cause the stalled handle to finally get a |
| 2440 | // ConnectJob. |
| 2441 | handle2.socket()->Disconnect(); |
| 2442 | handle2.Reset(); |
| 2443 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
| 2444 | } |
| 2445 | |
| 2446 | // Test GetLoadState in the case the per-pool limit is reached. |
| 2447 | TEST_F(ClientSocketPoolBaseTest, LoadStatePoolLimit) { |
| 2448 | CreatePool(2, 2); |
| 2449 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2450 | |
| 2451 | ClientSocketHandle handle; |
| 2452 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2453 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2454 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2455 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2456 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2457 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2458 | |
| 2459 | // Request for socket from another pool. |
| 2460 | ClientSocketHandle handle2; |
| 2461 | TestCompletionCallback callback2; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2462 | rv = handle2.Init(TestGroupId("b"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2463 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2464 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2465 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2466 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2467 | |
| 2468 | // Request another socket from the first pool. Request should stall at the |
| 2469 | // socket pool limit. |
| 2470 | ClientSocketHandle handle3; |
| 2471 | TestCompletionCallback callback3; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2472 | rv = handle3.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2473 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2474 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2475 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2476 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2477 | |
| 2478 | // The third handle should remain stalled as the other sockets in its group |
| 2479 | // goes through the connect process. |
| 2480 | |
| 2481 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
| 2482 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL, handle3.GetLoadState()); |
| 2483 | |
| 2484 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE); |
| 2485 | EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle.GetLoadState()); |
| 2486 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL, handle3.GetLoadState()); |
| 2487 | |
| 2488 | client_socket_factory_.SignalJob(0); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2489 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2490 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL, handle3.GetLoadState()); |
| 2491 | |
| 2492 | // Closing a socket should allow the stalled handle to finally get a new |
| 2493 | // ConnectJob. |
| 2494 | handle.socket()->Disconnect(); |
| 2495 | handle.Reset(); |
| 2496 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle3.GetLoadState()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2497 | } |
| 2498 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2499 | TEST_F(ClientSocketPoolBaseTest, CertError) { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2500 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2501 | connect_job_factory_->set_job_type(TestConnectJob::kMockCertErrorJob); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2502 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2503 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2504 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2505 | EXPECT_EQ( |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2506 | ERR_CERT_COMMON_NAME_INVALID, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2507 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2508 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2509 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2510 | pool_.get(), NetLogWithSource())); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2511 | EXPECT_TRUE(handle.is_initialized()); |
| 2512 | EXPECT_TRUE(handle.socket()); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2513 | } |
| 2514 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2515 | TEST_F(ClientSocketPoolBaseTest, AsyncCertError) { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2516 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2517 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2518 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingCertErrorJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2519 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2520 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2521 | EXPECT_EQ( |
| 2522 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2523 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2524 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2525 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2526 | pool_.get(), NetLogWithSource())); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2527 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2528 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2529 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_CERT_COMMON_NAME_INVALID)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2530 | EXPECT_TRUE(handle.is_initialized()); |
| 2531 | EXPECT_TRUE(handle.socket()); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2532 | } |
| 2533 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2534 | TEST_F(ClientSocketPoolBaseTest, AdditionalErrorStateSynchronous) { |
| 2535 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2536 | connect_job_factory_->set_job_type( |
| 2537 | TestConnectJob::kMockAdditionalErrorStateJob); |
| 2538 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2539 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2540 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2541 | EXPECT_EQ( |
| 2542 | ERR_CONNECTION_FAILED, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2543 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2544 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2545 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2546 | pool_.get(), NetLogWithSource())); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2547 | EXPECT_FALSE(handle.is_initialized()); |
| 2548 | EXPECT_FALSE(handle.socket()); |
| 2549 | EXPECT_TRUE(handle.is_ssl_error()); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 2550 | EXPECT_TRUE(handle.ssl_cert_request_info()); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2551 | } |
| 2552 | |
| 2553 | TEST_F(ClientSocketPoolBaseTest, AdditionalErrorStateAsynchronous) { |
| 2554 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2555 | |
| 2556 | connect_job_factory_->set_job_type( |
| 2557 | TestConnectJob::kMockPendingAdditionalErrorStateJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2558 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2559 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2560 | EXPECT_EQ( |
| 2561 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2562 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2563 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2564 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2565 | pool_.get(), NetLogWithSource())); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2566 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2567 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2568 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2569 | EXPECT_FALSE(handle.is_initialized()); |
| 2570 | EXPECT_FALSE(handle.socket()); |
| 2571 | EXPECT_TRUE(handle.is_ssl_error()); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 2572 | EXPECT_TRUE(handle.ssl_cert_request_info()); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2573 | } |
| 2574 | |
martijn | 003cd61 | 2016-05-19 22:24:38 | [diff] [blame] | 2575 | // Make sure we can reuse sockets. |
| 2576 | TEST_F(ClientSocketPoolBaseTest, CleanupTimedOutIdleSocketsReuse) { |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2577 | CreatePoolWithIdleTimeouts( |
| 2578 | kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2579 | base::TimeDelta(), // Time out unused sockets immediately. |
| 2580 | base::TimeDelta::FromDays(1)); // Don't time out used sockets. |
| 2581 | |
| 2582 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2583 | |
| 2584 | ClientSocketHandle handle; |
| 2585 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2586 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2587 | TestGroupId("a"), params_, base::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2588 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2589 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2590 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2591 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2592 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2593 | ASSERT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2594 | |
| 2595 | // Use and release the socket. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 2596 | EXPECT_EQ(1, handle.socket()->Write(nullptr, 1, CompletionOnceCallback(), |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 2597 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 2598 | TestLoadTimingInfoConnectedNotReused(handle); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2599 | handle.Reset(); |
| 2600 | |
| 2601 | // Should now have one idle socket. |
| 2602 | ASSERT_EQ(1, pool_->IdleSocketCount()); |
| 2603 | |
| 2604 | // Request a new socket. This should reuse the old socket and complete |
| 2605 | // synchronously. |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 2606 | RecordingBoundTestNetLog log; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2607 | rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2608 | TestGroupId("a"), params_, base::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2609 | ClientSocketPool::RespectLimits::ENABLED, CompletionOnceCallback(), |
| 2610 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2611 | ASSERT_THAT(rv, IsOk()); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2612 | EXPECT_TRUE(handle.is_reused()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 2613 | TestLoadTimingInfoConnectedReused(handle); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2614 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2615 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2616 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2617 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2618 | |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 2619 | auto entries = log.GetEntries(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2620 | EXPECT_TRUE(LogContainsEvent( |
| 2621 | entries, 0, NetLogEventType::TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET, |
| 2622 | NetLogEventPhase::NONE)); |
| 2623 | EXPECT_TRUE(LogContainsBeginEvent(entries, 1, NetLogEventType::SOCKET_POOL)); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2624 | EXPECT_TRUE(LogContainsEntryWithType( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2625 | entries, 2, NetLogEventType::SOCKET_POOL_REUSED_AN_EXISTING_SOCKET)); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2626 | } |
| 2627 | |
martijn | 003cd61 | 2016-05-19 22:24:38 | [diff] [blame] | 2628 | // Make sure we cleanup old unused sockets. |
Eric Roman | b49715e | 2018-04-24 22:41:17 | [diff] [blame] | 2629 | TEST_F(ClientSocketPoolBaseTest, CleanupTimedOutIdleSocketsNoReuse) { |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2630 | CreatePoolWithIdleTimeouts( |
| 2631 | kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 2632 | base::TimeDelta(), // Time out unused sockets immediately |
| 2633 | base::TimeDelta()); // Time out used sockets immediately |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2634 | |
| 2635 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2636 | |
| 2637 | // Startup two mock pending connect jobs, which will sit in the MessageLoop. |
| 2638 | |
| 2639 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2640 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2641 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2642 | TestGroupId("a"), params_, base::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2643 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2644 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2645 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2646 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2647 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2648 | |
| 2649 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2650 | TestCompletionCallback callback2; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2651 | rv = handle2.Init(TestGroupId("a"), params_, base::nullopt, LOWEST, |
| 2652 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2653 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2654 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2655 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2656 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2657 | pool_->GetLoadState(TestGroupId("a"), &handle2)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2658 | |
| 2659 | // Cancel one of the requests. Wait for the other, which will get the first |
| 2660 | // job. Release the socket. Run the loop again to make sure the second |
| 2661 | // socket is sitting idle and the first one is released (since ReleaseSocket() |
| 2662 | // just posts a DoReleaseSocket() task). |
| 2663 | |
| 2664 | handle.Reset(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2665 | ASSERT_THAT(callback2.WaitForResult(), IsOk()); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 2666 | // Get the NetLogSource for the socket, so the time out reason can be checked |
| 2667 | // at the end of the test. |
| 2668 | NetLogSource net_log_source2 = handle2.socket()->NetLog().source(); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2669 | // Use the socket. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 2670 | EXPECT_EQ(1, handle2.socket()->Write(nullptr, 1, CompletionOnceCallback(), |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 2671 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2672 | handle2.Reset(); |
| 2673 | |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2674 | // We post all of our delayed tasks with a 2ms delay. I.e. they don't |
| 2675 | // actually become pending until 2ms after they have been created. In order |
| 2676 | // to flush all tasks, we need to wait so that we know there are no |
| 2677 | // soon-to-be-pending tasks waiting. |
Alex Clarke | 0def209 | 2018-12-10 12:01:45 | [diff] [blame] | 2678 | FastForwardBy(base::TimeDelta::FromMilliseconds(10)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2679 | |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2680 | // Both sockets should now be idle. |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2681 | ASSERT_EQ(2, pool_->IdleSocketCount()); |
| 2682 | |
| 2683 | // Request a new socket. This should cleanup the unused and timed out ones. |
| 2684 | // A new socket will be created rather than reusing the idle one. |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 2685 | RecordingBoundTestNetLog log; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2686 | TestCompletionCallback callback3; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2687 | rv = handle.Init(TestGroupId("a"), params_, base::nullopt, LOWEST, |
| 2688 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2689 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2690 | pool_.get(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2691 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 2692 | ASSERT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2693 | EXPECT_FALSE(handle.is_reused()); |
| 2694 | |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2695 | // Make sure the idle socket is closed. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2696 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2697 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2698 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2699 | |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 2700 | auto entries = log.GetEntries(); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2701 | EXPECT_FALSE(LogContainsEntryWithType( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 2702 | entries, 1, NetLogEventType::SOCKET_POOL_REUSED_AN_EXISTING_SOCKET)); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 2703 | ExpectSocketClosedWithReason( |
| 2704 | net_log_source2, TransportClientSocketPool::kIdleTimeLimitExpired); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2705 | } |
| 2706 | |
[email protected] | 2041cf34 | 2010-02-19 03:15:59 | [diff] [blame] | 2707 | // Make sure that we process all pending requests even when we're stalling |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2708 | // because of multiple releasing disconnected sockets. |
| 2709 | TEST_F(ClientSocketPoolBaseTest, MultipleReleasingDisconnectedSockets) { |
| 2710 | CreatePoolWithIdleTimeouts( |
| 2711 | kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 2712 | base::TimeDelta(), // Time out unused sockets immediately. |
| 2713 | base::TimeDelta::FromDays(1)); // Don't time out used sockets. |
| 2714 | |
| 2715 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 2716 | |
| 2717 | // Startup 4 connect jobs. Two of them will be pending. |
| 2718 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2719 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2720 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2721 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2722 | TestGroupId("a"), params_, base::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2723 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2724 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2725 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2726 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2727 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2728 | TestCompletionCallback callback2; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2729 | rv = handle2.Init(TestGroupId("a"), params_, base::nullopt, LOWEST, |
| 2730 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2731 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2732 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2733 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2734 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2735 | ClientSocketHandle handle3; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2736 | TestCompletionCallback callback3; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2737 | rv = handle3.Init(TestGroupId("a"), params_, base::nullopt, LOWEST, |
| 2738 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2739 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2740 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2741 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2742 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2743 | ClientSocketHandle handle4; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2744 | TestCompletionCallback callback4; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2745 | rv = handle4.Init(TestGroupId("a"), params_, base::nullopt, LOWEST, |
| 2746 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2747 | callback4.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2748 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2749 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2750 | |
| 2751 | // Release two disconnected sockets. |
| 2752 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2753 | handle.socket()->Disconnect(); |
| 2754 | handle.Reset(); |
| 2755 | handle2.socket()->Disconnect(); |
| 2756 | handle2.Reset(); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2757 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2758 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2759 | EXPECT_FALSE(handle3.is_reused()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2760 | EXPECT_THAT(callback4.WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2761 | EXPECT_FALSE(handle4.is_reused()); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2762 | } |
| 2763 | |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2764 | // Regression test for http://crbug.com/42267. |
| 2765 | // When DoReleaseSocket() is processed for one socket, it is blocked because the |
| 2766 | // other stalled groups all have releasing sockets, so no progress can be made. |
| 2767 | TEST_F(ClientSocketPoolBaseTest, SocketLimitReleasingSockets) { |
| 2768 | CreatePoolWithIdleTimeouts( |
| 2769 | 4 /* socket limit */, 4 /* socket limit per group */, |
| 2770 | base::TimeDelta(), // Time out unused sockets immediately. |
| 2771 | base::TimeDelta::FromDays(1)); // Don't time out used sockets. |
| 2772 | |
| 2773 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 2774 | |
| 2775 | // Max out the socket limit with 2 per group. |
| 2776 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2777 | ClientSocketHandle handle_a[4]; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2778 | TestCompletionCallback callback_a[4]; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2779 | ClientSocketHandle handle_b[4]; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2780 | TestCompletionCallback callback_b[4]; |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2781 | |
| 2782 | for (int i = 0; i < 2; ++i) { |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2783 | EXPECT_EQ(OK, handle_a[i].Init(TestGroupId("a"), params_, base::nullopt, |
| 2784 | LOWEST, SocketTag(), |
| 2785 | ClientSocketPool::RespectLimits::ENABLED, |
| 2786 | callback_a[i].callback(), |
| 2787 | ClientSocketPool::ProxyAuthCallback(), |
| 2788 | pool_.get(), NetLogWithSource())); |
| 2789 | EXPECT_EQ(OK, handle_b[i].Init(TestGroupId("b"), params_, base::nullopt, |
| 2790 | LOWEST, SocketTag(), |
| 2791 | ClientSocketPool::RespectLimits::ENABLED, |
| 2792 | callback_b[i].callback(), |
| 2793 | ClientSocketPool::ProxyAuthCallback(), |
| 2794 | pool_.get(), NetLogWithSource())); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2795 | } |
[email protected] | b89f7e4 | 2010-05-20 20:37:00 | [diff] [blame] | 2796 | |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2797 | // Make 4 pending requests, 2 per group. |
| 2798 | |
| 2799 | for (int i = 2; i < 4; ++i) { |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2800 | EXPECT_EQ( |
| 2801 | ERR_IO_PENDING, |
| 2802 | handle_a[i].Init(TestGroupId("a"), params_, base::nullopt, LOWEST, |
| 2803 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2804 | callback_a[i].callback(), |
| 2805 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2806 | NetLogWithSource())); |
| 2807 | EXPECT_EQ( |
| 2808 | ERR_IO_PENDING, |
| 2809 | handle_b[i].Init(TestGroupId("b"), params_, base::nullopt, LOWEST, |
| 2810 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2811 | callback_b[i].callback(), |
| 2812 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2813 | NetLogWithSource())); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2814 | } |
| 2815 | |
| 2816 | // Release b's socket first. The order is important, because in |
| 2817 | // DoReleaseSocket(), we'll process b's released socket, and since both b and |
| 2818 | // a are stalled, but 'a' is lower lexicographically, we'll process group 'a' |
| 2819 | // first, which has a releasing socket, so it refuses to start up another |
| 2820 | // ConnectJob. So, we used to infinite loop on this. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2821 | handle_b[0].socket()->Disconnect(); |
| 2822 | handle_b[0].Reset(); |
| 2823 | handle_a[0].socket()->Disconnect(); |
| 2824 | handle_a[0].Reset(); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2825 | |
| 2826 | // Used to get stuck here. |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 2827 | base::RunLoop().RunUntilIdle(); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2828 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2829 | handle_b[1].socket()->Disconnect(); |
| 2830 | handle_b[1].Reset(); |
| 2831 | handle_a[1].socket()->Disconnect(); |
| 2832 | handle_a[1].Reset(); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2833 | |
| 2834 | for (int i = 2; i < 4; ++i) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2835 | EXPECT_THAT(callback_b[i].WaitForResult(), IsOk()); |
| 2836 | EXPECT_THAT(callback_a[i].WaitForResult(), IsOk()); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2837 | } |
| 2838 | } |
| 2839 | |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2840 | TEST_F(ClientSocketPoolBaseTest, |
| 2841 | ReleasingDisconnectedSocketsMaintainsPriorityOrder) { |
| 2842 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2843 | |
| 2844 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2845 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2846 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2847 | IsError(ERR_IO_PENDING)); |
| 2848 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2849 | IsError(ERR_IO_PENDING)); |
| 2850 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2851 | IsError(ERR_IO_PENDING)); |
| 2852 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2853 | IsError(ERR_IO_PENDING)); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2854 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2855 | EXPECT_THAT((*requests())[0]->WaitForResult(), IsOk()); |
| 2856 | EXPECT_THAT((*requests())[1]->WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2857 | EXPECT_EQ(2u, completion_count()); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2858 | |
| 2859 | // Releases one connection. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2860 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::NO_KEEP_ALIVE)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2861 | EXPECT_THAT((*requests())[2]->WaitForResult(), IsOk()); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2862 | |
[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())[3]->WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2865 | EXPECT_EQ(4u, completion_count()); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2866 | |
| 2867 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 2868 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 2869 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 2870 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 2871 | |
| 2872 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2873 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(5)); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2874 | } |
| 2875 | |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2876 | class TestReleasingSocketRequest : public TestCompletionCallbackBase { |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2877 | public: |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2878 | TestReleasingSocketRequest(TransportClientSocketPool* pool, |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2879 | int expected_result, |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2880 | bool reset_releasing_handle) |
| 2881 | : pool_(pool), |
| 2882 | expected_result_(expected_result), |
Bence Béky | 8ddc249 | 2018-06-13 01:02:04 | [diff] [blame] | 2883 | reset_releasing_handle_(reset_releasing_handle) {} |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2884 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 2885 | ~TestReleasingSocketRequest() override = default; |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2886 | |
| 2887 | ClientSocketHandle* handle() { return &handle_; } |
| 2888 | |
Bence Béky | 8ddc249 | 2018-06-13 01:02:04 | [diff] [blame] | 2889 | CompletionOnceCallback callback() { |
| 2890 | return base::BindOnce(&TestReleasingSocketRequest::OnComplete, |
| 2891 | base::Unretained(this)); |
| 2892 | } |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2893 | |
| 2894 | private: |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2895 | void OnComplete(int result) { |
| 2896 | SetResult(result); |
| 2897 | if (reset_releasing_handle_) |
| 2898 | handle_.Reset(); |
| 2899 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2900 | EXPECT_EQ( |
| 2901 | expected_result_, |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 2902 | handle2_.Init( |
| 2903 | TestGroupId("a"), |
| 2904 | ClientSocketPool::SocketParams::CreateForHttpForTesting(), |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2905 | base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 2906 | ClientSocketPool::RespectLimits::ENABLED, CompletionOnceCallback(), |
| 2907 | ClientSocketPool::ProxyAuthCallback(), pool_, NetLogWithSource())); |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2908 | } |
| 2909 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2910 | TransportClientSocketPool* const pool_; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2911 | int expected_result_; |
| 2912 | bool reset_releasing_handle_; |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2913 | ClientSocketHandle handle_; |
| 2914 | ClientSocketHandle handle2_; |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2915 | }; |
| 2916 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2917 | |
| 2918 | TEST_F(ClientSocketPoolBaseTest, AdditionalErrorSocketsDontUseSlot) { |
| 2919 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2920 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2921 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
| 2922 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 2923 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2924 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2925 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2926 | client_socket_factory_.allocation_count()); |
| 2927 | |
| 2928 | connect_job_factory_->set_job_type( |
| 2929 | TestConnectJob::kMockPendingAdditionalErrorStateJob); |
| 2930 | TestReleasingSocketRequest req(pool_.get(), OK, false); |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2931 | EXPECT_EQ(ERR_IO_PENDING, |
| 2932 | req.handle()->Init( |
| 2933 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2934 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2935 | req.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2936 | pool_.get(), NetLogWithSource())); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2937 | // The next job should complete synchronously |
| 2938 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 2939 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2940 | EXPECT_THAT(req.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2941 | EXPECT_FALSE(req.handle()->is_initialized()); |
| 2942 | EXPECT_FALSE(req.handle()->socket()); |
| 2943 | EXPECT_TRUE(req.handle()->is_ssl_error()); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 2944 | EXPECT_TRUE(req.handle()->ssl_cert_request_info()); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2945 | } |
| 2946 | |
[email protected] | b6501d3d | 2010-06-03 23:53:34 | [diff] [blame] | 2947 | // http://crbug.com/44724 regression test. |
| 2948 | // We start releasing the pool when we flush on network change. When that |
| 2949 | // happens, the only active references are in the ClientSocketHandles. When a |
| 2950 | // ConnectJob completes and calls back into the last ClientSocketHandle, that |
| 2951 | // callback can release the last reference and delete the pool. After the |
| 2952 | // callback finishes, we go back to the stack frame within the now-deleted pool. |
| 2953 | // Executing any code that refers to members of the now-deleted pool can cause |
| 2954 | // crashes. |
| 2955 | TEST_F(ClientSocketPoolBaseTest, CallbackThatReleasesPool) { |
| 2956 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2957 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
| 2958 | |
| 2959 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2960 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2961 | EXPECT_EQ( |
| 2962 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2963 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2964 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2965 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2966 | pool_.get(), NetLogWithSource())); |
[email protected] | b6501d3d | 2010-06-03 23:53:34 | [diff] [blame] | 2967 | |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 2968 | pool_->FlushWithError(ERR_NETWORK_CHANGED, "Network changed"); |
[email protected] | b6501d3d | 2010-06-03 23:53:34 | [diff] [blame] | 2969 | |
| 2970 | // We'll call back into this now. |
| 2971 | callback.WaitForResult(); |
| 2972 | } |
| 2973 | |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2974 | TEST_F(ClientSocketPoolBaseTest, DoNotReuseSocketAfterFlush) { |
| 2975 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2976 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2977 | |
| 2978 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2979 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2980 | EXPECT_EQ( |
| 2981 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2982 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2983 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2984 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2985 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2986 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2987 | EXPECT_EQ(ClientSocketHandle::UNUSED, handle.reuse_type()); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 2988 | NetLogSource source = handle.socket()->NetLog().source(); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2989 | |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 2990 | pool_->FlushWithError(ERR_NETWORK_CHANGED, "Network changed"); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2991 | |
| 2992 | handle.Reset(); |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 2993 | base::RunLoop().RunUntilIdle(); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2994 | |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2995 | EXPECT_EQ( |
| 2996 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2997 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2998 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2999 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3000 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3001 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 3002 | EXPECT_EQ(ClientSocketHandle::UNUSED, handle.reuse_type()); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 3003 | |
| 3004 | ExpectSocketClosedWithReason( |
| 3005 | source, TransportClientSocketPool::kSocketGenerationOutOfDate); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 3006 | } |
| 3007 | |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3008 | class ConnectWithinCallback : public TestCompletionCallbackBase { |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 3009 | public: |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3010 | ConnectWithinCallback( |
| 3011 | const ClientSocketPool::GroupId& group_id, |
Matt Menke | 84d11e56 | 2019-03-27 00:11:19 | [diff] [blame] | 3012 | const scoped_refptr<ClientSocketPool::SocketParams>& params, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3013 | TransportClientSocketPool* pool) |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3014 | : group_id_(group_id), params_(params), pool_(pool) {} |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 3015 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 3016 | ~ConnectWithinCallback() override = default; |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 3017 | |
| 3018 | int WaitForNestedResult() { |
| 3019 | return nested_callback_.WaitForResult(); |
| 3020 | } |
| 3021 | |
Bence Béky | 8ddc249 | 2018-06-13 01:02:04 | [diff] [blame] | 3022 | CompletionOnceCallback callback() { |
| 3023 | return base::BindOnce(&ConnectWithinCallback::OnComplete, |
| 3024 | base::Unretained(this)); |
| 3025 | } |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3026 | |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 3027 | private: |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3028 | void OnComplete(int result) { |
| 3029 | SetResult(result); |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3030 | EXPECT_EQ( |
| 3031 | ERR_IO_PENDING, |
| 3032 | handle_.Init(group_id_, params_, base::nullopt, DEFAULT_PRIORITY, |
| 3033 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 3034 | nested_callback_.callback(), |
| 3035 | ClientSocketPool::ProxyAuthCallback(), pool_, |
| 3036 | NetLogWithSource())); |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3037 | } |
| 3038 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3039 | const ClientSocketPool::GroupId group_id_; |
Matt Menke | 84d11e56 | 2019-03-27 00:11:19 | [diff] [blame] | 3040 | const scoped_refptr<ClientSocketPool::SocketParams> params_; |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3041 | TransportClientSocketPool* const pool_; |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 3042 | ClientSocketHandle handle_; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3043 | TestCompletionCallback nested_callback_; |
| 3044 | |
| 3045 | DISALLOW_COPY_AND_ASSIGN(ConnectWithinCallback); |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 3046 | }; |
| 3047 | |
| 3048 | TEST_F(ClientSocketPoolBaseTest, AbortAllRequestsOnFlush) { |
| 3049 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3050 | |
| 3051 | // First job will be waiting until it gets aborted. |
| 3052 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3053 | |
| 3054 | ClientSocketHandle handle; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3055 | ConnectWithinCallback callback(TestGroupId("a"), params_, pool_.get()); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3056 | EXPECT_EQ( |
| 3057 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3058 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3059 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 3060 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3061 | pool_.get(), NetLogWithSource())); |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 3062 | |
| 3063 | // Second job will be started during the first callback, and will |
| 3064 | // asynchronously complete with OK. |
| 3065 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 3066 | pool_->FlushWithError(ERR_NETWORK_CHANGED, "Network changed"); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3067 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_NETWORK_CHANGED)); |
| 3068 | EXPECT_THAT(callback.WaitForNestedResult(), IsOk()); |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 3069 | } |
| 3070 | |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 3071 | TEST_F(ClientSocketPoolBaseTest, BackupSocketWaitsForHostResolution) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3072 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets, |
| 3073 | true /* enable_backup_connect_jobs */); |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 3074 | |
| 3075 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3076 | ClientSocketHandle handle; |
| 3077 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3078 | EXPECT_EQ( |
| 3079 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3080 | handle.Init(TestGroupId("bar"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3081 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 3082 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3083 | pool_.get(), NetLogWithSource())); |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 3084 | // The backup timer fires but doesn't start a new ConnectJob while resolving |
| 3085 | // the hostname. |
| 3086 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_RESOLVING_HOST); |
| 3087 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
| 3088 | ClientSocketPool::kMaxConnectRetryIntervalMs * 100)); |
| 3089 | EXPECT_EQ(1, client_socket_factory_.allocation_count()); |
| 3090 | |
| 3091 | // Once the ConnectJob has finished resolving the hostname, the backup timer |
| 3092 | // will create a ConnectJob when it fires. |
| 3093 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_CONNECTING); |
| 3094 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
| 3095 | ClientSocketPool::kMaxConnectRetryIntervalMs)); |
| 3096 | EXPECT_EQ(2, client_socket_factory_.allocation_count()); |
| 3097 | } |
| 3098 | |
| 3099 | // Test that no backup socket is created when a ConnectJob connects before it |
| 3100 | // completes. |
| 3101 | TEST_F(ClientSocketPoolBaseTest, NoBackupSocketWhenConnected) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3102 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets, |
| 3103 | true /* enable_backup_connect_jobs */); |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 3104 | |
| 3105 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3106 | ClientSocketHandle handle; |
| 3107 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3108 | EXPECT_EQ( |
| 3109 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3110 | handle.Init(TestGroupId("bar"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3111 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 3112 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3113 | pool_.get(), NetLogWithSource())); |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 3114 | // The backup timer fires but doesn't start a new ConnectJob while resolving |
| 3115 | // the hostname. |
| 3116 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_RESOLVING_HOST); |
| 3117 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
| 3118 | ClientSocketPool::kMaxConnectRetryIntervalMs * 100)); |
| 3119 | EXPECT_EQ(1, client_socket_factory_.allocation_count()); |
| 3120 | |
| 3121 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE); |
| 3122 | client_socket_factory_.SetJobHasEstablishedConnection(0); |
| 3123 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
| 3124 | ClientSocketPool::kMaxConnectRetryIntervalMs * 100)); |
| 3125 | EXPECT_EQ(1, client_socket_factory_.allocation_count()); |
| 3126 | } |
| 3127 | |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3128 | // Cancel a pending socket request while we're at max sockets, |
| 3129 | // and verify that the backup socket firing doesn't cause a crash. |
| 3130 | TEST_F(ClientSocketPoolBaseTest, BackupSocketCancelAtMaxSockets) { |
| 3131 | // Max 4 sockets globally, max 4 sockets per group. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3132 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets, |
| 3133 | true /* enable_backup_connect_jobs */); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3134 | |
[email protected] | 4baaf9d | 2010-08-31 15:15:44 | [diff] [blame] | 3135 | // Create the first socket and set to ERR_IO_PENDING. This starts the backup |
| 3136 | // timer. |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3137 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3138 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3139 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3140 | EXPECT_EQ( |
| 3141 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3142 | handle.Init(TestGroupId("bar"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3143 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 3144 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3145 | pool_.get(), NetLogWithSource())); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3146 | |
| 3147 | // Start (MaxSockets - 1) connected sockets to reach max sockets. |
| 3148 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 3149 | ClientSocketHandle handles[kDefaultMaxSockets]; |
| 3150 | for (int i = 1; i < kDefaultMaxSockets; ++i) { |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3151 | TestCompletionCallback callback; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3152 | EXPECT_EQ(OK, handles[i].Init(TestGroupId("bar"), params_, base::nullopt, |
| 3153 | DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3154 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3155 | callback.callback(), |
| 3156 | ClientSocketPool::ProxyAuthCallback(), |
| 3157 | pool_.get(), NetLogWithSource())); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3158 | } |
| 3159 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3160 | base::RunLoop().RunUntilIdle(); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3161 | |
| 3162 | // Cancel the pending request. |
| 3163 | handle.Reset(); |
| 3164 | |
| 3165 | // 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] | 3166 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
[email protected] | 26b997396 | 2012-01-28 00:57:00 | [diff] [blame] | 3167 | ClientSocketPool::kMaxConnectRetryIntervalMs / 2 * 3)); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3168 | |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3169 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 3170 | } |
| 3171 | |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3172 | TEST_F(ClientSocketPoolBaseTest, CancelBackupSocketAfterCancelingAllRequests) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3173 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets, |
| 3174 | true /* enable_backup_connect_jobs */); |
[email protected] | 4baaf9d | 2010-08-31 15:15:44 | [diff] [blame] | 3175 | |
| 3176 | // Create the first socket and set to ERR_IO_PENDING. This starts the backup |
| 3177 | // timer. |
| 3178 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3179 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3180 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3181 | EXPECT_EQ( |
| 3182 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3183 | handle.Init(TestGroupId("bar"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3184 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 3185 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3186 | pool_.get(), NetLogWithSource())); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3187 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("bar"))); |
| 3188 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("bar"))); |
| 3189 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3190 | TestGroupId("bar"))); |
| 3191 | EXPECT_EQ( |
| 3192 | 0u, pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("bar"))); |
[email protected] | 4baaf9d | 2010-08-31 15:15:44 | [diff] [blame] | 3193 | |
| 3194 | // Cancel the socket request. This should cancel the backup timer. Wait for |
| 3195 | // the backup time to see if it indeed got canceled. |
| 3196 | handle.Reset(); |
| 3197 | // 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] | 3198 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
[email protected] | 26b997396 | 2012-01-28 00:57:00 | [diff] [blame] | 3199 | ClientSocketPool::kMaxConnectRetryIntervalMs / 2 * 3)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3200 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("bar"))); |
| 3201 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("bar"))); |
[email protected] | 4baaf9d | 2010-08-31 15:15:44 | [diff] [blame] | 3202 | } |
| 3203 | |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3204 | TEST_F(ClientSocketPoolBaseTest, CancelBackupSocketAfterFinishingAllRequests) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3205 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets, |
| 3206 | true /* enable_backup_connect_jobs */); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3207 | |
| 3208 | // Create the first socket and set to ERR_IO_PENDING. This starts the backup |
| 3209 | // timer. |
| 3210 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3211 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3212 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3213 | EXPECT_EQ( |
| 3214 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3215 | handle.Init(TestGroupId("bar"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3216 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 3217 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3218 | pool_.get(), NetLogWithSource())); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3219 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3220 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3221 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3222 | EXPECT_EQ( |
| 3223 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3224 | handle2.Init(TestGroupId("bar"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3225 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3226 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3227 | pool_.get(), NetLogWithSource())); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3228 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("bar"))); |
| 3229 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("bar"))); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3230 | |
| 3231 | // Cancel request 1 and then complete request 2. With the requests finished, |
| 3232 | // the backup timer should be cancelled. |
| 3233 | handle.Reset(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3234 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3235 | // 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] | 3236 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
[email protected] | 26b997396 | 2012-01-28 00:57:00 | [diff] [blame] | 3237 | ClientSocketPool::kMaxConnectRetryIntervalMs / 2 * 3)); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3238 | } |
| 3239 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3240 | // Test delayed socket binding for the case where we have two connects, |
| 3241 | // and while one is waiting on a connect, the other frees up. |
| 3242 | // The socket waiting on a connect should switch immediately to the freed |
| 3243 | // up socket. |
| 3244 | TEST_F(ClientSocketPoolBaseTest, DelayedSocketBindingWaitingForConnect) { |
| 3245 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3246 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3247 | |
| 3248 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3249 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3250 | EXPECT_EQ( |
| 3251 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3252 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3253 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3254 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3255 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3256 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3257 | |
| 3258 | // No idle sockets, no pending jobs. |
| 3259 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3260 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3261 | |
| 3262 | // Create a second socket to the same host, but this one will wait. |
| 3263 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3264 | ClientSocketHandle handle2; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3265 | EXPECT_EQ( |
| 3266 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3267 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3268 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3269 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3270 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3271 | // No idle sockets, and one connecting job. |
| 3272 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3273 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3274 | |
| 3275 | // Return the first handle to the pool. This will initiate the delayed |
| 3276 | // binding. |
| 3277 | handle1.Reset(); |
| 3278 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3279 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3280 | |
| 3281 | // Still no idle sockets, still one pending connect job. |
| 3282 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3283 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3284 | |
| 3285 | // The second socket connected, even though it was a Waiting Job. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3286 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3287 | |
| 3288 | // And we can see there is still one job waiting. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3289 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3290 | |
| 3291 | // Finally, signal the waiting Connect. |
| 3292 | client_socket_factory_.SignalJobs(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3293 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3294 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3295 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3296 | } |
| 3297 | |
| 3298 | // Test delayed socket binding when a group is at capacity and one |
| 3299 | // of the group's sockets frees up. |
| 3300 | TEST_F(ClientSocketPoolBaseTest, DelayedSocketBindingAtGroupCapacity) { |
| 3301 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3302 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3303 | |
| 3304 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3305 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3306 | EXPECT_EQ( |
| 3307 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3308 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3309 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3310 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3311 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3312 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3313 | |
| 3314 | // No idle sockets, no pending jobs. |
| 3315 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3316 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3317 | |
| 3318 | // Create a second socket to the same host, but this one will wait. |
| 3319 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3320 | ClientSocketHandle handle2; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3321 | EXPECT_EQ( |
| 3322 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3323 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3324 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3325 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3326 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3327 | // No idle sockets, and one connecting job. |
| 3328 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3329 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3330 | |
| 3331 | // Return the first handle to the pool. This will initiate the delayed |
| 3332 | // binding. |
| 3333 | handle1.Reset(); |
| 3334 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3335 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3336 | |
| 3337 | // Still no idle sockets, still one pending connect job. |
| 3338 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3339 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3340 | |
| 3341 | // The second socket connected, even though it was a Waiting Job. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3342 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3343 | |
| 3344 | // And we can see there is still one job waiting. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3345 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3346 | |
| 3347 | // Finally, signal the waiting Connect. |
| 3348 | client_socket_factory_.SignalJobs(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3349 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3350 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3351 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3352 | } |
| 3353 | |
| 3354 | // Test out the case where we have one socket connected, one |
| 3355 | // connecting, when the first socket finishes and goes idle. |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3356 | // Although the second connection is pending, the second request |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3357 | // should complete, by taking the first socket's idle socket. |
| 3358 | TEST_F(ClientSocketPoolBaseTest, DelayedSocketBindingAtStall) { |
| 3359 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3360 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3361 | |
| 3362 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3363 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3364 | EXPECT_EQ( |
| 3365 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3366 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3367 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3368 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3369 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3370 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3371 | |
| 3372 | // No idle sockets, no pending jobs. |
| 3373 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3374 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3375 | |
| 3376 | // Create a second socket to the same host, but this one will wait. |
| 3377 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3378 | ClientSocketHandle handle2; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3379 | EXPECT_EQ( |
| 3380 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3381 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3382 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3383 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3384 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3385 | // No idle sockets, and one connecting job. |
| 3386 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3387 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3388 | |
| 3389 | // Return the first handle to the pool. This will initiate the delayed |
| 3390 | // binding. |
| 3391 | handle1.Reset(); |
| 3392 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3393 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3394 | |
| 3395 | // Still no idle sockets, still one pending connect job. |
| 3396 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3397 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3398 | |
| 3399 | // The second socket connected, even though it was a Waiting Job. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3400 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3401 | |
| 3402 | // And we can see there is still one job waiting. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3403 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3404 | |
| 3405 | // Finally, signal the waiting Connect. |
| 3406 | client_socket_factory_.SignalJobs(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3407 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3408 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3409 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3410 | } |
| 3411 | |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3412 | // Cover the case where on an available socket slot, we have one pending |
| 3413 | // request that completes synchronously, thereby making the Group empty. |
| 3414 | TEST_F(ClientSocketPoolBaseTest, SynchronouslyProcessOnePendingRequest) { |
| 3415 | const int kUnlimitedSockets = 100; |
| 3416 | const int kOneSocketPerGroup = 1; |
| 3417 | CreatePool(kUnlimitedSockets, kOneSocketPerGroup); |
| 3418 | |
| 3419 | // Make the first request asynchronous fail. |
| 3420 | // This will free up a socket slot later. |
| 3421 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
| 3422 | |
| 3423 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3424 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3425 | EXPECT_EQ( |
| 3426 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3427 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3428 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3429 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3430 | pool_.get(), NetLogWithSource())); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3431 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3432 | |
| 3433 | // Make the second request synchronously fail. This should make the Group |
| 3434 | // empty. |
| 3435 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
| 3436 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3437 | TestCompletionCallback callback2; |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3438 | // It'll be ERR_IO_PENDING now, but the TestConnectJob will synchronously fail |
| 3439 | // when created. |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3440 | EXPECT_EQ( |
| 3441 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3442 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3443 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3444 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3445 | pool_.get(), NetLogWithSource())); |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3446 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3447 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3448 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3449 | EXPECT_THAT(callback1.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 3450 | EXPECT_THAT(callback2.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3451 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3452 | } |
| 3453 | |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3454 | TEST_F(ClientSocketPoolBaseTest, PreferUsedSocketToUnusedSocket) { |
| 3455 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 3456 | |
| 3457 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3458 | |
| 3459 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3460 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3461 | EXPECT_EQ( |
| 3462 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3463 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3464 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3465 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3466 | pool_.get(), NetLogWithSource())); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3467 | |
| 3468 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3469 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3470 | EXPECT_EQ( |
| 3471 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3472 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3473 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3474 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3475 | pool_.get(), NetLogWithSource())); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3476 | ClientSocketHandle handle3; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3477 | TestCompletionCallback callback3; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3478 | EXPECT_EQ( |
| 3479 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3480 | handle3.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3481 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3482 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3483 | pool_.get(), NetLogWithSource())); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3484 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3485 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3486 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
| 3487 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3488 | |
| 3489 | // Use the socket. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 3490 | EXPECT_EQ(1, handle1.socket()->Write(nullptr, 1, CompletionOnceCallback(), |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 3491 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 3492 | EXPECT_EQ(1, handle3.socket()->Write(nullptr, 1, CompletionOnceCallback(), |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 3493 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3494 | |
| 3495 | handle1.Reset(); |
| 3496 | handle2.Reset(); |
| 3497 | handle3.Reset(); |
| 3498 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3499 | EXPECT_EQ(OK, handle1.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3500 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3501 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3502 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3503 | pool_.get(), NetLogWithSource())); |
| 3504 | EXPECT_EQ(OK, handle2.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3505 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3506 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3507 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3508 | pool_.get(), NetLogWithSource())); |
| 3509 | EXPECT_EQ(OK, handle3.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3510 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3511 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3512 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3513 | pool_.get(), NetLogWithSource())); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3514 | |
| 3515 | EXPECT_TRUE(handle1.socket()->WasEverUsed()); |
| 3516 | EXPECT_TRUE(handle2.socket()->WasEverUsed()); |
| 3517 | EXPECT_FALSE(handle3.socket()->WasEverUsed()); |
| 3518 | } |
| 3519 | |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3520 | TEST_F(ClientSocketPoolBaseTest, RequestSockets) { |
| 3521 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3522 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3523 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3524 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 3525 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3526 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3527 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3528 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3529 | EXPECT_EQ(2u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3530 | TestGroupId("a"))); |
| 3531 | EXPECT_EQ(2u, |
| 3532 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3533 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3534 | |
| 3535 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3536 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3537 | EXPECT_EQ( |
| 3538 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3539 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3540 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3541 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3542 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3543 | |
| 3544 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3545 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3546 | EXPECT_EQ( |
| 3547 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3548 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3549 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3550 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3551 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3552 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3553 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3554 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3555 | TestGroupId("a"))); |
| 3556 | EXPECT_EQ(0u, |
| 3557 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3558 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3559 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3560 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3561 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3562 | handle1.Reset(); |
| 3563 | handle2.Reset(); |
| 3564 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3565 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3566 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3567 | TestGroupId("a"))); |
| 3568 | EXPECT_EQ(0u, |
| 3569 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3570 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3571 | } |
| 3572 | |
| 3573 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsWhenAlreadyHaveAConnectJob) { |
| 3574 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3575 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3576 | |
| 3577 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3578 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3579 | EXPECT_EQ( |
| 3580 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3581 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3582 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3583 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3584 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3585 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3586 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3587 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3588 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3589 | TestGroupId("a"))); |
| 3590 | EXPECT_EQ(0u, |
| 3591 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3592 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3593 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3594 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 3595 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3596 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3597 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3598 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3599 | TestGroupId("a"))); |
| 3600 | EXPECT_EQ(1u, |
| 3601 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3602 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3603 | |
| 3604 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3605 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3606 | EXPECT_EQ( |
| 3607 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3608 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3609 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3610 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3611 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3612 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3613 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3614 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3615 | TestGroupId("a"))); |
| 3616 | EXPECT_EQ(0u, |
| 3617 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3618 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3619 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3620 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3621 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3622 | handle1.Reset(); |
| 3623 | handle2.Reset(); |
| 3624 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3625 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3626 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3627 | TestGroupId("a"))); |
| 3628 | EXPECT_EQ(0u, |
| 3629 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3630 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3631 | } |
| 3632 | |
| 3633 | TEST_F(ClientSocketPoolBaseTest, |
| 3634 | RequestSocketsWhenAlreadyHaveMultipleConnectJob) { |
| 3635 | CreatePool(4, 4); |
| 3636 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3637 | |
| 3638 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3639 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3640 | EXPECT_EQ( |
| 3641 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3642 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3643 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3644 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3645 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3646 | |
| 3647 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3648 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3649 | EXPECT_EQ( |
| 3650 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3651 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3652 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3653 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3654 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3655 | |
| 3656 | ClientSocketHandle handle3; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3657 | TestCompletionCallback callback3; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3658 | EXPECT_EQ( |
| 3659 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3660 | handle3.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3661 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3662 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3663 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3664 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3665 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3666 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3667 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3668 | TestGroupId("a"))); |
| 3669 | EXPECT_EQ(0u, |
| 3670 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3671 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3672 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3673 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 3674 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3675 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3676 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3677 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3678 | TestGroupId("a"))); |
| 3679 | EXPECT_EQ(0u, |
| 3680 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3681 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3682 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3683 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3684 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
| 3685 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3686 | handle1.Reset(); |
| 3687 | handle2.Reset(); |
| 3688 | handle3.Reset(); |
| 3689 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3690 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3691 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3692 | TestGroupId("a"))); |
| 3693 | EXPECT_EQ(0u, |
| 3694 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3695 | EXPECT_EQ(3u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3696 | } |
| 3697 | |
| 3698 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsAtMaxSocketLimit) { |
| 3699 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 3700 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3701 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3702 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3703 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3704 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, |
| 3705 | kDefaultMaxSockets, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3706 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3707 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3708 | EXPECT_EQ(kDefaultMaxSockets, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3709 | static_cast<int>( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3710 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
| 3711 | EXPECT_EQ( |
| 3712 | kDefaultMaxSockets, |
| 3713 | static_cast<int>(pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3714 | TestGroupId("a")))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3715 | EXPECT_EQ(kDefaultMaxSockets, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3716 | static_cast<int>(pool_->NumUnassignedConnectJobsInGroupForTesting( |
| 3717 | TestGroupId("a")))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3718 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3719 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("b"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3720 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3721 | pool_->RequestSockets(TestGroupId("b"), params_, base::nullopt, |
| 3722 | kDefaultMaxSockets, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3723 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3724 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("b"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3725 | } |
| 3726 | |
| 3727 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsHitMaxSocketLimit) { |
| 3728 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 3729 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3730 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3731 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3732 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3733 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, |
| 3734 | kDefaultMaxSockets - 1, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3735 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3736 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3737 | EXPECT_EQ(kDefaultMaxSockets - 1, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3738 | static_cast<int>( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3739 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
| 3740 | EXPECT_EQ( |
| 3741 | kDefaultMaxSockets - 1, |
| 3742 | static_cast<int>(pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3743 | TestGroupId("a")))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3744 | EXPECT_EQ(kDefaultMaxSockets - 1, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3745 | static_cast<int>(pool_->NumUnassignedConnectJobsInGroupForTesting( |
| 3746 | TestGroupId("a")))); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 3747 | EXPECT_FALSE(pool_->IsStalled()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3748 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3749 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("b"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3750 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3751 | pool_->RequestSockets(TestGroupId("b"), params_, base::nullopt, |
| 3752 | kDefaultMaxSockets, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3753 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3754 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("b"))); |
| 3755 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 3756 | EXPECT_FALSE(pool_->IsStalled()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3757 | } |
| 3758 | |
| 3759 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsCountIdleSockets) { |
| 3760 | CreatePool(4, 4); |
| 3761 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3762 | |
| 3763 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3764 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3765 | EXPECT_EQ( |
| 3766 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3767 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3768 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3769 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3770 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3771 | ASSERT_THAT(callback1.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3772 | handle1.Reset(); |
| 3773 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3774 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3775 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3776 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3777 | TestGroupId("a"))); |
| 3778 | EXPECT_EQ(0u, |
| 3779 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3780 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3781 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3782 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 3783 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3784 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3785 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3786 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3787 | TestGroupId("a"))); |
| 3788 | EXPECT_EQ(1u, |
| 3789 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3790 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3791 | } |
| 3792 | |
| 3793 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsCountActiveSockets) { |
| 3794 | CreatePool(4, 4); |
| 3795 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3796 | |
| 3797 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3798 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3799 | EXPECT_EQ( |
| 3800 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3801 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3802 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3803 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3804 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3805 | ASSERT_THAT(callback1.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3806 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3807 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3808 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3809 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3810 | TestGroupId("a"))); |
| 3811 | EXPECT_EQ(0u, |
| 3812 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3813 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3814 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3815 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3816 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 3817 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3818 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3819 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3820 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3821 | TestGroupId("a"))); |
| 3822 | EXPECT_EQ(1u, |
| 3823 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3824 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3825 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3826 | } |
| 3827 | |
| 3828 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsSynchronous) { |
| 3829 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3830 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 3831 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3832 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, |
| 3833 | kDefaultMaxSocketsPerGroup, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3834 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3835 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3836 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3837 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3838 | TestGroupId("a"))); |
| 3839 | EXPECT_EQ(0u, |
| 3840 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3841 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3842 | static_cast<int>(pool_->IdleSocketCountInGroup(TestGroupId("a")))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3843 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3844 | pool_->RequestSockets(TestGroupId("b"), params_, base::nullopt, |
| 3845 | kDefaultMaxSocketsPerGroup, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3846 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3847 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 3848 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3849 | TestGroupId("b"))); |
| 3850 | EXPECT_EQ(0u, |
| 3851 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3852 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3853 | static_cast<int>(pool_->IdleSocketCountInGroup(TestGroupId("b")))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3854 | } |
| 3855 | |
[email protected] | 3c819f52 | 2010-12-02 02:03:12 | [diff] [blame] | 3856 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsSynchronousError) { |
| 3857 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3858 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
| 3859 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3860 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, |
| 3861 | kDefaultMaxSocketsPerGroup, NetLogWithSource()); |
[email protected] | 3c819f52 | 2010-12-02 02:03:12 | [diff] [blame] | 3862 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3863 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | fd2e53e | 2011-01-14 20:40:52 | [diff] [blame] | 3864 | |
| 3865 | connect_job_factory_->set_job_type( |
| 3866 | TestConnectJob::kMockAdditionalErrorStateJob); |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3867 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, |
| 3868 | kDefaultMaxSocketsPerGroup, NetLogWithSource()); |
[email protected] | fd2e53e | 2011-01-14 20:40:52 | [diff] [blame] | 3869 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3870 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | 3c819f52 | 2010-12-02 02:03:12 | [diff] [blame] | 3871 | } |
| 3872 | |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3873 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsMultipleTimesDoesNothing) { |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3874 | CreatePool(4, 4); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3875 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3876 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3877 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 3878 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3879 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3880 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3881 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3882 | EXPECT_EQ(2u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3883 | TestGroupId("a"))); |
| 3884 | EXPECT_EQ(2u, |
| 3885 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3886 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3887 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3888 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3889 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 3890 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3891 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3892 | EXPECT_EQ(2u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3893 | TestGroupId("a"))); |
| 3894 | EXPECT_EQ(2u, |
| 3895 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3896 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3897 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3898 | |
| 3899 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3900 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3901 | EXPECT_EQ( |
| 3902 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3903 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3904 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3905 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3906 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3907 | |
| 3908 | client_socket_factory_.SignalJob(0); |
| 3909 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3910 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3911 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3912 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3913 | TestGroupId("a"))); |
| 3914 | EXPECT_EQ(1u, |
| 3915 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3916 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3917 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3918 | |
| 3919 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3920 | TestCompletionCallback callback2; |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3921 | EXPECT_EQ( |
| 3922 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3923 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3924 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3925 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3926 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3927 | client_socket_factory_.SignalJob(0); |
| 3928 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3929 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3930 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3931 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3932 | TestGroupId("a"))); |
| 3933 | EXPECT_EQ(0u, |
| 3934 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3935 | EXPECT_EQ(2, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3936 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3937 | |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3938 | handle1.Reset(); |
| 3939 | handle2.Reset(); |
| 3940 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3941 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3942 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3943 | TestGroupId("a"))); |
| 3944 | EXPECT_EQ(0u, |
| 3945 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3946 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3947 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3948 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3949 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 3950 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3951 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3952 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3953 | TestGroupId("a"))); |
| 3954 | EXPECT_EQ(0u, |
| 3955 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3956 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3957 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3958 | } |
| 3959 | |
| 3960 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsDifferentNumSockets) { |
| 3961 | CreatePool(4, 4); |
| 3962 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3963 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3964 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 1, |
| 3965 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3966 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3967 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3968 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3969 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3970 | TestGroupId("a"))); |
| 3971 | EXPECT_EQ(1u, |
| 3972 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3973 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3974 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3975 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 3976 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3977 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3978 | EXPECT_EQ(2u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3979 | TestGroupId("a"))); |
| 3980 | EXPECT_EQ(2u, |
| 3981 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3982 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3983 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3984 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 3, |
| 3985 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3986 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3987 | EXPECT_EQ(3u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3988 | TestGroupId("a"))); |
| 3989 | EXPECT_EQ(3u, |
| 3990 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3991 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3992 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3993 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 1, |
| 3994 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3995 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3996 | EXPECT_EQ(3u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3997 | TestGroupId("a"))); |
| 3998 | EXPECT_EQ(3u, |
| 3999 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4000 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4001 | } |
| 4002 | |
| 4003 | TEST_F(ClientSocketPoolBaseTest, PreconnectJobsTakenByNormalRequests) { |
| 4004 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4005 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4006 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4007 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 1, |
| 4008 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4009 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4010 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 4011 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4012 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4013 | TestGroupId("a"))); |
| 4014 | EXPECT_EQ(1u, |
| 4015 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4016 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4017 | |
| 4018 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 4019 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4020 | EXPECT_EQ( |
| 4021 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4022 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4023 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4024 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4025 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4026 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4027 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4028 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4029 | TestGroupId("a"))); |
| 4030 | EXPECT_EQ(0u, |
| 4031 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4032 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4033 | |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4034 | client_socket_factory_.SignalJobs(); |
| 4035 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 4036 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4037 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4038 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4039 | TestGroupId("a"))); |
| 4040 | EXPECT_EQ(0u, |
| 4041 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4042 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4043 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4044 | |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4045 | // Make sure if a preconnected socket is not fully connected when a request |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 4046 | // starts, it has a connect start time. |
| 4047 | TestLoadTimingInfoConnectedNotReused(handle1); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4048 | handle1.Reset(); |
| 4049 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4050 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4051 | } |
| 4052 | |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 4053 | // Checks that fully connected preconnect jobs have no connect times, and are |
| 4054 | // marked as reused. |
| 4055 | TEST_F(ClientSocketPoolBaseTest, ConnectedPreconnectJobsHaveNoConnectTimes) { |
| 4056 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4057 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4058 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 1, |
| 4059 | NetLogWithSource()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 4060 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4061 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 4062 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4063 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4064 | TestGroupId("a"))); |
| 4065 | EXPECT_EQ(0u, |
| 4066 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4067 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 4068 | |
| 4069 | ClientSocketHandle handle; |
| 4070 | TestCompletionCallback callback; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4071 | EXPECT_EQ(OK, handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4072 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4073 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4074 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4075 | pool_.get(), NetLogWithSource())); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 4076 | |
| 4077 | // Make sure the idle socket was used. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4078 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 4079 | |
| 4080 | TestLoadTimingInfoConnectedReused(handle); |
| 4081 | handle.Reset(); |
| 4082 | TestLoadTimingInfoNotConnected(handle); |
| 4083 | } |
| 4084 | |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4085 | // http://crbug.com/64940 regression test. |
| 4086 | TEST_F(ClientSocketPoolBaseTest, PreconnectClosesIdleSocketRemovesGroup) { |
| 4087 | const int kMaxTotalSockets = 3; |
| 4088 | const int kMaxSocketsPerGroup = 2; |
| 4089 | CreatePool(kMaxTotalSockets, kMaxSocketsPerGroup); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4090 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4091 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 4092 | // Note that group id ordering matters here. "a" comes before "b", so |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4093 | // CloseOneIdleSocket() will try to close "a"'s idle socket. |
| 4094 | |
| 4095 | // Set up one idle socket in "a". |
| 4096 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 4097 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4098 | EXPECT_EQ( |
| 4099 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4100 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4101 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4102 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4103 | pool_.get(), NetLogWithSource())); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4104 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 4105 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4106 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4107 | TestGroupId("a"))); |
| 4108 | EXPECT_EQ(0u, |
| 4109 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4110 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4111 | |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4112 | client_socket_factory_.SignalJobs(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4113 | ASSERT_THAT(callback1.WaitForResult(), IsOk()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4114 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4115 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4116 | TestGroupId("a"))); |
| 4117 | EXPECT_EQ(0u, |
| 4118 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4119 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4120 | |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4121 | handle1.Reset(); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4122 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4123 | |
| 4124 | // Set up two active sockets in "b". |
| 4125 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 4126 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4127 | EXPECT_EQ( |
| 4128 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4129 | handle1.Init(TestGroupId("b"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4130 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4131 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4132 | pool_.get(), NetLogWithSource())); |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4133 | EXPECT_EQ( |
| 4134 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4135 | handle2.Init(TestGroupId("b"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4136 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4137 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4138 | pool_.get(), NetLogWithSource())); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4139 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4140 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("b"))); |
| 4141 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 4142 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4143 | TestGroupId("b"))); |
| 4144 | EXPECT_EQ(0u, |
| 4145 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4146 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4147 | |
| 4148 | client_socket_factory_.SignalJobs(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4149 | ASSERT_THAT(callback1.WaitForResult(), IsOk()); |
| 4150 | ASSERT_THAT(callback2.WaitForResult(), IsOk()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4151 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4152 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4153 | TestGroupId("b"))); |
| 4154 | EXPECT_EQ(0u, |
| 4155 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 4156 | EXPECT_EQ(2, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4157 | |
| 4158 | // Now we have 1 idle socket in "a" and 2 active sockets in "b". This means |
| 4159 | // we've maxed out on sockets, since we set |kMaxTotalSockets| to 3. |
| 4160 | // Requesting 2 preconnected sockets for "a" should fail to allocate any more |
| 4161 | // sockets for "a", and "b" should still have 2 active sockets. |
| 4162 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4163 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 4164 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4165 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4166 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4167 | TestGroupId("a"))); |
| 4168 | EXPECT_EQ(0u, |
| 4169 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4170 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4171 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 4172 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 4173 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4174 | TestGroupId("b"))); |
| 4175 | EXPECT_EQ(0u, |
| 4176 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4177 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4178 | EXPECT_EQ(2, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4179 | |
| 4180 | // Now release the 2 active sockets for "b". This will give us 1 idle socket |
| 4181 | // in "a" and 2 idle sockets in "b". Requesting 2 preconnected sockets for |
| 4182 | // "a" should result in closing 1 for "b". |
| 4183 | handle1.Reset(); |
| 4184 | handle2.Reset(); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4185 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4186 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4187 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4188 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 4189 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4190 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4191 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4192 | TestGroupId("a"))); |
| 4193 | EXPECT_EQ(1u, |
| 4194 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4195 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4196 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 4197 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 4198 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4199 | TestGroupId("b"))); |
| 4200 | EXPECT_EQ(0u, |
| 4201 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4202 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4203 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4204 | } |
| 4205 | |
[email protected] | b7b8be4 | 2011-07-12 12:46:41 | [diff] [blame] | 4206 | TEST_F(ClientSocketPoolBaseTest, PreconnectWithoutBackupJob) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4207 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 4208 | true /* enable_backup_connect_jobs */); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4209 | |
| 4210 | // Make the ConnectJob hang until it times out, shorten the timeout. |
| 4211 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4212 | connect_job_factory_->set_timeout_duration( |
| 4213 | base::TimeDelta::FromMilliseconds(500)); |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4214 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 1, |
| 4215 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4216 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4217 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4218 | TestGroupId("a"))); |
| 4219 | EXPECT_EQ(1u, |
| 4220 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4221 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4222 | |
[email protected] | b7b8be4 | 2011-07-12 12:46:41 | [diff] [blame] | 4223 | // Verify the backup timer doesn't create a backup job, by making |
| 4224 | // the backup job a pending job instead of a waiting job, so it |
| 4225 | // *would* complete if it were created. |
Lukasz Krakowiak | 28dcf9d6 | 2020-06-04 09:46:59 | [diff] [blame] | 4226 | base::RunLoop loop; |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4227 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 4228 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
Lukasz Krakowiak | 85529208 | 2020-06-19 12:43:58 | [diff] [blame] | 4229 | FROM_HERE, loop.QuitClosure(), base::TimeDelta::FromSeconds(1)); |
Lukasz Krakowiak | 28dcf9d6 | 2020-06-04 09:46:59 | [diff] [blame] | 4230 | loop.Run(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4231 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4232 | } |
| 4233 | |
[email protected] | b7b8be4 | 2011-07-12 12:46:41 | [diff] [blame] | 4234 | TEST_F(ClientSocketPoolBaseTest, PreconnectWithBackupJob) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4235 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 4236 | true /* enable_backup_connect_jobs */); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4237 | |
| 4238 | // Make the ConnectJob hang forever. |
| 4239 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4240 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 1, |
| 4241 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4242 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4243 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4244 | TestGroupId("a"))); |
| 4245 | EXPECT_EQ(1u, |
| 4246 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4247 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 4248 | base::RunLoop().RunUntilIdle(); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4249 | |
| 4250 | // Make the backup job be a pending job, so it completes normally. |
| 4251 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 4252 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 4253 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4254 | EXPECT_EQ( |
| 4255 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4256 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4257 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4258 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4259 | pool_.get(), NetLogWithSource())); |
[email protected] | b7b8be4 | 2011-07-12 12:46:41 | [diff] [blame] | 4260 | // Timer has started, but the backup connect job shouldn't be created yet. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4261 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4262 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4263 | TestGroupId("a"))); |
| 4264 | EXPECT_EQ(0u, |
| 4265 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4266 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4267 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4268 | ASSERT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4269 | |
| 4270 | // The hung connect job should still be there, but everything else should be |
| 4271 | // complete. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4272 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4273 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4274 | TestGroupId("a"))); |
| 4275 | EXPECT_EQ(1u, |
| 4276 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4277 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4278 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4279 | } |
| 4280 | |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4281 | // Tests that a preconnect that starts out with unread data can still be used. |
| 4282 | // http://crbug.com/334467 |
| 4283 | TEST_F(ClientSocketPoolBaseTest, PreconnectWithUnreadData) { |
| 4284 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4285 | connect_job_factory_->set_job_type(TestConnectJob::kMockUnreadDataJob); |
| 4286 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4287 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 1, |
| 4288 | NetLogWithSource()); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4289 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4290 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 4291 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4292 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4293 | TestGroupId("a"))); |
| 4294 | EXPECT_EQ(0u, |
| 4295 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4296 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4297 | |
| 4298 | // Fail future jobs to be sure that handle receives the preconnected socket |
| 4299 | // rather than closing it and making a new one. |
| 4300 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
| 4301 | ClientSocketHandle handle; |
| 4302 | TestCompletionCallback callback; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4303 | EXPECT_EQ(OK, handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4304 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4305 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4306 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4307 | pool_.get(), NetLogWithSource())); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4308 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4309 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 4310 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4311 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4312 | TestGroupId("a"))); |
| 4313 | EXPECT_EQ(0u, |
| 4314 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4315 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4316 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4317 | |
| 4318 | // Drain the pending read. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 4319 | EXPECT_EQ(1, handle.socket()->Read(nullptr, 1, CompletionOnceCallback())); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4320 | |
| 4321 | TestLoadTimingInfoConnectedReused(handle); |
| 4322 | handle.Reset(); |
| 4323 | |
| 4324 | // The socket should be usable now that it's idle again. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4325 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4326 | } |
| 4327 | |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4328 | TEST_F(ClientSocketPoolBaseTest, RequestGetsAssignedJob) { |
| 4329 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4330 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4331 | |
| 4332 | ClientSocketHandle handle1; |
| 4333 | TestCompletionCallback callback1; |
| 4334 | EXPECT_EQ( |
| 4335 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4336 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4337 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4338 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4339 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4340 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4341 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4342 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4343 | TestGroupId("a"))); |
| 4344 | EXPECT_EQ(0u, |
| 4345 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4346 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4347 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4348 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4349 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4350 | } |
| 4351 | |
| 4352 | TEST_F(ClientSocketPoolBaseTest, MultipleRequestsGetAssignedJobs) { |
| 4353 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4354 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4355 | |
| 4356 | ClientSocketHandle handle1; |
| 4357 | TestCompletionCallback callback1; |
| 4358 | EXPECT_EQ( |
| 4359 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4360 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4361 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4362 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4363 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4364 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4365 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4366 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4367 | TestGroupId("a"))); |
| 4368 | EXPECT_EQ(0u, |
| 4369 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4370 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4371 | |
| 4372 | ClientSocketHandle handle2; |
| 4373 | TestCompletionCallback callback2; |
| 4374 | EXPECT_EQ( |
| 4375 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4376 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4377 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4378 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4379 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4380 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4381 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4382 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4383 | TestGroupId("a"))); |
| 4384 | EXPECT_EQ(0u, |
| 4385 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4386 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4387 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4388 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4389 | &handle1)); |
| 4390 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4391 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4392 | |
| 4393 | // One job completes. The other request should still have its job. |
| 4394 | client_socket_factory_.SignalJob(0); |
| 4395 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 4396 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4397 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4398 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4399 | TestGroupId("a"))); |
| 4400 | EXPECT_EQ(0u, |
| 4401 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4402 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4403 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4404 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4405 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4406 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4407 | } |
| 4408 | |
| 4409 | TEST_F(ClientSocketPoolBaseTest, PreconnectJobGetsAssignedToRequest) { |
| 4410 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4411 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4412 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4413 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 1, |
| 4414 | NetLogWithSource()); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4415 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4416 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 4417 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4418 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4419 | TestGroupId("a"))); |
| 4420 | EXPECT_EQ(1u, |
| 4421 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4422 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4423 | |
| 4424 | ClientSocketHandle handle1; |
| 4425 | TestCompletionCallback callback1; |
| 4426 | EXPECT_EQ( |
| 4427 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4428 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4429 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4430 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4431 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4432 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4433 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4434 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4435 | TestGroupId("a"))); |
| 4436 | EXPECT_EQ(0u, |
| 4437 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4438 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4439 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4440 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4441 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4442 | } |
| 4443 | |
| 4444 | TEST_F(ClientSocketPoolBaseTest, HigherPriorityRequestStealsJob) { |
| 4445 | CreatePool(kDefaultMaxSockets, 1); |
| 4446 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4447 | |
| 4448 | ClientSocketHandle handle1; |
| 4449 | TestCompletionCallback callback1; |
| 4450 | EXPECT_EQ( |
| 4451 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4452 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4453 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4454 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4455 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4456 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4457 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4458 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4459 | TestGroupId("a"))); |
| 4460 | EXPECT_EQ(0u, |
| 4461 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4462 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4463 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4464 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4465 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4466 | |
| 4467 | // Insert a higher priority request |
| 4468 | ClientSocketHandle handle2; |
| 4469 | TestCompletionCallback callback2; |
| 4470 | EXPECT_EQ( |
| 4471 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4472 | handle2.Init(TestGroupId("a"), params_, base::nullopt, HIGHEST, |
| 4473 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4474 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4475 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4476 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4477 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4478 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4479 | TestGroupId("a"))); |
| 4480 | EXPECT_EQ(0u, |
| 4481 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4482 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4483 | |
| 4484 | // The highest priority request should steal the job from the default priority |
| 4485 | // request. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4486 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4487 | &handle2)); |
| 4488 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4489 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4490 | } |
| 4491 | |
| 4492 | TEST_F(ClientSocketPoolBaseTest, RequestStealsJobFromLowestRequestWithJob) { |
| 4493 | CreatePool(3, 3); |
| 4494 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4495 | |
| 4496 | ClientSocketHandle handle_lowest; |
| 4497 | TestCompletionCallback callback_lowest; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4498 | EXPECT_EQ( |
| 4499 | ERR_IO_PENDING, |
| 4500 | handle_lowest.Init(TestGroupId("a"), params_, base::nullopt, LOWEST, |
| 4501 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4502 | callback_lowest.callback(), |
| 4503 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4504 | NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4505 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4506 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4507 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4508 | TestGroupId("a"))); |
| 4509 | EXPECT_EQ(0u, |
| 4510 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4511 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4512 | |
| 4513 | ClientSocketHandle handle_highest; |
| 4514 | TestCompletionCallback callback_highest; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4515 | EXPECT_EQ( |
| 4516 | ERR_IO_PENDING, |
| 4517 | handle_highest.Init(TestGroupId("a"), params_, base::nullopt, HIGHEST, |
| 4518 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4519 | callback_highest.callback(), |
| 4520 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4521 | NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4522 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4523 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4524 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4525 | TestGroupId("a"))); |
| 4526 | EXPECT_EQ(0u, |
| 4527 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4528 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4529 | |
| 4530 | ClientSocketHandle handle_low; |
| 4531 | TestCompletionCallback callback_low; |
| 4532 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4533 | handle_low.Init( |
| 4534 | TestGroupId("a"), params_, base::nullopt, LOW, SocketTag(), |
| 4535 | ClientSocketPool::RespectLimits::ENABLED, |
| 4536 | callback_low.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4537 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4538 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4539 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4540 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4541 | TestGroupId("a"))); |
| 4542 | EXPECT_EQ(0u, |
| 4543 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4544 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4545 | |
| 4546 | ClientSocketHandle handle_lowest2; |
| 4547 | TestCompletionCallback callback_lowest2; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4548 | EXPECT_EQ( |
| 4549 | ERR_IO_PENDING, |
| 4550 | handle_lowest2.Init(TestGroupId("a"), params_, base::nullopt, LOWEST, |
| 4551 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4552 | callback_lowest2.callback(), |
| 4553 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4554 | NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4555 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4556 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4557 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4558 | TestGroupId("a"))); |
| 4559 | EXPECT_EQ(0u, |
| 4560 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4561 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4562 | |
| 4563 | // The top three requests in the queue should have jobs. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4564 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4565 | &handle_highest)); |
| 4566 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4567 | &handle_low)); |
| 4568 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4569 | &handle_lowest)); |
| 4570 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting( |
| 4571 | TestGroupId("a"), &handle_lowest2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4572 | |
| 4573 | // Add another request with medium priority. It should steal the job from the |
| 4574 | // lowest priority request with a job. |
| 4575 | ClientSocketHandle handle_medium; |
| 4576 | TestCompletionCallback callback_medium; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4577 | EXPECT_EQ( |
| 4578 | ERR_IO_PENDING, |
| 4579 | handle_medium.Init(TestGroupId("a"), params_, base::nullopt, MEDIUM, |
| 4580 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4581 | callback_medium.callback(), |
| 4582 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4583 | NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4584 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4585 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4586 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4587 | TestGroupId("a"))); |
| 4588 | EXPECT_EQ(0u, |
| 4589 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4590 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
| 4591 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4592 | &handle_highest)); |
| 4593 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4594 | &handle_medium)); |
| 4595 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4596 | &handle_low)); |
| 4597 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4598 | &handle_lowest)); |
| 4599 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting( |
| 4600 | TestGroupId("a"), &handle_lowest2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4601 | } |
| 4602 | |
| 4603 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeRequestStealsJob) { |
| 4604 | CreatePool(kDefaultMaxSockets, 1); |
| 4605 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4606 | |
| 4607 | ClientSocketHandle handle1; |
| 4608 | TestCompletionCallback callback1; |
| 4609 | EXPECT_EQ( |
| 4610 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4611 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4612 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4613 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4614 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4615 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4616 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4617 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4618 | TestGroupId("a"))); |
| 4619 | EXPECT_EQ(0u, |
| 4620 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4621 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4622 | |
| 4623 | ClientSocketHandle handle2; |
| 4624 | TestCompletionCallback callback2; |
| 4625 | EXPECT_EQ( |
| 4626 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4627 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4628 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4629 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4630 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4631 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4632 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4633 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4634 | TestGroupId("a"))); |
| 4635 | EXPECT_EQ(0u, |
| 4636 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4637 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4638 | |
| 4639 | // 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] | 4640 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4641 | &handle1)); |
| 4642 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4643 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4644 | |
| 4645 | // Reprioritizing the second request places it above the first, and it steals |
| 4646 | // the job from the first request. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4647 | pool_->SetPriority(TestGroupId("a"), &handle2, HIGHEST); |
| 4648 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4649 | &handle2)); |
| 4650 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4651 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4652 | } |
| 4653 | |
| 4654 | TEST_F(ClientSocketPoolBaseTest, CancelRequestReassignsJob) { |
| 4655 | CreatePool(kDefaultMaxSockets, 1); |
| 4656 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4657 | |
| 4658 | ClientSocketHandle handle1; |
| 4659 | TestCompletionCallback callback1; |
| 4660 | EXPECT_EQ( |
| 4661 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4662 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4663 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4664 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4665 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4666 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4667 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4668 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4669 | TestGroupId("a"))); |
| 4670 | EXPECT_EQ(0u, |
| 4671 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4672 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4673 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4674 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4675 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4676 | |
| 4677 | ClientSocketHandle handle2; |
| 4678 | TestCompletionCallback callback2; |
| 4679 | EXPECT_EQ( |
| 4680 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4681 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4682 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4683 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4684 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4685 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4686 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4687 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4688 | TestGroupId("a"))); |
| 4689 | EXPECT_EQ(0u, |
| 4690 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4691 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4692 | |
| 4693 | // The second request doesn't get a job because we are the limit. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4694 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4695 | &handle1)); |
| 4696 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4697 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4698 | |
| 4699 | // The second request should get a job upon cancelling the first request. |
| 4700 | handle1.Reset(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4701 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4702 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4703 | TestGroupId("a"))); |
| 4704 | EXPECT_EQ(0u, |
| 4705 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4706 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4707 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4708 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4709 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4710 | } |
| 4711 | |
| 4712 | TEST_F(ClientSocketPoolBaseTest, JobCompletionReassignsJob) { |
| 4713 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4714 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4715 | |
| 4716 | ClientSocketHandle handle1; |
| 4717 | TestCompletionCallback callback1; |
| 4718 | EXPECT_EQ( |
| 4719 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4720 | handle1.Init(TestGroupId("a"), params_, base::nullopt, HIGHEST, |
| 4721 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4722 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4723 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4724 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4725 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4726 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4727 | TestGroupId("a"))); |
| 4728 | EXPECT_EQ(0u, |
| 4729 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4730 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4731 | |
| 4732 | ClientSocketHandle handle2; |
| 4733 | TestCompletionCallback callback2; |
| 4734 | EXPECT_EQ( |
| 4735 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4736 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4737 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4738 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4739 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4740 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4741 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4742 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4743 | TestGroupId("a"))); |
| 4744 | EXPECT_EQ(0u, |
| 4745 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4746 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4747 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4748 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4749 | &handle1)); |
| 4750 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4751 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4752 | |
| 4753 | // The lower-priority job completes first. The higher-priority request should |
| 4754 | // get the socket, and the lower-priority request should get the remaining |
| 4755 | // job. |
| 4756 | client_socket_factory_.SignalJob(1); |
| 4757 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4758 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4759 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4760 | TestGroupId("a"))); |
| 4761 | EXPECT_EQ(0u, |
| 4762 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4763 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4764 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4765 | EXPECT_TRUE(handle1.socket()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4766 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4767 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4768 | } |
| 4769 | |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 4770 | class MockLayeredPool : public HigherLayeredPool { |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4771 | public: |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4772 | MockLayeredPool(TransportClientSocketPool* pool, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4773 | const ClientSocketPool::GroupId& group_id) |
| 4774 | : pool_(pool), group_id_(group_id), can_release_connection_(true) { |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 4775 | pool_->AddHigherLayeredPool(this); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4776 | } |
| 4777 | |
Daniel Cheng | 4496d082 | 2018-04-26 21:52:15 | [diff] [blame] | 4778 | ~MockLayeredPool() override { pool_->RemoveHigherLayeredPool(this); } |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4779 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4780 | int RequestSocket(TransportClientSocketPool* pool) { |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4781 | return handle_.Init( |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 4782 | group_id_, ClientSocketPool::SocketParams::CreateForHttpForTesting(), |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4783 | base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
| 4784 | ClientSocketPool::RespectLimits::ENABLED, callback_.callback(), |
| 4785 | ClientSocketPool::ProxyAuthCallback(), pool, NetLogWithSource()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4786 | } |
| 4787 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4788 | int RequestSocketWithoutLimits(TransportClientSocketPool* pool) { |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4789 | return handle_.Init( |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 4790 | group_id_, ClientSocketPool::SocketParams::CreateForHttpForTesting(), |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4791 | base::nullopt, MAXIMUM_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4792 | ClientSocketPool::RespectLimits::DISABLED, callback_.callback(), |
| 4793 | ClientSocketPool::ProxyAuthCallback(), pool, NetLogWithSource()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4794 | } |
| 4795 | |
| 4796 | bool ReleaseOneConnection() { |
| 4797 | if (!handle_.is_initialized() || !can_release_connection_) { |
| 4798 | return false; |
| 4799 | } |
| 4800 | handle_.socket()->Disconnect(); |
| 4801 | handle_.Reset(); |
| 4802 | return true; |
| 4803 | } |
| 4804 | |
| 4805 | void set_can_release_connection(bool can_release_connection) { |
| 4806 | can_release_connection_ = can_release_connection; |
| 4807 | } |
| 4808 | |
| 4809 | MOCK_METHOD0(CloseOneIdleConnection, bool()); |
| 4810 | |
| 4811 | private: |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4812 | TransportClientSocketPool* const pool_; |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4813 | ClientSocketHandle handle_; |
| 4814 | TestCompletionCallback callback_; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4815 | const ClientSocketPool::GroupId group_id_; |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4816 | bool can_release_connection_; |
| 4817 | }; |
| 4818 | |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4819 | // Tests the basic case of closing an idle socket in a higher layered pool when |
| 4820 | // a new request is issued and the lower layer pool is stalled. |
| 4821 | TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketsHeldByLayeredPoolWhenNeeded) { |
| 4822 | CreatePool(1, 1); |
| 4823 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 4824 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4825 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("foo")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4826 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4827 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4828 | .WillOnce(Invoke(&mock_layered_pool, |
| 4829 | &MockLayeredPool::ReleaseOneConnection)); |
| 4830 | ClientSocketHandle handle; |
| 4831 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4832 | EXPECT_EQ( |
| 4833 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4834 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4835 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4836 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4837 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4838 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4839 | } |
| 4840 | |
Matt Menke | 83367864 | 2019-03-05 22:05:51 | [diff] [blame] | 4841 | // Tests the case that trying to close an idle socket in a higher layered pool |
| 4842 | // fails. |
| 4843 | TEST_F(ClientSocketPoolBaseTest, |
| 4844 | CloseIdleSocketsHeldByLayeredPoolWhenNeededFails) { |
| 4845 | CreatePool(1, 1); |
| 4846 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 4847 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4848 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("foo")); |
Matt Menke | 83367864 | 2019-03-05 22:05:51 | [diff] [blame] | 4849 | mock_layered_pool.set_can_release_connection(false); |
| 4850 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
| 4851 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4852 | .WillOnce(Invoke(&mock_layered_pool, |
| 4853 | &MockLayeredPool::ReleaseOneConnection)); |
| 4854 | ClientSocketHandle handle; |
| 4855 | TestCompletionCallback callback; |
| 4856 | EXPECT_EQ( |
| 4857 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4858 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4859 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4860 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4861 | pool_.get(), NetLogWithSource())); |
Matt Menke | 83367864 | 2019-03-05 22:05:51 | [diff] [blame] | 4862 | base::RunLoop().RunUntilIdle(); |
| 4863 | EXPECT_FALSE(callback.have_result()); |
| 4864 | } |
| 4865 | |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4866 | // Same as above, but the idle socket is in the same group as the stalled |
| 4867 | // socket, and closes the only other request in its group when closing requests |
| 4868 | // in higher layered pools. This generally shouldn't happen, but it may be |
| 4869 | // possible if a higher level pool issues a request and the request is |
| 4870 | // subsequently cancelled. Even if it's not possible, best not to crash. |
| 4871 | TEST_F(ClientSocketPoolBaseTest, |
| 4872 | CloseIdleSocketsHeldByLayeredPoolWhenNeededSameGroup) { |
| 4873 | CreatePool(2, 2); |
| 4874 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 4875 | |
| 4876 | // Need a socket in another group for the pool to be stalled (If a group |
| 4877 | // has the maximum number of connections already, it's not stalled). |
| 4878 | ClientSocketHandle handle1; |
| 4879 | TestCompletionCallback callback1; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4880 | EXPECT_EQ(OK, handle1.Init(TestGroupId("group1"), params_, base::nullopt, |
| 4881 | DEFAULT_PRIORITY, SocketTag(), |
| 4882 | ClientSocketPool::RespectLimits::ENABLED, |
| 4883 | callback1.callback(), |
| 4884 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4885 | NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4886 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4887 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("group2")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4888 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4889 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4890 | .WillOnce(Invoke(&mock_layered_pool, |
| 4891 | &MockLayeredPool::ReleaseOneConnection)); |
| 4892 | ClientSocketHandle handle; |
| 4893 | TestCompletionCallback callback2; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4894 | EXPECT_EQ(ERR_IO_PENDING, |
| 4895 | handle.Init( |
| 4896 | TestGroupId("group2"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4897 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4898 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4899 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4900 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4901 | } |
| 4902 | |
| 4903 | // Tests the case when an idle socket can be closed when a new request is |
| 4904 | // issued, and the new request belongs to a group that was previously stalled. |
| 4905 | TEST_F(ClientSocketPoolBaseTest, |
| 4906 | CloseIdleSocketsHeldByLayeredPoolInSameGroupWhenNeeded) { |
| 4907 | CreatePool(2, 2); |
| 4908 | std::list<TestConnectJob::JobType> job_types; |
| 4909 | job_types.push_back(TestConnectJob::kMockJob); |
| 4910 | job_types.push_back(TestConnectJob::kMockJob); |
| 4911 | job_types.push_back(TestConnectJob::kMockJob); |
| 4912 | job_types.push_back(TestConnectJob::kMockJob); |
| 4913 | connect_job_factory_->set_job_types(&job_types); |
| 4914 | |
| 4915 | ClientSocketHandle handle1; |
| 4916 | TestCompletionCallback callback1; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4917 | EXPECT_EQ(OK, handle1.Init(TestGroupId("group1"), params_, base::nullopt, |
| 4918 | DEFAULT_PRIORITY, SocketTag(), |
| 4919 | ClientSocketPool::RespectLimits::ENABLED, |
| 4920 | callback1.callback(), |
| 4921 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4922 | NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4923 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4924 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("group2")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4925 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4926 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4927 | .WillRepeatedly(Invoke(&mock_layered_pool, |
| 4928 | &MockLayeredPool::ReleaseOneConnection)); |
| 4929 | mock_layered_pool.set_can_release_connection(false); |
| 4930 | |
| 4931 | // The third request is made when the socket pool is in a stalled state. |
| 4932 | ClientSocketHandle handle3; |
| 4933 | TestCompletionCallback callback3; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4934 | EXPECT_EQ(ERR_IO_PENDING, |
| 4935 | handle3.Init( |
| 4936 | TestGroupId("group3"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4937 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4938 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4939 | pool_.get(), NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4940 | |
| 4941 | base::RunLoop().RunUntilIdle(); |
| 4942 | EXPECT_FALSE(callback3.have_result()); |
| 4943 | |
| 4944 | // The fourth request is made when the pool is no longer stalled. The third |
| 4945 | // request should be serviced first, since it was issued first and has the |
| 4946 | // same priority. |
| 4947 | mock_layered_pool.set_can_release_connection(true); |
| 4948 | ClientSocketHandle handle4; |
| 4949 | TestCompletionCallback callback4; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4950 | EXPECT_EQ(ERR_IO_PENDING, |
| 4951 | handle4.Init( |
| 4952 | TestGroupId("group3"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4953 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4954 | callback4.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4955 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4956 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4957 | EXPECT_FALSE(callback4.have_result()); |
| 4958 | |
| 4959 | // Closing a handle should free up another socket slot. |
| 4960 | handle1.Reset(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4961 | EXPECT_THAT(callback4.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4962 | } |
| 4963 | |
| 4964 | // Tests the case when an idle socket can be closed when a new request is |
| 4965 | // issued, and the new request belongs to a group that was previously stalled. |
| 4966 | // |
| 4967 | // The two differences from the above test are that the stalled requests are not |
| 4968 | // in the same group as the layered pool's request, and the the fourth request |
| 4969 | // has a higher priority than the third one, so gets a socket first. |
| 4970 | TEST_F(ClientSocketPoolBaseTest, |
| 4971 | CloseIdleSocketsHeldByLayeredPoolInSameGroupWhenNeeded2) { |
| 4972 | CreatePool(2, 2); |
| 4973 | std::list<TestConnectJob::JobType> job_types; |
| 4974 | job_types.push_back(TestConnectJob::kMockJob); |
| 4975 | job_types.push_back(TestConnectJob::kMockJob); |
| 4976 | job_types.push_back(TestConnectJob::kMockJob); |
| 4977 | job_types.push_back(TestConnectJob::kMockJob); |
| 4978 | connect_job_factory_->set_job_types(&job_types); |
| 4979 | |
| 4980 | ClientSocketHandle handle1; |
| 4981 | TestCompletionCallback callback1; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4982 | EXPECT_EQ(OK, handle1.Init(TestGroupId("group1"), params_, base::nullopt, |
| 4983 | DEFAULT_PRIORITY, SocketTag(), |
| 4984 | ClientSocketPool::RespectLimits::ENABLED, |
| 4985 | callback1.callback(), |
| 4986 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4987 | NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4988 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4989 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("group2")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4990 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4991 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4992 | .WillRepeatedly(Invoke(&mock_layered_pool, |
| 4993 | &MockLayeredPool::ReleaseOneConnection)); |
| 4994 | mock_layered_pool.set_can_release_connection(false); |
| 4995 | |
| 4996 | // The third request is made when the socket pool is in a stalled state. |
| 4997 | ClientSocketHandle handle3; |
| 4998 | TestCompletionCallback callback3; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4999 | EXPECT_EQ( |
| 5000 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5001 | handle3.Init(TestGroupId("group3"), params_, base::nullopt, MEDIUM, |
| 5002 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 5003 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5004 | pool_.get(), NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 5005 | |
| 5006 | base::RunLoop().RunUntilIdle(); |
| 5007 | EXPECT_FALSE(callback3.have_result()); |
| 5008 | |
| 5009 | // The fourth request is made when the pool is no longer stalled. This |
| 5010 | // request has a higher priority than the third request, so is serviced first. |
| 5011 | mock_layered_pool.set_can_release_connection(true); |
| 5012 | ClientSocketHandle handle4; |
| 5013 | TestCompletionCallback callback4; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 5014 | EXPECT_EQ( |
| 5015 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5016 | handle4.Init(TestGroupId("group3"), params_, base::nullopt, HIGHEST, |
| 5017 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 5018 | callback4.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5019 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5020 | EXPECT_THAT(callback4.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 5021 | EXPECT_FALSE(callback3.have_result()); |
| 5022 | |
| 5023 | // Closing a handle should free up another socket slot. |
| 5024 | handle1.Reset(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5025 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 5026 | } |
| 5027 | |
| 5028 | TEST_F(ClientSocketPoolBaseTest, |
| 5029 | CloseMultipleIdleSocketsHeldByLayeredPoolWhenNeeded) { |
| 5030 | CreatePool(1, 1); |
| 5031 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 5032 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5033 | MockLayeredPool mock_layered_pool1(pool_.get(), TestGroupId("foo")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5034 | EXPECT_THAT(mock_layered_pool1.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 5035 | EXPECT_CALL(mock_layered_pool1, CloseOneIdleConnection()) |
| 5036 | .WillRepeatedly(Invoke(&mock_layered_pool1, |
| 5037 | &MockLayeredPool::ReleaseOneConnection)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5038 | MockLayeredPool mock_layered_pool2(pool_.get(), TestGroupId("bar")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5039 | EXPECT_THAT(mock_layered_pool2.RequestSocketWithoutLimits(pool_.get()), |
| 5040 | IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 5041 | EXPECT_CALL(mock_layered_pool2, CloseOneIdleConnection()) |
| 5042 | .WillRepeatedly(Invoke(&mock_layered_pool2, |
| 5043 | &MockLayeredPool::ReleaseOneConnection)); |
| 5044 | ClientSocketHandle handle; |
| 5045 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 5046 | EXPECT_EQ( |
| 5047 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5048 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 5049 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5050 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5051 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5052 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 5053 | } |
| 5054 | |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5055 | // Test that when a socket pool and group are at their limits, a request |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5056 | // with RespectLimits::DISABLED triggers creation of a new socket, and gets the |
| 5057 | // socket instead of a request with the same priority that was issued earlier, |
| 5058 | // but has RespectLimits::ENABLED. |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5059 | TEST_F(ClientSocketPoolBaseTest, IgnoreLimits) { |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5060 | CreatePool(1, 1); |
| 5061 | |
| 5062 | // Issue a request to reach the socket pool limit. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5063 | EXPECT_EQ(OK, StartRequestWithIgnoreLimits( |
| 5064 | TestGroupId("a"), MAXIMUM_PRIORITY, |
| 5065 | ClientSocketPool::RespectLimits::ENABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5066 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5067 | |
| 5068 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 5069 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5070 | EXPECT_EQ(ERR_IO_PENDING, StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5071 | TestGroupId("a"), MAXIMUM_PRIORITY, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5072 | ClientSocketPool::RespectLimits::ENABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5073 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5074 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5075 | // Issue a request that ignores the limits, so a new ConnectJob is |
| 5076 | // created. |
| 5077 | EXPECT_EQ(ERR_IO_PENDING, StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5078 | TestGroupId("a"), MAXIMUM_PRIORITY, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5079 | ClientSocketPool::RespectLimits::DISABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5080 | ASSERT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5081 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5082 | EXPECT_THAT(request(2)->WaitForResult(), IsOk()); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5083 | EXPECT_FALSE(request(1)->have_result()); |
| 5084 | } |
| 5085 | |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 5086 | // Test that when a socket pool and group are at their limits, a ConnectJob |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5087 | // issued for a request with RespectLimits::DISABLED is not cancelled when a |
| 5088 | // request with RespectLimits::ENABLED issued to the same group is cancelled. |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 5089 | TEST_F(ClientSocketPoolBaseTest, IgnoreLimitsCancelOtherJob) { |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 5090 | CreatePool(1, 1); |
| 5091 | |
| 5092 | // Issue a request to reach the socket pool limit. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5093 | EXPECT_EQ(OK, StartRequestWithIgnoreLimits( |
| 5094 | TestGroupId("a"), MAXIMUM_PRIORITY, |
| 5095 | ClientSocketPool::RespectLimits::ENABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5096 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 5097 | |
| 5098 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 5099 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5100 | EXPECT_EQ(ERR_IO_PENDING, StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5101 | TestGroupId("a"), MAXIMUM_PRIORITY, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5102 | ClientSocketPool::RespectLimits::ENABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5103 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 5104 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5105 | // Issue a request with RespectLimits::DISABLED, so a new ConnectJob is |
| 5106 | // created. |
| 5107 | EXPECT_EQ(ERR_IO_PENDING, StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5108 | TestGroupId("a"), MAXIMUM_PRIORITY, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5109 | ClientSocketPool::RespectLimits::DISABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5110 | ASSERT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5111 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5112 | // Cancel the pending request with RespectLimits::ENABLED. The ConnectJob |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5113 | // should not be cancelled. |
| 5114 | request(1)->handle()->Reset(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5115 | ASSERT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5116 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5117 | EXPECT_THAT(request(2)->WaitForResult(), IsOk()); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5118 | EXPECT_FALSE(request(1)->have_result()); |
| 5119 | } |
| 5120 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5121 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthNoAuthCallback) { |
| 5122 | CreatePool(1, 1); |
| 5123 | |
| 5124 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5125 | |
| 5126 | ClientSocketHandle handle; |
| 5127 | TestCompletionCallback callback; |
| 5128 | EXPECT_EQ( |
| 5129 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5130 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 5131 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5132 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5133 | pool_.get(), NetLogWithSource())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5134 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5135 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5136 | |
| 5137 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_PROXY_AUTH_REQUESTED)); |
| 5138 | EXPECT_FALSE(handle.is_initialized()); |
| 5139 | EXPECT_FALSE(handle.socket()); |
| 5140 | |
| 5141 | // The group should now be empty, and thus be deleted. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5142 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5143 | } |
| 5144 | |
| 5145 | class TestAuthHelper { |
| 5146 | public: |
| 5147 | TestAuthHelper() = default; |
| 5148 | ~TestAuthHelper() = default; |
| 5149 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5150 | void InitHandle( |
Matt Menke | 84d11e56 | 2019-03-27 00:11:19 | [diff] [blame] | 5151 | scoped_refptr<ClientSocketPool::SocketParams> params, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5152 | TransportClientSocketPool* pool, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5153 | RequestPriority priority = DEFAULT_PRIORITY, |
| 5154 | ClientSocketPool::RespectLimits respect_limits = |
| 5155 | ClientSocketPool::RespectLimits::ENABLED, |
| 5156 | const ClientSocketPool::GroupId& group_id_in = TestGroupId("a")) { |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5157 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5158 | handle_.Init(group_id_in, params, base::nullopt, priority, |
| 5159 | SocketTag(), respect_limits, callback_.callback(), |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5160 | base::BindRepeating(&TestAuthHelper::AuthCallback, |
| 5161 | base::Unretained(this)), |
| 5162 | pool, NetLogWithSource())); |
| 5163 | } |
| 5164 | |
| 5165 | void WaitForAuth() { |
| 5166 | run_loop_ = std::make_unique<base::RunLoop>(); |
| 5167 | run_loop_->Run(); |
| 5168 | run_loop_.reset(); |
| 5169 | } |
| 5170 | |
| 5171 | void WaitForAuthAndRestartSync() { |
| 5172 | restart_sync_ = true; |
| 5173 | WaitForAuth(); |
| 5174 | restart_sync_ = false; |
| 5175 | } |
| 5176 | |
| 5177 | void WaitForAuthAndResetHandleSync() { |
| 5178 | reset_handle_sync_ = true; |
| 5179 | WaitForAuth(); |
| 5180 | reset_handle_sync_ = false; |
| 5181 | } |
| 5182 | |
| 5183 | void RestartWithAuth() { |
| 5184 | DCHECK(restart_with_auth_callback_); |
| 5185 | std::move(restart_with_auth_callback_).Run(); |
| 5186 | } |
| 5187 | |
| 5188 | int WaitForResult() { |
| 5189 | int result = callback_.WaitForResult(); |
| 5190 | // There shouldn't be any callback waiting to be invoked once the request is |
| 5191 | // complete. |
| 5192 | EXPECT_FALSE(restart_with_auth_callback_); |
| 5193 | // The socket should only be initialized on success. |
| 5194 | EXPECT_EQ(result == OK, handle_.is_initialized()); |
| 5195 | EXPECT_EQ(result == OK, handle_.socket() != nullptr); |
| 5196 | return result; |
| 5197 | } |
| 5198 | |
| 5199 | ClientSocketHandle* handle() { return &handle_; } |
| 5200 | int auth_count() const { return auth_count_; } |
| 5201 | int have_result() const { return callback_.have_result(); } |
| 5202 | |
| 5203 | private: |
| 5204 | void AuthCallback(const HttpResponseInfo& response, |
| 5205 | HttpAuthController* auth_controller, |
| 5206 | base::OnceClosure restart_with_auth_callback) { |
| 5207 | EXPECT_FALSE(restart_with_auth_callback_); |
| 5208 | EXPECT_TRUE(restart_with_auth_callback); |
| 5209 | |
| 5210 | // Once there's a result, this method shouldn't be invoked again. |
| 5211 | EXPECT_FALSE(callback_.have_result()); |
| 5212 | |
| 5213 | ++auth_count_; |
| 5214 | run_loop_->Quit(); |
| 5215 | if (restart_sync_) { |
| 5216 | std::move(restart_with_auth_callback).Run(); |
| 5217 | return; |
| 5218 | } |
| 5219 | |
| 5220 | restart_with_auth_callback_ = std::move(restart_with_auth_callback); |
| 5221 | |
| 5222 | if (reset_handle_sync_) { |
| 5223 | handle_.Reset(); |
| 5224 | return; |
| 5225 | } |
| 5226 | } |
| 5227 | |
| 5228 | std::unique_ptr<base::RunLoop> run_loop_; |
| 5229 | base::OnceClosure restart_with_auth_callback_; |
| 5230 | |
| 5231 | bool restart_sync_ = false; |
| 5232 | bool reset_handle_sync_ = false; |
| 5233 | |
| 5234 | ClientSocketHandle handle_; |
| 5235 | int auth_count_ = 0; |
| 5236 | TestCompletionCallback callback_; |
| 5237 | |
| 5238 | DISALLOW_COPY_AND_ASSIGN(TestAuthHelper); |
| 5239 | }; |
| 5240 | |
| 5241 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnce) { |
| 5242 | CreatePool(1, 1); |
| 5243 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5244 | |
| 5245 | TestAuthHelper auth_helper; |
| 5246 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5247 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5248 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5249 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5250 | |
| 5251 | auth_helper.WaitForAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5252 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5253 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5254 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5255 | |
| 5256 | auth_helper.RestartWithAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5257 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5258 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5259 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5260 | |
| 5261 | EXPECT_THAT(auth_helper.WaitForResult(), IsOk()); |
| 5262 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5263 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5264 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5265 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5266 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5267 | } |
| 5268 | |
| 5269 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceSync) { |
| 5270 | CreatePool(1, 1); |
| 5271 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5272 | |
| 5273 | TestAuthHelper auth_helper; |
| 5274 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5275 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5276 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5277 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5278 | |
| 5279 | auth_helper.WaitForAuthAndRestartSync(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5280 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5281 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5282 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5283 | |
| 5284 | EXPECT_THAT(auth_helper.WaitForResult(), IsOk()); |
| 5285 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5286 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5287 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5288 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5289 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5290 | } |
| 5291 | |
| 5292 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceFails) { |
| 5293 | CreatePool(1, 1); |
| 5294 | connect_job_factory_->set_job_type( |
| 5295 | TestConnectJob::kMockAuthChallengeOnceFailingJob); |
| 5296 | |
| 5297 | TestAuthHelper auth_helper; |
| 5298 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5299 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5300 | |
| 5301 | auth_helper.WaitForAuth(); |
| 5302 | auth_helper.RestartWithAuth(); |
| 5303 | EXPECT_THAT(auth_helper.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 5304 | |
| 5305 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5306 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5307 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5308 | } |
| 5309 | |
| 5310 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceSyncFails) { |
| 5311 | CreatePool(1, 1); |
| 5312 | connect_job_factory_->set_job_type( |
| 5313 | TestConnectJob::kMockAuthChallengeOnceFailingJob); |
| 5314 | |
| 5315 | TestAuthHelper auth_helper; |
| 5316 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5317 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5318 | |
| 5319 | auth_helper.WaitForAuthAndRestartSync(); |
| 5320 | EXPECT_THAT(auth_helper.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 5321 | |
| 5322 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5323 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5324 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5325 | } |
| 5326 | |
| 5327 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceDeleteHandle) { |
| 5328 | CreatePool(1, 1); |
| 5329 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5330 | |
| 5331 | TestAuthHelper auth_helper; |
| 5332 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5333 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5334 | |
| 5335 | auth_helper.WaitForAuth(); |
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.handle()->Reset(); |
| 5339 | |
| 5340 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5341 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5342 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5343 | EXPECT_FALSE(auth_helper.handle()->is_initialized()); |
| 5344 | EXPECT_FALSE(auth_helper.handle()->socket()); |
| 5345 | } |
| 5346 | |
| 5347 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceDeleteHandleSync) { |
| 5348 | CreatePool(1, 1); |
| 5349 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5350 | |
| 5351 | TestAuthHelper auth_helper; |
| 5352 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5353 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5354 | |
| 5355 | auth_helper.WaitForAuthAndResetHandleSync(); |
| 5356 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5357 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5358 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5359 | EXPECT_FALSE(auth_helper.handle()->is_initialized()); |
| 5360 | EXPECT_FALSE(auth_helper.handle()->socket()); |
| 5361 | } |
| 5362 | |
| 5363 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceFlushWithError) { |
| 5364 | CreatePool(1, 1); |
| 5365 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5366 | |
| 5367 | TestAuthHelper auth_helper; |
| 5368 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5369 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5370 | |
| 5371 | auth_helper.WaitForAuth(); |
| 5372 | |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 5373 | pool_->FlushWithError(ERR_FAILED, "Network changed"); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5374 | base::RunLoop().RunUntilIdle(); |
| 5375 | |
| 5376 | // When flushing the socket pool, bound sockets should delay returning the |
| 5377 | // error until completion. |
| 5378 | EXPECT_FALSE(auth_helper.have_result()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5379 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5380 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5381 | |
| 5382 | auth_helper.RestartWithAuth(); |
| 5383 | // The callback should be called asynchronously. |
| 5384 | EXPECT_FALSE(auth_helper.have_result()); |
| 5385 | |
| 5386 | EXPECT_THAT(auth_helper.WaitForResult(), IsError(ERR_FAILED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5387 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5388 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5389 | } |
| 5390 | |
| 5391 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthTwice) { |
| 5392 | CreatePool(1, 1); |
| 5393 | connect_job_factory_->set_job_type( |
| 5394 | TestConnectJob::kMockAuthChallengeTwiceJob); |
| 5395 | |
| 5396 | TestAuthHelper auth_helper; |
| 5397 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5398 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5399 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5400 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5401 | |
| 5402 | auth_helper.WaitForAuth(); |
| 5403 | auth_helper.RestartWithAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5404 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5405 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5406 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5407 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5408 | |
| 5409 | auth_helper.WaitForAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5410 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5411 | EXPECT_EQ(2, auth_helper.auth_count()); |
| 5412 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5413 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5414 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5415 | auth_helper.RestartWithAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5416 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5417 | EXPECT_EQ(2, auth_helper.auth_count()); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5418 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5419 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5420 | |
| 5421 | EXPECT_THAT(auth_helper.WaitForResult(), IsOk()); |
| 5422 | EXPECT_EQ(2, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5423 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5424 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5425 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5426 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5427 | } |
| 5428 | |
| 5429 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthTwiceFails) { |
| 5430 | CreatePool(1, 1); |
| 5431 | connect_job_factory_->set_job_type( |
| 5432 | TestConnectJob::kMockAuthChallengeTwiceFailingJob); |
| 5433 | |
| 5434 | TestAuthHelper auth_helper; |
| 5435 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5436 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5437 | |
| 5438 | auth_helper.WaitForAuth(); |
| 5439 | auth_helper.RestartWithAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5440 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5441 | EXPECT_EQ(1, auth_helper.auth_count()); |
| 5442 | |
| 5443 | auth_helper.WaitForAuth(); |
| 5444 | auth_helper.RestartWithAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5445 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5446 | EXPECT_EQ(2, auth_helper.auth_count()); |
| 5447 | |
| 5448 | EXPECT_THAT(auth_helper.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 5449 | EXPECT_EQ(2, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5450 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5451 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5452 | } |
| 5453 | |
| 5454 | // Makes sure that when a bound request is destroyed, a new ConnectJob is |
| 5455 | // created, if needed. |
| 5456 | TEST_F(ClientSocketPoolBaseTest, |
| 5457 | ProxyAuthCreateNewConnectJobOnDestroyBoundRequest) { |
| 5458 | CreatePool(1 /* max_sockets */, 1 /* max_sockets_per_group */); |
| 5459 | connect_job_factory_->set_job_type( |
| 5460 | TestConnectJob::kMockAuthChallengeOnceFailingJob); |
| 5461 | |
| 5462 | // First request creates a ConnectJob. |
| 5463 | TestAuthHelper auth_helper1; |
| 5464 | auth_helper1.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5465 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5466 | |
| 5467 | // A second request come in, but no new ConnectJob is needed, since the limit |
| 5468 | // has been reached. |
| 5469 | TestAuthHelper auth_helper2; |
| 5470 | auth_helper2.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5471 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5472 | |
| 5473 | // Run until the auth callback for the first request is invoked. |
| 5474 | auth_helper1.WaitForAuth(); |
| 5475 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5476 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5477 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5478 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5479 | |
| 5480 | // Make connect jobs succeed, then cancel the first request, which should |
| 5481 | // destroy the bound ConnectJob, and cause a new ConnectJob to start. |
| 5482 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 5483 | auth_helper1.handle()->Reset(); |
| 5484 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5485 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5486 | |
| 5487 | // The second ConnectJob should succeed. |
| 5488 | EXPECT_THAT(auth_helper2.WaitForResult(), IsOk()); |
| 5489 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5490 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5491 | } |
| 5492 | |
| 5493 | // Makes sure that when a bound request is destroyed, a new ConnectJob is |
| 5494 | // created for another group, if needed. |
| 5495 | TEST_F(ClientSocketPoolBaseTest, |
| 5496 | ProxyAuthCreateNewConnectJobOnDestroyBoundRequestDifferentGroups) { |
| 5497 | CreatePool(1 /* max_sockets */, 1 /* max_sockets_per_group */); |
| 5498 | connect_job_factory_->set_job_type( |
| 5499 | TestConnectJob::kMockAuthChallengeOnceFailingJob); |
| 5500 | |
| 5501 | // First request creates a ConnectJob. |
| 5502 | TestAuthHelper auth_helper1; |
| 5503 | auth_helper1.InitHandle(params_, pool_.get(), DEFAULT_PRIORITY); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5504 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5505 | |
| 5506 | // A second request come in, but no new ConnectJob is needed, since the limit |
| 5507 | // has been reached. |
| 5508 | TestAuthHelper auth_helper2; |
| 5509 | auth_helper2.InitHandle(params_, pool_.get(), DEFAULT_PRIORITY, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5510 | ClientSocketPool::RespectLimits::ENABLED, |
| 5511 | TestGroupId("b")); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5512 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5513 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5514 | |
| 5515 | // Run until the auth callback for the first request is invoked. |
| 5516 | auth_helper1.WaitForAuth(); |
| 5517 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5518 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5519 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5520 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5521 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 5522 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5523 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5524 | |
| 5525 | // Make connect jobs succeed, then cancel the first request, which should |
| 5526 | // destroy the bound ConnectJob, and cause a new ConnectJob to start for the |
| 5527 | // other group. |
| 5528 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 5529 | auth_helper1.handle()->Reset(); |
| 5530 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5531 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 5532 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5533 | |
| 5534 | // The second ConnectJob should succeed. |
| 5535 | EXPECT_THAT(auth_helper2.WaitForResult(), IsOk()); |
| 5536 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5537 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 5538 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5539 | } |
| 5540 | |
| 5541 | // Test that once an auth challenge is bound, that's the request that gets all |
| 5542 | // subsequent calls and the socket itself. |
| 5543 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthStaysBound) { |
| 5544 | CreatePool(1, 1); |
| 5545 | connect_job_factory_->set_job_type( |
| 5546 | TestConnectJob::kMockAuthChallengeTwiceJob); |
| 5547 | |
| 5548 | // First request creates a ConnectJob. |
| 5549 | TestAuthHelper auth_helper1; |
| 5550 | auth_helper1.InitHandle(params_, pool_.get(), LOWEST); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5551 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5552 | |
| 5553 | // A second, higher priority request is made. |
| 5554 | TestAuthHelper auth_helper2; |
| 5555 | auth_helper2.InitHandle(params_, pool_.get(), LOW); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5556 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5557 | |
| 5558 | // Run until the auth callback for the second request is invoked. |
| 5559 | auth_helper2.WaitForAuth(); |
| 5560 | EXPECT_EQ(0, auth_helper1.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5561 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5562 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5563 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5564 | |
| 5565 | // Start a higher priority job. It shouldn't be able to steal |auth_helper2|'s |
| 5566 | // ConnectJob. |
| 5567 | TestAuthHelper auth_helper3; |
| 5568 | auth_helper3.InitHandle(params_, pool_.get(), HIGHEST); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5569 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5570 | |
| 5571 | // Start a higher job that ignores limits, creating a hanging socket. It |
| 5572 | // shouldn't be able to steal |auth_helper2|'s ConnectJob. |
| 5573 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 5574 | TestAuthHelper auth_helper4; |
| 5575 | auth_helper4.InitHandle(params_, pool_.get(), HIGHEST, |
| 5576 | ClientSocketPool::RespectLimits::DISABLED); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5577 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5578 | |
| 5579 | // Restart with auth, and |auth_helper2|'s auth method should be invoked |
| 5580 | // again. |
| 5581 | auth_helper2.RestartWithAuth(); |
| 5582 | auth_helper2.WaitForAuth(); |
| 5583 | EXPECT_EQ(0, auth_helper1.auth_count()); |
| 5584 | EXPECT_FALSE(auth_helper1.have_result()); |
| 5585 | EXPECT_EQ(2, auth_helper2.auth_count()); |
| 5586 | EXPECT_FALSE(auth_helper2.have_result()); |
| 5587 | EXPECT_EQ(0, auth_helper3.auth_count()); |
| 5588 | EXPECT_FALSE(auth_helper3.have_result()); |
| 5589 | EXPECT_EQ(0, auth_helper4.auth_count()); |
| 5590 | EXPECT_FALSE(auth_helper4.have_result()); |
| 5591 | |
| 5592 | // Advance auth again, and |auth_helper2| should get the socket. |
| 5593 | auth_helper2.RestartWithAuth(); |
| 5594 | EXPECT_THAT(auth_helper2.WaitForResult(), IsOk()); |
| 5595 | // The hung ConnectJob for the RespectLimits::DISABLED request is still in the |
| 5596 | // socket pool. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5597 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5598 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5599 | EXPECT_EQ(0, auth_helper1.auth_count()); |
| 5600 | EXPECT_FALSE(auth_helper1.have_result()); |
| 5601 | EXPECT_EQ(0, auth_helper3.auth_count()); |
| 5602 | EXPECT_FALSE(auth_helper3.have_result()); |
| 5603 | EXPECT_EQ(0, auth_helper4.auth_count()); |
| 5604 | EXPECT_FALSE(auth_helper4.have_result()); |
| 5605 | |
| 5606 | // If the socket is returned to the socket pool, the RespectLimits::DISABLED |
| 5607 | // socket request should be able to claim it. |
| 5608 | auth_helper2.handle()->Reset(); |
| 5609 | EXPECT_THAT(auth_helper4.WaitForResult(), IsOk()); |
| 5610 | EXPECT_EQ(0, auth_helper1.auth_count()); |
| 5611 | EXPECT_FALSE(auth_helper1.have_result()); |
| 5612 | EXPECT_EQ(0, auth_helper3.auth_count()); |
| 5613 | EXPECT_FALSE(auth_helper3.have_result()); |
| 5614 | EXPECT_EQ(0, auth_helper4.auth_count()); |
| 5615 | } |
| 5616 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5617 | enum class RefreshType { |
| 5618 | kServer, |
| 5619 | kProxy, |
| 5620 | }; |
| 5621 | |
| 5622 | // Common base class to test RefreshGroup() when called from either |
| 5623 | // OnSSLConfigForServerChanged() matching a specific group or the pool's proxy. |
| 5624 | // |
| 5625 | // Tests which test behavior specific to one or the other case should use |
| 5626 | // ClientSocketPoolBaseTest directly. In particular, there is no "other group" |
| 5627 | // when the pool's proxy matches. |
| 5628 | class ClientSocketPoolBaseRefreshTest |
| 5629 | : public ClientSocketPoolBaseTest, |
| 5630 | public testing::WithParamInterface<RefreshType> { |
| 5631 | public: |
| 5632 | void CreatePoolForRefresh(int max_sockets, |
| 5633 | int max_sockets_per_group, |
| 5634 | bool enable_backup_connect_jobs = false) { |
| 5635 | switch (GetParam()) { |
| 5636 | case RefreshType::kServer: |
| 5637 | CreatePool(max_sockets, max_sockets_per_group, |
| 5638 | enable_backup_connect_jobs); |
| 5639 | break; |
| 5640 | case RefreshType::kProxy: |
| 5641 | CreatePoolWithIdleTimeouts( |
| 5642 | max_sockets, max_sockets_per_group, kUnusedIdleSocketTimeout, |
| 5643 | ClientSocketPool::used_idle_socket_timeout(), |
| 5644 | enable_backup_connect_jobs, |
| 5645 | ProxyServer::FromPacString("HTTPS myproxy:70")); |
| 5646 | break; |
| 5647 | } |
| 5648 | } |
| 5649 | |
| 5650 | static ClientSocketPool::GroupId GetGroupId() { |
| 5651 | return TestGroupId("a", 443, ClientSocketPool::SocketType::kSsl); |
| 5652 | } |
| 5653 | |
David Benjamin | 6dd7e88 | 2019-10-10 02:35:23 | [diff] [blame] | 5654 | static ClientSocketPool::GroupId GetGroupIdInPartition() { |
| 5655 | // Note this GroupId will match GetGroupId() unless |
| 5656 | // kPartitionConnectionsByNetworkIsolationKey is enabled. |
Matt Menke | 4807a9a | 2020-11-21 00:14:41 | [diff] [blame] | 5657 | const SchemefulSite kSite(GURL("https://b/")); |
| 5658 | const NetworkIsolationKey kNetworkIsolationKey(kSite, kSite); |
David Benjamin | 6dd7e88 | 2019-10-10 02:35:23 | [diff] [blame] | 5659 | return TestGroupId("a", 443, ClientSocketPool::SocketType::kSsl, |
| 5660 | PrivacyMode::PRIVACY_MODE_DISABLED, |
| 5661 | kNetworkIsolationKey); |
| 5662 | } |
| 5663 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5664 | void OnSSLConfigForServerChanged() { |
| 5665 | switch (GetParam()) { |
| 5666 | case RefreshType::kServer: |
| 5667 | pool_->OnSSLConfigForServerChanged(HostPortPair("a", 443)); |
| 5668 | break; |
| 5669 | case RefreshType::kProxy: |
| 5670 | pool_->OnSSLConfigForServerChanged(HostPortPair("myproxy", 70)); |
| 5671 | break; |
| 5672 | } |
| 5673 | } |
| 5674 | }; |
| 5675 | |
| 5676 | INSTANTIATE_TEST_SUITE_P(RefreshType, |
| 5677 | ClientSocketPoolBaseRefreshTest, |
| 5678 | ::testing::Values(RefreshType::kServer, |
| 5679 | RefreshType::kProxy)); |
| 5680 | |
| 5681 | TEST_P(ClientSocketPoolBaseRefreshTest, RefreshGroupCreatesNewConnectJobs) { |
| 5682 | CreatePoolForRefresh(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 5683 | const ClientSocketPool::GroupId kGroupId = GetGroupId(); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5684 | |
| 5685 | // First job will be waiting until it gets aborted. |
| 5686 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 5687 | |
| 5688 | ClientSocketHandle handle; |
| 5689 | TestCompletionCallback callback; |
| 5690 | EXPECT_THAT( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5691 | handle.Init(kGroupId, params_, base::nullopt, DEFAULT_PRIORITY, |
| 5692 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5693 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5694 | pool_.get(), NetLogWithSource()), |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5695 | IsError(ERR_IO_PENDING)); |
| 5696 | |
| 5697 | // Switch connect job types, so creating a new ConnectJob will result in |
| 5698 | // success. |
| 5699 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 5700 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5701 | OnSSLConfigForServerChanged(); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5702 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 5703 | ASSERT_TRUE(handle.socket()); |
| 5704 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5705 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId)); |
| 5706 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(kGroupId)); |
| 5707 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(kGroupId)); |
| 5708 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId)); |
| 5709 | } |
| 5710 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5711 | TEST_P(ClientSocketPoolBaseRefreshTest, RefreshGroupClosesIdleConnectJobs) { |
David Benjamin | 6dd7e88 | 2019-10-10 02:35:23 | [diff] [blame] | 5712 | base::test::ScopedFeatureList feature_list; |
| 5713 | feature_list.InitAndEnableFeature( |
| 5714 | features::kPartitionConnectionsByNetworkIsolationKey); |
| 5715 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5716 | CreatePoolForRefresh(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 5717 | const ClientSocketPool::GroupId kGroupId = GetGroupId(); |
David Benjamin | 6dd7e88 | 2019-10-10 02:35:23 | [diff] [blame] | 5718 | const ClientSocketPool::GroupId kGroupIdInPartition = GetGroupIdInPartition(); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5719 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5720 | pool_->RequestSockets(kGroupId, params_, base::nullopt, 2, |
| 5721 | NetLogWithSource()); |
David Benjamin | 6dd7e88 | 2019-10-10 02:35:23 | [diff] [blame] | 5722 | pool_->RequestSockets(kGroupIdInPartition, params_, base::nullopt, 2, |
| 5723 | NetLogWithSource()); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5724 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId)); |
David Benjamin | 6dd7e88 | 2019-10-10 02:35:23 | [diff] [blame] | 5725 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupIdInPartition)); |
| 5726 | EXPECT_EQ(4, pool_->IdleSocketCount()); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5727 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(kGroupId)); |
David Benjamin | 6dd7e88 | 2019-10-10 02:35:23 | [diff] [blame] | 5728 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(kGroupIdInPartition)); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5729 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5730 | OnSSLConfigForServerChanged(); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5731 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5732 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupId)); |
David Benjamin | 6dd7e88 | 2019-10-10 02:35:23 | [diff] [blame] | 5733 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupIdInPartition)); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5734 | } |
| 5735 | |
| 5736 | TEST_F(ClientSocketPoolBaseTest, |
| 5737 | RefreshGroupDoesNotCloseIdleConnectJobsInOtherGroup) { |
| 5738 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5739 | const ClientSocketPool::GroupId kGroupId = |
| 5740 | TestGroupId("a", 443, ClientSocketPool::SocketType::kSsl); |
| 5741 | const ClientSocketPool::GroupId kOtherGroupId = |
| 5742 | TestGroupId("b", 443, ClientSocketPool::SocketType::kSsl); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5743 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5744 | pool_->RequestSockets(kOtherGroupId, params_, base::nullopt, 2, |
| 5745 | NetLogWithSource()); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5746 | ASSERT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 5747 | EXPECT_EQ(2, pool_->IdleSocketCount()); |
| 5748 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(kOtherGroupId)); |
| 5749 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5750 | pool_->OnSSLConfigForServerChanged(HostPortPair("a", 443)); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5751 | ASSERT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 5752 | EXPECT_EQ(2, pool_->IdleSocketCount()); |
| 5753 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(kOtherGroupId)); |
| 5754 | } |
| 5755 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5756 | TEST_P(ClientSocketPoolBaseRefreshTest, RefreshGroupPreventsSocketReuse) { |
| 5757 | CreatePoolForRefresh(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 5758 | const ClientSocketPool::GroupId kGroupId = GetGroupId(); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5759 | |
| 5760 | ClientSocketHandle handle; |
| 5761 | TestCompletionCallback callback; |
| 5762 | EXPECT_THAT( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5763 | handle.Init(kGroupId, params_, base::nullopt, DEFAULT_PRIORITY, |
| 5764 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5765 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5766 | pool_.get(), NetLogWithSource()), |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5767 | IsOk()); |
| 5768 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId)); |
| 5769 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId)); |
| 5770 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5771 | OnSSLConfigForServerChanged(); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5772 | |
| 5773 | handle.Reset(); |
| 5774 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5775 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupId)); |
| 5776 | } |
| 5777 | |
| 5778 | TEST_F(ClientSocketPoolBaseTest, |
| 5779 | RefreshGroupDoesNotPreventSocketReuseInOtherGroup) { |
| 5780 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5781 | const ClientSocketPool::GroupId kGroupId = |
| 5782 | TestGroupId("a", 443, ClientSocketPool::SocketType::kSsl); |
| 5783 | const ClientSocketPool::GroupId kOtherGroupId = |
| 5784 | TestGroupId("b", 443, ClientSocketPool::SocketType::kSsl); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5785 | |
| 5786 | ClientSocketHandle handle; |
| 5787 | TestCompletionCallback callback; |
| 5788 | EXPECT_THAT( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5789 | handle.Init(kOtherGroupId, params_, base::nullopt, DEFAULT_PRIORITY, |
| 5790 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5791 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5792 | pool_.get(), NetLogWithSource()), |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5793 | IsOk()); |
| 5794 | ASSERT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 5795 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kOtherGroupId)); |
| 5796 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5797 | pool_->OnSSLConfigForServerChanged(HostPortPair("a", 443)); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5798 | |
| 5799 | handle.Reset(); |
| 5800 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 5801 | ASSERT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 5802 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(kOtherGroupId)); |
| 5803 | } |
| 5804 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5805 | TEST_P(ClientSocketPoolBaseRefreshTest, |
| 5806 | RefreshGroupReplacesBoundConnectJobOnConnect) { |
| 5807 | CreatePoolForRefresh(1, 1); |
| 5808 | const ClientSocketPool::GroupId kGroupId = GetGroupId(); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5809 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5810 | |
| 5811 | TestAuthHelper auth_helper; |
| 5812 | auth_helper.InitHandle(params_, pool_.get(), DEFAULT_PRIORITY, |
| 5813 | ClientSocketPool::RespectLimits::ENABLED, kGroupId); |
| 5814 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(kGroupId)); |
| 5815 | |
| 5816 | auth_helper.WaitForAuth(); |
| 5817 | |
| 5818 | // This should update the generation, but not cancel the old ConnectJob - it's |
| 5819 | // not safe to do anything while waiting on the original ConnectJob. |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5820 | OnSSLConfigForServerChanged(); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5821 | |
| 5822 | // Providing auth credentials and restarting the request with them will cause |
| 5823 | // the ConnectJob to complete successfully, but the result will be discarded |
| 5824 | // because of the generation mismatch. |
| 5825 | auth_helper.RestartWithAuth(); |
| 5826 | |
| 5827 | // Despite using ConnectJobs that simulate a single challenge, a second |
| 5828 | // challenge will be seen, due to using a new ConnectJob. |
| 5829 | auth_helper.WaitForAuth(); |
| 5830 | auth_helper.RestartWithAuth(); |
| 5831 | |
| 5832 | EXPECT_THAT(auth_helper.WaitForResult(), IsOk()); |
| 5833 | EXPECT_TRUE(auth_helper.handle()->socket()); |
| 5834 | EXPECT_EQ(2, auth_helper.auth_count()); |
| 5835 | |
| 5836 | // When released, the socket will be returned to the socket pool, and |
| 5837 | // available for reuse. |
| 5838 | auth_helper.handle()->Reset(); |
| 5839 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 5840 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId)); |
| 5841 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(kGroupId)); |
| 5842 | } |
| 5843 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5844 | TEST_F(ClientSocketPoolBaseTest, RefreshProxyRefreshesAllGroups) { |
| 5845 | CreatePoolWithIdleTimeouts(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 5846 | kUnusedIdleSocketTimeout, |
| 5847 | ClientSocketPool::used_idle_socket_timeout(), |
| 5848 | false /* no backup connect jobs */, |
| 5849 | ProxyServer::FromPacString("HTTPS myproxy:70")); |
| 5850 | |
| 5851 | const ClientSocketPool::GroupId kGroupId1 = |
| 5852 | TestGroupId("a", 443, ClientSocketPool::SocketType::kSsl); |
| 5853 | const ClientSocketPool::GroupId kGroupId2 = |
| 5854 | TestGroupId("b", 443, ClientSocketPool::SocketType::kSsl); |
| 5855 | const ClientSocketPool::GroupId kGroupId3 = |
| 5856 | TestGroupId("c", 443, ClientSocketPool::SocketType::kSsl); |
| 5857 | |
| 5858 | // Make three sockets in three different groups. The third socket is released |
| 5859 | // to the pool as idle. |
| 5860 | ClientSocketHandle handle1, handle2, handle3; |
| 5861 | TestCompletionCallback callback; |
| 5862 | EXPECT_THAT( |
| 5863 | handle1.Init(kGroupId1, params_, base::nullopt, DEFAULT_PRIORITY, |
| 5864 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5865 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5866 | pool_.get(), NetLogWithSource()), |
| 5867 | IsOk()); |
| 5868 | EXPECT_THAT( |
| 5869 | handle2.Init(kGroupId2, params_, base::nullopt, DEFAULT_PRIORITY, |
| 5870 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5871 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5872 | pool_.get(), NetLogWithSource()), |
| 5873 | IsOk()); |
| 5874 | EXPECT_THAT( |
| 5875 | handle3.Init(kGroupId3, params_, base::nullopt, DEFAULT_PRIORITY, |
| 5876 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5877 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5878 | pool_.get(), NetLogWithSource()), |
| 5879 | IsOk()); |
| 5880 | handle3.Reset(); |
| 5881 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId1)); |
| 5882 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId1)); |
| 5883 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId2)); |
| 5884 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId2)); |
| 5885 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId3)); |
| 5886 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(kGroupId3)); |
| 5887 | |
| 5888 | // Changes to some other proxy do not affect the pool. The idle socket remains |
| 5889 | // alive and closing |handle2| makes the socket available for the pool. |
| 5890 | pool_->OnSSLConfigForServerChanged(HostPortPair("someotherproxy", 70)); |
| 5891 | |
| 5892 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId1)); |
| 5893 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId1)); |
| 5894 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId2)); |
| 5895 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId2)); |
| 5896 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId3)); |
| 5897 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(kGroupId3)); |
| 5898 | |
| 5899 | handle2.Reset(); |
| 5900 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId2)); |
| 5901 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(kGroupId2)); |
| 5902 | |
| 5903 | // Changes to the matching proxy refreshes all groups. |
| 5904 | pool_->OnSSLConfigForServerChanged(HostPortPair("myproxy", 70)); |
| 5905 | |
| 5906 | // Idle sockets are closed. |
| 5907 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5908 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupId2)); |
| 5909 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupId3)); |
| 5910 | |
| 5911 | // The active socket, however, continues to be active. |
| 5912 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId1)); |
| 5913 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId1)); |
| 5914 | |
| 5915 | // Closing it does not make it available for the pool. |
| 5916 | handle1.Reset(); |
| 5917 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5918 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupId1)); |
| 5919 | } |
| 5920 | |
| 5921 | TEST_F(ClientSocketPoolBaseTest, RefreshBothPrivacyAndNormalSockets) { |
| 5922 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 5923 | |
| 5924 | const ClientSocketPool::GroupId kGroupId = |
| 5925 | TestGroupId("a", 443, ClientSocketPool::SocketType::kSsl, |
| 5926 | PrivacyMode::PRIVACY_MODE_DISABLED); |
| 5927 | const ClientSocketPool::GroupId kGroupIdPrivacy = |
| 5928 | TestGroupId("a", 443, ClientSocketPool::SocketType::kSsl, |
| 5929 | PrivacyMode::PRIVACY_MODE_ENABLED); |
| 5930 | const ClientSocketPool::GroupId kOtherGroupId = |
| 5931 | TestGroupId("b", 443, ClientSocketPool::SocketType::kSsl); |
| 5932 | |
| 5933 | // Make a socket in each groups. |
| 5934 | ClientSocketHandle handle1, handle2, handle3; |
| 5935 | TestCompletionCallback callback; |
| 5936 | EXPECT_THAT( |
| 5937 | handle1.Init(kGroupId, params_, base::nullopt, DEFAULT_PRIORITY, |
| 5938 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5939 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5940 | pool_.get(), NetLogWithSource()), |
| 5941 | IsOk()); |
| 5942 | EXPECT_THAT( |
| 5943 | handle2.Init(kGroupIdPrivacy, params_, base::nullopt, DEFAULT_PRIORITY, |
| 5944 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5945 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5946 | pool_.get(), NetLogWithSource()), |
| 5947 | IsOk()); |
| 5948 | EXPECT_THAT( |
| 5949 | handle3.Init(kOtherGroupId, params_, base::nullopt, DEFAULT_PRIORITY, |
| 5950 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5951 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5952 | pool_.get(), NetLogWithSource()), |
| 5953 | IsOk()); |
| 5954 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId)); |
| 5955 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId)); |
| 5956 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupIdPrivacy)); |
| 5957 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupIdPrivacy)); |
| 5958 | ASSERT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 5959 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kOtherGroupId)); |
| 5960 | |
| 5961 | pool_->OnSSLConfigForServerChanged(HostPortPair("a", 443)); |
| 5962 | |
| 5963 | // Active sockets continue to be active. |
| 5964 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId)); |
| 5965 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId)); |
| 5966 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupIdPrivacy)); |
| 5967 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupIdPrivacy)); |
| 5968 | ASSERT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 5969 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kOtherGroupId)); |
| 5970 | |
| 5971 | // Closing them leaves kOtherGroupId alone, but kGroupId and kGroupIdPrivacy |
| 5972 | // are unusable. |
| 5973 | handle1.Reset(); |
| 5974 | handle2.Reset(); |
| 5975 | handle3.Reset(); |
| 5976 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 5977 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupId)); |
| 5978 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupIdPrivacy)); |
| 5979 | EXPECT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 5980 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(kOtherGroupId)); |
| 5981 | } |
| 5982 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 5983 | } // namespace |
| 5984 | |
| 5985 | } // namespace net |