[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" |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 19 | #include "base/run_loop.h" |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 20 | #include "base/single_thread_task_runner.h" |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 21 | #include "base/stl_util.h" |
[email protected] | fc9be580 | 2013-06-11 10:56:51 | [diff] [blame] | 22 | #include "base/strings/string_number_conversions.h" |
[email protected] | 18b57741 | 2013-07-18 04:19:15 | [diff] [blame] | 23 | #include "base/strings/stringprintf.h" |
[email protected] | f214f879 | 2011-01-01 02:17:08 | [diff] [blame] | 24 | #include "base/threading/platform_thread.h" |
gab | f767595f | 2016-05-11 18:50:35 | [diff] [blame] | 25 | #include "base/threading/thread_task_runner_handle.h" |
[email protected] | f3a1c64 | 2011-07-12 19:15:03 | [diff] [blame] | 26 | #include "base/values.h" |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 27 | #include "net/base/load_timing_info.h" |
[email protected] | b258e079 | 2013-01-12 07:11:59 | [diff] [blame] | 28 | #include "net/base/load_timing_info_test_util.h" |
[email protected] | d8eb8424 | 2010-09-25 02:25:06 | [diff] [blame] | 29 | #include "net/base/net_errors.h" |
[email protected] | ac790b4 | 2009-12-02 04:31:31 | [diff] [blame] | 30 | #include "net/base/request_priority.h" |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 31 | #include "net/base/test_completion_callback.h" |
[email protected] | 277d594 | 2010-08-11 21:02:35 | [diff] [blame] | 32 | #include "net/http/http_response_headers.h" |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame^] | 33 | #include "net/http/http_response_info.h" |
eroman | 87c53d6 | 2015-04-02 06:51:07 | [diff] [blame] | 34 | #include "net/log/net_log.h" |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 35 | #include "net/log/net_log_event_type.h" |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 36 | #include "net/log/net_log_source.h" |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 37 | #include "net/log/net_log_source_type.h" |
mmenke | 16a7cbdd | 2015-04-24 23:00:56 | [diff] [blame] | 38 | #include "net/log/test_net_log.h" |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 39 | #include "net/log/test_net_log_entry.h" |
| 40 | #include "net/log/test_net_log_util.h" |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 41 | #include "net/socket/client_socket_factory.h" |
| 42 | #include "net/socket/client_socket_handle.h" |
tfarina | 5dd13c2 | 2016-11-16 12:08:26 | [diff] [blame] | 43 | #include "net/socket/datagram_client_socket.h" |
tbansal | ca83c00 | 2016-04-28 20:56:28 | [diff] [blame] | 44 | #include "net/socket/socket_performance_watcher.h" |
Paul Jensen | 8d6f87ec | 2018-01-13 00:46:54 | [diff] [blame] | 45 | #include "net/socket/socket_tag.h" |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 46 | #include "net/socket/socket_test_util.h" |
[email protected] | 18ccfdb | 2013-08-15 00:13:44 | [diff] [blame] | 47 | #include "net/socket/ssl_client_socket.h" |
[email protected] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame] | 48 | #include "net/socket/stream_socket.h" |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 49 | #include "net/socket/transport_connect_job.h" |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame^] | 50 | #include "net/ssl/ssl_cert_request_info.h" |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 51 | #include "net/test/gtest_util.h" |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 52 | #include "net/test/test_with_scoped_task_environment.h" |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 53 | #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 54 | #include "testing/gmock/include/gmock/gmock.h" |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 55 | #include "testing/gtest/include/gtest/gtest.h" |
| 56 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 57 | using net::test::IsError; |
| 58 | using net::test::IsOk; |
| 59 | |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 60 | using ::testing::Invoke; |
| 61 | using ::testing::Return; |
| 62 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 63 | namespace net { |
| 64 | |
| 65 | namespace { |
| 66 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 67 | const int kDefaultMaxSockets = 4; |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 68 | const int kDefaultMaxSocketsPerGroup = 2; |
Tarun Bansal | a763509 | 2019-02-20 10:00:59 | [diff] [blame] | 69 | constexpr base::TimeDelta kUnusedIdleSocketTimeout = |
| 70 | base::TimeDelta::FromSeconds(10); |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 71 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 72 | ClientSocketPool::GroupId TestGroupId(const std::string& host, |
| 73 | int port = 80, |
| 74 | ClientSocketPool::SocketType socket_type = |
| 75 | ClientSocketPool::SocketType::kHttp, |
| 76 | bool privacy_mode = false) { |
| 77 | return ClientSocketPool::GroupId(HostPortPair(host, port), socket_type, |
| 78 | privacy_mode); |
| 79 | } |
| 80 | |
Matt Menke | 84d11e56 | 2019-03-27 00:11:19 | [diff] [blame] | 81 | // Returns a ClientSocketPool::SocketParams that will never be used to |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 82 | // create a real TreansportConnectJob. |
Matt Menke | 84d11e56 | 2019-03-27 00:11:19 | [diff] [blame] | 83 | scoped_refptr<ClientSocketPool::SocketParams> CreateDummyParams() { |
| 84 | return ClientSocketPool::SocketParams::CreateFromTransportSocketParams( |
| 85 | base::MakeRefCounted<TransportSocketParams>(HostPortPair("ignored", 80), |
| 86 | OnHostResolutionCallback())); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [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, |
| 266 | bool is_https_proxy, |
| 267 | const NetworkTrafficAnnotationTag& traffic_annotation) override { |
| 268 | NOTIMPLEMENTED(); |
| 269 | return nullptr; |
| 270 | } |
| 271 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 272 | void WaitForSignal(TestConnectJob* job) { waiting_jobs_.push_back(job); } |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 273 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 274 | void SignalJobs(); |
| 275 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 276 | void SignalJob(size_t job); |
| 277 | |
| 278 | void SetJobLoadState(size_t job, LoadState load_state); |
| 279 | |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 280 | // Sets the HasConnectionEstablished value of the specified job to true, |
| 281 | // without invoking the callback. |
| 282 | void SetJobHasEstablishedConnection(size_t job); |
| 283 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 284 | int allocation_count() const { return allocation_count_; } |
| 285 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 286 | private: |
| 287 | int allocation_count_; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 288 | std::vector<TestConnectJob*> waiting_jobs_; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 289 | }; |
| 290 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 291 | class TestConnectJob : public ConnectJob { |
| 292 | public: |
| 293 | enum JobType { |
| 294 | kMockJob, |
| 295 | kMockFailingJob, |
| 296 | kMockPendingJob, |
| 297 | kMockPendingFailingJob, |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 298 | kMockWaitingJob, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 299 | |
| 300 | // Certificate errors return a socket in addition to an error code. |
| 301 | kMockCertErrorJob, |
| 302 | kMockPendingCertErrorJob, |
| 303 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 304 | kMockAdditionalErrorStateJob, |
| 305 | kMockPendingAdditionalErrorStateJob, |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 306 | kMockUnreadDataJob, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 307 | |
| 308 | kMockAuthChallengeOnceJob, |
| 309 | kMockAuthChallengeTwiceJob, |
| 310 | kMockAuthChallengeOnceFailingJob, |
| 311 | kMockAuthChallengeTwiceFailingJob, |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 312 | }; |
| 313 | |
[email protected] | 994d493 | 2010-07-12 17:55:13 | [diff] [blame] | 314 | // The kMockPendingJob uses a slight delay before allowing the connect |
| 315 | // to complete. |
| 316 | static const int kPendingConnectDelay = 2; |
| 317 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 318 | TestConnectJob(JobType job_type, |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 319 | RequestPriority request_priority, |
| 320 | SocketTag socket_tag, |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 321 | base::TimeDelta timeout_duration, |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 322 | const CommonConnectJobParams* common_connect_job_params, |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 323 | ConnectJob::Delegate* delegate, |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 324 | MockClientSocketFactory* client_socket_factory) |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 325 | : ConnectJob(request_priority, |
| 326 | socket_tag, |
Matt Menke | 1a6c92d | 2019-02-23 00:25:38 | [diff] [blame] | 327 | timeout_duration, |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 328 | common_connect_job_params, |
Matt Menke | 1a6c92d | 2019-02-23 00:25:38 | [diff] [blame] | 329 | delegate, |
| 330 | nullptr /* net_log */, |
| 331 | NetLogSourceType::TRANSPORT_CONNECT_JOB, |
| 332 | NetLogEventType::TRANSPORT_CONNECT_JOB_CONNECT), |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 333 | job_type_(job_type), |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 334 | client_socket_factory_(client_socket_factory), |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 335 | load_state_(LOAD_STATE_IDLE), |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 336 | has_established_connection_(false), |
[email protected] | d5492c5 | 2013-11-10 20:44:39 | [diff] [blame] | 337 | store_additional_error_state_(false), |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 338 | weak_factory_(this) {} |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 339 | |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 340 | void Signal() { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 341 | DoConnect(waiting_success_, true /* async */, false /* recoverable */); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 342 | } |
| 343 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 344 | void set_load_state(LoadState load_state) { load_state_ = load_state; } |
| 345 | |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 346 | void set_has_established_connection() { |
| 347 | DCHECK(!has_established_connection_); |
| 348 | has_established_connection_ = true; |
| 349 | } |
| 350 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 351 | // From ConnectJob: |
| 352 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 353 | LoadState GetLoadState() const override { return load_state_; } |
[email protected] | 4645135 | 2009-09-01 14:54:21 | [diff] [blame] | 354 | |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 355 | bool HasEstablishedConnection() const override { |
| 356 | return has_established_connection_; |
| 357 | } |
| 358 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 359 | void GetAdditionalErrorState(ClientSocketHandle* handle) override { |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 360 | if (store_additional_error_state_) { |
| 361 | // Set all of the additional error state fields in some way. |
| 362 | handle->set_is_ssl_error(true); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame^] | 363 | handle->set_ssl_cert_request_info( |
| 364 | base::MakeRefCounted<SSLCertRequestInfo>()); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 365 | } |
| 366 | } |
| 367 | |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 368 | private: |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 369 | // From ConnectJob: |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 370 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 371 | int ConnectInternal() override { |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 372 | AddressList ignored; |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 373 | client_socket_factory_->CreateTransportClientSocket( |
| 374 | ignored, nullptr, nullptr, NetLogSource()); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 375 | switch (job_type_) { |
| 376 | case kMockJob: |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 377 | return DoConnect(true /* successful */, 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 kMockFailingJob: |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 380 | return DoConnect(false /* error */, false /* sync */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 381 | false /* cert_error */); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 382 | case kMockPendingJob: |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 383 | set_load_state(LOAD_STATE_CONNECTING); |
[email protected] | 6b17538 | 2009-10-13 06:47:47 | [diff] [blame] | 384 | |
| 385 | // Depending on execution timings, posting a delayed task can result |
| 386 | // in the task getting executed the at the earliest possible |
| 387 | // opportunity or only after returning once from the message loop and |
| 388 | // then a second call into the message loop. In order to make behavior |
| 389 | // more deterministic, we change the default delay to 2ms. This should |
| 390 | // always require us to wait for the second call into the message loop. |
| 391 | // |
| 392 | // N.B. The correct fix for this and similar timing problems is to |
| 393 | // abstract time for the purpose of unittests. Unfortunately, we have |
| 394 | // a lot of third-party components that directly call the various |
| 395 | // time functions, so this change would be rather invasive. |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 396 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 397 | FROM_HERE, |
kylechar | f4fe517 | 2019-02-15 18:53:49 | [diff] [blame] | 398 | base::BindOnce(base::IgnoreResult(&TestConnectJob::DoConnect), |
| 399 | weak_factory_.GetWeakPtr(), true /* successful */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 400 | true /* async */, false /* cert_error */), |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 401 | base::TimeDelta::FromMilliseconds(kPendingConnectDelay)); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 402 | return ERR_IO_PENDING; |
| 403 | case kMockPendingFailingJob: |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 404 | set_load_state(LOAD_STATE_CONNECTING); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 405 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 406 | FROM_HERE, |
kylechar | f4fe517 | 2019-02-15 18:53:49 | [diff] [blame] | 407 | base::BindOnce(base::IgnoreResult(&TestConnectJob::DoConnect), |
| 408 | weak_factory_.GetWeakPtr(), false /* error */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 409 | true /* async */, false /* cert_error */), |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 410 | base::TimeDelta::FromMilliseconds(2)); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 411 | return ERR_IO_PENDING; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 412 | case kMockWaitingJob: |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 413 | set_load_state(LOAD_STATE_CONNECTING); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 414 | client_socket_factory_->WaitForSignal(this); |
| 415 | waiting_success_ = true; |
| 416 | return ERR_IO_PENDING; |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 417 | case kMockCertErrorJob: |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 418 | return DoConnect(false /* error */, false /* sync */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 419 | true /* cert_error */); |
| 420 | case kMockPendingCertErrorJob: |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 421 | set_load_state(LOAD_STATE_CONNECTING); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 422 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 423 | FROM_HERE, |
kylechar | f4fe517 | 2019-02-15 18:53:49 | [diff] [blame] | 424 | base::BindOnce(base::IgnoreResult(&TestConnectJob::DoConnect), |
| 425 | weak_factory_.GetWeakPtr(), false /* error */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 426 | true /* async */, true /* cert_error */), |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 427 | base::TimeDelta::FromMilliseconds(2)); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 428 | return ERR_IO_PENDING; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 429 | case kMockAdditionalErrorStateJob: |
| 430 | store_additional_error_state_ = true; |
| 431 | return DoConnect(false /* error */, false /* sync */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 432 | false /* cert_error */); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 433 | case kMockPendingAdditionalErrorStateJob: |
| 434 | set_load_state(LOAD_STATE_CONNECTING); |
| 435 | store_additional_error_state_ = true; |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 436 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 437 | FROM_HERE, |
kylechar | f4fe517 | 2019-02-15 18:53:49 | [diff] [blame] | 438 | base::BindOnce(base::IgnoreResult(&TestConnectJob::DoConnect), |
| 439 | weak_factory_.GetWeakPtr(), false /* error */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 440 | true /* async */, false /* cert_error */), |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 441 | base::TimeDelta::FromMilliseconds(2)); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 442 | return ERR_IO_PENDING; |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 443 | case kMockUnreadDataJob: { |
| 444 | int ret = DoConnect(true /* successful */, false /* sync */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 445 | false /* cert_error */); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 446 | static_cast<MockClientSocket*>(socket())->set_has_unread_data(true); |
| 447 | return ret; |
| 448 | } |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 449 | case kMockAuthChallengeOnceJob: |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 450 | set_load_state(LOAD_STATE_CONNECTING); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 451 | DoAdvanceAuthChallenge(1, true /* succeed_after_last_challenge */); |
| 452 | return ERR_IO_PENDING; |
| 453 | case kMockAuthChallengeTwiceJob: |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 454 | set_load_state(LOAD_STATE_CONNECTING); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 455 | DoAdvanceAuthChallenge(2, true /* succeed_after_last_challenge */); |
| 456 | return ERR_IO_PENDING; |
| 457 | case kMockAuthChallengeOnceFailingJob: |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 458 | set_load_state(LOAD_STATE_CONNECTING); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 459 | DoAdvanceAuthChallenge(1, false /* succeed_after_last_challenge */); |
| 460 | return ERR_IO_PENDING; |
| 461 | case kMockAuthChallengeTwiceFailingJob: |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 462 | set_load_state(LOAD_STATE_CONNECTING); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 463 | DoAdvanceAuthChallenge(2, false /* succeed_after_last_challenge */); |
| 464 | return ERR_IO_PENDING; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 465 | default: |
| 466 | NOTREACHED(); |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 467 | SetSocket(std::unique_ptr<StreamSocket>()); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 468 | return ERR_FAILED; |
| 469 | } |
| 470 | } |
| 471 | |
Lily Chen | 02ef29a | 2018-11-30 16:31:43 | [diff] [blame] | 472 | void ChangePriorityInternal(RequestPriority priority) override {} |
| 473 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 474 | int DoConnect(bool succeed, bool was_async, bool cert_error) { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 475 | int result = OK; |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 476 | has_established_connection_ = true; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 477 | if (succeed) { |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 478 | SetSocket(std::make_unique<MockClientSocket>(net_log().net_log())); |
Bence Béky | bdbb0e7 | 2018-08-07 21:42:59 | [diff] [blame] | 479 | socket()->Connect(CompletionOnceCallback()); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 480 | } else if (cert_error) { |
| 481 | SetSocket(std::make_unique<MockClientSocket>(net_log().net_log())); |
| 482 | result = ERR_CERT_COMMON_NAME_INVALID; |
[email protected] | 6e713f0 | 2009-08-06 02:56:40 | [diff] [blame] | 483 | } else { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 484 | result = ERR_CONNECTION_FAILED; |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 485 | SetSocket(std::unique_ptr<StreamSocket>()); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 486 | } |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 487 | |
| 488 | if (was_async) |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 489 | NotifyDelegateOfCompletion(result); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 490 | return result; |
| 491 | } |
| 492 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 493 | void DoAdvanceAuthChallenge(int remaining_challenges, |
| 494 | bool succeed_after_last_challenge) { |
| 495 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 496 | FROM_HERE, |
| 497 | base::BindOnce(&TestConnectJob::InvokeNextProxyAuthCallback, |
| 498 | weak_factory_.GetWeakPtr(), remaining_challenges, |
| 499 | succeed_after_last_challenge)); |
| 500 | } |
| 501 | |
| 502 | void InvokeNextProxyAuthCallback(int remaining_challenges, |
| 503 | bool succeed_after_last_challenge) { |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 504 | set_load_state(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 505 | if (remaining_challenges == 0) { |
| 506 | DoConnect(succeed_after_last_challenge, true /* was_async */, |
| 507 | false /* cert_error */); |
| 508 | return; |
| 509 | } |
| 510 | |
| 511 | // Integration tests make sure HttpResponseInfo and HttpAuthController work. |
| 512 | // The auth tests here are just focused on ConnectJob bookkeeping. |
| 513 | HttpResponseInfo info; |
| 514 | NotifyDelegateOfProxyAuth( |
| 515 | info, nullptr /* http_auth_controller */, |
| 516 | base::BindOnce(&TestConnectJob::DoAdvanceAuthChallenge, |
| 517 | weak_factory_.GetWeakPtr(), remaining_challenges - 1, |
| 518 | succeed_after_last_challenge)); |
| 519 | } |
| 520 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 521 | bool waiting_success_; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 522 | const JobType job_type_; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 523 | MockClientSocketFactory* const client_socket_factory_; |
[email protected] | 4645135 | 2009-09-01 14:54:21 | [diff] [blame] | 524 | LoadState load_state_; |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 525 | bool has_established_connection_; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 526 | bool store_additional_error_state_; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 527 | |
[email protected] | d5492c5 | 2013-11-10 20:44:39 | [diff] [blame] | 528 | base::WeakPtrFactory<TestConnectJob> weak_factory_; |
| 529 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 530 | DISALLOW_COPY_AND_ASSIGN(TestConnectJob); |
| 531 | }; |
| 532 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 533 | class TestConnectJobFactory |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 534 | : public TransportClientSocketPool::ConnectJobFactory { |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 535 | public: |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 536 | TestConnectJobFactory(MockClientSocketFactory* client_socket_factory, |
| 537 | NetLog* net_log) |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 538 | : common_connect_job_params_( |
| 539 | nullptr /* client_socket_factory */, |
| 540 | nullptr /* host_resolver */, |
Matt Menke | b88837e | 2019-03-20 11:50:40 | [diff] [blame] | 541 | nullptr /* http_auth_cache */, |
| 542 | nullptr /* http_auth_handler_factory */, |
| 543 | nullptr /* spdy_session_pool */, |
Matt Menke | b5fb42b | 2019-03-22 17:26:13 | [diff] [blame] | 544 | nullptr /* quic_supported_versions */, |
Matt Menke | b88837e | 2019-03-20 11:50:40 | [diff] [blame] | 545 | nullptr /* quic_stream_factory */, |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 546 | nullptr /* proxy_delegate */, |
Matt Menke | d732ea4 | 2019-03-08 12:05:00 | [diff] [blame] | 547 | nullptr /* http_user_agent_settings */, |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 548 | SSLClientSocketContext(), |
| 549 | SSLClientSocketContext(), |
| 550 | nullptr /* socket_performance_watcher_factory */, |
| 551 | nullptr /* network_quality_estimator */, |
| 552 | net_log, |
| 553 | nullptr /* websocket_endpoint_lock_manager */), |
| 554 | job_type_(TestConnectJob::kMockJob), |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 555 | job_types_(nullptr), |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 556 | client_socket_factory_(client_socket_factory) {} |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 557 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 558 | ~TestConnectJobFactory() override = default; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 559 | |
| 560 | void set_job_type(TestConnectJob::JobType job_type) { job_type_ = job_type; } |
| 561 | |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 562 | void set_job_types(std::list<TestConnectJob::JobType>* job_types) { |
| 563 | job_types_ = job_types; |
| 564 | CHECK(!job_types_->empty()); |
| 565 | } |
| 566 | |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 567 | void set_timeout_duration(base::TimeDelta timeout_duration) { |
| 568 | timeout_duration_ = timeout_duration; |
| 569 | } |
| 570 | |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 571 | // ConnectJobFactory implementation. |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 572 | |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 573 | std::unique_ptr<ConnectJob> NewConnectJob( |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 574 | RequestPriority request_priority, |
| 575 | SocketTag socket_tag, |
| 576 | scoped_refptr<ClientSocketPool::SocketParams> socket_params, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 577 | ConnectJob::Delegate* delegate) const override { |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 578 | EXPECT_TRUE(!job_types_ || !job_types_->empty()); |
| 579 | TestConnectJob::JobType job_type = job_type_; |
| 580 | if (job_types_ && !job_types_->empty()) { |
| 581 | job_type = job_types_->front(); |
| 582 | job_types_->pop_front(); |
| 583 | } |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 584 | return std::make_unique<TestConnectJob>( |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 585 | job_type, request_priority, socket_tag, timeout_duration_, |
| 586 | &common_connect_job_params_, delegate, client_socket_factory_); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | private: |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 590 | const CommonConnectJobParams common_connect_job_params_; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 591 | TestConnectJob::JobType job_type_; |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 592 | std::list<TestConnectJob::JobType>* job_types_; |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 593 | base::TimeDelta timeout_duration_; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 594 | MockClientSocketFactory* const client_socket_factory_; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 595 | |
| 596 | DISALLOW_COPY_AND_ASSIGN(TestConnectJobFactory); |
| 597 | }; |
| 598 | |
[email protected] | a937a06d | 2009-08-19 21:19:24 | [diff] [blame] | 599 | } // namespace |
| 600 | |
[email protected] | a937a06d | 2009-08-19 21:19:24 | [diff] [blame] | 601 | namespace { |
| 602 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 603 | void MockClientSocketFactory::SignalJobs() { |
jdoerrie | 22a91d8b9 | 2018-10-05 08:43:26 | [diff] [blame] | 604 | for (auto it = waiting_jobs_.begin(); it != waiting_jobs_.end(); ++it) { |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 605 | (*it)->Signal(); |
| 606 | } |
| 607 | waiting_jobs_.clear(); |
| 608 | } |
| 609 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 610 | void MockClientSocketFactory::SignalJob(size_t job) { |
| 611 | ASSERT_LT(job, waiting_jobs_.size()); |
| 612 | waiting_jobs_[job]->Signal(); |
| 613 | waiting_jobs_.erase(waiting_jobs_.begin() + job); |
| 614 | } |
| 615 | |
| 616 | void MockClientSocketFactory::SetJobLoadState(size_t job, |
| 617 | LoadState load_state) { |
| 618 | ASSERT_LT(job, waiting_jobs_.size()); |
| 619 | waiting_jobs_[job]->set_load_state(load_state); |
| 620 | } |
| 621 | |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 622 | void MockClientSocketFactory::SetJobHasEstablishedConnection(size_t job) { |
| 623 | ASSERT_LT(job, waiting_jobs_.size()); |
| 624 | waiting_jobs_[job]->set_has_established_connection(); |
| 625 | } |
| 626 | |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 627 | class ClientSocketPoolBaseTest : public TestWithScopedTaskEnvironment { |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 628 | protected: |
Alex Clarke | 0def209 | 2018-12-10 12:01:45 | [diff] [blame] | 629 | ClientSocketPoolBaseTest() |
| 630 | : TestWithScopedTaskEnvironment( |
| 631 | base::test::ScopedTaskEnvironment::MainThreadType::MOCK_TIME), |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 632 | params_(CreateDummyParams()) { |
[email protected] | 636b825 | 2011-04-08 19:56:54 | [diff] [blame] | 633 | connect_backup_jobs_enabled_ = |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 634 | TransportClientSocketPool::connect_backup_jobs_enabled(); |
| 635 | TransportClientSocketPool::set_connect_backup_jobs_enabled(true); |
[email protected] | 636b825 | 2011-04-08 19:56:54 | [diff] [blame] | 636 | } |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 637 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 638 | ~ClientSocketPoolBaseTest() override { |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 639 | TransportClientSocketPool::set_connect_backup_jobs_enabled( |
[email protected] | 636b825 | 2011-04-08 19:56:54 | [diff] [blame] | 640 | connect_backup_jobs_enabled_); |
| 641 | } |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 642 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 643 | void CreatePool(int max_sockets, |
| 644 | int max_sockets_per_group, |
| 645 | bool enable_backup_connect_jobs = false) { |
Tarun Bansal | a763509 | 2019-02-20 10:00:59 | [diff] [blame] | 646 | CreatePoolWithIdleTimeouts(max_sockets, max_sockets_per_group, |
| 647 | kUnusedIdleSocketTimeout, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 648 | ClientSocketPool::used_idle_socket_timeout(), |
| 649 | enable_backup_connect_jobs); |
[email protected] | 9bf28db | 2009-08-29 01:35:16 | [diff] [blame] | 650 | } |
| 651 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 652 | void CreatePoolWithIdleTimeouts(int max_sockets, |
| 653 | int max_sockets_per_group, |
| 654 | base::TimeDelta unused_idle_socket_timeout, |
| 655 | base::TimeDelta used_idle_socket_timeout, |
| 656 | bool enable_backup_connect_jobs = false) { |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 657 | DCHECK(!pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 658 | std::unique_ptr<TestConnectJobFactory> connect_job_factory = |
| 659 | std::make_unique<TestConnectJobFactory>(&client_socket_factory_, |
| 660 | &net_log_); |
| 661 | connect_job_factory_ = connect_job_factory.get(); |
| 662 | pool_ = TransportClientSocketPool::CreateForTesting( |
| 663 | max_sockets, max_sockets_per_group, unused_idle_socket_timeout, |
| 664 | used_idle_socket_timeout, std::move(connect_job_factory), |
| 665 | nullptr /* ssl_config_service */, enable_backup_connect_jobs); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 666 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 667 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 668 | int StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 669 | const ClientSocketPool::GroupId& group_id, |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 670 | RequestPriority priority, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 671 | ClientSocketPool::RespectLimits respect_limits) { |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 672 | return test_base_.StartRequestUsingPool(pool_.get(), group_id, priority, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 673 | respect_limits, params_); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 674 | } |
| 675 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 676 | int StartRequest(const ClientSocketPool::GroupId& group_id, |
| 677 | RequestPriority priority) { |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 678 | return StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 679 | group_id, priority, ClientSocketPool::RespectLimits::ENABLED); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 680 | } |
| 681 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 682 | int GetOrderOfRequest(size_t index) const { |
| 683 | return test_base_.GetOrderOfRequest(index); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 684 | } |
| 685 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 686 | bool ReleaseOneConnection(ClientSocketPoolTest::KeepAlive keep_alive) { |
| 687 | return test_base_.ReleaseOneConnection(keep_alive); |
| 688 | } |
| 689 | |
| 690 | void ReleaseAllConnections(ClientSocketPoolTest::KeepAlive keep_alive) { |
| 691 | test_base_.ReleaseAllConnections(keep_alive); |
| 692 | } |
| 693 | |
| 694 | TestSocketRequest* request(int i) { return test_base_.request(i); } |
| 695 | size_t requests_size() const { return test_base_.requests_size(); } |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 696 | std::vector<std::unique_ptr<TestSocketRequest>>* requests() { |
olli.raula | 9d66b7d | 2015-11-23 08:30:42 | [diff] [blame] | 697 | return test_base_.requests(); |
| 698 | } |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 699 | // Only counts the requests that get sockets asynchronously; |
| 700 | // synchronous completions are not registered by this count. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 701 | size_t completion_count() const { return test_base_.completion_count(); } |
| 702 | |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 703 | TestNetLog net_log_; |
[email protected] | 636b825 | 2011-04-08 19:56:54 | [diff] [blame] | 704 | bool connect_backup_jobs_enabled_; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 705 | MockClientSocketFactory client_socket_factory_; |
[email protected] | 17a0c6c | 2009-08-04 00:07:04 | [diff] [blame] | 706 | TestConnectJobFactory* connect_job_factory_; |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 707 | // These parameters are never actually used to create a TransportConnectJob. |
Matt Menke | 84d11e56 | 2019-03-27 00:11:19 | [diff] [blame] | 708 | scoped_refptr<ClientSocketPool::SocketParams> params_; |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 709 | std::unique_ptr<TransportClientSocketPool> pool_; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 710 | ClientSocketPoolTest test_base_; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 711 | }; |
| 712 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 713 | TEST_F(ClientSocketPoolBaseTest, BasicSynchronous) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 714 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 715 | |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 716 | TestCompletionCallback callback; |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 717 | ClientSocketHandle handle; |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 718 | BoundTestNetLog log; |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 719 | TestLoadTimingInfoNotConnected(handle); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 720 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 721 | EXPECT_EQ( |
| 722 | OK, handle.Init( |
| 723 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
| 724 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 725 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound())); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 726 | EXPECT_TRUE(handle.is_initialized()); |
| 727 | EXPECT_TRUE(handle.socket()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 728 | TestLoadTimingInfoConnectedNotReused(handle); |
| 729 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 730 | handle.Reset(); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 731 | TestLoadTimingInfoNotConnected(handle); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 732 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 733 | TestNetLogEntry::List entries; |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 734 | log.GetEntries(&entries); |
| 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 | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 763 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 764 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 765 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 766 | 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 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 772 | TestNetLogEntry::List entries; |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 773 | log.GetEntries(&entries); |
| 774 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 775 | EXPECT_EQ(4u, entries.size()); |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 776 | EXPECT_TRUE(LogContainsEvent( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 777 | entries, 0, NetLogEventType::TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 778 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 779 | EXPECT_TRUE(LogContainsBeginEvent(entries, 1, NetLogEventType::SOCKET_POOL)); |
| 780 | EXPECT_TRUE(LogContainsEvent( |
| 781 | entries, 2, NetLogEventType::SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
| 782 | NetLogEventPhase::NONE)); |
| 783 | EXPECT_TRUE(LogContainsEndEvent(entries, 3, NetLogEventType::SOCKET_POOL)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 784 | } |
| 785 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 786 | // Make sure different groups do not share sockets. |
| 787 | TEST_F(ClientSocketPoolBaseTest, GroupSeparation) { |
| 788 | CreatePool(1000 /* max_sockets */, 2 /* max_sockets_per_group */); |
| 789 | |
| 790 | const HostPortPair kHostPortPairs[] = { |
| 791 | {"a", 80}, |
| 792 | {"a", 443}, |
| 793 | {"b", 80}, |
| 794 | }; |
| 795 | |
| 796 | const ClientSocketPool::SocketType kSocketTypes[] = { |
| 797 | ClientSocketPool::SocketType::kHttp, |
| 798 | ClientSocketPool::SocketType::kSsl, |
| 799 | ClientSocketPool::SocketType::kSslVersionInterferenceProbe, |
| 800 | ClientSocketPool::SocketType::kFtp, |
| 801 | }; |
| 802 | |
| 803 | const bool kPrivacyModes[] = {false, true}; |
| 804 | |
| 805 | int total_idle_sockets = 0; |
| 806 | |
| 807 | // Walk through each GroupId, making sure that requesting a socket for one |
| 808 | // group does not return a previously connected socket for another group. |
| 809 | for (const auto& host_port_pair : kHostPortPairs) { |
| 810 | SCOPED_TRACE(host_port_pair.ToString()); |
| 811 | for (const auto& socket_type : kSocketTypes) { |
| 812 | SCOPED_TRACE(static_cast<int>(socket_type)); |
| 813 | for (const auto& privacy_mode : kPrivacyModes) { |
| 814 | SCOPED_TRACE(privacy_mode); |
| 815 | |
| 816 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 817 | |
| 818 | ClientSocketPool::GroupId group_id(host_port_pair, socket_type, |
| 819 | privacy_mode); |
| 820 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 821 | EXPECT_FALSE(pool_->HasGroupForTesting(group_id)); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 822 | |
| 823 | TestCompletionCallback callback; |
| 824 | ClientSocketHandle handle; |
| 825 | |
| 826 | // Since the group is empty, requesting a socket should not complete |
| 827 | // synchronously. |
| 828 | EXPECT_THAT( |
| 829 | handle.Init(group_id, params_, DEFAULT_PRIORITY, SocketTag(), |
| 830 | ClientSocketPool::RespectLimits::ENABLED, |
| 831 | callback.callback(), |
| 832 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 833 | NetLogWithSource()), |
| 834 | IsError(ERR_IO_PENDING)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 835 | EXPECT_TRUE(pool_->HasGroupForTesting(group_id)); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 836 | EXPECT_EQ(total_idle_sockets, pool_->IdleSocketCount()); |
| 837 | |
| 838 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 839 | EXPECT_TRUE(handle.socket()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 840 | EXPECT_TRUE(pool_->HasGroupForTesting(group_id)); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 841 | EXPECT_EQ(total_idle_sockets, pool_->IdleSocketCount()); |
| 842 | |
| 843 | // Return socket to pool. |
| 844 | handle.Reset(); |
| 845 | EXPECT_EQ(total_idle_sockets + 1, pool_->IdleSocketCount()); |
| 846 | |
| 847 | // Requesting a socket again should return the same socket as before, so |
| 848 | // should complete synchronously. |
| 849 | EXPECT_THAT( |
| 850 | handle.Init(group_id, params_, DEFAULT_PRIORITY, SocketTag(), |
| 851 | ClientSocketPool::RespectLimits::ENABLED, |
| 852 | callback.callback(), |
| 853 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 854 | NetLogWithSource()), |
| 855 | IsOk()); |
| 856 | EXPECT_TRUE(handle.socket()); |
| 857 | EXPECT_EQ(total_idle_sockets, pool_->IdleSocketCount()); |
| 858 | |
| 859 | // Return socket to pool again. |
| 860 | handle.Reset(); |
| 861 | EXPECT_EQ(total_idle_sockets + 1, pool_->IdleSocketCount()); |
| 862 | |
| 863 | ++total_idle_sockets; |
| 864 | } |
| 865 | } |
| 866 | } |
| 867 | } |
| 868 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 869 | TEST_F(ClientSocketPoolBaseTest, TotalLimit) { |
| 870 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 871 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 872 | // TODO(eroman): Check that the NetLog contains this event. |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 873 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 874 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 875 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
| 876 | EXPECT_THAT(StartRequest(TestGroupId("c"), DEFAULT_PRIORITY), IsOk()); |
| 877 | EXPECT_THAT(StartRequest(TestGroupId("d"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 878 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 879 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 880 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 881 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 882 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 883 | EXPECT_THAT(StartRequest(TestGroupId("e"), DEFAULT_PRIORITY), |
| 884 | IsError(ERR_IO_PENDING)); |
| 885 | EXPECT_THAT(StartRequest(TestGroupId("f"), DEFAULT_PRIORITY), |
| 886 | IsError(ERR_IO_PENDING)); |
| 887 | EXPECT_THAT(StartRequest(TestGroupId("g"), DEFAULT_PRIORITY), |
| 888 | IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 889 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 890 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 891 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 892 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 893 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 894 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 895 | |
| 896 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 897 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 898 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 899 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 900 | EXPECT_EQ(5, GetOrderOfRequest(5)); |
| 901 | EXPECT_EQ(6, GetOrderOfRequest(6)); |
| 902 | EXPECT_EQ(7, GetOrderOfRequest(7)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 903 | |
| 904 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 905 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(8)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 906 | } |
| 907 | |
| 908 | TEST_F(ClientSocketPoolBaseTest, TotalLimitReachedNewGroup) { |
| 909 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 910 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 911 | // TODO(eroman): Check that the NetLog contains this event. |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 912 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 913 | // Reach all limits: max total sockets, and max sockets per group. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 914 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 915 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 916 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
| 917 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 918 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 919 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 920 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 921 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 922 | |
| 923 | // Now create a new group and verify that we don't starve it. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 924 | EXPECT_THAT(StartRequest(TestGroupId("c"), DEFAULT_PRIORITY), |
| 925 | IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 926 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 927 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 928 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 929 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 930 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 931 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 932 | |
| 933 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 934 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 935 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 936 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 937 | EXPECT_EQ(5, GetOrderOfRequest(5)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 938 | |
| 939 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 940 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(6)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | TEST_F(ClientSocketPoolBaseTest, TotalLimitRespectsPriority) { |
| 944 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 945 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 946 | EXPECT_THAT(StartRequest(TestGroupId("b"), LOWEST), IsOk()); |
| 947 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsOk()); |
| 948 | EXPECT_THAT(StartRequest(TestGroupId("b"), HIGHEST), IsOk()); |
| 949 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 950 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 951 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 952 | client_socket_factory_.allocation_count()); |
| 953 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 954 | EXPECT_THAT(StartRequest(TestGroupId("c"), LOWEST), IsError(ERR_IO_PENDING)); |
| 955 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 956 | EXPECT_THAT(StartRequest(TestGroupId("b"), HIGHEST), IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 957 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 958 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 959 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 960 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 961 | |
| 962 | // First 4 requests don't have to wait, and finish in order. |
| 963 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 964 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 965 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 966 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 967 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 968 | // Request ("b", HIGHEST) has the highest priority, then (TestGroupId("a"), |
| 969 | // MEDIUM), and then ("c", LOWEST). |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 970 | EXPECT_EQ(7, GetOrderOfRequest(5)); |
| 971 | EXPECT_EQ(6, GetOrderOfRequest(6)); |
| 972 | EXPECT_EQ(5, GetOrderOfRequest(7)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 973 | |
| 974 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 975 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(9)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 976 | } |
| 977 | |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 978 | // Test reprioritizing a request before completion doesn't interfere with |
| 979 | // its completion. |
| 980 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeOne) { |
| 981 | CreatePool(kDefaultMaxSockets, 1); |
| 982 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 983 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 984 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 985 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 986 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 987 | |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 988 | request(1)->handle()->SetPriority(HIGHEST); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 989 | |
| 990 | ReleaseOneConnection(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 991 | |
| 992 | EXPECT_TRUE(request(1)->handle()->socket()); |
| 993 | } |
| 994 | |
| 995 | // Reprioritize a request up past another one and make sure that changes the |
| 996 | // completion order. |
| 997 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeUpReorder) { |
| 998 | CreatePool(kDefaultMaxSockets, 1); |
| 999 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1000 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1001 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1002 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1003 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1004 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1005 | EXPECT_FALSE(request(2)->handle()->socket()); |
| 1006 | |
| 1007 | request(2)->handle()->SetPriority(HIGHEST); |
| 1008 | |
| 1009 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1010 | |
| 1011 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1012 | EXPECT_EQ(3, GetOrderOfRequest(2)); |
| 1013 | EXPECT_EQ(2, GetOrderOfRequest(3)); |
| 1014 | } |
| 1015 | |
| 1016 | // Reprioritize a request without changing relative priorities and check |
| 1017 | // that the order doesn't change. |
| 1018 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeUpNoReorder) { |
| 1019 | CreatePool(kDefaultMaxSockets, 1); |
| 1020 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1021 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1022 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1023 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1024 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1025 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1026 | EXPECT_FALSE(request(2)->handle()->socket()); |
| 1027 | |
| 1028 | request(2)->handle()->SetPriority(MEDIUM); |
| 1029 | |
| 1030 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1031 | |
| 1032 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1033 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1034 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 1035 | } |
| 1036 | |
| 1037 | // Reprioritize a request past down another one and make sure that changes the |
| 1038 | // completion order. |
| 1039 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeDownReorder) { |
| 1040 | CreatePool(kDefaultMaxSockets, 1); |
| 1041 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1042 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1043 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1044 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1045 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1046 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1047 | EXPECT_FALSE(request(2)->handle()->socket()); |
| 1048 | |
| 1049 | request(1)->handle()->SetPriority(LOW); |
| 1050 | |
| 1051 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1052 | |
| 1053 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1054 | EXPECT_EQ(3, GetOrderOfRequest(2)); |
| 1055 | EXPECT_EQ(2, GetOrderOfRequest(3)); |
| 1056 | } |
| 1057 | |
| 1058 | // Reprioritize a request to the same level as another and confirm it is |
| 1059 | // put after the old request. |
| 1060 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeResetFIFO) { |
| 1061 | CreatePool(kDefaultMaxSockets, 1); |
| 1062 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1063 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1064 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1065 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1066 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1067 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1068 | EXPECT_FALSE(request(2)->handle()->socket()); |
| 1069 | |
| 1070 | request(1)->handle()->SetPriority(MEDIUM); |
| 1071 | |
| 1072 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1073 | |
| 1074 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1075 | EXPECT_EQ(3, GetOrderOfRequest(2)); |
| 1076 | EXPECT_EQ(2, GetOrderOfRequest(3)); |
| 1077 | } |
| 1078 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1079 | TEST_F(ClientSocketPoolBaseTest, TotalLimitRespectsGroupLimit) { |
| 1080 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1081 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1082 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsOk()); |
| 1083 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsOk()); |
| 1084 | EXPECT_THAT(StartRequest(TestGroupId("b"), HIGHEST), IsOk()); |
| 1085 | EXPECT_THAT(StartRequest(TestGroupId("b"), MEDIUM), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1086 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1087 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1088 | client_socket_factory_.allocation_count()); |
| 1089 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1090 | EXPECT_THAT(StartRequest(TestGroupId("c"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1091 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 1092 | EXPECT_THAT(StartRequest(TestGroupId("b"), HIGHEST), IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1093 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1094 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1095 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1096 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1097 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1098 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1099 | |
| 1100 | // First 4 requests don't have to wait, and finish in order. |
| 1101 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1102 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1103 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 1104 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 1105 | |
| 1106 | // Request ("b", 7) has the highest priority, but we can't make new socket for |
| 1107 | // group "b", because it has reached the per-group limit. Then we make |
| 1108 | // socket for ("c", 6), because it has higher priority than ("a", 4), |
| 1109 | // and we still can't make a socket for group "b". |
| 1110 | EXPECT_EQ(5, GetOrderOfRequest(5)); |
| 1111 | EXPECT_EQ(6, GetOrderOfRequest(6)); |
| 1112 | EXPECT_EQ(7, GetOrderOfRequest(7)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1113 | |
| 1114 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1115 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(8)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1116 | } |
| 1117 | |
| 1118 | // Make sure that we count connecting sockets against the total limit. |
| 1119 | TEST_F(ClientSocketPoolBaseTest, TotalLimitCountsConnectingSockets) { |
| 1120 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1121 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1122 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1123 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
| 1124 | EXPECT_THAT(StartRequest(TestGroupId("c"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1125 | |
| 1126 | // Create one asynchronous request. |
| 1127 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1128 | EXPECT_THAT(StartRequest(TestGroupId("d"), DEFAULT_PRIORITY), |
| 1129 | IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1130 | |
[email protected] | 6b17538 | 2009-10-13 06:47:47 | [diff] [blame] | 1131 | // We post all of our delayed tasks with a 2ms delay. I.e. they don't |
| 1132 | // actually become pending until 2ms after they have been created. In order |
| 1133 | // to flush all tasks, we need to wait so that we know there are no |
| 1134 | // soon-to-be-pending tasks waiting. |
Alex Clarke | 0def209 | 2018-12-10 12:01:45 | [diff] [blame] | 1135 | FastForwardBy(base::TimeDelta::FromMilliseconds(10)); |
[email protected] | 6b17538 | 2009-10-13 06:47:47 | [diff] [blame] | 1136 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1137 | // The next synchronous request should wait for its turn. |
| 1138 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1139 | EXPECT_THAT(StartRequest(TestGroupId("e"), DEFAULT_PRIORITY), |
| 1140 | IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1141 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1142 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1143 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1144 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1145 | client_socket_factory_.allocation_count()); |
| 1146 | |
| 1147 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1148 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1149 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 1150 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1151 | EXPECT_EQ(5, GetOrderOfRequest(5)); |
| 1152 | |
| 1153 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1154 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(6)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1155 | } |
| 1156 | |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1157 | TEST_F(ClientSocketPoolBaseTest, CorrectlyCountStalledGroups) { |
| 1158 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 1159 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 1160 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1161 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1162 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1163 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1164 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1165 | |
| 1166 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 1167 | |
| 1168 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1169 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1170 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), |
| 1171 | IsError(ERR_IO_PENDING)); |
| 1172 | EXPECT_THAT(StartRequest(TestGroupId("c"), DEFAULT_PRIORITY), |
| 1173 | IsError(ERR_IO_PENDING)); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1174 | |
| 1175 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1176 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1177 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1178 | EXPECT_EQ(kDefaultMaxSockets + 1, client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1179 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1180 | EXPECT_EQ(kDefaultMaxSockets + 2, client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1181 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
| 1182 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1183 | EXPECT_EQ(kDefaultMaxSockets + 2, client_socket_factory_.allocation_count()); |
| 1184 | } |
| 1185 | |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 1186 | TEST_F(ClientSocketPoolBaseTest, StallAndThenCancelAndTriggerAvailableSocket) { |
| 1187 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 1188 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1189 | |
| 1190 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1191 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1192 | EXPECT_EQ( |
| 1193 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1194 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1195 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1196 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 1197 | NetLogWithSource())); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 1198 | |
| 1199 | ClientSocketHandle handles[4]; |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 1200 | for (size_t i = 0; i < base::size(handles); ++i) { |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1201 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1202 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1203 | handles[i].Init( |
| 1204 | TestGroupId("b"), params_, DEFAULT_PRIORITY, SocketTag(), |
| 1205 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1206 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 1207 | NetLogWithSource())); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 1208 | } |
| 1209 | |
| 1210 | // One will be stalled, cancel all the handles now. |
| 1211 | // This should hit the OnAvailableSocketSlot() code where we previously had |
| 1212 | // stalled groups, but no longer have any. |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 1213 | for (size_t i = 0; i < base::size(handles); ++i) |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 1214 | handles[i].Reset(); |
| 1215 | } |
| 1216 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1217 | TEST_F(ClientSocketPoolBaseTest, CancelStalledSocketAtSocketLimit) { |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1218 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1219 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 1220 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1221 | { |
| 1222 | ClientSocketHandle handles[kDefaultMaxSockets]; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1223 | TestCompletionCallback callbacks[kDefaultMaxSockets]; |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1224 | for (int i = 0; i < kDefaultMaxSockets; ++i) { |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1225 | EXPECT_EQ(OK, handles[i].Init(TestGroupId(base::NumberToString(i)), |
| 1226 | params_, DEFAULT_PRIORITY, SocketTag(), |
Paul Jensen | 8d6f87ec | 2018-01-13 00:46:54 | [diff] [blame] | 1227 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1228 | callbacks[i].callback(), |
| 1229 | ClientSocketPool::ProxyAuthCallback(), |
| 1230 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1231 | } |
| 1232 | |
| 1233 | // Force a stalled group. |
| 1234 | ClientSocketHandle stalled_handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1235 | TestCompletionCallback callback; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1236 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1237 | stalled_handle.Init( |
| 1238 | TestGroupId("foo"), params_, DEFAULT_PRIORITY, SocketTag(), |
| 1239 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1240 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 1241 | NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1242 | |
| 1243 | // Cancel the stalled request. |
| 1244 | stalled_handle.Reset(); |
| 1245 | |
| 1246 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1247 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 1248 | |
| 1249 | // Dropping out of scope will close all handles and return them to idle. |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1250 | } |
| 1251 | |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1252 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1253 | EXPECT_EQ(kDefaultMaxSockets, pool_->IdleSocketCount()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1254 | } |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1255 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1256 | TEST_F(ClientSocketPoolBaseTest, CancelPendingSocketAtSocketLimit) { |
| 1257 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1258 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 1259 | |
| 1260 | { |
| 1261 | ClientSocketHandle handles[kDefaultMaxSockets]; |
| 1262 | for (int i = 0; i < kDefaultMaxSockets; ++i) { |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1263 | TestCompletionCallback callback; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1264 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1265 | handles[i].Init(TestGroupId(base::NumberToString(i)), params_, |
| 1266 | DEFAULT_PRIORITY, SocketTag(), |
| 1267 | ClientSocketPool::RespectLimits::ENABLED, |
| 1268 | callback.callback(), |
| 1269 | ClientSocketPool::ProxyAuthCallback(), |
| 1270 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1271 | } |
| 1272 | |
| 1273 | // Force a stalled group. |
| 1274 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1275 | ClientSocketHandle stalled_handle; |
[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 | stalled_handle.Init( |
| 1279 | TestGroupId("foo"), params_, DEFAULT_PRIORITY, SocketTag(), |
| 1280 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1281 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 1282 | NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1283 | |
| 1284 | // Since it is stalled, it should have no connect jobs. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1285 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("foo"))); |
| 1286 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 1287 | TestGroupId("foo"))); |
| 1288 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroupForTesting( |
| 1289 | TestGroupId("foo"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1290 | |
| 1291 | // Cancel the stalled request. |
| 1292 | handles[0].Reset(); |
| 1293 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1294 | // Now we should have a connect job. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1295 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("foo"))); |
| 1296 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 1297 | TestGroupId("foo"))); |
| 1298 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroupForTesting( |
| 1299 | TestGroupId("foo"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1300 | |
| 1301 | // The stalled socket should connect. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1302 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1303 | |
| 1304 | EXPECT_EQ(kDefaultMaxSockets + 1, |
| 1305 | client_socket_factory_.allocation_count()); |
| 1306 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1307 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("foo"))); |
| 1308 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 1309 | TestGroupId("foo"))); |
| 1310 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroupForTesting( |
| 1311 | TestGroupId("foo"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1312 | |
| 1313 | // Dropping out of scope will close all handles and return them to idle. |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1314 | } |
| 1315 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1316 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 1317 | } |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1318 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1319 | TEST_F(ClientSocketPoolBaseTest, WaitForStalledSocketAtSocketLimit) { |
| 1320 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1321 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1322 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1323 | ClientSocketHandle stalled_handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1324 | TestCompletionCallback callback; |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1325 | { |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 1326 | EXPECT_FALSE(pool_->IsStalled()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1327 | ClientSocketHandle handles[kDefaultMaxSockets]; |
| 1328 | for (int i = 0; i < kDefaultMaxSockets; ++i) { |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1329 | TestCompletionCallback callback; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1330 | EXPECT_EQ( |
| 1331 | OK, handles[i].Init(TestGroupId(base::StringPrintf("Take 2: %d", i)), |
| 1332 | params_, DEFAULT_PRIORITY, SocketTag(), |
| 1333 | ClientSocketPool::RespectLimits::ENABLED, |
| 1334 | callback.callback(), |
| 1335 | ClientSocketPool::ProxyAuthCallback(), |
| 1336 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1337 | } |
| 1338 | |
| 1339 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1340 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 1341 | EXPECT_FALSE(pool_->IsStalled()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1342 | |
| 1343 | // Now we will hit the socket limit. |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1344 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1345 | stalled_handle.Init( |
| 1346 | TestGroupId("foo"), params_, DEFAULT_PRIORITY, SocketTag(), |
| 1347 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1348 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 1349 | NetLogWithSource())); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 1350 | EXPECT_TRUE(pool_->IsStalled()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1351 | |
| 1352 | // Dropping out of scope will close all handles and return them to idle. |
| 1353 | } |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1354 | |
| 1355 | // But if we wait for it, the released idle sockets will be closed in |
| 1356 | // preference of the waiting request. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1357 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1358 | |
| 1359 | EXPECT_EQ(kDefaultMaxSockets + 1, client_socket_factory_.allocation_count()); |
| 1360 | EXPECT_EQ(3, pool_->IdleSocketCount()); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1361 | } |
| 1362 | |
| 1363 | // Regression test for http://crbug.com/40952. |
| 1364 | TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketAtSocketLimitDeleteGroup) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1365 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 1366 | true /* enable_backup_connect_jobs */); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1367 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 1368 | |
| 1369 | for (int i = 0; i < kDefaultMaxSockets; ++i) { |
| 1370 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1371 | TestCompletionCallback callback; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1372 | EXPECT_EQ(OK, handle.Init(TestGroupId(base::NumberToString(i)), params_, |
| 1373 | DEFAULT_PRIORITY, SocketTag(), |
| 1374 | ClientSocketPool::RespectLimits::ENABLED, |
| 1375 | callback.callback(), |
| 1376 | ClientSocketPool::ProxyAuthCallback(), |
| 1377 | pool_.get(), NetLogWithSource())); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1378 | } |
| 1379 | |
| 1380 | // Flush all the DoReleaseSocket tasks. |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 1381 | base::RunLoop().RunUntilIdle(); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1382 | |
| 1383 | // Stall a group. Set a pending job so it'll trigger a backup job if we don't |
| 1384 | // reuse a socket. |
| 1385 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1386 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1387 | TestCompletionCallback callback; |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1388 | |
| 1389 | // "0" is special here, since it should be the first entry in the sorted map, |
| 1390 | // which is the one which we would close an idle socket for. We shouldn't |
| 1391 | // close an idle socket though, since we should reuse the idle socket. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1392 | EXPECT_EQ(OK, handle.Init( |
| 1393 | TestGroupId("0"), params_, DEFAULT_PRIORITY, SocketTag(), |
| 1394 | ClientSocketPool::RespectLimits::ENABLED, |
| 1395 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1396 | pool_.get(), NetLogWithSource())); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1397 | |
| 1398 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1399 | EXPECT_EQ(kDefaultMaxSockets - 1, pool_->IdleSocketCount()); |
| 1400 | } |
| 1401 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1402 | TEST_F(ClientSocketPoolBaseTest, PendingRequests) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1403 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1404 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1405 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1406 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1407 | EXPECT_THAT(StartRequest(TestGroupId("a"), IDLE), IsError(ERR_IO_PENDING)); |
| 1408 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
| 1409 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1410 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1411 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 1412 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1413 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1414 | ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1415 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
| 1416 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1417 | EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup, |
| 1418 | completion_count()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1419 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1420 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1421 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
[email protected] | c9c6f5c | 2010-07-31 01:30:03 | [diff] [blame] | 1422 | EXPECT_EQ(8, GetOrderOfRequest(3)); |
| 1423 | EXPECT_EQ(6, GetOrderOfRequest(4)); |
| 1424 | EXPECT_EQ(4, GetOrderOfRequest(5)); |
| 1425 | EXPECT_EQ(3, GetOrderOfRequest(6)); |
| 1426 | EXPECT_EQ(5, GetOrderOfRequest(7)); |
| 1427 | EXPECT_EQ(7, GetOrderOfRequest(8)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1428 | |
| 1429 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1430 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(9)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1431 | } |
| 1432 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1433 | TEST_F(ClientSocketPoolBaseTest, PendingRequests_NoKeepAlive) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1434 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1435 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1436 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1437 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1438 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
| 1439 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1440 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1441 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 1442 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1443 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1444 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1445 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1446 | for (size_t i = kDefaultMaxSocketsPerGroup; i < requests_size(); ++i) |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1447 | EXPECT_THAT(request(i)->WaitForResult(), IsOk()); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1448 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1449 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1450 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1451 | EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup, |
| 1452 | completion_count()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1453 | } |
| 1454 | |
| 1455 | // This test will start up a RequestSocket() and then immediately Cancel() it. |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1456 | // The pending connect job will be cancelled and should not call back into |
| 1457 | // ClientSocketPoolBase. |
| 1458 | TEST_F(ClientSocketPoolBaseTest, CancelRequestClearGroup) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1459 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1460 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1461 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1462 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1463 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1464 | EXPECT_EQ( |
| 1465 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1466 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1467 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1468 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 1469 | NetLogWithSource())); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1470 | handle.Reset(); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1471 | } |
| 1472 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1473 | TEST_F(ClientSocketPoolBaseTest, ConnectCancelConnect) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1474 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1475 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1476 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 1477 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1478 | TestCompletionCallback callback; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1479 | |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1480 | EXPECT_EQ( |
| 1481 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1482 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1483 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1484 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 1485 | NetLogWithSource())); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1486 | |
| 1487 | handle.Reset(); |
| 1488 | |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1489 | TestCompletionCallback callback2; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1490 | EXPECT_EQ( |
| 1491 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1492 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1493 | ClientSocketPool::RespectLimits::ENABLED, |
| 1494 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1495 | pool_.get(), NetLogWithSource())); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1496 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1497 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1498 | EXPECT_FALSE(callback.have_result()); |
| 1499 | |
| 1500 | handle.Reset(); |
| 1501 | } |
| 1502 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1503 | TEST_F(ClientSocketPoolBaseTest, CancelRequest) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1504 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1505 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1506 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1507 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1508 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
| 1509 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1510 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1511 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 1512 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1513 | |
| 1514 | // Cancel a request. |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1515 | size_t index_to_cancel = kDefaultMaxSocketsPerGroup + 2; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1516 | EXPECT_FALSE((*requests())[index_to_cancel]->handle()->is_initialized()); |
| 1517 | (*requests())[index_to_cancel]->handle()->Reset(); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1518 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1519 | ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1520 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1521 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
| 1522 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1523 | EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup - 1, |
| 1524 | completion_count()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1525 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1526 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1527 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1528 | EXPECT_EQ(5, GetOrderOfRequest(3)); |
| 1529 | EXPECT_EQ(3, GetOrderOfRequest(4)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1530 | EXPECT_EQ(ClientSocketPoolTest::kRequestNotFound, |
| 1531 | GetOrderOfRequest(5)); // Canceled request. |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1532 | EXPECT_EQ(4, GetOrderOfRequest(6)); |
| 1533 | EXPECT_EQ(6, GetOrderOfRequest(7)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1534 | |
| 1535 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1536 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(8)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1537 | } |
| 1538 | |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1539 | // Function to be used as a callback on socket request completion. It first |
| 1540 | // disconnects the successfully connected socket from the first request, and |
| 1541 | // then reuses the ClientSocketHandle to request another socket. |
| 1542 | // |
| 1543 | // |nested_callback| is called with the result of the second socket request. |
| 1544 | void RequestSocketOnComplete(ClientSocketHandle* handle, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1545 | TransportClientSocketPool* pool, |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1546 | TestConnectJobFactory* test_connect_job_factory, |
| 1547 | TestConnectJob::JobType next_job_type, |
Bence Béky | a4a5093 | 2018-08-10 13:39:41 | [diff] [blame] | 1548 | TestCompletionCallback* nested_callback, |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1549 | int first_request_result) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1550 | EXPECT_THAT(first_request_result, IsOk()); |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1551 | |
| 1552 | test_connect_job_factory->set_job_type(next_job_type); |
| 1553 | |
| 1554 | // Don't allow reuse of the socket. Disconnect it and then release it. |
| 1555 | if (handle->socket()) |
| 1556 | handle->socket()->Disconnect(); |
| 1557 | handle->Reset(); |
| 1558 | |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1559 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1560 | int rv = handle->Init( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1561 | TestGroupId("a"), CreateDummyParams(), LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1562 | ClientSocketPool::RespectLimits::ENABLED, nested_callback->callback(), |
| 1563 | ClientSocketPool::ProxyAuthCallback(), pool, NetLogWithSource()); |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1564 | if (rv != ERR_IO_PENDING) { |
| 1565 | DCHECK_EQ(TestConnectJob::kMockJob, next_job_type); |
Bence Béky | a4a5093 | 2018-08-10 13:39:41 | [diff] [blame] | 1566 | nested_callback->callback().Run(rv); |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1567 | } else { |
| 1568 | DCHECK_EQ(TestConnectJob::kMockPendingJob, next_job_type); |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1569 | } |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1570 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1571 | |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1572 | // Tests the case where a second socket is requested in a completion callback, |
| 1573 | // and the second socket connects asynchronously. Reuses the same |
| 1574 | // ClientSocketHandle for the second socket, after disconnecting the first. |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1575 | TEST_F(ClientSocketPoolBaseTest, RequestPendingJobTwice) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1576 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1577 | |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 1578 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 1579 | ClientSocketHandle handle; |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1580 | TestCompletionCallback second_result_callback; |
| 1581 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1582 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Paul Jensen | 8d6f87ec | 2018-01-13 00:46:54 | [diff] [blame] | 1583 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1584 | base::BindOnce(&RequestSocketOnComplete, &handle, pool_.get(), |
| 1585 | connect_job_factory_, TestConnectJob::kMockPendingJob, |
| 1586 | &second_result_callback), |
| 1587 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1588 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1589 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1590 | EXPECT_THAT(second_result_callback.WaitForResult(), IsOk()); |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1591 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1592 | |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1593 | // Tests the case where a second socket is requested in a completion callback, |
| 1594 | // and the second socket connects synchronously. Reuses the same |
| 1595 | // ClientSocketHandle for the second socket, after disconnecting the first. |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1596 | TEST_F(ClientSocketPoolBaseTest, RequestPendingJobThenSynchronous) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1597 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1598 | |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 1599 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 1600 | ClientSocketHandle handle; |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1601 | TestCompletionCallback second_result_callback; |
| 1602 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1603 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Paul Jensen | 8d6f87ec | 2018-01-13 00:46:54 | [diff] [blame] | 1604 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1605 | base::BindOnce(&RequestSocketOnComplete, &handle, pool_.get(), |
| 1606 | connect_job_factory_, TestConnectJob::kMockPendingJob, |
| 1607 | &second_result_callback), |
| 1608 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1609 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1610 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1611 | EXPECT_THAT(second_result_callback.WaitForResult(), IsOk()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1612 | } |
| 1613 | |
| 1614 | // Make sure that pending requests get serviced after active requests get |
| 1615 | // cancelled. |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1616 | TEST_F(ClientSocketPoolBaseTest, CancelActiveRequestWithPendingRequests) { |
[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] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 1619 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1620 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1621 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1622 | IsError(ERR_IO_PENDING)); |
| 1623 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1624 | IsError(ERR_IO_PENDING)); |
| 1625 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1626 | IsError(ERR_IO_PENDING)); |
| 1627 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1628 | IsError(ERR_IO_PENDING)); |
| 1629 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1630 | IsError(ERR_IO_PENDING)); |
| 1631 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1632 | IsError(ERR_IO_PENDING)); |
| 1633 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1634 | IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1635 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1636 | // Now, kDefaultMaxSocketsPerGroup requests should be active. |
| 1637 | // Let's cancel them. |
| 1638 | for (int i = 0; i < kDefaultMaxSocketsPerGroup; ++i) { |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1639 | ASSERT_FALSE(request(i)->handle()->is_initialized()); |
| 1640 | request(i)->handle()->Reset(); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1641 | } |
| 1642 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1643 | // Let's wait for the rest to complete now. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1644 | for (size_t i = kDefaultMaxSocketsPerGroup; i < requests_size(); ++i) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1645 | EXPECT_THAT(request(i)->WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1646 | request(i)->handle()->Reset(); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1647 | } |
| 1648 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1649 | EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup, |
| 1650 | completion_count()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1651 | } |
| 1652 | |
| 1653 | // Make sure that pending requests get serviced after active requests fail. |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1654 | TEST_F(ClientSocketPoolBaseTest, FailingActiveRequestWithPendingRequests) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1655 | const size_t kMaxSockets = 5; |
| 1656 | CreatePool(kMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1657 | |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 1658 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1659 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1660 | const size_t kNumberOfRequests = 2 * kDefaultMaxSocketsPerGroup + 1; |
| 1661 | ASSERT_LE(kNumberOfRequests, kMaxSockets); // Otherwise the test will hang. |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1662 | |
| 1663 | // Queue up all the requests |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1664 | for (size_t i = 0; i < kNumberOfRequests; ++i) |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1665 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1666 | IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1667 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1668 | for (size_t i = 0; i < kNumberOfRequests; ++i) |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1669 | EXPECT_THAT(request(i)->WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1670 | } |
| 1671 | |
mmenke | 9d72fe4 | 2017-05-18 22:36:07 | [diff] [blame] | 1672 | // Make sure that pending requests that complete synchronously get serviced |
| 1673 | // after active requests fail. See https://crbug.com/723748 |
| 1674 | TEST_F(ClientSocketPoolBaseTest, HandleMultipleSyncFailuresAfterAsyncFailure) { |
| 1675 | const size_t kNumberOfRequests = 10; |
| 1676 | const size_t kMaxSockets = 1; |
| 1677 | CreatePool(kMaxSockets, kMaxSockets); |
| 1678 | |
| 1679 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
| 1680 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1681 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1682 | IsError(ERR_IO_PENDING)); |
mmenke | 9d72fe4 | 2017-05-18 22:36:07 | [diff] [blame] | 1683 | |
| 1684 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
| 1685 | |
| 1686 | // Queue up all the other requests |
| 1687 | for (size_t i = 1; i < kNumberOfRequests; ++i) |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1688 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1689 | IsError(ERR_IO_PENDING)); |
mmenke | 9d72fe4 | 2017-05-18 22:36:07 | [diff] [blame] | 1690 | |
| 1691 | // Make sure all requests fail, instead of hanging. |
| 1692 | for (size_t i = 0; i < kNumberOfRequests; ++i) |
| 1693 | EXPECT_THAT(request(i)->WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 1694 | } |
| 1695 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1696 | TEST_F(ClientSocketPoolBaseTest, CancelActiveRequestThenRequestSocket) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1697 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1698 | |
| 1699 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1700 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1701 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1702 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1703 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1704 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1705 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1706 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1707 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1708 | |
| 1709 | // Cancel the active request. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1710 | handle.Reset(); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1711 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1712 | rv = handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 1713 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1714 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1715 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1716 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 1717 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1718 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1719 | EXPECT_FALSE(handle.is_reused()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 1720 | TestLoadTimingInfoConnectedNotReused(handle); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1721 | EXPECT_EQ(2, client_socket_factory_.allocation_count()); |
| 1722 | } |
| 1723 | |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1724 | TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketsForced) { |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1725 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1726 | ClientSocketHandle handle; |
| 1727 | TestCompletionCallback callback; |
| 1728 | BoundTestNetLog log; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1729 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1730 | TestGroupId("a"), params_, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1731 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1732 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1733 | EXPECT_THAT(rv, IsOk()); |
| 1734 | handle.Reset(); |
| 1735 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 1736 | pool_->CloseIdleSockets(); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1737 | } |
| 1738 | |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1739 | TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketsInGroupForced) { |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1740 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1741 | TestCompletionCallback callback; |
| 1742 | BoundTestNetLog log; |
| 1743 | ClientSocketHandle handle1; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1744 | int rv = handle1.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1745 | TestGroupId("a"), params_, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1746 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1747 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1748 | EXPECT_THAT(rv, IsOk()); |
| 1749 | ClientSocketHandle handle2; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1750 | rv = handle2.Init(TestGroupId("a"), params_, LOWEST, SocketTag(), |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1751 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1752 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1753 | pool_.get(), log.bound()); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1754 | ClientSocketHandle handle3; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1755 | rv = handle3.Init(TestGroupId("b"), params_, LOWEST, SocketTag(), |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1756 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1757 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1758 | pool_.get(), log.bound()); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1759 | EXPECT_THAT(rv, IsOk()); |
| 1760 | handle1.Reset(); |
| 1761 | handle2.Reset(); |
| 1762 | handle3.Reset(); |
| 1763 | EXPECT_EQ(3, pool_->IdleSocketCount()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1764 | pool_->CloseIdleSocketsInGroup(TestGroupId("a")); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1765 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1766 | } |
| 1767 | |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1768 | TEST_F(ClientSocketPoolBaseTest, CleanUpUnusableIdleSockets) { |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1769 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1770 | ClientSocketHandle handle; |
| 1771 | TestCompletionCallback callback; |
| 1772 | BoundTestNetLog log; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1773 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1774 | TestGroupId("a"), params_, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1775 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1776 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1777 | EXPECT_THAT(rv, IsOk()); |
| 1778 | StreamSocket* socket = handle.socket(); |
| 1779 | handle.Reset(); |
| 1780 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 1781 | |
| 1782 | // Disconnect socket now to make the socket unusable. |
| 1783 | socket->Disconnect(); |
| 1784 | ClientSocketHandle handle2; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1785 | rv = handle2.Init(TestGroupId("a"), params_, LOWEST, SocketTag(), |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1786 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1787 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1788 | pool_.get(), log.bound()); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1789 | EXPECT_THAT(rv, IsOk()); |
| 1790 | EXPECT_FALSE(handle2.is_reused()); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1791 | } |
| 1792 | |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1793 | // Regression test for http://crbug.com/17985. |
| 1794 | TEST_F(ClientSocketPoolBaseTest, GroupWithPendingRequestsIsNotEmpty) { |
| 1795 | const int kMaxSockets = 3; |
| 1796 | const int kMaxSocketsPerGroup = 2; |
| 1797 | CreatePool(kMaxSockets, kMaxSocketsPerGroup); |
| 1798 | |
[email protected] | ac790b4 | 2009-12-02 04:31:31 | [diff] [blame] | 1799 | const RequestPriority kHighPriority = HIGHEST; |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1800 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1801 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1802 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1803 | |
| 1804 | // This is going to be a pending request in an otherwise empty group. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1805 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1806 | IsError(ERR_IO_PENDING)); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1807 | |
| 1808 | // Reach the maximum socket limit. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1809 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1810 | |
| 1811 | // Create a stalled group with high priorities. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1812 | EXPECT_THAT(StartRequest(TestGroupId("c"), kHighPriority), |
| 1813 | IsError(ERR_IO_PENDING)); |
| 1814 | EXPECT_THAT(StartRequest(TestGroupId("c"), kHighPriority), |
| 1815 | IsError(ERR_IO_PENDING)); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1816 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1817 | // Release the first two sockets from TestGroupId("a"). Because this is a |
| 1818 | // keepalive, the first release will unblock the pending request for |
| 1819 | // TestGroupId("a"). The second release will unblock a request for "c", |
| 1820 | // because it is the next high priority socket. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1821 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
| 1822 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1823 | |
| 1824 | // Closing idle sockets should not get us into trouble, but in the bug |
| 1825 | // we were hitting a CHECK here. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1826 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1827 | pool_->CloseIdleSockets(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1828 | |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 1829 | // Run the released socket wakeups. |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 1830 | base::RunLoop().RunUntilIdle(); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1831 | } |
| 1832 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 1833 | TEST_F(ClientSocketPoolBaseTest, BasicAsynchronous) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1834 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1835 | |
| 1836 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1837 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1838 | TestCompletionCallback callback; |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 1839 | BoundTestNetLog log; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1840 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1841 | TestGroupId("a"), params_, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1842 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1843 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1844 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1845 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 1846 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 1847 | TestLoadTimingInfoNotConnected(handle); |
| 1848 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1849 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1850 | EXPECT_TRUE(handle.is_initialized()); |
| 1851 | EXPECT_TRUE(handle.socket()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 1852 | TestLoadTimingInfoConnectedNotReused(handle); |
| 1853 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1854 | handle.Reset(); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 1855 | TestLoadTimingInfoNotConnected(handle); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 1856 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 1857 | TestNetLogEntry::List entries; |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 1858 | log.GetEntries(&entries); |
| 1859 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1860 | EXPECT_EQ(5u, entries.size()); |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 1861 | EXPECT_TRUE(LogContainsEvent( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1862 | entries, 0, NetLogEventType::TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 1863 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1864 | EXPECT_TRUE(LogContainsBeginEvent(entries, 1, NetLogEventType::SOCKET_POOL)); |
| 1865 | EXPECT_TRUE(LogContainsEvent( |
| 1866 | entries, 2, NetLogEventType::SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
| 1867 | NetLogEventPhase::NONE)); |
| 1868 | EXPECT_TRUE(LogContainsEvent(entries, 3, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 1869 | NetLogEventType::SOCKET_POOL_BOUND_TO_SOCKET, |
| 1870 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1871 | EXPECT_TRUE(LogContainsEndEvent(entries, 4, NetLogEventType::SOCKET_POOL)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1872 | } |
| 1873 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 1874 | TEST_F(ClientSocketPoolBaseTest, |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1875 | InitConnectionAsynchronousFailure) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1876 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1877 | |
| 1878 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1879 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1880 | TestCompletionCallback callback; |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 1881 | BoundTestNetLog log; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 1882 | // Set the additional error state members to ensure that they get cleared. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1883 | handle.set_is_ssl_error(true); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame^] | 1884 | handle.set_ssl_cert_request_info(base::MakeRefCounted<SSLCertRequestInfo>()); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1885 | EXPECT_EQ( |
| 1886 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1887 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1888 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1889 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 1890 | log.bound())); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1891 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 1892 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1893 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1894 | EXPECT_FALSE(handle.is_ssl_error()); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame^] | 1895 | EXPECT_FALSE(handle.ssl_cert_request_info()); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 1896 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 1897 | TestNetLogEntry::List entries; |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 1898 | log.GetEntries(&entries); |
| 1899 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1900 | EXPECT_EQ(4u, entries.size()); |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 1901 | EXPECT_TRUE(LogContainsEvent( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1902 | entries, 0, NetLogEventType::TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 1903 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1904 | EXPECT_TRUE(LogContainsBeginEvent(entries, 1, NetLogEventType::SOCKET_POOL)); |
| 1905 | EXPECT_TRUE(LogContainsEvent( |
| 1906 | entries, 2, NetLogEventType::SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
| 1907 | NetLogEventPhase::NONE)); |
| 1908 | EXPECT_TRUE(LogContainsEndEvent(entries, 3, NetLogEventType::SOCKET_POOL)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1909 | } |
| 1910 | |
mmenke | 6be122f | 2015-03-09 22:22:47 | [diff] [blame] | 1911 | // Check that an async ConnectJob failure does not result in creation of a new |
| 1912 | // ConnectJob when there's another pending request also waiting on its own |
| 1913 | // ConnectJob. See http://crbug.com/463960. |
| 1914 | TEST_F(ClientSocketPoolBaseTest, AsyncFailureWithPendingRequestWithJob) { |
| 1915 | CreatePool(2, 2); |
| 1916 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
| 1917 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1918 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1919 | IsError(ERR_IO_PENDING)); |
| 1920 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1921 | IsError(ERR_IO_PENDING)); |
mmenke | 6be122f | 2015-03-09 22:22:47 | [diff] [blame] | 1922 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1923 | EXPECT_THAT(request(0)->WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 1924 | EXPECT_THAT(request(1)->WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
mmenke | 6be122f | 2015-03-09 22:22:47 | [diff] [blame] | 1925 | |
| 1926 | EXPECT_EQ(2, client_socket_factory_.allocation_count()); |
| 1927 | } |
| 1928 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 1929 | TEST_F(ClientSocketPoolBaseTest, TwoRequestsCancelOne) { |
[email protected] | b22b516 | 2010-03-16 07:53:10 | [diff] [blame] | 1930 | // TODO(eroman): Add back the log expectations! Removed them because the |
| 1931 | // ordering is difficult, and some may fire during destructor. |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1932 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1933 | |
| 1934 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1935 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1936 | TestCompletionCallback callback; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1937 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1938 | TestCompletionCallback callback2; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1939 | |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1940 | EXPECT_EQ( |
| 1941 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1942 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1943 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1944 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 1945 | NetLogWithSource())); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 1946 | BoundTestNetLog log2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1947 | EXPECT_EQ( |
| 1948 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1949 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1950 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1951 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1952 | pool_.get(), NetLogWithSource())); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1953 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1954 | handle.Reset(); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1955 | |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 1956 | |
| 1957 | // 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] | 1958 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1959 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1960 | handle2.Reset(); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 1961 | |
| 1962 | // Now request 2 has actually finished. |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 1963 | // TODO(eroman): Add back log expectations. |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1964 | } |
| 1965 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 1966 | TEST_F(ClientSocketPoolBaseTest, CancelRequestLimitsJobs) { |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 1967 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1968 | |
[email protected] | 17a0c6c | 2009-08-04 00:07:04 | [diff] [blame] | 1969 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1970 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1971 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
| 1972 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 1973 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1974 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 1975 | |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 1976 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1977 | static_cast<int>( |
| 1978 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1979 | (*requests())[2]->handle()->Reset(); |
| 1980 | (*requests())[3]->handle()->Reset(); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 1981 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1982 | static_cast<int>( |
| 1983 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 1984 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1985 | (*requests())[1]->handle()->Reset(); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 1986 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1987 | static_cast<int>( |
| 1988 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 1989 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1990 | (*requests())[0]->handle()->Reset(); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 1991 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1992 | static_cast<int>( |
| 1993 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 1994 | } |
| 1995 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1996 | // When requests and ConnectJobs are not coupled, the request will get serviced |
| 1997 | // by whatever comes first. |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 1998 | TEST_F(ClientSocketPoolBaseTest, ReleaseSockets) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1999 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2000 | |
| 2001 | // Start job 1 (async OK) |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 2002 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2003 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2004 | std::vector<TestSocketRequest*> request_order; |
| 2005 | size_t completion_count; // unused |
| 2006 | TestSocketRequest req1(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2007 | int rv = req1.handle()->Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2008 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2009 | ClientSocketPool::RespectLimits::ENABLED, req1.callback(), |
| 2010 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2011 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 2012 | EXPECT_THAT(req1.WaitForResult(), IsOk()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2013 | |
| 2014 | // Job 1 finished OK. Start job 2 (also async OK). Request 3 is pending |
| 2015 | // without a job. |
| 2016 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2017 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2018 | TestSocketRequest req2(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2019 | rv = req2.handle()->Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2020 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2021 | ClientSocketPool::RespectLimits::ENABLED, req2.callback(), |
| 2022 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2023 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2024 | TestSocketRequest req3(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2025 | rv = req3.handle()->Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2026 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2027 | ClientSocketPool::RespectLimits::ENABLED, req3.callback(), |
| 2028 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2029 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2030 | |
| 2031 | // Both Requests 2 and 3 are pending. We release socket 1 which should |
| 2032 | // service request 2. Request 3 should still be waiting. |
[email protected] | a6c59f6 | 2009-07-29 16:33:33 | [diff] [blame] | 2033 | req1.handle()->Reset(); |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 2034 | // Run the released socket wakeups. |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 2035 | base::RunLoop().RunUntilIdle(); |
[email protected] | a6c59f6 | 2009-07-29 16:33:33 | [diff] [blame] | 2036 | ASSERT_TRUE(req2.handle()->socket()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2037 | EXPECT_THAT(req2.WaitForResult(), IsOk()); |
[email protected] | a6c59f6 | 2009-07-29 16:33:33 | [diff] [blame] | 2038 | EXPECT_FALSE(req3.handle()->socket()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2039 | |
| 2040 | // Signal job 2, which should service request 3. |
| 2041 | |
| 2042 | client_socket_factory_.SignalJobs(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2043 | EXPECT_THAT(req3.WaitForResult(), IsOk()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2044 | |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2045 | ASSERT_EQ(3u, request_order.size()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2046 | EXPECT_EQ(&req1, request_order[0]); |
| 2047 | EXPECT_EQ(&req2, request_order[1]); |
| 2048 | EXPECT_EQ(&req3, request_order[2]); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2049 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2050 | } |
| 2051 | |
| 2052 | // The requests are not coupled to the jobs. So, the requests should finish in |
| 2053 | // their priority / insertion order. |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2054 | TEST_F(ClientSocketPoolBaseTest, PendingJobCompletionOrder) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2055 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2056 | // First two jobs are async. |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 2057 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2058 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2059 | std::vector<TestSocketRequest*> request_order; |
| 2060 | size_t completion_count; // unused |
| 2061 | TestSocketRequest req1(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2062 | int rv = req1.handle()->Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2063 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2064 | ClientSocketPool::RespectLimits::ENABLED, req1.callback(), |
| 2065 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2066 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2067 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2068 | TestSocketRequest req2(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2069 | rv = req2.handle()->Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2070 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2071 | ClientSocketPool::RespectLimits::ENABLED, req2.callback(), |
| 2072 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2073 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2074 | |
| 2075 | // The pending job is sync. |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 2076 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2077 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2078 | TestSocketRequest req3(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2079 | rv = req3.handle()->Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2080 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2081 | ClientSocketPool::RespectLimits::ENABLED, req3.callback(), |
| 2082 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2083 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2084 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2085 | EXPECT_THAT(req1.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 2086 | EXPECT_THAT(req2.WaitForResult(), IsOk()); |
| 2087 | EXPECT_THAT(req3.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2088 | |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2089 | ASSERT_EQ(3u, request_order.size()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2090 | EXPECT_EQ(&req1, request_order[0]); |
| 2091 | EXPECT_EQ(&req2, request_order[1]); |
| 2092 | EXPECT_EQ(&req3, request_order[2]); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2093 | } |
| 2094 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2095 | // Test GetLoadState in the case there's only one socket request. |
| 2096 | TEST_F(ClientSocketPoolBaseTest, LoadStateOneRequest) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2097 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2098 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2099 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2100 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2101 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2102 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2103 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2104 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2105 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2106 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2107 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2108 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2109 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE); |
| 2110 | EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle.GetLoadState()); |
| 2111 | |
| 2112 | // No point in completing the connection, since ClientSocketHandles only |
| 2113 | // expect the LoadState to be checked while connecting. |
| 2114 | } |
| 2115 | |
| 2116 | // Test GetLoadState in the case there are two socket requests. |
| 2117 | TEST_F(ClientSocketPoolBaseTest, LoadStateTwoRequests) { |
| 2118 | CreatePool(2, 2); |
| 2119 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2120 | |
| 2121 | ClientSocketHandle handle; |
| 2122 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2123 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2124 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2125 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2126 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2127 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2128 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_RESOLVING_HOST); |
| 2129 | |
| 2130 | ClientSocketHandle handle2; |
| 2131 | TestCompletionCallback callback2; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2132 | rv = handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 2133 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2134 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2135 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2136 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2137 | client_socket_factory_.SetJobLoadState(1, LOAD_STATE_RESOLVING_HOST); |
| 2138 | |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 2139 | // Each handle should reflect the state of its own job. |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2140 | EXPECT_EQ(LOAD_STATE_RESOLVING_HOST, handle.GetLoadState()); |
| 2141 | EXPECT_EQ(LOAD_STATE_RESOLVING_HOST, handle2.GetLoadState()); |
| 2142 | |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 2143 | // Update the state of the first job. |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2144 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_CONNECTING); |
| 2145 | |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 2146 | // Only the state of the first request should have changed. |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2147 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2148 | EXPECT_EQ(LOAD_STATE_RESOLVING_HOST, handle2.GetLoadState()); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 2149 | |
| 2150 | // Update the state of the second job. |
| 2151 | client_socket_factory_.SetJobLoadState(1, LOAD_STATE_SSL_HANDSHAKE); |
| 2152 | |
| 2153 | // Only the state of the second request should have changed. |
| 2154 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
| 2155 | EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle2.GetLoadState()); |
| 2156 | |
| 2157 | // Second job connects and the first request gets the socket. The |
| 2158 | // second handle switches to the state of the remaining ConnectJob. |
| 2159 | client_socket_factory_.SignalJob(1); |
| 2160 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 2161 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle2.GetLoadState()); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2162 | } |
| 2163 | |
| 2164 | // Test GetLoadState in the case the per-group limit is reached. |
| 2165 | TEST_F(ClientSocketPoolBaseTest, LoadStateGroupLimit) { |
| 2166 | CreatePool(2, 1); |
| 2167 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2168 | |
| 2169 | ClientSocketHandle handle; |
| 2170 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2171 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2172 | TestGroupId("a"), params_, MEDIUM, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2173 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2174 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2175 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2176 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
| 2177 | |
| 2178 | // Request another socket from the same pool, buth with a higher priority. |
| 2179 | // The first request should now be stalled at the socket group limit. |
| 2180 | ClientSocketHandle handle2; |
| 2181 | TestCompletionCallback callback2; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2182 | rv = handle2.Init(TestGroupId("a"), params_, HIGHEST, SocketTag(), |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 2183 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2184 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2185 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2186 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2187 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET, handle.GetLoadState()); |
| 2188 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle2.GetLoadState()); |
| 2189 | |
| 2190 | // The first handle should remain stalled as the other socket goes through |
| 2191 | // the connect process. |
| 2192 | |
| 2193 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE); |
| 2194 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET, handle.GetLoadState()); |
| 2195 | EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle2.GetLoadState()); |
| 2196 | |
| 2197 | client_socket_factory_.SignalJob(0); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2198 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2199 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET, handle.GetLoadState()); |
| 2200 | |
| 2201 | // Closing the second socket should cause the stalled handle to finally get a |
| 2202 | // ConnectJob. |
| 2203 | handle2.socket()->Disconnect(); |
| 2204 | handle2.Reset(); |
| 2205 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
| 2206 | } |
| 2207 | |
| 2208 | // Test GetLoadState in the case the per-pool limit is reached. |
| 2209 | TEST_F(ClientSocketPoolBaseTest, LoadStatePoolLimit) { |
| 2210 | CreatePool(2, 2); |
| 2211 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2212 | |
| 2213 | ClientSocketHandle handle; |
| 2214 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2215 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2216 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2217 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2218 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2219 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2220 | |
| 2221 | // Request for socket from another pool. |
| 2222 | ClientSocketHandle handle2; |
| 2223 | TestCompletionCallback callback2; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2224 | rv = handle2.Init(TestGroupId("b"), params_, DEFAULT_PRIORITY, SocketTag(), |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 2225 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2226 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2227 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2228 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2229 | |
| 2230 | // Request another socket from the first pool. Request should stall at the |
| 2231 | // socket pool limit. |
| 2232 | ClientSocketHandle handle3; |
| 2233 | TestCompletionCallback callback3; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2234 | rv = handle3.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 2235 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2236 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2237 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2238 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2239 | |
| 2240 | // The third handle should remain stalled as the other sockets in its group |
| 2241 | // goes through the connect process. |
| 2242 | |
| 2243 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
| 2244 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL, handle3.GetLoadState()); |
| 2245 | |
| 2246 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE); |
| 2247 | EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle.GetLoadState()); |
| 2248 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL, handle3.GetLoadState()); |
| 2249 | |
| 2250 | client_socket_factory_.SignalJob(0); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2251 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2252 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL, handle3.GetLoadState()); |
| 2253 | |
| 2254 | // Closing a socket should allow the stalled handle to finally get a new |
| 2255 | // ConnectJob. |
| 2256 | handle.socket()->Disconnect(); |
| 2257 | handle.Reset(); |
| 2258 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle3.GetLoadState()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2259 | } |
| 2260 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2261 | TEST_F(ClientSocketPoolBaseTest, CertError) { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2262 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2263 | connect_job_factory_->set_job_type(TestConnectJob::kMockCertErrorJob); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2264 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2265 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2266 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2267 | EXPECT_EQ( |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2268 | ERR_CERT_COMMON_NAME_INVALID, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2269 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2270 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2271 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2272 | NetLogWithSource())); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2273 | EXPECT_TRUE(handle.is_initialized()); |
| 2274 | EXPECT_TRUE(handle.socket()); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2275 | } |
| 2276 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2277 | TEST_F(ClientSocketPoolBaseTest, AsyncCertError) { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2278 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2279 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2280 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingCertErrorJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2281 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2282 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2283 | EXPECT_EQ( |
| 2284 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2285 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2286 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2287 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2288 | NetLogWithSource())); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2289 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2290 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2291 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_CERT_COMMON_NAME_INVALID)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2292 | EXPECT_TRUE(handle.is_initialized()); |
| 2293 | EXPECT_TRUE(handle.socket()); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2294 | } |
| 2295 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2296 | TEST_F(ClientSocketPoolBaseTest, AdditionalErrorStateSynchronous) { |
| 2297 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2298 | connect_job_factory_->set_job_type( |
| 2299 | TestConnectJob::kMockAdditionalErrorStateJob); |
| 2300 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2301 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2302 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2303 | EXPECT_EQ( |
| 2304 | ERR_CONNECTION_FAILED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2305 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2306 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2307 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2308 | NetLogWithSource())); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2309 | EXPECT_FALSE(handle.is_initialized()); |
| 2310 | EXPECT_FALSE(handle.socket()); |
| 2311 | EXPECT_TRUE(handle.is_ssl_error()); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame^] | 2312 | EXPECT_TRUE(handle.ssl_cert_request_info()); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2313 | } |
| 2314 | |
| 2315 | TEST_F(ClientSocketPoolBaseTest, AdditionalErrorStateAsynchronous) { |
| 2316 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2317 | |
| 2318 | connect_job_factory_->set_job_type( |
| 2319 | TestConnectJob::kMockPendingAdditionalErrorStateJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2320 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2321 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2322 | EXPECT_EQ( |
| 2323 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2324 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2325 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2326 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2327 | NetLogWithSource())); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2328 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2329 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2330 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2331 | EXPECT_FALSE(handle.is_initialized()); |
| 2332 | EXPECT_FALSE(handle.socket()); |
| 2333 | EXPECT_TRUE(handle.is_ssl_error()); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame^] | 2334 | EXPECT_TRUE(handle.ssl_cert_request_info()); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2335 | } |
| 2336 | |
martijn | 003cd61 | 2016-05-19 22:24:38 | [diff] [blame] | 2337 | // Make sure we can reuse sockets. |
| 2338 | TEST_F(ClientSocketPoolBaseTest, CleanupTimedOutIdleSocketsReuse) { |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2339 | CreatePoolWithIdleTimeouts( |
| 2340 | kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2341 | base::TimeDelta(), // Time out unused sockets immediately. |
| 2342 | base::TimeDelta::FromDays(1)); // Don't time out used sockets. |
| 2343 | |
| 2344 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2345 | |
| 2346 | ClientSocketHandle handle; |
| 2347 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2348 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2349 | TestGroupId("a"), params_, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2350 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2351 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2352 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2353 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2354 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2355 | ASSERT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2356 | |
| 2357 | // Use and release the socket. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 2358 | EXPECT_EQ(1, handle.socket()->Write(nullptr, 1, CompletionOnceCallback(), |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 2359 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 2360 | TestLoadTimingInfoConnectedNotReused(handle); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2361 | handle.Reset(); |
| 2362 | |
| 2363 | // Should now have one idle socket. |
| 2364 | ASSERT_EQ(1, pool_->IdleSocketCount()); |
| 2365 | |
| 2366 | // Request a new socket. This should reuse the old socket and complete |
| 2367 | // synchronously. |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 2368 | BoundTestNetLog log; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2369 | rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2370 | TestGroupId("a"), params_, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2371 | ClientSocketPool::RespectLimits::ENABLED, CompletionOnceCallback(), |
| 2372 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2373 | ASSERT_THAT(rv, IsOk()); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2374 | EXPECT_TRUE(handle.is_reused()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 2375 | TestLoadTimingInfoConnectedReused(handle); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2376 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2377 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2378 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2379 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2380 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 2381 | TestNetLogEntry::List entries; |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2382 | log.GetEntries(&entries); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2383 | EXPECT_TRUE(LogContainsEvent( |
| 2384 | entries, 0, NetLogEventType::TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET, |
| 2385 | NetLogEventPhase::NONE)); |
| 2386 | EXPECT_TRUE(LogContainsBeginEvent(entries, 1, NetLogEventType::SOCKET_POOL)); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2387 | EXPECT_TRUE(LogContainsEntryWithType( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2388 | entries, 2, NetLogEventType::SOCKET_POOL_REUSED_AN_EXISTING_SOCKET)); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2389 | } |
| 2390 | |
martijn | 003cd61 | 2016-05-19 22:24:38 | [diff] [blame] | 2391 | // Make sure we cleanup old unused sockets. |
Eric Roman | b49715e | 2018-04-24 22:41:17 | [diff] [blame] | 2392 | TEST_F(ClientSocketPoolBaseTest, CleanupTimedOutIdleSocketsNoReuse) { |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2393 | CreatePoolWithIdleTimeouts( |
| 2394 | kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 2395 | base::TimeDelta(), // Time out unused sockets immediately |
| 2396 | base::TimeDelta()); // Time out used sockets immediately |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2397 | |
| 2398 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2399 | |
| 2400 | // Startup two mock pending connect jobs, which will sit in the MessageLoop. |
| 2401 | |
| 2402 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2403 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2404 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2405 | TestGroupId("a"), params_, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2406 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2407 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2408 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2409 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2410 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2411 | |
| 2412 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2413 | TestCompletionCallback callback2; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2414 | rv = handle2.Init(TestGroupId("a"), params_, LOWEST, SocketTag(), |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 2415 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2416 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2417 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2418 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2419 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2420 | pool_->GetLoadState(TestGroupId("a"), &handle2)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2421 | |
| 2422 | // Cancel one of the requests. Wait for the other, which will get the first |
| 2423 | // job. Release the socket. Run the loop again to make sure the second |
| 2424 | // socket is sitting idle and the first one is released (since ReleaseSocket() |
| 2425 | // just posts a DoReleaseSocket() task). |
| 2426 | |
| 2427 | handle.Reset(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2428 | ASSERT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2429 | // Use the socket. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 2430 | EXPECT_EQ(1, handle2.socket()->Write(nullptr, 1, CompletionOnceCallback(), |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 2431 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2432 | handle2.Reset(); |
| 2433 | |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2434 | // We post all of our delayed tasks with a 2ms delay. I.e. they don't |
| 2435 | // actually become pending until 2ms after they have been created. In order |
| 2436 | // to flush all tasks, we need to wait so that we know there are no |
| 2437 | // soon-to-be-pending tasks waiting. |
Alex Clarke | 0def209 | 2018-12-10 12:01:45 | [diff] [blame] | 2438 | FastForwardBy(base::TimeDelta::FromMilliseconds(10)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2439 | |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2440 | // Both sockets should now be idle. |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2441 | ASSERT_EQ(2, pool_->IdleSocketCount()); |
| 2442 | |
| 2443 | // Request a new socket. This should cleanup the unused and timed out ones. |
| 2444 | // A new socket will be created rather than reusing the idle one. |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 2445 | BoundTestNetLog log; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2446 | TestCompletionCallback callback3; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2447 | rv = handle.Init(TestGroupId("a"), params_, LOWEST, SocketTag(), |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 2448 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2449 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2450 | pool_.get(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2451 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 2452 | ASSERT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2453 | EXPECT_FALSE(handle.is_reused()); |
| 2454 | |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2455 | // Make sure the idle socket is closed. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2456 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2457 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2458 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2459 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 2460 | TestNetLogEntry::List entries; |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2461 | log.GetEntries(&entries); |
| 2462 | EXPECT_FALSE(LogContainsEntryWithType( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 2463 | entries, 1, NetLogEventType::SOCKET_POOL_REUSED_AN_EXISTING_SOCKET)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2464 | } |
| 2465 | |
[email protected] | 2041cf34 | 2010-02-19 03:15:59 | [diff] [blame] | 2466 | // Make sure that we process all pending requests even when we're stalling |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2467 | // because of multiple releasing disconnected sockets. |
| 2468 | TEST_F(ClientSocketPoolBaseTest, MultipleReleasingDisconnectedSockets) { |
| 2469 | CreatePoolWithIdleTimeouts( |
| 2470 | kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 2471 | base::TimeDelta(), // Time out unused sockets immediately. |
| 2472 | base::TimeDelta::FromDays(1)); // Don't time out used sockets. |
| 2473 | |
| 2474 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 2475 | |
| 2476 | // Startup 4 connect jobs. Two of them will be pending. |
| 2477 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2478 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2479 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2480 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2481 | TestGroupId("a"), params_, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2482 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2483 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2484 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2485 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2486 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2487 | TestCompletionCallback callback2; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2488 | rv = handle2.Init(TestGroupId("a"), params_, LOWEST, SocketTag(), |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 2489 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2490 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2491 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2492 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2493 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2494 | ClientSocketHandle handle3; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2495 | TestCompletionCallback callback3; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2496 | rv = handle3.Init(TestGroupId("a"), params_, LOWEST, SocketTag(), |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 2497 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2498 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2499 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2500 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2501 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2502 | ClientSocketHandle handle4; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2503 | TestCompletionCallback callback4; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2504 | rv = handle4.Init(TestGroupId("a"), params_, LOWEST, SocketTag(), |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 2505 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2506 | callback4.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2507 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2508 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2509 | |
| 2510 | // Release two disconnected sockets. |
| 2511 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2512 | handle.socket()->Disconnect(); |
| 2513 | handle.Reset(); |
| 2514 | handle2.socket()->Disconnect(); |
| 2515 | handle2.Reset(); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2516 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2517 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2518 | EXPECT_FALSE(handle3.is_reused()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2519 | EXPECT_THAT(callback4.WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2520 | EXPECT_FALSE(handle4.is_reused()); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2521 | } |
| 2522 | |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2523 | // Regression test for http://crbug.com/42267. |
| 2524 | // When DoReleaseSocket() is processed for one socket, it is blocked because the |
| 2525 | // other stalled groups all have releasing sockets, so no progress can be made. |
| 2526 | TEST_F(ClientSocketPoolBaseTest, SocketLimitReleasingSockets) { |
| 2527 | CreatePoolWithIdleTimeouts( |
| 2528 | 4 /* socket limit */, 4 /* socket limit per group */, |
| 2529 | base::TimeDelta(), // Time out unused sockets immediately. |
| 2530 | base::TimeDelta::FromDays(1)); // Don't time out used sockets. |
| 2531 | |
| 2532 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 2533 | |
| 2534 | // Max out the socket limit with 2 per group. |
| 2535 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2536 | ClientSocketHandle handle_a[4]; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2537 | TestCompletionCallback callback_a[4]; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2538 | ClientSocketHandle handle_b[4]; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2539 | TestCompletionCallback callback_b[4]; |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2540 | |
| 2541 | for (int i = 0; i < 2; ++i) { |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2542 | EXPECT_EQ( |
| 2543 | OK, handle_a[i].Init(TestGroupId("a"), params_, LOWEST, SocketTag(), |
| 2544 | ClientSocketPool::RespectLimits::ENABLED, |
| 2545 | callback_a[i].callback(), |
| 2546 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2547 | NetLogWithSource())); |
| 2548 | EXPECT_EQ( |
| 2549 | OK, handle_b[i].Init(TestGroupId("b"), params_, LOWEST, SocketTag(), |
| 2550 | ClientSocketPool::RespectLimits::ENABLED, |
| 2551 | callback_b[i].callback(), |
| 2552 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2553 | NetLogWithSource())); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2554 | } |
[email protected] | b89f7e4 | 2010-05-20 20:37:00 | [diff] [blame] | 2555 | |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2556 | // Make 4 pending requests, 2 per group. |
| 2557 | |
| 2558 | for (int i = 2; i < 4; ++i) { |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 2559 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2560 | handle_a[i].Init(TestGroupId("a"), params_, LOWEST, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 2561 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2562 | callback_a[i].callback(), |
| 2563 | ClientSocketPool::ProxyAuthCallback(), |
| 2564 | pool_.get(), NetLogWithSource())); |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 2565 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2566 | handle_b[i].Init(TestGroupId("b"), params_, LOWEST, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 2567 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2568 | callback_b[i].callback(), |
| 2569 | ClientSocketPool::ProxyAuthCallback(), |
| 2570 | pool_.get(), NetLogWithSource())); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2571 | } |
| 2572 | |
| 2573 | // Release b's socket first. The order is important, because in |
| 2574 | // DoReleaseSocket(), we'll process b's released socket, and since both b and |
| 2575 | // a are stalled, but 'a' is lower lexicographically, we'll process group 'a' |
| 2576 | // first, which has a releasing socket, so it refuses to start up another |
| 2577 | // ConnectJob. So, we used to infinite loop on this. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2578 | handle_b[0].socket()->Disconnect(); |
| 2579 | handle_b[0].Reset(); |
| 2580 | handle_a[0].socket()->Disconnect(); |
| 2581 | handle_a[0].Reset(); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2582 | |
| 2583 | // Used to get stuck here. |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 2584 | base::RunLoop().RunUntilIdle(); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2585 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2586 | handle_b[1].socket()->Disconnect(); |
| 2587 | handle_b[1].Reset(); |
| 2588 | handle_a[1].socket()->Disconnect(); |
| 2589 | handle_a[1].Reset(); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2590 | |
| 2591 | for (int i = 2; i < 4; ++i) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2592 | EXPECT_THAT(callback_b[i].WaitForResult(), IsOk()); |
| 2593 | EXPECT_THAT(callback_a[i].WaitForResult(), IsOk()); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2594 | } |
| 2595 | } |
| 2596 | |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2597 | TEST_F(ClientSocketPoolBaseTest, |
| 2598 | ReleasingDisconnectedSocketsMaintainsPriorityOrder) { |
| 2599 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2600 | |
| 2601 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2602 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2603 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2604 | IsError(ERR_IO_PENDING)); |
| 2605 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2606 | IsError(ERR_IO_PENDING)); |
| 2607 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2608 | IsError(ERR_IO_PENDING)); |
| 2609 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2610 | IsError(ERR_IO_PENDING)); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2611 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2612 | EXPECT_THAT((*requests())[0]->WaitForResult(), IsOk()); |
| 2613 | EXPECT_THAT((*requests())[1]->WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2614 | EXPECT_EQ(2u, completion_count()); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2615 | |
| 2616 | // Releases one connection. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2617 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::NO_KEEP_ALIVE)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2618 | EXPECT_THAT((*requests())[2]->WaitForResult(), IsOk()); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2619 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2620 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::NO_KEEP_ALIVE)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2621 | EXPECT_THAT((*requests())[3]->WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2622 | EXPECT_EQ(4u, completion_count()); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2623 | |
| 2624 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 2625 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 2626 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 2627 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 2628 | |
| 2629 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2630 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(5)); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2631 | } |
| 2632 | |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2633 | class TestReleasingSocketRequest : public TestCompletionCallbackBase { |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2634 | public: |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2635 | TestReleasingSocketRequest(TransportClientSocketPool* pool, |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2636 | int expected_result, |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2637 | bool reset_releasing_handle) |
| 2638 | : pool_(pool), |
| 2639 | expected_result_(expected_result), |
Bence Béky | 8ddc249 | 2018-06-13 01:02:04 | [diff] [blame] | 2640 | reset_releasing_handle_(reset_releasing_handle) {} |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2641 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 2642 | ~TestReleasingSocketRequest() override = default; |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2643 | |
| 2644 | ClientSocketHandle* handle() { return &handle_; } |
| 2645 | |
Bence Béky | 8ddc249 | 2018-06-13 01:02:04 | [diff] [blame] | 2646 | CompletionOnceCallback callback() { |
| 2647 | return base::BindOnce(&TestReleasingSocketRequest::OnComplete, |
| 2648 | base::Unretained(this)); |
| 2649 | } |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2650 | |
| 2651 | private: |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2652 | void OnComplete(int result) { |
| 2653 | SetResult(result); |
| 2654 | if (reset_releasing_handle_) |
| 2655 | handle_.Reset(); |
| 2656 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2657 | EXPECT_EQ( |
| 2658 | expected_result_, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2659 | handle2_.Init(TestGroupId("a"), CreateDummyParams(), DEFAULT_PRIORITY, |
| 2660 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2661 | CompletionOnceCallback(), |
| 2662 | ClientSocketPool::ProxyAuthCallback(), pool_, |
| 2663 | NetLogWithSource())); |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2664 | } |
| 2665 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2666 | TransportClientSocketPool* const pool_; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2667 | int expected_result_; |
| 2668 | bool reset_releasing_handle_; |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2669 | ClientSocketHandle handle_; |
| 2670 | ClientSocketHandle handle2_; |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2671 | }; |
| 2672 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2673 | |
| 2674 | TEST_F(ClientSocketPoolBaseTest, AdditionalErrorSocketsDontUseSlot) { |
| 2675 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2676 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2677 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
| 2678 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 2679 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2680 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2681 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2682 | client_socket_factory_.allocation_count()); |
| 2683 | |
| 2684 | connect_job_factory_->set_job_type( |
| 2685 | TestConnectJob::kMockPendingAdditionalErrorStateJob); |
| 2686 | TestReleasingSocketRequest req(pool_.get(), OK, false); |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 2687 | EXPECT_EQ( |
| 2688 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2689 | req.handle()->Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, |
| 2690 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2691 | req.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2692 | pool_.get(), NetLogWithSource())); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2693 | // The next job should complete synchronously |
| 2694 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 2695 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2696 | EXPECT_THAT(req.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2697 | EXPECT_FALSE(req.handle()->is_initialized()); |
| 2698 | EXPECT_FALSE(req.handle()->socket()); |
| 2699 | EXPECT_TRUE(req.handle()->is_ssl_error()); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame^] | 2700 | EXPECT_TRUE(req.handle()->ssl_cert_request_info()); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2701 | } |
| 2702 | |
[email protected] | b6501d3d | 2010-06-03 23:53:34 | [diff] [blame] | 2703 | // http://crbug.com/44724 regression test. |
| 2704 | // We start releasing the pool when we flush on network change. When that |
| 2705 | // happens, the only active references are in the ClientSocketHandles. When a |
| 2706 | // ConnectJob completes and calls back into the last ClientSocketHandle, that |
| 2707 | // callback can release the last reference and delete the pool. After the |
| 2708 | // callback finishes, we go back to the stack frame within the now-deleted pool. |
| 2709 | // Executing any code that refers to members of the now-deleted pool can cause |
| 2710 | // crashes. |
| 2711 | TEST_F(ClientSocketPoolBaseTest, CallbackThatReleasesPool) { |
| 2712 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2713 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
| 2714 | |
| 2715 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2716 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2717 | EXPECT_EQ( |
| 2718 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2719 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2720 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2721 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2722 | NetLogWithSource())); |
[email protected] | b6501d3d | 2010-06-03 23:53:34 | [diff] [blame] | 2723 | |
[email protected] | 7af985a | 2012-12-14 22:40:42 | [diff] [blame] | 2724 | pool_->FlushWithError(ERR_NETWORK_CHANGED); |
[email protected] | b6501d3d | 2010-06-03 23:53:34 | [diff] [blame] | 2725 | |
| 2726 | // We'll call back into this now. |
| 2727 | callback.WaitForResult(); |
| 2728 | } |
| 2729 | |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2730 | TEST_F(ClientSocketPoolBaseTest, DoNotReuseSocketAfterFlush) { |
| 2731 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2732 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2733 | |
| 2734 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2735 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2736 | EXPECT_EQ( |
| 2737 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2738 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2739 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2740 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2741 | NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2742 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2743 | EXPECT_EQ(ClientSocketHandle::UNUSED, handle.reuse_type()); |
| 2744 | |
[email protected] | 7af985a | 2012-12-14 22:40:42 | [diff] [blame] | 2745 | pool_->FlushWithError(ERR_NETWORK_CHANGED); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2746 | |
| 2747 | handle.Reset(); |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 2748 | base::RunLoop().RunUntilIdle(); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2749 | |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2750 | EXPECT_EQ( |
| 2751 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2752 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2753 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2754 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2755 | NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2756 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2757 | EXPECT_EQ(ClientSocketHandle::UNUSED, handle.reuse_type()); |
| 2758 | } |
| 2759 | |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2760 | class ConnectWithinCallback : public TestCompletionCallbackBase { |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2761 | public: |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2762 | ConnectWithinCallback( |
| 2763 | const ClientSocketPool::GroupId& group_id, |
Matt Menke | 84d11e56 | 2019-03-27 00:11:19 | [diff] [blame] | 2764 | const scoped_refptr<ClientSocketPool::SocketParams>& params, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2765 | TransportClientSocketPool* pool) |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2766 | : group_id_(group_id), params_(params), pool_(pool) {} |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2767 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 2768 | ~ConnectWithinCallback() override = default; |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2769 | |
| 2770 | int WaitForNestedResult() { |
| 2771 | return nested_callback_.WaitForResult(); |
| 2772 | } |
| 2773 | |
Bence Béky | 8ddc249 | 2018-06-13 01:02:04 | [diff] [blame] | 2774 | CompletionOnceCallback callback() { |
| 2775 | return base::BindOnce(&ConnectWithinCallback::OnComplete, |
| 2776 | base::Unretained(this)); |
| 2777 | } |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2778 | |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2779 | private: |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2780 | void OnComplete(int result) { |
| 2781 | SetResult(result); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2782 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2783 | handle_.Init(group_id_, params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2784 | ClientSocketPool::RespectLimits::ENABLED, |
| 2785 | nested_callback_.callback(), |
| 2786 | ClientSocketPool::ProxyAuthCallback(), pool_, |
| 2787 | NetLogWithSource())); |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2788 | } |
| 2789 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2790 | const ClientSocketPool::GroupId group_id_; |
Matt Menke | 84d11e56 | 2019-03-27 00:11:19 | [diff] [blame] | 2791 | const scoped_refptr<ClientSocketPool::SocketParams> params_; |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2792 | TransportClientSocketPool* const pool_; |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2793 | ClientSocketHandle handle_; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2794 | TestCompletionCallback nested_callback_; |
| 2795 | |
| 2796 | DISALLOW_COPY_AND_ASSIGN(ConnectWithinCallback); |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2797 | }; |
| 2798 | |
| 2799 | TEST_F(ClientSocketPoolBaseTest, AbortAllRequestsOnFlush) { |
| 2800 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2801 | |
| 2802 | // First job will be waiting until it gets aborted. |
| 2803 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2804 | |
| 2805 | ClientSocketHandle handle; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2806 | ConnectWithinCallback callback(TestGroupId("a"), params_, pool_.get()); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2807 | EXPECT_EQ( |
| 2808 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2809 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2810 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2811 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2812 | NetLogWithSource())); |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2813 | |
| 2814 | // Second job will be started during the first callback, and will |
| 2815 | // asynchronously complete with OK. |
| 2816 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 7af985a | 2012-12-14 22:40:42 | [diff] [blame] | 2817 | pool_->FlushWithError(ERR_NETWORK_CHANGED); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2818 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_NETWORK_CHANGED)); |
| 2819 | EXPECT_THAT(callback.WaitForNestedResult(), IsOk()); |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2820 | } |
| 2821 | |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 2822 | TEST_F(ClientSocketPoolBaseTest, BackupSocketWaitsForHostResolution) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2823 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets, |
| 2824 | true /* enable_backup_connect_jobs */); |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 2825 | |
| 2826 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2827 | ClientSocketHandle handle; |
| 2828 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2829 | EXPECT_EQ( |
| 2830 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2831 | handle.Init(TestGroupId("bar"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2832 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2833 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2834 | NetLogWithSource())); |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 2835 | // The backup timer fires but doesn't start a new ConnectJob while resolving |
| 2836 | // the hostname. |
| 2837 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_RESOLVING_HOST); |
| 2838 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
| 2839 | ClientSocketPool::kMaxConnectRetryIntervalMs * 100)); |
| 2840 | EXPECT_EQ(1, client_socket_factory_.allocation_count()); |
| 2841 | |
| 2842 | // Once the ConnectJob has finished resolving the hostname, the backup timer |
| 2843 | // will create a ConnectJob when it fires. |
| 2844 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_CONNECTING); |
| 2845 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
| 2846 | ClientSocketPool::kMaxConnectRetryIntervalMs)); |
| 2847 | EXPECT_EQ(2, client_socket_factory_.allocation_count()); |
| 2848 | } |
| 2849 | |
| 2850 | // Test that no backup socket is created when a ConnectJob connects before it |
| 2851 | // completes. |
| 2852 | TEST_F(ClientSocketPoolBaseTest, NoBackupSocketWhenConnected) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2853 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets, |
| 2854 | true /* enable_backup_connect_jobs */); |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 2855 | |
| 2856 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2857 | ClientSocketHandle handle; |
| 2858 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2859 | EXPECT_EQ( |
| 2860 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2861 | handle.Init(TestGroupId("bar"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2862 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2863 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2864 | NetLogWithSource())); |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 2865 | // The backup timer fires but doesn't start a new ConnectJob while resolving |
| 2866 | // the hostname. |
| 2867 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_RESOLVING_HOST); |
| 2868 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
| 2869 | ClientSocketPool::kMaxConnectRetryIntervalMs * 100)); |
| 2870 | EXPECT_EQ(1, client_socket_factory_.allocation_count()); |
| 2871 | |
| 2872 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE); |
| 2873 | client_socket_factory_.SetJobHasEstablishedConnection(0); |
| 2874 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
| 2875 | ClientSocketPool::kMaxConnectRetryIntervalMs * 100)); |
| 2876 | EXPECT_EQ(1, client_socket_factory_.allocation_count()); |
| 2877 | } |
| 2878 | |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 2879 | // Cancel a pending socket request while we're at max sockets, |
| 2880 | // and verify that the backup socket firing doesn't cause a crash. |
| 2881 | TEST_F(ClientSocketPoolBaseTest, BackupSocketCancelAtMaxSockets) { |
| 2882 | // Max 4 sockets globally, max 4 sockets per group. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2883 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets, |
| 2884 | true /* enable_backup_connect_jobs */); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 2885 | |
[email protected] | 4baaf9d | 2010-08-31 15:15:44 | [diff] [blame] | 2886 | // Create the first socket and set to ERR_IO_PENDING. This starts the backup |
| 2887 | // timer. |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 2888 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2889 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2890 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2891 | EXPECT_EQ( |
| 2892 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2893 | handle.Init(TestGroupId("bar"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2894 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2895 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2896 | NetLogWithSource())); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 2897 | |
| 2898 | // Start (MaxSockets - 1) connected sockets to reach max sockets. |
| 2899 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 2900 | ClientSocketHandle handles[kDefaultMaxSockets]; |
| 2901 | for (int i = 1; i < kDefaultMaxSockets; ++i) { |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2902 | TestCompletionCallback callback; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2903 | EXPECT_EQ(OK, handles[i].Init(TestGroupId("bar"), params_, DEFAULT_PRIORITY, |
| 2904 | SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 2905 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2906 | callback.callback(), |
| 2907 | ClientSocketPool::ProxyAuthCallback(), |
| 2908 | pool_.get(), NetLogWithSource())); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 2909 | } |
| 2910 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 2911 | base::RunLoop().RunUntilIdle(); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 2912 | |
| 2913 | // Cancel the pending request. |
| 2914 | handle.Reset(); |
| 2915 | |
| 2916 | // 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] | 2917 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
[email protected] | 26b997396 | 2012-01-28 00:57:00 | [diff] [blame] | 2918 | ClientSocketPool::kMaxConnectRetryIntervalMs / 2 * 3)); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 2919 | |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 2920 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 2921 | } |
| 2922 | |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 2923 | TEST_F(ClientSocketPoolBaseTest, CancelBackupSocketAfterCancelingAllRequests) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2924 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets, |
| 2925 | true /* enable_backup_connect_jobs */); |
[email protected] | 4baaf9d | 2010-08-31 15:15:44 | [diff] [blame] | 2926 | |
| 2927 | // Create the first socket and set to ERR_IO_PENDING. This starts the backup |
| 2928 | // timer. |
| 2929 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2930 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2931 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2932 | EXPECT_EQ( |
| 2933 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2934 | handle.Init(TestGroupId("bar"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2935 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2936 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2937 | NetLogWithSource())); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2938 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("bar"))); |
| 2939 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("bar"))); |
| 2940 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 2941 | TestGroupId("bar"))); |
| 2942 | EXPECT_EQ( |
| 2943 | 0u, pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("bar"))); |
[email protected] | 4baaf9d | 2010-08-31 15:15:44 | [diff] [blame] | 2944 | |
| 2945 | // Cancel the socket request. This should cancel the backup timer. Wait for |
| 2946 | // the backup time to see if it indeed got canceled. |
| 2947 | handle.Reset(); |
| 2948 | // 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] | 2949 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
[email protected] | 26b997396 | 2012-01-28 00:57:00 | [diff] [blame] | 2950 | ClientSocketPool::kMaxConnectRetryIntervalMs / 2 * 3)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2951 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("bar"))); |
| 2952 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("bar"))); |
[email protected] | 4baaf9d | 2010-08-31 15:15:44 | [diff] [blame] | 2953 | } |
| 2954 | |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 2955 | TEST_F(ClientSocketPoolBaseTest, CancelBackupSocketAfterFinishingAllRequests) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2956 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets, |
| 2957 | true /* enable_backup_connect_jobs */); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 2958 | |
| 2959 | // Create the first socket and set to ERR_IO_PENDING. This starts the backup |
| 2960 | // timer. |
| 2961 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2962 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2963 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2964 | EXPECT_EQ( |
| 2965 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2966 | handle.Init(TestGroupId("bar"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2967 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2968 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2969 | NetLogWithSource())); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 2970 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2971 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2972 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 2973 | EXPECT_EQ( |
| 2974 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2975 | handle2.Init(TestGroupId("bar"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 2976 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2977 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2978 | pool_.get(), NetLogWithSource())); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2979 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("bar"))); |
| 2980 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("bar"))); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 2981 | |
| 2982 | // Cancel request 1 and then complete request 2. With the requests finished, |
| 2983 | // the backup timer should be cancelled. |
| 2984 | handle.Reset(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2985 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 2986 | // 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] | 2987 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
[email protected] | 26b997396 | 2012-01-28 00:57:00 | [diff] [blame] | 2988 | ClientSocketPool::kMaxConnectRetryIntervalMs / 2 * 3)); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 2989 | } |
| 2990 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 2991 | // Test delayed socket binding for the case where we have two connects, |
| 2992 | // and while one is waiting on a connect, the other frees up. |
| 2993 | // The socket waiting on a connect should switch immediately to the freed |
| 2994 | // up socket. |
| 2995 | TEST_F(ClientSocketPoolBaseTest, DelayedSocketBindingWaitingForConnect) { |
| 2996 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2997 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2998 | |
| 2999 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3000 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3001 | EXPECT_EQ( |
| 3002 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3003 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3004 | ClientSocketPool::RespectLimits::ENABLED, |
| 3005 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3006 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3007 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3008 | |
| 3009 | // No idle sockets, no pending jobs. |
| 3010 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3011 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3012 | |
| 3013 | // Create a second socket to the same host, but this one will wait. |
| 3014 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3015 | ClientSocketHandle handle2; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3016 | EXPECT_EQ( |
| 3017 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3018 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3019 | ClientSocketPool::RespectLimits::ENABLED, |
| 3020 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3021 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3022 | // No idle sockets, and one connecting job. |
| 3023 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3024 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3025 | |
| 3026 | // Return the first handle to the pool. This will initiate the delayed |
| 3027 | // binding. |
| 3028 | handle1.Reset(); |
| 3029 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3030 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3031 | |
| 3032 | // Still no idle sockets, still one pending connect job. |
| 3033 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3034 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3035 | |
| 3036 | // The second socket connected, even though it was a Waiting Job. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3037 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3038 | |
| 3039 | // And we can see there is still one job waiting. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3040 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3041 | |
| 3042 | // Finally, signal the waiting Connect. |
| 3043 | client_socket_factory_.SignalJobs(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3044 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3045 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3046 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3047 | } |
| 3048 | |
| 3049 | // Test delayed socket binding when a group is at capacity and one |
| 3050 | // of the group's sockets frees up. |
| 3051 | TEST_F(ClientSocketPoolBaseTest, DelayedSocketBindingAtGroupCapacity) { |
| 3052 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3053 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3054 | |
| 3055 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3056 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3057 | EXPECT_EQ( |
| 3058 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3059 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3060 | ClientSocketPool::RespectLimits::ENABLED, |
| 3061 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3062 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3063 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3064 | |
| 3065 | // No idle sockets, no pending jobs. |
| 3066 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3067 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3068 | |
| 3069 | // Create a second socket to the same host, but this one will wait. |
| 3070 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3071 | ClientSocketHandle handle2; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3072 | EXPECT_EQ( |
| 3073 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3074 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3075 | ClientSocketPool::RespectLimits::ENABLED, |
| 3076 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3077 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3078 | // No idle sockets, and one connecting job. |
| 3079 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3080 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3081 | |
| 3082 | // Return the first handle to the pool. This will initiate the delayed |
| 3083 | // binding. |
| 3084 | handle1.Reset(); |
| 3085 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3086 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3087 | |
| 3088 | // Still no idle sockets, still one pending connect job. |
| 3089 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3090 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3091 | |
| 3092 | // The second socket connected, even though it was a Waiting Job. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3093 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3094 | |
| 3095 | // And we can see there is still one job waiting. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3096 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3097 | |
| 3098 | // Finally, signal the waiting Connect. |
| 3099 | client_socket_factory_.SignalJobs(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3100 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3101 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3102 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3103 | } |
| 3104 | |
| 3105 | // Test out the case where we have one socket connected, one |
| 3106 | // connecting, when the first socket finishes and goes idle. |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3107 | // Although the second connection is pending, the second request |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3108 | // should complete, by taking the first socket's idle socket. |
| 3109 | TEST_F(ClientSocketPoolBaseTest, DelayedSocketBindingAtStall) { |
| 3110 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3111 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3112 | |
| 3113 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3114 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3115 | EXPECT_EQ( |
| 3116 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3117 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3118 | ClientSocketPool::RespectLimits::ENABLED, |
| 3119 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3120 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3121 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3122 | |
| 3123 | // No idle sockets, no pending jobs. |
| 3124 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3125 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3126 | |
| 3127 | // Create a second socket to the same host, but this one will wait. |
| 3128 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3129 | ClientSocketHandle handle2; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3130 | EXPECT_EQ( |
| 3131 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3132 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3133 | ClientSocketPool::RespectLimits::ENABLED, |
| 3134 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3135 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3136 | // No idle sockets, and one connecting job. |
| 3137 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3138 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3139 | |
| 3140 | // Return the first handle to the pool. This will initiate the delayed |
| 3141 | // binding. |
| 3142 | handle1.Reset(); |
| 3143 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3144 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3145 | |
| 3146 | // Still no idle sockets, still one pending connect job. |
| 3147 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3148 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3149 | |
| 3150 | // The second socket connected, even though it was a Waiting Job. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3151 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3152 | |
| 3153 | // And we can see there is still one job waiting. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3154 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3155 | |
| 3156 | // Finally, signal the waiting Connect. |
| 3157 | client_socket_factory_.SignalJobs(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3158 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3159 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3160 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3161 | } |
| 3162 | |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3163 | // Cover the case where on an available socket slot, we have one pending |
| 3164 | // request that completes synchronously, thereby making the Group empty. |
| 3165 | TEST_F(ClientSocketPoolBaseTest, SynchronouslyProcessOnePendingRequest) { |
| 3166 | const int kUnlimitedSockets = 100; |
| 3167 | const int kOneSocketPerGroup = 1; |
| 3168 | CreatePool(kUnlimitedSockets, kOneSocketPerGroup); |
| 3169 | |
| 3170 | // Make the first request asynchronous fail. |
| 3171 | // This will free up a socket slot later. |
| 3172 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
| 3173 | |
| 3174 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3175 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3176 | EXPECT_EQ( |
| 3177 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3178 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3179 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3180 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3181 | pool_.get(), NetLogWithSource())); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3182 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3183 | |
| 3184 | // Make the second request synchronously fail. This should make the Group |
| 3185 | // empty. |
| 3186 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
| 3187 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3188 | TestCompletionCallback callback2; |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3189 | // It'll be ERR_IO_PENDING now, but the TestConnectJob will synchronously fail |
| 3190 | // when created. |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3191 | EXPECT_EQ( |
| 3192 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3193 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3194 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3195 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3196 | pool_.get(), NetLogWithSource())); |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3197 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3198 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3199 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3200 | EXPECT_THAT(callback1.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 3201 | EXPECT_THAT(callback2.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3202 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3203 | } |
| 3204 | |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3205 | TEST_F(ClientSocketPoolBaseTest, PreferUsedSocketToUnusedSocket) { |
| 3206 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 3207 | |
| 3208 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3209 | |
| 3210 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3211 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3212 | EXPECT_EQ( |
| 3213 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3214 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3215 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3216 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3217 | pool_.get(), NetLogWithSource())); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3218 | |
| 3219 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3220 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3221 | EXPECT_EQ( |
| 3222 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3223 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3224 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3225 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3226 | pool_.get(), NetLogWithSource())); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3227 | ClientSocketHandle handle3; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3228 | TestCompletionCallback callback3; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3229 | EXPECT_EQ( |
| 3230 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3231 | handle3.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3232 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3233 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3234 | pool_.get(), NetLogWithSource())); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3235 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3236 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3237 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
| 3238 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3239 | |
| 3240 | // Use the socket. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 3241 | EXPECT_EQ(1, handle1.socket()->Write(nullptr, 1, CompletionOnceCallback(), |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 3242 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 3243 | EXPECT_EQ(1, handle3.socket()->Write(nullptr, 1, CompletionOnceCallback(), |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 3244 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3245 | |
| 3246 | handle1.Reset(); |
| 3247 | handle2.Reset(); |
| 3248 | handle3.Reset(); |
| 3249 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3250 | EXPECT_EQ(OK, handle1.Init( |
| 3251 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
| 3252 | ClientSocketPool::RespectLimits::ENABLED, |
| 3253 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3254 | pool_.get(), NetLogWithSource())); |
| 3255 | EXPECT_EQ(OK, handle2.Init( |
| 3256 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
| 3257 | ClientSocketPool::RespectLimits::ENABLED, |
| 3258 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3259 | pool_.get(), NetLogWithSource())); |
| 3260 | EXPECT_EQ(OK, handle3.Init( |
| 3261 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
| 3262 | ClientSocketPool::RespectLimits::ENABLED, |
| 3263 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3264 | pool_.get(), NetLogWithSource())); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3265 | |
| 3266 | EXPECT_TRUE(handle1.socket()->WasEverUsed()); |
| 3267 | EXPECT_TRUE(handle2.socket()->WasEverUsed()); |
| 3268 | EXPECT_FALSE(handle3.socket()->WasEverUsed()); |
| 3269 | } |
| 3270 | |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3271 | TEST_F(ClientSocketPoolBaseTest, RequestSockets) { |
| 3272 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3273 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3274 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3275 | pool_->RequestSockets(TestGroupId("a"), params_, 2, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3276 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3277 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3278 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3279 | EXPECT_EQ(2u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3280 | TestGroupId("a"))); |
| 3281 | EXPECT_EQ(2u, |
| 3282 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3283 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3284 | |
| 3285 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3286 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3287 | EXPECT_EQ( |
| 3288 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3289 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3290 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3291 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3292 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3293 | |
| 3294 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3295 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3296 | EXPECT_EQ( |
| 3297 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3298 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3299 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3300 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3301 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3302 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3303 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3304 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3305 | TestGroupId("a"))); |
| 3306 | EXPECT_EQ(0u, |
| 3307 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3308 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3309 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3310 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3311 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3312 | handle1.Reset(); |
| 3313 | handle2.Reset(); |
| 3314 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3315 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3316 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3317 | TestGroupId("a"))); |
| 3318 | EXPECT_EQ(0u, |
| 3319 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3320 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3321 | } |
| 3322 | |
| 3323 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsWhenAlreadyHaveAConnectJob) { |
| 3324 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3325 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3326 | |
| 3327 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3328 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3329 | EXPECT_EQ( |
| 3330 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3331 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3332 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3333 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3334 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3335 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3336 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3337 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3338 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3339 | TestGroupId("a"))); |
| 3340 | EXPECT_EQ(0u, |
| 3341 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3342 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3343 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3344 | pool_->RequestSockets(TestGroupId("a"), params_, 2, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3345 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3346 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3347 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3348 | TestGroupId("a"))); |
| 3349 | EXPECT_EQ(1u, |
| 3350 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3351 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3352 | |
| 3353 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3354 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3355 | EXPECT_EQ( |
| 3356 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3357 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3358 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3359 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3360 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3361 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3362 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3363 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3364 | TestGroupId("a"))); |
| 3365 | EXPECT_EQ(0u, |
| 3366 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3367 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3368 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3369 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3370 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3371 | handle1.Reset(); |
| 3372 | handle2.Reset(); |
| 3373 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3374 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3375 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3376 | TestGroupId("a"))); |
| 3377 | EXPECT_EQ(0u, |
| 3378 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3379 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3380 | } |
| 3381 | |
| 3382 | TEST_F(ClientSocketPoolBaseTest, |
| 3383 | RequestSocketsWhenAlreadyHaveMultipleConnectJob) { |
| 3384 | CreatePool(4, 4); |
| 3385 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3386 | |
| 3387 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3388 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3389 | EXPECT_EQ( |
| 3390 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3391 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3392 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3393 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3394 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3395 | |
| 3396 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3397 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3398 | EXPECT_EQ( |
| 3399 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3400 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3401 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3402 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3403 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3404 | |
| 3405 | ClientSocketHandle handle3; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3406 | TestCompletionCallback callback3; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3407 | EXPECT_EQ( |
| 3408 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3409 | handle3.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3410 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3411 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3412 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3413 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3414 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3415 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3416 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3417 | TestGroupId("a"))); |
| 3418 | EXPECT_EQ(0u, |
| 3419 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3420 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3421 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3422 | pool_->RequestSockets(TestGroupId("a"), params_, 2, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3423 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3424 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3425 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3426 | TestGroupId("a"))); |
| 3427 | EXPECT_EQ(0u, |
| 3428 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3429 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3430 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3431 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3432 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
| 3433 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3434 | handle1.Reset(); |
| 3435 | handle2.Reset(); |
| 3436 | handle3.Reset(); |
| 3437 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3438 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3439 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3440 | TestGroupId("a"))); |
| 3441 | EXPECT_EQ(0u, |
| 3442 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3443 | EXPECT_EQ(3u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3444 | } |
| 3445 | |
| 3446 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsAtMaxSocketLimit) { |
| 3447 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 3448 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3449 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3450 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3451 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3452 | pool_->RequestSockets(TestGroupId("a"), params_, kDefaultMaxSockets, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3453 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3454 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3455 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3456 | EXPECT_EQ(kDefaultMaxSockets, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3457 | static_cast<int>( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3458 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
| 3459 | EXPECT_EQ( |
| 3460 | kDefaultMaxSockets, |
| 3461 | static_cast<int>(pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3462 | TestGroupId("a")))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3463 | EXPECT_EQ(kDefaultMaxSockets, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3464 | static_cast<int>(pool_->NumUnassignedConnectJobsInGroupForTesting( |
| 3465 | TestGroupId("a")))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3466 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3467 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("b"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3468 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3469 | pool_->RequestSockets(TestGroupId("b"), params_, kDefaultMaxSockets, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3470 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3471 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3472 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("b"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3473 | } |
| 3474 | |
| 3475 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsHitMaxSocketLimit) { |
| 3476 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 3477 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3478 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3479 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3480 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3481 | pool_->RequestSockets(TestGroupId("a"), params_, kDefaultMaxSockets - 1, |
Charlie Harrison | 55ce608 | 2018-05-14 02:25:57 | [diff] [blame] | 3482 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3483 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3484 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3485 | EXPECT_EQ(kDefaultMaxSockets - 1, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3486 | static_cast<int>( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3487 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
| 3488 | EXPECT_EQ( |
| 3489 | kDefaultMaxSockets - 1, |
| 3490 | static_cast<int>(pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3491 | TestGroupId("a")))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3492 | EXPECT_EQ(kDefaultMaxSockets - 1, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3493 | static_cast<int>(pool_->NumUnassignedConnectJobsInGroupForTesting( |
| 3494 | TestGroupId("a")))); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 3495 | EXPECT_FALSE(pool_->IsStalled()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3496 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3497 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("b"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3498 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3499 | pool_->RequestSockets(TestGroupId("b"), params_, kDefaultMaxSockets, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3500 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3501 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3502 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("b"))); |
| 3503 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 3504 | EXPECT_FALSE(pool_->IsStalled()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3505 | } |
| 3506 | |
| 3507 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsCountIdleSockets) { |
| 3508 | CreatePool(4, 4); |
| 3509 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3510 | |
| 3511 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3512 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3513 | EXPECT_EQ( |
| 3514 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3515 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3516 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3517 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3518 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3519 | ASSERT_THAT(callback1.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3520 | handle1.Reset(); |
| 3521 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3522 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3523 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3524 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3525 | TestGroupId("a"))); |
| 3526 | EXPECT_EQ(0u, |
| 3527 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3528 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3529 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3530 | pool_->RequestSockets(TestGroupId("a"), params_, 2, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3531 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3532 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3533 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3534 | TestGroupId("a"))); |
| 3535 | EXPECT_EQ(1u, |
| 3536 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3537 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3538 | } |
| 3539 | |
| 3540 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsCountActiveSockets) { |
| 3541 | CreatePool(4, 4); |
| 3542 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3543 | |
| 3544 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3545 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3546 | EXPECT_EQ( |
| 3547 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3548 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3549 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3550 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3551 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3552 | ASSERT_THAT(callback1.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3553 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3554 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3555 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3556 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3557 | TestGroupId("a"))); |
| 3558 | EXPECT_EQ(0u, |
| 3559 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3560 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3561 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3562 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3563 | pool_->RequestSockets(TestGroupId("a"), params_, 2, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3564 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3565 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3566 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3567 | TestGroupId("a"))); |
| 3568 | EXPECT_EQ(1u, |
| 3569 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3570 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3571 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3572 | } |
| 3573 | |
| 3574 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsSynchronous) { |
| 3575 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3576 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 3577 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3578 | pool_->RequestSockets(TestGroupId("a"), params_, kDefaultMaxSocketsPerGroup, |
Charlie Harrison | 55ce608 | 2018-05-14 02:25:57 | [diff] [blame] | 3579 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3580 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3581 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3582 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3583 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3584 | TestGroupId("a"))); |
| 3585 | EXPECT_EQ(0u, |
| 3586 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3587 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3588 | static_cast<int>(pool_->IdleSocketCountInGroup(TestGroupId("a")))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3589 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3590 | pool_->RequestSockets(TestGroupId("b"), params_, kDefaultMaxSocketsPerGroup, |
Charlie Harrison | 55ce608 | 2018-05-14 02:25:57 | [diff] [blame] | 3591 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3592 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3593 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 3594 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3595 | TestGroupId("b"))); |
| 3596 | EXPECT_EQ(0u, |
| 3597 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3598 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3599 | static_cast<int>(pool_->IdleSocketCountInGroup(TestGroupId("b")))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3600 | } |
| 3601 | |
[email protected] | 3c819f52 | 2010-12-02 02:03:12 | [diff] [blame] | 3602 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsSynchronousError) { |
| 3603 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3604 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
| 3605 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3606 | pool_->RequestSockets(TestGroupId("a"), params_, kDefaultMaxSocketsPerGroup, |
Charlie Harrison | 55ce608 | 2018-05-14 02:25:57 | [diff] [blame] | 3607 | NetLogWithSource()); |
[email protected] | 3c819f52 | 2010-12-02 02:03:12 | [diff] [blame] | 3608 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3609 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | fd2e53e | 2011-01-14 20:40:52 | [diff] [blame] | 3610 | |
| 3611 | connect_job_factory_->set_job_type( |
| 3612 | TestConnectJob::kMockAdditionalErrorStateJob); |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3613 | pool_->RequestSockets(TestGroupId("a"), params_, kDefaultMaxSocketsPerGroup, |
Charlie Harrison | 55ce608 | 2018-05-14 02:25:57 | [diff] [blame] | 3614 | NetLogWithSource()); |
[email protected] | fd2e53e | 2011-01-14 20:40:52 | [diff] [blame] | 3615 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3616 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | 3c819f52 | 2010-12-02 02:03:12 | [diff] [blame] | 3617 | } |
| 3618 | |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3619 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsMultipleTimesDoesNothing) { |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3620 | CreatePool(4, 4); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3621 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3622 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3623 | pool_->RequestSockets(TestGroupId("a"), params_, 2, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3624 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3625 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3626 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3627 | EXPECT_EQ(2u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3628 | TestGroupId("a"))); |
| 3629 | EXPECT_EQ(2u, |
| 3630 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3631 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3632 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3633 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3634 | pool_->RequestSockets(TestGroupId("a"), params_, 2, NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3635 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3636 | EXPECT_EQ(2u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3637 | TestGroupId("a"))); |
| 3638 | EXPECT_EQ(2u, |
| 3639 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3640 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3641 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3642 | |
| 3643 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3644 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3645 | EXPECT_EQ( |
| 3646 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3647 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3648 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3649 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3650 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3651 | |
| 3652 | client_socket_factory_.SignalJob(0); |
| 3653 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3654 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3655 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3656 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3657 | TestGroupId("a"))); |
| 3658 | EXPECT_EQ(1u, |
| 3659 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3660 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3661 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3662 | |
| 3663 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3664 | TestCompletionCallback callback2; |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3665 | EXPECT_EQ( |
| 3666 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3667 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3668 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3669 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3670 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3671 | client_socket_factory_.SignalJob(0); |
| 3672 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3673 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3674 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3675 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3676 | TestGroupId("a"))); |
| 3677 | EXPECT_EQ(0u, |
| 3678 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3679 | EXPECT_EQ(2, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3680 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3681 | |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3682 | handle1.Reset(); |
| 3683 | handle2.Reset(); |
| 3684 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3685 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3686 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3687 | TestGroupId("a"))); |
| 3688 | EXPECT_EQ(0u, |
| 3689 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3690 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3691 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3692 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3693 | pool_->RequestSockets(TestGroupId("a"), params_, 2, NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3694 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3695 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3696 | TestGroupId("a"))); |
| 3697 | EXPECT_EQ(0u, |
| 3698 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3699 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3700 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3701 | } |
| 3702 | |
| 3703 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsDifferentNumSockets) { |
| 3704 | CreatePool(4, 4); |
| 3705 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3706 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3707 | pool_->RequestSockets(TestGroupId("a"), params_, 1, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3708 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3709 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3710 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3711 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3712 | TestGroupId("a"))); |
| 3713 | EXPECT_EQ(1u, |
| 3714 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3715 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3716 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3717 | pool_->RequestSockets(TestGroupId("a"), params_, 2, NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3718 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3719 | EXPECT_EQ(2u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3720 | TestGroupId("a"))); |
| 3721 | EXPECT_EQ(2u, |
| 3722 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3723 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3724 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3725 | pool_->RequestSockets(TestGroupId("a"), params_, 3, NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3726 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3727 | EXPECT_EQ(3u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3728 | TestGroupId("a"))); |
| 3729 | EXPECT_EQ(3u, |
| 3730 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3731 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3732 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3733 | pool_->RequestSockets(TestGroupId("a"), params_, 1, NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3734 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3735 | EXPECT_EQ(3u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3736 | TestGroupId("a"))); |
| 3737 | EXPECT_EQ(3u, |
| 3738 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3739 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3740 | } |
| 3741 | |
| 3742 | TEST_F(ClientSocketPoolBaseTest, PreconnectJobsTakenByNormalRequests) { |
| 3743 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3744 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3745 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3746 | pool_->RequestSockets(TestGroupId("a"), params_, 1, 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(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3750 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3751 | TestGroupId("a"))); |
| 3752 | EXPECT_EQ(1u, |
| 3753 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3754 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3755 | |
| 3756 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3757 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3758 | EXPECT_EQ( |
| 3759 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3760 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3761 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3762 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3763 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3764 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3765 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3766 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3767 | TestGroupId("a"))); |
| 3768 | EXPECT_EQ(0u, |
| 3769 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3770 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3771 | |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3772 | client_socket_factory_.SignalJobs(); |
| 3773 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3774 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3775 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3776 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3777 | TestGroupId("a"))); |
| 3778 | EXPECT_EQ(0u, |
| 3779 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3780 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3781 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3782 | |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 3783 | // Make sure if a preconnected socket is not fully connected when a request |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 3784 | // starts, it has a connect start time. |
| 3785 | TestLoadTimingInfoConnectedNotReused(handle1); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3786 | handle1.Reset(); |
| 3787 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3788 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3789 | } |
| 3790 | |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 3791 | // Checks that fully connected preconnect jobs have no connect times, and are |
| 3792 | // marked as reused. |
| 3793 | TEST_F(ClientSocketPoolBaseTest, ConnectedPreconnectJobsHaveNoConnectTimes) { |
| 3794 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3795 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3796 | pool_->RequestSockets(TestGroupId("a"), params_, 1, NetLogWithSource()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 3797 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3798 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3799 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3800 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3801 | TestGroupId("a"))); |
| 3802 | EXPECT_EQ(0u, |
| 3803 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3804 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 3805 | |
| 3806 | ClientSocketHandle handle; |
| 3807 | TestCompletionCallback callback; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3808 | EXPECT_EQ(OK, handle.Init( |
| 3809 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
| 3810 | ClientSocketPool::RespectLimits::ENABLED, |
| 3811 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3812 | pool_.get(), NetLogWithSource())); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 3813 | |
| 3814 | // Make sure the idle socket was used. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3815 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 3816 | |
| 3817 | TestLoadTimingInfoConnectedReused(handle); |
| 3818 | handle.Reset(); |
| 3819 | TestLoadTimingInfoNotConnected(handle); |
| 3820 | } |
| 3821 | |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3822 | // http://crbug.com/64940 regression test. |
| 3823 | TEST_F(ClientSocketPoolBaseTest, PreconnectClosesIdleSocketRemovesGroup) { |
| 3824 | const int kMaxTotalSockets = 3; |
| 3825 | const int kMaxSocketsPerGroup = 2; |
| 3826 | CreatePool(kMaxTotalSockets, kMaxSocketsPerGroup); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3827 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3828 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 3829 | // Note that group id ordering matters here. "a" comes before "b", so |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3830 | // CloseOneIdleSocket() will try to close "a"'s idle socket. |
| 3831 | |
| 3832 | // Set up one idle socket in "a". |
| 3833 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3834 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3835 | EXPECT_EQ( |
| 3836 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3837 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3838 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3839 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3840 | pool_.get(), NetLogWithSource())); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3841 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3842 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3843 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3844 | TestGroupId("a"))); |
| 3845 | EXPECT_EQ(0u, |
| 3846 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3847 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3848 | |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3849 | client_socket_factory_.SignalJobs(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3850 | ASSERT_THAT(callback1.WaitForResult(), IsOk()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3851 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3852 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3853 | TestGroupId("a"))); |
| 3854 | EXPECT_EQ(0u, |
| 3855 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3856 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3857 | |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3858 | handle1.Reset(); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3859 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3860 | |
| 3861 | // Set up two active sockets in "b". |
| 3862 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3863 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3864 | EXPECT_EQ( |
| 3865 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3866 | handle1.Init(TestGroupId("b"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3867 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3868 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3869 | pool_.get(), NetLogWithSource())); |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3870 | EXPECT_EQ( |
| 3871 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3872 | handle2.Init(TestGroupId("b"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3873 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3874 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3875 | pool_.get(), NetLogWithSource())); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3876 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3877 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("b"))); |
| 3878 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 3879 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3880 | TestGroupId("b"))); |
| 3881 | EXPECT_EQ(0u, |
| 3882 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3883 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3884 | |
| 3885 | client_socket_factory_.SignalJobs(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3886 | ASSERT_THAT(callback1.WaitForResult(), IsOk()); |
| 3887 | ASSERT_THAT(callback2.WaitForResult(), IsOk()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3888 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3889 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3890 | TestGroupId("b"))); |
| 3891 | EXPECT_EQ(0u, |
| 3892 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 3893 | EXPECT_EQ(2, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3894 | |
| 3895 | // Now we have 1 idle socket in "a" and 2 active sockets in "b". This means |
| 3896 | // we've maxed out on sockets, since we set |kMaxTotalSockets| to 3. |
| 3897 | // Requesting 2 preconnected sockets for "a" should fail to allocate any more |
| 3898 | // sockets for "a", and "b" should still have 2 active sockets. |
| 3899 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3900 | pool_->RequestSockets(TestGroupId("a"), params_, 2, NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3901 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3902 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3903 | TestGroupId("a"))); |
| 3904 | EXPECT_EQ(0u, |
| 3905 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3906 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3907 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 3908 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 3909 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3910 | TestGroupId("b"))); |
| 3911 | EXPECT_EQ(0u, |
| 3912 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3913 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3914 | EXPECT_EQ(2, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3915 | |
| 3916 | // Now release the 2 active sockets for "b". This will give us 1 idle socket |
| 3917 | // in "a" and 2 idle sockets in "b". Requesting 2 preconnected sockets for |
| 3918 | // "a" should result in closing 1 for "b". |
| 3919 | handle1.Reset(); |
| 3920 | handle2.Reset(); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3921 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3922 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3923 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3924 | pool_->RequestSockets(TestGroupId("a"), params_, 2, NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3925 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3926 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3927 | TestGroupId("a"))); |
| 3928 | EXPECT_EQ(1u, |
| 3929 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3930 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3931 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 3932 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 3933 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3934 | TestGroupId("b"))); |
| 3935 | EXPECT_EQ(0u, |
| 3936 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3937 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3938 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3939 | } |
| 3940 | |
[email protected] | b7b8be4 | 2011-07-12 12:46:41 | [diff] [blame] | 3941 | TEST_F(ClientSocketPoolBaseTest, PreconnectWithoutBackupJob) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3942 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 3943 | true /* enable_backup_connect_jobs */); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 3944 | |
| 3945 | // Make the ConnectJob hang until it times out, shorten the timeout. |
| 3946 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3947 | connect_job_factory_->set_timeout_duration( |
| 3948 | base::TimeDelta::FromMilliseconds(500)); |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3949 | pool_->RequestSockets(TestGroupId("a"), params_, 1, NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3950 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3951 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3952 | TestGroupId("a"))); |
| 3953 | EXPECT_EQ(1u, |
| 3954 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3955 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 3956 | |
[email protected] | b7b8be4 | 2011-07-12 12:46:41 | [diff] [blame] | 3957 | // Verify the backup timer doesn't create a backup job, by making |
| 3958 | // the backup job a pending job instead of a waiting job, so it |
| 3959 | // *would* complete if it were created. |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 3960 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 3961 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
Gabriel Charette | ea91801 | 2018-05-16 11:53:44 | [diff] [blame] | 3962 | FROM_HERE, base::RunLoop::QuitCurrentWhenIdleClosureDeprecated(), |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 3963 | base::TimeDelta::FromSeconds(1)); |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3964 | base::RunLoop().Run(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3965 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 3966 | } |
| 3967 | |
[email protected] | b7b8be4 | 2011-07-12 12:46:41 | [diff] [blame] | 3968 | TEST_F(ClientSocketPoolBaseTest, PreconnectWithBackupJob) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3969 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 3970 | true /* enable_backup_connect_jobs */); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 3971 | |
| 3972 | // Make the ConnectJob hang forever. |
| 3973 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3974 | pool_->RequestSockets(TestGroupId("a"), params_, 1, NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3975 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3976 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3977 | TestGroupId("a"))); |
| 3978 | EXPECT_EQ(1u, |
| 3979 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3980 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3981 | base::RunLoop().RunUntilIdle(); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 3982 | |
| 3983 | // Make the backup job be a pending job, so it completes normally. |
| 3984 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3985 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3986 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3987 | EXPECT_EQ( |
| 3988 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3989 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3990 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 3991 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 3992 | NetLogWithSource())); |
[email protected] | b7b8be4 | 2011-07-12 12:46:41 | [diff] [blame] | 3993 | // Timer has started, but the backup connect job shouldn't be created yet. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3994 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3995 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3996 | TestGroupId("a"))); |
| 3997 | EXPECT_EQ(0u, |
| 3998 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3999 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4000 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4001 | ASSERT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4002 | |
| 4003 | // The hung connect job should still be there, but everything else should be |
| 4004 | // complete. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4005 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4006 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4007 | TestGroupId("a"))); |
| 4008 | EXPECT_EQ(1u, |
| 4009 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4010 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4011 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4012 | } |
| 4013 | |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4014 | // Tests that a preconnect that starts out with unread data can still be used. |
| 4015 | // http://crbug.com/334467 |
| 4016 | TEST_F(ClientSocketPoolBaseTest, PreconnectWithUnreadData) { |
| 4017 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4018 | connect_job_factory_->set_job_type(TestConnectJob::kMockUnreadDataJob); |
| 4019 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 4020 | pool_->RequestSockets(TestGroupId("a"), params_, 1, NetLogWithSource()); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4021 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4022 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 4023 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4024 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4025 | TestGroupId("a"))); |
| 4026 | EXPECT_EQ(0u, |
| 4027 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4028 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4029 | |
| 4030 | // Fail future jobs to be sure that handle receives the preconnected socket |
| 4031 | // rather than closing it and making a new one. |
| 4032 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
| 4033 | ClientSocketHandle handle; |
| 4034 | TestCompletionCallback callback; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4035 | EXPECT_EQ(OK, handle.Init( |
| 4036 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
| 4037 | ClientSocketPool::RespectLimits::ENABLED, |
| 4038 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4039 | pool_.get(), NetLogWithSource())); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4040 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4041 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 4042 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4043 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4044 | TestGroupId("a"))); |
| 4045 | EXPECT_EQ(0u, |
| 4046 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4047 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4048 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4049 | |
| 4050 | // Drain the pending read. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 4051 | EXPECT_EQ(1, handle.socket()->Read(nullptr, 1, CompletionOnceCallback())); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4052 | |
| 4053 | TestLoadTimingInfoConnectedReused(handle); |
| 4054 | handle.Reset(); |
| 4055 | |
| 4056 | // The socket should be usable now that it's idle again. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4057 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4058 | } |
| 4059 | |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4060 | TEST_F(ClientSocketPoolBaseTest, RequestGetsAssignedJob) { |
| 4061 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4062 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4063 | |
| 4064 | ClientSocketHandle handle1; |
| 4065 | TestCompletionCallback callback1; |
| 4066 | EXPECT_EQ( |
| 4067 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4068 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4069 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4070 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4071 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4072 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4073 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4074 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4075 | TestGroupId("a"))); |
| 4076 | EXPECT_EQ(0u, |
| 4077 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4078 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4079 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4080 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4081 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4082 | } |
| 4083 | |
| 4084 | TEST_F(ClientSocketPoolBaseTest, MultipleRequestsGetAssignedJobs) { |
| 4085 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4086 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4087 | |
| 4088 | ClientSocketHandle handle1; |
| 4089 | TestCompletionCallback callback1; |
| 4090 | EXPECT_EQ( |
| 4091 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4092 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4093 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4094 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4095 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4096 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4097 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4098 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4099 | TestGroupId("a"))); |
| 4100 | EXPECT_EQ(0u, |
| 4101 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4102 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4103 | |
| 4104 | ClientSocketHandle handle2; |
| 4105 | TestCompletionCallback callback2; |
| 4106 | EXPECT_EQ( |
| 4107 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4108 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4109 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4110 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4111 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4112 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4113 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4114 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4115 | TestGroupId("a"))); |
| 4116 | EXPECT_EQ(0u, |
| 4117 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4118 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4119 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4120 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4121 | &handle1)); |
| 4122 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4123 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4124 | |
| 4125 | // One job completes. The other request should still have its job. |
| 4126 | client_socket_factory_.SignalJob(0); |
| 4127 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 4128 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4129 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4130 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4131 | TestGroupId("a"))); |
| 4132 | EXPECT_EQ(0u, |
| 4133 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4134 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4135 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4136 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4137 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4138 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4139 | } |
| 4140 | |
| 4141 | TEST_F(ClientSocketPoolBaseTest, PreconnectJobGetsAssignedToRequest) { |
| 4142 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4143 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4144 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 4145 | pool_->RequestSockets(TestGroupId("a"), params_, 1, NetLogWithSource()); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4146 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4147 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 4148 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4149 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4150 | TestGroupId("a"))); |
| 4151 | EXPECT_EQ(1u, |
| 4152 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4153 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4154 | |
| 4155 | ClientSocketHandle handle1; |
| 4156 | TestCompletionCallback callback1; |
| 4157 | EXPECT_EQ( |
| 4158 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4159 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4160 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4161 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4162 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4163 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4164 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4165 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4166 | TestGroupId("a"))); |
| 4167 | EXPECT_EQ(0u, |
| 4168 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4169 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4170 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4171 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4172 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4173 | } |
| 4174 | |
| 4175 | TEST_F(ClientSocketPoolBaseTest, HigherPriorityRequestStealsJob) { |
| 4176 | CreatePool(kDefaultMaxSockets, 1); |
| 4177 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4178 | |
| 4179 | ClientSocketHandle handle1; |
| 4180 | TestCompletionCallback callback1; |
| 4181 | EXPECT_EQ( |
| 4182 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4183 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4184 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4185 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4186 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4187 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4188 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4189 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4190 | TestGroupId("a"))); |
| 4191 | EXPECT_EQ(0u, |
| 4192 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4193 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4194 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4195 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4196 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4197 | |
| 4198 | // Insert a higher priority request |
| 4199 | ClientSocketHandle handle2; |
| 4200 | TestCompletionCallback callback2; |
| 4201 | EXPECT_EQ( |
| 4202 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4203 | handle2.Init(TestGroupId("a"), params_, HIGHEST, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4204 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4205 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4206 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4207 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4208 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4209 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4210 | TestGroupId("a"))); |
| 4211 | EXPECT_EQ(0u, |
| 4212 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4213 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4214 | |
| 4215 | // The highest priority request should steal the job from the default priority |
| 4216 | // request. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4217 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4218 | &handle2)); |
| 4219 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4220 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4221 | } |
| 4222 | |
| 4223 | TEST_F(ClientSocketPoolBaseTest, RequestStealsJobFromLowestRequestWithJob) { |
| 4224 | CreatePool(3, 3); |
| 4225 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4226 | |
| 4227 | ClientSocketHandle handle_lowest; |
| 4228 | TestCompletionCallback callback_lowest; |
| 4229 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4230 | handle_lowest.Init(TestGroupId("a"), params_, LOWEST, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4231 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4232 | callback_lowest.callback(), |
| 4233 | ClientSocketPool::ProxyAuthCallback(), |
| 4234 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4235 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4236 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4237 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4238 | TestGroupId("a"))); |
| 4239 | EXPECT_EQ(0u, |
| 4240 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4241 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4242 | |
| 4243 | ClientSocketHandle handle_highest; |
| 4244 | TestCompletionCallback callback_highest; |
| 4245 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4246 | handle_highest.Init(TestGroupId("a"), params_, HIGHEST, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4247 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4248 | callback_highest.callback(), |
| 4249 | ClientSocketPool::ProxyAuthCallback(), |
| 4250 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4251 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4252 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4253 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4254 | TestGroupId("a"))); |
| 4255 | EXPECT_EQ(0u, |
| 4256 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4257 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4258 | |
| 4259 | ClientSocketHandle handle_low; |
| 4260 | TestCompletionCallback callback_low; |
| 4261 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4262 | handle_low.Init(TestGroupId("a"), params_, LOW, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4263 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4264 | callback_low.callback(), |
| 4265 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4266 | NetLogWithSource())); |
| 4267 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4268 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4269 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4270 | TestGroupId("a"))); |
| 4271 | EXPECT_EQ(0u, |
| 4272 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4273 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4274 | |
| 4275 | ClientSocketHandle handle_lowest2; |
| 4276 | TestCompletionCallback callback_lowest2; |
| 4277 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4278 | handle_lowest2.Init(TestGroupId("a"), params_, LOWEST, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4279 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4280 | callback_lowest2.callback(), |
| 4281 | ClientSocketPool::ProxyAuthCallback(), |
| 4282 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4283 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4284 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4285 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4286 | TestGroupId("a"))); |
| 4287 | EXPECT_EQ(0u, |
| 4288 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4289 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4290 | |
| 4291 | // The top three requests in the queue should have jobs. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4292 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4293 | &handle_highest)); |
| 4294 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4295 | &handle_low)); |
| 4296 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4297 | &handle_lowest)); |
| 4298 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting( |
| 4299 | TestGroupId("a"), &handle_lowest2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4300 | |
| 4301 | // Add another request with medium priority. It should steal the job from the |
| 4302 | // lowest priority request with a job. |
| 4303 | ClientSocketHandle handle_medium; |
| 4304 | TestCompletionCallback callback_medium; |
| 4305 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4306 | handle_medium.Init(TestGroupId("a"), params_, MEDIUM, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4307 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4308 | callback_medium.callback(), |
| 4309 | ClientSocketPool::ProxyAuthCallback(), |
| 4310 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4311 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4312 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4313 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4314 | TestGroupId("a"))); |
| 4315 | EXPECT_EQ(0u, |
| 4316 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4317 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
| 4318 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4319 | &handle_highest)); |
| 4320 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4321 | &handle_medium)); |
| 4322 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4323 | &handle_low)); |
| 4324 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4325 | &handle_lowest)); |
| 4326 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting( |
| 4327 | TestGroupId("a"), &handle_lowest2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4328 | } |
| 4329 | |
| 4330 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeRequestStealsJob) { |
| 4331 | CreatePool(kDefaultMaxSockets, 1); |
| 4332 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4333 | |
| 4334 | ClientSocketHandle handle1; |
| 4335 | TestCompletionCallback callback1; |
| 4336 | EXPECT_EQ( |
| 4337 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4338 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4339 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4340 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4341 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4342 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4343 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4344 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4345 | TestGroupId("a"))); |
| 4346 | EXPECT_EQ(0u, |
| 4347 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4348 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4349 | |
| 4350 | ClientSocketHandle handle2; |
| 4351 | TestCompletionCallback callback2; |
| 4352 | EXPECT_EQ( |
| 4353 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4354 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4355 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4356 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4357 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4358 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4359 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4360 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4361 | TestGroupId("a"))); |
| 4362 | EXPECT_EQ(0u, |
| 4363 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4364 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4365 | |
| 4366 | // 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] | 4367 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4368 | &handle1)); |
| 4369 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4370 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4371 | |
| 4372 | // Reprioritizing the second request places it above the first, and it steals |
| 4373 | // the job from the first request. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4374 | pool_->SetPriority(TestGroupId("a"), &handle2, HIGHEST); |
| 4375 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4376 | &handle2)); |
| 4377 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4378 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4379 | } |
| 4380 | |
| 4381 | TEST_F(ClientSocketPoolBaseTest, CancelRequestReassignsJob) { |
| 4382 | CreatePool(kDefaultMaxSockets, 1); |
| 4383 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4384 | |
| 4385 | ClientSocketHandle handle1; |
| 4386 | TestCompletionCallback callback1; |
| 4387 | EXPECT_EQ( |
| 4388 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4389 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4390 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4391 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4392 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4393 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4394 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4395 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4396 | TestGroupId("a"))); |
| 4397 | EXPECT_EQ(0u, |
| 4398 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4399 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4400 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4401 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4402 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4403 | |
| 4404 | ClientSocketHandle handle2; |
| 4405 | TestCompletionCallback callback2; |
| 4406 | EXPECT_EQ( |
| 4407 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4408 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4409 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4410 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4411 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4412 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4413 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4414 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4415 | TestGroupId("a"))); |
| 4416 | EXPECT_EQ(0u, |
| 4417 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4418 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4419 | |
| 4420 | // The second request doesn't get a job because we are the limit. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4421 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4422 | &handle1)); |
| 4423 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4424 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4425 | |
| 4426 | // The second request should get a job upon cancelling the first request. |
| 4427 | handle1.Reset(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4428 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4429 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4430 | TestGroupId("a"))); |
| 4431 | EXPECT_EQ(0u, |
| 4432 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4433 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4434 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4435 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4436 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4437 | } |
| 4438 | |
| 4439 | TEST_F(ClientSocketPoolBaseTest, JobCompletionReassignsJob) { |
| 4440 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4441 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4442 | |
| 4443 | ClientSocketHandle handle1; |
| 4444 | TestCompletionCallback callback1; |
| 4445 | EXPECT_EQ( |
| 4446 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4447 | handle1.Init(TestGroupId("a"), params_, HIGHEST, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4448 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4449 | callback1.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(1u, 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 handle2; |
| 4460 | TestCompletionCallback callback2; |
| 4461 | EXPECT_EQ( |
| 4462 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4463 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4464 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4465 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4466 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4467 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4468 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4469 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4470 | TestGroupId("a"))); |
| 4471 | EXPECT_EQ(0u, |
| 4472 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4473 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4474 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4475 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4476 | &handle1)); |
| 4477 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4478 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4479 | |
| 4480 | // The lower-priority job completes first. The higher-priority request should |
| 4481 | // get the socket, and the lower-priority request should get the remaining |
| 4482 | // job. |
| 4483 | client_socket_factory_.SignalJob(1); |
| 4484 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4485 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4486 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4487 | TestGroupId("a"))); |
| 4488 | EXPECT_EQ(0u, |
| 4489 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4490 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4491 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4492 | EXPECT_TRUE(handle1.socket()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4493 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4494 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4495 | } |
| 4496 | |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 4497 | class MockLayeredPool : public HigherLayeredPool { |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4498 | public: |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4499 | MockLayeredPool(TransportClientSocketPool* pool, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4500 | const ClientSocketPool::GroupId& group_id) |
| 4501 | : pool_(pool), group_id_(group_id), can_release_connection_(true) { |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 4502 | pool_->AddHigherLayeredPool(this); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4503 | } |
| 4504 | |
Daniel Cheng | 4496d082 | 2018-04-26 21:52:15 | [diff] [blame] | 4505 | ~MockLayeredPool() override { pool_->RemoveHigherLayeredPool(this); } |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4506 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4507 | int RequestSocket(TransportClientSocketPool* pool) { |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4508 | return handle_.Init( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4509 | group_id_, CreateDummyParams(), DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4510 | ClientSocketPool::RespectLimits::ENABLED, callback_.callback(), |
| 4511 | ClientSocketPool::ProxyAuthCallback(), pool, NetLogWithSource()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4512 | } |
| 4513 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4514 | int RequestSocketWithoutLimits(TransportClientSocketPool* pool) { |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4515 | return handle_.Init( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4516 | group_id_, CreateDummyParams(), MAXIMUM_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4517 | ClientSocketPool::RespectLimits::DISABLED, callback_.callback(), |
| 4518 | ClientSocketPool::ProxyAuthCallback(), pool, NetLogWithSource()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4519 | } |
| 4520 | |
| 4521 | bool ReleaseOneConnection() { |
| 4522 | if (!handle_.is_initialized() || !can_release_connection_) { |
| 4523 | return false; |
| 4524 | } |
| 4525 | handle_.socket()->Disconnect(); |
| 4526 | handle_.Reset(); |
| 4527 | return true; |
| 4528 | } |
| 4529 | |
| 4530 | void set_can_release_connection(bool can_release_connection) { |
| 4531 | can_release_connection_ = can_release_connection; |
| 4532 | } |
| 4533 | |
| 4534 | MOCK_METHOD0(CloseOneIdleConnection, bool()); |
| 4535 | |
| 4536 | private: |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4537 | TransportClientSocketPool* const pool_; |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4538 | ClientSocketHandle handle_; |
| 4539 | TestCompletionCallback callback_; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4540 | const ClientSocketPool::GroupId group_id_; |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4541 | bool can_release_connection_; |
| 4542 | }; |
| 4543 | |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4544 | // Tests the basic case of closing an idle socket in a higher layered pool when |
| 4545 | // a new request is issued and the lower layer pool is stalled. |
| 4546 | TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketsHeldByLayeredPoolWhenNeeded) { |
| 4547 | CreatePool(1, 1); |
| 4548 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 4549 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4550 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("foo")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4551 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4552 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4553 | .WillOnce(Invoke(&mock_layered_pool, |
| 4554 | &MockLayeredPool::ReleaseOneConnection)); |
| 4555 | ClientSocketHandle handle; |
| 4556 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4557 | EXPECT_EQ( |
| 4558 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4559 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4560 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 4561 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4562 | NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4563 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4564 | } |
| 4565 | |
Matt Menke | 83367864 | 2019-03-05 22:05:51 | [diff] [blame] | 4566 | // Tests the case that trying to close an idle socket in a higher layered pool |
| 4567 | // fails. |
| 4568 | TEST_F(ClientSocketPoolBaseTest, |
| 4569 | CloseIdleSocketsHeldByLayeredPoolWhenNeededFails) { |
| 4570 | CreatePool(1, 1); |
| 4571 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 4572 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4573 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("foo")); |
Matt Menke | 83367864 | 2019-03-05 22:05:51 | [diff] [blame] | 4574 | mock_layered_pool.set_can_release_connection(false); |
| 4575 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
| 4576 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4577 | .WillOnce(Invoke(&mock_layered_pool, |
| 4578 | &MockLayeredPool::ReleaseOneConnection)); |
| 4579 | ClientSocketHandle handle; |
| 4580 | TestCompletionCallback callback; |
| 4581 | EXPECT_EQ( |
| 4582 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4583 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 83367864 | 2019-03-05 22:05:51 | [diff] [blame] | 4584 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 4585 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4586 | NetLogWithSource())); |
| 4587 | base::RunLoop().RunUntilIdle(); |
| 4588 | EXPECT_FALSE(callback.have_result()); |
| 4589 | } |
| 4590 | |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4591 | // Same as above, but the idle socket is in the same group as the stalled |
| 4592 | // socket, and closes the only other request in its group when closing requests |
| 4593 | // in higher layered pools. This generally shouldn't happen, but it may be |
| 4594 | // possible if a higher level pool issues a request and the request is |
| 4595 | // subsequently cancelled. Even if it's not possible, best not to crash. |
| 4596 | TEST_F(ClientSocketPoolBaseTest, |
| 4597 | CloseIdleSocketsHeldByLayeredPoolWhenNeededSameGroup) { |
| 4598 | CreatePool(2, 2); |
| 4599 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 4600 | |
| 4601 | // Need a socket in another group for the pool to be stalled (If a group |
| 4602 | // has the maximum number of connections already, it's not stalled). |
| 4603 | ClientSocketHandle handle1; |
| 4604 | TestCompletionCallback callback1; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4605 | EXPECT_EQ(OK, handle1.Init( |
| 4606 | TestGroupId("group1"), params_, DEFAULT_PRIORITY, |
| 4607 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4608 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4609 | pool_.get(), NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4610 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4611 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("group2")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4612 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4613 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4614 | .WillOnce(Invoke(&mock_layered_pool, |
| 4615 | &MockLayeredPool::ReleaseOneConnection)); |
| 4616 | ClientSocketHandle handle; |
| 4617 | TestCompletionCallback callback2; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4618 | EXPECT_EQ( |
| 4619 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4620 | handle.Init(TestGroupId("group2"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4621 | ClientSocketPool::RespectLimits::ENABLED, |
| 4622 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4623 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4624 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4625 | } |
| 4626 | |
| 4627 | // Tests the case when an idle socket can be closed when a new request is |
| 4628 | // issued, and the new request belongs to a group that was previously stalled. |
| 4629 | TEST_F(ClientSocketPoolBaseTest, |
| 4630 | CloseIdleSocketsHeldByLayeredPoolInSameGroupWhenNeeded) { |
| 4631 | CreatePool(2, 2); |
| 4632 | std::list<TestConnectJob::JobType> job_types; |
| 4633 | job_types.push_back(TestConnectJob::kMockJob); |
| 4634 | job_types.push_back(TestConnectJob::kMockJob); |
| 4635 | job_types.push_back(TestConnectJob::kMockJob); |
| 4636 | job_types.push_back(TestConnectJob::kMockJob); |
| 4637 | connect_job_factory_->set_job_types(&job_types); |
| 4638 | |
| 4639 | ClientSocketHandle handle1; |
| 4640 | TestCompletionCallback callback1; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4641 | EXPECT_EQ(OK, handle1.Init( |
| 4642 | TestGroupId("group1"), params_, DEFAULT_PRIORITY, |
| 4643 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4644 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4645 | pool_.get(), NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4646 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4647 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("group2")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4648 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4649 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4650 | .WillRepeatedly(Invoke(&mock_layered_pool, |
| 4651 | &MockLayeredPool::ReleaseOneConnection)); |
| 4652 | mock_layered_pool.set_can_release_connection(false); |
| 4653 | |
| 4654 | // The third request is made when the socket pool is in a stalled state. |
| 4655 | ClientSocketHandle handle3; |
| 4656 | TestCompletionCallback callback3; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4657 | EXPECT_EQ( |
| 4658 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4659 | handle3.Init(TestGroupId("group3"), params_, DEFAULT_PRIORITY, |
| 4660 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4661 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4662 | pool_.get(), NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4663 | |
| 4664 | base::RunLoop().RunUntilIdle(); |
| 4665 | EXPECT_FALSE(callback3.have_result()); |
| 4666 | |
| 4667 | // The fourth request is made when the pool is no longer stalled. The third |
| 4668 | // request should be serviced first, since it was issued first and has the |
| 4669 | // same priority. |
| 4670 | mock_layered_pool.set_can_release_connection(true); |
| 4671 | ClientSocketHandle handle4; |
| 4672 | TestCompletionCallback callback4; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4673 | EXPECT_EQ( |
| 4674 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4675 | handle4.Init(TestGroupId("group3"), params_, DEFAULT_PRIORITY, |
| 4676 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4677 | callback4.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4678 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4679 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4680 | EXPECT_FALSE(callback4.have_result()); |
| 4681 | |
| 4682 | // Closing a handle should free up another socket slot. |
| 4683 | handle1.Reset(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4684 | EXPECT_THAT(callback4.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4685 | } |
| 4686 | |
| 4687 | // Tests the case when an idle socket can be closed when a new request is |
| 4688 | // issued, and the new request belongs to a group that was previously stalled. |
| 4689 | // |
| 4690 | // The two differences from the above test are that the stalled requests are not |
| 4691 | // in the same group as the layered pool's request, and the the fourth request |
| 4692 | // has a higher priority than the third one, so gets a socket first. |
| 4693 | TEST_F(ClientSocketPoolBaseTest, |
| 4694 | CloseIdleSocketsHeldByLayeredPoolInSameGroupWhenNeeded2) { |
| 4695 | CreatePool(2, 2); |
| 4696 | std::list<TestConnectJob::JobType> job_types; |
| 4697 | job_types.push_back(TestConnectJob::kMockJob); |
| 4698 | job_types.push_back(TestConnectJob::kMockJob); |
| 4699 | job_types.push_back(TestConnectJob::kMockJob); |
| 4700 | job_types.push_back(TestConnectJob::kMockJob); |
| 4701 | connect_job_factory_->set_job_types(&job_types); |
| 4702 | |
| 4703 | ClientSocketHandle handle1; |
| 4704 | TestCompletionCallback callback1; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4705 | EXPECT_EQ(OK, handle1.Init( |
| 4706 | TestGroupId("group1"), params_, DEFAULT_PRIORITY, |
| 4707 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4708 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4709 | pool_.get(), NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4710 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4711 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("group2")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4712 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4713 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4714 | .WillRepeatedly(Invoke(&mock_layered_pool, |
| 4715 | &MockLayeredPool::ReleaseOneConnection)); |
| 4716 | mock_layered_pool.set_can_release_connection(false); |
| 4717 | |
| 4718 | // The third request is made when the socket pool is in a stalled state. |
| 4719 | ClientSocketHandle handle3; |
| 4720 | TestCompletionCallback callback3; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4721 | EXPECT_EQ( |
| 4722 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4723 | handle3.Init(TestGroupId("group3"), params_, MEDIUM, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4724 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4725 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4726 | pool_.get(), NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4727 | |
| 4728 | base::RunLoop().RunUntilIdle(); |
| 4729 | EXPECT_FALSE(callback3.have_result()); |
| 4730 | |
| 4731 | // The fourth request is made when the pool is no longer stalled. This |
| 4732 | // request has a higher priority than the third request, so is serviced first. |
| 4733 | mock_layered_pool.set_can_release_connection(true); |
| 4734 | ClientSocketHandle handle4; |
| 4735 | TestCompletionCallback callback4; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4736 | EXPECT_EQ( |
| 4737 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4738 | handle4.Init(TestGroupId("group3"), params_, HIGHEST, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4739 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4740 | callback4.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4741 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4742 | EXPECT_THAT(callback4.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4743 | EXPECT_FALSE(callback3.have_result()); |
| 4744 | |
| 4745 | // Closing a handle should free up another socket slot. |
| 4746 | handle1.Reset(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4747 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4748 | } |
| 4749 | |
| 4750 | TEST_F(ClientSocketPoolBaseTest, |
| 4751 | CloseMultipleIdleSocketsHeldByLayeredPoolWhenNeeded) { |
| 4752 | CreatePool(1, 1); |
| 4753 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 4754 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4755 | MockLayeredPool mock_layered_pool1(pool_.get(), TestGroupId("foo")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4756 | EXPECT_THAT(mock_layered_pool1.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4757 | EXPECT_CALL(mock_layered_pool1, CloseOneIdleConnection()) |
| 4758 | .WillRepeatedly(Invoke(&mock_layered_pool1, |
| 4759 | &MockLayeredPool::ReleaseOneConnection)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4760 | MockLayeredPool mock_layered_pool2(pool_.get(), TestGroupId("bar")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4761 | EXPECT_THAT(mock_layered_pool2.RequestSocketWithoutLimits(pool_.get()), |
| 4762 | IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4763 | EXPECT_CALL(mock_layered_pool2, CloseOneIdleConnection()) |
| 4764 | .WillRepeatedly(Invoke(&mock_layered_pool2, |
| 4765 | &MockLayeredPool::ReleaseOneConnection)); |
| 4766 | ClientSocketHandle handle; |
| 4767 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4768 | EXPECT_EQ( |
| 4769 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4770 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4771 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 4772 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4773 | NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4774 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4775 | } |
| 4776 | |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4777 | // Test that when a socket pool and group are at their limits, a request |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4778 | // with RespectLimits::DISABLED triggers creation of a new socket, and gets the |
| 4779 | // socket instead of a request with the same priority that was issued earlier, |
| 4780 | // but has RespectLimits::ENABLED. |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4781 | TEST_F(ClientSocketPoolBaseTest, IgnoreLimits) { |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4782 | CreatePool(1, 1); |
| 4783 | |
| 4784 | // Issue a request to reach the socket pool limit. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4785 | EXPECT_EQ(OK, StartRequestWithIgnoreLimits( |
| 4786 | TestGroupId("a"), MAXIMUM_PRIORITY, |
| 4787 | ClientSocketPool::RespectLimits::ENABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4788 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4789 | |
| 4790 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 4791 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4792 | EXPECT_EQ(ERR_IO_PENDING, StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4793 | TestGroupId("a"), MAXIMUM_PRIORITY, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4794 | ClientSocketPool::RespectLimits::ENABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4795 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4796 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4797 | // Issue a request that ignores the limits, so a new ConnectJob is |
| 4798 | // created. |
| 4799 | EXPECT_EQ(ERR_IO_PENDING, StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4800 | TestGroupId("a"), MAXIMUM_PRIORITY, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4801 | ClientSocketPool::RespectLimits::DISABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4802 | ASSERT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4803 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4804 | EXPECT_THAT(request(2)->WaitForResult(), IsOk()); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4805 | EXPECT_FALSE(request(1)->have_result()); |
| 4806 | } |
| 4807 | |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 4808 | // Test that when a socket pool and group are at their limits, a ConnectJob |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4809 | // issued for a request with RespectLimits::DISABLED is not cancelled when a |
| 4810 | // request with RespectLimits::ENABLED issued to the same group is cancelled. |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 4811 | TEST_F(ClientSocketPoolBaseTest, IgnoreLimitsCancelOtherJob) { |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 4812 | CreatePool(1, 1); |
| 4813 | |
| 4814 | // Issue a request to reach the socket pool limit. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4815 | EXPECT_EQ(OK, StartRequestWithIgnoreLimits( |
| 4816 | TestGroupId("a"), MAXIMUM_PRIORITY, |
| 4817 | ClientSocketPool::RespectLimits::ENABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4818 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 4819 | |
| 4820 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 4821 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4822 | EXPECT_EQ(ERR_IO_PENDING, StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4823 | TestGroupId("a"), MAXIMUM_PRIORITY, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4824 | ClientSocketPool::RespectLimits::ENABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4825 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 4826 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4827 | // Issue a request with RespectLimits::DISABLED, so a new ConnectJob is |
| 4828 | // created. |
| 4829 | EXPECT_EQ(ERR_IO_PENDING, StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4830 | TestGroupId("a"), MAXIMUM_PRIORITY, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4831 | ClientSocketPool::RespectLimits::DISABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4832 | ASSERT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4833 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4834 | // Cancel the pending request with RespectLimits::ENABLED. The ConnectJob |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4835 | // should not be cancelled. |
| 4836 | request(1)->handle()->Reset(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4837 | ASSERT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4838 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4839 | EXPECT_THAT(request(2)->WaitForResult(), IsOk()); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4840 | EXPECT_FALSE(request(1)->have_result()); |
| 4841 | } |
| 4842 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4843 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthNoAuthCallback) { |
| 4844 | CreatePool(1, 1); |
| 4845 | |
| 4846 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 4847 | |
| 4848 | ClientSocketHandle handle; |
| 4849 | TestCompletionCallback callback; |
| 4850 | EXPECT_EQ( |
| 4851 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4852 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4853 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 4854 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4855 | NetLogWithSource())); |
| 4856 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4857 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4858 | |
| 4859 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_PROXY_AUTH_REQUESTED)); |
| 4860 | EXPECT_FALSE(handle.is_initialized()); |
| 4861 | EXPECT_FALSE(handle.socket()); |
| 4862 | |
| 4863 | // The group should now be empty, and thus be deleted. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4864 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4865 | } |
| 4866 | |
| 4867 | class TestAuthHelper { |
| 4868 | public: |
| 4869 | TestAuthHelper() = default; |
| 4870 | ~TestAuthHelper() = default; |
| 4871 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4872 | void InitHandle( |
Matt Menke | 84d11e56 | 2019-03-27 00:11:19 | [diff] [blame] | 4873 | scoped_refptr<ClientSocketPool::SocketParams> params, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4874 | TransportClientSocketPool* pool, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4875 | RequestPriority priority = DEFAULT_PRIORITY, |
| 4876 | ClientSocketPool::RespectLimits respect_limits = |
| 4877 | ClientSocketPool::RespectLimits::ENABLED, |
| 4878 | const ClientSocketPool::GroupId& group_id_in = TestGroupId("a")) { |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4879 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4880 | handle_.Init(group_id_in, params, priority, SocketTag(), |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4881 | respect_limits, callback_.callback(), |
| 4882 | base::BindRepeating(&TestAuthHelper::AuthCallback, |
| 4883 | base::Unretained(this)), |
| 4884 | pool, NetLogWithSource())); |
| 4885 | } |
| 4886 | |
| 4887 | void WaitForAuth() { |
| 4888 | run_loop_ = std::make_unique<base::RunLoop>(); |
| 4889 | run_loop_->Run(); |
| 4890 | run_loop_.reset(); |
| 4891 | } |
| 4892 | |
| 4893 | void WaitForAuthAndRestartSync() { |
| 4894 | restart_sync_ = true; |
| 4895 | WaitForAuth(); |
| 4896 | restart_sync_ = false; |
| 4897 | } |
| 4898 | |
| 4899 | void WaitForAuthAndResetHandleSync() { |
| 4900 | reset_handle_sync_ = true; |
| 4901 | WaitForAuth(); |
| 4902 | reset_handle_sync_ = false; |
| 4903 | } |
| 4904 | |
| 4905 | void RestartWithAuth() { |
| 4906 | DCHECK(restart_with_auth_callback_); |
| 4907 | std::move(restart_with_auth_callback_).Run(); |
| 4908 | } |
| 4909 | |
| 4910 | int WaitForResult() { |
| 4911 | int result = callback_.WaitForResult(); |
| 4912 | // There shouldn't be any callback waiting to be invoked once the request is |
| 4913 | // complete. |
| 4914 | EXPECT_FALSE(restart_with_auth_callback_); |
| 4915 | // The socket should only be initialized on success. |
| 4916 | EXPECT_EQ(result == OK, handle_.is_initialized()); |
| 4917 | EXPECT_EQ(result == OK, handle_.socket() != nullptr); |
| 4918 | return result; |
| 4919 | } |
| 4920 | |
| 4921 | ClientSocketHandle* handle() { return &handle_; } |
| 4922 | int auth_count() const { return auth_count_; } |
| 4923 | int have_result() const { return callback_.have_result(); } |
| 4924 | |
| 4925 | private: |
| 4926 | void AuthCallback(const HttpResponseInfo& response, |
| 4927 | HttpAuthController* auth_controller, |
| 4928 | base::OnceClosure restart_with_auth_callback) { |
| 4929 | EXPECT_FALSE(restart_with_auth_callback_); |
| 4930 | EXPECT_TRUE(restart_with_auth_callback); |
| 4931 | |
| 4932 | // Once there's a result, this method shouldn't be invoked again. |
| 4933 | EXPECT_FALSE(callback_.have_result()); |
| 4934 | |
| 4935 | ++auth_count_; |
| 4936 | run_loop_->Quit(); |
| 4937 | if (restart_sync_) { |
| 4938 | std::move(restart_with_auth_callback).Run(); |
| 4939 | return; |
| 4940 | } |
| 4941 | |
| 4942 | restart_with_auth_callback_ = std::move(restart_with_auth_callback); |
| 4943 | |
| 4944 | if (reset_handle_sync_) { |
| 4945 | handle_.Reset(); |
| 4946 | return; |
| 4947 | } |
| 4948 | } |
| 4949 | |
| 4950 | std::unique_ptr<base::RunLoop> run_loop_; |
| 4951 | base::OnceClosure restart_with_auth_callback_; |
| 4952 | |
| 4953 | bool restart_sync_ = false; |
| 4954 | bool reset_handle_sync_ = false; |
| 4955 | |
| 4956 | ClientSocketHandle handle_; |
| 4957 | int auth_count_ = 0; |
| 4958 | TestCompletionCallback callback_; |
| 4959 | |
| 4960 | DISALLOW_COPY_AND_ASSIGN(TestAuthHelper); |
| 4961 | }; |
| 4962 | |
| 4963 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnce) { |
| 4964 | CreatePool(1, 1); |
| 4965 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 4966 | |
| 4967 | TestAuthHelper auth_helper; |
| 4968 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4969 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 4970 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4971 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4972 | |
| 4973 | auth_helper.WaitForAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4974 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 4975 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4976 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4977 | |
| 4978 | auth_helper.RestartWithAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4979 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 4980 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4981 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4982 | |
| 4983 | EXPECT_THAT(auth_helper.WaitForResult(), IsOk()); |
| 4984 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4985 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4986 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4987 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4988 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 4989 | } |
| 4990 | |
| 4991 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceSync) { |
| 4992 | CreatePool(1, 1); |
| 4993 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 4994 | |
| 4995 | TestAuthHelper auth_helper; |
| 4996 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4997 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 4998 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4999 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5000 | |
| 5001 | auth_helper.WaitForAuthAndRestartSync(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5002 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5003 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5004 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5005 | |
| 5006 | EXPECT_THAT(auth_helper.WaitForResult(), IsOk()); |
| 5007 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5008 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5009 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5010 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5011 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5012 | } |
| 5013 | |
| 5014 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceFails) { |
| 5015 | CreatePool(1, 1); |
| 5016 | connect_job_factory_->set_job_type( |
| 5017 | TestConnectJob::kMockAuthChallengeOnceFailingJob); |
| 5018 | |
| 5019 | TestAuthHelper auth_helper; |
| 5020 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5021 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5022 | |
| 5023 | auth_helper.WaitForAuth(); |
| 5024 | auth_helper.RestartWithAuth(); |
| 5025 | EXPECT_THAT(auth_helper.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 5026 | |
| 5027 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5028 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5029 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5030 | } |
| 5031 | |
| 5032 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceSyncFails) { |
| 5033 | CreatePool(1, 1); |
| 5034 | connect_job_factory_->set_job_type( |
| 5035 | TestConnectJob::kMockAuthChallengeOnceFailingJob); |
| 5036 | |
| 5037 | TestAuthHelper auth_helper; |
| 5038 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5039 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5040 | |
| 5041 | auth_helper.WaitForAuthAndRestartSync(); |
| 5042 | EXPECT_THAT(auth_helper.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 5043 | |
| 5044 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5045 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5046 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5047 | } |
| 5048 | |
| 5049 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceDeleteHandle) { |
| 5050 | CreatePool(1, 1); |
| 5051 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5052 | |
| 5053 | TestAuthHelper auth_helper; |
| 5054 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5055 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5056 | |
| 5057 | auth_helper.WaitForAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5058 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5059 | |
| 5060 | auth_helper.handle()->Reset(); |
| 5061 | |
| 5062 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5063 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5064 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5065 | EXPECT_FALSE(auth_helper.handle()->is_initialized()); |
| 5066 | EXPECT_FALSE(auth_helper.handle()->socket()); |
| 5067 | } |
| 5068 | |
| 5069 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceDeleteHandleSync) { |
| 5070 | CreatePool(1, 1); |
| 5071 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5072 | |
| 5073 | TestAuthHelper auth_helper; |
| 5074 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5075 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5076 | |
| 5077 | auth_helper.WaitForAuthAndResetHandleSync(); |
| 5078 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5079 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5080 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5081 | EXPECT_FALSE(auth_helper.handle()->is_initialized()); |
| 5082 | EXPECT_FALSE(auth_helper.handle()->socket()); |
| 5083 | } |
| 5084 | |
| 5085 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceFlushWithError) { |
| 5086 | CreatePool(1, 1); |
| 5087 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5088 | |
| 5089 | TestAuthHelper auth_helper; |
| 5090 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5091 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5092 | |
| 5093 | auth_helper.WaitForAuth(); |
| 5094 | |
| 5095 | pool_->FlushWithError(ERR_FAILED); |
| 5096 | base::RunLoop().RunUntilIdle(); |
| 5097 | |
| 5098 | // When flushing the socket pool, bound sockets should delay returning the |
| 5099 | // error until completion. |
| 5100 | EXPECT_FALSE(auth_helper.have_result()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5101 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5102 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5103 | |
| 5104 | auth_helper.RestartWithAuth(); |
| 5105 | // The callback should be called asynchronously. |
| 5106 | EXPECT_FALSE(auth_helper.have_result()); |
| 5107 | |
| 5108 | EXPECT_THAT(auth_helper.WaitForResult(), IsError(ERR_FAILED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5109 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5110 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5111 | } |
| 5112 | |
| 5113 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthTwice) { |
| 5114 | CreatePool(1, 1); |
| 5115 | connect_job_factory_->set_job_type( |
| 5116 | TestConnectJob::kMockAuthChallengeTwiceJob); |
| 5117 | |
| 5118 | TestAuthHelper auth_helper; |
| 5119 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5120 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5121 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5122 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5123 | |
| 5124 | auth_helper.WaitForAuth(); |
| 5125 | auth_helper.RestartWithAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5126 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5127 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5128 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5129 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5130 | |
| 5131 | auth_helper.WaitForAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5132 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5133 | EXPECT_EQ(2, auth_helper.auth_count()); |
| 5134 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5135 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5136 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5137 | auth_helper.RestartWithAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5138 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5139 | EXPECT_EQ(2, auth_helper.auth_count()); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5140 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5141 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5142 | |
| 5143 | EXPECT_THAT(auth_helper.WaitForResult(), IsOk()); |
| 5144 | EXPECT_EQ(2, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5145 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5146 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5147 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5148 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5149 | } |
| 5150 | |
| 5151 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthTwiceFails) { |
| 5152 | CreatePool(1, 1); |
| 5153 | connect_job_factory_->set_job_type( |
| 5154 | TestConnectJob::kMockAuthChallengeTwiceFailingJob); |
| 5155 | |
| 5156 | TestAuthHelper auth_helper; |
| 5157 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5158 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5159 | |
| 5160 | auth_helper.WaitForAuth(); |
| 5161 | auth_helper.RestartWithAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5162 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5163 | EXPECT_EQ(1, auth_helper.auth_count()); |
| 5164 | |
| 5165 | auth_helper.WaitForAuth(); |
| 5166 | auth_helper.RestartWithAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5167 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5168 | EXPECT_EQ(2, auth_helper.auth_count()); |
| 5169 | |
| 5170 | EXPECT_THAT(auth_helper.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 5171 | EXPECT_EQ(2, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5172 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5173 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5174 | } |
| 5175 | |
| 5176 | // Makes sure that when a bound request is destroyed, a new ConnectJob is |
| 5177 | // created, if needed. |
| 5178 | TEST_F(ClientSocketPoolBaseTest, |
| 5179 | ProxyAuthCreateNewConnectJobOnDestroyBoundRequest) { |
| 5180 | CreatePool(1 /* max_sockets */, 1 /* max_sockets_per_group */); |
| 5181 | connect_job_factory_->set_job_type( |
| 5182 | TestConnectJob::kMockAuthChallengeOnceFailingJob); |
| 5183 | |
| 5184 | // First request creates a ConnectJob. |
| 5185 | TestAuthHelper auth_helper1; |
| 5186 | auth_helper1.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5187 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5188 | |
| 5189 | // A second request come in, but no new ConnectJob is needed, since the limit |
| 5190 | // has been reached. |
| 5191 | TestAuthHelper auth_helper2; |
| 5192 | auth_helper2.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5193 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5194 | |
| 5195 | // Run until the auth callback for the first request is invoked. |
| 5196 | auth_helper1.WaitForAuth(); |
| 5197 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5198 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5199 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5200 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5201 | |
| 5202 | // Make connect jobs succeed, then cancel the first request, which should |
| 5203 | // destroy the bound ConnectJob, and cause a new ConnectJob to start. |
| 5204 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 5205 | auth_helper1.handle()->Reset(); |
| 5206 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5207 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5208 | |
| 5209 | // The second ConnectJob should succeed. |
| 5210 | EXPECT_THAT(auth_helper2.WaitForResult(), IsOk()); |
| 5211 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5212 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5213 | } |
| 5214 | |
| 5215 | // Makes sure that when a bound request is destroyed, a new ConnectJob is |
| 5216 | // created for another group, if needed. |
| 5217 | TEST_F(ClientSocketPoolBaseTest, |
| 5218 | ProxyAuthCreateNewConnectJobOnDestroyBoundRequestDifferentGroups) { |
| 5219 | CreatePool(1 /* max_sockets */, 1 /* max_sockets_per_group */); |
| 5220 | connect_job_factory_->set_job_type( |
| 5221 | TestConnectJob::kMockAuthChallengeOnceFailingJob); |
| 5222 | |
| 5223 | // First request creates a ConnectJob. |
| 5224 | TestAuthHelper auth_helper1; |
| 5225 | auth_helper1.InitHandle(params_, pool_.get(), DEFAULT_PRIORITY); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5226 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5227 | |
| 5228 | // A second request come in, but no new ConnectJob is needed, since the limit |
| 5229 | // has been reached. |
| 5230 | TestAuthHelper auth_helper2; |
| 5231 | auth_helper2.InitHandle(params_, pool_.get(), DEFAULT_PRIORITY, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5232 | ClientSocketPool::RespectLimits::ENABLED, |
| 5233 | TestGroupId("b")); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5234 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5235 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5236 | |
| 5237 | // Run until the auth callback for the first request is invoked. |
| 5238 | auth_helper1.WaitForAuth(); |
| 5239 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5240 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5241 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5242 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5243 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 5244 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5245 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5246 | |
| 5247 | // Make connect jobs succeed, then cancel the first request, which should |
| 5248 | // destroy the bound ConnectJob, and cause a new ConnectJob to start for the |
| 5249 | // other group. |
| 5250 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 5251 | auth_helper1.handle()->Reset(); |
| 5252 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5253 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 5254 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5255 | |
| 5256 | // The second ConnectJob should succeed. |
| 5257 | EXPECT_THAT(auth_helper2.WaitForResult(), IsOk()); |
| 5258 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5259 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 5260 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5261 | } |
| 5262 | |
| 5263 | // Test that once an auth challenge is bound, that's the request that gets all |
| 5264 | // subsequent calls and the socket itself. |
| 5265 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthStaysBound) { |
| 5266 | CreatePool(1, 1); |
| 5267 | connect_job_factory_->set_job_type( |
| 5268 | TestConnectJob::kMockAuthChallengeTwiceJob); |
| 5269 | |
| 5270 | // First request creates a ConnectJob. |
| 5271 | TestAuthHelper auth_helper1; |
| 5272 | auth_helper1.InitHandle(params_, pool_.get(), LOWEST); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5273 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5274 | |
| 5275 | // A second, higher priority request is made. |
| 5276 | TestAuthHelper auth_helper2; |
| 5277 | auth_helper2.InitHandle(params_, pool_.get(), LOW); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5278 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5279 | |
| 5280 | // Run until the auth callback for the second request is invoked. |
| 5281 | auth_helper2.WaitForAuth(); |
| 5282 | EXPECT_EQ(0, auth_helper1.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5283 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5284 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5285 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5286 | |
| 5287 | // Start a higher priority job. It shouldn't be able to steal |auth_helper2|'s |
| 5288 | // ConnectJob. |
| 5289 | TestAuthHelper auth_helper3; |
| 5290 | auth_helper3.InitHandle(params_, pool_.get(), HIGHEST); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5291 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5292 | |
| 5293 | // Start a higher job that ignores limits, creating a hanging socket. It |
| 5294 | // shouldn't be able to steal |auth_helper2|'s ConnectJob. |
| 5295 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 5296 | TestAuthHelper auth_helper4; |
| 5297 | auth_helper4.InitHandle(params_, pool_.get(), HIGHEST, |
| 5298 | ClientSocketPool::RespectLimits::DISABLED); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5299 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5300 | |
| 5301 | // Restart with auth, and |auth_helper2|'s auth method should be invoked |
| 5302 | // again. |
| 5303 | auth_helper2.RestartWithAuth(); |
| 5304 | auth_helper2.WaitForAuth(); |
| 5305 | EXPECT_EQ(0, auth_helper1.auth_count()); |
| 5306 | EXPECT_FALSE(auth_helper1.have_result()); |
| 5307 | EXPECT_EQ(2, auth_helper2.auth_count()); |
| 5308 | EXPECT_FALSE(auth_helper2.have_result()); |
| 5309 | EXPECT_EQ(0, auth_helper3.auth_count()); |
| 5310 | EXPECT_FALSE(auth_helper3.have_result()); |
| 5311 | EXPECT_EQ(0, auth_helper4.auth_count()); |
| 5312 | EXPECT_FALSE(auth_helper4.have_result()); |
| 5313 | |
| 5314 | // Advance auth again, and |auth_helper2| should get the socket. |
| 5315 | auth_helper2.RestartWithAuth(); |
| 5316 | EXPECT_THAT(auth_helper2.WaitForResult(), IsOk()); |
| 5317 | // The hung ConnectJob for the RespectLimits::DISABLED request is still in the |
| 5318 | // socket pool. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5319 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5320 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5321 | EXPECT_EQ(0, auth_helper1.auth_count()); |
| 5322 | EXPECT_FALSE(auth_helper1.have_result()); |
| 5323 | EXPECT_EQ(0, auth_helper3.auth_count()); |
| 5324 | EXPECT_FALSE(auth_helper3.have_result()); |
| 5325 | EXPECT_EQ(0, auth_helper4.auth_count()); |
| 5326 | EXPECT_FALSE(auth_helper4.have_result()); |
| 5327 | |
| 5328 | // If the socket is returned to the socket pool, the RespectLimits::DISABLED |
| 5329 | // socket request should be able to claim it. |
| 5330 | auth_helper2.handle()->Reset(); |
| 5331 | EXPECT_THAT(auth_helper4.WaitForResult(), IsOk()); |
| 5332 | EXPECT_EQ(0, auth_helper1.auth_count()); |
| 5333 | EXPECT_FALSE(auth_helper1.have_result()); |
| 5334 | EXPECT_EQ(0, auth_helper3.auth_count()); |
| 5335 | EXPECT_FALSE(auth_helper3.have_result()); |
| 5336 | EXPECT_EQ(0, auth_helper4.auth_count()); |
| 5337 | } |
| 5338 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 5339 | } // namespace |
| 5340 | |
| 5341 | } // namespace net |