[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" |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 57 | #include "net/test/test_with_scoped_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(); |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 234 | return std::unique_ptr<DatagramClientSocket>(); |
[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( |
Matt Menke | 841fc41 | 2019-03-05 23:20:12 | [diff] [blame] | 248 | std::unique_ptr<StreamSocket> stream_socket, |
[email protected] | 4f4de7e6 | 2010-11-12 19:55:27 | [diff] [blame] | 249 | const HostPortPair& host_and_port, |
[email protected] | 7ab5bbd1 | 2010-10-19 13:33:21 | [diff] [blame] | 250 | const SSLConfig& ssl_config, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 251 | const SSLClientSocketContext& context) override { |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 252 | NOTIMPLEMENTED(); |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 253 | return std::unique_ptr<SSLClientSocket>(); |
[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 */, |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 545 | SSLClientSocketContext(), |
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 | |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 625 | class ClientSocketPoolBaseTest : public TestWithScopedTaskEnvironment { |
[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() |
| 628 | : TestWithScopedTaskEnvironment( |
Gabriel Charette | 29f1e2c6 | 2019-07-15 21:30:35 | [diff] [blame] | 629 | base::test::ScopedTaskEnvironment::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 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 650 | void CreatePoolWithIdleTimeouts(int max_sockets, |
| 651 | int max_sockets_per_group, |
| 652 | base::TimeDelta unused_idle_socket_timeout, |
| 653 | base::TimeDelta used_idle_socket_timeout, |
| 654 | bool enable_backup_connect_jobs = false) { |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 655 | DCHECK(!pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 656 | std::unique_ptr<TestConnectJobFactory> connect_job_factory = |
| 657 | std::make_unique<TestConnectJobFactory>(&client_socket_factory_, |
| 658 | &net_log_); |
| 659 | connect_job_factory_ = connect_job_factory.get(); |
| 660 | pool_ = TransportClientSocketPool::CreateForTesting( |
| 661 | max_sockets, max_sockets_per_group, unused_idle_socket_timeout, |
| 662 | used_idle_socket_timeout, std::move(connect_job_factory), |
| 663 | nullptr /* ssl_config_service */, enable_backup_connect_jobs); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 664 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 665 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 666 | int StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 667 | const ClientSocketPool::GroupId& group_id, |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 668 | RequestPriority priority, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 669 | ClientSocketPool::RespectLimits respect_limits) { |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 670 | return test_base_.StartRequestUsingPool(pool_.get(), group_id, priority, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 671 | respect_limits, params_); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 672 | } |
| 673 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 674 | int StartRequest(const ClientSocketPool::GroupId& group_id, |
| 675 | RequestPriority priority) { |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 676 | return StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 677 | group_id, priority, ClientSocketPool::RespectLimits::ENABLED); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 678 | } |
| 679 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 680 | int GetOrderOfRequest(size_t index) const { |
| 681 | return test_base_.GetOrderOfRequest(index); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 682 | } |
| 683 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 684 | bool ReleaseOneConnection(ClientSocketPoolTest::KeepAlive keep_alive) { |
| 685 | return test_base_.ReleaseOneConnection(keep_alive); |
| 686 | } |
| 687 | |
| 688 | void ReleaseAllConnections(ClientSocketPoolTest::KeepAlive keep_alive) { |
| 689 | test_base_.ReleaseAllConnections(keep_alive); |
| 690 | } |
| 691 | |
| 692 | TestSocketRequest* request(int i) { return test_base_.request(i); } |
| 693 | size_t requests_size() const { return test_base_.requests_size(); } |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 694 | std::vector<std::unique_ptr<TestSocketRequest>>* requests() { |
olli.raula | 9d66b7d | 2015-11-23 08:30:42 | [diff] [blame] | 695 | return test_base_.requests(); |
| 696 | } |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 697 | // Only counts the requests that get sockets asynchronously; |
| 698 | // synchronous completions are not registered by this count. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 699 | size_t completion_count() const { return test_base_.completion_count(); } |
| 700 | |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 701 | TestNetLog net_log_; |
[email protected] | 636b825 | 2011-04-08 19:56:54 | [diff] [blame] | 702 | bool connect_backup_jobs_enabled_; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 703 | MockClientSocketFactory client_socket_factory_; |
[email protected] | 17a0c6c | 2009-08-04 00:07:04 | [diff] [blame] | 704 | TestConnectJobFactory* connect_job_factory_; |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 705 | // These parameters are never actually used to create a TransportConnectJob. |
Matt Menke | 84d11e56 | 2019-03-27 00:11:19 | [diff] [blame] | 706 | scoped_refptr<ClientSocketPool::SocketParams> params_; |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 707 | std::unique_ptr<TransportClientSocketPool> pool_; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 708 | ClientSocketPoolTest test_base_; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 709 | }; |
| 710 | |
Shivani Sharma | 8ae506c | 2019-07-21 21:08:27 | [diff] [blame] | 711 | // TODO(950069): Add testing for frame_origin in NetworkIsolationKey |
| 712 | // using kAppendInitiatingFrameOriginToNetworkIsolationKey. |
| 713 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 714 | TEST_F(ClientSocketPoolBaseTest, BasicSynchronous) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 715 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 716 | |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 717 | TestCompletionCallback callback; |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 718 | ClientSocketHandle handle; |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 719 | BoundTestNetLog log; |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 720 | TestLoadTimingInfoNotConnected(handle); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 721 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 722 | EXPECT_EQ(OK, handle.Init( |
| 723 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 724 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 725 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 726 | pool_.get(), log.bound())); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 727 | EXPECT_TRUE(handle.is_initialized()); |
| 728 | EXPECT_TRUE(handle.socket()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 729 | TestLoadTimingInfoConnectedNotReused(handle); |
| 730 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 731 | handle.Reset(); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 732 | TestLoadTimingInfoNotConnected(handle); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 733 | |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 734 | auto entries = log.GetEntries(); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 735 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 736 | EXPECT_EQ(5u, entries.size()); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 737 | EXPECT_TRUE(LogContainsEvent( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 738 | entries, 0, NetLogEventType::TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 739 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 740 | EXPECT_TRUE(LogContainsBeginEvent(entries, 1, NetLogEventType::SOCKET_POOL)); |
| 741 | EXPECT_TRUE(LogContainsEvent( |
| 742 | entries, 2, NetLogEventType::SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
| 743 | NetLogEventPhase::NONE)); |
| 744 | EXPECT_TRUE(LogContainsEvent(entries, 3, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 745 | NetLogEventType::SOCKET_POOL_BOUND_TO_SOCKET, |
| 746 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 747 | EXPECT_TRUE(LogContainsEndEvent(entries, 4, NetLogEventType::SOCKET_POOL)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 748 | } |
| 749 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 750 | TEST_F(ClientSocketPoolBaseTest, InitConnectionFailure) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 751 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 752 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 753 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 754 | BoundTestNetLog log; |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 755 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 756 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 757 | TestCompletionCallback callback; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 758 | // Set the additional error state members to ensure that they get cleared. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 759 | handle.set_is_ssl_error(true); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 760 | handle.set_ssl_cert_request_info(base::MakeRefCounted<SSLCertRequestInfo>()); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 761 | EXPECT_EQ( |
| 762 | ERR_CONNECTION_FAILED, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 763 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 764 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 765 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 766 | pool_.get(), log.bound())); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 767 | EXPECT_FALSE(handle.socket()); |
| 768 | EXPECT_FALSE(handle.is_ssl_error()); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 769 | EXPECT_FALSE(handle.ssl_cert_request_info()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 770 | TestLoadTimingInfoNotConnected(handle); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 771 | |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 772 | auto entries = log.GetEntries(); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 773 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 774 | EXPECT_EQ(4u, entries.size()); |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 775 | EXPECT_TRUE(LogContainsEvent( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 776 | entries, 0, NetLogEventType::TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 777 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 778 | EXPECT_TRUE(LogContainsBeginEvent(entries, 1, NetLogEventType::SOCKET_POOL)); |
| 779 | EXPECT_TRUE(LogContainsEvent( |
| 780 | entries, 2, NetLogEventType::SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
| 781 | NetLogEventPhase::NONE)); |
| 782 | EXPECT_TRUE(LogContainsEndEvent(entries, 3, NetLogEventType::SOCKET_POOL)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 783 | } |
| 784 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 785 | // Make sure different groups do not share sockets. |
| 786 | TEST_F(ClientSocketPoolBaseTest, GroupSeparation) { |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 787 | base::test::ScopedFeatureList feature_list; |
| 788 | feature_list.InitAndEnableFeature( |
| 789 | features::kPartitionConnectionsByNetworkIsolationKey); |
| 790 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 791 | CreatePool(1000 /* max_sockets */, 2 /* max_sockets_per_group */); |
| 792 | |
| 793 | const HostPortPair kHostPortPairs[] = { |
| 794 | {"a", 80}, |
| 795 | {"a", 443}, |
| 796 | {"b", 80}, |
| 797 | }; |
| 798 | |
| 799 | const ClientSocketPool::SocketType kSocketTypes[] = { |
| 800 | ClientSocketPool::SocketType::kHttp, |
| 801 | ClientSocketPool::SocketType::kSsl, |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 802 | }; |
| 803 | |
Matt Menke | bdf77780 | 2019-04-22 19:38:59 | [diff] [blame] | 804 | const PrivacyMode kPrivacyModes[] = {PrivacyMode::PRIVACY_MODE_DISABLED, |
| 805 | PrivacyMode::PRIVACY_MODE_ENABLED}; |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 806 | |
Shivani Sharma | 8ae506c | 2019-07-21 21:08:27 | [diff] [blame] | 807 | const auto kOriginA = url::Origin::Create(GURL("http://a.test/")); |
| 808 | const auto kOriginB = url::Origin::Create(GURL("http://b.test/")); |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 809 | const NetworkIsolationKey kNetworkIsolationKeys[] = { |
Shivani Sharma | 8ae506c | 2019-07-21 21:08:27 | [diff] [blame] | 810 | NetworkIsolationKey(kOriginA, kOriginA), |
| 811 | NetworkIsolationKey(kOriginB, kOriginB), |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 812 | }; |
| 813 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 814 | int total_idle_sockets = 0; |
| 815 | |
| 816 | // Walk through each GroupId, making sure that requesting a socket for one |
| 817 | // group does not return a previously connected socket for another group. |
| 818 | for (const auto& host_port_pair : kHostPortPairs) { |
| 819 | SCOPED_TRACE(host_port_pair.ToString()); |
| 820 | for (const auto& socket_type : kSocketTypes) { |
| 821 | SCOPED_TRACE(static_cast<int>(socket_type)); |
| 822 | for (const auto& privacy_mode : kPrivacyModes) { |
| 823 | SCOPED_TRACE(privacy_mode); |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 824 | for (const auto& network_isolation_key : kNetworkIsolationKeys) { |
| 825 | SCOPED_TRACE(network_isolation_key.ToString()); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 826 | |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 827 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 828 | |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 829 | ClientSocketPool::GroupId group_id( |
| 830 | host_port_pair, socket_type, privacy_mode, network_isolation_key); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 831 | |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 832 | EXPECT_FALSE(pool_->HasGroupForTesting(group_id)); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 833 | |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 834 | TestCompletionCallback callback; |
| 835 | ClientSocketHandle handle; |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 836 | |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 837 | // Since the group is empty, requesting a socket should not complete |
| 838 | // synchronously. |
| 839 | EXPECT_THAT( |
| 840 | handle.Init(group_id, params_, base::nullopt, DEFAULT_PRIORITY, |
| 841 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 842 | callback.callback(), |
| 843 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 844 | NetLogWithSource()), |
| 845 | IsError(ERR_IO_PENDING)); |
| 846 | EXPECT_TRUE(pool_->HasGroupForTesting(group_id)); |
| 847 | EXPECT_EQ(total_idle_sockets, pool_->IdleSocketCount()); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 848 | |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 849 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 850 | EXPECT_TRUE(handle.socket()); |
| 851 | EXPECT_TRUE(pool_->HasGroupForTesting(group_id)); |
| 852 | EXPECT_EQ(total_idle_sockets, pool_->IdleSocketCount()); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 853 | |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 854 | // Return socket to pool. |
| 855 | handle.Reset(); |
| 856 | EXPECT_EQ(total_idle_sockets + 1, pool_->IdleSocketCount()); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 857 | |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 858 | // Requesting a socket again should return the same socket as before, |
| 859 | // so should complete synchronously. |
| 860 | EXPECT_THAT( |
| 861 | handle.Init(group_id, params_, base::nullopt, DEFAULT_PRIORITY, |
| 862 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 863 | callback.callback(), |
| 864 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 865 | NetLogWithSource()), |
| 866 | IsOk()); |
| 867 | EXPECT_TRUE(handle.socket()); |
| 868 | EXPECT_EQ(total_idle_sockets, pool_->IdleSocketCount()); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 869 | |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 870 | // Return socket to pool again. |
| 871 | handle.Reset(); |
| 872 | EXPECT_EQ(total_idle_sockets + 1, pool_->IdleSocketCount()); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 873 | |
Matt Menke | 166443c | 2019-05-24 18:45:59 | [diff] [blame] | 874 | ++total_idle_sockets; |
| 875 | } |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 876 | } |
| 877 | } |
| 878 | } |
| 879 | } |
| 880 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 881 | TEST_F(ClientSocketPoolBaseTest, TotalLimit) { |
| 882 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 883 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 884 | // TODO(eroman): Check that the NetLog contains this event. |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 885 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 886 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 887 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
| 888 | EXPECT_THAT(StartRequest(TestGroupId("c"), DEFAULT_PRIORITY), IsOk()); |
| 889 | EXPECT_THAT(StartRequest(TestGroupId("d"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 890 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 891 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 892 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 893 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 894 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 895 | EXPECT_THAT(StartRequest(TestGroupId("e"), DEFAULT_PRIORITY), |
| 896 | IsError(ERR_IO_PENDING)); |
| 897 | EXPECT_THAT(StartRequest(TestGroupId("f"), DEFAULT_PRIORITY), |
| 898 | IsError(ERR_IO_PENDING)); |
| 899 | EXPECT_THAT(StartRequest(TestGroupId("g"), DEFAULT_PRIORITY), |
| 900 | IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 901 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 902 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 903 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 904 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 905 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 906 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 907 | |
| 908 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 909 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 910 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 911 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 912 | EXPECT_EQ(5, GetOrderOfRequest(5)); |
| 913 | EXPECT_EQ(6, GetOrderOfRequest(6)); |
| 914 | EXPECT_EQ(7, GetOrderOfRequest(7)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 915 | |
| 916 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 917 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(8)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 918 | } |
| 919 | |
| 920 | TEST_F(ClientSocketPoolBaseTest, TotalLimitReachedNewGroup) { |
| 921 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 922 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 923 | // TODO(eroman): Check that the NetLog contains this event. |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 924 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 925 | // Reach all limits: max total sockets, and max sockets per group. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 926 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 927 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 928 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
| 929 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 930 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 931 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 932 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 933 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 934 | |
| 935 | // Now create a new group and verify that we don't starve it. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 936 | EXPECT_THAT(StartRequest(TestGroupId("c"), DEFAULT_PRIORITY), |
| 937 | IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 938 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 939 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 940 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 941 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 942 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 943 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 944 | |
| 945 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 946 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 947 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 948 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 949 | EXPECT_EQ(5, GetOrderOfRequest(5)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 950 | |
| 951 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 952 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(6)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 953 | } |
| 954 | |
| 955 | TEST_F(ClientSocketPoolBaseTest, TotalLimitRespectsPriority) { |
| 956 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 957 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 958 | EXPECT_THAT(StartRequest(TestGroupId("b"), LOWEST), IsOk()); |
| 959 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsOk()); |
| 960 | EXPECT_THAT(StartRequest(TestGroupId("b"), HIGHEST), IsOk()); |
| 961 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 962 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 963 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 964 | client_socket_factory_.allocation_count()); |
| 965 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 966 | EXPECT_THAT(StartRequest(TestGroupId("c"), LOWEST), IsError(ERR_IO_PENDING)); |
| 967 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 968 | EXPECT_THAT(StartRequest(TestGroupId("b"), HIGHEST), IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 969 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 970 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 971 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 972 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 973 | |
| 974 | // First 4 requests don't have to wait, and finish in order. |
| 975 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 976 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 977 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 978 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 979 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 980 | // Request ("b", HIGHEST) has the highest priority, then (TestGroupId("a"), |
| 981 | // MEDIUM), and then ("c", LOWEST). |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 982 | EXPECT_EQ(7, GetOrderOfRequest(5)); |
| 983 | EXPECT_EQ(6, GetOrderOfRequest(6)); |
| 984 | EXPECT_EQ(5, GetOrderOfRequest(7)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 985 | |
| 986 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 987 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(9)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 988 | } |
| 989 | |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 990 | // Test reprioritizing a request before completion doesn't interfere with |
| 991 | // its completion. |
| 992 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeOne) { |
| 993 | CreatePool(kDefaultMaxSockets, 1); |
| 994 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 995 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 996 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 997 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 998 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 999 | |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 1000 | request(1)->handle()->SetPriority(HIGHEST); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1001 | |
| 1002 | ReleaseOneConnection(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1003 | |
| 1004 | EXPECT_TRUE(request(1)->handle()->socket()); |
| 1005 | } |
| 1006 | |
| 1007 | // Reprioritize a request up past another one and make sure that changes the |
| 1008 | // completion order. |
| 1009 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeUpReorder) { |
| 1010 | CreatePool(kDefaultMaxSockets, 1); |
| 1011 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1012 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1013 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1014 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1015 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1016 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1017 | EXPECT_FALSE(request(2)->handle()->socket()); |
| 1018 | |
| 1019 | request(2)->handle()->SetPriority(HIGHEST); |
| 1020 | |
| 1021 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1022 | |
| 1023 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1024 | EXPECT_EQ(3, GetOrderOfRequest(2)); |
| 1025 | EXPECT_EQ(2, GetOrderOfRequest(3)); |
| 1026 | } |
| 1027 | |
| 1028 | // Reprioritize a request without changing relative priorities and check |
| 1029 | // that the order doesn't change. |
| 1030 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeUpNoReorder) { |
| 1031 | CreatePool(kDefaultMaxSockets, 1); |
| 1032 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1033 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1034 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1035 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1036 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1037 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1038 | EXPECT_FALSE(request(2)->handle()->socket()); |
| 1039 | |
| 1040 | request(2)->handle()->SetPriority(MEDIUM); |
| 1041 | |
| 1042 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1043 | |
| 1044 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1045 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1046 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 1047 | } |
| 1048 | |
| 1049 | // Reprioritize a request past down another one and make sure that changes the |
| 1050 | // completion order. |
| 1051 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeDownReorder) { |
| 1052 | CreatePool(kDefaultMaxSockets, 1); |
| 1053 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1054 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1055 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1056 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1057 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1058 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1059 | EXPECT_FALSE(request(2)->handle()->socket()); |
| 1060 | |
| 1061 | request(1)->handle()->SetPriority(LOW); |
| 1062 | |
| 1063 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1064 | |
| 1065 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1066 | EXPECT_EQ(3, GetOrderOfRequest(2)); |
| 1067 | EXPECT_EQ(2, GetOrderOfRequest(3)); |
| 1068 | } |
| 1069 | |
| 1070 | // Reprioritize a request to the same level as another and confirm it is |
| 1071 | // put after the old request. |
| 1072 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeResetFIFO) { |
| 1073 | CreatePool(kDefaultMaxSockets, 1); |
| 1074 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1075 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1076 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1077 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1078 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1079 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1080 | EXPECT_FALSE(request(2)->handle()->socket()); |
| 1081 | |
| 1082 | request(1)->handle()->SetPriority(MEDIUM); |
| 1083 | |
| 1084 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1085 | |
| 1086 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1087 | EXPECT_EQ(3, GetOrderOfRequest(2)); |
| 1088 | EXPECT_EQ(2, GetOrderOfRequest(3)); |
| 1089 | } |
| 1090 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1091 | TEST_F(ClientSocketPoolBaseTest, TotalLimitRespectsGroupLimit) { |
| 1092 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1093 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1094 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsOk()); |
| 1095 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsOk()); |
| 1096 | EXPECT_THAT(StartRequest(TestGroupId("b"), HIGHEST), IsOk()); |
| 1097 | EXPECT_THAT(StartRequest(TestGroupId("b"), MEDIUM), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1098 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1099 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1100 | client_socket_factory_.allocation_count()); |
| 1101 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1102 | EXPECT_THAT(StartRequest(TestGroupId("c"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1103 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 1104 | EXPECT_THAT(StartRequest(TestGroupId("b"), HIGHEST), IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1105 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1106 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1107 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1108 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1109 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1110 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1111 | |
| 1112 | // First 4 requests don't have to wait, and finish in order. |
| 1113 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1114 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1115 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 1116 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 1117 | |
| 1118 | // Request ("b", 7) has the highest priority, but we can't make new socket for |
| 1119 | // group "b", because it has reached the per-group limit. Then we make |
| 1120 | // socket for ("c", 6), because it has higher priority than ("a", 4), |
| 1121 | // and we still can't make a socket for group "b". |
| 1122 | EXPECT_EQ(5, GetOrderOfRequest(5)); |
| 1123 | EXPECT_EQ(6, GetOrderOfRequest(6)); |
| 1124 | EXPECT_EQ(7, GetOrderOfRequest(7)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1125 | |
| 1126 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1127 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(8)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1128 | } |
| 1129 | |
| 1130 | // Make sure that we count connecting sockets against the total limit. |
| 1131 | TEST_F(ClientSocketPoolBaseTest, TotalLimitCountsConnectingSockets) { |
| 1132 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1133 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1134 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1135 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
| 1136 | EXPECT_THAT(StartRequest(TestGroupId("c"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1137 | |
| 1138 | // Create one asynchronous request. |
| 1139 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1140 | EXPECT_THAT(StartRequest(TestGroupId("d"), DEFAULT_PRIORITY), |
| 1141 | IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1142 | |
[email protected] | 6b17538 | 2009-10-13 06:47:47 | [diff] [blame] | 1143 | // We post all of our delayed tasks with a 2ms delay. I.e. they don't |
| 1144 | // actually become pending until 2ms after they have been created. In order |
| 1145 | // to flush all tasks, we need to wait so that we know there are no |
| 1146 | // soon-to-be-pending tasks waiting. |
Alex Clarke | 0def209 | 2018-12-10 12:01:45 | [diff] [blame] | 1147 | FastForwardBy(base::TimeDelta::FromMilliseconds(10)); |
[email protected] | 6b17538 | 2009-10-13 06:47:47 | [diff] [blame] | 1148 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1149 | // The next synchronous request should wait for its turn. |
| 1150 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1151 | EXPECT_THAT(StartRequest(TestGroupId("e"), DEFAULT_PRIORITY), |
| 1152 | IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1153 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1154 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1155 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1156 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1157 | client_socket_factory_.allocation_count()); |
| 1158 | |
| 1159 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1160 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1161 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 1162 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1163 | EXPECT_EQ(5, GetOrderOfRequest(5)); |
| 1164 | |
| 1165 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1166 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(6)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1167 | } |
| 1168 | |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1169 | TEST_F(ClientSocketPoolBaseTest, CorrectlyCountStalledGroups) { |
| 1170 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 1171 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 1172 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1173 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1174 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1175 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1176 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1177 | |
| 1178 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 1179 | |
| 1180 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1181 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1182 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), |
| 1183 | IsError(ERR_IO_PENDING)); |
| 1184 | EXPECT_THAT(StartRequest(TestGroupId("c"), DEFAULT_PRIORITY), |
| 1185 | IsError(ERR_IO_PENDING)); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1186 | |
| 1187 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1188 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1189 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1190 | EXPECT_EQ(kDefaultMaxSockets + 1, client_socket_factory_.allocation_count()); |
[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 + 2, client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1193 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
| 1194 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1195 | EXPECT_EQ(kDefaultMaxSockets + 2, client_socket_factory_.allocation_count()); |
| 1196 | } |
| 1197 | |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 1198 | TEST_F(ClientSocketPoolBaseTest, StallAndThenCancelAndTriggerAvailableSocket) { |
| 1199 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 1200 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1201 | |
| 1202 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1203 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1204 | EXPECT_EQ( |
| 1205 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1206 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1207 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1208 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1209 | pool_.get(), NetLogWithSource())); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 1210 | |
| 1211 | ClientSocketHandle handles[4]; |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 1212 | for (size_t i = 0; i < base::size(handles); ++i) { |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1213 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1214 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1215 | handles[i].Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1216 | TestGroupId("b"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1217 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1218 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1219 | pool_.get(), NetLogWithSource())); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 1220 | } |
| 1221 | |
| 1222 | // One will be stalled, cancel all the handles now. |
| 1223 | // This should hit the OnAvailableSocketSlot() code where we previously had |
| 1224 | // stalled groups, but no longer have any. |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 1225 | for (size_t i = 0; i < base::size(handles); ++i) |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 1226 | handles[i].Reset(); |
| 1227 | } |
| 1228 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1229 | TEST_F(ClientSocketPoolBaseTest, CancelStalledSocketAtSocketLimit) { |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1230 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1231 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 1232 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1233 | { |
| 1234 | ClientSocketHandle handles[kDefaultMaxSockets]; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1235 | TestCompletionCallback callbacks[kDefaultMaxSockets]; |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1236 | for (int i = 0; i < kDefaultMaxSockets; ++i) { |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1237 | EXPECT_EQ(OK, |
| 1238 | handles[i].Init(TestGroupId(base::NumberToString(i)), params_, |
| 1239 | base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
| 1240 | ClientSocketPool::RespectLimits::ENABLED, |
| 1241 | callbacks[i].callback(), |
| 1242 | ClientSocketPool::ProxyAuthCallback(), |
| 1243 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1244 | } |
| 1245 | |
| 1246 | // Force a stalled group. |
| 1247 | ClientSocketHandle stalled_handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1248 | TestCompletionCallback callback; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1249 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1250 | stalled_handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1251 | TestGroupId("foo"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1252 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1253 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1254 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1255 | |
| 1256 | // Cancel the stalled request. |
| 1257 | stalled_handle.Reset(); |
| 1258 | |
| 1259 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1260 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 1261 | |
| 1262 | // Dropping out of scope will close all handles and return them to idle. |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1263 | } |
| 1264 | |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1265 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1266 | EXPECT_EQ(kDefaultMaxSockets, pool_->IdleSocketCount()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1267 | } |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1268 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1269 | TEST_F(ClientSocketPoolBaseTest, CancelPendingSocketAtSocketLimit) { |
| 1270 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1271 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 1272 | |
| 1273 | { |
| 1274 | ClientSocketHandle handles[kDefaultMaxSockets]; |
| 1275 | for (int i = 0; i < kDefaultMaxSockets; ++i) { |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1276 | TestCompletionCallback callback; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1277 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1278 | handles[i].Init(TestGroupId(base::NumberToString(i)), params_, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1279 | base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1280 | ClientSocketPool::RespectLimits::ENABLED, |
| 1281 | callback.callback(), |
| 1282 | ClientSocketPool::ProxyAuthCallback(), |
| 1283 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1284 | } |
| 1285 | |
| 1286 | // Force a stalled group. |
| 1287 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1288 | ClientSocketHandle stalled_handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1289 | TestCompletionCallback callback; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1290 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1291 | stalled_handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1292 | TestGroupId("foo"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1293 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1294 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1295 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1296 | |
| 1297 | // Since it is stalled, it should have no connect jobs. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1298 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("foo"))); |
| 1299 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 1300 | TestGroupId("foo"))); |
| 1301 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroupForTesting( |
| 1302 | TestGroupId("foo"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1303 | |
| 1304 | // Cancel the stalled request. |
| 1305 | handles[0].Reset(); |
| 1306 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1307 | // Now we should have a connect job. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1308 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("foo"))); |
| 1309 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 1310 | TestGroupId("foo"))); |
| 1311 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroupForTesting( |
| 1312 | TestGroupId("foo"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1313 | |
| 1314 | // The stalled socket should connect. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1315 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1316 | |
| 1317 | EXPECT_EQ(kDefaultMaxSockets + 1, |
| 1318 | client_socket_factory_.allocation_count()); |
| 1319 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1320 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("foo"))); |
| 1321 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 1322 | TestGroupId("foo"))); |
| 1323 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroupForTesting( |
| 1324 | TestGroupId("foo"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1325 | |
| 1326 | // Dropping out of scope will close all handles and return them to idle. |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1327 | } |
| 1328 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1329 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 1330 | } |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1331 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1332 | TEST_F(ClientSocketPoolBaseTest, WaitForStalledSocketAtSocketLimit) { |
| 1333 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1334 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1335 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1336 | ClientSocketHandle stalled_handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1337 | TestCompletionCallback callback; |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1338 | { |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 1339 | EXPECT_FALSE(pool_->IsStalled()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1340 | ClientSocketHandle handles[kDefaultMaxSockets]; |
| 1341 | for (int i = 0; i < kDefaultMaxSockets; ++i) { |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1342 | TestCompletionCallback callback; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1343 | EXPECT_EQ( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1344 | OK, handles[i].Init( |
| 1345 | TestGroupId(base::StringPrintf("Take 2: %d", i)), params_, |
| 1346 | base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
| 1347 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1348 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 1349 | NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1350 | } |
| 1351 | |
| 1352 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1353 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 1354 | EXPECT_FALSE(pool_->IsStalled()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1355 | |
| 1356 | // Now we will hit the socket limit. |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1357 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1358 | stalled_handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1359 | TestGroupId("foo"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1360 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1361 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1362 | pool_.get(), NetLogWithSource())); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 1363 | EXPECT_TRUE(pool_->IsStalled()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1364 | |
| 1365 | // Dropping out of scope will close all handles and return them to idle. |
| 1366 | } |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1367 | |
| 1368 | // But if we wait for it, the released idle sockets will be closed in |
| 1369 | // preference of the waiting request. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1370 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1371 | |
| 1372 | EXPECT_EQ(kDefaultMaxSockets + 1, client_socket_factory_.allocation_count()); |
| 1373 | EXPECT_EQ(3, pool_->IdleSocketCount()); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1374 | } |
| 1375 | |
| 1376 | // Regression test for http://crbug.com/40952. |
| 1377 | TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketAtSocketLimitDeleteGroup) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1378 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 1379 | true /* enable_backup_connect_jobs */); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1380 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 1381 | |
| 1382 | for (int i = 0; i < kDefaultMaxSockets; ++i) { |
| 1383 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1384 | TestCompletionCallback callback; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1385 | EXPECT_EQ(OK, handle.Init(TestGroupId(base::NumberToString(i)), params_, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1386 | base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1387 | ClientSocketPool::RespectLimits::ENABLED, |
| 1388 | callback.callback(), |
| 1389 | ClientSocketPool::ProxyAuthCallback(), |
| 1390 | pool_.get(), NetLogWithSource())); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1391 | } |
| 1392 | |
| 1393 | // Flush all the DoReleaseSocket tasks. |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 1394 | base::RunLoop().RunUntilIdle(); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1395 | |
| 1396 | // Stall a group. Set a pending job so it'll trigger a backup job if we don't |
| 1397 | // reuse a socket. |
| 1398 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1399 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1400 | TestCompletionCallback callback; |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1401 | |
| 1402 | // "0" is special here, since it should be the first entry in the sorted map, |
| 1403 | // which is the one which we would close an idle socket for. We shouldn't |
| 1404 | // close an idle socket though, since we should reuse the idle socket. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1405 | EXPECT_EQ(OK, handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1406 | TestGroupId("0"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1407 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1408 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1409 | pool_.get(), NetLogWithSource())); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1410 | |
| 1411 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1412 | EXPECT_EQ(kDefaultMaxSockets - 1, pool_->IdleSocketCount()); |
| 1413 | } |
| 1414 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1415 | TEST_F(ClientSocketPoolBaseTest, PendingRequests) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1416 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1417 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1418 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1419 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1420 | EXPECT_THAT(StartRequest(TestGroupId("a"), IDLE), IsError(ERR_IO_PENDING)); |
| 1421 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
| 1422 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1423 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1424 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 1425 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1426 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1427 | ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1428 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
| 1429 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1430 | EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup, |
| 1431 | completion_count()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1432 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1433 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1434 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
[email protected] | c9c6f5c | 2010-07-31 01:30:03 | [diff] [blame] | 1435 | EXPECT_EQ(8, GetOrderOfRequest(3)); |
| 1436 | EXPECT_EQ(6, GetOrderOfRequest(4)); |
| 1437 | EXPECT_EQ(4, GetOrderOfRequest(5)); |
| 1438 | EXPECT_EQ(3, GetOrderOfRequest(6)); |
| 1439 | EXPECT_EQ(5, GetOrderOfRequest(7)); |
| 1440 | EXPECT_EQ(7, GetOrderOfRequest(8)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1441 | |
| 1442 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1443 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(9)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1444 | } |
| 1445 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1446 | TEST_F(ClientSocketPoolBaseTest, PendingRequests_NoKeepAlive) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1447 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1448 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1449 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1450 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1451 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
| 1452 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1453 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1454 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 1455 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1456 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1457 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1458 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1459 | for (size_t i = kDefaultMaxSocketsPerGroup; i < requests_size(); ++i) |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1460 | EXPECT_THAT(request(i)->WaitForResult(), IsOk()); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1461 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1462 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1463 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1464 | EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup, |
| 1465 | completion_count()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1466 | } |
| 1467 | |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1468 | TEST_F(ClientSocketPoolBaseTest, ResetAndCloseSocket) { |
| 1469 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1470 | |
| 1471 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1472 | ClientSocketHandle handle; |
| 1473 | TestCompletionCallback callback; |
| 1474 | EXPECT_EQ( |
| 1475 | ERR_IO_PENDING, |
| 1476 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1477 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1478 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1479 | pool_.get(), NetLogWithSource())); |
| 1480 | |
| 1481 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 1482 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1483 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1484 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
| 1485 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 1486 | |
| 1487 | handle.ResetAndCloseSocket(); |
| 1488 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1489 | } |
| 1490 | |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1491 | // This test will start up a socket request and then call Reset() on the handle. |
| 1492 | // The pending ConnectJob should not be destroyed. |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1493 | TEST_F(ClientSocketPoolBaseTest, CancelRequestKeepsConnectJob) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1494 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1495 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1496 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1497 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1498 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1499 | EXPECT_EQ( |
| 1500 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1501 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1502 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1503 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1504 | pool_.get(), NetLogWithSource())); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1505 | handle.Reset(); |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1506 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1507 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1508 | } |
| 1509 | |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1510 | // This test will start up a socket request and then call ResetAndCloseSocket() |
| 1511 | // on the handle. The pending ConnectJob or connected socket should be |
| 1512 | // destroyed. |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1513 | TEST_F(ClientSocketPoolBaseTest, CancelRequestAndCloseSocket) { |
| 1514 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1515 | |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1516 | // When true, the socket connects before it's canceled. |
| 1517 | for (bool cancel_when_callback_pending : {false, true}) { |
| 1518 | if (cancel_when_callback_pending) { |
| 1519 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 1520 | } else { |
| 1521 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1522 | } |
| 1523 | ClientSocketHandle handle; |
| 1524 | TestCompletionCallback callback; |
| 1525 | EXPECT_EQ( |
| 1526 | ERR_IO_PENDING, |
| 1527 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1528 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1529 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1530 | pool_.get(), NetLogWithSource())); |
| 1531 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1532 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1533 | |
| 1534 | if (cancel_when_callback_pending) { |
| 1535 | client_socket_factory_.SignalJobs(); |
| 1536 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1537 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 1538 | } |
| 1539 | |
| 1540 | handle.ResetAndCloseSocket(); |
| 1541 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1542 | } |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1543 | } |
| 1544 | |
| 1545 | TEST_F(ClientSocketPoolBaseTest, |
| 1546 | CancelRequestAndCloseSocketWhenMoreRequestsThanConnectJobs) { |
| 1547 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1548 | |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1549 | // When true, the sockets connect before they're canceled. |
| 1550 | for (bool cancel_when_callback_pending : {false, true}) { |
| 1551 | if (cancel_when_callback_pending) { |
| 1552 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 1553 | } else { |
| 1554 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1555 | } |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1556 | |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1557 | std::vector<std::unique_ptr<ClientSocketHandle>> handles; |
| 1558 | TestCompletionCallback callback; |
| 1559 | // Make |kDefaultMaxSockets + 1| socket requests. |
| 1560 | for (int i = 0; i < kDefaultMaxSocketsPerGroup + 1; ++i) { |
| 1561 | std::unique_ptr<ClientSocketHandle> handle = |
| 1562 | std::make_unique<ClientSocketHandle>(); |
| 1563 | EXPECT_EQ(ERR_IO_PENDING, |
| 1564 | handle->Init( |
| 1565 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1566 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1567 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1568 | pool_.get(), NetLogWithSource())); |
| 1569 | handles.push_back(std::move(handle)); |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1570 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1571 | EXPECT_EQ( |
| 1572 | static_cast<size_t>(std::min(i + 1, kDefaultMaxSocketsPerGroup)), |
| 1573 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1574 | } |
| 1575 | |
| 1576 | if (cancel_when_callback_pending) { |
| 1577 | client_socket_factory_.SignalJobs(); |
| 1578 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1579 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
| 1580 | pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 1581 | } |
| 1582 | |
| 1583 | // Calling ResetAndCloseSocket() on a handle should not cancel a ConnectJob |
| 1584 | // or close a socket, since there are more requests than ConnectJobs or |
| 1585 | // sockets. |
| 1586 | handles[kDefaultMaxSocketsPerGroup]->ResetAndCloseSocket(); |
| 1587 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1588 | if (cancel_when_callback_pending) { |
| 1589 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
| 1590 | pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 1591 | } else { |
| 1592 | EXPECT_EQ(static_cast<size_t>(kDefaultMaxSocketsPerGroup), |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1593 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1594 | } |
Matt Menke | 99251ea4 | 2019-04-25 22:59:02 | [diff] [blame] | 1595 | |
| 1596 | // Calling ResetAndCloseSocket() on other handles should cancel a ConnectJob |
| 1597 | // or close a socket. |
| 1598 | for (int i = kDefaultMaxSocketsPerGroup - 1; i >= 0; --i) { |
| 1599 | handles[i]->ResetAndCloseSocket(); |
| 1600 | if (i > 0) { |
| 1601 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1602 | if (cancel_when_callback_pending) { |
| 1603 | EXPECT_EQ(i, |
| 1604 | pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 1605 | } else { |
| 1606 | EXPECT_EQ(static_cast<size_t>(i), |
| 1607 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1608 | } |
| 1609 | } else { |
| 1610 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1611 | } |
| 1612 | } |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1613 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1614 | } |
| 1615 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1616 | TEST_F(ClientSocketPoolBaseTest, ConnectCancelConnect) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1617 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1618 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1619 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 1620 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1621 | TestCompletionCallback callback; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1622 | |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1623 | EXPECT_EQ( |
| 1624 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1625 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1626 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1627 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1628 | pool_.get(), NetLogWithSource())); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1629 | |
| 1630 | handle.Reset(); |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1631 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1632 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1633 | |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1634 | // This will create a second ConnectJob, since the other ConnectJob was |
| 1635 | // previously assigned to a request. |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1636 | TestCompletionCallback callback2; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1637 | EXPECT_EQ( |
| 1638 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1639 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1640 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1641 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1642 | pool_.get(), NetLogWithSource())); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1643 | |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1644 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1645 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1646 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1647 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1648 | EXPECT_FALSE(callback.have_result()); |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1649 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1650 | // One ConnectJob completed, and its socket is now assigned to |handle|. |
| 1651 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 1652 | // The other ConnectJob should have either completed, or still be connecting. |
| 1653 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")) + |
| 1654 | pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1655 | |
| 1656 | handle.Reset(); |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1657 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1658 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")) + |
| 1659 | pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
| 1660 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1661 | } |
| 1662 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1663 | TEST_F(ClientSocketPoolBaseTest, CancelRequest) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1664 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1665 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1666 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1667 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1668 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
| 1669 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1670 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1671 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 1672 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1673 | |
| 1674 | // Cancel a request. |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1675 | size_t index_to_cancel = kDefaultMaxSocketsPerGroup + 2; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1676 | EXPECT_FALSE((*requests())[index_to_cancel]->handle()->is_initialized()); |
| 1677 | (*requests())[index_to_cancel]->handle()->Reset(); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1678 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1679 | ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1680 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1681 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
| 1682 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1683 | EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup - 1, |
| 1684 | completion_count()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1685 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1686 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1687 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1688 | EXPECT_EQ(5, GetOrderOfRequest(3)); |
| 1689 | EXPECT_EQ(3, GetOrderOfRequest(4)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1690 | EXPECT_EQ(ClientSocketPoolTest::kRequestNotFound, |
| 1691 | GetOrderOfRequest(5)); // Canceled request. |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1692 | EXPECT_EQ(4, GetOrderOfRequest(6)); |
| 1693 | EXPECT_EQ(6, GetOrderOfRequest(7)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1694 | |
| 1695 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1696 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(8)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1697 | } |
| 1698 | |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1699 | // Function to be used as a callback on socket request completion. It first |
| 1700 | // disconnects the successfully connected socket from the first request, and |
| 1701 | // then reuses the ClientSocketHandle to request another socket. |
| 1702 | // |
| 1703 | // |nested_callback| is called with the result of the second socket request. |
| 1704 | void RequestSocketOnComplete(ClientSocketHandle* handle, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1705 | TransportClientSocketPool* pool, |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1706 | TestConnectJobFactory* test_connect_job_factory, |
| 1707 | TestConnectJob::JobType next_job_type, |
Bence Béky | a4a5093 | 2018-08-10 13:39:41 | [diff] [blame] | 1708 | TestCompletionCallback* nested_callback, |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1709 | int first_request_result) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1710 | EXPECT_THAT(first_request_result, IsOk()); |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1711 | |
| 1712 | test_connect_job_factory->set_job_type(next_job_type); |
| 1713 | |
| 1714 | // Don't allow reuse of the socket. Disconnect it and then release it. |
| 1715 | if (handle->socket()) |
| 1716 | handle->socket()->Disconnect(); |
| 1717 | handle->Reset(); |
| 1718 | |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1719 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1720 | int rv = handle->Init( |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 1721 | TestGroupId("a"), |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1722 | ClientSocketPool::SocketParams::CreateForHttpForTesting(), base::nullopt, |
| 1723 | LOWEST, SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 1724 | nested_callback->callback(), ClientSocketPool::ProxyAuthCallback(), pool, |
| 1725 | NetLogWithSource()); |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1726 | if (rv != ERR_IO_PENDING) { |
| 1727 | DCHECK_EQ(TestConnectJob::kMockJob, next_job_type); |
Bence Béky | a4a5093 | 2018-08-10 13:39:41 | [diff] [blame] | 1728 | nested_callback->callback().Run(rv); |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1729 | } else { |
| 1730 | DCHECK_EQ(TestConnectJob::kMockPendingJob, next_job_type); |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1731 | } |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1732 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1733 | |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1734 | // Tests the case where a second socket is requested in a completion callback, |
| 1735 | // and the second socket connects asynchronously. Reuses the same |
| 1736 | // ClientSocketHandle for the second socket, after disconnecting the first. |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1737 | TEST_F(ClientSocketPoolBaseTest, RequestPendingJobTwice) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1738 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1739 | |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 1740 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 1741 | ClientSocketHandle handle; |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1742 | TestCompletionCallback second_result_callback; |
| 1743 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1744 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Paul Jensen | 8d6f87ec | 2018-01-13 00:46:54 | [diff] [blame] | 1745 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1746 | base::BindOnce(&RequestSocketOnComplete, &handle, pool_.get(), |
| 1747 | connect_job_factory_, TestConnectJob::kMockPendingJob, |
| 1748 | &second_result_callback), |
| 1749 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1750 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1751 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1752 | EXPECT_THAT(second_result_callback.WaitForResult(), IsOk()); |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1753 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1754 | |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1755 | // Tests the case where a second socket is requested in a completion callback, |
| 1756 | // and the second socket connects synchronously. Reuses the same |
| 1757 | // ClientSocketHandle for the second socket, after disconnecting the first. |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1758 | TEST_F(ClientSocketPoolBaseTest, RequestPendingJobThenSynchronous) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1759 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1760 | |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 1761 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 1762 | ClientSocketHandle handle; |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1763 | TestCompletionCallback second_result_callback; |
| 1764 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1765 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Paul Jensen | 8d6f87ec | 2018-01-13 00:46:54 | [diff] [blame] | 1766 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1767 | base::BindOnce(&RequestSocketOnComplete, &handle, pool_.get(), |
| 1768 | connect_job_factory_, TestConnectJob::kMockPendingJob, |
| 1769 | &second_result_callback), |
| 1770 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1771 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1772 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1773 | EXPECT_THAT(second_result_callback.WaitForResult(), IsOk()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1774 | } |
| 1775 | |
| 1776 | // Make sure that pending requests get serviced after active requests get |
| 1777 | // cancelled. |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1778 | TEST_F(ClientSocketPoolBaseTest, CancelActiveRequestWithPendingRequests) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1779 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1780 | |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 1781 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1782 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1783 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1784 | IsError(ERR_IO_PENDING)); |
| 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)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1797 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1798 | // Now, kDefaultMaxSocketsPerGroup requests should be active. |
| 1799 | // Let's cancel them. |
| 1800 | for (int i = 0; i < kDefaultMaxSocketsPerGroup; ++i) { |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1801 | ASSERT_FALSE(request(i)->handle()->is_initialized()); |
| 1802 | request(i)->handle()->Reset(); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1803 | } |
| 1804 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1805 | // Let's wait for the rest to complete now. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1806 | for (size_t i = kDefaultMaxSocketsPerGroup; i < requests_size(); ++i) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1807 | EXPECT_THAT(request(i)->WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1808 | request(i)->handle()->Reset(); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1809 | } |
| 1810 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1811 | EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup, |
| 1812 | completion_count()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1813 | } |
| 1814 | |
| 1815 | // Make sure that pending requests get serviced after active requests fail. |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1816 | TEST_F(ClientSocketPoolBaseTest, FailingActiveRequestWithPendingRequests) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1817 | const size_t kMaxSockets = 5; |
| 1818 | CreatePool(kMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1819 | |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 1820 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1821 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1822 | const size_t kNumberOfRequests = 2 * kDefaultMaxSocketsPerGroup + 1; |
| 1823 | ASSERT_LE(kNumberOfRequests, kMaxSockets); // Otherwise the test will hang. |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1824 | |
| 1825 | // Queue up all the requests |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1826 | for (size_t i = 0; i < kNumberOfRequests; ++i) |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1827 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1828 | IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1829 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1830 | for (size_t i = 0; i < kNumberOfRequests; ++i) |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1831 | EXPECT_THAT(request(i)->WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1832 | } |
| 1833 | |
mmenke | 9d72fe4 | 2017-05-18 22:36:07 | [diff] [blame] | 1834 | // Make sure that pending requests that complete synchronously get serviced |
| 1835 | // after active requests fail. See https://crbug.com/723748 |
| 1836 | TEST_F(ClientSocketPoolBaseTest, HandleMultipleSyncFailuresAfterAsyncFailure) { |
| 1837 | const size_t kNumberOfRequests = 10; |
| 1838 | const size_t kMaxSockets = 1; |
| 1839 | CreatePool(kMaxSockets, kMaxSockets); |
| 1840 | |
| 1841 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
| 1842 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1843 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1844 | IsError(ERR_IO_PENDING)); |
mmenke | 9d72fe4 | 2017-05-18 22:36:07 | [diff] [blame] | 1845 | |
| 1846 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
| 1847 | |
| 1848 | // Queue up all the other requests |
| 1849 | for (size_t i = 1; i < kNumberOfRequests; ++i) |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1850 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1851 | IsError(ERR_IO_PENDING)); |
mmenke | 9d72fe4 | 2017-05-18 22:36:07 | [diff] [blame] | 1852 | |
| 1853 | // Make sure all requests fail, instead of hanging. |
| 1854 | for (size_t i = 0; i < kNumberOfRequests; ++i) |
| 1855 | EXPECT_THAT(request(i)->WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 1856 | } |
| 1857 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1858 | TEST_F(ClientSocketPoolBaseTest, CancelActiveRequestThenRequestSocket) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1859 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1860 | |
| 1861 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1862 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1863 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1864 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1865 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1866 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1867 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1868 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1869 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1870 | |
| 1871 | // Cancel the active request. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1872 | handle.Reset(); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1873 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1874 | rv = handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1875 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1876 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1877 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1878 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 1879 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1880 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1881 | EXPECT_FALSE(handle.is_reused()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 1882 | TestLoadTimingInfoConnectedNotReused(handle); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1883 | EXPECT_EQ(2, client_socket_factory_.allocation_count()); |
| 1884 | } |
| 1885 | |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1886 | TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketsForced) { |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1887 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1888 | ClientSocketHandle handle; |
| 1889 | TestCompletionCallback callback; |
| 1890 | BoundTestNetLog log; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1891 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1892 | TestGroupId("a"), params_, base::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1893 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1894 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1895 | EXPECT_THAT(rv, IsOk()); |
| 1896 | handle.Reset(); |
| 1897 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 1898 | pool_->CloseIdleSockets(); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1899 | } |
| 1900 | |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1901 | TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketsInGroupForced) { |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1902 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1903 | TestCompletionCallback callback; |
| 1904 | BoundTestNetLog log; |
| 1905 | ClientSocketHandle handle1; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1906 | int rv = handle1.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1907 | TestGroupId("a"), params_, base::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1908 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1909 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1910 | EXPECT_THAT(rv, IsOk()); |
| 1911 | ClientSocketHandle handle2; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1912 | rv = handle2.Init(TestGroupId("a"), params_, base::nullopt, LOWEST, |
| 1913 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1914 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1915 | pool_.get(), log.bound()); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1916 | ClientSocketHandle handle3; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1917 | rv = handle3.Init(TestGroupId("b"), params_, base::nullopt, LOWEST, |
| 1918 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1919 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1920 | pool_.get(), log.bound()); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1921 | EXPECT_THAT(rv, IsOk()); |
| 1922 | handle1.Reset(); |
| 1923 | handle2.Reset(); |
| 1924 | handle3.Reset(); |
| 1925 | EXPECT_EQ(3, pool_->IdleSocketCount()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1926 | pool_->CloseIdleSocketsInGroup(TestGroupId("a")); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1927 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1928 | } |
| 1929 | |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1930 | TEST_F(ClientSocketPoolBaseTest, CleanUpUnusableIdleSockets) { |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1931 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1932 | ClientSocketHandle handle; |
| 1933 | TestCompletionCallback callback; |
| 1934 | BoundTestNetLog log; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1935 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1936 | TestGroupId("a"), params_, base::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1937 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1938 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1939 | EXPECT_THAT(rv, IsOk()); |
| 1940 | StreamSocket* socket = handle.socket(); |
| 1941 | handle.Reset(); |
| 1942 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 1943 | |
| 1944 | // Disconnect socket now to make the socket unusable. |
| 1945 | socket->Disconnect(); |
| 1946 | ClientSocketHandle handle2; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1947 | rv = handle2.Init(TestGroupId("a"), params_, base::nullopt, LOWEST, |
| 1948 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1949 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1950 | pool_.get(), log.bound()); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1951 | EXPECT_THAT(rv, IsOk()); |
| 1952 | EXPECT_FALSE(handle2.is_reused()); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1953 | } |
| 1954 | |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1955 | // Regression test for http://crbug.com/17985. |
| 1956 | TEST_F(ClientSocketPoolBaseTest, GroupWithPendingRequestsIsNotEmpty) { |
| 1957 | const int kMaxSockets = 3; |
| 1958 | const int kMaxSocketsPerGroup = 2; |
| 1959 | CreatePool(kMaxSockets, kMaxSocketsPerGroup); |
| 1960 | |
[email protected] | ac790b4 | 2009-12-02 04:31:31 | [diff] [blame] | 1961 | const RequestPriority kHighPriority = HIGHEST; |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1962 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1963 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1964 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1965 | |
| 1966 | // This is going to be a pending request in an otherwise empty group. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1967 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1968 | IsError(ERR_IO_PENDING)); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1969 | |
| 1970 | // Reach the maximum socket limit. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1971 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1972 | |
| 1973 | // Create a stalled group with high priorities. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1974 | EXPECT_THAT(StartRequest(TestGroupId("c"), kHighPriority), |
| 1975 | IsError(ERR_IO_PENDING)); |
| 1976 | EXPECT_THAT(StartRequest(TestGroupId("c"), kHighPriority), |
| 1977 | IsError(ERR_IO_PENDING)); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1978 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1979 | // Release the first two sockets from TestGroupId("a"). Because this is a |
| 1980 | // keepalive, the first release will unblock the pending request for |
| 1981 | // TestGroupId("a"). The second release will unblock a request for "c", |
| 1982 | // because it is the next high priority socket. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1983 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
| 1984 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1985 | |
| 1986 | // Closing idle sockets should not get us into trouble, but in the bug |
| 1987 | // we were hitting a CHECK here. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1988 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1989 | pool_->CloseIdleSockets(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1990 | |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 1991 | // Run the released socket wakeups. |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 1992 | base::RunLoop().RunUntilIdle(); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1993 | } |
| 1994 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 1995 | TEST_F(ClientSocketPoolBaseTest, BasicAsynchronous) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1996 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1997 | |
| 1998 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1999 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2000 | TestCompletionCallback callback; |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 2001 | BoundTestNetLog log; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2002 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2003 | TestGroupId("a"), params_, base::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2004 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2005 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2006 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2007 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2008 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 2009 | TestLoadTimingInfoNotConnected(handle); |
| 2010 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2011 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2012 | EXPECT_TRUE(handle.is_initialized()); |
| 2013 | EXPECT_TRUE(handle.socket()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 2014 | TestLoadTimingInfoConnectedNotReused(handle); |
| 2015 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2016 | handle.Reset(); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 2017 | TestLoadTimingInfoNotConnected(handle); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 2018 | |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 2019 | auto entries = log.GetEntries(); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 2020 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2021 | EXPECT_EQ(5u, entries.size()); |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 2022 | EXPECT_TRUE(LogContainsEvent( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2023 | entries, 0, NetLogEventType::TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 2024 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2025 | EXPECT_TRUE(LogContainsBeginEvent(entries, 1, NetLogEventType::SOCKET_POOL)); |
| 2026 | EXPECT_TRUE(LogContainsEvent( |
| 2027 | entries, 2, NetLogEventType::SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
| 2028 | NetLogEventPhase::NONE)); |
| 2029 | EXPECT_TRUE(LogContainsEvent(entries, 3, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 2030 | NetLogEventType::SOCKET_POOL_BOUND_TO_SOCKET, |
| 2031 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2032 | EXPECT_TRUE(LogContainsEndEvent(entries, 4, NetLogEventType::SOCKET_POOL)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2033 | } |
| 2034 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2035 | TEST_F(ClientSocketPoolBaseTest, |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2036 | InitConnectionAsynchronousFailure) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2037 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2038 | |
| 2039 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2040 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2041 | TestCompletionCallback callback; |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 2042 | BoundTestNetLog log; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2043 | // Set the additional error state members to ensure that they get cleared. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2044 | handle.set_is_ssl_error(true); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 2045 | handle.set_ssl_cert_request_info(base::MakeRefCounted<SSLCertRequestInfo>()); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2046 | EXPECT_EQ( |
| 2047 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2048 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2049 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2050 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2051 | pool_.get(), log.bound())); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2052 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2053 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2054 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2055 | EXPECT_FALSE(handle.is_ssl_error()); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 2056 | EXPECT_FALSE(handle.ssl_cert_request_info()); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 2057 | |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 2058 | auto entries = log.GetEntries(); |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 2059 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2060 | EXPECT_EQ(4u, entries.size()); |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 2061 | EXPECT_TRUE(LogContainsEvent( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2062 | entries, 0, NetLogEventType::TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 2063 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2064 | EXPECT_TRUE(LogContainsBeginEvent(entries, 1, NetLogEventType::SOCKET_POOL)); |
| 2065 | EXPECT_TRUE(LogContainsEvent( |
| 2066 | entries, 2, NetLogEventType::SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
| 2067 | NetLogEventPhase::NONE)); |
| 2068 | EXPECT_TRUE(LogContainsEndEvent(entries, 3, NetLogEventType::SOCKET_POOL)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2069 | } |
| 2070 | |
mmenke | 6be122f | 2015-03-09 22:22:47 | [diff] [blame] | 2071 | // Check that an async ConnectJob failure does not result in creation of a new |
| 2072 | // ConnectJob when there's another pending request also waiting on its own |
| 2073 | // ConnectJob. See http://crbug.com/463960. |
| 2074 | TEST_F(ClientSocketPoolBaseTest, AsyncFailureWithPendingRequestWithJob) { |
| 2075 | CreatePool(2, 2); |
| 2076 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
| 2077 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2078 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2079 | IsError(ERR_IO_PENDING)); |
| 2080 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2081 | IsError(ERR_IO_PENDING)); |
mmenke | 6be122f | 2015-03-09 22:22:47 | [diff] [blame] | 2082 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2083 | EXPECT_THAT(request(0)->WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 2084 | EXPECT_THAT(request(1)->WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
mmenke | 6be122f | 2015-03-09 22:22:47 | [diff] [blame] | 2085 | |
| 2086 | EXPECT_EQ(2, client_socket_factory_.allocation_count()); |
| 2087 | } |
| 2088 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2089 | TEST_F(ClientSocketPoolBaseTest, TwoRequestsCancelOne) { |
[email protected] | b22b516 | 2010-03-16 07:53:10 | [diff] [blame] | 2090 | // TODO(eroman): Add back the log expectations! Removed them because the |
| 2091 | // ordering is difficult, and some may fire during destructor. |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2092 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2093 | |
| 2094 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2095 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2096 | TestCompletionCallback callback; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2097 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2098 | TestCompletionCallback callback2; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2099 | |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2100 | EXPECT_EQ( |
| 2101 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2102 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2103 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2104 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2105 | pool_.get(), NetLogWithSource())); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 2106 | BoundTestNetLog log2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 2107 | EXPECT_EQ( |
| 2108 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2109 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2110 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2111 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2112 | pool_.get(), NetLogWithSource())); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2113 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2114 | handle.Reset(); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2115 | |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 2116 | |
| 2117 | // 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] | 2118 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2119 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2120 | handle2.Reset(); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 2121 | |
| 2122 | // Now request 2 has actually finished. |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 2123 | // TODO(eroman): Add back log expectations. |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2124 | } |
| 2125 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2126 | TEST_F(ClientSocketPoolBaseTest, CancelRequestLimitsJobs) { |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 2127 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2128 | |
[email protected] | 17a0c6c | 2009-08-04 00:07:04 | [diff] [blame] | 2129 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2130 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2131 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
| 2132 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 2133 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 2134 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 2135 | |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2136 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2137 | static_cast<int>( |
| 2138 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2139 | (*requests())[2]->handle()->Reset(); |
| 2140 | (*requests())[3]->handle()->Reset(); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2141 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2142 | static_cast<int>( |
| 2143 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 2144 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2145 | (*requests())[1]->handle()->Reset(); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2146 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2147 | static_cast<int>( |
| 2148 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 2149 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2150 | (*requests())[0]->handle()->Reset(); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2151 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2152 | static_cast<int>( |
| 2153 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 2154 | } |
| 2155 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2156 | // When requests and ConnectJobs are not coupled, the request will get serviced |
| 2157 | // by whatever comes first. |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2158 | TEST_F(ClientSocketPoolBaseTest, ReleaseSockets) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2159 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2160 | |
| 2161 | // Start job 1 (async OK) |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 2162 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2163 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2164 | std::vector<TestSocketRequest*> request_order; |
| 2165 | size_t completion_count; // unused |
| 2166 | TestSocketRequest req1(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2167 | int rv = req1.handle()->Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2168 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2169 | ClientSocketPool::RespectLimits::ENABLED, req1.callback(), |
| 2170 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2171 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 2172 | EXPECT_THAT(req1.WaitForResult(), IsOk()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2173 | |
| 2174 | // Job 1 finished OK. Start job 2 (also async OK). Request 3 is pending |
| 2175 | // without a job. |
| 2176 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2177 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2178 | TestSocketRequest req2(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2179 | rv = req2.handle()->Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2180 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2181 | ClientSocketPool::RespectLimits::ENABLED, req2.callback(), |
| 2182 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2183 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2184 | TestSocketRequest req3(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2185 | rv = req3.handle()->Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2186 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2187 | ClientSocketPool::RespectLimits::ENABLED, req3.callback(), |
| 2188 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2189 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2190 | |
| 2191 | // Both Requests 2 and 3 are pending. We release socket 1 which should |
| 2192 | // service request 2. Request 3 should still be waiting. |
[email protected] | a6c59f6 | 2009-07-29 16:33:33 | [diff] [blame] | 2193 | req1.handle()->Reset(); |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 2194 | // Run the released socket wakeups. |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 2195 | base::RunLoop().RunUntilIdle(); |
[email protected] | a6c59f6 | 2009-07-29 16:33:33 | [diff] [blame] | 2196 | ASSERT_TRUE(req2.handle()->socket()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2197 | EXPECT_THAT(req2.WaitForResult(), IsOk()); |
[email protected] | a6c59f6 | 2009-07-29 16:33:33 | [diff] [blame] | 2198 | EXPECT_FALSE(req3.handle()->socket()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2199 | |
| 2200 | // Signal job 2, which should service request 3. |
| 2201 | |
| 2202 | client_socket_factory_.SignalJobs(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2203 | EXPECT_THAT(req3.WaitForResult(), IsOk()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2204 | |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2205 | ASSERT_EQ(3u, request_order.size()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2206 | EXPECT_EQ(&req1, request_order[0]); |
| 2207 | EXPECT_EQ(&req2, request_order[1]); |
| 2208 | EXPECT_EQ(&req3, request_order[2]); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2209 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2210 | } |
| 2211 | |
| 2212 | // The requests are not coupled to the jobs. So, the requests should finish in |
| 2213 | // their priority / insertion order. |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2214 | TEST_F(ClientSocketPoolBaseTest, PendingJobCompletionOrder) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2215 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2216 | // First two jobs are async. |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 2217 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2218 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2219 | std::vector<TestSocketRequest*> request_order; |
| 2220 | size_t completion_count; // unused |
| 2221 | TestSocketRequest req1(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2222 | int rv = req1.handle()->Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2223 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2224 | ClientSocketPool::RespectLimits::ENABLED, req1.callback(), |
| 2225 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2226 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2227 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2228 | TestSocketRequest req2(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2229 | rv = req2.handle()->Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2230 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2231 | ClientSocketPool::RespectLimits::ENABLED, req2.callback(), |
| 2232 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2233 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2234 | |
| 2235 | // The pending job is sync. |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 2236 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2237 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2238 | TestSocketRequest req3(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2239 | rv = req3.handle()->Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2240 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2241 | ClientSocketPool::RespectLimits::ENABLED, req3.callback(), |
| 2242 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2243 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2244 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2245 | EXPECT_THAT(req1.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 2246 | EXPECT_THAT(req2.WaitForResult(), IsOk()); |
| 2247 | EXPECT_THAT(req3.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2248 | |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2249 | ASSERT_EQ(3u, request_order.size()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2250 | EXPECT_EQ(&req1, request_order[0]); |
| 2251 | EXPECT_EQ(&req2, request_order[1]); |
| 2252 | EXPECT_EQ(&req3, request_order[2]); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2253 | } |
| 2254 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2255 | // Test GetLoadState in the case there's only one socket request. |
| 2256 | TEST_F(ClientSocketPoolBaseTest, LoadStateOneRequest) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2257 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2258 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2259 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2260 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2261 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2262 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2263 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2264 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2265 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2266 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2267 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2268 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2269 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE); |
| 2270 | EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle.GetLoadState()); |
| 2271 | |
| 2272 | // No point in completing the connection, since ClientSocketHandles only |
| 2273 | // expect the LoadState to be checked while connecting. |
| 2274 | } |
| 2275 | |
| 2276 | // Test GetLoadState in the case there are two socket requests. |
| 2277 | TEST_F(ClientSocketPoolBaseTest, LoadStateTwoRequests) { |
| 2278 | CreatePool(2, 2); |
| 2279 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2280 | |
| 2281 | ClientSocketHandle handle; |
| 2282 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2283 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2284 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2285 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2286 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2287 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2288 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_RESOLVING_HOST); |
| 2289 | |
| 2290 | ClientSocketHandle handle2; |
| 2291 | TestCompletionCallback callback2; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2292 | rv = handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2293 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2294 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2295 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2296 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2297 | client_socket_factory_.SetJobLoadState(1, LOAD_STATE_RESOLVING_HOST); |
| 2298 | |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 2299 | // Each handle should reflect the state of its own job. |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2300 | EXPECT_EQ(LOAD_STATE_RESOLVING_HOST, handle.GetLoadState()); |
| 2301 | EXPECT_EQ(LOAD_STATE_RESOLVING_HOST, handle2.GetLoadState()); |
| 2302 | |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 2303 | // Update the state of the first job. |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2304 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_CONNECTING); |
| 2305 | |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 2306 | // Only the state of the first request should have changed. |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2307 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2308 | EXPECT_EQ(LOAD_STATE_RESOLVING_HOST, handle2.GetLoadState()); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 2309 | |
| 2310 | // Update the state of the second job. |
| 2311 | client_socket_factory_.SetJobLoadState(1, LOAD_STATE_SSL_HANDSHAKE); |
| 2312 | |
| 2313 | // Only the state of the second request should have changed. |
| 2314 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
| 2315 | EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle2.GetLoadState()); |
| 2316 | |
| 2317 | // Second job connects and the first request gets the socket. The |
| 2318 | // second handle switches to the state of the remaining ConnectJob. |
| 2319 | client_socket_factory_.SignalJob(1); |
| 2320 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 2321 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle2.GetLoadState()); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2322 | } |
| 2323 | |
| 2324 | // Test GetLoadState in the case the per-group limit is reached. |
| 2325 | TEST_F(ClientSocketPoolBaseTest, LoadStateGroupLimit) { |
| 2326 | CreatePool(2, 1); |
| 2327 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2328 | |
| 2329 | ClientSocketHandle handle; |
| 2330 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2331 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2332 | TestGroupId("a"), params_, base::nullopt, MEDIUM, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2333 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2334 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2335 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2336 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
| 2337 | |
| 2338 | // Request another socket from the same pool, buth with a higher priority. |
| 2339 | // The first request should now be stalled at the socket group limit. |
| 2340 | ClientSocketHandle handle2; |
| 2341 | TestCompletionCallback callback2; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2342 | rv = handle2.Init(TestGroupId("a"), params_, base::nullopt, HIGHEST, |
| 2343 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2344 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2345 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2346 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2347 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET, handle.GetLoadState()); |
| 2348 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle2.GetLoadState()); |
| 2349 | |
| 2350 | // The first handle should remain stalled as the other socket goes through |
| 2351 | // the connect process. |
| 2352 | |
| 2353 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE); |
| 2354 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET, handle.GetLoadState()); |
| 2355 | EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle2.GetLoadState()); |
| 2356 | |
| 2357 | client_socket_factory_.SignalJob(0); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2358 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2359 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET, handle.GetLoadState()); |
| 2360 | |
| 2361 | // Closing the second socket should cause the stalled handle to finally get a |
| 2362 | // ConnectJob. |
| 2363 | handle2.socket()->Disconnect(); |
| 2364 | handle2.Reset(); |
| 2365 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
| 2366 | } |
| 2367 | |
| 2368 | // Test GetLoadState in the case the per-pool limit is reached. |
| 2369 | TEST_F(ClientSocketPoolBaseTest, LoadStatePoolLimit) { |
| 2370 | CreatePool(2, 2); |
| 2371 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2372 | |
| 2373 | ClientSocketHandle handle; |
| 2374 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2375 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2376 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2377 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2378 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2379 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2380 | |
| 2381 | // Request for socket from another pool. |
| 2382 | ClientSocketHandle handle2; |
| 2383 | TestCompletionCallback callback2; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2384 | rv = handle2.Init(TestGroupId("b"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2385 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2386 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2387 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2388 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2389 | |
| 2390 | // Request another socket from the first pool. Request should stall at the |
| 2391 | // socket pool limit. |
| 2392 | ClientSocketHandle handle3; |
| 2393 | TestCompletionCallback callback3; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2394 | rv = handle3.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2395 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2396 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2397 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2398 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2399 | |
| 2400 | // The third handle should remain stalled as the other sockets in its group |
| 2401 | // goes through the connect process. |
| 2402 | |
| 2403 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
| 2404 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL, handle3.GetLoadState()); |
| 2405 | |
| 2406 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE); |
| 2407 | EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle.GetLoadState()); |
| 2408 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL, handle3.GetLoadState()); |
| 2409 | |
| 2410 | client_socket_factory_.SignalJob(0); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2411 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2412 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL, handle3.GetLoadState()); |
| 2413 | |
| 2414 | // Closing a socket should allow the stalled handle to finally get a new |
| 2415 | // ConnectJob. |
| 2416 | handle.socket()->Disconnect(); |
| 2417 | handle.Reset(); |
| 2418 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle3.GetLoadState()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2419 | } |
| 2420 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2421 | TEST_F(ClientSocketPoolBaseTest, CertError) { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2422 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2423 | connect_job_factory_->set_job_type(TestConnectJob::kMockCertErrorJob); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2424 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2425 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2426 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2427 | EXPECT_EQ( |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2428 | ERR_CERT_COMMON_NAME_INVALID, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2429 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2430 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2431 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2432 | pool_.get(), NetLogWithSource())); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2433 | EXPECT_TRUE(handle.is_initialized()); |
| 2434 | EXPECT_TRUE(handle.socket()); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2435 | } |
| 2436 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2437 | TEST_F(ClientSocketPoolBaseTest, AsyncCertError) { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2438 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2439 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2440 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingCertErrorJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2441 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2442 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2443 | EXPECT_EQ( |
| 2444 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2445 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2446 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2447 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2448 | pool_.get(), NetLogWithSource())); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2449 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2450 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2451 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_CERT_COMMON_NAME_INVALID)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2452 | EXPECT_TRUE(handle.is_initialized()); |
| 2453 | EXPECT_TRUE(handle.socket()); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2454 | } |
| 2455 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2456 | TEST_F(ClientSocketPoolBaseTest, AdditionalErrorStateSynchronous) { |
| 2457 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2458 | connect_job_factory_->set_job_type( |
| 2459 | TestConnectJob::kMockAdditionalErrorStateJob); |
| 2460 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2461 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2462 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2463 | EXPECT_EQ( |
| 2464 | ERR_CONNECTION_FAILED, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2465 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2466 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2467 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2468 | pool_.get(), NetLogWithSource())); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2469 | EXPECT_FALSE(handle.is_initialized()); |
| 2470 | EXPECT_FALSE(handle.socket()); |
| 2471 | EXPECT_TRUE(handle.is_ssl_error()); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 2472 | EXPECT_TRUE(handle.ssl_cert_request_info()); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2473 | } |
| 2474 | |
| 2475 | TEST_F(ClientSocketPoolBaseTest, AdditionalErrorStateAsynchronous) { |
| 2476 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2477 | |
| 2478 | connect_job_factory_->set_job_type( |
| 2479 | TestConnectJob::kMockPendingAdditionalErrorStateJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2480 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2481 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2482 | EXPECT_EQ( |
| 2483 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2484 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2485 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2486 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2487 | pool_.get(), NetLogWithSource())); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2488 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2489 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2490 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2491 | EXPECT_FALSE(handle.is_initialized()); |
| 2492 | EXPECT_FALSE(handle.socket()); |
| 2493 | EXPECT_TRUE(handle.is_ssl_error()); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 2494 | EXPECT_TRUE(handle.ssl_cert_request_info()); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2495 | } |
| 2496 | |
martijn | 003cd61 | 2016-05-19 22:24:38 | [diff] [blame] | 2497 | // Make sure we can reuse sockets. |
| 2498 | TEST_F(ClientSocketPoolBaseTest, CleanupTimedOutIdleSocketsReuse) { |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2499 | CreatePoolWithIdleTimeouts( |
| 2500 | kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2501 | base::TimeDelta(), // Time out unused sockets immediately. |
| 2502 | base::TimeDelta::FromDays(1)); // Don't time out used sockets. |
| 2503 | |
| 2504 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2505 | |
| 2506 | ClientSocketHandle handle; |
| 2507 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2508 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2509 | TestGroupId("a"), params_, base::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2510 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2511 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2512 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2513 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2514 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2515 | ASSERT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2516 | |
| 2517 | // Use and release the socket. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 2518 | EXPECT_EQ(1, handle.socket()->Write(nullptr, 1, CompletionOnceCallback(), |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 2519 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 2520 | TestLoadTimingInfoConnectedNotReused(handle); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2521 | handle.Reset(); |
| 2522 | |
| 2523 | // Should now have one idle socket. |
| 2524 | ASSERT_EQ(1, pool_->IdleSocketCount()); |
| 2525 | |
| 2526 | // Request a new socket. This should reuse the old socket and complete |
| 2527 | // synchronously. |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 2528 | BoundTestNetLog log; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2529 | rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2530 | TestGroupId("a"), params_, base::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2531 | ClientSocketPool::RespectLimits::ENABLED, CompletionOnceCallback(), |
| 2532 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2533 | ASSERT_THAT(rv, IsOk()); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2534 | EXPECT_TRUE(handle.is_reused()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 2535 | TestLoadTimingInfoConnectedReused(handle); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2536 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2537 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2538 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2539 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2540 | |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 2541 | auto entries = log.GetEntries(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2542 | EXPECT_TRUE(LogContainsEvent( |
| 2543 | entries, 0, NetLogEventType::TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET, |
| 2544 | NetLogEventPhase::NONE)); |
| 2545 | EXPECT_TRUE(LogContainsBeginEvent(entries, 1, NetLogEventType::SOCKET_POOL)); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2546 | EXPECT_TRUE(LogContainsEntryWithType( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2547 | entries, 2, NetLogEventType::SOCKET_POOL_REUSED_AN_EXISTING_SOCKET)); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2548 | } |
| 2549 | |
martijn | 003cd61 | 2016-05-19 22:24:38 | [diff] [blame] | 2550 | // Make sure we cleanup old unused sockets. |
Eric Roman | b49715e | 2018-04-24 22:41:17 | [diff] [blame] | 2551 | TEST_F(ClientSocketPoolBaseTest, CleanupTimedOutIdleSocketsNoReuse) { |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2552 | CreatePoolWithIdleTimeouts( |
| 2553 | kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 2554 | base::TimeDelta(), // Time out unused sockets immediately |
| 2555 | base::TimeDelta()); // Time out used sockets immediately |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2556 | |
| 2557 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2558 | |
| 2559 | // Startup two mock pending connect jobs, which will sit in the MessageLoop. |
| 2560 | |
| 2561 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2562 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2563 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2564 | TestGroupId("a"), params_, base::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2565 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2566 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2567 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2568 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2569 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2570 | |
| 2571 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2572 | TestCompletionCallback callback2; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2573 | rv = handle2.Init(TestGroupId("a"), params_, base::nullopt, LOWEST, |
| 2574 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2575 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2576 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2577 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2578 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2579 | pool_->GetLoadState(TestGroupId("a"), &handle2)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2580 | |
| 2581 | // Cancel one of the requests. Wait for the other, which will get the first |
| 2582 | // job. Release the socket. Run the loop again to make sure the second |
| 2583 | // socket is sitting idle and the first one is released (since ReleaseSocket() |
| 2584 | // just posts a DoReleaseSocket() task). |
| 2585 | |
| 2586 | handle.Reset(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2587 | ASSERT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2588 | // Use the socket. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 2589 | EXPECT_EQ(1, handle2.socket()->Write(nullptr, 1, CompletionOnceCallback(), |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 2590 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2591 | handle2.Reset(); |
| 2592 | |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2593 | // We post all of our delayed tasks with a 2ms delay. I.e. they don't |
| 2594 | // actually become pending until 2ms after they have been created. In order |
| 2595 | // to flush all tasks, we need to wait so that we know there are no |
| 2596 | // soon-to-be-pending tasks waiting. |
Alex Clarke | 0def209 | 2018-12-10 12:01:45 | [diff] [blame] | 2597 | FastForwardBy(base::TimeDelta::FromMilliseconds(10)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2598 | |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2599 | // Both sockets should now be idle. |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2600 | ASSERT_EQ(2, pool_->IdleSocketCount()); |
| 2601 | |
| 2602 | // Request a new socket. This should cleanup the unused and timed out ones. |
| 2603 | // A new socket will be created rather than reusing the idle one. |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 2604 | BoundTestNetLog log; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2605 | TestCompletionCallback callback3; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2606 | rv = handle.Init(TestGroupId("a"), params_, base::nullopt, LOWEST, |
| 2607 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2608 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2609 | pool_.get(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2610 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 2611 | ASSERT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2612 | EXPECT_FALSE(handle.is_reused()); |
| 2613 | |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2614 | // Make sure the idle socket is closed. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2615 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2616 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2617 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2618 | |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 2619 | auto entries = log.GetEntries(); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2620 | EXPECT_FALSE(LogContainsEntryWithType( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 2621 | entries, 1, NetLogEventType::SOCKET_POOL_REUSED_AN_EXISTING_SOCKET)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2622 | } |
| 2623 | |
[email protected] | 2041cf34 | 2010-02-19 03:15:59 | [diff] [blame] | 2624 | // Make sure that we process all pending requests even when we're stalling |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2625 | // because of multiple releasing disconnected sockets. |
| 2626 | TEST_F(ClientSocketPoolBaseTest, MultipleReleasingDisconnectedSockets) { |
| 2627 | CreatePoolWithIdleTimeouts( |
| 2628 | kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 2629 | base::TimeDelta(), // Time out unused sockets immediately. |
| 2630 | base::TimeDelta::FromDays(1)); // Don't time out used sockets. |
| 2631 | |
| 2632 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 2633 | |
| 2634 | // Startup 4 connect jobs. Two of them will be pending. |
| 2635 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2636 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2637 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2638 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2639 | TestGroupId("a"), params_, base::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2640 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2641 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2642 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2643 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2644 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2645 | TestCompletionCallback callback2; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2646 | rv = handle2.Init(TestGroupId("a"), params_, base::nullopt, LOWEST, |
| 2647 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2648 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2649 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2650 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2651 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2652 | ClientSocketHandle handle3; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2653 | TestCompletionCallback callback3; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2654 | rv = handle3.Init(TestGroupId("a"), params_, base::nullopt, LOWEST, |
| 2655 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2656 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2657 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2658 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2659 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2660 | ClientSocketHandle handle4; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2661 | TestCompletionCallback callback4; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2662 | rv = handle4.Init(TestGroupId("a"), params_, base::nullopt, LOWEST, |
| 2663 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2664 | callback4.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2665 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2666 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2667 | |
| 2668 | // Release two disconnected sockets. |
| 2669 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2670 | handle.socket()->Disconnect(); |
| 2671 | handle.Reset(); |
| 2672 | handle2.socket()->Disconnect(); |
| 2673 | handle2.Reset(); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2674 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2675 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2676 | EXPECT_FALSE(handle3.is_reused()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2677 | EXPECT_THAT(callback4.WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2678 | EXPECT_FALSE(handle4.is_reused()); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2679 | } |
| 2680 | |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2681 | // Regression test for http://crbug.com/42267. |
| 2682 | // When DoReleaseSocket() is processed for one socket, it is blocked because the |
| 2683 | // other stalled groups all have releasing sockets, so no progress can be made. |
| 2684 | TEST_F(ClientSocketPoolBaseTest, SocketLimitReleasingSockets) { |
| 2685 | CreatePoolWithIdleTimeouts( |
| 2686 | 4 /* socket limit */, 4 /* socket limit per group */, |
| 2687 | base::TimeDelta(), // Time out unused sockets immediately. |
| 2688 | base::TimeDelta::FromDays(1)); // Don't time out used sockets. |
| 2689 | |
| 2690 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 2691 | |
| 2692 | // Max out the socket limit with 2 per group. |
| 2693 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2694 | ClientSocketHandle handle_a[4]; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2695 | TestCompletionCallback callback_a[4]; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2696 | ClientSocketHandle handle_b[4]; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2697 | TestCompletionCallback callback_b[4]; |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2698 | |
| 2699 | for (int i = 0; i < 2; ++i) { |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2700 | EXPECT_EQ(OK, handle_a[i].Init(TestGroupId("a"), params_, base::nullopt, |
| 2701 | LOWEST, SocketTag(), |
| 2702 | ClientSocketPool::RespectLimits::ENABLED, |
| 2703 | callback_a[i].callback(), |
| 2704 | ClientSocketPool::ProxyAuthCallback(), |
| 2705 | pool_.get(), NetLogWithSource())); |
| 2706 | EXPECT_EQ(OK, handle_b[i].Init(TestGroupId("b"), params_, base::nullopt, |
| 2707 | LOWEST, SocketTag(), |
| 2708 | ClientSocketPool::RespectLimits::ENABLED, |
| 2709 | callback_b[i].callback(), |
| 2710 | ClientSocketPool::ProxyAuthCallback(), |
| 2711 | pool_.get(), NetLogWithSource())); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2712 | } |
[email protected] | b89f7e4 | 2010-05-20 20:37:00 | [diff] [blame] | 2713 | |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2714 | // Make 4 pending requests, 2 per group. |
| 2715 | |
| 2716 | for (int i = 2; i < 4; ++i) { |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2717 | EXPECT_EQ( |
| 2718 | ERR_IO_PENDING, |
| 2719 | handle_a[i].Init(TestGroupId("a"), params_, base::nullopt, LOWEST, |
| 2720 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2721 | callback_a[i].callback(), |
| 2722 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2723 | NetLogWithSource())); |
| 2724 | EXPECT_EQ( |
| 2725 | ERR_IO_PENDING, |
| 2726 | handle_b[i].Init(TestGroupId("b"), params_, base::nullopt, LOWEST, |
| 2727 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2728 | callback_b[i].callback(), |
| 2729 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2730 | NetLogWithSource())); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2731 | } |
| 2732 | |
| 2733 | // Release b's socket first. The order is important, because in |
| 2734 | // DoReleaseSocket(), we'll process b's released socket, and since both b and |
| 2735 | // a are stalled, but 'a' is lower lexicographically, we'll process group 'a' |
| 2736 | // first, which has a releasing socket, so it refuses to start up another |
| 2737 | // ConnectJob. So, we used to infinite loop on this. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2738 | handle_b[0].socket()->Disconnect(); |
| 2739 | handle_b[0].Reset(); |
| 2740 | handle_a[0].socket()->Disconnect(); |
| 2741 | handle_a[0].Reset(); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2742 | |
| 2743 | // Used to get stuck here. |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 2744 | base::RunLoop().RunUntilIdle(); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2745 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2746 | handle_b[1].socket()->Disconnect(); |
| 2747 | handle_b[1].Reset(); |
| 2748 | handle_a[1].socket()->Disconnect(); |
| 2749 | handle_a[1].Reset(); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2750 | |
| 2751 | for (int i = 2; i < 4; ++i) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2752 | EXPECT_THAT(callback_b[i].WaitForResult(), IsOk()); |
| 2753 | EXPECT_THAT(callback_a[i].WaitForResult(), IsOk()); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2754 | } |
| 2755 | } |
| 2756 | |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2757 | TEST_F(ClientSocketPoolBaseTest, |
| 2758 | ReleasingDisconnectedSocketsMaintainsPriorityOrder) { |
| 2759 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2760 | |
| 2761 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2762 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2763 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2764 | IsError(ERR_IO_PENDING)); |
| 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)); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2771 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2772 | EXPECT_THAT((*requests())[0]->WaitForResult(), IsOk()); |
| 2773 | EXPECT_THAT((*requests())[1]->WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2774 | EXPECT_EQ(2u, completion_count()); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2775 | |
| 2776 | // Releases one connection. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2777 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::NO_KEEP_ALIVE)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2778 | EXPECT_THAT((*requests())[2]->WaitForResult(), IsOk()); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2779 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2780 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::NO_KEEP_ALIVE)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2781 | EXPECT_THAT((*requests())[3]->WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2782 | EXPECT_EQ(4u, completion_count()); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2783 | |
| 2784 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 2785 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 2786 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 2787 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 2788 | |
| 2789 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2790 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(5)); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2791 | } |
| 2792 | |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2793 | class TestReleasingSocketRequest : public TestCompletionCallbackBase { |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2794 | public: |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2795 | TestReleasingSocketRequest(TransportClientSocketPool* pool, |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2796 | int expected_result, |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2797 | bool reset_releasing_handle) |
| 2798 | : pool_(pool), |
| 2799 | expected_result_(expected_result), |
Bence Béky | 8ddc249 | 2018-06-13 01:02:04 | [diff] [blame] | 2800 | reset_releasing_handle_(reset_releasing_handle) {} |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2801 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 2802 | ~TestReleasingSocketRequest() override = default; |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2803 | |
| 2804 | ClientSocketHandle* handle() { return &handle_; } |
| 2805 | |
Bence Béky | 8ddc249 | 2018-06-13 01:02:04 | [diff] [blame] | 2806 | CompletionOnceCallback callback() { |
| 2807 | return base::BindOnce(&TestReleasingSocketRequest::OnComplete, |
| 2808 | base::Unretained(this)); |
| 2809 | } |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2810 | |
| 2811 | private: |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2812 | void OnComplete(int result) { |
| 2813 | SetResult(result); |
| 2814 | if (reset_releasing_handle_) |
| 2815 | handle_.Reset(); |
| 2816 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2817 | EXPECT_EQ( |
| 2818 | expected_result_, |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 2819 | handle2_.Init( |
| 2820 | TestGroupId("a"), |
| 2821 | ClientSocketPool::SocketParams::CreateForHttpForTesting(), |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2822 | base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 2823 | ClientSocketPool::RespectLimits::ENABLED, CompletionOnceCallback(), |
| 2824 | ClientSocketPool::ProxyAuthCallback(), pool_, NetLogWithSource())); |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2825 | } |
| 2826 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2827 | TransportClientSocketPool* const pool_; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2828 | int expected_result_; |
| 2829 | bool reset_releasing_handle_; |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2830 | ClientSocketHandle handle_; |
| 2831 | ClientSocketHandle handle2_; |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2832 | }; |
| 2833 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2834 | |
| 2835 | TEST_F(ClientSocketPoolBaseTest, AdditionalErrorSocketsDontUseSlot) { |
| 2836 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2837 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2838 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
| 2839 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 2840 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2841 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2842 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2843 | client_socket_factory_.allocation_count()); |
| 2844 | |
| 2845 | connect_job_factory_->set_job_type( |
| 2846 | TestConnectJob::kMockPendingAdditionalErrorStateJob); |
| 2847 | TestReleasingSocketRequest req(pool_.get(), OK, false); |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2848 | EXPECT_EQ(ERR_IO_PENDING, |
| 2849 | req.handle()->Init( |
| 2850 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2851 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2852 | req.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2853 | pool_.get(), NetLogWithSource())); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2854 | // The next job should complete synchronously |
| 2855 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 2856 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2857 | EXPECT_THAT(req.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2858 | EXPECT_FALSE(req.handle()->is_initialized()); |
| 2859 | EXPECT_FALSE(req.handle()->socket()); |
| 2860 | EXPECT_TRUE(req.handle()->is_ssl_error()); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 2861 | EXPECT_TRUE(req.handle()->ssl_cert_request_info()); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2862 | } |
| 2863 | |
[email protected] | b6501d3d | 2010-06-03 23:53:34 | [diff] [blame] | 2864 | // http://crbug.com/44724 regression test. |
| 2865 | // We start releasing the pool when we flush on network change. When that |
| 2866 | // happens, the only active references are in the ClientSocketHandles. When a |
| 2867 | // ConnectJob completes and calls back into the last ClientSocketHandle, that |
| 2868 | // callback can release the last reference and delete the pool. After the |
| 2869 | // callback finishes, we go back to the stack frame within the now-deleted pool. |
| 2870 | // Executing any code that refers to members of the now-deleted pool can cause |
| 2871 | // crashes. |
| 2872 | TEST_F(ClientSocketPoolBaseTest, CallbackThatReleasesPool) { |
| 2873 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2874 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
| 2875 | |
| 2876 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2877 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2878 | EXPECT_EQ( |
| 2879 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2880 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2881 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2882 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2883 | pool_.get(), NetLogWithSource())); |
[email protected] | b6501d3d | 2010-06-03 23:53:34 | [diff] [blame] | 2884 | |
[email protected] | 7af985a | 2012-12-14 22:40:42 | [diff] [blame] | 2885 | pool_->FlushWithError(ERR_NETWORK_CHANGED); |
[email protected] | b6501d3d | 2010-06-03 23:53:34 | [diff] [blame] | 2886 | |
| 2887 | // We'll call back into this now. |
| 2888 | callback.WaitForResult(); |
| 2889 | } |
| 2890 | |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2891 | TEST_F(ClientSocketPoolBaseTest, DoNotReuseSocketAfterFlush) { |
| 2892 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2893 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2894 | |
| 2895 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2896 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2897 | EXPECT_EQ( |
| 2898 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2899 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2900 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2901 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2902 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2903 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2904 | EXPECT_EQ(ClientSocketHandle::UNUSED, handle.reuse_type()); |
| 2905 | |
[email protected] | 7af985a | 2012-12-14 22:40:42 | [diff] [blame] | 2906 | pool_->FlushWithError(ERR_NETWORK_CHANGED); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2907 | |
| 2908 | handle.Reset(); |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 2909 | base::RunLoop().RunUntilIdle(); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2910 | |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2911 | EXPECT_EQ( |
| 2912 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2913 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2914 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2915 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2916 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2917 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2918 | EXPECT_EQ(ClientSocketHandle::UNUSED, handle.reuse_type()); |
| 2919 | } |
| 2920 | |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2921 | class ConnectWithinCallback : public TestCompletionCallbackBase { |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2922 | public: |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2923 | ConnectWithinCallback( |
| 2924 | const ClientSocketPool::GroupId& group_id, |
Matt Menke | 84d11e56 | 2019-03-27 00:11:19 | [diff] [blame] | 2925 | const scoped_refptr<ClientSocketPool::SocketParams>& params, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2926 | TransportClientSocketPool* pool) |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2927 | : group_id_(group_id), params_(params), pool_(pool) {} |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2928 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 2929 | ~ConnectWithinCallback() override = default; |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2930 | |
| 2931 | int WaitForNestedResult() { |
| 2932 | return nested_callback_.WaitForResult(); |
| 2933 | } |
| 2934 | |
Bence Béky | 8ddc249 | 2018-06-13 01:02:04 | [diff] [blame] | 2935 | CompletionOnceCallback callback() { |
| 2936 | return base::BindOnce(&ConnectWithinCallback::OnComplete, |
| 2937 | base::Unretained(this)); |
| 2938 | } |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2939 | |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2940 | private: |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2941 | void OnComplete(int result) { |
| 2942 | SetResult(result); |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2943 | EXPECT_EQ( |
| 2944 | ERR_IO_PENDING, |
| 2945 | handle_.Init(group_id_, params_, base::nullopt, DEFAULT_PRIORITY, |
| 2946 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2947 | nested_callback_.callback(), |
| 2948 | ClientSocketPool::ProxyAuthCallback(), pool_, |
| 2949 | NetLogWithSource())); |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2950 | } |
| 2951 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2952 | const ClientSocketPool::GroupId group_id_; |
Matt Menke | 84d11e56 | 2019-03-27 00:11:19 | [diff] [blame] | 2953 | const scoped_refptr<ClientSocketPool::SocketParams> params_; |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2954 | TransportClientSocketPool* const pool_; |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2955 | ClientSocketHandle handle_; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2956 | TestCompletionCallback nested_callback_; |
| 2957 | |
| 2958 | DISALLOW_COPY_AND_ASSIGN(ConnectWithinCallback); |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2959 | }; |
| 2960 | |
| 2961 | TEST_F(ClientSocketPoolBaseTest, AbortAllRequestsOnFlush) { |
| 2962 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2963 | |
| 2964 | // First job will be waiting until it gets aborted. |
| 2965 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2966 | |
| 2967 | ClientSocketHandle handle; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2968 | ConnectWithinCallback callback(TestGroupId("a"), params_, pool_.get()); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2969 | EXPECT_EQ( |
| 2970 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2971 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2972 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2973 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2974 | pool_.get(), NetLogWithSource())); |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2975 | |
| 2976 | // Second job will be started during the first callback, and will |
| 2977 | // asynchronously complete with OK. |
| 2978 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 7af985a | 2012-12-14 22:40:42 | [diff] [blame] | 2979 | pool_->FlushWithError(ERR_NETWORK_CHANGED); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2980 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_NETWORK_CHANGED)); |
| 2981 | EXPECT_THAT(callback.WaitForNestedResult(), IsOk()); |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2982 | } |
| 2983 | |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 2984 | TEST_F(ClientSocketPoolBaseTest, BackupSocketWaitsForHostResolution) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2985 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets, |
| 2986 | true /* enable_backup_connect_jobs */); |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 2987 | |
| 2988 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2989 | ClientSocketHandle handle; |
| 2990 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2991 | EXPECT_EQ( |
| 2992 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2993 | handle.Init(TestGroupId("bar"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2994 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2995 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2996 | pool_.get(), NetLogWithSource())); |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 2997 | // The backup timer fires but doesn't start a new ConnectJob while resolving |
| 2998 | // the hostname. |
| 2999 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_RESOLVING_HOST); |
| 3000 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
| 3001 | ClientSocketPool::kMaxConnectRetryIntervalMs * 100)); |
| 3002 | EXPECT_EQ(1, client_socket_factory_.allocation_count()); |
| 3003 | |
| 3004 | // Once the ConnectJob has finished resolving the hostname, the backup timer |
| 3005 | // will create a ConnectJob when it fires. |
| 3006 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_CONNECTING); |
| 3007 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
| 3008 | ClientSocketPool::kMaxConnectRetryIntervalMs)); |
| 3009 | EXPECT_EQ(2, client_socket_factory_.allocation_count()); |
| 3010 | } |
| 3011 | |
| 3012 | // Test that no backup socket is created when a ConnectJob connects before it |
| 3013 | // completes. |
| 3014 | TEST_F(ClientSocketPoolBaseTest, NoBackupSocketWhenConnected) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3015 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets, |
| 3016 | true /* enable_backup_connect_jobs */); |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 3017 | |
| 3018 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3019 | ClientSocketHandle handle; |
| 3020 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3021 | EXPECT_EQ( |
| 3022 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3023 | handle.Init(TestGroupId("bar"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3024 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 3025 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3026 | pool_.get(), NetLogWithSource())); |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 3027 | // The backup timer fires but doesn't start a new ConnectJob while resolving |
| 3028 | // the hostname. |
| 3029 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_RESOLVING_HOST); |
| 3030 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
| 3031 | ClientSocketPool::kMaxConnectRetryIntervalMs * 100)); |
| 3032 | EXPECT_EQ(1, client_socket_factory_.allocation_count()); |
| 3033 | |
| 3034 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE); |
| 3035 | client_socket_factory_.SetJobHasEstablishedConnection(0); |
| 3036 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
| 3037 | ClientSocketPool::kMaxConnectRetryIntervalMs * 100)); |
| 3038 | EXPECT_EQ(1, client_socket_factory_.allocation_count()); |
| 3039 | } |
| 3040 | |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3041 | // Cancel a pending socket request while we're at max sockets, |
| 3042 | // and verify that the backup socket firing doesn't cause a crash. |
| 3043 | TEST_F(ClientSocketPoolBaseTest, BackupSocketCancelAtMaxSockets) { |
| 3044 | // Max 4 sockets globally, max 4 sockets per group. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3045 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets, |
| 3046 | true /* enable_backup_connect_jobs */); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3047 | |
[email protected] | 4baaf9d | 2010-08-31 15:15:44 | [diff] [blame] | 3048 | // Create the first socket and set to ERR_IO_PENDING. This starts the backup |
| 3049 | // timer. |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3050 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3051 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3052 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3053 | EXPECT_EQ( |
| 3054 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3055 | handle.Init(TestGroupId("bar"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3056 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 3057 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3058 | pool_.get(), NetLogWithSource())); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3059 | |
| 3060 | // Start (MaxSockets - 1) connected sockets to reach max sockets. |
| 3061 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 3062 | ClientSocketHandle handles[kDefaultMaxSockets]; |
| 3063 | for (int i = 1; i < kDefaultMaxSockets; ++i) { |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3064 | TestCompletionCallback callback; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3065 | EXPECT_EQ(OK, handles[i].Init(TestGroupId("bar"), params_, base::nullopt, |
| 3066 | DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3067 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3068 | callback.callback(), |
| 3069 | ClientSocketPool::ProxyAuthCallback(), |
| 3070 | pool_.get(), NetLogWithSource())); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3071 | } |
| 3072 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3073 | base::RunLoop().RunUntilIdle(); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3074 | |
| 3075 | // Cancel the pending request. |
| 3076 | handle.Reset(); |
| 3077 | |
| 3078 | // 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] | 3079 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
[email protected] | 26b997396 | 2012-01-28 00:57:00 | [diff] [blame] | 3080 | ClientSocketPool::kMaxConnectRetryIntervalMs / 2 * 3)); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3081 | |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3082 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 3083 | } |
| 3084 | |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3085 | TEST_F(ClientSocketPoolBaseTest, CancelBackupSocketAfterCancelingAllRequests) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3086 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets, |
| 3087 | true /* enable_backup_connect_jobs */); |
[email protected] | 4baaf9d | 2010-08-31 15:15:44 | [diff] [blame] | 3088 | |
| 3089 | // Create the first socket and set to ERR_IO_PENDING. This starts the backup |
| 3090 | // timer. |
| 3091 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3092 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3093 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3094 | EXPECT_EQ( |
| 3095 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3096 | handle.Init(TestGroupId("bar"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3097 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 3098 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3099 | pool_.get(), NetLogWithSource())); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3100 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("bar"))); |
| 3101 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("bar"))); |
| 3102 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3103 | TestGroupId("bar"))); |
| 3104 | EXPECT_EQ( |
| 3105 | 0u, pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("bar"))); |
[email protected] | 4baaf9d | 2010-08-31 15:15:44 | [diff] [blame] | 3106 | |
| 3107 | // Cancel the socket request. This should cancel the backup timer. Wait for |
| 3108 | // the backup time to see if it indeed got canceled. |
| 3109 | handle.Reset(); |
| 3110 | // 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] | 3111 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
[email protected] | 26b997396 | 2012-01-28 00:57:00 | [diff] [blame] | 3112 | ClientSocketPool::kMaxConnectRetryIntervalMs / 2 * 3)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3113 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("bar"))); |
| 3114 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("bar"))); |
[email protected] | 4baaf9d | 2010-08-31 15:15:44 | [diff] [blame] | 3115 | } |
| 3116 | |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3117 | TEST_F(ClientSocketPoolBaseTest, CancelBackupSocketAfterFinishingAllRequests) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3118 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets, |
| 3119 | true /* enable_backup_connect_jobs */); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3120 | |
| 3121 | // Create the first socket and set to ERR_IO_PENDING. This starts the backup |
| 3122 | // timer. |
| 3123 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3124 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3125 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3126 | EXPECT_EQ( |
| 3127 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3128 | handle.Init(TestGroupId("bar"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3129 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 3130 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3131 | pool_.get(), NetLogWithSource())); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3132 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3133 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3134 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3135 | EXPECT_EQ( |
| 3136 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3137 | handle2.Init(TestGroupId("bar"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3138 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3139 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3140 | pool_.get(), NetLogWithSource())); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3141 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("bar"))); |
| 3142 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("bar"))); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3143 | |
| 3144 | // Cancel request 1 and then complete request 2. With the requests finished, |
| 3145 | // the backup timer should be cancelled. |
| 3146 | handle.Reset(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3147 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3148 | // 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] | 3149 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
[email protected] | 26b997396 | 2012-01-28 00:57:00 | [diff] [blame] | 3150 | ClientSocketPool::kMaxConnectRetryIntervalMs / 2 * 3)); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3151 | } |
| 3152 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3153 | // Test delayed socket binding for the case where we have two connects, |
| 3154 | // and while one is waiting on a connect, the other frees up. |
| 3155 | // The socket waiting on a connect should switch immediately to the freed |
| 3156 | // up socket. |
| 3157 | TEST_F(ClientSocketPoolBaseTest, DelayedSocketBindingWaitingForConnect) { |
| 3158 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3159 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3160 | |
| 3161 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3162 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3163 | EXPECT_EQ( |
| 3164 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3165 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3166 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3167 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3168 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3169 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3170 | |
| 3171 | // No idle sockets, no pending jobs. |
| 3172 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3173 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3174 | |
| 3175 | // Create a second socket to the same host, but this one will wait. |
| 3176 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3177 | ClientSocketHandle handle2; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3178 | EXPECT_EQ( |
| 3179 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3180 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3181 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3182 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3183 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3184 | // No idle sockets, and one connecting job. |
| 3185 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3186 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3187 | |
| 3188 | // Return the first handle to the pool. This will initiate the delayed |
| 3189 | // binding. |
| 3190 | handle1.Reset(); |
| 3191 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3192 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3193 | |
| 3194 | // Still no idle sockets, still one pending connect job. |
| 3195 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3196 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3197 | |
| 3198 | // The second socket connected, even though it was a Waiting Job. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3199 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3200 | |
| 3201 | // And we can see there is still one job waiting. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3202 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3203 | |
| 3204 | // Finally, signal the waiting Connect. |
| 3205 | client_socket_factory_.SignalJobs(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3206 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3207 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3208 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3209 | } |
| 3210 | |
| 3211 | // Test delayed socket binding when a group is at capacity and one |
| 3212 | // of the group's sockets frees up. |
| 3213 | TEST_F(ClientSocketPoolBaseTest, DelayedSocketBindingAtGroupCapacity) { |
| 3214 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3215 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3216 | |
| 3217 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3218 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3219 | EXPECT_EQ( |
| 3220 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3221 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3222 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3223 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3224 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3225 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3226 | |
| 3227 | // No idle sockets, no pending jobs. |
| 3228 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3229 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3230 | |
| 3231 | // Create a second socket to the same host, but this one will wait. |
| 3232 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3233 | ClientSocketHandle handle2; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3234 | EXPECT_EQ( |
| 3235 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3236 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3237 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3238 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3239 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3240 | // No idle sockets, and one connecting job. |
| 3241 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3242 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3243 | |
| 3244 | // Return the first handle to the pool. This will initiate the delayed |
| 3245 | // binding. |
| 3246 | handle1.Reset(); |
| 3247 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3248 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3249 | |
| 3250 | // Still no idle sockets, still one pending connect job. |
| 3251 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3252 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3253 | |
| 3254 | // The second socket connected, even though it was a Waiting Job. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3255 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3256 | |
| 3257 | // And we can see there is still one job waiting. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3258 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3259 | |
| 3260 | // Finally, signal the waiting Connect. |
| 3261 | client_socket_factory_.SignalJobs(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3262 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3263 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3264 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3265 | } |
| 3266 | |
| 3267 | // Test out the case where we have one socket connected, one |
| 3268 | // connecting, when the first socket finishes and goes idle. |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3269 | // Although the second connection is pending, the second request |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3270 | // should complete, by taking the first socket's idle socket. |
| 3271 | TEST_F(ClientSocketPoolBaseTest, DelayedSocketBindingAtStall) { |
| 3272 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3273 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3274 | |
| 3275 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3276 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3277 | EXPECT_EQ( |
| 3278 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3279 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3280 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3281 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3282 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3283 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3284 | |
| 3285 | // No idle sockets, no pending jobs. |
| 3286 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3287 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3288 | |
| 3289 | // Create a second socket to the same host, but this one will wait. |
| 3290 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3291 | ClientSocketHandle handle2; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3292 | EXPECT_EQ( |
| 3293 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3294 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3295 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3296 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3297 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3298 | // No idle sockets, and one connecting job. |
| 3299 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3300 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3301 | |
| 3302 | // Return the first handle to the pool. This will initiate the delayed |
| 3303 | // binding. |
| 3304 | handle1.Reset(); |
| 3305 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3306 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3307 | |
| 3308 | // Still no idle sockets, still one pending connect job. |
| 3309 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3310 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3311 | |
| 3312 | // The second socket connected, even though it was a Waiting Job. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3313 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3314 | |
| 3315 | // And we can see there is still one job waiting. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3316 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3317 | |
| 3318 | // Finally, signal the waiting Connect. |
| 3319 | client_socket_factory_.SignalJobs(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3320 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3321 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3322 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3323 | } |
| 3324 | |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3325 | // Cover the case where on an available socket slot, we have one pending |
| 3326 | // request that completes synchronously, thereby making the Group empty. |
| 3327 | TEST_F(ClientSocketPoolBaseTest, SynchronouslyProcessOnePendingRequest) { |
| 3328 | const int kUnlimitedSockets = 100; |
| 3329 | const int kOneSocketPerGroup = 1; |
| 3330 | CreatePool(kUnlimitedSockets, kOneSocketPerGroup); |
| 3331 | |
| 3332 | // Make the first request asynchronous fail. |
| 3333 | // This will free up a socket slot later. |
| 3334 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
| 3335 | |
| 3336 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3337 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3338 | EXPECT_EQ( |
| 3339 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3340 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3341 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3342 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3343 | pool_.get(), NetLogWithSource())); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3344 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3345 | |
| 3346 | // Make the second request synchronously fail. This should make the Group |
| 3347 | // empty. |
| 3348 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
| 3349 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3350 | TestCompletionCallback callback2; |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3351 | // It'll be ERR_IO_PENDING now, but the TestConnectJob will synchronously fail |
| 3352 | // when created. |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3353 | EXPECT_EQ( |
| 3354 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3355 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3356 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3357 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3358 | pool_.get(), NetLogWithSource())); |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3359 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3360 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3361 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3362 | EXPECT_THAT(callback1.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 3363 | EXPECT_THAT(callback2.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3364 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3365 | } |
| 3366 | |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3367 | TEST_F(ClientSocketPoolBaseTest, PreferUsedSocketToUnusedSocket) { |
| 3368 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 3369 | |
| 3370 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3371 | |
| 3372 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3373 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3374 | EXPECT_EQ( |
| 3375 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3376 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3377 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3378 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3379 | pool_.get(), NetLogWithSource())); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3380 | |
| 3381 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3382 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3383 | EXPECT_EQ( |
| 3384 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3385 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3386 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3387 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3388 | pool_.get(), NetLogWithSource())); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3389 | ClientSocketHandle handle3; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3390 | TestCompletionCallback callback3; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3391 | EXPECT_EQ( |
| 3392 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3393 | handle3.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3394 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3395 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3396 | pool_.get(), NetLogWithSource())); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3397 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3398 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3399 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
| 3400 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3401 | |
| 3402 | // Use the socket. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 3403 | EXPECT_EQ(1, handle1.socket()->Write(nullptr, 1, CompletionOnceCallback(), |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 3404 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 3405 | EXPECT_EQ(1, handle3.socket()->Write(nullptr, 1, CompletionOnceCallback(), |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 3406 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3407 | |
| 3408 | handle1.Reset(); |
| 3409 | handle2.Reset(); |
| 3410 | handle3.Reset(); |
| 3411 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3412 | EXPECT_EQ(OK, handle1.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3413 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3414 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3415 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3416 | pool_.get(), NetLogWithSource())); |
| 3417 | EXPECT_EQ(OK, handle2.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3418 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3419 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3420 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3421 | pool_.get(), NetLogWithSource())); |
| 3422 | EXPECT_EQ(OK, handle3.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3423 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3424 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3425 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3426 | pool_.get(), NetLogWithSource())); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3427 | |
| 3428 | EXPECT_TRUE(handle1.socket()->WasEverUsed()); |
| 3429 | EXPECT_TRUE(handle2.socket()->WasEverUsed()); |
| 3430 | EXPECT_FALSE(handle3.socket()->WasEverUsed()); |
| 3431 | } |
| 3432 | |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3433 | TEST_F(ClientSocketPoolBaseTest, RequestSockets) { |
| 3434 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3435 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3436 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3437 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 3438 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3439 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3440 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3441 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3442 | EXPECT_EQ(2u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3443 | TestGroupId("a"))); |
| 3444 | EXPECT_EQ(2u, |
| 3445 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3446 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3447 | |
| 3448 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3449 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3450 | EXPECT_EQ( |
| 3451 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3452 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3453 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3454 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3455 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3456 | |
| 3457 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3458 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3459 | EXPECT_EQ( |
| 3460 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3461 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3462 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3463 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3464 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3465 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3466 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3467 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3468 | TestGroupId("a"))); |
| 3469 | EXPECT_EQ(0u, |
| 3470 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3471 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3472 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3473 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3474 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3475 | handle1.Reset(); |
| 3476 | handle2.Reset(); |
| 3477 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3478 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3479 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3480 | TestGroupId("a"))); |
| 3481 | EXPECT_EQ(0u, |
| 3482 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3483 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3484 | } |
| 3485 | |
| 3486 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsWhenAlreadyHaveAConnectJob) { |
| 3487 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3488 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3489 | |
| 3490 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3491 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3492 | EXPECT_EQ( |
| 3493 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3494 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3495 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3496 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3497 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3498 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3499 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3500 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3501 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3502 | TestGroupId("a"))); |
| 3503 | EXPECT_EQ(0u, |
| 3504 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3505 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3506 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3507 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 3508 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3509 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3510 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3511 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3512 | TestGroupId("a"))); |
| 3513 | EXPECT_EQ(1u, |
| 3514 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3515 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3516 | |
| 3517 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3518 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3519 | EXPECT_EQ( |
| 3520 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3521 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3522 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3523 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3524 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3525 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3526 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3527 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3528 | TestGroupId("a"))); |
| 3529 | EXPECT_EQ(0u, |
| 3530 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3531 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3532 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3533 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3534 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3535 | handle1.Reset(); |
| 3536 | handle2.Reset(); |
| 3537 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3538 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3539 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3540 | TestGroupId("a"))); |
| 3541 | EXPECT_EQ(0u, |
| 3542 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3543 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3544 | } |
| 3545 | |
| 3546 | TEST_F(ClientSocketPoolBaseTest, |
| 3547 | RequestSocketsWhenAlreadyHaveMultipleConnectJob) { |
| 3548 | CreatePool(4, 4); |
| 3549 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3550 | |
| 3551 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3552 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3553 | EXPECT_EQ( |
| 3554 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3555 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3556 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3557 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3558 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3559 | |
| 3560 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3561 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3562 | EXPECT_EQ( |
| 3563 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3564 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3565 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3566 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3567 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3568 | |
| 3569 | ClientSocketHandle handle3; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3570 | TestCompletionCallback callback3; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3571 | EXPECT_EQ( |
| 3572 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3573 | handle3.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3574 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3575 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3576 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3577 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3578 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3579 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3580 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3581 | TestGroupId("a"))); |
| 3582 | EXPECT_EQ(0u, |
| 3583 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3584 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3585 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3586 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 3587 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3588 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3589 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3590 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3591 | TestGroupId("a"))); |
| 3592 | EXPECT_EQ(0u, |
| 3593 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3594 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3595 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3596 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3597 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
| 3598 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3599 | handle1.Reset(); |
| 3600 | handle2.Reset(); |
| 3601 | handle3.Reset(); |
| 3602 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3603 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3604 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3605 | TestGroupId("a"))); |
| 3606 | EXPECT_EQ(0u, |
| 3607 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3608 | EXPECT_EQ(3u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3609 | } |
| 3610 | |
| 3611 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsAtMaxSocketLimit) { |
| 3612 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 3613 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3614 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3615 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3616 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3617 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, |
| 3618 | kDefaultMaxSockets, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3619 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3620 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3621 | EXPECT_EQ(kDefaultMaxSockets, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3622 | static_cast<int>( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3623 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
| 3624 | EXPECT_EQ( |
| 3625 | kDefaultMaxSockets, |
| 3626 | static_cast<int>(pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3627 | TestGroupId("a")))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3628 | EXPECT_EQ(kDefaultMaxSockets, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3629 | static_cast<int>(pool_->NumUnassignedConnectJobsInGroupForTesting( |
| 3630 | TestGroupId("a")))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3631 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3632 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("b"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3633 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3634 | pool_->RequestSockets(TestGroupId("b"), params_, base::nullopt, |
| 3635 | kDefaultMaxSockets, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3636 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3637 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("b"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3638 | } |
| 3639 | |
| 3640 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsHitMaxSocketLimit) { |
| 3641 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 3642 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3643 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3644 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3645 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3646 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, |
| 3647 | kDefaultMaxSockets - 1, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3648 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3649 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3650 | EXPECT_EQ(kDefaultMaxSockets - 1, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3651 | static_cast<int>( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3652 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
| 3653 | EXPECT_EQ( |
| 3654 | kDefaultMaxSockets - 1, |
| 3655 | static_cast<int>(pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3656 | TestGroupId("a")))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3657 | EXPECT_EQ(kDefaultMaxSockets - 1, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3658 | static_cast<int>(pool_->NumUnassignedConnectJobsInGroupForTesting( |
| 3659 | TestGroupId("a")))); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 3660 | EXPECT_FALSE(pool_->IsStalled()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3661 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3662 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("b"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3663 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3664 | pool_->RequestSockets(TestGroupId("b"), params_, base::nullopt, |
| 3665 | kDefaultMaxSockets, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3666 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3667 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("b"))); |
| 3668 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 3669 | EXPECT_FALSE(pool_->IsStalled()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3670 | } |
| 3671 | |
| 3672 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsCountIdleSockets) { |
| 3673 | CreatePool(4, 4); |
| 3674 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3675 | |
| 3676 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3677 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3678 | EXPECT_EQ( |
| 3679 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3680 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3681 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3682 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3683 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3684 | ASSERT_THAT(callback1.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3685 | handle1.Reset(); |
| 3686 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3687 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3688 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3689 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3690 | TestGroupId("a"))); |
| 3691 | EXPECT_EQ(0u, |
| 3692 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3693 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3694 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3695 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 3696 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3697 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3698 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3699 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3700 | TestGroupId("a"))); |
| 3701 | EXPECT_EQ(1u, |
| 3702 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3703 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3704 | } |
| 3705 | |
| 3706 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsCountActiveSockets) { |
| 3707 | CreatePool(4, 4); |
| 3708 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3709 | |
| 3710 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3711 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3712 | EXPECT_EQ( |
| 3713 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3714 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3715 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3716 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3717 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3718 | ASSERT_THAT(callback1.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3719 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3720 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3721 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3722 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3723 | TestGroupId("a"))); |
| 3724 | EXPECT_EQ(0u, |
| 3725 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3726 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3727 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3728 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3729 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 3730 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3731 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3732 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3733 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3734 | TestGroupId("a"))); |
| 3735 | EXPECT_EQ(1u, |
| 3736 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3737 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3738 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3739 | } |
| 3740 | |
| 3741 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsSynchronous) { |
| 3742 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3743 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 3744 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3745 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, |
| 3746 | kDefaultMaxSocketsPerGroup, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3747 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3748 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3749 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3750 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3751 | TestGroupId("a"))); |
| 3752 | EXPECT_EQ(0u, |
| 3753 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3754 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3755 | static_cast<int>(pool_->IdleSocketCountInGroup(TestGroupId("a")))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3756 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3757 | pool_->RequestSockets(TestGroupId("b"), params_, base::nullopt, |
| 3758 | kDefaultMaxSocketsPerGroup, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3759 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3760 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 3761 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3762 | TestGroupId("b"))); |
| 3763 | EXPECT_EQ(0u, |
| 3764 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3765 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3766 | static_cast<int>(pool_->IdleSocketCountInGroup(TestGroupId("b")))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3767 | } |
| 3768 | |
[email protected] | 3c819f52 | 2010-12-02 02:03:12 | [diff] [blame] | 3769 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsSynchronousError) { |
| 3770 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3771 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
| 3772 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3773 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, |
| 3774 | kDefaultMaxSocketsPerGroup, NetLogWithSource()); |
[email protected] | 3c819f52 | 2010-12-02 02:03:12 | [diff] [blame] | 3775 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3776 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | fd2e53e | 2011-01-14 20:40:52 | [diff] [blame] | 3777 | |
| 3778 | connect_job_factory_->set_job_type( |
| 3779 | TestConnectJob::kMockAdditionalErrorStateJob); |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3780 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, |
| 3781 | kDefaultMaxSocketsPerGroup, NetLogWithSource()); |
[email protected] | fd2e53e | 2011-01-14 20:40:52 | [diff] [blame] | 3782 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3783 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | 3c819f52 | 2010-12-02 02:03:12 | [diff] [blame] | 3784 | } |
| 3785 | |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3786 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsMultipleTimesDoesNothing) { |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3787 | CreatePool(4, 4); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3788 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3789 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3790 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 3791 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3792 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3793 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3794 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3795 | EXPECT_EQ(2u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3796 | TestGroupId("a"))); |
| 3797 | EXPECT_EQ(2u, |
| 3798 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3799 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3800 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3801 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3802 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 3803 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3804 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3805 | EXPECT_EQ(2u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3806 | TestGroupId("a"))); |
| 3807 | EXPECT_EQ(2u, |
| 3808 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3809 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3810 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3811 | |
| 3812 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3813 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3814 | EXPECT_EQ( |
| 3815 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3816 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3817 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3818 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3819 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3820 | |
| 3821 | client_socket_factory_.SignalJob(0); |
| 3822 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3823 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3824 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3825 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3826 | TestGroupId("a"))); |
| 3827 | EXPECT_EQ(1u, |
| 3828 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3829 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3830 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3831 | |
| 3832 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3833 | TestCompletionCallback callback2; |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3834 | EXPECT_EQ( |
| 3835 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3836 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3837 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3838 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3839 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3840 | client_socket_factory_.SignalJob(0); |
| 3841 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3842 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3843 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3844 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3845 | TestGroupId("a"))); |
| 3846 | EXPECT_EQ(0u, |
| 3847 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3848 | EXPECT_EQ(2, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3849 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3850 | |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3851 | handle1.Reset(); |
| 3852 | handle2.Reset(); |
| 3853 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3854 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3855 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3856 | TestGroupId("a"))); |
| 3857 | EXPECT_EQ(0u, |
| 3858 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3859 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3860 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3861 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3862 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 3863 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3864 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3865 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3866 | TestGroupId("a"))); |
| 3867 | EXPECT_EQ(0u, |
| 3868 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3869 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3870 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3871 | } |
| 3872 | |
| 3873 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsDifferentNumSockets) { |
| 3874 | CreatePool(4, 4); |
| 3875 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3876 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3877 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 1, |
| 3878 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3879 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3880 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3881 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3882 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3883 | TestGroupId("a"))); |
| 3884 | EXPECT_EQ(1u, |
| 3885 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3886 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3887 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3888 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 3889 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3890 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3891 | EXPECT_EQ(2u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3892 | TestGroupId("a"))); |
| 3893 | EXPECT_EQ(2u, |
| 3894 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3895 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3896 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3897 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 3, |
| 3898 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3899 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3900 | EXPECT_EQ(3u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3901 | TestGroupId("a"))); |
| 3902 | EXPECT_EQ(3u, |
| 3903 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3904 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3905 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3906 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 1, |
| 3907 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3908 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3909 | EXPECT_EQ(3u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3910 | TestGroupId("a"))); |
| 3911 | EXPECT_EQ(3u, |
| 3912 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3913 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3914 | } |
| 3915 | |
| 3916 | TEST_F(ClientSocketPoolBaseTest, PreconnectJobsTakenByNormalRequests) { |
| 3917 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3918 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3919 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3920 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 1, |
| 3921 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3922 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3923 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3924 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3925 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3926 | TestGroupId("a"))); |
| 3927 | EXPECT_EQ(1u, |
| 3928 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3929 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3930 | |
| 3931 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3932 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3933 | EXPECT_EQ( |
| 3934 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3935 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3936 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3937 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3938 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3939 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3940 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3941 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3942 | TestGroupId("a"))); |
| 3943 | EXPECT_EQ(0u, |
| 3944 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3945 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3946 | |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3947 | client_socket_factory_.SignalJobs(); |
| 3948 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3949 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3950 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3951 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3952 | TestGroupId("a"))); |
| 3953 | EXPECT_EQ(0u, |
| 3954 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3955 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3956 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3957 | |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 3958 | // Make sure if a preconnected socket is not fully connected when a request |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 3959 | // starts, it has a connect start time. |
| 3960 | TestLoadTimingInfoConnectedNotReused(handle1); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3961 | handle1.Reset(); |
| 3962 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3963 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3964 | } |
| 3965 | |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 3966 | // Checks that fully connected preconnect jobs have no connect times, and are |
| 3967 | // marked as reused. |
| 3968 | TEST_F(ClientSocketPoolBaseTest, ConnectedPreconnectJobsHaveNoConnectTimes) { |
| 3969 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3970 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3971 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 1, |
| 3972 | NetLogWithSource()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 3973 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3974 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3975 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3976 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3977 | TestGroupId("a"))); |
| 3978 | EXPECT_EQ(0u, |
| 3979 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3980 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 3981 | |
| 3982 | ClientSocketHandle handle; |
| 3983 | TestCompletionCallback callback; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3984 | EXPECT_EQ(OK, handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3985 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3986 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3987 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3988 | pool_.get(), NetLogWithSource())); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 3989 | |
| 3990 | // Make sure the idle socket was used. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3991 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 3992 | |
| 3993 | TestLoadTimingInfoConnectedReused(handle); |
| 3994 | handle.Reset(); |
| 3995 | TestLoadTimingInfoNotConnected(handle); |
| 3996 | } |
| 3997 | |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3998 | // http://crbug.com/64940 regression test. |
| 3999 | TEST_F(ClientSocketPoolBaseTest, PreconnectClosesIdleSocketRemovesGroup) { |
| 4000 | const int kMaxTotalSockets = 3; |
| 4001 | const int kMaxSocketsPerGroup = 2; |
| 4002 | CreatePool(kMaxTotalSockets, kMaxSocketsPerGroup); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4003 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4004 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 4005 | // Note that group id ordering matters here. "a" comes before "b", so |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4006 | // CloseOneIdleSocket() will try to close "a"'s idle socket. |
| 4007 | |
| 4008 | // Set up one idle socket in "a". |
| 4009 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 4010 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4011 | EXPECT_EQ( |
| 4012 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4013 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4014 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4015 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4016 | pool_.get(), NetLogWithSource())); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4017 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 4018 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4019 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4020 | TestGroupId("a"))); |
| 4021 | EXPECT_EQ(0u, |
| 4022 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4023 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4024 | |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4025 | client_socket_factory_.SignalJobs(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4026 | ASSERT_THAT(callback1.WaitForResult(), IsOk()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4027 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4028 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4029 | TestGroupId("a"))); |
| 4030 | EXPECT_EQ(0u, |
| 4031 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4032 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4033 | |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4034 | handle1.Reset(); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4035 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4036 | |
| 4037 | // Set up two active sockets in "b". |
| 4038 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 4039 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4040 | EXPECT_EQ( |
| 4041 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4042 | handle1.Init(TestGroupId("b"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4043 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4044 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4045 | pool_.get(), NetLogWithSource())); |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4046 | EXPECT_EQ( |
| 4047 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4048 | handle2.Init(TestGroupId("b"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4049 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4050 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4051 | pool_.get(), NetLogWithSource())); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4052 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4053 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("b"))); |
| 4054 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 4055 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4056 | TestGroupId("b"))); |
| 4057 | EXPECT_EQ(0u, |
| 4058 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4059 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4060 | |
| 4061 | client_socket_factory_.SignalJobs(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4062 | ASSERT_THAT(callback1.WaitForResult(), IsOk()); |
| 4063 | ASSERT_THAT(callback2.WaitForResult(), IsOk()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4064 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4065 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4066 | TestGroupId("b"))); |
| 4067 | EXPECT_EQ(0u, |
| 4068 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 4069 | EXPECT_EQ(2, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4070 | |
| 4071 | // Now we have 1 idle socket in "a" and 2 active sockets in "b". This means |
| 4072 | // we've maxed out on sockets, since we set |kMaxTotalSockets| to 3. |
| 4073 | // Requesting 2 preconnected sockets for "a" should fail to allocate any more |
| 4074 | // sockets for "a", and "b" should still have 2 active sockets. |
| 4075 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4076 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 4077 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4078 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4079 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4080 | TestGroupId("a"))); |
| 4081 | EXPECT_EQ(0u, |
| 4082 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4083 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4084 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 4085 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 4086 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4087 | TestGroupId("b"))); |
| 4088 | EXPECT_EQ(0u, |
| 4089 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4090 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4091 | EXPECT_EQ(2, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4092 | |
| 4093 | // Now release the 2 active sockets for "b". This will give us 1 idle socket |
| 4094 | // in "a" and 2 idle sockets in "b". Requesting 2 preconnected sockets for |
| 4095 | // "a" should result in closing 1 for "b". |
| 4096 | handle1.Reset(); |
| 4097 | handle2.Reset(); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4098 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4099 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4100 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4101 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 4102 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4103 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4104 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4105 | TestGroupId("a"))); |
| 4106 | EXPECT_EQ(1u, |
| 4107 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4108 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4109 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 4110 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 4111 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4112 | TestGroupId("b"))); |
| 4113 | EXPECT_EQ(0u, |
| 4114 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4115 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4116 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4117 | } |
| 4118 | |
[email protected] | b7b8be4 | 2011-07-12 12:46:41 | [diff] [blame] | 4119 | TEST_F(ClientSocketPoolBaseTest, PreconnectWithoutBackupJob) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4120 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 4121 | true /* enable_backup_connect_jobs */); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4122 | |
| 4123 | // Make the ConnectJob hang until it times out, shorten the timeout. |
| 4124 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4125 | connect_job_factory_->set_timeout_duration( |
| 4126 | base::TimeDelta::FromMilliseconds(500)); |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4127 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 1, |
| 4128 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4129 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4130 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4131 | TestGroupId("a"))); |
| 4132 | EXPECT_EQ(1u, |
| 4133 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4134 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4135 | |
[email protected] | b7b8be4 | 2011-07-12 12:46:41 | [diff] [blame] | 4136 | // Verify the backup timer doesn't create a backup job, by making |
| 4137 | // the backup job a pending job instead of a waiting job, so it |
| 4138 | // *would* complete if it were created. |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4139 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 4140 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
Gabriel Charette | ea91801 | 2018-05-16 11:53:44 | [diff] [blame] | 4141 | FROM_HERE, base::RunLoop::QuitCurrentWhenIdleClosureDeprecated(), |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 4142 | base::TimeDelta::FromSeconds(1)); |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 4143 | base::RunLoop().Run(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4144 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4145 | } |
| 4146 | |
[email protected] | b7b8be4 | 2011-07-12 12:46:41 | [diff] [blame] | 4147 | TEST_F(ClientSocketPoolBaseTest, PreconnectWithBackupJob) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4148 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 4149 | true /* enable_backup_connect_jobs */); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4150 | |
| 4151 | // Make the ConnectJob hang forever. |
| 4152 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4153 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 1, |
| 4154 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4155 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4156 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4157 | TestGroupId("a"))); |
| 4158 | EXPECT_EQ(1u, |
| 4159 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4160 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 4161 | base::RunLoop().RunUntilIdle(); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4162 | |
| 4163 | // Make the backup job be a pending job, so it completes normally. |
| 4164 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 4165 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 4166 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4167 | EXPECT_EQ( |
| 4168 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4169 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4170 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4171 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4172 | pool_.get(), NetLogWithSource())); |
[email protected] | b7b8be4 | 2011-07-12 12:46:41 | [diff] [blame] | 4173 | // Timer has started, but the backup connect job shouldn't be created yet. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4174 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4175 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4176 | TestGroupId("a"))); |
| 4177 | EXPECT_EQ(0u, |
| 4178 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4179 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4180 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4181 | ASSERT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4182 | |
| 4183 | // The hung connect job should still be there, but everything else should be |
| 4184 | // complete. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4185 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4186 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4187 | TestGroupId("a"))); |
| 4188 | EXPECT_EQ(1u, |
| 4189 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4190 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4191 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4192 | } |
| 4193 | |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4194 | // Tests that a preconnect that starts out with unread data can still be used. |
| 4195 | // http://crbug.com/334467 |
| 4196 | TEST_F(ClientSocketPoolBaseTest, PreconnectWithUnreadData) { |
| 4197 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4198 | connect_job_factory_->set_job_type(TestConnectJob::kMockUnreadDataJob); |
| 4199 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4200 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 1, |
| 4201 | NetLogWithSource()); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4202 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4203 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 4204 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4205 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4206 | TestGroupId("a"))); |
| 4207 | EXPECT_EQ(0u, |
| 4208 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4209 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4210 | |
| 4211 | // Fail future jobs to be sure that handle receives the preconnected socket |
| 4212 | // rather than closing it and making a new one. |
| 4213 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
| 4214 | ClientSocketHandle handle; |
| 4215 | TestCompletionCallback callback; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4216 | EXPECT_EQ(OK, handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4217 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4218 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4219 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4220 | pool_.get(), NetLogWithSource())); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4221 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4222 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 4223 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4224 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4225 | TestGroupId("a"))); |
| 4226 | EXPECT_EQ(0u, |
| 4227 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4228 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4229 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4230 | |
| 4231 | // Drain the pending read. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 4232 | EXPECT_EQ(1, handle.socket()->Read(nullptr, 1, CompletionOnceCallback())); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4233 | |
| 4234 | TestLoadTimingInfoConnectedReused(handle); |
| 4235 | handle.Reset(); |
| 4236 | |
| 4237 | // The socket should be usable now that it's idle again. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4238 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4239 | } |
| 4240 | |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4241 | TEST_F(ClientSocketPoolBaseTest, RequestGetsAssignedJob) { |
| 4242 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4243 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4244 | |
| 4245 | ClientSocketHandle handle1; |
| 4246 | TestCompletionCallback callback1; |
| 4247 | EXPECT_EQ( |
| 4248 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4249 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4250 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4251 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4252 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4253 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4254 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4255 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4256 | TestGroupId("a"))); |
| 4257 | EXPECT_EQ(0u, |
| 4258 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4259 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4260 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4261 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4262 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4263 | } |
| 4264 | |
| 4265 | TEST_F(ClientSocketPoolBaseTest, MultipleRequestsGetAssignedJobs) { |
| 4266 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4267 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4268 | |
| 4269 | ClientSocketHandle handle1; |
| 4270 | TestCompletionCallback callback1; |
| 4271 | EXPECT_EQ( |
| 4272 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4273 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4274 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4275 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4276 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4277 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4278 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4279 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4280 | TestGroupId("a"))); |
| 4281 | EXPECT_EQ(0u, |
| 4282 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4283 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4284 | |
| 4285 | ClientSocketHandle handle2; |
| 4286 | TestCompletionCallback callback2; |
| 4287 | EXPECT_EQ( |
| 4288 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4289 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4290 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4291 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4292 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4293 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4294 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4295 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4296 | TestGroupId("a"))); |
| 4297 | EXPECT_EQ(0u, |
| 4298 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4299 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4300 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4301 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4302 | &handle1)); |
| 4303 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4304 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4305 | |
| 4306 | // One job completes. The other request should still have its job. |
| 4307 | client_socket_factory_.SignalJob(0); |
| 4308 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 4309 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4310 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4311 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4312 | TestGroupId("a"))); |
| 4313 | EXPECT_EQ(0u, |
| 4314 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4315 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4316 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4317 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4318 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4319 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4320 | } |
| 4321 | |
| 4322 | TEST_F(ClientSocketPoolBaseTest, PreconnectJobGetsAssignedToRequest) { |
| 4323 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4324 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4325 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4326 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 1, |
| 4327 | NetLogWithSource()); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4328 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4329 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 4330 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4331 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4332 | TestGroupId("a"))); |
| 4333 | EXPECT_EQ(1u, |
| 4334 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4335 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4336 | |
| 4337 | ClientSocketHandle handle1; |
| 4338 | TestCompletionCallback callback1; |
| 4339 | EXPECT_EQ( |
| 4340 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4341 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4342 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4343 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4344 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4345 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4346 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4347 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4348 | TestGroupId("a"))); |
| 4349 | EXPECT_EQ(0u, |
| 4350 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4351 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4352 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4353 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4354 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4355 | } |
| 4356 | |
| 4357 | TEST_F(ClientSocketPoolBaseTest, HigherPriorityRequestStealsJob) { |
| 4358 | CreatePool(kDefaultMaxSockets, 1); |
| 4359 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4360 | |
| 4361 | ClientSocketHandle handle1; |
| 4362 | TestCompletionCallback callback1; |
| 4363 | EXPECT_EQ( |
| 4364 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4365 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4366 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4367 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4368 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4369 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4370 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4371 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4372 | TestGroupId("a"))); |
| 4373 | EXPECT_EQ(0u, |
| 4374 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4375 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4376 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4377 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4378 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4379 | |
| 4380 | // Insert a higher priority request |
| 4381 | ClientSocketHandle handle2; |
| 4382 | TestCompletionCallback callback2; |
| 4383 | EXPECT_EQ( |
| 4384 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4385 | handle2.Init(TestGroupId("a"), params_, base::nullopt, HIGHEST, |
| 4386 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4387 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4388 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4389 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4390 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4391 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4392 | TestGroupId("a"))); |
| 4393 | EXPECT_EQ(0u, |
| 4394 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4395 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4396 | |
| 4397 | // The highest priority request should steal the job from the default priority |
| 4398 | // request. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4399 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4400 | &handle2)); |
| 4401 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4402 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4403 | } |
| 4404 | |
| 4405 | TEST_F(ClientSocketPoolBaseTest, RequestStealsJobFromLowestRequestWithJob) { |
| 4406 | CreatePool(3, 3); |
| 4407 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4408 | |
| 4409 | ClientSocketHandle handle_lowest; |
| 4410 | TestCompletionCallback callback_lowest; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4411 | EXPECT_EQ( |
| 4412 | ERR_IO_PENDING, |
| 4413 | handle_lowest.Init(TestGroupId("a"), params_, base::nullopt, LOWEST, |
| 4414 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4415 | callback_lowest.callback(), |
| 4416 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4417 | NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4418 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4419 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4420 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4421 | TestGroupId("a"))); |
| 4422 | EXPECT_EQ(0u, |
| 4423 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4424 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4425 | |
| 4426 | ClientSocketHandle handle_highest; |
| 4427 | TestCompletionCallback callback_highest; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4428 | EXPECT_EQ( |
| 4429 | ERR_IO_PENDING, |
| 4430 | handle_highest.Init(TestGroupId("a"), params_, base::nullopt, HIGHEST, |
| 4431 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4432 | callback_highest.callback(), |
| 4433 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4434 | NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4435 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4436 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4437 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4438 | TestGroupId("a"))); |
| 4439 | EXPECT_EQ(0u, |
| 4440 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4441 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4442 | |
| 4443 | ClientSocketHandle handle_low; |
| 4444 | TestCompletionCallback callback_low; |
| 4445 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4446 | handle_low.Init( |
| 4447 | TestGroupId("a"), params_, base::nullopt, LOW, SocketTag(), |
| 4448 | ClientSocketPool::RespectLimits::ENABLED, |
| 4449 | callback_low.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4450 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4451 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4452 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4453 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4454 | TestGroupId("a"))); |
| 4455 | EXPECT_EQ(0u, |
| 4456 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4457 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4458 | |
| 4459 | ClientSocketHandle handle_lowest2; |
| 4460 | TestCompletionCallback callback_lowest2; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4461 | EXPECT_EQ( |
| 4462 | ERR_IO_PENDING, |
| 4463 | handle_lowest2.Init(TestGroupId("a"), params_, base::nullopt, LOWEST, |
| 4464 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4465 | callback_lowest2.callback(), |
| 4466 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4467 | NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4468 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4469 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4470 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4471 | TestGroupId("a"))); |
| 4472 | EXPECT_EQ(0u, |
| 4473 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4474 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4475 | |
| 4476 | // The top three requests in the queue should have jobs. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4477 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4478 | &handle_highest)); |
| 4479 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4480 | &handle_low)); |
| 4481 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4482 | &handle_lowest)); |
| 4483 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting( |
| 4484 | TestGroupId("a"), &handle_lowest2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4485 | |
| 4486 | // Add another request with medium priority. It should steal the job from the |
| 4487 | // lowest priority request with a job. |
| 4488 | ClientSocketHandle handle_medium; |
| 4489 | TestCompletionCallback callback_medium; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4490 | EXPECT_EQ( |
| 4491 | ERR_IO_PENDING, |
| 4492 | handle_medium.Init(TestGroupId("a"), params_, base::nullopt, MEDIUM, |
| 4493 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4494 | callback_medium.callback(), |
| 4495 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4496 | NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4497 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4498 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4499 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4500 | TestGroupId("a"))); |
| 4501 | EXPECT_EQ(0u, |
| 4502 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4503 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
| 4504 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4505 | &handle_highest)); |
| 4506 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4507 | &handle_medium)); |
| 4508 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4509 | &handle_low)); |
| 4510 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4511 | &handle_lowest)); |
| 4512 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting( |
| 4513 | TestGroupId("a"), &handle_lowest2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4514 | } |
| 4515 | |
| 4516 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeRequestStealsJob) { |
| 4517 | CreatePool(kDefaultMaxSockets, 1); |
| 4518 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4519 | |
| 4520 | ClientSocketHandle handle1; |
| 4521 | TestCompletionCallback callback1; |
| 4522 | EXPECT_EQ( |
| 4523 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4524 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4525 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4526 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4527 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4528 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4529 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4530 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4531 | TestGroupId("a"))); |
| 4532 | EXPECT_EQ(0u, |
| 4533 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4534 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4535 | |
| 4536 | ClientSocketHandle handle2; |
| 4537 | TestCompletionCallback callback2; |
| 4538 | EXPECT_EQ( |
| 4539 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4540 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4541 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4542 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4543 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4544 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4545 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4546 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4547 | TestGroupId("a"))); |
| 4548 | EXPECT_EQ(0u, |
| 4549 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4550 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4551 | |
| 4552 | // 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] | 4553 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4554 | &handle1)); |
| 4555 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4556 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4557 | |
| 4558 | // Reprioritizing the second request places it above the first, and it steals |
| 4559 | // the job from the first request. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4560 | pool_->SetPriority(TestGroupId("a"), &handle2, HIGHEST); |
| 4561 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4562 | &handle2)); |
| 4563 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4564 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4565 | } |
| 4566 | |
| 4567 | TEST_F(ClientSocketPoolBaseTest, CancelRequestReassignsJob) { |
| 4568 | CreatePool(kDefaultMaxSockets, 1); |
| 4569 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4570 | |
| 4571 | ClientSocketHandle handle1; |
| 4572 | TestCompletionCallback callback1; |
| 4573 | EXPECT_EQ( |
| 4574 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4575 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4576 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4577 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4578 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4579 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4580 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4581 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4582 | TestGroupId("a"))); |
| 4583 | EXPECT_EQ(0u, |
| 4584 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4585 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4586 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4587 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4588 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4589 | |
| 4590 | ClientSocketHandle handle2; |
| 4591 | TestCompletionCallback callback2; |
| 4592 | EXPECT_EQ( |
| 4593 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4594 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4595 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4596 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4597 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4598 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4599 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4600 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4601 | TestGroupId("a"))); |
| 4602 | EXPECT_EQ(0u, |
| 4603 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4604 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4605 | |
| 4606 | // The second request doesn't get a job because we are the limit. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4607 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4608 | &handle1)); |
| 4609 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4610 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4611 | |
| 4612 | // The second request should get a job upon cancelling the first request. |
| 4613 | handle1.Reset(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4614 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4615 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4616 | TestGroupId("a"))); |
| 4617 | EXPECT_EQ(0u, |
| 4618 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4619 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4620 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4621 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4622 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4623 | } |
| 4624 | |
| 4625 | TEST_F(ClientSocketPoolBaseTest, JobCompletionReassignsJob) { |
| 4626 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4627 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4628 | |
| 4629 | ClientSocketHandle handle1; |
| 4630 | TestCompletionCallback callback1; |
| 4631 | EXPECT_EQ( |
| 4632 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4633 | handle1.Init(TestGroupId("a"), params_, base::nullopt, HIGHEST, |
| 4634 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4635 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4636 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4637 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4638 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4639 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4640 | TestGroupId("a"))); |
| 4641 | EXPECT_EQ(0u, |
| 4642 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4643 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4644 | |
| 4645 | ClientSocketHandle handle2; |
| 4646 | TestCompletionCallback callback2; |
| 4647 | EXPECT_EQ( |
| 4648 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4649 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4650 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4651 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4652 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4653 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4654 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4655 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4656 | TestGroupId("a"))); |
| 4657 | EXPECT_EQ(0u, |
| 4658 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4659 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4660 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4661 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4662 | &handle1)); |
| 4663 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4664 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4665 | |
| 4666 | // The lower-priority job completes first. The higher-priority request should |
| 4667 | // get the socket, and the lower-priority request should get the remaining |
| 4668 | // job. |
| 4669 | client_socket_factory_.SignalJob(1); |
| 4670 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4671 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4672 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4673 | TestGroupId("a"))); |
| 4674 | EXPECT_EQ(0u, |
| 4675 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4676 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4677 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4678 | EXPECT_TRUE(handle1.socket()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4679 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4680 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4681 | } |
| 4682 | |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 4683 | class MockLayeredPool : public HigherLayeredPool { |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4684 | public: |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4685 | MockLayeredPool(TransportClientSocketPool* pool, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4686 | const ClientSocketPool::GroupId& group_id) |
| 4687 | : pool_(pool), group_id_(group_id), can_release_connection_(true) { |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 4688 | pool_->AddHigherLayeredPool(this); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4689 | } |
| 4690 | |
Daniel Cheng | 4496d082 | 2018-04-26 21:52:15 | [diff] [blame] | 4691 | ~MockLayeredPool() override { pool_->RemoveHigherLayeredPool(this); } |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4692 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4693 | int RequestSocket(TransportClientSocketPool* pool) { |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4694 | return handle_.Init( |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 4695 | group_id_, ClientSocketPool::SocketParams::CreateForHttpForTesting(), |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4696 | base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
| 4697 | ClientSocketPool::RespectLimits::ENABLED, callback_.callback(), |
| 4698 | ClientSocketPool::ProxyAuthCallback(), pool, NetLogWithSource()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4699 | } |
| 4700 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4701 | int RequestSocketWithoutLimits(TransportClientSocketPool* pool) { |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4702 | return handle_.Init( |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 4703 | group_id_, ClientSocketPool::SocketParams::CreateForHttpForTesting(), |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4704 | base::nullopt, MAXIMUM_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4705 | ClientSocketPool::RespectLimits::DISABLED, callback_.callback(), |
| 4706 | ClientSocketPool::ProxyAuthCallback(), pool, NetLogWithSource()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4707 | } |
| 4708 | |
| 4709 | bool ReleaseOneConnection() { |
| 4710 | if (!handle_.is_initialized() || !can_release_connection_) { |
| 4711 | return false; |
| 4712 | } |
| 4713 | handle_.socket()->Disconnect(); |
| 4714 | handle_.Reset(); |
| 4715 | return true; |
| 4716 | } |
| 4717 | |
| 4718 | void set_can_release_connection(bool can_release_connection) { |
| 4719 | can_release_connection_ = can_release_connection; |
| 4720 | } |
| 4721 | |
| 4722 | MOCK_METHOD0(CloseOneIdleConnection, bool()); |
| 4723 | |
| 4724 | private: |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4725 | TransportClientSocketPool* const pool_; |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4726 | ClientSocketHandle handle_; |
| 4727 | TestCompletionCallback callback_; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4728 | const ClientSocketPool::GroupId group_id_; |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4729 | bool can_release_connection_; |
| 4730 | }; |
| 4731 | |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4732 | // Tests the basic case of closing an idle socket in a higher layered pool when |
| 4733 | // a new request is issued and the lower layer pool is stalled. |
| 4734 | TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketsHeldByLayeredPoolWhenNeeded) { |
| 4735 | CreatePool(1, 1); |
| 4736 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 4737 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4738 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("foo")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4739 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4740 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4741 | .WillOnce(Invoke(&mock_layered_pool, |
| 4742 | &MockLayeredPool::ReleaseOneConnection)); |
| 4743 | ClientSocketHandle handle; |
| 4744 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4745 | EXPECT_EQ( |
| 4746 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4747 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4748 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4749 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4750 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4751 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4752 | } |
| 4753 | |
Matt Menke | 83367864 | 2019-03-05 22:05:51 | [diff] [blame] | 4754 | // Tests the case that trying to close an idle socket in a higher layered pool |
| 4755 | // fails. |
| 4756 | TEST_F(ClientSocketPoolBaseTest, |
| 4757 | CloseIdleSocketsHeldByLayeredPoolWhenNeededFails) { |
| 4758 | CreatePool(1, 1); |
| 4759 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 4760 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4761 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("foo")); |
Matt Menke | 83367864 | 2019-03-05 22:05:51 | [diff] [blame] | 4762 | mock_layered_pool.set_can_release_connection(false); |
| 4763 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
| 4764 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4765 | .WillOnce(Invoke(&mock_layered_pool, |
| 4766 | &MockLayeredPool::ReleaseOneConnection)); |
| 4767 | ClientSocketHandle handle; |
| 4768 | TestCompletionCallback callback; |
| 4769 | EXPECT_EQ( |
| 4770 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4771 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4772 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4773 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4774 | pool_.get(), NetLogWithSource())); |
Matt Menke | 83367864 | 2019-03-05 22:05:51 | [diff] [blame] | 4775 | base::RunLoop().RunUntilIdle(); |
| 4776 | EXPECT_FALSE(callback.have_result()); |
| 4777 | } |
| 4778 | |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4779 | // Same as above, but the idle socket is in the same group as the stalled |
| 4780 | // socket, and closes the only other request in its group when closing requests |
| 4781 | // in higher layered pools. This generally shouldn't happen, but it may be |
| 4782 | // possible if a higher level pool issues a request and the request is |
| 4783 | // subsequently cancelled. Even if it's not possible, best not to crash. |
| 4784 | TEST_F(ClientSocketPoolBaseTest, |
| 4785 | CloseIdleSocketsHeldByLayeredPoolWhenNeededSameGroup) { |
| 4786 | CreatePool(2, 2); |
| 4787 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 4788 | |
| 4789 | // Need a socket in another group for the pool to be stalled (If a group |
| 4790 | // has the maximum number of connections already, it's not stalled). |
| 4791 | ClientSocketHandle handle1; |
| 4792 | TestCompletionCallback callback1; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4793 | EXPECT_EQ(OK, handle1.Init(TestGroupId("group1"), params_, base::nullopt, |
| 4794 | DEFAULT_PRIORITY, SocketTag(), |
| 4795 | ClientSocketPool::RespectLimits::ENABLED, |
| 4796 | callback1.callback(), |
| 4797 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4798 | NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4799 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4800 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("group2")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4801 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4802 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4803 | .WillOnce(Invoke(&mock_layered_pool, |
| 4804 | &MockLayeredPool::ReleaseOneConnection)); |
| 4805 | ClientSocketHandle handle; |
| 4806 | TestCompletionCallback callback2; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4807 | EXPECT_EQ(ERR_IO_PENDING, |
| 4808 | handle.Init( |
| 4809 | TestGroupId("group2"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4810 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4811 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4812 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4813 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4814 | } |
| 4815 | |
| 4816 | // Tests the case when an idle socket can be closed when a new request is |
| 4817 | // issued, and the new request belongs to a group that was previously stalled. |
| 4818 | TEST_F(ClientSocketPoolBaseTest, |
| 4819 | CloseIdleSocketsHeldByLayeredPoolInSameGroupWhenNeeded) { |
| 4820 | CreatePool(2, 2); |
| 4821 | std::list<TestConnectJob::JobType> job_types; |
| 4822 | job_types.push_back(TestConnectJob::kMockJob); |
| 4823 | job_types.push_back(TestConnectJob::kMockJob); |
| 4824 | job_types.push_back(TestConnectJob::kMockJob); |
| 4825 | job_types.push_back(TestConnectJob::kMockJob); |
| 4826 | connect_job_factory_->set_job_types(&job_types); |
| 4827 | |
| 4828 | ClientSocketHandle handle1; |
| 4829 | TestCompletionCallback callback1; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4830 | EXPECT_EQ(OK, handle1.Init(TestGroupId("group1"), params_, base::nullopt, |
| 4831 | DEFAULT_PRIORITY, SocketTag(), |
| 4832 | ClientSocketPool::RespectLimits::ENABLED, |
| 4833 | callback1.callback(), |
| 4834 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4835 | NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4836 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4837 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("group2")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4838 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4839 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4840 | .WillRepeatedly(Invoke(&mock_layered_pool, |
| 4841 | &MockLayeredPool::ReleaseOneConnection)); |
| 4842 | mock_layered_pool.set_can_release_connection(false); |
| 4843 | |
| 4844 | // The third request is made when the socket pool is in a stalled state. |
| 4845 | ClientSocketHandle handle3; |
| 4846 | TestCompletionCallback callback3; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4847 | EXPECT_EQ(ERR_IO_PENDING, |
| 4848 | handle3.Init( |
| 4849 | TestGroupId("group3"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4850 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4851 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4852 | pool_.get(), NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4853 | |
| 4854 | base::RunLoop().RunUntilIdle(); |
| 4855 | EXPECT_FALSE(callback3.have_result()); |
| 4856 | |
| 4857 | // The fourth request is made when the pool is no longer stalled. The third |
| 4858 | // request should be serviced first, since it was issued first and has the |
| 4859 | // same priority. |
| 4860 | mock_layered_pool.set_can_release_connection(true); |
| 4861 | ClientSocketHandle handle4; |
| 4862 | TestCompletionCallback callback4; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4863 | EXPECT_EQ(ERR_IO_PENDING, |
| 4864 | handle4.Init( |
| 4865 | TestGroupId("group3"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4866 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4867 | callback4.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4868 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4869 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4870 | EXPECT_FALSE(callback4.have_result()); |
| 4871 | |
| 4872 | // Closing a handle should free up another socket slot. |
| 4873 | handle1.Reset(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4874 | EXPECT_THAT(callback4.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4875 | } |
| 4876 | |
| 4877 | // Tests the case when an idle socket can be closed when a new request is |
| 4878 | // issued, and the new request belongs to a group that was previously stalled. |
| 4879 | // |
| 4880 | // The two differences from the above test are that the stalled requests are not |
| 4881 | // in the same group as the layered pool's request, and the the fourth request |
| 4882 | // has a higher priority than the third one, so gets a socket first. |
| 4883 | TEST_F(ClientSocketPoolBaseTest, |
| 4884 | CloseIdleSocketsHeldByLayeredPoolInSameGroupWhenNeeded2) { |
| 4885 | CreatePool(2, 2); |
| 4886 | std::list<TestConnectJob::JobType> job_types; |
| 4887 | job_types.push_back(TestConnectJob::kMockJob); |
| 4888 | job_types.push_back(TestConnectJob::kMockJob); |
| 4889 | job_types.push_back(TestConnectJob::kMockJob); |
| 4890 | job_types.push_back(TestConnectJob::kMockJob); |
| 4891 | connect_job_factory_->set_job_types(&job_types); |
| 4892 | |
| 4893 | ClientSocketHandle handle1; |
| 4894 | TestCompletionCallback callback1; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4895 | EXPECT_EQ(OK, handle1.Init(TestGroupId("group1"), params_, base::nullopt, |
| 4896 | DEFAULT_PRIORITY, SocketTag(), |
| 4897 | ClientSocketPool::RespectLimits::ENABLED, |
| 4898 | callback1.callback(), |
| 4899 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4900 | NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4901 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4902 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("group2")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4903 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4904 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4905 | .WillRepeatedly(Invoke(&mock_layered_pool, |
| 4906 | &MockLayeredPool::ReleaseOneConnection)); |
| 4907 | mock_layered_pool.set_can_release_connection(false); |
| 4908 | |
| 4909 | // The third request is made when the socket pool is in a stalled state. |
| 4910 | ClientSocketHandle handle3; |
| 4911 | TestCompletionCallback callback3; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4912 | EXPECT_EQ( |
| 4913 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4914 | handle3.Init(TestGroupId("group3"), params_, base::nullopt, MEDIUM, |
| 4915 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4916 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4917 | pool_.get(), NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4918 | |
| 4919 | base::RunLoop().RunUntilIdle(); |
| 4920 | EXPECT_FALSE(callback3.have_result()); |
| 4921 | |
| 4922 | // The fourth request is made when the pool is no longer stalled. This |
| 4923 | // request has a higher priority than the third request, so is serviced first. |
| 4924 | mock_layered_pool.set_can_release_connection(true); |
| 4925 | ClientSocketHandle handle4; |
| 4926 | TestCompletionCallback callback4; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4927 | EXPECT_EQ( |
| 4928 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4929 | handle4.Init(TestGroupId("group3"), params_, base::nullopt, HIGHEST, |
| 4930 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4931 | callback4.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4932 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4933 | EXPECT_THAT(callback4.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4934 | EXPECT_FALSE(callback3.have_result()); |
| 4935 | |
| 4936 | // Closing a handle should free up another socket slot. |
| 4937 | handle1.Reset(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4938 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4939 | } |
| 4940 | |
| 4941 | TEST_F(ClientSocketPoolBaseTest, |
| 4942 | CloseMultipleIdleSocketsHeldByLayeredPoolWhenNeeded) { |
| 4943 | CreatePool(1, 1); |
| 4944 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 4945 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4946 | MockLayeredPool mock_layered_pool1(pool_.get(), TestGroupId("foo")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4947 | EXPECT_THAT(mock_layered_pool1.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4948 | EXPECT_CALL(mock_layered_pool1, CloseOneIdleConnection()) |
| 4949 | .WillRepeatedly(Invoke(&mock_layered_pool1, |
| 4950 | &MockLayeredPool::ReleaseOneConnection)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4951 | MockLayeredPool mock_layered_pool2(pool_.get(), TestGroupId("bar")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4952 | EXPECT_THAT(mock_layered_pool2.RequestSocketWithoutLimits(pool_.get()), |
| 4953 | IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4954 | EXPECT_CALL(mock_layered_pool2, CloseOneIdleConnection()) |
| 4955 | .WillRepeatedly(Invoke(&mock_layered_pool2, |
| 4956 | &MockLayeredPool::ReleaseOneConnection)); |
| 4957 | ClientSocketHandle handle; |
| 4958 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4959 | EXPECT_EQ( |
| 4960 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4961 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4962 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4963 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4964 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4965 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4966 | } |
| 4967 | |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4968 | // Test that when a socket pool and group are at their limits, a request |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4969 | // with RespectLimits::DISABLED triggers creation of a new socket, and gets the |
| 4970 | // socket instead of a request with the same priority that was issued earlier, |
| 4971 | // but has RespectLimits::ENABLED. |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4972 | TEST_F(ClientSocketPoolBaseTest, IgnoreLimits) { |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4973 | CreatePool(1, 1); |
| 4974 | |
| 4975 | // Issue a request to reach the socket pool limit. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4976 | EXPECT_EQ(OK, StartRequestWithIgnoreLimits( |
| 4977 | TestGroupId("a"), MAXIMUM_PRIORITY, |
| 4978 | ClientSocketPool::RespectLimits::ENABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4979 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4980 | |
| 4981 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 4982 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4983 | EXPECT_EQ(ERR_IO_PENDING, StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4984 | TestGroupId("a"), MAXIMUM_PRIORITY, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4985 | ClientSocketPool::RespectLimits::ENABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4986 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4987 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4988 | // Issue a request that ignores the limits, so a new ConnectJob is |
| 4989 | // created. |
| 4990 | EXPECT_EQ(ERR_IO_PENDING, StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4991 | TestGroupId("a"), MAXIMUM_PRIORITY, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4992 | ClientSocketPool::RespectLimits::DISABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4993 | ASSERT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4994 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4995 | EXPECT_THAT(request(2)->WaitForResult(), IsOk()); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4996 | EXPECT_FALSE(request(1)->have_result()); |
| 4997 | } |
| 4998 | |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 4999 | // Test that when a socket pool and group are at their limits, a ConnectJob |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5000 | // issued for a request with RespectLimits::DISABLED is not cancelled when a |
| 5001 | // request with RespectLimits::ENABLED issued to the same group is cancelled. |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 5002 | TEST_F(ClientSocketPoolBaseTest, IgnoreLimitsCancelOtherJob) { |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 5003 | CreatePool(1, 1); |
| 5004 | |
| 5005 | // Issue a request to reach the socket pool limit. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5006 | EXPECT_EQ(OK, StartRequestWithIgnoreLimits( |
| 5007 | TestGroupId("a"), MAXIMUM_PRIORITY, |
| 5008 | ClientSocketPool::RespectLimits::ENABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5009 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 5010 | |
| 5011 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 5012 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5013 | EXPECT_EQ(ERR_IO_PENDING, StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5014 | TestGroupId("a"), MAXIMUM_PRIORITY, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5015 | ClientSocketPool::RespectLimits::ENABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5016 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 5017 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5018 | // Issue a request with RespectLimits::DISABLED, so a new ConnectJob is |
| 5019 | // created. |
| 5020 | EXPECT_EQ(ERR_IO_PENDING, StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5021 | TestGroupId("a"), MAXIMUM_PRIORITY, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5022 | ClientSocketPool::RespectLimits::DISABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5023 | ASSERT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5024 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 5025 | // Cancel the pending request with RespectLimits::ENABLED. The ConnectJob |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5026 | // should not be cancelled. |
| 5027 | request(1)->handle()->Reset(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5028 | ASSERT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5029 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 5030 | EXPECT_THAT(request(2)->WaitForResult(), IsOk()); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 5031 | EXPECT_FALSE(request(1)->have_result()); |
| 5032 | } |
| 5033 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5034 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthNoAuthCallback) { |
| 5035 | CreatePool(1, 1); |
| 5036 | |
| 5037 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5038 | |
| 5039 | ClientSocketHandle handle; |
| 5040 | TestCompletionCallback callback; |
| 5041 | EXPECT_EQ( |
| 5042 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5043 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 5044 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5045 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5046 | pool_.get(), NetLogWithSource())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5047 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5048 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5049 | |
| 5050 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_PROXY_AUTH_REQUESTED)); |
| 5051 | EXPECT_FALSE(handle.is_initialized()); |
| 5052 | EXPECT_FALSE(handle.socket()); |
| 5053 | |
| 5054 | // The group should now be empty, and thus be deleted. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5055 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5056 | } |
| 5057 | |
| 5058 | class TestAuthHelper { |
| 5059 | public: |
| 5060 | TestAuthHelper() = default; |
| 5061 | ~TestAuthHelper() = default; |
| 5062 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5063 | void InitHandle( |
Matt Menke | 84d11e56 | 2019-03-27 00:11:19 | [diff] [blame] | 5064 | scoped_refptr<ClientSocketPool::SocketParams> params, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5065 | TransportClientSocketPool* pool, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5066 | RequestPriority priority = DEFAULT_PRIORITY, |
| 5067 | ClientSocketPool::RespectLimits respect_limits = |
| 5068 | ClientSocketPool::RespectLimits::ENABLED, |
| 5069 | const ClientSocketPool::GroupId& group_id_in = TestGroupId("a")) { |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5070 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5071 | handle_.Init(group_id_in, params, base::nullopt, priority, |
| 5072 | SocketTag(), respect_limits, callback_.callback(), |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5073 | base::BindRepeating(&TestAuthHelper::AuthCallback, |
| 5074 | base::Unretained(this)), |
| 5075 | pool, NetLogWithSource())); |
| 5076 | } |
| 5077 | |
| 5078 | void WaitForAuth() { |
| 5079 | run_loop_ = std::make_unique<base::RunLoop>(); |
| 5080 | run_loop_->Run(); |
| 5081 | run_loop_.reset(); |
| 5082 | } |
| 5083 | |
| 5084 | void WaitForAuthAndRestartSync() { |
| 5085 | restart_sync_ = true; |
| 5086 | WaitForAuth(); |
| 5087 | restart_sync_ = false; |
| 5088 | } |
| 5089 | |
| 5090 | void WaitForAuthAndResetHandleSync() { |
| 5091 | reset_handle_sync_ = true; |
| 5092 | WaitForAuth(); |
| 5093 | reset_handle_sync_ = false; |
| 5094 | } |
| 5095 | |
| 5096 | void RestartWithAuth() { |
| 5097 | DCHECK(restart_with_auth_callback_); |
| 5098 | std::move(restart_with_auth_callback_).Run(); |
| 5099 | } |
| 5100 | |
| 5101 | int WaitForResult() { |
| 5102 | int result = callback_.WaitForResult(); |
| 5103 | // There shouldn't be any callback waiting to be invoked once the request is |
| 5104 | // complete. |
| 5105 | EXPECT_FALSE(restart_with_auth_callback_); |
| 5106 | // The socket should only be initialized on success. |
| 5107 | EXPECT_EQ(result == OK, handle_.is_initialized()); |
| 5108 | EXPECT_EQ(result == OK, handle_.socket() != nullptr); |
| 5109 | return result; |
| 5110 | } |
| 5111 | |
| 5112 | ClientSocketHandle* handle() { return &handle_; } |
| 5113 | int auth_count() const { return auth_count_; } |
| 5114 | int have_result() const { return callback_.have_result(); } |
| 5115 | |
| 5116 | private: |
| 5117 | void AuthCallback(const HttpResponseInfo& response, |
| 5118 | HttpAuthController* auth_controller, |
| 5119 | base::OnceClosure restart_with_auth_callback) { |
| 5120 | EXPECT_FALSE(restart_with_auth_callback_); |
| 5121 | EXPECT_TRUE(restart_with_auth_callback); |
| 5122 | |
| 5123 | // Once there's a result, this method shouldn't be invoked again. |
| 5124 | EXPECT_FALSE(callback_.have_result()); |
| 5125 | |
| 5126 | ++auth_count_; |
| 5127 | run_loop_->Quit(); |
| 5128 | if (restart_sync_) { |
| 5129 | std::move(restart_with_auth_callback).Run(); |
| 5130 | return; |
| 5131 | } |
| 5132 | |
| 5133 | restart_with_auth_callback_ = std::move(restart_with_auth_callback); |
| 5134 | |
| 5135 | if (reset_handle_sync_) { |
| 5136 | handle_.Reset(); |
| 5137 | return; |
| 5138 | } |
| 5139 | } |
| 5140 | |
| 5141 | std::unique_ptr<base::RunLoop> run_loop_; |
| 5142 | base::OnceClosure restart_with_auth_callback_; |
| 5143 | |
| 5144 | bool restart_sync_ = false; |
| 5145 | bool reset_handle_sync_ = false; |
| 5146 | |
| 5147 | ClientSocketHandle handle_; |
| 5148 | int auth_count_ = 0; |
| 5149 | TestCompletionCallback callback_; |
| 5150 | |
| 5151 | DISALLOW_COPY_AND_ASSIGN(TestAuthHelper); |
| 5152 | }; |
| 5153 | |
| 5154 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnce) { |
| 5155 | CreatePool(1, 1); |
| 5156 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5157 | |
| 5158 | TestAuthHelper auth_helper; |
| 5159 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5160 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5161 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5162 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5163 | |
| 5164 | auth_helper.WaitForAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5165 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5166 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5167 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5168 | |
| 5169 | auth_helper.RestartWithAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5170 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5171 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5172 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5173 | |
| 5174 | EXPECT_THAT(auth_helper.WaitForResult(), IsOk()); |
| 5175 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5176 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5177 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5178 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5179 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5180 | } |
| 5181 | |
| 5182 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceSync) { |
| 5183 | CreatePool(1, 1); |
| 5184 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5185 | |
| 5186 | TestAuthHelper auth_helper; |
| 5187 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5188 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5189 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5190 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5191 | |
| 5192 | auth_helper.WaitForAuthAndRestartSync(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5193 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5194 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5195 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5196 | |
| 5197 | EXPECT_THAT(auth_helper.WaitForResult(), IsOk()); |
| 5198 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5199 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5200 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5201 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5202 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5203 | } |
| 5204 | |
| 5205 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceFails) { |
| 5206 | CreatePool(1, 1); |
| 5207 | connect_job_factory_->set_job_type( |
| 5208 | TestConnectJob::kMockAuthChallengeOnceFailingJob); |
| 5209 | |
| 5210 | TestAuthHelper auth_helper; |
| 5211 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5212 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5213 | |
| 5214 | auth_helper.WaitForAuth(); |
| 5215 | auth_helper.RestartWithAuth(); |
| 5216 | EXPECT_THAT(auth_helper.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 5217 | |
| 5218 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5219 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5220 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5221 | } |
| 5222 | |
| 5223 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceSyncFails) { |
| 5224 | CreatePool(1, 1); |
| 5225 | connect_job_factory_->set_job_type( |
| 5226 | TestConnectJob::kMockAuthChallengeOnceFailingJob); |
| 5227 | |
| 5228 | TestAuthHelper auth_helper; |
| 5229 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5230 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5231 | |
| 5232 | auth_helper.WaitForAuthAndRestartSync(); |
| 5233 | EXPECT_THAT(auth_helper.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 5234 | |
| 5235 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5236 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5237 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5238 | } |
| 5239 | |
| 5240 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceDeleteHandle) { |
| 5241 | CreatePool(1, 1); |
| 5242 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5243 | |
| 5244 | TestAuthHelper auth_helper; |
| 5245 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5246 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5247 | |
| 5248 | auth_helper.WaitForAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5249 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5250 | |
| 5251 | auth_helper.handle()->Reset(); |
| 5252 | |
| 5253 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5254 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5255 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5256 | EXPECT_FALSE(auth_helper.handle()->is_initialized()); |
| 5257 | EXPECT_FALSE(auth_helper.handle()->socket()); |
| 5258 | } |
| 5259 | |
| 5260 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceDeleteHandleSync) { |
| 5261 | CreatePool(1, 1); |
| 5262 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5263 | |
| 5264 | TestAuthHelper auth_helper; |
| 5265 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5266 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5267 | |
| 5268 | auth_helper.WaitForAuthAndResetHandleSync(); |
| 5269 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5270 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5271 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5272 | EXPECT_FALSE(auth_helper.handle()->is_initialized()); |
| 5273 | EXPECT_FALSE(auth_helper.handle()->socket()); |
| 5274 | } |
| 5275 | |
| 5276 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceFlushWithError) { |
| 5277 | CreatePool(1, 1); |
| 5278 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5279 | |
| 5280 | TestAuthHelper auth_helper; |
| 5281 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5282 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5283 | |
| 5284 | auth_helper.WaitForAuth(); |
| 5285 | |
| 5286 | pool_->FlushWithError(ERR_FAILED); |
| 5287 | base::RunLoop().RunUntilIdle(); |
| 5288 | |
| 5289 | // When flushing the socket pool, bound sockets should delay returning the |
| 5290 | // error until completion. |
| 5291 | EXPECT_FALSE(auth_helper.have_result()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5292 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5293 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5294 | |
| 5295 | auth_helper.RestartWithAuth(); |
| 5296 | // The callback should be called asynchronously. |
| 5297 | EXPECT_FALSE(auth_helper.have_result()); |
| 5298 | |
| 5299 | EXPECT_THAT(auth_helper.WaitForResult(), IsError(ERR_FAILED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5300 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5301 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5302 | } |
| 5303 | |
| 5304 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthTwice) { |
| 5305 | CreatePool(1, 1); |
| 5306 | connect_job_factory_->set_job_type( |
| 5307 | TestConnectJob::kMockAuthChallengeTwiceJob); |
| 5308 | |
| 5309 | TestAuthHelper auth_helper; |
| 5310 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5311 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5312 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5313 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5314 | |
| 5315 | auth_helper.WaitForAuth(); |
| 5316 | auth_helper.RestartWithAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5317 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5318 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5319 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5320 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5321 | |
| 5322 | auth_helper.WaitForAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5323 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5324 | EXPECT_EQ(2, auth_helper.auth_count()); |
| 5325 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5326 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5327 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5328 | auth_helper.RestartWithAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5329 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5330 | EXPECT_EQ(2, auth_helper.auth_count()); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5331 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5332 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5333 | |
| 5334 | EXPECT_THAT(auth_helper.WaitForResult(), IsOk()); |
| 5335 | EXPECT_EQ(2, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5336 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5337 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5338 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5339 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5340 | } |
| 5341 | |
| 5342 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthTwiceFails) { |
| 5343 | CreatePool(1, 1); |
| 5344 | connect_job_factory_->set_job_type( |
| 5345 | TestConnectJob::kMockAuthChallengeTwiceFailingJob); |
| 5346 | |
| 5347 | TestAuthHelper auth_helper; |
| 5348 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5349 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5350 | |
| 5351 | auth_helper.WaitForAuth(); |
| 5352 | auth_helper.RestartWithAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5353 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5354 | EXPECT_EQ(1, auth_helper.auth_count()); |
| 5355 | |
| 5356 | auth_helper.WaitForAuth(); |
| 5357 | auth_helper.RestartWithAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5358 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5359 | EXPECT_EQ(2, auth_helper.auth_count()); |
| 5360 | |
| 5361 | EXPECT_THAT(auth_helper.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 5362 | EXPECT_EQ(2, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5363 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5364 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5365 | } |
| 5366 | |
| 5367 | // Makes sure that when a bound request is destroyed, a new ConnectJob is |
| 5368 | // created, if needed. |
| 5369 | TEST_F(ClientSocketPoolBaseTest, |
| 5370 | ProxyAuthCreateNewConnectJobOnDestroyBoundRequest) { |
| 5371 | CreatePool(1 /* max_sockets */, 1 /* max_sockets_per_group */); |
| 5372 | connect_job_factory_->set_job_type( |
| 5373 | TestConnectJob::kMockAuthChallengeOnceFailingJob); |
| 5374 | |
| 5375 | // First request creates a ConnectJob. |
| 5376 | TestAuthHelper auth_helper1; |
| 5377 | auth_helper1.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5378 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5379 | |
| 5380 | // A second request come in, but no new ConnectJob is needed, since the limit |
| 5381 | // has been reached. |
| 5382 | TestAuthHelper auth_helper2; |
| 5383 | auth_helper2.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5384 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5385 | |
| 5386 | // Run until the auth callback for the first request is invoked. |
| 5387 | auth_helper1.WaitForAuth(); |
| 5388 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5389 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5390 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5391 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5392 | |
| 5393 | // Make connect jobs succeed, then cancel the first request, which should |
| 5394 | // destroy the bound ConnectJob, and cause a new ConnectJob to start. |
| 5395 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 5396 | auth_helper1.handle()->Reset(); |
| 5397 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5398 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5399 | |
| 5400 | // The second ConnectJob should succeed. |
| 5401 | EXPECT_THAT(auth_helper2.WaitForResult(), IsOk()); |
| 5402 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5403 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5404 | } |
| 5405 | |
| 5406 | // Makes sure that when a bound request is destroyed, a new ConnectJob is |
| 5407 | // created for another group, if needed. |
| 5408 | TEST_F(ClientSocketPoolBaseTest, |
| 5409 | ProxyAuthCreateNewConnectJobOnDestroyBoundRequestDifferentGroups) { |
| 5410 | CreatePool(1 /* max_sockets */, 1 /* max_sockets_per_group */); |
| 5411 | connect_job_factory_->set_job_type( |
| 5412 | TestConnectJob::kMockAuthChallengeOnceFailingJob); |
| 5413 | |
| 5414 | // First request creates a ConnectJob. |
| 5415 | TestAuthHelper auth_helper1; |
| 5416 | auth_helper1.InitHandle(params_, pool_.get(), DEFAULT_PRIORITY); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5417 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5418 | |
| 5419 | // A second request come in, but no new ConnectJob is needed, since the limit |
| 5420 | // has been reached. |
| 5421 | TestAuthHelper auth_helper2; |
| 5422 | auth_helper2.InitHandle(params_, pool_.get(), DEFAULT_PRIORITY, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5423 | ClientSocketPool::RespectLimits::ENABLED, |
| 5424 | TestGroupId("b")); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5425 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5426 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5427 | |
| 5428 | // Run until the auth callback for the first request is invoked. |
| 5429 | auth_helper1.WaitForAuth(); |
| 5430 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5431 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5432 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5433 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5434 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 5435 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5436 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5437 | |
| 5438 | // Make connect jobs succeed, then cancel the first request, which should |
| 5439 | // destroy the bound ConnectJob, and cause a new ConnectJob to start for the |
| 5440 | // other group. |
| 5441 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 5442 | auth_helper1.handle()->Reset(); |
| 5443 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5444 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 5445 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5446 | |
| 5447 | // The second ConnectJob should succeed. |
| 5448 | EXPECT_THAT(auth_helper2.WaitForResult(), IsOk()); |
| 5449 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5450 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 5451 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5452 | } |
| 5453 | |
| 5454 | // Test that once an auth challenge is bound, that's the request that gets all |
| 5455 | // subsequent calls and the socket itself. |
| 5456 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthStaysBound) { |
| 5457 | CreatePool(1, 1); |
| 5458 | connect_job_factory_->set_job_type( |
| 5459 | TestConnectJob::kMockAuthChallengeTwiceJob); |
| 5460 | |
| 5461 | // First request creates a ConnectJob. |
| 5462 | TestAuthHelper auth_helper1; |
| 5463 | auth_helper1.InitHandle(params_, pool_.get(), LOWEST); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5464 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5465 | |
| 5466 | // A second, higher priority request is made. |
| 5467 | TestAuthHelper auth_helper2; |
| 5468 | auth_helper2.InitHandle(params_, pool_.get(), LOW); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5469 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5470 | |
| 5471 | // Run until the auth callback for the second request is invoked. |
| 5472 | auth_helper2.WaitForAuth(); |
| 5473 | EXPECT_EQ(0, auth_helper1.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5474 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5475 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5476 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5477 | |
| 5478 | // Start a higher priority job. It shouldn't be able to steal |auth_helper2|'s |
| 5479 | // ConnectJob. |
| 5480 | TestAuthHelper auth_helper3; |
| 5481 | auth_helper3.InitHandle(params_, pool_.get(), HIGHEST); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5482 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5483 | |
| 5484 | // Start a higher job that ignores limits, creating a hanging socket. It |
| 5485 | // shouldn't be able to steal |auth_helper2|'s ConnectJob. |
| 5486 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 5487 | TestAuthHelper auth_helper4; |
| 5488 | auth_helper4.InitHandle(params_, pool_.get(), HIGHEST, |
| 5489 | ClientSocketPool::RespectLimits::DISABLED); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5490 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5491 | |
| 5492 | // Restart with auth, and |auth_helper2|'s auth method should be invoked |
| 5493 | // again. |
| 5494 | auth_helper2.RestartWithAuth(); |
| 5495 | auth_helper2.WaitForAuth(); |
| 5496 | EXPECT_EQ(0, auth_helper1.auth_count()); |
| 5497 | EXPECT_FALSE(auth_helper1.have_result()); |
| 5498 | EXPECT_EQ(2, auth_helper2.auth_count()); |
| 5499 | EXPECT_FALSE(auth_helper2.have_result()); |
| 5500 | EXPECT_EQ(0, auth_helper3.auth_count()); |
| 5501 | EXPECT_FALSE(auth_helper3.have_result()); |
| 5502 | EXPECT_EQ(0, auth_helper4.auth_count()); |
| 5503 | EXPECT_FALSE(auth_helper4.have_result()); |
| 5504 | |
| 5505 | // Advance auth again, and |auth_helper2| should get the socket. |
| 5506 | auth_helper2.RestartWithAuth(); |
| 5507 | EXPECT_THAT(auth_helper2.WaitForResult(), IsOk()); |
| 5508 | // The hung ConnectJob for the RespectLimits::DISABLED request is still in the |
| 5509 | // socket pool. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5510 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5511 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5512 | EXPECT_EQ(0, auth_helper1.auth_count()); |
| 5513 | EXPECT_FALSE(auth_helper1.have_result()); |
| 5514 | EXPECT_EQ(0, auth_helper3.auth_count()); |
| 5515 | EXPECT_FALSE(auth_helper3.have_result()); |
| 5516 | EXPECT_EQ(0, auth_helper4.auth_count()); |
| 5517 | EXPECT_FALSE(auth_helper4.have_result()); |
| 5518 | |
| 5519 | // If the socket is returned to the socket pool, the RespectLimits::DISABLED |
| 5520 | // socket request should be able to claim it. |
| 5521 | auth_helper2.handle()->Reset(); |
| 5522 | EXPECT_THAT(auth_helper4.WaitForResult(), IsOk()); |
| 5523 | EXPECT_EQ(0, auth_helper1.auth_count()); |
| 5524 | EXPECT_FALSE(auth_helper1.have_result()); |
| 5525 | EXPECT_EQ(0, auth_helper3.auth_count()); |
| 5526 | EXPECT_FALSE(auth_helper3.have_result()); |
| 5527 | EXPECT_EQ(0, auth_helper4.auth_count()); |
| 5528 | } |
| 5529 | |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5530 | TEST_F(ClientSocketPoolBaseTest, RefreshGroupCreatesNewConnectJobs) { |
| 5531 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 5532 | const ClientSocketPool::GroupId kGroupId = TestGroupId("a"); |
| 5533 | |
| 5534 | // First job will be waiting until it gets aborted. |
| 5535 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 5536 | |
| 5537 | ClientSocketHandle handle; |
| 5538 | TestCompletionCallback callback; |
| 5539 | EXPECT_THAT( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5540 | handle.Init(kGroupId, params_, base::nullopt, DEFAULT_PRIORITY, |
| 5541 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5542 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5543 | pool_.get(), NetLogWithSource()), |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5544 | IsError(ERR_IO_PENDING)); |
| 5545 | |
| 5546 | // Switch connect job types, so creating a new ConnectJob will result in |
| 5547 | // success. |
| 5548 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 5549 | |
| 5550 | pool_->RefreshGroupForTesting(kGroupId); |
| 5551 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 5552 | ASSERT_TRUE(handle.socket()); |
| 5553 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5554 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId)); |
| 5555 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(kGroupId)); |
| 5556 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(kGroupId)); |
| 5557 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId)); |
| 5558 | } |
| 5559 | |
| 5560 | TEST_F(ClientSocketPoolBaseTest, RefreshGroupClosesIdleConnectJobs) { |
| 5561 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 5562 | const ClientSocketPool::GroupId kGroupId = TestGroupId("a"); |
| 5563 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5564 | pool_->RequestSockets(kGroupId, params_, base::nullopt, 2, |
| 5565 | NetLogWithSource()); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5566 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId)); |
| 5567 | EXPECT_EQ(2, pool_->IdleSocketCount()); |
| 5568 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(kGroupId)); |
| 5569 | |
| 5570 | pool_->RefreshGroupForTesting(kGroupId); |
| 5571 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5572 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupId)); |
| 5573 | } |
| 5574 | |
| 5575 | TEST_F(ClientSocketPoolBaseTest, |
| 5576 | RefreshGroupDoesNotCloseIdleConnectJobsInOtherGroup) { |
| 5577 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 5578 | const ClientSocketPool::GroupId kGroupId = TestGroupId("a"); |
| 5579 | const ClientSocketPool::GroupId kOtherGroupId = TestGroupId("b"); |
| 5580 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5581 | pool_->RequestSockets(kOtherGroupId, params_, base::nullopt, 2, |
| 5582 | NetLogWithSource()); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5583 | ASSERT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 5584 | EXPECT_EQ(2, pool_->IdleSocketCount()); |
| 5585 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(kOtherGroupId)); |
| 5586 | |
| 5587 | pool_->RefreshGroupForTesting(kGroupId); |
| 5588 | ASSERT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 5589 | EXPECT_EQ(2, pool_->IdleSocketCount()); |
| 5590 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(kOtherGroupId)); |
| 5591 | } |
| 5592 | |
| 5593 | TEST_F(ClientSocketPoolBaseTest, RefreshGroupPreventsSocketReuse) { |
| 5594 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 5595 | const ClientSocketPool::GroupId kGroupId = TestGroupId("a"); |
| 5596 | |
| 5597 | ClientSocketHandle handle; |
| 5598 | TestCompletionCallback callback; |
| 5599 | EXPECT_THAT( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5600 | handle.Init(kGroupId, params_, base::nullopt, DEFAULT_PRIORITY, |
| 5601 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5602 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5603 | pool_.get(), NetLogWithSource()), |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5604 | IsOk()); |
| 5605 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId)); |
| 5606 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId)); |
| 5607 | |
| 5608 | pool_->RefreshGroupForTesting(kGroupId); |
| 5609 | |
| 5610 | handle.Reset(); |
| 5611 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5612 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupId)); |
| 5613 | } |
| 5614 | |
| 5615 | TEST_F(ClientSocketPoolBaseTest, |
| 5616 | RefreshGroupDoesNotPreventSocketReuseInOtherGroup) { |
| 5617 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 5618 | const ClientSocketPool::GroupId kGroupId = TestGroupId("a"); |
| 5619 | const ClientSocketPool::GroupId kOtherGroupId = TestGroupId("b"); |
| 5620 | |
| 5621 | ClientSocketHandle handle; |
| 5622 | TestCompletionCallback callback; |
| 5623 | EXPECT_THAT( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5624 | handle.Init(kOtherGroupId, params_, base::nullopt, DEFAULT_PRIORITY, |
| 5625 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5626 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5627 | pool_.get(), NetLogWithSource()), |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5628 | IsOk()); |
| 5629 | ASSERT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 5630 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kOtherGroupId)); |
| 5631 | |
| 5632 | pool_->RefreshGroupForTesting(kGroupId); |
| 5633 | |
| 5634 | handle.Reset(); |
| 5635 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 5636 | ASSERT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 5637 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(kOtherGroupId)); |
| 5638 | } |
| 5639 | |
| 5640 | TEST_F(ClientSocketPoolBaseTest, RefreshGroupReplacesBoundConnectJobOnConnect) { |
| 5641 | CreatePool(1, 1); |
| 5642 | const ClientSocketPool::GroupId kGroupId = TestGroupId("a"); |
| 5643 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5644 | |
| 5645 | TestAuthHelper auth_helper; |
| 5646 | auth_helper.InitHandle(params_, pool_.get(), DEFAULT_PRIORITY, |
| 5647 | ClientSocketPool::RespectLimits::ENABLED, kGroupId); |
| 5648 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(kGroupId)); |
| 5649 | |
| 5650 | auth_helper.WaitForAuth(); |
| 5651 | |
| 5652 | // This should update the generation, but not cancel the old ConnectJob - it's |
| 5653 | // not safe to do anything while waiting on the original ConnectJob. |
| 5654 | pool_->RefreshGroupForTesting(kGroupId); |
| 5655 | |
| 5656 | // Providing auth credentials and restarting the request with them will cause |
| 5657 | // the ConnectJob to complete successfully, but the result will be discarded |
| 5658 | // because of the generation mismatch. |
| 5659 | auth_helper.RestartWithAuth(); |
| 5660 | |
| 5661 | // Despite using ConnectJobs that simulate a single challenge, a second |
| 5662 | // challenge will be seen, due to using a new ConnectJob. |
| 5663 | auth_helper.WaitForAuth(); |
| 5664 | auth_helper.RestartWithAuth(); |
| 5665 | |
| 5666 | EXPECT_THAT(auth_helper.WaitForResult(), IsOk()); |
| 5667 | EXPECT_TRUE(auth_helper.handle()->socket()); |
| 5668 | EXPECT_EQ(2, auth_helper.auth_count()); |
| 5669 | |
| 5670 | // When released, the socket will be returned to the socket pool, and |
| 5671 | // available for reuse. |
| 5672 | auth_helper.handle()->Reset(); |
| 5673 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 5674 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId)); |
| 5675 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(kGroupId)); |
| 5676 | } |
| 5677 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 5678 | } // namespace |
| 5679 | |
| 5680 | } // namespace net |