[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" |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 14 | #include "base/location.h" |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 15 | #include "base/logging.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" |
Alexander Timin | 4f9c35c | 2018-11-01 20:15:20 | [diff] [blame] | 18 | #include "base/message_loop/message_loop.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" |
[email protected] | 277d594 | 2010-08-11 21:02:35 | [diff] [blame] | 38 | #include "net/http/http_response_headers.h" |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 39 | #include "net/http/http_response_info.h" |
eroman | 87c53d6 | 2015-04-02 06:51:07 | [diff] [blame] | 40 | #include "net/log/net_log.h" |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 41 | #include "net/log/net_log_event_type.h" |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 42 | #include "net/log/net_log_source.h" |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 43 | #include "net/log/net_log_source_type.h" |
mmenke | 16a7cbdd | 2015-04-24 23:00:56 | [diff] [blame] | 44 | #include "net/log/test_net_log.h" |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 45 | #include "net/log/test_net_log_util.h" |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 46 | #include "net/socket/client_socket_factory.h" |
| 47 | #include "net/socket/client_socket_handle.h" |
tfarina | 5dd13c2 | 2016-11-16 12:08:26 | [diff] [blame] | 48 | #include "net/socket/datagram_client_socket.h" |
tbansal | ca83c00 | 2016-04-28 20:56:28 | [diff] [blame] | 49 | #include "net/socket/socket_performance_watcher.h" |
Paul Jensen | 8d6f87ec | 2018-01-13 00:46:54 | [diff] [blame] | 50 | #include "net/socket/socket_tag.h" |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 51 | #include "net/socket/socket_test_util.h" |
[email protected] | 18ccfdb | 2013-08-15 00:13:44 | [diff] [blame] | 52 | #include "net/socket/ssl_client_socket.h" |
[email protected] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame] | 53 | #include "net/socket/stream_socket.h" |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 54 | #include "net/socket/transport_connect_job.h" |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 55 | #include "net/ssl/ssl_cert_request_info.h" |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 56 | #include "net/test/gtest_util.h" |
Gabriel Charette | c710874 | 2019-08-23 03:31:40 | [diff] [blame] | 57 | #include "net/test/test_with_task_environment.h" |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 58 | #include "net/traffic_annotation/network_traffic_annotation.h" |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 59 | #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 60 | #include "testing/gmock/include/gmock/gmock.h" |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 61 | #include "testing/gtest/include/gtest/gtest.h" |
| 62 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 63 | using net::test::IsError; |
| 64 | using net::test::IsOk; |
| 65 | |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 66 | using ::testing::Invoke; |
| 67 | using ::testing::Return; |
| 68 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 69 | namespace net { |
| 70 | |
| 71 | namespace { |
| 72 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 73 | const int kDefaultMaxSockets = 4; |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 74 | const int kDefaultMaxSocketsPerGroup = 2; |
Tarun Bansal | a763509 | 2019-02-20 10:00:59 | [diff] [blame] | 75 | constexpr base::TimeDelta kUnusedIdleSocketTimeout = |
| 76 | base::TimeDelta::FromSeconds(10); |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 77 | |
Matt Menke | bdf77780 | 2019-04-22 19:38:59 | [diff] [blame] | 78 | ClientSocketPool::GroupId TestGroupId( |
| 79 | const std::string& host, |
| 80 | int port = 80, |
| 81 | ClientSocketPool::SocketType socket_type = |
| 82 | ClientSocketPool::SocketType::kHttp, |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 83 | PrivacyMode privacy_mode = PrivacyMode::PRIVACY_MODE_DISABLED, |
| 84 | NetworkIsolationKey network_isolation_key = NetworkIsolationKey()) { |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 85 | return ClientSocketPool::GroupId(HostPortPair(host, port), socket_type, |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 86 | privacy_mode, network_isolation_key); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 87 | } |
| 88 | |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 89 | // Make sure |handle| sets load times correctly when it has been assigned a |
| 90 | // reused socket. |
| 91 | void TestLoadTimingInfoConnectedReused(const ClientSocketHandle& handle) { |
| 92 | LoadTimingInfo load_timing_info; |
| 93 | // Only pass true in as |is_reused|, as in general, HttpStream types should |
| 94 | // have stricter concepts of reuse than socket pools. |
| 95 | EXPECT_TRUE(handle.GetLoadTimingInfo(true, &load_timing_info)); |
| 96 | |
| 97 | EXPECT_EQ(true, load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 98 | EXPECT_NE(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 99 | |
[email protected] | b258e079 | 2013-01-12 07:11:59 | [diff] [blame] | 100 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 101 | ExpectLoadTimingHasOnlyConnectionTimes(load_timing_info); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | // Make sure |handle| sets load times correctly when it has been assigned a |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 105 | // fresh socket. Also runs TestLoadTimingInfoConnectedReused, since the owner |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 106 | // of a connection where |is_reused| is false may consider the connection |
| 107 | // reused. |
| 108 | void TestLoadTimingInfoConnectedNotReused(const ClientSocketHandle& handle) { |
| 109 | EXPECT_FALSE(handle.is_reused()); |
| 110 | |
| 111 | LoadTimingInfo load_timing_info; |
| 112 | EXPECT_TRUE(handle.GetLoadTimingInfo(false, &load_timing_info)); |
| 113 | |
| 114 | EXPECT_FALSE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 115 | EXPECT_NE(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 116 | |
[email protected] | b258e079 | 2013-01-12 07:11:59 | [diff] [blame] | 117 | ExpectConnectTimingHasTimes(load_timing_info.connect_timing, |
| 118 | CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY); |
| 119 | ExpectLoadTimingHasOnlyConnectionTimes(load_timing_info); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 120 | |
| 121 | TestLoadTimingInfoConnectedReused(handle); |
| 122 | } |
| 123 | |
| 124 | // Make sure |handle| sets load times correctly, in the case that it does not |
| 125 | // currently have a socket. |
| 126 | void TestLoadTimingInfoNotConnected(const ClientSocketHandle& handle) { |
| 127 | // Should only be set to true once a socket is assigned, if at all. |
| 128 | EXPECT_FALSE(handle.is_reused()); |
| 129 | |
| 130 | LoadTimingInfo load_timing_info; |
| 131 | EXPECT_FALSE(handle.GetLoadTimingInfo(false, &load_timing_info)); |
| 132 | |
| 133 | EXPECT_FALSE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 134 | EXPECT_EQ(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 135 | |
[email protected] | b258e079 | 2013-01-12 07:11:59 | [diff] [blame] | 136 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 137 | ExpectLoadTimingHasOnlyConnectionTimes(load_timing_info); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 138 | } |
| 139 | |
[email protected] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame] | 140 | class MockClientSocket : public StreamSocket { |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 141 | public: |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 142 | explicit MockClientSocket(net::NetLog* net_log) |
| 143 | : connected_(false), |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 144 | has_unread_data_(false), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 145 | net_log_(NetLogWithSource::Make(net_log, NetLogSourceType::SOCKET)), |
Charlie Harrison | 3e4c062 | 2018-05-13 15:44:30 | [diff] [blame] | 146 | was_used_to_convey_data_(false) {} |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 147 | |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 148 | // Sets whether the socket has unread data. If true, the next call to Read() |
| 149 | // will return 1 byte and IsConnectedAndIdle() will return false. |
| 150 | void set_has_unread_data(bool has_unread_data) { |
| 151 | has_unread_data_ = has_unread_data; |
| 152 | } |
| 153 | |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 154 | // Socket implementation. |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 155 | int Read(IOBuffer* /* buf */, |
| 156 | int len, |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 157 | CompletionOnceCallback /* callback */) override { |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 158 | if (has_unread_data_ && len > 0) { |
| 159 | has_unread_data_ = false; |
| 160 | was_used_to_convey_data_ = true; |
| 161 | return 1; |
| 162 | } |
[email protected] | e86df8dc | 2013-03-30 13:18:28 | [diff] [blame] | 163 | return ERR_UNEXPECTED; |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 164 | } |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 165 | |
[email protected] | a2b2cfc | 2017-12-06 09:06:08 | [diff] [blame] | 166 | int Write( |
| 167 | IOBuffer* /* buf */, |
| 168 | int len, |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 169 | CompletionOnceCallback /* callback */, |
[email protected] | a2b2cfc | 2017-12-06 09:06:08 | [diff] [blame] | 170 | const NetworkTrafficAnnotationTag& /*traffic_annotation*/) override { |
[email protected] | 0f873e8 | 2010-09-02 16:09:01 | [diff] [blame] | 171 | was_used_to_convey_data_ = true; |
| 172 | return len; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 173 | } |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 174 | int SetReceiveBufferSize(int32_t size) override { return OK; } |
| 175 | int SetSendBufferSize(int32_t size) override { return OK; } |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 176 | |
[email protected] | dbf036f | 2011-12-06 23:33:24 | [diff] [blame] | 177 | // StreamSocket implementation. |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 178 | int Connect(CompletionOnceCallback callback) override { |
[email protected] | dbf036f | 2011-12-06 23:33:24 | [diff] [blame] | 179 | connected_ = true; |
| 180 | return OK; |
| 181 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 182 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 183 | void Disconnect() override { connected_ = false; } |
| 184 | bool IsConnected() const override { return connected_; } |
| 185 | bool IsConnectedAndIdle() const override { |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 186 | return connected_ && !has_unread_data_; |
| 187 | } |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 188 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 189 | int GetPeerAddress(IPEndPoint* /* address */) const override { |
[email protected] | 9f864b3 | 2010-01-20 15:01:16 | [diff] [blame] | 190 | return ERR_UNEXPECTED; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 191 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 192 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 193 | int GetLocalAddress(IPEndPoint* /* address */) const override { |
[email protected] | e7f74da | 2011-04-19 23:49:35 | [diff] [blame] | 194 | return ERR_UNEXPECTED; |
| 195 | } |
| 196 | |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 197 | const NetLogWithSource& NetLog() const override { return net_log_; } |
[email protected] | a2006ece | 2010-04-23 16:44:02 | [diff] [blame] | 198 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 199 | bool WasEverUsed() const override { return was_used_to_convey_data_; } |
tfarina | 2846404c | 2016-12-25 14:31:37 | [diff] [blame] | 200 | bool WasAlpnNegotiated() const override { return false; } |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 201 | NextProto GetNegotiatedProtocol() const override { return kProtoUnknown; } |
| 202 | bool GetSSLInfo(SSLInfo* ssl_info) override { return false; } |
ttuttle | 23fdb7b | 2015-05-15 01:28:03 | [diff] [blame] | 203 | void GetConnectionAttempts(ConnectionAttempts* out) const override { |
| 204 | out->clear(); |
| 205 | } |
| 206 | void ClearConnectionAttempts() override {} |
| 207 | void AddConnectionAttempts(const ConnectionAttempts& attempts) override {} |
tbansal | f82cc8e | 2015-10-14 20:05:49 | [diff] [blame] | 208 | int64_t GetTotalReceivedBytes() const override { |
| 209 | NOTIMPLEMENTED(); |
| 210 | return 0; |
| 211 | } |
Paul Jensen | 0f49dec | 2017-12-12 23:39:58 | [diff] [blame] | 212 | void ApplySocketTag(const SocketTag& tag) override {} |
[email protected] | 9b5614a | 2010-08-25 20:29:45 | [diff] [blame] | 213 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 214 | private: |
| 215 | bool connected_; |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 216 | bool has_unread_data_; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 217 | NetLogWithSource net_log_; |
[email protected] | 0f873e8 | 2010-09-02 16:09:01 | [diff] [blame] | 218 | bool was_used_to_convey_data_; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 219 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 220 | DISALLOW_COPY_AND_ASSIGN(MockClientSocket); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 221 | }; |
| 222 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 223 | class TestConnectJob; |
| 224 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 225 | class MockClientSocketFactory : public ClientSocketFactory { |
| 226 | public: |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 227 | MockClientSocketFactory() : allocation_count_(0) {} |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 228 | |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 229 | std::unique_ptr<DatagramClientSocket> CreateDatagramClientSocket( |
[email protected] | 5370c01 | 2011-06-29 03:47:04 | [diff] [blame] | 230 | DatagramSocket::BindType bind_type, |
[email protected] | 98b0e58 | 2011-06-22 14:31:41 | [diff] [blame] | 231 | NetLog* net_log, |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 232 | const NetLogSource& source) override { |
[email protected] | 98b0e58 | 2011-06-22 14:31:41 | [diff] [blame] | 233 | NOTREACHED(); |
David Benjamin | 24725be | 2019-07-24 20:57:18 | [diff] [blame] | 234 | return nullptr; |
[email protected] | 98b0e58 | 2011-06-22 14:31:41 | [diff] [blame] | 235 | } |
| 236 | |
Helen Li | d5bb922 | 2018-04-12 15:33:09 | [diff] [blame] | 237 | std::unique_ptr<TransportClientSocket> CreateTransportClientSocket( |
[email protected] | 0a0b768 | 2010-08-25 17:08:07 | [diff] [blame] | 238 | const AddressList& addresses, |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 239 | std::unique_ptr< |
| 240 | SocketPerformanceWatcher> /* socket_performance_watcher */, |
[email protected] | 0a0b768 | 2010-08-25 17:08:07 | [diff] [blame] | 241 | NetLog* /* net_log */, |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 242 | const NetLogSource& /*source*/) override { |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 243 | allocation_count_++; |
Helen Li | d5bb922 | 2018-04-12 15:33:09 | [diff] [blame] | 244 | return nullptr; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 245 | } |
| 246 | |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 247 | std::unique_ptr<SSLClientSocket> CreateSSLClientSocket( |
David Benjamin | 24725be | 2019-07-24 20:57:18 | [diff] [blame] | 248 | SSLClientContext* context, |
Matt Menke | 841fc41 | 2019-03-05 23:20:12 | [diff] [blame] | 249 | std::unique_ptr<StreamSocket> stream_socket, |
[email protected] | 4f4de7e6 | 2010-11-12 19:55:27 | [diff] [blame] | 250 | const HostPortPair& host_and_port, |
David Benjamin | 24725be | 2019-07-24 20:57:18 | [diff] [blame] | 251 | const SSLConfig& ssl_config) override { |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 252 | NOTIMPLEMENTED(); |
David Benjamin | 24725be | 2019-07-24 20:57:18 | [diff] [blame] | 253 | return nullptr; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 254 | } |
Matt Menke | fd95692 | 2019-02-04 23:44:03 | [diff] [blame] | 255 | |
Matt Menke | 52cd95a | 2019-02-08 06:16:27 | [diff] [blame] | 256 | std::unique_ptr<ProxyClientSocket> CreateProxyClientSocket( |
| 257 | std::unique_ptr<StreamSocket> stream_socket, |
| 258 | const std::string& user_agent, |
| 259 | const HostPortPair& endpoint, |
| 260 | const ProxyServer& proxy_server, |
| 261 | HttpAuthController* http_auth_controller, |
| 262 | bool tunnel, |
| 263 | bool using_spdy, |
| 264 | NextProto negotiated_protocol, |
| 265 | ProxyDelegate* proxy_delegate, |
Matt Menke | 52cd95a | 2019-02-08 06:16:27 | [diff] [blame] | 266 | const NetworkTrafficAnnotationTag& traffic_annotation) override { |
| 267 | NOTIMPLEMENTED(); |
| 268 | return nullptr; |
| 269 | } |
| 270 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 271 | void WaitForSignal(TestConnectJob* job) { waiting_jobs_.push_back(job); } |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 272 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 273 | void SignalJobs(); |
| 274 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 275 | void SignalJob(size_t job); |
| 276 | |
| 277 | void SetJobLoadState(size_t job, LoadState load_state); |
| 278 | |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 279 | // Sets the HasConnectionEstablished value of the specified job to true, |
| 280 | // without invoking the callback. |
| 281 | void SetJobHasEstablishedConnection(size_t job); |
| 282 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 283 | int allocation_count() const { return allocation_count_; } |
| 284 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 285 | private: |
| 286 | int allocation_count_; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 287 | std::vector<TestConnectJob*> waiting_jobs_; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 288 | }; |
| 289 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 290 | class TestConnectJob : public ConnectJob { |
| 291 | public: |
| 292 | enum JobType { |
| 293 | kMockJob, |
| 294 | kMockFailingJob, |
| 295 | kMockPendingJob, |
| 296 | kMockPendingFailingJob, |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 297 | kMockWaitingJob, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 298 | |
| 299 | // Certificate errors return a socket in addition to an error code. |
| 300 | kMockCertErrorJob, |
| 301 | kMockPendingCertErrorJob, |
| 302 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 303 | kMockAdditionalErrorStateJob, |
| 304 | kMockPendingAdditionalErrorStateJob, |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 305 | kMockUnreadDataJob, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 306 | |
| 307 | kMockAuthChallengeOnceJob, |
| 308 | kMockAuthChallengeTwiceJob, |
| 309 | kMockAuthChallengeOnceFailingJob, |
| 310 | kMockAuthChallengeTwiceFailingJob, |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 311 | }; |
| 312 | |
[email protected] | 994d493 | 2010-07-12 17:55:13 | [diff] [blame] | 313 | // The kMockPendingJob uses a slight delay before allowing the connect |
| 314 | // to complete. |
| 315 | static const int kPendingConnectDelay = 2; |
| 316 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 317 | TestConnectJob(JobType job_type, |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 318 | RequestPriority request_priority, |
| 319 | SocketTag socket_tag, |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 320 | base::TimeDelta timeout_duration, |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 321 | const CommonConnectJobParams* common_connect_job_params, |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 322 | ConnectJob::Delegate* delegate, |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 323 | MockClientSocketFactory* client_socket_factory) |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 324 | : ConnectJob(request_priority, |
| 325 | socket_tag, |
Matt Menke | 1a6c92d | 2019-02-23 00:25:38 | [diff] [blame] | 326 | timeout_duration, |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 327 | common_connect_job_params, |
Matt Menke | 1a6c92d | 2019-02-23 00:25:38 | [diff] [blame] | 328 | delegate, |
| 329 | nullptr /* net_log */, |
| 330 | NetLogSourceType::TRANSPORT_CONNECT_JOB, |
| 331 | NetLogEventType::TRANSPORT_CONNECT_JOB_CONNECT), |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 332 | job_type_(job_type), |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 333 | client_socket_factory_(client_socket_factory), |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 334 | load_state_(LOAD_STATE_IDLE), |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 335 | has_established_connection_(false), |
Jeremy Roman | d54000b2 | 2019-07-08 18:40:16 | [diff] [blame] | 336 | store_additional_error_state_(false) {} |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 337 | |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 338 | void Signal() { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 339 | DoConnect(waiting_success_, true /* async */, false /* recoverable */); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 340 | } |
| 341 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 342 | void set_load_state(LoadState load_state) { load_state_ = load_state; } |
| 343 | |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 344 | void set_has_established_connection() { |
| 345 | DCHECK(!has_established_connection_); |
| 346 | has_established_connection_ = true; |
| 347 | } |
| 348 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 349 | // From ConnectJob: |
| 350 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 351 | LoadState GetLoadState() const override { return load_state_; } |
[email protected] | 4645135 | 2009-09-01 14:54:21 | [diff] [blame] | 352 | |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 353 | bool HasEstablishedConnection() const override { |
| 354 | return has_established_connection_; |
| 355 | } |
| 356 | |
Matt Menke | 6f84d1f1 | 2019-04-11 19:26:47 | [diff] [blame] | 357 | bool IsSSLError() const override { return store_additional_error_state_; } |
| 358 | |
| 359 | scoped_refptr<SSLCertRequestInfo> GetCertRequestInfo() override { |
| 360 | if (store_additional_error_state_) |
| 361 | return base::MakeRefCounted<SSLCertRequestInfo>(); |
| 362 | return nullptr; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 363 | } |
| 364 | |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 365 | private: |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 366 | // From ConnectJob: |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 367 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 368 | int ConnectInternal() override { |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 369 | AddressList ignored; |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 370 | client_socket_factory_->CreateTransportClientSocket( |
| 371 | ignored, nullptr, nullptr, NetLogSource()); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 372 | switch (job_type_) { |
| 373 | case kMockJob: |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 374 | return DoConnect(true /* successful */, false /* sync */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 375 | false /* cert_error */); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 376 | case kMockFailingJob: |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 377 | return DoConnect(false /* error */, false /* sync */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 378 | false /* cert_error */); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 379 | case kMockPendingJob: |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 380 | set_load_state(LOAD_STATE_CONNECTING); |
[email protected] | 6b17538 | 2009-10-13 06:47:47 | [diff] [blame] | 381 | |
| 382 | // Depending on execution timings, posting a delayed task can result |
| 383 | // in the task getting executed the at the earliest possible |
| 384 | // opportunity or only after returning once from the message loop and |
| 385 | // then a second call into the message loop. In order to make behavior |
| 386 | // more deterministic, we change the default delay to 2ms. This should |
| 387 | // always require us to wait for the second call into the message loop. |
| 388 | // |
| 389 | // N.B. The correct fix for this and similar timing problems is to |
| 390 | // abstract time for the purpose of unittests. Unfortunately, we have |
| 391 | // a lot of third-party components that directly call the various |
| 392 | // time functions, so this change would be rather invasive. |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 393 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 394 | FROM_HERE, |
kylechar | f4fe517 | 2019-02-15 18:53:49 | [diff] [blame] | 395 | base::BindOnce(base::IgnoreResult(&TestConnectJob::DoConnect), |
| 396 | weak_factory_.GetWeakPtr(), true /* successful */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 397 | true /* async */, false /* cert_error */), |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 398 | base::TimeDelta::FromMilliseconds(kPendingConnectDelay)); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 399 | return ERR_IO_PENDING; |
| 400 | case kMockPendingFailingJob: |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 401 | set_load_state(LOAD_STATE_CONNECTING); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 402 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 403 | FROM_HERE, |
kylechar | f4fe517 | 2019-02-15 18:53:49 | [diff] [blame] | 404 | base::BindOnce(base::IgnoreResult(&TestConnectJob::DoConnect), |
| 405 | weak_factory_.GetWeakPtr(), false /* error */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 406 | true /* async */, false /* cert_error */), |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 407 | base::TimeDelta::FromMilliseconds(2)); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 408 | return ERR_IO_PENDING; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 409 | case kMockWaitingJob: |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 410 | set_load_state(LOAD_STATE_CONNECTING); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 411 | client_socket_factory_->WaitForSignal(this); |
| 412 | waiting_success_ = true; |
| 413 | return ERR_IO_PENDING; |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 414 | case kMockCertErrorJob: |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 415 | return DoConnect(false /* error */, false /* sync */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 416 | true /* cert_error */); |
| 417 | case kMockPendingCertErrorJob: |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 418 | set_load_state(LOAD_STATE_CONNECTING); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 419 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 420 | FROM_HERE, |
kylechar | f4fe517 | 2019-02-15 18:53:49 | [diff] [blame] | 421 | base::BindOnce(base::IgnoreResult(&TestConnectJob::DoConnect), |
| 422 | weak_factory_.GetWeakPtr(), false /* error */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 423 | true /* async */, true /* cert_error */), |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 424 | base::TimeDelta::FromMilliseconds(2)); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 425 | return ERR_IO_PENDING; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 426 | case kMockAdditionalErrorStateJob: |
| 427 | store_additional_error_state_ = true; |
| 428 | return DoConnect(false /* error */, false /* sync */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 429 | false /* cert_error */); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 430 | case kMockPendingAdditionalErrorStateJob: |
| 431 | set_load_state(LOAD_STATE_CONNECTING); |
| 432 | store_additional_error_state_ = true; |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 433 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 434 | FROM_HERE, |
kylechar | f4fe517 | 2019-02-15 18:53:49 | [diff] [blame] | 435 | base::BindOnce(base::IgnoreResult(&TestConnectJob::DoConnect), |
| 436 | weak_factory_.GetWeakPtr(), false /* error */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 437 | true /* async */, false /* cert_error */), |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 438 | base::TimeDelta::FromMilliseconds(2)); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 439 | return ERR_IO_PENDING; |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 440 | case kMockUnreadDataJob: { |
| 441 | int ret = DoConnect(true /* successful */, false /* sync */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 442 | false /* cert_error */); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 443 | static_cast<MockClientSocket*>(socket())->set_has_unread_data(true); |
| 444 | return ret; |
| 445 | } |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 446 | case kMockAuthChallengeOnceJob: |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 447 | set_load_state(LOAD_STATE_CONNECTING); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 448 | DoAdvanceAuthChallenge(1, true /* succeed_after_last_challenge */); |
| 449 | return ERR_IO_PENDING; |
| 450 | case kMockAuthChallengeTwiceJob: |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 451 | set_load_state(LOAD_STATE_CONNECTING); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 452 | DoAdvanceAuthChallenge(2, true /* succeed_after_last_challenge */); |
| 453 | return ERR_IO_PENDING; |
| 454 | case kMockAuthChallengeOnceFailingJob: |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 455 | set_load_state(LOAD_STATE_CONNECTING); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 456 | DoAdvanceAuthChallenge(1, false /* succeed_after_last_challenge */); |
| 457 | return ERR_IO_PENDING; |
| 458 | case kMockAuthChallengeTwiceFailingJob: |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 459 | set_load_state(LOAD_STATE_CONNECTING); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 460 | DoAdvanceAuthChallenge(2, false /* succeed_after_last_challenge */); |
| 461 | return ERR_IO_PENDING; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 462 | default: |
| 463 | NOTREACHED(); |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 464 | SetSocket(std::unique_ptr<StreamSocket>()); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 465 | return ERR_FAILED; |
| 466 | } |
| 467 | } |
| 468 | |
Lily Chen | 02ef29a | 2018-11-30 16:31:43 | [diff] [blame] | 469 | void ChangePriorityInternal(RequestPriority priority) override {} |
| 470 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 471 | int DoConnect(bool succeed, bool was_async, bool cert_error) { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 472 | int result = OK; |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 473 | has_established_connection_ = true; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 474 | if (succeed) { |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 475 | SetSocket(std::make_unique<MockClientSocket>(net_log().net_log())); |
Bence Béky | bdbb0e7 | 2018-08-07 21:42:59 | [diff] [blame] | 476 | socket()->Connect(CompletionOnceCallback()); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 477 | } else if (cert_error) { |
| 478 | SetSocket(std::make_unique<MockClientSocket>(net_log().net_log())); |
| 479 | result = ERR_CERT_COMMON_NAME_INVALID; |
[email protected] | 6e713f0 | 2009-08-06 02:56:40 | [diff] [blame] | 480 | } else { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 481 | result = ERR_CONNECTION_FAILED; |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 482 | SetSocket(std::unique_ptr<StreamSocket>()); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 483 | } |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 484 | |
| 485 | if (was_async) |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 486 | NotifyDelegateOfCompletion(result); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 487 | return result; |
| 488 | } |
| 489 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 490 | void DoAdvanceAuthChallenge(int remaining_challenges, |
| 491 | bool succeed_after_last_challenge) { |
| 492 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 493 | FROM_HERE, |
| 494 | base::BindOnce(&TestConnectJob::InvokeNextProxyAuthCallback, |
| 495 | weak_factory_.GetWeakPtr(), remaining_challenges, |
| 496 | succeed_after_last_challenge)); |
| 497 | } |
| 498 | |
| 499 | void InvokeNextProxyAuthCallback(int remaining_challenges, |
| 500 | bool succeed_after_last_challenge) { |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 501 | set_load_state(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 502 | if (remaining_challenges == 0) { |
| 503 | DoConnect(succeed_after_last_challenge, true /* was_async */, |
| 504 | false /* cert_error */); |
| 505 | return; |
| 506 | } |
| 507 | |
| 508 | // Integration tests make sure HttpResponseInfo and HttpAuthController work. |
| 509 | // The auth tests here are just focused on ConnectJob bookkeeping. |
| 510 | HttpResponseInfo info; |
| 511 | NotifyDelegateOfProxyAuth( |
| 512 | info, nullptr /* http_auth_controller */, |
| 513 | base::BindOnce(&TestConnectJob::DoAdvanceAuthChallenge, |
| 514 | weak_factory_.GetWeakPtr(), remaining_challenges - 1, |
| 515 | succeed_after_last_challenge)); |
| 516 | } |
| 517 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 518 | bool waiting_success_; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 519 | const JobType job_type_; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 520 | MockClientSocketFactory* const client_socket_factory_; |
[email protected] | 4645135 | 2009-09-01 14:54:21 | [diff] [blame] | 521 | LoadState load_state_; |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 522 | bool has_established_connection_; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 523 | bool store_additional_error_state_; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 524 | |
Jeremy Roman | d54000b2 | 2019-07-08 18:40:16 | [diff] [blame] | 525 | base::WeakPtrFactory<TestConnectJob> weak_factory_{this}; |
[email protected] | d5492c5 | 2013-11-10 20:44:39 | [diff] [blame] | 526 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 527 | DISALLOW_COPY_AND_ASSIGN(TestConnectJob); |
| 528 | }; |
| 529 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 530 | class TestConnectJobFactory |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 531 | : public TransportClientSocketPool::ConnectJobFactory { |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 532 | public: |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 533 | TestConnectJobFactory(MockClientSocketFactory* client_socket_factory, |
| 534 | NetLog* net_log) |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 535 | : common_connect_job_params_( |
| 536 | nullptr /* client_socket_factory */, |
| 537 | nullptr /* host_resolver */, |
Matt Menke | b88837e | 2019-03-20 11:50:40 | [diff] [blame] | 538 | nullptr /* http_auth_cache */, |
| 539 | nullptr /* http_auth_handler_factory */, |
| 540 | nullptr /* spdy_session_pool */, |
Matt Menke | b5fb42b | 2019-03-22 17:26:13 | [diff] [blame] | 541 | nullptr /* quic_supported_versions */, |
Matt Menke | b88837e | 2019-03-20 11:50:40 | [diff] [blame] | 542 | nullptr /* quic_stream_factory */, |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 543 | nullptr /* proxy_delegate */, |
Matt Menke | d732ea4 | 2019-03-08 12:05:00 | [diff] [blame] | 544 | nullptr /* http_user_agent_settings */, |
David Benjamin | 24725be | 2019-07-24 20:57:18 | [diff] [blame] | 545 | nullptr /* ssl_client_context */, |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 546 | nullptr /* socket_performance_watcher_factory */, |
| 547 | nullptr /* network_quality_estimator */, |
| 548 | net_log, |
| 549 | nullptr /* websocket_endpoint_lock_manager */), |
| 550 | job_type_(TestConnectJob::kMockJob), |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 551 | job_types_(nullptr), |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 552 | client_socket_factory_(client_socket_factory) {} |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 553 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 554 | ~TestConnectJobFactory() override = default; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 555 | |
| 556 | void set_job_type(TestConnectJob::JobType job_type) { job_type_ = job_type; } |
| 557 | |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 558 | void set_job_types(std::list<TestConnectJob::JobType>* job_types) { |
| 559 | job_types_ = job_types; |
| 560 | CHECK(!job_types_->empty()); |
| 561 | } |
| 562 | |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 563 | void set_timeout_duration(base::TimeDelta timeout_duration) { |
| 564 | timeout_duration_ = timeout_duration; |
| 565 | } |
| 566 | |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 567 | // ConnectJobFactory implementation. |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 568 | |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 569 | std::unique_ptr<ConnectJob> NewConnectJob( |
Matt Menke | aafff54 | 2019-04-22 22:09:36 | [diff] [blame] | 570 | ClientSocketPool::GroupId group_id, |
| 571 | scoped_refptr<ClientSocketPool::SocketParams> socket_params, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 572 | const base::Optional<NetworkTrafficAnnotationTag>& proxy_annotation_tag, |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 573 | RequestPriority request_priority, |
| 574 | SocketTag socket_tag, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 575 | ConnectJob::Delegate* delegate) const override { |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 576 | EXPECT_TRUE(!job_types_ || !job_types_->empty()); |
| 577 | TestConnectJob::JobType job_type = job_type_; |
| 578 | if (job_types_ && !job_types_->empty()) { |
| 579 | job_type = job_types_->front(); |
| 580 | job_types_->pop_front(); |
| 581 | } |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 582 | return std::make_unique<TestConnectJob>( |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 583 | job_type, request_priority, socket_tag, timeout_duration_, |
| 584 | &common_connect_job_params_, delegate, client_socket_factory_); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | private: |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 588 | const CommonConnectJobParams common_connect_job_params_; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 589 | TestConnectJob::JobType job_type_; |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 590 | std::list<TestConnectJob::JobType>* job_types_; |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 591 | base::TimeDelta timeout_duration_; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 592 | MockClientSocketFactory* const client_socket_factory_; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 593 | |
| 594 | DISALLOW_COPY_AND_ASSIGN(TestConnectJobFactory); |
| 595 | }; |
| 596 | |
[email protected] | a937a06d | 2009-08-19 21:19:24 | [diff] [blame] | 597 | } // namespace |
| 598 | |
[email protected] | a937a06d | 2009-08-19 21:19:24 | [diff] [blame] | 599 | namespace { |
| 600 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 601 | void MockClientSocketFactory::SignalJobs() { |
jdoerrie | 22a91d8b9 | 2018-10-05 08:43:26 | [diff] [blame] | 602 | for (auto it = waiting_jobs_.begin(); it != waiting_jobs_.end(); ++it) { |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 603 | (*it)->Signal(); |
| 604 | } |
| 605 | waiting_jobs_.clear(); |
| 606 | } |
| 607 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 608 | void MockClientSocketFactory::SignalJob(size_t job) { |
| 609 | ASSERT_LT(job, waiting_jobs_.size()); |
| 610 | waiting_jobs_[job]->Signal(); |
| 611 | waiting_jobs_.erase(waiting_jobs_.begin() + job); |
| 612 | } |
| 613 | |
| 614 | void MockClientSocketFactory::SetJobLoadState(size_t job, |
| 615 | LoadState load_state) { |
| 616 | ASSERT_LT(job, waiting_jobs_.size()); |
| 617 | waiting_jobs_[job]->set_load_state(load_state); |
| 618 | } |
| 619 | |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 620 | void MockClientSocketFactory::SetJobHasEstablishedConnection(size_t job) { |
| 621 | ASSERT_LT(job, waiting_jobs_.size()); |
| 622 | waiting_jobs_[job]->set_has_established_connection(); |
| 623 | } |
| 624 | |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 625 | class ClientSocketPoolBaseTest : public TestWithTaskEnvironment { |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 626 | protected: |
Alex Clarke | 0def209 | 2018-12-10 12:01:45 | [diff] [blame] | 627 | ClientSocketPoolBaseTest() |
Gabriel Charette | 694c3c33 | 2019-08-19 14:53:05 | [diff] [blame] | 628 | : TestWithTaskEnvironment( |
| 629 | base::test::TaskEnvironment::TimeSource::MOCK_TIME), |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 630 | params_(ClientSocketPool::SocketParams::CreateForHttpForTesting()) { |
[email protected] | 636b825 | 2011-04-08 19:56:54 | [diff] [blame] | 631 | connect_backup_jobs_enabled_ = |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 632 | TransportClientSocketPool::connect_backup_jobs_enabled(); |
| 633 | TransportClientSocketPool::set_connect_backup_jobs_enabled(true); |
[email protected] | 636b825 | 2011-04-08 19:56:54 | [diff] [blame] | 634 | } |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 635 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 636 | ~ClientSocketPoolBaseTest() override { |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 637 | TransportClientSocketPool::set_connect_backup_jobs_enabled( |
[email protected] | 636b825 | 2011-04-08 19:56:54 | [diff] [blame] | 638 | connect_backup_jobs_enabled_); |
| 639 | } |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 640 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 641 | void CreatePool(int max_sockets, |
| 642 | int max_sockets_per_group, |
| 643 | bool enable_backup_connect_jobs = false) { |
Tarun Bansal | a763509 | 2019-02-20 10:00:59 | [diff] [blame] | 644 | CreatePoolWithIdleTimeouts(max_sockets, max_sockets_per_group, |
| 645 | kUnusedIdleSocketTimeout, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 646 | ClientSocketPool::used_idle_socket_timeout(), |
| 647 | enable_backup_connect_jobs); |
[email protected] | 9bf28db | 2009-08-29 01:35:16 | [diff] [blame] | 648 | } |
| 649 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 650 | void CreatePoolWithIdleTimeouts( |
| 651 | int max_sockets, |
| 652 | int max_sockets_per_group, |
| 653 | base::TimeDelta unused_idle_socket_timeout, |
| 654 | base::TimeDelta used_idle_socket_timeout, |
| 655 | bool enable_backup_connect_jobs = false, |
| 656 | ProxyServer proxy_server = ProxyServer::Direct()) { |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 657 | DCHECK(!pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 658 | std::unique_ptr<TestConnectJobFactory> connect_job_factory = |
| 659 | std::make_unique<TestConnectJobFactory>(&client_socket_factory_, |
| 660 | &net_log_); |
| 661 | connect_job_factory_ = connect_job_factory.get(); |
| 662 | pool_ = TransportClientSocketPool::CreateForTesting( |
| 663 | max_sockets, max_sockets_per_group, unused_idle_socket_timeout, |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 664 | used_idle_socket_timeout, proxy_server, std::move(connect_job_factory), |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 665 | nullptr /* ssl_config_service */, enable_backup_connect_jobs); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 666 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 667 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 668 | int StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 669 | const ClientSocketPool::GroupId& group_id, |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 670 | RequestPriority priority, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 671 | ClientSocketPool::RespectLimits respect_limits) { |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 672 | return test_base_.StartRequestUsingPool(pool_.get(), group_id, priority, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 673 | respect_limits, params_); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 674 | } |
| 675 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 676 | int StartRequest(const ClientSocketPool::GroupId& group_id, |
| 677 | RequestPriority priority) { |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 678 | return StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 679 | group_id, priority, ClientSocketPool::RespectLimits::ENABLED); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 680 | } |
| 681 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 682 | int GetOrderOfRequest(size_t index) const { |
| 683 | return test_base_.GetOrderOfRequest(index); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 684 | } |
| 685 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 686 | bool ReleaseOneConnection(ClientSocketPoolTest::KeepAlive keep_alive) { |
| 687 | return test_base_.ReleaseOneConnection(keep_alive); |
| 688 | } |
| 689 | |
| 690 | void ReleaseAllConnections(ClientSocketPoolTest::KeepAlive keep_alive) { |
| 691 | test_base_.ReleaseAllConnections(keep_alive); |
| 692 | } |
| 693 | |
| 694 | TestSocketRequest* request(int i) { return test_base_.request(i); } |
| 695 | size_t requests_size() const { return test_base_.requests_size(); } |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 696 | std::vector<std::unique_ptr<TestSocketRequest>>* requests() { |
olli.raula | 9d66b7d | 2015-11-23 08:30:42 | [diff] [blame] | 697 | return test_base_.requests(); |
| 698 | } |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 699 | // Only counts the requests that get sockets asynchronously; |
| 700 | // synchronous completions are not registered by this count. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 701 | size_t completion_count() const { return test_base_.completion_count(); } |
| 702 | |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 703 | TestNetLog net_log_; |
[email protected] | 636b825 | 2011-04-08 19:56:54 | [diff] [blame] | 704 | bool connect_backup_jobs_enabled_; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 705 | MockClientSocketFactory client_socket_factory_; |
[email protected] | 17a0c6c | 2009-08-04 00:07:04 | [diff] [blame] | 706 | TestConnectJobFactory* connect_job_factory_; |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 707 | // These parameters are never actually used to create a TransportConnectJob. |
Matt Menke | 84d11e56 | 2019-03-27 00:11:19 | [diff] [blame] | 708 | scoped_refptr<ClientSocketPool::SocketParams> params_; |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 709 | std::unique_ptr<TransportClientSocketPool> pool_; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 710 | ClientSocketPoolTest test_base_; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 711 | }; |
| 712 | |
Shivani Sharma | 8ae506c | 2019-07-21 21:08:27 | [diff] [blame] | 713 | // TODO(950069): Add testing for frame_origin in NetworkIsolationKey |
| 714 | // using kAppendInitiatingFrameOriginToNetworkIsolationKey. |
| 715 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 716 | TEST_F(ClientSocketPoolBaseTest, BasicSynchronous) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 717 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 718 | |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 719 | TestCompletionCallback callback; |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 720 | ClientSocketHandle handle; |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 721 | BoundTestNetLog log; |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 722 | TestLoadTimingInfoNotConnected(handle); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 723 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 724 | EXPECT_EQ(OK, handle.Init( |
| 725 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 726 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 727 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 728 | pool_.get(), log.bound())); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 729 | EXPECT_TRUE(handle.is_initialized()); |
| 730 | EXPECT_TRUE(handle.socket()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 731 | TestLoadTimingInfoConnectedNotReused(handle); |
| 732 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 733 | handle.Reset(); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 734 | TestLoadTimingInfoNotConnected(handle); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 735 | |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 736 | auto entries = log.GetEntries(); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 737 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 738 | EXPECT_EQ(5u, entries.size()); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 739 | EXPECT_TRUE(LogContainsEvent( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 740 | entries, 0, NetLogEventType::TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 741 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 742 | EXPECT_TRUE(LogContainsBeginEvent(entries, 1, NetLogEventType::SOCKET_POOL)); |
| 743 | EXPECT_TRUE(LogContainsEvent( |
| 744 | entries, 2, NetLogEventType::SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
| 745 | NetLogEventPhase::NONE)); |
| 746 | EXPECT_TRUE(LogContainsEvent(entries, 3, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 747 | NetLogEventType::SOCKET_POOL_BOUND_TO_SOCKET, |
| 748 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 749 | EXPECT_TRUE(LogContainsEndEvent(entries, 4, NetLogEventType::SOCKET_POOL)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 750 | } |
| 751 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 752 | TEST_F(ClientSocketPoolBaseTest, InitConnectionFailure) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 753 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 754 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 755 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 756 | BoundTestNetLog log; |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 757 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 758 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 759 | TestCompletionCallback callback; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 760 | // Set the additional error state members to ensure that they get cleared. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 761 | handle.set_is_ssl_error(true); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 762 | handle.set_ssl_cert_request_info(base::MakeRefCounted<SSLCertRequestInfo>()); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 763 | EXPECT_EQ( |
| 764 | ERR_CONNECTION_FAILED, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 765 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 766 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 767 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 768 | pool_.get(), log.bound())); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 769 | EXPECT_FALSE(handle.socket()); |
| 770 | EXPECT_FALSE(handle.is_ssl_error()); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 771 | EXPECT_FALSE(handle.ssl_cert_request_info()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 772 | TestLoadTimingInfoNotConnected(handle); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 773 | |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 774 | auto entries = log.GetEntries(); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 775 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 776 | EXPECT_EQ(4u, entries.size()); |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 777 | EXPECT_TRUE(LogContainsEvent( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 778 | entries, 0, NetLogEventType::TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 779 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 780 | EXPECT_TRUE(LogContainsBeginEvent(entries, 1, NetLogEventType::SOCKET_POOL)); |
| 781 | EXPECT_TRUE(LogContainsEvent( |
| 782 | entries, 2, NetLogEventType::SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
| 783 | NetLogEventPhase::NONE)); |
| 784 | EXPECT_TRUE(LogContainsEndEvent(entries, 3, NetLogEventType::SOCKET_POOL)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 785 | } |
| 786 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 787 | // Make sure different groups do not share sockets. |
| 788 | TEST_F(ClientSocketPoolBaseTest, GroupSeparation) { |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 789 | base::test::ScopedFeatureList feature_list; |
| 790 | feature_list.InitAndEnableFeature( |
| 791 | features::kPartitionConnectionsByNetworkIsolationKey); |
| 792 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 793 | CreatePool(1000 /* max_sockets */, 2 /* max_sockets_per_group */); |
| 794 | |
| 795 | const HostPortPair kHostPortPairs[] = { |
| 796 | {"a", 80}, |
| 797 | {"a", 443}, |
| 798 | {"b", 80}, |
| 799 | }; |
| 800 | |
| 801 | const ClientSocketPool::SocketType kSocketTypes[] = { |
| 802 | ClientSocketPool::SocketType::kHttp, |
| 803 | ClientSocketPool::SocketType::kSsl, |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 804 | }; |
| 805 | |
Matt Menke | bdf77780 | 2019-04-22 19:38:59 | [diff] [blame] | 806 | const PrivacyMode kPrivacyModes[] = {PrivacyMode::PRIVACY_MODE_DISABLED, |
| 807 | PrivacyMode::PRIVACY_MODE_ENABLED}; |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 808 | |
Shivani Sharma | 8ae506c | 2019-07-21 21:08:27 | [diff] [blame] | 809 | const auto kOriginA = url::Origin::Create(GURL("http://a.test/")); |
| 810 | const auto kOriginB = url::Origin::Create(GURL("http://b.test/")); |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 811 | const NetworkIsolationKey kNetworkIsolationKeys[] = { |
Shivani Sharma | 8ae506c | 2019-07-21 21:08:27 | [diff] [blame] | 812 | NetworkIsolationKey(kOriginA, kOriginA), |
| 813 | NetworkIsolationKey(kOriginB, kOriginB), |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 814 | }; |
| 815 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 816 | int total_idle_sockets = 0; |
| 817 | |
| 818 | // Walk through each GroupId, making sure that requesting a socket for one |
| 819 | // group does not return a previously connected socket for another group. |
| 820 | for (const auto& host_port_pair : kHostPortPairs) { |
| 821 | SCOPED_TRACE(host_port_pair.ToString()); |
| 822 | for (const auto& socket_type : kSocketTypes) { |
| 823 | SCOPED_TRACE(static_cast<int>(socket_type)); |
| 824 | for (const auto& privacy_mode : kPrivacyModes) { |
| 825 | SCOPED_TRACE(privacy_mode); |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 826 | for (const auto& network_isolation_key : kNetworkIsolationKeys) { |
| 827 | SCOPED_TRACE(network_isolation_key.ToString()); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 828 | |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 829 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 830 | |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 831 | ClientSocketPool::GroupId group_id( |
| 832 | host_port_pair, socket_type, privacy_mode, network_isolation_key); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 833 | |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 834 | EXPECT_FALSE(pool_->HasGroupForTesting(group_id)); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 835 | |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 836 | TestCompletionCallback callback; |
| 837 | ClientSocketHandle handle; |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 838 | |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 839 | // Since the group is empty, requesting a socket should not complete |
| 840 | // synchronously. |
| 841 | EXPECT_THAT( |
| 842 | handle.Init(group_id, params_, base::nullopt, DEFAULT_PRIORITY, |
| 843 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 844 | callback.callback(), |
| 845 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 846 | NetLogWithSource()), |
| 847 | IsError(ERR_IO_PENDING)); |
| 848 | EXPECT_TRUE(pool_->HasGroupForTesting(group_id)); |
| 849 | EXPECT_EQ(total_idle_sockets, pool_->IdleSocketCount()); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 850 | |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 851 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 852 | EXPECT_TRUE(handle.socket()); |
| 853 | EXPECT_TRUE(pool_->HasGroupForTesting(group_id)); |
| 854 | EXPECT_EQ(total_idle_sockets, pool_->IdleSocketCount()); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 855 | |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 856 | // Return socket to pool. |
| 857 | handle.Reset(); |
| 858 | EXPECT_EQ(total_idle_sockets + 1, pool_->IdleSocketCount()); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 859 | |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 860 | // Requesting a socket again should return the same socket as before, |
| 861 | // so should complete synchronously. |
| 862 | EXPECT_THAT( |
| 863 | handle.Init(group_id, params_, base::nullopt, DEFAULT_PRIORITY, |
| 864 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 865 | callback.callback(), |
| 866 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 867 | NetLogWithSource()), |
| 868 | IsOk()); |
| 869 | EXPECT_TRUE(handle.socket()); |
| 870 | EXPECT_EQ(total_idle_sockets, pool_->IdleSocketCount()); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 871 | |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 872 | // Return socket to pool again. |
| 873 | handle.Reset(); |
| 874 | EXPECT_EQ(total_idle_sockets + 1, pool_->IdleSocketCount()); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 875 | |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 876 | ++total_idle_sockets; |
| 877 | } |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 878 | } |
| 879 | } |
| 880 | } |
| 881 | } |
| 882 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 883 | TEST_F(ClientSocketPoolBaseTest, TotalLimit) { |
| 884 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 885 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 886 | // TODO(eroman): Check that the NetLog contains this event. |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 887 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 888 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 889 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
| 890 | EXPECT_THAT(StartRequest(TestGroupId("c"), DEFAULT_PRIORITY), IsOk()); |
| 891 | EXPECT_THAT(StartRequest(TestGroupId("d"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 892 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 893 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 894 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 895 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 896 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 897 | EXPECT_THAT(StartRequest(TestGroupId("e"), DEFAULT_PRIORITY), |
| 898 | IsError(ERR_IO_PENDING)); |
| 899 | EXPECT_THAT(StartRequest(TestGroupId("f"), DEFAULT_PRIORITY), |
| 900 | IsError(ERR_IO_PENDING)); |
| 901 | EXPECT_THAT(StartRequest(TestGroupId("g"), DEFAULT_PRIORITY), |
| 902 | IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 903 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 904 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 905 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 906 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 907 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 908 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 909 | |
| 910 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 911 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 912 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 913 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 914 | EXPECT_EQ(5, GetOrderOfRequest(5)); |
| 915 | EXPECT_EQ(6, GetOrderOfRequest(6)); |
| 916 | EXPECT_EQ(7, GetOrderOfRequest(7)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 917 | |
| 918 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 919 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(8)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 920 | } |
| 921 | |
| 922 | TEST_F(ClientSocketPoolBaseTest, TotalLimitReachedNewGroup) { |
| 923 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 924 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 925 | // TODO(eroman): Check that the NetLog contains this event. |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 926 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 927 | // Reach all limits: max total sockets, and max sockets per group. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 928 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 929 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 930 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
| 931 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 932 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 933 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 934 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 935 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 936 | |
| 937 | // Now create a new group and verify that we don't starve it. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 938 | EXPECT_THAT(StartRequest(TestGroupId("c"), DEFAULT_PRIORITY), |
| 939 | IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 940 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 941 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 942 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 943 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 944 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 945 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 946 | |
| 947 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 948 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 949 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 950 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 951 | EXPECT_EQ(5, GetOrderOfRequest(5)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 952 | |
| 953 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 954 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(6)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 955 | } |
| 956 | |
| 957 | TEST_F(ClientSocketPoolBaseTest, TotalLimitRespectsPriority) { |
| 958 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 959 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 960 | EXPECT_THAT(StartRequest(TestGroupId("b"), LOWEST), IsOk()); |
| 961 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsOk()); |
| 962 | EXPECT_THAT(StartRequest(TestGroupId("b"), HIGHEST), IsOk()); |
| 963 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 964 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 965 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 966 | client_socket_factory_.allocation_count()); |
| 967 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 968 | EXPECT_THAT(StartRequest(TestGroupId("c"), LOWEST), IsError(ERR_IO_PENDING)); |
| 969 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 970 | EXPECT_THAT(StartRequest(TestGroupId("b"), HIGHEST), IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 971 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 972 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 973 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 974 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 975 | |
| 976 | // First 4 requests don't have to wait, and finish in order. |
| 977 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 978 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 979 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 980 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 981 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 982 | // Request ("b", HIGHEST) has the highest priority, then (TestGroupId("a"), |
| 983 | // MEDIUM), and then ("c", LOWEST). |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 984 | EXPECT_EQ(7, GetOrderOfRequest(5)); |
| 985 | EXPECT_EQ(6, GetOrderOfRequest(6)); |
| 986 | EXPECT_EQ(5, GetOrderOfRequest(7)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 987 | |
| 988 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 989 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(9)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 990 | } |
| 991 | |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 992 | // Test reprioritizing a request before completion doesn't interfere with |
| 993 | // its completion. |
| 994 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeOne) { |
| 995 | CreatePool(kDefaultMaxSockets, 1); |
| 996 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 997 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 998 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 999 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1000 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1001 | |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 1002 | request(1)->handle()->SetPriority(HIGHEST); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1003 | |
| 1004 | ReleaseOneConnection(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1005 | |
| 1006 | EXPECT_TRUE(request(1)->handle()->socket()); |
| 1007 | } |
| 1008 | |
| 1009 | // Reprioritize a request up past another one and make sure that changes the |
| 1010 | // completion order. |
| 1011 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeUpReorder) { |
| 1012 | CreatePool(kDefaultMaxSockets, 1); |
| 1013 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1014 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1015 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1016 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1017 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1018 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1019 | EXPECT_FALSE(request(2)->handle()->socket()); |
| 1020 | |
| 1021 | request(2)->handle()->SetPriority(HIGHEST); |
| 1022 | |
| 1023 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1024 | |
| 1025 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1026 | EXPECT_EQ(3, GetOrderOfRequest(2)); |
| 1027 | EXPECT_EQ(2, GetOrderOfRequest(3)); |
| 1028 | } |
| 1029 | |
| 1030 | // Reprioritize a request without changing relative priorities and check |
| 1031 | // that the order doesn't change. |
| 1032 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeUpNoReorder) { |
| 1033 | CreatePool(kDefaultMaxSockets, 1); |
| 1034 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1035 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1036 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1037 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1038 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1039 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1040 | EXPECT_FALSE(request(2)->handle()->socket()); |
| 1041 | |
| 1042 | request(2)->handle()->SetPriority(MEDIUM); |
| 1043 | |
| 1044 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1045 | |
| 1046 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1047 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1048 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 1049 | } |
| 1050 | |
| 1051 | // Reprioritize a request past down another one and make sure that changes the |
| 1052 | // completion order. |
| 1053 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeDownReorder) { |
| 1054 | CreatePool(kDefaultMaxSockets, 1); |
| 1055 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1056 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1057 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1058 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1059 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1060 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1061 | EXPECT_FALSE(request(2)->handle()->socket()); |
| 1062 | |
| 1063 | request(1)->handle()->SetPriority(LOW); |
| 1064 | |
| 1065 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1066 | |
| 1067 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1068 | EXPECT_EQ(3, GetOrderOfRequest(2)); |
| 1069 | EXPECT_EQ(2, GetOrderOfRequest(3)); |
| 1070 | } |
| 1071 | |
| 1072 | // Reprioritize a request to the same level as another and confirm it is |
| 1073 | // put after the old request. |
| 1074 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeResetFIFO) { |
| 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"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1079 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), 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(1)->handle()->SetPriority(MEDIUM); |
| 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 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1093 | TEST_F(ClientSocketPoolBaseTest, TotalLimitRespectsGroupLimit) { |
| 1094 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1095 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1096 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsOk()); |
| 1097 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsOk()); |
| 1098 | EXPECT_THAT(StartRequest(TestGroupId("b"), HIGHEST), IsOk()); |
| 1099 | EXPECT_THAT(StartRequest(TestGroupId("b"), MEDIUM), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1100 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1101 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1102 | client_socket_factory_.allocation_count()); |
| 1103 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1104 | EXPECT_THAT(StartRequest(TestGroupId("c"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1105 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 1106 | EXPECT_THAT(StartRequest(TestGroupId("b"), HIGHEST), IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1107 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1108 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1109 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1110 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1111 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1112 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1113 | |
| 1114 | // First 4 requests don't have to wait, and finish in order. |
| 1115 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1116 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1117 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 1118 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 1119 | |
| 1120 | // Request ("b", 7) has the highest priority, but we can't make new socket for |
| 1121 | // group "b", because it has reached the per-group limit. Then we make |
| 1122 | // socket for ("c", 6), because it has higher priority than ("a", 4), |
| 1123 | // and we still can't make a socket for group "b". |
| 1124 | EXPECT_EQ(5, GetOrderOfRequest(5)); |
| 1125 | EXPECT_EQ(6, GetOrderOfRequest(6)); |
| 1126 | EXPECT_EQ(7, GetOrderOfRequest(7)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1127 | |
| 1128 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1129 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(8)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1130 | } |
| 1131 | |
| 1132 | // Make sure that we count connecting sockets against the total limit. |
| 1133 | TEST_F(ClientSocketPoolBaseTest, TotalLimitCountsConnectingSockets) { |
| 1134 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1135 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1136 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1137 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
| 1138 | EXPECT_THAT(StartRequest(TestGroupId("c"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1139 | |
| 1140 | // Create one asynchronous request. |
| 1141 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1142 | EXPECT_THAT(StartRequest(TestGroupId("d"), DEFAULT_PRIORITY), |
| 1143 | IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1144 | |
[email protected] | 6b17538 | 2009-10-13 06:47:47 | [diff] [blame] | 1145 | // We post all of our delayed tasks with a 2ms delay. I.e. they don't |
| 1146 | // actually become pending until 2ms after they have been created. In order |
| 1147 | // to flush all tasks, we need to wait so that we know there are no |
| 1148 | // soon-to-be-pending tasks waiting. |
Alex Clarke | 0def209 | 2018-12-10 12:01:45 | [diff] [blame] | 1149 | FastForwardBy(base::TimeDelta::FromMilliseconds(10)); |
[email protected] | 6b17538 | 2009-10-13 06:47:47 | [diff] [blame] | 1150 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1151 | // The next synchronous request should wait for its turn. |
| 1152 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1153 | EXPECT_THAT(StartRequest(TestGroupId("e"), DEFAULT_PRIORITY), |
| 1154 | IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1155 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1156 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1157 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1158 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1159 | client_socket_factory_.allocation_count()); |
| 1160 | |
| 1161 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1162 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1163 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 1164 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1165 | EXPECT_EQ(5, GetOrderOfRequest(5)); |
| 1166 | |
| 1167 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1168 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(6)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1169 | } |
| 1170 | |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1171 | TEST_F(ClientSocketPoolBaseTest, CorrectlyCountStalledGroups) { |
| 1172 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 1173 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 1174 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1175 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1176 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1177 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1178 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1179 | |
| 1180 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 1181 | |
| 1182 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1183 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1184 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), |
| 1185 | IsError(ERR_IO_PENDING)); |
| 1186 | EXPECT_THAT(StartRequest(TestGroupId("c"), DEFAULT_PRIORITY), |
| 1187 | IsError(ERR_IO_PENDING)); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1188 | |
| 1189 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1190 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1191 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1192 | EXPECT_EQ(kDefaultMaxSockets + 1, client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1193 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1194 | EXPECT_EQ(kDefaultMaxSockets + 2, client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1195 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
| 1196 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1197 | EXPECT_EQ(kDefaultMaxSockets + 2, client_socket_factory_.allocation_count()); |
| 1198 | } |
| 1199 | |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 1200 | TEST_F(ClientSocketPoolBaseTest, StallAndThenCancelAndTriggerAvailableSocket) { |
| 1201 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 1202 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1203 | |
| 1204 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1205 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1206 | EXPECT_EQ( |
| 1207 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1208 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1209 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1210 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1211 | pool_.get(), NetLogWithSource())); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 1212 | |
| 1213 | ClientSocketHandle handles[4]; |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 1214 | for (size_t i = 0; i < base::size(handles); ++i) { |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1215 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1216 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1217 | handles[i].Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1218 | TestGroupId("b"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1219 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1220 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1221 | pool_.get(), NetLogWithSource())); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 1222 | } |
| 1223 | |
| 1224 | // One will be stalled, cancel all the handles now. |
| 1225 | // This should hit the OnAvailableSocketSlot() code where we previously had |
| 1226 | // stalled groups, but no longer have any. |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 1227 | for (size_t i = 0; i < base::size(handles); ++i) |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 1228 | handles[i].Reset(); |
| 1229 | } |
| 1230 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1231 | TEST_F(ClientSocketPoolBaseTest, CancelStalledSocketAtSocketLimit) { |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1232 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1233 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 1234 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1235 | { |
| 1236 | ClientSocketHandle handles[kDefaultMaxSockets]; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1237 | TestCompletionCallback callbacks[kDefaultMaxSockets]; |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1238 | for (int i = 0; i < kDefaultMaxSockets; ++i) { |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1239 | EXPECT_EQ(OK, |
| 1240 | handles[i].Init(TestGroupId(base::NumberToString(i)), params_, |
| 1241 | base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
| 1242 | ClientSocketPool::RespectLimits::ENABLED, |
| 1243 | callbacks[i].callback(), |
| 1244 | ClientSocketPool::ProxyAuthCallback(), |
| 1245 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1246 | } |
| 1247 | |
| 1248 | // Force a stalled group. |
| 1249 | ClientSocketHandle stalled_handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1250 | TestCompletionCallback callback; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1251 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1252 | stalled_handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1253 | TestGroupId("foo"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1254 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1255 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1256 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1257 | |
| 1258 | // Cancel the stalled request. |
| 1259 | stalled_handle.Reset(); |
| 1260 | |
| 1261 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1262 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 1263 | |
| 1264 | // Dropping out of scope will close all handles and return them to idle. |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1265 | } |
| 1266 | |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1267 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1268 | EXPECT_EQ(kDefaultMaxSockets, pool_->IdleSocketCount()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1269 | } |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1270 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1271 | TEST_F(ClientSocketPoolBaseTest, CancelPendingSocketAtSocketLimit) { |
| 1272 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1273 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 1274 | |
| 1275 | { |
| 1276 | ClientSocketHandle handles[kDefaultMaxSockets]; |
| 1277 | for (int i = 0; i < kDefaultMaxSockets; ++i) { |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1278 | TestCompletionCallback callback; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1279 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1280 | handles[i].Init(TestGroupId(base::NumberToString(i)), params_, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1281 | base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1282 | ClientSocketPool::RespectLimits::ENABLED, |
| 1283 | callback.callback(), |
| 1284 | ClientSocketPool::ProxyAuthCallback(), |
| 1285 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1286 | } |
| 1287 | |
| 1288 | // Force a stalled group. |
| 1289 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1290 | ClientSocketHandle stalled_handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1291 | TestCompletionCallback callback; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1292 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1293 | stalled_handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1294 | TestGroupId("foo"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1295 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1296 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1297 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1298 | |
| 1299 | // Since it is stalled, it should have no connect jobs. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1300 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("foo"))); |
| 1301 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 1302 | TestGroupId("foo"))); |
| 1303 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroupForTesting( |
| 1304 | TestGroupId("foo"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1305 | |
| 1306 | // Cancel the stalled request. |
| 1307 | handles[0].Reset(); |
| 1308 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1309 | // Now we should have a connect job. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1310 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("foo"))); |
| 1311 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 1312 | TestGroupId("foo"))); |
| 1313 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroupForTesting( |
| 1314 | TestGroupId("foo"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1315 | |
| 1316 | // The stalled socket should connect. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1317 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1318 | |
| 1319 | EXPECT_EQ(kDefaultMaxSockets + 1, |
| 1320 | client_socket_factory_.allocation_count()); |
| 1321 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1322 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("foo"))); |
| 1323 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 1324 | TestGroupId("foo"))); |
| 1325 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroupForTesting( |
| 1326 | TestGroupId("foo"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1327 | |
| 1328 | // Dropping out of scope will close all handles and return them to idle. |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1329 | } |
| 1330 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1331 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 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, WaitForStalledSocketAtSocketLimit) { |
| 1335 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1336 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1337 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1338 | ClientSocketHandle stalled_handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1339 | TestCompletionCallback callback; |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1340 | { |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 1341 | EXPECT_FALSE(pool_->IsStalled()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1342 | ClientSocketHandle handles[kDefaultMaxSockets]; |
| 1343 | for (int i = 0; i < kDefaultMaxSockets; ++i) { |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1344 | TestCompletionCallback callback; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1345 | EXPECT_EQ( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1346 | OK, handles[i].Init( |
| 1347 | TestGroupId(base::StringPrintf("Take 2: %d", i)), params_, |
| 1348 | base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
| 1349 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1350 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 1351 | NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1352 | } |
| 1353 | |
| 1354 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1355 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 1356 | EXPECT_FALSE(pool_->IsStalled()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1357 | |
| 1358 | // Now we will hit the socket limit. |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1359 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1360 | stalled_handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1361 | TestGroupId("foo"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1362 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1363 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1364 | pool_.get(), NetLogWithSource())); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 1365 | EXPECT_TRUE(pool_->IsStalled()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1366 | |
| 1367 | // Dropping out of scope will close all handles and return them to idle. |
| 1368 | } |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1369 | |
| 1370 | // But if we wait for it, the released idle sockets will be closed in |
| 1371 | // preference of the waiting request. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1372 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1373 | |
| 1374 | EXPECT_EQ(kDefaultMaxSockets + 1, client_socket_factory_.allocation_count()); |
| 1375 | EXPECT_EQ(3, pool_->IdleSocketCount()); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1376 | } |
| 1377 | |
| 1378 | // Regression test for http://crbug.com/40952. |
| 1379 | TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketAtSocketLimitDeleteGroup) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1380 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 1381 | true /* enable_backup_connect_jobs */); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1382 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 1383 | |
| 1384 | for (int i = 0; i < kDefaultMaxSockets; ++i) { |
| 1385 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1386 | TestCompletionCallback callback; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1387 | EXPECT_EQ(OK, handle.Init(TestGroupId(base::NumberToString(i)), params_, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1388 | base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1389 | ClientSocketPool::RespectLimits::ENABLED, |
| 1390 | callback.callback(), |
| 1391 | ClientSocketPool::ProxyAuthCallback(), |
| 1392 | pool_.get(), NetLogWithSource())); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1393 | } |
| 1394 | |
| 1395 | // Flush all the DoReleaseSocket tasks. |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 1396 | base::RunLoop().RunUntilIdle(); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1397 | |
| 1398 | // Stall a group. Set a pending job so it'll trigger a backup job if we don't |
| 1399 | // reuse a socket. |
| 1400 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1401 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1402 | TestCompletionCallback callback; |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1403 | |
| 1404 | // "0" is special here, since it should be the first entry in the sorted map, |
| 1405 | // which is the one which we would close an idle socket for. We shouldn't |
| 1406 | // close an idle socket though, since we should reuse the idle socket. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1407 | EXPECT_EQ(OK, handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1408 | TestGroupId("0"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1409 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1410 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1411 | pool_.get(), NetLogWithSource())); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1412 | |
| 1413 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1414 | EXPECT_EQ(kDefaultMaxSockets - 1, pool_->IdleSocketCount()); |
| 1415 | } |
| 1416 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1417 | TEST_F(ClientSocketPoolBaseTest, PendingRequests) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1418 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1419 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1420 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1421 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1422 | EXPECT_THAT(StartRequest(TestGroupId("a"), IDLE), IsError(ERR_IO_PENDING)); |
| 1423 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
| 1424 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1425 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1426 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 1427 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1428 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1429 | ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1430 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
| 1431 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1432 | EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup, |
| 1433 | completion_count()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1434 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1435 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1436 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
[email protected] | c9c6f5c | 2010-07-31 01:30:03 | [diff] [blame] | 1437 | EXPECT_EQ(8, GetOrderOfRequest(3)); |
| 1438 | EXPECT_EQ(6, GetOrderOfRequest(4)); |
| 1439 | EXPECT_EQ(4, GetOrderOfRequest(5)); |
| 1440 | EXPECT_EQ(3, GetOrderOfRequest(6)); |
| 1441 | EXPECT_EQ(5, GetOrderOfRequest(7)); |
| 1442 | EXPECT_EQ(7, GetOrderOfRequest(8)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1443 | |
| 1444 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1445 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(9)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1446 | } |
| 1447 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1448 | TEST_F(ClientSocketPoolBaseTest, PendingRequests_NoKeepAlive) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1449 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1450 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1451 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1452 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1453 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
| 1454 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1455 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1456 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 1457 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1458 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1459 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1460 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1461 | for (size_t i = kDefaultMaxSocketsPerGroup; i < requests_size(); ++i) |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1462 | EXPECT_THAT(request(i)->WaitForResult(), IsOk()); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1463 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1464 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1465 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1466 | EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup, |
| 1467 | completion_count()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1468 | } |
| 1469 | |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1470 | TEST_F(ClientSocketPoolBaseTest, ResetAndCloseSocket) { |
| 1471 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1472 | |
| 1473 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1474 | ClientSocketHandle handle; |
| 1475 | TestCompletionCallback callback; |
| 1476 | EXPECT_EQ( |
| 1477 | ERR_IO_PENDING, |
| 1478 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1479 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1480 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1481 | pool_.get(), NetLogWithSource())); |
| 1482 | |
| 1483 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 1484 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1485 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1486 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
| 1487 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 1488 | |
| 1489 | handle.ResetAndCloseSocket(); |
| 1490 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1491 | } |
| 1492 | |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1493 | // This test will start up a socket request and then call Reset() on the handle. |
| 1494 | // The pending ConnectJob should not be destroyed. |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1495 | TEST_F(ClientSocketPoolBaseTest, CancelRequestKeepsConnectJob) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1496 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1497 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1498 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1499 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1500 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1501 | EXPECT_EQ( |
| 1502 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1503 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1504 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1505 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1506 | pool_.get(), NetLogWithSource())); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1507 | handle.Reset(); |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1508 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1509 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1510 | } |
| 1511 | |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1512 | // This test will start up a socket request and then call ResetAndCloseSocket() |
| 1513 | // on the handle. The pending ConnectJob or connected socket should be |
| 1514 | // destroyed. |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1515 | TEST_F(ClientSocketPoolBaseTest, CancelRequestAndCloseSocket) { |
| 1516 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1517 | |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1518 | // When true, the socket connects before it's canceled. |
| 1519 | for (bool cancel_when_callback_pending : {false, true}) { |
| 1520 | if (cancel_when_callback_pending) { |
| 1521 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 1522 | } else { |
| 1523 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1524 | } |
| 1525 | ClientSocketHandle handle; |
| 1526 | TestCompletionCallback callback; |
| 1527 | EXPECT_EQ( |
| 1528 | ERR_IO_PENDING, |
| 1529 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1530 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1531 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1532 | pool_.get(), NetLogWithSource())); |
| 1533 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1534 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1535 | |
| 1536 | if (cancel_when_callback_pending) { |
| 1537 | client_socket_factory_.SignalJobs(); |
| 1538 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1539 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 1540 | } |
| 1541 | |
| 1542 | handle.ResetAndCloseSocket(); |
| 1543 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1544 | } |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1545 | } |
| 1546 | |
| 1547 | TEST_F(ClientSocketPoolBaseTest, |
| 1548 | CancelRequestAndCloseSocketWhenMoreRequestsThanConnectJobs) { |
| 1549 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1550 | |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1551 | // When true, the sockets connect before they're canceled. |
| 1552 | for (bool cancel_when_callback_pending : {false, true}) { |
| 1553 | if (cancel_when_callback_pending) { |
| 1554 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 1555 | } else { |
| 1556 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1557 | } |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1558 | |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1559 | std::vector<std::unique_ptr<ClientSocketHandle>> handles; |
| 1560 | TestCompletionCallback callback; |
| 1561 | // Make |kDefaultMaxSockets + 1| socket requests. |
| 1562 | for (int i = 0; i < kDefaultMaxSocketsPerGroup + 1; ++i) { |
| 1563 | std::unique_ptr<ClientSocketHandle> handle = |
| 1564 | std::make_unique<ClientSocketHandle>(); |
| 1565 | EXPECT_EQ(ERR_IO_PENDING, |
| 1566 | handle->Init( |
| 1567 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1568 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1569 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1570 | pool_.get(), NetLogWithSource())); |
| 1571 | handles.push_back(std::move(handle)); |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1572 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1573 | EXPECT_EQ( |
| 1574 | static_cast<size_t>(std::min(i + 1, kDefaultMaxSocketsPerGroup)), |
| 1575 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1576 | } |
| 1577 | |
| 1578 | if (cancel_when_callback_pending) { |
| 1579 | client_socket_factory_.SignalJobs(); |
| 1580 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1581 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
| 1582 | pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 1583 | } |
| 1584 | |
| 1585 | // Calling ResetAndCloseSocket() on a handle should not cancel a ConnectJob |
| 1586 | // or close a socket, since there are more requests than ConnectJobs or |
| 1587 | // sockets. |
| 1588 | handles[kDefaultMaxSocketsPerGroup]->ResetAndCloseSocket(); |
| 1589 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1590 | if (cancel_when_callback_pending) { |
| 1591 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
| 1592 | pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 1593 | } else { |
| 1594 | EXPECT_EQ(static_cast<size_t>(kDefaultMaxSocketsPerGroup), |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1595 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1596 | } |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1597 | |
| 1598 | // Calling ResetAndCloseSocket() on other handles should cancel a ConnectJob |
| 1599 | // or close a socket. |
| 1600 | for (int i = kDefaultMaxSocketsPerGroup - 1; i >= 0; --i) { |
| 1601 | handles[i]->ResetAndCloseSocket(); |
| 1602 | if (i > 0) { |
| 1603 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1604 | if (cancel_when_callback_pending) { |
| 1605 | EXPECT_EQ(i, |
| 1606 | pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 1607 | } else { |
| 1608 | EXPECT_EQ(static_cast<size_t>(i), |
| 1609 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1610 | } |
| 1611 | } else { |
| 1612 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1613 | } |
| 1614 | } |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1615 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1616 | } |
| 1617 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1618 | TEST_F(ClientSocketPoolBaseTest, ConnectCancelConnect) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1619 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1620 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1621 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 1622 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1623 | TestCompletionCallback callback; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1624 | |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1625 | EXPECT_EQ( |
| 1626 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1627 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1628 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1629 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1630 | pool_.get(), NetLogWithSource())); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1631 | |
| 1632 | handle.Reset(); |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1633 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1634 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1635 | |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1636 | // This will create a second ConnectJob, since the other ConnectJob was |
| 1637 | // previously assigned to a request. |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1638 | TestCompletionCallback callback2; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1639 | EXPECT_EQ( |
| 1640 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1641 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1642 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1643 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1644 | pool_.get(), NetLogWithSource())); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1645 | |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1646 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1647 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1648 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1649 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1650 | EXPECT_FALSE(callback.have_result()); |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1651 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1652 | // One ConnectJob completed, and its socket is now assigned to |handle|. |
| 1653 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 1654 | // The other ConnectJob should have either completed, or still be connecting. |
| 1655 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")) + |
| 1656 | pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1657 | |
| 1658 | handle.Reset(); |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1659 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1660 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")) + |
| 1661 | pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
| 1662 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1663 | } |
| 1664 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1665 | TEST_F(ClientSocketPoolBaseTest, CancelRequest) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1666 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1667 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1668 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1669 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1670 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
| 1671 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1672 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1673 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 1674 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1675 | |
| 1676 | // Cancel a request. |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1677 | size_t index_to_cancel = kDefaultMaxSocketsPerGroup + 2; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1678 | EXPECT_FALSE((*requests())[index_to_cancel]->handle()->is_initialized()); |
| 1679 | (*requests())[index_to_cancel]->handle()->Reset(); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1680 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1681 | ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1682 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1683 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
| 1684 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1685 | EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup - 1, |
| 1686 | completion_count()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1687 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1688 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1689 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1690 | EXPECT_EQ(5, GetOrderOfRequest(3)); |
| 1691 | EXPECT_EQ(3, GetOrderOfRequest(4)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1692 | EXPECT_EQ(ClientSocketPoolTest::kRequestNotFound, |
| 1693 | GetOrderOfRequest(5)); // Canceled request. |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1694 | EXPECT_EQ(4, GetOrderOfRequest(6)); |
| 1695 | EXPECT_EQ(6, GetOrderOfRequest(7)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1696 | |
| 1697 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1698 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(8)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1699 | } |
| 1700 | |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1701 | // Function to be used as a callback on socket request completion. It first |
| 1702 | // disconnects the successfully connected socket from the first request, and |
| 1703 | // then reuses the ClientSocketHandle to request another socket. |
| 1704 | // |
| 1705 | // |nested_callback| is called with the result of the second socket request. |
| 1706 | void RequestSocketOnComplete(ClientSocketHandle* handle, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1707 | TransportClientSocketPool* pool, |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1708 | TestConnectJobFactory* test_connect_job_factory, |
| 1709 | TestConnectJob::JobType next_job_type, |
Bence Béky | a4a5093 | 2018-08-10 13:39:41 | [diff] [blame] | 1710 | TestCompletionCallback* nested_callback, |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1711 | int first_request_result) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1712 | EXPECT_THAT(first_request_result, IsOk()); |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1713 | |
| 1714 | test_connect_job_factory->set_job_type(next_job_type); |
| 1715 | |
| 1716 | // Don't allow reuse of the socket. Disconnect it and then release it. |
| 1717 | if (handle->socket()) |
| 1718 | handle->socket()->Disconnect(); |
| 1719 | handle->Reset(); |
| 1720 | |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1721 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1722 | int rv = handle->Init( |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 1723 | TestGroupId("a"), |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1724 | ClientSocketPool::SocketParams::CreateForHttpForTesting(), base::nullopt, |
| 1725 | LOWEST, SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 1726 | nested_callback->callback(), ClientSocketPool::ProxyAuthCallback(), pool, |
| 1727 | NetLogWithSource()); |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1728 | if (rv != ERR_IO_PENDING) { |
| 1729 | DCHECK_EQ(TestConnectJob::kMockJob, next_job_type); |
Bence Béky | a4a5093 | 2018-08-10 13:39:41 | [diff] [blame] | 1730 | nested_callback->callback().Run(rv); |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1731 | } else { |
| 1732 | DCHECK_EQ(TestConnectJob::kMockPendingJob, next_job_type); |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1733 | } |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1734 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1735 | |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1736 | // Tests the case where a second socket is requested in a completion callback, |
| 1737 | // and the second socket connects asynchronously. Reuses the same |
| 1738 | // ClientSocketHandle for the second socket, after disconnecting the first. |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1739 | TEST_F(ClientSocketPoolBaseTest, RequestPendingJobTwice) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1740 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1741 | |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 1742 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 1743 | ClientSocketHandle handle; |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1744 | TestCompletionCallback second_result_callback; |
| 1745 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1746 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Paul Jensen | 8d6f87ec | 2018-01-13 00:46:54 | [diff] [blame] | 1747 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1748 | base::BindOnce(&RequestSocketOnComplete, &handle, pool_.get(), |
| 1749 | connect_job_factory_, TestConnectJob::kMockPendingJob, |
| 1750 | &second_result_callback), |
| 1751 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1752 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1753 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1754 | EXPECT_THAT(second_result_callback.WaitForResult(), IsOk()); |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1755 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1756 | |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1757 | // Tests the case where a second socket is requested in a completion callback, |
| 1758 | // and the second socket connects synchronously. Reuses the same |
| 1759 | // ClientSocketHandle for the second socket, after disconnecting the first. |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1760 | TEST_F(ClientSocketPoolBaseTest, RequestPendingJobThenSynchronous) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1761 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1762 | |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 1763 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 1764 | ClientSocketHandle handle; |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1765 | TestCompletionCallback second_result_callback; |
| 1766 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1767 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Paul Jensen | 8d6f87ec | 2018-01-13 00:46:54 | [diff] [blame] | 1768 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1769 | base::BindOnce(&RequestSocketOnComplete, &handle, pool_.get(), |
| 1770 | connect_job_factory_, TestConnectJob::kMockPendingJob, |
| 1771 | &second_result_callback), |
| 1772 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1773 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1774 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1775 | EXPECT_THAT(second_result_callback.WaitForResult(), IsOk()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1776 | } |
| 1777 | |
| 1778 | // Make sure that pending requests get serviced after active requests get |
| 1779 | // cancelled. |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1780 | TEST_F(ClientSocketPoolBaseTest, CancelActiveRequestWithPendingRequests) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1781 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1782 | |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 1783 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1784 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1785 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1786 | IsError(ERR_IO_PENDING)); |
| 1787 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1788 | IsError(ERR_IO_PENDING)); |
| 1789 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1790 | IsError(ERR_IO_PENDING)); |
| 1791 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1792 | IsError(ERR_IO_PENDING)); |
| 1793 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1794 | IsError(ERR_IO_PENDING)); |
| 1795 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1796 | IsError(ERR_IO_PENDING)); |
| 1797 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1798 | IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1799 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1800 | // Now, kDefaultMaxSocketsPerGroup requests should be active. |
| 1801 | // Let's cancel them. |
| 1802 | for (int i = 0; i < kDefaultMaxSocketsPerGroup; ++i) { |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1803 | ASSERT_FALSE(request(i)->handle()->is_initialized()); |
| 1804 | request(i)->handle()->Reset(); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1805 | } |
| 1806 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1807 | // Let's wait for the rest to complete now. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1808 | for (size_t i = kDefaultMaxSocketsPerGroup; i < requests_size(); ++i) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1809 | EXPECT_THAT(request(i)->WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1810 | request(i)->handle()->Reset(); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1811 | } |
| 1812 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1813 | EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup, |
| 1814 | completion_count()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1815 | } |
| 1816 | |
| 1817 | // Make sure that pending requests get serviced after active requests fail. |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1818 | TEST_F(ClientSocketPoolBaseTest, FailingActiveRequestWithPendingRequests) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1819 | const size_t kMaxSockets = 5; |
| 1820 | CreatePool(kMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1821 | |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 1822 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1823 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1824 | const size_t kNumberOfRequests = 2 * kDefaultMaxSocketsPerGroup + 1; |
| 1825 | ASSERT_LE(kNumberOfRequests, kMaxSockets); // Otherwise the test will hang. |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1826 | |
| 1827 | // Queue up all the requests |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1828 | for (size_t i = 0; i < kNumberOfRequests; ++i) |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1829 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1830 | IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1831 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1832 | for (size_t i = 0; i < kNumberOfRequests; ++i) |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1833 | EXPECT_THAT(request(i)->WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1834 | } |
| 1835 | |
mmenke | 9d72fe4 | 2017-05-18 22:36:07 | [diff] [blame] | 1836 | // Make sure that pending requests that complete synchronously get serviced |
| 1837 | // after active requests fail. See https://crbug.com/723748 |
| 1838 | TEST_F(ClientSocketPoolBaseTest, HandleMultipleSyncFailuresAfterAsyncFailure) { |
| 1839 | const size_t kNumberOfRequests = 10; |
| 1840 | const size_t kMaxSockets = 1; |
| 1841 | CreatePool(kMaxSockets, kMaxSockets); |
| 1842 | |
| 1843 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
| 1844 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1845 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1846 | IsError(ERR_IO_PENDING)); |
mmenke | 9d72fe4 | 2017-05-18 22:36:07 | [diff] [blame] | 1847 | |
| 1848 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
| 1849 | |
| 1850 | // Queue up all the other requests |
| 1851 | for (size_t i = 1; i < kNumberOfRequests; ++i) |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1852 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1853 | IsError(ERR_IO_PENDING)); |
mmenke | 9d72fe4 | 2017-05-18 22:36:07 | [diff] [blame] | 1854 | |
| 1855 | // Make sure all requests fail, instead of hanging. |
| 1856 | for (size_t i = 0; i < kNumberOfRequests; ++i) |
| 1857 | EXPECT_THAT(request(i)->WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 1858 | } |
| 1859 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1860 | TEST_F(ClientSocketPoolBaseTest, CancelActiveRequestThenRequestSocket) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1861 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1862 | |
| 1863 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1864 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1865 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1866 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1867 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1868 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1869 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1870 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1871 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1872 | |
| 1873 | // Cancel the active request. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1874 | handle.Reset(); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1875 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1876 | rv = handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1877 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1878 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1879 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1880 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 1881 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1882 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1883 | EXPECT_FALSE(handle.is_reused()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 1884 | TestLoadTimingInfoConnectedNotReused(handle); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1885 | EXPECT_EQ(2, client_socket_factory_.allocation_count()); |
| 1886 | } |
| 1887 | |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1888 | TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketsForced) { |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1889 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1890 | ClientSocketHandle handle; |
| 1891 | TestCompletionCallback callback; |
| 1892 | BoundTestNetLog log; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1893 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1894 | TestGroupId("a"), params_, base::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1895 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1896 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1897 | EXPECT_THAT(rv, IsOk()); |
| 1898 | handle.Reset(); |
| 1899 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 1900 | pool_->CloseIdleSockets(); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1901 | } |
| 1902 | |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1903 | TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketsInGroupForced) { |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1904 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1905 | TestCompletionCallback callback; |
| 1906 | BoundTestNetLog log; |
| 1907 | ClientSocketHandle handle1; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1908 | int rv = handle1.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1909 | TestGroupId("a"), params_, base::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1910 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1911 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1912 | EXPECT_THAT(rv, IsOk()); |
| 1913 | ClientSocketHandle handle2; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1914 | rv = handle2.Init(TestGroupId("a"), params_, base::nullopt, LOWEST, |
| 1915 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1916 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1917 | pool_.get(), log.bound()); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1918 | ClientSocketHandle handle3; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1919 | rv = handle3.Init(TestGroupId("b"), params_, base::nullopt, LOWEST, |
| 1920 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1921 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1922 | pool_.get(), log.bound()); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1923 | EXPECT_THAT(rv, IsOk()); |
| 1924 | handle1.Reset(); |
| 1925 | handle2.Reset(); |
| 1926 | handle3.Reset(); |
| 1927 | EXPECT_EQ(3, pool_->IdleSocketCount()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1928 | pool_->CloseIdleSocketsInGroup(TestGroupId("a")); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1929 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1930 | } |
| 1931 | |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1932 | TEST_F(ClientSocketPoolBaseTest, CleanUpUnusableIdleSockets) { |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1933 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1934 | ClientSocketHandle handle; |
| 1935 | TestCompletionCallback callback; |
| 1936 | BoundTestNetLog log; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1937 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1938 | TestGroupId("a"), params_, base::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1939 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1940 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1941 | EXPECT_THAT(rv, IsOk()); |
| 1942 | StreamSocket* socket = handle.socket(); |
| 1943 | handle.Reset(); |
| 1944 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 1945 | |
| 1946 | // Disconnect socket now to make the socket unusable. |
| 1947 | socket->Disconnect(); |
| 1948 | ClientSocketHandle handle2; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1949 | rv = handle2.Init(TestGroupId("a"), params_, base::nullopt, LOWEST, |
| 1950 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1951 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1952 | pool_.get(), log.bound()); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1953 | EXPECT_THAT(rv, IsOk()); |
| 1954 | EXPECT_FALSE(handle2.is_reused()); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1955 | } |
| 1956 | |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1957 | // Regression test for http://crbug.com/17985. |
| 1958 | TEST_F(ClientSocketPoolBaseTest, GroupWithPendingRequestsIsNotEmpty) { |
| 1959 | const int kMaxSockets = 3; |
| 1960 | const int kMaxSocketsPerGroup = 2; |
| 1961 | CreatePool(kMaxSockets, kMaxSocketsPerGroup); |
| 1962 | |
[email protected] | ac790b4 | 2009-12-02 04:31:31 | [diff] [blame] | 1963 | const RequestPriority kHighPriority = HIGHEST; |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1964 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1965 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1966 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1967 | |
| 1968 | // This is going to be a pending request in an otherwise empty group. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1969 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1970 | IsError(ERR_IO_PENDING)); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1971 | |
| 1972 | // Reach the maximum socket limit. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1973 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1974 | |
| 1975 | // Create a stalled group with high priorities. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1976 | EXPECT_THAT(StartRequest(TestGroupId("c"), kHighPriority), |
| 1977 | IsError(ERR_IO_PENDING)); |
| 1978 | EXPECT_THAT(StartRequest(TestGroupId("c"), kHighPriority), |
| 1979 | IsError(ERR_IO_PENDING)); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1980 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1981 | // Release the first two sockets from TestGroupId("a"). Because this is a |
| 1982 | // keepalive, the first release will unblock the pending request for |
| 1983 | // TestGroupId("a"). The second release will unblock a request for "c", |
| 1984 | // because it is the next high priority socket. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1985 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
| 1986 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1987 | |
| 1988 | // Closing idle sockets should not get us into trouble, but in the bug |
| 1989 | // we were hitting a CHECK here. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1990 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1991 | pool_->CloseIdleSockets(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1992 | |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 1993 | // Run the released socket wakeups. |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 1994 | base::RunLoop().RunUntilIdle(); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1995 | } |
| 1996 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 1997 | TEST_F(ClientSocketPoolBaseTest, BasicAsynchronous) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1998 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1999 | |
| 2000 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2001 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2002 | TestCompletionCallback callback; |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 2003 | BoundTestNetLog log; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2004 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2005 | TestGroupId("a"), params_, base::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2006 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2007 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2008 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2009 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2010 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 2011 | TestLoadTimingInfoNotConnected(handle); |
| 2012 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2013 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2014 | EXPECT_TRUE(handle.is_initialized()); |
| 2015 | EXPECT_TRUE(handle.socket()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 2016 | TestLoadTimingInfoConnectedNotReused(handle); |
| 2017 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2018 | handle.Reset(); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 2019 | TestLoadTimingInfoNotConnected(handle); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 2020 | |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 2021 | auto entries = log.GetEntries(); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 2022 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2023 | EXPECT_EQ(5u, entries.size()); |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 2024 | EXPECT_TRUE(LogContainsEvent( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2025 | entries, 0, NetLogEventType::TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 2026 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2027 | EXPECT_TRUE(LogContainsBeginEvent(entries, 1, NetLogEventType::SOCKET_POOL)); |
| 2028 | EXPECT_TRUE(LogContainsEvent( |
| 2029 | entries, 2, NetLogEventType::SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
| 2030 | NetLogEventPhase::NONE)); |
| 2031 | EXPECT_TRUE(LogContainsEvent(entries, 3, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 2032 | NetLogEventType::SOCKET_POOL_BOUND_TO_SOCKET, |
| 2033 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2034 | EXPECT_TRUE(LogContainsEndEvent(entries, 4, NetLogEventType::SOCKET_POOL)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2035 | } |
| 2036 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2037 | TEST_F(ClientSocketPoolBaseTest, |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2038 | InitConnectionAsynchronousFailure) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2039 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2040 | |
| 2041 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2042 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2043 | TestCompletionCallback callback; |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 2044 | BoundTestNetLog log; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2045 | // Set the additional error state members to ensure that they get cleared. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2046 | handle.set_is_ssl_error(true); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 2047 | handle.set_ssl_cert_request_info(base::MakeRefCounted<SSLCertRequestInfo>()); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2048 | EXPECT_EQ( |
| 2049 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2050 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2051 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2052 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2053 | pool_.get(), log.bound())); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2054 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2055 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2056 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2057 | EXPECT_FALSE(handle.is_ssl_error()); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 2058 | EXPECT_FALSE(handle.ssl_cert_request_info()); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 2059 | |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 2060 | auto entries = log.GetEntries(); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 2061 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2062 | EXPECT_EQ(4u, entries.size()); |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 2063 | EXPECT_TRUE(LogContainsEvent( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2064 | entries, 0, NetLogEventType::TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 2065 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2066 | EXPECT_TRUE(LogContainsBeginEvent(entries, 1, NetLogEventType::SOCKET_POOL)); |
| 2067 | EXPECT_TRUE(LogContainsEvent( |
| 2068 | entries, 2, NetLogEventType::SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
| 2069 | NetLogEventPhase::NONE)); |
| 2070 | EXPECT_TRUE(LogContainsEndEvent(entries, 3, NetLogEventType::SOCKET_POOL)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2071 | } |
| 2072 | |
mmenke | 6be122f | 2015-03-09 22:22:47 | [diff] [blame] | 2073 | // Check that an async ConnectJob failure does not result in creation of a new |
| 2074 | // ConnectJob when there's another pending request also waiting on its own |
| 2075 | // ConnectJob. See http://crbug.com/463960. |
| 2076 | TEST_F(ClientSocketPoolBaseTest, AsyncFailureWithPendingRequestWithJob) { |
| 2077 | CreatePool(2, 2); |
| 2078 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
| 2079 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2080 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2081 | IsError(ERR_IO_PENDING)); |
| 2082 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2083 | IsError(ERR_IO_PENDING)); |
mmenke | 6be122f | 2015-03-09 22:22:47 | [diff] [blame] | 2084 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2085 | EXPECT_THAT(request(0)->WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 2086 | EXPECT_THAT(request(1)->WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
mmenke | 6be122f | 2015-03-09 22:22:47 | [diff] [blame] | 2087 | |
| 2088 | EXPECT_EQ(2, client_socket_factory_.allocation_count()); |
| 2089 | } |
| 2090 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2091 | TEST_F(ClientSocketPoolBaseTest, TwoRequestsCancelOne) { |
[email protected] | b22b516 | 2010-03-16 07:53:10 | [diff] [blame] | 2092 | // TODO(eroman): Add back the log expectations! Removed them because the |
| 2093 | // ordering is difficult, and some may fire during destructor. |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2094 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2095 | |
| 2096 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2097 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2098 | TestCompletionCallback callback; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2099 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2100 | TestCompletionCallback callback2; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2101 | |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2102 | EXPECT_EQ( |
| 2103 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2104 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2105 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2106 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2107 | pool_.get(), NetLogWithSource())); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 2108 | BoundTestNetLog log2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 2109 | EXPECT_EQ( |
| 2110 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2111 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2112 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2113 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2114 | pool_.get(), NetLogWithSource())); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2115 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2116 | handle.Reset(); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2117 | |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 2118 | |
| 2119 | // 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] | 2120 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2121 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2122 | handle2.Reset(); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 2123 | |
| 2124 | // Now request 2 has actually finished. |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 2125 | // TODO(eroman): Add back log expectations. |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2126 | } |
| 2127 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2128 | TEST_F(ClientSocketPoolBaseTest, CancelRequestLimitsJobs) { |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 2129 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2130 | |
[email protected] | 17a0c6c | 2009-08-04 00:07:04 | [diff] [blame] | 2131 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2132 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2133 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
| 2134 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 2135 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 2136 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 2137 | |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2138 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2139 | static_cast<int>( |
| 2140 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2141 | (*requests())[2]->handle()->Reset(); |
| 2142 | (*requests())[3]->handle()->Reset(); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2143 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2144 | static_cast<int>( |
| 2145 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 2146 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2147 | (*requests())[1]->handle()->Reset(); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2148 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2149 | static_cast<int>( |
| 2150 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 2151 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2152 | (*requests())[0]->handle()->Reset(); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2153 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2154 | static_cast<int>( |
| 2155 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 2156 | } |
| 2157 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2158 | // When requests and ConnectJobs are not coupled, the request will get serviced |
| 2159 | // by whatever comes first. |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2160 | TEST_F(ClientSocketPoolBaseTest, ReleaseSockets) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2161 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2162 | |
| 2163 | // Start job 1 (async OK) |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 2164 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2165 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2166 | std::vector<TestSocketRequest*> request_order; |
| 2167 | size_t completion_count; // unused |
| 2168 | TestSocketRequest req1(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2169 | int rv = req1.handle()->Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2170 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2171 | ClientSocketPool::RespectLimits::ENABLED, req1.callback(), |
| 2172 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2173 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 2174 | EXPECT_THAT(req1.WaitForResult(), IsOk()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2175 | |
| 2176 | // Job 1 finished OK. Start job 2 (also async OK). Request 3 is pending |
| 2177 | // without a job. |
| 2178 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2179 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2180 | TestSocketRequest req2(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2181 | rv = req2.handle()->Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2182 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2183 | ClientSocketPool::RespectLimits::ENABLED, req2.callback(), |
| 2184 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2185 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2186 | TestSocketRequest req3(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2187 | rv = req3.handle()->Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2188 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2189 | ClientSocketPool::RespectLimits::ENABLED, req3.callback(), |
| 2190 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2191 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2192 | |
| 2193 | // Both Requests 2 and 3 are pending. We release socket 1 which should |
| 2194 | // service request 2. Request 3 should still be waiting. |
[email protected] | a6c59f6 | 2009-07-29 16:33:33 | [diff] [blame] | 2195 | req1.handle()->Reset(); |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 2196 | // Run the released socket wakeups. |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 2197 | base::RunLoop().RunUntilIdle(); |
[email protected] | a6c59f6 | 2009-07-29 16:33:33 | [diff] [blame] | 2198 | ASSERT_TRUE(req2.handle()->socket()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2199 | EXPECT_THAT(req2.WaitForResult(), IsOk()); |
[email protected] | a6c59f6 | 2009-07-29 16:33:33 | [diff] [blame] | 2200 | EXPECT_FALSE(req3.handle()->socket()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2201 | |
| 2202 | // Signal job 2, which should service request 3. |
| 2203 | |
| 2204 | client_socket_factory_.SignalJobs(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2205 | EXPECT_THAT(req3.WaitForResult(), IsOk()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2206 | |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2207 | ASSERT_EQ(3u, request_order.size()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2208 | EXPECT_EQ(&req1, request_order[0]); |
| 2209 | EXPECT_EQ(&req2, request_order[1]); |
| 2210 | EXPECT_EQ(&req3, request_order[2]); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2211 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2212 | } |
| 2213 | |
| 2214 | // The requests are not coupled to the jobs. So, the requests should finish in |
| 2215 | // their priority / insertion order. |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2216 | TEST_F(ClientSocketPoolBaseTest, PendingJobCompletionOrder) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2217 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2218 | // First two jobs are async. |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 2219 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2220 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2221 | std::vector<TestSocketRequest*> request_order; |
| 2222 | size_t completion_count; // unused |
| 2223 | TestSocketRequest req1(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2224 | int rv = req1.handle()->Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2225 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2226 | ClientSocketPool::RespectLimits::ENABLED, req1.callback(), |
| 2227 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2228 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2229 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2230 | TestSocketRequest req2(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2231 | rv = req2.handle()->Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2232 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2233 | ClientSocketPool::RespectLimits::ENABLED, req2.callback(), |
| 2234 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2235 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2236 | |
| 2237 | // The pending job is sync. |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 2238 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2239 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2240 | TestSocketRequest req3(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2241 | rv = req3.handle()->Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2242 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2243 | ClientSocketPool::RespectLimits::ENABLED, req3.callback(), |
| 2244 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2245 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2246 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2247 | EXPECT_THAT(req1.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 2248 | EXPECT_THAT(req2.WaitForResult(), IsOk()); |
| 2249 | EXPECT_THAT(req3.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2250 | |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2251 | ASSERT_EQ(3u, request_order.size()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2252 | EXPECT_EQ(&req1, request_order[0]); |
| 2253 | EXPECT_EQ(&req2, request_order[1]); |
| 2254 | EXPECT_EQ(&req3, request_order[2]); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2255 | } |
| 2256 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2257 | // Test GetLoadState in the case there's only one socket request. |
| 2258 | TEST_F(ClientSocketPoolBaseTest, LoadStateOneRequest) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2259 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2260 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2261 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2262 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2263 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2264 | int rv = 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, callback.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] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2269 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2270 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2271 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE); |
| 2272 | EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle.GetLoadState()); |
| 2273 | |
| 2274 | // No point in completing the connection, since ClientSocketHandles only |
| 2275 | // expect the LoadState to be checked while connecting. |
| 2276 | } |
| 2277 | |
| 2278 | // Test GetLoadState in the case there are two socket requests. |
| 2279 | TEST_F(ClientSocketPoolBaseTest, LoadStateTwoRequests) { |
| 2280 | CreatePool(2, 2); |
| 2281 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2282 | |
| 2283 | ClientSocketHandle handle; |
| 2284 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2285 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2286 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2287 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2288 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2289 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2290 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_RESOLVING_HOST); |
| 2291 | |
| 2292 | ClientSocketHandle handle2; |
| 2293 | TestCompletionCallback callback2; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2294 | rv = handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2295 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2296 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2297 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2298 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2299 | client_socket_factory_.SetJobLoadState(1, LOAD_STATE_RESOLVING_HOST); |
| 2300 | |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 2301 | // Each handle should reflect the state of its own job. |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2302 | EXPECT_EQ(LOAD_STATE_RESOLVING_HOST, handle.GetLoadState()); |
| 2303 | EXPECT_EQ(LOAD_STATE_RESOLVING_HOST, handle2.GetLoadState()); |
| 2304 | |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 2305 | // Update the state of the first job. |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2306 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_CONNECTING); |
| 2307 | |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 2308 | // Only the state of the first request should have changed. |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2309 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2310 | EXPECT_EQ(LOAD_STATE_RESOLVING_HOST, handle2.GetLoadState()); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 2311 | |
| 2312 | // Update the state of the second job. |
| 2313 | client_socket_factory_.SetJobLoadState(1, LOAD_STATE_SSL_HANDSHAKE); |
| 2314 | |
| 2315 | // Only the state of the second request should have changed. |
| 2316 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
| 2317 | EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle2.GetLoadState()); |
| 2318 | |
| 2319 | // Second job connects and the first request gets the socket. The |
| 2320 | // second handle switches to the state of the remaining ConnectJob. |
| 2321 | client_socket_factory_.SignalJob(1); |
| 2322 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 2323 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle2.GetLoadState()); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2324 | } |
| 2325 | |
| 2326 | // Test GetLoadState in the case the per-group limit is reached. |
| 2327 | TEST_F(ClientSocketPoolBaseTest, LoadStateGroupLimit) { |
| 2328 | CreatePool(2, 1); |
| 2329 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2330 | |
| 2331 | ClientSocketHandle handle; |
| 2332 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2333 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2334 | TestGroupId("a"), params_, base::nullopt, MEDIUM, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2335 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2336 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2337 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2338 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
| 2339 | |
| 2340 | // Request another socket from the same pool, buth with a higher priority. |
| 2341 | // The first request should now be stalled at the socket group limit. |
| 2342 | ClientSocketHandle handle2; |
| 2343 | TestCompletionCallback callback2; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2344 | rv = handle2.Init(TestGroupId("a"), params_, base::nullopt, HIGHEST, |
| 2345 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2346 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2347 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2348 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2349 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET, handle.GetLoadState()); |
| 2350 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle2.GetLoadState()); |
| 2351 | |
| 2352 | // The first handle should remain stalled as the other socket goes through |
| 2353 | // the connect process. |
| 2354 | |
| 2355 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE); |
| 2356 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET, handle.GetLoadState()); |
| 2357 | EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle2.GetLoadState()); |
| 2358 | |
| 2359 | client_socket_factory_.SignalJob(0); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2360 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2361 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET, handle.GetLoadState()); |
| 2362 | |
| 2363 | // Closing the second socket should cause the stalled handle to finally get a |
| 2364 | // ConnectJob. |
| 2365 | handle2.socket()->Disconnect(); |
| 2366 | handle2.Reset(); |
| 2367 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
| 2368 | } |
| 2369 | |
| 2370 | // Test GetLoadState in the case the per-pool limit is reached. |
| 2371 | TEST_F(ClientSocketPoolBaseTest, LoadStatePoolLimit) { |
| 2372 | CreatePool(2, 2); |
| 2373 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2374 | |
| 2375 | ClientSocketHandle handle; |
| 2376 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2377 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2378 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2379 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2380 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2381 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2382 | |
| 2383 | // Request for socket from another pool. |
| 2384 | ClientSocketHandle handle2; |
| 2385 | TestCompletionCallback callback2; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2386 | rv = handle2.Init(TestGroupId("b"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2387 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2388 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2389 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2390 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2391 | |
| 2392 | // Request another socket from the first pool. Request should stall at the |
| 2393 | // socket pool limit. |
| 2394 | ClientSocketHandle handle3; |
| 2395 | TestCompletionCallback callback3; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2396 | rv = handle3.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2397 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2398 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2399 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2400 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2401 | |
| 2402 | // The third handle should remain stalled as the other sockets in its group |
| 2403 | // goes through the connect process. |
| 2404 | |
| 2405 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
| 2406 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL, handle3.GetLoadState()); |
| 2407 | |
| 2408 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE); |
| 2409 | EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle.GetLoadState()); |
| 2410 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL, handle3.GetLoadState()); |
| 2411 | |
| 2412 | client_socket_factory_.SignalJob(0); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2413 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2414 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL, handle3.GetLoadState()); |
| 2415 | |
| 2416 | // Closing a socket should allow the stalled handle to finally get a new |
| 2417 | // ConnectJob. |
| 2418 | handle.socket()->Disconnect(); |
| 2419 | handle.Reset(); |
| 2420 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle3.GetLoadState()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2421 | } |
| 2422 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2423 | TEST_F(ClientSocketPoolBaseTest, CertError) { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2424 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2425 | connect_job_factory_->set_job_type(TestConnectJob::kMockCertErrorJob); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2426 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2427 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2428 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2429 | EXPECT_EQ( |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2430 | ERR_CERT_COMMON_NAME_INVALID, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2431 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2432 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2433 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2434 | pool_.get(), NetLogWithSource())); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2435 | EXPECT_TRUE(handle.is_initialized()); |
| 2436 | EXPECT_TRUE(handle.socket()); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2437 | } |
| 2438 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2439 | TEST_F(ClientSocketPoolBaseTest, AsyncCertError) { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2440 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2441 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2442 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingCertErrorJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2443 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2444 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2445 | EXPECT_EQ( |
| 2446 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2447 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2448 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2449 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2450 | pool_.get(), NetLogWithSource())); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2451 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2452 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2453 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_CERT_COMMON_NAME_INVALID)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2454 | EXPECT_TRUE(handle.is_initialized()); |
| 2455 | EXPECT_TRUE(handle.socket()); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2456 | } |
| 2457 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2458 | TEST_F(ClientSocketPoolBaseTest, AdditionalErrorStateSynchronous) { |
| 2459 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2460 | connect_job_factory_->set_job_type( |
| 2461 | TestConnectJob::kMockAdditionalErrorStateJob); |
| 2462 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2463 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2464 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2465 | EXPECT_EQ( |
| 2466 | ERR_CONNECTION_FAILED, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2467 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2468 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2469 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2470 | pool_.get(), NetLogWithSource())); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2471 | EXPECT_FALSE(handle.is_initialized()); |
| 2472 | EXPECT_FALSE(handle.socket()); |
| 2473 | EXPECT_TRUE(handle.is_ssl_error()); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 2474 | EXPECT_TRUE(handle.ssl_cert_request_info()); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2475 | } |
| 2476 | |
| 2477 | TEST_F(ClientSocketPoolBaseTest, AdditionalErrorStateAsynchronous) { |
| 2478 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2479 | |
| 2480 | connect_job_factory_->set_job_type( |
| 2481 | TestConnectJob::kMockPendingAdditionalErrorStateJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2482 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2483 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2484 | EXPECT_EQ( |
| 2485 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2486 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2487 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2488 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2489 | pool_.get(), NetLogWithSource())); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2490 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2491 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2492 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2493 | EXPECT_FALSE(handle.is_initialized()); |
| 2494 | EXPECT_FALSE(handle.socket()); |
| 2495 | EXPECT_TRUE(handle.is_ssl_error()); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 2496 | EXPECT_TRUE(handle.ssl_cert_request_info()); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2497 | } |
| 2498 | |
martijn | 003cd61 | 2016-05-19 22:24:38 | [diff] [blame] | 2499 | // Make sure we can reuse sockets. |
| 2500 | TEST_F(ClientSocketPoolBaseTest, CleanupTimedOutIdleSocketsReuse) { |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2501 | CreatePoolWithIdleTimeouts( |
| 2502 | kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2503 | base::TimeDelta(), // Time out unused sockets immediately. |
| 2504 | base::TimeDelta::FromDays(1)); // Don't time out used sockets. |
| 2505 | |
| 2506 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2507 | |
| 2508 | ClientSocketHandle handle; |
| 2509 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2510 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2511 | TestGroupId("a"), params_, base::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2512 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2513 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2514 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2515 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2516 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2517 | ASSERT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2518 | |
| 2519 | // Use and release the socket. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 2520 | EXPECT_EQ(1, handle.socket()->Write(nullptr, 1, CompletionOnceCallback(), |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 2521 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 2522 | TestLoadTimingInfoConnectedNotReused(handle); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2523 | handle.Reset(); |
| 2524 | |
| 2525 | // Should now have one idle socket. |
| 2526 | ASSERT_EQ(1, pool_->IdleSocketCount()); |
| 2527 | |
| 2528 | // Request a new socket. This should reuse the old socket and complete |
| 2529 | // synchronously. |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 2530 | BoundTestNetLog log; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2531 | rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2532 | TestGroupId("a"), params_, base::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2533 | ClientSocketPool::RespectLimits::ENABLED, CompletionOnceCallback(), |
| 2534 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2535 | ASSERT_THAT(rv, IsOk()); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2536 | EXPECT_TRUE(handle.is_reused()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 2537 | TestLoadTimingInfoConnectedReused(handle); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2538 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2539 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2540 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2541 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2542 | |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 2543 | auto entries = log.GetEntries(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2544 | EXPECT_TRUE(LogContainsEvent( |
| 2545 | entries, 0, NetLogEventType::TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET, |
| 2546 | NetLogEventPhase::NONE)); |
| 2547 | EXPECT_TRUE(LogContainsBeginEvent(entries, 1, NetLogEventType::SOCKET_POOL)); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2548 | EXPECT_TRUE(LogContainsEntryWithType( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2549 | entries, 2, NetLogEventType::SOCKET_POOL_REUSED_AN_EXISTING_SOCKET)); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2550 | } |
| 2551 | |
martijn | 003cd61 | 2016-05-19 22:24:38 | [diff] [blame] | 2552 | // Make sure we cleanup old unused sockets. |
Eric Roman | b49715e | 2018-04-24 22:41:17 | [diff] [blame] | 2553 | TEST_F(ClientSocketPoolBaseTest, CleanupTimedOutIdleSocketsNoReuse) { |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2554 | CreatePoolWithIdleTimeouts( |
| 2555 | kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 2556 | base::TimeDelta(), // Time out unused sockets immediately |
| 2557 | base::TimeDelta()); // Time out used sockets immediately |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2558 | |
| 2559 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2560 | |
| 2561 | // Startup two mock pending connect jobs, which will sit in the MessageLoop. |
| 2562 | |
| 2563 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2564 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2565 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2566 | TestGroupId("a"), params_, base::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2567 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2568 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2569 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2570 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2571 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2572 | |
| 2573 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2574 | TestCompletionCallback callback2; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2575 | rv = handle2.Init(TestGroupId("a"), params_, base::nullopt, LOWEST, |
| 2576 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2577 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2578 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2579 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2580 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2581 | pool_->GetLoadState(TestGroupId("a"), &handle2)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2582 | |
| 2583 | // Cancel one of the requests. Wait for the other, which will get the first |
| 2584 | // job. Release the socket. Run the loop again to make sure the second |
| 2585 | // socket is sitting idle and the first one is released (since ReleaseSocket() |
| 2586 | // just posts a DoReleaseSocket() task). |
| 2587 | |
| 2588 | handle.Reset(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2589 | ASSERT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2590 | // Use the socket. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 2591 | EXPECT_EQ(1, handle2.socket()->Write(nullptr, 1, CompletionOnceCallback(), |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 2592 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2593 | handle2.Reset(); |
| 2594 | |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2595 | // We post all of our delayed tasks with a 2ms delay. I.e. they don't |
| 2596 | // actually become pending until 2ms after they have been created. In order |
| 2597 | // to flush all tasks, we need to wait so that we know there are no |
| 2598 | // soon-to-be-pending tasks waiting. |
Alex Clarke | 0def209 | 2018-12-10 12:01:45 | [diff] [blame] | 2599 | FastForwardBy(base::TimeDelta::FromMilliseconds(10)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2600 | |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2601 | // Both sockets should now be idle. |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2602 | ASSERT_EQ(2, pool_->IdleSocketCount()); |
| 2603 | |
| 2604 | // Request a new socket. This should cleanup the unused and timed out ones. |
| 2605 | // A new socket will be created rather than reusing the idle one. |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 2606 | BoundTestNetLog log; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2607 | TestCompletionCallback callback3; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2608 | rv = handle.Init(TestGroupId("a"), params_, base::nullopt, LOWEST, |
| 2609 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2610 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2611 | pool_.get(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2612 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 2613 | ASSERT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2614 | EXPECT_FALSE(handle.is_reused()); |
| 2615 | |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2616 | // Make sure the idle socket is closed. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2617 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2618 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2619 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2620 | |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 2621 | auto entries = log.GetEntries(); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2622 | EXPECT_FALSE(LogContainsEntryWithType( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 2623 | entries, 1, NetLogEventType::SOCKET_POOL_REUSED_AN_EXISTING_SOCKET)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2624 | } |
| 2625 | |
[email protected] | 2041cf34 | 2010-02-19 03:15:59 | [diff] [blame] | 2626 | // Make sure that we process all pending requests even when we're stalling |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2627 | // because of multiple releasing disconnected sockets. |
| 2628 | TEST_F(ClientSocketPoolBaseTest, MultipleReleasingDisconnectedSockets) { |
| 2629 | CreatePoolWithIdleTimeouts( |
| 2630 | kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 2631 | base::TimeDelta(), // Time out unused sockets immediately. |
| 2632 | base::TimeDelta::FromDays(1)); // Don't time out used sockets. |
| 2633 | |
| 2634 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 2635 | |
| 2636 | // Startup 4 connect jobs. Two of them will be pending. |
| 2637 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2638 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2639 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2640 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2641 | TestGroupId("a"), params_, base::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2642 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2643 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2644 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2645 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2646 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2647 | TestCompletionCallback callback2; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2648 | rv = handle2.Init(TestGroupId("a"), params_, base::nullopt, LOWEST, |
| 2649 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2650 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2651 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2652 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2653 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2654 | ClientSocketHandle handle3; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2655 | TestCompletionCallback callback3; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2656 | rv = handle3.Init(TestGroupId("a"), params_, base::nullopt, LOWEST, |
| 2657 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2658 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2659 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2660 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2661 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2662 | ClientSocketHandle handle4; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2663 | TestCompletionCallback callback4; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2664 | rv = handle4.Init(TestGroupId("a"), params_, base::nullopt, LOWEST, |
| 2665 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2666 | callback4.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2667 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2668 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2669 | |
| 2670 | // Release two disconnected sockets. |
| 2671 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2672 | handle.socket()->Disconnect(); |
| 2673 | handle.Reset(); |
| 2674 | handle2.socket()->Disconnect(); |
| 2675 | handle2.Reset(); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2676 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2677 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2678 | EXPECT_FALSE(handle3.is_reused()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2679 | EXPECT_THAT(callback4.WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2680 | EXPECT_FALSE(handle4.is_reused()); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2681 | } |
| 2682 | |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2683 | // Regression test for http://crbug.com/42267. |
| 2684 | // When DoReleaseSocket() is processed for one socket, it is blocked because the |
| 2685 | // other stalled groups all have releasing sockets, so no progress can be made. |
| 2686 | TEST_F(ClientSocketPoolBaseTest, SocketLimitReleasingSockets) { |
| 2687 | CreatePoolWithIdleTimeouts( |
| 2688 | 4 /* socket limit */, 4 /* socket limit per group */, |
| 2689 | base::TimeDelta(), // Time out unused sockets immediately. |
| 2690 | base::TimeDelta::FromDays(1)); // Don't time out used sockets. |
| 2691 | |
| 2692 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 2693 | |
| 2694 | // Max out the socket limit with 2 per group. |
| 2695 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2696 | ClientSocketHandle handle_a[4]; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2697 | TestCompletionCallback callback_a[4]; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2698 | ClientSocketHandle handle_b[4]; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2699 | TestCompletionCallback callback_b[4]; |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2700 | |
| 2701 | for (int i = 0; i < 2; ++i) { |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2702 | EXPECT_EQ(OK, handle_a[i].Init(TestGroupId("a"), params_, base::nullopt, |
| 2703 | LOWEST, SocketTag(), |
| 2704 | ClientSocketPool::RespectLimits::ENABLED, |
| 2705 | callback_a[i].callback(), |
| 2706 | ClientSocketPool::ProxyAuthCallback(), |
| 2707 | pool_.get(), NetLogWithSource())); |
| 2708 | EXPECT_EQ(OK, handle_b[i].Init(TestGroupId("b"), params_, base::nullopt, |
| 2709 | LOWEST, SocketTag(), |
| 2710 | ClientSocketPool::RespectLimits::ENABLED, |
| 2711 | callback_b[i].callback(), |
| 2712 | ClientSocketPool::ProxyAuthCallback(), |
| 2713 | pool_.get(), NetLogWithSource())); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2714 | } |
[email protected] | b89f7e4 | 2010-05-20 20:37:00 | [diff] [blame] | 2715 | |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2716 | // Make 4 pending requests, 2 per group. |
| 2717 | |
| 2718 | for (int i = 2; i < 4; ++i) { |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2719 | EXPECT_EQ( |
| 2720 | ERR_IO_PENDING, |
| 2721 | handle_a[i].Init(TestGroupId("a"), params_, base::nullopt, LOWEST, |
| 2722 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2723 | callback_a[i].callback(), |
| 2724 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2725 | NetLogWithSource())); |
| 2726 | EXPECT_EQ( |
| 2727 | ERR_IO_PENDING, |
| 2728 | handle_b[i].Init(TestGroupId("b"), params_, base::nullopt, LOWEST, |
| 2729 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2730 | callback_b[i].callback(), |
| 2731 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2732 | NetLogWithSource())); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2733 | } |
| 2734 | |
| 2735 | // Release b's socket first. The order is important, because in |
| 2736 | // DoReleaseSocket(), we'll process b's released socket, and since both b and |
| 2737 | // a are stalled, but 'a' is lower lexicographically, we'll process group 'a' |
| 2738 | // first, which has a releasing socket, so it refuses to start up another |
| 2739 | // ConnectJob. So, we used to infinite loop on this. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2740 | handle_b[0].socket()->Disconnect(); |
| 2741 | handle_b[0].Reset(); |
| 2742 | handle_a[0].socket()->Disconnect(); |
| 2743 | handle_a[0].Reset(); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2744 | |
| 2745 | // Used to get stuck here. |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 2746 | base::RunLoop().RunUntilIdle(); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2747 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2748 | handle_b[1].socket()->Disconnect(); |
| 2749 | handle_b[1].Reset(); |
| 2750 | handle_a[1].socket()->Disconnect(); |
| 2751 | handle_a[1].Reset(); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2752 | |
| 2753 | for (int i = 2; i < 4; ++i) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2754 | EXPECT_THAT(callback_b[i].WaitForResult(), IsOk()); |
| 2755 | EXPECT_THAT(callback_a[i].WaitForResult(), IsOk()); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2756 | } |
| 2757 | } |
| 2758 | |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2759 | TEST_F(ClientSocketPoolBaseTest, |
| 2760 | ReleasingDisconnectedSocketsMaintainsPriorityOrder) { |
| 2761 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2762 | |
| 2763 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2764 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2765 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2766 | IsError(ERR_IO_PENDING)); |
| 2767 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2768 | IsError(ERR_IO_PENDING)); |
| 2769 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2770 | IsError(ERR_IO_PENDING)); |
| 2771 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2772 | IsError(ERR_IO_PENDING)); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2773 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2774 | EXPECT_THAT((*requests())[0]->WaitForResult(), IsOk()); |
| 2775 | EXPECT_THAT((*requests())[1]->WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2776 | EXPECT_EQ(2u, completion_count()); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2777 | |
| 2778 | // Releases one connection. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2779 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::NO_KEEP_ALIVE)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2780 | EXPECT_THAT((*requests())[2]->WaitForResult(), IsOk()); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2781 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2782 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::NO_KEEP_ALIVE)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2783 | EXPECT_THAT((*requests())[3]->WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2784 | EXPECT_EQ(4u, completion_count()); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2785 | |
| 2786 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 2787 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 2788 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 2789 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 2790 | |
| 2791 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2792 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(5)); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2793 | } |
| 2794 | |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2795 | class TestReleasingSocketRequest : public TestCompletionCallbackBase { |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2796 | public: |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2797 | TestReleasingSocketRequest(TransportClientSocketPool* pool, |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2798 | int expected_result, |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2799 | bool reset_releasing_handle) |
| 2800 | : pool_(pool), |
| 2801 | expected_result_(expected_result), |
Bence Béky | 8ddc249 | 2018-06-13 01:02:04 | [diff] [blame] | 2802 | reset_releasing_handle_(reset_releasing_handle) {} |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2803 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 2804 | ~TestReleasingSocketRequest() override = default; |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2805 | |
| 2806 | ClientSocketHandle* handle() { return &handle_; } |
| 2807 | |
Bence Béky | 8ddc249 | 2018-06-13 01:02:04 | [diff] [blame] | 2808 | CompletionOnceCallback callback() { |
| 2809 | return base::BindOnce(&TestReleasingSocketRequest::OnComplete, |
| 2810 | base::Unretained(this)); |
| 2811 | } |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2812 | |
| 2813 | private: |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2814 | void OnComplete(int result) { |
| 2815 | SetResult(result); |
| 2816 | if (reset_releasing_handle_) |
| 2817 | handle_.Reset(); |
| 2818 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2819 | EXPECT_EQ( |
| 2820 | expected_result_, |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 2821 | handle2_.Init( |
| 2822 | TestGroupId("a"), |
| 2823 | ClientSocketPool::SocketParams::CreateForHttpForTesting(), |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2824 | base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 2825 | ClientSocketPool::RespectLimits::ENABLED, CompletionOnceCallback(), |
| 2826 | ClientSocketPool::ProxyAuthCallback(), pool_, NetLogWithSource())); |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2827 | } |
| 2828 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2829 | TransportClientSocketPool* const pool_; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2830 | int expected_result_; |
| 2831 | bool reset_releasing_handle_; |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2832 | ClientSocketHandle handle_; |
| 2833 | ClientSocketHandle handle2_; |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2834 | }; |
| 2835 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2836 | |
| 2837 | TEST_F(ClientSocketPoolBaseTest, AdditionalErrorSocketsDontUseSlot) { |
| 2838 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2839 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2840 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
| 2841 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 2842 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2843 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2844 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2845 | client_socket_factory_.allocation_count()); |
| 2846 | |
| 2847 | connect_job_factory_->set_job_type( |
| 2848 | TestConnectJob::kMockPendingAdditionalErrorStateJob); |
| 2849 | TestReleasingSocketRequest req(pool_.get(), OK, false); |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2850 | EXPECT_EQ(ERR_IO_PENDING, |
| 2851 | req.handle()->Init( |
| 2852 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2853 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2854 | req.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2855 | pool_.get(), NetLogWithSource())); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2856 | // The next job should complete synchronously |
| 2857 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 2858 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2859 | EXPECT_THAT(req.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2860 | EXPECT_FALSE(req.handle()->is_initialized()); |
| 2861 | EXPECT_FALSE(req.handle()->socket()); |
| 2862 | EXPECT_TRUE(req.handle()->is_ssl_error()); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 2863 | EXPECT_TRUE(req.handle()->ssl_cert_request_info()); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2864 | } |
| 2865 | |
[email protected] | b6501d3d | 2010-06-03 23:53:34 | [diff] [blame] | 2866 | // http://crbug.com/44724 regression test. |
| 2867 | // We start releasing the pool when we flush on network change. When that |
| 2868 | // happens, the only active references are in the ClientSocketHandles. When a |
| 2869 | // ConnectJob completes and calls back into the last ClientSocketHandle, that |
| 2870 | // callback can release the last reference and delete the pool. After the |
| 2871 | // callback finishes, we go back to the stack frame within the now-deleted pool. |
| 2872 | // Executing any code that refers to members of the now-deleted pool can cause |
| 2873 | // crashes. |
| 2874 | TEST_F(ClientSocketPoolBaseTest, CallbackThatReleasesPool) { |
| 2875 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2876 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
| 2877 | |
| 2878 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2879 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2880 | EXPECT_EQ( |
| 2881 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2882 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2883 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2884 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2885 | pool_.get(), NetLogWithSource())); |
[email protected] | b6501d3d | 2010-06-03 23:53:34 | [diff] [blame] | 2886 | |
[email protected] | 7af985a | 2012-12-14 22:40:42 | [diff] [blame] | 2887 | pool_->FlushWithError(ERR_NETWORK_CHANGED); |
[email protected] | b6501d3d | 2010-06-03 23:53:34 | [diff] [blame] | 2888 | |
| 2889 | // We'll call back into this now. |
| 2890 | callback.WaitForResult(); |
| 2891 | } |
| 2892 | |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2893 | TEST_F(ClientSocketPoolBaseTest, DoNotReuseSocketAfterFlush) { |
| 2894 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2895 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2896 | |
| 2897 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2898 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2899 | EXPECT_EQ( |
| 2900 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2901 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2902 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2903 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2904 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2905 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2906 | EXPECT_EQ(ClientSocketHandle::UNUSED, handle.reuse_type()); |
| 2907 | |
[email protected] | 7af985a | 2012-12-14 22:40:42 | [diff] [blame] | 2908 | pool_->FlushWithError(ERR_NETWORK_CHANGED); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2909 | |
| 2910 | handle.Reset(); |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 2911 | base::RunLoop().RunUntilIdle(); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2912 | |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2913 | EXPECT_EQ( |
| 2914 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2915 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2916 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2917 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2918 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2919 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2920 | EXPECT_EQ(ClientSocketHandle::UNUSED, handle.reuse_type()); |
| 2921 | } |
| 2922 | |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2923 | class ConnectWithinCallback : public TestCompletionCallbackBase { |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2924 | public: |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2925 | ConnectWithinCallback( |
| 2926 | const ClientSocketPool::GroupId& group_id, |
Matt Menke | 84d11e56 | 2019-03-27 00:11:19 | [diff] [blame] | 2927 | const scoped_refptr<ClientSocketPool::SocketParams>& params, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2928 | TransportClientSocketPool* pool) |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2929 | : group_id_(group_id), params_(params), pool_(pool) {} |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2930 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 2931 | ~ConnectWithinCallback() override = default; |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2932 | |
| 2933 | int WaitForNestedResult() { |
| 2934 | return nested_callback_.WaitForResult(); |
| 2935 | } |
| 2936 | |
Bence Béky | 8ddc249 | 2018-06-13 01:02:04 | [diff] [blame] | 2937 | CompletionOnceCallback callback() { |
| 2938 | return base::BindOnce(&ConnectWithinCallback::OnComplete, |
| 2939 | base::Unretained(this)); |
| 2940 | } |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2941 | |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2942 | private: |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2943 | void OnComplete(int result) { |
| 2944 | SetResult(result); |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2945 | EXPECT_EQ( |
| 2946 | ERR_IO_PENDING, |
| 2947 | handle_.Init(group_id_, params_, base::nullopt, DEFAULT_PRIORITY, |
| 2948 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2949 | nested_callback_.callback(), |
| 2950 | ClientSocketPool::ProxyAuthCallback(), pool_, |
| 2951 | NetLogWithSource())); |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2952 | } |
| 2953 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2954 | const ClientSocketPool::GroupId group_id_; |
Matt Menke | 84d11e56 | 2019-03-27 00:11:19 | [diff] [blame] | 2955 | const scoped_refptr<ClientSocketPool::SocketParams> params_; |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2956 | TransportClientSocketPool* const pool_; |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2957 | ClientSocketHandle handle_; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2958 | TestCompletionCallback nested_callback_; |
| 2959 | |
| 2960 | DISALLOW_COPY_AND_ASSIGN(ConnectWithinCallback); |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2961 | }; |
| 2962 | |
| 2963 | TEST_F(ClientSocketPoolBaseTest, AbortAllRequestsOnFlush) { |
| 2964 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2965 | |
| 2966 | // First job will be waiting until it gets aborted. |
| 2967 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2968 | |
| 2969 | ClientSocketHandle handle; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2970 | ConnectWithinCallback callback(TestGroupId("a"), params_, pool_.get()); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2971 | EXPECT_EQ( |
| 2972 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2973 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2974 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2975 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2976 | pool_.get(), NetLogWithSource())); |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2977 | |
| 2978 | // Second job will be started during the first callback, and will |
| 2979 | // asynchronously complete with OK. |
| 2980 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 7af985a | 2012-12-14 22:40:42 | [diff] [blame] | 2981 | pool_->FlushWithError(ERR_NETWORK_CHANGED); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2982 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_NETWORK_CHANGED)); |
| 2983 | EXPECT_THAT(callback.WaitForNestedResult(), IsOk()); |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2984 | } |
| 2985 | |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 2986 | TEST_F(ClientSocketPoolBaseTest, BackupSocketWaitsForHostResolution) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2987 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets, |
| 2988 | true /* enable_backup_connect_jobs */); |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 2989 | |
| 2990 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2991 | ClientSocketHandle handle; |
| 2992 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2993 | EXPECT_EQ( |
| 2994 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2995 | handle.Init(TestGroupId("bar"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2996 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2997 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2998 | pool_.get(), NetLogWithSource())); |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 2999 | // The backup timer fires but doesn't start a new ConnectJob while resolving |
| 3000 | // the hostname. |
| 3001 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_RESOLVING_HOST); |
| 3002 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
| 3003 | ClientSocketPool::kMaxConnectRetryIntervalMs * 100)); |
| 3004 | EXPECT_EQ(1, client_socket_factory_.allocation_count()); |
| 3005 | |
| 3006 | // Once the ConnectJob has finished resolving the hostname, the backup timer |
| 3007 | // will create a ConnectJob when it fires. |
| 3008 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_CONNECTING); |
| 3009 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
| 3010 | ClientSocketPool::kMaxConnectRetryIntervalMs)); |
| 3011 | EXPECT_EQ(2, client_socket_factory_.allocation_count()); |
| 3012 | } |
| 3013 | |
| 3014 | // Test that no backup socket is created when a ConnectJob connects before it |
| 3015 | // completes. |
| 3016 | TEST_F(ClientSocketPoolBaseTest, NoBackupSocketWhenConnected) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3017 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets, |
| 3018 | true /* enable_backup_connect_jobs */); |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 3019 | |
| 3020 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3021 | ClientSocketHandle handle; |
| 3022 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3023 | EXPECT_EQ( |
| 3024 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3025 | handle.Init(TestGroupId("bar"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3026 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 3027 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3028 | pool_.get(), NetLogWithSource())); |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 3029 | // The backup timer fires but doesn't start a new ConnectJob while resolving |
| 3030 | // the hostname. |
| 3031 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_RESOLVING_HOST); |
| 3032 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
| 3033 | ClientSocketPool::kMaxConnectRetryIntervalMs * 100)); |
| 3034 | EXPECT_EQ(1, client_socket_factory_.allocation_count()); |
| 3035 | |
| 3036 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE); |
| 3037 | client_socket_factory_.SetJobHasEstablishedConnection(0); |
| 3038 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
| 3039 | ClientSocketPool::kMaxConnectRetryIntervalMs * 100)); |
| 3040 | EXPECT_EQ(1, client_socket_factory_.allocation_count()); |
| 3041 | } |
| 3042 | |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3043 | // Cancel a pending socket request while we're at max sockets, |
| 3044 | // and verify that the backup socket firing doesn't cause a crash. |
| 3045 | TEST_F(ClientSocketPoolBaseTest, BackupSocketCancelAtMaxSockets) { |
| 3046 | // Max 4 sockets globally, max 4 sockets per group. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3047 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets, |
| 3048 | true /* enable_backup_connect_jobs */); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3049 | |
[email protected] | 4baaf9d | 2010-08-31 15:15:44 | [diff] [blame] | 3050 | // Create the first socket and set to ERR_IO_PENDING. This starts the backup |
| 3051 | // timer. |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3052 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3053 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3054 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3055 | EXPECT_EQ( |
| 3056 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3057 | handle.Init(TestGroupId("bar"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3058 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 3059 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3060 | pool_.get(), NetLogWithSource())); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3061 | |
| 3062 | // Start (MaxSockets - 1) connected sockets to reach max sockets. |
| 3063 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 3064 | ClientSocketHandle handles[kDefaultMaxSockets]; |
| 3065 | for (int i = 1; i < kDefaultMaxSockets; ++i) { |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3066 | TestCompletionCallback callback; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3067 | EXPECT_EQ(OK, handles[i].Init(TestGroupId("bar"), params_, base::nullopt, |
| 3068 | DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3069 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3070 | callback.callback(), |
| 3071 | ClientSocketPool::ProxyAuthCallback(), |
| 3072 | pool_.get(), NetLogWithSource())); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3073 | } |
| 3074 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3075 | base::RunLoop().RunUntilIdle(); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3076 | |
| 3077 | // Cancel the pending request. |
| 3078 | handle.Reset(); |
| 3079 | |
| 3080 | // 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] | 3081 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
[email protected] | 26b997396 | 2012-01-28 00:57:00 | [diff] [blame] | 3082 | ClientSocketPool::kMaxConnectRetryIntervalMs / 2 * 3)); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3083 | |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3084 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 3085 | } |
| 3086 | |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3087 | TEST_F(ClientSocketPoolBaseTest, CancelBackupSocketAfterCancelingAllRequests) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3088 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets, |
| 3089 | true /* enable_backup_connect_jobs */); |
[email protected] | 4baaf9d | 2010-08-31 15:15:44 | [diff] [blame] | 3090 | |
| 3091 | // Create the first socket and set to ERR_IO_PENDING. This starts the backup |
| 3092 | // timer. |
| 3093 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3094 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3095 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3096 | EXPECT_EQ( |
| 3097 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3098 | handle.Init(TestGroupId("bar"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3099 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 3100 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3101 | pool_.get(), NetLogWithSource())); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3102 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("bar"))); |
| 3103 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("bar"))); |
| 3104 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3105 | TestGroupId("bar"))); |
| 3106 | EXPECT_EQ( |
| 3107 | 0u, pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("bar"))); |
[email protected] | 4baaf9d | 2010-08-31 15:15:44 | [diff] [blame] | 3108 | |
| 3109 | // Cancel the socket request. This should cancel the backup timer. Wait for |
| 3110 | // the backup time to see if it indeed got canceled. |
| 3111 | handle.Reset(); |
| 3112 | // 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] | 3113 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
[email protected] | 26b997396 | 2012-01-28 00:57:00 | [diff] [blame] | 3114 | ClientSocketPool::kMaxConnectRetryIntervalMs / 2 * 3)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3115 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("bar"))); |
| 3116 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("bar"))); |
[email protected] | 4baaf9d | 2010-08-31 15:15:44 | [diff] [blame] | 3117 | } |
| 3118 | |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3119 | TEST_F(ClientSocketPoolBaseTest, CancelBackupSocketAfterFinishingAllRequests) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3120 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets, |
| 3121 | true /* enable_backup_connect_jobs */); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3122 | |
| 3123 | // Create the first socket and set to ERR_IO_PENDING. This starts the backup |
| 3124 | // timer. |
| 3125 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3126 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3127 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3128 | EXPECT_EQ( |
| 3129 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3130 | handle.Init(TestGroupId("bar"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3131 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 3132 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3133 | pool_.get(), NetLogWithSource())); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3134 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3135 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3136 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3137 | EXPECT_EQ( |
| 3138 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3139 | handle2.Init(TestGroupId("bar"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3140 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3141 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3142 | pool_.get(), NetLogWithSource())); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3143 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("bar"))); |
| 3144 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("bar"))); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3145 | |
| 3146 | // Cancel request 1 and then complete request 2. With the requests finished, |
| 3147 | // the backup timer should be cancelled. |
| 3148 | handle.Reset(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3149 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3150 | // 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] | 3151 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
[email protected] | 26b997396 | 2012-01-28 00:57:00 | [diff] [blame] | 3152 | ClientSocketPool::kMaxConnectRetryIntervalMs / 2 * 3)); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3153 | } |
| 3154 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3155 | // Test delayed socket binding for the case where we have two connects, |
| 3156 | // and while one is waiting on a connect, the other frees up. |
| 3157 | // The socket waiting on a connect should switch immediately to the freed |
| 3158 | // up socket. |
| 3159 | TEST_F(ClientSocketPoolBaseTest, DelayedSocketBindingWaitingForConnect) { |
| 3160 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3161 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3162 | |
| 3163 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3164 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3165 | EXPECT_EQ( |
| 3166 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3167 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3168 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3169 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3170 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3171 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3172 | |
| 3173 | // No idle sockets, no pending jobs. |
| 3174 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3175 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3176 | |
| 3177 | // Create a second socket to the same host, but this one will wait. |
| 3178 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3179 | ClientSocketHandle handle2; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3180 | EXPECT_EQ( |
| 3181 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3182 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3183 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3184 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3185 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3186 | // No idle sockets, and one connecting job. |
| 3187 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3188 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3189 | |
| 3190 | // Return the first handle to the pool. This will initiate the delayed |
| 3191 | // binding. |
| 3192 | handle1.Reset(); |
| 3193 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3194 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3195 | |
| 3196 | // Still no idle sockets, still one pending connect job. |
| 3197 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3198 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3199 | |
| 3200 | // The second socket connected, even though it was a Waiting Job. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3201 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3202 | |
| 3203 | // And we can see there is still one job waiting. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3204 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3205 | |
| 3206 | // Finally, signal the waiting Connect. |
| 3207 | client_socket_factory_.SignalJobs(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3208 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3209 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3210 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3211 | } |
| 3212 | |
| 3213 | // Test delayed socket binding when a group is at capacity and one |
| 3214 | // of the group's sockets frees up. |
| 3215 | TEST_F(ClientSocketPoolBaseTest, DelayedSocketBindingAtGroupCapacity) { |
| 3216 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3217 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3218 | |
| 3219 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3220 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3221 | EXPECT_EQ( |
| 3222 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3223 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3224 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3225 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3226 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3227 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3228 | |
| 3229 | // No idle sockets, no pending jobs. |
| 3230 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3231 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3232 | |
| 3233 | // Create a second socket to the same host, but this one will wait. |
| 3234 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3235 | ClientSocketHandle handle2; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3236 | EXPECT_EQ( |
| 3237 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3238 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3239 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3240 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3241 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3242 | // No idle sockets, and one connecting job. |
| 3243 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3244 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3245 | |
| 3246 | // Return the first handle to the pool. This will initiate the delayed |
| 3247 | // binding. |
| 3248 | handle1.Reset(); |
| 3249 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3250 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3251 | |
| 3252 | // Still no idle sockets, still one pending connect job. |
| 3253 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3254 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3255 | |
| 3256 | // The second socket connected, even though it was a Waiting Job. |
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 | // And we can see there is still one job waiting. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3260 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3261 | |
| 3262 | // Finally, signal the waiting Connect. |
| 3263 | client_socket_factory_.SignalJobs(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3264 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3265 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3266 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3267 | } |
| 3268 | |
| 3269 | // Test out the case where we have one socket connected, one |
| 3270 | // connecting, when the first socket finishes and goes idle. |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3271 | // Although the second connection is pending, the second request |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3272 | // should complete, by taking the first socket's idle socket. |
| 3273 | TEST_F(ClientSocketPoolBaseTest, DelayedSocketBindingAtStall) { |
| 3274 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3275 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3276 | |
| 3277 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3278 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3279 | EXPECT_EQ( |
| 3280 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3281 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3282 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3283 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3284 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3285 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3286 | |
| 3287 | // No idle sockets, no pending jobs. |
| 3288 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3289 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3290 | |
| 3291 | // Create a second socket to the same host, but this one will wait. |
| 3292 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3293 | ClientSocketHandle handle2; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3294 | EXPECT_EQ( |
| 3295 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3296 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3297 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3298 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3299 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3300 | // No idle sockets, and one connecting job. |
| 3301 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3302 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3303 | |
| 3304 | // Return the first handle to the pool. This will initiate the delayed |
| 3305 | // binding. |
| 3306 | handle1.Reset(); |
| 3307 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3308 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3309 | |
| 3310 | // Still no idle sockets, still one pending connect job. |
| 3311 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3312 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3313 | |
| 3314 | // The second socket connected, even though it was a Waiting Job. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3315 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3316 | |
| 3317 | // And we can see there is still one job waiting. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3318 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3319 | |
| 3320 | // Finally, signal the waiting Connect. |
| 3321 | client_socket_factory_.SignalJobs(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3322 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3323 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3324 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3325 | } |
| 3326 | |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3327 | // Cover the case where on an available socket slot, we have one pending |
| 3328 | // request that completes synchronously, thereby making the Group empty. |
| 3329 | TEST_F(ClientSocketPoolBaseTest, SynchronouslyProcessOnePendingRequest) { |
| 3330 | const int kUnlimitedSockets = 100; |
| 3331 | const int kOneSocketPerGroup = 1; |
| 3332 | CreatePool(kUnlimitedSockets, kOneSocketPerGroup); |
| 3333 | |
| 3334 | // Make the first request asynchronous fail. |
| 3335 | // This will free up a socket slot later. |
| 3336 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
| 3337 | |
| 3338 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3339 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3340 | EXPECT_EQ( |
| 3341 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3342 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3343 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3344 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3345 | pool_.get(), NetLogWithSource())); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3346 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3347 | |
| 3348 | // Make the second request synchronously fail. This should make the Group |
| 3349 | // empty. |
| 3350 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
| 3351 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3352 | TestCompletionCallback callback2; |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3353 | // It'll be ERR_IO_PENDING now, but the TestConnectJob will synchronously fail |
| 3354 | // when created. |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3355 | EXPECT_EQ( |
| 3356 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3357 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3358 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3359 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3360 | pool_.get(), NetLogWithSource())); |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3361 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3362 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3363 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3364 | EXPECT_THAT(callback1.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 3365 | EXPECT_THAT(callback2.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3366 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3367 | } |
| 3368 | |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3369 | TEST_F(ClientSocketPoolBaseTest, PreferUsedSocketToUnusedSocket) { |
| 3370 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 3371 | |
| 3372 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3373 | |
| 3374 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3375 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3376 | EXPECT_EQ( |
| 3377 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3378 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3379 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3380 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3381 | pool_.get(), NetLogWithSource())); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3382 | |
| 3383 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3384 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3385 | EXPECT_EQ( |
| 3386 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3387 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3388 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3389 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3390 | pool_.get(), NetLogWithSource())); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3391 | ClientSocketHandle handle3; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3392 | TestCompletionCallback callback3; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3393 | EXPECT_EQ( |
| 3394 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3395 | handle3.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3396 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3397 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3398 | pool_.get(), NetLogWithSource())); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3399 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3400 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3401 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
| 3402 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3403 | |
| 3404 | // Use the socket. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 3405 | EXPECT_EQ(1, handle1.socket()->Write(nullptr, 1, CompletionOnceCallback(), |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 3406 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 3407 | EXPECT_EQ(1, handle3.socket()->Write(nullptr, 1, CompletionOnceCallback(), |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 3408 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3409 | |
| 3410 | handle1.Reset(); |
| 3411 | handle2.Reset(); |
| 3412 | handle3.Reset(); |
| 3413 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3414 | EXPECT_EQ(OK, handle1.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3415 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3416 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3417 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3418 | pool_.get(), NetLogWithSource())); |
| 3419 | EXPECT_EQ(OK, handle2.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3420 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3421 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3422 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3423 | pool_.get(), NetLogWithSource())); |
| 3424 | EXPECT_EQ(OK, handle3.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3425 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3426 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3427 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3428 | pool_.get(), NetLogWithSource())); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3429 | |
| 3430 | EXPECT_TRUE(handle1.socket()->WasEverUsed()); |
| 3431 | EXPECT_TRUE(handle2.socket()->WasEverUsed()); |
| 3432 | EXPECT_FALSE(handle3.socket()->WasEverUsed()); |
| 3433 | } |
| 3434 | |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3435 | TEST_F(ClientSocketPoolBaseTest, RequestSockets) { |
| 3436 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3437 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3438 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3439 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 3440 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3441 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3442 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3443 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3444 | EXPECT_EQ(2u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3445 | TestGroupId("a"))); |
| 3446 | EXPECT_EQ(2u, |
| 3447 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3448 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3449 | |
| 3450 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3451 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3452 | EXPECT_EQ( |
| 3453 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3454 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3455 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3456 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3457 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3458 | |
| 3459 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3460 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3461 | EXPECT_EQ( |
| 3462 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3463 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3464 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3465 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3466 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3467 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3468 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3469 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3470 | TestGroupId("a"))); |
| 3471 | EXPECT_EQ(0u, |
| 3472 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3473 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3474 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3475 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3476 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3477 | handle1.Reset(); |
| 3478 | handle2.Reset(); |
| 3479 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3480 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3481 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3482 | TestGroupId("a"))); |
| 3483 | EXPECT_EQ(0u, |
| 3484 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3485 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3486 | } |
| 3487 | |
| 3488 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsWhenAlreadyHaveAConnectJob) { |
| 3489 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3490 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3491 | |
| 3492 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3493 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3494 | EXPECT_EQ( |
| 3495 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3496 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3497 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3498 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3499 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3500 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3501 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3502 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3503 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3504 | TestGroupId("a"))); |
| 3505 | EXPECT_EQ(0u, |
| 3506 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3507 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3508 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3509 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 3510 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3511 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3512 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3513 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3514 | TestGroupId("a"))); |
| 3515 | EXPECT_EQ(1u, |
| 3516 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3517 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3518 | |
| 3519 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3520 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3521 | EXPECT_EQ( |
| 3522 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3523 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3524 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3525 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3526 | pool_.get(), 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 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3529 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3530 | TestGroupId("a"))); |
| 3531 | EXPECT_EQ(0u, |
| 3532 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3533 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3534 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3535 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3536 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3537 | handle1.Reset(); |
| 3538 | handle2.Reset(); |
| 3539 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3540 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3541 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3542 | TestGroupId("a"))); |
| 3543 | EXPECT_EQ(0u, |
| 3544 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3545 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3546 | } |
| 3547 | |
| 3548 | TEST_F(ClientSocketPoolBaseTest, |
| 3549 | RequestSocketsWhenAlreadyHaveMultipleConnectJob) { |
| 3550 | CreatePool(4, 4); |
| 3551 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3552 | |
| 3553 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3554 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3555 | EXPECT_EQ( |
| 3556 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3557 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3558 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3559 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3560 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3561 | |
| 3562 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3563 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3564 | EXPECT_EQ( |
| 3565 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3566 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3567 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3568 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3569 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3570 | |
| 3571 | ClientSocketHandle handle3; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3572 | TestCompletionCallback callback3; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3573 | EXPECT_EQ( |
| 3574 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3575 | handle3.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3576 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3577 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3578 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3579 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3580 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3581 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3582 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3583 | TestGroupId("a"))); |
| 3584 | EXPECT_EQ(0u, |
| 3585 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3586 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3587 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3588 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 3589 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3590 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3591 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3592 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3593 | TestGroupId("a"))); |
| 3594 | EXPECT_EQ(0u, |
| 3595 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3596 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3597 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3598 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3599 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
| 3600 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3601 | handle1.Reset(); |
| 3602 | handle2.Reset(); |
| 3603 | handle3.Reset(); |
| 3604 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3605 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3606 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3607 | TestGroupId("a"))); |
| 3608 | EXPECT_EQ(0u, |
| 3609 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3610 | EXPECT_EQ(3u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3611 | } |
| 3612 | |
| 3613 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsAtMaxSocketLimit) { |
| 3614 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 3615 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3616 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3617 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3618 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3619 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, |
| 3620 | kDefaultMaxSockets, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3621 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3622 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3623 | EXPECT_EQ(kDefaultMaxSockets, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3624 | static_cast<int>( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3625 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
| 3626 | EXPECT_EQ( |
| 3627 | kDefaultMaxSockets, |
| 3628 | static_cast<int>(pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3629 | TestGroupId("a")))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3630 | EXPECT_EQ(kDefaultMaxSockets, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3631 | static_cast<int>(pool_->NumUnassignedConnectJobsInGroupForTesting( |
| 3632 | TestGroupId("a")))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3633 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3634 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("b"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3635 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3636 | pool_->RequestSockets(TestGroupId("b"), params_, base::nullopt, |
| 3637 | kDefaultMaxSockets, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3638 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3639 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("b"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3640 | } |
| 3641 | |
| 3642 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsHitMaxSocketLimit) { |
| 3643 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 3644 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3645 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3646 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3647 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3648 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, |
| 3649 | kDefaultMaxSockets - 1, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3650 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3651 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3652 | EXPECT_EQ(kDefaultMaxSockets - 1, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3653 | static_cast<int>( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3654 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
| 3655 | EXPECT_EQ( |
| 3656 | kDefaultMaxSockets - 1, |
| 3657 | static_cast<int>(pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3658 | TestGroupId("a")))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3659 | EXPECT_EQ(kDefaultMaxSockets - 1, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3660 | static_cast<int>(pool_->NumUnassignedConnectJobsInGroupForTesting( |
| 3661 | TestGroupId("a")))); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 3662 | EXPECT_FALSE(pool_->IsStalled()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3663 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3664 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("b"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3665 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3666 | pool_->RequestSockets(TestGroupId("b"), params_, base::nullopt, |
| 3667 | kDefaultMaxSockets, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3668 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3669 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("b"))); |
| 3670 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 3671 | EXPECT_FALSE(pool_->IsStalled()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3672 | } |
| 3673 | |
| 3674 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsCountIdleSockets) { |
| 3675 | CreatePool(4, 4); |
| 3676 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3677 | |
| 3678 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3679 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3680 | EXPECT_EQ( |
| 3681 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3682 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3683 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3684 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3685 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3686 | ASSERT_THAT(callback1.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3687 | handle1.Reset(); |
| 3688 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3689 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3690 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3691 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3692 | TestGroupId("a"))); |
| 3693 | EXPECT_EQ(0u, |
| 3694 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3695 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3696 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3697 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 3698 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3699 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3700 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3701 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3702 | TestGroupId("a"))); |
| 3703 | EXPECT_EQ(1u, |
| 3704 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3705 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3706 | } |
| 3707 | |
| 3708 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsCountActiveSockets) { |
| 3709 | CreatePool(4, 4); |
| 3710 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3711 | |
| 3712 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3713 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3714 | EXPECT_EQ( |
| 3715 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3716 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3717 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3718 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3719 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3720 | ASSERT_THAT(callback1.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3721 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3722 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3723 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3724 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3725 | TestGroupId("a"))); |
| 3726 | EXPECT_EQ(0u, |
| 3727 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3728 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3729 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3730 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3731 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 3732 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3733 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3734 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3735 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3736 | TestGroupId("a"))); |
| 3737 | EXPECT_EQ(1u, |
| 3738 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3739 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3740 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3741 | } |
| 3742 | |
| 3743 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsSynchronous) { |
| 3744 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3745 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 3746 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3747 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, |
| 3748 | kDefaultMaxSocketsPerGroup, NetLogWithSource()); |
[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_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3751 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3752 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3753 | TestGroupId("a"))); |
| 3754 | EXPECT_EQ(0u, |
| 3755 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3756 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3757 | static_cast<int>(pool_->IdleSocketCountInGroup(TestGroupId("a")))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3758 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3759 | pool_->RequestSockets(TestGroupId("b"), params_, base::nullopt, |
| 3760 | kDefaultMaxSocketsPerGroup, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3761 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3762 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 3763 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3764 | TestGroupId("b"))); |
| 3765 | EXPECT_EQ(0u, |
| 3766 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3767 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3768 | static_cast<int>(pool_->IdleSocketCountInGroup(TestGroupId("b")))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3769 | } |
| 3770 | |
[email protected] | 3c819f52 | 2010-12-02 02:03:12 | [diff] [blame] | 3771 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsSynchronousError) { |
| 3772 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3773 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
| 3774 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3775 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, |
| 3776 | kDefaultMaxSocketsPerGroup, NetLogWithSource()); |
[email protected] | 3c819f52 | 2010-12-02 02:03:12 | [diff] [blame] | 3777 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3778 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | fd2e53e | 2011-01-14 20:40:52 | [diff] [blame] | 3779 | |
| 3780 | connect_job_factory_->set_job_type( |
| 3781 | TestConnectJob::kMockAdditionalErrorStateJob); |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3782 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, |
| 3783 | kDefaultMaxSocketsPerGroup, NetLogWithSource()); |
[email protected] | fd2e53e | 2011-01-14 20:40:52 | [diff] [blame] | 3784 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3785 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | 3c819f52 | 2010-12-02 02:03:12 | [diff] [blame] | 3786 | } |
| 3787 | |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3788 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsMultipleTimesDoesNothing) { |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3789 | CreatePool(4, 4); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3790 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3791 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3792 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 3793 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3794 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3795 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3796 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3797 | EXPECT_EQ(2u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3798 | TestGroupId("a"))); |
| 3799 | EXPECT_EQ(2u, |
| 3800 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3801 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3802 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3803 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3804 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 3805 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3806 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3807 | EXPECT_EQ(2u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3808 | TestGroupId("a"))); |
| 3809 | EXPECT_EQ(2u, |
| 3810 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3811 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3812 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3813 | |
| 3814 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3815 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3816 | EXPECT_EQ( |
| 3817 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3818 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3819 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3820 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3821 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3822 | |
| 3823 | client_socket_factory_.SignalJob(0); |
| 3824 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3825 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3826 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3827 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3828 | TestGroupId("a"))); |
| 3829 | EXPECT_EQ(1u, |
| 3830 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3831 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3832 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3833 | |
| 3834 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3835 | TestCompletionCallback callback2; |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3836 | EXPECT_EQ( |
| 3837 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3838 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3839 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3840 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3841 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3842 | client_socket_factory_.SignalJob(0); |
| 3843 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3844 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3845 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3846 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3847 | TestGroupId("a"))); |
| 3848 | EXPECT_EQ(0u, |
| 3849 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3850 | EXPECT_EQ(2, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3851 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3852 | |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3853 | handle1.Reset(); |
| 3854 | handle2.Reset(); |
| 3855 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3856 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3857 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3858 | TestGroupId("a"))); |
| 3859 | EXPECT_EQ(0u, |
| 3860 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3861 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3862 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3863 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3864 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 3865 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3866 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3867 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3868 | TestGroupId("a"))); |
| 3869 | EXPECT_EQ(0u, |
| 3870 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3871 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3872 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3873 | } |
| 3874 | |
| 3875 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsDifferentNumSockets) { |
| 3876 | CreatePool(4, 4); |
| 3877 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3878 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3879 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 1, |
| 3880 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3881 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3882 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3883 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3884 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3885 | TestGroupId("a"))); |
| 3886 | EXPECT_EQ(1u, |
| 3887 | pool_->NumUnassignedConnectJobsInGroupForTesting(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"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3897 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3898 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3899 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 3, |
| 3900 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3901 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3902 | EXPECT_EQ(3u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3903 | TestGroupId("a"))); |
| 3904 | EXPECT_EQ(3u, |
| 3905 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3906 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3907 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3908 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 1, |
| 3909 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3910 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3911 | EXPECT_EQ(3u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3912 | TestGroupId("a"))); |
| 3913 | EXPECT_EQ(3u, |
| 3914 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3915 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3916 | } |
| 3917 | |
| 3918 | TEST_F(ClientSocketPoolBaseTest, PreconnectJobsTakenByNormalRequests) { |
| 3919 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3920 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3921 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3922 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 1, |
| 3923 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3924 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3925 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3926 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3927 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3928 | TestGroupId("a"))); |
| 3929 | EXPECT_EQ(1u, |
| 3930 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3931 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3932 | |
| 3933 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3934 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3935 | EXPECT_EQ( |
| 3936 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3937 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3938 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3939 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3940 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3941 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3942 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3943 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3944 | TestGroupId("a"))); |
| 3945 | EXPECT_EQ(0u, |
| 3946 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3947 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3948 | |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3949 | client_socket_factory_.SignalJobs(); |
| 3950 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3951 | |
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"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3957 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3958 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3959 | |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 3960 | // Make sure if a preconnected socket is not fully connected when a request |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 3961 | // starts, it has a connect start time. |
| 3962 | TestLoadTimingInfoConnectedNotReused(handle1); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3963 | handle1.Reset(); |
| 3964 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3965 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3966 | } |
| 3967 | |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 3968 | // Checks that fully connected preconnect jobs have no connect times, and are |
| 3969 | // marked as reused. |
| 3970 | TEST_F(ClientSocketPoolBaseTest, ConnectedPreconnectJobsHaveNoConnectTimes) { |
| 3971 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3972 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3973 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 1, |
| 3974 | NetLogWithSource()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 3975 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3976 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3977 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3978 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3979 | TestGroupId("a"))); |
| 3980 | EXPECT_EQ(0u, |
| 3981 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3982 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 3983 | |
| 3984 | ClientSocketHandle handle; |
| 3985 | TestCompletionCallback callback; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3986 | EXPECT_EQ(OK, handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3987 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3988 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3989 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3990 | pool_.get(), NetLogWithSource())); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 3991 | |
| 3992 | // Make sure the idle socket was used. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3993 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 3994 | |
| 3995 | TestLoadTimingInfoConnectedReused(handle); |
| 3996 | handle.Reset(); |
| 3997 | TestLoadTimingInfoNotConnected(handle); |
| 3998 | } |
| 3999 | |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4000 | // http://crbug.com/64940 regression test. |
| 4001 | TEST_F(ClientSocketPoolBaseTest, PreconnectClosesIdleSocketRemovesGroup) { |
| 4002 | const int kMaxTotalSockets = 3; |
| 4003 | const int kMaxSocketsPerGroup = 2; |
| 4004 | CreatePool(kMaxTotalSockets, kMaxSocketsPerGroup); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4005 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4006 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 4007 | // Note that group id ordering matters here. "a" comes before "b", so |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4008 | // CloseOneIdleSocket() will try to close "a"'s idle socket. |
| 4009 | |
| 4010 | // Set up one idle socket in "a". |
| 4011 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 4012 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4013 | EXPECT_EQ( |
| 4014 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4015 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4016 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4017 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4018 | pool_.get(), NetLogWithSource())); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4019 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 4020 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4021 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4022 | TestGroupId("a"))); |
| 4023 | EXPECT_EQ(0u, |
| 4024 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4025 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4026 | |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4027 | client_socket_factory_.SignalJobs(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4028 | ASSERT_THAT(callback1.WaitForResult(), IsOk()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4029 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4030 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4031 | TestGroupId("a"))); |
| 4032 | EXPECT_EQ(0u, |
| 4033 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4034 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4035 | |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4036 | handle1.Reset(); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4037 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4038 | |
| 4039 | // Set up two active sockets in "b". |
| 4040 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 4041 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4042 | EXPECT_EQ( |
| 4043 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4044 | handle1.Init(TestGroupId("b"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4045 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4046 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4047 | pool_.get(), NetLogWithSource())); |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4048 | EXPECT_EQ( |
| 4049 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4050 | handle2.Init(TestGroupId("b"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4051 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4052 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4053 | pool_.get(), NetLogWithSource())); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4054 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4055 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("b"))); |
| 4056 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 4057 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4058 | TestGroupId("b"))); |
| 4059 | EXPECT_EQ(0u, |
| 4060 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4061 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4062 | |
| 4063 | client_socket_factory_.SignalJobs(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4064 | ASSERT_THAT(callback1.WaitForResult(), IsOk()); |
| 4065 | ASSERT_THAT(callback2.WaitForResult(), IsOk()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4066 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4067 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4068 | TestGroupId("b"))); |
| 4069 | EXPECT_EQ(0u, |
| 4070 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 4071 | EXPECT_EQ(2, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4072 | |
| 4073 | // Now we have 1 idle socket in "a" and 2 active sockets in "b". This means |
| 4074 | // we've maxed out on sockets, since we set |kMaxTotalSockets| to 3. |
| 4075 | // Requesting 2 preconnected sockets for "a" should fail to allocate any more |
| 4076 | // sockets for "a", and "b" should still have 2 active sockets. |
| 4077 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4078 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 4079 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4080 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4081 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4082 | TestGroupId("a"))); |
| 4083 | EXPECT_EQ(0u, |
| 4084 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4085 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4086 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 4087 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 4088 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4089 | TestGroupId("b"))); |
| 4090 | EXPECT_EQ(0u, |
| 4091 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4092 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4093 | EXPECT_EQ(2, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4094 | |
| 4095 | // Now release the 2 active sockets for "b". This will give us 1 idle socket |
| 4096 | // in "a" and 2 idle sockets in "b". Requesting 2 preconnected sockets for |
| 4097 | // "a" should result in closing 1 for "b". |
| 4098 | handle1.Reset(); |
| 4099 | handle2.Reset(); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4100 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4101 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4102 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4103 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 4104 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4105 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4106 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4107 | TestGroupId("a"))); |
| 4108 | EXPECT_EQ(1u, |
| 4109 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4110 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4111 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 4112 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 4113 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4114 | TestGroupId("b"))); |
| 4115 | EXPECT_EQ(0u, |
| 4116 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4117 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4118 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4119 | } |
| 4120 | |
[email protected] | b7b8be4 | 2011-07-12 12:46:41 | [diff] [blame] | 4121 | TEST_F(ClientSocketPoolBaseTest, PreconnectWithoutBackupJob) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4122 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 4123 | true /* enable_backup_connect_jobs */); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4124 | |
| 4125 | // Make the ConnectJob hang until it times out, shorten the timeout. |
| 4126 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4127 | connect_job_factory_->set_timeout_duration( |
| 4128 | base::TimeDelta::FromMilliseconds(500)); |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4129 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 1, |
| 4130 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4131 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4132 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4133 | TestGroupId("a"))); |
| 4134 | EXPECT_EQ(1u, |
| 4135 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4136 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4137 | |
[email protected] | b7b8be4 | 2011-07-12 12:46:41 | [diff] [blame] | 4138 | // Verify the backup timer doesn't create a backup job, by making |
| 4139 | // the backup job a pending job instead of a waiting job, so it |
| 4140 | // *would* complete if it were created. |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4141 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 4142 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
Gabriel Charette | ea91801 | 2018-05-16 11:53:44 | [diff] [blame] | 4143 | FROM_HERE, base::RunLoop::QuitCurrentWhenIdleClosureDeprecated(), |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 4144 | base::TimeDelta::FromSeconds(1)); |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 4145 | base::RunLoop().Run(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4146 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4147 | } |
| 4148 | |
[email protected] | b7b8be4 | 2011-07-12 12:46:41 | [diff] [blame] | 4149 | TEST_F(ClientSocketPoolBaseTest, PreconnectWithBackupJob) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4150 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 4151 | true /* enable_backup_connect_jobs */); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4152 | |
| 4153 | // Make the ConnectJob hang forever. |
| 4154 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4155 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 1, |
| 4156 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4157 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4158 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4159 | TestGroupId("a"))); |
| 4160 | EXPECT_EQ(1u, |
| 4161 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4162 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 4163 | base::RunLoop().RunUntilIdle(); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4164 | |
| 4165 | // Make the backup job be a pending job, so it completes normally. |
| 4166 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 4167 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 4168 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4169 | EXPECT_EQ( |
| 4170 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4171 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4172 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4173 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4174 | pool_.get(), NetLogWithSource())); |
[email protected] | b7b8be4 | 2011-07-12 12:46:41 | [diff] [blame] | 4175 | // Timer has started, but the backup connect job shouldn't be created yet. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4176 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4177 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4178 | TestGroupId("a"))); |
| 4179 | EXPECT_EQ(0u, |
| 4180 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4181 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4182 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4183 | ASSERT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4184 | |
| 4185 | // The hung connect job should still be there, but everything else should be |
| 4186 | // complete. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4187 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4188 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4189 | TestGroupId("a"))); |
| 4190 | EXPECT_EQ(1u, |
| 4191 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4192 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4193 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4194 | } |
| 4195 | |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4196 | // Tests that a preconnect that starts out with unread data can still be used. |
| 4197 | // http://crbug.com/334467 |
| 4198 | TEST_F(ClientSocketPoolBaseTest, PreconnectWithUnreadData) { |
| 4199 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4200 | connect_job_factory_->set_job_type(TestConnectJob::kMockUnreadDataJob); |
| 4201 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4202 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 1, |
| 4203 | NetLogWithSource()); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4204 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4205 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 4206 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4207 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4208 | TestGroupId("a"))); |
| 4209 | EXPECT_EQ(0u, |
| 4210 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4211 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4212 | |
| 4213 | // Fail future jobs to be sure that handle receives the preconnected socket |
| 4214 | // rather than closing it and making a new one. |
| 4215 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
| 4216 | ClientSocketHandle handle; |
| 4217 | TestCompletionCallback callback; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4218 | EXPECT_EQ(OK, handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4219 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4220 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4221 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4222 | pool_.get(), NetLogWithSource())); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4223 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4224 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 4225 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4226 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4227 | TestGroupId("a"))); |
| 4228 | EXPECT_EQ(0u, |
| 4229 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4230 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4231 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4232 | |
| 4233 | // Drain the pending read. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 4234 | EXPECT_EQ(1, handle.socket()->Read(nullptr, 1, CompletionOnceCallback())); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4235 | |
| 4236 | TestLoadTimingInfoConnectedReused(handle); |
| 4237 | handle.Reset(); |
| 4238 | |
| 4239 | // The socket should be usable now that it's idle again. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4240 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4241 | } |
| 4242 | |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4243 | TEST_F(ClientSocketPoolBaseTest, RequestGetsAssignedJob) { |
| 4244 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4245 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4246 | |
| 4247 | ClientSocketHandle handle1; |
| 4248 | TestCompletionCallback callback1; |
| 4249 | EXPECT_EQ( |
| 4250 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4251 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4252 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4253 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4254 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4255 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4256 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4257 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4258 | TestGroupId("a"))); |
| 4259 | EXPECT_EQ(0u, |
| 4260 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4261 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4262 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4263 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4264 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4265 | } |
| 4266 | |
| 4267 | TEST_F(ClientSocketPoolBaseTest, MultipleRequestsGetAssignedJobs) { |
| 4268 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4269 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4270 | |
| 4271 | ClientSocketHandle handle1; |
| 4272 | TestCompletionCallback callback1; |
| 4273 | EXPECT_EQ( |
| 4274 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4275 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4276 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4277 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4278 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4279 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4280 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4281 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4282 | TestGroupId("a"))); |
| 4283 | EXPECT_EQ(0u, |
| 4284 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4285 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4286 | |
| 4287 | ClientSocketHandle handle2; |
| 4288 | TestCompletionCallback callback2; |
| 4289 | EXPECT_EQ( |
| 4290 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4291 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4292 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4293 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4294 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4295 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4296 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4297 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4298 | TestGroupId("a"))); |
| 4299 | EXPECT_EQ(0u, |
| 4300 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4301 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4302 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4303 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4304 | &handle1)); |
| 4305 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4306 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4307 | |
| 4308 | // One job completes. The other request should still have its job. |
| 4309 | client_socket_factory_.SignalJob(0); |
| 4310 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 4311 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4312 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4313 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4314 | TestGroupId("a"))); |
| 4315 | EXPECT_EQ(0u, |
| 4316 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4317 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4318 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4319 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4320 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4321 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4322 | } |
| 4323 | |
| 4324 | TEST_F(ClientSocketPoolBaseTest, PreconnectJobGetsAssignedToRequest) { |
| 4325 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4326 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4327 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4328 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 1, |
| 4329 | NetLogWithSource()); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4330 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4331 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 4332 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4333 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4334 | TestGroupId("a"))); |
| 4335 | EXPECT_EQ(1u, |
| 4336 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4337 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4338 | |
| 4339 | ClientSocketHandle handle1; |
| 4340 | TestCompletionCallback callback1; |
| 4341 | EXPECT_EQ( |
| 4342 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4343 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4344 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4345 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4346 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4347 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4348 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4349 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4350 | TestGroupId("a"))); |
| 4351 | EXPECT_EQ(0u, |
| 4352 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4353 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4354 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4355 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4356 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4357 | } |
| 4358 | |
| 4359 | TEST_F(ClientSocketPoolBaseTest, HigherPriorityRequestStealsJob) { |
| 4360 | CreatePool(kDefaultMaxSockets, 1); |
| 4361 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4362 | |
| 4363 | ClientSocketHandle handle1; |
| 4364 | TestCompletionCallback callback1; |
| 4365 | EXPECT_EQ( |
| 4366 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4367 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4368 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4369 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4370 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4371 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4372 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4373 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4374 | TestGroupId("a"))); |
| 4375 | EXPECT_EQ(0u, |
| 4376 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4377 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4378 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4379 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4380 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4381 | |
| 4382 | // Insert a higher priority request |
| 4383 | ClientSocketHandle handle2; |
| 4384 | TestCompletionCallback callback2; |
| 4385 | EXPECT_EQ( |
| 4386 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4387 | handle2.Init(TestGroupId("a"), params_, base::nullopt, HIGHEST, |
| 4388 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4389 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4390 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4391 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4392 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4393 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4394 | TestGroupId("a"))); |
| 4395 | EXPECT_EQ(0u, |
| 4396 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4397 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4398 | |
| 4399 | // The highest priority request should steal the job from the default priority |
| 4400 | // request. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4401 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4402 | &handle2)); |
| 4403 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4404 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4405 | } |
| 4406 | |
| 4407 | TEST_F(ClientSocketPoolBaseTest, RequestStealsJobFromLowestRequestWithJob) { |
| 4408 | CreatePool(3, 3); |
| 4409 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4410 | |
| 4411 | ClientSocketHandle handle_lowest; |
| 4412 | TestCompletionCallback callback_lowest; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4413 | EXPECT_EQ( |
| 4414 | ERR_IO_PENDING, |
| 4415 | handle_lowest.Init(TestGroupId("a"), params_, base::nullopt, LOWEST, |
| 4416 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4417 | callback_lowest.callback(), |
| 4418 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4419 | NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4420 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4421 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4422 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4423 | TestGroupId("a"))); |
| 4424 | EXPECT_EQ(0u, |
| 4425 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4426 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4427 | |
| 4428 | ClientSocketHandle handle_highest; |
| 4429 | TestCompletionCallback callback_highest; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4430 | EXPECT_EQ( |
| 4431 | ERR_IO_PENDING, |
| 4432 | handle_highest.Init(TestGroupId("a"), params_, base::nullopt, HIGHEST, |
| 4433 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4434 | callback_highest.callback(), |
| 4435 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4436 | NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4437 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4438 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4439 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4440 | TestGroupId("a"))); |
| 4441 | EXPECT_EQ(0u, |
| 4442 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4443 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4444 | |
| 4445 | ClientSocketHandle handle_low; |
| 4446 | TestCompletionCallback callback_low; |
| 4447 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4448 | handle_low.Init( |
| 4449 | TestGroupId("a"), params_, base::nullopt, LOW, SocketTag(), |
| 4450 | ClientSocketPool::RespectLimits::ENABLED, |
| 4451 | callback_low.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4452 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4453 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4454 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4455 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4456 | TestGroupId("a"))); |
| 4457 | EXPECT_EQ(0u, |
| 4458 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4459 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4460 | |
| 4461 | ClientSocketHandle handle_lowest2; |
| 4462 | TestCompletionCallback callback_lowest2; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4463 | EXPECT_EQ( |
| 4464 | ERR_IO_PENDING, |
| 4465 | handle_lowest2.Init(TestGroupId("a"), params_, base::nullopt, LOWEST, |
| 4466 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4467 | callback_lowest2.callback(), |
| 4468 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4469 | NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4470 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4471 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4472 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4473 | TestGroupId("a"))); |
| 4474 | EXPECT_EQ(0u, |
| 4475 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4476 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4477 | |
| 4478 | // The top three requests in the queue should have jobs. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4479 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4480 | &handle_highest)); |
| 4481 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4482 | &handle_low)); |
| 4483 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4484 | &handle_lowest)); |
| 4485 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting( |
| 4486 | TestGroupId("a"), &handle_lowest2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4487 | |
| 4488 | // Add another request with medium priority. It should steal the job from the |
| 4489 | // lowest priority request with a job. |
| 4490 | ClientSocketHandle handle_medium; |
| 4491 | TestCompletionCallback callback_medium; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4492 | EXPECT_EQ( |
| 4493 | ERR_IO_PENDING, |
| 4494 | handle_medium.Init(TestGroupId("a"), params_, base::nullopt, MEDIUM, |
| 4495 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4496 | callback_medium.callback(), |
| 4497 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4498 | NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4499 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4500 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4501 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4502 | TestGroupId("a"))); |
| 4503 | EXPECT_EQ(0u, |
| 4504 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4505 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
| 4506 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4507 | &handle_highest)); |
| 4508 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4509 | &handle_medium)); |
| 4510 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4511 | &handle_low)); |
| 4512 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4513 | &handle_lowest)); |
| 4514 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting( |
| 4515 | TestGroupId("a"), &handle_lowest2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4516 | } |
| 4517 | |
| 4518 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeRequestStealsJob) { |
| 4519 | CreatePool(kDefaultMaxSockets, 1); |
| 4520 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4521 | |
| 4522 | ClientSocketHandle handle1; |
| 4523 | TestCompletionCallback callback1; |
| 4524 | EXPECT_EQ( |
| 4525 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4526 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4527 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4528 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4529 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4530 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4531 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4532 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4533 | TestGroupId("a"))); |
| 4534 | EXPECT_EQ(0u, |
| 4535 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4536 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4537 | |
| 4538 | ClientSocketHandle handle2; |
| 4539 | TestCompletionCallback callback2; |
| 4540 | EXPECT_EQ( |
| 4541 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4542 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4543 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4544 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4545 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4546 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4547 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4548 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4549 | TestGroupId("a"))); |
| 4550 | EXPECT_EQ(0u, |
| 4551 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4552 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4553 | |
| 4554 | // 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] | 4555 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4556 | &handle1)); |
| 4557 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4558 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4559 | |
| 4560 | // Reprioritizing the second request places it above the first, and it steals |
| 4561 | // the job from the first request. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4562 | pool_->SetPriority(TestGroupId("a"), &handle2, HIGHEST); |
| 4563 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4564 | &handle2)); |
| 4565 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4566 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4567 | } |
| 4568 | |
| 4569 | TEST_F(ClientSocketPoolBaseTest, CancelRequestReassignsJob) { |
| 4570 | CreatePool(kDefaultMaxSockets, 1); |
| 4571 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4572 | |
| 4573 | ClientSocketHandle handle1; |
| 4574 | TestCompletionCallback callback1; |
| 4575 | EXPECT_EQ( |
| 4576 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4577 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4578 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4579 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4580 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4581 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4582 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4583 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4584 | TestGroupId("a"))); |
| 4585 | EXPECT_EQ(0u, |
| 4586 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4587 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4588 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4589 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4590 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4591 | |
| 4592 | ClientSocketHandle handle2; |
| 4593 | TestCompletionCallback callback2; |
| 4594 | EXPECT_EQ( |
| 4595 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4596 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4597 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4598 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4599 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4600 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4601 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4602 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4603 | TestGroupId("a"))); |
| 4604 | EXPECT_EQ(0u, |
| 4605 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4606 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4607 | |
| 4608 | // The second request doesn't get a job because we are the limit. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4609 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4610 | &handle1)); |
| 4611 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4612 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4613 | |
| 4614 | // The second request should get a job upon cancelling the first request. |
| 4615 | handle1.Reset(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4616 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4617 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4618 | TestGroupId("a"))); |
| 4619 | EXPECT_EQ(0u, |
| 4620 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4621 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4622 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4623 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4624 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4625 | } |
| 4626 | |
| 4627 | TEST_F(ClientSocketPoolBaseTest, JobCompletionReassignsJob) { |
| 4628 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4629 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4630 | |
| 4631 | ClientSocketHandle handle1; |
| 4632 | TestCompletionCallback callback1; |
| 4633 | EXPECT_EQ( |
| 4634 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4635 | handle1.Init(TestGroupId("a"), params_, base::nullopt, HIGHEST, |
| 4636 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4637 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4638 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4639 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4640 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4641 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4642 | TestGroupId("a"))); |
| 4643 | EXPECT_EQ(0u, |
| 4644 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4645 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4646 | |
| 4647 | ClientSocketHandle handle2; |
| 4648 | TestCompletionCallback callback2; |
| 4649 | EXPECT_EQ( |
| 4650 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4651 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4652 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4653 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4654 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4655 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4656 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4657 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4658 | TestGroupId("a"))); |
| 4659 | EXPECT_EQ(0u, |
| 4660 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4661 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4662 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4663 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4664 | &handle1)); |
| 4665 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4666 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4667 | |
| 4668 | // The lower-priority job completes first. The higher-priority request should |
| 4669 | // get the socket, and the lower-priority request should get the remaining |
| 4670 | // job. |
| 4671 | client_socket_factory_.SignalJob(1); |
| 4672 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4673 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4674 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4675 | TestGroupId("a"))); |
| 4676 | EXPECT_EQ(0u, |
| 4677 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4678 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4679 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4680 | EXPECT_TRUE(handle1.socket()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4681 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4682 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4683 | } |
| 4684 | |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 4685 | class MockLayeredPool : public HigherLayeredPool { |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4686 | public: |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4687 | MockLayeredPool(TransportClientSocketPool* pool, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4688 | const ClientSocketPool::GroupId& group_id) |
| 4689 | : pool_(pool), group_id_(group_id), can_release_connection_(true) { |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 4690 | pool_->AddHigherLayeredPool(this); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4691 | } |
| 4692 | |
Daniel Cheng | 4496d082 | 2018-04-26 21:52:15 | [diff] [blame] | 4693 | ~MockLayeredPool() override { pool_->RemoveHigherLayeredPool(this); } |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4694 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4695 | int RequestSocket(TransportClientSocketPool* pool) { |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4696 | return handle_.Init( |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 4697 | group_id_, ClientSocketPool::SocketParams::CreateForHttpForTesting(), |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4698 | base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
| 4699 | ClientSocketPool::RespectLimits::ENABLED, callback_.callback(), |
| 4700 | ClientSocketPool::ProxyAuthCallback(), pool, NetLogWithSource()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4701 | } |
| 4702 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4703 | int RequestSocketWithoutLimits(TransportClientSocketPool* pool) { |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4704 | return handle_.Init( |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 4705 | group_id_, ClientSocketPool::SocketParams::CreateForHttpForTesting(), |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4706 | base::nullopt, MAXIMUM_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4707 | ClientSocketPool::RespectLimits::DISABLED, callback_.callback(), |
| 4708 | ClientSocketPool::ProxyAuthCallback(), pool, NetLogWithSource()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4709 | } |
| 4710 | |
| 4711 | bool ReleaseOneConnection() { |
| 4712 | if (!handle_.is_initialized() || !can_release_connection_) { |
| 4713 | return false; |
| 4714 | } |
| 4715 | handle_.socket()->Disconnect(); |
| 4716 | handle_.Reset(); |
| 4717 | return true; |
| 4718 | } |
| 4719 | |
| 4720 | void set_can_release_connection(bool can_release_connection) { |
| 4721 | can_release_connection_ = can_release_connection; |
| 4722 | } |
| 4723 | |
| 4724 | MOCK_METHOD0(CloseOneIdleConnection, bool()); |
| 4725 | |
| 4726 | private: |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4727 | TransportClientSocketPool* const pool_; |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4728 | ClientSocketHandle handle_; |
| 4729 | TestCompletionCallback callback_; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4730 | const ClientSocketPool::GroupId group_id_; |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4731 | bool can_release_connection_; |
| 4732 | }; |
| 4733 | |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4734 | // Tests the basic case of closing an idle socket in a higher layered pool when |
| 4735 | // a new request is issued and the lower layer pool is stalled. |
| 4736 | TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketsHeldByLayeredPoolWhenNeeded) { |
| 4737 | CreatePool(1, 1); |
| 4738 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 4739 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4740 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("foo")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4741 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4742 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4743 | .WillOnce(Invoke(&mock_layered_pool, |
| 4744 | &MockLayeredPool::ReleaseOneConnection)); |
| 4745 | ClientSocketHandle handle; |
| 4746 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4747 | EXPECT_EQ( |
| 4748 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4749 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4750 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4751 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4752 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4753 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4754 | } |
| 4755 | |
Matt Menke | 83367864 | 2019-03-05 22:05:51 | [diff] [blame] | 4756 | // Tests the case that trying to close an idle socket in a higher layered pool |
| 4757 | // fails. |
| 4758 | TEST_F(ClientSocketPoolBaseTest, |
| 4759 | CloseIdleSocketsHeldByLayeredPoolWhenNeededFails) { |
| 4760 | CreatePool(1, 1); |
| 4761 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 4762 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4763 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("foo")); |
Matt Menke | 83367864 | 2019-03-05 22:05:51 | [diff] [blame] | 4764 | mock_layered_pool.set_can_release_connection(false); |
| 4765 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
| 4766 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4767 | .WillOnce(Invoke(&mock_layered_pool, |
| 4768 | &MockLayeredPool::ReleaseOneConnection)); |
| 4769 | ClientSocketHandle handle; |
| 4770 | TestCompletionCallback callback; |
| 4771 | EXPECT_EQ( |
| 4772 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4773 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4774 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4775 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4776 | pool_.get(), NetLogWithSource())); |
Matt Menke | 83367864 | 2019-03-05 22:05:51 | [diff] [blame] | 4777 | base::RunLoop().RunUntilIdle(); |
| 4778 | EXPECT_FALSE(callback.have_result()); |
| 4779 | } |
| 4780 | |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4781 | // Same as above, but the idle socket is in the same group as the stalled |
| 4782 | // socket, and closes the only other request in its group when closing requests |
| 4783 | // in higher layered pools. This generally shouldn't happen, but it may be |
| 4784 | // possible if a higher level pool issues a request and the request is |
| 4785 | // subsequently cancelled. Even if it's not possible, best not to crash. |
| 4786 | TEST_F(ClientSocketPoolBaseTest, |
| 4787 | CloseIdleSocketsHeldByLayeredPoolWhenNeededSameGroup) { |
| 4788 | CreatePool(2, 2); |
| 4789 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 4790 | |
| 4791 | // Need a socket in another group for the pool to be stalled (If a group |
| 4792 | // has the maximum number of connections already, it's not stalled). |
| 4793 | ClientSocketHandle handle1; |
| 4794 | TestCompletionCallback callback1; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4795 | EXPECT_EQ(OK, handle1.Init(TestGroupId("group1"), params_, base::nullopt, |
| 4796 | DEFAULT_PRIORITY, SocketTag(), |
| 4797 | ClientSocketPool::RespectLimits::ENABLED, |
| 4798 | callback1.callback(), |
| 4799 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4800 | NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4801 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4802 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("group2")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4803 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4804 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4805 | .WillOnce(Invoke(&mock_layered_pool, |
| 4806 | &MockLayeredPool::ReleaseOneConnection)); |
| 4807 | ClientSocketHandle handle; |
| 4808 | TestCompletionCallback callback2; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4809 | EXPECT_EQ(ERR_IO_PENDING, |
| 4810 | handle.Init( |
| 4811 | TestGroupId("group2"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4812 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4813 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4814 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4815 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4816 | } |
| 4817 | |
| 4818 | // Tests the case when an idle socket can be closed when a new request is |
| 4819 | // issued, and the new request belongs to a group that was previously stalled. |
| 4820 | TEST_F(ClientSocketPoolBaseTest, |
| 4821 | CloseIdleSocketsHeldByLayeredPoolInSameGroupWhenNeeded) { |
| 4822 | CreatePool(2, 2); |
| 4823 | std::list<TestConnectJob::JobType> job_types; |
| 4824 | job_types.push_back(TestConnectJob::kMockJob); |
| 4825 | job_types.push_back(TestConnectJob::kMockJob); |
| 4826 | job_types.push_back(TestConnectJob::kMockJob); |
| 4827 | job_types.push_back(TestConnectJob::kMockJob); |
| 4828 | connect_job_factory_->set_job_types(&job_types); |
| 4829 | |
| 4830 | ClientSocketHandle handle1; |
| 4831 | TestCompletionCallback callback1; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4832 | EXPECT_EQ(OK, handle1.Init(TestGroupId("group1"), params_, base::nullopt, |
| 4833 | DEFAULT_PRIORITY, SocketTag(), |
| 4834 | ClientSocketPool::RespectLimits::ENABLED, |
| 4835 | callback1.callback(), |
| 4836 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4837 | NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4838 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4839 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("group2")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4840 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4841 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4842 | .WillRepeatedly(Invoke(&mock_layered_pool, |
| 4843 | &MockLayeredPool::ReleaseOneConnection)); |
| 4844 | mock_layered_pool.set_can_release_connection(false); |
| 4845 | |
| 4846 | // The third request is made when the socket pool is in a stalled state. |
| 4847 | ClientSocketHandle handle3; |
| 4848 | TestCompletionCallback callback3; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4849 | EXPECT_EQ(ERR_IO_PENDING, |
| 4850 | handle3.Init( |
| 4851 | TestGroupId("group3"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4852 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4853 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4854 | pool_.get(), NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4855 | |
| 4856 | base::RunLoop().RunUntilIdle(); |
| 4857 | EXPECT_FALSE(callback3.have_result()); |
| 4858 | |
| 4859 | // The fourth request is made when the pool is no longer stalled. The third |
| 4860 | // request should be serviced first, since it was issued first and has the |
| 4861 | // same priority. |
| 4862 | mock_layered_pool.set_can_release_connection(true); |
| 4863 | ClientSocketHandle handle4; |
| 4864 | TestCompletionCallback callback4; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4865 | EXPECT_EQ(ERR_IO_PENDING, |
| 4866 | handle4.Init( |
| 4867 | TestGroupId("group3"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4868 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4869 | callback4.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4870 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4871 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4872 | EXPECT_FALSE(callback4.have_result()); |
| 4873 | |
| 4874 | // Closing a handle should free up another socket slot. |
| 4875 | handle1.Reset(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4876 | EXPECT_THAT(callback4.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4877 | } |
| 4878 | |
| 4879 | // Tests the case when an idle socket can be closed when a new request is |
| 4880 | // issued, and the new request belongs to a group that was previously stalled. |
| 4881 | // |
| 4882 | // The two differences from the above test are that the stalled requests are not |
| 4883 | // in the same group as the layered pool's request, and the the fourth request |
| 4884 | // has a higher priority than the third one, so gets a socket first. |
| 4885 | TEST_F(ClientSocketPoolBaseTest, |
| 4886 | CloseIdleSocketsHeldByLayeredPoolInSameGroupWhenNeeded2) { |
| 4887 | CreatePool(2, 2); |
| 4888 | std::list<TestConnectJob::JobType> job_types; |
| 4889 | job_types.push_back(TestConnectJob::kMockJob); |
| 4890 | job_types.push_back(TestConnectJob::kMockJob); |
| 4891 | job_types.push_back(TestConnectJob::kMockJob); |
| 4892 | job_types.push_back(TestConnectJob::kMockJob); |
| 4893 | connect_job_factory_->set_job_types(&job_types); |
| 4894 | |
| 4895 | ClientSocketHandle handle1; |
| 4896 | TestCompletionCallback callback1; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4897 | EXPECT_EQ(OK, handle1.Init(TestGroupId("group1"), params_, base::nullopt, |
| 4898 | DEFAULT_PRIORITY, SocketTag(), |
| 4899 | ClientSocketPool::RespectLimits::ENABLED, |
| 4900 | callback1.callback(), |
| 4901 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4902 | NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4903 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4904 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("group2")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4905 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4906 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4907 | .WillRepeatedly(Invoke(&mock_layered_pool, |
| 4908 | &MockLayeredPool::ReleaseOneConnection)); |
| 4909 | mock_layered_pool.set_can_release_connection(false); |
| 4910 | |
| 4911 | // The third request is made when the socket pool is in a stalled state. |
| 4912 | ClientSocketHandle handle3; |
| 4913 | TestCompletionCallback callback3; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4914 | EXPECT_EQ( |
| 4915 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4916 | handle3.Init(TestGroupId("group3"), params_, base::nullopt, MEDIUM, |
| 4917 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4918 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4919 | pool_.get(), NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4920 | |
| 4921 | base::RunLoop().RunUntilIdle(); |
| 4922 | EXPECT_FALSE(callback3.have_result()); |
| 4923 | |
| 4924 | // The fourth request is made when the pool is no longer stalled. This |
| 4925 | // request has a higher priority than the third request, so is serviced first. |
| 4926 | mock_layered_pool.set_can_release_connection(true); |
| 4927 | ClientSocketHandle handle4; |
| 4928 | TestCompletionCallback callback4; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4929 | EXPECT_EQ( |
| 4930 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4931 | handle4.Init(TestGroupId("group3"), params_, base::nullopt, HIGHEST, |
| 4932 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4933 | callback4.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4934 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4935 | EXPECT_THAT(callback4.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4936 | EXPECT_FALSE(callback3.have_result()); |
| 4937 | |
| 4938 | // Closing a handle should free up another socket slot. |
| 4939 | handle1.Reset(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4940 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4941 | } |
| 4942 | |
| 4943 | TEST_F(ClientSocketPoolBaseTest, |
| 4944 | CloseMultipleIdleSocketsHeldByLayeredPoolWhenNeeded) { |
| 4945 | CreatePool(1, 1); |
| 4946 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 4947 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4948 | MockLayeredPool mock_layered_pool1(pool_.get(), TestGroupId("foo")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4949 | EXPECT_THAT(mock_layered_pool1.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4950 | EXPECT_CALL(mock_layered_pool1, CloseOneIdleConnection()) |
| 4951 | .WillRepeatedly(Invoke(&mock_layered_pool1, |
| 4952 | &MockLayeredPool::ReleaseOneConnection)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4953 | MockLayeredPool mock_layered_pool2(pool_.get(), TestGroupId("bar")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4954 | EXPECT_THAT(mock_layered_pool2.RequestSocketWithoutLimits(pool_.get()), |
| 4955 | IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4956 | EXPECT_CALL(mock_layered_pool2, CloseOneIdleConnection()) |
| 4957 | .WillRepeatedly(Invoke(&mock_layered_pool2, |
| 4958 | &MockLayeredPool::ReleaseOneConnection)); |
| 4959 | ClientSocketHandle handle; |
| 4960 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4961 | EXPECT_EQ( |
| 4962 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4963 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4964 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4965 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4966 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4967 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4968 | } |
| 4969 | |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4970 | // Test that when a socket pool and group are at their limits, a request |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4971 | // with RespectLimits::DISABLED triggers creation of a new socket, and gets the |
| 4972 | // socket instead of a request with the same priority that was issued earlier, |
| 4973 | // but has RespectLimits::ENABLED. |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4974 | TEST_F(ClientSocketPoolBaseTest, IgnoreLimits) { |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4975 | CreatePool(1, 1); |
| 4976 | |
| 4977 | // Issue a request to reach the socket pool limit. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4978 | EXPECT_EQ(OK, StartRequestWithIgnoreLimits( |
| 4979 | TestGroupId("a"), MAXIMUM_PRIORITY, |
| 4980 | ClientSocketPool::RespectLimits::ENABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4981 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4982 | |
| 4983 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 4984 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4985 | EXPECT_EQ(ERR_IO_PENDING, StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4986 | TestGroupId("a"), MAXIMUM_PRIORITY, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4987 | ClientSocketPool::RespectLimits::ENABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4988 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4989 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4990 | // Issue a request that ignores the limits, so a new ConnectJob is |
| 4991 | // created. |
| 4992 | EXPECT_EQ(ERR_IO_PENDING, StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4993 | TestGroupId("a"), MAXIMUM_PRIORITY, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4994 | ClientSocketPool::RespectLimits::DISABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4995 | ASSERT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4996 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4997 | EXPECT_THAT(request(2)->WaitForResult(), IsOk()); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4998 | EXPECT_FALSE(request(1)->have_result()); |
| 4999 | } |
| 5000 | |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 5001 | // Test that when a socket pool and group are at their limits, a ConnectJob |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5002 | // issued for a request with RespectLimits::DISABLED is not cancelled when a |
| 5003 | // request with RespectLimits::ENABLED issued to the same group is cancelled. |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 5004 | TEST_F(ClientSocketPoolBaseTest, IgnoreLimitsCancelOtherJob) { |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 5005 | CreatePool(1, 1); |
| 5006 | |
| 5007 | // Issue a request to reach the socket pool limit. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5008 | EXPECT_EQ(OK, StartRequestWithIgnoreLimits( |
| 5009 | TestGroupId("a"), MAXIMUM_PRIORITY, |
| 5010 | ClientSocketPool::RespectLimits::ENABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5011 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 5012 | |
| 5013 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 5014 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5015 | EXPECT_EQ(ERR_IO_PENDING, StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5016 | TestGroupId("a"), MAXIMUM_PRIORITY, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5017 | ClientSocketPool::RespectLimits::ENABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5018 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 5019 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5020 | // Issue a request with RespectLimits::DISABLED, so a new ConnectJob is |
| 5021 | // created. |
| 5022 | EXPECT_EQ(ERR_IO_PENDING, StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5023 | TestGroupId("a"), MAXIMUM_PRIORITY, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5024 | ClientSocketPool::RespectLimits::DISABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5025 | ASSERT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5026 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5027 | // Cancel the pending request with RespectLimits::ENABLED. The ConnectJob |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5028 | // should not be cancelled. |
| 5029 | request(1)->handle()->Reset(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5030 | ASSERT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5031 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5032 | EXPECT_THAT(request(2)->WaitForResult(), IsOk()); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5033 | EXPECT_FALSE(request(1)->have_result()); |
| 5034 | } |
| 5035 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5036 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthNoAuthCallback) { |
| 5037 | CreatePool(1, 1); |
| 5038 | |
| 5039 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5040 | |
| 5041 | ClientSocketHandle handle; |
| 5042 | TestCompletionCallback callback; |
| 5043 | EXPECT_EQ( |
| 5044 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5045 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 5046 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5047 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5048 | pool_.get(), NetLogWithSource())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5049 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5050 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5051 | |
| 5052 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_PROXY_AUTH_REQUESTED)); |
| 5053 | EXPECT_FALSE(handle.is_initialized()); |
| 5054 | EXPECT_FALSE(handle.socket()); |
| 5055 | |
| 5056 | // The group should now be empty, and thus be deleted. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5057 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5058 | } |
| 5059 | |
| 5060 | class TestAuthHelper { |
| 5061 | public: |
| 5062 | TestAuthHelper() = default; |
| 5063 | ~TestAuthHelper() = default; |
| 5064 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5065 | void InitHandle( |
Matt Menke | 84d11e56 | 2019-03-27 00:11:19 | [diff] [blame] | 5066 | scoped_refptr<ClientSocketPool::SocketParams> params, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5067 | TransportClientSocketPool* pool, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5068 | RequestPriority priority = DEFAULT_PRIORITY, |
| 5069 | ClientSocketPool::RespectLimits respect_limits = |
| 5070 | ClientSocketPool::RespectLimits::ENABLED, |
| 5071 | const ClientSocketPool::GroupId& group_id_in = TestGroupId("a")) { |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5072 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5073 | handle_.Init(group_id_in, params, base::nullopt, priority, |
| 5074 | SocketTag(), respect_limits, callback_.callback(), |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5075 | base::BindRepeating(&TestAuthHelper::AuthCallback, |
| 5076 | base::Unretained(this)), |
| 5077 | pool, NetLogWithSource())); |
| 5078 | } |
| 5079 | |
| 5080 | void WaitForAuth() { |
| 5081 | run_loop_ = std::make_unique<base::RunLoop>(); |
| 5082 | run_loop_->Run(); |
| 5083 | run_loop_.reset(); |
| 5084 | } |
| 5085 | |
| 5086 | void WaitForAuthAndRestartSync() { |
| 5087 | restart_sync_ = true; |
| 5088 | WaitForAuth(); |
| 5089 | restart_sync_ = false; |
| 5090 | } |
| 5091 | |
| 5092 | void WaitForAuthAndResetHandleSync() { |
| 5093 | reset_handle_sync_ = true; |
| 5094 | WaitForAuth(); |
| 5095 | reset_handle_sync_ = false; |
| 5096 | } |
| 5097 | |
| 5098 | void RestartWithAuth() { |
| 5099 | DCHECK(restart_with_auth_callback_); |
| 5100 | std::move(restart_with_auth_callback_).Run(); |
| 5101 | } |
| 5102 | |
| 5103 | int WaitForResult() { |
| 5104 | int result = callback_.WaitForResult(); |
| 5105 | // There shouldn't be any callback waiting to be invoked once the request is |
| 5106 | // complete. |
| 5107 | EXPECT_FALSE(restart_with_auth_callback_); |
| 5108 | // The socket should only be initialized on success. |
| 5109 | EXPECT_EQ(result == OK, handle_.is_initialized()); |
| 5110 | EXPECT_EQ(result == OK, handle_.socket() != nullptr); |
| 5111 | return result; |
| 5112 | } |
| 5113 | |
| 5114 | ClientSocketHandle* handle() { return &handle_; } |
| 5115 | int auth_count() const { return auth_count_; } |
| 5116 | int have_result() const { return callback_.have_result(); } |
| 5117 | |
| 5118 | private: |
| 5119 | void AuthCallback(const HttpResponseInfo& response, |
| 5120 | HttpAuthController* auth_controller, |
| 5121 | base::OnceClosure restart_with_auth_callback) { |
| 5122 | EXPECT_FALSE(restart_with_auth_callback_); |
| 5123 | EXPECT_TRUE(restart_with_auth_callback); |
| 5124 | |
| 5125 | // Once there's a result, this method shouldn't be invoked again. |
| 5126 | EXPECT_FALSE(callback_.have_result()); |
| 5127 | |
| 5128 | ++auth_count_; |
| 5129 | run_loop_->Quit(); |
| 5130 | if (restart_sync_) { |
| 5131 | std::move(restart_with_auth_callback).Run(); |
| 5132 | return; |
| 5133 | } |
| 5134 | |
| 5135 | restart_with_auth_callback_ = std::move(restart_with_auth_callback); |
| 5136 | |
| 5137 | if (reset_handle_sync_) { |
| 5138 | handle_.Reset(); |
| 5139 | return; |
| 5140 | } |
| 5141 | } |
| 5142 | |
| 5143 | std::unique_ptr<base::RunLoop> run_loop_; |
| 5144 | base::OnceClosure restart_with_auth_callback_; |
| 5145 | |
| 5146 | bool restart_sync_ = false; |
| 5147 | bool reset_handle_sync_ = false; |
| 5148 | |
| 5149 | ClientSocketHandle handle_; |
| 5150 | int auth_count_ = 0; |
| 5151 | TestCompletionCallback callback_; |
| 5152 | |
| 5153 | DISALLOW_COPY_AND_ASSIGN(TestAuthHelper); |
| 5154 | }; |
| 5155 | |
| 5156 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnce) { |
| 5157 | CreatePool(1, 1); |
| 5158 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5159 | |
| 5160 | TestAuthHelper auth_helper; |
| 5161 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5162 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5163 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5164 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5165 | |
| 5166 | auth_helper.WaitForAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5167 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5168 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5169 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5170 | |
| 5171 | auth_helper.RestartWithAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5172 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5173 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5174 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5175 | |
| 5176 | EXPECT_THAT(auth_helper.WaitForResult(), IsOk()); |
| 5177 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5178 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5179 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5180 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5181 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5182 | } |
| 5183 | |
| 5184 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceSync) { |
| 5185 | CreatePool(1, 1); |
| 5186 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5187 | |
| 5188 | TestAuthHelper auth_helper; |
| 5189 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5190 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5191 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5192 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5193 | |
| 5194 | auth_helper.WaitForAuthAndRestartSync(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5195 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5196 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5197 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5198 | |
| 5199 | EXPECT_THAT(auth_helper.WaitForResult(), IsOk()); |
| 5200 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5201 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5202 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5203 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5204 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5205 | } |
| 5206 | |
| 5207 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceFails) { |
| 5208 | CreatePool(1, 1); |
| 5209 | connect_job_factory_->set_job_type( |
| 5210 | TestConnectJob::kMockAuthChallengeOnceFailingJob); |
| 5211 | |
| 5212 | TestAuthHelper auth_helper; |
| 5213 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5214 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5215 | |
| 5216 | auth_helper.WaitForAuth(); |
| 5217 | auth_helper.RestartWithAuth(); |
| 5218 | EXPECT_THAT(auth_helper.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 5219 | |
| 5220 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5221 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5222 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5223 | } |
| 5224 | |
| 5225 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceSyncFails) { |
| 5226 | CreatePool(1, 1); |
| 5227 | connect_job_factory_->set_job_type( |
| 5228 | TestConnectJob::kMockAuthChallengeOnceFailingJob); |
| 5229 | |
| 5230 | TestAuthHelper auth_helper; |
| 5231 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5232 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5233 | |
| 5234 | auth_helper.WaitForAuthAndRestartSync(); |
| 5235 | EXPECT_THAT(auth_helper.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 5236 | |
| 5237 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5238 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5239 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5240 | } |
| 5241 | |
| 5242 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceDeleteHandle) { |
| 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 | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5249 | |
| 5250 | auth_helper.WaitForAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5251 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5252 | |
| 5253 | auth_helper.handle()->Reset(); |
| 5254 | |
| 5255 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5256 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5257 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5258 | EXPECT_FALSE(auth_helper.handle()->is_initialized()); |
| 5259 | EXPECT_FALSE(auth_helper.handle()->socket()); |
| 5260 | } |
| 5261 | |
| 5262 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceDeleteHandleSync) { |
| 5263 | CreatePool(1, 1); |
| 5264 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5265 | |
| 5266 | TestAuthHelper auth_helper; |
| 5267 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5268 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5269 | |
| 5270 | auth_helper.WaitForAuthAndResetHandleSync(); |
| 5271 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5272 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5273 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5274 | EXPECT_FALSE(auth_helper.handle()->is_initialized()); |
| 5275 | EXPECT_FALSE(auth_helper.handle()->socket()); |
| 5276 | } |
| 5277 | |
| 5278 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceFlushWithError) { |
| 5279 | CreatePool(1, 1); |
| 5280 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5281 | |
| 5282 | TestAuthHelper auth_helper; |
| 5283 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5284 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5285 | |
| 5286 | auth_helper.WaitForAuth(); |
| 5287 | |
| 5288 | pool_->FlushWithError(ERR_FAILED); |
| 5289 | base::RunLoop().RunUntilIdle(); |
| 5290 | |
| 5291 | // When flushing the socket pool, bound sockets should delay returning the |
| 5292 | // error until completion. |
| 5293 | EXPECT_FALSE(auth_helper.have_result()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5294 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5295 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5296 | |
| 5297 | auth_helper.RestartWithAuth(); |
| 5298 | // The callback should be called asynchronously. |
| 5299 | EXPECT_FALSE(auth_helper.have_result()); |
| 5300 | |
| 5301 | EXPECT_THAT(auth_helper.WaitForResult(), IsError(ERR_FAILED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5302 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5303 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5304 | } |
| 5305 | |
| 5306 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthTwice) { |
| 5307 | CreatePool(1, 1); |
| 5308 | connect_job_factory_->set_job_type( |
| 5309 | TestConnectJob::kMockAuthChallengeTwiceJob); |
| 5310 | |
| 5311 | TestAuthHelper auth_helper; |
| 5312 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5313 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5314 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5315 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5316 | |
| 5317 | auth_helper.WaitForAuth(); |
| 5318 | auth_helper.RestartWithAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5319 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5320 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5321 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5322 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5323 | |
| 5324 | auth_helper.WaitForAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5325 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5326 | EXPECT_EQ(2, auth_helper.auth_count()); |
| 5327 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5328 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5329 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5330 | auth_helper.RestartWithAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5331 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5332 | EXPECT_EQ(2, auth_helper.auth_count()); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5333 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5334 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5335 | |
| 5336 | EXPECT_THAT(auth_helper.WaitForResult(), IsOk()); |
| 5337 | EXPECT_EQ(2, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5338 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5339 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5340 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5341 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5342 | } |
| 5343 | |
| 5344 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthTwiceFails) { |
| 5345 | CreatePool(1, 1); |
| 5346 | connect_job_factory_->set_job_type( |
| 5347 | TestConnectJob::kMockAuthChallengeTwiceFailingJob); |
| 5348 | |
| 5349 | TestAuthHelper auth_helper; |
| 5350 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5351 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5352 | |
| 5353 | auth_helper.WaitForAuth(); |
| 5354 | auth_helper.RestartWithAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5355 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5356 | EXPECT_EQ(1, auth_helper.auth_count()); |
| 5357 | |
| 5358 | auth_helper.WaitForAuth(); |
| 5359 | auth_helper.RestartWithAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5360 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5361 | EXPECT_EQ(2, auth_helper.auth_count()); |
| 5362 | |
| 5363 | EXPECT_THAT(auth_helper.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 5364 | EXPECT_EQ(2, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5365 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5366 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5367 | } |
| 5368 | |
| 5369 | // Makes sure that when a bound request is destroyed, a new ConnectJob is |
| 5370 | // created, if needed. |
| 5371 | TEST_F(ClientSocketPoolBaseTest, |
| 5372 | ProxyAuthCreateNewConnectJobOnDestroyBoundRequest) { |
| 5373 | CreatePool(1 /* max_sockets */, 1 /* max_sockets_per_group */); |
| 5374 | connect_job_factory_->set_job_type( |
| 5375 | TestConnectJob::kMockAuthChallengeOnceFailingJob); |
| 5376 | |
| 5377 | // First request creates a ConnectJob. |
| 5378 | TestAuthHelper auth_helper1; |
| 5379 | auth_helper1.InitHandle(params_, pool_.get()); |
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 | |
| 5382 | // A second request come in, but no new ConnectJob is needed, since the limit |
| 5383 | // has been reached. |
| 5384 | TestAuthHelper auth_helper2; |
| 5385 | auth_helper2.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5386 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5387 | |
| 5388 | // Run until the auth callback for the first request is invoked. |
| 5389 | auth_helper1.WaitForAuth(); |
| 5390 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5391 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5392 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5393 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5394 | |
| 5395 | // Make connect jobs succeed, then cancel the first request, which should |
| 5396 | // destroy the bound ConnectJob, and cause a new ConnectJob to start. |
| 5397 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 5398 | auth_helper1.handle()->Reset(); |
| 5399 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5400 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5401 | |
| 5402 | // The second ConnectJob should succeed. |
| 5403 | EXPECT_THAT(auth_helper2.WaitForResult(), IsOk()); |
| 5404 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5405 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5406 | } |
| 5407 | |
| 5408 | // Makes sure that when a bound request is destroyed, a new ConnectJob is |
| 5409 | // created for another group, if needed. |
| 5410 | TEST_F(ClientSocketPoolBaseTest, |
| 5411 | ProxyAuthCreateNewConnectJobOnDestroyBoundRequestDifferentGroups) { |
| 5412 | CreatePool(1 /* max_sockets */, 1 /* max_sockets_per_group */); |
| 5413 | connect_job_factory_->set_job_type( |
| 5414 | TestConnectJob::kMockAuthChallengeOnceFailingJob); |
| 5415 | |
| 5416 | // First request creates a ConnectJob. |
| 5417 | TestAuthHelper auth_helper1; |
| 5418 | auth_helper1.InitHandle(params_, pool_.get(), DEFAULT_PRIORITY); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5419 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5420 | |
| 5421 | // A second request come in, but no new ConnectJob is needed, since the limit |
| 5422 | // has been reached. |
| 5423 | TestAuthHelper auth_helper2; |
| 5424 | auth_helper2.InitHandle(params_, pool_.get(), DEFAULT_PRIORITY, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5425 | ClientSocketPool::RespectLimits::ENABLED, |
| 5426 | TestGroupId("b")); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5427 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5428 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5429 | |
| 5430 | // Run until the auth callback for the first request is invoked. |
| 5431 | auth_helper1.WaitForAuth(); |
| 5432 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5433 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5434 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5435 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5436 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 5437 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5438 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5439 | |
| 5440 | // Make connect jobs succeed, then cancel the first request, which should |
| 5441 | // destroy the bound ConnectJob, and cause a new ConnectJob to start for the |
| 5442 | // other group. |
| 5443 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 5444 | auth_helper1.handle()->Reset(); |
| 5445 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5446 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 5447 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5448 | |
| 5449 | // The second ConnectJob should succeed. |
| 5450 | EXPECT_THAT(auth_helper2.WaitForResult(), IsOk()); |
| 5451 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5452 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 5453 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5454 | } |
| 5455 | |
| 5456 | // Test that once an auth challenge is bound, that's the request that gets all |
| 5457 | // subsequent calls and the socket itself. |
| 5458 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthStaysBound) { |
| 5459 | CreatePool(1, 1); |
| 5460 | connect_job_factory_->set_job_type( |
| 5461 | TestConnectJob::kMockAuthChallengeTwiceJob); |
| 5462 | |
| 5463 | // First request creates a ConnectJob. |
| 5464 | TestAuthHelper auth_helper1; |
| 5465 | auth_helper1.InitHandle(params_, pool_.get(), LOWEST); |
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, higher priority request is made. |
| 5469 | TestAuthHelper auth_helper2; |
| 5470 | auth_helper2.InitHandle(params_, pool_.get(), LOW); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5471 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5472 | |
| 5473 | // Run until the auth callback for the second request is invoked. |
| 5474 | auth_helper2.WaitForAuth(); |
| 5475 | EXPECT_EQ(0, auth_helper1.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5476 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5477 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5478 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5479 | |
| 5480 | // Start a higher priority job. It shouldn't be able to steal |auth_helper2|'s |
| 5481 | // ConnectJob. |
| 5482 | TestAuthHelper auth_helper3; |
| 5483 | auth_helper3.InitHandle(params_, pool_.get(), HIGHEST); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5484 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5485 | |
| 5486 | // Start a higher job that ignores limits, creating a hanging socket. It |
| 5487 | // shouldn't be able to steal |auth_helper2|'s ConnectJob. |
| 5488 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 5489 | TestAuthHelper auth_helper4; |
| 5490 | auth_helper4.InitHandle(params_, pool_.get(), HIGHEST, |
| 5491 | ClientSocketPool::RespectLimits::DISABLED); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5492 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5493 | |
| 5494 | // Restart with auth, and |auth_helper2|'s auth method should be invoked |
| 5495 | // again. |
| 5496 | auth_helper2.RestartWithAuth(); |
| 5497 | auth_helper2.WaitForAuth(); |
| 5498 | EXPECT_EQ(0, auth_helper1.auth_count()); |
| 5499 | EXPECT_FALSE(auth_helper1.have_result()); |
| 5500 | EXPECT_EQ(2, auth_helper2.auth_count()); |
| 5501 | EXPECT_FALSE(auth_helper2.have_result()); |
| 5502 | EXPECT_EQ(0, auth_helper3.auth_count()); |
| 5503 | EXPECT_FALSE(auth_helper3.have_result()); |
| 5504 | EXPECT_EQ(0, auth_helper4.auth_count()); |
| 5505 | EXPECT_FALSE(auth_helper4.have_result()); |
| 5506 | |
| 5507 | // Advance auth again, and |auth_helper2| should get the socket. |
| 5508 | auth_helper2.RestartWithAuth(); |
| 5509 | EXPECT_THAT(auth_helper2.WaitForResult(), IsOk()); |
| 5510 | // The hung ConnectJob for the RespectLimits::DISABLED request is still in the |
| 5511 | // socket pool. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5512 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5513 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5514 | EXPECT_EQ(0, auth_helper1.auth_count()); |
| 5515 | EXPECT_FALSE(auth_helper1.have_result()); |
| 5516 | EXPECT_EQ(0, auth_helper3.auth_count()); |
| 5517 | EXPECT_FALSE(auth_helper3.have_result()); |
| 5518 | EXPECT_EQ(0, auth_helper4.auth_count()); |
| 5519 | EXPECT_FALSE(auth_helper4.have_result()); |
| 5520 | |
| 5521 | // If the socket is returned to the socket pool, the RespectLimits::DISABLED |
| 5522 | // socket request should be able to claim it. |
| 5523 | auth_helper2.handle()->Reset(); |
| 5524 | EXPECT_THAT(auth_helper4.WaitForResult(), IsOk()); |
| 5525 | EXPECT_EQ(0, auth_helper1.auth_count()); |
| 5526 | EXPECT_FALSE(auth_helper1.have_result()); |
| 5527 | EXPECT_EQ(0, auth_helper3.auth_count()); |
| 5528 | EXPECT_FALSE(auth_helper3.have_result()); |
| 5529 | EXPECT_EQ(0, auth_helper4.auth_count()); |
| 5530 | } |
| 5531 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5532 | enum class RefreshType { |
| 5533 | kServer, |
| 5534 | kProxy, |
| 5535 | }; |
| 5536 | |
| 5537 | // Common base class to test RefreshGroup() when called from either |
| 5538 | // OnSSLConfigForServerChanged() matching a specific group or the pool's proxy. |
| 5539 | // |
| 5540 | // Tests which test behavior specific to one or the other case should use |
| 5541 | // ClientSocketPoolBaseTest directly. In particular, there is no "other group" |
| 5542 | // when the pool's proxy matches. |
| 5543 | class ClientSocketPoolBaseRefreshTest |
| 5544 | : public ClientSocketPoolBaseTest, |
| 5545 | public testing::WithParamInterface<RefreshType> { |
| 5546 | public: |
| 5547 | void CreatePoolForRefresh(int max_sockets, |
| 5548 | int max_sockets_per_group, |
| 5549 | bool enable_backup_connect_jobs = false) { |
| 5550 | switch (GetParam()) { |
| 5551 | case RefreshType::kServer: |
| 5552 | CreatePool(max_sockets, max_sockets_per_group, |
| 5553 | enable_backup_connect_jobs); |
| 5554 | break; |
| 5555 | case RefreshType::kProxy: |
| 5556 | CreatePoolWithIdleTimeouts( |
| 5557 | max_sockets, max_sockets_per_group, kUnusedIdleSocketTimeout, |
| 5558 | ClientSocketPool::used_idle_socket_timeout(), |
| 5559 | enable_backup_connect_jobs, |
| 5560 | ProxyServer::FromPacString("HTTPS myproxy:70")); |
| 5561 | break; |
| 5562 | } |
| 5563 | } |
| 5564 | |
| 5565 | static ClientSocketPool::GroupId GetGroupId() { |
| 5566 | return TestGroupId("a", 443, ClientSocketPool::SocketType::kSsl); |
| 5567 | } |
| 5568 | |
David Benjamin | 6dd7e88 | 2019-10-10 02:35:23 | [diff] [blame^] | 5569 | static ClientSocketPool::GroupId GetGroupIdInPartition() { |
| 5570 | // Note this GroupId will match GetGroupId() unless |
| 5571 | // kPartitionConnectionsByNetworkIsolationKey is enabled. |
| 5572 | const auto kOrigin = url::Origin::Create(GURL("https://b/")); |
| 5573 | const NetworkIsolationKey kNetworkIsolationKey(kOrigin, kOrigin); |
| 5574 | return TestGroupId("a", 443, ClientSocketPool::SocketType::kSsl, |
| 5575 | PrivacyMode::PRIVACY_MODE_DISABLED, |
| 5576 | kNetworkIsolationKey); |
| 5577 | } |
| 5578 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5579 | void OnSSLConfigForServerChanged() { |
| 5580 | switch (GetParam()) { |
| 5581 | case RefreshType::kServer: |
| 5582 | pool_->OnSSLConfigForServerChanged(HostPortPair("a", 443)); |
| 5583 | break; |
| 5584 | case RefreshType::kProxy: |
| 5585 | pool_->OnSSLConfigForServerChanged(HostPortPair("myproxy", 70)); |
| 5586 | break; |
| 5587 | } |
| 5588 | } |
| 5589 | }; |
| 5590 | |
| 5591 | INSTANTIATE_TEST_SUITE_P(RefreshType, |
| 5592 | ClientSocketPoolBaseRefreshTest, |
| 5593 | ::testing::Values(RefreshType::kServer, |
| 5594 | RefreshType::kProxy)); |
| 5595 | |
| 5596 | TEST_P(ClientSocketPoolBaseRefreshTest, RefreshGroupCreatesNewConnectJobs) { |
| 5597 | CreatePoolForRefresh(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 5598 | const ClientSocketPool::GroupId kGroupId = GetGroupId(); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5599 | |
| 5600 | // First job will be waiting until it gets aborted. |
| 5601 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 5602 | |
| 5603 | ClientSocketHandle handle; |
| 5604 | TestCompletionCallback callback; |
| 5605 | EXPECT_THAT( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5606 | handle.Init(kGroupId, params_, base::nullopt, DEFAULT_PRIORITY, |
| 5607 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5608 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5609 | pool_.get(), NetLogWithSource()), |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5610 | IsError(ERR_IO_PENDING)); |
| 5611 | |
| 5612 | // Switch connect job types, so creating a new ConnectJob will result in |
| 5613 | // success. |
| 5614 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 5615 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5616 | OnSSLConfigForServerChanged(); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5617 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 5618 | ASSERT_TRUE(handle.socket()); |
| 5619 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5620 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId)); |
| 5621 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(kGroupId)); |
| 5622 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(kGroupId)); |
| 5623 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId)); |
| 5624 | } |
| 5625 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5626 | TEST_P(ClientSocketPoolBaseRefreshTest, RefreshGroupClosesIdleConnectJobs) { |
David Benjamin | 6dd7e88 | 2019-10-10 02:35:23 | [diff] [blame^] | 5627 | base::test::ScopedFeatureList feature_list; |
| 5628 | feature_list.InitAndEnableFeature( |
| 5629 | features::kPartitionConnectionsByNetworkIsolationKey); |
| 5630 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5631 | CreatePoolForRefresh(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 5632 | const ClientSocketPool::GroupId kGroupId = GetGroupId(); |
David Benjamin | 6dd7e88 | 2019-10-10 02:35:23 | [diff] [blame^] | 5633 | const ClientSocketPool::GroupId kGroupIdInPartition = GetGroupIdInPartition(); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5634 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5635 | pool_->RequestSockets(kGroupId, params_, base::nullopt, 2, |
| 5636 | NetLogWithSource()); |
David Benjamin | 6dd7e88 | 2019-10-10 02:35:23 | [diff] [blame^] | 5637 | pool_->RequestSockets(kGroupIdInPartition, params_, base::nullopt, 2, |
| 5638 | NetLogWithSource()); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5639 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId)); |
David Benjamin | 6dd7e88 | 2019-10-10 02:35:23 | [diff] [blame^] | 5640 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupIdInPartition)); |
| 5641 | EXPECT_EQ(4, pool_->IdleSocketCount()); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5642 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(kGroupId)); |
David Benjamin | 6dd7e88 | 2019-10-10 02:35:23 | [diff] [blame^] | 5643 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(kGroupIdInPartition)); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5644 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5645 | OnSSLConfigForServerChanged(); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5646 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5647 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupId)); |
David Benjamin | 6dd7e88 | 2019-10-10 02:35:23 | [diff] [blame^] | 5648 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupIdInPartition)); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5649 | } |
| 5650 | |
| 5651 | TEST_F(ClientSocketPoolBaseTest, |
| 5652 | RefreshGroupDoesNotCloseIdleConnectJobsInOtherGroup) { |
| 5653 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5654 | const ClientSocketPool::GroupId kGroupId = |
| 5655 | TestGroupId("a", 443, ClientSocketPool::SocketType::kSsl); |
| 5656 | const ClientSocketPool::GroupId kOtherGroupId = |
| 5657 | TestGroupId("b", 443, ClientSocketPool::SocketType::kSsl); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5658 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5659 | pool_->RequestSockets(kOtherGroupId, params_, base::nullopt, 2, |
| 5660 | NetLogWithSource()); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5661 | ASSERT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 5662 | EXPECT_EQ(2, pool_->IdleSocketCount()); |
| 5663 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(kOtherGroupId)); |
| 5664 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5665 | pool_->OnSSLConfigForServerChanged(HostPortPair("a", 443)); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5666 | ASSERT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 5667 | EXPECT_EQ(2, pool_->IdleSocketCount()); |
| 5668 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(kOtherGroupId)); |
| 5669 | } |
| 5670 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5671 | TEST_P(ClientSocketPoolBaseRefreshTest, RefreshGroupPreventsSocketReuse) { |
| 5672 | CreatePoolForRefresh(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 5673 | const ClientSocketPool::GroupId kGroupId = GetGroupId(); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5674 | |
| 5675 | ClientSocketHandle handle; |
| 5676 | TestCompletionCallback callback; |
| 5677 | EXPECT_THAT( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5678 | handle.Init(kGroupId, params_, base::nullopt, DEFAULT_PRIORITY, |
| 5679 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5680 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5681 | pool_.get(), NetLogWithSource()), |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5682 | IsOk()); |
| 5683 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId)); |
| 5684 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId)); |
| 5685 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5686 | OnSSLConfigForServerChanged(); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5687 | |
| 5688 | handle.Reset(); |
| 5689 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5690 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupId)); |
| 5691 | } |
| 5692 | |
| 5693 | TEST_F(ClientSocketPoolBaseTest, |
| 5694 | RefreshGroupDoesNotPreventSocketReuseInOtherGroup) { |
| 5695 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5696 | const ClientSocketPool::GroupId kGroupId = |
| 5697 | TestGroupId("a", 443, ClientSocketPool::SocketType::kSsl); |
| 5698 | const ClientSocketPool::GroupId kOtherGroupId = |
| 5699 | TestGroupId("b", 443, ClientSocketPool::SocketType::kSsl); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5700 | |
| 5701 | ClientSocketHandle handle; |
| 5702 | TestCompletionCallback callback; |
| 5703 | EXPECT_THAT( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5704 | handle.Init(kOtherGroupId, params_, base::nullopt, DEFAULT_PRIORITY, |
| 5705 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5706 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5707 | pool_.get(), NetLogWithSource()), |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5708 | IsOk()); |
| 5709 | ASSERT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 5710 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kOtherGroupId)); |
| 5711 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5712 | pool_->OnSSLConfigForServerChanged(HostPortPair("a", 443)); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5713 | |
| 5714 | handle.Reset(); |
| 5715 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 5716 | ASSERT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 5717 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(kOtherGroupId)); |
| 5718 | } |
| 5719 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5720 | TEST_P(ClientSocketPoolBaseRefreshTest, |
| 5721 | RefreshGroupReplacesBoundConnectJobOnConnect) { |
| 5722 | CreatePoolForRefresh(1, 1); |
| 5723 | const ClientSocketPool::GroupId kGroupId = GetGroupId(); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5724 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5725 | |
| 5726 | TestAuthHelper auth_helper; |
| 5727 | auth_helper.InitHandle(params_, pool_.get(), DEFAULT_PRIORITY, |
| 5728 | ClientSocketPool::RespectLimits::ENABLED, kGroupId); |
| 5729 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(kGroupId)); |
| 5730 | |
| 5731 | auth_helper.WaitForAuth(); |
| 5732 | |
| 5733 | // This should update the generation, but not cancel the old ConnectJob - it's |
| 5734 | // not safe to do anything while waiting on the original ConnectJob. |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5735 | OnSSLConfigForServerChanged(); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5736 | |
| 5737 | // Providing auth credentials and restarting the request with them will cause |
| 5738 | // the ConnectJob to complete successfully, but the result will be discarded |
| 5739 | // because of the generation mismatch. |
| 5740 | auth_helper.RestartWithAuth(); |
| 5741 | |
| 5742 | // Despite using ConnectJobs that simulate a single challenge, a second |
| 5743 | // challenge will be seen, due to using a new ConnectJob. |
| 5744 | auth_helper.WaitForAuth(); |
| 5745 | auth_helper.RestartWithAuth(); |
| 5746 | |
| 5747 | EXPECT_THAT(auth_helper.WaitForResult(), IsOk()); |
| 5748 | EXPECT_TRUE(auth_helper.handle()->socket()); |
| 5749 | EXPECT_EQ(2, auth_helper.auth_count()); |
| 5750 | |
| 5751 | // When released, the socket will be returned to the socket pool, and |
| 5752 | // available for reuse. |
| 5753 | auth_helper.handle()->Reset(); |
| 5754 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 5755 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId)); |
| 5756 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(kGroupId)); |
| 5757 | } |
| 5758 | |
David Benjamin | bac8dff | 2019-08-07 01:30:41 | [diff] [blame] | 5759 | TEST_F(ClientSocketPoolBaseTest, RefreshProxyRefreshesAllGroups) { |
| 5760 | CreatePoolWithIdleTimeouts(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 5761 | kUnusedIdleSocketTimeout, |
| 5762 | ClientSocketPool::used_idle_socket_timeout(), |
| 5763 | false /* no backup connect jobs */, |
| 5764 | ProxyServer::FromPacString("HTTPS myproxy:70")); |
| 5765 | |
| 5766 | const ClientSocketPool::GroupId kGroupId1 = |
| 5767 | TestGroupId("a", 443, ClientSocketPool::SocketType::kSsl); |
| 5768 | const ClientSocketPool::GroupId kGroupId2 = |
| 5769 | TestGroupId("b", 443, ClientSocketPool::SocketType::kSsl); |
| 5770 | const ClientSocketPool::GroupId kGroupId3 = |
| 5771 | TestGroupId("c", 443, ClientSocketPool::SocketType::kSsl); |
| 5772 | |
| 5773 | // Make three sockets in three different groups. The third socket is released |
| 5774 | // to the pool as idle. |
| 5775 | ClientSocketHandle handle1, handle2, handle3; |
| 5776 | TestCompletionCallback callback; |
| 5777 | EXPECT_THAT( |
| 5778 | handle1.Init(kGroupId1, params_, base::nullopt, DEFAULT_PRIORITY, |
| 5779 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5780 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5781 | pool_.get(), NetLogWithSource()), |
| 5782 | IsOk()); |
| 5783 | EXPECT_THAT( |
| 5784 | handle2.Init(kGroupId2, params_, base::nullopt, DEFAULT_PRIORITY, |
| 5785 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5786 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5787 | pool_.get(), NetLogWithSource()), |
| 5788 | IsOk()); |
| 5789 | EXPECT_THAT( |
| 5790 | handle3.Init(kGroupId3, params_, base::nullopt, DEFAULT_PRIORITY, |
| 5791 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5792 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5793 | pool_.get(), NetLogWithSource()), |
| 5794 | IsOk()); |
| 5795 | handle3.Reset(); |
| 5796 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId1)); |
| 5797 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId1)); |
| 5798 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId2)); |
| 5799 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId2)); |
| 5800 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId3)); |
| 5801 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(kGroupId3)); |
| 5802 | |
| 5803 | // Changes to some other proxy do not affect the pool. The idle socket remains |
| 5804 | // alive and closing |handle2| makes the socket available for the pool. |
| 5805 | pool_->OnSSLConfigForServerChanged(HostPortPair("someotherproxy", 70)); |
| 5806 | |
| 5807 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId1)); |
| 5808 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId1)); |
| 5809 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId2)); |
| 5810 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId2)); |
| 5811 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId3)); |
| 5812 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(kGroupId3)); |
| 5813 | |
| 5814 | handle2.Reset(); |
| 5815 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId2)); |
| 5816 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(kGroupId2)); |
| 5817 | |
| 5818 | // Changes to the matching proxy refreshes all groups. |
| 5819 | pool_->OnSSLConfigForServerChanged(HostPortPair("myproxy", 70)); |
| 5820 | |
| 5821 | // Idle sockets are closed. |
| 5822 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5823 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupId2)); |
| 5824 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupId3)); |
| 5825 | |
| 5826 | // The active socket, however, continues to be active. |
| 5827 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId1)); |
| 5828 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId1)); |
| 5829 | |
| 5830 | // Closing it does not make it available for the pool. |
| 5831 | handle1.Reset(); |
| 5832 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5833 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupId1)); |
| 5834 | } |
| 5835 | |
| 5836 | TEST_F(ClientSocketPoolBaseTest, RefreshBothPrivacyAndNormalSockets) { |
| 5837 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 5838 | |
| 5839 | const ClientSocketPool::GroupId kGroupId = |
| 5840 | TestGroupId("a", 443, ClientSocketPool::SocketType::kSsl, |
| 5841 | PrivacyMode::PRIVACY_MODE_DISABLED); |
| 5842 | const ClientSocketPool::GroupId kGroupIdPrivacy = |
| 5843 | TestGroupId("a", 443, ClientSocketPool::SocketType::kSsl, |
| 5844 | PrivacyMode::PRIVACY_MODE_ENABLED); |
| 5845 | const ClientSocketPool::GroupId kOtherGroupId = |
| 5846 | TestGroupId("b", 443, ClientSocketPool::SocketType::kSsl); |
| 5847 | |
| 5848 | // Make a socket in each groups. |
| 5849 | ClientSocketHandle handle1, handle2, handle3; |
| 5850 | TestCompletionCallback callback; |
| 5851 | EXPECT_THAT( |
| 5852 | handle1.Init(kGroupId, params_, base::nullopt, DEFAULT_PRIORITY, |
| 5853 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5854 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5855 | pool_.get(), NetLogWithSource()), |
| 5856 | IsOk()); |
| 5857 | EXPECT_THAT( |
| 5858 | handle2.Init(kGroupIdPrivacy, params_, base::nullopt, DEFAULT_PRIORITY, |
| 5859 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5860 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5861 | pool_.get(), NetLogWithSource()), |
| 5862 | IsOk()); |
| 5863 | EXPECT_THAT( |
| 5864 | handle3.Init(kOtherGroupId, params_, base::nullopt, DEFAULT_PRIORITY, |
| 5865 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5866 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5867 | pool_.get(), NetLogWithSource()), |
| 5868 | IsOk()); |
| 5869 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId)); |
| 5870 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId)); |
| 5871 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupIdPrivacy)); |
| 5872 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupIdPrivacy)); |
| 5873 | ASSERT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 5874 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kOtherGroupId)); |
| 5875 | |
| 5876 | pool_->OnSSLConfigForServerChanged(HostPortPair("a", 443)); |
| 5877 | |
| 5878 | // Active sockets continue to be active. |
| 5879 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId)); |
| 5880 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId)); |
| 5881 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupIdPrivacy)); |
| 5882 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupIdPrivacy)); |
| 5883 | ASSERT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 5884 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kOtherGroupId)); |
| 5885 | |
| 5886 | // Closing them leaves kOtherGroupId alone, but kGroupId and kGroupIdPrivacy |
| 5887 | // are unusable. |
| 5888 | handle1.Reset(); |
| 5889 | handle2.Reset(); |
| 5890 | handle3.Reset(); |
| 5891 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 5892 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupId)); |
| 5893 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupIdPrivacy)); |
| 5894 | EXPECT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 5895 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(kOtherGroupId)); |
| 5896 | } |
| 5897 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 5898 | } // namespace |
| 5899 | |
| 5900 | } // namespace net |