[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" |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 37 | #include "net/base/test_completion_callback.h" |
dalyk | edd30d98 | 2019-12-16 15:31:10 | [diff] [blame] | 38 | #include "net/dns/public/resolve_error_info.h" |
[email protected] | 277d594 | 2010-08-11 21:02:35 | [diff] [blame] | 39 | #include "net/http/http_response_headers.h" |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 40 | #include "net/http/http_response_info.h" |
eroman | 87c53d6 | 2015-04-02 06:51:07 | [diff] [blame] | 41 | #include "net/log/net_log.h" |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 42 | #include "net/log/net_log_event_type.h" |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 43 | #include "net/log/net_log_source.h" |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 44 | #include "net/log/net_log_source_type.h" |
mmenke | 16a7cbdd | 2015-04-24 23:00:56 | [diff] [blame] | 45 | #include "net/log/test_net_log.h" |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 46 | #include "net/log/test_net_log_util.h" |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 47 | #include "net/socket/client_socket_factory.h" |
| 48 | #include "net/socket/client_socket_handle.h" |
tfarina | 5dd13c2 | 2016-11-16 12:08:26 | [diff] [blame] | 49 | #include "net/socket/datagram_client_socket.h" |
tbansal | ca83c00 | 2016-04-28 20:56:28 | [diff] [blame] | 50 | #include "net/socket/socket_performance_watcher.h" |
Paul Jensen | 8d6f87ec | 2018-01-13 00:46:54 | [diff] [blame] | 51 | #include "net/socket/socket_tag.h" |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 52 | #include "net/socket/socket_test_util.h" |
[email protected] | 18ccfdb | 2013-08-15 00:13:44 | [diff] [blame] | 53 | #include "net/socket/ssl_client_socket.h" |
[email protected] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame] | 54 | #include "net/socket/stream_socket.h" |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 55 | #include "net/socket/transport_connect_job.h" |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 56 | #include "net/ssl/ssl_cert_request_info.h" |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 57 | #include "net/test/gtest_util.h" |
Gabriel Charette | c710874 | 2019-08-23 03:31:40 | [diff] [blame] | 58 | #include "net/test/test_with_task_environment.h" |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 59 | #include "net/traffic_annotation/network_traffic_annotation.h" |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 60 | #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 61 | #include "testing/gmock/include/gmock/gmock.h" |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 62 | #include "testing/gtest/include/gtest/gtest.h" |
| 63 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 64 | using net::test::IsError; |
| 65 | using net::test::IsOk; |
| 66 | |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 67 | using ::testing::Invoke; |
| 68 | using ::testing::Return; |
| 69 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 70 | namespace net { |
| 71 | |
| 72 | namespace { |
| 73 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 74 | const int kDefaultMaxSockets = 4; |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 75 | const int kDefaultMaxSocketsPerGroup = 2; |
Tarun Bansal | a763509 | 2019-02-20 10:00:59 | [diff] [blame] | 76 | constexpr base::TimeDelta kUnusedIdleSocketTimeout = |
| 77 | base::TimeDelta::FromSeconds(10); |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 78 | |
Matt Menke | bdf77780 | 2019-04-22 19:38:59 | [diff] [blame] | 79 | ClientSocketPool::GroupId TestGroupId( |
| 80 | const std::string& host, |
| 81 | int port = 80, |
| 82 | ClientSocketPool::SocketType socket_type = |
| 83 | ClientSocketPool::SocketType::kHttp, |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 84 | PrivacyMode privacy_mode = PrivacyMode::PRIVACY_MODE_DISABLED, |
| 85 | NetworkIsolationKey network_isolation_key = NetworkIsolationKey()) { |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 86 | bool disable_secure_dns = false; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 87 | return ClientSocketPool::GroupId(HostPortPair(host, port), socket_type, |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 88 | privacy_mode, network_isolation_key, |
| 89 | disable_secure_dns); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 90 | } |
| 91 | |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 92 | // Make sure |handle| sets load times correctly when it has been assigned a |
| 93 | // reused socket. |
| 94 | void TestLoadTimingInfoConnectedReused(const ClientSocketHandle& handle) { |
| 95 | LoadTimingInfo load_timing_info; |
| 96 | // Only pass true in as |is_reused|, as in general, HttpStream types should |
| 97 | // have stricter concepts of reuse than socket pools. |
| 98 | EXPECT_TRUE(handle.GetLoadTimingInfo(true, &load_timing_info)); |
| 99 | |
| 100 | EXPECT_EQ(true, load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 101 | EXPECT_NE(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 102 | |
[email protected] | b258e079 | 2013-01-12 07:11:59 | [diff] [blame] | 103 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 104 | ExpectLoadTimingHasOnlyConnectionTimes(load_timing_info); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | // Make sure |handle| sets load times correctly when it has been assigned a |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 108 | // fresh socket. Also runs TestLoadTimingInfoConnectedReused, since the owner |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 109 | // of a connection where |is_reused| is false may consider the connection |
| 110 | // reused. |
| 111 | void TestLoadTimingInfoConnectedNotReused(const ClientSocketHandle& handle) { |
| 112 | EXPECT_FALSE(handle.is_reused()); |
| 113 | |
| 114 | LoadTimingInfo load_timing_info; |
| 115 | EXPECT_TRUE(handle.GetLoadTimingInfo(false, &load_timing_info)); |
| 116 | |
| 117 | EXPECT_FALSE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 118 | EXPECT_NE(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 119 | |
[email protected] | b258e079 | 2013-01-12 07:11:59 | [diff] [blame] | 120 | ExpectConnectTimingHasTimes(load_timing_info.connect_timing, |
| 121 | CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY); |
| 122 | ExpectLoadTimingHasOnlyConnectionTimes(load_timing_info); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 123 | |
| 124 | TestLoadTimingInfoConnectedReused(handle); |
| 125 | } |
| 126 | |
| 127 | // Make sure |handle| sets load times correctly, in the case that it does not |
| 128 | // currently have a socket. |
| 129 | void TestLoadTimingInfoNotConnected(const ClientSocketHandle& handle) { |
| 130 | // Should only be set to true once a socket is assigned, if at all. |
| 131 | EXPECT_FALSE(handle.is_reused()); |
| 132 | |
| 133 | LoadTimingInfo load_timing_info; |
| 134 | EXPECT_FALSE(handle.GetLoadTimingInfo(false, &load_timing_info)); |
| 135 | |
| 136 | EXPECT_FALSE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 137 | EXPECT_EQ(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 138 | |
[email protected] | b258e079 | 2013-01-12 07:11:59 | [diff] [blame] | 139 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 140 | ExpectLoadTimingHasOnlyConnectionTimes(load_timing_info); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 141 | } |
| 142 | |
[email protected] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame] | 143 | class MockClientSocket : public StreamSocket { |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 144 | public: |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 145 | explicit MockClientSocket(net::NetLog* net_log) |
| 146 | : connected_(false), |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 147 | has_unread_data_(false), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 148 | net_log_(NetLogWithSource::Make(net_log, NetLogSourceType::SOCKET)), |
Charlie Harrison | 3e4c062 | 2018-05-13 15:44:30 | [diff] [blame] | 149 | was_used_to_convey_data_(false) {} |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 150 | |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 151 | // Sets whether the socket has unread data. If true, the next call to Read() |
| 152 | // will return 1 byte and IsConnectedAndIdle() will return false. |
| 153 | void set_has_unread_data(bool has_unread_data) { |
| 154 | has_unread_data_ = has_unread_data; |
| 155 | } |
| 156 | |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 157 | // Socket implementation. |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 158 | int Read(IOBuffer* /* buf */, |
| 159 | int len, |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 160 | CompletionOnceCallback /* callback */) override { |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 161 | if (has_unread_data_ && len > 0) { |
| 162 | has_unread_data_ = false; |
| 163 | was_used_to_convey_data_ = true; |
| 164 | return 1; |
| 165 | } |
[email protected] | e86df8dc | 2013-03-30 13:18:28 | [diff] [blame] | 166 | return ERR_UNEXPECTED; |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 167 | } |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 168 | |
[email protected] | a2b2cfc | 2017-12-06 09:06:08 | [diff] [blame] | 169 | int Write( |
| 170 | IOBuffer* /* buf */, |
| 171 | int len, |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 172 | CompletionOnceCallback /* callback */, |
[email protected] | a2b2cfc | 2017-12-06 09:06:08 | [diff] [blame] | 173 | const NetworkTrafficAnnotationTag& /*traffic_annotation*/) override { |
[email protected] | 0f873e8 | 2010-09-02 16:09:01 | [diff] [blame] | 174 | was_used_to_convey_data_ = true; |
| 175 | return len; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 176 | } |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 177 | int SetReceiveBufferSize(int32_t size) override { return OK; } |
| 178 | int SetSendBufferSize(int32_t size) override { return OK; } |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 179 | |
[email protected] | dbf036f | 2011-12-06 23:33:24 | [diff] [blame] | 180 | // StreamSocket implementation. |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 181 | int Connect(CompletionOnceCallback callback) override { |
[email protected] | dbf036f | 2011-12-06 23:33:24 | [diff] [blame] | 182 | connected_ = true; |
| 183 | return OK; |
| 184 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 185 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 186 | void Disconnect() override { connected_ = false; } |
| 187 | bool IsConnected() const override { return connected_; } |
| 188 | bool IsConnectedAndIdle() const override { |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 189 | return connected_ && !has_unread_data_; |
| 190 | } |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 191 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 192 | int GetPeerAddress(IPEndPoint* /* address */) const override { |
[email protected] | 9f864b3 | 2010-01-20 15:01:16 | [diff] [blame] | 193 | return ERR_UNEXPECTED; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 194 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 195 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 196 | int GetLocalAddress(IPEndPoint* /* address */) const override { |
[email protected] | e7f74da | 2011-04-19 23:49:35 | [diff] [blame] | 197 | return ERR_UNEXPECTED; |
| 198 | } |
| 199 | |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 200 | const NetLogWithSource& NetLog() const override { return net_log_; } |
[email protected] | a2006ece | 2010-04-23 16:44:02 | [diff] [blame] | 201 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 202 | bool WasEverUsed() const override { return was_used_to_convey_data_; } |
tfarina | 2846404c | 2016-12-25 14:31:37 | [diff] [blame] | 203 | bool WasAlpnNegotiated() const override { return false; } |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 204 | NextProto GetNegotiatedProtocol() const override { return kProtoUnknown; } |
| 205 | bool GetSSLInfo(SSLInfo* ssl_info) override { return false; } |
ttuttle | 23fdb7b | 2015-05-15 01:28:03 | [diff] [blame] | 206 | void GetConnectionAttempts(ConnectionAttempts* out) const override { |
| 207 | out->clear(); |
| 208 | } |
| 209 | void ClearConnectionAttempts() override {} |
| 210 | void AddConnectionAttempts(const ConnectionAttempts& attempts) override {} |
tbansal | f82cc8e | 2015-10-14 20:05:49 | [diff] [blame] | 211 | int64_t GetTotalReceivedBytes() const override { |
| 212 | NOTIMPLEMENTED(); |
| 213 | return 0; |
| 214 | } |
Paul Jensen | 0f49dec | 2017-12-12 23:39:58 | [diff] [blame] | 215 | void ApplySocketTag(const SocketTag& tag) override {} |
[email protected] | 9b5614a | 2010-08-25 20:29:45 | [diff] [blame] | 216 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 217 | private: |
| 218 | bool connected_; |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 219 | bool has_unread_data_; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 220 | NetLogWithSource net_log_; |
[email protected] | 0f873e8 | 2010-09-02 16:09:01 | [diff] [blame] | 221 | bool was_used_to_convey_data_; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 222 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 223 | DISALLOW_COPY_AND_ASSIGN(MockClientSocket); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 224 | }; |
| 225 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 226 | class TestConnectJob; |
| 227 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 228 | class MockClientSocketFactory : public ClientSocketFactory { |
| 229 | public: |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 230 | MockClientSocketFactory() : allocation_count_(0) {} |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 231 | |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 232 | std::unique_ptr<DatagramClientSocket> CreateDatagramClientSocket( |
[email protected] | 5370c01 | 2011-06-29 03:47:04 | [diff] [blame] | 233 | DatagramSocket::BindType bind_type, |
[email protected] | 98b0e58 | 2011-06-22 14:31:41 | [diff] [blame] | 234 | NetLog* net_log, |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 235 | const NetLogSource& source) override { |
[email protected] | 98b0e58 | 2011-06-22 14:31:41 | [diff] [blame] | 236 | NOTREACHED(); |
David Benjamin | 24725be | 2019-07-24 20:57:18 | [diff] [blame] | 237 | return nullptr; |
[email protected] | 98b0e58 | 2011-06-22 14:31:41 | [diff] [blame] | 238 | } |
| 239 | |
Helen Li | d5bb922 | 2018-04-12 15:33:09 | [diff] [blame] | 240 | std::unique_ptr<TransportClientSocket> CreateTransportClientSocket( |
[email protected] | 0a0b768 | 2010-08-25 17:08:07 | [diff] [blame] | 241 | const AddressList& addresses, |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 242 | std::unique_ptr< |
| 243 | SocketPerformanceWatcher> /* socket_performance_watcher */, |
Eric Roman | 2bc7716 | 2020-09-16 18:30:45 | [diff] [blame] | 244 | NetworkQualityEstimator* /* network_quality_estimator */, |
[email protected] | 0a0b768 | 2010-08-25 17:08:07 | [diff] [blame] | 245 | NetLog* /* net_log */, |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 246 | const NetLogSource& /*source*/) override { |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 247 | allocation_count_++; |
Helen Li | d5bb922 | 2018-04-12 15:33:09 | [diff] [blame] | 248 | return nullptr; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 249 | } |
| 250 | |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 251 | std::unique_ptr<SSLClientSocket> CreateSSLClientSocket( |
David Benjamin | 24725be | 2019-07-24 20:57:18 | [diff] [blame] | 252 | SSLClientContext* context, |
Matt Menke | 841fc41 | 2019-03-05 23:20:12 | [diff] [blame] | 253 | std::unique_ptr<StreamSocket> stream_socket, |
[email protected] | 4f4de7e6 | 2010-11-12 19:55:27 | [diff] [blame] | 254 | const HostPortPair& host_and_port, |
David Benjamin | 24725be | 2019-07-24 20:57:18 | [diff] [blame] | 255 | const SSLConfig& ssl_config) override { |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 256 | NOTIMPLEMENTED(); |
David Benjamin | 24725be | 2019-07-24 20:57:18 | [diff] [blame] | 257 | return nullptr; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 258 | } |
Matt Menke | fd95692 | 2019-02-04 23:44:03 | [diff] [blame] | 259 | |
Matt Menke | 52cd95a | 2019-02-08 06:16:27 | [diff] [blame] | 260 | std::unique_ptr<ProxyClientSocket> CreateProxyClientSocket( |
| 261 | std::unique_ptr<StreamSocket> stream_socket, |
| 262 | const std::string& user_agent, |
| 263 | const HostPortPair& endpoint, |
| 264 | const ProxyServer& proxy_server, |
| 265 | HttpAuthController* http_auth_controller, |
| 266 | bool tunnel, |
| 267 | bool using_spdy, |
| 268 | NextProto negotiated_protocol, |
| 269 | ProxyDelegate* proxy_delegate, |
Matt Menke | 52cd95a | 2019-02-08 06:16:27 | [diff] [blame] | 270 | const NetworkTrafficAnnotationTag& traffic_annotation) override { |
| 271 | NOTIMPLEMENTED(); |
| 272 | return nullptr; |
| 273 | } |
| 274 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 275 | void WaitForSignal(TestConnectJob* job) { waiting_jobs_.push_back(job); } |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 276 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 277 | void SignalJobs(); |
| 278 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 279 | void SignalJob(size_t job); |
| 280 | |
| 281 | void SetJobLoadState(size_t job, LoadState load_state); |
| 282 | |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 283 | // Sets the HasConnectionEstablished value of the specified job to true, |
| 284 | // without invoking the callback. |
| 285 | void SetJobHasEstablishedConnection(size_t job); |
| 286 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 287 | int allocation_count() const { return allocation_count_; } |
| 288 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 289 | private: |
| 290 | int allocation_count_; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 291 | std::vector<TestConnectJob*> waiting_jobs_; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 292 | }; |
| 293 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 294 | class TestConnectJob : public ConnectJob { |
| 295 | public: |
| 296 | enum JobType { |
| 297 | kMockJob, |
| 298 | kMockFailingJob, |
| 299 | kMockPendingJob, |
| 300 | kMockPendingFailingJob, |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 301 | kMockWaitingJob, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 302 | |
| 303 | // Certificate errors return a socket in addition to an error code. |
| 304 | kMockCertErrorJob, |
| 305 | kMockPendingCertErrorJob, |
| 306 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 307 | kMockAdditionalErrorStateJob, |
| 308 | kMockPendingAdditionalErrorStateJob, |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 309 | kMockUnreadDataJob, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 310 | |
| 311 | kMockAuthChallengeOnceJob, |
| 312 | kMockAuthChallengeTwiceJob, |
| 313 | kMockAuthChallengeOnceFailingJob, |
| 314 | kMockAuthChallengeTwiceFailingJob, |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 315 | }; |
| 316 | |
[email protected] | 994d493 | 2010-07-12 17:55:13 | [diff] [blame] | 317 | // The kMockPendingJob uses a slight delay before allowing the connect |
| 318 | // to complete. |
| 319 | static const int kPendingConnectDelay = 2; |
| 320 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 321 | TestConnectJob(JobType job_type, |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 322 | RequestPriority request_priority, |
| 323 | SocketTag socket_tag, |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 324 | base::TimeDelta timeout_duration, |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 325 | const CommonConnectJobParams* common_connect_job_params, |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 326 | ConnectJob::Delegate* delegate, |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 327 | MockClientSocketFactory* client_socket_factory) |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 328 | : ConnectJob(request_priority, |
| 329 | socket_tag, |
Matt Menke | 1a6c92d | 2019-02-23 00:25:38 | [diff] [blame] | 330 | timeout_duration, |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 331 | common_connect_job_params, |
Matt Menke | 1a6c92d | 2019-02-23 00:25:38 | [diff] [blame] | 332 | delegate, |
| 333 | nullptr /* net_log */, |
| 334 | NetLogSourceType::TRANSPORT_CONNECT_JOB, |
| 335 | NetLogEventType::TRANSPORT_CONNECT_JOB_CONNECT), |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 336 | job_type_(job_type), |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 337 | client_socket_factory_(client_socket_factory), |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 338 | load_state_(LOAD_STATE_IDLE), |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 339 | has_established_connection_(false), |
Jeremy Roman | d54000b2 | 2019-07-08 18:40:16 | [diff] [blame] | 340 | store_additional_error_state_(false) {} |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 341 | |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 342 | void Signal() { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 343 | DoConnect(waiting_success_, true /* async */, false /* recoverable */); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 344 | } |
| 345 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 346 | void set_load_state(LoadState load_state) { load_state_ = load_state; } |
| 347 | |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 348 | void set_has_established_connection() { |
| 349 | DCHECK(!has_established_connection_); |
| 350 | has_established_connection_ = true; |
| 351 | } |
| 352 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 353 | // From ConnectJob: |
| 354 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 355 | LoadState GetLoadState() const override { return load_state_; } |
[email protected] | 4645135 | 2009-09-01 14:54:21 | [diff] [blame] | 356 | |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 357 | bool HasEstablishedConnection() const override { |
| 358 | return has_established_connection_; |
| 359 | } |
| 360 | |
dalyk | edd30d98 | 2019-12-16 15:31:10 | [diff] [blame] | 361 | ResolveErrorInfo GetResolveErrorInfo() const override { |
| 362 | return ResolveErrorInfo(OK); |
| 363 | } |
| 364 | |
Matt Menke | 6f84d1f1 | 2019-04-11 19:26:47 | [diff] [blame] | 365 | bool IsSSLError() const override { return store_additional_error_state_; } |
| 366 | |
| 367 | scoped_refptr<SSLCertRequestInfo> GetCertRequestInfo() override { |
| 368 | if (store_additional_error_state_) |
| 369 | return base::MakeRefCounted<SSLCertRequestInfo>(); |
| 370 | return nullptr; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 371 | } |
| 372 | |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 373 | private: |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 374 | // From ConnectJob: |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 375 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 376 | int ConnectInternal() override { |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 377 | AddressList ignored; |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 378 | client_socket_factory_->CreateTransportClientSocket( |
Eric Roman | 2bc7716 | 2020-09-16 18:30:45 | [diff] [blame] | 379 | ignored, nullptr, nullptr, nullptr, NetLogSource()); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 380 | switch (job_type_) { |
| 381 | case kMockJob: |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 382 | return DoConnect(true /* successful */, false /* sync */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 383 | false /* cert_error */); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 384 | case kMockFailingJob: |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 385 | return DoConnect(false /* error */, false /* sync */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 386 | false /* cert_error */); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 387 | case kMockPendingJob: |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 388 | set_load_state(LOAD_STATE_CONNECTING); |
[email protected] | 6b17538 | 2009-10-13 06:47:47 | [diff] [blame] | 389 | |
| 390 | // Depending on execution timings, posting a delayed task can result |
| 391 | // in the task getting executed the at the earliest possible |
| 392 | // opportunity or only after returning once from the message loop and |
| 393 | // then a second call into the message loop. In order to make behavior |
| 394 | // more deterministic, we change the default delay to 2ms. This should |
| 395 | // always require us to wait for the second call into the message loop. |
| 396 | // |
| 397 | // N.B. The correct fix for this and similar timing problems is to |
| 398 | // abstract time for the purpose of unittests. Unfortunately, we have |
| 399 | // a lot of third-party components that directly call the various |
| 400 | // time functions, so this change would be rather invasive. |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 401 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 402 | FROM_HERE, |
kylechar | f4fe517 | 2019-02-15 18:53:49 | [diff] [blame] | 403 | base::BindOnce(base::IgnoreResult(&TestConnectJob::DoConnect), |
| 404 | weak_factory_.GetWeakPtr(), true /* successful */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 405 | true /* async */, false /* cert_error */), |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 406 | base::TimeDelta::FromMilliseconds(kPendingConnectDelay)); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 407 | return ERR_IO_PENDING; |
| 408 | case kMockPendingFailingJob: |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 409 | set_load_state(LOAD_STATE_CONNECTING); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 410 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 411 | FROM_HERE, |
kylechar | f4fe517 | 2019-02-15 18:53:49 | [diff] [blame] | 412 | base::BindOnce(base::IgnoreResult(&TestConnectJob::DoConnect), |
| 413 | weak_factory_.GetWeakPtr(), false /* error */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 414 | true /* async */, false /* cert_error */), |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 415 | base::TimeDelta::FromMilliseconds(2)); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 416 | return ERR_IO_PENDING; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 417 | case kMockWaitingJob: |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 418 | set_load_state(LOAD_STATE_CONNECTING); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 419 | client_socket_factory_->WaitForSignal(this); |
| 420 | waiting_success_ = true; |
| 421 | return ERR_IO_PENDING; |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 422 | case kMockCertErrorJob: |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 423 | return DoConnect(false /* error */, false /* sync */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 424 | true /* cert_error */); |
| 425 | case kMockPendingCertErrorJob: |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 426 | set_load_state(LOAD_STATE_CONNECTING); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 427 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 428 | FROM_HERE, |
kylechar | f4fe517 | 2019-02-15 18:53:49 | [diff] [blame] | 429 | base::BindOnce(base::IgnoreResult(&TestConnectJob::DoConnect), |
| 430 | weak_factory_.GetWeakPtr(), false /* error */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 431 | true /* async */, true /* cert_error */), |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 432 | base::TimeDelta::FromMilliseconds(2)); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 433 | return ERR_IO_PENDING; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 434 | case kMockAdditionalErrorStateJob: |
| 435 | store_additional_error_state_ = true; |
| 436 | return DoConnect(false /* error */, false /* sync */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 437 | false /* cert_error */); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 438 | case kMockPendingAdditionalErrorStateJob: |
| 439 | set_load_state(LOAD_STATE_CONNECTING); |
| 440 | store_additional_error_state_ = true; |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 441 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 442 | FROM_HERE, |
kylechar | f4fe517 | 2019-02-15 18:53:49 | [diff] [blame] | 443 | base::BindOnce(base::IgnoreResult(&TestConnectJob::DoConnect), |
| 444 | weak_factory_.GetWeakPtr(), false /* error */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 445 | true /* async */, false /* cert_error */), |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 446 | base::TimeDelta::FromMilliseconds(2)); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 447 | return ERR_IO_PENDING; |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 448 | case kMockUnreadDataJob: { |
| 449 | int ret = DoConnect(true /* successful */, false /* sync */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 450 | false /* cert_error */); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 451 | static_cast<MockClientSocket*>(socket())->set_has_unread_data(true); |
| 452 | return ret; |
| 453 | } |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 454 | case kMockAuthChallengeOnceJob: |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 455 | set_load_state(LOAD_STATE_CONNECTING); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 456 | DoAdvanceAuthChallenge(1, true /* succeed_after_last_challenge */); |
| 457 | return ERR_IO_PENDING; |
| 458 | case kMockAuthChallengeTwiceJob: |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 459 | set_load_state(LOAD_STATE_CONNECTING); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 460 | DoAdvanceAuthChallenge(2, true /* succeed_after_last_challenge */); |
| 461 | return ERR_IO_PENDING; |
| 462 | case kMockAuthChallengeOnceFailingJob: |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 463 | set_load_state(LOAD_STATE_CONNECTING); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 464 | DoAdvanceAuthChallenge(1, false /* succeed_after_last_challenge */); |
| 465 | return ERR_IO_PENDING; |
| 466 | case kMockAuthChallengeTwiceFailingJob: |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 467 | set_load_state(LOAD_STATE_CONNECTING); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 468 | DoAdvanceAuthChallenge(2, false /* succeed_after_last_challenge */); |
| 469 | return ERR_IO_PENDING; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 470 | default: |
| 471 | NOTREACHED(); |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 472 | SetSocket(std::unique_ptr<StreamSocket>()); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 473 | return ERR_FAILED; |
| 474 | } |
| 475 | } |
| 476 | |
Lily Chen | 02ef29a | 2018-11-30 16:31:43 | [diff] [blame] | 477 | void ChangePriorityInternal(RequestPriority priority) override {} |
| 478 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 479 | int DoConnect(bool succeed, bool was_async, bool cert_error) { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 480 | int result = OK; |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 481 | has_established_connection_ = true; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 482 | if (succeed) { |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 483 | SetSocket(std::make_unique<MockClientSocket>(net_log().net_log())); |
Bence Béky | bdbb0e7 | 2018-08-07 21:42:59 | [diff] [blame] | 484 | socket()->Connect(CompletionOnceCallback()); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 485 | } else if (cert_error) { |
| 486 | SetSocket(std::make_unique<MockClientSocket>(net_log().net_log())); |
| 487 | result = ERR_CERT_COMMON_NAME_INVALID; |
[email protected] | 6e713f0 | 2009-08-06 02:56:40 | [diff] [blame] | 488 | } else { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 489 | result = ERR_CONNECTION_FAILED; |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 490 | SetSocket(std::unique_ptr<StreamSocket>()); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 491 | } |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 492 | |
| 493 | if (was_async) |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 494 | NotifyDelegateOfCompletion(result); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 495 | return result; |
| 496 | } |
| 497 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 498 | void DoAdvanceAuthChallenge(int remaining_challenges, |
| 499 | bool succeed_after_last_challenge) { |
| 500 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 501 | FROM_HERE, |
| 502 | base::BindOnce(&TestConnectJob::InvokeNextProxyAuthCallback, |
| 503 | weak_factory_.GetWeakPtr(), remaining_challenges, |
| 504 | succeed_after_last_challenge)); |
| 505 | } |
| 506 | |
| 507 | void InvokeNextProxyAuthCallback(int remaining_challenges, |
| 508 | bool succeed_after_last_challenge) { |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 509 | set_load_state(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 510 | if (remaining_challenges == 0) { |
| 511 | DoConnect(succeed_after_last_challenge, true /* was_async */, |
| 512 | false /* cert_error */); |
| 513 | return; |
| 514 | } |
| 515 | |
| 516 | // Integration tests make sure HttpResponseInfo and HttpAuthController work. |
| 517 | // The auth tests here are just focused on ConnectJob bookkeeping. |
| 518 | HttpResponseInfo info; |
| 519 | NotifyDelegateOfProxyAuth( |
| 520 | info, nullptr /* http_auth_controller */, |
| 521 | base::BindOnce(&TestConnectJob::DoAdvanceAuthChallenge, |
| 522 | weak_factory_.GetWeakPtr(), remaining_challenges - 1, |
| 523 | succeed_after_last_challenge)); |
| 524 | } |
| 525 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 526 | bool waiting_success_; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 527 | const JobType job_type_; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 528 | MockClientSocketFactory* const client_socket_factory_; |
[email protected] | 4645135 | 2009-09-01 14:54:21 | [diff] [blame] | 529 | LoadState load_state_; |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 530 | bool has_established_connection_; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 531 | bool store_additional_error_state_; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 532 | |
Jeremy Roman | d54000b2 | 2019-07-08 18:40:16 | [diff] [blame] | 533 | base::WeakPtrFactory<TestConnectJob> weak_factory_{this}; |
[email protected] | d5492c5 | 2013-11-10 20:44:39 | [diff] [blame] | 534 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 535 | DISALLOW_COPY_AND_ASSIGN(TestConnectJob); |
| 536 | }; |
| 537 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 538 | class TestConnectJobFactory |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 539 | : public TransportClientSocketPool::ConnectJobFactory { |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 540 | public: |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 541 | TestConnectJobFactory(MockClientSocketFactory* client_socket_factory, |
| 542 | NetLog* net_log) |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 543 | : common_connect_job_params_( |
| 544 | nullptr /* client_socket_factory */, |
| 545 | nullptr /* host_resolver */, |
Matt Menke | b88837e | 2019-03-20 11:50:40 | [diff] [blame] | 546 | nullptr /* http_auth_cache */, |
| 547 | nullptr /* http_auth_handler_factory */, |
| 548 | nullptr /* spdy_session_pool */, |
Matt Menke | b5fb42b | 2019-03-22 17:26:13 | [diff] [blame] | 549 | nullptr /* quic_supported_versions */, |
Matt Menke | b88837e | 2019-03-20 11:50:40 | [diff] [blame] | 550 | nullptr /* quic_stream_factory */, |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 551 | nullptr /* proxy_delegate */, |
Matt Menke | d732ea4 | 2019-03-08 12:05:00 | [diff] [blame] | 552 | nullptr /* http_user_agent_settings */, |
David Benjamin | 24725be | 2019-07-24 20:57:18 | [diff] [blame] | 553 | nullptr /* ssl_client_context */, |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 554 | nullptr /* socket_performance_watcher_factory */, |
| 555 | nullptr /* network_quality_estimator */, |
| 556 | net_log, |
| 557 | nullptr /* websocket_endpoint_lock_manager */), |
| 558 | job_type_(TestConnectJob::kMockJob), |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 559 | job_types_(nullptr), |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 560 | client_socket_factory_(client_socket_factory) {} |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 561 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 562 | ~TestConnectJobFactory() override = default; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 563 | |
| 564 | void set_job_type(TestConnectJob::JobType job_type) { job_type_ = job_type; } |
| 565 | |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 566 | void set_job_types(std::list<TestConnectJob::JobType>* job_types) { |
| 567 | job_types_ = job_types; |
| 568 | CHECK(!job_types_->empty()); |
| 569 | } |
| 570 | |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 571 | void set_timeout_duration(base::TimeDelta timeout_duration) { |
| 572 | timeout_duration_ = timeout_duration; |
| 573 | } |
| 574 | |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 575 | // ConnectJobFactory implementation. |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 576 | |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 577 | std::unique_ptr<ConnectJob> NewConnectJob( |
Matt Menke | aafff54 | 2019-04-22 22:09:36 | [diff] [blame] | 578 | ClientSocketPool::GroupId group_id, |
| 579 | scoped_refptr<ClientSocketPool::SocketParams> socket_params, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 580 | const base::Optional<NetworkTrafficAnnotationTag>& proxy_annotation_tag, |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 581 | RequestPriority request_priority, |
| 582 | SocketTag socket_tag, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 583 | ConnectJob::Delegate* delegate) const override { |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 584 | EXPECT_TRUE(!job_types_ || !job_types_->empty()); |
| 585 | TestConnectJob::JobType job_type = job_type_; |
| 586 | if (job_types_ && !job_types_->empty()) { |
| 587 | job_type = job_types_->front(); |
| 588 | job_types_->pop_front(); |
| 589 | } |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 590 | return std::make_unique<TestConnectJob>( |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 591 | job_type, request_priority, socket_tag, timeout_duration_, |
| 592 | &common_connect_job_params_, delegate, client_socket_factory_); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | private: |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 596 | const CommonConnectJobParams common_connect_job_params_; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 597 | TestConnectJob::JobType job_type_; |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 598 | std::list<TestConnectJob::JobType>* job_types_; |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 599 | base::TimeDelta timeout_duration_; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 600 | MockClientSocketFactory* const client_socket_factory_; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 601 | |
| 602 | DISALLOW_COPY_AND_ASSIGN(TestConnectJobFactory); |
| 603 | }; |
| 604 | |
[email protected] | a937a06d | 2009-08-19 21:19:24 | [diff] [blame] | 605 | } // namespace |
| 606 | |
[email protected] | a937a06d | 2009-08-19 21:19:24 | [diff] [blame] | 607 | namespace { |
| 608 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 609 | void MockClientSocketFactory::SignalJobs() { |
jdoerrie | 22a91d8b9 | 2018-10-05 08:43:26 | [diff] [blame] | 610 | for (auto it = waiting_jobs_.begin(); it != waiting_jobs_.end(); ++it) { |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 611 | (*it)->Signal(); |
| 612 | } |
| 613 | waiting_jobs_.clear(); |
| 614 | } |
| 615 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 616 | void MockClientSocketFactory::SignalJob(size_t job) { |
| 617 | ASSERT_LT(job, waiting_jobs_.size()); |
| 618 | waiting_jobs_[job]->Signal(); |
| 619 | waiting_jobs_.erase(waiting_jobs_.begin() + job); |
| 620 | } |
| 621 | |
| 622 | void MockClientSocketFactory::SetJobLoadState(size_t job, |
| 623 | LoadState load_state) { |
| 624 | ASSERT_LT(job, waiting_jobs_.size()); |
| 625 | waiting_jobs_[job]->set_load_state(load_state); |
| 626 | } |
| 627 | |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 628 | void MockClientSocketFactory::SetJobHasEstablishedConnection(size_t job) { |
| 629 | ASSERT_LT(job, waiting_jobs_.size()); |
| 630 | waiting_jobs_[job]->set_has_established_connection(); |
| 631 | } |
| 632 | |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 633 | class ClientSocketPoolBaseTest : public TestWithTaskEnvironment { |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 634 | protected: |
Alex Clarke | 0def209 | 2018-12-10 12:01:45 | [diff] [blame] | 635 | ClientSocketPoolBaseTest() |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 636 | : TestWithTaskEnvironment( |
| 637 | base::test::TaskEnvironment::TimeSource::MOCK_TIME), |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 638 | params_(ClientSocketPool::SocketParams::CreateForHttpForTesting()) { |
[email protected] | 636b825 | 2011-04-08 19:56:54 | [diff] [blame] | 639 | connect_backup_jobs_enabled_ = |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 640 | TransportClientSocketPool::connect_backup_jobs_enabled(); |
| 641 | TransportClientSocketPool::set_connect_backup_jobs_enabled(true); |
[email protected] | 636b825 | 2011-04-08 19:56:54 | [diff] [blame] | 642 | } |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 643 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 644 | ~ClientSocketPoolBaseTest() override { |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 645 | TransportClientSocketPool::set_connect_backup_jobs_enabled( |
[email protected] | 636b825 | 2011-04-08 19:56:54 | [diff] [blame] | 646 | connect_backup_jobs_enabled_); |
| 647 | } |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 648 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 649 | void CreatePool(int max_sockets, |
| 650 | int max_sockets_per_group, |
| 651 | bool enable_backup_connect_jobs = false) { |
Tarun Bansal | a763509 | 2019-02-20 10:00:59 | [diff] [blame] | 652 | CreatePoolWithIdleTimeouts(max_sockets, max_sockets_per_group, |
| 653 | kUnusedIdleSocketTimeout, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 654 | ClientSocketPool::used_idle_socket_timeout(), |
| 655 | enable_backup_connect_jobs); |
[email protected] | 9bf28db | 2009-08-29 01:35:16 | [diff] [blame] | 656 | } |
| 657 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 658 | void CreatePoolWithIdleTimeouts( |
| 659 | int max_sockets, |
| 660 | int max_sockets_per_group, |
| 661 | base::TimeDelta unused_idle_socket_timeout, |
| 662 | base::TimeDelta used_idle_socket_timeout, |
| 663 | bool enable_backup_connect_jobs = false, |
| 664 | ProxyServer proxy_server = ProxyServer::Direct()) { |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 665 | DCHECK(!pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 666 | std::unique_ptr<TestConnectJobFactory> connect_job_factory = |
| 667 | std::make_unique<TestConnectJobFactory>(&client_socket_factory_, |
| 668 | &net_log_); |
| 669 | connect_job_factory_ = connect_job_factory.get(); |
| 670 | pool_ = TransportClientSocketPool::CreateForTesting( |
| 671 | max_sockets, max_sockets_per_group, unused_idle_socket_timeout, |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 672 | used_idle_socket_timeout, proxy_server, std::move(connect_job_factory), |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 673 | nullptr /* ssl_config_service */, enable_backup_connect_jobs); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 674 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 675 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 676 | int StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 677 | const ClientSocketPool::GroupId& group_id, |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 678 | RequestPriority priority, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 679 | ClientSocketPool::RespectLimits respect_limits) { |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 680 | return test_base_.StartRequestUsingPool(pool_.get(), group_id, priority, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 681 | respect_limits, params_); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 682 | } |
| 683 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 684 | int StartRequest(const ClientSocketPool::GroupId& group_id, |
| 685 | RequestPriority priority) { |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 686 | return StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 687 | group_id, priority, ClientSocketPool::RespectLimits::ENABLED); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 688 | } |
| 689 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 690 | int GetOrderOfRequest(size_t index) const { |
| 691 | return test_base_.GetOrderOfRequest(index); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 692 | } |
| 693 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 694 | bool ReleaseOneConnection(ClientSocketPoolTest::KeepAlive keep_alive) { |
| 695 | return test_base_.ReleaseOneConnection(keep_alive); |
| 696 | } |
| 697 | |
| 698 | void ReleaseAllConnections(ClientSocketPoolTest::KeepAlive keep_alive) { |
| 699 | test_base_.ReleaseAllConnections(keep_alive); |
| 700 | } |
| 701 | |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 702 | // Expects a single NetLogEventType::SOCKET_POOL_CLOSING_SOCKET in |net_log_|. |
| 703 | // It should be logged for the provided source and have the indicated reason. |
| 704 | void ExpectSocketClosedWithReason(NetLogSource expected_source, |
| 705 | const char* expected_reason) { |
| 706 | auto entries = net_log_.GetEntriesForSourceWithType( |
| 707 | expected_source, NetLogEventType::SOCKET_POOL_CLOSING_SOCKET, |
| 708 | NetLogEventPhase::NONE); |
| 709 | ASSERT_EQ(1u, entries.size()); |
| 710 | ASSERT_TRUE(entries[0].HasParams()); |
| 711 | ASSERT_TRUE(entries[0].params.is_dict()); |
| 712 | const std::string* reason = entries[0].params.FindStringKey("reason"); |
| 713 | ASSERT_TRUE(reason); |
| 714 | EXPECT_EQ(expected_reason, *reason); |
| 715 | } |
| 716 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 717 | TestSocketRequest* request(int i) { return test_base_.request(i); } |
| 718 | size_t requests_size() const { return test_base_.requests_size(); } |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 719 | std::vector<std::unique_ptr<TestSocketRequest>>* requests() { |
olli.raula | 9d66b7d | 2015-11-23 08:30:42 | [diff] [blame] | 720 | return test_base_.requests(); |
| 721 | } |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 722 | // Only counts the requests that get sockets asynchronously; |
| 723 | // synchronous completions are not registered by this count. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 724 | size_t completion_count() const { return test_base_.completion_count(); } |
| 725 | |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 726 | RecordingTestNetLog net_log_; |
[email protected] | 636b825 | 2011-04-08 19:56:54 | [diff] [blame] | 727 | bool connect_backup_jobs_enabled_; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 728 | MockClientSocketFactory client_socket_factory_; |
[email protected] | 17a0c6c | 2009-08-04 00:07:04 | [diff] [blame] | 729 | TestConnectJobFactory* connect_job_factory_; |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 730 | // These parameters are never actually used to create a TransportConnectJob. |
Matt Menke | 84d11e56 | 2019-03-27 00:11:19 | [diff] [blame] | 731 | scoped_refptr<ClientSocketPool::SocketParams> params_; |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 732 | std::unique_ptr<TransportClientSocketPool> pool_; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 733 | ClientSocketPoolTest test_base_; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 734 | }; |
| 735 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 736 | TEST_F(ClientSocketPoolBaseTest, BasicSynchronous) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 737 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 738 | |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 739 | TestCompletionCallback callback; |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 740 | ClientSocketHandle handle; |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 741 | RecordingBoundTestNetLog log; |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 742 | TestLoadTimingInfoNotConnected(handle); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 743 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 744 | EXPECT_EQ(OK, handle.Init( |
| 745 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 746 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 747 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 748 | pool_.get(), log.bound())); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 749 | EXPECT_TRUE(handle.is_initialized()); |
| 750 | EXPECT_TRUE(handle.socket()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 751 | TestLoadTimingInfoConnectedNotReused(handle); |
| 752 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 753 | handle.Reset(); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 754 | TestLoadTimingInfoNotConnected(handle); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 755 | |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 756 | auto entries = log.GetEntries(); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 757 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 758 | EXPECT_EQ(5u, entries.size()); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 759 | EXPECT_TRUE(LogContainsEvent( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 760 | entries, 0, NetLogEventType::TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 761 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 762 | EXPECT_TRUE(LogContainsBeginEvent(entries, 1, NetLogEventType::SOCKET_POOL)); |
| 763 | EXPECT_TRUE(LogContainsEvent( |
| 764 | entries, 2, NetLogEventType::SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
| 765 | NetLogEventPhase::NONE)); |
| 766 | EXPECT_TRUE(LogContainsEvent(entries, 3, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 767 | NetLogEventType::SOCKET_POOL_BOUND_TO_SOCKET, |
| 768 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 769 | EXPECT_TRUE(LogContainsEndEvent(entries, 4, NetLogEventType::SOCKET_POOL)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 770 | } |
| 771 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 772 | TEST_F(ClientSocketPoolBaseTest, InitConnectionFailure) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 773 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 774 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 775 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 776 | RecordingBoundTestNetLog log; |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 777 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 778 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 779 | TestCompletionCallback callback; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 780 | // Set the additional error state members to ensure that they get cleared. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 781 | handle.set_is_ssl_error(true); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 782 | handle.set_ssl_cert_request_info(base::MakeRefCounted<SSLCertRequestInfo>()); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 783 | EXPECT_EQ( |
| 784 | ERR_CONNECTION_FAILED, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 785 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 786 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 787 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 788 | pool_.get(), log.bound())); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 789 | EXPECT_FALSE(handle.socket()); |
| 790 | EXPECT_FALSE(handle.is_ssl_error()); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 791 | EXPECT_FALSE(handle.ssl_cert_request_info()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 792 | TestLoadTimingInfoNotConnected(handle); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 793 | |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 794 | auto entries = log.GetEntries(); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 795 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 796 | EXPECT_EQ(4u, entries.size()); |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 797 | EXPECT_TRUE(LogContainsEvent( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 798 | entries, 0, NetLogEventType::TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 799 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 800 | EXPECT_TRUE(LogContainsBeginEvent(entries, 1, NetLogEventType::SOCKET_POOL)); |
| 801 | EXPECT_TRUE(LogContainsEvent( |
| 802 | entries, 2, NetLogEventType::SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
| 803 | NetLogEventPhase::NONE)); |
| 804 | EXPECT_TRUE(LogContainsEndEvent(entries, 3, NetLogEventType::SOCKET_POOL)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 805 | } |
| 806 | |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 807 | // Test releasing an open socket into the socket pool, telling the socket pool |
| 808 | // to close the socket. |
| 809 | TEST_F(ClientSocketPoolBaseTest, ReleaseAndCloseConnection) { |
| 810 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 811 | |
| 812 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 813 | ASSERT_TRUE(request(0)->handle()->socket()); |
| 814 | net::NetLogSource source = request(0)->handle()->socket()->NetLog().source(); |
| 815 | ReleaseOneConnection(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 816 | |
| 817 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 818 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 819 | |
| 820 | ExpectSocketClosedWithReason( |
| 821 | source, TransportClientSocketPool::kClosedConnectionReturnedToPool); |
| 822 | } |
| 823 | |
| 824 | TEST_F(ClientSocketPoolBaseTest, SocketWithUnreadDataReturnedToPool) { |
| 825 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 826 | connect_job_factory_->set_job_type(TestConnectJob::kMockUnreadDataJob); |
| 827 | |
| 828 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 829 | ASSERT_TRUE(request(0)->handle()->socket()); |
| 830 | net::NetLogSource source = request(0)->handle()->socket()->NetLog().source(); |
| 831 | EXPECT_TRUE(request(0)->handle()->socket()->IsConnected()); |
| 832 | EXPECT_FALSE(request(0)->handle()->socket()->IsConnectedAndIdle()); |
| 833 | ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE); |
| 834 | |
| 835 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 836 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 837 | |
| 838 | ExpectSocketClosedWithReason( |
| 839 | source, TransportClientSocketPool::kDataReceivedUnexpectedly); |
| 840 | } |
| 841 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 842 | // Make sure different groups do not share sockets. |
| 843 | TEST_F(ClientSocketPoolBaseTest, GroupSeparation) { |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 844 | base::test::ScopedFeatureList feature_list; |
| 845 | feature_list.InitAndEnableFeature( |
| 846 | features::kPartitionConnectionsByNetworkIsolationKey); |
| 847 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 848 | CreatePool(1000 /* max_sockets */, 2 /* max_sockets_per_group */); |
| 849 | |
| 850 | const HostPortPair kHostPortPairs[] = { |
| 851 | {"a", 80}, |
| 852 | {"a", 443}, |
| 853 | {"b", 80}, |
| 854 | }; |
| 855 | |
| 856 | const ClientSocketPool::SocketType kSocketTypes[] = { |
| 857 | ClientSocketPool::SocketType::kHttp, |
| 858 | ClientSocketPool::SocketType::kSsl, |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 859 | }; |
| 860 | |
Matt Menke | bdf77780 | 2019-04-22 19:38:59 | [diff] [blame] | 861 | const PrivacyMode kPrivacyModes[] = {PrivacyMode::PRIVACY_MODE_DISABLED, |
| 862 | PrivacyMode::PRIVACY_MODE_ENABLED}; |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 863 | |
Shivani Sharma | 8ae506c | 2019-07-21 21:08:27 | [diff] [blame] | 864 | const auto kOriginA = url::Origin::Create(GURL("http://a.test/")); |
| 865 | const auto kOriginB = url::Origin::Create(GURL("http://b.test/")); |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 866 | const NetworkIsolationKey kNetworkIsolationKeys[] = { |
Shivani Sharma | 8ae506c | 2019-07-21 21:08:27 | [diff] [blame] | 867 | NetworkIsolationKey(kOriginA, kOriginA), |
| 868 | NetworkIsolationKey(kOriginB, kOriginB), |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 869 | }; |
| 870 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 871 | const bool kDisableSecureDnsValues[] = {false, true}; |
| 872 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 873 | int total_idle_sockets = 0; |
| 874 | |
| 875 | // Walk through each GroupId, making sure that requesting a socket for one |
| 876 | // group does not return a previously connected socket for another group. |
| 877 | for (const auto& host_port_pair : kHostPortPairs) { |
| 878 | SCOPED_TRACE(host_port_pair.ToString()); |
| 879 | for (const auto& socket_type : kSocketTypes) { |
| 880 | SCOPED_TRACE(static_cast<int>(socket_type)); |
| 881 | for (const auto& privacy_mode : kPrivacyModes) { |
| 882 | SCOPED_TRACE(privacy_mode); |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 883 | for (const auto& network_isolation_key : kNetworkIsolationKeys) { |
| 884 | SCOPED_TRACE(network_isolation_key.ToString()); |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 885 | for (const auto& disable_secure_dns : kDisableSecureDnsValues) { |
| 886 | SCOPED_TRACE(disable_secure_dns); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 887 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 888 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 889 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 890 | ClientSocketPool::GroupId group_id( |
| 891 | host_port_pair, socket_type, privacy_mode, |
| 892 | network_isolation_key, disable_secure_dns); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 893 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 894 | EXPECT_FALSE(pool_->HasGroupForTesting(group_id)); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 895 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 896 | TestCompletionCallback callback; |
| 897 | ClientSocketHandle handle; |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 898 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 899 | // Since the group is empty, requesting a socket should not complete |
| 900 | // synchronously. |
| 901 | EXPECT_THAT(handle.Init(group_id, params_, base::nullopt, |
| 902 | DEFAULT_PRIORITY, SocketTag(), |
| 903 | ClientSocketPool::RespectLimits::ENABLED, |
| 904 | callback.callback(), |
| 905 | ClientSocketPool::ProxyAuthCallback(), |
| 906 | pool_.get(), NetLogWithSource()), |
| 907 | IsError(ERR_IO_PENDING)); |
| 908 | EXPECT_TRUE(pool_->HasGroupForTesting(group_id)); |
| 909 | EXPECT_EQ(total_idle_sockets, pool_->IdleSocketCount()); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 910 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 911 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 912 | EXPECT_TRUE(handle.socket()); |
| 913 | EXPECT_TRUE(pool_->HasGroupForTesting(group_id)); |
| 914 | EXPECT_EQ(total_idle_sockets, pool_->IdleSocketCount()); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 915 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 916 | // Return socket to pool. |
| 917 | handle.Reset(); |
| 918 | EXPECT_EQ(total_idle_sockets + 1, pool_->IdleSocketCount()); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 919 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 920 | // Requesting a socket again should return the same socket as |
| 921 | // before, so should complete synchronously. |
| 922 | EXPECT_THAT(handle.Init(group_id, params_, base::nullopt, |
| 923 | DEFAULT_PRIORITY, SocketTag(), |
| 924 | ClientSocketPool::RespectLimits::ENABLED, |
| 925 | callback.callback(), |
| 926 | ClientSocketPool::ProxyAuthCallback(), |
| 927 | pool_.get(), NetLogWithSource()), |
| 928 | IsOk()); |
| 929 | EXPECT_TRUE(handle.socket()); |
| 930 | EXPECT_EQ(total_idle_sockets, pool_->IdleSocketCount()); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 931 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 932 | // Return socket to pool again. |
| 933 | handle.Reset(); |
| 934 | EXPECT_EQ(total_idle_sockets + 1, pool_->IdleSocketCount()); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 935 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 936 | ++total_idle_sockets; |
| 937 | } |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 938 | } |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 939 | } |
| 940 | } |
| 941 | } |
| 942 | } |
| 943 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 944 | TEST_F(ClientSocketPoolBaseTest, TotalLimit) { |
| 945 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 946 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 947 | // TODO(eroman): Check that the NetLog contains this event. |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 948 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 949 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 950 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
| 951 | EXPECT_THAT(StartRequest(TestGroupId("c"), DEFAULT_PRIORITY), IsOk()); |
| 952 | EXPECT_THAT(StartRequest(TestGroupId("d"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 953 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 954 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 955 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 956 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 957 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 958 | EXPECT_THAT(StartRequest(TestGroupId("e"), DEFAULT_PRIORITY), |
| 959 | IsError(ERR_IO_PENDING)); |
| 960 | EXPECT_THAT(StartRequest(TestGroupId("f"), DEFAULT_PRIORITY), |
| 961 | IsError(ERR_IO_PENDING)); |
| 962 | EXPECT_THAT(StartRequest(TestGroupId("g"), DEFAULT_PRIORITY), |
| 963 | IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 964 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 965 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 966 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 967 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 968 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 969 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 970 | |
| 971 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 972 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 973 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 974 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 975 | EXPECT_EQ(5, GetOrderOfRequest(5)); |
| 976 | EXPECT_EQ(6, GetOrderOfRequest(6)); |
| 977 | EXPECT_EQ(7, GetOrderOfRequest(7)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 978 | |
| 979 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 980 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(8)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 981 | } |
| 982 | |
| 983 | TEST_F(ClientSocketPoolBaseTest, TotalLimitReachedNewGroup) { |
| 984 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 985 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 986 | // TODO(eroman): Check that the NetLog contains this event. |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 987 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 988 | // Reach all limits: max total sockets, and max sockets per group. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 989 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 990 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 991 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
| 992 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 993 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 994 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 995 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 996 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 997 | |
| 998 | // Now create a new group and verify that we don't starve it. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 999 | EXPECT_THAT(StartRequest(TestGroupId("c"), DEFAULT_PRIORITY), |
| 1000 | IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1001 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1002 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1003 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1004 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1005 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1006 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1007 | |
| 1008 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1009 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1010 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 1011 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 1012 | EXPECT_EQ(5, GetOrderOfRequest(5)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1013 | |
| 1014 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1015 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(6)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1016 | } |
| 1017 | |
| 1018 | TEST_F(ClientSocketPoolBaseTest, TotalLimitRespectsPriority) { |
| 1019 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1020 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1021 | EXPECT_THAT(StartRequest(TestGroupId("b"), LOWEST), IsOk()); |
| 1022 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsOk()); |
| 1023 | EXPECT_THAT(StartRequest(TestGroupId("b"), HIGHEST), IsOk()); |
| 1024 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1025 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1026 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1027 | client_socket_factory_.allocation_count()); |
| 1028 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1029 | EXPECT_THAT(StartRequest(TestGroupId("c"), LOWEST), IsError(ERR_IO_PENDING)); |
| 1030 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1031 | EXPECT_THAT(StartRequest(TestGroupId("b"), HIGHEST), IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1032 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1033 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1034 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1035 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1036 | |
| 1037 | // First 4 requests don't have to wait, and finish in order. |
| 1038 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1039 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1040 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 1041 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 1042 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1043 | // Request ("b", HIGHEST) has the highest priority, then (TestGroupId("a"), |
| 1044 | // MEDIUM), and then ("c", LOWEST). |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1045 | EXPECT_EQ(7, GetOrderOfRequest(5)); |
| 1046 | EXPECT_EQ(6, GetOrderOfRequest(6)); |
| 1047 | EXPECT_EQ(5, GetOrderOfRequest(7)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1048 | |
| 1049 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1050 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(9)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1051 | } |
| 1052 | |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1053 | // Test reprioritizing a request before completion doesn't interfere with |
| 1054 | // its completion. |
| 1055 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeOne) { |
| 1056 | CreatePool(kDefaultMaxSockets, 1); |
| 1057 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1058 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1059 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1060 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1061 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1062 | |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 1063 | request(1)->handle()->SetPriority(HIGHEST); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1064 | |
| 1065 | ReleaseOneConnection(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1066 | |
| 1067 | EXPECT_TRUE(request(1)->handle()->socket()); |
| 1068 | } |
| 1069 | |
| 1070 | // Reprioritize a request up past another one and make sure that changes the |
| 1071 | // completion order. |
| 1072 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeUpReorder) { |
| 1073 | CreatePool(kDefaultMaxSockets, 1); |
| 1074 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1075 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1076 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1077 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1078 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1079 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1080 | EXPECT_FALSE(request(2)->handle()->socket()); |
| 1081 | |
| 1082 | request(2)->handle()->SetPriority(HIGHEST); |
| 1083 | |
| 1084 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1085 | |
| 1086 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1087 | EXPECT_EQ(3, GetOrderOfRequest(2)); |
| 1088 | EXPECT_EQ(2, GetOrderOfRequest(3)); |
| 1089 | } |
| 1090 | |
| 1091 | // Reprioritize a request without changing relative priorities and check |
| 1092 | // that the order doesn't change. |
| 1093 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeUpNoReorder) { |
| 1094 | CreatePool(kDefaultMaxSockets, 1); |
| 1095 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1096 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1097 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1098 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1099 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1100 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1101 | EXPECT_FALSE(request(2)->handle()->socket()); |
| 1102 | |
| 1103 | request(2)->handle()->SetPriority(MEDIUM); |
| 1104 | |
| 1105 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1106 | |
| 1107 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1108 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1109 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 1110 | } |
| 1111 | |
| 1112 | // Reprioritize a request past down another one and make sure that changes the |
| 1113 | // completion order. |
| 1114 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeDownReorder) { |
| 1115 | CreatePool(kDefaultMaxSockets, 1); |
| 1116 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1117 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1118 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1119 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1120 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1121 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1122 | EXPECT_FALSE(request(2)->handle()->socket()); |
| 1123 | |
| 1124 | request(1)->handle()->SetPriority(LOW); |
| 1125 | |
| 1126 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1127 | |
| 1128 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1129 | EXPECT_EQ(3, GetOrderOfRequest(2)); |
| 1130 | EXPECT_EQ(2, GetOrderOfRequest(3)); |
| 1131 | } |
| 1132 | |
| 1133 | // Reprioritize a request to the same level as another and confirm it is |
| 1134 | // put after the old request. |
| 1135 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeResetFIFO) { |
| 1136 | CreatePool(kDefaultMaxSockets, 1); |
| 1137 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1138 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1139 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1140 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1141 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1142 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1143 | EXPECT_FALSE(request(2)->handle()->socket()); |
| 1144 | |
| 1145 | request(1)->handle()->SetPriority(MEDIUM); |
| 1146 | |
| 1147 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1148 | |
| 1149 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1150 | EXPECT_EQ(3, GetOrderOfRequest(2)); |
| 1151 | EXPECT_EQ(2, GetOrderOfRequest(3)); |
| 1152 | } |
| 1153 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1154 | TEST_F(ClientSocketPoolBaseTest, TotalLimitRespectsGroupLimit) { |
| 1155 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1156 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1157 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsOk()); |
| 1158 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsOk()); |
| 1159 | EXPECT_THAT(StartRequest(TestGroupId("b"), HIGHEST), IsOk()); |
| 1160 | EXPECT_THAT(StartRequest(TestGroupId("b"), MEDIUM), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1161 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1162 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1163 | client_socket_factory_.allocation_count()); |
| 1164 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1165 | EXPECT_THAT(StartRequest(TestGroupId("c"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1166 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 1167 | EXPECT_THAT(StartRequest(TestGroupId("b"), HIGHEST), IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1168 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1169 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1170 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1171 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1172 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1173 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1174 | |
| 1175 | // First 4 requests don't have to wait, and finish in order. |
| 1176 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1177 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1178 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 1179 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 1180 | |
| 1181 | // Request ("b", 7) has the highest priority, but we can't make new socket for |
| 1182 | // group "b", because it has reached the per-group limit. Then we make |
| 1183 | // socket for ("c", 6), because it has higher priority than ("a", 4), |
| 1184 | // and we still can't make a socket for group "b". |
| 1185 | EXPECT_EQ(5, GetOrderOfRequest(5)); |
| 1186 | EXPECT_EQ(6, GetOrderOfRequest(6)); |
| 1187 | EXPECT_EQ(7, GetOrderOfRequest(7)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1188 | |
| 1189 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1190 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(8)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1191 | } |
| 1192 | |
| 1193 | // Make sure that we count connecting sockets against the total limit. |
| 1194 | TEST_F(ClientSocketPoolBaseTest, TotalLimitCountsConnectingSockets) { |
| 1195 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1196 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1197 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1198 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
| 1199 | EXPECT_THAT(StartRequest(TestGroupId("c"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1200 | |
| 1201 | // Create one asynchronous request. |
| 1202 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1203 | EXPECT_THAT(StartRequest(TestGroupId("d"), DEFAULT_PRIORITY), |
| 1204 | IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1205 | |
[email protected] | 6b17538 | 2009-10-13 06:47:47 | [diff] [blame] | 1206 | // We post all of our delayed tasks with a 2ms delay. I.e. they don't |
| 1207 | // actually become pending until 2ms after they have been created. In order |
| 1208 | // to flush all tasks, we need to wait so that we know there are no |
| 1209 | // soon-to-be-pending tasks waiting. |
Alex Clarke | 0def209 | 2018-12-10 12:01:45 | [diff] [blame] | 1210 | FastForwardBy(base::TimeDelta::FromMilliseconds(10)); |
[email protected] | 6b17538 | 2009-10-13 06:47:47 | [diff] [blame] | 1211 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1212 | // The next synchronous request should wait for its turn. |
| 1213 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1214 | EXPECT_THAT(StartRequest(TestGroupId("e"), DEFAULT_PRIORITY), |
| 1215 | IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1216 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1217 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1218 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1219 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1220 | client_socket_factory_.allocation_count()); |
| 1221 | |
| 1222 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1223 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1224 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 1225 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1226 | EXPECT_EQ(5, GetOrderOfRequest(5)); |
| 1227 | |
| 1228 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1229 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(6)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1230 | } |
| 1231 | |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1232 | TEST_F(ClientSocketPoolBaseTest, CorrectlyCountStalledGroups) { |
| 1233 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 1234 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 1235 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1236 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 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()); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1240 | |
| 1241 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 1242 | |
| 1243 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1244 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1245 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), |
| 1246 | IsError(ERR_IO_PENDING)); |
| 1247 | EXPECT_THAT(StartRequest(TestGroupId("c"), DEFAULT_PRIORITY), |
| 1248 | IsError(ERR_IO_PENDING)); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1249 | |
| 1250 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1251 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1252 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1253 | EXPECT_EQ(kDefaultMaxSockets + 1, client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1254 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1255 | EXPECT_EQ(kDefaultMaxSockets + 2, client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1256 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
| 1257 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1258 | EXPECT_EQ(kDefaultMaxSockets + 2, client_socket_factory_.allocation_count()); |
| 1259 | } |
| 1260 | |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 1261 | TEST_F(ClientSocketPoolBaseTest, StallAndThenCancelAndTriggerAvailableSocket) { |
| 1262 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 1263 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1264 | |
| 1265 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1266 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1267 | EXPECT_EQ( |
| 1268 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1269 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1270 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1271 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1272 | pool_.get(), NetLogWithSource())); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 1273 | |
| 1274 | ClientSocketHandle handles[4]; |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 1275 | for (size_t i = 0; i < base::size(handles); ++i) { |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1276 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1277 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1278 | handles[i].Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1279 | TestGroupId("b"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1280 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1281 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1282 | pool_.get(), NetLogWithSource())); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 1283 | } |
| 1284 | |
| 1285 | // One will be stalled, cancel all the handles now. |
| 1286 | // This should hit the OnAvailableSocketSlot() code where we previously had |
| 1287 | // stalled groups, but no longer have any. |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 1288 | for (size_t i = 0; i < base::size(handles); ++i) |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 1289 | handles[i].Reset(); |
| 1290 | } |
| 1291 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1292 | TEST_F(ClientSocketPoolBaseTest, CancelStalledSocketAtSocketLimit) { |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1293 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1294 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 1295 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1296 | { |
| 1297 | ClientSocketHandle handles[kDefaultMaxSockets]; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1298 | TestCompletionCallback callbacks[kDefaultMaxSockets]; |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1299 | for (int i = 0; i < kDefaultMaxSockets; ++i) { |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1300 | EXPECT_EQ(OK, |
| 1301 | handles[i].Init(TestGroupId(base::NumberToString(i)), params_, |
| 1302 | base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
| 1303 | ClientSocketPool::RespectLimits::ENABLED, |
| 1304 | callbacks[i].callback(), |
| 1305 | ClientSocketPool::ProxyAuthCallback(), |
| 1306 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1307 | } |
| 1308 | |
| 1309 | // Force a stalled group. |
| 1310 | ClientSocketHandle stalled_handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1311 | TestCompletionCallback callback; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1312 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1313 | stalled_handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1314 | TestGroupId("foo"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1315 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1316 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1317 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1318 | |
| 1319 | // Cancel the stalled request. |
| 1320 | stalled_handle.Reset(); |
| 1321 | |
| 1322 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1323 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 1324 | |
| 1325 | // Dropping out of scope will close all handles and return them to idle. |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1326 | } |
| 1327 | |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1328 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1329 | EXPECT_EQ(kDefaultMaxSockets, pool_->IdleSocketCount()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1330 | } |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1331 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1332 | TEST_F(ClientSocketPoolBaseTest, CancelPendingSocketAtSocketLimit) { |
| 1333 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1334 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 1335 | |
| 1336 | { |
| 1337 | ClientSocketHandle handles[kDefaultMaxSockets]; |
| 1338 | for (int i = 0; i < kDefaultMaxSockets; ++i) { |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1339 | TestCompletionCallback callback; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1340 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1341 | handles[i].Init(TestGroupId(base::NumberToString(i)), params_, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1342 | base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1343 | ClientSocketPool::RespectLimits::ENABLED, |
| 1344 | callback.callback(), |
| 1345 | ClientSocketPool::ProxyAuthCallback(), |
| 1346 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1347 | } |
| 1348 | |
| 1349 | // Force a stalled group. |
| 1350 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1351 | ClientSocketHandle stalled_handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1352 | TestCompletionCallback callback; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1353 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1354 | stalled_handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1355 | TestGroupId("foo"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1356 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1357 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1358 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1359 | |
| 1360 | // Since it is stalled, it should have no connect jobs. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1361 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("foo"))); |
| 1362 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 1363 | TestGroupId("foo"))); |
| 1364 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroupForTesting( |
| 1365 | TestGroupId("foo"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1366 | |
| 1367 | // Cancel the stalled request. |
| 1368 | handles[0].Reset(); |
| 1369 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1370 | // Now we should have a connect job. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1371 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("foo"))); |
| 1372 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 1373 | TestGroupId("foo"))); |
| 1374 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroupForTesting( |
| 1375 | TestGroupId("foo"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1376 | |
| 1377 | // The stalled socket should connect. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1378 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1379 | |
| 1380 | EXPECT_EQ(kDefaultMaxSockets + 1, |
| 1381 | client_socket_factory_.allocation_count()); |
| 1382 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1383 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("foo"))); |
| 1384 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 1385 | TestGroupId("foo"))); |
| 1386 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroupForTesting( |
| 1387 | TestGroupId("foo"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1388 | |
| 1389 | // Dropping out of scope will close all handles and return them to idle. |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1390 | } |
| 1391 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1392 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 1393 | } |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1394 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1395 | TEST_F(ClientSocketPoolBaseTest, WaitForStalledSocketAtSocketLimit) { |
| 1396 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1397 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1398 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1399 | ClientSocketHandle stalled_handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1400 | TestCompletionCallback callback; |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1401 | { |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 1402 | EXPECT_FALSE(pool_->IsStalled()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1403 | ClientSocketHandle handles[kDefaultMaxSockets]; |
| 1404 | for (int i = 0; i < kDefaultMaxSockets; ++i) { |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1405 | TestCompletionCallback callback; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1406 | EXPECT_EQ( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1407 | OK, handles[i].Init( |
| 1408 | TestGroupId(base::StringPrintf("Take 2: %d", i)), params_, |
| 1409 | base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
| 1410 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1411 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 1412 | NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1413 | } |
| 1414 | |
| 1415 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1416 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 1417 | EXPECT_FALSE(pool_->IsStalled()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1418 | |
| 1419 | // Now we will hit the socket limit. |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1420 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1421 | stalled_handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1422 | TestGroupId("foo"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1423 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1424 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1425 | pool_.get(), NetLogWithSource())); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 1426 | EXPECT_TRUE(pool_->IsStalled()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1427 | |
| 1428 | // Dropping out of scope will close all handles and return them to idle. |
| 1429 | } |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1430 | |
| 1431 | // But if we wait for it, the released idle sockets will be closed in |
| 1432 | // preference of the waiting request. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1433 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1434 | |
| 1435 | EXPECT_EQ(kDefaultMaxSockets + 1, client_socket_factory_.allocation_count()); |
| 1436 | EXPECT_EQ(3, pool_->IdleSocketCount()); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1437 | } |
| 1438 | |
| 1439 | // Regression test for http://crbug.com/40952. |
| 1440 | TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketAtSocketLimitDeleteGroup) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1441 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 1442 | true /* enable_backup_connect_jobs */); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1443 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 1444 | |
| 1445 | for (int i = 0; i < kDefaultMaxSockets; ++i) { |
| 1446 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1447 | TestCompletionCallback callback; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1448 | EXPECT_EQ(OK, handle.Init(TestGroupId(base::NumberToString(i)), params_, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1449 | base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1450 | ClientSocketPool::RespectLimits::ENABLED, |
| 1451 | callback.callback(), |
| 1452 | ClientSocketPool::ProxyAuthCallback(), |
| 1453 | pool_.get(), NetLogWithSource())); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1454 | } |
| 1455 | |
| 1456 | // Flush all the DoReleaseSocket tasks. |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 1457 | base::RunLoop().RunUntilIdle(); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1458 | |
| 1459 | // Stall a group. Set a pending job so it'll trigger a backup job if we don't |
| 1460 | // reuse a socket. |
| 1461 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1462 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1463 | TestCompletionCallback callback; |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1464 | |
| 1465 | // "0" is special here, since it should be the first entry in the sorted map, |
| 1466 | // which is the one which we would close an idle socket for. We shouldn't |
| 1467 | // close an idle socket though, since we should reuse the idle socket. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1468 | EXPECT_EQ(OK, handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1469 | TestGroupId("0"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1470 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1471 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1472 | pool_.get(), NetLogWithSource())); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1473 | |
| 1474 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1475 | EXPECT_EQ(kDefaultMaxSockets - 1, pool_->IdleSocketCount()); |
| 1476 | } |
| 1477 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1478 | TEST_F(ClientSocketPoolBaseTest, PendingRequests) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1479 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1480 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1481 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1482 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1483 | EXPECT_THAT(StartRequest(TestGroupId("a"), IDLE), IsError(ERR_IO_PENDING)); |
| 1484 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
| 1485 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1486 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1487 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 1488 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1489 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1490 | ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1491 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
| 1492 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1493 | EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup, |
| 1494 | completion_count()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1495 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1496 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1497 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
[email protected] | c9c6f5c | 2010-07-31 01:30:03 | [diff] [blame] | 1498 | EXPECT_EQ(8, GetOrderOfRequest(3)); |
| 1499 | EXPECT_EQ(6, GetOrderOfRequest(4)); |
| 1500 | EXPECT_EQ(4, GetOrderOfRequest(5)); |
| 1501 | EXPECT_EQ(3, GetOrderOfRequest(6)); |
| 1502 | EXPECT_EQ(5, GetOrderOfRequest(7)); |
| 1503 | EXPECT_EQ(7, GetOrderOfRequest(8)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1504 | |
| 1505 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1506 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(9)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1507 | } |
| 1508 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1509 | TEST_F(ClientSocketPoolBaseTest, PendingRequests_NoKeepAlive) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1510 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1511 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1512 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1513 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1514 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
| 1515 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1516 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1517 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 1518 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1519 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1520 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1521 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1522 | for (size_t i = kDefaultMaxSocketsPerGroup; i < requests_size(); ++i) |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1523 | EXPECT_THAT(request(i)->WaitForResult(), IsOk()); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1524 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1525 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1526 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1527 | EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup, |
| 1528 | completion_count()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1529 | } |
| 1530 | |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1531 | TEST_F(ClientSocketPoolBaseTest, ResetAndCloseSocket) { |
| 1532 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1533 | |
| 1534 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1535 | ClientSocketHandle handle; |
| 1536 | TestCompletionCallback callback; |
| 1537 | EXPECT_EQ( |
| 1538 | ERR_IO_PENDING, |
| 1539 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1540 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1541 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1542 | pool_.get(), NetLogWithSource())); |
| 1543 | |
| 1544 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 1545 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1546 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1547 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
| 1548 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 1549 | |
| 1550 | handle.ResetAndCloseSocket(); |
| 1551 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1552 | } |
| 1553 | |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1554 | // This test will start up a socket request and then call Reset() on the handle. |
| 1555 | // The pending ConnectJob should not be destroyed. |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1556 | TEST_F(ClientSocketPoolBaseTest, CancelRequestKeepsConnectJob) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1557 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1558 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1559 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1560 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1561 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1562 | EXPECT_EQ( |
| 1563 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1564 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1565 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1566 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1567 | pool_.get(), NetLogWithSource())); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1568 | handle.Reset(); |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1569 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1570 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1571 | } |
| 1572 | |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1573 | // This test will start up a socket request and then call ResetAndCloseSocket() |
| 1574 | // on the handle. The pending ConnectJob or connected socket should be |
| 1575 | // destroyed. |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1576 | TEST_F(ClientSocketPoolBaseTest, CancelRequestAndCloseSocket) { |
| 1577 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1578 | |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1579 | // When true, the socket connects before it's canceled. |
| 1580 | for (bool cancel_when_callback_pending : {false, true}) { |
| 1581 | if (cancel_when_callback_pending) { |
| 1582 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 1583 | } else { |
| 1584 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1585 | } |
| 1586 | ClientSocketHandle handle; |
| 1587 | TestCompletionCallback callback; |
| 1588 | EXPECT_EQ( |
| 1589 | ERR_IO_PENDING, |
| 1590 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1591 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1592 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1593 | pool_.get(), NetLogWithSource())); |
| 1594 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1595 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1596 | |
| 1597 | if (cancel_when_callback_pending) { |
| 1598 | client_socket_factory_.SignalJobs(); |
| 1599 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1600 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 1601 | } |
| 1602 | |
| 1603 | handle.ResetAndCloseSocket(); |
| 1604 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1605 | } |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1606 | } |
| 1607 | |
| 1608 | TEST_F(ClientSocketPoolBaseTest, |
| 1609 | CancelRequestAndCloseSocketWhenMoreRequestsThanConnectJobs) { |
| 1610 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1611 | |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1612 | // When true, the sockets connect before they're canceled. |
| 1613 | for (bool cancel_when_callback_pending : {false, true}) { |
| 1614 | if (cancel_when_callback_pending) { |
| 1615 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 1616 | } else { |
| 1617 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1618 | } |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1619 | |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1620 | std::vector<std::unique_ptr<ClientSocketHandle>> handles; |
| 1621 | TestCompletionCallback callback; |
| 1622 | // Make |kDefaultMaxSockets + 1| socket requests. |
| 1623 | for (int i = 0; i < kDefaultMaxSocketsPerGroup + 1; ++i) { |
| 1624 | std::unique_ptr<ClientSocketHandle> handle = |
| 1625 | std::make_unique<ClientSocketHandle>(); |
| 1626 | EXPECT_EQ(ERR_IO_PENDING, |
| 1627 | handle->Init( |
| 1628 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1629 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1630 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1631 | pool_.get(), NetLogWithSource())); |
| 1632 | handles.push_back(std::move(handle)); |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1633 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1634 | EXPECT_EQ( |
| 1635 | static_cast<size_t>(std::min(i + 1, kDefaultMaxSocketsPerGroup)), |
| 1636 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1637 | } |
| 1638 | |
| 1639 | if (cancel_when_callback_pending) { |
| 1640 | client_socket_factory_.SignalJobs(); |
| 1641 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1642 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
| 1643 | pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 1644 | } |
| 1645 | |
| 1646 | // Calling ResetAndCloseSocket() on a handle should not cancel a ConnectJob |
| 1647 | // or close a socket, since there are more requests than ConnectJobs or |
| 1648 | // sockets. |
| 1649 | handles[kDefaultMaxSocketsPerGroup]->ResetAndCloseSocket(); |
| 1650 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1651 | if (cancel_when_callback_pending) { |
| 1652 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
| 1653 | pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 1654 | } else { |
| 1655 | EXPECT_EQ(static_cast<size_t>(kDefaultMaxSocketsPerGroup), |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1656 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1657 | } |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1658 | |
| 1659 | // Calling ResetAndCloseSocket() on other handles should cancel a ConnectJob |
| 1660 | // or close a socket. |
| 1661 | for (int i = kDefaultMaxSocketsPerGroup - 1; i >= 0; --i) { |
| 1662 | handles[i]->ResetAndCloseSocket(); |
| 1663 | if (i > 0) { |
| 1664 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1665 | if (cancel_when_callback_pending) { |
| 1666 | EXPECT_EQ(i, |
| 1667 | pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 1668 | } else { |
| 1669 | EXPECT_EQ(static_cast<size_t>(i), |
| 1670 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1671 | } |
| 1672 | } else { |
| 1673 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1674 | } |
| 1675 | } |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1676 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1677 | } |
| 1678 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1679 | TEST_F(ClientSocketPoolBaseTest, ConnectCancelConnect) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1680 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1681 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1682 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 1683 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1684 | TestCompletionCallback callback; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1685 | |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1686 | EXPECT_EQ( |
| 1687 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1688 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1689 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1690 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1691 | pool_.get(), NetLogWithSource())); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1692 | |
| 1693 | handle.Reset(); |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1694 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1695 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1696 | |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1697 | // This will create a second ConnectJob, since the other ConnectJob was |
| 1698 | // previously assigned to a request. |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1699 | TestCompletionCallback callback2; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1700 | EXPECT_EQ( |
| 1701 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1702 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1703 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1704 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1705 | pool_.get(), NetLogWithSource())); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1706 | |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1707 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1708 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1709 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1710 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1711 | EXPECT_FALSE(callback.have_result()); |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1712 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1713 | // One ConnectJob completed, and its socket is now assigned to |handle|. |
| 1714 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 1715 | // The other ConnectJob should have either completed, or still be connecting. |
| 1716 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")) + |
| 1717 | pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1718 | |
| 1719 | handle.Reset(); |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1720 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1721 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")) + |
| 1722 | pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
| 1723 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1724 | } |
| 1725 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1726 | TEST_F(ClientSocketPoolBaseTest, CancelRequest) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1727 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1728 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1729 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1730 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1731 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
| 1732 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1733 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1734 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 1735 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1736 | |
| 1737 | // Cancel a request. |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1738 | size_t index_to_cancel = kDefaultMaxSocketsPerGroup + 2; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1739 | EXPECT_FALSE((*requests())[index_to_cancel]->handle()->is_initialized()); |
| 1740 | (*requests())[index_to_cancel]->handle()->Reset(); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1741 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1742 | ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1743 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1744 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
| 1745 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1746 | EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup - 1, |
| 1747 | completion_count()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1748 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1749 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1750 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1751 | EXPECT_EQ(5, GetOrderOfRequest(3)); |
| 1752 | EXPECT_EQ(3, GetOrderOfRequest(4)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1753 | EXPECT_EQ(ClientSocketPoolTest::kRequestNotFound, |
| 1754 | GetOrderOfRequest(5)); // Canceled request. |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1755 | EXPECT_EQ(4, GetOrderOfRequest(6)); |
| 1756 | EXPECT_EQ(6, GetOrderOfRequest(7)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1757 | |
| 1758 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1759 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(8)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1760 | } |
| 1761 | |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1762 | // Function to be used as a callback on socket request completion. It first |
| 1763 | // disconnects the successfully connected socket from the first request, and |
| 1764 | // then reuses the ClientSocketHandle to request another socket. |
| 1765 | // |
| 1766 | // |nested_callback| is called with the result of the second socket request. |
| 1767 | void RequestSocketOnComplete(ClientSocketHandle* handle, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1768 | TransportClientSocketPool* pool, |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1769 | TestConnectJobFactory* test_connect_job_factory, |
| 1770 | TestConnectJob::JobType next_job_type, |
Bence Béky | a4a5093 | 2018-08-10 13:39:41 | [diff] [blame] | 1771 | TestCompletionCallback* nested_callback, |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1772 | int first_request_result) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1773 | EXPECT_THAT(first_request_result, IsOk()); |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1774 | |
| 1775 | test_connect_job_factory->set_job_type(next_job_type); |
| 1776 | |
| 1777 | // Don't allow reuse of the socket. Disconnect it and then release it. |
| 1778 | if (handle->socket()) |
| 1779 | handle->socket()->Disconnect(); |
| 1780 | handle->Reset(); |
| 1781 | |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1782 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1783 | int rv = handle->Init( |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 1784 | TestGroupId("a"), |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1785 | ClientSocketPool::SocketParams::CreateForHttpForTesting(), base::nullopt, |
| 1786 | LOWEST, SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 1787 | nested_callback->callback(), ClientSocketPool::ProxyAuthCallback(), pool, |
| 1788 | NetLogWithSource()); |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1789 | if (rv != ERR_IO_PENDING) { |
| 1790 | DCHECK_EQ(TestConnectJob::kMockJob, next_job_type); |
Bence Béky | a4a5093 | 2018-08-10 13:39:41 | [diff] [blame] | 1791 | nested_callback->callback().Run(rv); |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1792 | } else { |
| 1793 | DCHECK_EQ(TestConnectJob::kMockPendingJob, next_job_type); |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1794 | } |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1795 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1796 | |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1797 | // Tests the case where a second socket is requested in a completion callback, |
| 1798 | // and the second socket connects asynchronously. Reuses the same |
| 1799 | // ClientSocketHandle for the second socket, after disconnecting the first. |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1800 | TEST_F(ClientSocketPoolBaseTest, RequestPendingJobTwice) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1801 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1802 | |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 1803 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 1804 | ClientSocketHandle handle; |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1805 | TestCompletionCallback second_result_callback; |
| 1806 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1807 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Paul Jensen | 8d6f87ec | 2018-01-13 00:46:54 | [diff] [blame] | 1808 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1809 | base::BindOnce(&RequestSocketOnComplete, &handle, pool_.get(), |
| 1810 | connect_job_factory_, TestConnectJob::kMockPendingJob, |
| 1811 | &second_result_callback), |
| 1812 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1813 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1814 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1815 | EXPECT_THAT(second_result_callback.WaitForResult(), IsOk()); |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1816 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1817 | |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1818 | // Tests the case where a second socket is requested in a completion callback, |
| 1819 | // and the second socket connects synchronously. Reuses the same |
| 1820 | // ClientSocketHandle for the second socket, after disconnecting the first. |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1821 | TEST_F(ClientSocketPoolBaseTest, RequestPendingJobThenSynchronous) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1822 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1823 | |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 1824 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 1825 | ClientSocketHandle handle; |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1826 | TestCompletionCallback second_result_callback; |
| 1827 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1828 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Paul Jensen | 8d6f87ec | 2018-01-13 00:46:54 | [diff] [blame] | 1829 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1830 | base::BindOnce(&RequestSocketOnComplete, &handle, pool_.get(), |
| 1831 | connect_job_factory_, TestConnectJob::kMockPendingJob, |
| 1832 | &second_result_callback), |
| 1833 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1834 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1835 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1836 | EXPECT_THAT(second_result_callback.WaitForResult(), IsOk()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1837 | } |
| 1838 | |
| 1839 | // Make sure that pending requests get serviced after active requests get |
| 1840 | // cancelled. |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1841 | TEST_F(ClientSocketPoolBaseTest, CancelActiveRequestWithPendingRequests) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1842 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1843 | |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 1844 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1845 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1846 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1847 | IsError(ERR_IO_PENDING)); |
| 1848 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1849 | IsError(ERR_IO_PENDING)); |
| 1850 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1851 | IsError(ERR_IO_PENDING)); |
| 1852 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1853 | IsError(ERR_IO_PENDING)); |
| 1854 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1855 | IsError(ERR_IO_PENDING)); |
| 1856 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1857 | IsError(ERR_IO_PENDING)); |
| 1858 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1859 | IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1860 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1861 | // Now, kDefaultMaxSocketsPerGroup requests should be active. |
| 1862 | // Let's cancel them. |
| 1863 | for (int i = 0; i < kDefaultMaxSocketsPerGroup; ++i) { |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1864 | ASSERT_FALSE(request(i)->handle()->is_initialized()); |
| 1865 | request(i)->handle()->Reset(); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1866 | } |
| 1867 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1868 | // Let's wait for the rest to complete now. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1869 | for (size_t i = kDefaultMaxSocketsPerGroup; i < requests_size(); ++i) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1870 | EXPECT_THAT(request(i)->WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1871 | request(i)->handle()->Reset(); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1872 | } |
| 1873 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1874 | EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup, |
| 1875 | completion_count()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1876 | } |
| 1877 | |
| 1878 | // Make sure that pending requests get serviced after active requests fail. |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1879 | TEST_F(ClientSocketPoolBaseTest, FailingActiveRequestWithPendingRequests) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1880 | const size_t kMaxSockets = 5; |
| 1881 | CreatePool(kMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1882 | |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 1883 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1884 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1885 | const size_t kNumberOfRequests = 2 * kDefaultMaxSocketsPerGroup + 1; |
| 1886 | ASSERT_LE(kNumberOfRequests, kMaxSockets); // Otherwise the test will hang. |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1887 | |
| 1888 | // Queue up all the requests |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1889 | for (size_t i = 0; i < kNumberOfRequests; ++i) |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1890 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1891 | IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1892 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1893 | for (size_t i = 0; i < kNumberOfRequests; ++i) |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1894 | EXPECT_THAT(request(i)->WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1895 | } |
| 1896 | |
mmenke | 9d72fe4 | 2017-05-18 22:36:07 | [diff] [blame] | 1897 | // Make sure that pending requests that complete synchronously get serviced |
| 1898 | // after active requests fail. See https://crbug.com/723748 |
| 1899 | TEST_F(ClientSocketPoolBaseTest, HandleMultipleSyncFailuresAfterAsyncFailure) { |
| 1900 | const size_t kNumberOfRequests = 10; |
| 1901 | const size_t kMaxSockets = 1; |
| 1902 | CreatePool(kMaxSockets, kMaxSockets); |
| 1903 | |
| 1904 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
| 1905 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1906 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1907 | IsError(ERR_IO_PENDING)); |
mmenke | 9d72fe4 | 2017-05-18 22:36:07 | [diff] [blame] | 1908 | |
| 1909 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
| 1910 | |
| 1911 | // Queue up all the other requests |
| 1912 | for (size_t i = 1; i < kNumberOfRequests; ++i) |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1913 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1914 | IsError(ERR_IO_PENDING)); |
mmenke | 9d72fe4 | 2017-05-18 22:36:07 | [diff] [blame] | 1915 | |
| 1916 | // Make sure all requests fail, instead of hanging. |
| 1917 | for (size_t i = 0; i < kNumberOfRequests; ++i) |
| 1918 | EXPECT_THAT(request(i)->WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 1919 | } |
| 1920 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1921 | TEST_F(ClientSocketPoolBaseTest, CancelActiveRequestThenRequestSocket) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1922 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1923 | |
| 1924 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1925 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1926 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1927 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1928 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1929 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1930 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1931 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1932 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1933 | |
| 1934 | // Cancel the active request. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1935 | handle.Reset(); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1936 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1937 | rv = handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1938 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1939 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1940 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1941 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 1942 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1943 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1944 | EXPECT_FALSE(handle.is_reused()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 1945 | TestLoadTimingInfoConnectedNotReused(handle); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1946 | EXPECT_EQ(2, client_socket_factory_.allocation_count()); |
| 1947 | } |
| 1948 | |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1949 | TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketsForced) { |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 1950 | const char kReason[] = "Really nifty reason"; |
| 1951 | |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1952 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1953 | ClientSocketHandle handle; |
| 1954 | TestCompletionCallback callback; |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 1955 | RecordingBoundTestNetLog log; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1956 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1957 | TestGroupId("a"), params_, base::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1958 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1959 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1960 | EXPECT_THAT(rv, IsOk()); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 1961 | ASSERT_TRUE(handle.socket()); |
| 1962 | NetLogSource source = handle.socket()->NetLog().source(); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1963 | handle.Reset(); |
| 1964 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 1965 | pool_->CloseIdleSockets(kReason); |
| 1966 | ExpectSocketClosedWithReason(source, kReason); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1967 | } |
| 1968 | |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1969 | TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketsInGroupForced) { |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1970 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1971 | TestCompletionCallback callback; |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 1972 | RecordingBoundTestNetLog log; |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1973 | ClientSocketHandle handle1; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1974 | int rv = handle1.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1975 | TestGroupId("a"), params_, base::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1976 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1977 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1978 | EXPECT_THAT(rv, IsOk()); |
| 1979 | ClientSocketHandle handle2; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1980 | rv = handle2.Init(TestGroupId("a"), params_, base::nullopt, LOWEST, |
| 1981 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1982 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1983 | pool_.get(), log.bound()); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1984 | ClientSocketHandle handle3; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1985 | rv = handle3.Init(TestGroupId("b"), params_, base::nullopt, LOWEST, |
| 1986 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1987 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1988 | pool_.get(), log.bound()); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1989 | EXPECT_THAT(rv, IsOk()); |
| 1990 | handle1.Reset(); |
| 1991 | handle2.Reset(); |
| 1992 | handle3.Reset(); |
| 1993 | EXPECT_EQ(3, pool_->IdleSocketCount()); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 1994 | pool_->CloseIdleSocketsInGroup(TestGroupId("a"), "Very good reason"); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1995 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1996 | } |
| 1997 | |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1998 | TEST_F(ClientSocketPoolBaseTest, CleanUpUnusableIdleSockets) { |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1999 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2000 | ClientSocketHandle handle; |
| 2001 | TestCompletionCallback callback; |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 2002 | RecordingBoundTestNetLog log; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2003 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2004 | TestGroupId("a"), params_, base::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2005 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2006 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 2007 | EXPECT_THAT(rv, IsOk()); |
| 2008 | StreamSocket* socket = handle.socket(); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 2009 | ASSERT_TRUE(socket); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 2010 | handle.Reset(); |
| 2011 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 2012 | |
| 2013 | // Disconnect socket now to make the socket unusable. |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 2014 | NetLogSource source = socket->NetLog().source(); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 2015 | socket->Disconnect(); |
| 2016 | ClientSocketHandle handle2; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2017 | rv = handle2.Init(TestGroupId("a"), params_, base::nullopt, LOWEST, |
| 2018 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2019 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2020 | pool_.get(), log.bound()); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 2021 | EXPECT_THAT(rv, IsOk()); |
| 2022 | EXPECT_FALSE(handle2.is_reused()); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 2023 | |
| 2024 | // This is admittedly not an accurate error in this case, but normally code |
| 2025 | // doesn't secretly keep a raw pointers to sockets returned to the socket pool |
| 2026 | // and close them out of band, so discovering an idle socket was closed when |
| 2027 | // trying to reuse it normally means it was closed by the remote side. |
| 2028 | ExpectSocketClosedWithReason( |
| 2029 | source, TransportClientSocketPool::kRemoteSideClosedConnection); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 2030 | } |
| 2031 | |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 2032 | // Regression test for http://crbug.com/17985. |
| 2033 | TEST_F(ClientSocketPoolBaseTest, GroupWithPendingRequestsIsNotEmpty) { |
| 2034 | const int kMaxSockets = 3; |
| 2035 | const int kMaxSocketsPerGroup = 2; |
| 2036 | CreatePool(kMaxSockets, kMaxSocketsPerGroup); |
| 2037 | |
[email protected] | ac790b4 | 2009-12-02 04:31:31 | [diff] [blame] | 2038 | const RequestPriority kHighPriority = HIGHEST; |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 2039 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2040 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 2041 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 2042 | |
| 2043 | // This is going to be a pending request in an otherwise empty group. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2044 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2045 | IsError(ERR_IO_PENDING)); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 2046 | |
| 2047 | // Reach the maximum socket limit. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2048 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 2049 | |
| 2050 | // Create a stalled group with high priorities. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2051 | EXPECT_THAT(StartRequest(TestGroupId("c"), kHighPriority), |
| 2052 | IsError(ERR_IO_PENDING)); |
| 2053 | EXPECT_THAT(StartRequest(TestGroupId("c"), kHighPriority), |
| 2054 | IsError(ERR_IO_PENDING)); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 2055 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2056 | // Release the first two sockets from TestGroupId("a"). Because this is a |
| 2057 | // keepalive, the first release will unblock the pending request for |
| 2058 | // TestGroupId("a"). The second release will unblock a request for "c", |
| 2059 | // because it is the next high priority socket. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2060 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
| 2061 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 2062 | |
| 2063 | // Closing idle sockets should not get us into trouble, but in the bug |
| 2064 | // we were hitting a CHECK here. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2065 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 2066 | pool_->CloseIdleSockets("Very good reason"); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 2067 | |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 2068 | // Run the released socket wakeups. |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 2069 | base::RunLoop().RunUntilIdle(); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 2070 | } |
| 2071 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2072 | TEST_F(ClientSocketPoolBaseTest, BasicAsynchronous) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2073 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2074 | |
| 2075 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2076 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2077 | TestCompletionCallback callback; |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 2078 | RecordingBoundTestNetLog log; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2079 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2080 | TestGroupId("a"), params_, base::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2081 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2082 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2083 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2084 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2085 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 2086 | TestLoadTimingInfoNotConnected(handle); |
| 2087 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2088 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2089 | EXPECT_TRUE(handle.is_initialized()); |
| 2090 | EXPECT_TRUE(handle.socket()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 2091 | TestLoadTimingInfoConnectedNotReused(handle); |
| 2092 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2093 | handle.Reset(); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 2094 | TestLoadTimingInfoNotConnected(handle); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 2095 | |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 2096 | auto entries = log.GetEntries(); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 2097 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2098 | EXPECT_EQ(5u, entries.size()); |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 2099 | EXPECT_TRUE(LogContainsEvent( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2100 | entries, 0, NetLogEventType::TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 2101 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2102 | EXPECT_TRUE(LogContainsBeginEvent(entries, 1, NetLogEventType::SOCKET_POOL)); |
| 2103 | EXPECT_TRUE(LogContainsEvent( |
| 2104 | entries, 2, NetLogEventType::SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
| 2105 | NetLogEventPhase::NONE)); |
| 2106 | EXPECT_TRUE(LogContainsEvent(entries, 3, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 2107 | NetLogEventType::SOCKET_POOL_BOUND_TO_SOCKET, |
| 2108 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2109 | EXPECT_TRUE(LogContainsEndEvent(entries, 4, NetLogEventType::SOCKET_POOL)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2110 | } |
| 2111 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2112 | TEST_F(ClientSocketPoolBaseTest, |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2113 | InitConnectionAsynchronousFailure) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2114 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2115 | |
| 2116 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2117 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2118 | TestCompletionCallback callback; |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 2119 | RecordingBoundTestNetLog log; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2120 | // Set the additional error state members to ensure that they get cleared. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2121 | handle.set_is_ssl_error(true); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 2122 | handle.set_ssl_cert_request_info(base::MakeRefCounted<SSLCertRequestInfo>()); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2123 | EXPECT_EQ( |
| 2124 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2125 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2126 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2127 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2128 | pool_.get(), log.bound())); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2129 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2130 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2131 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2132 | EXPECT_FALSE(handle.is_ssl_error()); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 2133 | EXPECT_FALSE(handle.ssl_cert_request_info()); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 2134 | |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 2135 | auto entries = log.GetEntries(); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 2136 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2137 | EXPECT_EQ(4u, entries.size()); |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 2138 | EXPECT_TRUE(LogContainsEvent( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2139 | entries, 0, NetLogEventType::TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 2140 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2141 | EXPECT_TRUE(LogContainsBeginEvent(entries, 1, NetLogEventType::SOCKET_POOL)); |
| 2142 | EXPECT_TRUE(LogContainsEvent( |
| 2143 | entries, 2, NetLogEventType::SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
| 2144 | NetLogEventPhase::NONE)); |
| 2145 | EXPECT_TRUE(LogContainsEndEvent(entries, 3, NetLogEventType::SOCKET_POOL)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2146 | } |
| 2147 | |
mmenke | 6be122f | 2015-03-09 22:22:47 | [diff] [blame] | 2148 | // Check that an async ConnectJob failure does not result in creation of a new |
| 2149 | // ConnectJob when there's another pending request also waiting on its own |
| 2150 | // ConnectJob. See http://crbug.com/463960. |
| 2151 | TEST_F(ClientSocketPoolBaseTest, AsyncFailureWithPendingRequestWithJob) { |
| 2152 | CreatePool(2, 2); |
| 2153 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
| 2154 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2155 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2156 | IsError(ERR_IO_PENDING)); |
| 2157 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2158 | IsError(ERR_IO_PENDING)); |
mmenke | 6be122f | 2015-03-09 22:22:47 | [diff] [blame] | 2159 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2160 | EXPECT_THAT(request(0)->WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 2161 | EXPECT_THAT(request(1)->WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
mmenke | 6be122f | 2015-03-09 22:22:47 | [diff] [blame] | 2162 | |
| 2163 | EXPECT_EQ(2, client_socket_factory_.allocation_count()); |
| 2164 | } |
| 2165 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2166 | TEST_F(ClientSocketPoolBaseTest, TwoRequestsCancelOne) { |
[email protected] | b22b516 | 2010-03-16 07:53:10 | [diff] [blame] | 2167 | // TODO(eroman): Add back the log expectations! Removed them because the |
| 2168 | // ordering is difficult, and some may fire during destructor. |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2169 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2170 | |
| 2171 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2172 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2173 | TestCompletionCallback callback; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2174 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2175 | TestCompletionCallback callback2; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2176 | |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2177 | EXPECT_EQ( |
| 2178 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2179 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2180 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2181 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2182 | pool_.get(), NetLogWithSource())); |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 2183 | RecordingBoundTestNetLog log2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 2184 | EXPECT_EQ( |
| 2185 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2186 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2187 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2188 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2189 | pool_.get(), NetLogWithSource())); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2190 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2191 | handle.Reset(); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2192 | |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 2193 | |
| 2194 | // 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] | 2195 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2196 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2197 | handle2.Reset(); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 2198 | |
| 2199 | // Now request 2 has actually finished. |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 2200 | // TODO(eroman): Add back log expectations. |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2201 | } |
| 2202 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2203 | TEST_F(ClientSocketPoolBaseTest, CancelRequestLimitsJobs) { |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 2204 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2205 | |
[email protected] | 17a0c6c | 2009-08-04 00:07:04 | [diff] [blame] | 2206 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2207 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2208 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
| 2209 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 2210 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 2211 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 2212 | |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2213 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2214 | static_cast<int>( |
| 2215 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2216 | (*requests())[2]->handle()->Reset(); |
| 2217 | (*requests())[3]->handle()->Reset(); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2218 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2219 | static_cast<int>( |
| 2220 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 2221 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2222 | (*requests())[1]->handle()->Reset(); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2223 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2224 | static_cast<int>( |
| 2225 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 2226 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2227 | (*requests())[0]->handle()->Reset(); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2228 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2229 | static_cast<int>( |
| 2230 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 2231 | } |
| 2232 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2233 | // When requests and ConnectJobs are not coupled, the request will get serviced |
| 2234 | // by whatever comes first. |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2235 | TEST_F(ClientSocketPoolBaseTest, ReleaseSockets) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2236 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2237 | |
| 2238 | // Start job 1 (async OK) |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 2239 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2240 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2241 | std::vector<TestSocketRequest*> request_order; |
| 2242 | size_t completion_count; // unused |
| 2243 | TestSocketRequest req1(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2244 | int rv = req1.handle()->Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2245 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2246 | ClientSocketPool::RespectLimits::ENABLED, req1.callback(), |
| 2247 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2248 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 2249 | EXPECT_THAT(req1.WaitForResult(), IsOk()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2250 | |
| 2251 | // Job 1 finished OK. Start job 2 (also async OK). Request 3 is pending |
| 2252 | // without a job. |
| 2253 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2254 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2255 | TestSocketRequest req2(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2256 | rv = req2.handle()->Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2257 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2258 | ClientSocketPool::RespectLimits::ENABLED, req2.callback(), |
| 2259 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2260 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2261 | TestSocketRequest req3(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2262 | rv = req3.handle()->Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2263 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2264 | ClientSocketPool::RespectLimits::ENABLED, req3.callback(), |
| 2265 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2266 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2267 | |
| 2268 | // Both Requests 2 and 3 are pending. We release socket 1 which should |
| 2269 | // service request 2. Request 3 should still be waiting. |
[email protected] | a6c59f6 | 2009-07-29 16:33:33 | [diff] [blame] | 2270 | req1.handle()->Reset(); |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 2271 | // Run the released socket wakeups. |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 2272 | base::RunLoop().RunUntilIdle(); |
[email protected] | a6c59f6 | 2009-07-29 16:33:33 | [diff] [blame] | 2273 | ASSERT_TRUE(req2.handle()->socket()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2274 | EXPECT_THAT(req2.WaitForResult(), IsOk()); |
[email protected] | a6c59f6 | 2009-07-29 16:33:33 | [diff] [blame] | 2275 | EXPECT_FALSE(req3.handle()->socket()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2276 | |
| 2277 | // Signal job 2, which should service request 3. |
| 2278 | |
| 2279 | client_socket_factory_.SignalJobs(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2280 | EXPECT_THAT(req3.WaitForResult(), IsOk()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2281 | |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2282 | ASSERT_EQ(3u, request_order.size()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2283 | EXPECT_EQ(&req1, request_order[0]); |
| 2284 | EXPECT_EQ(&req2, request_order[1]); |
| 2285 | EXPECT_EQ(&req3, request_order[2]); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2286 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2287 | } |
| 2288 | |
| 2289 | // The requests are not coupled to the jobs. So, the requests should finish in |
| 2290 | // their priority / insertion order. |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2291 | TEST_F(ClientSocketPoolBaseTest, PendingJobCompletionOrder) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2292 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2293 | // First two jobs are async. |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 2294 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2295 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2296 | std::vector<TestSocketRequest*> request_order; |
| 2297 | size_t completion_count; // unused |
| 2298 | TestSocketRequest req1(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2299 | int rv = req1.handle()->Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2300 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2301 | ClientSocketPool::RespectLimits::ENABLED, req1.callback(), |
| 2302 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2303 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2304 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2305 | TestSocketRequest req2(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2306 | rv = req2.handle()->Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2307 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2308 | ClientSocketPool::RespectLimits::ENABLED, req2.callback(), |
| 2309 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2310 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2311 | |
| 2312 | // The pending job is sync. |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 2313 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2314 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2315 | TestSocketRequest req3(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2316 | rv = req3.handle()->Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2317 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2318 | ClientSocketPool::RespectLimits::ENABLED, req3.callback(), |
| 2319 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2320 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2321 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2322 | EXPECT_THAT(req1.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 2323 | EXPECT_THAT(req2.WaitForResult(), IsOk()); |
| 2324 | EXPECT_THAT(req3.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2325 | |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2326 | ASSERT_EQ(3u, request_order.size()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2327 | EXPECT_EQ(&req1, request_order[0]); |
| 2328 | EXPECT_EQ(&req2, request_order[1]); |
| 2329 | EXPECT_EQ(&req3, request_order[2]); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2330 | } |
| 2331 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2332 | // Test GetLoadState in the case there's only one socket request. |
| 2333 | TEST_F(ClientSocketPoolBaseTest, LoadStateOneRequest) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2334 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2335 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2336 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2337 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2338 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2339 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2340 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2341 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2342 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2343 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2344 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2345 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2346 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE); |
| 2347 | EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle.GetLoadState()); |
| 2348 | |
| 2349 | // No point in completing the connection, since ClientSocketHandles only |
| 2350 | // expect the LoadState to be checked while connecting. |
| 2351 | } |
| 2352 | |
| 2353 | // Test GetLoadState in the case there are two socket requests. |
| 2354 | TEST_F(ClientSocketPoolBaseTest, LoadStateTwoRequests) { |
| 2355 | CreatePool(2, 2); |
| 2356 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2357 | |
| 2358 | ClientSocketHandle handle; |
| 2359 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2360 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2361 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2362 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2363 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2364 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2365 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_RESOLVING_HOST); |
| 2366 | |
| 2367 | ClientSocketHandle handle2; |
| 2368 | TestCompletionCallback callback2; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2369 | rv = handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2370 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2371 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2372 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2373 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2374 | client_socket_factory_.SetJobLoadState(1, LOAD_STATE_RESOLVING_HOST); |
| 2375 | |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 2376 | // Each handle should reflect the state of its own job. |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2377 | EXPECT_EQ(LOAD_STATE_RESOLVING_HOST, handle.GetLoadState()); |
| 2378 | EXPECT_EQ(LOAD_STATE_RESOLVING_HOST, handle2.GetLoadState()); |
| 2379 | |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 2380 | // Update the state of the first job. |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2381 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_CONNECTING); |
| 2382 | |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 2383 | // Only the state of the first request should have changed. |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2384 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2385 | EXPECT_EQ(LOAD_STATE_RESOLVING_HOST, handle2.GetLoadState()); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 2386 | |
| 2387 | // Update the state of the second job. |
| 2388 | client_socket_factory_.SetJobLoadState(1, LOAD_STATE_SSL_HANDSHAKE); |
| 2389 | |
| 2390 | // Only the state of the second request should have changed. |
| 2391 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
| 2392 | EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle2.GetLoadState()); |
| 2393 | |
| 2394 | // Second job connects and the first request gets the socket. The |
| 2395 | // second handle switches to the state of the remaining ConnectJob. |
| 2396 | client_socket_factory_.SignalJob(1); |
| 2397 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 2398 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle2.GetLoadState()); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2399 | } |
| 2400 | |
| 2401 | // Test GetLoadState in the case the per-group limit is reached. |
| 2402 | TEST_F(ClientSocketPoolBaseTest, LoadStateGroupLimit) { |
| 2403 | CreatePool(2, 1); |
| 2404 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2405 | |
| 2406 | ClientSocketHandle handle; |
| 2407 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2408 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2409 | TestGroupId("a"), params_, base::nullopt, MEDIUM, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2410 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2411 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2412 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2413 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
| 2414 | |
| 2415 | // Request another socket from the same pool, buth with a higher priority. |
| 2416 | // The first request should now be stalled at the socket group limit. |
| 2417 | ClientSocketHandle handle2; |
| 2418 | TestCompletionCallback callback2; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2419 | rv = handle2.Init(TestGroupId("a"), params_, base::nullopt, HIGHEST, |
| 2420 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2421 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2422 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2423 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2424 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET, handle.GetLoadState()); |
| 2425 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle2.GetLoadState()); |
| 2426 | |
| 2427 | // The first handle should remain stalled as the other socket goes through |
| 2428 | // the connect process. |
| 2429 | |
| 2430 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE); |
| 2431 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET, handle.GetLoadState()); |
| 2432 | EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle2.GetLoadState()); |
| 2433 | |
| 2434 | client_socket_factory_.SignalJob(0); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2435 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2436 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET, handle.GetLoadState()); |
| 2437 | |
| 2438 | // Closing the second socket should cause the stalled handle to finally get a |
| 2439 | // ConnectJob. |
| 2440 | handle2.socket()->Disconnect(); |
| 2441 | handle2.Reset(); |
| 2442 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
| 2443 | } |
| 2444 | |
| 2445 | // Test GetLoadState in the case the per-pool limit is reached. |
| 2446 | TEST_F(ClientSocketPoolBaseTest, LoadStatePoolLimit) { |
| 2447 | CreatePool(2, 2); |
| 2448 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2449 | |
| 2450 | ClientSocketHandle handle; |
| 2451 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2452 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2453 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2454 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2455 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2456 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2457 | |
| 2458 | // Request for socket from another pool. |
| 2459 | ClientSocketHandle handle2; |
| 2460 | TestCompletionCallback callback2; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2461 | rv = handle2.Init(TestGroupId("b"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2462 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2463 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2464 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2465 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2466 | |
| 2467 | // Request another socket from the first pool. Request should stall at the |
| 2468 | // socket pool limit. |
| 2469 | ClientSocketHandle handle3; |
| 2470 | TestCompletionCallback callback3; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2471 | rv = handle3.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2472 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2473 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2474 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2475 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2476 | |
| 2477 | // The third handle should remain stalled as the other sockets in its group |
| 2478 | // goes through the connect process. |
| 2479 | |
| 2480 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
| 2481 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL, handle3.GetLoadState()); |
| 2482 | |
| 2483 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE); |
| 2484 | EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle.GetLoadState()); |
| 2485 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL, handle3.GetLoadState()); |
| 2486 | |
| 2487 | client_socket_factory_.SignalJob(0); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2488 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2489 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL, handle3.GetLoadState()); |
| 2490 | |
| 2491 | // Closing a socket should allow the stalled handle to finally get a new |
| 2492 | // ConnectJob. |
| 2493 | handle.socket()->Disconnect(); |
| 2494 | handle.Reset(); |
| 2495 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle3.GetLoadState()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2496 | } |
| 2497 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2498 | TEST_F(ClientSocketPoolBaseTest, CertError) { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2499 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2500 | connect_job_factory_->set_job_type(TestConnectJob::kMockCertErrorJob); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2501 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2502 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2503 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2504 | EXPECT_EQ( |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2505 | ERR_CERT_COMMON_NAME_INVALID, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2506 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2507 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2508 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2509 | pool_.get(), NetLogWithSource())); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2510 | EXPECT_TRUE(handle.is_initialized()); |
| 2511 | EXPECT_TRUE(handle.socket()); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2512 | } |
| 2513 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2514 | TEST_F(ClientSocketPoolBaseTest, AsyncCertError) { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2515 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2516 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2517 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingCertErrorJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2518 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2519 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2520 | EXPECT_EQ( |
| 2521 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2522 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2523 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2524 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2525 | pool_.get(), NetLogWithSource())); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2526 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2527 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2528 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_CERT_COMMON_NAME_INVALID)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2529 | EXPECT_TRUE(handle.is_initialized()); |
| 2530 | EXPECT_TRUE(handle.socket()); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2531 | } |
| 2532 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2533 | TEST_F(ClientSocketPoolBaseTest, AdditionalErrorStateSynchronous) { |
| 2534 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2535 | connect_job_factory_->set_job_type( |
| 2536 | TestConnectJob::kMockAdditionalErrorStateJob); |
| 2537 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2538 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2539 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2540 | EXPECT_EQ( |
| 2541 | ERR_CONNECTION_FAILED, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2542 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2543 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2544 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2545 | pool_.get(), NetLogWithSource())); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2546 | EXPECT_FALSE(handle.is_initialized()); |
| 2547 | EXPECT_FALSE(handle.socket()); |
| 2548 | EXPECT_TRUE(handle.is_ssl_error()); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 2549 | EXPECT_TRUE(handle.ssl_cert_request_info()); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2550 | } |
| 2551 | |
| 2552 | TEST_F(ClientSocketPoolBaseTest, AdditionalErrorStateAsynchronous) { |
| 2553 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2554 | |
| 2555 | connect_job_factory_->set_job_type( |
| 2556 | TestConnectJob::kMockPendingAdditionalErrorStateJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2557 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2558 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2559 | EXPECT_EQ( |
| 2560 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2561 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2562 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2563 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2564 | pool_.get(), NetLogWithSource())); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2565 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2566 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2567 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2568 | EXPECT_FALSE(handle.is_initialized()); |
| 2569 | EXPECT_FALSE(handle.socket()); |
| 2570 | EXPECT_TRUE(handle.is_ssl_error()); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 2571 | EXPECT_TRUE(handle.ssl_cert_request_info()); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2572 | } |
| 2573 | |
martijn | 003cd61 | 2016-05-19 22:24:38 | [diff] [blame] | 2574 | // Make sure we can reuse sockets. |
| 2575 | TEST_F(ClientSocketPoolBaseTest, CleanupTimedOutIdleSocketsReuse) { |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2576 | CreatePoolWithIdleTimeouts( |
| 2577 | kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2578 | base::TimeDelta(), // Time out unused sockets immediately. |
| 2579 | base::TimeDelta::FromDays(1)); // Don't time out used sockets. |
| 2580 | |
| 2581 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2582 | |
| 2583 | ClientSocketHandle handle; |
| 2584 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2585 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2586 | TestGroupId("a"), params_, base::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2587 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2588 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2589 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2590 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2591 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2592 | ASSERT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2593 | |
| 2594 | // Use and release the socket. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 2595 | EXPECT_EQ(1, handle.socket()->Write(nullptr, 1, CompletionOnceCallback(), |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 2596 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 2597 | TestLoadTimingInfoConnectedNotReused(handle); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2598 | handle.Reset(); |
| 2599 | |
| 2600 | // Should now have one idle socket. |
| 2601 | ASSERT_EQ(1, pool_->IdleSocketCount()); |
| 2602 | |
| 2603 | // Request a new socket. This should reuse the old socket and complete |
| 2604 | // synchronously. |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 2605 | RecordingBoundTestNetLog log; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2606 | rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2607 | TestGroupId("a"), params_, base::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2608 | ClientSocketPool::RespectLimits::ENABLED, CompletionOnceCallback(), |
| 2609 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2610 | ASSERT_THAT(rv, IsOk()); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2611 | EXPECT_TRUE(handle.is_reused()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 2612 | TestLoadTimingInfoConnectedReused(handle); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2613 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2614 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2615 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2616 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2617 | |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 2618 | auto entries = log.GetEntries(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2619 | EXPECT_TRUE(LogContainsEvent( |
| 2620 | entries, 0, NetLogEventType::TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET, |
| 2621 | NetLogEventPhase::NONE)); |
| 2622 | EXPECT_TRUE(LogContainsBeginEvent(entries, 1, NetLogEventType::SOCKET_POOL)); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2623 | EXPECT_TRUE(LogContainsEntryWithType( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2624 | entries, 2, NetLogEventType::SOCKET_POOL_REUSED_AN_EXISTING_SOCKET)); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2625 | } |
| 2626 | |
martijn | 003cd61 | 2016-05-19 22:24:38 | [diff] [blame] | 2627 | // Make sure we cleanup old unused sockets. |
Eric Roman | b49715e | 2018-04-24 22:41:17 | [diff] [blame] | 2628 | TEST_F(ClientSocketPoolBaseTest, CleanupTimedOutIdleSocketsNoReuse) { |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2629 | CreatePoolWithIdleTimeouts( |
| 2630 | kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 2631 | base::TimeDelta(), // Time out unused sockets immediately |
| 2632 | base::TimeDelta()); // Time out used sockets immediately |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2633 | |
| 2634 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2635 | |
| 2636 | // Startup two mock pending connect jobs, which will sit in the MessageLoop. |
| 2637 | |
| 2638 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2639 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2640 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2641 | TestGroupId("a"), params_, base::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2642 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2643 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2644 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2645 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2646 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2647 | |
| 2648 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2649 | TestCompletionCallback callback2; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2650 | rv = handle2.Init(TestGroupId("a"), params_, base::nullopt, LOWEST, |
| 2651 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2652 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2653 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2654 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2655 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2656 | pool_->GetLoadState(TestGroupId("a"), &handle2)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2657 | |
| 2658 | // Cancel one of the requests. Wait for the other, which will get the first |
| 2659 | // job. Release the socket. Run the loop again to make sure the second |
| 2660 | // socket is sitting idle and the first one is released (since ReleaseSocket() |
| 2661 | // just posts a DoReleaseSocket() task). |
| 2662 | |
| 2663 | handle.Reset(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2664 | ASSERT_THAT(callback2.WaitForResult(), IsOk()); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 2665 | // Get the NetLogSource for the socket, so the time out reason can be checked |
| 2666 | // at the end of the test. |
| 2667 | NetLogSource net_log_source2 = handle2.socket()->NetLog().source(); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2668 | // Use the socket. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 2669 | EXPECT_EQ(1, handle2.socket()->Write(nullptr, 1, CompletionOnceCallback(), |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 2670 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2671 | handle2.Reset(); |
| 2672 | |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2673 | // We post all of our delayed tasks with a 2ms delay. I.e. they don't |
| 2674 | // actually become pending until 2ms after they have been created. In order |
| 2675 | // to flush all tasks, we need to wait so that we know there are no |
| 2676 | // soon-to-be-pending tasks waiting. |
Alex Clarke | 0def209 | 2018-12-10 12:01:45 | [diff] [blame] | 2677 | FastForwardBy(base::TimeDelta::FromMilliseconds(10)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2678 | |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2679 | // Both sockets should now be idle. |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2680 | ASSERT_EQ(2, pool_->IdleSocketCount()); |
| 2681 | |
| 2682 | // Request a new socket. This should cleanup the unused and timed out ones. |
| 2683 | // A new socket will be created rather than reusing the idle one. |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 2684 | RecordingBoundTestNetLog log; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2685 | TestCompletionCallback callback3; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2686 | rv = handle.Init(TestGroupId("a"), params_, base::nullopt, LOWEST, |
| 2687 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2688 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2689 | pool_.get(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2690 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 2691 | ASSERT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2692 | EXPECT_FALSE(handle.is_reused()); |
| 2693 | |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2694 | // Make sure the idle socket is closed. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2695 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2696 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2697 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2698 | |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 2699 | auto entries = log.GetEntries(); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2700 | EXPECT_FALSE(LogContainsEntryWithType( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 2701 | entries, 1, NetLogEventType::SOCKET_POOL_REUSED_AN_EXISTING_SOCKET)); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 2702 | ExpectSocketClosedWithReason( |
| 2703 | net_log_source2, TransportClientSocketPool::kIdleTimeLimitExpired); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2704 | } |
| 2705 | |
[email protected] | 2041cf34 | 2010-02-19 03:15:59 | [diff] [blame] | 2706 | // Make sure that we process all pending requests even when we're stalling |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2707 | // because of multiple releasing disconnected sockets. |
| 2708 | TEST_F(ClientSocketPoolBaseTest, MultipleReleasingDisconnectedSockets) { |
| 2709 | CreatePoolWithIdleTimeouts( |
| 2710 | kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 2711 | base::TimeDelta(), // Time out unused sockets immediately. |
| 2712 | base::TimeDelta::FromDays(1)); // Don't time out used sockets. |
| 2713 | |
| 2714 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 2715 | |
| 2716 | // Startup 4 connect jobs. Two of them will be pending. |
| 2717 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2718 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2719 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2720 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2721 | TestGroupId("a"), params_, base::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2722 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2723 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2724 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2725 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2726 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2727 | TestCompletionCallback callback2; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2728 | rv = handle2.Init(TestGroupId("a"), params_, base::nullopt, LOWEST, |
| 2729 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2730 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2731 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2732 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2733 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2734 | ClientSocketHandle handle3; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2735 | TestCompletionCallback callback3; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2736 | rv = handle3.Init(TestGroupId("a"), params_, base::nullopt, LOWEST, |
| 2737 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2738 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2739 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2740 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2741 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2742 | ClientSocketHandle handle4; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2743 | TestCompletionCallback callback4; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2744 | rv = handle4.Init(TestGroupId("a"), params_, base::nullopt, LOWEST, |
| 2745 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2746 | callback4.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2747 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2748 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2749 | |
| 2750 | // Release two disconnected sockets. |
| 2751 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2752 | handle.socket()->Disconnect(); |
| 2753 | handle.Reset(); |
| 2754 | handle2.socket()->Disconnect(); |
| 2755 | handle2.Reset(); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2756 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2757 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2758 | EXPECT_FALSE(handle3.is_reused()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2759 | EXPECT_THAT(callback4.WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2760 | EXPECT_FALSE(handle4.is_reused()); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2761 | } |
| 2762 | |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2763 | // Regression test for http://crbug.com/42267. |
| 2764 | // When DoReleaseSocket() is processed for one socket, it is blocked because the |
| 2765 | // other stalled groups all have releasing sockets, so no progress can be made. |
| 2766 | TEST_F(ClientSocketPoolBaseTest, SocketLimitReleasingSockets) { |
| 2767 | CreatePoolWithIdleTimeouts( |
| 2768 | 4 /* socket limit */, 4 /* socket limit per group */, |
| 2769 | base::TimeDelta(), // Time out unused sockets immediately. |
| 2770 | base::TimeDelta::FromDays(1)); // Don't time out used sockets. |
| 2771 | |
| 2772 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 2773 | |
| 2774 | // Max out the socket limit with 2 per group. |
| 2775 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2776 | ClientSocketHandle handle_a[4]; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2777 | TestCompletionCallback callback_a[4]; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2778 | ClientSocketHandle handle_b[4]; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2779 | TestCompletionCallback callback_b[4]; |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2780 | |
| 2781 | for (int i = 0; i < 2; ++i) { |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2782 | EXPECT_EQ(OK, handle_a[i].Init(TestGroupId("a"), params_, base::nullopt, |
| 2783 | LOWEST, SocketTag(), |
| 2784 | ClientSocketPool::RespectLimits::ENABLED, |
| 2785 | callback_a[i].callback(), |
| 2786 | ClientSocketPool::ProxyAuthCallback(), |
| 2787 | pool_.get(), NetLogWithSource())); |
| 2788 | EXPECT_EQ(OK, handle_b[i].Init(TestGroupId("b"), params_, base::nullopt, |
| 2789 | LOWEST, SocketTag(), |
| 2790 | ClientSocketPool::RespectLimits::ENABLED, |
| 2791 | callback_b[i].callback(), |
| 2792 | ClientSocketPool::ProxyAuthCallback(), |
| 2793 | pool_.get(), NetLogWithSource())); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2794 | } |
[email protected] | b89f7e4 | 2010-05-20 20:37:00 | [diff] [blame] | 2795 | |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2796 | // Make 4 pending requests, 2 per group. |
| 2797 | |
| 2798 | for (int i = 2; i < 4; ++i) { |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2799 | EXPECT_EQ( |
| 2800 | ERR_IO_PENDING, |
| 2801 | handle_a[i].Init(TestGroupId("a"), params_, base::nullopt, LOWEST, |
| 2802 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2803 | callback_a[i].callback(), |
| 2804 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2805 | NetLogWithSource())); |
| 2806 | EXPECT_EQ( |
| 2807 | ERR_IO_PENDING, |
| 2808 | handle_b[i].Init(TestGroupId("b"), params_, base::nullopt, LOWEST, |
| 2809 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2810 | callback_b[i].callback(), |
| 2811 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2812 | NetLogWithSource())); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2813 | } |
| 2814 | |
| 2815 | // Release b's socket first. The order is important, because in |
| 2816 | // DoReleaseSocket(), we'll process b's released socket, and since both b and |
| 2817 | // a are stalled, but 'a' is lower lexicographically, we'll process group 'a' |
| 2818 | // first, which has a releasing socket, so it refuses to start up another |
| 2819 | // ConnectJob. So, we used to infinite loop on this. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2820 | handle_b[0].socket()->Disconnect(); |
| 2821 | handle_b[0].Reset(); |
| 2822 | handle_a[0].socket()->Disconnect(); |
| 2823 | handle_a[0].Reset(); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2824 | |
| 2825 | // Used to get stuck here. |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 2826 | base::RunLoop().RunUntilIdle(); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2827 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2828 | handle_b[1].socket()->Disconnect(); |
| 2829 | handle_b[1].Reset(); |
| 2830 | handle_a[1].socket()->Disconnect(); |
| 2831 | handle_a[1].Reset(); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2832 | |
| 2833 | for (int i = 2; i < 4; ++i) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2834 | EXPECT_THAT(callback_b[i].WaitForResult(), IsOk()); |
| 2835 | EXPECT_THAT(callback_a[i].WaitForResult(), IsOk()); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2836 | } |
| 2837 | } |
| 2838 | |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2839 | TEST_F(ClientSocketPoolBaseTest, |
| 2840 | ReleasingDisconnectedSocketsMaintainsPriorityOrder) { |
| 2841 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2842 | |
| 2843 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2844 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2845 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2846 | IsError(ERR_IO_PENDING)); |
| 2847 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2848 | IsError(ERR_IO_PENDING)); |
| 2849 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2850 | IsError(ERR_IO_PENDING)); |
| 2851 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2852 | IsError(ERR_IO_PENDING)); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2853 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2854 | EXPECT_THAT((*requests())[0]->WaitForResult(), IsOk()); |
| 2855 | EXPECT_THAT((*requests())[1]->WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2856 | EXPECT_EQ(2u, completion_count()); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2857 | |
| 2858 | // Releases one connection. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2859 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::NO_KEEP_ALIVE)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2860 | EXPECT_THAT((*requests())[2]->WaitForResult(), IsOk()); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2861 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2862 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::NO_KEEP_ALIVE)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2863 | EXPECT_THAT((*requests())[3]->WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2864 | EXPECT_EQ(4u, completion_count()); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2865 | |
| 2866 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 2867 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 2868 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 2869 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 2870 | |
| 2871 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2872 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(5)); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2873 | } |
| 2874 | |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2875 | class TestReleasingSocketRequest : public TestCompletionCallbackBase { |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2876 | public: |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2877 | TestReleasingSocketRequest(TransportClientSocketPool* pool, |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2878 | int expected_result, |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2879 | bool reset_releasing_handle) |
| 2880 | : pool_(pool), |
| 2881 | expected_result_(expected_result), |
Bence Béky | 8ddc249 | 2018-06-13 01:02:04 | [diff] [blame] | 2882 | reset_releasing_handle_(reset_releasing_handle) {} |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2883 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 2884 | ~TestReleasingSocketRequest() override = default; |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2885 | |
| 2886 | ClientSocketHandle* handle() { return &handle_; } |
| 2887 | |
Bence Béky | 8ddc249 | 2018-06-13 01:02:04 | [diff] [blame] | 2888 | CompletionOnceCallback callback() { |
| 2889 | return base::BindOnce(&TestReleasingSocketRequest::OnComplete, |
| 2890 | base::Unretained(this)); |
| 2891 | } |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2892 | |
| 2893 | private: |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2894 | void OnComplete(int result) { |
| 2895 | SetResult(result); |
| 2896 | if (reset_releasing_handle_) |
| 2897 | handle_.Reset(); |
| 2898 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2899 | EXPECT_EQ( |
| 2900 | expected_result_, |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 2901 | handle2_.Init( |
| 2902 | TestGroupId("a"), |
| 2903 | ClientSocketPool::SocketParams::CreateForHttpForTesting(), |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2904 | base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 2905 | ClientSocketPool::RespectLimits::ENABLED, CompletionOnceCallback(), |
| 2906 | ClientSocketPool::ProxyAuthCallback(), pool_, NetLogWithSource())); |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2907 | } |
| 2908 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2909 | TransportClientSocketPool* const pool_; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2910 | int expected_result_; |
| 2911 | bool reset_releasing_handle_; |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2912 | ClientSocketHandle handle_; |
| 2913 | ClientSocketHandle handle2_; |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2914 | }; |
| 2915 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2916 | |
| 2917 | TEST_F(ClientSocketPoolBaseTest, AdditionalErrorSocketsDontUseSlot) { |
| 2918 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2919 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2920 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
| 2921 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 2922 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2923 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2924 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2925 | client_socket_factory_.allocation_count()); |
| 2926 | |
| 2927 | connect_job_factory_->set_job_type( |
| 2928 | TestConnectJob::kMockPendingAdditionalErrorStateJob); |
| 2929 | TestReleasingSocketRequest req(pool_.get(), OK, false); |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2930 | EXPECT_EQ(ERR_IO_PENDING, |
| 2931 | req.handle()->Init( |
| 2932 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2933 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2934 | req.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2935 | pool_.get(), NetLogWithSource())); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2936 | // The next job should complete synchronously |
| 2937 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 2938 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2939 | EXPECT_THAT(req.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2940 | EXPECT_FALSE(req.handle()->is_initialized()); |
| 2941 | EXPECT_FALSE(req.handle()->socket()); |
| 2942 | EXPECT_TRUE(req.handle()->is_ssl_error()); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 2943 | EXPECT_TRUE(req.handle()->ssl_cert_request_info()); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2944 | } |
| 2945 | |
[email protected] | b6501d3d | 2010-06-03 23:53:34 | [diff] [blame] | 2946 | // http://crbug.com/44724 regression test. |
| 2947 | // We start releasing the pool when we flush on network change. When that |
| 2948 | // happens, the only active references are in the ClientSocketHandles. When a |
| 2949 | // ConnectJob completes and calls back into the last ClientSocketHandle, that |
| 2950 | // callback can release the last reference and delete the pool. After the |
| 2951 | // callback finishes, we go back to the stack frame within the now-deleted pool. |
| 2952 | // Executing any code that refers to members of the now-deleted pool can cause |
| 2953 | // crashes. |
| 2954 | TEST_F(ClientSocketPoolBaseTest, CallbackThatReleasesPool) { |
| 2955 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2956 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
| 2957 | |
| 2958 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2959 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2960 | EXPECT_EQ( |
| 2961 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2962 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2963 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2964 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2965 | pool_.get(), NetLogWithSource())); |
[email protected] | b6501d3d | 2010-06-03 23:53:34 | [diff] [blame] | 2966 | |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 2967 | pool_->FlushWithError(ERR_NETWORK_CHANGED, "Network changed"); |
[email protected] | b6501d3d | 2010-06-03 23:53:34 | [diff] [blame] | 2968 | |
| 2969 | // We'll call back into this now. |
| 2970 | callback.WaitForResult(); |
| 2971 | } |
| 2972 | |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2973 | TEST_F(ClientSocketPoolBaseTest, DoNotReuseSocketAfterFlush) { |
| 2974 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2975 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2976 | |
| 2977 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2978 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2979 | EXPECT_EQ( |
| 2980 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2981 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2982 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2983 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2984 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2985 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2986 | EXPECT_EQ(ClientSocketHandle::UNUSED, handle.reuse_type()); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 2987 | NetLogSource source = handle.socket()->NetLog().source(); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2988 | |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 2989 | pool_->FlushWithError(ERR_NETWORK_CHANGED, "Network changed"); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2990 | |
| 2991 | handle.Reset(); |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 2992 | base::RunLoop().RunUntilIdle(); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2993 | |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2994 | EXPECT_EQ( |
| 2995 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2996 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2997 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2998 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2999 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3000 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 3001 | EXPECT_EQ(ClientSocketHandle::UNUSED, handle.reuse_type()); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 3002 | |
| 3003 | ExpectSocketClosedWithReason( |
| 3004 | source, TransportClientSocketPool::kSocketGenerationOutOfDate); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 3005 | } |
| 3006 | |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3007 | class ConnectWithinCallback : public TestCompletionCallbackBase { |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 3008 | public: |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3009 | ConnectWithinCallback( |
| 3010 | const ClientSocketPool::GroupId& group_id, |
Matt Menke | 84d11e56 | 2019-03-27 00:11:19 | [diff] [blame] | 3011 | const scoped_refptr<ClientSocketPool::SocketParams>& params, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3012 | TransportClientSocketPool* pool) |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3013 | : group_id_(group_id), params_(params), pool_(pool) {} |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 3014 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 3015 | ~ConnectWithinCallback() override = default; |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 3016 | |
| 3017 | int WaitForNestedResult() { |
| 3018 | return nested_callback_.WaitForResult(); |
| 3019 | } |
| 3020 | |
Bence Béky | 8ddc249 | 2018-06-13 01:02:04 | [diff] [blame] | 3021 | CompletionOnceCallback callback() { |
| 3022 | return base::BindOnce(&ConnectWithinCallback::OnComplete, |
| 3023 | base::Unretained(this)); |
| 3024 | } |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3025 | |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 3026 | private: |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3027 | void OnComplete(int result) { |
| 3028 | SetResult(result); |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3029 | EXPECT_EQ( |
| 3030 | ERR_IO_PENDING, |
| 3031 | handle_.Init(group_id_, params_, base::nullopt, DEFAULT_PRIORITY, |
| 3032 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 3033 | nested_callback_.callback(), |
| 3034 | ClientSocketPool::ProxyAuthCallback(), pool_, |
| 3035 | NetLogWithSource())); |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3036 | } |
| 3037 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3038 | const ClientSocketPool::GroupId group_id_; |
Matt Menke | 84d11e56 | 2019-03-27 00:11:19 | [diff] [blame] | 3039 | const scoped_refptr<ClientSocketPool::SocketParams> params_; |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3040 | TransportClientSocketPool* const pool_; |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 3041 | ClientSocketHandle handle_; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3042 | TestCompletionCallback nested_callback_; |
| 3043 | |
| 3044 | DISALLOW_COPY_AND_ASSIGN(ConnectWithinCallback); |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 3045 | }; |
| 3046 | |
| 3047 | TEST_F(ClientSocketPoolBaseTest, AbortAllRequestsOnFlush) { |
| 3048 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3049 | |
| 3050 | // First job will be waiting until it gets aborted. |
| 3051 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3052 | |
| 3053 | ClientSocketHandle handle; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3054 | ConnectWithinCallback callback(TestGroupId("a"), params_, pool_.get()); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3055 | EXPECT_EQ( |
| 3056 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3057 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3058 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 3059 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3060 | pool_.get(), NetLogWithSource())); |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 3061 | |
| 3062 | // Second job will be started during the first callback, and will |
| 3063 | // asynchronously complete with OK. |
| 3064 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 3065 | pool_->FlushWithError(ERR_NETWORK_CHANGED, "Network changed"); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3066 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_NETWORK_CHANGED)); |
| 3067 | EXPECT_THAT(callback.WaitForNestedResult(), IsOk()); |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 3068 | } |
| 3069 | |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 3070 | TEST_F(ClientSocketPoolBaseTest, BackupSocketWaitsForHostResolution) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3071 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets, |
| 3072 | true /* enable_backup_connect_jobs */); |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 3073 | |
| 3074 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3075 | ClientSocketHandle handle; |
| 3076 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3077 | EXPECT_EQ( |
| 3078 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3079 | handle.Init(TestGroupId("bar"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3080 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 3081 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3082 | pool_.get(), NetLogWithSource())); |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 3083 | // The backup timer fires but doesn't start a new ConnectJob while resolving |
| 3084 | // the hostname. |
| 3085 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_RESOLVING_HOST); |
| 3086 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
| 3087 | ClientSocketPool::kMaxConnectRetryIntervalMs * 100)); |
| 3088 | EXPECT_EQ(1, client_socket_factory_.allocation_count()); |
| 3089 | |
| 3090 | // Once the ConnectJob has finished resolving the hostname, the backup timer |
| 3091 | // will create a ConnectJob when it fires. |
| 3092 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_CONNECTING); |
| 3093 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
| 3094 | ClientSocketPool::kMaxConnectRetryIntervalMs)); |
| 3095 | EXPECT_EQ(2, client_socket_factory_.allocation_count()); |
| 3096 | } |
| 3097 | |
| 3098 | // Test that no backup socket is created when a ConnectJob connects before it |
| 3099 | // completes. |
| 3100 | TEST_F(ClientSocketPoolBaseTest, NoBackupSocketWhenConnected) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3101 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets, |
| 3102 | true /* enable_backup_connect_jobs */); |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 3103 | |
| 3104 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3105 | ClientSocketHandle handle; |
| 3106 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3107 | EXPECT_EQ( |
| 3108 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3109 | handle.Init(TestGroupId("bar"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3110 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 3111 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3112 | pool_.get(), NetLogWithSource())); |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 3113 | // The backup timer fires but doesn't start a new ConnectJob while resolving |
| 3114 | // the hostname. |
| 3115 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_RESOLVING_HOST); |
| 3116 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
| 3117 | ClientSocketPool::kMaxConnectRetryIntervalMs * 100)); |
| 3118 | EXPECT_EQ(1, client_socket_factory_.allocation_count()); |
| 3119 | |
| 3120 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE); |
| 3121 | client_socket_factory_.SetJobHasEstablishedConnection(0); |
| 3122 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
| 3123 | ClientSocketPool::kMaxConnectRetryIntervalMs * 100)); |
| 3124 | EXPECT_EQ(1, client_socket_factory_.allocation_count()); |
| 3125 | } |
| 3126 | |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3127 | // Cancel a pending socket request while we're at max sockets, |
| 3128 | // and verify that the backup socket firing doesn't cause a crash. |
| 3129 | TEST_F(ClientSocketPoolBaseTest, BackupSocketCancelAtMaxSockets) { |
| 3130 | // Max 4 sockets globally, max 4 sockets per group. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3131 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets, |
| 3132 | true /* enable_backup_connect_jobs */); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3133 | |
[email protected] | 4baaf9d | 2010-08-31 15:15:44 | [diff] [blame] | 3134 | // Create the first socket and set to ERR_IO_PENDING. This starts the backup |
| 3135 | // timer. |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3136 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3137 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3138 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3139 | EXPECT_EQ( |
| 3140 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3141 | handle.Init(TestGroupId("bar"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3142 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 3143 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3144 | pool_.get(), NetLogWithSource())); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3145 | |
| 3146 | // Start (MaxSockets - 1) connected sockets to reach max sockets. |
| 3147 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 3148 | ClientSocketHandle handles[kDefaultMaxSockets]; |
| 3149 | for (int i = 1; i < kDefaultMaxSockets; ++i) { |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3150 | TestCompletionCallback callback; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3151 | EXPECT_EQ(OK, handles[i].Init(TestGroupId("bar"), params_, base::nullopt, |
| 3152 | DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3153 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3154 | callback.callback(), |
| 3155 | ClientSocketPool::ProxyAuthCallback(), |
| 3156 | pool_.get(), NetLogWithSource())); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3157 | } |
| 3158 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3159 | base::RunLoop().RunUntilIdle(); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3160 | |
| 3161 | // Cancel the pending request. |
| 3162 | handle.Reset(); |
| 3163 | |
| 3164 | // 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] | 3165 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
[email protected] | 26b997396 | 2012-01-28 00:57:00 | [diff] [blame] | 3166 | ClientSocketPool::kMaxConnectRetryIntervalMs / 2 * 3)); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3167 | |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3168 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 3169 | } |
| 3170 | |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3171 | TEST_F(ClientSocketPoolBaseTest, CancelBackupSocketAfterCancelingAllRequests) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3172 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets, |
| 3173 | true /* enable_backup_connect_jobs */); |
[email protected] | 4baaf9d | 2010-08-31 15:15:44 | [diff] [blame] | 3174 | |
| 3175 | // Create the first socket and set to ERR_IO_PENDING. This starts the backup |
| 3176 | // timer. |
| 3177 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3178 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3179 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3180 | EXPECT_EQ( |
| 3181 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3182 | handle.Init(TestGroupId("bar"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3183 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 3184 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3185 | pool_.get(), NetLogWithSource())); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3186 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("bar"))); |
| 3187 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("bar"))); |
| 3188 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3189 | TestGroupId("bar"))); |
| 3190 | EXPECT_EQ( |
| 3191 | 0u, pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("bar"))); |
[email protected] | 4baaf9d | 2010-08-31 15:15:44 | [diff] [blame] | 3192 | |
| 3193 | // Cancel the socket request. This should cancel the backup timer. Wait for |
| 3194 | // the backup time to see if it indeed got canceled. |
| 3195 | handle.Reset(); |
| 3196 | // 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] | 3197 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
[email protected] | 26b997396 | 2012-01-28 00:57:00 | [diff] [blame] | 3198 | ClientSocketPool::kMaxConnectRetryIntervalMs / 2 * 3)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3199 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("bar"))); |
| 3200 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("bar"))); |
[email protected] | 4baaf9d | 2010-08-31 15:15:44 | [diff] [blame] | 3201 | } |
| 3202 | |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3203 | TEST_F(ClientSocketPoolBaseTest, CancelBackupSocketAfterFinishingAllRequests) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3204 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets, |
| 3205 | true /* enable_backup_connect_jobs */); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3206 | |
| 3207 | // Create the first socket and set to ERR_IO_PENDING. This starts the backup |
| 3208 | // timer. |
| 3209 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3210 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3211 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3212 | EXPECT_EQ( |
| 3213 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3214 | handle.Init(TestGroupId("bar"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3215 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 3216 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3217 | pool_.get(), NetLogWithSource())); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3218 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3219 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3220 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3221 | EXPECT_EQ( |
| 3222 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3223 | handle2.Init(TestGroupId("bar"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3224 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3225 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3226 | pool_.get(), NetLogWithSource())); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3227 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("bar"))); |
| 3228 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("bar"))); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3229 | |
| 3230 | // Cancel request 1 and then complete request 2. With the requests finished, |
| 3231 | // the backup timer should be cancelled. |
| 3232 | handle.Reset(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3233 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3234 | // 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] | 3235 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
[email protected] | 26b997396 | 2012-01-28 00:57:00 | [diff] [blame] | 3236 | ClientSocketPool::kMaxConnectRetryIntervalMs / 2 * 3)); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3237 | } |
| 3238 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3239 | // Test delayed socket binding for the case where we have two connects, |
| 3240 | // and while one is waiting on a connect, the other frees up. |
| 3241 | // The socket waiting on a connect should switch immediately to the freed |
| 3242 | // up socket. |
| 3243 | TEST_F(ClientSocketPoolBaseTest, DelayedSocketBindingWaitingForConnect) { |
| 3244 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3245 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3246 | |
| 3247 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3248 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3249 | EXPECT_EQ( |
| 3250 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3251 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3252 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3253 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3254 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3255 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3256 | |
| 3257 | // No idle sockets, no pending jobs. |
| 3258 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3259 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3260 | |
| 3261 | // Create a second socket to the same host, but this one will wait. |
| 3262 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3263 | ClientSocketHandle handle2; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3264 | EXPECT_EQ( |
| 3265 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3266 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3267 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3268 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3269 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3270 | // No idle sockets, and one connecting job. |
| 3271 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3272 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3273 | |
| 3274 | // Return the first handle to the pool. This will initiate the delayed |
| 3275 | // binding. |
| 3276 | handle1.Reset(); |
| 3277 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3278 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3279 | |
| 3280 | // Still no idle sockets, still one pending connect job. |
| 3281 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3282 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3283 | |
| 3284 | // The second socket connected, even though it was a Waiting Job. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3285 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3286 | |
| 3287 | // And we can see there is still one job waiting. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3288 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3289 | |
| 3290 | // Finally, signal the waiting Connect. |
| 3291 | client_socket_factory_.SignalJobs(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3292 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3293 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3294 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3295 | } |
| 3296 | |
| 3297 | // Test delayed socket binding when a group is at capacity and one |
| 3298 | // of the group's sockets frees up. |
| 3299 | TEST_F(ClientSocketPoolBaseTest, DelayedSocketBindingAtGroupCapacity) { |
| 3300 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3301 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3302 | |
| 3303 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3304 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3305 | EXPECT_EQ( |
| 3306 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3307 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3308 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3309 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3310 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3311 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3312 | |
| 3313 | // No idle sockets, no pending jobs. |
| 3314 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3315 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3316 | |
| 3317 | // Create a second socket to the same host, but this one will wait. |
| 3318 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3319 | ClientSocketHandle handle2; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3320 | EXPECT_EQ( |
| 3321 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3322 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3323 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3324 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3325 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3326 | // No idle sockets, and one connecting job. |
| 3327 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3328 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3329 | |
| 3330 | // Return the first handle to the pool. This will initiate the delayed |
| 3331 | // binding. |
| 3332 | handle1.Reset(); |
| 3333 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3334 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3335 | |
| 3336 | // Still no idle sockets, still one pending connect job. |
| 3337 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3338 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3339 | |
| 3340 | // The second socket connected, even though it was a Waiting Job. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3341 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3342 | |
| 3343 | // And we can see there is still one job waiting. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3344 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3345 | |
| 3346 | // Finally, signal the waiting Connect. |
| 3347 | client_socket_factory_.SignalJobs(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3348 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3349 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3350 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3351 | } |
| 3352 | |
| 3353 | // Test out the case where we have one socket connected, one |
| 3354 | // connecting, when the first socket finishes and goes idle. |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3355 | // Although the second connection is pending, the second request |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3356 | // should complete, by taking the first socket's idle socket. |
| 3357 | TEST_F(ClientSocketPoolBaseTest, DelayedSocketBindingAtStall) { |
| 3358 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3359 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3360 | |
| 3361 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3362 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3363 | EXPECT_EQ( |
| 3364 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3365 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3366 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3367 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3368 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3369 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3370 | |
| 3371 | // No idle sockets, no pending jobs. |
| 3372 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3373 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3374 | |
| 3375 | // Create a second socket to the same host, but this one will wait. |
| 3376 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3377 | ClientSocketHandle handle2; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3378 | EXPECT_EQ( |
| 3379 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3380 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3381 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3382 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3383 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3384 | // No idle sockets, and one connecting job. |
| 3385 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3386 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3387 | |
| 3388 | // Return the first handle to the pool. This will initiate the delayed |
| 3389 | // binding. |
| 3390 | handle1.Reset(); |
| 3391 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3392 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3393 | |
| 3394 | // Still no idle sockets, still one pending connect job. |
| 3395 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3396 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3397 | |
| 3398 | // The second socket connected, even though it was a Waiting Job. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3399 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3400 | |
| 3401 | // And we can see there is still one job waiting. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3402 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3403 | |
| 3404 | // Finally, signal the waiting Connect. |
| 3405 | client_socket_factory_.SignalJobs(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3406 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3407 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3408 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3409 | } |
| 3410 | |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3411 | // Cover the case where on an available socket slot, we have one pending |
| 3412 | // request that completes synchronously, thereby making the Group empty. |
| 3413 | TEST_F(ClientSocketPoolBaseTest, SynchronouslyProcessOnePendingRequest) { |
| 3414 | const int kUnlimitedSockets = 100; |
| 3415 | const int kOneSocketPerGroup = 1; |
| 3416 | CreatePool(kUnlimitedSockets, kOneSocketPerGroup); |
| 3417 | |
| 3418 | // Make the first request asynchronous fail. |
| 3419 | // This will free up a socket slot later. |
| 3420 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
| 3421 | |
| 3422 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3423 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3424 | EXPECT_EQ( |
| 3425 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3426 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3427 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3428 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3429 | pool_.get(), NetLogWithSource())); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3430 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3431 | |
| 3432 | // Make the second request synchronously fail. This should make the Group |
| 3433 | // empty. |
| 3434 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
| 3435 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3436 | TestCompletionCallback callback2; |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3437 | // It'll be ERR_IO_PENDING now, but the TestConnectJob will synchronously fail |
| 3438 | // when created. |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3439 | EXPECT_EQ( |
| 3440 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3441 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3442 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3443 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3444 | pool_.get(), NetLogWithSource())); |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3445 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3446 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3447 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3448 | EXPECT_THAT(callback1.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 3449 | EXPECT_THAT(callback2.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3450 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3451 | } |
| 3452 | |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3453 | TEST_F(ClientSocketPoolBaseTest, PreferUsedSocketToUnusedSocket) { |
| 3454 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 3455 | |
| 3456 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3457 | |
| 3458 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3459 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3460 | EXPECT_EQ( |
| 3461 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3462 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3463 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3464 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3465 | pool_.get(), NetLogWithSource())); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3466 | |
| 3467 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3468 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3469 | EXPECT_EQ( |
| 3470 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3471 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3472 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3473 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3474 | pool_.get(), NetLogWithSource())); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3475 | ClientSocketHandle handle3; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3476 | TestCompletionCallback callback3; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3477 | EXPECT_EQ( |
| 3478 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3479 | handle3.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3480 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3481 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3482 | pool_.get(), NetLogWithSource())); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3483 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3484 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3485 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
| 3486 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3487 | |
| 3488 | // Use the socket. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 3489 | EXPECT_EQ(1, handle1.socket()->Write(nullptr, 1, CompletionOnceCallback(), |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 3490 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 3491 | EXPECT_EQ(1, handle3.socket()->Write(nullptr, 1, CompletionOnceCallback(), |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 3492 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3493 | |
| 3494 | handle1.Reset(); |
| 3495 | handle2.Reset(); |
| 3496 | handle3.Reset(); |
| 3497 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3498 | EXPECT_EQ(OK, handle1.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3499 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3500 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3501 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3502 | pool_.get(), NetLogWithSource())); |
| 3503 | EXPECT_EQ(OK, handle2.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3504 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3505 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3506 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3507 | pool_.get(), NetLogWithSource())); |
| 3508 | EXPECT_EQ(OK, handle3.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3509 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3510 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3511 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3512 | pool_.get(), NetLogWithSource())); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3513 | |
| 3514 | EXPECT_TRUE(handle1.socket()->WasEverUsed()); |
| 3515 | EXPECT_TRUE(handle2.socket()->WasEverUsed()); |
| 3516 | EXPECT_FALSE(handle3.socket()->WasEverUsed()); |
| 3517 | } |
| 3518 | |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3519 | TEST_F(ClientSocketPoolBaseTest, RequestSockets) { |
| 3520 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3521 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3522 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3523 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 3524 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3525 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3526 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3527 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3528 | EXPECT_EQ(2u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3529 | TestGroupId("a"))); |
| 3530 | EXPECT_EQ(2u, |
| 3531 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3532 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3533 | |
| 3534 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3535 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3536 | EXPECT_EQ( |
| 3537 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3538 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3539 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3540 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3541 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3542 | |
| 3543 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3544 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3545 | EXPECT_EQ( |
| 3546 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3547 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3548 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3549 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3550 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3551 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3552 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3553 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3554 | TestGroupId("a"))); |
| 3555 | EXPECT_EQ(0u, |
| 3556 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3557 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3558 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3559 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3560 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3561 | handle1.Reset(); |
| 3562 | handle2.Reset(); |
| 3563 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3564 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3565 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3566 | TestGroupId("a"))); |
| 3567 | EXPECT_EQ(0u, |
| 3568 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3569 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3570 | } |
| 3571 | |
| 3572 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsWhenAlreadyHaveAConnectJob) { |
| 3573 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3574 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3575 | |
| 3576 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3577 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3578 | EXPECT_EQ( |
| 3579 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3580 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3581 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3582 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3583 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3584 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3585 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3586 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3587 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3588 | TestGroupId("a"))); |
| 3589 | EXPECT_EQ(0u, |
| 3590 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3591 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3592 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3593 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 3594 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3595 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3596 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3597 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3598 | TestGroupId("a"))); |
| 3599 | EXPECT_EQ(1u, |
| 3600 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3601 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3602 | |
| 3603 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3604 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3605 | EXPECT_EQ( |
| 3606 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3607 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3608 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3609 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3610 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3611 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3612 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3613 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3614 | TestGroupId("a"))); |
| 3615 | EXPECT_EQ(0u, |
| 3616 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3617 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3618 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3619 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3620 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3621 | handle1.Reset(); |
| 3622 | handle2.Reset(); |
| 3623 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3624 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3625 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3626 | TestGroupId("a"))); |
| 3627 | EXPECT_EQ(0u, |
| 3628 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3629 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3630 | } |
| 3631 | |
| 3632 | TEST_F(ClientSocketPoolBaseTest, |
| 3633 | RequestSocketsWhenAlreadyHaveMultipleConnectJob) { |
| 3634 | CreatePool(4, 4); |
| 3635 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3636 | |
| 3637 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3638 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3639 | EXPECT_EQ( |
| 3640 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3641 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3642 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3643 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3644 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3645 | |
| 3646 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3647 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3648 | EXPECT_EQ( |
| 3649 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3650 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3651 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3652 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3653 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3654 | |
| 3655 | ClientSocketHandle handle3; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3656 | TestCompletionCallback callback3; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3657 | EXPECT_EQ( |
| 3658 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3659 | handle3.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3660 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3661 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3662 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3663 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3664 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3665 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3666 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3667 | TestGroupId("a"))); |
| 3668 | EXPECT_EQ(0u, |
| 3669 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3670 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3671 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3672 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 3673 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3674 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3675 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3676 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3677 | TestGroupId("a"))); |
| 3678 | EXPECT_EQ(0u, |
| 3679 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3680 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3681 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3682 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3683 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
| 3684 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3685 | handle1.Reset(); |
| 3686 | handle2.Reset(); |
| 3687 | handle3.Reset(); |
| 3688 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3689 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3690 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3691 | TestGroupId("a"))); |
| 3692 | EXPECT_EQ(0u, |
| 3693 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3694 | EXPECT_EQ(3u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3695 | } |
| 3696 | |
| 3697 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsAtMaxSocketLimit) { |
| 3698 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 3699 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3700 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3701 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3702 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3703 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, |
| 3704 | kDefaultMaxSockets, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3705 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3706 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3707 | EXPECT_EQ(kDefaultMaxSockets, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3708 | static_cast<int>( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3709 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
| 3710 | EXPECT_EQ( |
| 3711 | kDefaultMaxSockets, |
| 3712 | static_cast<int>(pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3713 | TestGroupId("a")))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3714 | EXPECT_EQ(kDefaultMaxSockets, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3715 | static_cast<int>(pool_->NumUnassignedConnectJobsInGroupForTesting( |
| 3716 | TestGroupId("a")))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3717 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3718 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("b"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3719 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3720 | pool_->RequestSockets(TestGroupId("b"), params_, base::nullopt, |
| 3721 | kDefaultMaxSockets, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3722 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3723 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("b"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3724 | } |
| 3725 | |
| 3726 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsHitMaxSocketLimit) { |
| 3727 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 3728 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3729 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3730 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3731 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3732 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, |
| 3733 | kDefaultMaxSockets - 1, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3734 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3735 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3736 | EXPECT_EQ(kDefaultMaxSockets - 1, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3737 | static_cast<int>( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3738 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
| 3739 | EXPECT_EQ( |
| 3740 | kDefaultMaxSockets - 1, |
| 3741 | static_cast<int>(pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3742 | TestGroupId("a")))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3743 | EXPECT_EQ(kDefaultMaxSockets - 1, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3744 | static_cast<int>(pool_->NumUnassignedConnectJobsInGroupForTesting( |
| 3745 | TestGroupId("a")))); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 3746 | EXPECT_FALSE(pool_->IsStalled()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3747 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3748 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("b"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3749 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3750 | pool_->RequestSockets(TestGroupId("b"), params_, base::nullopt, |
| 3751 | kDefaultMaxSockets, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3752 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3753 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("b"))); |
| 3754 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 3755 | EXPECT_FALSE(pool_->IsStalled()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3756 | } |
| 3757 | |
| 3758 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsCountIdleSockets) { |
| 3759 | CreatePool(4, 4); |
| 3760 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3761 | |
| 3762 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3763 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3764 | EXPECT_EQ( |
| 3765 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3766 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3767 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3768 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3769 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3770 | ASSERT_THAT(callback1.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3771 | handle1.Reset(); |
| 3772 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3773 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3774 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3775 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3776 | TestGroupId("a"))); |
| 3777 | EXPECT_EQ(0u, |
| 3778 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3779 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3780 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3781 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 3782 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3783 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3784 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3785 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3786 | TestGroupId("a"))); |
| 3787 | EXPECT_EQ(1u, |
| 3788 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3789 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3790 | } |
| 3791 | |
| 3792 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsCountActiveSockets) { |
| 3793 | CreatePool(4, 4); |
| 3794 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3795 | |
| 3796 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3797 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3798 | EXPECT_EQ( |
| 3799 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3800 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3801 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3802 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3803 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3804 | ASSERT_THAT(callback1.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3805 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3806 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3807 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3808 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3809 | TestGroupId("a"))); |
| 3810 | EXPECT_EQ(0u, |
| 3811 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3812 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3813 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3814 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3815 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 3816 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3817 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3818 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3819 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3820 | TestGroupId("a"))); |
| 3821 | EXPECT_EQ(1u, |
| 3822 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3823 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3824 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3825 | } |
| 3826 | |
| 3827 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsSynchronous) { |
| 3828 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3829 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 3830 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3831 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, |
| 3832 | kDefaultMaxSocketsPerGroup, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3833 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3834 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3835 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3836 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3837 | TestGroupId("a"))); |
| 3838 | EXPECT_EQ(0u, |
| 3839 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3840 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3841 | static_cast<int>(pool_->IdleSocketCountInGroup(TestGroupId("a")))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3842 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3843 | pool_->RequestSockets(TestGroupId("b"), params_, base::nullopt, |
| 3844 | kDefaultMaxSocketsPerGroup, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3845 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3846 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 3847 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3848 | TestGroupId("b"))); |
| 3849 | EXPECT_EQ(0u, |
| 3850 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3851 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3852 | static_cast<int>(pool_->IdleSocketCountInGroup(TestGroupId("b")))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3853 | } |
| 3854 | |
[email protected] | 3c819f52 | 2010-12-02 02:03:12 | [diff] [blame] | 3855 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsSynchronousError) { |
| 3856 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3857 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
| 3858 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3859 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, |
| 3860 | kDefaultMaxSocketsPerGroup, NetLogWithSource()); |
[email protected] | 3c819f52 | 2010-12-02 02:03:12 | [diff] [blame] | 3861 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3862 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | fd2e53e | 2011-01-14 20:40:52 | [diff] [blame] | 3863 | |
| 3864 | connect_job_factory_->set_job_type( |
| 3865 | TestConnectJob::kMockAdditionalErrorStateJob); |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3866 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, |
| 3867 | kDefaultMaxSocketsPerGroup, NetLogWithSource()); |
[email protected] | fd2e53e | 2011-01-14 20:40:52 | [diff] [blame] | 3868 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3869 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | 3c819f52 | 2010-12-02 02:03:12 | [diff] [blame] | 3870 | } |
| 3871 | |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3872 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsMultipleTimesDoesNothing) { |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3873 | CreatePool(4, 4); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3874 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3875 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3876 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 3877 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3878 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3879 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3880 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3881 | EXPECT_EQ(2u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3882 | TestGroupId("a"))); |
| 3883 | EXPECT_EQ(2u, |
| 3884 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3885 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3886 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3887 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3888 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 3889 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3890 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3891 | EXPECT_EQ(2u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3892 | TestGroupId("a"))); |
| 3893 | EXPECT_EQ(2u, |
| 3894 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3895 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3896 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3897 | |
| 3898 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3899 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3900 | EXPECT_EQ( |
| 3901 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3902 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3903 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3904 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3905 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3906 | |
| 3907 | client_socket_factory_.SignalJob(0); |
| 3908 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3909 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3910 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3911 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3912 | TestGroupId("a"))); |
| 3913 | EXPECT_EQ(1u, |
| 3914 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3915 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3916 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3917 | |
| 3918 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3919 | TestCompletionCallback callback2; |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3920 | EXPECT_EQ( |
| 3921 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3922 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3923 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3924 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3925 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3926 | client_socket_factory_.SignalJob(0); |
| 3927 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3928 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3929 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3930 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3931 | TestGroupId("a"))); |
| 3932 | EXPECT_EQ(0u, |
| 3933 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3934 | EXPECT_EQ(2, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3935 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3936 | |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3937 | handle1.Reset(); |
| 3938 | handle2.Reset(); |
| 3939 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3940 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3941 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3942 | TestGroupId("a"))); |
| 3943 | EXPECT_EQ(0u, |
| 3944 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3945 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3946 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3947 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3948 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 3949 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3950 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3951 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3952 | TestGroupId("a"))); |
| 3953 | EXPECT_EQ(0u, |
| 3954 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3955 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3956 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3957 | } |
| 3958 | |
| 3959 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsDifferentNumSockets) { |
| 3960 | CreatePool(4, 4); |
| 3961 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3962 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3963 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 1, |
| 3964 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3965 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3966 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3967 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3968 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3969 | TestGroupId("a"))); |
| 3970 | EXPECT_EQ(1u, |
| 3971 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3972 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3973 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3974 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 3975 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3976 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3977 | EXPECT_EQ(2u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3978 | TestGroupId("a"))); |
| 3979 | EXPECT_EQ(2u, |
| 3980 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3981 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3982 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3983 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 3, |
| 3984 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3985 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3986 | EXPECT_EQ(3u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3987 | TestGroupId("a"))); |
| 3988 | EXPECT_EQ(3u, |
| 3989 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3990 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3991 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3992 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 1, |
| 3993 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3994 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3995 | EXPECT_EQ(3u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3996 | TestGroupId("a"))); |
| 3997 | EXPECT_EQ(3u, |
| 3998 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3999 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4000 | } |
| 4001 | |
| 4002 | TEST_F(ClientSocketPoolBaseTest, PreconnectJobsTakenByNormalRequests) { |
| 4003 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4004 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4005 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4006 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 1, |
| 4007 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4008 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4009 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 4010 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4011 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4012 | TestGroupId("a"))); |
| 4013 | EXPECT_EQ(1u, |
| 4014 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4015 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4016 | |
| 4017 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 4018 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4019 | EXPECT_EQ( |
| 4020 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4021 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4022 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4023 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4024 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4025 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4026 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4027 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4028 | TestGroupId("a"))); |
| 4029 | EXPECT_EQ(0u, |
| 4030 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4031 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4032 | |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4033 | client_socket_factory_.SignalJobs(); |
| 4034 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 4035 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4036 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4037 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4038 | TestGroupId("a"))); |
| 4039 | EXPECT_EQ(0u, |
| 4040 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4041 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4042 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4043 | |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4044 | // Make sure if a preconnected socket is not fully connected when a request |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 4045 | // starts, it has a connect start time. |
| 4046 | TestLoadTimingInfoConnectedNotReused(handle1); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4047 | handle1.Reset(); |
| 4048 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4049 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4050 | } |
| 4051 | |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 4052 | // Checks that fully connected preconnect jobs have no connect times, and are |
| 4053 | // marked as reused. |
| 4054 | TEST_F(ClientSocketPoolBaseTest, ConnectedPreconnectJobsHaveNoConnectTimes) { |
| 4055 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4056 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4057 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 1, |
| 4058 | NetLogWithSource()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 4059 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4060 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 4061 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4062 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4063 | TestGroupId("a"))); |
| 4064 | EXPECT_EQ(0u, |
| 4065 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4066 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 4067 | |
| 4068 | ClientSocketHandle handle; |
| 4069 | TestCompletionCallback callback; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4070 | EXPECT_EQ(OK, handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4071 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4072 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4073 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4074 | pool_.get(), NetLogWithSource())); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 4075 | |
| 4076 | // Make sure the idle socket was used. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4077 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 4078 | |
| 4079 | TestLoadTimingInfoConnectedReused(handle); |
| 4080 | handle.Reset(); |
| 4081 | TestLoadTimingInfoNotConnected(handle); |
| 4082 | } |
| 4083 | |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4084 | // http://crbug.com/64940 regression test. |
| 4085 | TEST_F(ClientSocketPoolBaseTest, PreconnectClosesIdleSocketRemovesGroup) { |
| 4086 | const int kMaxTotalSockets = 3; |
| 4087 | const int kMaxSocketsPerGroup = 2; |
| 4088 | CreatePool(kMaxTotalSockets, kMaxSocketsPerGroup); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4089 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4090 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 4091 | // Note that group id ordering matters here. "a" comes before "b", so |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4092 | // CloseOneIdleSocket() will try to close "a"'s idle socket. |
| 4093 | |
| 4094 | // Set up one idle socket in "a". |
| 4095 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 4096 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4097 | EXPECT_EQ( |
| 4098 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4099 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4100 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4101 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4102 | pool_.get(), NetLogWithSource())); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4103 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 4104 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4105 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4106 | TestGroupId("a"))); |
| 4107 | EXPECT_EQ(0u, |
| 4108 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4109 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4110 | |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4111 | client_socket_factory_.SignalJobs(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4112 | ASSERT_THAT(callback1.WaitForResult(), IsOk()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4113 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4114 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4115 | TestGroupId("a"))); |
| 4116 | EXPECT_EQ(0u, |
| 4117 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4118 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4119 | |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4120 | handle1.Reset(); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4121 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4122 | |
| 4123 | // Set up two active sockets in "b". |
| 4124 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 4125 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4126 | EXPECT_EQ( |
| 4127 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4128 | handle1.Init(TestGroupId("b"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4129 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4130 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4131 | pool_.get(), NetLogWithSource())); |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4132 | EXPECT_EQ( |
| 4133 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4134 | handle2.Init(TestGroupId("b"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4135 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4136 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4137 | pool_.get(), NetLogWithSource())); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4138 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4139 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("b"))); |
| 4140 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 4141 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4142 | TestGroupId("b"))); |
| 4143 | EXPECT_EQ(0u, |
| 4144 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4145 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4146 | |
| 4147 | client_socket_factory_.SignalJobs(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4148 | ASSERT_THAT(callback1.WaitForResult(), IsOk()); |
| 4149 | ASSERT_THAT(callback2.WaitForResult(), IsOk()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4150 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4151 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4152 | TestGroupId("b"))); |
| 4153 | EXPECT_EQ(0u, |
| 4154 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 4155 | EXPECT_EQ(2, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4156 | |
| 4157 | // Now we have 1 idle socket in "a" and 2 active sockets in "b". This means |
| 4158 | // we've maxed out on sockets, since we set |kMaxTotalSockets| to 3. |
| 4159 | // Requesting 2 preconnected sockets for "a" should fail to allocate any more |
| 4160 | // sockets for "a", and "b" should still have 2 active sockets. |
| 4161 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4162 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 4163 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4164 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4165 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4166 | TestGroupId("a"))); |
| 4167 | EXPECT_EQ(0u, |
| 4168 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4169 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4170 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 4171 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 4172 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4173 | TestGroupId("b"))); |
| 4174 | EXPECT_EQ(0u, |
| 4175 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4176 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4177 | EXPECT_EQ(2, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4178 | |
| 4179 | // Now release the 2 active sockets for "b". This will give us 1 idle socket |
| 4180 | // in "a" and 2 idle sockets in "b". Requesting 2 preconnected sockets for |
| 4181 | // "a" should result in closing 1 for "b". |
| 4182 | handle1.Reset(); |
| 4183 | handle2.Reset(); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4184 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4185 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4186 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4187 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 4188 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4189 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4190 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4191 | TestGroupId("a"))); |
| 4192 | EXPECT_EQ(1u, |
| 4193 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4194 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4195 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 4196 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 4197 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4198 | TestGroupId("b"))); |
| 4199 | EXPECT_EQ(0u, |
| 4200 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4201 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4202 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4203 | } |
| 4204 | |
[email protected] | b7b8be4 | 2011-07-12 12:46:41 | [diff] [blame] | 4205 | TEST_F(ClientSocketPoolBaseTest, PreconnectWithoutBackupJob) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4206 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 4207 | true /* enable_backup_connect_jobs */); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4208 | |
| 4209 | // Make the ConnectJob hang until it times out, shorten the timeout. |
| 4210 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4211 | connect_job_factory_->set_timeout_duration( |
| 4212 | base::TimeDelta::FromMilliseconds(500)); |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4213 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 1, |
| 4214 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4215 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4216 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4217 | TestGroupId("a"))); |
| 4218 | EXPECT_EQ(1u, |
| 4219 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4220 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4221 | |
[email protected] | b7b8be4 | 2011-07-12 12:46:41 | [diff] [blame] | 4222 | // Verify the backup timer doesn't create a backup job, by making |
| 4223 | // the backup job a pending job instead of a waiting job, so it |
| 4224 | // *would* complete if it were created. |
Lukasz Krakowiak | 28dcf9d6 | 2020-06-04 09:46:59 | [diff] [blame] | 4225 | base::RunLoop loop; |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4226 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 4227 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
Lukasz Krakowiak | 85529208 | 2020-06-19 12:43:58 | [diff] [blame] | 4228 | FROM_HERE, loop.QuitClosure(), base::TimeDelta::FromSeconds(1)); |
Lukasz Krakowiak | 28dcf9d6 | 2020-06-04 09:46:59 | [diff] [blame] | 4229 | loop.Run(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4230 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4231 | } |
| 4232 | |
[email protected] | b7b8be4 | 2011-07-12 12:46:41 | [diff] [blame] | 4233 | TEST_F(ClientSocketPoolBaseTest, PreconnectWithBackupJob) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4234 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 4235 | true /* enable_backup_connect_jobs */); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4236 | |
| 4237 | // Make the ConnectJob hang forever. |
| 4238 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4239 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 1, |
| 4240 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4241 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4242 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4243 | TestGroupId("a"))); |
| 4244 | EXPECT_EQ(1u, |
| 4245 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4246 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 4247 | base::RunLoop().RunUntilIdle(); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4248 | |
| 4249 | // Make the backup job be a pending job, so it completes normally. |
| 4250 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 4251 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 4252 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4253 | EXPECT_EQ( |
| 4254 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4255 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4256 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4257 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4258 | pool_.get(), NetLogWithSource())); |
[email protected] | b7b8be4 | 2011-07-12 12:46:41 | [diff] [blame] | 4259 | // Timer has started, but the backup connect job shouldn't be created yet. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4260 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4261 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4262 | TestGroupId("a"))); |
| 4263 | EXPECT_EQ(0u, |
| 4264 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4265 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4266 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4267 | ASSERT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4268 | |
| 4269 | // The hung connect job should still be there, but everything else should be |
| 4270 | // complete. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4271 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4272 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4273 | TestGroupId("a"))); |
| 4274 | EXPECT_EQ(1u, |
| 4275 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4276 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4277 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4278 | } |
| 4279 | |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4280 | // Tests that a preconnect that starts out with unread data can still be used. |
| 4281 | // http://crbug.com/334467 |
| 4282 | TEST_F(ClientSocketPoolBaseTest, PreconnectWithUnreadData) { |
| 4283 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4284 | connect_job_factory_->set_job_type(TestConnectJob::kMockUnreadDataJob); |
| 4285 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4286 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 1, |
| 4287 | NetLogWithSource()); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4288 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4289 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 4290 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4291 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4292 | TestGroupId("a"))); |
| 4293 | EXPECT_EQ(0u, |
| 4294 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4295 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4296 | |
| 4297 | // Fail future jobs to be sure that handle receives the preconnected socket |
| 4298 | // rather than closing it and making a new one. |
| 4299 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
| 4300 | ClientSocketHandle handle; |
| 4301 | TestCompletionCallback callback; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4302 | EXPECT_EQ(OK, handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4303 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4304 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4305 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4306 | pool_.get(), NetLogWithSource())); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4307 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4308 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 4309 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4310 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4311 | TestGroupId("a"))); |
| 4312 | EXPECT_EQ(0u, |
| 4313 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4314 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4315 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4316 | |
| 4317 | // Drain the pending read. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 4318 | EXPECT_EQ(1, handle.socket()->Read(nullptr, 1, CompletionOnceCallback())); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4319 | |
| 4320 | TestLoadTimingInfoConnectedReused(handle); |
| 4321 | handle.Reset(); |
| 4322 | |
| 4323 | // The socket should be usable now that it's idle again. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4324 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4325 | } |
| 4326 | |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4327 | TEST_F(ClientSocketPoolBaseTest, RequestGetsAssignedJob) { |
| 4328 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4329 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4330 | |
| 4331 | ClientSocketHandle handle1; |
| 4332 | TestCompletionCallback callback1; |
| 4333 | EXPECT_EQ( |
| 4334 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4335 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4336 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4337 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4338 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4339 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4340 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4341 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4342 | TestGroupId("a"))); |
| 4343 | EXPECT_EQ(0u, |
| 4344 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4345 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4346 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4347 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4348 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4349 | } |
| 4350 | |
| 4351 | TEST_F(ClientSocketPoolBaseTest, MultipleRequestsGetAssignedJobs) { |
| 4352 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4353 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4354 | |
| 4355 | ClientSocketHandle handle1; |
| 4356 | TestCompletionCallback callback1; |
| 4357 | EXPECT_EQ( |
| 4358 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4359 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4360 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4361 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4362 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4363 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4364 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4365 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4366 | TestGroupId("a"))); |
| 4367 | EXPECT_EQ(0u, |
| 4368 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4369 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4370 | |
| 4371 | ClientSocketHandle handle2; |
| 4372 | TestCompletionCallback callback2; |
| 4373 | EXPECT_EQ( |
| 4374 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4375 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4376 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4377 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4378 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4379 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4380 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4381 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4382 | TestGroupId("a"))); |
| 4383 | EXPECT_EQ(0u, |
| 4384 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4385 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4386 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4387 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4388 | &handle1)); |
| 4389 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4390 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4391 | |
| 4392 | // One job completes. The other request should still have its job. |
| 4393 | client_socket_factory_.SignalJob(0); |
| 4394 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 4395 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4396 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4397 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4398 | TestGroupId("a"))); |
| 4399 | EXPECT_EQ(0u, |
| 4400 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4401 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4402 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4403 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4404 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4405 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4406 | } |
| 4407 | |
| 4408 | TEST_F(ClientSocketPoolBaseTest, PreconnectJobGetsAssignedToRequest) { |
| 4409 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4410 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4411 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4412 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 1, |
| 4413 | NetLogWithSource()); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4414 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4415 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 4416 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4417 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4418 | TestGroupId("a"))); |
| 4419 | EXPECT_EQ(1u, |
| 4420 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4421 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4422 | |
| 4423 | ClientSocketHandle handle1; |
| 4424 | TestCompletionCallback callback1; |
| 4425 | EXPECT_EQ( |
| 4426 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4427 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4428 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4429 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4430 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4431 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4432 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4433 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4434 | TestGroupId("a"))); |
| 4435 | EXPECT_EQ(0u, |
| 4436 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4437 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4438 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4439 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4440 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4441 | } |
| 4442 | |
| 4443 | TEST_F(ClientSocketPoolBaseTest, HigherPriorityRequestStealsJob) { |
| 4444 | CreatePool(kDefaultMaxSockets, 1); |
| 4445 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4446 | |
| 4447 | ClientSocketHandle handle1; |
| 4448 | TestCompletionCallback callback1; |
| 4449 | EXPECT_EQ( |
| 4450 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4451 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4452 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4453 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4454 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4455 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4456 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4457 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4458 | TestGroupId("a"))); |
| 4459 | EXPECT_EQ(0u, |
| 4460 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4461 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4462 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4463 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4464 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4465 | |
| 4466 | // Insert a higher priority request |
| 4467 | ClientSocketHandle handle2; |
| 4468 | TestCompletionCallback callback2; |
| 4469 | EXPECT_EQ( |
| 4470 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4471 | handle2.Init(TestGroupId("a"), params_, base::nullopt, HIGHEST, |
| 4472 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4473 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4474 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4475 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4476 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4477 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4478 | TestGroupId("a"))); |
| 4479 | EXPECT_EQ(0u, |
| 4480 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4481 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4482 | |
| 4483 | // The highest priority request should steal the job from the default priority |
| 4484 | // request. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4485 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4486 | &handle2)); |
| 4487 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4488 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4489 | } |
| 4490 | |
| 4491 | TEST_F(ClientSocketPoolBaseTest, RequestStealsJobFromLowestRequestWithJob) { |
| 4492 | CreatePool(3, 3); |
| 4493 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4494 | |
| 4495 | ClientSocketHandle handle_lowest; |
| 4496 | TestCompletionCallback callback_lowest; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4497 | EXPECT_EQ( |
| 4498 | ERR_IO_PENDING, |
| 4499 | handle_lowest.Init(TestGroupId("a"), params_, base::nullopt, LOWEST, |
| 4500 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4501 | callback_lowest.callback(), |
| 4502 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4503 | NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4504 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4505 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4506 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4507 | TestGroupId("a"))); |
| 4508 | EXPECT_EQ(0u, |
| 4509 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4510 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4511 | |
| 4512 | ClientSocketHandle handle_highest; |
| 4513 | TestCompletionCallback callback_highest; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4514 | EXPECT_EQ( |
| 4515 | ERR_IO_PENDING, |
| 4516 | handle_highest.Init(TestGroupId("a"), params_, base::nullopt, HIGHEST, |
| 4517 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4518 | callback_highest.callback(), |
| 4519 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4520 | NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4521 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4522 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4523 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4524 | TestGroupId("a"))); |
| 4525 | EXPECT_EQ(0u, |
| 4526 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4527 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4528 | |
| 4529 | ClientSocketHandle handle_low; |
| 4530 | TestCompletionCallback callback_low; |
| 4531 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4532 | handle_low.Init( |
| 4533 | TestGroupId("a"), params_, base::nullopt, LOW, SocketTag(), |
| 4534 | ClientSocketPool::RespectLimits::ENABLED, |
| 4535 | callback_low.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4536 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4537 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4538 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4539 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4540 | TestGroupId("a"))); |
| 4541 | EXPECT_EQ(0u, |
| 4542 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4543 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4544 | |
| 4545 | ClientSocketHandle handle_lowest2; |
| 4546 | TestCompletionCallback callback_lowest2; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4547 | EXPECT_EQ( |
| 4548 | ERR_IO_PENDING, |
| 4549 | handle_lowest2.Init(TestGroupId("a"), params_, base::nullopt, LOWEST, |
| 4550 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4551 | callback_lowest2.callback(), |
| 4552 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4553 | NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4554 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4555 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4556 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4557 | TestGroupId("a"))); |
| 4558 | EXPECT_EQ(0u, |
| 4559 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4560 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4561 | |
| 4562 | // The top three requests in the queue should have jobs. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4563 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4564 | &handle_highest)); |
| 4565 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4566 | &handle_low)); |
| 4567 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4568 | &handle_lowest)); |
| 4569 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting( |
| 4570 | TestGroupId("a"), &handle_lowest2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4571 | |
| 4572 | // Add another request with medium priority. It should steal the job from the |
| 4573 | // lowest priority request with a job. |
| 4574 | ClientSocketHandle handle_medium; |
| 4575 | TestCompletionCallback callback_medium; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4576 | EXPECT_EQ( |
| 4577 | ERR_IO_PENDING, |
| 4578 | handle_medium.Init(TestGroupId("a"), params_, base::nullopt, MEDIUM, |
| 4579 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4580 | callback_medium.callback(), |
| 4581 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4582 | NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4583 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4584 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4585 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4586 | TestGroupId("a"))); |
| 4587 | EXPECT_EQ(0u, |
| 4588 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4589 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
| 4590 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4591 | &handle_highest)); |
| 4592 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4593 | &handle_medium)); |
| 4594 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4595 | &handle_low)); |
| 4596 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4597 | &handle_lowest)); |
| 4598 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting( |
| 4599 | TestGroupId("a"), &handle_lowest2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4600 | } |
| 4601 | |
| 4602 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeRequestStealsJob) { |
| 4603 | CreatePool(kDefaultMaxSockets, 1); |
| 4604 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4605 | |
| 4606 | ClientSocketHandle handle1; |
| 4607 | TestCompletionCallback callback1; |
| 4608 | EXPECT_EQ( |
| 4609 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4610 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4611 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4612 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4613 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4614 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4615 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4616 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4617 | TestGroupId("a"))); |
| 4618 | EXPECT_EQ(0u, |
| 4619 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4620 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4621 | |
| 4622 | ClientSocketHandle handle2; |
| 4623 | TestCompletionCallback callback2; |
| 4624 | EXPECT_EQ( |
| 4625 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4626 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4627 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4628 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4629 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4630 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4631 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4632 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4633 | TestGroupId("a"))); |
| 4634 | EXPECT_EQ(0u, |
| 4635 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4636 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4637 | |
| 4638 | // 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] | 4639 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4640 | &handle1)); |
| 4641 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4642 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4643 | |
| 4644 | // Reprioritizing the second request places it above the first, and it steals |
| 4645 | // the job from the first request. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4646 | pool_->SetPriority(TestGroupId("a"), &handle2, HIGHEST); |
| 4647 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4648 | &handle2)); |
| 4649 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4650 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4651 | } |
| 4652 | |
| 4653 | TEST_F(ClientSocketPoolBaseTest, CancelRequestReassignsJob) { |
| 4654 | CreatePool(kDefaultMaxSockets, 1); |
| 4655 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4656 | |
| 4657 | ClientSocketHandle handle1; |
| 4658 | TestCompletionCallback callback1; |
| 4659 | EXPECT_EQ( |
| 4660 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4661 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4662 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4663 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4664 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4665 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4666 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4667 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4668 | TestGroupId("a"))); |
| 4669 | EXPECT_EQ(0u, |
| 4670 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4671 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4672 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4673 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4674 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4675 | |
| 4676 | ClientSocketHandle handle2; |
| 4677 | TestCompletionCallback callback2; |
| 4678 | EXPECT_EQ( |
| 4679 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4680 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4681 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4682 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4683 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4684 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4685 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4686 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4687 | TestGroupId("a"))); |
| 4688 | EXPECT_EQ(0u, |
| 4689 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4690 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4691 | |
| 4692 | // The second request doesn't get a job because we are the limit. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4693 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4694 | &handle1)); |
| 4695 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4696 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4697 | |
| 4698 | // The second request should get a job upon cancelling the first request. |
| 4699 | handle1.Reset(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4700 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4701 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4702 | TestGroupId("a"))); |
| 4703 | EXPECT_EQ(0u, |
| 4704 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4705 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4706 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4707 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4708 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4709 | } |
| 4710 | |
| 4711 | TEST_F(ClientSocketPoolBaseTest, JobCompletionReassignsJob) { |
| 4712 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4713 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4714 | |
| 4715 | ClientSocketHandle handle1; |
| 4716 | TestCompletionCallback callback1; |
| 4717 | EXPECT_EQ( |
| 4718 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4719 | handle1.Init(TestGroupId("a"), params_, base::nullopt, HIGHEST, |
| 4720 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4721 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4722 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4723 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4724 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4725 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4726 | TestGroupId("a"))); |
| 4727 | EXPECT_EQ(0u, |
| 4728 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4729 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4730 | |
| 4731 | ClientSocketHandle handle2; |
| 4732 | TestCompletionCallback callback2; |
| 4733 | EXPECT_EQ( |
| 4734 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4735 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4736 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4737 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4738 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4739 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4740 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4741 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4742 | TestGroupId("a"))); |
| 4743 | EXPECT_EQ(0u, |
| 4744 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4745 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4746 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4747 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4748 | &handle1)); |
| 4749 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4750 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4751 | |
| 4752 | // The lower-priority job completes first. The higher-priority request should |
| 4753 | // get the socket, and the lower-priority request should get the remaining |
| 4754 | // job. |
| 4755 | client_socket_factory_.SignalJob(1); |
| 4756 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4757 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4758 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4759 | TestGroupId("a"))); |
| 4760 | EXPECT_EQ(0u, |
| 4761 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4762 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4763 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4764 | EXPECT_TRUE(handle1.socket()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4765 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4766 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4767 | } |
| 4768 | |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 4769 | class MockLayeredPool : public HigherLayeredPool { |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4770 | public: |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4771 | MockLayeredPool(TransportClientSocketPool* pool, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4772 | const ClientSocketPool::GroupId& group_id) |
| 4773 | : pool_(pool), group_id_(group_id), can_release_connection_(true) { |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 4774 | pool_->AddHigherLayeredPool(this); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4775 | } |
| 4776 | |
Daniel Cheng | 4496d082 | 2018-04-26 21:52:15 | [diff] [blame] | 4777 | ~MockLayeredPool() override { pool_->RemoveHigherLayeredPool(this); } |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4778 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4779 | int RequestSocket(TransportClientSocketPool* pool) { |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4780 | return handle_.Init( |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 4781 | group_id_, ClientSocketPool::SocketParams::CreateForHttpForTesting(), |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4782 | base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
| 4783 | ClientSocketPool::RespectLimits::ENABLED, callback_.callback(), |
| 4784 | ClientSocketPool::ProxyAuthCallback(), pool, NetLogWithSource()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4785 | } |
| 4786 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4787 | int RequestSocketWithoutLimits(TransportClientSocketPool* pool) { |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4788 | return handle_.Init( |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 4789 | group_id_, ClientSocketPool::SocketParams::CreateForHttpForTesting(), |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4790 | base::nullopt, MAXIMUM_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4791 | ClientSocketPool::RespectLimits::DISABLED, callback_.callback(), |
| 4792 | ClientSocketPool::ProxyAuthCallback(), pool, NetLogWithSource()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4793 | } |
| 4794 | |
| 4795 | bool ReleaseOneConnection() { |
| 4796 | if (!handle_.is_initialized() || !can_release_connection_) { |
| 4797 | return false; |
| 4798 | } |
| 4799 | handle_.socket()->Disconnect(); |
| 4800 | handle_.Reset(); |
| 4801 | return true; |
| 4802 | } |
| 4803 | |
| 4804 | void set_can_release_connection(bool can_release_connection) { |
| 4805 | can_release_connection_ = can_release_connection; |
| 4806 | } |
| 4807 | |
| 4808 | MOCK_METHOD0(CloseOneIdleConnection, bool()); |
| 4809 | |
| 4810 | private: |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4811 | TransportClientSocketPool* const pool_; |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4812 | ClientSocketHandle handle_; |
| 4813 | TestCompletionCallback callback_; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4814 | const ClientSocketPool::GroupId group_id_; |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4815 | bool can_release_connection_; |
| 4816 | }; |
| 4817 | |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4818 | // Tests the basic case of closing an idle socket in a higher layered pool when |
| 4819 | // a new request is issued and the lower layer pool is stalled. |
| 4820 | TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketsHeldByLayeredPoolWhenNeeded) { |
| 4821 | CreatePool(1, 1); |
| 4822 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 4823 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4824 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("foo")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4825 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4826 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4827 | .WillOnce(Invoke(&mock_layered_pool, |
| 4828 | &MockLayeredPool::ReleaseOneConnection)); |
| 4829 | ClientSocketHandle handle; |
| 4830 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4831 | EXPECT_EQ( |
| 4832 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4833 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4834 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4835 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4836 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4837 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4838 | } |
| 4839 | |
Matt Menke | 83367864 | 2019-03-05 22:05:51 | [diff] [blame] | 4840 | // Tests the case that trying to close an idle socket in a higher layered pool |
| 4841 | // fails. |
| 4842 | TEST_F(ClientSocketPoolBaseTest, |
| 4843 | CloseIdleSocketsHeldByLayeredPoolWhenNeededFails) { |
| 4844 | CreatePool(1, 1); |
| 4845 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 4846 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4847 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("foo")); |
Matt Menke | 83367864 | 2019-03-05 22:05:51 | [diff] [blame] | 4848 | mock_layered_pool.set_can_release_connection(false); |
| 4849 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
| 4850 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4851 | .WillOnce(Invoke(&mock_layered_pool, |
| 4852 | &MockLayeredPool::ReleaseOneConnection)); |
| 4853 | ClientSocketHandle handle; |
| 4854 | TestCompletionCallback callback; |
| 4855 | EXPECT_EQ( |
| 4856 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4857 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4858 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4859 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4860 | pool_.get(), NetLogWithSource())); |
Matt Menke | 83367864 | 2019-03-05 22:05:51 | [diff] [blame] | 4861 | base::RunLoop().RunUntilIdle(); |
| 4862 | EXPECT_FALSE(callback.have_result()); |
| 4863 | } |
| 4864 | |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4865 | // Same as above, but the idle socket is in the same group as the stalled |
| 4866 | // socket, and closes the only other request in its group when closing requests |
| 4867 | // in higher layered pools. This generally shouldn't happen, but it may be |
| 4868 | // possible if a higher level pool issues a request and the request is |
| 4869 | // subsequently cancelled. Even if it's not possible, best not to crash. |
| 4870 | TEST_F(ClientSocketPoolBaseTest, |
| 4871 | CloseIdleSocketsHeldByLayeredPoolWhenNeededSameGroup) { |
| 4872 | CreatePool(2, 2); |
| 4873 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 4874 | |
| 4875 | // Need a socket in another group for the pool to be stalled (If a group |
| 4876 | // has the maximum number of connections already, it's not stalled). |
| 4877 | ClientSocketHandle handle1; |
| 4878 | TestCompletionCallback callback1; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4879 | EXPECT_EQ(OK, handle1.Init(TestGroupId("group1"), params_, base::nullopt, |
| 4880 | DEFAULT_PRIORITY, SocketTag(), |
| 4881 | ClientSocketPool::RespectLimits::ENABLED, |
| 4882 | callback1.callback(), |
| 4883 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4884 | NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4885 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4886 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("group2")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4887 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4888 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4889 | .WillOnce(Invoke(&mock_layered_pool, |
| 4890 | &MockLayeredPool::ReleaseOneConnection)); |
| 4891 | ClientSocketHandle handle; |
| 4892 | TestCompletionCallback callback2; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4893 | EXPECT_EQ(ERR_IO_PENDING, |
| 4894 | handle.Init( |
| 4895 | TestGroupId("group2"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4896 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4897 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4898 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4899 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4900 | } |
| 4901 | |
| 4902 | // Tests the case when an idle socket can be closed when a new request is |
| 4903 | // issued, and the new request belongs to a group that was previously stalled. |
| 4904 | TEST_F(ClientSocketPoolBaseTest, |
| 4905 | CloseIdleSocketsHeldByLayeredPoolInSameGroupWhenNeeded) { |
| 4906 | CreatePool(2, 2); |
| 4907 | std::list<TestConnectJob::JobType> job_types; |
| 4908 | job_types.push_back(TestConnectJob::kMockJob); |
| 4909 | job_types.push_back(TestConnectJob::kMockJob); |
| 4910 | job_types.push_back(TestConnectJob::kMockJob); |
| 4911 | job_types.push_back(TestConnectJob::kMockJob); |
| 4912 | connect_job_factory_->set_job_types(&job_types); |
| 4913 | |
| 4914 | ClientSocketHandle handle1; |
| 4915 | TestCompletionCallback callback1; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4916 | EXPECT_EQ(OK, handle1.Init(TestGroupId("group1"), params_, base::nullopt, |
| 4917 | DEFAULT_PRIORITY, SocketTag(), |
| 4918 | ClientSocketPool::RespectLimits::ENABLED, |
| 4919 | callback1.callback(), |
| 4920 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4921 | NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4922 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4923 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("group2")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4924 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4925 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4926 | .WillRepeatedly(Invoke(&mock_layered_pool, |
| 4927 | &MockLayeredPool::ReleaseOneConnection)); |
| 4928 | mock_layered_pool.set_can_release_connection(false); |
| 4929 | |
| 4930 | // The third request is made when the socket pool is in a stalled state. |
| 4931 | ClientSocketHandle handle3; |
| 4932 | TestCompletionCallback callback3; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4933 | EXPECT_EQ(ERR_IO_PENDING, |
| 4934 | handle3.Init( |
| 4935 | TestGroupId("group3"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4936 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4937 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4938 | pool_.get(), NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4939 | |
| 4940 | base::RunLoop().RunUntilIdle(); |
| 4941 | EXPECT_FALSE(callback3.have_result()); |
| 4942 | |
| 4943 | // The fourth request is made when the pool is no longer stalled. The third |
| 4944 | // request should be serviced first, since it was issued first and has the |
| 4945 | // same priority. |
| 4946 | mock_layered_pool.set_can_release_connection(true); |
| 4947 | ClientSocketHandle handle4; |
| 4948 | TestCompletionCallback callback4; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4949 | EXPECT_EQ(ERR_IO_PENDING, |
| 4950 | handle4.Init( |
| 4951 | TestGroupId("group3"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4952 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4953 | callback4.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4954 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4955 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4956 | EXPECT_FALSE(callback4.have_result()); |
| 4957 | |
| 4958 | // Closing a handle should free up another socket slot. |
| 4959 | handle1.Reset(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4960 | EXPECT_THAT(callback4.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4961 | } |
| 4962 | |
| 4963 | // Tests the case when an idle socket can be closed when a new request is |
| 4964 | // issued, and the new request belongs to a group that was previously stalled. |
| 4965 | // |
| 4966 | // The two differences from the above test are that the stalled requests are not |
| 4967 | // in the same group as the layered pool's request, and the the fourth request |
| 4968 | // has a higher priority than the third one, so gets a socket first. |
| 4969 | TEST_F(ClientSocketPoolBaseTest, |
| 4970 | CloseIdleSocketsHeldByLayeredPoolInSameGroupWhenNeeded2) { |
| 4971 | CreatePool(2, 2); |
| 4972 | std::list<TestConnectJob::JobType> job_types; |
| 4973 | job_types.push_back(TestConnectJob::kMockJob); |
| 4974 | job_types.push_back(TestConnectJob::kMockJob); |
| 4975 | job_types.push_back(TestConnectJob::kMockJob); |
| 4976 | job_types.push_back(TestConnectJob::kMockJob); |
| 4977 | connect_job_factory_->set_job_types(&job_types); |
| 4978 | |
| 4979 | ClientSocketHandle handle1; |
| 4980 | TestCompletionCallback callback1; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4981 | EXPECT_EQ(OK, handle1.Init(TestGroupId("group1"), params_, base::nullopt, |
| 4982 | DEFAULT_PRIORITY, SocketTag(), |
| 4983 | ClientSocketPool::RespectLimits::ENABLED, |
| 4984 | callback1.callback(), |
| 4985 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4986 | NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4987 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4988 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("group2")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4989 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4990 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4991 | .WillRepeatedly(Invoke(&mock_layered_pool, |
| 4992 | &MockLayeredPool::ReleaseOneConnection)); |
| 4993 | mock_layered_pool.set_can_release_connection(false); |
| 4994 | |
| 4995 | // The third request is made when the socket pool is in a stalled state. |
| 4996 | ClientSocketHandle handle3; |
| 4997 | TestCompletionCallback callback3; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4998 | EXPECT_EQ( |
| 4999 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5000 | handle3.Init(TestGroupId("group3"), params_, base::nullopt, MEDIUM, |
| 5001 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 5002 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5003 | pool_.get(), NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 5004 | |
| 5005 | base::RunLoop().RunUntilIdle(); |
| 5006 | EXPECT_FALSE(callback3.have_result()); |
| 5007 | |
| 5008 | // The fourth request is made when the pool is no longer stalled. This |
| 5009 | // request has a higher priority than the third request, so is serviced first. |
| 5010 | mock_layered_pool.set_can_release_connection(true); |
| 5011 | ClientSocketHandle handle4; |
| 5012 | TestCompletionCallback callback4; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 5013 | EXPECT_EQ( |
| 5014 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5015 | handle4.Init(TestGroupId("group3"), params_, base::nullopt, HIGHEST, |
| 5016 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 5017 | callback4.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5018 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5019 | EXPECT_THAT(callback4.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 5020 | EXPECT_FALSE(callback3.have_result()); |
| 5021 | |
| 5022 | // Closing a handle should free up another socket slot. |
| 5023 | handle1.Reset(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5024 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 5025 | } |
| 5026 | |
| 5027 | TEST_F(ClientSocketPoolBaseTest, |
| 5028 | CloseMultipleIdleSocketsHeldByLayeredPoolWhenNeeded) { |
| 5029 | CreatePool(1, 1); |
| 5030 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 5031 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5032 | MockLayeredPool mock_layered_pool1(pool_.get(), TestGroupId("foo")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5033 | EXPECT_THAT(mock_layered_pool1.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 5034 | EXPECT_CALL(mock_layered_pool1, CloseOneIdleConnection()) |
| 5035 | .WillRepeatedly(Invoke(&mock_layered_pool1, |
| 5036 | &MockLayeredPool::ReleaseOneConnection)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5037 | MockLayeredPool mock_layered_pool2(pool_.get(), TestGroupId("bar")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5038 | EXPECT_THAT(mock_layered_pool2.RequestSocketWithoutLimits(pool_.get()), |
| 5039 | IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 5040 | EXPECT_CALL(mock_layered_pool2, CloseOneIdleConnection()) |
| 5041 | .WillRepeatedly(Invoke(&mock_layered_pool2, |
| 5042 | &MockLayeredPool::ReleaseOneConnection)); |
| 5043 | ClientSocketHandle handle; |
| 5044 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 5045 | EXPECT_EQ( |
| 5046 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5047 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 5048 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5049 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5050 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5051 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 5052 | } |
| 5053 | |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5054 | // Test that when a socket pool and group are at their limits, a request |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5055 | // with RespectLimits::DISABLED triggers creation of a new socket, and gets the |
| 5056 | // socket instead of a request with the same priority that was issued earlier, |
| 5057 | // but has RespectLimits::ENABLED. |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5058 | TEST_F(ClientSocketPoolBaseTest, IgnoreLimits) { |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5059 | CreatePool(1, 1); |
| 5060 | |
| 5061 | // Issue a request to reach the socket pool limit. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5062 | EXPECT_EQ(OK, StartRequestWithIgnoreLimits( |
| 5063 | TestGroupId("a"), MAXIMUM_PRIORITY, |
| 5064 | ClientSocketPool::RespectLimits::ENABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5065 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5066 | |
| 5067 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 5068 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5069 | EXPECT_EQ(ERR_IO_PENDING, StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5070 | TestGroupId("a"), MAXIMUM_PRIORITY, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5071 | ClientSocketPool::RespectLimits::ENABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5072 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5073 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5074 | // Issue a request that ignores the limits, so a new ConnectJob is |
| 5075 | // created. |
| 5076 | EXPECT_EQ(ERR_IO_PENDING, StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5077 | TestGroupId("a"), MAXIMUM_PRIORITY, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5078 | ClientSocketPool::RespectLimits::DISABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5079 | ASSERT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5080 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5081 | EXPECT_THAT(request(2)->WaitForResult(), IsOk()); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5082 | EXPECT_FALSE(request(1)->have_result()); |
| 5083 | } |
| 5084 | |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 5085 | // Test that when a socket pool and group are at their limits, a ConnectJob |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5086 | // issued for a request with RespectLimits::DISABLED is not cancelled when a |
| 5087 | // request with RespectLimits::ENABLED issued to the same group is cancelled. |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 5088 | TEST_F(ClientSocketPoolBaseTest, IgnoreLimitsCancelOtherJob) { |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 5089 | CreatePool(1, 1); |
| 5090 | |
| 5091 | // Issue a request to reach the socket pool limit. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5092 | EXPECT_EQ(OK, StartRequestWithIgnoreLimits( |
| 5093 | TestGroupId("a"), MAXIMUM_PRIORITY, |
| 5094 | ClientSocketPool::RespectLimits::ENABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5095 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 5096 | |
| 5097 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 5098 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5099 | EXPECT_EQ(ERR_IO_PENDING, StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5100 | TestGroupId("a"), MAXIMUM_PRIORITY, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5101 | ClientSocketPool::RespectLimits::ENABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5102 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 5103 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5104 | // Issue a request with RespectLimits::DISABLED, so a new ConnectJob is |
| 5105 | // created. |
| 5106 | EXPECT_EQ(ERR_IO_PENDING, StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5107 | TestGroupId("a"), MAXIMUM_PRIORITY, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5108 | ClientSocketPool::RespectLimits::DISABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5109 | ASSERT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5110 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5111 | // Cancel the pending request with RespectLimits::ENABLED. The ConnectJob |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5112 | // should not be cancelled. |
| 5113 | request(1)->handle()->Reset(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5114 | ASSERT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5115 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5116 | EXPECT_THAT(request(2)->WaitForResult(), IsOk()); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5117 | EXPECT_FALSE(request(1)->have_result()); |
| 5118 | } |
| 5119 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5120 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthNoAuthCallback) { |
| 5121 | CreatePool(1, 1); |
| 5122 | |
| 5123 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5124 | |
| 5125 | ClientSocketHandle handle; |
| 5126 | TestCompletionCallback callback; |
| 5127 | EXPECT_EQ( |
| 5128 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5129 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 5130 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5131 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5132 | pool_.get(), NetLogWithSource())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5133 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5134 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5135 | |
| 5136 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_PROXY_AUTH_REQUESTED)); |
| 5137 | EXPECT_FALSE(handle.is_initialized()); |
| 5138 | EXPECT_FALSE(handle.socket()); |
| 5139 | |
| 5140 | // The group should now be empty, and thus be deleted. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5141 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5142 | } |
| 5143 | |
| 5144 | class TestAuthHelper { |
| 5145 | public: |
| 5146 | TestAuthHelper() = default; |
| 5147 | ~TestAuthHelper() = default; |
| 5148 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5149 | void InitHandle( |
Matt Menke | 84d11e56 | 2019-03-27 00:11:19 | [diff] [blame] | 5150 | scoped_refptr<ClientSocketPool::SocketParams> params, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5151 | TransportClientSocketPool* pool, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5152 | RequestPriority priority = DEFAULT_PRIORITY, |
| 5153 | ClientSocketPool::RespectLimits respect_limits = |
| 5154 | ClientSocketPool::RespectLimits::ENABLED, |
| 5155 | const ClientSocketPool::GroupId& group_id_in = TestGroupId("a")) { |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5156 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5157 | handle_.Init(group_id_in, params, base::nullopt, priority, |
| 5158 | SocketTag(), respect_limits, callback_.callback(), |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5159 | base::BindRepeating(&TestAuthHelper::AuthCallback, |
| 5160 | base::Unretained(this)), |
| 5161 | pool, NetLogWithSource())); |
| 5162 | } |
| 5163 | |
| 5164 | void WaitForAuth() { |
| 5165 | run_loop_ = std::make_unique<base::RunLoop>(); |
| 5166 | run_loop_->Run(); |
| 5167 | run_loop_.reset(); |
| 5168 | } |
| 5169 | |
| 5170 | void WaitForAuthAndRestartSync() { |
| 5171 | restart_sync_ = true; |
| 5172 | WaitForAuth(); |
| 5173 | restart_sync_ = false; |
| 5174 | } |
| 5175 | |
| 5176 | void WaitForAuthAndResetHandleSync() { |
| 5177 | reset_handle_sync_ = true; |
| 5178 | WaitForAuth(); |
| 5179 | reset_handle_sync_ = false; |
| 5180 | } |
| 5181 | |
| 5182 | void RestartWithAuth() { |
| 5183 | DCHECK(restart_with_auth_callback_); |
| 5184 | std::move(restart_with_auth_callback_).Run(); |
| 5185 | } |
| 5186 | |
| 5187 | int WaitForResult() { |
| 5188 | int result = callback_.WaitForResult(); |
| 5189 | // There shouldn't be any callback waiting to be invoked once the request is |
| 5190 | // complete. |
| 5191 | EXPECT_FALSE(restart_with_auth_callback_); |
| 5192 | // The socket should only be initialized on success. |
| 5193 | EXPECT_EQ(result == OK, handle_.is_initialized()); |
| 5194 | EXPECT_EQ(result == OK, handle_.socket() != nullptr); |
| 5195 | return result; |
| 5196 | } |
| 5197 | |
| 5198 | ClientSocketHandle* handle() { return &handle_; } |
| 5199 | int auth_count() const { return auth_count_; } |
| 5200 | int have_result() const { return callback_.have_result(); } |
| 5201 | |
| 5202 | private: |
| 5203 | void AuthCallback(const HttpResponseInfo& response, |
| 5204 | HttpAuthController* auth_controller, |
| 5205 | base::OnceClosure restart_with_auth_callback) { |
| 5206 | EXPECT_FALSE(restart_with_auth_callback_); |
| 5207 | EXPECT_TRUE(restart_with_auth_callback); |
| 5208 | |
| 5209 | // Once there's a result, this method shouldn't be invoked again. |
| 5210 | EXPECT_FALSE(callback_.have_result()); |
| 5211 | |
| 5212 | ++auth_count_; |
| 5213 | run_loop_->Quit(); |
| 5214 | if (restart_sync_) { |
| 5215 | std::move(restart_with_auth_callback).Run(); |
| 5216 | return; |
| 5217 | } |
| 5218 | |
| 5219 | restart_with_auth_callback_ = std::move(restart_with_auth_callback); |
| 5220 | |
| 5221 | if (reset_handle_sync_) { |
| 5222 | handle_.Reset(); |
| 5223 | return; |
| 5224 | } |
| 5225 | } |
| 5226 | |
| 5227 | std::unique_ptr<base::RunLoop> run_loop_; |
| 5228 | base::OnceClosure restart_with_auth_callback_; |
| 5229 | |
| 5230 | bool restart_sync_ = false; |
| 5231 | bool reset_handle_sync_ = false; |
| 5232 | |
| 5233 | ClientSocketHandle handle_; |
| 5234 | int auth_count_ = 0; |
| 5235 | TestCompletionCallback callback_; |
| 5236 | |
| 5237 | DISALLOW_COPY_AND_ASSIGN(TestAuthHelper); |
| 5238 | }; |
| 5239 | |
| 5240 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnce) { |
| 5241 | CreatePool(1, 1); |
| 5242 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5243 | |
| 5244 | TestAuthHelper auth_helper; |
| 5245 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5246 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5247 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5248 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5249 | |
| 5250 | auth_helper.WaitForAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5251 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5252 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5253 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5254 | |
| 5255 | auth_helper.RestartWithAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5256 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5257 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5258 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5259 | |
| 5260 | EXPECT_THAT(auth_helper.WaitForResult(), IsOk()); |
| 5261 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5262 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5263 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5264 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5265 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5266 | } |
| 5267 | |
| 5268 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceSync) { |
| 5269 | CreatePool(1, 1); |
| 5270 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5271 | |
| 5272 | TestAuthHelper auth_helper; |
| 5273 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5274 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5275 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5276 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5277 | |
| 5278 | auth_helper.WaitForAuthAndRestartSync(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5279 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5280 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5281 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5282 | |
| 5283 | EXPECT_THAT(auth_helper.WaitForResult(), IsOk()); |
| 5284 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5285 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5286 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5287 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5288 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5289 | } |
| 5290 | |
| 5291 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceFails) { |
| 5292 | CreatePool(1, 1); |
| 5293 | connect_job_factory_->set_job_type( |
| 5294 | TestConnectJob::kMockAuthChallengeOnceFailingJob); |
| 5295 | |
| 5296 | TestAuthHelper auth_helper; |
| 5297 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5298 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5299 | |
| 5300 | auth_helper.WaitForAuth(); |
| 5301 | auth_helper.RestartWithAuth(); |
| 5302 | EXPECT_THAT(auth_helper.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 5303 | |
| 5304 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5305 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5306 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5307 | } |
| 5308 | |
| 5309 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceSyncFails) { |
| 5310 | CreatePool(1, 1); |
| 5311 | connect_job_factory_->set_job_type( |
| 5312 | TestConnectJob::kMockAuthChallengeOnceFailingJob); |
| 5313 | |
| 5314 | TestAuthHelper auth_helper; |
| 5315 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5316 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5317 | |
| 5318 | auth_helper.WaitForAuthAndRestartSync(); |
| 5319 | EXPECT_THAT(auth_helper.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 5320 | |
| 5321 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5322 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5323 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5324 | } |
| 5325 | |
| 5326 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceDeleteHandle) { |
| 5327 | CreatePool(1, 1); |
| 5328 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5329 | |
| 5330 | TestAuthHelper auth_helper; |
| 5331 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5332 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5333 | |
| 5334 | auth_helper.WaitForAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5335 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5336 | |
| 5337 | auth_helper.handle()->Reset(); |
| 5338 | |
| 5339 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5340 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5341 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5342 | EXPECT_FALSE(auth_helper.handle()->is_initialized()); |
| 5343 | EXPECT_FALSE(auth_helper.handle()->socket()); |
| 5344 | } |
| 5345 | |
| 5346 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceDeleteHandleSync) { |
| 5347 | CreatePool(1, 1); |
| 5348 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5349 | |
| 5350 | TestAuthHelper auth_helper; |
| 5351 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5352 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5353 | |
| 5354 | auth_helper.WaitForAuthAndResetHandleSync(); |
| 5355 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5356 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5357 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5358 | EXPECT_FALSE(auth_helper.handle()->is_initialized()); |
| 5359 | EXPECT_FALSE(auth_helper.handle()->socket()); |
| 5360 | } |
| 5361 | |
| 5362 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceFlushWithError) { |
| 5363 | CreatePool(1, 1); |
| 5364 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5365 | |
| 5366 | TestAuthHelper auth_helper; |
| 5367 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5368 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5369 | |
| 5370 | auth_helper.WaitForAuth(); |
| 5371 | |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 5372 | pool_->FlushWithError(ERR_FAILED, "Network changed"); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5373 | base::RunLoop().RunUntilIdle(); |
| 5374 | |
| 5375 | // When flushing the socket pool, bound sockets should delay returning the |
| 5376 | // error until completion. |
| 5377 | EXPECT_FALSE(auth_helper.have_result()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5378 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5379 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5380 | |
| 5381 | auth_helper.RestartWithAuth(); |
| 5382 | // The callback should be called asynchronously. |
| 5383 | EXPECT_FALSE(auth_helper.have_result()); |
| 5384 | |
| 5385 | EXPECT_THAT(auth_helper.WaitForResult(), IsError(ERR_FAILED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5386 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5387 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5388 | } |
| 5389 | |
| 5390 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthTwice) { |
| 5391 | CreatePool(1, 1); |
| 5392 | connect_job_factory_->set_job_type( |
| 5393 | TestConnectJob::kMockAuthChallengeTwiceJob); |
| 5394 | |
| 5395 | TestAuthHelper auth_helper; |
| 5396 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5397 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5398 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5399 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5400 | |
| 5401 | auth_helper.WaitForAuth(); |
| 5402 | auth_helper.RestartWithAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5403 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5404 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5405 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5406 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5407 | |
| 5408 | auth_helper.WaitForAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5409 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5410 | EXPECT_EQ(2, auth_helper.auth_count()); |
| 5411 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5412 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5413 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5414 | auth_helper.RestartWithAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5415 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5416 | EXPECT_EQ(2, auth_helper.auth_count()); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5417 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5418 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5419 | |
| 5420 | EXPECT_THAT(auth_helper.WaitForResult(), IsOk()); |
| 5421 | EXPECT_EQ(2, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5422 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5423 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5424 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5425 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5426 | } |
| 5427 | |
| 5428 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthTwiceFails) { |
| 5429 | CreatePool(1, 1); |
| 5430 | connect_job_factory_->set_job_type( |
| 5431 | TestConnectJob::kMockAuthChallengeTwiceFailingJob); |
| 5432 | |
| 5433 | TestAuthHelper auth_helper; |
| 5434 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5435 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5436 | |
| 5437 | auth_helper.WaitForAuth(); |
| 5438 | auth_helper.RestartWithAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5439 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5440 | EXPECT_EQ(1, auth_helper.auth_count()); |
| 5441 | |
| 5442 | auth_helper.WaitForAuth(); |
| 5443 | auth_helper.RestartWithAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5444 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5445 | EXPECT_EQ(2, auth_helper.auth_count()); |
| 5446 | |
| 5447 | EXPECT_THAT(auth_helper.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 5448 | EXPECT_EQ(2, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5449 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5450 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5451 | } |
| 5452 | |
| 5453 | // Makes sure that when a bound request is destroyed, a new ConnectJob is |
| 5454 | // created, if needed. |
| 5455 | TEST_F(ClientSocketPoolBaseTest, |
| 5456 | ProxyAuthCreateNewConnectJobOnDestroyBoundRequest) { |
| 5457 | CreatePool(1 /* max_sockets */, 1 /* max_sockets_per_group */); |
| 5458 | connect_job_factory_->set_job_type( |
| 5459 | TestConnectJob::kMockAuthChallengeOnceFailingJob); |
| 5460 | |
| 5461 | // First request creates a ConnectJob. |
| 5462 | TestAuthHelper auth_helper1; |
| 5463 | auth_helper1.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5464 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5465 | |
| 5466 | // A second request come in, but no new ConnectJob is needed, since the limit |
| 5467 | // has been reached. |
| 5468 | TestAuthHelper auth_helper2; |
| 5469 | auth_helper2.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5470 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5471 | |
| 5472 | // Run until the auth callback for the first request is invoked. |
| 5473 | auth_helper1.WaitForAuth(); |
| 5474 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5475 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5476 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5477 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5478 | |
| 5479 | // Make connect jobs succeed, then cancel the first request, which should |
| 5480 | // destroy the bound ConnectJob, and cause a new ConnectJob to start. |
| 5481 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 5482 | auth_helper1.handle()->Reset(); |
| 5483 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5484 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5485 | |
| 5486 | // The second ConnectJob should succeed. |
| 5487 | EXPECT_THAT(auth_helper2.WaitForResult(), IsOk()); |
| 5488 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5489 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5490 | } |
| 5491 | |
| 5492 | // Makes sure that when a bound request is destroyed, a new ConnectJob is |
| 5493 | // created for another group, if needed. |
| 5494 | TEST_F(ClientSocketPoolBaseTest, |
| 5495 | ProxyAuthCreateNewConnectJobOnDestroyBoundRequestDifferentGroups) { |
| 5496 | CreatePool(1 /* max_sockets */, 1 /* max_sockets_per_group */); |
| 5497 | connect_job_factory_->set_job_type( |
| 5498 | TestConnectJob::kMockAuthChallengeOnceFailingJob); |
| 5499 | |
| 5500 | // First request creates a ConnectJob. |
| 5501 | TestAuthHelper auth_helper1; |
| 5502 | auth_helper1.InitHandle(params_, pool_.get(), DEFAULT_PRIORITY); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5503 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5504 | |
| 5505 | // A second request come in, but no new ConnectJob is needed, since the limit |
| 5506 | // has been reached. |
| 5507 | TestAuthHelper auth_helper2; |
| 5508 | auth_helper2.InitHandle(params_, pool_.get(), DEFAULT_PRIORITY, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5509 | ClientSocketPool::RespectLimits::ENABLED, |
| 5510 | TestGroupId("b")); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5511 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5512 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5513 | |
| 5514 | // Run until the auth callback for the first request is invoked. |
| 5515 | auth_helper1.WaitForAuth(); |
| 5516 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5517 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5518 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5519 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5520 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 5521 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5522 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5523 | |
| 5524 | // Make connect jobs succeed, then cancel the first request, which should |
| 5525 | // destroy the bound ConnectJob, and cause a new ConnectJob to start for the |
| 5526 | // other group. |
| 5527 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 5528 | auth_helper1.handle()->Reset(); |
| 5529 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5530 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 5531 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5532 | |
| 5533 | // The second ConnectJob should succeed. |
| 5534 | EXPECT_THAT(auth_helper2.WaitForResult(), IsOk()); |
| 5535 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5536 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 5537 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5538 | } |
| 5539 | |
| 5540 | // Test that once an auth challenge is bound, that's the request that gets all |
| 5541 | // subsequent calls and the socket itself. |
| 5542 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthStaysBound) { |
| 5543 | CreatePool(1, 1); |
| 5544 | connect_job_factory_->set_job_type( |
| 5545 | TestConnectJob::kMockAuthChallengeTwiceJob); |
| 5546 | |
| 5547 | // First request creates a ConnectJob. |
| 5548 | TestAuthHelper auth_helper1; |
| 5549 | auth_helper1.InitHandle(params_, pool_.get(), LOWEST); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5550 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5551 | |
| 5552 | // A second, higher priority request is made. |
| 5553 | TestAuthHelper auth_helper2; |
| 5554 | auth_helper2.InitHandle(params_, pool_.get(), LOW); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5555 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5556 | |
| 5557 | // Run until the auth callback for the second request is invoked. |
| 5558 | auth_helper2.WaitForAuth(); |
| 5559 | EXPECT_EQ(0, auth_helper1.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5560 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5561 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5562 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5563 | |
| 5564 | // Start a higher priority job. It shouldn't be able to steal |auth_helper2|'s |
| 5565 | // ConnectJob. |
| 5566 | TestAuthHelper auth_helper3; |
| 5567 | auth_helper3.InitHandle(params_, pool_.get(), HIGHEST); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5568 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5569 | |
| 5570 | // Start a higher job that ignores limits, creating a hanging socket. It |
| 5571 | // shouldn't be able to steal |auth_helper2|'s ConnectJob. |
| 5572 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 5573 | TestAuthHelper auth_helper4; |
| 5574 | auth_helper4.InitHandle(params_, pool_.get(), HIGHEST, |
| 5575 | ClientSocketPool::RespectLimits::DISABLED); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5576 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5577 | |
| 5578 | // Restart with auth, and |auth_helper2|'s auth method should be invoked |
| 5579 | // again. |
| 5580 | auth_helper2.RestartWithAuth(); |
| 5581 | auth_helper2.WaitForAuth(); |
| 5582 | EXPECT_EQ(0, auth_helper1.auth_count()); |
| 5583 | EXPECT_FALSE(auth_helper1.have_result()); |
| 5584 | EXPECT_EQ(2, auth_helper2.auth_count()); |
| 5585 | EXPECT_FALSE(auth_helper2.have_result()); |
| 5586 | EXPECT_EQ(0, auth_helper3.auth_count()); |
| 5587 | EXPECT_FALSE(auth_helper3.have_result()); |
| 5588 | EXPECT_EQ(0, auth_helper4.auth_count()); |
| 5589 | EXPECT_FALSE(auth_helper4.have_result()); |
| 5590 | |
| 5591 | // Advance auth again, and |auth_helper2| should get the socket. |
| 5592 | auth_helper2.RestartWithAuth(); |
| 5593 | EXPECT_THAT(auth_helper2.WaitForResult(), IsOk()); |
| 5594 | // The hung ConnectJob for the RespectLimits::DISABLED request is still in the |
| 5595 | // socket pool. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5596 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5597 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5598 | EXPECT_EQ(0, auth_helper1.auth_count()); |
| 5599 | EXPECT_FALSE(auth_helper1.have_result()); |
| 5600 | EXPECT_EQ(0, auth_helper3.auth_count()); |
| 5601 | EXPECT_FALSE(auth_helper3.have_result()); |
| 5602 | EXPECT_EQ(0, auth_helper4.auth_count()); |
| 5603 | EXPECT_FALSE(auth_helper4.have_result()); |
| 5604 | |
| 5605 | // If the socket is returned to the socket pool, the RespectLimits::DISABLED |
| 5606 | // socket request should be able to claim it. |
| 5607 | auth_helper2.handle()->Reset(); |
| 5608 | EXPECT_THAT(auth_helper4.WaitForResult(), IsOk()); |
| 5609 | EXPECT_EQ(0, auth_helper1.auth_count()); |
| 5610 | EXPECT_FALSE(auth_helper1.have_result()); |
| 5611 | EXPECT_EQ(0, auth_helper3.auth_count()); |
| 5612 | EXPECT_FALSE(auth_helper3.have_result()); |
| 5613 | EXPECT_EQ(0, auth_helper4.auth_count()); |
| 5614 | } |
| 5615 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5616 | enum class RefreshType { |
| 5617 | kServer, |
| 5618 | kProxy, |
| 5619 | }; |
| 5620 | |
| 5621 | // Common base class to test RefreshGroup() when called from either |
| 5622 | // OnSSLConfigForServerChanged() matching a specific group or the pool's proxy. |
| 5623 | // |
| 5624 | // Tests which test behavior specific to one or the other case should use |
| 5625 | // ClientSocketPoolBaseTest directly. In particular, there is no "other group" |
| 5626 | // when the pool's proxy matches. |
| 5627 | class ClientSocketPoolBaseRefreshTest |
| 5628 | : public ClientSocketPoolBaseTest, |
| 5629 | public testing::WithParamInterface<RefreshType> { |
| 5630 | public: |
| 5631 | void CreatePoolForRefresh(int max_sockets, |
| 5632 | int max_sockets_per_group, |
| 5633 | bool enable_backup_connect_jobs = false) { |
| 5634 | switch (GetParam()) { |
| 5635 | case RefreshType::kServer: |
| 5636 | CreatePool(max_sockets, max_sockets_per_group, |
| 5637 | enable_backup_connect_jobs); |
| 5638 | break; |
| 5639 | case RefreshType::kProxy: |
| 5640 | CreatePoolWithIdleTimeouts( |
| 5641 | max_sockets, max_sockets_per_group, kUnusedIdleSocketTimeout, |
| 5642 | ClientSocketPool::used_idle_socket_timeout(), |
| 5643 | enable_backup_connect_jobs, |
| 5644 | ProxyServer::FromPacString("HTTPS myproxy:70")); |
| 5645 | break; |
| 5646 | } |
| 5647 | } |
| 5648 | |
| 5649 | static ClientSocketPool::GroupId GetGroupId() { |
| 5650 | return TestGroupId("a", 443, ClientSocketPool::SocketType::kSsl); |
| 5651 | } |
| 5652 | |
David Benjamin | 6dd7e88 | 2019-10-10 02:35:23 | [diff] [blame] | 5653 | static ClientSocketPool::GroupId GetGroupIdInPartition() { |
| 5654 | // Note this GroupId will match GetGroupId() unless |
| 5655 | // kPartitionConnectionsByNetworkIsolationKey is enabled. |
| 5656 | const auto kOrigin = url::Origin::Create(GURL("https://b/")); |
| 5657 | const NetworkIsolationKey kNetworkIsolationKey(kOrigin, kOrigin); |
| 5658 | return TestGroupId("a", 443, ClientSocketPool::SocketType::kSsl, |
| 5659 | PrivacyMode::PRIVACY_MODE_DISABLED, |
| 5660 | kNetworkIsolationKey); |
| 5661 | } |
| 5662 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5663 | void OnSSLConfigForServerChanged() { |
| 5664 | switch (GetParam()) { |
| 5665 | case RefreshType::kServer: |
| 5666 | pool_->OnSSLConfigForServerChanged(HostPortPair("a", 443)); |
| 5667 | break; |
| 5668 | case RefreshType::kProxy: |
| 5669 | pool_->OnSSLConfigForServerChanged(HostPortPair("myproxy", 70)); |
| 5670 | break; |
| 5671 | } |
| 5672 | } |
| 5673 | }; |
| 5674 | |
| 5675 | INSTANTIATE_TEST_SUITE_P(RefreshType, |
| 5676 | ClientSocketPoolBaseRefreshTest, |
| 5677 | ::testing::Values(RefreshType::kServer, |
| 5678 | RefreshType::kProxy)); |
| 5679 | |
| 5680 | TEST_P(ClientSocketPoolBaseRefreshTest, RefreshGroupCreatesNewConnectJobs) { |
| 5681 | CreatePoolForRefresh(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 5682 | const ClientSocketPool::GroupId kGroupId = GetGroupId(); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5683 | |
| 5684 | // First job will be waiting until it gets aborted. |
| 5685 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 5686 | |
| 5687 | ClientSocketHandle handle; |
| 5688 | TestCompletionCallback callback; |
| 5689 | EXPECT_THAT( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5690 | handle.Init(kGroupId, params_, base::nullopt, DEFAULT_PRIORITY, |
| 5691 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5692 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5693 | pool_.get(), NetLogWithSource()), |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5694 | IsError(ERR_IO_PENDING)); |
| 5695 | |
| 5696 | // Switch connect job types, so creating a new ConnectJob will result in |
| 5697 | // success. |
| 5698 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 5699 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5700 | OnSSLConfigForServerChanged(); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5701 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 5702 | ASSERT_TRUE(handle.socket()); |
| 5703 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5704 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId)); |
| 5705 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(kGroupId)); |
| 5706 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(kGroupId)); |
| 5707 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId)); |
| 5708 | } |
| 5709 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5710 | TEST_P(ClientSocketPoolBaseRefreshTest, RefreshGroupClosesIdleConnectJobs) { |
David Benjamin | 6dd7e88 | 2019-10-10 02:35:23 | [diff] [blame] | 5711 | base::test::ScopedFeatureList feature_list; |
| 5712 | feature_list.InitAndEnableFeature( |
| 5713 | features::kPartitionConnectionsByNetworkIsolationKey); |
| 5714 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5715 | CreatePoolForRefresh(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 5716 | const ClientSocketPool::GroupId kGroupId = GetGroupId(); |
David Benjamin | 6dd7e88 | 2019-10-10 02:35:23 | [diff] [blame] | 5717 | const ClientSocketPool::GroupId kGroupIdInPartition = GetGroupIdInPartition(); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5718 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5719 | pool_->RequestSockets(kGroupId, params_, base::nullopt, 2, |
| 5720 | NetLogWithSource()); |
David Benjamin | 6dd7e88 | 2019-10-10 02:35:23 | [diff] [blame] | 5721 | pool_->RequestSockets(kGroupIdInPartition, params_, base::nullopt, 2, |
| 5722 | NetLogWithSource()); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5723 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId)); |
David Benjamin | 6dd7e88 | 2019-10-10 02:35:23 | [diff] [blame] | 5724 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupIdInPartition)); |
| 5725 | EXPECT_EQ(4, pool_->IdleSocketCount()); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5726 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(kGroupId)); |
David Benjamin | 6dd7e88 | 2019-10-10 02:35:23 | [diff] [blame] | 5727 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(kGroupIdInPartition)); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5728 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5729 | OnSSLConfigForServerChanged(); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5730 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5731 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupId)); |
David Benjamin | 6dd7e88 | 2019-10-10 02:35:23 | [diff] [blame] | 5732 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupIdInPartition)); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5733 | } |
| 5734 | |
| 5735 | TEST_F(ClientSocketPoolBaseTest, |
| 5736 | RefreshGroupDoesNotCloseIdleConnectJobsInOtherGroup) { |
| 5737 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5738 | const ClientSocketPool::GroupId kGroupId = |
| 5739 | TestGroupId("a", 443, ClientSocketPool::SocketType::kSsl); |
| 5740 | const ClientSocketPool::GroupId kOtherGroupId = |
| 5741 | TestGroupId("b", 443, ClientSocketPool::SocketType::kSsl); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5742 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5743 | pool_->RequestSockets(kOtherGroupId, params_, base::nullopt, 2, |
| 5744 | NetLogWithSource()); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5745 | ASSERT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 5746 | EXPECT_EQ(2, pool_->IdleSocketCount()); |
| 5747 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(kOtherGroupId)); |
| 5748 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5749 | pool_->OnSSLConfigForServerChanged(HostPortPair("a", 443)); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5750 | ASSERT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 5751 | EXPECT_EQ(2, pool_->IdleSocketCount()); |
| 5752 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(kOtherGroupId)); |
| 5753 | } |
| 5754 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5755 | TEST_P(ClientSocketPoolBaseRefreshTest, RefreshGroupPreventsSocketReuse) { |
| 5756 | CreatePoolForRefresh(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 5757 | const ClientSocketPool::GroupId kGroupId = GetGroupId(); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5758 | |
| 5759 | ClientSocketHandle handle; |
| 5760 | TestCompletionCallback callback; |
| 5761 | EXPECT_THAT( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5762 | handle.Init(kGroupId, params_, base::nullopt, DEFAULT_PRIORITY, |
| 5763 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5764 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5765 | pool_.get(), NetLogWithSource()), |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5766 | IsOk()); |
| 5767 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId)); |
| 5768 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId)); |
| 5769 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5770 | OnSSLConfigForServerChanged(); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5771 | |
| 5772 | handle.Reset(); |
| 5773 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5774 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupId)); |
| 5775 | } |
| 5776 | |
| 5777 | TEST_F(ClientSocketPoolBaseTest, |
| 5778 | RefreshGroupDoesNotPreventSocketReuseInOtherGroup) { |
| 5779 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5780 | const ClientSocketPool::GroupId kGroupId = |
| 5781 | TestGroupId("a", 443, ClientSocketPool::SocketType::kSsl); |
| 5782 | const ClientSocketPool::GroupId kOtherGroupId = |
| 5783 | TestGroupId("b", 443, ClientSocketPool::SocketType::kSsl); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5784 | |
| 5785 | ClientSocketHandle handle; |
| 5786 | TestCompletionCallback callback; |
| 5787 | EXPECT_THAT( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5788 | handle.Init(kOtherGroupId, params_, base::nullopt, DEFAULT_PRIORITY, |
| 5789 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5790 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5791 | pool_.get(), NetLogWithSource()), |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5792 | IsOk()); |
| 5793 | ASSERT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 5794 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kOtherGroupId)); |
| 5795 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5796 | pool_->OnSSLConfigForServerChanged(HostPortPair("a", 443)); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5797 | |
| 5798 | handle.Reset(); |
| 5799 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 5800 | ASSERT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 5801 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(kOtherGroupId)); |
| 5802 | } |
| 5803 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5804 | TEST_P(ClientSocketPoolBaseRefreshTest, |
| 5805 | RefreshGroupReplacesBoundConnectJobOnConnect) { |
| 5806 | CreatePoolForRefresh(1, 1); |
| 5807 | const ClientSocketPool::GroupId kGroupId = GetGroupId(); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5808 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5809 | |
| 5810 | TestAuthHelper auth_helper; |
| 5811 | auth_helper.InitHandle(params_, pool_.get(), DEFAULT_PRIORITY, |
| 5812 | ClientSocketPool::RespectLimits::ENABLED, kGroupId); |
| 5813 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(kGroupId)); |
| 5814 | |
| 5815 | auth_helper.WaitForAuth(); |
| 5816 | |
| 5817 | // This should update the generation, but not cancel the old ConnectJob - it's |
| 5818 | // not safe to do anything while waiting on the original ConnectJob. |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5819 | OnSSLConfigForServerChanged(); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5820 | |
| 5821 | // Providing auth credentials and restarting the request with them will cause |
| 5822 | // the ConnectJob to complete successfully, but the result will be discarded |
| 5823 | // because of the generation mismatch. |
| 5824 | auth_helper.RestartWithAuth(); |
| 5825 | |
| 5826 | // Despite using ConnectJobs that simulate a single challenge, a second |
| 5827 | // challenge will be seen, due to using a new ConnectJob. |
| 5828 | auth_helper.WaitForAuth(); |
| 5829 | auth_helper.RestartWithAuth(); |
| 5830 | |
| 5831 | EXPECT_THAT(auth_helper.WaitForResult(), IsOk()); |
| 5832 | EXPECT_TRUE(auth_helper.handle()->socket()); |
| 5833 | EXPECT_EQ(2, auth_helper.auth_count()); |
| 5834 | |
| 5835 | // When released, the socket will be returned to the socket pool, and |
| 5836 | // available for reuse. |
| 5837 | auth_helper.handle()->Reset(); |
| 5838 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 5839 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId)); |
| 5840 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(kGroupId)); |
| 5841 | } |
| 5842 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5843 | TEST_F(ClientSocketPoolBaseTest, RefreshProxyRefreshesAllGroups) { |
| 5844 | CreatePoolWithIdleTimeouts(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 5845 | kUnusedIdleSocketTimeout, |
| 5846 | ClientSocketPool::used_idle_socket_timeout(), |
| 5847 | false /* no backup connect jobs */, |
| 5848 | ProxyServer::FromPacString("HTTPS myproxy:70")); |
| 5849 | |
| 5850 | const ClientSocketPool::GroupId kGroupId1 = |
| 5851 | TestGroupId("a", 443, ClientSocketPool::SocketType::kSsl); |
| 5852 | const ClientSocketPool::GroupId kGroupId2 = |
| 5853 | TestGroupId("b", 443, ClientSocketPool::SocketType::kSsl); |
| 5854 | const ClientSocketPool::GroupId kGroupId3 = |
| 5855 | TestGroupId("c", 443, ClientSocketPool::SocketType::kSsl); |
| 5856 | |
| 5857 | // Make three sockets in three different groups. The third socket is released |
| 5858 | // to the pool as idle. |
| 5859 | ClientSocketHandle handle1, handle2, handle3; |
| 5860 | TestCompletionCallback callback; |
| 5861 | EXPECT_THAT( |
| 5862 | handle1.Init(kGroupId1, params_, base::nullopt, DEFAULT_PRIORITY, |
| 5863 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5864 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5865 | pool_.get(), NetLogWithSource()), |
| 5866 | IsOk()); |
| 5867 | EXPECT_THAT( |
| 5868 | handle2.Init(kGroupId2, params_, base::nullopt, DEFAULT_PRIORITY, |
| 5869 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5870 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5871 | pool_.get(), NetLogWithSource()), |
| 5872 | IsOk()); |
| 5873 | EXPECT_THAT( |
| 5874 | handle3.Init(kGroupId3, params_, base::nullopt, DEFAULT_PRIORITY, |
| 5875 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5876 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5877 | pool_.get(), NetLogWithSource()), |
| 5878 | IsOk()); |
| 5879 | handle3.Reset(); |
| 5880 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId1)); |
| 5881 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId1)); |
| 5882 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId2)); |
| 5883 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId2)); |
| 5884 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId3)); |
| 5885 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(kGroupId3)); |
| 5886 | |
| 5887 | // Changes to some other proxy do not affect the pool. The idle socket remains |
| 5888 | // alive and closing |handle2| makes the socket available for the pool. |
| 5889 | pool_->OnSSLConfigForServerChanged(HostPortPair("someotherproxy", 70)); |
| 5890 | |
| 5891 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId1)); |
| 5892 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId1)); |
| 5893 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId2)); |
| 5894 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId2)); |
| 5895 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId3)); |
| 5896 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(kGroupId3)); |
| 5897 | |
| 5898 | handle2.Reset(); |
| 5899 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId2)); |
| 5900 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(kGroupId2)); |
| 5901 | |
| 5902 | // Changes to the matching proxy refreshes all groups. |
| 5903 | pool_->OnSSLConfigForServerChanged(HostPortPair("myproxy", 70)); |
| 5904 | |
| 5905 | // Idle sockets are closed. |
| 5906 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5907 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupId2)); |
| 5908 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupId3)); |
| 5909 | |
| 5910 | // The active socket, however, continues to be active. |
| 5911 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId1)); |
| 5912 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId1)); |
| 5913 | |
| 5914 | // Closing it does not make it available for the pool. |
| 5915 | handle1.Reset(); |
| 5916 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5917 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupId1)); |
| 5918 | } |
| 5919 | |
| 5920 | TEST_F(ClientSocketPoolBaseTest, RefreshBothPrivacyAndNormalSockets) { |
| 5921 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 5922 | |
| 5923 | const ClientSocketPool::GroupId kGroupId = |
| 5924 | TestGroupId("a", 443, ClientSocketPool::SocketType::kSsl, |
| 5925 | PrivacyMode::PRIVACY_MODE_DISABLED); |
| 5926 | const ClientSocketPool::GroupId kGroupIdPrivacy = |
| 5927 | TestGroupId("a", 443, ClientSocketPool::SocketType::kSsl, |
| 5928 | PrivacyMode::PRIVACY_MODE_ENABLED); |
| 5929 | const ClientSocketPool::GroupId kOtherGroupId = |
| 5930 | TestGroupId("b", 443, ClientSocketPool::SocketType::kSsl); |
| 5931 | |
| 5932 | // Make a socket in each groups. |
| 5933 | ClientSocketHandle handle1, handle2, handle3; |
| 5934 | TestCompletionCallback callback; |
| 5935 | EXPECT_THAT( |
| 5936 | handle1.Init(kGroupId, params_, base::nullopt, DEFAULT_PRIORITY, |
| 5937 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5938 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5939 | pool_.get(), NetLogWithSource()), |
| 5940 | IsOk()); |
| 5941 | EXPECT_THAT( |
| 5942 | handle2.Init(kGroupIdPrivacy, params_, base::nullopt, DEFAULT_PRIORITY, |
| 5943 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5944 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5945 | pool_.get(), NetLogWithSource()), |
| 5946 | IsOk()); |
| 5947 | EXPECT_THAT( |
| 5948 | handle3.Init(kOtherGroupId, params_, base::nullopt, DEFAULT_PRIORITY, |
| 5949 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5950 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5951 | pool_.get(), NetLogWithSource()), |
| 5952 | IsOk()); |
| 5953 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId)); |
| 5954 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId)); |
| 5955 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupIdPrivacy)); |
| 5956 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupIdPrivacy)); |
| 5957 | ASSERT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 5958 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kOtherGroupId)); |
| 5959 | |
| 5960 | pool_->OnSSLConfigForServerChanged(HostPortPair("a", 443)); |
| 5961 | |
| 5962 | // Active sockets continue to be active. |
| 5963 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId)); |
| 5964 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId)); |
| 5965 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupIdPrivacy)); |
| 5966 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupIdPrivacy)); |
| 5967 | ASSERT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 5968 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kOtherGroupId)); |
| 5969 | |
| 5970 | // Closing them leaves kOtherGroupId alone, but kGroupId and kGroupIdPrivacy |
| 5971 | // are unusable. |
| 5972 | handle1.Reset(); |
| 5973 | handle2.Reset(); |
| 5974 | handle3.Reset(); |
| 5975 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 5976 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupId)); |
| 5977 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupIdPrivacy)); |
| 5978 | EXPECT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 5979 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(kOtherGroupId)); |
| 5980 | } |
| 5981 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 5982 | } // namespace |
| 5983 | |
| 5984 | } // namespace net |