[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" |
| 12 | #include "base/bind_helpers.h" |
[email protected] | 2041cf34 | 2010-02-19 03:15:59 | [diff] [blame] | 13 | #include "base/callback.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 */, |
[email protected] | 0a0b768 | 2010-08-25 17:08:07 | [diff] [blame] | 244 | NetLog* /* net_log */, |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 245 | const NetLogSource& /*source*/) override { |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 246 | allocation_count_++; |
Helen Li | d5bb922 | 2018-04-12 15:33:09 | [diff] [blame] | 247 | return nullptr; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 248 | } |
| 249 | |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 250 | std::unique_ptr<SSLClientSocket> CreateSSLClientSocket( |
David Benjamin | 24725be | 2019-07-24 20:57:18 | [diff] [blame] | 251 | SSLClientContext* context, |
Matt Menke | 841fc41 | 2019-03-05 23:20:12 | [diff] [blame] | 252 | std::unique_ptr<StreamSocket> stream_socket, |
[email protected] | 4f4de7e6 | 2010-11-12 19:55:27 | [diff] [blame] | 253 | const HostPortPair& host_and_port, |
David Benjamin | 24725be | 2019-07-24 20:57:18 | [diff] [blame] | 254 | const SSLConfig& ssl_config) override { |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 255 | NOTIMPLEMENTED(); |
David Benjamin | 24725be | 2019-07-24 20:57:18 | [diff] [blame] | 256 | return nullptr; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 257 | } |
Matt Menke | fd95692 | 2019-02-04 23:44:03 | [diff] [blame] | 258 | |
Matt Menke | 52cd95a | 2019-02-08 06:16:27 | [diff] [blame] | 259 | std::unique_ptr<ProxyClientSocket> CreateProxyClientSocket( |
| 260 | std::unique_ptr<StreamSocket> stream_socket, |
| 261 | const std::string& user_agent, |
| 262 | const HostPortPair& endpoint, |
| 263 | const ProxyServer& proxy_server, |
| 264 | HttpAuthController* http_auth_controller, |
| 265 | bool tunnel, |
| 266 | bool using_spdy, |
| 267 | NextProto negotiated_protocol, |
| 268 | ProxyDelegate* proxy_delegate, |
Matt Menke | 52cd95a | 2019-02-08 06:16:27 | [diff] [blame] | 269 | const NetworkTrafficAnnotationTag& traffic_annotation) override { |
| 270 | NOTIMPLEMENTED(); |
| 271 | return nullptr; |
| 272 | } |
| 273 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 274 | void WaitForSignal(TestConnectJob* job) { waiting_jobs_.push_back(job); } |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 275 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 276 | void SignalJobs(); |
| 277 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 278 | void SignalJob(size_t job); |
| 279 | |
| 280 | void SetJobLoadState(size_t job, LoadState load_state); |
| 281 | |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 282 | // Sets the HasConnectionEstablished value of the specified job to true, |
| 283 | // without invoking the callback. |
| 284 | void SetJobHasEstablishedConnection(size_t job); |
| 285 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 286 | int allocation_count() const { return allocation_count_; } |
| 287 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 288 | private: |
| 289 | int allocation_count_; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 290 | std::vector<TestConnectJob*> waiting_jobs_; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 291 | }; |
| 292 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 293 | class TestConnectJob : public ConnectJob { |
| 294 | public: |
| 295 | enum JobType { |
| 296 | kMockJob, |
| 297 | kMockFailingJob, |
| 298 | kMockPendingJob, |
| 299 | kMockPendingFailingJob, |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 300 | kMockWaitingJob, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 301 | |
| 302 | // Certificate errors return a socket in addition to an error code. |
| 303 | kMockCertErrorJob, |
| 304 | kMockPendingCertErrorJob, |
| 305 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 306 | kMockAdditionalErrorStateJob, |
| 307 | kMockPendingAdditionalErrorStateJob, |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 308 | kMockUnreadDataJob, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 309 | |
| 310 | kMockAuthChallengeOnceJob, |
| 311 | kMockAuthChallengeTwiceJob, |
| 312 | kMockAuthChallengeOnceFailingJob, |
| 313 | kMockAuthChallengeTwiceFailingJob, |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 314 | }; |
| 315 | |
[email protected] | 994d493 | 2010-07-12 17:55:13 | [diff] [blame] | 316 | // The kMockPendingJob uses a slight delay before allowing the connect |
| 317 | // to complete. |
| 318 | static const int kPendingConnectDelay = 2; |
| 319 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 320 | TestConnectJob(JobType job_type, |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 321 | RequestPriority request_priority, |
| 322 | SocketTag socket_tag, |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 323 | base::TimeDelta timeout_duration, |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 324 | const CommonConnectJobParams* common_connect_job_params, |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 325 | ConnectJob::Delegate* delegate, |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 326 | MockClientSocketFactory* client_socket_factory) |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 327 | : ConnectJob(request_priority, |
| 328 | socket_tag, |
Matt Menke | 1a6c92d | 2019-02-23 00:25:38 | [diff] [blame] | 329 | timeout_duration, |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 330 | common_connect_job_params, |
Matt Menke | 1a6c92d | 2019-02-23 00:25:38 | [diff] [blame] | 331 | delegate, |
| 332 | nullptr /* net_log */, |
| 333 | NetLogSourceType::TRANSPORT_CONNECT_JOB, |
| 334 | NetLogEventType::TRANSPORT_CONNECT_JOB_CONNECT), |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 335 | job_type_(job_type), |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 336 | client_socket_factory_(client_socket_factory), |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 337 | load_state_(LOAD_STATE_IDLE), |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 338 | has_established_connection_(false), |
Jeremy Roman | d54000b2 | 2019-07-08 18:40:16 | [diff] [blame] | 339 | store_additional_error_state_(false) {} |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 340 | |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 341 | void Signal() { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 342 | DoConnect(waiting_success_, true /* async */, false /* recoverable */); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 343 | } |
| 344 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 345 | void set_load_state(LoadState load_state) { load_state_ = load_state; } |
| 346 | |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 347 | void set_has_established_connection() { |
| 348 | DCHECK(!has_established_connection_); |
| 349 | has_established_connection_ = true; |
| 350 | } |
| 351 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 352 | // From ConnectJob: |
| 353 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 354 | LoadState GetLoadState() const override { return load_state_; } |
[email protected] | 4645135 | 2009-09-01 14:54:21 | [diff] [blame] | 355 | |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 356 | bool HasEstablishedConnection() const override { |
| 357 | return has_established_connection_; |
| 358 | } |
| 359 | |
dalyk | edd30d98 | 2019-12-16 15:31:10 | [diff] [blame] | 360 | ResolveErrorInfo GetResolveErrorInfo() const override { |
| 361 | return ResolveErrorInfo(OK); |
| 362 | } |
| 363 | |
Matt Menke | 6f84d1f1 | 2019-04-11 19:26:47 | [diff] [blame] | 364 | bool IsSSLError() const override { return store_additional_error_state_; } |
| 365 | |
| 366 | scoped_refptr<SSLCertRequestInfo> GetCertRequestInfo() override { |
| 367 | if (store_additional_error_state_) |
| 368 | return base::MakeRefCounted<SSLCertRequestInfo>(); |
| 369 | return nullptr; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 370 | } |
| 371 | |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 372 | private: |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 373 | // From ConnectJob: |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 374 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 375 | int ConnectInternal() override { |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 376 | AddressList ignored; |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 377 | client_socket_factory_->CreateTransportClientSocket( |
| 378 | ignored, nullptr, nullptr, NetLogSource()); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 379 | switch (job_type_) { |
| 380 | case kMockJob: |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 381 | return DoConnect(true /* successful */, false /* sync */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 382 | false /* cert_error */); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 383 | case kMockFailingJob: |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 384 | return DoConnect(false /* error */, false /* sync */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 385 | false /* cert_error */); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 386 | case kMockPendingJob: |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 387 | set_load_state(LOAD_STATE_CONNECTING); |
[email protected] | 6b17538 | 2009-10-13 06:47:47 | [diff] [blame] | 388 | |
| 389 | // Depending on execution timings, posting a delayed task can result |
| 390 | // in the task getting executed the at the earliest possible |
| 391 | // opportunity or only after returning once from the message loop and |
| 392 | // then a second call into the message loop. In order to make behavior |
| 393 | // more deterministic, we change the default delay to 2ms. This should |
| 394 | // always require us to wait for the second call into the message loop. |
| 395 | // |
| 396 | // N.B. The correct fix for this and similar timing problems is to |
| 397 | // abstract time for the purpose of unittests. Unfortunately, we have |
| 398 | // a lot of third-party components that directly call the various |
| 399 | // time functions, so this change would be rather invasive. |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 400 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 401 | FROM_HERE, |
kylechar | f4fe517 | 2019-02-15 18:53:49 | [diff] [blame] | 402 | base::BindOnce(base::IgnoreResult(&TestConnectJob::DoConnect), |
| 403 | weak_factory_.GetWeakPtr(), true /* successful */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 404 | true /* async */, false /* cert_error */), |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 405 | base::TimeDelta::FromMilliseconds(kPendingConnectDelay)); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 406 | return ERR_IO_PENDING; |
| 407 | case kMockPendingFailingJob: |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 408 | set_load_state(LOAD_STATE_CONNECTING); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 409 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 410 | FROM_HERE, |
kylechar | f4fe517 | 2019-02-15 18:53:49 | [diff] [blame] | 411 | base::BindOnce(base::IgnoreResult(&TestConnectJob::DoConnect), |
| 412 | weak_factory_.GetWeakPtr(), false /* error */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 413 | true /* async */, false /* cert_error */), |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 414 | base::TimeDelta::FromMilliseconds(2)); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 415 | return ERR_IO_PENDING; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 416 | case kMockWaitingJob: |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 417 | set_load_state(LOAD_STATE_CONNECTING); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 418 | client_socket_factory_->WaitForSignal(this); |
| 419 | waiting_success_ = true; |
| 420 | return ERR_IO_PENDING; |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 421 | case kMockCertErrorJob: |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 422 | return DoConnect(false /* error */, false /* sync */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 423 | true /* cert_error */); |
| 424 | case kMockPendingCertErrorJob: |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 425 | set_load_state(LOAD_STATE_CONNECTING); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 426 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 427 | FROM_HERE, |
kylechar | f4fe517 | 2019-02-15 18:53:49 | [diff] [blame] | 428 | base::BindOnce(base::IgnoreResult(&TestConnectJob::DoConnect), |
| 429 | weak_factory_.GetWeakPtr(), false /* error */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 430 | true /* async */, true /* cert_error */), |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 431 | base::TimeDelta::FromMilliseconds(2)); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 432 | return ERR_IO_PENDING; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 433 | case kMockAdditionalErrorStateJob: |
| 434 | store_additional_error_state_ = true; |
| 435 | return DoConnect(false /* error */, false /* sync */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 436 | false /* cert_error */); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 437 | case kMockPendingAdditionalErrorStateJob: |
| 438 | set_load_state(LOAD_STATE_CONNECTING); |
| 439 | store_additional_error_state_ = true; |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 440 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 441 | FROM_HERE, |
kylechar | f4fe517 | 2019-02-15 18:53:49 | [diff] [blame] | 442 | base::BindOnce(base::IgnoreResult(&TestConnectJob::DoConnect), |
| 443 | weak_factory_.GetWeakPtr(), false /* error */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 444 | true /* async */, false /* cert_error */), |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 445 | base::TimeDelta::FromMilliseconds(2)); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 446 | return ERR_IO_PENDING; |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 447 | case kMockUnreadDataJob: { |
| 448 | int ret = DoConnect(true /* successful */, false /* sync */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 449 | false /* cert_error */); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 450 | static_cast<MockClientSocket*>(socket())->set_has_unread_data(true); |
| 451 | return ret; |
| 452 | } |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 453 | case kMockAuthChallengeOnceJob: |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 454 | set_load_state(LOAD_STATE_CONNECTING); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 455 | DoAdvanceAuthChallenge(1, true /* succeed_after_last_challenge */); |
| 456 | return ERR_IO_PENDING; |
| 457 | case kMockAuthChallengeTwiceJob: |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 458 | set_load_state(LOAD_STATE_CONNECTING); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 459 | DoAdvanceAuthChallenge(2, true /* succeed_after_last_challenge */); |
| 460 | return ERR_IO_PENDING; |
| 461 | case kMockAuthChallengeOnceFailingJob: |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 462 | set_load_state(LOAD_STATE_CONNECTING); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 463 | DoAdvanceAuthChallenge(1, false /* succeed_after_last_challenge */); |
| 464 | return ERR_IO_PENDING; |
| 465 | case kMockAuthChallengeTwiceFailingJob: |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 466 | set_load_state(LOAD_STATE_CONNECTING); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 467 | DoAdvanceAuthChallenge(2, false /* succeed_after_last_challenge */); |
| 468 | return ERR_IO_PENDING; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 469 | default: |
| 470 | NOTREACHED(); |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 471 | SetSocket(std::unique_ptr<StreamSocket>()); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 472 | return ERR_FAILED; |
| 473 | } |
| 474 | } |
| 475 | |
Lily Chen | 02ef29a | 2018-11-30 16:31:43 | [diff] [blame] | 476 | void ChangePriorityInternal(RequestPriority priority) override {} |
| 477 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 478 | int DoConnect(bool succeed, bool was_async, bool cert_error) { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 479 | int result = OK; |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 480 | has_established_connection_ = true; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 481 | if (succeed) { |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 482 | SetSocket(std::make_unique<MockClientSocket>(net_log().net_log())); |
Bence Béky | bdbb0e7 | 2018-08-07 21:42:59 | [diff] [blame] | 483 | socket()->Connect(CompletionOnceCallback()); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 484 | } else if (cert_error) { |
| 485 | SetSocket(std::make_unique<MockClientSocket>(net_log().net_log())); |
| 486 | result = ERR_CERT_COMMON_NAME_INVALID; |
[email protected] | 6e713f0 | 2009-08-06 02:56:40 | [diff] [blame] | 487 | } else { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 488 | result = ERR_CONNECTION_FAILED; |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 489 | SetSocket(std::unique_ptr<StreamSocket>()); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 490 | } |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 491 | |
| 492 | if (was_async) |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 493 | NotifyDelegateOfCompletion(result); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 494 | return result; |
| 495 | } |
| 496 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 497 | void DoAdvanceAuthChallenge(int remaining_challenges, |
| 498 | bool succeed_after_last_challenge) { |
| 499 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 500 | FROM_HERE, |
| 501 | base::BindOnce(&TestConnectJob::InvokeNextProxyAuthCallback, |
| 502 | weak_factory_.GetWeakPtr(), remaining_challenges, |
| 503 | succeed_after_last_challenge)); |
| 504 | } |
| 505 | |
| 506 | void InvokeNextProxyAuthCallback(int remaining_challenges, |
| 507 | bool succeed_after_last_challenge) { |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 508 | set_load_state(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 509 | if (remaining_challenges == 0) { |
| 510 | DoConnect(succeed_after_last_challenge, true /* was_async */, |
| 511 | false /* cert_error */); |
| 512 | return; |
| 513 | } |
| 514 | |
| 515 | // Integration tests make sure HttpResponseInfo and HttpAuthController work. |
| 516 | // The auth tests here are just focused on ConnectJob bookkeeping. |
| 517 | HttpResponseInfo info; |
| 518 | NotifyDelegateOfProxyAuth( |
| 519 | info, nullptr /* http_auth_controller */, |
| 520 | base::BindOnce(&TestConnectJob::DoAdvanceAuthChallenge, |
| 521 | weak_factory_.GetWeakPtr(), remaining_challenges - 1, |
| 522 | succeed_after_last_challenge)); |
| 523 | } |
| 524 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 525 | bool waiting_success_; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 526 | const JobType job_type_; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 527 | MockClientSocketFactory* const client_socket_factory_; |
[email protected] | 4645135 | 2009-09-01 14:54:21 | [diff] [blame] | 528 | LoadState load_state_; |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 529 | bool has_established_connection_; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 530 | bool store_additional_error_state_; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 531 | |
Jeremy Roman | d54000b2 | 2019-07-08 18:40:16 | [diff] [blame] | 532 | base::WeakPtrFactory<TestConnectJob> weak_factory_{this}; |
[email protected] | d5492c5 | 2013-11-10 20:44:39 | [diff] [blame] | 533 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 534 | DISALLOW_COPY_AND_ASSIGN(TestConnectJob); |
| 535 | }; |
| 536 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 537 | class TestConnectJobFactory |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 538 | : public TransportClientSocketPool::ConnectJobFactory { |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 539 | public: |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 540 | TestConnectJobFactory(MockClientSocketFactory* client_socket_factory, |
| 541 | NetLog* net_log) |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 542 | : common_connect_job_params_( |
| 543 | nullptr /* client_socket_factory */, |
| 544 | nullptr /* host_resolver */, |
Matt Menke | b88837e | 2019-03-20 11:50:40 | [diff] [blame] | 545 | nullptr /* http_auth_cache */, |
| 546 | nullptr /* http_auth_handler_factory */, |
| 547 | nullptr /* spdy_session_pool */, |
Matt Menke | b5fb42b | 2019-03-22 17:26:13 | [diff] [blame] | 548 | nullptr /* quic_supported_versions */, |
Matt Menke | b88837e | 2019-03-20 11:50:40 | [diff] [blame] | 549 | nullptr /* quic_stream_factory */, |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 550 | nullptr /* proxy_delegate */, |
Matt Menke | d732ea4 | 2019-03-08 12:05:00 | [diff] [blame] | 551 | nullptr /* http_user_agent_settings */, |
David Benjamin | 24725be | 2019-07-24 20:57:18 | [diff] [blame] | 552 | nullptr /* ssl_client_context */, |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 553 | nullptr /* socket_performance_watcher_factory */, |
| 554 | nullptr /* network_quality_estimator */, |
| 555 | net_log, |
| 556 | nullptr /* websocket_endpoint_lock_manager */), |
| 557 | job_type_(TestConnectJob::kMockJob), |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 558 | job_types_(nullptr), |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 559 | client_socket_factory_(client_socket_factory) {} |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 560 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 561 | ~TestConnectJobFactory() override = default; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 562 | |
| 563 | void set_job_type(TestConnectJob::JobType job_type) { job_type_ = job_type; } |
| 564 | |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 565 | void set_job_types(std::list<TestConnectJob::JobType>* job_types) { |
| 566 | job_types_ = job_types; |
| 567 | CHECK(!job_types_->empty()); |
| 568 | } |
| 569 | |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 570 | void set_timeout_duration(base::TimeDelta timeout_duration) { |
| 571 | timeout_duration_ = timeout_duration; |
| 572 | } |
| 573 | |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 574 | // ConnectJobFactory implementation. |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 575 | |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 576 | std::unique_ptr<ConnectJob> NewConnectJob( |
Matt Menke | aafff54 | 2019-04-22 22:09:36 | [diff] [blame] | 577 | ClientSocketPool::GroupId group_id, |
| 578 | scoped_refptr<ClientSocketPool::SocketParams> socket_params, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 579 | const base::Optional<NetworkTrafficAnnotationTag>& proxy_annotation_tag, |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 580 | RequestPriority request_priority, |
| 581 | SocketTag socket_tag, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 582 | ConnectJob::Delegate* delegate) const override { |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 583 | EXPECT_TRUE(!job_types_ || !job_types_->empty()); |
| 584 | TestConnectJob::JobType job_type = job_type_; |
| 585 | if (job_types_ && !job_types_->empty()) { |
| 586 | job_type = job_types_->front(); |
| 587 | job_types_->pop_front(); |
| 588 | } |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 589 | return std::make_unique<TestConnectJob>( |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 590 | job_type, request_priority, socket_tag, timeout_duration_, |
| 591 | &common_connect_job_params_, delegate, client_socket_factory_); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | private: |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 595 | const CommonConnectJobParams common_connect_job_params_; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 596 | TestConnectJob::JobType job_type_; |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 597 | std::list<TestConnectJob::JobType>* job_types_; |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 598 | base::TimeDelta timeout_duration_; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 599 | MockClientSocketFactory* const client_socket_factory_; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 600 | |
| 601 | DISALLOW_COPY_AND_ASSIGN(TestConnectJobFactory); |
| 602 | }; |
| 603 | |
[email protected] | a937a06d | 2009-08-19 21:19:24 | [diff] [blame] | 604 | } // namespace |
| 605 | |
[email protected] | a937a06d | 2009-08-19 21:19:24 | [diff] [blame] | 606 | namespace { |
| 607 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 608 | void MockClientSocketFactory::SignalJobs() { |
jdoerrie | 22a91d8b9 | 2018-10-05 08:43:26 | [diff] [blame] | 609 | for (auto it = waiting_jobs_.begin(); it != waiting_jobs_.end(); ++it) { |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 610 | (*it)->Signal(); |
| 611 | } |
| 612 | waiting_jobs_.clear(); |
| 613 | } |
| 614 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 615 | void MockClientSocketFactory::SignalJob(size_t job) { |
| 616 | ASSERT_LT(job, waiting_jobs_.size()); |
| 617 | waiting_jobs_[job]->Signal(); |
| 618 | waiting_jobs_.erase(waiting_jobs_.begin() + job); |
| 619 | } |
| 620 | |
| 621 | void MockClientSocketFactory::SetJobLoadState(size_t job, |
| 622 | LoadState load_state) { |
| 623 | ASSERT_LT(job, waiting_jobs_.size()); |
| 624 | waiting_jobs_[job]->set_load_state(load_state); |
| 625 | } |
| 626 | |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 627 | void MockClientSocketFactory::SetJobHasEstablishedConnection(size_t job) { |
| 628 | ASSERT_LT(job, waiting_jobs_.size()); |
| 629 | waiting_jobs_[job]->set_has_established_connection(); |
| 630 | } |
| 631 | |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 632 | class ClientSocketPoolBaseTest : public TestWithTaskEnvironment { |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 633 | protected: |
Alex Clarke | 0def209 | 2018-12-10 12:01:45 | [diff] [blame] | 634 | ClientSocketPoolBaseTest() |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 635 | : TestWithTaskEnvironment( |
| 636 | base::test::TaskEnvironment::TimeSource::MOCK_TIME), |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 637 | params_(ClientSocketPool::SocketParams::CreateForHttpForTesting()) { |
[email protected] | 636b825 | 2011-04-08 19:56:54 | [diff] [blame] | 638 | connect_backup_jobs_enabled_ = |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 639 | TransportClientSocketPool::connect_backup_jobs_enabled(); |
| 640 | TransportClientSocketPool::set_connect_backup_jobs_enabled(true); |
[email protected] | 636b825 | 2011-04-08 19:56:54 | [diff] [blame] | 641 | } |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 642 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 643 | ~ClientSocketPoolBaseTest() override { |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 644 | TransportClientSocketPool::set_connect_backup_jobs_enabled( |
[email protected] | 636b825 | 2011-04-08 19:56:54 | [diff] [blame] | 645 | connect_backup_jobs_enabled_); |
| 646 | } |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 647 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 648 | void CreatePool(int max_sockets, |
| 649 | int max_sockets_per_group, |
| 650 | bool enable_backup_connect_jobs = false) { |
Tarun Bansal | a763509 | 2019-02-20 10:00:59 | [diff] [blame] | 651 | CreatePoolWithIdleTimeouts(max_sockets, max_sockets_per_group, |
| 652 | kUnusedIdleSocketTimeout, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 653 | ClientSocketPool::used_idle_socket_timeout(), |
| 654 | enable_backup_connect_jobs); |
[email protected] | 9bf28db | 2009-08-29 01:35:16 | [diff] [blame] | 655 | } |
| 656 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 657 | void CreatePoolWithIdleTimeouts( |
| 658 | int max_sockets, |
| 659 | int max_sockets_per_group, |
| 660 | base::TimeDelta unused_idle_socket_timeout, |
| 661 | base::TimeDelta used_idle_socket_timeout, |
| 662 | bool enable_backup_connect_jobs = false, |
| 663 | ProxyServer proxy_server = ProxyServer::Direct()) { |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 664 | DCHECK(!pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 665 | std::unique_ptr<TestConnectJobFactory> connect_job_factory = |
| 666 | std::make_unique<TestConnectJobFactory>(&client_socket_factory_, |
| 667 | &net_log_); |
| 668 | connect_job_factory_ = connect_job_factory.get(); |
| 669 | pool_ = TransportClientSocketPool::CreateForTesting( |
| 670 | max_sockets, max_sockets_per_group, unused_idle_socket_timeout, |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 671 | used_idle_socket_timeout, proxy_server, std::move(connect_job_factory), |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 672 | nullptr /* ssl_config_service */, enable_backup_connect_jobs); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 673 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 674 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 675 | int StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 676 | const ClientSocketPool::GroupId& group_id, |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 677 | RequestPriority priority, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 678 | ClientSocketPool::RespectLimits respect_limits) { |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 679 | return test_base_.StartRequestUsingPool(pool_.get(), group_id, priority, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 680 | respect_limits, params_); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 681 | } |
| 682 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 683 | int StartRequest(const ClientSocketPool::GroupId& group_id, |
| 684 | RequestPriority priority) { |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 685 | return StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 686 | group_id, priority, ClientSocketPool::RespectLimits::ENABLED); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 687 | } |
| 688 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 689 | int GetOrderOfRequest(size_t index) const { |
| 690 | return test_base_.GetOrderOfRequest(index); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 691 | } |
| 692 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 693 | bool ReleaseOneConnection(ClientSocketPoolTest::KeepAlive keep_alive) { |
| 694 | return test_base_.ReleaseOneConnection(keep_alive); |
| 695 | } |
| 696 | |
| 697 | void ReleaseAllConnections(ClientSocketPoolTest::KeepAlive keep_alive) { |
| 698 | test_base_.ReleaseAllConnections(keep_alive); |
| 699 | } |
| 700 | |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 701 | // Expects a single NetLogEventType::SOCKET_POOL_CLOSING_SOCKET in |net_log_|. |
| 702 | // It should be logged for the provided source and have the indicated reason. |
| 703 | void ExpectSocketClosedWithReason(NetLogSource expected_source, |
| 704 | const char* expected_reason) { |
| 705 | auto entries = net_log_.GetEntriesForSourceWithType( |
| 706 | expected_source, NetLogEventType::SOCKET_POOL_CLOSING_SOCKET, |
| 707 | NetLogEventPhase::NONE); |
| 708 | ASSERT_EQ(1u, entries.size()); |
| 709 | ASSERT_TRUE(entries[0].HasParams()); |
| 710 | ASSERT_TRUE(entries[0].params.is_dict()); |
| 711 | const std::string* reason = entries[0].params.FindStringKey("reason"); |
| 712 | ASSERT_TRUE(reason); |
| 713 | EXPECT_EQ(expected_reason, *reason); |
| 714 | } |
| 715 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 716 | TestSocketRequest* request(int i) { return test_base_.request(i); } |
| 717 | size_t requests_size() const { return test_base_.requests_size(); } |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 718 | std::vector<std::unique_ptr<TestSocketRequest>>* requests() { |
olli.raula | 9d66b7d | 2015-11-23 08:30:42 | [diff] [blame] | 719 | return test_base_.requests(); |
| 720 | } |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 721 | // Only counts the requests that get sockets asynchronously; |
| 722 | // synchronous completions are not registered by this count. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 723 | size_t completion_count() const { return test_base_.completion_count(); } |
| 724 | |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 725 | RecordingTestNetLog net_log_; |
[email protected] | 636b825 | 2011-04-08 19:56:54 | [diff] [blame] | 726 | bool connect_backup_jobs_enabled_; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 727 | MockClientSocketFactory client_socket_factory_; |
[email protected] | 17a0c6c | 2009-08-04 00:07:04 | [diff] [blame] | 728 | TestConnectJobFactory* connect_job_factory_; |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 729 | // These parameters are never actually used to create a TransportConnectJob. |
Matt Menke | 84d11e56 | 2019-03-27 00:11:19 | [diff] [blame] | 730 | scoped_refptr<ClientSocketPool::SocketParams> params_; |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 731 | std::unique_ptr<TransportClientSocketPool> pool_; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 732 | ClientSocketPoolTest test_base_; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 733 | }; |
| 734 | |
Shivani Sharma | 8ae506c | 2019-07-21 21:08:27 | [diff] [blame] | 735 | // TODO(950069): Add testing for frame_origin in NetworkIsolationKey |
| 736 | // using kAppendInitiatingFrameOriginToNetworkIsolationKey. |
| 737 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 738 | TEST_F(ClientSocketPoolBaseTest, BasicSynchronous) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 739 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 740 | |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 741 | TestCompletionCallback callback; |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 742 | ClientSocketHandle handle; |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 743 | RecordingBoundTestNetLog log; |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 744 | TestLoadTimingInfoNotConnected(handle); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 745 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 746 | EXPECT_EQ(OK, handle.Init( |
| 747 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 748 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 749 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 750 | pool_.get(), log.bound())); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 751 | EXPECT_TRUE(handle.is_initialized()); |
| 752 | EXPECT_TRUE(handle.socket()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 753 | TestLoadTimingInfoConnectedNotReused(handle); |
| 754 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 755 | handle.Reset(); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 756 | TestLoadTimingInfoNotConnected(handle); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 757 | |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 758 | auto entries = log.GetEntries(); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 759 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 760 | EXPECT_EQ(5u, entries.size()); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 761 | EXPECT_TRUE(LogContainsEvent( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 762 | entries, 0, NetLogEventType::TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 763 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 764 | EXPECT_TRUE(LogContainsBeginEvent(entries, 1, NetLogEventType::SOCKET_POOL)); |
| 765 | EXPECT_TRUE(LogContainsEvent( |
| 766 | entries, 2, NetLogEventType::SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
| 767 | NetLogEventPhase::NONE)); |
| 768 | EXPECT_TRUE(LogContainsEvent(entries, 3, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 769 | NetLogEventType::SOCKET_POOL_BOUND_TO_SOCKET, |
| 770 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 771 | EXPECT_TRUE(LogContainsEndEvent(entries, 4, NetLogEventType::SOCKET_POOL)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 772 | } |
| 773 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 774 | TEST_F(ClientSocketPoolBaseTest, InitConnectionFailure) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 775 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 776 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 777 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 778 | RecordingBoundTestNetLog log; |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 779 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 780 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 781 | TestCompletionCallback callback; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 782 | // Set the additional error state members to ensure that they get cleared. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 783 | handle.set_is_ssl_error(true); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 784 | handle.set_ssl_cert_request_info(base::MakeRefCounted<SSLCertRequestInfo>()); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 785 | EXPECT_EQ( |
| 786 | ERR_CONNECTION_FAILED, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 787 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 788 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 789 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 790 | pool_.get(), log.bound())); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 791 | EXPECT_FALSE(handle.socket()); |
| 792 | EXPECT_FALSE(handle.is_ssl_error()); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 793 | EXPECT_FALSE(handle.ssl_cert_request_info()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 794 | TestLoadTimingInfoNotConnected(handle); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 795 | |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 796 | auto entries = log.GetEntries(); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 797 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 798 | EXPECT_EQ(4u, entries.size()); |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 799 | EXPECT_TRUE(LogContainsEvent( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 800 | entries, 0, NetLogEventType::TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 801 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 802 | EXPECT_TRUE(LogContainsBeginEvent(entries, 1, NetLogEventType::SOCKET_POOL)); |
| 803 | EXPECT_TRUE(LogContainsEvent( |
| 804 | entries, 2, NetLogEventType::SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
| 805 | NetLogEventPhase::NONE)); |
| 806 | EXPECT_TRUE(LogContainsEndEvent(entries, 3, NetLogEventType::SOCKET_POOL)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 807 | } |
| 808 | |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 809 | // Test releasing an open socket into the socket pool, telling the socket pool |
| 810 | // to close the socket. |
| 811 | TEST_F(ClientSocketPoolBaseTest, ReleaseAndCloseConnection) { |
| 812 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 813 | |
| 814 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 815 | ASSERT_TRUE(request(0)->handle()->socket()); |
| 816 | net::NetLogSource source = request(0)->handle()->socket()->NetLog().source(); |
| 817 | ReleaseOneConnection(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 818 | |
| 819 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 820 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 821 | |
| 822 | ExpectSocketClosedWithReason( |
| 823 | source, TransportClientSocketPool::kClosedConnectionReturnedToPool); |
| 824 | } |
| 825 | |
| 826 | TEST_F(ClientSocketPoolBaseTest, SocketWithUnreadDataReturnedToPool) { |
| 827 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 828 | connect_job_factory_->set_job_type(TestConnectJob::kMockUnreadDataJob); |
| 829 | |
| 830 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 831 | ASSERT_TRUE(request(0)->handle()->socket()); |
| 832 | net::NetLogSource source = request(0)->handle()->socket()->NetLog().source(); |
| 833 | EXPECT_TRUE(request(0)->handle()->socket()->IsConnected()); |
| 834 | EXPECT_FALSE(request(0)->handle()->socket()->IsConnectedAndIdle()); |
| 835 | ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE); |
| 836 | |
| 837 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 838 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 839 | |
| 840 | ExpectSocketClosedWithReason( |
| 841 | source, TransportClientSocketPool::kDataReceivedUnexpectedly); |
| 842 | } |
| 843 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 844 | // Make sure different groups do not share sockets. |
| 845 | TEST_F(ClientSocketPoolBaseTest, GroupSeparation) { |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 846 | base::test::ScopedFeatureList feature_list; |
| 847 | feature_list.InitAndEnableFeature( |
| 848 | features::kPartitionConnectionsByNetworkIsolationKey); |
| 849 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 850 | CreatePool(1000 /* max_sockets */, 2 /* max_sockets_per_group */); |
| 851 | |
| 852 | const HostPortPair kHostPortPairs[] = { |
| 853 | {"a", 80}, |
| 854 | {"a", 443}, |
| 855 | {"b", 80}, |
| 856 | }; |
| 857 | |
| 858 | const ClientSocketPool::SocketType kSocketTypes[] = { |
| 859 | ClientSocketPool::SocketType::kHttp, |
| 860 | ClientSocketPool::SocketType::kSsl, |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 861 | }; |
| 862 | |
Matt Menke | bdf77780 | 2019-04-22 19:38:59 | [diff] [blame] | 863 | const PrivacyMode kPrivacyModes[] = {PrivacyMode::PRIVACY_MODE_DISABLED, |
| 864 | PrivacyMode::PRIVACY_MODE_ENABLED}; |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 865 | |
Shivani Sharma | 8ae506c | 2019-07-21 21:08:27 | [diff] [blame] | 866 | const auto kOriginA = url::Origin::Create(GURL("http://a.test/")); |
| 867 | const auto kOriginB = url::Origin::Create(GURL("http://b.test/")); |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 868 | const NetworkIsolationKey kNetworkIsolationKeys[] = { |
Shivani Sharma | 8ae506c | 2019-07-21 21:08:27 | [diff] [blame] | 869 | NetworkIsolationKey(kOriginA, kOriginA), |
| 870 | NetworkIsolationKey(kOriginB, kOriginB), |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 871 | }; |
| 872 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 873 | const bool kDisableSecureDnsValues[] = {false, true}; |
| 874 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 875 | int total_idle_sockets = 0; |
| 876 | |
| 877 | // Walk through each GroupId, making sure that requesting a socket for one |
| 878 | // group does not return a previously connected socket for another group. |
| 879 | for (const auto& host_port_pair : kHostPortPairs) { |
| 880 | SCOPED_TRACE(host_port_pair.ToString()); |
| 881 | for (const auto& socket_type : kSocketTypes) { |
| 882 | SCOPED_TRACE(static_cast<int>(socket_type)); |
| 883 | for (const auto& privacy_mode : kPrivacyModes) { |
| 884 | SCOPED_TRACE(privacy_mode); |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 885 | for (const auto& network_isolation_key : kNetworkIsolationKeys) { |
| 886 | SCOPED_TRACE(network_isolation_key.ToString()); |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 887 | for (const auto& disable_secure_dns : kDisableSecureDnsValues) { |
| 888 | SCOPED_TRACE(disable_secure_dns); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 889 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 890 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 891 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 892 | ClientSocketPool::GroupId group_id( |
| 893 | host_port_pair, socket_type, privacy_mode, |
| 894 | network_isolation_key, disable_secure_dns); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 895 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 896 | EXPECT_FALSE(pool_->HasGroupForTesting(group_id)); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 897 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 898 | TestCompletionCallback callback; |
| 899 | ClientSocketHandle handle; |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 900 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 901 | // Since the group is empty, requesting a socket should not complete |
| 902 | // synchronously. |
| 903 | EXPECT_THAT(handle.Init(group_id, params_, base::nullopt, |
| 904 | DEFAULT_PRIORITY, SocketTag(), |
| 905 | ClientSocketPool::RespectLimits::ENABLED, |
| 906 | callback.callback(), |
| 907 | ClientSocketPool::ProxyAuthCallback(), |
| 908 | pool_.get(), NetLogWithSource()), |
| 909 | IsError(ERR_IO_PENDING)); |
| 910 | EXPECT_TRUE(pool_->HasGroupForTesting(group_id)); |
| 911 | EXPECT_EQ(total_idle_sockets, pool_->IdleSocketCount()); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 912 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 913 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 914 | EXPECT_TRUE(handle.socket()); |
| 915 | EXPECT_TRUE(pool_->HasGroupForTesting(group_id)); |
| 916 | EXPECT_EQ(total_idle_sockets, pool_->IdleSocketCount()); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 917 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 918 | // Return socket to pool. |
| 919 | handle.Reset(); |
| 920 | EXPECT_EQ(total_idle_sockets + 1, pool_->IdleSocketCount()); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 921 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 922 | // Requesting a socket again should return the same socket as |
| 923 | // before, so should complete synchronously. |
| 924 | EXPECT_THAT(handle.Init(group_id, params_, base::nullopt, |
| 925 | DEFAULT_PRIORITY, SocketTag(), |
| 926 | ClientSocketPool::RespectLimits::ENABLED, |
| 927 | callback.callback(), |
| 928 | ClientSocketPool::ProxyAuthCallback(), |
| 929 | pool_.get(), NetLogWithSource()), |
| 930 | IsOk()); |
| 931 | EXPECT_TRUE(handle.socket()); |
| 932 | EXPECT_EQ(total_idle_sockets, pool_->IdleSocketCount()); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 933 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 934 | // Return socket to pool again. |
| 935 | handle.Reset(); |
| 936 | EXPECT_EQ(total_idle_sockets + 1, pool_->IdleSocketCount()); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 937 | |
dalyk | 5f48a13 | 2019-10-14 15:20:19 | [diff] [blame] | 938 | ++total_idle_sockets; |
| 939 | } |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 940 | } |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 941 | } |
| 942 | } |
| 943 | } |
| 944 | } |
| 945 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 946 | TEST_F(ClientSocketPoolBaseTest, TotalLimit) { |
| 947 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 948 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 949 | // TODO(eroman): Check that the NetLog contains this event. |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 950 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 951 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 952 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
| 953 | EXPECT_THAT(StartRequest(TestGroupId("c"), DEFAULT_PRIORITY), IsOk()); |
| 954 | EXPECT_THAT(StartRequest(TestGroupId("d"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 955 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 956 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 957 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 958 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 959 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 960 | EXPECT_THAT(StartRequest(TestGroupId("e"), DEFAULT_PRIORITY), |
| 961 | IsError(ERR_IO_PENDING)); |
| 962 | EXPECT_THAT(StartRequest(TestGroupId("f"), DEFAULT_PRIORITY), |
| 963 | IsError(ERR_IO_PENDING)); |
| 964 | EXPECT_THAT(StartRequest(TestGroupId("g"), DEFAULT_PRIORITY), |
| 965 | IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 966 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 967 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 968 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 969 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 970 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 971 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 972 | |
| 973 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 974 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 975 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 976 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 977 | EXPECT_EQ(5, GetOrderOfRequest(5)); |
| 978 | EXPECT_EQ(6, GetOrderOfRequest(6)); |
| 979 | EXPECT_EQ(7, GetOrderOfRequest(7)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 980 | |
| 981 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 982 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(8)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 983 | } |
| 984 | |
| 985 | TEST_F(ClientSocketPoolBaseTest, TotalLimitReachedNewGroup) { |
| 986 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 987 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 988 | // TODO(eroman): Check that the NetLog contains this event. |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 989 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 990 | // Reach all limits: max total sockets, and max sockets per group. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 991 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 992 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 993 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
| 994 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 995 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 996 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 997 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 998 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 999 | |
| 1000 | // Now create a new group and verify that we don't starve it. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1001 | EXPECT_THAT(StartRequest(TestGroupId("c"), DEFAULT_PRIORITY), |
| 1002 | IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1003 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1004 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1005 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1006 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1007 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1008 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1009 | |
| 1010 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1011 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1012 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 1013 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 1014 | EXPECT_EQ(5, GetOrderOfRequest(5)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1015 | |
| 1016 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1017 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(6)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1018 | } |
| 1019 | |
| 1020 | TEST_F(ClientSocketPoolBaseTest, TotalLimitRespectsPriority) { |
| 1021 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1022 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1023 | EXPECT_THAT(StartRequest(TestGroupId("b"), LOWEST), IsOk()); |
| 1024 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsOk()); |
| 1025 | EXPECT_THAT(StartRequest(TestGroupId("b"), HIGHEST), IsOk()); |
| 1026 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1027 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1028 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1029 | client_socket_factory_.allocation_count()); |
| 1030 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1031 | EXPECT_THAT(StartRequest(TestGroupId("c"), LOWEST), IsError(ERR_IO_PENDING)); |
| 1032 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1033 | EXPECT_THAT(StartRequest(TestGroupId("b"), HIGHEST), IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1034 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1035 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1036 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1037 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1038 | |
| 1039 | // First 4 requests don't have to wait, and finish in order. |
| 1040 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1041 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1042 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 1043 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 1044 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1045 | // Request ("b", HIGHEST) has the highest priority, then (TestGroupId("a"), |
| 1046 | // MEDIUM), and then ("c", LOWEST). |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1047 | EXPECT_EQ(7, GetOrderOfRequest(5)); |
| 1048 | EXPECT_EQ(6, GetOrderOfRequest(6)); |
| 1049 | EXPECT_EQ(5, GetOrderOfRequest(7)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1050 | |
| 1051 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1052 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(9)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1053 | } |
| 1054 | |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1055 | // Test reprioritizing a request before completion doesn't interfere with |
| 1056 | // its completion. |
| 1057 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeOne) { |
| 1058 | CreatePool(kDefaultMaxSockets, 1); |
| 1059 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1060 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1061 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1062 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1063 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1064 | |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 1065 | request(1)->handle()->SetPriority(HIGHEST); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1066 | |
| 1067 | ReleaseOneConnection(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1068 | |
| 1069 | EXPECT_TRUE(request(1)->handle()->socket()); |
| 1070 | } |
| 1071 | |
| 1072 | // Reprioritize a request up past another one and make sure that changes the |
| 1073 | // completion order. |
| 1074 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeUpReorder) { |
| 1075 | CreatePool(kDefaultMaxSockets, 1); |
| 1076 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1077 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1078 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1079 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1080 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1081 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1082 | EXPECT_FALSE(request(2)->handle()->socket()); |
| 1083 | |
| 1084 | request(2)->handle()->SetPriority(HIGHEST); |
| 1085 | |
| 1086 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1087 | |
| 1088 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1089 | EXPECT_EQ(3, GetOrderOfRequest(2)); |
| 1090 | EXPECT_EQ(2, GetOrderOfRequest(3)); |
| 1091 | } |
| 1092 | |
| 1093 | // Reprioritize a request without changing relative priorities and check |
| 1094 | // that the order doesn't change. |
| 1095 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeUpNoReorder) { |
| 1096 | CreatePool(kDefaultMaxSockets, 1); |
| 1097 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1098 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1099 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1100 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1101 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1102 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1103 | EXPECT_FALSE(request(2)->handle()->socket()); |
| 1104 | |
| 1105 | request(2)->handle()->SetPriority(MEDIUM); |
| 1106 | |
| 1107 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1108 | |
| 1109 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1110 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1111 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 1112 | } |
| 1113 | |
| 1114 | // Reprioritize a request past down another one and make sure that changes the |
| 1115 | // completion order. |
| 1116 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeDownReorder) { |
| 1117 | CreatePool(kDefaultMaxSockets, 1); |
| 1118 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1119 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1120 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1121 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1122 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1123 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1124 | EXPECT_FALSE(request(2)->handle()->socket()); |
| 1125 | |
| 1126 | request(1)->handle()->SetPriority(LOW); |
| 1127 | |
| 1128 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1129 | |
| 1130 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1131 | EXPECT_EQ(3, GetOrderOfRequest(2)); |
| 1132 | EXPECT_EQ(2, GetOrderOfRequest(3)); |
| 1133 | } |
| 1134 | |
| 1135 | // Reprioritize a request to the same level as another and confirm it is |
| 1136 | // put after the old request. |
| 1137 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeResetFIFO) { |
| 1138 | CreatePool(kDefaultMaxSockets, 1); |
| 1139 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1140 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1141 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1142 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1143 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1144 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1145 | EXPECT_FALSE(request(2)->handle()->socket()); |
| 1146 | |
| 1147 | request(1)->handle()->SetPriority(MEDIUM); |
| 1148 | |
| 1149 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1150 | |
| 1151 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1152 | EXPECT_EQ(3, GetOrderOfRequest(2)); |
| 1153 | EXPECT_EQ(2, GetOrderOfRequest(3)); |
| 1154 | } |
| 1155 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1156 | TEST_F(ClientSocketPoolBaseTest, TotalLimitRespectsGroupLimit) { |
| 1157 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1158 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1159 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsOk()); |
| 1160 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsOk()); |
| 1161 | EXPECT_THAT(StartRequest(TestGroupId("b"), HIGHEST), IsOk()); |
| 1162 | EXPECT_THAT(StartRequest(TestGroupId("b"), MEDIUM), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1163 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1164 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1165 | client_socket_factory_.allocation_count()); |
| 1166 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1167 | EXPECT_THAT(StartRequest(TestGroupId("c"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1168 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 1169 | EXPECT_THAT(StartRequest(TestGroupId("b"), HIGHEST), IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1170 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1171 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1172 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1173 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1174 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1175 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1176 | |
| 1177 | // First 4 requests don't have to wait, and finish in order. |
| 1178 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1179 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1180 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 1181 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 1182 | |
| 1183 | // Request ("b", 7) has the highest priority, but we can't make new socket for |
| 1184 | // group "b", because it has reached the per-group limit. Then we make |
| 1185 | // socket for ("c", 6), because it has higher priority than ("a", 4), |
| 1186 | // and we still can't make a socket for group "b". |
| 1187 | EXPECT_EQ(5, GetOrderOfRequest(5)); |
| 1188 | EXPECT_EQ(6, GetOrderOfRequest(6)); |
| 1189 | EXPECT_EQ(7, GetOrderOfRequest(7)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1190 | |
| 1191 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1192 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(8)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1193 | } |
| 1194 | |
| 1195 | // Make sure that we count connecting sockets against the total limit. |
| 1196 | TEST_F(ClientSocketPoolBaseTest, TotalLimitCountsConnectingSockets) { |
| 1197 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1198 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1199 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1200 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
| 1201 | EXPECT_THAT(StartRequest(TestGroupId("c"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1202 | |
| 1203 | // Create one asynchronous request. |
| 1204 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1205 | EXPECT_THAT(StartRequest(TestGroupId("d"), DEFAULT_PRIORITY), |
| 1206 | IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1207 | |
[email protected] | 6b17538 | 2009-10-13 06:47:47 | [diff] [blame] | 1208 | // We post all of our delayed tasks with a 2ms delay. I.e. they don't |
| 1209 | // actually become pending until 2ms after they have been created. In order |
| 1210 | // to flush all tasks, we need to wait so that we know there are no |
| 1211 | // soon-to-be-pending tasks waiting. |
Alex Clarke | 0def209 | 2018-12-10 12:01:45 | [diff] [blame] | 1212 | FastForwardBy(base::TimeDelta::FromMilliseconds(10)); |
[email protected] | 6b17538 | 2009-10-13 06:47:47 | [diff] [blame] | 1213 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1214 | // The next synchronous request should wait for its turn. |
| 1215 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1216 | EXPECT_THAT(StartRequest(TestGroupId("e"), DEFAULT_PRIORITY), |
| 1217 | IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1218 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1219 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1220 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1221 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1222 | client_socket_factory_.allocation_count()); |
| 1223 | |
| 1224 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1225 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1226 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 1227 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1228 | EXPECT_EQ(5, GetOrderOfRequest(5)); |
| 1229 | |
| 1230 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1231 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(6)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1232 | } |
| 1233 | |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1234 | TEST_F(ClientSocketPoolBaseTest, CorrectlyCountStalledGroups) { |
| 1235 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 1236 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 1237 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1238 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1239 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1240 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1241 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1242 | |
| 1243 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 1244 | |
| 1245 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1246 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1247 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), |
| 1248 | IsError(ERR_IO_PENDING)); |
| 1249 | EXPECT_THAT(StartRequest(TestGroupId("c"), DEFAULT_PRIORITY), |
| 1250 | IsError(ERR_IO_PENDING)); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1251 | |
| 1252 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1253 | |
[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 + 1, client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1256 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1257 | EXPECT_EQ(kDefaultMaxSockets + 2, client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1258 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
| 1259 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1260 | EXPECT_EQ(kDefaultMaxSockets + 2, client_socket_factory_.allocation_count()); |
| 1261 | } |
| 1262 | |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 1263 | TEST_F(ClientSocketPoolBaseTest, StallAndThenCancelAndTriggerAvailableSocket) { |
| 1264 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 1265 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1266 | |
| 1267 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1268 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1269 | EXPECT_EQ( |
| 1270 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1271 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1272 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1273 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1274 | pool_.get(), NetLogWithSource())); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 1275 | |
| 1276 | ClientSocketHandle handles[4]; |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 1277 | for (size_t i = 0; i < base::size(handles); ++i) { |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1278 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1279 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1280 | handles[i].Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1281 | TestGroupId("b"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1282 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1283 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1284 | pool_.get(), NetLogWithSource())); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 1285 | } |
| 1286 | |
| 1287 | // One will be stalled, cancel all the handles now. |
| 1288 | // This should hit the OnAvailableSocketSlot() code where we previously had |
| 1289 | // stalled groups, but no longer have any. |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 1290 | for (size_t i = 0; i < base::size(handles); ++i) |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 1291 | handles[i].Reset(); |
| 1292 | } |
| 1293 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1294 | TEST_F(ClientSocketPoolBaseTest, CancelStalledSocketAtSocketLimit) { |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1295 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1296 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 1297 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1298 | { |
| 1299 | ClientSocketHandle handles[kDefaultMaxSockets]; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1300 | TestCompletionCallback callbacks[kDefaultMaxSockets]; |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1301 | for (int i = 0; i < kDefaultMaxSockets; ++i) { |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1302 | EXPECT_EQ(OK, |
| 1303 | handles[i].Init(TestGroupId(base::NumberToString(i)), params_, |
| 1304 | base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
| 1305 | ClientSocketPool::RespectLimits::ENABLED, |
| 1306 | callbacks[i].callback(), |
| 1307 | ClientSocketPool::ProxyAuthCallback(), |
| 1308 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1309 | } |
| 1310 | |
| 1311 | // Force a stalled group. |
| 1312 | ClientSocketHandle stalled_handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1313 | TestCompletionCallback callback; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1314 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1315 | stalled_handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1316 | TestGroupId("foo"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1317 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1318 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1319 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1320 | |
| 1321 | // Cancel the stalled request. |
| 1322 | stalled_handle.Reset(); |
| 1323 | |
| 1324 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1325 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 1326 | |
| 1327 | // Dropping out of scope will close all handles and return them to idle. |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1328 | } |
| 1329 | |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1330 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1331 | EXPECT_EQ(kDefaultMaxSockets, pool_->IdleSocketCount()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1332 | } |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1333 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1334 | TEST_F(ClientSocketPoolBaseTest, CancelPendingSocketAtSocketLimit) { |
| 1335 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1336 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 1337 | |
| 1338 | { |
| 1339 | ClientSocketHandle handles[kDefaultMaxSockets]; |
| 1340 | for (int i = 0; i < kDefaultMaxSockets; ++i) { |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1341 | TestCompletionCallback callback; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1342 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1343 | handles[i].Init(TestGroupId(base::NumberToString(i)), params_, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1344 | base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1345 | ClientSocketPool::RespectLimits::ENABLED, |
| 1346 | callback.callback(), |
| 1347 | ClientSocketPool::ProxyAuthCallback(), |
| 1348 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1349 | } |
| 1350 | |
| 1351 | // Force a stalled group. |
| 1352 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1353 | ClientSocketHandle stalled_handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1354 | TestCompletionCallback callback; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1355 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1356 | stalled_handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1357 | TestGroupId("foo"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1358 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1359 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1360 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1361 | |
| 1362 | // Since it is stalled, it should have no connect jobs. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1363 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("foo"))); |
| 1364 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 1365 | TestGroupId("foo"))); |
| 1366 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroupForTesting( |
| 1367 | TestGroupId("foo"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1368 | |
| 1369 | // Cancel the stalled request. |
| 1370 | handles[0].Reset(); |
| 1371 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1372 | // Now we should have a connect job. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1373 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("foo"))); |
| 1374 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 1375 | TestGroupId("foo"))); |
| 1376 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroupForTesting( |
| 1377 | TestGroupId("foo"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1378 | |
| 1379 | // The stalled socket should connect. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1380 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1381 | |
| 1382 | EXPECT_EQ(kDefaultMaxSockets + 1, |
| 1383 | client_socket_factory_.allocation_count()); |
| 1384 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1385 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("foo"))); |
| 1386 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 1387 | TestGroupId("foo"))); |
| 1388 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroupForTesting( |
| 1389 | TestGroupId("foo"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1390 | |
| 1391 | // Dropping out of scope will close all handles and return them to idle. |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1392 | } |
| 1393 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1394 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 1395 | } |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1396 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1397 | TEST_F(ClientSocketPoolBaseTest, WaitForStalledSocketAtSocketLimit) { |
| 1398 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1399 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1400 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1401 | ClientSocketHandle stalled_handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1402 | TestCompletionCallback callback; |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1403 | { |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 1404 | EXPECT_FALSE(pool_->IsStalled()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1405 | ClientSocketHandle handles[kDefaultMaxSockets]; |
| 1406 | for (int i = 0; i < kDefaultMaxSockets; ++i) { |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1407 | TestCompletionCallback callback; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1408 | EXPECT_EQ( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1409 | OK, handles[i].Init( |
| 1410 | TestGroupId(base::StringPrintf("Take 2: %d", i)), params_, |
| 1411 | base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
| 1412 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1413 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 1414 | NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1415 | } |
| 1416 | |
| 1417 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1418 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 1419 | EXPECT_FALSE(pool_->IsStalled()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1420 | |
| 1421 | // Now we will hit the socket limit. |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1422 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1423 | stalled_handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1424 | TestGroupId("foo"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1425 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1426 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1427 | pool_.get(), NetLogWithSource())); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 1428 | EXPECT_TRUE(pool_->IsStalled()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1429 | |
| 1430 | // Dropping out of scope will close all handles and return them to idle. |
| 1431 | } |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1432 | |
| 1433 | // But if we wait for it, the released idle sockets will be closed in |
| 1434 | // preference of the waiting request. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1435 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1436 | |
| 1437 | EXPECT_EQ(kDefaultMaxSockets + 1, client_socket_factory_.allocation_count()); |
| 1438 | EXPECT_EQ(3, pool_->IdleSocketCount()); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1439 | } |
| 1440 | |
| 1441 | // Regression test for http://crbug.com/40952. |
| 1442 | TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketAtSocketLimitDeleteGroup) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1443 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 1444 | true /* enable_backup_connect_jobs */); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1445 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 1446 | |
| 1447 | for (int i = 0; i < kDefaultMaxSockets; ++i) { |
| 1448 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1449 | TestCompletionCallback callback; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1450 | EXPECT_EQ(OK, handle.Init(TestGroupId(base::NumberToString(i)), params_, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1451 | base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1452 | ClientSocketPool::RespectLimits::ENABLED, |
| 1453 | callback.callback(), |
| 1454 | ClientSocketPool::ProxyAuthCallback(), |
| 1455 | pool_.get(), NetLogWithSource())); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1456 | } |
| 1457 | |
| 1458 | // Flush all the DoReleaseSocket tasks. |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 1459 | base::RunLoop().RunUntilIdle(); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1460 | |
| 1461 | // Stall a group. Set a pending job so it'll trigger a backup job if we don't |
| 1462 | // reuse a socket. |
| 1463 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1464 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1465 | TestCompletionCallback callback; |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1466 | |
| 1467 | // "0" is special here, since it should be the first entry in the sorted map, |
| 1468 | // which is the one which we would close an idle socket for. We shouldn't |
| 1469 | // close an idle socket though, since we should reuse the idle socket. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1470 | EXPECT_EQ(OK, handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1471 | TestGroupId("0"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1472 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1473 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1474 | pool_.get(), NetLogWithSource())); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1475 | |
| 1476 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1477 | EXPECT_EQ(kDefaultMaxSockets - 1, pool_->IdleSocketCount()); |
| 1478 | } |
| 1479 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1480 | TEST_F(ClientSocketPoolBaseTest, PendingRequests) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1481 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1482 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1483 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1484 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1485 | EXPECT_THAT(StartRequest(TestGroupId("a"), IDLE), IsError(ERR_IO_PENDING)); |
| 1486 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
| 1487 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1488 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1489 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 1490 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1491 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1492 | ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1493 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
| 1494 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1495 | EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup, |
| 1496 | completion_count()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1497 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1498 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1499 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
[email protected] | c9c6f5c | 2010-07-31 01:30:03 | [diff] [blame] | 1500 | EXPECT_EQ(8, GetOrderOfRequest(3)); |
| 1501 | EXPECT_EQ(6, GetOrderOfRequest(4)); |
| 1502 | EXPECT_EQ(4, GetOrderOfRequest(5)); |
| 1503 | EXPECT_EQ(3, GetOrderOfRequest(6)); |
| 1504 | EXPECT_EQ(5, GetOrderOfRequest(7)); |
| 1505 | EXPECT_EQ(7, GetOrderOfRequest(8)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1506 | |
| 1507 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1508 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(9)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1509 | } |
| 1510 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1511 | TEST_F(ClientSocketPoolBaseTest, PendingRequests_NoKeepAlive) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1512 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1513 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1514 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1515 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1516 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
| 1517 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1518 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1519 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 1520 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1521 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1522 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1523 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1524 | for (size_t i = kDefaultMaxSocketsPerGroup; i < requests_size(); ++i) |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1525 | EXPECT_THAT(request(i)->WaitForResult(), IsOk()); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1526 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1527 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1528 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1529 | EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup, |
| 1530 | completion_count()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1531 | } |
| 1532 | |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1533 | TEST_F(ClientSocketPoolBaseTest, ResetAndCloseSocket) { |
| 1534 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1535 | |
| 1536 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1537 | ClientSocketHandle handle; |
| 1538 | TestCompletionCallback callback; |
| 1539 | EXPECT_EQ( |
| 1540 | ERR_IO_PENDING, |
| 1541 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1542 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1543 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1544 | pool_.get(), NetLogWithSource())); |
| 1545 | |
| 1546 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 1547 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1548 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1549 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
| 1550 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 1551 | |
| 1552 | handle.ResetAndCloseSocket(); |
| 1553 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1554 | } |
| 1555 | |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1556 | // This test will start up a socket request and then call Reset() on the handle. |
| 1557 | // The pending ConnectJob should not be destroyed. |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1558 | TEST_F(ClientSocketPoolBaseTest, CancelRequestKeepsConnectJob) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1559 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1560 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1561 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1562 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1563 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1564 | EXPECT_EQ( |
| 1565 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1566 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1567 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1568 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1569 | pool_.get(), NetLogWithSource())); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1570 | handle.Reset(); |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1571 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1572 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1573 | } |
| 1574 | |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1575 | // This test will start up a socket request and then call ResetAndCloseSocket() |
| 1576 | // on the handle. The pending ConnectJob or connected socket should be |
| 1577 | // destroyed. |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1578 | TEST_F(ClientSocketPoolBaseTest, CancelRequestAndCloseSocket) { |
| 1579 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1580 | |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1581 | // When true, the socket connects before it's canceled. |
| 1582 | for (bool cancel_when_callback_pending : {false, true}) { |
| 1583 | if (cancel_when_callback_pending) { |
| 1584 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 1585 | } else { |
| 1586 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1587 | } |
| 1588 | ClientSocketHandle handle; |
| 1589 | TestCompletionCallback callback; |
| 1590 | EXPECT_EQ( |
| 1591 | ERR_IO_PENDING, |
| 1592 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1593 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1594 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1595 | pool_.get(), NetLogWithSource())); |
| 1596 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1597 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1598 | |
| 1599 | if (cancel_when_callback_pending) { |
| 1600 | client_socket_factory_.SignalJobs(); |
| 1601 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1602 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 1603 | } |
| 1604 | |
| 1605 | handle.ResetAndCloseSocket(); |
| 1606 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1607 | } |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1608 | } |
| 1609 | |
| 1610 | TEST_F(ClientSocketPoolBaseTest, |
| 1611 | CancelRequestAndCloseSocketWhenMoreRequestsThanConnectJobs) { |
| 1612 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1613 | |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1614 | // When true, the sockets connect before they're canceled. |
| 1615 | for (bool cancel_when_callback_pending : {false, true}) { |
| 1616 | if (cancel_when_callback_pending) { |
| 1617 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 1618 | } else { |
| 1619 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1620 | } |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1621 | |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1622 | std::vector<std::unique_ptr<ClientSocketHandle>> handles; |
| 1623 | TestCompletionCallback callback; |
| 1624 | // Make |kDefaultMaxSockets + 1| socket requests. |
| 1625 | for (int i = 0; i < kDefaultMaxSocketsPerGroup + 1; ++i) { |
| 1626 | std::unique_ptr<ClientSocketHandle> handle = |
| 1627 | std::make_unique<ClientSocketHandle>(); |
| 1628 | EXPECT_EQ(ERR_IO_PENDING, |
| 1629 | handle->Init( |
| 1630 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1631 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1632 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1633 | pool_.get(), NetLogWithSource())); |
| 1634 | handles.push_back(std::move(handle)); |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1635 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1636 | EXPECT_EQ( |
| 1637 | static_cast<size_t>(std::min(i + 1, kDefaultMaxSocketsPerGroup)), |
| 1638 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1639 | } |
| 1640 | |
| 1641 | if (cancel_when_callback_pending) { |
| 1642 | client_socket_factory_.SignalJobs(); |
| 1643 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1644 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
| 1645 | pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 1646 | } |
| 1647 | |
| 1648 | // Calling ResetAndCloseSocket() on a handle should not cancel a ConnectJob |
| 1649 | // or close a socket, since there are more requests than ConnectJobs or |
| 1650 | // sockets. |
| 1651 | handles[kDefaultMaxSocketsPerGroup]->ResetAndCloseSocket(); |
| 1652 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1653 | if (cancel_when_callback_pending) { |
| 1654 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
| 1655 | pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 1656 | } else { |
| 1657 | EXPECT_EQ(static_cast<size_t>(kDefaultMaxSocketsPerGroup), |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1658 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1659 | } |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1660 | |
| 1661 | // Calling ResetAndCloseSocket() on other handles should cancel a ConnectJob |
| 1662 | // or close a socket. |
| 1663 | for (int i = kDefaultMaxSocketsPerGroup - 1; i >= 0; --i) { |
| 1664 | handles[i]->ResetAndCloseSocket(); |
| 1665 | if (i > 0) { |
| 1666 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1667 | if (cancel_when_callback_pending) { |
| 1668 | EXPECT_EQ(i, |
| 1669 | pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 1670 | } else { |
| 1671 | EXPECT_EQ(static_cast<size_t>(i), |
| 1672 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1673 | } |
| 1674 | } else { |
| 1675 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1676 | } |
| 1677 | } |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1678 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1679 | } |
| 1680 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1681 | TEST_F(ClientSocketPoolBaseTest, ConnectCancelConnect) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1682 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1683 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1684 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 1685 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1686 | TestCompletionCallback callback; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1687 | |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1688 | EXPECT_EQ( |
| 1689 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1690 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1691 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1692 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1693 | pool_.get(), NetLogWithSource())); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1694 | |
| 1695 | handle.Reset(); |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1696 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1697 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1698 | |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1699 | // This will create a second ConnectJob, since the other ConnectJob was |
| 1700 | // previously assigned to a request. |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1701 | TestCompletionCallback callback2; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1702 | EXPECT_EQ( |
| 1703 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1704 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1705 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1706 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1707 | pool_.get(), NetLogWithSource())); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1708 | |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1709 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1710 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1711 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1712 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1713 | EXPECT_FALSE(callback.have_result()); |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1714 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1715 | // One ConnectJob completed, and its socket is now assigned to |handle|. |
| 1716 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 1717 | // The other ConnectJob should have either completed, or still be connecting. |
| 1718 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")) + |
| 1719 | pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1720 | |
| 1721 | handle.Reset(); |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1722 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1723 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")) + |
| 1724 | pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
| 1725 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1726 | } |
| 1727 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1728 | TEST_F(ClientSocketPoolBaseTest, CancelRequest) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1729 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1730 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1731 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1732 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1733 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
| 1734 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1735 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1736 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 1737 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1738 | |
| 1739 | // Cancel a request. |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1740 | size_t index_to_cancel = kDefaultMaxSocketsPerGroup + 2; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1741 | EXPECT_FALSE((*requests())[index_to_cancel]->handle()->is_initialized()); |
| 1742 | (*requests())[index_to_cancel]->handle()->Reset(); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1743 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1744 | ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1745 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1746 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
| 1747 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1748 | EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup - 1, |
| 1749 | completion_count()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1750 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1751 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1752 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1753 | EXPECT_EQ(5, GetOrderOfRequest(3)); |
| 1754 | EXPECT_EQ(3, GetOrderOfRequest(4)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1755 | EXPECT_EQ(ClientSocketPoolTest::kRequestNotFound, |
| 1756 | GetOrderOfRequest(5)); // Canceled request. |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1757 | EXPECT_EQ(4, GetOrderOfRequest(6)); |
| 1758 | EXPECT_EQ(6, GetOrderOfRequest(7)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1759 | |
| 1760 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1761 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(8)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1762 | } |
| 1763 | |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1764 | // Function to be used as a callback on socket request completion. It first |
| 1765 | // disconnects the successfully connected socket from the first request, and |
| 1766 | // then reuses the ClientSocketHandle to request another socket. |
| 1767 | // |
| 1768 | // |nested_callback| is called with the result of the second socket request. |
| 1769 | void RequestSocketOnComplete(ClientSocketHandle* handle, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1770 | TransportClientSocketPool* pool, |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1771 | TestConnectJobFactory* test_connect_job_factory, |
| 1772 | TestConnectJob::JobType next_job_type, |
Bence Béky | a4a5093 | 2018-08-10 13:39:41 | [diff] [blame] | 1773 | TestCompletionCallback* nested_callback, |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1774 | int first_request_result) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1775 | EXPECT_THAT(first_request_result, IsOk()); |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1776 | |
| 1777 | test_connect_job_factory->set_job_type(next_job_type); |
| 1778 | |
| 1779 | // Don't allow reuse of the socket. Disconnect it and then release it. |
| 1780 | if (handle->socket()) |
| 1781 | handle->socket()->Disconnect(); |
| 1782 | handle->Reset(); |
| 1783 | |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1784 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1785 | int rv = handle->Init( |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 1786 | TestGroupId("a"), |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1787 | ClientSocketPool::SocketParams::CreateForHttpForTesting(), base::nullopt, |
| 1788 | LOWEST, SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 1789 | nested_callback->callback(), ClientSocketPool::ProxyAuthCallback(), pool, |
| 1790 | NetLogWithSource()); |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1791 | if (rv != ERR_IO_PENDING) { |
| 1792 | DCHECK_EQ(TestConnectJob::kMockJob, next_job_type); |
Bence Béky | a4a5093 | 2018-08-10 13:39:41 | [diff] [blame] | 1793 | nested_callback->callback().Run(rv); |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1794 | } else { |
| 1795 | DCHECK_EQ(TestConnectJob::kMockPendingJob, next_job_type); |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1796 | } |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1797 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1798 | |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1799 | // Tests the case where a second socket is requested in a completion callback, |
| 1800 | // and the second socket connects asynchronously. Reuses the same |
| 1801 | // ClientSocketHandle for the second socket, after disconnecting the first. |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1802 | TEST_F(ClientSocketPoolBaseTest, RequestPendingJobTwice) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1803 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1804 | |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 1805 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 1806 | ClientSocketHandle handle; |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1807 | TestCompletionCallback second_result_callback; |
| 1808 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1809 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Paul Jensen | 8d6f87ec | 2018-01-13 00:46:54 | [diff] [blame] | 1810 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1811 | base::BindOnce(&RequestSocketOnComplete, &handle, pool_.get(), |
| 1812 | connect_job_factory_, TestConnectJob::kMockPendingJob, |
| 1813 | &second_result_callback), |
| 1814 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1815 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1816 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1817 | EXPECT_THAT(second_result_callback.WaitForResult(), IsOk()); |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1818 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1819 | |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1820 | // Tests the case where a second socket is requested in a completion callback, |
| 1821 | // and the second socket connects synchronously. Reuses the same |
| 1822 | // ClientSocketHandle for the second socket, after disconnecting the first. |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1823 | TEST_F(ClientSocketPoolBaseTest, RequestPendingJobThenSynchronous) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1824 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1825 | |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 1826 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 1827 | ClientSocketHandle handle; |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1828 | TestCompletionCallback second_result_callback; |
| 1829 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1830 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Paul Jensen | 8d6f87ec | 2018-01-13 00:46:54 | [diff] [blame] | 1831 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1832 | base::BindOnce(&RequestSocketOnComplete, &handle, pool_.get(), |
| 1833 | connect_job_factory_, TestConnectJob::kMockPendingJob, |
| 1834 | &second_result_callback), |
| 1835 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1836 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1837 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1838 | EXPECT_THAT(second_result_callback.WaitForResult(), IsOk()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1839 | } |
| 1840 | |
| 1841 | // Make sure that pending requests get serviced after active requests get |
| 1842 | // cancelled. |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1843 | TEST_F(ClientSocketPoolBaseTest, CancelActiveRequestWithPendingRequests) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1844 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1845 | |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 1846 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1847 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 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)); |
| 1860 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1861 | IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1862 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1863 | // Now, kDefaultMaxSocketsPerGroup requests should be active. |
| 1864 | // Let's cancel them. |
| 1865 | for (int i = 0; i < kDefaultMaxSocketsPerGroup; ++i) { |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1866 | ASSERT_FALSE(request(i)->handle()->is_initialized()); |
| 1867 | request(i)->handle()->Reset(); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1868 | } |
| 1869 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1870 | // Let's wait for the rest to complete now. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1871 | for (size_t i = kDefaultMaxSocketsPerGroup; i < requests_size(); ++i) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1872 | EXPECT_THAT(request(i)->WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1873 | request(i)->handle()->Reset(); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1874 | } |
| 1875 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1876 | EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup, |
| 1877 | completion_count()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1878 | } |
| 1879 | |
| 1880 | // Make sure that pending requests get serviced after active requests fail. |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1881 | TEST_F(ClientSocketPoolBaseTest, FailingActiveRequestWithPendingRequests) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1882 | const size_t kMaxSockets = 5; |
| 1883 | CreatePool(kMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1884 | |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 1885 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1886 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1887 | const size_t kNumberOfRequests = 2 * kDefaultMaxSocketsPerGroup + 1; |
| 1888 | ASSERT_LE(kNumberOfRequests, kMaxSockets); // Otherwise the test will hang. |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1889 | |
| 1890 | // Queue up all the requests |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1891 | for (size_t i = 0; i < kNumberOfRequests; ++i) |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1892 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1893 | IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1894 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1895 | for (size_t i = 0; i < kNumberOfRequests; ++i) |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1896 | EXPECT_THAT(request(i)->WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1897 | } |
| 1898 | |
mmenke | 9d72fe4 | 2017-05-18 22:36:07 | [diff] [blame] | 1899 | // Make sure that pending requests that complete synchronously get serviced |
| 1900 | // after active requests fail. See https://crbug.com/723748 |
| 1901 | TEST_F(ClientSocketPoolBaseTest, HandleMultipleSyncFailuresAfterAsyncFailure) { |
| 1902 | const size_t kNumberOfRequests = 10; |
| 1903 | const size_t kMaxSockets = 1; |
| 1904 | CreatePool(kMaxSockets, kMaxSockets); |
| 1905 | |
| 1906 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
| 1907 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1908 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1909 | IsError(ERR_IO_PENDING)); |
mmenke | 9d72fe4 | 2017-05-18 22:36:07 | [diff] [blame] | 1910 | |
| 1911 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
| 1912 | |
| 1913 | // Queue up all the other requests |
| 1914 | for (size_t i = 1; i < kNumberOfRequests; ++i) |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1915 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1916 | IsError(ERR_IO_PENDING)); |
mmenke | 9d72fe4 | 2017-05-18 22:36:07 | [diff] [blame] | 1917 | |
| 1918 | // Make sure all requests fail, instead of hanging. |
| 1919 | for (size_t i = 0; i < kNumberOfRequests; ++i) |
| 1920 | EXPECT_THAT(request(i)->WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 1921 | } |
| 1922 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1923 | TEST_F(ClientSocketPoolBaseTest, CancelActiveRequestThenRequestSocket) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1924 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1925 | |
| 1926 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1927 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1928 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1929 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1930 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1931 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1932 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1933 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1934 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1935 | |
| 1936 | // Cancel the active request. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1937 | handle.Reset(); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1938 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1939 | rv = handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1940 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1941 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1942 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1943 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 1944 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1945 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1946 | EXPECT_FALSE(handle.is_reused()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 1947 | TestLoadTimingInfoConnectedNotReused(handle); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1948 | EXPECT_EQ(2, client_socket_factory_.allocation_count()); |
| 1949 | } |
| 1950 | |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1951 | TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketsForced) { |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 1952 | const char kReason[] = "Really nifty reason"; |
| 1953 | |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1954 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1955 | ClientSocketHandle handle; |
| 1956 | TestCompletionCallback callback; |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 1957 | RecordingBoundTestNetLog log; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1958 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1959 | TestGroupId("a"), params_, base::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1960 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1961 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1962 | EXPECT_THAT(rv, IsOk()); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 1963 | ASSERT_TRUE(handle.socket()); |
| 1964 | NetLogSource source = handle.socket()->NetLog().source(); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1965 | handle.Reset(); |
| 1966 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 1967 | pool_->CloseIdleSockets(kReason); |
| 1968 | ExpectSocketClosedWithReason(source, kReason); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1969 | } |
| 1970 | |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1971 | TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketsInGroupForced) { |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1972 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1973 | TestCompletionCallback callback; |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 1974 | RecordingBoundTestNetLog log; |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1975 | ClientSocketHandle handle1; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1976 | int rv = handle1.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1977 | TestGroupId("a"), params_, base::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1978 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1979 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1980 | EXPECT_THAT(rv, IsOk()); |
| 1981 | ClientSocketHandle handle2; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1982 | rv = handle2.Init(TestGroupId("a"), params_, base::nullopt, LOWEST, |
| 1983 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1984 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1985 | pool_.get(), log.bound()); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1986 | ClientSocketHandle handle3; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1987 | rv = handle3.Init(TestGroupId("b"), params_, base::nullopt, LOWEST, |
| 1988 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1989 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1990 | pool_.get(), log.bound()); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1991 | EXPECT_THAT(rv, IsOk()); |
| 1992 | handle1.Reset(); |
| 1993 | handle2.Reset(); |
| 1994 | handle3.Reset(); |
| 1995 | EXPECT_EQ(3, pool_->IdleSocketCount()); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 1996 | pool_->CloseIdleSocketsInGroup(TestGroupId("a"), "Very good reason"); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1997 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1998 | } |
| 1999 | |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 2000 | TEST_F(ClientSocketPoolBaseTest, CleanUpUnusableIdleSockets) { |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 2001 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2002 | ClientSocketHandle handle; |
| 2003 | TestCompletionCallback callback; |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 2004 | RecordingBoundTestNetLog log; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2005 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2006 | TestGroupId("a"), params_, base::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2007 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2008 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 2009 | EXPECT_THAT(rv, IsOk()); |
| 2010 | StreamSocket* socket = handle.socket(); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 2011 | ASSERT_TRUE(socket); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 2012 | handle.Reset(); |
| 2013 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 2014 | |
| 2015 | // Disconnect socket now to make the socket unusable. |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 2016 | NetLogSource source = socket->NetLog().source(); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 2017 | socket->Disconnect(); |
| 2018 | ClientSocketHandle handle2; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2019 | rv = handle2.Init(TestGroupId("a"), params_, base::nullopt, LOWEST, |
| 2020 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2021 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2022 | pool_.get(), log.bound()); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 2023 | EXPECT_THAT(rv, IsOk()); |
| 2024 | EXPECT_FALSE(handle2.is_reused()); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 2025 | |
| 2026 | // This is admittedly not an accurate error in this case, but normally code |
| 2027 | // doesn't secretly keep a raw pointers to sockets returned to the socket pool |
| 2028 | // and close them out of band, so discovering an idle socket was closed when |
| 2029 | // trying to reuse it normally means it was closed by the remote side. |
| 2030 | ExpectSocketClosedWithReason( |
| 2031 | source, TransportClientSocketPool::kRemoteSideClosedConnection); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 2032 | } |
| 2033 | |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 2034 | // Regression test for http://crbug.com/17985. |
| 2035 | TEST_F(ClientSocketPoolBaseTest, GroupWithPendingRequestsIsNotEmpty) { |
| 2036 | const int kMaxSockets = 3; |
| 2037 | const int kMaxSocketsPerGroup = 2; |
| 2038 | CreatePool(kMaxSockets, kMaxSocketsPerGroup); |
| 2039 | |
[email protected] | ac790b4 | 2009-12-02 04:31:31 | [diff] [blame] | 2040 | const RequestPriority kHighPriority = HIGHEST; |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 2041 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2042 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 2043 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 2044 | |
| 2045 | // This is going to be a pending request in an otherwise empty group. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2046 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2047 | IsError(ERR_IO_PENDING)); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 2048 | |
| 2049 | // Reach the maximum socket limit. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2050 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 2051 | |
| 2052 | // Create a stalled group with high priorities. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2053 | EXPECT_THAT(StartRequest(TestGroupId("c"), kHighPriority), |
| 2054 | IsError(ERR_IO_PENDING)); |
| 2055 | EXPECT_THAT(StartRequest(TestGroupId("c"), kHighPriority), |
| 2056 | IsError(ERR_IO_PENDING)); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 2057 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2058 | // Release the first two sockets from TestGroupId("a"). Because this is a |
| 2059 | // keepalive, the first release will unblock the pending request for |
| 2060 | // TestGroupId("a"). The second release will unblock a request for "c", |
| 2061 | // because it is the next high priority socket. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2062 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
| 2063 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 2064 | |
| 2065 | // Closing idle sockets should not get us into trouble, but in the bug |
| 2066 | // we were hitting a CHECK here. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2067 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 2068 | pool_->CloseIdleSockets("Very good reason"); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 2069 | |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 2070 | // Run the released socket wakeups. |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 2071 | base::RunLoop().RunUntilIdle(); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 2072 | } |
| 2073 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2074 | TEST_F(ClientSocketPoolBaseTest, BasicAsynchronous) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2075 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2076 | |
| 2077 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2078 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2079 | TestCompletionCallback callback; |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 2080 | RecordingBoundTestNetLog log; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2081 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2082 | TestGroupId("a"), params_, base::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2083 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2084 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2085 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2086 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2087 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 2088 | TestLoadTimingInfoNotConnected(handle); |
| 2089 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2090 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2091 | EXPECT_TRUE(handle.is_initialized()); |
| 2092 | EXPECT_TRUE(handle.socket()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 2093 | TestLoadTimingInfoConnectedNotReused(handle); |
| 2094 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2095 | handle.Reset(); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 2096 | TestLoadTimingInfoNotConnected(handle); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 2097 | |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 2098 | auto entries = log.GetEntries(); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 2099 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2100 | EXPECT_EQ(5u, entries.size()); |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 2101 | EXPECT_TRUE(LogContainsEvent( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2102 | entries, 0, NetLogEventType::TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 2103 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2104 | EXPECT_TRUE(LogContainsBeginEvent(entries, 1, NetLogEventType::SOCKET_POOL)); |
| 2105 | EXPECT_TRUE(LogContainsEvent( |
| 2106 | entries, 2, NetLogEventType::SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
| 2107 | NetLogEventPhase::NONE)); |
| 2108 | EXPECT_TRUE(LogContainsEvent(entries, 3, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 2109 | NetLogEventType::SOCKET_POOL_BOUND_TO_SOCKET, |
| 2110 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2111 | EXPECT_TRUE(LogContainsEndEvent(entries, 4, NetLogEventType::SOCKET_POOL)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2112 | } |
| 2113 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2114 | TEST_F(ClientSocketPoolBaseTest, |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2115 | InitConnectionAsynchronousFailure) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2116 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2117 | |
| 2118 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2119 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2120 | TestCompletionCallback callback; |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 2121 | RecordingBoundTestNetLog log; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2122 | // Set the additional error state members to ensure that they get cleared. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2123 | handle.set_is_ssl_error(true); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 2124 | handle.set_ssl_cert_request_info(base::MakeRefCounted<SSLCertRequestInfo>()); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2125 | EXPECT_EQ( |
| 2126 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2127 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2128 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2129 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2130 | pool_.get(), log.bound())); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2131 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2132 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2133 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2134 | EXPECT_FALSE(handle.is_ssl_error()); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 2135 | EXPECT_FALSE(handle.ssl_cert_request_info()); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 2136 | |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 2137 | auto entries = log.GetEntries(); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 2138 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2139 | EXPECT_EQ(4u, entries.size()); |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 2140 | EXPECT_TRUE(LogContainsEvent( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2141 | entries, 0, NetLogEventType::TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 2142 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2143 | EXPECT_TRUE(LogContainsBeginEvent(entries, 1, NetLogEventType::SOCKET_POOL)); |
| 2144 | EXPECT_TRUE(LogContainsEvent( |
| 2145 | entries, 2, NetLogEventType::SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
| 2146 | NetLogEventPhase::NONE)); |
| 2147 | EXPECT_TRUE(LogContainsEndEvent(entries, 3, NetLogEventType::SOCKET_POOL)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2148 | } |
| 2149 | |
mmenke | 6be122f | 2015-03-09 22:22:47 | [diff] [blame] | 2150 | // Check that an async ConnectJob failure does not result in creation of a new |
| 2151 | // ConnectJob when there's another pending request also waiting on its own |
| 2152 | // ConnectJob. See http://crbug.com/463960. |
| 2153 | TEST_F(ClientSocketPoolBaseTest, AsyncFailureWithPendingRequestWithJob) { |
| 2154 | CreatePool(2, 2); |
| 2155 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
| 2156 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2157 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2158 | IsError(ERR_IO_PENDING)); |
| 2159 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2160 | IsError(ERR_IO_PENDING)); |
mmenke | 6be122f | 2015-03-09 22:22:47 | [diff] [blame] | 2161 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2162 | EXPECT_THAT(request(0)->WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 2163 | EXPECT_THAT(request(1)->WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
mmenke | 6be122f | 2015-03-09 22:22:47 | [diff] [blame] | 2164 | |
| 2165 | EXPECT_EQ(2, client_socket_factory_.allocation_count()); |
| 2166 | } |
| 2167 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2168 | TEST_F(ClientSocketPoolBaseTest, TwoRequestsCancelOne) { |
[email protected] | b22b516 | 2010-03-16 07:53:10 | [diff] [blame] | 2169 | // TODO(eroman): Add back the log expectations! Removed them because the |
| 2170 | // ordering is difficult, and some may fire during destructor. |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2171 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2172 | |
| 2173 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2174 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2175 | TestCompletionCallback callback; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2176 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2177 | TestCompletionCallback callback2; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2178 | |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2179 | EXPECT_EQ( |
| 2180 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2181 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2182 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2183 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2184 | pool_.get(), NetLogWithSource())); |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 2185 | RecordingBoundTestNetLog log2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 2186 | EXPECT_EQ( |
| 2187 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2188 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2189 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2190 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2191 | pool_.get(), NetLogWithSource())); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2192 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2193 | handle.Reset(); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2194 | |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 2195 | |
| 2196 | // 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] | 2197 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2198 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2199 | handle2.Reset(); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 2200 | |
| 2201 | // Now request 2 has actually finished. |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 2202 | // TODO(eroman): Add back log expectations. |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2203 | } |
| 2204 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2205 | TEST_F(ClientSocketPoolBaseTest, CancelRequestLimitsJobs) { |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 2206 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2207 | |
[email protected] | 17a0c6c | 2009-08-04 00:07:04 | [diff] [blame] | 2208 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2209 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2210 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
| 2211 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 2212 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 2213 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 2214 | |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2215 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2216 | static_cast<int>( |
| 2217 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2218 | (*requests())[2]->handle()->Reset(); |
| 2219 | (*requests())[3]->handle()->Reset(); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2220 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2221 | static_cast<int>( |
| 2222 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 2223 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2224 | (*requests())[1]->handle()->Reset(); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2225 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2226 | static_cast<int>( |
| 2227 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 2228 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2229 | (*requests())[0]->handle()->Reset(); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2230 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2231 | static_cast<int>( |
| 2232 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 2233 | } |
| 2234 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2235 | // When requests and ConnectJobs are not coupled, the request will get serviced |
| 2236 | // by whatever comes first. |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2237 | TEST_F(ClientSocketPoolBaseTest, ReleaseSockets) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2238 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2239 | |
| 2240 | // Start job 1 (async OK) |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 2241 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2242 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2243 | std::vector<TestSocketRequest*> request_order; |
| 2244 | size_t completion_count; // unused |
| 2245 | TestSocketRequest req1(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2246 | int rv = req1.handle()->Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2247 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2248 | ClientSocketPool::RespectLimits::ENABLED, req1.callback(), |
| 2249 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2250 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 2251 | EXPECT_THAT(req1.WaitForResult(), IsOk()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2252 | |
| 2253 | // Job 1 finished OK. Start job 2 (also async OK). Request 3 is pending |
| 2254 | // without a job. |
| 2255 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2256 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2257 | TestSocketRequest req2(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2258 | rv = req2.handle()->Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2259 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2260 | ClientSocketPool::RespectLimits::ENABLED, req2.callback(), |
| 2261 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2262 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2263 | TestSocketRequest req3(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2264 | rv = req3.handle()->Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2265 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2266 | ClientSocketPool::RespectLimits::ENABLED, req3.callback(), |
| 2267 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2268 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2269 | |
| 2270 | // Both Requests 2 and 3 are pending. We release socket 1 which should |
| 2271 | // service request 2. Request 3 should still be waiting. |
[email protected] | a6c59f6 | 2009-07-29 16:33:33 | [diff] [blame] | 2272 | req1.handle()->Reset(); |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 2273 | // Run the released socket wakeups. |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 2274 | base::RunLoop().RunUntilIdle(); |
[email protected] | a6c59f6 | 2009-07-29 16:33:33 | [diff] [blame] | 2275 | ASSERT_TRUE(req2.handle()->socket()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2276 | EXPECT_THAT(req2.WaitForResult(), IsOk()); |
[email protected] | a6c59f6 | 2009-07-29 16:33:33 | [diff] [blame] | 2277 | EXPECT_FALSE(req3.handle()->socket()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2278 | |
| 2279 | // Signal job 2, which should service request 3. |
| 2280 | |
| 2281 | client_socket_factory_.SignalJobs(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2282 | EXPECT_THAT(req3.WaitForResult(), IsOk()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2283 | |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2284 | ASSERT_EQ(3u, request_order.size()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2285 | EXPECT_EQ(&req1, request_order[0]); |
| 2286 | EXPECT_EQ(&req2, request_order[1]); |
| 2287 | EXPECT_EQ(&req3, request_order[2]); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2288 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2289 | } |
| 2290 | |
| 2291 | // The requests are not coupled to the jobs. So, the requests should finish in |
| 2292 | // their priority / insertion order. |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2293 | TEST_F(ClientSocketPoolBaseTest, PendingJobCompletionOrder) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2294 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2295 | // First two jobs are async. |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 2296 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2297 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2298 | std::vector<TestSocketRequest*> request_order; |
| 2299 | size_t completion_count; // unused |
| 2300 | TestSocketRequest req1(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2301 | int rv = req1.handle()->Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2302 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2303 | ClientSocketPool::RespectLimits::ENABLED, req1.callback(), |
| 2304 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2305 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2306 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2307 | TestSocketRequest req2(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2308 | rv = req2.handle()->Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2309 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2310 | ClientSocketPool::RespectLimits::ENABLED, req2.callback(), |
| 2311 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2312 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2313 | |
| 2314 | // The pending job is sync. |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 2315 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2316 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2317 | TestSocketRequest req3(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2318 | rv = req3.handle()->Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2319 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2320 | ClientSocketPool::RespectLimits::ENABLED, req3.callback(), |
| 2321 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2322 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2323 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2324 | EXPECT_THAT(req1.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 2325 | EXPECT_THAT(req2.WaitForResult(), IsOk()); |
| 2326 | EXPECT_THAT(req3.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2327 | |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2328 | ASSERT_EQ(3u, request_order.size()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2329 | EXPECT_EQ(&req1, request_order[0]); |
| 2330 | EXPECT_EQ(&req2, request_order[1]); |
| 2331 | EXPECT_EQ(&req3, request_order[2]); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2332 | } |
| 2333 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2334 | // Test GetLoadState in the case there's only one socket request. |
| 2335 | TEST_F(ClientSocketPoolBaseTest, LoadStateOneRequest) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2336 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2337 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2338 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2339 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2340 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2341 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2342 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2343 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2344 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2345 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2346 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2347 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2348 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE); |
| 2349 | EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle.GetLoadState()); |
| 2350 | |
| 2351 | // No point in completing the connection, since ClientSocketHandles only |
| 2352 | // expect the LoadState to be checked while connecting. |
| 2353 | } |
| 2354 | |
| 2355 | // Test GetLoadState in the case there are two socket requests. |
| 2356 | TEST_F(ClientSocketPoolBaseTest, LoadStateTwoRequests) { |
| 2357 | CreatePool(2, 2); |
| 2358 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2359 | |
| 2360 | ClientSocketHandle handle; |
| 2361 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2362 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2363 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2364 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2365 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2366 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2367 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_RESOLVING_HOST); |
| 2368 | |
| 2369 | ClientSocketHandle handle2; |
| 2370 | TestCompletionCallback callback2; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2371 | rv = handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2372 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2373 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2374 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2375 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2376 | client_socket_factory_.SetJobLoadState(1, LOAD_STATE_RESOLVING_HOST); |
| 2377 | |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 2378 | // Each handle should reflect the state of its own job. |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2379 | EXPECT_EQ(LOAD_STATE_RESOLVING_HOST, handle.GetLoadState()); |
| 2380 | EXPECT_EQ(LOAD_STATE_RESOLVING_HOST, handle2.GetLoadState()); |
| 2381 | |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 2382 | // Update the state of the first job. |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2383 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_CONNECTING); |
| 2384 | |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 2385 | // Only the state of the first request should have changed. |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2386 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2387 | EXPECT_EQ(LOAD_STATE_RESOLVING_HOST, handle2.GetLoadState()); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 2388 | |
| 2389 | // Update the state of the second job. |
| 2390 | client_socket_factory_.SetJobLoadState(1, LOAD_STATE_SSL_HANDSHAKE); |
| 2391 | |
| 2392 | // Only the state of the second request should have changed. |
| 2393 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
| 2394 | EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle2.GetLoadState()); |
| 2395 | |
| 2396 | // Second job connects and the first request gets the socket. The |
| 2397 | // second handle switches to the state of the remaining ConnectJob. |
| 2398 | client_socket_factory_.SignalJob(1); |
| 2399 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 2400 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle2.GetLoadState()); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2401 | } |
| 2402 | |
| 2403 | // Test GetLoadState in the case the per-group limit is reached. |
| 2404 | TEST_F(ClientSocketPoolBaseTest, LoadStateGroupLimit) { |
| 2405 | CreatePool(2, 1); |
| 2406 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2407 | |
| 2408 | ClientSocketHandle handle; |
| 2409 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2410 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2411 | TestGroupId("a"), params_, base::nullopt, MEDIUM, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2412 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2413 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2414 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2415 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
| 2416 | |
| 2417 | // Request another socket from the same pool, buth with a higher priority. |
| 2418 | // The first request should now be stalled at the socket group limit. |
| 2419 | ClientSocketHandle handle2; |
| 2420 | TestCompletionCallback callback2; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2421 | rv = handle2.Init(TestGroupId("a"), params_, base::nullopt, HIGHEST, |
| 2422 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2423 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2424 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2425 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2426 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET, handle.GetLoadState()); |
| 2427 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle2.GetLoadState()); |
| 2428 | |
| 2429 | // The first handle should remain stalled as the other socket goes through |
| 2430 | // the connect process. |
| 2431 | |
| 2432 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE); |
| 2433 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET, handle.GetLoadState()); |
| 2434 | EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle2.GetLoadState()); |
| 2435 | |
| 2436 | client_socket_factory_.SignalJob(0); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2437 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2438 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET, handle.GetLoadState()); |
| 2439 | |
| 2440 | // Closing the second socket should cause the stalled handle to finally get a |
| 2441 | // ConnectJob. |
| 2442 | handle2.socket()->Disconnect(); |
| 2443 | handle2.Reset(); |
| 2444 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
| 2445 | } |
| 2446 | |
| 2447 | // Test GetLoadState in the case the per-pool limit is reached. |
| 2448 | TEST_F(ClientSocketPoolBaseTest, LoadStatePoolLimit) { |
| 2449 | CreatePool(2, 2); |
| 2450 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2451 | |
| 2452 | ClientSocketHandle handle; |
| 2453 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2454 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2455 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2456 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2457 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2458 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2459 | |
| 2460 | // Request for socket from another pool. |
| 2461 | ClientSocketHandle handle2; |
| 2462 | TestCompletionCallback callback2; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2463 | rv = handle2.Init(TestGroupId("b"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2464 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2465 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2466 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2467 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2468 | |
| 2469 | // Request another socket from the first pool. Request should stall at the |
| 2470 | // socket pool limit. |
| 2471 | ClientSocketHandle handle3; |
| 2472 | TestCompletionCallback callback3; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2473 | rv = handle3.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2474 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2475 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2476 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2477 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2478 | |
| 2479 | // The third handle should remain stalled as the other sockets in its group |
| 2480 | // goes through the connect process. |
| 2481 | |
| 2482 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
| 2483 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL, handle3.GetLoadState()); |
| 2484 | |
| 2485 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE); |
| 2486 | EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle.GetLoadState()); |
| 2487 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL, handle3.GetLoadState()); |
| 2488 | |
| 2489 | client_socket_factory_.SignalJob(0); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2490 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2491 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL, handle3.GetLoadState()); |
| 2492 | |
| 2493 | // Closing a socket should allow the stalled handle to finally get a new |
| 2494 | // ConnectJob. |
| 2495 | handle.socket()->Disconnect(); |
| 2496 | handle.Reset(); |
| 2497 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle3.GetLoadState()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2498 | } |
| 2499 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2500 | TEST_F(ClientSocketPoolBaseTest, CertError) { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2501 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2502 | connect_job_factory_->set_job_type(TestConnectJob::kMockCertErrorJob); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2503 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2504 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2505 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2506 | EXPECT_EQ( |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2507 | ERR_CERT_COMMON_NAME_INVALID, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2508 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2509 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2510 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2511 | pool_.get(), NetLogWithSource())); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2512 | EXPECT_TRUE(handle.is_initialized()); |
| 2513 | EXPECT_TRUE(handle.socket()); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2514 | } |
| 2515 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2516 | TEST_F(ClientSocketPoolBaseTest, AsyncCertError) { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2517 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2518 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2519 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingCertErrorJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2520 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2521 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2522 | EXPECT_EQ( |
| 2523 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2524 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2525 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2526 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2527 | pool_.get(), NetLogWithSource())); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2528 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2529 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2530 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_CERT_COMMON_NAME_INVALID)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2531 | EXPECT_TRUE(handle.is_initialized()); |
| 2532 | EXPECT_TRUE(handle.socket()); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2533 | } |
| 2534 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2535 | TEST_F(ClientSocketPoolBaseTest, AdditionalErrorStateSynchronous) { |
| 2536 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2537 | connect_job_factory_->set_job_type( |
| 2538 | TestConnectJob::kMockAdditionalErrorStateJob); |
| 2539 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2540 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2541 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2542 | EXPECT_EQ( |
| 2543 | ERR_CONNECTION_FAILED, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2544 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2545 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2546 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2547 | pool_.get(), NetLogWithSource())); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2548 | EXPECT_FALSE(handle.is_initialized()); |
| 2549 | EXPECT_FALSE(handle.socket()); |
| 2550 | EXPECT_TRUE(handle.is_ssl_error()); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 2551 | EXPECT_TRUE(handle.ssl_cert_request_info()); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2552 | } |
| 2553 | |
| 2554 | TEST_F(ClientSocketPoolBaseTest, AdditionalErrorStateAsynchronous) { |
| 2555 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2556 | |
| 2557 | connect_job_factory_->set_job_type( |
| 2558 | TestConnectJob::kMockPendingAdditionalErrorStateJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2559 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2560 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2561 | EXPECT_EQ( |
| 2562 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2563 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2564 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2565 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2566 | pool_.get(), NetLogWithSource())); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2567 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2568 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2569 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2570 | EXPECT_FALSE(handle.is_initialized()); |
| 2571 | EXPECT_FALSE(handle.socket()); |
| 2572 | EXPECT_TRUE(handle.is_ssl_error()); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 2573 | EXPECT_TRUE(handle.ssl_cert_request_info()); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2574 | } |
| 2575 | |
martijn | 003cd61 | 2016-05-19 22:24:38 | [diff] [blame] | 2576 | // Make sure we can reuse sockets. |
| 2577 | TEST_F(ClientSocketPoolBaseTest, CleanupTimedOutIdleSocketsReuse) { |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2578 | CreatePoolWithIdleTimeouts( |
| 2579 | kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2580 | base::TimeDelta(), // Time out unused sockets immediately. |
| 2581 | base::TimeDelta::FromDays(1)); // Don't time out used sockets. |
| 2582 | |
| 2583 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2584 | |
| 2585 | ClientSocketHandle handle; |
| 2586 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2587 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2588 | TestGroupId("a"), params_, base::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2589 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2590 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2591 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2592 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2593 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2594 | ASSERT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2595 | |
| 2596 | // Use and release the socket. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 2597 | EXPECT_EQ(1, handle.socket()->Write(nullptr, 1, CompletionOnceCallback(), |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 2598 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 2599 | TestLoadTimingInfoConnectedNotReused(handle); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2600 | handle.Reset(); |
| 2601 | |
| 2602 | // Should now have one idle socket. |
| 2603 | ASSERT_EQ(1, pool_->IdleSocketCount()); |
| 2604 | |
| 2605 | // Request a new socket. This should reuse the old socket and complete |
| 2606 | // synchronously. |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 2607 | RecordingBoundTestNetLog log; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2608 | rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2609 | TestGroupId("a"), params_, base::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2610 | ClientSocketPool::RespectLimits::ENABLED, CompletionOnceCallback(), |
| 2611 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2612 | ASSERT_THAT(rv, IsOk()); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2613 | EXPECT_TRUE(handle.is_reused()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 2614 | TestLoadTimingInfoConnectedReused(handle); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2615 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2616 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2617 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2618 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2619 | |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 2620 | auto entries = log.GetEntries(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2621 | EXPECT_TRUE(LogContainsEvent( |
| 2622 | entries, 0, NetLogEventType::TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET, |
| 2623 | NetLogEventPhase::NONE)); |
| 2624 | EXPECT_TRUE(LogContainsBeginEvent(entries, 1, NetLogEventType::SOCKET_POOL)); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2625 | EXPECT_TRUE(LogContainsEntryWithType( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2626 | entries, 2, NetLogEventType::SOCKET_POOL_REUSED_AN_EXISTING_SOCKET)); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2627 | } |
| 2628 | |
martijn | 003cd61 | 2016-05-19 22:24:38 | [diff] [blame] | 2629 | // Make sure we cleanup old unused sockets. |
Eric Roman | b49715e | 2018-04-24 22:41:17 | [diff] [blame] | 2630 | TEST_F(ClientSocketPoolBaseTest, CleanupTimedOutIdleSocketsNoReuse) { |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2631 | CreatePoolWithIdleTimeouts( |
| 2632 | kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 2633 | base::TimeDelta(), // Time out unused sockets immediately |
| 2634 | base::TimeDelta()); // Time out used sockets immediately |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2635 | |
| 2636 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2637 | |
| 2638 | // Startup two mock pending connect jobs, which will sit in the MessageLoop. |
| 2639 | |
| 2640 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2641 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2642 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2643 | TestGroupId("a"), params_, base::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2644 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2645 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2646 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2647 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2648 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2649 | |
| 2650 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2651 | TestCompletionCallback callback2; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2652 | rv = handle2.Init(TestGroupId("a"), params_, base::nullopt, LOWEST, |
| 2653 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2654 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2655 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2656 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2657 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2658 | pool_->GetLoadState(TestGroupId("a"), &handle2)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2659 | |
| 2660 | // Cancel one of the requests. Wait for the other, which will get the first |
| 2661 | // job. Release the socket. Run the loop again to make sure the second |
| 2662 | // socket is sitting idle and the first one is released (since ReleaseSocket() |
| 2663 | // just posts a DoReleaseSocket() task). |
| 2664 | |
| 2665 | handle.Reset(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2666 | ASSERT_THAT(callback2.WaitForResult(), IsOk()); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 2667 | // Get the NetLogSource for the socket, so the time out reason can be checked |
| 2668 | // at the end of the test. |
| 2669 | NetLogSource net_log_source2 = handle2.socket()->NetLog().source(); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2670 | // Use the socket. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 2671 | EXPECT_EQ(1, handle2.socket()->Write(nullptr, 1, CompletionOnceCallback(), |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 2672 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2673 | handle2.Reset(); |
| 2674 | |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2675 | // We post all of our delayed tasks with a 2ms delay. I.e. they don't |
| 2676 | // actually become pending until 2ms after they have been created. In order |
| 2677 | // to flush all tasks, we need to wait so that we know there are no |
| 2678 | // soon-to-be-pending tasks waiting. |
Alex Clarke | 0def209 | 2018-12-10 12:01:45 | [diff] [blame] | 2679 | FastForwardBy(base::TimeDelta::FromMilliseconds(10)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2680 | |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2681 | // Both sockets should now be idle. |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2682 | ASSERT_EQ(2, pool_->IdleSocketCount()); |
| 2683 | |
| 2684 | // Request a new socket. This should cleanup the unused and timed out ones. |
| 2685 | // A new socket will be created rather than reusing the idle one. |
Matt Mueller | d9342e3a | 2019-11-26 01:41:14 | [diff] [blame] | 2686 | RecordingBoundTestNetLog log; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2687 | TestCompletionCallback callback3; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2688 | rv = handle.Init(TestGroupId("a"), params_, base::nullopt, LOWEST, |
| 2689 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2690 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2691 | pool_.get(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2692 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 2693 | ASSERT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2694 | EXPECT_FALSE(handle.is_reused()); |
| 2695 | |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2696 | // Make sure the idle socket is closed. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2697 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2698 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2699 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2700 | |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 2701 | auto entries = log.GetEntries(); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2702 | EXPECT_FALSE(LogContainsEntryWithType( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 2703 | entries, 1, NetLogEventType::SOCKET_POOL_REUSED_AN_EXISTING_SOCKET)); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 2704 | ExpectSocketClosedWithReason( |
| 2705 | net_log_source2, TransportClientSocketPool::kIdleTimeLimitExpired); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2706 | } |
| 2707 | |
[email protected] | 2041cf34 | 2010-02-19 03:15:59 | [diff] [blame] | 2708 | // Make sure that we process all pending requests even when we're stalling |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2709 | // because of multiple releasing disconnected sockets. |
| 2710 | TEST_F(ClientSocketPoolBaseTest, MultipleReleasingDisconnectedSockets) { |
| 2711 | CreatePoolWithIdleTimeouts( |
| 2712 | kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 2713 | base::TimeDelta(), // Time out unused sockets immediately. |
| 2714 | base::TimeDelta::FromDays(1)); // Don't time out used sockets. |
| 2715 | |
| 2716 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 2717 | |
| 2718 | // Startup 4 connect jobs. Two of them will be pending. |
| 2719 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2720 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2721 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2722 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2723 | TestGroupId("a"), params_, base::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2724 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2725 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2726 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2727 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2728 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2729 | TestCompletionCallback callback2; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2730 | rv = handle2.Init(TestGroupId("a"), params_, base::nullopt, LOWEST, |
| 2731 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2732 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2733 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2734 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2735 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2736 | ClientSocketHandle handle3; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2737 | TestCompletionCallback callback3; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2738 | rv = handle3.Init(TestGroupId("a"), params_, base::nullopt, LOWEST, |
| 2739 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2740 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2741 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2742 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2743 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2744 | ClientSocketHandle handle4; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2745 | TestCompletionCallback callback4; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2746 | rv = handle4.Init(TestGroupId("a"), params_, base::nullopt, LOWEST, |
| 2747 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2748 | callback4.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2749 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2750 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2751 | |
| 2752 | // Release two disconnected sockets. |
| 2753 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2754 | handle.socket()->Disconnect(); |
| 2755 | handle.Reset(); |
| 2756 | handle2.socket()->Disconnect(); |
| 2757 | handle2.Reset(); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2758 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2759 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2760 | EXPECT_FALSE(handle3.is_reused()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2761 | EXPECT_THAT(callback4.WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2762 | EXPECT_FALSE(handle4.is_reused()); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2763 | } |
| 2764 | |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2765 | // Regression test for http://crbug.com/42267. |
| 2766 | // When DoReleaseSocket() is processed for one socket, it is blocked because the |
| 2767 | // other stalled groups all have releasing sockets, so no progress can be made. |
| 2768 | TEST_F(ClientSocketPoolBaseTest, SocketLimitReleasingSockets) { |
| 2769 | CreatePoolWithIdleTimeouts( |
| 2770 | 4 /* socket limit */, 4 /* socket limit per group */, |
| 2771 | base::TimeDelta(), // Time out unused sockets immediately. |
| 2772 | base::TimeDelta::FromDays(1)); // Don't time out used sockets. |
| 2773 | |
| 2774 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 2775 | |
| 2776 | // Max out the socket limit with 2 per group. |
| 2777 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2778 | ClientSocketHandle handle_a[4]; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2779 | TestCompletionCallback callback_a[4]; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2780 | ClientSocketHandle handle_b[4]; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2781 | TestCompletionCallback callback_b[4]; |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2782 | |
| 2783 | for (int i = 0; i < 2; ++i) { |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2784 | EXPECT_EQ(OK, handle_a[i].Init(TestGroupId("a"), params_, base::nullopt, |
| 2785 | LOWEST, SocketTag(), |
| 2786 | ClientSocketPool::RespectLimits::ENABLED, |
| 2787 | callback_a[i].callback(), |
| 2788 | ClientSocketPool::ProxyAuthCallback(), |
| 2789 | pool_.get(), NetLogWithSource())); |
| 2790 | EXPECT_EQ(OK, handle_b[i].Init(TestGroupId("b"), params_, base::nullopt, |
| 2791 | LOWEST, SocketTag(), |
| 2792 | ClientSocketPool::RespectLimits::ENABLED, |
| 2793 | callback_b[i].callback(), |
| 2794 | ClientSocketPool::ProxyAuthCallback(), |
| 2795 | pool_.get(), NetLogWithSource())); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2796 | } |
[email protected] | b89f7e4 | 2010-05-20 20:37:00 | [diff] [blame] | 2797 | |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2798 | // Make 4 pending requests, 2 per group. |
| 2799 | |
| 2800 | for (int i = 2; i < 4; ++i) { |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2801 | EXPECT_EQ( |
| 2802 | ERR_IO_PENDING, |
| 2803 | handle_a[i].Init(TestGroupId("a"), params_, base::nullopt, LOWEST, |
| 2804 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2805 | callback_a[i].callback(), |
| 2806 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2807 | NetLogWithSource())); |
| 2808 | EXPECT_EQ( |
| 2809 | ERR_IO_PENDING, |
| 2810 | handle_b[i].Init(TestGroupId("b"), params_, base::nullopt, LOWEST, |
| 2811 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2812 | callback_b[i].callback(), |
| 2813 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2814 | NetLogWithSource())); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2815 | } |
| 2816 | |
| 2817 | // Release b's socket first. The order is important, because in |
| 2818 | // DoReleaseSocket(), we'll process b's released socket, and since both b and |
| 2819 | // a are stalled, but 'a' is lower lexicographically, we'll process group 'a' |
| 2820 | // first, which has a releasing socket, so it refuses to start up another |
| 2821 | // ConnectJob. So, we used to infinite loop on this. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2822 | handle_b[0].socket()->Disconnect(); |
| 2823 | handle_b[0].Reset(); |
| 2824 | handle_a[0].socket()->Disconnect(); |
| 2825 | handle_a[0].Reset(); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2826 | |
| 2827 | // Used to get stuck here. |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 2828 | base::RunLoop().RunUntilIdle(); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2829 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2830 | handle_b[1].socket()->Disconnect(); |
| 2831 | handle_b[1].Reset(); |
| 2832 | handle_a[1].socket()->Disconnect(); |
| 2833 | handle_a[1].Reset(); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2834 | |
| 2835 | for (int i = 2; i < 4; ++i) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2836 | EXPECT_THAT(callback_b[i].WaitForResult(), IsOk()); |
| 2837 | EXPECT_THAT(callback_a[i].WaitForResult(), IsOk()); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2838 | } |
| 2839 | } |
| 2840 | |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2841 | TEST_F(ClientSocketPoolBaseTest, |
| 2842 | ReleasingDisconnectedSocketsMaintainsPriorityOrder) { |
| 2843 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2844 | |
| 2845 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2846 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 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)); |
| 2853 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2854 | IsError(ERR_IO_PENDING)); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2855 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2856 | EXPECT_THAT((*requests())[0]->WaitForResult(), IsOk()); |
| 2857 | EXPECT_THAT((*requests())[1]->WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2858 | EXPECT_EQ(2u, completion_count()); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2859 | |
| 2860 | // Releases one connection. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2861 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::NO_KEEP_ALIVE)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2862 | EXPECT_THAT((*requests())[2]->WaitForResult(), IsOk()); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2863 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2864 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::NO_KEEP_ALIVE)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2865 | EXPECT_THAT((*requests())[3]->WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2866 | EXPECT_EQ(4u, completion_count()); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2867 | |
| 2868 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 2869 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 2870 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 2871 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 2872 | |
| 2873 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2874 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(5)); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2875 | } |
| 2876 | |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2877 | class TestReleasingSocketRequest : public TestCompletionCallbackBase { |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2878 | public: |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2879 | TestReleasingSocketRequest(TransportClientSocketPool* pool, |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2880 | int expected_result, |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2881 | bool reset_releasing_handle) |
| 2882 | : pool_(pool), |
| 2883 | expected_result_(expected_result), |
Bence Béky | 8ddc249 | 2018-06-13 01:02:04 | [diff] [blame] | 2884 | reset_releasing_handle_(reset_releasing_handle) {} |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2885 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 2886 | ~TestReleasingSocketRequest() override = default; |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2887 | |
| 2888 | ClientSocketHandle* handle() { return &handle_; } |
| 2889 | |
Bence Béky | 8ddc249 | 2018-06-13 01:02:04 | [diff] [blame] | 2890 | CompletionOnceCallback callback() { |
| 2891 | return base::BindOnce(&TestReleasingSocketRequest::OnComplete, |
| 2892 | base::Unretained(this)); |
| 2893 | } |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2894 | |
| 2895 | private: |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2896 | void OnComplete(int result) { |
| 2897 | SetResult(result); |
| 2898 | if (reset_releasing_handle_) |
| 2899 | handle_.Reset(); |
| 2900 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2901 | EXPECT_EQ( |
| 2902 | expected_result_, |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 2903 | handle2_.Init( |
| 2904 | TestGroupId("a"), |
| 2905 | ClientSocketPool::SocketParams::CreateForHttpForTesting(), |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2906 | base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 2907 | ClientSocketPool::RespectLimits::ENABLED, CompletionOnceCallback(), |
| 2908 | ClientSocketPool::ProxyAuthCallback(), pool_, NetLogWithSource())); |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2909 | } |
| 2910 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2911 | TransportClientSocketPool* const pool_; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2912 | int expected_result_; |
| 2913 | bool reset_releasing_handle_; |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2914 | ClientSocketHandle handle_; |
| 2915 | ClientSocketHandle handle2_; |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2916 | }; |
| 2917 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2918 | |
| 2919 | TEST_F(ClientSocketPoolBaseTest, AdditionalErrorSocketsDontUseSlot) { |
| 2920 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2921 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2922 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
| 2923 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 2924 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2925 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2926 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2927 | client_socket_factory_.allocation_count()); |
| 2928 | |
| 2929 | connect_job_factory_->set_job_type( |
| 2930 | TestConnectJob::kMockPendingAdditionalErrorStateJob); |
| 2931 | TestReleasingSocketRequest req(pool_.get(), OK, false); |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2932 | EXPECT_EQ(ERR_IO_PENDING, |
| 2933 | req.handle()->Init( |
| 2934 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2935 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2936 | req.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2937 | pool_.get(), NetLogWithSource())); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2938 | // The next job should complete synchronously |
| 2939 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 2940 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2941 | EXPECT_THAT(req.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2942 | EXPECT_FALSE(req.handle()->is_initialized()); |
| 2943 | EXPECT_FALSE(req.handle()->socket()); |
| 2944 | EXPECT_TRUE(req.handle()->is_ssl_error()); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 2945 | EXPECT_TRUE(req.handle()->ssl_cert_request_info()); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2946 | } |
| 2947 | |
[email protected] | b6501d3d | 2010-06-03 23:53:34 | [diff] [blame] | 2948 | // http://crbug.com/44724 regression test. |
| 2949 | // We start releasing the pool when we flush on network change. When that |
| 2950 | // happens, the only active references are in the ClientSocketHandles. When a |
| 2951 | // ConnectJob completes and calls back into the last ClientSocketHandle, that |
| 2952 | // callback can release the last reference and delete the pool. After the |
| 2953 | // callback finishes, we go back to the stack frame within the now-deleted pool. |
| 2954 | // Executing any code that refers to members of the now-deleted pool can cause |
| 2955 | // crashes. |
| 2956 | TEST_F(ClientSocketPoolBaseTest, CallbackThatReleasesPool) { |
| 2957 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2958 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
| 2959 | |
| 2960 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2961 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2962 | EXPECT_EQ( |
| 2963 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2964 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2965 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2966 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2967 | pool_.get(), NetLogWithSource())); |
[email protected] | b6501d3d | 2010-06-03 23:53:34 | [diff] [blame] | 2968 | |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 2969 | pool_->FlushWithError(ERR_NETWORK_CHANGED, "Network changed"); |
[email protected] | b6501d3d | 2010-06-03 23:53:34 | [diff] [blame] | 2970 | |
| 2971 | // We'll call back into this now. |
| 2972 | callback.WaitForResult(); |
| 2973 | } |
| 2974 | |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2975 | TEST_F(ClientSocketPoolBaseTest, DoNotReuseSocketAfterFlush) { |
| 2976 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2977 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2978 | |
| 2979 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2980 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2981 | EXPECT_EQ( |
| 2982 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2983 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2984 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2985 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2986 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2987 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2988 | EXPECT_EQ(ClientSocketHandle::UNUSED, handle.reuse_type()); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 2989 | NetLogSource source = handle.socket()->NetLog().source(); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2990 | |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 2991 | pool_->FlushWithError(ERR_NETWORK_CHANGED, "Network changed"); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2992 | |
| 2993 | handle.Reset(); |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 2994 | base::RunLoop().RunUntilIdle(); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2995 | |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2996 | EXPECT_EQ( |
| 2997 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2998 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2999 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 3000 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3001 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3002 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 3003 | EXPECT_EQ(ClientSocketHandle::UNUSED, handle.reuse_type()); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 3004 | |
| 3005 | ExpectSocketClosedWithReason( |
| 3006 | source, TransportClientSocketPool::kSocketGenerationOutOfDate); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 3007 | } |
| 3008 | |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3009 | class ConnectWithinCallback : public TestCompletionCallbackBase { |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 3010 | public: |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3011 | ConnectWithinCallback( |
| 3012 | const ClientSocketPool::GroupId& group_id, |
Matt Menke | 84d11e56 | 2019-03-27 00:11:19 | [diff] [blame] | 3013 | const scoped_refptr<ClientSocketPool::SocketParams>& params, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3014 | TransportClientSocketPool* pool) |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3015 | : group_id_(group_id), params_(params), pool_(pool) {} |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 3016 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 3017 | ~ConnectWithinCallback() override = default; |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 3018 | |
| 3019 | int WaitForNestedResult() { |
| 3020 | return nested_callback_.WaitForResult(); |
| 3021 | } |
| 3022 | |
Bence Béky | 8ddc249 | 2018-06-13 01:02:04 | [diff] [blame] | 3023 | CompletionOnceCallback callback() { |
| 3024 | return base::BindOnce(&ConnectWithinCallback::OnComplete, |
| 3025 | base::Unretained(this)); |
| 3026 | } |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3027 | |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 3028 | private: |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3029 | void OnComplete(int result) { |
| 3030 | SetResult(result); |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3031 | EXPECT_EQ( |
| 3032 | ERR_IO_PENDING, |
| 3033 | handle_.Init(group_id_, params_, base::nullopt, DEFAULT_PRIORITY, |
| 3034 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 3035 | nested_callback_.callback(), |
| 3036 | ClientSocketPool::ProxyAuthCallback(), pool_, |
| 3037 | NetLogWithSource())); |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3038 | } |
| 3039 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3040 | const ClientSocketPool::GroupId group_id_; |
Matt Menke | 84d11e56 | 2019-03-27 00:11:19 | [diff] [blame] | 3041 | const scoped_refptr<ClientSocketPool::SocketParams> params_; |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3042 | TransportClientSocketPool* const pool_; |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 3043 | ClientSocketHandle handle_; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3044 | TestCompletionCallback nested_callback_; |
| 3045 | |
| 3046 | DISALLOW_COPY_AND_ASSIGN(ConnectWithinCallback); |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 3047 | }; |
| 3048 | |
| 3049 | TEST_F(ClientSocketPoolBaseTest, AbortAllRequestsOnFlush) { |
| 3050 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3051 | |
| 3052 | // First job will be waiting until it gets aborted. |
| 3053 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3054 | |
| 3055 | ClientSocketHandle handle; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3056 | ConnectWithinCallback callback(TestGroupId("a"), params_, pool_.get()); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3057 | EXPECT_EQ( |
| 3058 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3059 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3060 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 3061 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3062 | pool_.get(), NetLogWithSource())); |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 3063 | |
| 3064 | // Second job will be started during the first callback, and will |
| 3065 | // asynchronously complete with OK. |
| 3066 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 3067 | pool_->FlushWithError(ERR_NETWORK_CHANGED, "Network changed"); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3068 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_NETWORK_CHANGED)); |
| 3069 | EXPECT_THAT(callback.WaitForNestedResult(), IsOk()); |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 3070 | } |
| 3071 | |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 3072 | TEST_F(ClientSocketPoolBaseTest, BackupSocketWaitsForHostResolution) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3073 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets, |
| 3074 | true /* enable_backup_connect_jobs */); |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 3075 | |
| 3076 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3077 | ClientSocketHandle handle; |
| 3078 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3079 | EXPECT_EQ( |
| 3080 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3081 | handle.Init(TestGroupId("bar"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3082 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 3083 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3084 | pool_.get(), NetLogWithSource())); |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 3085 | // The backup timer fires but doesn't start a new ConnectJob while resolving |
| 3086 | // the hostname. |
| 3087 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_RESOLVING_HOST); |
| 3088 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
| 3089 | ClientSocketPool::kMaxConnectRetryIntervalMs * 100)); |
| 3090 | EXPECT_EQ(1, client_socket_factory_.allocation_count()); |
| 3091 | |
| 3092 | // Once the ConnectJob has finished resolving the hostname, the backup timer |
| 3093 | // will create a ConnectJob when it fires. |
| 3094 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_CONNECTING); |
| 3095 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
| 3096 | ClientSocketPool::kMaxConnectRetryIntervalMs)); |
| 3097 | EXPECT_EQ(2, client_socket_factory_.allocation_count()); |
| 3098 | } |
| 3099 | |
| 3100 | // Test that no backup socket is created when a ConnectJob connects before it |
| 3101 | // completes. |
| 3102 | TEST_F(ClientSocketPoolBaseTest, NoBackupSocketWhenConnected) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3103 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets, |
| 3104 | true /* enable_backup_connect_jobs */); |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 3105 | |
| 3106 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3107 | ClientSocketHandle handle; |
| 3108 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3109 | EXPECT_EQ( |
| 3110 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3111 | handle.Init(TestGroupId("bar"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3112 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 3113 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3114 | pool_.get(), NetLogWithSource())); |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 3115 | // The backup timer fires but doesn't start a new ConnectJob while resolving |
| 3116 | // the hostname. |
| 3117 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_RESOLVING_HOST); |
| 3118 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
| 3119 | ClientSocketPool::kMaxConnectRetryIntervalMs * 100)); |
| 3120 | EXPECT_EQ(1, client_socket_factory_.allocation_count()); |
| 3121 | |
| 3122 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE); |
| 3123 | client_socket_factory_.SetJobHasEstablishedConnection(0); |
| 3124 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
| 3125 | ClientSocketPool::kMaxConnectRetryIntervalMs * 100)); |
| 3126 | EXPECT_EQ(1, client_socket_factory_.allocation_count()); |
| 3127 | } |
| 3128 | |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3129 | // Cancel a pending socket request while we're at max sockets, |
| 3130 | // and verify that the backup socket firing doesn't cause a crash. |
| 3131 | TEST_F(ClientSocketPoolBaseTest, BackupSocketCancelAtMaxSockets) { |
| 3132 | // Max 4 sockets globally, max 4 sockets per group. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3133 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets, |
| 3134 | true /* enable_backup_connect_jobs */); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3135 | |
[email protected] | 4baaf9d | 2010-08-31 15:15:44 | [diff] [blame] | 3136 | // Create the first socket and set to ERR_IO_PENDING. This starts the backup |
| 3137 | // timer. |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3138 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3139 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3140 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3141 | EXPECT_EQ( |
| 3142 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3143 | handle.Init(TestGroupId("bar"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3144 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 3145 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3146 | pool_.get(), NetLogWithSource())); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3147 | |
| 3148 | // Start (MaxSockets - 1) connected sockets to reach max sockets. |
| 3149 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 3150 | ClientSocketHandle handles[kDefaultMaxSockets]; |
| 3151 | for (int i = 1; i < kDefaultMaxSockets; ++i) { |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3152 | TestCompletionCallback callback; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3153 | EXPECT_EQ(OK, handles[i].Init(TestGroupId("bar"), params_, base::nullopt, |
| 3154 | DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3155 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3156 | callback.callback(), |
| 3157 | ClientSocketPool::ProxyAuthCallback(), |
| 3158 | pool_.get(), NetLogWithSource())); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3159 | } |
| 3160 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3161 | base::RunLoop().RunUntilIdle(); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3162 | |
| 3163 | // Cancel the pending request. |
| 3164 | handle.Reset(); |
| 3165 | |
| 3166 | // 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] | 3167 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
[email protected] | 26b997396 | 2012-01-28 00:57:00 | [diff] [blame] | 3168 | ClientSocketPool::kMaxConnectRetryIntervalMs / 2 * 3)); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3169 | |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3170 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 3171 | } |
| 3172 | |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3173 | TEST_F(ClientSocketPoolBaseTest, CancelBackupSocketAfterCancelingAllRequests) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3174 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets, |
| 3175 | true /* enable_backup_connect_jobs */); |
[email protected] | 4baaf9d | 2010-08-31 15:15:44 | [diff] [blame] | 3176 | |
| 3177 | // Create the first socket and set to ERR_IO_PENDING. This starts the backup |
| 3178 | // timer. |
| 3179 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3180 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3181 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3182 | EXPECT_EQ( |
| 3183 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3184 | handle.Init(TestGroupId("bar"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3185 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 3186 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3187 | pool_.get(), NetLogWithSource())); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3188 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("bar"))); |
| 3189 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("bar"))); |
| 3190 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3191 | TestGroupId("bar"))); |
| 3192 | EXPECT_EQ( |
| 3193 | 0u, pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("bar"))); |
[email protected] | 4baaf9d | 2010-08-31 15:15:44 | [diff] [blame] | 3194 | |
| 3195 | // Cancel the socket request. This should cancel the backup timer. Wait for |
| 3196 | // the backup time to see if it indeed got canceled. |
| 3197 | handle.Reset(); |
| 3198 | // 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] | 3199 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
[email protected] | 26b997396 | 2012-01-28 00:57:00 | [diff] [blame] | 3200 | ClientSocketPool::kMaxConnectRetryIntervalMs / 2 * 3)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3201 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("bar"))); |
| 3202 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("bar"))); |
[email protected] | 4baaf9d | 2010-08-31 15:15:44 | [diff] [blame] | 3203 | } |
| 3204 | |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3205 | TEST_F(ClientSocketPoolBaseTest, CancelBackupSocketAfterFinishingAllRequests) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3206 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets, |
| 3207 | true /* enable_backup_connect_jobs */); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3208 | |
| 3209 | // Create the first socket and set to ERR_IO_PENDING. This starts the backup |
| 3210 | // timer. |
| 3211 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3212 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3213 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3214 | EXPECT_EQ( |
| 3215 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3216 | handle.Init(TestGroupId("bar"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3217 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 3218 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3219 | pool_.get(), NetLogWithSource())); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3220 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3221 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3222 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3223 | EXPECT_EQ( |
| 3224 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3225 | handle2.Init(TestGroupId("bar"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3226 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3227 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3228 | pool_.get(), NetLogWithSource())); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3229 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("bar"))); |
| 3230 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("bar"))); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3231 | |
| 3232 | // Cancel request 1 and then complete request 2. With the requests finished, |
| 3233 | // the backup timer should be cancelled. |
| 3234 | handle.Reset(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3235 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3236 | // 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] | 3237 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
[email protected] | 26b997396 | 2012-01-28 00:57:00 | [diff] [blame] | 3238 | ClientSocketPool::kMaxConnectRetryIntervalMs / 2 * 3)); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3239 | } |
| 3240 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3241 | // Test delayed socket binding for the case where we have two connects, |
| 3242 | // and while one is waiting on a connect, the other frees up. |
| 3243 | // The socket waiting on a connect should switch immediately to the freed |
| 3244 | // up socket. |
| 3245 | TEST_F(ClientSocketPoolBaseTest, DelayedSocketBindingWaitingForConnect) { |
| 3246 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3247 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3248 | |
| 3249 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3250 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3251 | EXPECT_EQ( |
| 3252 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3253 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3254 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3255 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3256 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3257 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3258 | |
| 3259 | // No idle sockets, no pending jobs. |
| 3260 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3261 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3262 | |
| 3263 | // Create a second socket to the same host, but this one will wait. |
| 3264 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3265 | ClientSocketHandle handle2; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3266 | EXPECT_EQ( |
| 3267 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3268 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3269 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3270 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3271 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3272 | // No idle sockets, and one connecting job. |
| 3273 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3274 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3275 | |
| 3276 | // Return the first handle to the pool. This will initiate the delayed |
| 3277 | // binding. |
| 3278 | handle1.Reset(); |
| 3279 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3280 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3281 | |
| 3282 | // Still no idle sockets, still one pending connect job. |
| 3283 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3284 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3285 | |
| 3286 | // The second socket connected, even though it was a Waiting Job. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3287 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3288 | |
| 3289 | // And we can see there is still one job waiting. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3290 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3291 | |
| 3292 | // Finally, signal the waiting Connect. |
| 3293 | client_socket_factory_.SignalJobs(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3294 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3295 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3296 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3297 | } |
| 3298 | |
| 3299 | // Test delayed socket binding when a group is at capacity and one |
| 3300 | // of the group's sockets frees up. |
| 3301 | TEST_F(ClientSocketPoolBaseTest, DelayedSocketBindingAtGroupCapacity) { |
| 3302 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3303 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3304 | |
| 3305 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3306 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3307 | EXPECT_EQ( |
| 3308 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3309 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3310 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3311 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3312 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3313 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3314 | |
| 3315 | // No idle sockets, no pending jobs. |
| 3316 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3317 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3318 | |
| 3319 | // Create a second socket to the same host, but this one will wait. |
| 3320 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3321 | ClientSocketHandle handle2; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3322 | EXPECT_EQ( |
| 3323 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3324 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3325 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3326 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3327 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3328 | // No idle sockets, and one connecting job. |
| 3329 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3330 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3331 | |
| 3332 | // Return the first handle to the pool. This will initiate the delayed |
| 3333 | // binding. |
| 3334 | handle1.Reset(); |
| 3335 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3336 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3337 | |
| 3338 | // Still no idle sockets, still one pending connect job. |
| 3339 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3340 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3341 | |
| 3342 | // The second socket connected, even though it was a Waiting Job. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3343 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3344 | |
| 3345 | // And we can see there is still one job waiting. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3346 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3347 | |
| 3348 | // Finally, signal the waiting Connect. |
| 3349 | client_socket_factory_.SignalJobs(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3350 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3351 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3352 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3353 | } |
| 3354 | |
| 3355 | // Test out the case where we have one socket connected, one |
| 3356 | // connecting, when the first socket finishes and goes idle. |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3357 | // Although the second connection is pending, the second request |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3358 | // should complete, by taking the first socket's idle socket. |
| 3359 | TEST_F(ClientSocketPoolBaseTest, DelayedSocketBindingAtStall) { |
| 3360 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3361 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3362 | |
| 3363 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3364 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3365 | EXPECT_EQ( |
| 3366 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3367 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3368 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3369 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3370 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3371 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3372 | |
| 3373 | // No idle sockets, no pending jobs. |
| 3374 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3375 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3376 | |
| 3377 | // Create a second socket to the same host, but this one will wait. |
| 3378 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3379 | ClientSocketHandle handle2; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3380 | EXPECT_EQ( |
| 3381 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3382 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3383 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3384 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3385 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3386 | // No idle sockets, and one connecting job. |
| 3387 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3388 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3389 | |
| 3390 | // Return the first handle to the pool. This will initiate the delayed |
| 3391 | // binding. |
| 3392 | handle1.Reset(); |
| 3393 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3394 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3395 | |
| 3396 | // Still no idle sockets, still one pending connect job. |
| 3397 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3398 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3399 | |
| 3400 | // The second socket connected, even though it was a Waiting Job. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3401 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3402 | |
| 3403 | // And we can see there is still one job waiting. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3404 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3405 | |
| 3406 | // Finally, signal the waiting Connect. |
| 3407 | client_socket_factory_.SignalJobs(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3408 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3409 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3410 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3411 | } |
| 3412 | |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3413 | // Cover the case where on an available socket slot, we have one pending |
| 3414 | // request that completes synchronously, thereby making the Group empty. |
| 3415 | TEST_F(ClientSocketPoolBaseTest, SynchronouslyProcessOnePendingRequest) { |
| 3416 | const int kUnlimitedSockets = 100; |
| 3417 | const int kOneSocketPerGroup = 1; |
| 3418 | CreatePool(kUnlimitedSockets, kOneSocketPerGroup); |
| 3419 | |
| 3420 | // Make the first request asynchronous fail. |
| 3421 | // This will free up a socket slot later. |
| 3422 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
| 3423 | |
| 3424 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3425 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3426 | EXPECT_EQ( |
| 3427 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3428 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3429 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3430 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3431 | pool_.get(), NetLogWithSource())); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3432 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3433 | |
| 3434 | // Make the second request synchronously fail. This should make the Group |
| 3435 | // empty. |
| 3436 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
| 3437 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3438 | TestCompletionCallback callback2; |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3439 | // It'll be ERR_IO_PENDING now, but the TestConnectJob will synchronously fail |
| 3440 | // when created. |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3441 | EXPECT_EQ( |
| 3442 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3443 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3444 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3445 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3446 | pool_.get(), NetLogWithSource())); |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3447 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3448 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3449 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3450 | EXPECT_THAT(callback1.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 3451 | EXPECT_THAT(callback2.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3452 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3453 | } |
| 3454 | |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3455 | TEST_F(ClientSocketPoolBaseTest, PreferUsedSocketToUnusedSocket) { |
| 3456 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 3457 | |
| 3458 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3459 | |
| 3460 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3461 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3462 | EXPECT_EQ( |
| 3463 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3464 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3465 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3466 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3467 | pool_.get(), NetLogWithSource())); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3468 | |
| 3469 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3470 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3471 | EXPECT_EQ( |
| 3472 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3473 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3474 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3475 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3476 | pool_.get(), NetLogWithSource())); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3477 | ClientSocketHandle handle3; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3478 | TestCompletionCallback callback3; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3479 | EXPECT_EQ( |
| 3480 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3481 | handle3.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3482 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3483 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3484 | pool_.get(), NetLogWithSource())); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3485 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3486 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3487 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
| 3488 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3489 | |
| 3490 | // Use the socket. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 3491 | EXPECT_EQ(1, handle1.socket()->Write(nullptr, 1, CompletionOnceCallback(), |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 3492 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 3493 | EXPECT_EQ(1, handle3.socket()->Write(nullptr, 1, CompletionOnceCallback(), |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 3494 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3495 | |
| 3496 | handle1.Reset(); |
| 3497 | handle2.Reset(); |
| 3498 | handle3.Reset(); |
| 3499 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3500 | EXPECT_EQ(OK, handle1.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3501 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3502 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3503 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3504 | pool_.get(), NetLogWithSource())); |
| 3505 | EXPECT_EQ(OK, handle2.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3506 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3507 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3508 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3509 | pool_.get(), NetLogWithSource())); |
| 3510 | EXPECT_EQ(OK, handle3.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3511 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3512 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3513 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3514 | pool_.get(), NetLogWithSource())); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3515 | |
| 3516 | EXPECT_TRUE(handle1.socket()->WasEverUsed()); |
| 3517 | EXPECT_TRUE(handle2.socket()->WasEverUsed()); |
| 3518 | EXPECT_FALSE(handle3.socket()->WasEverUsed()); |
| 3519 | } |
| 3520 | |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3521 | TEST_F(ClientSocketPoolBaseTest, RequestSockets) { |
| 3522 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3523 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3524 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3525 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 3526 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3527 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3528 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3529 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3530 | EXPECT_EQ(2u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3531 | TestGroupId("a"))); |
| 3532 | EXPECT_EQ(2u, |
| 3533 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3534 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3535 | |
| 3536 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3537 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3538 | EXPECT_EQ( |
| 3539 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3540 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3541 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3542 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3543 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3544 | |
| 3545 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3546 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3547 | EXPECT_EQ( |
| 3548 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3549 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3550 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3551 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3552 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3553 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3554 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3555 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3556 | TestGroupId("a"))); |
| 3557 | EXPECT_EQ(0u, |
| 3558 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3559 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3560 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3561 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3562 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3563 | handle1.Reset(); |
| 3564 | handle2.Reset(); |
| 3565 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3566 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3567 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3568 | TestGroupId("a"))); |
| 3569 | EXPECT_EQ(0u, |
| 3570 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3571 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3572 | } |
| 3573 | |
| 3574 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsWhenAlreadyHaveAConnectJob) { |
| 3575 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3576 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3577 | |
| 3578 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3579 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3580 | EXPECT_EQ( |
| 3581 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3582 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3583 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3584 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3585 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3586 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3587 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3588 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3589 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3590 | TestGroupId("a"))); |
| 3591 | EXPECT_EQ(0u, |
| 3592 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3593 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3594 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3595 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 3596 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3597 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3598 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3599 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3600 | TestGroupId("a"))); |
| 3601 | EXPECT_EQ(1u, |
| 3602 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3603 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3604 | |
| 3605 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3606 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3607 | EXPECT_EQ( |
| 3608 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3609 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3610 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3611 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3612 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3613 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3614 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3615 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3616 | TestGroupId("a"))); |
| 3617 | EXPECT_EQ(0u, |
| 3618 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3619 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3620 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3621 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3622 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3623 | handle1.Reset(); |
| 3624 | handle2.Reset(); |
| 3625 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3626 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3627 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3628 | TestGroupId("a"))); |
| 3629 | EXPECT_EQ(0u, |
| 3630 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3631 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3632 | } |
| 3633 | |
| 3634 | TEST_F(ClientSocketPoolBaseTest, |
| 3635 | RequestSocketsWhenAlreadyHaveMultipleConnectJob) { |
| 3636 | CreatePool(4, 4); |
| 3637 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3638 | |
| 3639 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3640 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3641 | EXPECT_EQ( |
| 3642 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3643 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3644 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3645 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3646 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3647 | |
| 3648 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3649 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3650 | EXPECT_EQ( |
| 3651 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3652 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3653 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3654 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3655 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3656 | |
| 3657 | ClientSocketHandle handle3; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3658 | TestCompletionCallback callback3; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3659 | EXPECT_EQ( |
| 3660 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3661 | handle3.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3662 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3663 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3664 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3665 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3666 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3667 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3668 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3669 | TestGroupId("a"))); |
| 3670 | EXPECT_EQ(0u, |
| 3671 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3672 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3673 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3674 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 3675 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3676 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3677 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3678 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3679 | TestGroupId("a"))); |
| 3680 | EXPECT_EQ(0u, |
| 3681 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3682 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3683 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3684 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3685 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
| 3686 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3687 | handle1.Reset(); |
| 3688 | handle2.Reset(); |
| 3689 | handle3.Reset(); |
| 3690 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3691 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3692 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3693 | TestGroupId("a"))); |
| 3694 | EXPECT_EQ(0u, |
| 3695 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3696 | EXPECT_EQ(3u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3697 | } |
| 3698 | |
| 3699 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsAtMaxSocketLimit) { |
| 3700 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 3701 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3702 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3703 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3704 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3705 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, |
| 3706 | kDefaultMaxSockets, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3707 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3708 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3709 | EXPECT_EQ(kDefaultMaxSockets, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3710 | static_cast<int>( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3711 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
| 3712 | EXPECT_EQ( |
| 3713 | kDefaultMaxSockets, |
| 3714 | static_cast<int>(pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3715 | TestGroupId("a")))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3716 | EXPECT_EQ(kDefaultMaxSockets, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3717 | static_cast<int>(pool_->NumUnassignedConnectJobsInGroupForTesting( |
| 3718 | TestGroupId("a")))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3719 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3720 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("b"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3721 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3722 | pool_->RequestSockets(TestGroupId("b"), params_, base::nullopt, |
| 3723 | kDefaultMaxSockets, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3724 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3725 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("b"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3726 | } |
| 3727 | |
| 3728 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsHitMaxSocketLimit) { |
| 3729 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 3730 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3731 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3732 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3733 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3734 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, |
| 3735 | kDefaultMaxSockets - 1, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3736 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3737 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3738 | EXPECT_EQ(kDefaultMaxSockets - 1, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3739 | static_cast<int>( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3740 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
| 3741 | EXPECT_EQ( |
| 3742 | kDefaultMaxSockets - 1, |
| 3743 | static_cast<int>(pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3744 | TestGroupId("a")))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3745 | EXPECT_EQ(kDefaultMaxSockets - 1, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3746 | static_cast<int>(pool_->NumUnassignedConnectJobsInGroupForTesting( |
| 3747 | TestGroupId("a")))); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 3748 | EXPECT_FALSE(pool_->IsStalled()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3749 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3750 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("b"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3751 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3752 | pool_->RequestSockets(TestGroupId("b"), params_, base::nullopt, |
| 3753 | kDefaultMaxSockets, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3754 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3755 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("b"))); |
| 3756 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 3757 | EXPECT_FALSE(pool_->IsStalled()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3758 | } |
| 3759 | |
| 3760 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsCountIdleSockets) { |
| 3761 | CreatePool(4, 4); |
| 3762 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3763 | |
| 3764 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3765 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3766 | EXPECT_EQ( |
| 3767 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3768 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3769 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3770 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3771 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3772 | ASSERT_THAT(callback1.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3773 | handle1.Reset(); |
| 3774 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3775 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3776 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3777 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3778 | TestGroupId("a"))); |
| 3779 | EXPECT_EQ(0u, |
| 3780 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3781 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3782 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3783 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 3784 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3785 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3786 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3787 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3788 | TestGroupId("a"))); |
| 3789 | EXPECT_EQ(1u, |
| 3790 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3791 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3792 | } |
| 3793 | |
| 3794 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsCountActiveSockets) { |
| 3795 | CreatePool(4, 4); |
| 3796 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3797 | |
| 3798 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3799 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3800 | EXPECT_EQ( |
| 3801 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3802 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3803 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3804 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3805 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3806 | ASSERT_THAT(callback1.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3807 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3808 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3809 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3810 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3811 | TestGroupId("a"))); |
| 3812 | EXPECT_EQ(0u, |
| 3813 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3814 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3815 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3816 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3817 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 3818 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3819 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3820 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3821 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3822 | TestGroupId("a"))); |
| 3823 | EXPECT_EQ(1u, |
| 3824 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3825 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3826 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3827 | } |
| 3828 | |
| 3829 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsSynchronous) { |
| 3830 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3831 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 3832 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3833 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, |
| 3834 | kDefaultMaxSocketsPerGroup, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3835 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3836 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3837 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3838 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3839 | TestGroupId("a"))); |
| 3840 | EXPECT_EQ(0u, |
| 3841 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3842 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3843 | static_cast<int>(pool_->IdleSocketCountInGroup(TestGroupId("a")))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3844 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3845 | pool_->RequestSockets(TestGroupId("b"), params_, base::nullopt, |
| 3846 | kDefaultMaxSocketsPerGroup, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3847 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3848 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 3849 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3850 | TestGroupId("b"))); |
| 3851 | EXPECT_EQ(0u, |
| 3852 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3853 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3854 | static_cast<int>(pool_->IdleSocketCountInGroup(TestGroupId("b")))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3855 | } |
| 3856 | |
[email protected] | 3c819f52 | 2010-12-02 02:03:12 | [diff] [blame] | 3857 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsSynchronousError) { |
| 3858 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3859 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
| 3860 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3861 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, |
| 3862 | kDefaultMaxSocketsPerGroup, NetLogWithSource()); |
[email protected] | 3c819f52 | 2010-12-02 02:03:12 | [diff] [blame] | 3863 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3864 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | fd2e53e | 2011-01-14 20:40:52 | [diff] [blame] | 3865 | |
| 3866 | connect_job_factory_->set_job_type( |
| 3867 | TestConnectJob::kMockAdditionalErrorStateJob); |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3868 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, |
| 3869 | kDefaultMaxSocketsPerGroup, NetLogWithSource()); |
[email protected] | fd2e53e | 2011-01-14 20:40:52 | [diff] [blame] | 3870 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3871 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | 3c819f52 | 2010-12-02 02:03:12 | [diff] [blame] | 3872 | } |
| 3873 | |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3874 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsMultipleTimesDoesNothing) { |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3875 | CreatePool(4, 4); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3876 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3877 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3878 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 3879 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3880 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3881 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3882 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3883 | EXPECT_EQ(2u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3884 | TestGroupId("a"))); |
| 3885 | EXPECT_EQ(2u, |
| 3886 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3887 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3888 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3889 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3890 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 3891 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3892 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3893 | EXPECT_EQ(2u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3894 | TestGroupId("a"))); |
| 3895 | EXPECT_EQ(2u, |
| 3896 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3897 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3898 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3899 | |
| 3900 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3901 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3902 | EXPECT_EQ( |
| 3903 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3904 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3905 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3906 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3907 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3908 | |
| 3909 | client_socket_factory_.SignalJob(0); |
| 3910 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3911 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3912 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3913 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3914 | TestGroupId("a"))); |
| 3915 | EXPECT_EQ(1u, |
| 3916 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3917 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3918 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3919 | |
| 3920 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3921 | TestCompletionCallback callback2; |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3922 | EXPECT_EQ( |
| 3923 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3924 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3925 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3926 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3927 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3928 | client_socket_factory_.SignalJob(0); |
| 3929 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3930 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3931 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3932 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3933 | TestGroupId("a"))); |
| 3934 | EXPECT_EQ(0u, |
| 3935 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3936 | EXPECT_EQ(2, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3937 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3938 | |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3939 | handle1.Reset(); |
| 3940 | handle2.Reset(); |
| 3941 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3942 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3943 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3944 | TestGroupId("a"))); |
| 3945 | EXPECT_EQ(0u, |
| 3946 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3947 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3948 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3949 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3950 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 3951 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3952 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3953 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3954 | TestGroupId("a"))); |
| 3955 | EXPECT_EQ(0u, |
| 3956 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3957 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3958 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3959 | } |
| 3960 | |
| 3961 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsDifferentNumSockets) { |
| 3962 | CreatePool(4, 4); |
| 3963 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3964 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3965 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 1, |
| 3966 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3967 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3968 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3969 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3970 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3971 | TestGroupId("a"))); |
| 3972 | EXPECT_EQ(1u, |
| 3973 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3974 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3975 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3976 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 3977 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3978 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3979 | EXPECT_EQ(2u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3980 | TestGroupId("a"))); |
| 3981 | EXPECT_EQ(2u, |
| 3982 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3983 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3984 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3985 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 3, |
| 3986 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3987 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3988 | EXPECT_EQ(3u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3989 | TestGroupId("a"))); |
| 3990 | EXPECT_EQ(3u, |
| 3991 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3992 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3993 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3994 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 1, |
| 3995 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3996 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3997 | EXPECT_EQ(3u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3998 | TestGroupId("a"))); |
| 3999 | EXPECT_EQ(3u, |
| 4000 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4001 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4002 | } |
| 4003 | |
| 4004 | TEST_F(ClientSocketPoolBaseTest, PreconnectJobsTakenByNormalRequests) { |
| 4005 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4006 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4007 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4008 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 1, |
| 4009 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4010 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4011 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 4012 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4013 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4014 | TestGroupId("a"))); |
| 4015 | EXPECT_EQ(1u, |
| 4016 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4017 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4018 | |
| 4019 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 4020 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4021 | EXPECT_EQ( |
| 4022 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4023 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4024 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4025 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4026 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4027 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4028 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4029 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4030 | TestGroupId("a"))); |
| 4031 | EXPECT_EQ(0u, |
| 4032 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4033 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4034 | |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4035 | client_socket_factory_.SignalJobs(); |
| 4036 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 4037 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4038 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4039 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4040 | TestGroupId("a"))); |
| 4041 | EXPECT_EQ(0u, |
| 4042 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4043 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4044 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4045 | |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4046 | // Make sure if a preconnected socket is not fully connected when a request |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 4047 | // starts, it has a connect start time. |
| 4048 | TestLoadTimingInfoConnectedNotReused(handle1); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4049 | handle1.Reset(); |
| 4050 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4051 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 4052 | } |
| 4053 | |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 4054 | // Checks that fully connected preconnect jobs have no connect times, and are |
| 4055 | // marked as reused. |
| 4056 | TEST_F(ClientSocketPoolBaseTest, ConnectedPreconnectJobsHaveNoConnectTimes) { |
| 4057 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4058 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4059 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 1, |
| 4060 | NetLogWithSource()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 4061 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4062 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 4063 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4064 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4065 | TestGroupId("a"))); |
| 4066 | EXPECT_EQ(0u, |
| 4067 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4068 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 4069 | |
| 4070 | ClientSocketHandle handle; |
| 4071 | TestCompletionCallback callback; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4072 | EXPECT_EQ(OK, handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4073 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4074 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4075 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4076 | pool_.get(), NetLogWithSource())); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 4077 | |
| 4078 | // Make sure the idle socket was used. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4079 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 4080 | |
| 4081 | TestLoadTimingInfoConnectedReused(handle); |
| 4082 | handle.Reset(); |
| 4083 | TestLoadTimingInfoNotConnected(handle); |
| 4084 | } |
| 4085 | |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4086 | // http://crbug.com/64940 regression test. |
| 4087 | TEST_F(ClientSocketPoolBaseTest, PreconnectClosesIdleSocketRemovesGroup) { |
| 4088 | const int kMaxTotalSockets = 3; |
| 4089 | const int kMaxSocketsPerGroup = 2; |
| 4090 | CreatePool(kMaxTotalSockets, kMaxSocketsPerGroup); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4091 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4092 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 4093 | // Note that group id ordering matters here. "a" comes before "b", so |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4094 | // CloseOneIdleSocket() will try to close "a"'s idle socket. |
| 4095 | |
| 4096 | // Set up one idle socket in "a". |
| 4097 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 4098 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4099 | EXPECT_EQ( |
| 4100 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4101 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4102 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4103 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4104 | pool_.get(), NetLogWithSource())); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4105 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 4106 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4107 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4108 | TestGroupId("a"))); |
| 4109 | EXPECT_EQ(0u, |
| 4110 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4111 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4112 | |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4113 | client_socket_factory_.SignalJobs(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4114 | ASSERT_THAT(callback1.WaitForResult(), IsOk()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4115 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4116 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4117 | TestGroupId("a"))); |
| 4118 | EXPECT_EQ(0u, |
| 4119 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4120 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4121 | |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4122 | handle1.Reset(); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4123 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4124 | |
| 4125 | // Set up two active sockets in "b". |
| 4126 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 4127 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4128 | EXPECT_EQ( |
| 4129 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4130 | handle1.Init(TestGroupId("b"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4131 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4132 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4133 | pool_.get(), NetLogWithSource())); |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4134 | EXPECT_EQ( |
| 4135 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4136 | handle2.Init(TestGroupId("b"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4137 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4138 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4139 | pool_.get(), NetLogWithSource())); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4140 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4141 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("b"))); |
| 4142 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 4143 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4144 | TestGroupId("b"))); |
| 4145 | EXPECT_EQ(0u, |
| 4146 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4147 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4148 | |
| 4149 | client_socket_factory_.SignalJobs(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4150 | ASSERT_THAT(callback1.WaitForResult(), IsOk()); |
| 4151 | ASSERT_THAT(callback2.WaitForResult(), IsOk()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4152 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4153 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4154 | TestGroupId("b"))); |
| 4155 | EXPECT_EQ(0u, |
| 4156 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 4157 | EXPECT_EQ(2, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4158 | |
| 4159 | // Now we have 1 idle socket in "a" and 2 active sockets in "b". This means |
| 4160 | // we've maxed out on sockets, since we set |kMaxTotalSockets| to 3. |
| 4161 | // Requesting 2 preconnected sockets for "a" should fail to allocate any more |
| 4162 | // sockets for "a", and "b" should still have 2 active sockets. |
| 4163 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4164 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 4165 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4166 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4167 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4168 | TestGroupId("a"))); |
| 4169 | EXPECT_EQ(0u, |
| 4170 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4171 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4172 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 4173 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 4174 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4175 | TestGroupId("b"))); |
| 4176 | EXPECT_EQ(0u, |
| 4177 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4178 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4179 | EXPECT_EQ(2, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4180 | |
| 4181 | // Now release the 2 active sockets for "b". This will give us 1 idle socket |
| 4182 | // in "a" and 2 idle sockets in "b". Requesting 2 preconnected sockets for |
| 4183 | // "a" should result in closing 1 for "b". |
| 4184 | handle1.Reset(); |
| 4185 | handle2.Reset(); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4186 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4187 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4188 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4189 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 4190 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4191 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4192 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4193 | TestGroupId("a"))); |
| 4194 | EXPECT_EQ(1u, |
| 4195 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4196 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4197 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 4198 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 4199 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4200 | TestGroupId("b"))); |
| 4201 | EXPECT_EQ(0u, |
| 4202 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4203 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4204 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4205 | } |
| 4206 | |
[email protected] | b7b8be4 | 2011-07-12 12:46:41 | [diff] [blame] | 4207 | TEST_F(ClientSocketPoolBaseTest, PreconnectWithoutBackupJob) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4208 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 4209 | true /* enable_backup_connect_jobs */); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4210 | |
| 4211 | // Make the ConnectJob hang until it times out, shorten the timeout. |
| 4212 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4213 | connect_job_factory_->set_timeout_duration( |
| 4214 | base::TimeDelta::FromMilliseconds(500)); |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4215 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 1, |
| 4216 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4217 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4218 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4219 | TestGroupId("a"))); |
| 4220 | EXPECT_EQ(1u, |
| 4221 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4222 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4223 | |
[email protected] | b7b8be4 | 2011-07-12 12:46:41 | [diff] [blame] | 4224 | // Verify the backup timer doesn't create a backup job, by making |
| 4225 | // the backup job a pending job instead of a waiting job, so it |
| 4226 | // *would* complete if it were created. |
Lukasz Krakowiak | 28dcf9d6 | 2020-06-04 09:46:59 | [diff] [blame^] | 4227 | base::RunLoop loop; |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4228 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 4229 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
Lukasz Krakowiak | 28dcf9d6 | 2020-06-04 09:46:59 | [diff] [blame^] | 4230 | FROM_HERE, loop.QuitWhenIdleClosure(), base::TimeDelta::FromSeconds(1)); |
| 4231 | loop.Run(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4232 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4233 | } |
| 4234 | |
[email protected] | b7b8be4 | 2011-07-12 12:46:41 | [diff] [blame] | 4235 | TEST_F(ClientSocketPoolBaseTest, PreconnectWithBackupJob) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4236 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 4237 | true /* enable_backup_connect_jobs */); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4238 | |
| 4239 | // Make the ConnectJob hang forever. |
| 4240 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4241 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 1, |
| 4242 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4243 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4244 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4245 | TestGroupId("a"))); |
| 4246 | EXPECT_EQ(1u, |
| 4247 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4248 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 4249 | base::RunLoop().RunUntilIdle(); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4250 | |
| 4251 | // Make the backup job be a pending job, so it completes normally. |
| 4252 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 4253 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 4254 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4255 | EXPECT_EQ( |
| 4256 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4257 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4258 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4259 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4260 | pool_.get(), NetLogWithSource())); |
[email protected] | b7b8be4 | 2011-07-12 12:46:41 | [diff] [blame] | 4261 | // Timer has started, but the backup connect job shouldn't be created yet. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4262 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4263 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4264 | TestGroupId("a"))); |
| 4265 | EXPECT_EQ(0u, |
| 4266 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4267 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4268 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4269 | ASSERT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4270 | |
| 4271 | // The hung connect job should still be there, but everything else should be |
| 4272 | // complete. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4273 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4274 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4275 | TestGroupId("a"))); |
| 4276 | EXPECT_EQ(1u, |
| 4277 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4278 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4279 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4280 | } |
| 4281 | |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4282 | // Tests that a preconnect that starts out with unread data can still be used. |
| 4283 | // http://crbug.com/334467 |
| 4284 | TEST_F(ClientSocketPoolBaseTest, PreconnectWithUnreadData) { |
| 4285 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4286 | connect_job_factory_->set_job_type(TestConnectJob::kMockUnreadDataJob); |
| 4287 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4288 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 1, |
| 4289 | NetLogWithSource()); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4290 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4291 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 4292 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4293 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4294 | TestGroupId("a"))); |
| 4295 | EXPECT_EQ(0u, |
| 4296 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4297 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4298 | |
| 4299 | // Fail future jobs to be sure that handle receives the preconnected socket |
| 4300 | // rather than closing it and making a new one. |
| 4301 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
| 4302 | ClientSocketHandle handle; |
| 4303 | TestCompletionCallback callback; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4304 | EXPECT_EQ(OK, handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4305 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4306 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4307 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4308 | pool_.get(), NetLogWithSource())); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4309 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4310 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 4311 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4312 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4313 | TestGroupId("a"))); |
| 4314 | EXPECT_EQ(0u, |
| 4315 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4316 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4317 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4318 | |
| 4319 | // Drain the pending read. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 4320 | EXPECT_EQ(1, handle.socket()->Read(nullptr, 1, CompletionOnceCallback())); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4321 | |
| 4322 | TestLoadTimingInfoConnectedReused(handle); |
| 4323 | handle.Reset(); |
| 4324 | |
| 4325 | // The socket should be usable now that it's idle again. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4326 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4327 | } |
| 4328 | |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4329 | TEST_F(ClientSocketPoolBaseTest, RequestGetsAssignedJob) { |
| 4330 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4331 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4332 | |
| 4333 | ClientSocketHandle handle1; |
| 4334 | TestCompletionCallback callback1; |
| 4335 | EXPECT_EQ( |
| 4336 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4337 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4338 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4339 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4340 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4341 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4342 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4343 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4344 | TestGroupId("a"))); |
| 4345 | EXPECT_EQ(0u, |
| 4346 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4347 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4348 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4349 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4350 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4351 | } |
| 4352 | |
| 4353 | TEST_F(ClientSocketPoolBaseTest, MultipleRequestsGetAssignedJobs) { |
| 4354 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4355 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4356 | |
| 4357 | ClientSocketHandle handle1; |
| 4358 | TestCompletionCallback callback1; |
| 4359 | EXPECT_EQ( |
| 4360 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4361 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4362 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4363 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4364 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4365 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4366 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4367 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4368 | TestGroupId("a"))); |
| 4369 | EXPECT_EQ(0u, |
| 4370 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4371 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4372 | |
| 4373 | ClientSocketHandle handle2; |
| 4374 | TestCompletionCallback callback2; |
| 4375 | EXPECT_EQ( |
| 4376 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4377 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4378 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4379 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4380 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4381 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4382 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4383 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4384 | TestGroupId("a"))); |
| 4385 | EXPECT_EQ(0u, |
| 4386 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4387 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4388 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4389 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4390 | &handle1)); |
| 4391 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4392 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4393 | |
| 4394 | // One job completes. The other request should still have its job. |
| 4395 | client_socket_factory_.SignalJob(0); |
| 4396 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 4397 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4398 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4399 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4400 | TestGroupId("a"))); |
| 4401 | EXPECT_EQ(0u, |
| 4402 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4403 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4404 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4405 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4406 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4407 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4408 | } |
| 4409 | |
| 4410 | TEST_F(ClientSocketPoolBaseTest, PreconnectJobGetsAssignedToRequest) { |
| 4411 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4412 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4413 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4414 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 1, |
| 4415 | NetLogWithSource()); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4416 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4417 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 4418 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4419 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4420 | TestGroupId("a"))); |
| 4421 | EXPECT_EQ(1u, |
| 4422 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4423 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4424 | |
| 4425 | ClientSocketHandle handle1; |
| 4426 | TestCompletionCallback callback1; |
| 4427 | EXPECT_EQ( |
| 4428 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4429 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4430 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4431 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4432 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4433 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4434 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4435 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4436 | TestGroupId("a"))); |
| 4437 | EXPECT_EQ(0u, |
| 4438 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4439 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4440 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4441 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4442 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4443 | } |
| 4444 | |
| 4445 | TEST_F(ClientSocketPoolBaseTest, HigherPriorityRequestStealsJob) { |
| 4446 | CreatePool(kDefaultMaxSockets, 1); |
| 4447 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4448 | |
| 4449 | ClientSocketHandle handle1; |
| 4450 | TestCompletionCallback callback1; |
| 4451 | EXPECT_EQ( |
| 4452 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4453 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4454 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4455 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4456 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4457 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4458 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4459 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4460 | TestGroupId("a"))); |
| 4461 | EXPECT_EQ(0u, |
| 4462 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4463 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4464 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4465 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4466 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4467 | |
| 4468 | // Insert a higher priority request |
| 4469 | ClientSocketHandle handle2; |
| 4470 | TestCompletionCallback callback2; |
| 4471 | EXPECT_EQ( |
| 4472 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4473 | handle2.Init(TestGroupId("a"), params_, base::nullopt, HIGHEST, |
| 4474 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4475 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4476 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4477 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4478 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4479 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4480 | TestGroupId("a"))); |
| 4481 | EXPECT_EQ(0u, |
| 4482 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4483 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4484 | |
| 4485 | // The highest priority request should steal the job from the default priority |
| 4486 | // request. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4487 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4488 | &handle2)); |
| 4489 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4490 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4491 | } |
| 4492 | |
| 4493 | TEST_F(ClientSocketPoolBaseTest, RequestStealsJobFromLowestRequestWithJob) { |
| 4494 | CreatePool(3, 3); |
| 4495 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4496 | |
| 4497 | ClientSocketHandle handle_lowest; |
| 4498 | TestCompletionCallback callback_lowest; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4499 | EXPECT_EQ( |
| 4500 | ERR_IO_PENDING, |
| 4501 | handle_lowest.Init(TestGroupId("a"), params_, base::nullopt, LOWEST, |
| 4502 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4503 | callback_lowest.callback(), |
| 4504 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4505 | NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4506 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4507 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4508 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4509 | TestGroupId("a"))); |
| 4510 | EXPECT_EQ(0u, |
| 4511 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4512 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4513 | |
| 4514 | ClientSocketHandle handle_highest; |
| 4515 | TestCompletionCallback callback_highest; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4516 | EXPECT_EQ( |
| 4517 | ERR_IO_PENDING, |
| 4518 | handle_highest.Init(TestGroupId("a"), params_, base::nullopt, HIGHEST, |
| 4519 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4520 | callback_highest.callback(), |
| 4521 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4522 | NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4523 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4524 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4525 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4526 | TestGroupId("a"))); |
| 4527 | EXPECT_EQ(0u, |
| 4528 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4529 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4530 | |
| 4531 | ClientSocketHandle handle_low; |
| 4532 | TestCompletionCallback callback_low; |
| 4533 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4534 | handle_low.Init( |
| 4535 | TestGroupId("a"), params_, base::nullopt, LOW, SocketTag(), |
| 4536 | ClientSocketPool::RespectLimits::ENABLED, |
| 4537 | callback_low.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4538 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4539 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4540 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4541 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4542 | TestGroupId("a"))); |
| 4543 | EXPECT_EQ(0u, |
| 4544 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4545 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4546 | |
| 4547 | ClientSocketHandle handle_lowest2; |
| 4548 | TestCompletionCallback callback_lowest2; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4549 | EXPECT_EQ( |
| 4550 | ERR_IO_PENDING, |
| 4551 | handle_lowest2.Init(TestGroupId("a"), params_, base::nullopt, LOWEST, |
| 4552 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4553 | callback_lowest2.callback(), |
| 4554 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4555 | NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4556 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4557 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4558 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4559 | TestGroupId("a"))); |
| 4560 | EXPECT_EQ(0u, |
| 4561 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4562 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4563 | |
| 4564 | // The top three requests in the queue should have jobs. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4565 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4566 | &handle_highest)); |
| 4567 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4568 | &handle_low)); |
| 4569 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4570 | &handle_lowest)); |
| 4571 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting( |
| 4572 | TestGroupId("a"), &handle_lowest2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4573 | |
| 4574 | // Add another request with medium priority. It should steal the job from the |
| 4575 | // lowest priority request with a job. |
| 4576 | ClientSocketHandle handle_medium; |
| 4577 | TestCompletionCallback callback_medium; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4578 | EXPECT_EQ( |
| 4579 | ERR_IO_PENDING, |
| 4580 | handle_medium.Init(TestGroupId("a"), params_, base::nullopt, MEDIUM, |
| 4581 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4582 | callback_medium.callback(), |
| 4583 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4584 | NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4585 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4586 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4587 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4588 | TestGroupId("a"))); |
| 4589 | EXPECT_EQ(0u, |
| 4590 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4591 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
| 4592 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4593 | &handle_highest)); |
| 4594 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4595 | &handle_medium)); |
| 4596 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4597 | &handle_low)); |
| 4598 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4599 | &handle_lowest)); |
| 4600 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting( |
| 4601 | TestGroupId("a"), &handle_lowest2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4602 | } |
| 4603 | |
| 4604 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeRequestStealsJob) { |
| 4605 | CreatePool(kDefaultMaxSockets, 1); |
| 4606 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4607 | |
| 4608 | ClientSocketHandle handle1; |
| 4609 | TestCompletionCallback callback1; |
| 4610 | EXPECT_EQ( |
| 4611 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4612 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4613 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4614 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4615 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4616 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4617 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4618 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4619 | TestGroupId("a"))); |
| 4620 | EXPECT_EQ(0u, |
| 4621 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4622 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4623 | |
| 4624 | ClientSocketHandle handle2; |
| 4625 | TestCompletionCallback callback2; |
| 4626 | EXPECT_EQ( |
| 4627 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4628 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4629 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4630 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4631 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4632 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4633 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4634 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4635 | TestGroupId("a"))); |
| 4636 | EXPECT_EQ(0u, |
| 4637 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4638 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4639 | |
| 4640 | // 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] | 4641 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4642 | &handle1)); |
| 4643 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4644 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4645 | |
| 4646 | // Reprioritizing the second request places it above the first, and it steals |
| 4647 | // the job from the first request. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4648 | pool_->SetPriority(TestGroupId("a"), &handle2, HIGHEST); |
| 4649 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4650 | &handle2)); |
| 4651 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4652 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4653 | } |
| 4654 | |
| 4655 | TEST_F(ClientSocketPoolBaseTest, CancelRequestReassignsJob) { |
| 4656 | CreatePool(kDefaultMaxSockets, 1); |
| 4657 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4658 | |
| 4659 | ClientSocketHandle handle1; |
| 4660 | TestCompletionCallback callback1; |
| 4661 | EXPECT_EQ( |
| 4662 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4663 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4664 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4665 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4666 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4667 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4668 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4669 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4670 | TestGroupId("a"))); |
| 4671 | EXPECT_EQ(0u, |
| 4672 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4673 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4674 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4675 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4676 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4677 | |
| 4678 | ClientSocketHandle handle2; |
| 4679 | TestCompletionCallback callback2; |
| 4680 | EXPECT_EQ( |
| 4681 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4682 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4683 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4684 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4685 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4686 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4687 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4688 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4689 | TestGroupId("a"))); |
| 4690 | EXPECT_EQ(0u, |
| 4691 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4692 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4693 | |
| 4694 | // The second request doesn't get a job because we are the limit. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4695 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4696 | &handle1)); |
| 4697 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4698 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4699 | |
| 4700 | // The second request should get a job upon cancelling the first request. |
| 4701 | handle1.Reset(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4702 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4703 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4704 | TestGroupId("a"))); |
| 4705 | EXPECT_EQ(0u, |
| 4706 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4707 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4708 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4709 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4710 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4711 | } |
| 4712 | |
| 4713 | TEST_F(ClientSocketPoolBaseTest, JobCompletionReassignsJob) { |
| 4714 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4715 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4716 | |
| 4717 | ClientSocketHandle handle1; |
| 4718 | TestCompletionCallback callback1; |
| 4719 | EXPECT_EQ( |
| 4720 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4721 | handle1.Init(TestGroupId("a"), params_, base::nullopt, HIGHEST, |
| 4722 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4723 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4724 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4725 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4726 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4727 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4728 | TestGroupId("a"))); |
| 4729 | EXPECT_EQ(0u, |
| 4730 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4731 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4732 | |
| 4733 | ClientSocketHandle handle2; |
| 4734 | TestCompletionCallback callback2; |
| 4735 | EXPECT_EQ( |
| 4736 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4737 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4738 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4739 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4740 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4741 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4742 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4743 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4744 | TestGroupId("a"))); |
| 4745 | EXPECT_EQ(0u, |
| 4746 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4747 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4748 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4749 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4750 | &handle1)); |
| 4751 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4752 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4753 | |
| 4754 | // The lower-priority job completes first. The higher-priority request should |
| 4755 | // get the socket, and the lower-priority request should get the remaining |
| 4756 | // job. |
| 4757 | client_socket_factory_.SignalJob(1); |
| 4758 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4759 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4760 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4761 | TestGroupId("a"))); |
| 4762 | EXPECT_EQ(0u, |
| 4763 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4764 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4765 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4766 | EXPECT_TRUE(handle1.socket()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4767 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4768 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4769 | } |
| 4770 | |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 4771 | class MockLayeredPool : public HigherLayeredPool { |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4772 | public: |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4773 | MockLayeredPool(TransportClientSocketPool* pool, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4774 | const ClientSocketPool::GroupId& group_id) |
| 4775 | : pool_(pool), group_id_(group_id), can_release_connection_(true) { |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 4776 | pool_->AddHigherLayeredPool(this); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4777 | } |
| 4778 | |
Daniel Cheng | 4496d082 | 2018-04-26 21:52:15 | [diff] [blame] | 4779 | ~MockLayeredPool() override { pool_->RemoveHigherLayeredPool(this); } |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4780 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4781 | int RequestSocket(TransportClientSocketPool* pool) { |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4782 | return handle_.Init( |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 4783 | group_id_, ClientSocketPool::SocketParams::CreateForHttpForTesting(), |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4784 | base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
| 4785 | ClientSocketPool::RespectLimits::ENABLED, callback_.callback(), |
| 4786 | ClientSocketPool::ProxyAuthCallback(), pool, NetLogWithSource()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4787 | } |
| 4788 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4789 | int RequestSocketWithoutLimits(TransportClientSocketPool* pool) { |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4790 | return handle_.Init( |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 4791 | group_id_, ClientSocketPool::SocketParams::CreateForHttpForTesting(), |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4792 | base::nullopt, MAXIMUM_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4793 | ClientSocketPool::RespectLimits::DISABLED, callback_.callback(), |
| 4794 | ClientSocketPool::ProxyAuthCallback(), pool, NetLogWithSource()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4795 | } |
| 4796 | |
| 4797 | bool ReleaseOneConnection() { |
| 4798 | if (!handle_.is_initialized() || !can_release_connection_) { |
| 4799 | return false; |
| 4800 | } |
| 4801 | handle_.socket()->Disconnect(); |
| 4802 | handle_.Reset(); |
| 4803 | return true; |
| 4804 | } |
| 4805 | |
| 4806 | void set_can_release_connection(bool can_release_connection) { |
| 4807 | can_release_connection_ = can_release_connection; |
| 4808 | } |
| 4809 | |
| 4810 | MOCK_METHOD0(CloseOneIdleConnection, bool()); |
| 4811 | |
| 4812 | private: |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4813 | TransportClientSocketPool* const pool_; |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4814 | ClientSocketHandle handle_; |
| 4815 | TestCompletionCallback callback_; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4816 | const ClientSocketPool::GroupId group_id_; |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4817 | bool can_release_connection_; |
| 4818 | }; |
| 4819 | |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4820 | // Tests the basic case of closing an idle socket in a higher layered pool when |
| 4821 | // a new request is issued and the lower layer pool is stalled. |
| 4822 | TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketsHeldByLayeredPoolWhenNeeded) { |
| 4823 | CreatePool(1, 1); |
| 4824 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 4825 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4826 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("foo")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4827 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4828 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4829 | .WillOnce(Invoke(&mock_layered_pool, |
| 4830 | &MockLayeredPool::ReleaseOneConnection)); |
| 4831 | ClientSocketHandle handle; |
| 4832 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4833 | EXPECT_EQ( |
| 4834 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4835 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4836 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4837 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4838 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4839 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4840 | } |
| 4841 | |
Matt Menke | 83367864 | 2019-03-05 22:05:51 | [diff] [blame] | 4842 | // Tests the case that trying to close an idle socket in a higher layered pool |
| 4843 | // fails. |
| 4844 | TEST_F(ClientSocketPoolBaseTest, |
| 4845 | CloseIdleSocketsHeldByLayeredPoolWhenNeededFails) { |
| 4846 | CreatePool(1, 1); |
| 4847 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 4848 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4849 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("foo")); |
Matt Menke | 83367864 | 2019-03-05 22:05:51 | [diff] [blame] | 4850 | mock_layered_pool.set_can_release_connection(false); |
| 4851 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
| 4852 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4853 | .WillOnce(Invoke(&mock_layered_pool, |
| 4854 | &MockLayeredPool::ReleaseOneConnection)); |
| 4855 | ClientSocketHandle handle; |
| 4856 | TestCompletionCallback callback; |
| 4857 | EXPECT_EQ( |
| 4858 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4859 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4860 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4861 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4862 | pool_.get(), NetLogWithSource())); |
Matt Menke | 83367864 | 2019-03-05 22:05:51 | [diff] [blame] | 4863 | base::RunLoop().RunUntilIdle(); |
| 4864 | EXPECT_FALSE(callback.have_result()); |
| 4865 | } |
| 4866 | |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4867 | // Same as above, but the idle socket is in the same group as the stalled |
| 4868 | // socket, and closes the only other request in its group when closing requests |
| 4869 | // in higher layered pools. This generally shouldn't happen, but it may be |
| 4870 | // possible if a higher level pool issues a request and the request is |
| 4871 | // subsequently cancelled. Even if it's not possible, best not to crash. |
| 4872 | TEST_F(ClientSocketPoolBaseTest, |
| 4873 | CloseIdleSocketsHeldByLayeredPoolWhenNeededSameGroup) { |
| 4874 | CreatePool(2, 2); |
| 4875 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 4876 | |
| 4877 | // Need a socket in another group for the pool to be stalled (If a group |
| 4878 | // has the maximum number of connections already, it's not stalled). |
| 4879 | ClientSocketHandle handle1; |
| 4880 | TestCompletionCallback callback1; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4881 | EXPECT_EQ(OK, handle1.Init(TestGroupId("group1"), params_, base::nullopt, |
| 4882 | DEFAULT_PRIORITY, SocketTag(), |
| 4883 | ClientSocketPool::RespectLimits::ENABLED, |
| 4884 | callback1.callback(), |
| 4885 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4886 | NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4887 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4888 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("group2")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4889 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4890 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4891 | .WillOnce(Invoke(&mock_layered_pool, |
| 4892 | &MockLayeredPool::ReleaseOneConnection)); |
| 4893 | ClientSocketHandle handle; |
| 4894 | TestCompletionCallback callback2; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4895 | EXPECT_EQ(ERR_IO_PENDING, |
| 4896 | handle.Init( |
| 4897 | TestGroupId("group2"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4898 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4899 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4900 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4901 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4902 | } |
| 4903 | |
| 4904 | // Tests the case when an idle socket can be closed when a new request is |
| 4905 | // issued, and the new request belongs to a group that was previously stalled. |
| 4906 | TEST_F(ClientSocketPoolBaseTest, |
| 4907 | CloseIdleSocketsHeldByLayeredPoolInSameGroupWhenNeeded) { |
| 4908 | CreatePool(2, 2); |
| 4909 | std::list<TestConnectJob::JobType> job_types; |
| 4910 | job_types.push_back(TestConnectJob::kMockJob); |
| 4911 | job_types.push_back(TestConnectJob::kMockJob); |
| 4912 | job_types.push_back(TestConnectJob::kMockJob); |
| 4913 | job_types.push_back(TestConnectJob::kMockJob); |
| 4914 | connect_job_factory_->set_job_types(&job_types); |
| 4915 | |
| 4916 | ClientSocketHandle handle1; |
| 4917 | TestCompletionCallback callback1; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4918 | EXPECT_EQ(OK, handle1.Init(TestGroupId("group1"), params_, base::nullopt, |
| 4919 | DEFAULT_PRIORITY, SocketTag(), |
| 4920 | ClientSocketPool::RespectLimits::ENABLED, |
| 4921 | callback1.callback(), |
| 4922 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4923 | NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4924 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4925 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("group2")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4926 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4927 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4928 | .WillRepeatedly(Invoke(&mock_layered_pool, |
| 4929 | &MockLayeredPool::ReleaseOneConnection)); |
| 4930 | mock_layered_pool.set_can_release_connection(false); |
| 4931 | |
| 4932 | // The third request is made when the socket pool is in a stalled state. |
| 4933 | ClientSocketHandle handle3; |
| 4934 | TestCompletionCallback callback3; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4935 | EXPECT_EQ(ERR_IO_PENDING, |
| 4936 | handle3.Init( |
| 4937 | TestGroupId("group3"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4938 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4939 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4940 | pool_.get(), NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4941 | |
| 4942 | base::RunLoop().RunUntilIdle(); |
| 4943 | EXPECT_FALSE(callback3.have_result()); |
| 4944 | |
| 4945 | // The fourth request is made when the pool is no longer stalled. The third |
| 4946 | // request should be serviced first, since it was issued first and has the |
| 4947 | // same priority. |
| 4948 | mock_layered_pool.set_can_release_connection(true); |
| 4949 | ClientSocketHandle handle4; |
| 4950 | TestCompletionCallback callback4; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4951 | EXPECT_EQ(ERR_IO_PENDING, |
| 4952 | handle4.Init( |
| 4953 | TestGroupId("group3"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4954 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4955 | callback4.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4956 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4957 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4958 | EXPECT_FALSE(callback4.have_result()); |
| 4959 | |
| 4960 | // Closing a handle should free up another socket slot. |
| 4961 | handle1.Reset(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4962 | EXPECT_THAT(callback4.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4963 | } |
| 4964 | |
| 4965 | // Tests the case when an idle socket can be closed when a new request is |
| 4966 | // issued, and the new request belongs to a group that was previously stalled. |
| 4967 | // |
| 4968 | // The two differences from the above test are that the stalled requests are not |
| 4969 | // in the same group as the layered pool's request, and the the fourth request |
| 4970 | // has a higher priority than the third one, so gets a socket first. |
| 4971 | TEST_F(ClientSocketPoolBaseTest, |
| 4972 | CloseIdleSocketsHeldByLayeredPoolInSameGroupWhenNeeded2) { |
| 4973 | CreatePool(2, 2); |
| 4974 | std::list<TestConnectJob::JobType> job_types; |
| 4975 | job_types.push_back(TestConnectJob::kMockJob); |
| 4976 | job_types.push_back(TestConnectJob::kMockJob); |
| 4977 | job_types.push_back(TestConnectJob::kMockJob); |
| 4978 | job_types.push_back(TestConnectJob::kMockJob); |
| 4979 | connect_job_factory_->set_job_types(&job_types); |
| 4980 | |
| 4981 | ClientSocketHandle handle1; |
| 4982 | TestCompletionCallback callback1; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4983 | EXPECT_EQ(OK, handle1.Init(TestGroupId("group1"), params_, base::nullopt, |
| 4984 | DEFAULT_PRIORITY, SocketTag(), |
| 4985 | ClientSocketPool::RespectLimits::ENABLED, |
| 4986 | callback1.callback(), |
| 4987 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4988 | NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4989 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4990 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("group2")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4991 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4992 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4993 | .WillRepeatedly(Invoke(&mock_layered_pool, |
| 4994 | &MockLayeredPool::ReleaseOneConnection)); |
| 4995 | mock_layered_pool.set_can_release_connection(false); |
| 4996 | |
| 4997 | // The third request is made when the socket pool is in a stalled state. |
| 4998 | ClientSocketHandle handle3; |
| 4999 | TestCompletionCallback callback3; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 5000 | EXPECT_EQ( |
| 5001 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5002 | handle3.Init(TestGroupId("group3"), params_, base::nullopt, MEDIUM, |
| 5003 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 5004 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5005 | pool_.get(), NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 5006 | |
| 5007 | base::RunLoop().RunUntilIdle(); |
| 5008 | EXPECT_FALSE(callback3.have_result()); |
| 5009 | |
| 5010 | // The fourth request is made when the pool is no longer stalled. This |
| 5011 | // request has a higher priority than the third request, so is serviced first. |
| 5012 | mock_layered_pool.set_can_release_connection(true); |
| 5013 | ClientSocketHandle handle4; |
| 5014 | TestCompletionCallback callback4; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 5015 | EXPECT_EQ( |
| 5016 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5017 | handle4.Init(TestGroupId("group3"), params_, base::nullopt, HIGHEST, |
| 5018 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 5019 | callback4.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5020 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5021 | EXPECT_THAT(callback4.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 5022 | EXPECT_FALSE(callback3.have_result()); |
| 5023 | |
| 5024 | // Closing a handle should free up another socket slot. |
| 5025 | handle1.Reset(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5026 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 5027 | } |
| 5028 | |
| 5029 | TEST_F(ClientSocketPoolBaseTest, |
| 5030 | CloseMultipleIdleSocketsHeldByLayeredPoolWhenNeeded) { |
| 5031 | CreatePool(1, 1); |
| 5032 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 5033 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5034 | MockLayeredPool mock_layered_pool1(pool_.get(), TestGroupId("foo")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5035 | EXPECT_THAT(mock_layered_pool1.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 5036 | EXPECT_CALL(mock_layered_pool1, CloseOneIdleConnection()) |
| 5037 | .WillRepeatedly(Invoke(&mock_layered_pool1, |
| 5038 | &MockLayeredPool::ReleaseOneConnection)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5039 | MockLayeredPool mock_layered_pool2(pool_.get(), TestGroupId("bar")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5040 | EXPECT_THAT(mock_layered_pool2.RequestSocketWithoutLimits(pool_.get()), |
| 5041 | IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 5042 | EXPECT_CALL(mock_layered_pool2, CloseOneIdleConnection()) |
| 5043 | .WillRepeatedly(Invoke(&mock_layered_pool2, |
| 5044 | &MockLayeredPool::ReleaseOneConnection)); |
| 5045 | ClientSocketHandle handle; |
| 5046 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 5047 | EXPECT_EQ( |
| 5048 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5049 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 5050 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5051 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5052 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5053 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 5054 | } |
| 5055 | |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5056 | // Test that when a socket pool and group are at their limits, a request |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5057 | // with RespectLimits::DISABLED triggers creation of a new socket, and gets the |
| 5058 | // socket instead of a request with the same priority that was issued earlier, |
| 5059 | // but has RespectLimits::ENABLED. |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5060 | TEST_F(ClientSocketPoolBaseTest, IgnoreLimits) { |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5061 | CreatePool(1, 1); |
| 5062 | |
| 5063 | // Issue a request to reach the socket pool limit. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5064 | EXPECT_EQ(OK, StartRequestWithIgnoreLimits( |
| 5065 | TestGroupId("a"), MAXIMUM_PRIORITY, |
| 5066 | ClientSocketPool::RespectLimits::ENABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5067 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5068 | |
| 5069 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 5070 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5071 | EXPECT_EQ(ERR_IO_PENDING, StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5072 | TestGroupId("a"), MAXIMUM_PRIORITY, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5073 | ClientSocketPool::RespectLimits::ENABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5074 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5075 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5076 | // Issue a request that ignores the limits, so a new ConnectJob is |
| 5077 | // created. |
| 5078 | EXPECT_EQ(ERR_IO_PENDING, StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5079 | TestGroupId("a"), MAXIMUM_PRIORITY, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5080 | ClientSocketPool::RespectLimits::DISABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5081 | ASSERT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5082 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5083 | EXPECT_THAT(request(2)->WaitForResult(), IsOk()); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5084 | EXPECT_FALSE(request(1)->have_result()); |
| 5085 | } |
| 5086 | |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 5087 | // Test that when a socket pool and group are at their limits, a ConnectJob |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5088 | // issued for a request with RespectLimits::DISABLED is not cancelled when a |
| 5089 | // request with RespectLimits::ENABLED issued to the same group is cancelled. |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 5090 | TEST_F(ClientSocketPoolBaseTest, IgnoreLimitsCancelOtherJob) { |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 5091 | CreatePool(1, 1); |
| 5092 | |
| 5093 | // Issue a request to reach the socket pool limit. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5094 | EXPECT_EQ(OK, StartRequestWithIgnoreLimits( |
| 5095 | TestGroupId("a"), MAXIMUM_PRIORITY, |
| 5096 | ClientSocketPool::RespectLimits::ENABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5097 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 5098 | |
| 5099 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 5100 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5101 | EXPECT_EQ(ERR_IO_PENDING, StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5102 | TestGroupId("a"), MAXIMUM_PRIORITY, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5103 | ClientSocketPool::RespectLimits::ENABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5104 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 5105 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5106 | // Issue a request with RespectLimits::DISABLED, so a new ConnectJob is |
| 5107 | // created. |
| 5108 | EXPECT_EQ(ERR_IO_PENDING, StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5109 | TestGroupId("a"), MAXIMUM_PRIORITY, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5110 | ClientSocketPool::RespectLimits::DISABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5111 | ASSERT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5112 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5113 | // Cancel the pending request with RespectLimits::ENABLED. The ConnectJob |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5114 | // should not be cancelled. |
| 5115 | request(1)->handle()->Reset(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5116 | ASSERT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5117 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5118 | EXPECT_THAT(request(2)->WaitForResult(), IsOk()); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5119 | EXPECT_FALSE(request(1)->have_result()); |
| 5120 | } |
| 5121 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5122 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthNoAuthCallback) { |
| 5123 | CreatePool(1, 1); |
| 5124 | |
| 5125 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5126 | |
| 5127 | ClientSocketHandle handle; |
| 5128 | TestCompletionCallback callback; |
| 5129 | EXPECT_EQ( |
| 5130 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5131 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 5132 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5133 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5134 | pool_.get(), NetLogWithSource())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5135 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5136 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5137 | |
| 5138 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_PROXY_AUTH_REQUESTED)); |
| 5139 | EXPECT_FALSE(handle.is_initialized()); |
| 5140 | EXPECT_FALSE(handle.socket()); |
| 5141 | |
| 5142 | // The group should now be empty, and thus be deleted. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5143 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5144 | } |
| 5145 | |
| 5146 | class TestAuthHelper { |
| 5147 | public: |
| 5148 | TestAuthHelper() = default; |
| 5149 | ~TestAuthHelper() = default; |
| 5150 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5151 | void InitHandle( |
Matt Menke | 84d11e56 | 2019-03-27 00:11:19 | [diff] [blame] | 5152 | scoped_refptr<ClientSocketPool::SocketParams> params, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5153 | TransportClientSocketPool* pool, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5154 | RequestPriority priority = DEFAULT_PRIORITY, |
| 5155 | ClientSocketPool::RespectLimits respect_limits = |
| 5156 | ClientSocketPool::RespectLimits::ENABLED, |
| 5157 | const ClientSocketPool::GroupId& group_id_in = TestGroupId("a")) { |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5158 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5159 | handle_.Init(group_id_in, params, base::nullopt, priority, |
| 5160 | SocketTag(), respect_limits, callback_.callback(), |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5161 | base::BindRepeating(&TestAuthHelper::AuthCallback, |
| 5162 | base::Unretained(this)), |
| 5163 | pool, NetLogWithSource())); |
| 5164 | } |
| 5165 | |
| 5166 | void WaitForAuth() { |
| 5167 | run_loop_ = std::make_unique<base::RunLoop>(); |
| 5168 | run_loop_->Run(); |
| 5169 | run_loop_.reset(); |
| 5170 | } |
| 5171 | |
| 5172 | void WaitForAuthAndRestartSync() { |
| 5173 | restart_sync_ = true; |
| 5174 | WaitForAuth(); |
| 5175 | restart_sync_ = false; |
| 5176 | } |
| 5177 | |
| 5178 | void WaitForAuthAndResetHandleSync() { |
| 5179 | reset_handle_sync_ = true; |
| 5180 | WaitForAuth(); |
| 5181 | reset_handle_sync_ = false; |
| 5182 | } |
| 5183 | |
| 5184 | void RestartWithAuth() { |
| 5185 | DCHECK(restart_with_auth_callback_); |
| 5186 | std::move(restart_with_auth_callback_).Run(); |
| 5187 | } |
| 5188 | |
| 5189 | int WaitForResult() { |
| 5190 | int result = callback_.WaitForResult(); |
| 5191 | // There shouldn't be any callback waiting to be invoked once the request is |
| 5192 | // complete. |
| 5193 | EXPECT_FALSE(restart_with_auth_callback_); |
| 5194 | // The socket should only be initialized on success. |
| 5195 | EXPECT_EQ(result == OK, handle_.is_initialized()); |
| 5196 | EXPECT_EQ(result == OK, handle_.socket() != nullptr); |
| 5197 | return result; |
| 5198 | } |
| 5199 | |
| 5200 | ClientSocketHandle* handle() { return &handle_; } |
| 5201 | int auth_count() const { return auth_count_; } |
| 5202 | int have_result() const { return callback_.have_result(); } |
| 5203 | |
| 5204 | private: |
| 5205 | void AuthCallback(const HttpResponseInfo& response, |
| 5206 | HttpAuthController* auth_controller, |
| 5207 | base::OnceClosure restart_with_auth_callback) { |
| 5208 | EXPECT_FALSE(restart_with_auth_callback_); |
| 5209 | EXPECT_TRUE(restart_with_auth_callback); |
| 5210 | |
| 5211 | // Once there's a result, this method shouldn't be invoked again. |
| 5212 | EXPECT_FALSE(callback_.have_result()); |
| 5213 | |
| 5214 | ++auth_count_; |
| 5215 | run_loop_->Quit(); |
| 5216 | if (restart_sync_) { |
| 5217 | std::move(restart_with_auth_callback).Run(); |
| 5218 | return; |
| 5219 | } |
| 5220 | |
| 5221 | restart_with_auth_callback_ = std::move(restart_with_auth_callback); |
| 5222 | |
| 5223 | if (reset_handle_sync_) { |
| 5224 | handle_.Reset(); |
| 5225 | return; |
| 5226 | } |
| 5227 | } |
| 5228 | |
| 5229 | std::unique_ptr<base::RunLoop> run_loop_; |
| 5230 | base::OnceClosure restart_with_auth_callback_; |
| 5231 | |
| 5232 | bool restart_sync_ = false; |
| 5233 | bool reset_handle_sync_ = false; |
| 5234 | |
| 5235 | ClientSocketHandle handle_; |
| 5236 | int auth_count_ = 0; |
| 5237 | TestCompletionCallback callback_; |
| 5238 | |
| 5239 | DISALLOW_COPY_AND_ASSIGN(TestAuthHelper); |
| 5240 | }; |
| 5241 | |
| 5242 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnce) { |
| 5243 | CreatePool(1, 1); |
| 5244 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5245 | |
| 5246 | TestAuthHelper auth_helper; |
| 5247 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5248 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5249 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5250 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5251 | |
| 5252 | auth_helper.WaitForAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5253 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5254 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5255 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5256 | |
| 5257 | auth_helper.RestartWithAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5258 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5259 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5260 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5261 | |
| 5262 | EXPECT_THAT(auth_helper.WaitForResult(), IsOk()); |
| 5263 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5264 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5265 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5266 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5267 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5268 | } |
| 5269 | |
| 5270 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceSync) { |
| 5271 | CreatePool(1, 1); |
| 5272 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5273 | |
| 5274 | TestAuthHelper auth_helper; |
| 5275 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5276 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5277 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5278 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5279 | |
| 5280 | auth_helper.WaitForAuthAndRestartSync(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5281 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5282 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5283 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5284 | |
| 5285 | EXPECT_THAT(auth_helper.WaitForResult(), IsOk()); |
| 5286 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5287 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5288 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5289 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5290 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5291 | } |
| 5292 | |
| 5293 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceFails) { |
| 5294 | CreatePool(1, 1); |
| 5295 | connect_job_factory_->set_job_type( |
| 5296 | TestConnectJob::kMockAuthChallengeOnceFailingJob); |
| 5297 | |
| 5298 | TestAuthHelper auth_helper; |
| 5299 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5300 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5301 | |
| 5302 | auth_helper.WaitForAuth(); |
| 5303 | auth_helper.RestartWithAuth(); |
| 5304 | EXPECT_THAT(auth_helper.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 5305 | |
| 5306 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5307 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5308 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5309 | } |
| 5310 | |
| 5311 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceSyncFails) { |
| 5312 | CreatePool(1, 1); |
| 5313 | connect_job_factory_->set_job_type( |
| 5314 | TestConnectJob::kMockAuthChallengeOnceFailingJob); |
| 5315 | |
| 5316 | TestAuthHelper auth_helper; |
| 5317 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5318 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5319 | |
| 5320 | auth_helper.WaitForAuthAndRestartSync(); |
| 5321 | EXPECT_THAT(auth_helper.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 5322 | |
| 5323 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5324 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5325 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5326 | } |
| 5327 | |
| 5328 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceDeleteHandle) { |
| 5329 | CreatePool(1, 1); |
| 5330 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5331 | |
| 5332 | TestAuthHelper auth_helper; |
| 5333 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5334 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5335 | |
| 5336 | auth_helper.WaitForAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5337 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5338 | |
| 5339 | auth_helper.handle()->Reset(); |
| 5340 | |
| 5341 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5342 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5343 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5344 | EXPECT_FALSE(auth_helper.handle()->is_initialized()); |
| 5345 | EXPECT_FALSE(auth_helper.handle()->socket()); |
| 5346 | } |
| 5347 | |
| 5348 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceDeleteHandleSync) { |
| 5349 | CreatePool(1, 1); |
| 5350 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5351 | |
| 5352 | TestAuthHelper auth_helper; |
| 5353 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5354 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5355 | |
| 5356 | auth_helper.WaitForAuthAndResetHandleSync(); |
| 5357 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5358 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5359 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5360 | EXPECT_FALSE(auth_helper.handle()->is_initialized()); |
| 5361 | EXPECT_FALSE(auth_helper.handle()->socket()); |
| 5362 | } |
| 5363 | |
| 5364 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceFlushWithError) { |
| 5365 | CreatePool(1, 1); |
| 5366 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5367 | |
| 5368 | TestAuthHelper auth_helper; |
| 5369 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5370 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5371 | |
| 5372 | auth_helper.WaitForAuth(); |
| 5373 | |
Matt Menke | 433de6d | 2020-03-04 00:24:11 | [diff] [blame] | 5374 | pool_->FlushWithError(ERR_FAILED, "Network changed"); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5375 | base::RunLoop().RunUntilIdle(); |
| 5376 | |
| 5377 | // When flushing the socket pool, bound sockets should delay returning the |
| 5378 | // error until completion. |
| 5379 | EXPECT_FALSE(auth_helper.have_result()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5380 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5381 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5382 | |
| 5383 | auth_helper.RestartWithAuth(); |
| 5384 | // The callback should be called asynchronously. |
| 5385 | EXPECT_FALSE(auth_helper.have_result()); |
| 5386 | |
| 5387 | EXPECT_THAT(auth_helper.WaitForResult(), IsError(ERR_FAILED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5388 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5389 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5390 | } |
| 5391 | |
| 5392 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthTwice) { |
| 5393 | CreatePool(1, 1); |
| 5394 | connect_job_factory_->set_job_type( |
| 5395 | TestConnectJob::kMockAuthChallengeTwiceJob); |
| 5396 | |
| 5397 | TestAuthHelper auth_helper; |
| 5398 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5399 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5400 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5401 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5402 | |
| 5403 | auth_helper.WaitForAuth(); |
| 5404 | auth_helper.RestartWithAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5405 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5406 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5407 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5408 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5409 | |
| 5410 | auth_helper.WaitForAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5411 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5412 | EXPECT_EQ(2, auth_helper.auth_count()); |
| 5413 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5414 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5415 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5416 | auth_helper.RestartWithAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5417 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5418 | EXPECT_EQ(2, auth_helper.auth_count()); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5419 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5420 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5421 | |
| 5422 | EXPECT_THAT(auth_helper.WaitForResult(), IsOk()); |
| 5423 | EXPECT_EQ(2, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5424 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5425 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5426 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5427 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5428 | } |
| 5429 | |
| 5430 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthTwiceFails) { |
| 5431 | CreatePool(1, 1); |
| 5432 | connect_job_factory_->set_job_type( |
| 5433 | TestConnectJob::kMockAuthChallengeTwiceFailingJob); |
| 5434 | |
| 5435 | TestAuthHelper auth_helper; |
| 5436 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5437 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5438 | |
| 5439 | auth_helper.WaitForAuth(); |
| 5440 | auth_helper.RestartWithAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5441 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5442 | EXPECT_EQ(1, auth_helper.auth_count()); |
| 5443 | |
| 5444 | auth_helper.WaitForAuth(); |
| 5445 | auth_helper.RestartWithAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5446 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5447 | EXPECT_EQ(2, auth_helper.auth_count()); |
| 5448 | |
| 5449 | EXPECT_THAT(auth_helper.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 5450 | EXPECT_EQ(2, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5451 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5452 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5453 | } |
| 5454 | |
| 5455 | // Makes sure that when a bound request is destroyed, a new ConnectJob is |
| 5456 | // created, if needed. |
| 5457 | TEST_F(ClientSocketPoolBaseTest, |
| 5458 | ProxyAuthCreateNewConnectJobOnDestroyBoundRequest) { |
| 5459 | CreatePool(1 /* max_sockets */, 1 /* max_sockets_per_group */); |
| 5460 | connect_job_factory_->set_job_type( |
| 5461 | TestConnectJob::kMockAuthChallengeOnceFailingJob); |
| 5462 | |
| 5463 | // First request creates a ConnectJob. |
| 5464 | TestAuthHelper auth_helper1; |
| 5465 | auth_helper1.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5466 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5467 | |
| 5468 | // A second request come in, but no new ConnectJob is needed, since the limit |
| 5469 | // has been reached. |
| 5470 | TestAuthHelper auth_helper2; |
| 5471 | auth_helper2.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5472 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5473 | |
| 5474 | // Run until the auth callback for the first request is invoked. |
| 5475 | auth_helper1.WaitForAuth(); |
| 5476 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5477 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5478 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5479 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5480 | |
| 5481 | // Make connect jobs succeed, then cancel the first request, which should |
| 5482 | // destroy the bound ConnectJob, and cause a new ConnectJob to start. |
| 5483 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 5484 | auth_helper1.handle()->Reset(); |
| 5485 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5486 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5487 | |
| 5488 | // The second ConnectJob should succeed. |
| 5489 | EXPECT_THAT(auth_helper2.WaitForResult(), IsOk()); |
| 5490 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5491 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5492 | } |
| 5493 | |
| 5494 | // Makes sure that when a bound request is destroyed, a new ConnectJob is |
| 5495 | // created for another group, if needed. |
| 5496 | TEST_F(ClientSocketPoolBaseTest, |
| 5497 | ProxyAuthCreateNewConnectJobOnDestroyBoundRequestDifferentGroups) { |
| 5498 | CreatePool(1 /* max_sockets */, 1 /* max_sockets_per_group */); |
| 5499 | connect_job_factory_->set_job_type( |
| 5500 | TestConnectJob::kMockAuthChallengeOnceFailingJob); |
| 5501 | |
| 5502 | // First request creates a ConnectJob. |
| 5503 | TestAuthHelper auth_helper1; |
| 5504 | auth_helper1.InitHandle(params_, pool_.get(), DEFAULT_PRIORITY); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5505 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5506 | |
| 5507 | // A second request come in, but no new ConnectJob is needed, since the limit |
| 5508 | // has been reached. |
| 5509 | TestAuthHelper auth_helper2; |
| 5510 | auth_helper2.InitHandle(params_, pool_.get(), DEFAULT_PRIORITY, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5511 | ClientSocketPool::RespectLimits::ENABLED, |
| 5512 | TestGroupId("b")); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5513 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5514 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5515 | |
| 5516 | // Run until the auth callback for the first request is invoked. |
| 5517 | auth_helper1.WaitForAuth(); |
| 5518 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5519 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5520 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5521 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5522 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 5523 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5524 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5525 | |
| 5526 | // Make connect jobs succeed, then cancel the first request, which should |
| 5527 | // destroy the bound ConnectJob, and cause a new ConnectJob to start for the |
| 5528 | // other group. |
| 5529 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 5530 | auth_helper1.handle()->Reset(); |
| 5531 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5532 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 5533 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5534 | |
| 5535 | // The second ConnectJob should succeed. |
| 5536 | EXPECT_THAT(auth_helper2.WaitForResult(), IsOk()); |
| 5537 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5538 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 5539 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5540 | } |
| 5541 | |
| 5542 | // Test that once an auth challenge is bound, that's the request that gets all |
| 5543 | // subsequent calls and the socket itself. |
| 5544 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthStaysBound) { |
| 5545 | CreatePool(1, 1); |
| 5546 | connect_job_factory_->set_job_type( |
| 5547 | TestConnectJob::kMockAuthChallengeTwiceJob); |
| 5548 | |
| 5549 | // First request creates a ConnectJob. |
| 5550 | TestAuthHelper auth_helper1; |
| 5551 | auth_helper1.InitHandle(params_, pool_.get(), LOWEST); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5552 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5553 | |
| 5554 | // A second, higher priority request is made. |
| 5555 | TestAuthHelper auth_helper2; |
| 5556 | auth_helper2.InitHandle(params_, pool_.get(), LOW); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5557 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5558 | |
| 5559 | // Run until the auth callback for the second request is invoked. |
| 5560 | auth_helper2.WaitForAuth(); |
| 5561 | EXPECT_EQ(0, auth_helper1.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5562 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5563 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5564 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5565 | |
| 5566 | // Start a higher priority job. It shouldn't be able to steal |auth_helper2|'s |
| 5567 | // ConnectJob. |
| 5568 | TestAuthHelper auth_helper3; |
| 5569 | auth_helper3.InitHandle(params_, pool_.get(), HIGHEST); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5570 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5571 | |
| 5572 | // Start a higher job that ignores limits, creating a hanging socket. It |
| 5573 | // shouldn't be able to steal |auth_helper2|'s ConnectJob. |
| 5574 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 5575 | TestAuthHelper auth_helper4; |
| 5576 | auth_helper4.InitHandle(params_, pool_.get(), HIGHEST, |
| 5577 | ClientSocketPool::RespectLimits::DISABLED); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5578 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5579 | |
| 5580 | // Restart with auth, and |auth_helper2|'s auth method should be invoked |
| 5581 | // again. |
| 5582 | auth_helper2.RestartWithAuth(); |
| 5583 | auth_helper2.WaitForAuth(); |
| 5584 | EXPECT_EQ(0, auth_helper1.auth_count()); |
| 5585 | EXPECT_FALSE(auth_helper1.have_result()); |
| 5586 | EXPECT_EQ(2, auth_helper2.auth_count()); |
| 5587 | EXPECT_FALSE(auth_helper2.have_result()); |
| 5588 | EXPECT_EQ(0, auth_helper3.auth_count()); |
| 5589 | EXPECT_FALSE(auth_helper3.have_result()); |
| 5590 | EXPECT_EQ(0, auth_helper4.auth_count()); |
| 5591 | EXPECT_FALSE(auth_helper4.have_result()); |
| 5592 | |
| 5593 | // Advance auth again, and |auth_helper2| should get the socket. |
| 5594 | auth_helper2.RestartWithAuth(); |
| 5595 | EXPECT_THAT(auth_helper2.WaitForResult(), IsOk()); |
| 5596 | // The hung ConnectJob for the RespectLimits::DISABLED request is still in the |
| 5597 | // socket pool. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5598 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5599 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5600 | EXPECT_EQ(0, auth_helper1.auth_count()); |
| 5601 | EXPECT_FALSE(auth_helper1.have_result()); |
| 5602 | EXPECT_EQ(0, auth_helper3.auth_count()); |
| 5603 | EXPECT_FALSE(auth_helper3.have_result()); |
| 5604 | EXPECT_EQ(0, auth_helper4.auth_count()); |
| 5605 | EXPECT_FALSE(auth_helper4.have_result()); |
| 5606 | |
| 5607 | // If the socket is returned to the socket pool, the RespectLimits::DISABLED |
| 5608 | // socket request should be able to claim it. |
| 5609 | auth_helper2.handle()->Reset(); |
| 5610 | EXPECT_THAT(auth_helper4.WaitForResult(), IsOk()); |
| 5611 | EXPECT_EQ(0, auth_helper1.auth_count()); |
| 5612 | EXPECT_FALSE(auth_helper1.have_result()); |
| 5613 | EXPECT_EQ(0, auth_helper3.auth_count()); |
| 5614 | EXPECT_FALSE(auth_helper3.have_result()); |
| 5615 | EXPECT_EQ(0, auth_helper4.auth_count()); |
| 5616 | } |
| 5617 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5618 | enum class RefreshType { |
| 5619 | kServer, |
| 5620 | kProxy, |
| 5621 | }; |
| 5622 | |
| 5623 | // Common base class to test RefreshGroup() when called from either |
| 5624 | // OnSSLConfigForServerChanged() matching a specific group or the pool's proxy. |
| 5625 | // |
| 5626 | // Tests which test behavior specific to one or the other case should use |
| 5627 | // ClientSocketPoolBaseTest directly. In particular, there is no "other group" |
| 5628 | // when the pool's proxy matches. |
| 5629 | class ClientSocketPoolBaseRefreshTest |
| 5630 | : public ClientSocketPoolBaseTest, |
| 5631 | public testing::WithParamInterface<RefreshType> { |
| 5632 | public: |
| 5633 | void CreatePoolForRefresh(int max_sockets, |
| 5634 | int max_sockets_per_group, |
| 5635 | bool enable_backup_connect_jobs = false) { |
| 5636 | switch (GetParam()) { |
| 5637 | case RefreshType::kServer: |
| 5638 | CreatePool(max_sockets, max_sockets_per_group, |
| 5639 | enable_backup_connect_jobs); |
| 5640 | break; |
| 5641 | case RefreshType::kProxy: |
| 5642 | CreatePoolWithIdleTimeouts( |
| 5643 | max_sockets, max_sockets_per_group, kUnusedIdleSocketTimeout, |
| 5644 | ClientSocketPool::used_idle_socket_timeout(), |
| 5645 | enable_backup_connect_jobs, |
| 5646 | ProxyServer::FromPacString("HTTPS myproxy:70")); |
| 5647 | break; |
| 5648 | } |
| 5649 | } |
| 5650 | |
| 5651 | static ClientSocketPool::GroupId GetGroupId() { |
| 5652 | return TestGroupId("a", 443, ClientSocketPool::SocketType::kSsl); |
| 5653 | } |
| 5654 | |
David Benjamin | 6dd7e88 | 2019-10-10 02:35:23 | [diff] [blame] | 5655 | static ClientSocketPool::GroupId GetGroupIdInPartition() { |
| 5656 | // Note this GroupId will match GetGroupId() unless |
| 5657 | // kPartitionConnectionsByNetworkIsolationKey is enabled. |
| 5658 | const auto kOrigin = url::Origin::Create(GURL("https://b/")); |
| 5659 | const NetworkIsolationKey kNetworkIsolationKey(kOrigin, kOrigin); |
| 5660 | return TestGroupId("a", 443, ClientSocketPool::SocketType::kSsl, |
| 5661 | PrivacyMode::PRIVACY_MODE_DISABLED, |
| 5662 | kNetworkIsolationKey); |
| 5663 | } |
| 5664 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5665 | void OnSSLConfigForServerChanged() { |
| 5666 | switch (GetParam()) { |
| 5667 | case RefreshType::kServer: |
| 5668 | pool_->OnSSLConfigForServerChanged(HostPortPair("a", 443)); |
| 5669 | break; |
| 5670 | case RefreshType::kProxy: |
| 5671 | pool_->OnSSLConfigForServerChanged(HostPortPair("myproxy", 70)); |
| 5672 | break; |
| 5673 | } |
| 5674 | } |
| 5675 | }; |
| 5676 | |
| 5677 | INSTANTIATE_TEST_SUITE_P(RefreshType, |
| 5678 | ClientSocketPoolBaseRefreshTest, |
| 5679 | ::testing::Values(RefreshType::kServer, |
| 5680 | RefreshType::kProxy)); |
| 5681 | |
| 5682 | TEST_P(ClientSocketPoolBaseRefreshTest, RefreshGroupCreatesNewConnectJobs) { |
| 5683 | CreatePoolForRefresh(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 5684 | const ClientSocketPool::GroupId kGroupId = GetGroupId(); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5685 | |
| 5686 | // First job will be waiting until it gets aborted. |
| 5687 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 5688 | |
| 5689 | ClientSocketHandle handle; |
| 5690 | TestCompletionCallback callback; |
| 5691 | EXPECT_THAT( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5692 | handle.Init(kGroupId, params_, base::nullopt, DEFAULT_PRIORITY, |
| 5693 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5694 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5695 | pool_.get(), NetLogWithSource()), |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5696 | IsError(ERR_IO_PENDING)); |
| 5697 | |
| 5698 | // Switch connect job types, so creating a new ConnectJob will result in |
| 5699 | // success. |
| 5700 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 5701 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5702 | OnSSLConfigForServerChanged(); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5703 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 5704 | ASSERT_TRUE(handle.socket()); |
| 5705 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5706 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId)); |
| 5707 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(kGroupId)); |
| 5708 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(kGroupId)); |
| 5709 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId)); |
| 5710 | } |
| 5711 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5712 | TEST_P(ClientSocketPoolBaseRefreshTest, RefreshGroupClosesIdleConnectJobs) { |
David Benjamin | 6dd7e88 | 2019-10-10 02:35:23 | [diff] [blame] | 5713 | base::test::ScopedFeatureList feature_list; |
| 5714 | feature_list.InitAndEnableFeature( |
| 5715 | features::kPartitionConnectionsByNetworkIsolationKey); |
| 5716 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5717 | CreatePoolForRefresh(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 5718 | const ClientSocketPool::GroupId kGroupId = GetGroupId(); |
David Benjamin | 6dd7e88 | 2019-10-10 02:35:23 | [diff] [blame] | 5719 | const ClientSocketPool::GroupId kGroupIdInPartition = GetGroupIdInPartition(); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5720 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5721 | pool_->RequestSockets(kGroupId, params_, base::nullopt, 2, |
| 5722 | NetLogWithSource()); |
David Benjamin | 6dd7e88 | 2019-10-10 02:35:23 | [diff] [blame] | 5723 | pool_->RequestSockets(kGroupIdInPartition, params_, base::nullopt, 2, |
| 5724 | NetLogWithSource()); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5725 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId)); |
David Benjamin | 6dd7e88 | 2019-10-10 02:35:23 | [diff] [blame] | 5726 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupIdInPartition)); |
| 5727 | EXPECT_EQ(4, pool_->IdleSocketCount()); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5728 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(kGroupId)); |
David Benjamin | 6dd7e88 | 2019-10-10 02:35:23 | [diff] [blame] | 5729 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(kGroupIdInPartition)); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5730 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5731 | OnSSLConfigForServerChanged(); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5732 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5733 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupId)); |
David Benjamin | 6dd7e88 | 2019-10-10 02:35:23 | [diff] [blame] | 5734 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupIdInPartition)); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5735 | } |
| 5736 | |
| 5737 | TEST_F(ClientSocketPoolBaseTest, |
| 5738 | RefreshGroupDoesNotCloseIdleConnectJobsInOtherGroup) { |
| 5739 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5740 | const ClientSocketPool::GroupId kGroupId = |
| 5741 | TestGroupId("a", 443, ClientSocketPool::SocketType::kSsl); |
| 5742 | const ClientSocketPool::GroupId kOtherGroupId = |
| 5743 | TestGroupId("b", 443, ClientSocketPool::SocketType::kSsl); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5744 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5745 | pool_->RequestSockets(kOtherGroupId, params_, base::nullopt, 2, |
| 5746 | NetLogWithSource()); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5747 | ASSERT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 5748 | EXPECT_EQ(2, pool_->IdleSocketCount()); |
| 5749 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(kOtherGroupId)); |
| 5750 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5751 | pool_->OnSSLConfigForServerChanged(HostPortPair("a", 443)); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5752 | ASSERT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 5753 | EXPECT_EQ(2, pool_->IdleSocketCount()); |
| 5754 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(kOtherGroupId)); |
| 5755 | } |
| 5756 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5757 | TEST_P(ClientSocketPoolBaseRefreshTest, RefreshGroupPreventsSocketReuse) { |
| 5758 | CreatePoolForRefresh(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 5759 | const ClientSocketPool::GroupId kGroupId = GetGroupId(); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5760 | |
| 5761 | ClientSocketHandle handle; |
| 5762 | TestCompletionCallback callback; |
| 5763 | EXPECT_THAT( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5764 | handle.Init(kGroupId, params_, base::nullopt, DEFAULT_PRIORITY, |
| 5765 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5766 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5767 | pool_.get(), NetLogWithSource()), |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5768 | IsOk()); |
| 5769 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId)); |
| 5770 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId)); |
| 5771 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5772 | OnSSLConfigForServerChanged(); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5773 | |
| 5774 | handle.Reset(); |
| 5775 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5776 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupId)); |
| 5777 | } |
| 5778 | |
| 5779 | TEST_F(ClientSocketPoolBaseTest, |
| 5780 | RefreshGroupDoesNotPreventSocketReuseInOtherGroup) { |
| 5781 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5782 | const ClientSocketPool::GroupId kGroupId = |
| 5783 | TestGroupId("a", 443, ClientSocketPool::SocketType::kSsl); |
| 5784 | const ClientSocketPool::GroupId kOtherGroupId = |
| 5785 | TestGroupId("b", 443, ClientSocketPool::SocketType::kSsl); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5786 | |
| 5787 | ClientSocketHandle handle; |
| 5788 | TestCompletionCallback callback; |
| 5789 | EXPECT_THAT( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5790 | handle.Init(kOtherGroupId, params_, base::nullopt, DEFAULT_PRIORITY, |
| 5791 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5792 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5793 | pool_.get(), NetLogWithSource()), |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5794 | IsOk()); |
| 5795 | ASSERT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 5796 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kOtherGroupId)); |
| 5797 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5798 | pool_->OnSSLConfigForServerChanged(HostPortPair("a", 443)); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5799 | |
| 5800 | handle.Reset(); |
| 5801 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 5802 | ASSERT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 5803 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(kOtherGroupId)); |
| 5804 | } |
| 5805 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5806 | TEST_P(ClientSocketPoolBaseRefreshTest, |
| 5807 | RefreshGroupReplacesBoundConnectJobOnConnect) { |
| 5808 | CreatePoolForRefresh(1, 1); |
| 5809 | const ClientSocketPool::GroupId kGroupId = GetGroupId(); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5810 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5811 | |
| 5812 | TestAuthHelper auth_helper; |
| 5813 | auth_helper.InitHandle(params_, pool_.get(), DEFAULT_PRIORITY, |
| 5814 | ClientSocketPool::RespectLimits::ENABLED, kGroupId); |
| 5815 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(kGroupId)); |
| 5816 | |
| 5817 | auth_helper.WaitForAuth(); |
| 5818 | |
| 5819 | // This should update the generation, but not cancel the old ConnectJob - it's |
| 5820 | // not safe to do anything while waiting on the original ConnectJob. |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5821 | OnSSLConfigForServerChanged(); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5822 | |
| 5823 | // Providing auth credentials and restarting the request with them will cause |
| 5824 | // the ConnectJob to complete successfully, but the result will be discarded |
| 5825 | // because of the generation mismatch. |
| 5826 | auth_helper.RestartWithAuth(); |
| 5827 | |
| 5828 | // Despite using ConnectJobs that simulate a single challenge, a second |
| 5829 | // challenge will be seen, due to using a new ConnectJob. |
| 5830 | auth_helper.WaitForAuth(); |
| 5831 | auth_helper.RestartWithAuth(); |
| 5832 | |
| 5833 | EXPECT_THAT(auth_helper.WaitForResult(), IsOk()); |
| 5834 | EXPECT_TRUE(auth_helper.handle()->socket()); |
| 5835 | EXPECT_EQ(2, auth_helper.auth_count()); |
| 5836 | |
| 5837 | // When released, the socket will be returned to the socket pool, and |
| 5838 | // available for reuse. |
| 5839 | auth_helper.handle()->Reset(); |
| 5840 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 5841 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId)); |
| 5842 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(kGroupId)); |
| 5843 | } |
| 5844 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5845 | TEST_F(ClientSocketPoolBaseTest, RefreshProxyRefreshesAllGroups) { |
| 5846 | CreatePoolWithIdleTimeouts(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 5847 | kUnusedIdleSocketTimeout, |
| 5848 | ClientSocketPool::used_idle_socket_timeout(), |
| 5849 | false /* no backup connect jobs */, |
| 5850 | ProxyServer::FromPacString("HTTPS myproxy:70")); |
| 5851 | |
| 5852 | const ClientSocketPool::GroupId kGroupId1 = |
| 5853 | TestGroupId("a", 443, ClientSocketPool::SocketType::kSsl); |
| 5854 | const ClientSocketPool::GroupId kGroupId2 = |
| 5855 | TestGroupId("b", 443, ClientSocketPool::SocketType::kSsl); |
| 5856 | const ClientSocketPool::GroupId kGroupId3 = |
| 5857 | TestGroupId("c", 443, ClientSocketPool::SocketType::kSsl); |
| 5858 | |
| 5859 | // Make three sockets in three different groups. The third socket is released |
| 5860 | // to the pool as idle. |
| 5861 | ClientSocketHandle handle1, handle2, handle3; |
| 5862 | TestCompletionCallback callback; |
| 5863 | EXPECT_THAT( |
| 5864 | handle1.Init(kGroupId1, params_, base::nullopt, DEFAULT_PRIORITY, |
| 5865 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5866 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5867 | pool_.get(), NetLogWithSource()), |
| 5868 | IsOk()); |
| 5869 | EXPECT_THAT( |
| 5870 | handle2.Init(kGroupId2, params_, base::nullopt, DEFAULT_PRIORITY, |
| 5871 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5872 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5873 | pool_.get(), NetLogWithSource()), |
| 5874 | IsOk()); |
| 5875 | EXPECT_THAT( |
| 5876 | handle3.Init(kGroupId3, params_, base::nullopt, DEFAULT_PRIORITY, |
| 5877 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5878 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5879 | pool_.get(), NetLogWithSource()), |
| 5880 | IsOk()); |
| 5881 | handle3.Reset(); |
| 5882 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId1)); |
| 5883 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId1)); |
| 5884 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId2)); |
| 5885 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId2)); |
| 5886 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId3)); |
| 5887 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(kGroupId3)); |
| 5888 | |
| 5889 | // Changes to some other proxy do not affect the pool. The idle socket remains |
| 5890 | // alive and closing |handle2| makes the socket available for the pool. |
| 5891 | pool_->OnSSLConfigForServerChanged(HostPortPair("someotherproxy", 70)); |
| 5892 | |
| 5893 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId1)); |
| 5894 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId1)); |
| 5895 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId2)); |
| 5896 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId2)); |
| 5897 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId3)); |
| 5898 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(kGroupId3)); |
| 5899 | |
| 5900 | handle2.Reset(); |
| 5901 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId2)); |
| 5902 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(kGroupId2)); |
| 5903 | |
| 5904 | // Changes to the matching proxy refreshes all groups. |
| 5905 | pool_->OnSSLConfigForServerChanged(HostPortPair("myproxy", 70)); |
| 5906 | |
| 5907 | // Idle sockets are closed. |
| 5908 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5909 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupId2)); |
| 5910 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupId3)); |
| 5911 | |
| 5912 | // The active socket, however, continues to be active. |
| 5913 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId1)); |
| 5914 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId1)); |
| 5915 | |
| 5916 | // Closing it does not make it available for the pool. |
| 5917 | handle1.Reset(); |
| 5918 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5919 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupId1)); |
| 5920 | } |
| 5921 | |
| 5922 | TEST_F(ClientSocketPoolBaseTest, RefreshBothPrivacyAndNormalSockets) { |
| 5923 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 5924 | |
| 5925 | const ClientSocketPool::GroupId kGroupId = |
| 5926 | TestGroupId("a", 443, ClientSocketPool::SocketType::kSsl, |
| 5927 | PrivacyMode::PRIVACY_MODE_DISABLED); |
| 5928 | const ClientSocketPool::GroupId kGroupIdPrivacy = |
| 5929 | TestGroupId("a", 443, ClientSocketPool::SocketType::kSsl, |
| 5930 | PrivacyMode::PRIVACY_MODE_ENABLED); |
| 5931 | const ClientSocketPool::GroupId kOtherGroupId = |
| 5932 | TestGroupId("b", 443, ClientSocketPool::SocketType::kSsl); |
| 5933 | |
| 5934 | // Make a socket in each groups. |
| 5935 | ClientSocketHandle handle1, handle2, handle3; |
| 5936 | TestCompletionCallback callback; |
| 5937 | EXPECT_THAT( |
| 5938 | handle1.Init(kGroupId, params_, base::nullopt, DEFAULT_PRIORITY, |
| 5939 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5940 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5941 | pool_.get(), NetLogWithSource()), |
| 5942 | IsOk()); |
| 5943 | EXPECT_THAT( |
| 5944 | handle2.Init(kGroupIdPrivacy, params_, base::nullopt, DEFAULT_PRIORITY, |
| 5945 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5946 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5947 | pool_.get(), NetLogWithSource()), |
| 5948 | IsOk()); |
| 5949 | EXPECT_THAT( |
| 5950 | handle3.Init(kOtherGroupId, params_, base::nullopt, DEFAULT_PRIORITY, |
| 5951 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5952 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5953 | pool_.get(), NetLogWithSource()), |
| 5954 | IsOk()); |
| 5955 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId)); |
| 5956 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId)); |
| 5957 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupIdPrivacy)); |
| 5958 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupIdPrivacy)); |
| 5959 | ASSERT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 5960 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kOtherGroupId)); |
| 5961 | |
| 5962 | pool_->OnSSLConfigForServerChanged(HostPortPair("a", 443)); |
| 5963 | |
| 5964 | // Active sockets continue to be active. |
| 5965 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId)); |
| 5966 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId)); |
| 5967 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupIdPrivacy)); |
| 5968 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupIdPrivacy)); |
| 5969 | ASSERT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 5970 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kOtherGroupId)); |
| 5971 | |
| 5972 | // Closing them leaves kOtherGroupId alone, but kGroupId and kGroupIdPrivacy |
| 5973 | // are unusable. |
| 5974 | handle1.Reset(); |
| 5975 | handle2.Reset(); |
| 5976 | handle3.Reset(); |
| 5977 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 5978 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupId)); |
| 5979 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupIdPrivacy)); |
| 5980 | EXPECT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 5981 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(kOtherGroupId)); |
| 5982 | } |
| 5983 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 5984 | } // namespace |
| 5985 | |
| 5986 | } // namespace net |