[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" |
eroman | 87c53d6 | 2015-04-02 06:51:07 | [diff] [blame] | 33 | #include "net/log/net_log.h" |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 34 | #include "net/log/net_log_event_type.h" |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 35 | #include "net/log/net_log_source.h" |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 36 | #include "net/log/net_log_source_type.h" |
mmenke | 16a7cbdd | 2015-04-24 23:00:56 | [diff] [blame] | 37 | #include "net/log/test_net_log.h" |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 38 | #include "net/log/test_net_log_entry.h" |
| 39 | #include "net/log/test_net_log_util.h" |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 40 | #include "net/socket/client_socket_factory.h" |
| 41 | #include "net/socket/client_socket_handle.h" |
tfarina | 5dd13c2 | 2016-11-16 12:08:26 | [diff] [blame] | 42 | #include "net/socket/datagram_client_socket.h" |
tbansal | ca83c00 | 2016-04-28 20:56:28 | [diff] [blame] | 43 | #include "net/socket/socket_performance_watcher.h" |
Paul Jensen | 8d6f87ec | 2018-01-13 00:46:54 | [diff] [blame] | 44 | #include "net/socket/socket_tag.h" |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 45 | #include "net/socket/socket_test_util.h" |
[email protected] | 18ccfdb | 2013-08-15 00:13:44 | [diff] [blame] | 46 | #include "net/socket/ssl_client_socket.h" |
[email protected] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame] | 47 | #include "net/socket/stream_socket.h" |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 48 | #include "net/socket/transport_connect_job.h" |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 49 | #include "net/test/gtest_util.h" |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 50 | #include "net/test/test_with_scoped_task_environment.h" |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 51 | #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 52 | #include "testing/gmock/include/gmock/gmock.h" |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 53 | #include "testing/gtest/include/gtest/gtest.h" |
| 54 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 55 | using net::test::IsError; |
| 56 | using net::test::IsOk; |
| 57 | |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 58 | using ::testing::Invoke; |
| 59 | using ::testing::Return; |
| 60 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 61 | namespace net { |
| 62 | |
| 63 | namespace { |
| 64 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 65 | const int kDefaultMaxSockets = 4; |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 66 | const int kDefaultMaxSocketsPerGroup = 2; |
Tarun Bansal | a763509 | 2019-02-20 10:00:59 | [diff] [blame] | 67 | constexpr base::TimeDelta kUnusedIdleSocketTimeout = |
| 68 | base::TimeDelta::FromSeconds(10); |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 69 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 70 | ClientSocketPool::GroupId TestGroupId(const std::string& host, |
| 71 | int port = 80, |
| 72 | ClientSocketPool::SocketType socket_type = |
| 73 | ClientSocketPool::SocketType::kHttp, |
| 74 | bool privacy_mode = false) { |
| 75 | return ClientSocketPool::GroupId(HostPortPair(host, port), socket_type, |
| 76 | privacy_mode); |
| 77 | } |
| 78 | |
Matt Menke | 84d11e56 | 2019-03-27 00:11:19 | [diff] [blame] | 79 | // Returns a ClientSocketPool::SocketParams that will never be used to |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 80 | // create a real TreansportConnectJob. |
Matt Menke | 84d11e56 | 2019-03-27 00:11:19 | [diff] [blame] | 81 | scoped_refptr<ClientSocketPool::SocketParams> CreateDummyParams() { |
| 82 | return ClientSocketPool::SocketParams::CreateFromTransportSocketParams( |
| 83 | base::MakeRefCounted<TransportSocketParams>(HostPortPair("ignored", 80), |
| 84 | OnHostResolutionCallback())); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 85 | } |
| 86 | |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 87 | // Make sure |handle| sets load times correctly when it has been assigned a |
| 88 | // reused socket. |
| 89 | void TestLoadTimingInfoConnectedReused(const ClientSocketHandle& handle) { |
| 90 | LoadTimingInfo load_timing_info; |
| 91 | // Only pass true in as |is_reused|, as in general, HttpStream types should |
| 92 | // have stricter concepts of reuse than socket pools. |
| 93 | EXPECT_TRUE(handle.GetLoadTimingInfo(true, &load_timing_info)); |
| 94 | |
| 95 | EXPECT_EQ(true, load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 96 | EXPECT_NE(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 97 | |
[email protected] | b258e079 | 2013-01-12 07:11:59 | [diff] [blame] | 98 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 99 | ExpectLoadTimingHasOnlyConnectionTimes(load_timing_info); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | // Make sure |handle| sets load times correctly when it has been assigned a |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 103 | // fresh socket. Also runs TestLoadTimingInfoConnectedReused, since the owner |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 104 | // of a connection where |is_reused| is false may consider the connection |
| 105 | // reused. |
| 106 | void TestLoadTimingInfoConnectedNotReused(const ClientSocketHandle& handle) { |
| 107 | EXPECT_FALSE(handle.is_reused()); |
| 108 | |
| 109 | LoadTimingInfo load_timing_info; |
| 110 | EXPECT_TRUE(handle.GetLoadTimingInfo(false, &load_timing_info)); |
| 111 | |
| 112 | EXPECT_FALSE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 113 | EXPECT_NE(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 114 | |
[email protected] | b258e079 | 2013-01-12 07:11:59 | [diff] [blame] | 115 | ExpectConnectTimingHasTimes(load_timing_info.connect_timing, |
| 116 | CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY); |
| 117 | ExpectLoadTimingHasOnlyConnectionTimes(load_timing_info); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 118 | |
| 119 | TestLoadTimingInfoConnectedReused(handle); |
| 120 | } |
| 121 | |
| 122 | // Make sure |handle| sets load times correctly, in the case that it does not |
| 123 | // currently have a socket. |
| 124 | void TestLoadTimingInfoNotConnected(const ClientSocketHandle& handle) { |
| 125 | // Should only be set to true once a socket is assigned, if at all. |
| 126 | EXPECT_FALSE(handle.is_reused()); |
| 127 | |
| 128 | LoadTimingInfo load_timing_info; |
| 129 | EXPECT_FALSE(handle.GetLoadTimingInfo(false, &load_timing_info)); |
| 130 | |
| 131 | EXPECT_FALSE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 132 | EXPECT_EQ(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 133 | |
[email protected] | b258e079 | 2013-01-12 07:11:59 | [diff] [blame] | 134 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 135 | ExpectLoadTimingHasOnlyConnectionTimes(load_timing_info); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 136 | } |
| 137 | |
[email protected] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame] | 138 | class MockClientSocket : public StreamSocket { |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 139 | public: |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 140 | explicit MockClientSocket(net::NetLog* net_log) |
| 141 | : connected_(false), |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 142 | has_unread_data_(false), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 143 | net_log_(NetLogWithSource::Make(net_log, NetLogSourceType::SOCKET)), |
Charlie Harrison | 3e4c062 | 2018-05-13 15:44:30 | [diff] [blame] | 144 | was_used_to_convey_data_(false) {} |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 145 | |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 146 | // Sets whether the socket has unread data. If true, the next call to Read() |
| 147 | // will return 1 byte and IsConnectedAndIdle() will return false. |
| 148 | void set_has_unread_data(bool has_unread_data) { |
| 149 | has_unread_data_ = has_unread_data; |
| 150 | } |
| 151 | |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 152 | // Socket implementation. |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 153 | int Read(IOBuffer* /* buf */, |
| 154 | int len, |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 155 | CompletionOnceCallback /* callback */) override { |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 156 | if (has_unread_data_ && len > 0) { |
| 157 | has_unread_data_ = false; |
| 158 | was_used_to_convey_data_ = true; |
| 159 | return 1; |
| 160 | } |
[email protected] | e86df8dc | 2013-03-30 13:18:28 | [diff] [blame] | 161 | return ERR_UNEXPECTED; |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 162 | } |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 163 | |
[email protected] | a2b2cfc | 2017-12-06 09:06:08 | [diff] [blame] | 164 | int Write( |
| 165 | IOBuffer* /* buf */, |
| 166 | int len, |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 167 | CompletionOnceCallback /* callback */, |
[email protected] | a2b2cfc | 2017-12-06 09:06:08 | [diff] [blame] | 168 | const NetworkTrafficAnnotationTag& /*traffic_annotation*/) override { |
[email protected] | 0f873e8 | 2010-09-02 16:09:01 | [diff] [blame] | 169 | was_used_to_convey_data_ = true; |
| 170 | return len; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 171 | } |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 172 | int SetReceiveBufferSize(int32_t size) override { return OK; } |
| 173 | int SetSendBufferSize(int32_t size) override { return OK; } |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 174 | |
[email protected] | dbf036f | 2011-12-06 23:33:24 | [diff] [blame] | 175 | // StreamSocket implementation. |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 176 | int Connect(CompletionOnceCallback callback) override { |
[email protected] | dbf036f | 2011-12-06 23:33:24 | [diff] [blame] | 177 | connected_ = true; |
| 178 | return OK; |
| 179 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 180 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 181 | void Disconnect() override { connected_ = false; } |
| 182 | bool IsConnected() const override { return connected_; } |
| 183 | bool IsConnectedAndIdle() const override { |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 184 | return connected_ && !has_unread_data_; |
| 185 | } |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 186 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 187 | int GetPeerAddress(IPEndPoint* /* address */) const override { |
[email protected] | 9f864b3 | 2010-01-20 15:01:16 | [diff] [blame] | 188 | return ERR_UNEXPECTED; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 189 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 190 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 191 | int GetLocalAddress(IPEndPoint* /* address */) const override { |
[email protected] | e7f74da | 2011-04-19 23:49:35 | [diff] [blame] | 192 | return ERR_UNEXPECTED; |
| 193 | } |
| 194 | |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 195 | const NetLogWithSource& NetLog() const override { return net_log_; } |
[email protected] | a2006ece | 2010-04-23 16:44:02 | [diff] [blame] | 196 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 197 | bool WasEverUsed() const override { return was_used_to_convey_data_; } |
tfarina | 2846404c | 2016-12-25 14:31:37 | [diff] [blame] | 198 | bool WasAlpnNegotiated() const override { return false; } |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 199 | NextProto GetNegotiatedProtocol() const override { return kProtoUnknown; } |
| 200 | bool GetSSLInfo(SSLInfo* ssl_info) override { return false; } |
ttuttle | 23fdb7b | 2015-05-15 01:28:03 | [diff] [blame] | 201 | void GetConnectionAttempts(ConnectionAttempts* out) const override { |
| 202 | out->clear(); |
| 203 | } |
| 204 | void ClearConnectionAttempts() override {} |
| 205 | void AddConnectionAttempts(const ConnectionAttempts& attempts) override {} |
tbansal | f82cc8e | 2015-10-14 20:05:49 | [diff] [blame] | 206 | int64_t GetTotalReceivedBytes() const override { |
| 207 | NOTIMPLEMENTED(); |
| 208 | return 0; |
| 209 | } |
Paul Jensen | 0f49dec | 2017-12-12 23:39:58 | [diff] [blame] | 210 | void ApplySocketTag(const SocketTag& tag) override {} |
[email protected] | 9b5614a | 2010-08-25 20:29:45 | [diff] [blame] | 211 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 212 | private: |
| 213 | bool connected_; |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 214 | bool has_unread_data_; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 215 | NetLogWithSource net_log_; |
[email protected] | 0f873e8 | 2010-09-02 16:09:01 | [diff] [blame] | 216 | bool was_used_to_convey_data_; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 217 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 218 | DISALLOW_COPY_AND_ASSIGN(MockClientSocket); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 219 | }; |
| 220 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 221 | class TestConnectJob; |
| 222 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 223 | class MockClientSocketFactory : public ClientSocketFactory { |
| 224 | public: |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 225 | MockClientSocketFactory() : allocation_count_(0) {} |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 226 | |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 227 | std::unique_ptr<DatagramClientSocket> CreateDatagramClientSocket( |
[email protected] | 5370c01 | 2011-06-29 03:47:04 | [diff] [blame] | 228 | DatagramSocket::BindType bind_type, |
[email protected] | 98b0e58 | 2011-06-22 14:31:41 | [diff] [blame] | 229 | NetLog* net_log, |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 230 | const NetLogSource& source) override { |
[email protected] | 98b0e58 | 2011-06-22 14:31:41 | [diff] [blame] | 231 | NOTREACHED(); |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 232 | return std::unique_ptr<DatagramClientSocket>(); |
[email protected] | 98b0e58 | 2011-06-22 14:31:41 | [diff] [blame] | 233 | } |
| 234 | |
Helen Li | d5bb922 | 2018-04-12 15:33:09 | [diff] [blame] | 235 | std::unique_ptr<TransportClientSocket> CreateTransportClientSocket( |
[email protected] | 0a0b768 | 2010-08-25 17:08:07 | [diff] [blame] | 236 | const AddressList& addresses, |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 237 | std::unique_ptr< |
| 238 | SocketPerformanceWatcher> /* socket_performance_watcher */, |
[email protected] | 0a0b768 | 2010-08-25 17:08:07 | [diff] [blame] | 239 | NetLog* /* net_log */, |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 240 | const NetLogSource& /*source*/) override { |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 241 | allocation_count_++; |
Helen Li | d5bb922 | 2018-04-12 15:33:09 | [diff] [blame] | 242 | return nullptr; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 243 | } |
| 244 | |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 245 | std::unique_ptr<SSLClientSocket> CreateSSLClientSocket( |
Matt Menke | 841fc41 | 2019-03-05 23:20:12 | [diff] [blame] | 246 | std::unique_ptr<StreamSocket> stream_socket, |
[email protected] | 4f4de7e6 | 2010-11-12 19:55:27 | [diff] [blame] | 247 | const HostPortPair& host_and_port, |
[email protected] | 7ab5bbd1 | 2010-10-19 13:33:21 | [diff] [blame] | 248 | const SSLConfig& ssl_config, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 249 | const SSLClientSocketContext& context) override { |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 250 | NOTIMPLEMENTED(); |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 251 | return std::unique_ptr<SSLClientSocket>(); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 252 | } |
Matt Menke | fd95692 | 2019-02-04 23:44:03 | [diff] [blame] | 253 | |
Matt Menke | 52cd95a | 2019-02-08 06:16:27 | [diff] [blame] | 254 | std::unique_ptr<ProxyClientSocket> CreateProxyClientSocket( |
| 255 | std::unique_ptr<StreamSocket> stream_socket, |
| 256 | const std::string& user_agent, |
| 257 | const HostPortPair& endpoint, |
| 258 | const ProxyServer& proxy_server, |
| 259 | HttpAuthController* http_auth_controller, |
| 260 | bool tunnel, |
| 261 | bool using_spdy, |
| 262 | NextProto negotiated_protocol, |
| 263 | ProxyDelegate* proxy_delegate, |
| 264 | bool is_https_proxy, |
| 265 | const NetworkTrafficAnnotationTag& traffic_annotation) override { |
| 266 | NOTIMPLEMENTED(); |
| 267 | return nullptr; |
| 268 | } |
| 269 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 270 | void WaitForSignal(TestConnectJob* job) { waiting_jobs_.push_back(job); } |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 271 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 272 | void SignalJobs(); |
| 273 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 274 | void SignalJob(size_t job); |
| 275 | |
| 276 | void SetJobLoadState(size_t job, LoadState load_state); |
| 277 | |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 278 | // Sets the HasConnectionEstablished value of the specified job to true, |
| 279 | // without invoking the callback. |
| 280 | void SetJobHasEstablishedConnection(size_t job); |
| 281 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 282 | int allocation_count() const { return allocation_count_; } |
| 283 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 284 | private: |
| 285 | int allocation_count_; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 286 | std::vector<TestConnectJob*> waiting_jobs_; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 287 | }; |
| 288 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 289 | class TestConnectJob : public ConnectJob { |
| 290 | public: |
| 291 | enum JobType { |
| 292 | kMockJob, |
| 293 | kMockFailingJob, |
| 294 | kMockPendingJob, |
| 295 | kMockPendingFailingJob, |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 296 | kMockWaitingJob, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 297 | |
| 298 | // Certificate errors return a socket in addition to an error code. |
| 299 | kMockCertErrorJob, |
| 300 | kMockPendingCertErrorJob, |
| 301 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 302 | kMockAdditionalErrorStateJob, |
| 303 | kMockPendingAdditionalErrorStateJob, |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 304 | kMockUnreadDataJob, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 305 | |
| 306 | kMockAuthChallengeOnceJob, |
| 307 | kMockAuthChallengeTwiceJob, |
| 308 | kMockAuthChallengeOnceFailingJob, |
| 309 | kMockAuthChallengeTwiceFailingJob, |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 310 | }; |
| 311 | |
[email protected] | 994d493 | 2010-07-12 17:55:13 | [diff] [blame] | 312 | // The kMockPendingJob uses a slight delay before allowing the connect |
| 313 | // to complete. |
| 314 | static const int kPendingConnectDelay = 2; |
| 315 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 316 | TestConnectJob(JobType job_type, |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 317 | RequestPriority request_priority, |
| 318 | SocketTag socket_tag, |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 319 | base::TimeDelta timeout_duration, |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 320 | const CommonConnectJobParams* common_connect_job_params, |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 321 | ConnectJob::Delegate* delegate, |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 322 | MockClientSocketFactory* client_socket_factory) |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 323 | : ConnectJob(request_priority, |
| 324 | socket_tag, |
Matt Menke | 1a6c92d | 2019-02-23 00:25:38 | [diff] [blame] | 325 | timeout_duration, |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 326 | common_connect_job_params, |
Matt Menke | 1a6c92d | 2019-02-23 00:25:38 | [diff] [blame] | 327 | delegate, |
| 328 | nullptr /* net_log */, |
| 329 | NetLogSourceType::TRANSPORT_CONNECT_JOB, |
| 330 | NetLogEventType::TRANSPORT_CONNECT_JOB_CONNECT), |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 331 | job_type_(job_type), |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 332 | client_socket_factory_(client_socket_factory), |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 333 | load_state_(LOAD_STATE_IDLE), |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 334 | has_established_connection_(false), |
[email protected] | d5492c5 | 2013-11-10 20:44:39 | [diff] [blame] | 335 | store_additional_error_state_(false), |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 336 | weak_factory_(this) {} |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 337 | |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 338 | void Signal() { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 339 | DoConnect(waiting_success_, true /* async */, false /* recoverable */); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 340 | } |
| 341 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 342 | void set_load_state(LoadState load_state) { load_state_ = load_state; } |
| 343 | |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 344 | void set_has_established_connection() { |
| 345 | DCHECK(!has_established_connection_); |
| 346 | has_established_connection_ = true; |
| 347 | } |
| 348 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 349 | // From ConnectJob: |
| 350 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 351 | LoadState GetLoadState() const override { return load_state_; } |
[email protected] | 4645135 | 2009-09-01 14:54:21 | [diff] [blame] | 352 | |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 353 | bool HasEstablishedConnection() const override { |
| 354 | return has_established_connection_; |
| 355 | } |
| 356 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 357 | void GetAdditionalErrorState(ClientSocketHandle* handle) override { |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 358 | if (store_additional_error_state_) { |
| 359 | // Set all of the additional error state fields in some way. |
| 360 | handle->set_is_ssl_error(true); |
[email protected] | 8b49869 | 2010-07-16 17:11:43 | [diff] [blame] | 361 | HttpResponseInfo info; |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 362 | info.headers = new HttpResponseHeaders(std::string()); |
[email protected] | 8b49869 | 2010-07-16 17:11:43 | [diff] [blame] | 363 | handle->set_ssl_error_response_info(info); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 364 | } |
| 365 | } |
| 366 | |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 367 | private: |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 368 | // From ConnectJob: |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 369 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 370 | int ConnectInternal() override { |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 371 | AddressList ignored; |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 372 | client_socket_factory_->CreateTransportClientSocket( |
| 373 | ignored, nullptr, nullptr, NetLogSource()); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 374 | switch (job_type_) { |
| 375 | case kMockJob: |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 376 | return DoConnect(true /* successful */, false /* sync */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 377 | false /* cert_error */); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 378 | case kMockFailingJob: |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 379 | return DoConnect(false /* error */, false /* sync */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 380 | false /* cert_error */); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 381 | case kMockPendingJob: |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 382 | set_load_state(LOAD_STATE_CONNECTING); |
[email protected] | 6b17538 | 2009-10-13 06:47:47 | [diff] [blame] | 383 | |
| 384 | // Depending on execution timings, posting a delayed task can result |
| 385 | // in the task getting executed the at the earliest possible |
| 386 | // opportunity or only after returning once from the message loop and |
| 387 | // then a second call into the message loop. In order to make behavior |
| 388 | // more deterministic, we change the default delay to 2ms. This should |
| 389 | // always require us to wait for the second call into the message loop. |
| 390 | // |
| 391 | // N.B. The correct fix for this and similar timing problems is to |
| 392 | // abstract time for the purpose of unittests. Unfortunately, we have |
| 393 | // a lot of third-party components that directly call the various |
| 394 | // time functions, so this change would be rather invasive. |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 395 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 396 | FROM_HERE, |
kylechar | f4fe517 | 2019-02-15 18:53:49 | [diff] [blame] | 397 | base::BindOnce(base::IgnoreResult(&TestConnectJob::DoConnect), |
| 398 | weak_factory_.GetWeakPtr(), true /* successful */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 399 | true /* async */, false /* cert_error */), |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 400 | base::TimeDelta::FromMilliseconds(kPendingConnectDelay)); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 401 | return ERR_IO_PENDING; |
| 402 | case kMockPendingFailingJob: |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 403 | set_load_state(LOAD_STATE_CONNECTING); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 404 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 405 | FROM_HERE, |
kylechar | f4fe517 | 2019-02-15 18:53:49 | [diff] [blame] | 406 | base::BindOnce(base::IgnoreResult(&TestConnectJob::DoConnect), |
| 407 | weak_factory_.GetWeakPtr(), false /* error */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 408 | true /* async */, false /* cert_error */), |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 409 | base::TimeDelta::FromMilliseconds(2)); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 410 | return ERR_IO_PENDING; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 411 | case kMockWaitingJob: |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 412 | set_load_state(LOAD_STATE_CONNECTING); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 413 | client_socket_factory_->WaitForSignal(this); |
| 414 | waiting_success_ = true; |
| 415 | return ERR_IO_PENDING; |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 416 | case kMockCertErrorJob: |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 417 | return DoConnect(false /* error */, false /* sync */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 418 | true /* cert_error */); |
| 419 | case kMockPendingCertErrorJob: |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 420 | set_load_state(LOAD_STATE_CONNECTING); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 421 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 422 | FROM_HERE, |
kylechar | f4fe517 | 2019-02-15 18:53:49 | [diff] [blame] | 423 | base::BindOnce(base::IgnoreResult(&TestConnectJob::DoConnect), |
| 424 | weak_factory_.GetWeakPtr(), false /* error */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 425 | true /* async */, true /* cert_error */), |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 426 | base::TimeDelta::FromMilliseconds(2)); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 427 | return ERR_IO_PENDING; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 428 | case kMockAdditionalErrorStateJob: |
| 429 | store_additional_error_state_ = true; |
| 430 | return DoConnect(false /* error */, false /* sync */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 431 | false /* cert_error */); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 432 | case kMockPendingAdditionalErrorStateJob: |
| 433 | set_load_state(LOAD_STATE_CONNECTING); |
| 434 | store_additional_error_state_ = true; |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 435 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 436 | FROM_HERE, |
kylechar | f4fe517 | 2019-02-15 18:53:49 | [diff] [blame] | 437 | base::BindOnce(base::IgnoreResult(&TestConnectJob::DoConnect), |
| 438 | weak_factory_.GetWeakPtr(), false /* error */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 439 | true /* async */, false /* cert_error */), |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 440 | base::TimeDelta::FromMilliseconds(2)); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 441 | return ERR_IO_PENDING; |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 442 | case kMockUnreadDataJob: { |
| 443 | int ret = DoConnect(true /* successful */, false /* sync */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 444 | false /* cert_error */); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 445 | static_cast<MockClientSocket*>(socket())->set_has_unread_data(true); |
| 446 | return ret; |
| 447 | } |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 448 | case kMockAuthChallengeOnceJob: |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 449 | set_load_state(LOAD_STATE_CONNECTING); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 450 | DoAdvanceAuthChallenge(1, true /* succeed_after_last_challenge */); |
| 451 | return ERR_IO_PENDING; |
| 452 | case kMockAuthChallengeTwiceJob: |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 453 | set_load_state(LOAD_STATE_CONNECTING); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 454 | DoAdvanceAuthChallenge(2, true /* succeed_after_last_challenge */); |
| 455 | return ERR_IO_PENDING; |
| 456 | case kMockAuthChallengeOnceFailingJob: |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 457 | set_load_state(LOAD_STATE_CONNECTING); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 458 | DoAdvanceAuthChallenge(1, false /* succeed_after_last_challenge */); |
| 459 | return ERR_IO_PENDING; |
| 460 | case kMockAuthChallengeTwiceFailingJob: |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 461 | set_load_state(LOAD_STATE_CONNECTING); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 462 | DoAdvanceAuthChallenge(2, false /* succeed_after_last_challenge */); |
| 463 | return ERR_IO_PENDING; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 464 | default: |
| 465 | NOTREACHED(); |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 466 | SetSocket(std::unique_ptr<StreamSocket>()); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 467 | return ERR_FAILED; |
| 468 | } |
| 469 | } |
| 470 | |
Lily Chen | 02ef29a | 2018-11-30 16:31:43 | [diff] [blame] | 471 | void ChangePriorityInternal(RequestPriority priority) override {} |
| 472 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 473 | int DoConnect(bool succeed, bool was_async, bool cert_error) { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 474 | int result = OK; |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 475 | has_established_connection_ = true; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 476 | if (succeed) { |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 477 | SetSocket(std::make_unique<MockClientSocket>(net_log().net_log())); |
Bence Béky | bdbb0e7 | 2018-08-07 21:42:59 | [diff] [blame] | 478 | socket()->Connect(CompletionOnceCallback()); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 479 | } else if (cert_error) { |
| 480 | SetSocket(std::make_unique<MockClientSocket>(net_log().net_log())); |
| 481 | result = ERR_CERT_COMMON_NAME_INVALID; |
[email protected] | 6e713f0 | 2009-08-06 02:56:40 | [diff] [blame] | 482 | } else { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 483 | result = ERR_CONNECTION_FAILED; |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 484 | SetSocket(std::unique_ptr<StreamSocket>()); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 485 | } |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 486 | |
| 487 | if (was_async) |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 488 | NotifyDelegateOfCompletion(result); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 489 | return result; |
| 490 | } |
| 491 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 492 | void DoAdvanceAuthChallenge(int remaining_challenges, |
| 493 | bool succeed_after_last_challenge) { |
| 494 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 495 | FROM_HERE, |
| 496 | base::BindOnce(&TestConnectJob::InvokeNextProxyAuthCallback, |
| 497 | weak_factory_.GetWeakPtr(), remaining_challenges, |
| 498 | succeed_after_last_challenge)); |
| 499 | } |
| 500 | |
| 501 | void InvokeNextProxyAuthCallback(int remaining_challenges, |
| 502 | bool succeed_after_last_challenge) { |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 503 | set_load_state(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 504 | if (remaining_challenges == 0) { |
| 505 | DoConnect(succeed_after_last_challenge, true /* was_async */, |
| 506 | false /* cert_error */); |
| 507 | return; |
| 508 | } |
| 509 | |
| 510 | // Integration tests make sure HttpResponseInfo and HttpAuthController work. |
| 511 | // The auth tests here are just focused on ConnectJob bookkeeping. |
| 512 | HttpResponseInfo info; |
| 513 | NotifyDelegateOfProxyAuth( |
| 514 | info, nullptr /* http_auth_controller */, |
| 515 | base::BindOnce(&TestConnectJob::DoAdvanceAuthChallenge, |
| 516 | weak_factory_.GetWeakPtr(), remaining_challenges - 1, |
| 517 | succeed_after_last_challenge)); |
| 518 | } |
| 519 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 520 | bool waiting_success_; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 521 | const JobType job_type_; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 522 | MockClientSocketFactory* const client_socket_factory_; |
[email protected] | 4645135 | 2009-09-01 14:54:21 | [diff] [blame] | 523 | LoadState load_state_; |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 524 | bool has_established_connection_; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 525 | bool store_additional_error_state_; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 526 | |
[email protected] | d5492c5 | 2013-11-10 20:44:39 | [diff] [blame] | 527 | base::WeakPtrFactory<TestConnectJob> weak_factory_; |
| 528 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 529 | DISALLOW_COPY_AND_ASSIGN(TestConnectJob); |
| 530 | }; |
| 531 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 532 | class TestConnectJobFactory |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 533 | : public TransportClientSocketPool::ConnectJobFactory { |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 534 | public: |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 535 | TestConnectJobFactory(MockClientSocketFactory* client_socket_factory, |
| 536 | NetLog* net_log) |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 537 | : common_connect_job_params_( |
| 538 | nullptr /* client_socket_factory */, |
| 539 | nullptr /* host_resolver */, |
Matt Menke | b88837e | 2019-03-20 11:50:40 | [diff] [blame] | 540 | nullptr /* http_auth_cache */, |
| 541 | nullptr /* http_auth_handler_factory */, |
| 542 | nullptr /* spdy_session_pool */, |
Matt Menke | b5fb42b | 2019-03-22 17:26:13 | [diff] [blame] | 543 | nullptr /* quic_supported_versions */, |
Matt Menke | b88837e | 2019-03-20 11:50:40 | [diff] [blame] | 544 | nullptr /* quic_stream_factory */, |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 545 | nullptr /* proxy_delegate */, |
Matt Menke | d732ea4 | 2019-03-08 12:05:00 | [diff] [blame] | 546 | nullptr /* http_user_agent_settings */, |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 547 | SSLClientSocketContext(), |
| 548 | SSLClientSocketContext(), |
| 549 | nullptr /* socket_performance_watcher_factory */, |
| 550 | nullptr /* network_quality_estimator */, |
| 551 | net_log, |
| 552 | nullptr /* websocket_endpoint_lock_manager */), |
| 553 | job_type_(TestConnectJob::kMockJob), |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 554 | job_types_(nullptr), |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 555 | client_socket_factory_(client_socket_factory) {} |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 556 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 557 | ~TestConnectJobFactory() override = default; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 558 | |
| 559 | void set_job_type(TestConnectJob::JobType job_type) { job_type_ = job_type; } |
| 560 | |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 561 | void set_job_types(std::list<TestConnectJob::JobType>* job_types) { |
| 562 | job_types_ = job_types; |
| 563 | CHECK(!job_types_->empty()); |
| 564 | } |
| 565 | |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 566 | void set_timeout_duration(base::TimeDelta timeout_duration) { |
| 567 | timeout_duration_ = timeout_duration; |
| 568 | } |
| 569 | |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 570 | // ConnectJobFactory implementation. |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 571 | |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 572 | std::unique_ptr<ConnectJob> NewConnectJob( |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 573 | RequestPriority request_priority, |
| 574 | SocketTag socket_tag, |
| 575 | scoped_refptr<ClientSocketPool::SocketParams> socket_params, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 576 | ConnectJob::Delegate* delegate) const override { |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 577 | EXPECT_TRUE(!job_types_ || !job_types_->empty()); |
| 578 | TestConnectJob::JobType job_type = job_type_; |
| 579 | if (job_types_ && !job_types_->empty()) { |
| 580 | job_type = job_types_->front(); |
| 581 | job_types_->pop_front(); |
| 582 | } |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 583 | return std::make_unique<TestConnectJob>( |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 584 | job_type, request_priority, socket_tag, timeout_duration_, |
| 585 | &common_connect_job_params_, delegate, client_socket_factory_); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | private: |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 589 | const CommonConnectJobParams common_connect_job_params_; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 590 | TestConnectJob::JobType job_type_; |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 591 | std::list<TestConnectJob::JobType>* job_types_; |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 592 | base::TimeDelta timeout_duration_; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 593 | MockClientSocketFactory* const client_socket_factory_; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 594 | |
| 595 | DISALLOW_COPY_AND_ASSIGN(TestConnectJobFactory); |
| 596 | }; |
| 597 | |
[email protected] | a937a06d | 2009-08-19 21:19:24 | [diff] [blame] | 598 | } // namespace |
| 599 | |
[email protected] | a937a06d | 2009-08-19 21:19:24 | [diff] [blame] | 600 | namespace { |
| 601 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 602 | void MockClientSocketFactory::SignalJobs() { |
jdoerrie | 22a91d8b9 | 2018-10-05 08:43:26 | [diff] [blame] | 603 | for (auto it = waiting_jobs_.begin(); it != waiting_jobs_.end(); ++it) { |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 604 | (*it)->Signal(); |
| 605 | } |
| 606 | waiting_jobs_.clear(); |
| 607 | } |
| 608 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 609 | void MockClientSocketFactory::SignalJob(size_t job) { |
| 610 | ASSERT_LT(job, waiting_jobs_.size()); |
| 611 | waiting_jobs_[job]->Signal(); |
| 612 | waiting_jobs_.erase(waiting_jobs_.begin() + job); |
| 613 | } |
| 614 | |
| 615 | void MockClientSocketFactory::SetJobLoadState(size_t job, |
| 616 | LoadState load_state) { |
| 617 | ASSERT_LT(job, waiting_jobs_.size()); |
| 618 | waiting_jobs_[job]->set_load_state(load_state); |
| 619 | } |
| 620 | |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 621 | void MockClientSocketFactory::SetJobHasEstablishedConnection(size_t job) { |
| 622 | ASSERT_LT(job, waiting_jobs_.size()); |
| 623 | waiting_jobs_[job]->set_has_established_connection(); |
| 624 | } |
| 625 | |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 626 | class ClientSocketPoolBaseTest : public TestWithScopedTaskEnvironment { |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 627 | protected: |
Alex Clarke | 0def209 | 2018-12-10 12:01:45 | [diff] [blame] | 628 | ClientSocketPoolBaseTest() |
| 629 | : TestWithScopedTaskEnvironment( |
| 630 | base::test::ScopedTaskEnvironment::MainThreadType::MOCK_TIME), |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 631 | params_(CreateDummyParams()) { |
[email protected] | 636b825 | 2011-04-08 19:56:54 | [diff] [blame] | 632 | connect_backup_jobs_enabled_ = |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 633 | TransportClientSocketPool::connect_backup_jobs_enabled(); |
| 634 | TransportClientSocketPool::set_connect_backup_jobs_enabled(true); |
[email protected] | 636b825 | 2011-04-08 19:56:54 | [diff] [blame] | 635 | } |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 636 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 637 | ~ClientSocketPoolBaseTest() override { |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 638 | TransportClientSocketPool::set_connect_backup_jobs_enabled( |
[email protected] | 636b825 | 2011-04-08 19:56:54 | [diff] [blame] | 639 | connect_backup_jobs_enabled_); |
| 640 | } |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 641 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 642 | void CreatePool(int max_sockets, |
| 643 | int max_sockets_per_group, |
| 644 | bool enable_backup_connect_jobs = false) { |
Tarun Bansal | a763509 | 2019-02-20 10:00:59 | [diff] [blame] | 645 | CreatePoolWithIdleTimeouts(max_sockets, max_sockets_per_group, |
| 646 | kUnusedIdleSocketTimeout, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 647 | ClientSocketPool::used_idle_socket_timeout(), |
| 648 | enable_backup_connect_jobs); |
[email protected] | 9bf28db | 2009-08-29 01:35:16 | [diff] [blame] | 649 | } |
| 650 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 651 | void CreatePoolWithIdleTimeouts(int max_sockets, |
| 652 | int max_sockets_per_group, |
| 653 | base::TimeDelta unused_idle_socket_timeout, |
| 654 | base::TimeDelta used_idle_socket_timeout, |
| 655 | bool enable_backup_connect_jobs = false) { |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 656 | DCHECK(!pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 657 | std::unique_ptr<TestConnectJobFactory> connect_job_factory = |
| 658 | std::make_unique<TestConnectJobFactory>(&client_socket_factory_, |
| 659 | &net_log_); |
| 660 | connect_job_factory_ = connect_job_factory.get(); |
| 661 | pool_ = TransportClientSocketPool::CreateForTesting( |
| 662 | max_sockets, max_sockets_per_group, unused_idle_socket_timeout, |
| 663 | used_idle_socket_timeout, std::move(connect_job_factory), |
| 664 | nullptr /* ssl_config_service */, enable_backup_connect_jobs); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 665 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 666 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 667 | int StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 668 | const ClientSocketPool::GroupId& group_id, |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 669 | RequestPriority priority, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 670 | ClientSocketPool::RespectLimits respect_limits) { |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 671 | return test_base_.StartRequestUsingPool(pool_.get(), group_id, priority, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 672 | respect_limits, params_); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 673 | } |
| 674 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 675 | int StartRequest(const ClientSocketPool::GroupId& group_id, |
| 676 | RequestPriority priority) { |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 677 | return StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 678 | group_id, priority, ClientSocketPool::RespectLimits::ENABLED); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 679 | } |
| 680 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 681 | int GetOrderOfRequest(size_t index) const { |
| 682 | return test_base_.GetOrderOfRequest(index); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 683 | } |
| 684 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 685 | bool ReleaseOneConnection(ClientSocketPoolTest::KeepAlive keep_alive) { |
| 686 | return test_base_.ReleaseOneConnection(keep_alive); |
| 687 | } |
| 688 | |
| 689 | void ReleaseAllConnections(ClientSocketPoolTest::KeepAlive keep_alive) { |
| 690 | test_base_.ReleaseAllConnections(keep_alive); |
| 691 | } |
| 692 | |
| 693 | TestSocketRequest* request(int i) { return test_base_.request(i); } |
| 694 | size_t requests_size() const { return test_base_.requests_size(); } |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 695 | std::vector<std::unique_ptr<TestSocketRequest>>* requests() { |
olli.raula | 9d66b7d | 2015-11-23 08:30:42 | [diff] [blame] | 696 | return test_base_.requests(); |
| 697 | } |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 698 | // Only counts the requests that get sockets asynchronously; |
| 699 | // synchronous completions are not registered by this count. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 700 | size_t completion_count() const { return test_base_.completion_count(); } |
| 701 | |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 702 | TestNetLog net_log_; |
[email protected] | 636b825 | 2011-04-08 19:56:54 | [diff] [blame] | 703 | bool connect_backup_jobs_enabled_; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 704 | MockClientSocketFactory client_socket_factory_; |
[email protected] | 17a0c6c | 2009-08-04 00:07:04 | [diff] [blame] | 705 | TestConnectJobFactory* connect_job_factory_; |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 706 | // These parameters are never actually used to create a TransportConnectJob. |
Matt Menke | 84d11e56 | 2019-03-27 00:11:19 | [diff] [blame] | 707 | scoped_refptr<ClientSocketPool::SocketParams> params_; |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 708 | std::unique_ptr<TransportClientSocketPool> pool_; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 709 | ClientSocketPoolTest test_base_; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 710 | }; |
| 711 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 712 | TEST_F(ClientSocketPoolBaseTest, BasicSynchronous) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 713 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 714 | |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 715 | TestCompletionCallback callback; |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 716 | ClientSocketHandle handle; |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 717 | BoundTestNetLog log; |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 718 | TestLoadTimingInfoNotConnected(handle); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 719 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 720 | EXPECT_EQ( |
| 721 | OK, handle.Init( |
| 722 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
| 723 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 724 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound())); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 725 | EXPECT_TRUE(handle.is_initialized()); |
| 726 | EXPECT_TRUE(handle.socket()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 727 | TestLoadTimingInfoConnectedNotReused(handle); |
| 728 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 729 | handle.Reset(); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 730 | TestLoadTimingInfoNotConnected(handle); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 731 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 732 | TestNetLogEntry::List entries; |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 733 | log.GetEntries(&entries); |
| 734 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 735 | EXPECT_EQ(5u, entries.size()); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 736 | EXPECT_TRUE(LogContainsEvent( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 737 | entries, 0, NetLogEventType::TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 738 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 739 | EXPECT_TRUE(LogContainsBeginEvent(entries, 1, NetLogEventType::SOCKET_POOL)); |
| 740 | EXPECT_TRUE(LogContainsEvent( |
| 741 | entries, 2, NetLogEventType::SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
| 742 | NetLogEventPhase::NONE)); |
| 743 | EXPECT_TRUE(LogContainsEvent(entries, 3, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 744 | NetLogEventType::SOCKET_POOL_BOUND_TO_SOCKET, |
| 745 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 746 | EXPECT_TRUE(LogContainsEndEvent(entries, 4, NetLogEventType::SOCKET_POOL)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 747 | } |
| 748 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 749 | TEST_F(ClientSocketPoolBaseTest, InitConnectionFailure) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 750 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 751 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 752 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 753 | BoundTestNetLog log; |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 754 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 755 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 756 | TestCompletionCallback callback; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 757 | // Set the additional error state members to ensure that they get cleared. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 758 | handle.set_is_ssl_error(true); |
[email protected] | 8b49869 | 2010-07-16 17:11:43 | [diff] [blame] | 759 | HttpResponseInfo info; |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 760 | info.headers = new HttpResponseHeaders(std::string()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 761 | handle.set_ssl_error_response_info(info); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 762 | EXPECT_EQ( |
| 763 | ERR_CONNECTION_FAILED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 764 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 765 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 766 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 767 | log.bound())); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 768 | EXPECT_FALSE(handle.socket()); |
| 769 | EXPECT_FALSE(handle.is_ssl_error()); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 770 | EXPECT_TRUE(handle.ssl_error_response_info().headers.get() == nullptr); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 771 | TestLoadTimingInfoNotConnected(handle); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 772 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 773 | TestNetLogEntry::List entries; |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 774 | log.GetEntries(&entries); |
| 775 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 776 | EXPECT_EQ(4u, entries.size()); |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 777 | EXPECT_TRUE(LogContainsEvent( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 778 | entries, 0, NetLogEventType::TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 779 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 780 | EXPECT_TRUE(LogContainsBeginEvent(entries, 1, NetLogEventType::SOCKET_POOL)); |
| 781 | EXPECT_TRUE(LogContainsEvent( |
| 782 | entries, 2, NetLogEventType::SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
| 783 | NetLogEventPhase::NONE)); |
| 784 | EXPECT_TRUE(LogContainsEndEvent(entries, 3, NetLogEventType::SOCKET_POOL)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 785 | } |
| 786 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 787 | // Make sure different groups do not share sockets. |
| 788 | TEST_F(ClientSocketPoolBaseTest, GroupSeparation) { |
| 789 | CreatePool(1000 /* max_sockets */, 2 /* max_sockets_per_group */); |
| 790 | |
| 791 | const HostPortPair kHostPortPairs[] = { |
| 792 | {"a", 80}, |
| 793 | {"a", 443}, |
| 794 | {"b", 80}, |
| 795 | }; |
| 796 | |
| 797 | const ClientSocketPool::SocketType kSocketTypes[] = { |
| 798 | ClientSocketPool::SocketType::kHttp, |
| 799 | ClientSocketPool::SocketType::kSsl, |
| 800 | ClientSocketPool::SocketType::kSslVersionInterferenceProbe, |
| 801 | ClientSocketPool::SocketType::kFtp, |
| 802 | }; |
| 803 | |
| 804 | const bool kPrivacyModes[] = {false, true}; |
| 805 | |
| 806 | int total_idle_sockets = 0; |
| 807 | |
| 808 | // Walk through each GroupId, making sure that requesting a socket for one |
| 809 | // group does not return a previously connected socket for another group. |
| 810 | for (const auto& host_port_pair : kHostPortPairs) { |
| 811 | SCOPED_TRACE(host_port_pair.ToString()); |
| 812 | for (const auto& socket_type : kSocketTypes) { |
| 813 | SCOPED_TRACE(static_cast<int>(socket_type)); |
| 814 | for (const auto& privacy_mode : kPrivacyModes) { |
| 815 | SCOPED_TRACE(privacy_mode); |
| 816 | |
| 817 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 818 | |
| 819 | ClientSocketPool::GroupId group_id(host_port_pair, socket_type, |
| 820 | privacy_mode); |
| 821 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 822 | EXPECT_FALSE(pool_->HasGroupForTesting(group_id)); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 823 | |
| 824 | TestCompletionCallback callback; |
| 825 | ClientSocketHandle handle; |
| 826 | |
| 827 | // Since the group is empty, requesting a socket should not complete |
| 828 | // synchronously. |
| 829 | EXPECT_THAT( |
| 830 | handle.Init(group_id, params_, DEFAULT_PRIORITY, SocketTag(), |
| 831 | ClientSocketPool::RespectLimits::ENABLED, |
| 832 | callback.callback(), |
| 833 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 834 | NetLogWithSource()), |
| 835 | IsError(ERR_IO_PENDING)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 836 | EXPECT_TRUE(pool_->HasGroupForTesting(group_id)); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 837 | EXPECT_EQ(total_idle_sockets, pool_->IdleSocketCount()); |
| 838 | |
| 839 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 840 | EXPECT_TRUE(handle.socket()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 841 | EXPECT_TRUE(pool_->HasGroupForTesting(group_id)); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 842 | EXPECT_EQ(total_idle_sockets, pool_->IdleSocketCount()); |
| 843 | |
| 844 | // Return socket to pool. |
| 845 | handle.Reset(); |
| 846 | EXPECT_EQ(total_idle_sockets + 1, pool_->IdleSocketCount()); |
| 847 | |
| 848 | // Requesting a socket again should return the same socket as before, so |
| 849 | // should complete synchronously. |
| 850 | EXPECT_THAT( |
| 851 | handle.Init(group_id, params_, DEFAULT_PRIORITY, SocketTag(), |
| 852 | ClientSocketPool::RespectLimits::ENABLED, |
| 853 | callback.callback(), |
| 854 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 855 | NetLogWithSource()), |
| 856 | IsOk()); |
| 857 | EXPECT_TRUE(handle.socket()); |
| 858 | EXPECT_EQ(total_idle_sockets, pool_->IdleSocketCount()); |
| 859 | |
| 860 | // Return socket to pool again. |
| 861 | handle.Reset(); |
| 862 | EXPECT_EQ(total_idle_sockets + 1, pool_->IdleSocketCount()); |
| 863 | |
| 864 | ++total_idle_sockets; |
| 865 | } |
| 866 | } |
| 867 | } |
| 868 | } |
| 869 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 870 | TEST_F(ClientSocketPoolBaseTest, TotalLimit) { |
| 871 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 872 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 873 | // TODO(eroman): Check that the NetLog contains this event. |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 874 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 875 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 876 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
| 877 | EXPECT_THAT(StartRequest(TestGroupId("c"), DEFAULT_PRIORITY), IsOk()); |
| 878 | EXPECT_THAT(StartRequest(TestGroupId("d"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 879 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 880 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 881 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 882 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 883 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 884 | EXPECT_THAT(StartRequest(TestGroupId("e"), DEFAULT_PRIORITY), |
| 885 | IsError(ERR_IO_PENDING)); |
| 886 | EXPECT_THAT(StartRequest(TestGroupId("f"), DEFAULT_PRIORITY), |
| 887 | IsError(ERR_IO_PENDING)); |
| 888 | EXPECT_THAT(StartRequest(TestGroupId("g"), DEFAULT_PRIORITY), |
| 889 | IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 890 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 891 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 892 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 893 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 894 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 895 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 896 | |
| 897 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 898 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 899 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 900 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 901 | EXPECT_EQ(5, GetOrderOfRequest(5)); |
| 902 | EXPECT_EQ(6, GetOrderOfRequest(6)); |
| 903 | EXPECT_EQ(7, GetOrderOfRequest(7)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 904 | |
| 905 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 906 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(8)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | TEST_F(ClientSocketPoolBaseTest, TotalLimitReachedNewGroup) { |
| 910 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 911 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 912 | // TODO(eroman): Check that the NetLog contains this event. |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 913 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 914 | // Reach all limits: max total sockets, and max sockets per group. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 915 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 916 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 917 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
| 918 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 919 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 920 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 921 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 922 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 923 | |
| 924 | // Now create a new group and verify that we don't starve it. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 925 | EXPECT_THAT(StartRequest(TestGroupId("c"), DEFAULT_PRIORITY), |
| 926 | IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 927 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 928 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 929 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 930 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 931 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 932 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 933 | |
| 934 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 935 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 936 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 937 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 938 | EXPECT_EQ(5, GetOrderOfRequest(5)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 939 | |
| 940 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 941 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(6)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 942 | } |
| 943 | |
| 944 | TEST_F(ClientSocketPoolBaseTest, TotalLimitRespectsPriority) { |
| 945 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 946 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 947 | EXPECT_THAT(StartRequest(TestGroupId("b"), LOWEST), IsOk()); |
| 948 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsOk()); |
| 949 | EXPECT_THAT(StartRequest(TestGroupId("b"), HIGHEST), IsOk()); |
| 950 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 951 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 952 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 953 | client_socket_factory_.allocation_count()); |
| 954 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 955 | EXPECT_THAT(StartRequest(TestGroupId("c"), LOWEST), IsError(ERR_IO_PENDING)); |
| 956 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 957 | EXPECT_THAT(StartRequest(TestGroupId("b"), HIGHEST), IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 958 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 959 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 960 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 961 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 962 | |
| 963 | // First 4 requests don't have to wait, and finish in order. |
| 964 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 965 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 966 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 967 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 968 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 969 | // Request ("b", HIGHEST) has the highest priority, then (TestGroupId("a"), |
| 970 | // MEDIUM), and then ("c", LOWEST). |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 971 | EXPECT_EQ(7, GetOrderOfRequest(5)); |
| 972 | EXPECT_EQ(6, GetOrderOfRequest(6)); |
| 973 | EXPECT_EQ(5, GetOrderOfRequest(7)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 974 | |
| 975 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 976 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(9)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 977 | } |
| 978 | |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 979 | // Test reprioritizing a request before completion doesn't interfere with |
| 980 | // its completion. |
| 981 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeOne) { |
| 982 | CreatePool(kDefaultMaxSockets, 1); |
| 983 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 984 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 985 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 986 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 987 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 988 | |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 989 | request(1)->handle()->SetPriority(HIGHEST); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 990 | |
| 991 | ReleaseOneConnection(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 992 | |
| 993 | EXPECT_TRUE(request(1)->handle()->socket()); |
| 994 | } |
| 995 | |
| 996 | // Reprioritize a request up past another one and make sure that changes the |
| 997 | // completion order. |
| 998 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeUpReorder) { |
| 999 | CreatePool(kDefaultMaxSockets, 1); |
| 1000 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1001 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1002 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1003 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1004 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1005 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1006 | EXPECT_FALSE(request(2)->handle()->socket()); |
| 1007 | |
| 1008 | request(2)->handle()->SetPriority(HIGHEST); |
| 1009 | |
| 1010 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1011 | |
| 1012 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1013 | EXPECT_EQ(3, GetOrderOfRequest(2)); |
| 1014 | EXPECT_EQ(2, GetOrderOfRequest(3)); |
| 1015 | } |
| 1016 | |
| 1017 | // Reprioritize a request without changing relative priorities and check |
| 1018 | // that the order doesn't change. |
| 1019 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeUpNoReorder) { |
| 1020 | CreatePool(kDefaultMaxSockets, 1); |
| 1021 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1022 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1023 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1024 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1025 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1026 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1027 | EXPECT_FALSE(request(2)->handle()->socket()); |
| 1028 | |
| 1029 | request(2)->handle()->SetPriority(MEDIUM); |
| 1030 | |
| 1031 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1032 | |
| 1033 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1034 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1035 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 1036 | } |
| 1037 | |
| 1038 | // Reprioritize a request past down another one and make sure that changes the |
| 1039 | // completion order. |
| 1040 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeDownReorder) { |
| 1041 | CreatePool(kDefaultMaxSockets, 1); |
| 1042 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1043 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1044 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1045 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1046 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1047 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1048 | EXPECT_FALSE(request(2)->handle()->socket()); |
| 1049 | |
| 1050 | request(1)->handle()->SetPriority(LOW); |
| 1051 | |
| 1052 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1053 | |
| 1054 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1055 | EXPECT_EQ(3, GetOrderOfRequest(2)); |
| 1056 | EXPECT_EQ(2, GetOrderOfRequest(3)); |
| 1057 | } |
| 1058 | |
| 1059 | // Reprioritize a request to the same level as another and confirm it is |
| 1060 | // put after the old request. |
| 1061 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeResetFIFO) { |
| 1062 | CreatePool(kDefaultMaxSockets, 1); |
| 1063 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1064 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1065 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1066 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1067 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1068 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1069 | EXPECT_FALSE(request(2)->handle()->socket()); |
| 1070 | |
| 1071 | request(1)->handle()->SetPriority(MEDIUM); |
| 1072 | |
| 1073 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1074 | |
| 1075 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1076 | EXPECT_EQ(3, GetOrderOfRequest(2)); |
| 1077 | EXPECT_EQ(2, GetOrderOfRequest(3)); |
| 1078 | } |
| 1079 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1080 | TEST_F(ClientSocketPoolBaseTest, TotalLimitRespectsGroupLimit) { |
| 1081 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1082 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1083 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsOk()); |
| 1084 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsOk()); |
| 1085 | EXPECT_THAT(StartRequest(TestGroupId("b"), HIGHEST), IsOk()); |
| 1086 | EXPECT_THAT(StartRequest(TestGroupId("b"), MEDIUM), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1087 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1088 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1089 | client_socket_factory_.allocation_count()); |
| 1090 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1091 | EXPECT_THAT(StartRequest(TestGroupId("c"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1092 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 1093 | EXPECT_THAT(StartRequest(TestGroupId("b"), HIGHEST), IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1094 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1095 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1096 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1097 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1098 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1099 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1100 | |
| 1101 | // First 4 requests don't have to wait, and finish in order. |
| 1102 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1103 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1104 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 1105 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 1106 | |
| 1107 | // Request ("b", 7) has the highest priority, but we can't make new socket for |
| 1108 | // group "b", because it has reached the per-group limit. Then we make |
| 1109 | // socket for ("c", 6), because it has higher priority than ("a", 4), |
| 1110 | // and we still can't make a socket for group "b". |
| 1111 | EXPECT_EQ(5, GetOrderOfRequest(5)); |
| 1112 | EXPECT_EQ(6, GetOrderOfRequest(6)); |
| 1113 | EXPECT_EQ(7, GetOrderOfRequest(7)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1114 | |
| 1115 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1116 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(8)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1117 | } |
| 1118 | |
| 1119 | // Make sure that we count connecting sockets against the total limit. |
| 1120 | TEST_F(ClientSocketPoolBaseTest, TotalLimitCountsConnectingSockets) { |
| 1121 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1122 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1123 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1124 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
| 1125 | EXPECT_THAT(StartRequest(TestGroupId("c"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1126 | |
| 1127 | // Create one asynchronous request. |
| 1128 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1129 | EXPECT_THAT(StartRequest(TestGroupId("d"), DEFAULT_PRIORITY), |
| 1130 | IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1131 | |
[email protected] | 6b17538 | 2009-10-13 06:47:47 | [diff] [blame] | 1132 | // We post all of our delayed tasks with a 2ms delay. I.e. they don't |
| 1133 | // actually become pending until 2ms after they have been created. In order |
| 1134 | // to flush all tasks, we need to wait so that we know there are no |
| 1135 | // soon-to-be-pending tasks waiting. |
Alex Clarke | 0def209 | 2018-12-10 12:01:45 | [diff] [blame] | 1136 | FastForwardBy(base::TimeDelta::FromMilliseconds(10)); |
[email protected] | 6b17538 | 2009-10-13 06:47:47 | [diff] [blame] | 1137 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1138 | // The next synchronous request should wait for its turn. |
| 1139 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1140 | EXPECT_THAT(StartRequest(TestGroupId("e"), DEFAULT_PRIORITY), |
| 1141 | IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1142 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1143 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1144 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1145 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1146 | client_socket_factory_.allocation_count()); |
| 1147 | |
| 1148 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1149 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1150 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 1151 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1152 | EXPECT_EQ(5, GetOrderOfRequest(5)); |
| 1153 | |
| 1154 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1155 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(6)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1156 | } |
| 1157 | |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1158 | TEST_F(ClientSocketPoolBaseTest, CorrectlyCountStalledGroups) { |
| 1159 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 1160 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 1161 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 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()); |
| 1165 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1166 | |
| 1167 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 1168 | |
| 1169 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1170 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1171 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), |
| 1172 | IsError(ERR_IO_PENDING)); |
| 1173 | EXPECT_THAT(StartRequest(TestGroupId("c"), DEFAULT_PRIORITY), |
| 1174 | IsError(ERR_IO_PENDING)); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1175 | |
| 1176 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1177 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1178 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1179 | EXPECT_EQ(kDefaultMaxSockets + 1, client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1180 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1181 | EXPECT_EQ(kDefaultMaxSockets + 2, client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1182 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
| 1183 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1184 | EXPECT_EQ(kDefaultMaxSockets + 2, client_socket_factory_.allocation_count()); |
| 1185 | } |
| 1186 | |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 1187 | TEST_F(ClientSocketPoolBaseTest, StallAndThenCancelAndTriggerAvailableSocket) { |
| 1188 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 1189 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1190 | |
| 1191 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1192 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1193 | EXPECT_EQ( |
| 1194 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1195 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1196 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1197 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 1198 | NetLogWithSource())); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 1199 | |
| 1200 | ClientSocketHandle handles[4]; |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 1201 | for (size_t i = 0; i < base::size(handles); ++i) { |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1202 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1203 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1204 | handles[i].Init( |
| 1205 | TestGroupId("b"), params_, DEFAULT_PRIORITY, SocketTag(), |
| 1206 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1207 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 1208 | NetLogWithSource())); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 1209 | } |
| 1210 | |
| 1211 | // One will be stalled, cancel all the handles now. |
| 1212 | // This should hit the OnAvailableSocketSlot() code where we previously had |
| 1213 | // stalled groups, but no longer have any. |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 1214 | for (size_t i = 0; i < base::size(handles); ++i) |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 1215 | handles[i].Reset(); |
| 1216 | } |
| 1217 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1218 | TEST_F(ClientSocketPoolBaseTest, CancelStalledSocketAtSocketLimit) { |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1219 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1220 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 1221 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1222 | { |
| 1223 | ClientSocketHandle handles[kDefaultMaxSockets]; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1224 | TestCompletionCallback callbacks[kDefaultMaxSockets]; |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1225 | for (int i = 0; i < kDefaultMaxSockets; ++i) { |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1226 | EXPECT_EQ(OK, handles[i].Init(TestGroupId(base::NumberToString(i)), |
| 1227 | params_, DEFAULT_PRIORITY, SocketTag(), |
Paul Jensen | 8d6f87ec | 2018-01-13 00:46:54 | [diff] [blame] | 1228 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1229 | callbacks[i].callback(), |
| 1230 | ClientSocketPool::ProxyAuthCallback(), |
| 1231 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1232 | } |
| 1233 | |
| 1234 | // Force a stalled group. |
| 1235 | ClientSocketHandle stalled_handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1236 | TestCompletionCallback callback; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1237 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1238 | stalled_handle.Init( |
| 1239 | TestGroupId("foo"), params_, DEFAULT_PRIORITY, SocketTag(), |
| 1240 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1241 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 1242 | NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1243 | |
| 1244 | // Cancel the stalled request. |
| 1245 | stalled_handle.Reset(); |
| 1246 | |
| 1247 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1248 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 1249 | |
| 1250 | // Dropping out of scope will close all handles and return them to idle. |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1251 | } |
| 1252 | |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1253 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1254 | EXPECT_EQ(kDefaultMaxSockets, pool_->IdleSocketCount()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1255 | } |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1256 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1257 | TEST_F(ClientSocketPoolBaseTest, CancelPendingSocketAtSocketLimit) { |
| 1258 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1259 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 1260 | |
| 1261 | { |
| 1262 | ClientSocketHandle handles[kDefaultMaxSockets]; |
| 1263 | for (int i = 0; i < kDefaultMaxSockets; ++i) { |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1264 | TestCompletionCallback callback; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1265 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1266 | handles[i].Init(TestGroupId(base::NumberToString(i)), params_, |
| 1267 | DEFAULT_PRIORITY, SocketTag(), |
| 1268 | ClientSocketPool::RespectLimits::ENABLED, |
| 1269 | callback.callback(), |
| 1270 | ClientSocketPool::ProxyAuthCallback(), |
| 1271 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1272 | } |
| 1273 | |
| 1274 | // Force a stalled group. |
| 1275 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1276 | ClientSocketHandle stalled_handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1277 | TestCompletionCallback callback; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1278 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1279 | stalled_handle.Init( |
| 1280 | TestGroupId("foo"), params_, DEFAULT_PRIORITY, SocketTag(), |
| 1281 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1282 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 1283 | NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1284 | |
| 1285 | // Since it is stalled, it should have no connect jobs. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1286 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("foo"))); |
| 1287 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 1288 | TestGroupId("foo"))); |
| 1289 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroupForTesting( |
| 1290 | TestGroupId("foo"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1291 | |
| 1292 | // Cancel the stalled request. |
| 1293 | handles[0].Reset(); |
| 1294 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1295 | // Now we should have a connect job. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1296 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("foo"))); |
| 1297 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 1298 | TestGroupId("foo"))); |
| 1299 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroupForTesting( |
| 1300 | TestGroupId("foo"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1301 | |
| 1302 | // The stalled socket should connect. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1303 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1304 | |
| 1305 | EXPECT_EQ(kDefaultMaxSockets + 1, |
| 1306 | client_socket_factory_.allocation_count()); |
| 1307 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1308 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("foo"))); |
| 1309 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 1310 | TestGroupId("foo"))); |
| 1311 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroupForTesting( |
| 1312 | TestGroupId("foo"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1313 | |
| 1314 | // Dropping out of scope will close all handles and return them to idle. |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1315 | } |
| 1316 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1317 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 1318 | } |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1319 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1320 | TEST_F(ClientSocketPoolBaseTest, WaitForStalledSocketAtSocketLimit) { |
| 1321 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1322 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1323 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1324 | ClientSocketHandle stalled_handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1325 | TestCompletionCallback callback; |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1326 | { |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 1327 | EXPECT_FALSE(pool_->IsStalled()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1328 | ClientSocketHandle handles[kDefaultMaxSockets]; |
| 1329 | for (int i = 0; i < kDefaultMaxSockets; ++i) { |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1330 | TestCompletionCallback callback; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1331 | EXPECT_EQ( |
| 1332 | OK, handles[i].Init(TestGroupId(base::StringPrintf("Take 2: %d", i)), |
| 1333 | params_, DEFAULT_PRIORITY, SocketTag(), |
| 1334 | ClientSocketPool::RespectLimits::ENABLED, |
| 1335 | callback.callback(), |
| 1336 | ClientSocketPool::ProxyAuthCallback(), |
| 1337 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1338 | } |
| 1339 | |
| 1340 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1341 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 1342 | EXPECT_FALSE(pool_->IsStalled()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1343 | |
| 1344 | // Now we will hit the socket limit. |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1345 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1346 | stalled_handle.Init( |
| 1347 | TestGroupId("foo"), params_, DEFAULT_PRIORITY, SocketTag(), |
| 1348 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1349 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 1350 | NetLogWithSource())); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 1351 | EXPECT_TRUE(pool_->IsStalled()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1352 | |
| 1353 | // Dropping out of scope will close all handles and return them to idle. |
| 1354 | } |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1355 | |
| 1356 | // But if we wait for it, the released idle sockets will be closed in |
| 1357 | // preference of the waiting request. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1358 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1359 | |
| 1360 | EXPECT_EQ(kDefaultMaxSockets + 1, client_socket_factory_.allocation_count()); |
| 1361 | EXPECT_EQ(3, pool_->IdleSocketCount()); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1362 | } |
| 1363 | |
| 1364 | // Regression test for http://crbug.com/40952. |
| 1365 | TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketAtSocketLimitDeleteGroup) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1366 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 1367 | true /* enable_backup_connect_jobs */); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1368 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 1369 | |
| 1370 | for (int i = 0; i < kDefaultMaxSockets; ++i) { |
| 1371 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1372 | TestCompletionCallback callback; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1373 | EXPECT_EQ(OK, handle.Init(TestGroupId(base::NumberToString(i)), params_, |
| 1374 | DEFAULT_PRIORITY, SocketTag(), |
| 1375 | ClientSocketPool::RespectLimits::ENABLED, |
| 1376 | callback.callback(), |
| 1377 | ClientSocketPool::ProxyAuthCallback(), |
| 1378 | pool_.get(), NetLogWithSource())); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1379 | } |
| 1380 | |
| 1381 | // Flush all the DoReleaseSocket tasks. |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 1382 | base::RunLoop().RunUntilIdle(); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1383 | |
| 1384 | // Stall a group. Set a pending job so it'll trigger a backup job if we don't |
| 1385 | // reuse a socket. |
| 1386 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1387 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1388 | TestCompletionCallback callback; |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1389 | |
| 1390 | // "0" is special here, since it should be the first entry in the sorted map, |
| 1391 | // which is the one which we would close an idle socket for. We shouldn't |
| 1392 | // close an idle socket though, since we should reuse the idle socket. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1393 | EXPECT_EQ(OK, handle.Init( |
| 1394 | TestGroupId("0"), params_, DEFAULT_PRIORITY, SocketTag(), |
| 1395 | ClientSocketPool::RespectLimits::ENABLED, |
| 1396 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1397 | pool_.get(), NetLogWithSource())); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1398 | |
| 1399 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1400 | EXPECT_EQ(kDefaultMaxSockets - 1, pool_->IdleSocketCount()); |
| 1401 | } |
| 1402 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1403 | TEST_F(ClientSocketPoolBaseTest, PendingRequests) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1404 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1405 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1406 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1407 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1408 | EXPECT_THAT(StartRequest(TestGroupId("a"), IDLE), IsError(ERR_IO_PENDING)); |
| 1409 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
| 1410 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1411 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1412 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 1413 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1414 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1415 | ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1416 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
| 1417 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1418 | EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup, |
| 1419 | completion_count()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1420 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1421 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1422 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
[email protected] | c9c6f5c | 2010-07-31 01:30:03 | [diff] [blame] | 1423 | EXPECT_EQ(8, GetOrderOfRequest(3)); |
| 1424 | EXPECT_EQ(6, GetOrderOfRequest(4)); |
| 1425 | EXPECT_EQ(4, GetOrderOfRequest(5)); |
| 1426 | EXPECT_EQ(3, GetOrderOfRequest(6)); |
| 1427 | EXPECT_EQ(5, GetOrderOfRequest(7)); |
| 1428 | EXPECT_EQ(7, GetOrderOfRequest(8)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1429 | |
| 1430 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1431 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(9)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1432 | } |
| 1433 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1434 | TEST_F(ClientSocketPoolBaseTest, PendingRequests_NoKeepAlive) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1435 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1436 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1437 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1438 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1439 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
| 1440 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1441 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1442 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 1443 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1444 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1445 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1446 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1447 | for (size_t i = kDefaultMaxSocketsPerGroup; i < requests_size(); ++i) |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1448 | EXPECT_THAT(request(i)->WaitForResult(), IsOk()); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1449 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1450 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1451 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1452 | EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup, |
| 1453 | completion_count()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1454 | } |
| 1455 | |
| 1456 | // This test will start up a RequestSocket() and then immediately Cancel() it. |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1457 | // The pending connect job will be cancelled and should not call back into |
| 1458 | // ClientSocketPoolBase. |
| 1459 | TEST_F(ClientSocketPoolBaseTest, CancelRequestClearGroup) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1460 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1461 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1462 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1463 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1464 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1465 | EXPECT_EQ( |
| 1466 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1467 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1468 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1469 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 1470 | NetLogWithSource())); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1471 | handle.Reset(); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1472 | } |
| 1473 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1474 | TEST_F(ClientSocketPoolBaseTest, ConnectCancelConnect) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1475 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1476 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1477 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 1478 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1479 | TestCompletionCallback callback; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1480 | |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1481 | EXPECT_EQ( |
| 1482 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1483 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1484 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1485 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 1486 | NetLogWithSource())); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1487 | |
| 1488 | handle.Reset(); |
| 1489 | |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1490 | TestCompletionCallback callback2; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1491 | EXPECT_EQ( |
| 1492 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1493 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1494 | ClientSocketPool::RespectLimits::ENABLED, |
| 1495 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1496 | pool_.get(), NetLogWithSource())); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1497 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1498 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1499 | EXPECT_FALSE(callback.have_result()); |
| 1500 | |
| 1501 | handle.Reset(); |
| 1502 | } |
| 1503 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1504 | TEST_F(ClientSocketPoolBaseTest, CancelRequest) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1505 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1506 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1507 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1508 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1509 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
| 1510 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1511 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1512 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 1513 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1514 | |
| 1515 | // Cancel a request. |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1516 | size_t index_to_cancel = kDefaultMaxSocketsPerGroup + 2; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1517 | EXPECT_FALSE((*requests())[index_to_cancel]->handle()->is_initialized()); |
| 1518 | (*requests())[index_to_cancel]->handle()->Reset(); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1519 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1520 | ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1521 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1522 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
| 1523 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1524 | EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup - 1, |
| 1525 | completion_count()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1526 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1527 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1528 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1529 | EXPECT_EQ(5, GetOrderOfRequest(3)); |
| 1530 | EXPECT_EQ(3, GetOrderOfRequest(4)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1531 | EXPECT_EQ(ClientSocketPoolTest::kRequestNotFound, |
| 1532 | GetOrderOfRequest(5)); // Canceled request. |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1533 | EXPECT_EQ(4, GetOrderOfRequest(6)); |
| 1534 | EXPECT_EQ(6, GetOrderOfRequest(7)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1535 | |
| 1536 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1537 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(8)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1538 | } |
| 1539 | |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1540 | // Function to be used as a callback on socket request completion. It first |
| 1541 | // disconnects the successfully connected socket from the first request, and |
| 1542 | // then reuses the ClientSocketHandle to request another socket. |
| 1543 | // |
| 1544 | // |nested_callback| is called with the result of the second socket request. |
| 1545 | void RequestSocketOnComplete(ClientSocketHandle* handle, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1546 | TransportClientSocketPool* pool, |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1547 | TestConnectJobFactory* test_connect_job_factory, |
| 1548 | TestConnectJob::JobType next_job_type, |
Bence Béky | a4a5093 | 2018-08-10 13:39:41 | [diff] [blame] | 1549 | TestCompletionCallback* nested_callback, |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1550 | int first_request_result) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1551 | EXPECT_THAT(first_request_result, IsOk()); |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1552 | |
| 1553 | test_connect_job_factory->set_job_type(next_job_type); |
| 1554 | |
| 1555 | // Don't allow reuse of the socket. Disconnect it and then release it. |
| 1556 | if (handle->socket()) |
| 1557 | handle->socket()->Disconnect(); |
| 1558 | handle->Reset(); |
| 1559 | |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1560 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1561 | int rv = handle->Init( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1562 | TestGroupId("a"), CreateDummyParams(), LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1563 | ClientSocketPool::RespectLimits::ENABLED, nested_callback->callback(), |
| 1564 | ClientSocketPool::ProxyAuthCallback(), pool, NetLogWithSource()); |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1565 | if (rv != ERR_IO_PENDING) { |
| 1566 | DCHECK_EQ(TestConnectJob::kMockJob, next_job_type); |
Bence Béky | a4a5093 | 2018-08-10 13:39:41 | [diff] [blame] | 1567 | nested_callback->callback().Run(rv); |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1568 | } else { |
| 1569 | DCHECK_EQ(TestConnectJob::kMockPendingJob, next_job_type); |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1570 | } |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1571 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1572 | |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1573 | // Tests the case where a second socket is requested in a completion callback, |
| 1574 | // and the second socket connects asynchronously. Reuses the same |
| 1575 | // ClientSocketHandle for the second socket, after disconnecting the first. |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1576 | TEST_F(ClientSocketPoolBaseTest, RequestPendingJobTwice) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1577 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1578 | |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 1579 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 1580 | ClientSocketHandle handle; |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1581 | TestCompletionCallback second_result_callback; |
| 1582 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1583 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Paul Jensen | 8d6f87ec | 2018-01-13 00:46:54 | [diff] [blame] | 1584 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1585 | base::BindOnce(&RequestSocketOnComplete, &handle, pool_.get(), |
| 1586 | connect_job_factory_, TestConnectJob::kMockPendingJob, |
| 1587 | &second_result_callback), |
| 1588 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1589 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1590 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1591 | EXPECT_THAT(second_result_callback.WaitForResult(), IsOk()); |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1592 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1593 | |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1594 | // Tests the case where a second socket is requested in a completion callback, |
| 1595 | // and the second socket connects synchronously. Reuses the same |
| 1596 | // ClientSocketHandle for the second socket, after disconnecting the first. |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1597 | TEST_F(ClientSocketPoolBaseTest, RequestPendingJobThenSynchronous) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1598 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1599 | |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 1600 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 1601 | ClientSocketHandle handle; |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1602 | TestCompletionCallback second_result_callback; |
| 1603 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1604 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Paul Jensen | 8d6f87ec | 2018-01-13 00:46:54 | [diff] [blame] | 1605 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1606 | base::BindOnce(&RequestSocketOnComplete, &handle, pool_.get(), |
| 1607 | connect_job_factory_, TestConnectJob::kMockPendingJob, |
| 1608 | &second_result_callback), |
| 1609 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1610 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1611 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1612 | EXPECT_THAT(second_result_callback.WaitForResult(), IsOk()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1613 | } |
| 1614 | |
| 1615 | // Make sure that pending requests get serviced after active requests get |
| 1616 | // cancelled. |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1617 | TEST_F(ClientSocketPoolBaseTest, CancelActiveRequestWithPendingRequests) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1618 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1619 | |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 1620 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1621 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1622 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1623 | IsError(ERR_IO_PENDING)); |
| 1624 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1625 | IsError(ERR_IO_PENDING)); |
| 1626 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1627 | IsError(ERR_IO_PENDING)); |
| 1628 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1629 | IsError(ERR_IO_PENDING)); |
| 1630 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1631 | IsError(ERR_IO_PENDING)); |
| 1632 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1633 | IsError(ERR_IO_PENDING)); |
| 1634 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1635 | IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1636 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1637 | // Now, kDefaultMaxSocketsPerGroup requests should be active. |
| 1638 | // Let's cancel them. |
| 1639 | for (int i = 0; i < kDefaultMaxSocketsPerGroup; ++i) { |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1640 | ASSERT_FALSE(request(i)->handle()->is_initialized()); |
| 1641 | request(i)->handle()->Reset(); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1642 | } |
| 1643 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1644 | // Let's wait for the rest to complete now. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1645 | for (size_t i = kDefaultMaxSocketsPerGroup; i < requests_size(); ++i) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1646 | EXPECT_THAT(request(i)->WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1647 | request(i)->handle()->Reset(); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1648 | } |
| 1649 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1650 | EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup, |
| 1651 | completion_count()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1652 | } |
| 1653 | |
| 1654 | // Make sure that pending requests get serviced after active requests fail. |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1655 | TEST_F(ClientSocketPoolBaseTest, FailingActiveRequestWithPendingRequests) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1656 | const size_t kMaxSockets = 5; |
| 1657 | CreatePool(kMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1658 | |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 1659 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1660 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1661 | const size_t kNumberOfRequests = 2 * kDefaultMaxSocketsPerGroup + 1; |
| 1662 | ASSERT_LE(kNumberOfRequests, kMaxSockets); // Otherwise the test will hang. |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1663 | |
| 1664 | // Queue up all the requests |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1665 | for (size_t i = 0; i < kNumberOfRequests; ++i) |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1666 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1667 | IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1668 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1669 | for (size_t i = 0; i < kNumberOfRequests; ++i) |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1670 | EXPECT_THAT(request(i)->WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1671 | } |
| 1672 | |
mmenke | 9d72fe4 | 2017-05-18 22:36:07 | [diff] [blame] | 1673 | // Make sure that pending requests that complete synchronously get serviced |
| 1674 | // after active requests fail. See https://crbug.com/723748 |
| 1675 | TEST_F(ClientSocketPoolBaseTest, HandleMultipleSyncFailuresAfterAsyncFailure) { |
| 1676 | const size_t kNumberOfRequests = 10; |
| 1677 | const size_t kMaxSockets = 1; |
| 1678 | CreatePool(kMaxSockets, kMaxSockets); |
| 1679 | |
| 1680 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
| 1681 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1682 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1683 | IsError(ERR_IO_PENDING)); |
mmenke | 9d72fe4 | 2017-05-18 22:36:07 | [diff] [blame] | 1684 | |
| 1685 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
| 1686 | |
| 1687 | // Queue up all the other requests |
| 1688 | for (size_t i = 1; i < kNumberOfRequests; ++i) |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1689 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1690 | IsError(ERR_IO_PENDING)); |
mmenke | 9d72fe4 | 2017-05-18 22:36:07 | [diff] [blame] | 1691 | |
| 1692 | // Make sure all requests fail, instead of hanging. |
| 1693 | for (size_t i = 0; i < kNumberOfRequests; ++i) |
| 1694 | EXPECT_THAT(request(i)->WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 1695 | } |
| 1696 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1697 | TEST_F(ClientSocketPoolBaseTest, CancelActiveRequestThenRequestSocket) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1698 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1699 | |
| 1700 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1701 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1702 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1703 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1704 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1705 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1706 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1707 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1708 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1709 | |
| 1710 | // Cancel the active request. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1711 | handle.Reset(); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1712 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1713 | rv = handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 1714 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1715 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1716 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1717 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 1718 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1719 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1720 | EXPECT_FALSE(handle.is_reused()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 1721 | TestLoadTimingInfoConnectedNotReused(handle); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1722 | EXPECT_EQ(2, client_socket_factory_.allocation_count()); |
| 1723 | } |
| 1724 | |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1725 | TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketsForced) { |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1726 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1727 | ClientSocketHandle handle; |
| 1728 | TestCompletionCallback callback; |
| 1729 | BoundTestNetLog log; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1730 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1731 | TestGroupId("a"), params_, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1732 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1733 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1734 | EXPECT_THAT(rv, IsOk()); |
| 1735 | handle.Reset(); |
| 1736 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 1737 | pool_->CloseIdleSockets(); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1738 | } |
| 1739 | |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1740 | TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketsInGroupForced) { |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1741 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1742 | TestCompletionCallback callback; |
| 1743 | BoundTestNetLog log; |
| 1744 | ClientSocketHandle handle1; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1745 | int rv = handle1.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1746 | TestGroupId("a"), params_, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1747 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1748 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1749 | EXPECT_THAT(rv, IsOk()); |
| 1750 | ClientSocketHandle handle2; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1751 | rv = handle2.Init(TestGroupId("a"), params_, LOWEST, SocketTag(), |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1752 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1753 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1754 | pool_.get(), log.bound()); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1755 | ClientSocketHandle handle3; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1756 | rv = handle3.Init(TestGroupId("b"), params_, LOWEST, SocketTag(), |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1757 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1758 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1759 | pool_.get(), log.bound()); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1760 | EXPECT_THAT(rv, IsOk()); |
| 1761 | handle1.Reset(); |
| 1762 | handle2.Reset(); |
| 1763 | handle3.Reset(); |
| 1764 | EXPECT_EQ(3, pool_->IdleSocketCount()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1765 | pool_->CloseIdleSocketsInGroup(TestGroupId("a")); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1766 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1767 | } |
| 1768 | |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1769 | TEST_F(ClientSocketPoolBaseTest, CleanUpUnusableIdleSockets) { |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1770 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1771 | ClientSocketHandle handle; |
| 1772 | TestCompletionCallback callback; |
| 1773 | BoundTestNetLog log; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1774 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1775 | TestGroupId("a"), params_, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1776 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1777 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1778 | EXPECT_THAT(rv, IsOk()); |
| 1779 | StreamSocket* socket = handle.socket(); |
| 1780 | handle.Reset(); |
| 1781 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 1782 | |
| 1783 | // Disconnect socket now to make the socket unusable. |
| 1784 | socket->Disconnect(); |
| 1785 | ClientSocketHandle handle2; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1786 | rv = handle2.Init(TestGroupId("a"), params_, LOWEST, SocketTag(), |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1787 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1788 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1789 | pool_.get(), log.bound()); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1790 | EXPECT_THAT(rv, IsOk()); |
| 1791 | EXPECT_FALSE(handle2.is_reused()); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1792 | } |
| 1793 | |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1794 | // Regression test for http://crbug.com/17985. |
| 1795 | TEST_F(ClientSocketPoolBaseTest, GroupWithPendingRequestsIsNotEmpty) { |
| 1796 | const int kMaxSockets = 3; |
| 1797 | const int kMaxSocketsPerGroup = 2; |
| 1798 | CreatePool(kMaxSockets, kMaxSocketsPerGroup); |
| 1799 | |
[email protected] | ac790b4 | 2009-12-02 04:31:31 | [diff] [blame] | 1800 | const RequestPriority kHighPriority = HIGHEST; |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1801 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1802 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1803 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1804 | |
| 1805 | // This is going to be a pending request in an otherwise empty group. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1806 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1807 | IsError(ERR_IO_PENDING)); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1808 | |
| 1809 | // Reach the maximum socket limit. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1810 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1811 | |
| 1812 | // Create a stalled group with high priorities. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1813 | EXPECT_THAT(StartRequest(TestGroupId("c"), kHighPriority), |
| 1814 | IsError(ERR_IO_PENDING)); |
| 1815 | EXPECT_THAT(StartRequest(TestGroupId("c"), kHighPriority), |
| 1816 | IsError(ERR_IO_PENDING)); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1817 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1818 | // Release the first two sockets from TestGroupId("a"). Because this is a |
| 1819 | // keepalive, the first release will unblock the pending request for |
| 1820 | // TestGroupId("a"). The second release will unblock a request for "c", |
| 1821 | // because it is the next high priority socket. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1822 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
| 1823 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1824 | |
| 1825 | // Closing idle sockets should not get us into trouble, but in the bug |
| 1826 | // we were hitting a CHECK here. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1827 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1828 | pool_->CloseIdleSockets(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1829 | |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 1830 | // Run the released socket wakeups. |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 1831 | base::RunLoop().RunUntilIdle(); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1832 | } |
| 1833 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 1834 | TEST_F(ClientSocketPoolBaseTest, BasicAsynchronous) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1835 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1836 | |
| 1837 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1838 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1839 | TestCompletionCallback callback; |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 1840 | BoundTestNetLog log; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1841 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1842 | TestGroupId("a"), params_, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1843 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1844 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1845 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1846 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 1847 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 1848 | TestLoadTimingInfoNotConnected(handle); |
| 1849 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1850 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1851 | EXPECT_TRUE(handle.is_initialized()); |
| 1852 | EXPECT_TRUE(handle.socket()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 1853 | TestLoadTimingInfoConnectedNotReused(handle); |
| 1854 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1855 | handle.Reset(); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 1856 | TestLoadTimingInfoNotConnected(handle); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 1857 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 1858 | TestNetLogEntry::List entries; |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 1859 | log.GetEntries(&entries); |
| 1860 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1861 | EXPECT_EQ(5u, entries.size()); |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 1862 | EXPECT_TRUE(LogContainsEvent( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1863 | entries, 0, NetLogEventType::TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 1864 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1865 | EXPECT_TRUE(LogContainsBeginEvent(entries, 1, NetLogEventType::SOCKET_POOL)); |
| 1866 | EXPECT_TRUE(LogContainsEvent( |
| 1867 | entries, 2, NetLogEventType::SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
| 1868 | NetLogEventPhase::NONE)); |
| 1869 | EXPECT_TRUE(LogContainsEvent(entries, 3, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 1870 | NetLogEventType::SOCKET_POOL_BOUND_TO_SOCKET, |
| 1871 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1872 | EXPECT_TRUE(LogContainsEndEvent(entries, 4, NetLogEventType::SOCKET_POOL)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1873 | } |
| 1874 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 1875 | TEST_F(ClientSocketPoolBaseTest, |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1876 | InitConnectionAsynchronousFailure) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1877 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1878 | |
| 1879 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1880 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1881 | TestCompletionCallback callback; |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 1882 | BoundTestNetLog log; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 1883 | // Set the additional error state members to ensure that they get cleared. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1884 | handle.set_is_ssl_error(true); |
[email protected] | 8b49869 | 2010-07-16 17:11:43 | [diff] [blame] | 1885 | HttpResponseInfo info; |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 1886 | info.headers = new HttpResponseHeaders(std::string()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1887 | handle.set_ssl_error_response_info(info); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1888 | EXPECT_EQ( |
| 1889 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1890 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1891 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1892 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 1893 | log.bound())); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1894 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 1895 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1896 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1897 | EXPECT_FALSE(handle.is_ssl_error()); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 1898 | EXPECT_TRUE(handle.ssl_error_response_info().headers.get() == nullptr); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 1899 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 1900 | TestNetLogEntry::List entries; |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 1901 | log.GetEntries(&entries); |
| 1902 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1903 | EXPECT_EQ(4u, entries.size()); |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 1904 | EXPECT_TRUE(LogContainsEvent( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1905 | entries, 0, NetLogEventType::TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 1906 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1907 | EXPECT_TRUE(LogContainsBeginEvent(entries, 1, NetLogEventType::SOCKET_POOL)); |
| 1908 | EXPECT_TRUE(LogContainsEvent( |
| 1909 | entries, 2, NetLogEventType::SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
| 1910 | NetLogEventPhase::NONE)); |
| 1911 | EXPECT_TRUE(LogContainsEndEvent(entries, 3, NetLogEventType::SOCKET_POOL)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1912 | } |
| 1913 | |
mmenke | 6be122f | 2015-03-09 22:22:47 | [diff] [blame] | 1914 | // Check that an async ConnectJob failure does not result in creation of a new |
| 1915 | // ConnectJob when there's another pending request also waiting on its own |
| 1916 | // ConnectJob. See http://crbug.com/463960. |
| 1917 | TEST_F(ClientSocketPoolBaseTest, AsyncFailureWithPendingRequestWithJob) { |
| 1918 | CreatePool(2, 2); |
| 1919 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
| 1920 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1921 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1922 | IsError(ERR_IO_PENDING)); |
| 1923 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1924 | IsError(ERR_IO_PENDING)); |
mmenke | 6be122f | 2015-03-09 22:22:47 | [diff] [blame] | 1925 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1926 | EXPECT_THAT(request(0)->WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 1927 | EXPECT_THAT(request(1)->WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
mmenke | 6be122f | 2015-03-09 22:22:47 | [diff] [blame] | 1928 | |
| 1929 | EXPECT_EQ(2, client_socket_factory_.allocation_count()); |
| 1930 | } |
| 1931 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 1932 | TEST_F(ClientSocketPoolBaseTest, TwoRequestsCancelOne) { |
[email protected] | b22b516 | 2010-03-16 07:53:10 | [diff] [blame] | 1933 | // TODO(eroman): Add back the log expectations! Removed them because the |
| 1934 | // ordering is difficult, and some may fire during destructor. |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1935 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1936 | |
| 1937 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1938 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1939 | TestCompletionCallback callback; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1940 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1941 | TestCompletionCallback callback2; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1942 | |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1943 | EXPECT_EQ( |
| 1944 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1945 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1946 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1947 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 1948 | NetLogWithSource())); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 1949 | BoundTestNetLog log2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1950 | EXPECT_EQ( |
| 1951 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1952 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1953 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1954 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1955 | pool_.get(), NetLogWithSource())); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1956 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1957 | handle.Reset(); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1958 | |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 1959 | |
| 1960 | // 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] | 1961 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1962 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1963 | handle2.Reset(); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 1964 | |
| 1965 | // Now request 2 has actually finished. |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 1966 | // TODO(eroman): Add back log expectations. |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1967 | } |
| 1968 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 1969 | TEST_F(ClientSocketPoolBaseTest, CancelRequestLimitsJobs) { |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 1970 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1971 | |
[email protected] | 17a0c6c | 2009-08-04 00:07:04 | [diff] [blame] | 1972 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1973 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1974 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
| 1975 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 1976 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1977 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 1978 | |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 1979 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1980 | static_cast<int>( |
| 1981 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1982 | (*requests())[2]->handle()->Reset(); |
| 1983 | (*requests())[3]->handle()->Reset(); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 1984 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1985 | static_cast<int>( |
| 1986 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 1987 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1988 | (*requests())[1]->handle()->Reset(); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 1989 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1990 | static_cast<int>( |
| 1991 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 1992 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1993 | (*requests())[0]->handle()->Reset(); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 1994 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1995 | static_cast<int>( |
| 1996 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 1997 | } |
| 1998 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1999 | // When requests and ConnectJobs are not coupled, the request will get serviced |
| 2000 | // by whatever comes first. |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2001 | TEST_F(ClientSocketPoolBaseTest, ReleaseSockets) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2002 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2003 | |
| 2004 | // Start job 1 (async OK) |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 2005 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2006 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2007 | std::vector<TestSocketRequest*> request_order; |
| 2008 | size_t completion_count; // unused |
| 2009 | TestSocketRequest req1(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2010 | int rv = req1.handle()->Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2011 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2012 | ClientSocketPool::RespectLimits::ENABLED, req1.callback(), |
| 2013 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2014 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 2015 | EXPECT_THAT(req1.WaitForResult(), IsOk()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2016 | |
| 2017 | // Job 1 finished OK. Start job 2 (also async OK). Request 3 is pending |
| 2018 | // without a job. |
| 2019 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2020 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2021 | TestSocketRequest req2(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2022 | rv = req2.handle()->Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2023 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2024 | ClientSocketPool::RespectLimits::ENABLED, req2.callback(), |
| 2025 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2026 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2027 | TestSocketRequest req3(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2028 | rv = req3.handle()->Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2029 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2030 | ClientSocketPool::RespectLimits::ENABLED, req3.callback(), |
| 2031 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2032 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2033 | |
| 2034 | // Both Requests 2 and 3 are pending. We release socket 1 which should |
| 2035 | // service request 2. Request 3 should still be waiting. |
[email protected] | a6c59f6 | 2009-07-29 16:33:33 | [diff] [blame] | 2036 | req1.handle()->Reset(); |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 2037 | // Run the released socket wakeups. |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 2038 | base::RunLoop().RunUntilIdle(); |
[email protected] | a6c59f6 | 2009-07-29 16:33:33 | [diff] [blame] | 2039 | ASSERT_TRUE(req2.handle()->socket()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2040 | EXPECT_THAT(req2.WaitForResult(), IsOk()); |
[email protected] | a6c59f6 | 2009-07-29 16:33:33 | [diff] [blame] | 2041 | EXPECT_FALSE(req3.handle()->socket()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2042 | |
| 2043 | // Signal job 2, which should service request 3. |
| 2044 | |
| 2045 | client_socket_factory_.SignalJobs(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2046 | EXPECT_THAT(req3.WaitForResult(), IsOk()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2047 | |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2048 | ASSERT_EQ(3u, request_order.size()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2049 | EXPECT_EQ(&req1, request_order[0]); |
| 2050 | EXPECT_EQ(&req2, request_order[1]); |
| 2051 | EXPECT_EQ(&req3, request_order[2]); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2052 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2053 | } |
| 2054 | |
| 2055 | // The requests are not coupled to the jobs. So, the requests should finish in |
| 2056 | // their priority / insertion order. |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2057 | TEST_F(ClientSocketPoolBaseTest, PendingJobCompletionOrder) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2058 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2059 | // First two jobs are async. |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 2060 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2061 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2062 | std::vector<TestSocketRequest*> request_order; |
| 2063 | size_t completion_count; // unused |
| 2064 | TestSocketRequest req1(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2065 | int rv = req1.handle()->Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2066 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2067 | ClientSocketPool::RespectLimits::ENABLED, req1.callback(), |
| 2068 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2069 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2070 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2071 | TestSocketRequest req2(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2072 | rv = req2.handle()->Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2073 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2074 | ClientSocketPool::RespectLimits::ENABLED, req2.callback(), |
| 2075 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2076 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2077 | |
| 2078 | // The pending job is sync. |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 2079 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2080 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2081 | TestSocketRequest req3(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2082 | rv = req3.handle()->Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2083 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2084 | ClientSocketPool::RespectLimits::ENABLED, req3.callback(), |
| 2085 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2086 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2087 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2088 | EXPECT_THAT(req1.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 2089 | EXPECT_THAT(req2.WaitForResult(), IsOk()); |
| 2090 | EXPECT_THAT(req3.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2091 | |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2092 | ASSERT_EQ(3u, request_order.size()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2093 | EXPECT_EQ(&req1, request_order[0]); |
| 2094 | EXPECT_EQ(&req2, request_order[1]); |
| 2095 | EXPECT_EQ(&req3, request_order[2]); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2096 | } |
| 2097 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2098 | // Test GetLoadState in the case there's only one socket request. |
| 2099 | TEST_F(ClientSocketPoolBaseTest, LoadStateOneRequest) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2100 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2101 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2102 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2103 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2104 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2105 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2106 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2107 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2108 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2109 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2110 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2111 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2112 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE); |
| 2113 | EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle.GetLoadState()); |
| 2114 | |
| 2115 | // No point in completing the connection, since ClientSocketHandles only |
| 2116 | // expect the LoadState to be checked while connecting. |
| 2117 | } |
| 2118 | |
| 2119 | // Test GetLoadState in the case there are two socket requests. |
| 2120 | TEST_F(ClientSocketPoolBaseTest, LoadStateTwoRequests) { |
| 2121 | CreatePool(2, 2); |
| 2122 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2123 | |
| 2124 | ClientSocketHandle handle; |
| 2125 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2126 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2127 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2128 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2129 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2130 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2131 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_RESOLVING_HOST); |
| 2132 | |
| 2133 | ClientSocketHandle handle2; |
| 2134 | TestCompletionCallback callback2; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2135 | rv = handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 2136 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2137 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2138 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2139 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2140 | client_socket_factory_.SetJobLoadState(1, LOAD_STATE_RESOLVING_HOST); |
| 2141 | |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 2142 | // Each handle should reflect the state of its own job. |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2143 | EXPECT_EQ(LOAD_STATE_RESOLVING_HOST, handle.GetLoadState()); |
| 2144 | EXPECT_EQ(LOAD_STATE_RESOLVING_HOST, handle2.GetLoadState()); |
| 2145 | |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 2146 | // Update the state of the first job. |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2147 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_CONNECTING); |
| 2148 | |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 2149 | // Only the state of the first request should have changed. |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2150 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2151 | EXPECT_EQ(LOAD_STATE_RESOLVING_HOST, handle2.GetLoadState()); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 2152 | |
| 2153 | // Update the state of the second job. |
| 2154 | client_socket_factory_.SetJobLoadState(1, LOAD_STATE_SSL_HANDSHAKE); |
| 2155 | |
| 2156 | // Only the state of the second request should have changed. |
| 2157 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
| 2158 | EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle2.GetLoadState()); |
| 2159 | |
| 2160 | // Second job connects and the first request gets the socket. The |
| 2161 | // second handle switches to the state of the remaining ConnectJob. |
| 2162 | client_socket_factory_.SignalJob(1); |
| 2163 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 2164 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle2.GetLoadState()); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2165 | } |
| 2166 | |
| 2167 | // Test GetLoadState in the case the per-group limit is reached. |
| 2168 | TEST_F(ClientSocketPoolBaseTest, LoadStateGroupLimit) { |
| 2169 | CreatePool(2, 1); |
| 2170 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2171 | |
| 2172 | ClientSocketHandle handle; |
| 2173 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2174 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2175 | TestGroupId("a"), params_, MEDIUM, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2176 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2177 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2178 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2179 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
| 2180 | |
| 2181 | // Request another socket from the same pool, buth with a higher priority. |
| 2182 | // The first request should now be stalled at the socket group limit. |
| 2183 | ClientSocketHandle handle2; |
| 2184 | TestCompletionCallback callback2; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2185 | rv = handle2.Init(TestGroupId("a"), params_, HIGHEST, SocketTag(), |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 2186 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2187 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2188 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2189 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2190 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET, handle.GetLoadState()); |
| 2191 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle2.GetLoadState()); |
| 2192 | |
| 2193 | // The first handle should remain stalled as the other socket goes through |
| 2194 | // the connect process. |
| 2195 | |
| 2196 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE); |
| 2197 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET, handle.GetLoadState()); |
| 2198 | EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle2.GetLoadState()); |
| 2199 | |
| 2200 | client_socket_factory_.SignalJob(0); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2201 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2202 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET, handle.GetLoadState()); |
| 2203 | |
| 2204 | // Closing the second socket should cause the stalled handle to finally get a |
| 2205 | // ConnectJob. |
| 2206 | handle2.socket()->Disconnect(); |
| 2207 | handle2.Reset(); |
| 2208 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
| 2209 | } |
| 2210 | |
| 2211 | // Test GetLoadState in the case the per-pool limit is reached. |
| 2212 | TEST_F(ClientSocketPoolBaseTest, LoadStatePoolLimit) { |
| 2213 | CreatePool(2, 2); |
| 2214 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2215 | |
| 2216 | ClientSocketHandle handle; |
| 2217 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2218 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2219 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2220 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2221 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2222 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2223 | |
| 2224 | // Request for socket from another pool. |
| 2225 | ClientSocketHandle handle2; |
| 2226 | TestCompletionCallback callback2; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2227 | rv = handle2.Init(TestGroupId("b"), params_, DEFAULT_PRIORITY, SocketTag(), |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 2228 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2229 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2230 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2231 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2232 | |
| 2233 | // Request another socket from the first pool. Request should stall at the |
| 2234 | // socket pool limit. |
| 2235 | ClientSocketHandle handle3; |
| 2236 | TestCompletionCallback callback3; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2237 | rv = handle3.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 2238 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2239 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2240 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2241 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2242 | |
| 2243 | // The third handle should remain stalled as the other sockets in its group |
| 2244 | // goes through the connect process. |
| 2245 | |
| 2246 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
| 2247 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL, handle3.GetLoadState()); |
| 2248 | |
| 2249 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE); |
| 2250 | EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle.GetLoadState()); |
| 2251 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL, handle3.GetLoadState()); |
| 2252 | |
| 2253 | client_socket_factory_.SignalJob(0); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2254 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2255 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL, handle3.GetLoadState()); |
| 2256 | |
| 2257 | // Closing a socket should allow the stalled handle to finally get a new |
| 2258 | // ConnectJob. |
| 2259 | handle.socket()->Disconnect(); |
| 2260 | handle.Reset(); |
| 2261 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle3.GetLoadState()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2262 | } |
| 2263 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2264 | TEST_F(ClientSocketPoolBaseTest, CertError) { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2265 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2266 | connect_job_factory_->set_job_type(TestConnectJob::kMockCertErrorJob); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2267 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2268 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2269 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2270 | EXPECT_EQ( |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2271 | ERR_CERT_COMMON_NAME_INVALID, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2272 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2273 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2274 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2275 | NetLogWithSource())); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2276 | EXPECT_TRUE(handle.is_initialized()); |
| 2277 | EXPECT_TRUE(handle.socket()); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2278 | } |
| 2279 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2280 | TEST_F(ClientSocketPoolBaseTest, AsyncCertError) { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2281 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2282 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2283 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingCertErrorJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2284 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2285 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2286 | EXPECT_EQ( |
| 2287 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2288 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2289 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2290 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2291 | NetLogWithSource())); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2292 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2293 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2294 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_CERT_COMMON_NAME_INVALID)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2295 | EXPECT_TRUE(handle.is_initialized()); |
| 2296 | EXPECT_TRUE(handle.socket()); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2297 | } |
| 2298 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2299 | TEST_F(ClientSocketPoolBaseTest, AdditionalErrorStateSynchronous) { |
| 2300 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2301 | connect_job_factory_->set_job_type( |
| 2302 | TestConnectJob::kMockAdditionalErrorStateJob); |
| 2303 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2304 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2305 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2306 | EXPECT_EQ( |
| 2307 | ERR_CONNECTION_FAILED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2308 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2309 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2310 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2311 | NetLogWithSource())); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2312 | EXPECT_FALSE(handle.is_initialized()); |
| 2313 | EXPECT_FALSE(handle.socket()); |
| 2314 | EXPECT_TRUE(handle.is_ssl_error()); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 2315 | EXPECT_FALSE(handle.ssl_error_response_info().headers.get() == nullptr); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2316 | } |
| 2317 | |
| 2318 | TEST_F(ClientSocketPoolBaseTest, AdditionalErrorStateAsynchronous) { |
| 2319 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2320 | |
| 2321 | connect_job_factory_->set_job_type( |
| 2322 | TestConnectJob::kMockPendingAdditionalErrorStateJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2323 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2324 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2325 | EXPECT_EQ( |
| 2326 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2327 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2328 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2329 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2330 | NetLogWithSource())); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2331 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2332 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2333 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2334 | EXPECT_FALSE(handle.is_initialized()); |
| 2335 | EXPECT_FALSE(handle.socket()); |
| 2336 | EXPECT_TRUE(handle.is_ssl_error()); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 2337 | EXPECT_FALSE(handle.ssl_error_response_info().headers.get() == nullptr); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2338 | } |
| 2339 | |
martijn | 003cd61 | 2016-05-19 22:24:38 | [diff] [blame] | 2340 | // Make sure we can reuse sockets. |
| 2341 | TEST_F(ClientSocketPoolBaseTest, CleanupTimedOutIdleSocketsReuse) { |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2342 | CreatePoolWithIdleTimeouts( |
| 2343 | kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2344 | base::TimeDelta(), // Time out unused sockets immediately. |
| 2345 | base::TimeDelta::FromDays(1)); // Don't time out used sockets. |
| 2346 | |
| 2347 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2348 | |
| 2349 | ClientSocketHandle handle; |
| 2350 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2351 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2352 | TestGroupId("a"), params_, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2353 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2354 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2355 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2356 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2357 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2358 | ASSERT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2359 | |
| 2360 | // Use and release the socket. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 2361 | EXPECT_EQ(1, handle.socket()->Write(nullptr, 1, CompletionOnceCallback(), |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 2362 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 2363 | TestLoadTimingInfoConnectedNotReused(handle); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2364 | handle.Reset(); |
| 2365 | |
| 2366 | // Should now have one idle socket. |
| 2367 | ASSERT_EQ(1, pool_->IdleSocketCount()); |
| 2368 | |
| 2369 | // Request a new socket. This should reuse the old socket and complete |
| 2370 | // synchronously. |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 2371 | BoundTestNetLog log; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2372 | rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2373 | TestGroupId("a"), params_, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2374 | ClientSocketPool::RespectLimits::ENABLED, CompletionOnceCallback(), |
| 2375 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2376 | ASSERT_THAT(rv, IsOk()); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2377 | EXPECT_TRUE(handle.is_reused()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 2378 | TestLoadTimingInfoConnectedReused(handle); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2379 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2380 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2381 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2382 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2383 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 2384 | TestNetLogEntry::List entries; |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2385 | log.GetEntries(&entries); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2386 | EXPECT_TRUE(LogContainsEvent( |
| 2387 | entries, 0, NetLogEventType::TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET, |
| 2388 | NetLogEventPhase::NONE)); |
| 2389 | EXPECT_TRUE(LogContainsBeginEvent(entries, 1, NetLogEventType::SOCKET_POOL)); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2390 | EXPECT_TRUE(LogContainsEntryWithType( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2391 | entries, 2, NetLogEventType::SOCKET_POOL_REUSED_AN_EXISTING_SOCKET)); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2392 | } |
| 2393 | |
martijn | 003cd61 | 2016-05-19 22:24:38 | [diff] [blame] | 2394 | // Make sure we cleanup old unused sockets. |
Eric Roman | b49715e | 2018-04-24 22:41:17 | [diff] [blame] | 2395 | TEST_F(ClientSocketPoolBaseTest, CleanupTimedOutIdleSocketsNoReuse) { |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2396 | CreatePoolWithIdleTimeouts( |
| 2397 | kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 2398 | base::TimeDelta(), // Time out unused sockets immediately |
| 2399 | base::TimeDelta()); // Time out used sockets immediately |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2400 | |
| 2401 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2402 | |
| 2403 | // Startup two mock pending connect jobs, which will sit in the MessageLoop. |
| 2404 | |
| 2405 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2406 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2407 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2408 | TestGroupId("a"), params_, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2409 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2410 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2411 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2412 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2413 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2414 | |
| 2415 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2416 | TestCompletionCallback callback2; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2417 | rv = handle2.Init(TestGroupId("a"), params_, LOWEST, SocketTag(), |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 2418 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2419 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2420 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2421 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2422 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2423 | pool_->GetLoadState(TestGroupId("a"), &handle2)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2424 | |
| 2425 | // Cancel one of the requests. Wait for the other, which will get the first |
| 2426 | // job. Release the socket. Run the loop again to make sure the second |
| 2427 | // socket is sitting idle and the first one is released (since ReleaseSocket() |
| 2428 | // just posts a DoReleaseSocket() task). |
| 2429 | |
| 2430 | handle.Reset(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2431 | ASSERT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2432 | // Use the socket. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 2433 | EXPECT_EQ(1, handle2.socket()->Write(nullptr, 1, CompletionOnceCallback(), |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 2434 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2435 | handle2.Reset(); |
| 2436 | |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2437 | // We post all of our delayed tasks with a 2ms delay. I.e. they don't |
| 2438 | // actually become pending until 2ms after they have been created. In order |
| 2439 | // to flush all tasks, we need to wait so that we know there are no |
| 2440 | // soon-to-be-pending tasks waiting. |
Alex Clarke | 0def209 | 2018-12-10 12:01:45 | [diff] [blame] | 2441 | FastForwardBy(base::TimeDelta::FromMilliseconds(10)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2442 | |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2443 | // Both sockets should now be idle. |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2444 | ASSERT_EQ(2, pool_->IdleSocketCount()); |
| 2445 | |
| 2446 | // Request a new socket. This should cleanup the unused and timed out ones. |
| 2447 | // A new socket will be created rather than reusing the idle one. |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 2448 | BoundTestNetLog log; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2449 | TestCompletionCallback callback3; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2450 | rv = handle.Init(TestGroupId("a"), params_, LOWEST, SocketTag(), |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 2451 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2452 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2453 | pool_.get(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2454 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 2455 | ASSERT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2456 | EXPECT_FALSE(handle.is_reused()); |
| 2457 | |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2458 | // Make sure the idle socket is closed. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2459 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2460 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2461 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2462 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 2463 | TestNetLogEntry::List entries; |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2464 | log.GetEntries(&entries); |
| 2465 | EXPECT_FALSE(LogContainsEntryWithType( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 2466 | entries, 1, NetLogEventType::SOCKET_POOL_REUSED_AN_EXISTING_SOCKET)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2467 | } |
| 2468 | |
[email protected] | 2041cf34 | 2010-02-19 03:15:59 | [diff] [blame] | 2469 | // Make sure that we process all pending requests even when we're stalling |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2470 | // because of multiple releasing disconnected sockets. |
| 2471 | TEST_F(ClientSocketPoolBaseTest, MultipleReleasingDisconnectedSockets) { |
| 2472 | CreatePoolWithIdleTimeouts( |
| 2473 | kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 2474 | base::TimeDelta(), // Time out unused sockets immediately. |
| 2475 | base::TimeDelta::FromDays(1)); // Don't time out used sockets. |
| 2476 | |
| 2477 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 2478 | |
| 2479 | // Startup 4 connect jobs. Two of them will be pending. |
| 2480 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2481 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2482 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2483 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2484 | TestGroupId("a"), params_, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2485 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2486 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2487 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2488 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2489 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2490 | TestCompletionCallback callback2; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2491 | rv = handle2.Init(TestGroupId("a"), params_, LOWEST, SocketTag(), |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 2492 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2493 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2494 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2495 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2496 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2497 | ClientSocketHandle handle3; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2498 | TestCompletionCallback callback3; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2499 | rv = handle3.Init(TestGroupId("a"), params_, LOWEST, SocketTag(), |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 2500 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2501 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2502 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2503 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2504 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2505 | ClientSocketHandle handle4; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2506 | TestCompletionCallback callback4; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2507 | rv = handle4.Init(TestGroupId("a"), params_, LOWEST, SocketTag(), |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 2508 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2509 | callback4.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2510 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2511 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2512 | |
| 2513 | // Release two disconnected sockets. |
| 2514 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2515 | handle.socket()->Disconnect(); |
| 2516 | handle.Reset(); |
| 2517 | handle2.socket()->Disconnect(); |
| 2518 | handle2.Reset(); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2519 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2520 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2521 | EXPECT_FALSE(handle3.is_reused()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2522 | EXPECT_THAT(callback4.WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2523 | EXPECT_FALSE(handle4.is_reused()); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2524 | } |
| 2525 | |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2526 | // Regression test for http://crbug.com/42267. |
| 2527 | // When DoReleaseSocket() is processed for one socket, it is blocked because the |
| 2528 | // other stalled groups all have releasing sockets, so no progress can be made. |
| 2529 | TEST_F(ClientSocketPoolBaseTest, SocketLimitReleasingSockets) { |
| 2530 | CreatePoolWithIdleTimeouts( |
| 2531 | 4 /* socket limit */, 4 /* socket limit per group */, |
| 2532 | base::TimeDelta(), // Time out unused sockets immediately. |
| 2533 | base::TimeDelta::FromDays(1)); // Don't time out used sockets. |
| 2534 | |
| 2535 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 2536 | |
| 2537 | // Max out the socket limit with 2 per group. |
| 2538 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2539 | ClientSocketHandle handle_a[4]; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2540 | TestCompletionCallback callback_a[4]; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2541 | ClientSocketHandle handle_b[4]; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2542 | TestCompletionCallback callback_b[4]; |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2543 | |
| 2544 | for (int i = 0; i < 2; ++i) { |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2545 | EXPECT_EQ( |
| 2546 | OK, handle_a[i].Init(TestGroupId("a"), params_, LOWEST, SocketTag(), |
| 2547 | ClientSocketPool::RespectLimits::ENABLED, |
| 2548 | callback_a[i].callback(), |
| 2549 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2550 | NetLogWithSource())); |
| 2551 | EXPECT_EQ( |
| 2552 | OK, handle_b[i].Init(TestGroupId("b"), params_, LOWEST, SocketTag(), |
| 2553 | ClientSocketPool::RespectLimits::ENABLED, |
| 2554 | callback_b[i].callback(), |
| 2555 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2556 | NetLogWithSource())); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2557 | } |
[email protected] | b89f7e4 | 2010-05-20 20:37:00 | [diff] [blame] | 2558 | |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2559 | // Make 4 pending requests, 2 per group. |
| 2560 | |
| 2561 | for (int i = 2; i < 4; ++i) { |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 2562 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2563 | handle_a[i].Init(TestGroupId("a"), params_, LOWEST, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 2564 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2565 | callback_a[i].callback(), |
| 2566 | ClientSocketPool::ProxyAuthCallback(), |
| 2567 | pool_.get(), NetLogWithSource())); |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 2568 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2569 | handle_b[i].Init(TestGroupId("b"), params_, LOWEST, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 2570 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2571 | callback_b[i].callback(), |
| 2572 | ClientSocketPool::ProxyAuthCallback(), |
| 2573 | pool_.get(), NetLogWithSource())); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2574 | } |
| 2575 | |
| 2576 | // Release b's socket first. The order is important, because in |
| 2577 | // DoReleaseSocket(), we'll process b's released socket, and since both b and |
| 2578 | // a are stalled, but 'a' is lower lexicographically, we'll process group 'a' |
| 2579 | // first, which has a releasing socket, so it refuses to start up another |
| 2580 | // ConnectJob. So, we used to infinite loop on this. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2581 | handle_b[0].socket()->Disconnect(); |
| 2582 | handle_b[0].Reset(); |
| 2583 | handle_a[0].socket()->Disconnect(); |
| 2584 | handle_a[0].Reset(); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2585 | |
| 2586 | // Used to get stuck here. |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 2587 | base::RunLoop().RunUntilIdle(); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2588 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2589 | handle_b[1].socket()->Disconnect(); |
| 2590 | handle_b[1].Reset(); |
| 2591 | handle_a[1].socket()->Disconnect(); |
| 2592 | handle_a[1].Reset(); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2593 | |
| 2594 | for (int i = 2; i < 4; ++i) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2595 | EXPECT_THAT(callback_b[i].WaitForResult(), IsOk()); |
| 2596 | EXPECT_THAT(callback_a[i].WaitForResult(), IsOk()); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2597 | } |
| 2598 | } |
| 2599 | |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2600 | TEST_F(ClientSocketPoolBaseTest, |
| 2601 | ReleasingDisconnectedSocketsMaintainsPriorityOrder) { |
| 2602 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2603 | |
| 2604 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2605 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2606 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2607 | IsError(ERR_IO_PENDING)); |
| 2608 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2609 | IsError(ERR_IO_PENDING)); |
| 2610 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2611 | IsError(ERR_IO_PENDING)); |
| 2612 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2613 | IsError(ERR_IO_PENDING)); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2614 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2615 | EXPECT_THAT((*requests())[0]->WaitForResult(), IsOk()); |
| 2616 | EXPECT_THAT((*requests())[1]->WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2617 | EXPECT_EQ(2u, completion_count()); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2618 | |
| 2619 | // Releases one connection. |
[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())[2]->WaitForResult(), IsOk()); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2622 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2623 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::NO_KEEP_ALIVE)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2624 | EXPECT_THAT((*requests())[3]->WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2625 | EXPECT_EQ(4u, completion_count()); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2626 | |
| 2627 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 2628 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 2629 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 2630 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 2631 | |
| 2632 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2633 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(5)); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2634 | } |
| 2635 | |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2636 | class TestReleasingSocketRequest : public TestCompletionCallbackBase { |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2637 | public: |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2638 | TestReleasingSocketRequest(TransportClientSocketPool* pool, |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2639 | int expected_result, |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2640 | bool reset_releasing_handle) |
| 2641 | : pool_(pool), |
| 2642 | expected_result_(expected_result), |
Bence Béky | 8ddc249 | 2018-06-13 01:02:04 | [diff] [blame] | 2643 | reset_releasing_handle_(reset_releasing_handle) {} |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2644 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 2645 | ~TestReleasingSocketRequest() override = default; |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2646 | |
| 2647 | ClientSocketHandle* handle() { return &handle_; } |
| 2648 | |
Bence Béky | 8ddc249 | 2018-06-13 01:02:04 | [diff] [blame] | 2649 | CompletionOnceCallback callback() { |
| 2650 | return base::BindOnce(&TestReleasingSocketRequest::OnComplete, |
| 2651 | base::Unretained(this)); |
| 2652 | } |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2653 | |
| 2654 | private: |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2655 | void OnComplete(int result) { |
| 2656 | SetResult(result); |
| 2657 | if (reset_releasing_handle_) |
| 2658 | handle_.Reset(); |
| 2659 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2660 | EXPECT_EQ( |
| 2661 | expected_result_, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2662 | handle2_.Init(TestGroupId("a"), CreateDummyParams(), DEFAULT_PRIORITY, |
| 2663 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2664 | CompletionOnceCallback(), |
| 2665 | ClientSocketPool::ProxyAuthCallback(), pool_, |
| 2666 | NetLogWithSource())); |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2667 | } |
| 2668 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2669 | TransportClientSocketPool* const pool_; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2670 | int expected_result_; |
| 2671 | bool reset_releasing_handle_; |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2672 | ClientSocketHandle handle_; |
| 2673 | ClientSocketHandle handle2_; |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2674 | }; |
| 2675 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2676 | |
| 2677 | TEST_F(ClientSocketPoolBaseTest, AdditionalErrorSocketsDontUseSlot) { |
| 2678 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2679 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2680 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
| 2681 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 2682 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2683 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2684 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2685 | client_socket_factory_.allocation_count()); |
| 2686 | |
| 2687 | connect_job_factory_->set_job_type( |
| 2688 | TestConnectJob::kMockPendingAdditionalErrorStateJob); |
| 2689 | TestReleasingSocketRequest req(pool_.get(), OK, false); |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 2690 | EXPECT_EQ( |
| 2691 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2692 | req.handle()->Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, |
| 2693 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2694 | req.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2695 | pool_.get(), NetLogWithSource())); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2696 | // The next job should complete synchronously |
| 2697 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 2698 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2699 | EXPECT_THAT(req.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2700 | EXPECT_FALSE(req.handle()->is_initialized()); |
| 2701 | EXPECT_FALSE(req.handle()->socket()); |
| 2702 | EXPECT_TRUE(req.handle()->is_ssl_error()); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 2703 | EXPECT_FALSE(req.handle()->ssl_error_response_info().headers.get() == |
| 2704 | nullptr); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2705 | } |
| 2706 | |
[email protected] | b6501d3d | 2010-06-03 23:53:34 | [diff] [blame] | 2707 | // http://crbug.com/44724 regression test. |
| 2708 | // We start releasing the pool when we flush on network change. When that |
| 2709 | // happens, the only active references are in the ClientSocketHandles. When a |
| 2710 | // ConnectJob completes and calls back into the last ClientSocketHandle, that |
| 2711 | // callback can release the last reference and delete the pool. After the |
| 2712 | // callback finishes, we go back to the stack frame within the now-deleted pool. |
| 2713 | // Executing any code that refers to members of the now-deleted pool can cause |
| 2714 | // crashes. |
| 2715 | TEST_F(ClientSocketPoolBaseTest, CallbackThatReleasesPool) { |
| 2716 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2717 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
| 2718 | |
| 2719 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2720 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2721 | EXPECT_EQ( |
| 2722 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2723 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2724 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2725 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2726 | NetLogWithSource())); |
[email protected] | b6501d3d | 2010-06-03 23:53:34 | [diff] [blame] | 2727 | |
[email protected] | 7af985a | 2012-12-14 22:40:42 | [diff] [blame] | 2728 | pool_->FlushWithError(ERR_NETWORK_CHANGED); |
[email protected] | b6501d3d | 2010-06-03 23:53:34 | [diff] [blame] | 2729 | |
| 2730 | // We'll call back into this now. |
| 2731 | callback.WaitForResult(); |
| 2732 | } |
| 2733 | |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2734 | TEST_F(ClientSocketPoolBaseTest, DoNotReuseSocketAfterFlush) { |
| 2735 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2736 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2737 | |
| 2738 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2739 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2740 | EXPECT_EQ( |
| 2741 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2742 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2743 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2744 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2745 | NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2746 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2747 | EXPECT_EQ(ClientSocketHandle::UNUSED, handle.reuse_type()); |
| 2748 | |
[email protected] | 7af985a | 2012-12-14 22:40:42 | [diff] [blame] | 2749 | pool_->FlushWithError(ERR_NETWORK_CHANGED); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2750 | |
| 2751 | handle.Reset(); |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 2752 | base::RunLoop().RunUntilIdle(); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2753 | |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2754 | EXPECT_EQ( |
| 2755 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2756 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2757 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2758 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2759 | NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2760 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2761 | EXPECT_EQ(ClientSocketHandle::UNUSED, handle.reuse_type()); |
| 2762 | } |
| 2763 | |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2764 | class ConnectWithinCallback : public TestCompletionCallbackBase { |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2765 | public: |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2766 | ConnectWithinCallback( |
| 2767 | const ClientSocketPool::GroupId& group_id, |
Matt Menke | 84d11e56 | 2019-03-27 00:11:19 | [diff] [blame] | 2768 | const scoped_refptr<ClientSocketPool::SocketParams>& params, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2769 | TransportClientSocketPool* pool) |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2770 | : group_id_(group_id), params_(params), pool_(pool) {} |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2771 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 2772 | ~ConnectWithinCallback() override = default; |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2773 | |
| 2774 | int WaitForNestedResult() { |
| 2775 | return nested_callback_.WaitForResult(); |
| 2776 | } |
| 2777 | |
Bence Béky | 8ddc249 | 2018-06-13 01:02:04 | [diff] [blame] | 2778 | CompletionOnceCallback callback() { |
| 2779 | return base::BindOnce(&ConnectWithinCallback::OnComplete, |
| 2780 | base::Unretained(this)); |
| 2781 | } |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2782 | |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2783 | private: |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2784 | void OnComplete(int result) { |
| 2785 | SetResult(result); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2786 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2787 | handle_.Init(group_id_, params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2788 | ClientSocketPool::RespectLimits::ENABLED, |
| 2789 | nested_callback_.callback(), |
| 2790 | ClientSocketPool::ProxyAuthCallback(), pool_, |
| 2791 | NetLogWithSource())); |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2792 | } |
| 2793 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2794 | const ClientSocketPool::GroupId group_id_; |
Matt Menke | 84d11e56 | 2019-03-27 00:11:19 | [diff] [blame] | 2795 | const scoped_refptr<ClientSocketPool::SocketParams> params_; |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2796 | TransportClientSocketPool* const pool_; |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2797 | ClientSocketHandle handle_; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2798 | TestCompletionCallback nested_callback_; |
| 2799 | |
| 2800 | DISALLOW_COPY_AND_ASSIGN(ConnectWithinCallback); |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2801 | }; |
| 2802 | |
| 2803 | TEST_F(ClientSocketPoolBaseTest, AbortAllRequestsOnFlush) { |
| 2804 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2805 | |
| 2806 | // First job will be waiting until it gets aborted. |
| 2807 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2808 | |
| 2809 | ClientSocketHandle handle; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2810 | ConnectWithinCallback callback(TestGroupId("a"), params_, pool_.get()); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2811 | EXPECT_EQ( |
| 2812 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2813 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2814 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2815 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2816 | NetLogWithSource())); |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2817 | |
| 2818 | // Second job will be started during the first callback, and will |
| 2819 | // asynchronously complete with OK. |
| 2820 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 7af985a | 2012-12-14 22:40:42 | [diff] [blame] | 2821 | pool_->FlushWithError(ERR_NETWORK_CHANGED); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2822 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_NETWORK_CHANGED)); |
| 2823 | EXPECT_THAT(callback.WaitForNestedResult(), IsOk()); |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2824 | } |
| 2825 | |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 2826 | TEST_F(ClientSocketPoolBaseTest, BackupSocketWaitsForHostResolution) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2827 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets, |
| 2828 | true /* enable_backup_connect_jobs */); |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 2829 | |
| 2830 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2831 | ClientSocketHandle handle; |
| 2832 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2833 | EXPECT_EQ( |
| 2834 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2835 | handle.Init(TestGroupId("bar"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2836 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2837 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2838 | NetLogWithSource())); |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 2839 | // The backup timer fires but doesn't start a new ConnectJob while resolving |
| 2840 | // the hostname. |
| 2841 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_RESOLVING_HOST); |
| 2842 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
| 2843 | ClientSocketPool::kMaxConnectRetryIntervalMs * 100)); |
| 2844 | EXPECT_EQ(1, client_socket_factory_.allocation_count()); |
| 2845 | |
| 2846 | // Once the ConnectJob has finished resolving the hostname, the backup timer |
| 2847 | // will create a ConnectJob when it fires. |
| 2848 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_CONNECTING); |
| 2849 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
| 2850 | ClientSocketPool::kMaxConnectRetryIntervalMs)); |
| 2851 | EXPECT_EQ(2, client_socket_factory_.allocation_count()); |
| 2852 | } |
| 2853 | |
| 2854 | // Test that no backup socket is created when a ConnectJob connects before it |
| 2855 | // completes. |
| 2856 | TEST_F(ClientSocketPoolBaseTest, NoBackupSocketWhenConnected) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2857 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets, |
| 2858 | true /* enable_backup_connect_jobs */); |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 2859 | |
| 2860 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2861 | ClientSocketHandle handle; |
| 2862 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2863 | EXPECT_EQ( |
| 2864 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2865 | handle.Init(TestGroupId("bar"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2866 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2867 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2868 | NetLogWithSource())); |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 2869 | // The backup timer fires but doesn't start a new ConnectJob while resolving |
| 2870 | // the hostname. |
| 2871 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_RESOLVING_HOST); |
| 2872 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
| 2873 | ClientSocketPool::kMaxConnectRetryIntervalMs * 100)); |
| 2874 | EXPECT_EQ(1, client_socket_factory_.allocation_count()); |
| 2875 | |
| 2876 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE); |
| 2877 | client_socket_factory_.SetJobHasEstablishedConnection(0); |
| 2878 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
| 2879 | ClientSocketPool::kMaxConnectRetryIntervalMs * 100)); |
| 2880 | EXPECT_EQ(1, client_socket_factory_.allocation_count()); |
| 2881 | } |
| 2882 | |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 2883 | // Cancel a pending socket request while we're at max sockets, |
| 2884 | // and verify that the backup socket firing doesn't cause a crash. |
| 2885 | TEST_F(ClientSocketPoolBaseTest, BackupSocketCancelAtMaxSockets) { |
| 2886 | // Max 4 sockets globally, max 4 sockets per group. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2887 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets, |
| 2888 | true /* enable_backup_connect_jobs */); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 2889 | |
[email protected] | 4baaf9d | 2010-08-31 15:15:44 | [diff] [blame] | 2890 | // Create the first socket and set to ERR_IO_PENDING. This starts the backup |
| 2891 | // timer. |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 2892 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2893 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2894 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2895 | EXPECT_EQ( |
| 2896 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2897 | handle.Init(TestGroupId("bar"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2898 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2899 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2900 | NetLogWithSource())); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 2901 | |
| 2902 | // Start (MaxSockets - 1) connected sockets to reach max sockets. |
| 2903 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 2904 | ClientSocketHandle handles[kDefaultMaxSockets]; |
| 2905 | for (int i = 1; i < kDefaultMaxSockets; ++i) { |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2906 | TestCompletionCallback callback; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2907 | EXPECT_EQ(OK, handles[i].Init(TestGroupId("bar"), params_, DEFAULT_PRIORITY, |
| 2908 | SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 2909 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2910 | callback.callback(), |
| 2911 | ClientSocketPool::ProxyAuthCallback(), |
| 2912 | pool_.get(), NetLogWithSource())); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 2913 | } |
| 2914 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 2915 | base::RunLoop().RunUntilIdle(); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 2916 | |
| 2917 | // Cancel the pending request. |
| 2918 | handle.Reset(); |
| 2919 | |
| 2920 | // 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] | 2921 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
[email protected] | 26b997396 | 2012-01-28 00:57:00 | [diff] [blame] | 2922 | ClientSocketPool::kMaxConnectRetryIntervalMs / 2 * 3)); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 2923 | |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 2924 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 2925 | } |
| 2926 | |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 2927 | TEST_F(ClientSocketPoolBaseTest, CancelBackupSocketAfterCancelingAllRequests) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2928 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets, |
| 2929 | true /* enable_backup_connect_jobs */); |
[email protected] | 4baaf9d | 2010-08-31 15:15:44 | [diff] [blame] | 2930 | |
| 2931 | // Create the first socket and set to ERR_IO_PENDING. This starts the backup |
| 2932 | // timer. |
| 2933 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2934 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2935 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2936 | EXPECT_EQ( |
| 2937 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2938 | handle.Init(TestGroupId("bar"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2939 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2940 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2941 | NetLogWithSource())); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2942 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("bar"))); |
| 2943 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("bar"))); |
| 2944 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 2945 | TestGroupId("bar"))); |
| 2946 | EXPECT_EQ( |
| 2947 | 0u, pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("bar"))); |
[email protected] | 4baaf9d | 2010-08-31 15:15:44 | [diff] [blame] | 2948 | |
| 2949 | // Cancel the socket request. This should cancel the backup timer. Wait for |
| 2950 | // the backup time to see if it indeed got canceled. |
| 2951 | handle.Reset(); |
| 2952 | // 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] | 2953 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
[email protected] | 26b997396 | 2012-01-28 00:57:00 | [diff] [blame] | 2954 | ClientSocketPool::kMaxConnectRetryIntervalMs / 2 * 3)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2955 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("bar"))); |
| 2956 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("bar"))); |
[email protected] | 4baaf9d | 2010-08-31 15:15:44 | [diff] [blame] | 2957 | } |
| 2958 | |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 2959 | TEST_F(ClientSocketPoolBaseTest, CancelBackupSocketAfterFinishingAllRequests) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2960 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets, |
| 2961 | true /* enable_backup_connect_jobs */); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 2962 | |
| 2963 | // Create the first socket and set to ERR_IO_PENDING. This starts the backup |
| 2964 | // timer. |
| 2965 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2966 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2967 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2968 | EXPECT_EQ( |
| 2969 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2970 | handle.Init(TestGroupId("bar"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2971 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2972 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2973 | NetLogWithSource())); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 2974 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2975 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2976 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 2977 | EXPECT_EQ( |
| 2978 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2979 | handle2.Init(TestGroupId("bar"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 2980 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2981 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2982 | pool_.get(), NetLogWithSource())); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2983 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("bar"))); |
| 2984 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("bar"))); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 2985 | |
| 2986 | // Cancel request 1 and then complete request 2. With the requests finished, |
| 2987 | // the backup timer should be cancelled. |
| 2988 | handle.Reset(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2989 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 2990 | // 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] | 2991 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
[email protected] | 26b997396 | 2012-01-28 00:57:00 | [diff] [blame] | 2992 | ClientSocketPool::kMaxConnectRetryIntervalMs / 2 * 3)); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 2993 | } |
| 2994 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 2995 | // Test delayed socket binding for the case where we have two connects, |
| 2996 | // and while one is waiting on a connect, the other frees up. |
| 2997 | // The socket waiting on a connect should switch immediately to the freed |
| 2998 | // up socket. |
| 2999 | TEST_F(ClientSocketPoolBaseTest, DelayedSocketBindingWaitingForConnect) { |
| 3000 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3001 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3002 | |
| 3003 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3004 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3005 | EXPECT_EQ( |
| 3006 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3007 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3008 | ClientSocketPool::RespectLimits::ENABLED, |
| 3009 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3010 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3011 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3012 | |
| 3013 | // No idle sockets, no pending jobs. |
| 3014 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3015 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3016 | |
| 3017 | // Create a second socket to the same host, but this one will wait. |
| 3018 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3019 | ClientSocketHandle handle2; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3020 | EXPECT_EQ( |
| 3021 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3022 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3023 | ClientSocketPool::RespectLimits::ENABLED, |
| 3024 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3025 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3026 | // No idle sockets, and one connecting job. |
| 3027 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3028 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3029 | |
| 3030 | // Return the first handle to the pool. This will initiate the delayed |
| 3031 | // binding. |
| 3032 | handle1.Reset(); |
| 3033 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3034 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3035 | |
| 3036 | // Still no idle sockets, still one pending connect job. |
| 3037 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3038 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3039 | |
| 3040 | // The second socket connected, even though it was a Waiting Job. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3041 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3042 | |
| 3043 | // And we can see there is still one job waiting. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3044 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3045 | |
| 3046 | // Finally, signal the waiting Connect. |
| 3047 | client_socket_factory_.SignalJobs(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3048 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3049 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3050 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3051 | } |
| 3052 | |
| 3053 | // Test delayed socket binding when a group is at capacity and one |
| 3054 | // of the group's sockets frees up. |
| 3055 | TEST_F(ClientSocketPoolBaseTest, DelayedSocketBindingAtGroupCapacity) { |
| 3056 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3057 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3058 | |
| 3059 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3060 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3061 | EXPECT_EQ( |
| 3062 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3063 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3064 | ClientSocketPool::RespectLimits::ENABLED, |
| 3065 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3066 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3067 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3068 | |
| 3069 | // No idle sockets, no pending jobs. |
| 3070 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3071 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3072 | |
| 3073 | // Create a second socket to the same host, but this one will wait. |
| 3074 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3075 | ClientSocketHandle handle2; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3076 | EXPECT_EQ( |
| 3077 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3078 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3079 | ClientSocketPool::RespectLimits::ENABLED, |
| 3080 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3081 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3082 | // No idle sockets, and one connecting job. |
| 3083 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3084 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3085 | |
| 3086 | // Return the first handle to the pool. This will initiate the delayed |
| 3087 | // binding. |
| 3088 | handle1.Reset(); |
| 3089 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3090 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3091 | |
| 3092 | // Still no idle sockets, still one pending connect job. |
| 3093 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3094 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3095 | |
| 3096 | // The second socket connected, even though it was a Waiting Job. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3097 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3098 | |
| 3099 | // And we can see there is still one job waiting. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3100 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3101 | |
| 3102 | // Finally, signal the waiting Connect. |
| 3103 | client_socket_factory_.SignalJobs(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3104 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3105 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3106 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3107 | } |
| 3108 | |
| 3109 | // Test out the case where we have one socket connected, one |
| 3110 | // connecting, when the first socket finishes and goes idle. |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3111 | // Although the second connection is pending, the second request |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3112 | // should complete, by taking the first socket's idle socket. |
| 3113 | TEST_F(ClientSocketPoolBaseTest, DelayedSocketBindingAtStall) { |
| 3114 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3115 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3116 | |
| 3117 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3118 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3119 | EXPECT_EQ( |
| 3120 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3121 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3122 | ClientSocketPool::RespectLimits::ENABLED, |
| 3123 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3124 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3125 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3126 | |
| 3127 | // No idle sockets, no pending jobs. |
| 3128 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3129 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3130 | |
| 3131 | // Create a second socket to the same host, but this one will wait. |
| 3132 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3133 | ClientSocketHandle handle2; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3134 | EXPECT_EQ( |
| 3135 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3136 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3137 | ClientSocketPool::RespectLimits::ENABLED, |
| 3138 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3139 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3140 | // No idle sockets, and one connecting job. |
| 3141 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3142 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3143 | |
| 3144 | // Return the first handle to the pool. This will initiate the delayed |
| 3145 | // binding. |
| 3146 | handle1.Reset(); |
| 3147 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3148 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3149 | |
| 3150 | // Still no idle sockets, still one pending connect job. |
| 3151 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3152 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3153 | |
| 3154 | // The second socket connected, even though it was a Waiting Job. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3155 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3156 | |
| 3157 | // And we can see there is still one job waiting. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3158 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3159 | |
| 3160 | // Finally, signal the waiting Connect. |
| 3161 | client_socket_factory_.SignalJobs(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3162 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3163 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3164 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3165 | } |
| 3166 | |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3167 | // Cover the case where on an available socket slot, we have one pending |
| 3168 | // request that completes synchronously, thereby making the Group empty. |
| 3169 | TEST_F(ClientSocketPoolBaseTest, SynchronouslyProcessOnePendingRequest) { |
| 3170 | const int kUnlimitedSockets = 100; |
| 3171 | const int kOneSocketPerGroup = 1; |
| 3172 | CreatePool(kUnlimitedSockets, kOneSocketPerGroup); |
| 3173 | |
| 3174 | // Make the first request asynchronous fail. |
| 3175 | // This will free up a socket slot later. |
| 3176 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
| 3177 | |
| 3178 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3179 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3180 | EXPECT_EQ( |
| 3181 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3182 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3183 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3184 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3185 | pool_.get(), NetLogWithSource())); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3186 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3187 | |
| 3188 | // Make the second request synchronously fail. This should make the Group |
| 3189 | // empty. |
| 3190 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
| 3191 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3192 | TestCompletionCallback callback2; |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3193 | // It'll be ERR_IO_PENDING now, but the TestConnectJob will synchronously fail |
| 3194 | // when created. |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3195 | EXPECT_EQ( |
| 3196 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3197 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3198 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3199 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3200 | pool_.get(), NetLogWithSource())); |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3201 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3202 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3203 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3204 | EXPECT_THAT(callback1.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 3205 | EXPECT_THAT(callback2.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3206 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3207 | } |
| 3208 | |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3209 | TEST_F(ClientSocketPoolBaseTest, PreferUsedSocketToUnusedSocket) { |
| 3210 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 3211 | |
| 3212 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3213 | |
| 3214 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3215 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3216 | EXPECT_EQ( |
| 3217 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3218 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3219 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3220 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3221 | pool_.get(), NetLogWithSource())); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3222 | |
| 3223 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3224 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3225 | EXPECT_EQ( |
| 3226 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3227 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3228 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3229 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3230 | pool_.get(), NetLogWithSource())); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3231 | ClientSocketHandle handle3; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3232 | TestCompletionCallback callback3; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3233 | EXPECT_EQ( |
| 3234 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3235 | handle3.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3236 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3237 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3238 | pool_.get(), NetLogWithSource())); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3239 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3240 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3241 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
| 3242 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3243 | |
| 3244 | // Use the socket. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 3245 | EXPECT_EQ(1, handle1.socket()->Write(nullptr, 1, CompletionOnceCallback(), |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 3246 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 3247 | EXPECT_EQ(1, handle3.socket()->Write(nullptr, 1, CompletionOnceCallback(), |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 3248 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3249 | |
| 3250 | handle1.Reset(); |
| 3251 | handle2.Reset(); |
| 3252 | handle3.Reset(); |
| 3253 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3254 | EXPECT_EQ(OK, handle1.Init( |
| 3255 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
| 3256 | ClientSocketPool::RespectLimits::ENABLED, |
| 3257 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3258 | pool_.get(), NetLogWithSource())); |
| 3259 | EXPECT_EQ(OK, handle2.Init( |
| 3260 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
| 3261 | ClientSocketPool::RespectLimits::ENABLED, |
| 3262 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3263 | pool_.get(), NetLogWithSource())); |
| 3264 | EXPECT_EQ(OK, handle3.Init( |
| 3265 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
| 3266 | ClientSocketPool::RespectLimits::ENABLED, |
| 3267 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3268 | pool_.get(), NetLogWithSource())); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3269 | |
| 3270 | EXPECT_TRUE(handle1.socket()->WasEverUsed()); |
| 3271 | EXPECT_TRUE(handle2.socket()->WasEverUsed()); |
| 3272 | EXPECT_FALSE(handle3.socket()->WasEverUsed()); |
| 3273 | } |
| 3274 | |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3275 | TEST_F(ClientSocketPoolBaseTest, RequestSockets) { |
| 3276 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3277 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3278 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3279 | pool_->RequestSockets(TestGroupId("a"), params_, 2, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3280 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3281 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3282 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3283 | EXPECT_EQ(2u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3284 | TestGroupId("a"))); |
| 3285 | EXPECT_EQ(2u, |
| 3286 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3287 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3288 | |
| 3289 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3290 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3291 | EXPECT_EQ( |
| 3292 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3293 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3294 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3295 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3296 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3297 | |
| 3298 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3299 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3300 | EXPECT_EQ( |
| 3301 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3302 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3303 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3304 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3305 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3306 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3307 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3308 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3309 | TestGroupId("a"))); |
| 3310 | EXPECT_EQ(0u, |
| 3311 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3312 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3313 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3314 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3315 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3316 | handle1.Reset(); |
| 3317 | handle2.Reset(); |
| 3318 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3319 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3320 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3321 | TestGroupId("a"))); |
| 3322 | EXPECT_EQ(0u, |
| 3323 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3324 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3325 | } |
| 3326 | |
| 3327 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsWhenAlreadyHaveAConnectJob) { |
| 3328 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3329 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3330 | |
| 3331 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3332 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3333 | EXPECT_EQ( |
| 3334 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3335 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3336 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3337 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3338 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3339 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3340 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3341 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3342 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3343 | TestGroupId("a"))); |
| 3344 | EXPECT_EQ(0u, |
| 3345 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3346 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3347 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3348 | pool_->RequestSockets(TestGroupId("a"), params_, 2, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3349 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3350 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3351 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3352 | TestGroupId("a"))); |
| 3353 | EXPECT_EQ(1u, |
| 3354 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3355 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3356 | |
| 3357 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3358 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3359 | EXPECT_EQ( |
| 3360 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3361 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3362 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3363 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3364 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3365 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3366 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3367 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3368 | TestGroupId("a"))); |
| 3369 | EXPECT_EQ(0u, |
| 3370 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3371 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3372 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3373 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3374 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3375 | handle1.Reset(); |
| 3376 | handle2.Reset(); |
| 3377 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3378 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3379 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3380 | TestGroupId("a"))); |
| 3381 | EXPECT_EQ(0u, |
| 3382 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3383 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3384 | } |
| 3385 | |
| 3386 | TEST_F(ClientSocketPoolBaseTest, |
| 3387 | RequestSocketsWhenAlreadyHaveMultipleConnectJob) { |
| 3388 | CreatePool(4, 4); |
| 3389 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3390 | |
| 3391 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3392 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3393 | EXPECT_EQ( |
| 3394 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3395 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3396 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3397 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3398 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3399 | |
| 3400 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3401 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3402 | EXPECT_EQ( |
| 3403 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3404 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3405 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3406 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3407 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3408 | |
| 3409 | ClientSocketHandle handle3; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3410 | TestCompletionCallback callback3; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3411 | EXPECT_EQ( |
| 3412 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3413 | handle3.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3414 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3415 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3416 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3417 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3418 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3419 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3420 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3421 | TestGroupId("a"))); |
| 3422 | EXPECT_EQ(0u, |
| 3423 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3424 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3425 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3426 | pool_->RequestSockets(TestGroupId("a"), params_, 2, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3427 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3428 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3429 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3430 | TestGroupId("a"))); |
| 3431 | EXPECT_EQ(0u, |
| 3432 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3433 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3434 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3435 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3436 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
| 3437 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3438 | handle1.Reset(); |
| 3439 | handle2.Reset(); |
| 3440 | handle3.Reset(); |
| 3441 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3442 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3443 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3444 | TestGroupId("a"))); |
| 3445 | EXPECT_EQ(0u, |
| 3446 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3447 | EXPECT_EQ(3u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3448 | } |
| 3449 | |
| 3450 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsAtMaxSocketLimit) { |
| 3451 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 3452 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3453 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3454 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3455 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3456 | pool_->RequestSockets(TestGroupId("a"), params_, kDefaultMaxSockets, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3457 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3458 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3459 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3460 | EXPECT_EQ(kDefaultMaxSockets, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3461 | static_cast<int>( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3462 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
| 3463 | EXPECT_EQ( |
| 3464 | kDefaultMaxSockets, |
| 3465 | static_cast<int>(pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3466 | TestGroupId("a")))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3467 | EXPECT_EQ(kDefaultMaxSockets, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3468 | static_cast<int>(pool_->NumUnassignedConnectJobsInGroupForTesting( |
| 3469 | TestGroupId("a")))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3470 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3471 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("b"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3472 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3473 | pool_->RequestSockets(TestGroupId("b"), params_, kDefaultMaxSockets, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3474 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3475 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3476 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("b"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3477 | } |
| 3478 | |
| 3479 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsHitMaxSocketLimit) { |
| 3480 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 3481 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3482 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3483 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3484 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3485 | pool_->RequestSockets(TestGroupId("a"), params_, kDefaultMaxSockets - 1, |
Charlie Harrison | 55ce608 | 2018-05-14 02:25:57 | [diff] [blame] | 3486 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3487 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3488 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3489 | EXPECT_EQ(kDefaultMaxSockets - 1, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3490 | static_cast<int>( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3491 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
| 3492 | EXPECT_EQ( |
| 3493 | kDefaultMaxSockets - 1, |
| 3494 | static_cast<int>(pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3495 | TestGroupId("a")))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3496 | EXPECT_EQ(kDefaultMaxSockets - 1, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3497 | static_cast<int>(pool_->NumUnassignedConnectJobsInGroupForTesting( |
| 3498 | TestGroupId("a")))); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 3499 | EXPECT_FALSE(pool_->IsStalled()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3500 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3501 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("b"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3502 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3503 | pool_->RequestSockets(TestGroupId("b"), params_, kDefaultMaxSockets, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3504 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3505 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3506 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("b"))); |
| 3507 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 3508 | EXPECT_FALSE(pool_->IsStalled()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3509 | } |
| 3510 | |
| 3511 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsCountIdleSockets) { |
| 3512 | CreatePool(4, 4); |
| 3513 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3514 | |
| 3515 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3516 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3517 | EXPECT_EQ( |
| 3518 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3519 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3520 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3521 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3522 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3523 | ASSERT_THAT(callback1.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3524 | handle1.Reset(); |
| 3525 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3526 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3527 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3528 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3529 | TestGroupId("a"))); |
| 3530 | EXPECT_EQ(0u, |
| 3531 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3532 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3533 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3534 | pool_->RequestSockets(TestGroupId("a"), params_, 2, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3535 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3536 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3537 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3538 | TestGroupId("a"))); |
| 3539 | EXPECT_EQ(1u, |
| 3540 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3541 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3542 | } |
| 3543 | |
| 3544 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsCountActiveSockets) { |
| 3545 | CreatePool(4, 4); |
| 3546 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3547 | |
| 3548 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3549 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3550 | EXPECT_EQ( |
| 3551 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3552 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3553 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3554 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3555 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3556 | ASSERT_THAT(callback1.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3557 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3558 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3559 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3560 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3561 | TestGroupId("a"))); |
| 3562 | EXPECT_EQ(0u, |
| 3563 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3564 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3565 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3566 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3567 | pool_->RequestSockets(TestGroupId("a"), params_, 2, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3568 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3569 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3570 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3571 | TestGroupId("a"))); |
| 3572 | EXPECT_EQ(1u, |
| 3573 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3574 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3575 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3576 | } |
| 3577 | |
| 3578 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsSynchronous) { |
| 3579 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3580 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 3581 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3582 | pool_->RequestSockets(TestGroupId("a"), params_, kDefaultMaxSocketsPerGroup, |
Charlie Harrison | 55ce608 | 2018-05-14 02:25:57 | [diff] [blame] | 3583 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3584 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3585 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3586 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3587 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3588 | TestGroupId("a"))); |
| 3589 | EXPECT_EQ(0u, |
| 3590 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3591 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3592 | static_cast<int>(pool_->IdleSocketCountInGroup(TestGroupId("a")))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3593 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3594 | pool_->RequestSockets(TestGroupId("b"), params_, kDefaultMaxSocketsPerGroup, |
Charlie Harrison | 55ce608 | 2018-05-14 02:25:57 | [diff] [blame] | 3595 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3596 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3597 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 3598 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3599 | TestGroupId("b"))); |
| 3600 | EXPECT_EQ(0u, |
| 3601 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3602 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3603 | static_cast<int>(pool_->IdleSocketCountInGroup(TestGroupId("b")))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3604 | } |
| 3605 | |
[email protected] | 3c819f52 | 2010-12-02 02:03:12 | [diff] [blame] | 3606 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsSynchronousError) { |
| 3607 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3608 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
| 3609 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3610 | pool_->RequestSockets(TestGroupId("a"), params_, kDefaultMaxSocketsPerGroup, |
Charlie Harrison | 55ce608 | 2018-05-14 02:25:57 | [diff] [blame] | 3611 | NetLogWithSource()); |
[email protected] | 3c819f52 | 2010-12-02 02:03:12 | [diff] [blame] | 3612 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3613 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | fd2e53e | 2011-01-14 20:40:52 | [diff] [blame] | 3614 | |
| 3615 | connect_job_factory_->set_job_type( |
| 3616 | TestConnectJob::kMockAdditionalErrorStateJob); |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3617 | pool_->RequestSockets(TestGroupId("a"), params_, kDefaultMaxSocketsPerGroup, |
Charlie Harrison | 55ce608 | 2018-05-14 02:25:57 | [diff] [blame] | 3618 | NetLogWithSource()); |
[email protected] | fd2e53e | 2011-01-14 20:40:52 | [diff] [blame] | 3619 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3620 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | 3c819f52 | 2010-12-02 02:03:12 | [diff] [blame] | 3621 | } |
| 3622 | |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3623 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsMultipleTimesDoesNothing) { |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3624 | CreatePool(4, 4); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3625 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3626 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3627 | pool_->RequestSockets(TestGroupId("a"), params_, 2, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3628 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3629 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3630 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3631 | EXPECT_EQ(2u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3632 | TestGroupId("a"))); |
| 3633 | EXPECT_EQ(2u, |
| 3634 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3635 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3636 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3637 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3638 | pool_->RequestSockets(TestGroupId("a"), params_, 2, NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3639 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3640 | EXPECT_EQ(2u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3641 | TestGroupId("a"))); |
| 3642 | EXPECT_EQ(2u, |
| 3643 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3644 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3645 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3646 | |
| 3647 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3648 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3649 | EXPECT_EQ( |
| 3650 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3651 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3652 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3653 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3654 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3655 | |
| 3656 | client_socket_factory_.SignalJob(0); |
| 3657 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3658 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3659 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3660 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3661 | TestGroupId("a"))); |
| 3662 | EXPECT_EQ(1u, |
| 3663 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3664 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3665 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3666 | |
| 3667 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3668 | TestCompletionCallback callback2; |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3669 | EXPECT_EQ( |
| 3670 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3671 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3672 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3673 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3674 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3675 | client_socket_factory_.SignalJob(0); |
| 3676 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3677 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3678 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3679 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3680 | TestGroupId("a"))); |
| 3681 | EXPECT_EQ(0u, |
| 3682 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3683 | EXPECT_EQ(2, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3684 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3685 | |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3686 | handle1.Reset(); |
| 3687 | handle2.Reset(); |
| 3688 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3689 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3690 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3691 | TestGroupId("a"))); |
| 3692 | EXPECT_EQ(0u, |
| 3693 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3694 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3695 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3696 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3697 | pool_->RequestSockets(TestGroupId("a"), params_, 2, NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3698 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3699 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3700 | TestGroupId("a"))); |
| 3701 | EXPECT_EQ(0u, |
| 3702 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3703 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3704 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3705 | } |
| 3706 | |
| 3707 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsDifferentNumSockets) { |
| 3708 | CreatePool(4, 4); |
| 3709 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3710 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3711 | pool_->RequestSockets(TestGroupId("a"), params_, 1, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3712 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3713 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3714 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3715 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3716 | TestGroupId("a"))); |
| 3717 | EXPECT_EQ(1u, |
| 3718 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3719 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3720 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3721 | pool_->RequestSockets(TestGroupId("a"), params_, 2, NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3722 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3723 | EXPECT_EQ(2u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3724 | TestGroupId("a"))); |
| 3725 | EXPECT_EQ(2u, |
| 3726 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3727 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3728 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3729 | pool_->RequestSockets(TestGroupId("a"), params_, 3, NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3730 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3731 | EXPECT_EQ(3u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3732 | TestGroupId("a"))); |
| 3733 | EXPECT_EQ(3u, |
| 3734 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3735 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3736 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3737 | pool_->RequestSockets(TestGroupId("a"), params_, 1, NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3738 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3739 | EXPECT_EQ(3u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3740 | TestGroupId("a"))); |
| 3741 | EXPECT_EQ(3u, |
| 3742 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3743 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3744 | } |
| 3745 | |
| 3746 | TEST_F(ClientSocketPoolBaseTest, PreconnectJobsTakenByNormalRequests) { |
| 3747 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3748 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3749 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3750 | pool_->RequestSockets(TestGroupId("a"), params_, 1, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3751 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3752 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3753 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3754 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3755 | TestGroupId("a"))); |
| 3756 | EXPECT_EQ(1u, |
| 3757 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3758 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3759 | |
| 3760 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3761 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3762 | EXPECT_EQ( |
| 3763 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3764 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3765 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3766 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3767 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3768 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3769 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3770 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3771 | TestGroupId("a"))); |
| 3772 | EXPECT_EQ(0u, |
| 3773 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3774 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3775 | |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3776 | client_socket_factory_.SignalJobs(); |
| 3777 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3778 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3779 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3780 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3781 | TestGroupId("a"))); |
| 3782 | EXPECT_EQ(0u, |
| 3783 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3784 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3785 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3786 | |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 3787 | // Make sure if a preconnected socket is not fully connected when a request |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 3788 | // starts, it has a connect start time. |
| 3789 | TestLoadTimingInfoConnectedNotReused(handle1); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3790 | handle1.Reset(); |
| 3791 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3792 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3793 | } |
| 3794 | |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 3795 | // Checks that fully connected preconnect jobs have no connect times, and are |
| 3796 | // marked as reused. |
| 3797 | TEST_F(ClientSocketPoolBaseTest, ConnectedPreconnectJobsHaveNoConnectTimes) { |
| 3798 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3799 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3800 | pool_->RequestSockets(TestGroupId("a"), params_, 1, NetLogWithSource()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 3801 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3802 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3803 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3804 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3805 | TestGroupId("a"))); |
| 3806 | EXPECT_EQ(0u, |
| 3807 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3808 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 3809 | |
| 3810 | ClientSocketHandle handle; |
| 3811 | TestCompletionCallback callback; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3812 | EXPECT_EQ(OK, handle.Init( |
| 3813 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
| 3814 | ClientSocketPool::RespectLimits::ENABLED, |
| 3815 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3816 | pool_.get(), NetLogWithSource())); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 3817 | |
| 3818 | // Make sure the idle socket was used. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3819 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 3820 | |
| 3821 | TestLoadTimingInfoConnectedReused(handle); |
| 3822 | handle.Reset(); |
| 3823 | TestLoadTimingInfoNotConnected(handle); |
| 3824 | } |
| 3825 | |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3826 | // http://crbug.com/64940 regression test. |
| 3827 | TEST_F(ClientSocketPoolBaseTest, PreconnectClosesIdleSocketRemovesGroup) { |
| 3828 | const int kMaxTotalSockets = 3; |
| 3829 | const int kMaxSocketsPerGroup = 2; |
| 3830 | CreatePool(kMaxTotalSockets, kMaxSocketsPerGroup); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3831 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3832 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 3833 | // Note that group id ordering matters here. "a" comes before "b", so |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3834 | // CloseOneIdleSocket() will try to close "a"'s idle socket. |
| 3835 | |
| 3836 | // Set up one idle socket in "a". |
| 3837 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3838 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3839 | EXPECT_EQ( |
| 3840 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3841 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3842 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3843 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3844 | pool_.get(), NetLogWithSource())); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3845 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3846 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3847 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3848 | TestGroupId("a"))); |
| 3849 | EXPECT_EQ(0u, |
| 3850 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3851 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3852 | |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3853 | client_socket_factory_.SignalJobs(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3854 | ASSERT_THAT(callback1.WaitForResult(), IsOk()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3855 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3856 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3857 | TestGroupId("a"))); |
| 3858 | EXPECT_EQ(0u, |
| 3859 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3860 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3861 | |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3862 | handle1.Reset(); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3863 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3864 | |
| 3865 | // Set up two active sockets in "b". |
| 3866 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3867 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3868 | EXPECT_EQ( |
| 3869 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3870 | handle1.Init(TestGroupId("b"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3871 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3872 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3873 | pool_.get(), NetLogWithSource())); |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3874 | EXPECT_EQ( |
| 3875 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3876 | handle2.Init(TestGroupId("b"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3877 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3878 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3879 | pool_.get(), NetLogWithSource())); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3880 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3881 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("b"))); |
| 3882 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 3883 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3884 | TestGroupId("b"))); |
| 3885 | EXPECT_EQ(0u, |
| 3886 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3887 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3888 | |
| 3889 | client_socket_factory_.SignalJobs(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3890 | ASSERT_THAT(callback1.WaitForResult(), IsOk()); |
| 3891 | ASSERT_THAT(callback2.WaitForResult(), IsOk()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3892 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3893 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3894 | TestGroupId("b"))); |
| 3895 | EXPECT_EQ(0u, |
| 3896 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 3897 | EXPECT_EQ(2, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3898 | |
| 3899 | // Now we have 1 idle socket in "a" and 2 active sockets in "b". This means |
| 3900 | // we've maxed out on sockets, since we set |kMaxTotalSockets| to 3. |
| 3901 | // Requesting 2 preconnected sockets for "a" should fail to allocate any more |
| 3902 | // sockets for "a", and "b" should still have 2 active sockets. |
| 3903 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3904 | pool_->RequestSockets(TestGroupId("a"), params_, 2, NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3905 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3906 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3907 | TestGroupId("a"))); |
| 3908 | EXPECT_EQ(0u, |
| 3909 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3910 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3911 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 3912 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 3913 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3914 | TestGroupId("b"))); |
| 3915 | EXPECT_EQ(0u, |
| 3916 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3917 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3918 | EXPECT_EQ(2, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3919 | |
| 3920 | // Now release the 2 active sockets for "b". This will give us 1 idle socket |
| 3921 | // in "a" and 2 idle sockets in "b". Requesting 2 preconnected sockets for |
| 3922 | // "a" should result in closing 1 for "b". |
| 3923 | handle1.Reset(); |
| 3924 | handle2.Reset(); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3925 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3926 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3927 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3928 | pool_->RequestSockets(TestGroupId("a"), params_, 2, NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3929 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3930 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3931 | TestGroupId("a"))); |
| 3932 | EXPECT_EQ(1u, |
| 3933 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3934 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3935 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 3936 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 3937 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3938 | TestGroupId("b"))); |
| 3939 | EXPECT_EQ(0u, |
| 3940 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3941 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3942 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3943 | } |
| 3944 | |
[email protected] | b7b8be4 | 2011-07-12 12:46:41 | [diff] [blame] | 3945 | TEST_F(ClientSocketPoolBaseTest, PreconnectWithoutBackupJob) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3946 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 3947 | true /* enable_backup_connect_jobs */); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 3948 | |
| 3949 | // Make the ConnectJob hang until it times out, shorten the timeout. |
| 3950 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3951 | connect_job_factory_->set_timeout_duration( |
| 3952 | base::TimeDelta::FromMilliseconds(500)); |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3953 | pool_->RequestSockets(TestGroupId("a"), params_, 1, NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3954 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3955 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3956 | TestGroupId("a"))); |
| 3957 | EXPECT_EQ(1u, |
| 3958 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3959 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 3960 | |
[email protected] | b7b8be4 | 2011-07-12 12:46:41 | [diff] [blame] | 3961 | // Verify the backup timer doesn't create a backup job, by making |
| 3962 | // the backup job a pending job instead of a waiting job, so it |
| 3963 | // *would* complete if it were created. |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 3964 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 3965 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
Gabriel Charette | ea91801 | 2018-05-16 11:53:44 | [diff] [blame] | 3966 | FROM_HERE, base::RunLoop::QuitCurrentWhenIdleClosureDeprecated(), |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 3967 | base::TimeDelta::FromSeconds(1)); |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3968 | base::RunLoop().Run(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3969 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 3970 | } |
| 3971 | |
[email protected] | b7b8be4 | 2011-07-12 12:46:41 | [diff] [blame] | 3972 | TEST_F(ClientSocketPoolBaseTest, PreconnectWithBackupJob) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3973 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 3974 | true /* enable_backup_connect_jobs */); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 3975 | |
| 3976 | // Make the ConnectJob hang forever. |
| 3977 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3978 | pool_->RequestSockets(TestGroupId("a"), params_, 1, NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3979 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3980 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3981 | TestGroupId("a"))); |
| 3982 | EXPECT_EQ(1u, |
| 3983 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3984 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3985 | base::RunLoop().RunUntilIdle(); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 3986 | |
| 3987 | // Make the backup job be a pending job, so it completes normally. |
| 3988 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3989 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3990 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3991 | EXPECT_EQ( |
| 3992 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3993 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3994 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 3995 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 3996 | NetLogWithSource())); |
[email protected] | b7b8be4 | 2011-07-12 12:46:41 | [diff] [blame] | 3997 | // Timer has started, but the backup connect job shouldn't be created yet. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3998 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3999 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4000 | TestGroupId("a"))); |
| 4001 | EXPECT_EQ(0u, |
| 4002 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4003 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4004 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4005 | ASSERT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4006 | |
| 4007 | // The hung connect job should still be there, but everything else should be |
| 4008 | // complete. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4009 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4010 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4011 | TestGroupId("a"))); |
| 4012 | EXPECT_EQ(1u, |
| 4013 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4014 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4015 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4016 | } |
| 4017 | |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4018 | // Tests that a preconnect that starts out with unread data can still be used. |
| 4019 | // http://crbug.com/334467 |
| 4020 | TEST_F(ClientSocketPoolBaseTest, PreconnectWithUnreadData) { |
| 4021 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4022 | connect_job_factory_->set_job_type(TestConnectJob::kMockUnreadDataJob); |
| 4023 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 4024 | pool_->RequestSockets(TestGroupId("a"), params_, 1, NetLogWithSource()); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4025 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4026 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 4027 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4028 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4029 | TestGroupId("a"))); |
| 4030 | EXPECT_EQ(0u, |
| 4031 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4032 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4033 | |
| 4034 | // Fail future jobs to be sure that handle receives the preconnected socket |
| 4035 | // rather than closing it and making a new one. |
| 4036 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
| 4037 | ClientSocketHandle handle; |
| 4038 | TestCompletionCallback callback; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4039 | EXPECT_EQ(OK, handle.Init( |
| 4040 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
| 4041 | ClientSocketPool::RespectLimits::ENABLED, |
| 4042 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4043 | pool_.get(), NetLogWithSource())); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4044 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4045 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 4046 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4047 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4048 | TestGroupId("a"))); |
| 4049 | EXPECT_EQ(0u, |
| 4050 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4051 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4052 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4053 | |
| 4054 | // Drain the pending read. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 4055 | EXPECT_EQ(1, handle.socket()->Read(nullptr, 1, CompletionOnceCallback())); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4056 | |
| 4057 | TestLoadTimingInfoConnectedReused(handle); |
| 4058 | handle.Reset(); |
| 4059 | |
| 4060 | // The socket should be usable now that it's idle again. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4061 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4062 | } |
| 4063 | |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4064 | TEST_F(ClientSocketPoolBaseTest, RequestGetsAssignedJob) { |
| 4065 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4066 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4067 | |
| 4068 | ClientSocketHandle handle1; |
| 4069 | TestCompletionCallback callback1; |
| 4070 | EXPECT_EQ( |
| 4071 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4072 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4073 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4074 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4075 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4076 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4077 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4078 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4079 | TestGroupId("a"))); |
| 4080 | EXPECT_EQ(0u, |
| 4081 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4082 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4083 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4084 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4085 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4086 | } |
| 4087 | |
| 4088 | TEST_F(ClientSocketPoolBaseTest, MultipleRequestsGetAssignedJobs) { |
| 4089 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4090 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4091 | |
| 4092 | ClientSocketHandle handle1; |
| 4093 | TestCompletionCallback callback1; |
| 4094 | EXPECT_EQ( |
| 4095 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4096 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4097 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4098 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4099 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4100 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4101 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4102 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4103 | TestGroupId("a"))); |
| 4104 | EXPECT_EQ(0u, |
| 4105 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4106 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4107 | |
| 4108 | ClientSocketHandle handle2; |
| 4109 | TestCompletionCallback callback2; |
| 4110 | EXPECT_EQ( |
| 4111 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4112 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4113 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4114 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4115 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4116 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4117 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4118 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4119 | TestGroupId("a"))); |
| 4120 | EXPECT_EQ(0u, |
| 4121 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4122 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4123 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4124 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4125 | &handle1)); |
| 4126 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4127 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4128 | |
| 4129 | // One job completes. The other request should still have its job. |
| 4130 | client_socket_factory_.SignalJob(0); |
| 4131 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 4132 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4133 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4134 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4135 | TestGroupId("a"))); |
| 4136 | EXPECT_EQ(0u, |
| 4137 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4138 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4139 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4140 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4141 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4142 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4143 | } |
| 4144 | |
| 4145 | TEST_F(ClientSocketPoolBaseTest, PreconnectJobGetsAssignedToRequest) { |
| 4146 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4147 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4148 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 4149 | pool_->RequestSockets(TestGroupId("a"), params_, 1, NetLogWithSource()); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4150 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4151 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 4152 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4153 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4154 | TestGroupId("a"))); |
| 4155 | EXPECT_EQ(1u, |
| 4156 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4157 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4158 | |
| 4159 | ClientSocketHandle handle1; |
| 4160 | TestCompletionCallback callback1; |
| 4161 | EXPECT_EQ( |
| 4162 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4163 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4164 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4165 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4166 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4167 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4168 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4169 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4170 | TestGroupId("a"))); |
| 4171 | EXPECT_EQ(0u, |
| 4172 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4173 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4174 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4175 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4176 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4177 | } |
| 4178 | |
| 4179 | TEST_F(ClientSocketPoolBaseTest, HigherPriorityRequestStealsJob) { |
| 4180 | CreatePool(kDefaultMaxSockets, 1); |
| 4181 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4182 | |
| 4183 | ClientSocketHandle handle1; |
| 4184 | TestCompletionCallback callback1; |
| 4185 | EXPECT_EQ( |
| 4186 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4187 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4188 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4189 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4190 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4191 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4192 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4193 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4194 | TestGroupId("a"))); |
| 4195 | EXPECT_EQ(0u, |
| 4196 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4197 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4198 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4199 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4200 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4201 | |
| 4202 | // Insert a higher priority request |
| 4203 | ClientSocketHandle handle2; |
| 4204 | TestCompletionCallback callback2; |
| 4205 | EXPECT_EQ( |
| 4206 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4207 | handle2.Init(TestGroupId("a"), params_, HIGHEST, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4208 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4209 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4210 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4211 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4212 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4213 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4214 | TestGroupId("a"))); |
| 4215 | EXPECT_EQ(0u, |
| 4216 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4217 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4218 | |
| 4219 | // The highest priority request should steal the job from the default priority |
| 4220 | // request. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4221 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4222 | &handle2)); |
| 4223 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4224 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4225 | } |
| 4226 | |
| 4227 | TEST_F(ClientSocketPoolBaseTest, RequestStealsJobFromLowestRequestWithJob) { |
| 4228 | CreatePool(3, 3); |
| 4229 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4230 | |
| 4231 | ClientSocketHandle handle_lowest; |
| 4232 | TestCompletionCallback callback_lowest; |
| 4233 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4234 | handle_lowest.Init(TestGroupId("a"), params_, LOWEST, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4235 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4236 | callback_lowest.callback(), |
| 4237 | ClientSocketPool::ProxyAuthCallback(), |
| 4238 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4239 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4240 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4241 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4242 | TestGroupId("a"))); |
| 4243 | EXPECT_EQ(0u, |
| 4244 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4245 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4246 | |
| 4247 | ClientSocketHandle handle_highest; |
| 4248 | TestCompletionCallback callback_highest; |
| 4249 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4250 | handle_highest.Init(TestGroupId("a"), params_, HIGHEST, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4251 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4252 | callback_highest.callback(), |
| 4253 | ClientSocketPool::ProxyAuthCallback(), |
| 4254 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4255 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4256 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4257 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4258 | TestGroupId("a"))); |
| 4259 | EXPECT_EQ(0u, |
| 4260 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4261 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4262 | |
| 4263 | ClientSocketHandle handle_low; |
| 4264 | TestCompletionCallback callback_low; |
| 4265 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4266 | handle_low.Init(TestGroupId("a"), params_, LOW, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4267 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4268 | callback_low.callback(), |
| 4269 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4270 | NetLogWithSource())); |
| 4271 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4272 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4273 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4274 | TestGroupId("a"))); |
| 4275 | EXPECT_EQ(0u, |
| 4276 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4277 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4278 | |
| 4279 | ClientSocketHandle handle_lowest2; |
| 4280 | TestCompletionCallback callback_lowest2; |
| 4281 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4282 | handle_lowest2.Init(TestGroupId("a"), params_, LOWEST, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4283 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4284 | callback_lowest2.callback(), |
| 4285 | ClientSocketPool::ProxyAuthCallback(), |
| 4286 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4287 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4288 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4289 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4290 | TestGroupId("a"))); |
| 4291 | EXPECT_EQ(0u, |
| 4292 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4293 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4294 | |
| 4295 | // The top three requests in the queue should have jobs. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4296 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4297 | &handle_highest)); |
| 4298 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4299 | &handle_low)); |
| 4300 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4301 | &handle_lowest)); |
| 4302 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting( |
| 4303 | TestGroupId("a"), &handle_lowest2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4304 | |
| 4305 | // Add another request with medium priority. It should steal the job from the |
| 4306 | // lowest priority request with a job. |
| 4307 | ClientSocketHandle handle_medium; |
| 4308 | TestCompletionCallback callback_medium; |
| 4309 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4310 | handle_medium.Init(TestGroupId("a"), params_, MEDIUM, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4311 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4312 | callback_medium.callback(), |
| 4313 | ClientSocketPool::ProxyAuthCallback(), |
| 4314 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4315 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4316 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4317 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4318 | TestGroupId("a"))); |
| 4319 | EXPECT_EQ(0u, |
| 4320 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4321 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
| 4322 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4323 | &handle_highest)); |
| 4324 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4325 | &handle_medium)); |
| 4326 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4327 | &handle_low)); |
| 4328 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4329 | &handle_lowest)); |
| 4330 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting( |
| 4331 | TestGroupId("a"), &handle_lowest2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4332 | } |
| 4333 | |
| 4334 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeRequestStealsJob) { |
| 4335 | CreatePool(kDefaultMaxSockets, 1); |
| 4336 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4337 | |
| 4338 | ClientSocketHandle handle1; |
| 4339 | TestCompletionCallback callback1; |
| 4340 | EXPECT_EQ( |
| 4341 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4342 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4343 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4344 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4345 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4346 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4347 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4348 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4349 | TestGroupId("a"))); |
| 4350 | EXPECT_EQ(0u, |
| 4351 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4352 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4353 | |
| 4354 | ClientSocketHandle handle2; |
| 4355 | TestCompletionCallback callback2; |
| 4356 | EXPECT_EQ( |
| 4357 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4358 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4359 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4360 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4361 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4362 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4363 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4364 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4365 | TestGroupId("a"))); |
| 4366 | EXPECT_EQ(0u, |
| 4367 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4368 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4369 | |
| 4370 | // 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] | 4371 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4372 | &handle1)); |
| 4373 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4374 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4375 | |
| 4376 | // Reprioritizing the second request places it above the first, and it steals |
| 4377 | // the job from the first request. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4378 | pool_->SetPriority(TestGroupId("a"), &handle2, HIGHEST); |
| 4379 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4380 | &handle2)); |
| 4381 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4382 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4383 | } |
| 4384 | |
| 4385 | TEST_F(ClientSocketPoolBaseTest, CancelRequestReassignsJob) { |
| 4386 | CreatePool(kDefaultMaxSockets, 1); |
| 4387 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4388 | |
| 4389 | ClientSocketHandle handle1; |
| 4390 | TestCompletionCallback callback1; |
| 4391 | EXPECT_EQ( |
| 4392 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4393 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4394 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4395 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4396 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4397 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4398 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4399 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4400 | TestGroupId("a"))); |
| 4401 | EXPECT_EQ(0u, |
| 4402 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4403 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4404 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4405 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4406 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4407 | |
| 4408 | ClientSocketHandle handle2; |
| 4409 | TestCompletionCallback callback2; |
| 4410 | EXPECT_EQ( |
| 4411 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4412 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4413 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4414 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4415 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4416 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4417 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4418 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4419 | TestGroupId("a"))); |
| 4420 | EXPECT_EQ(0u, |
| 4421 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4422 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4423 | |
| 4424 | // The second request doesn't get a job because we are the limit. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4425 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4426 | &handle1)); |
| 4427 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4428 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4429 | |
| 4430 | // The second request should get a job upon cancelling the first request. |
| 4431 | handle1.Reset(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4432 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4433 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4434 | TestGroupId("a"))); |
| 4435 | EXPECT_EQ(0u, |
| 4436 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4437 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4438 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4439 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4440 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4441 | } |
| 4442 | |
| 4443 | TEST_F(ClientSocketPoolBaseTest, JobCompletionReassignsJob) { |
| 4444 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4445 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4446 | |
| 4447 | ClientSocketHandle handle1; |
| 4448 | TestCompletionCallback callback1; |
| 4449 | EXPECT_EQ( |
| 4450 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4451 | handle1.Init(TestGroupId("a"), params_, HIGHEST, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4452 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4453 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4454 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4455 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4456 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4457 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4458 | TestGroupId("a"))); |
| 4459 | EXPECT_EQ(0u, |
| 4460 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4461 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4462 | |
| 4463 | ClientSocketHandle handle2; |
| 4464 | TestCompletionCallback callback2; |
| 4465 | EXPECT_EQ( |
| 4466 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4467 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4468 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4469 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4470 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4471 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4472 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4473 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4474 | TestGroupId("a"))); |
| 4475 | EXPECT_EQ(0u, |
| 4476 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4477 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4478 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4479 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4480 | &handle1)); |
| 4481 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4482 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4483 | |
| 4484 | // The lower-priority job completes first. The higher-priority request should |
| 4485 | // get the socket, and the lower-priority request should get the remaining |
| 4486 | // job. |
| 4487 | client_socket_factory_.SignalJob(1); |
| 4488 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4489 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4490 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4491 | TestGroupId("a"))); |
| 4492 | EXPECT_EQ(0u, |
| 4493 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4494 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4495 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4496 | EXPECT_TRUE(handle1.socket()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4497 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4498 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4499 | } |
| 4500 | |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 4501 | class MockLayeredPool : public HigherLayeredPool { |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4502 | public: |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4503 | MockLayeredPool(TransportClientSocketPool* pool, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4504 | const ClientSocketPool::GroupId& group_id) |
| 4505 | : pool_(pool), group_id_(group_id), can_release_connection_(true) { |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 4506 | pool_->AddHigherLayeredPool(this); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4507 | } |
| 4508 | |
Daniel Cheng | 4496d082 | 2018-04-26 21:52:15 | [diff] [blame] | 4509 | ~MockLayeredPool() override { pool_->RemoveHigherLayeredPool(this); } |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4510 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4511 | int RequestSocket(TransportClientSocketPool* pool) { |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4512 | return handle_.Init( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4513 | group_id_, CreateDummyParams(), DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4514 | ClientSocketPool::RespectLimits::ENABLED, callback_.callback(), |
| 4515 | ClientSocketPool::ProxyAuthCallback(), pool, NetLogWithSource()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4516 | } |
| 4517 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4518 | int RequestSocketWithoutLimits(TransportClientSocketPool* pool) { |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4519 | return handle_.Init( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4520 | group_id_, CreateDummyParams(), MAXIMUM_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4521 | ClientSocketPool::RespectLimits::DISABLED, callback_.callback(), |
| 4522 | ClientSocketPool::ProxyAuthCallback(), pool, NetLogWithSource()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4523 | } |
| 4524 | |
| 4525 | bool ReleaseOneConnection() { |
| 4526 | if (!handle_.is_initialized() || !can_release_connection_) { |
| 4527 | return false; |
| 4528 | } |
| 4529 | handle_.socket()->Disconnect(); |
| 4530 | handle_.Reset(); |
| 4531 | return true; |
| 4532 | } |
| 4533 | |
| 4534 | void set_can_release_connection(bool can_release_connection) { |
| 4535 | can_release_connection_ = can_release_connection; |
| 4536 | } |
| 4537 | |
| 4538 | MOCK_METHOD0(CloseOneIdleConnection, bool()); |
| 4539 | |
| 4540 | private: |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4541 | TransportClientSocketPool* const pool_; |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4542 | ClientSocketHandle handle_; |
| 4543 | TestCompletionCallback callback_; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4544 | const ClientSocketPool::GroupId group_id_; |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4545 | bool can_release_connection_; |
| 4546 | }; |
| 4547 | |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4548 | // Tests the basic case of closing an idle socket in a higher layered pool when |
| 4549 | // a new request is issued and the lower layer pool is stalled. |
| 4550 | TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketsHeldByLayeredPoolWhenNeeded) { |
| 4551 | CreatePool(1, 1); |
| 4552 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 4553 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4554 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("foo")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4555 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4556 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4557 | .WillOnce(Invoke(&mock_layered_pool, |
| 4558 | &MockLayeredPool::ReleaseOneConnection)); |
| 4559 | ClientSocketHandle handle; |
| 4560 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4561 | EXPECT_EQ( |
| 4562 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4563 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4564 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 4565 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4566 | NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4567 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4568 | } |
| 4569 | |
Matt Menke | 83367864 | 2019-03-05 22:05:51 | [diff] [blame] | 4570 | // Tests the case that trying to close an idle socket in a higher layered pool |
| 4571 | // fails. |
| 4572 | TEST_F(ClientSocketPoolBaseTest, |
| 4573 | CloseIdleSocketsHeldByLayeredPoolWhenNeededFails) { |
| 4574 | CreatePool(1, 1); |
| 4575 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 4576 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4577 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("foo")); |
Matt Menke | 83367864 | 2019-03-05 22:05:51 | [diff] [blame] | 4578 | mock_layered_pool.set_can_release_connection(false); |
| 4579 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
| 4580 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4581 | .WillOnce(Invoke(&mock_layered_pool, |
| 4582 | &MockLayeredPool::ReleaseOneConnection)); |
| 4583 | ClientSocketHandle handle; |
| 4584 | TestCompletionCallback callback; |
| 4585 | EXPECT_EQ( |
| 4586 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4587 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 83367864 | 2019-03-05 22:05:51 | [diff] [blame] | 4588 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 4589 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4590 | NetLogWithSource())); |
| 4591 | base::RunLoop().RunUntilIdle(); |
| 4592 | EXPECT_FALSE(callback.have_result()); |
| 4593 | } |
| 4594 | |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4595 | // Same as above, but the idle socket is in the same group as the stalled |
| 4596 | // socket, and closes the only other request in its group when closing requests |
| 4597 | // in higher layered pools. This generally shouldn't happen, but it may be |
| 4598 | // possible if a higher level pool issues a request and the request is |
| 4599 | // subsequently cancelled. Even if it's not possible, best not to crash. |
| 4600 | TEST_F(ClientSocketPoolBaseTest, |
| 4601 | CloseIdleSocketsHeldByLayeredPoolWhenNeededSameGroup) { |
| 4602 | CreatePool(2, 2); |
| 4603 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 4604 | |
| 4605 | // Need a socket in another group for the pool to be stalled (If a group |
| 4606 | // has the maximum number of connections already, it's not stalled). |
| 4607 | ClientSocketHandle handle1; |
| 4608 | TestCompletionCallback callback1; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4609 | EXPECT_EQ(OK, handle1.Init( |
| 4610 | TestGroupId("group1"), params_, DEFAULT_PRIORITY, |
| 4611 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4612 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4613 | pool_.get(), NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4614 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4615 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("group2")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4616 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4617 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4618 | .WillOnce(Invoke(&mock_layered_pool, |
| 4619 | &MockLayeredPool::ReleaseOneConnection)); |
| 4620 | ClientSocketHandle handle; |
| 4621 | TestCompletionCallback callback2; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4622 | EXPECT_EQ( |
| 4623 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4624 | handle.Init(TestGroupId("group2"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4625 | ClientSocketPool::RespectLimits::ENABLED, |
| 4626 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4627 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4628 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4629 | } |
| 4630 | |
| 4631 | // Tests the case when an idle socket can be closed when a new request is |
| 4632 | // issued, and the new request belongs to a group that was previously stalled. |
| 4633 | TEST_F(ClientSocketPoolBaseTest, |
| 4634 | CloseIdleSocketsHeldByLayeredPoolInSameGroupWhenNeeded) { |
| 4635 | CreatePool(2, 2); |
| 4636 | std::list<TestConnectJob::JobType> job_types; |
| 4637 | job_types.push_back(TestConnectJob::kMockJob); |
| 4638 | job_types.push_back(TestConnectJob::kMockJob); |
| 4639 | job_types.push_back(TestConnectJob::kMockJob); |
| 4640 | job_types.push_back(TestConnectJob::kMockJob); |
| 4641 | connect_job_factory_->set_job_types(&job_types); |
| 4642 | |
| 4643 | ClientSocketHandle handle1; |
| 4644 | TestCompletionCallback callback1; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4645 | EXPECT_EQ(OK, handle1.Init( |
| 4646 | TestGroupId("group1"), params_, DEFAULT_PRIORITY, |
| 4647 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4648 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4649 | pool_.get(), NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4650 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4651 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("group2")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4652 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4653 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4654 | .WillRepeatedly(Invoke(&mock_layered_pool, |
| 4655 | &MockLayeredPool::ReleaseOneConnection)); |
| 4656 | mock_layered_pool.set_can_release_connection(false); |
| 4657 | |
| 4658 | // The third request is made when the socket pool is in a stalled state. |
| 4659 | ClientSocketHandle handle3; |
| 4660 | TestCompletionCallback callback3; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4661 | EXPECT_EQ( |
| 4662 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4663 | handle3.Init(TestGroupId("group3"), params_, DEFAULT_PRIORITY, |
| 4664 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4665 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4666 | pool_.get(), NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4667 | |
| 4668 | base::RunLoop().RunUntilIdle(); |
| 4669 | EXPECT_FALSE(callback3.have_result()); |
| 4670 | |
| 4671 | // The fourth request is made when the pool is no longer stalled. The third |
| 4672 | // request should be serviced first, since it was issued first and has the |
| 4673 | // same priority. |
| 4674 | mock_layered_pool.set_can_release_connection(true); |
| 4675 | ClientSocketHandle handle4; |
| 4676 | TestCompletionCallback callback4; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4677 | EXPECT_EQ( |
| 4678 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4679 | handle4.Init(TestGroupId("group3"), params_, DEFAULT_PRIORITY, |
| 4680 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4681 | callback4.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4682 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4683 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4684 | EXPECT_FALSE(callback4.have_result()); |
| 4685 | |
| 4686 | // Closing a handle should free up another socket slot. |
| 4687 | handle1.Reset(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4688 | EXPECT_THAT(callback4.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4689 | } |
| 4690 | |
| 4691 | // Tests the case when an idle socket can be closed when a new request is |
| 4692 | // issued, and the new request belongs to a group that was previously stalled. |
| 4693 | // |
| 4694 | // The two differences from the above test are that the stalled requests are not |
| 4695 | // in the same group as the layered pool's request, and the the fourth request |
| 4696 | // has a higher priority than the third one, so gets a socket first. |
| 4697 | TEST_F(ClientSocketPoolBaseTest, |
| 4698 | CloseIdleSocketsHeldByLayeredPoolInSameGroupWhenNeeded2) { |
| 4699 | CreatePool(2, 2); |
| 4700 | std::list<TestConnectJob::JobType> job_types; |
| 4701 | job_types.push_back(TestConnectJob::kMockJob); |
| 4702 | job_types.push_back(TestConnectJob::kMockJob); |
| 4703 | job_types.push_back(TestConnectJob::kMockJob); |
| 4704 | job_types.push_back(TestConnectJob::kMockJob); |
| 4705 | connect_job_factory_->set_job_types(&job_types); |
| 4706 | |
| 4707 | ClientSocketHandle handle1; |
| 4708 | TestCompletionCallback callback1; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4709 | EXPECT_EQ(OK, handle1.Init( |
| 4710 | TestGroupId("group1"), params_, DEFAULT_PRIORITY, |
| 4711 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4712 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4713 | pool_.get(), NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4714 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4715 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("group2")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4716 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4717 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4718 | .WillRepeatedly(Invoke(&mock_layered_pool, |
| 4719 | &MockLayeredPool::ReleaseOneConnection)); |
| 4720 | mock_layered_pool.set_can_release_connection(false); |
| 4721 | |
| 4722 | // The third request is made when the socket pool is in a stalled state. |
| 4723 | ClientSocketHandle handle3; |
| 4724 | TestCompletionCallback callback3; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4725 | EXPECT_EQ( |
| 4726 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4727 | handle3.Init(TestGroupId("group3"), params_, MEDIUM, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4728 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4729 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4730 | pool_.get(), NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4731 | |
| 4732 | base::RunLoop().RunUntilIdle(); |
| 4733 | EXPECT_FALSE(callback3.have_result()); |
| 4734 | |
| 4735 | // The fourth request is made when the pool is no longer stalled. This |
| 4736 | // request has a higher priority than the third request, so is serviced first. |
| 4737 | mock_layered_pool.set_can_release_connection(true); |
| 4738 | ClientSocketHandle handle4; |
| 4739 | TestCompletionCallback callback4; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4740 | EXPECT_EQ( |
| 4741 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4742 | handle4.Init(TestGroupId("group3"), params_, HIGHEST, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4743 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4744 | callback4.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4745 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4746 | EXPECT_THAT(callback4.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4747 | EXPECT_FALSE(callback3.have_result()); |
| 4748 | |
| 4749 | // Closing a handle should free up another socket slot. |
| 4750 | handle1.Reset(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4751 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4752 | } |
| 4753 | |
| 4754 | TEST_F(ClientSocketPoolBaseTest, |
| 4755 | CloseMultipleIdleSocketsHeldByLayeredPoolWhenNeeded) { |
| 4756 | CreatePool(1, 1); |
| 4757 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 4758 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4759 | MockLayeredPool mock_layered_pool1(pool_.get(), TestGroupId("foo")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4760 | EXPECT_THAT(mock_layered_pool1.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4761 | EXPECT_CALL(mock_layered_pool1, CloseOneIdleConnection()) |
| 4762 | .WillRepeatedly(Invoke(&mock_layered_pool1, |
| 4763 | &MockLayeredPool::ReleaseOneConnection)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4764 | MockLayeredPool mock_layered_pool2(pool_.get(), TestGroupId("bar")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4765 | EXPECT_THAT(mock_layered_pool2.RequestSocketWithoutLimits(pool_.get()), |
| 4766 | IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4767 | EXPECT_CALL(mock_layered_pool2, CloseOneIdleConnection()) |
| 4768 | .WillRepeatedly(Invoke(&mock_layered_pool2, |
| 4769 | &MockLayeredPool::ReleaseOneConnection)); |
| 4770 | ClientSocketHandle handle; |
| 4771 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4772 | EXPECT_EQ( |
| 4773 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4774 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4775 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 4776 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4777 | NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4778 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4779 | } |
| 4780 | |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4781 | // Test that when a socket pool and group are at their limits, a request |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4782 | // with RespectLimits::DISABLED triggers creation of a new socket, and gets the |
| 4783 | // socket instead of a request with the same priority that was issued earlier, |
| 4784 | // but has RespectLimits::ENABLED. |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4785 | TEST_F(ClientSocketPoolBaseTest, IgnoreLimits) { |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4786 | CreatePool(1, 1); |
| 4787 | |
| 4788 | // Issue a request to reach the socket pool limit. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4789 | EXPECT_EQ(OK, StartRequestWithIgnoreLimits( |
| 4790 | TestGroupId("a"), MAXIMUM_PRIORITY, |
| 4791 | ClientSocketPool::RespectLimits::ENABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4792 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4793 | |
| 4794 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 4795 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4796 | EXPECT_EQ(ERR_IO_PENDING, StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4797 | TestGroupId("a"), MAXIMUM_PRIORITY, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4798 | ClientSocketPool::RespectLimits::ENABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4799 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4800 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4801 | // Issue a request that ignores the limits, so a new ConnectJob is |
| 4802 | // created. |
| 4803 | EXPECT_EQ(ERR_IO_PENDING, StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4804 | TestGroupId("a"), MAXIMUM_PRIORITY, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4805 | ClientSocketPool::RespectLimits::DISABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4806 | ASSERT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4807 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4808 | EXPECT_THAT(request(2)->WaitForResult(), IsOk()); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4809 | EXPECT_FALSE(request(1)->have_result()); |
| 4810 | } |
| 4811 | |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 4812 | // Test that when a socket pool and group are at their limits, a ConnectJob |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4813 | // issued for a request with RespectLimits::DISABLED is not cancelled when a |
| 4814 | // request with RespectLimits::ENABLED issued to the same group is cancelled. |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 4815 | TEST_F(ClientSocketPoolBaseTest, IgnoreLimitsCancelOtherJob) { |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 4816 | CreatePool(1, 1); |
| 4817 | |
| 4818 | // Issue a request to reach the socket pool limit. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4819 | EXPECT_EQ(OK, StartRequestWithIgnoreLimits( |
| 4820 | TestGroupId("a"), MAXIMUM_PRIORITY, |
| 4821 | ClientSocketPool::RespectLimits::ENABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4822 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 4823 | |
| 4824 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 4825 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4826 | EXPECT_EQ(ERR_IO_PENDING, StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4827 | TestGroupId("a"), MAXIMUM_PRIORITY, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4828 | ClientSocketPool::RespectLimits::ENABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4829 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 4830 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4831 | // Issue a request with RespectLimits::DISABLED, so a new ConnectJob is |
| 4832 | // created. |
| 4833 | EXPECT_EQ(ERR_IO_PENDING, StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4834 | TestGroupId("a"), MAXIMUM_PRIORITY, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4835 | ClientSocketPool::RespectLimits::DISABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4836 | ASSERT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4837 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4838 | // Cancel the pending request with RespectLimits::ENABLED. The ConnectJob |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4839 | // should not be cancelled. |
| 4840 | request(1)->handle()->Reset(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4841 | ASSERT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4842 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4843 | EXPECT_THAT(request(2)->WaitForResult(), IsOk()); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4844 | EXPECT_FALSE(request(1)->have_result()); |
| 4845 | } |
| 4846 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4847 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthNoAuthCallback) { |
| 4848 | CreatePool(1, 1); |
| 4849 | |
| 4850 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 4851 | |
| 4852 | ClientSocketHandle handle; |
| 4853 | TestCompletionCallback callback; |
| 4854 | EXPECT_EQ( |
| 4855 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4856 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4857 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 4858 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4859 | NetLogWithSource())); |
| 4860 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4861 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4862 | |
| 4863 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_PROXY_AUTH_REQUESTED)); |
| 4864 | EXPECT_FALSE(handle.is_initialized()); |
| 4865 | EXPECT_FALSE(handle.socket()); |
| 4866 | |
| 4867 | // The group should now be empty, and thus be deleted. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4868 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4869 | } |
| 4870 | |
| 4871 | class TestAuthHelper { |
| 4872 | public: |
| 4873 | TestAuthHelper() = default; |
| 4874 | ~TestAuthHelper() = default; |
| 4875 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4876 | void InitHandle( |
Matt Menke | 84d11e56 | 2019-03-27 00:11:19 | [diff] [blame] | 4877 | scoped_refptr<ClientSocketPool::SocketParams> params, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4878 | TransportClientSocketPool* pool, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4879 | RequestPriority priority = DEFAULT_PRIORITY, |
| 4880 | ClientSocketPool::RespectLimits respect_limits = |
| 4881 | ClientSocketPool::RespectLimits::ENABLED, |
| 4882 | const ClientSocketPool::GroupId& group_id_in = TestGroupId("a")) { |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4883 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4884 | handle_.Init(group_id_in, params, priority, SocketTag(), |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4885 | respect_limits, callback_.callback(), |
| 4886 | base::BindRepeating(&TestAuthHelper::AuthCallback, |
| 4887 | base::Unretained(this)), |
| 4888 | pool, NetLogWithSource())); |
| 4889 | } |
| 4890 | |
| 4891 | void WaitForAuth() { |
| 4892 | run_loop_ = std::make_unique<base::RunLoop>(); |
| 4893 | run_loop_->Run(); |
| 4894 | run_loop_.reset(); |
| 4895 | } |
| 4896 | |
| 4897 | void WaitForAuthAndRestartSync() { |
| 4898 | restart_sync_ = true; |
| 4899 | WaitForAuth(); |
| 4900 | restart_sync_ = false; |
| 4901 | } |
| 4902 | |
| 4903 | void WaitForAuthAndResetHandleSync() { |
| 4904 | reset_handle_sync_ = true; |
| 4905 | WaitForAuth(); |
| 4906 | reset_handle_sync_ = false; |
| 4907 | } |
| 4908 | |
| 4909 | void RestartWithAuth() { |
| 4910 | DCHECK(restart_with_auth_callback_); |
| 4911 | std::move(restart_with_auth_callback_).Run(); |
| 4912 | } |
| 4913 | |
| 4914 | int WaitForResult() { |
| 4915 | int result = callback_.WaitForResult(); |
| 4916 | // There shouldn't be any callback waiting to be invoked once the request is |
| 4917 | // complete. |
| 4918 | EXPECT_FALSE(restart_with_auth_callback_); |
| 4919 | // The socket should only be initialized on success. |
| 4920 | EXPECT_EQ(result == OK, handle_.is_initialized()); |
| 4921 | EXPECT_EQ(result == OK, handle_.socket() != nullptr); |
| 4922 | return result; |
| 4923 | } |
| 4924 | |
| 4925 | ClientSocketHandle* handle() { return &handle_; } |
| 4926 | int auth_count() const { return auth_count_; } |
| 4927 | int have_result() const { return callback_.have_result(); } |
| 4928 | |
| 4929 | private: |
| 4930 | void AuthCallback(const HttpResponseInfo& response, |
| 4931 | HttpAuthController* auth_controller, |
| 4932 | base::OnceClosure restart_with_auth_callback) { |
| 4933 | EXPECT_FALSE(restart_with_auth_callback_); |
| 4934 | EXPECT_TRUE(restart_with_auth_callback); |
| 4935 | |
| 4936 | // Once there's a result, this method shouldn't be invoked again. |
| 4937 | EXPECT_FALSE(callback_.have_result()); |
| 4938 | |
| 4939 | ++auth_count_; |
| 4940 | run_loop_->Quit(); |
| 4941 | if (restart_sync_) { |
| 4942 | std::move(restart_with_auth_callback).Run(); |
| 4943 | return; |
| 4944 | } |
| 4945 | |
| 4946 | restart_with_auth_callback_ = std::move(restart_with_auth_callback); |
| 4947 | |
| 4948 | if (reset_handle_sync_) { |
| 4949 | handle_.Reset(); |
| 4950 | return; |
| 4951 | } |
| 4952 | } |
| 4953 | |
| 4954 | std::unique_ptr<base::RunLoop> run_loop_; |
| 4955 | base::OnceClosure restart_with_auth_callback_; |
| 4956 | |
| 4957 | bool restart_sync_ = false; |
| 4958 | bool reset_handle_sync_ = false; |
| 4959 | |
| 4960 | ClientSocketHandle handle_; |
| 4961 | int auth_count_ = 0; |
| 4962 | TestCompletionCallback callback_; |
| 4963 | |
| 4964 | DISALLOW_COPY_AND_ASSIGN(TestAuthHelper); |
| 4965 | }; |
| 4966 | |
| 4967 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnce) { |
| 4968 | CreatePool(1, 1); |
| 4969 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 4970 | |
| 4971 | TestAuthHelper auth_helper; |
| 4972 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4973 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 4974 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4975 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4976 | |
| 4977 | auth_helper.WaitForAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4978 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 4979 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4980 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4981 | |
| 4982 | auth_helper.RestartWithAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4983 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 4984 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4985 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4986 | |
| 4987 | EXPECT_THAT(auth_helper.WaitForResult(), IsOk()); |
| 4988 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4989 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4990 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4991 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4992 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 4993 | } |
| 4994 | |
| 4995 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceSync) { |
| 4996 | CreatePool(1, 1); |
| 4997 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 4998 | |
| 4999 | TestAuthHelper auth_helper; |
| 5000 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5001 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5002 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5003 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5004 | |
| 5005 | auth_helper.WaitForAuthAndRestartSync(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5006 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5007 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5008 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5009 | |
| 5010 | EXPECT_THAT(auth_helper.WaitForResult(), IsOk()); |
| 5011 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5012 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5013 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5014 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5015 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5016 | } |
| 5017 | |
| 5018 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceFails) { |
| 5019 | CreatePool(1, 1); |
| 5020 | connect_job_factory_->set_job_type( |
| 5021 | TestConnectJob::kMockAuthChallengeOnceFailingJob); |
| 5022 | |
| 5023 | TestAuthHelper auth_helper; |
| 5024 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5025 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5026 | |
| 5027 | auth_helper.WaitForAuth(); |
| 5028 | auth_helper.RestartWithAuth(); |
| 5029 | EXPECT_THAT(auth_helper.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 5030 | |
| 5031 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5032 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5033 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5034 | } |
| 5035 | |
| 5036 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceSyncFails) { |
| 5037 | CreatePool(1, 1); |
| 5038 | connect_job_factory_->set_job_type( |
| 5039 | TestConnectJob::kMockAuthChallengeOnceFailingJob); |
| 5040 | |
| 5041 | TestAuthHelper auth_helper; |
| 5042 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5043 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5044 | |
| 5045 | auth_helper.WaitForAuthAndRestartSync(); |
| 5046 | EXPECT_THAT(auth_helper.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 5047 | |
| 5048 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5049 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5050 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5051 | } |
| 5052 | |
| 5053 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceDeleteHandle) { |
| 5054 | CreatePool(1, 1); |
| 5055 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5056 | |
| 5057 | TestAuthHelper auth_helper; |
| 5058 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5059 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5060 | |
| 5061 | auth_helper.WaitForAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5062 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5063 | |
| 5064 | auth_helper.handle()->Reset(); |
| 5065 | |
| 5066 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5067 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5068 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5069 | EXPECT_FALSE(auth_helper.handle()->is_initialized()); |
| 5070 | EXPECT_FALSE(auth_helper.handle()->socket()); |
| 5071 | } |
| 5072 | |
| 5073 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceDeleteHandleSync) { |
| 5074 | CreatePool(1, 1); |
| 5075 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5076 | |
| 5077 | TestAuthHelper auth_helper; |
| 5078 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5079 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5080 | |
| 5081 | auth_helper.WaitForAuthAndResetHandleSync(); |
| 5082 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5083 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5084 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5085 | EXPECT_FALSE(auth_helper.handle()->is_initialized()); |
| 5086 | EXPECT_FALSE(auth_helper.handle()->socket()); |
| 5087 | } |
| 5088 | |
| 5089 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceFlushWithError) { |
| 5090 | CreatePool(1, 1); |
| 5091 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5092 | |
| 5093 | TestAuthHelper auth_helper; |
| 5094 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5095 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5096 | |
| 5097 | auth_helper.WaitForAuth(); |
| 5098 | |
| 5099 | pool_->FlushWithError(ERR_FAILED); |
| 5100 | base::RunLoop().RunUntilIdle(); |
| 5101 | |
| 5102 | // When flushing the socket pool, bound sockets should delay returning the |
| 5103 | // error until completion. |
| 5104 | EXPECT_FALSE(auth_helper.have_result()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5105 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5106 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5107 | |
| 5108 | auth_helper.RestartWithAuth(); |
| 5109 | // The callback should be called asynchronously. |
| 5110 | EXPECT_FALSE(auth_helper.have_result()); |
| 5111 | |
| 5112 | EXPECT_THAT(auth_helper.WaitForResult(), IsError(ERR_FAILED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5113 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5114 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5115 | } |
| 5116 | |
| 5117 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthTwice) { |
| 5118 | CreatePool(1, 1); |
| 5119 | connect_job_factory_->set_job_type( |
| 5120 | TestConnectJob::kMockAuthChallengeTwiceJob); |
| 5121 | |
| 5122 | TestAuthHelper auth_helper; |
| 5123 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5124 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5125 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5126 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5127 | |
| 5128 | auth_helper.WaitForAuth(); |
| 5129 | auth_helper.RestartWithAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5130 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5131 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5132 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5133 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5134 | |
| 5135 | auth_helper.WaitForAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5136 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5137 | EXPECT_EQ(2, auth_helper.auth_count()); |
| 5138 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5139 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5140 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5141 | auth_helper.RestartWithAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5142 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5143 | EXPECT_EQ(2, auth_helper.auth_count()); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5144 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5145 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5146 | |
| 5147 | EXPECT_THAT(auth_helper.WaitForResult(), IsOk()); |
| 5148 | EXPECT_EQ(2, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5149 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5150 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5151 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5152 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5153 | } |
| 5154 | |
| 5155 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthTwiceFails) { |
| 5156 | CreatePool(1, 1); |
| 5157 | connect_job_factory_->set_job_type( |
| 5158 | TestConnectJob::kMockAuthChallengeTwiceFailingJob); |
| 5159 | |
| 5160 | TestAuthHelper auth_helper; |
| 5161 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5162 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5163 | |
| 5164 | auth_helper.WaitForAuth(); |
| 5165 | auth_helper.RestartWithAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5166 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5167 | EXPECT_EQ(1, auth_helper.auth_count()); |
| 5168 | |
| 5169 | auth_helper.WaitForAuth(); |
| 5170 | auth_helper.RestartWithAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5171 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5172 | EXPECT_EQ(2, auth_helper.auth_count()); |
| 5173 | |
| 5174 | EXPECT_THAT(auth_helper.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 5175 | EXPECT_EQ(2, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5176 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5177 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5178 | } |
| 5179 | |
| 5180 | // Makes sure that when a bound request is destroyed, a new ConnectJob is |
| 5181 | // created, if needed. |
| 5182 | TEST_F(ClientSocketPoolBaseTest, |
| 5183 | ProxyAuthCreateNewConnectJobOnDestroyBoundRequest) { |
| 5184 | CreatePool(1 /* max_sockets */, 1 /* max_sockets_per_group */); |
| 5185 | connect_job_factory_->set_job_type( |
| 5186 | TestConnectJob::kMockAuthChallengeOnceFailingJob); |
| 5187 | |
| 5188 | // First request creates a ConnectJob. |
| 5189 | TestAuthHelper auth_helper1; |
| 5190 | auth_helper1.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5191 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5192 | |
| 5193 | // A second request come in, but no new ConnectJob is needed, since the limit |
| 5194 | // has been reached. |
| 5195 | TestAuthHelper auth_helper2; |
| 5196 | auth_helper2.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5197 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5198 | |
| 5199 | // Run until the auth callback for the first request is invoked. |
| 5200 | auth_helper1.WaitForAuth(); |
| 5201 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5202 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5203 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5204 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5205 | |
| 5206 | // Make connect jobs succeed, then cancel the first request, which should |
| 5207 | // destroy the bound ConnectJob, and cause a new ConnectJob to start. |
| 5208 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 5209 | auth_helper1.handle()->Reset(); |
| 5210 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5211 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5212 | |
| 5213 | // The second ConnectJob should succeed. |
| 5214 | EXPECT_THAT(auth_helper2.WaitForResult(), IsOk()); |
| 5215 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5216 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5217 | } |
| 5218 | |
| 5219 | // Makes sure that when a bound request is destroyed, a new ConnectJob is |
| 5220 | // created for another group, if needed. |
| 5221 | TEST_F(ClientSocketPoolBaseTest, |
| 5222 | ProxyAuthCreateNewConnectJobOnDestroyBoundRequestDifferentGroups) { |
| 5223 | CreatePool(1 /* max_sockets */, 1 /* max_sockets_per_group */); |
| 5224 | connect_job_factory_->set_job_type( |
| 5225 | TestConnectJob::kMockAuthChallengeOnceFailingJob); |
| 5226 | |
| 5227 | // First request creates a ConnectJob. |
| 5228 | TestAuthHelper auth_helper1; |
| 5229 | auth_helper1.InitHandle(params_, pool_.get(), DEFAULT_PRIORITY); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5230 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5231 | |
| 5232 | // A second request come in, but no new ConnectJob is needed, since the limit |
| 5233 | // has been reached. |
| 5234 | TestAuthHelper auth_helper2; |
| 5235 | auth_helper2.InitHandle(params_, pool_.get(), DEFAULT_PRIORITY, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5236 | ClientSocketPool::RespectLimits::ENABLED, |
| 5237 | TestGroupId("b")); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5238 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5239 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5240 | |
| 5241 | // Run until the auth callback for the first request is invoked. |
| 5242 | auth_helper1.WaitForAuth(); |
| 5243 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5244 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5245 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5246 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5247 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 5248 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5249 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5250 | |
| 5251 | // Make connect jobs succeed, then cancel the first request, which should |
| 5252 | // destroy the bound ConnectJob, and cause a new ConnectJob to start for the |
| 5253 | // other group. |
| 5254 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 5255 | auth_helper1.handle()->Reset(); |
| 5256 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5257 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 5258 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5259 | |
| 5260 | // The second ConnectJob should succeed. |
| 5261 | EXPECT_THAT(auth_helper2.WaitForResult(), IsOk()); |
| 5262 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5263 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 5264 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5265 | } |
| 5266 | |
| 5267 | // Test that once an auth challenge is bound, that's the request that gets all |
| 5268 | // subsequent calls and the socket itself. |
| 5269 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthStaysBound) { |
| 5270 | CreatePool(1, 1); |
| 5271 | connect_job_factory_->set_job_type( |
| 5272 | TestConnectJob::kMockAuthChallengeTwiceJob); |
| 5273 | |
| 5274 | // First request creates a ConnectJob. |
| 5275 | TestAuthHelper auth_helper1; |
| 5276 | auth_helper1.InitHandle(params_, pool_.get(), LOWEST); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5277 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5278 | |
| 5279 | // A second, higher priority request is made. |
| 5280 | TestAuthHelper auth_helper2; |
| 5281 | auth_helper2.InitHandle(params_, pool_.get(), LOW); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5282 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5283 | |
| 5284 | // Run until the auth callback for the second request is invoked. |
| 5285 | auth_helper2.WaitForAuth(); |
| 5286 | EXPECT_EQ(0, auth_helper1.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5287 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5288 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5289 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5290 | |
| 5291 | // Start a higher priority job. It shouldn't be able to steal |auth_helper2|'s |
| 5292 | // ConnectJob. |
| 5293 | TestAuthHelper auth_helper3; |
| 5294 | auth_helper3.InitHandle(params_, pool_.get(), HIGHEST); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5295 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5296 | |
| 5297 | // Start a higher job that ignores limits, creating a hanging socket. It |
| 5298 | // shouldn't be able to steal |auth_helper2|'s ConnectJob. |
| 5299 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 5300 | TestAuthHelper auth_helper4; |
| 5301 | auth_helper4.InitHandle(params_, pool_.get(), HIGHEST, |
| 5302 | ClientSocketPool::RespectLimits::DISABLED); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5303 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5304 | |
| 5305 | // Restart with auth, and |auth_helper2|'s auth method should be invoked |
| 5306 | // again. |
| 5307 | auth_helper2.RestartWithAuth(); |
| 5308 | auth_helper2.WaitForAuth(); |
| 5309 | EXPECT_EQ(0, auth_helper1.auth_count()); |
| 5310 | EXPECT_FALSE(auth_helper1.have_result()); |
| 5311 | EXPECT_EQ(2, auth_helper2.auth_count()); |
| 5312 | EXPECT_FALSE(auth_helper2.have_result()); |
| 5313 | EXPECT_EQ(0, auth_helper3.auth_count()); |
| 5314 | EXPECT_FALSE(auth_helper3.have_result()); |
| 5315 | EXPECT_EQ(0, auth_helper4.auth_count()); |
| 5316 | EXPECT_FALSE(auth_helper4.have_result()); |
| 5317 | |
| 5318 | // Advance auth again, and |auth_helper2| should get the socket. |
| 5319 | auth_helper2.RestartWithAuth(); |
| 5320 | EXPECT_THAT(auth_helper2.WaitForResult(), IsOk()); |
| 5321 | // The hung ConnectJob for the RespectLimits::DISABLED request is still in the |
| 5322 | // socket pool. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5323 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5324 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5325 | EXPECT_EQ(0, auth_helper1.auth_count()); |
| 5326 | EXPECT_FALSE(auth_helper1.have_result()); |
| 5327 | EXPECT_EQ(0, auth_helper3.auth_count()); |
| 5328 | EXPECT_FALSE(auth_helper3.have_result()); |
| 5329 | EXPECT_EQ(0, auth_helper4.auth_count()); |
| 5330 | EXPECT_FALSE(auth_helper4.have_result()); |
| 5331 | |
| 5332 | // If the socket is returned to the socket pool, the RespectLimits::DISABLED |
| 5333 | // socket request should be able to claim it. |
| 5334 | auth_helper2.handle()->Reset(); |
| 5335 | EXPECT_THAT(auth_helper4.WaitForResult(), IsOk()); |
| 5336 | EXPECT_EQ(0, auth_helper1.auth_count()); |
| 5337 | EXPECT_FALSE(auth_helper1.have_result()); |
| 5338 | EXPECT_EQ(0, auth_helper3.auth_count()); |
| 5339 | EXPECT_FALSE(auth_helper3.have_result()); |
| 5340 | EXPECT_EQ(0, auth_helper4.auth_count()); |
| 5341 | } |
| 5342 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 5343 | } // namespace |
| 5344 | |
| 5345 | } // namespace net |