[email protected] | e34400c3 | 2012-01-24 02:49:33 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5 | #include "net/socket/transport_client_socket_pool.h" |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 6 | |
tbansal | f82cc8e | 2015-10-14 20:05:49 | [diff] [blame] | 7 | #include <stdint.h> |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 8 | #include <utility> |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 9 | #include <vector> |
| 10 | |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 11 | #include "base/bind.h" |
| 12 | #include "base/bind_helpers.h" |
[email protected] | 2041cf34 | 2010-02-19 03:15:59 | [diff] [blame] | 13 | #include "base/callback.h" |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 14 | #include "base/location.h" |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 15 | #include "base/logging.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 16 | #include "base/memory/ref_counted.h" |
[email protected] | 6ea7b15 | 2011-12-21 21:21:13 | [diff] [blame] | 17 | #include "base/memory/weak_ptr.h" |
Alexander Timin | 4f9c35c | 2018-11-01 20:15:20 | [diff] [blame] | 18 | #include "base/message_loop/message_loop.h" |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 19 | #include "base/run_loop.h" |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 20 | #include "base/single_thread_task_runner.h" |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 21 | #include "base/stl_util.h" |
[email protected] | fc9be580 | 2013-06-11 10:56:51 | [diff] [blame] | 22 | #include "base/strings/string_number_conversions.h" |
[email protected] | 18b57741 | 2013-07-18 04:19:15 | [diff] [blame] | 23 | #include "base/strings/stringprintf.h" |
[email protected] | f214f879 | 2011-01-01 02:17:08 | [diff] [blame] | 24 | #include "base/threading/platform_thread.h" |
gab | f767595f | 2016-05-11 18:50:35 | [diff] [blame] | 25 | #include "base/threading/thread_task_runner_handle.h" |
[email protected] | f3a1c64 | 2011-07-12 19:15:03 | [diff] [blame] | 26 | #include "base/values.h" |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 27 | #include "net/base/load_timing_info.h" |
[email protected] | b258e079 | 2013-01-12 07:11:59 | [diff] [blame] | 28 | #include "net/base/load_timing_info_test_util.h" |
[email protected] | d8eb8424 | 2010-09-25 02:25:06 | [diff] [blame] | 29 | #include "net/base/net_errors.h" |
[email protected] | ac790b4 | 2009-12-02 04:31:31 | [diff] [blame] | 30 | #include "net/base/request_priority.h" |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 31 | #include "net/base/test_completion_callback.h" |
[email protected] | 277d594 | 2010-08-11 21:02:35 | [diff] [blame] | 32 | #include "net/http/http_response_headers.h" |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 33 | #include "net/http/http_response_info.h" |
eroman | 87c53d6 | 2015-04-02 06:51:07 | [diff] [blame] | 34 | #include "net/log/net_log.h" |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 35 | #include "net/log/net_log_event_type.h" |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 36 | #include "net/log/net_log_source.h" |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 37 | #include "net/log/net_log_source_type.h" |
mmenke | 16a7cbdd | 2015-04-24 23:00:56 | [diff] [blame] | 38 | #include "net/log/test_net_log.h" |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 39 | #include "net/log/test_net_log_entry.h" |
| 40 | #include "net/log/test_net_log_util.h" |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 41 | #include "net/socket/client_socket_factory.h" |
| 42 | #include "net/socket/client_socket_handle.h" |
tfarina | 5dd13c2 | 2016-11-16 12:08:26 | [diff] [blame] | 43 | #include "net/socket/datagram_client_socket.h" |
tbansal | ca83c00 | 2016-04-28 20:56:28 | [diff] [blame] | 44 | #include "net/socket/socket_performance_watcher.h" |
Paul Jensen | 8d6f87ec | 2018-01-13 00:46:54 | [diff] [blame] | 45 | #include "net/socket/socket_tag.h" |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 46 | #include "net/socket/socket_test_util.h" |
[email protected] | 18ccfdb | 2013-08-15 00:13:44 | [diff] [blame] | 47 | #include "net/socket/ssl_client_socket.h" |
[email protected] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame] | 48 | #include "net/socket/stream_socket.h" |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 49 | #include "net/socket/transport_connect_job.h" |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 50 | #include "net/ssl/ssl_cert_request_info.h" |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 51 | #include "net/test/gtest_util.h" |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 52 | #include "net/test/test_with_scoped_task_environment.h" |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 53 | #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 54 | #include "testing/gmock/include/gmock/gmock.h" |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 55 | #include "testing/gtest/include/gtest/gtest.h" |
| 56 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 57 | using net::test::IsError; |
| 58 | using net::test::IsOk; |
| 59 | |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 60 | using ::testing::Invoke; |
| 61 | using ::testing::Return; |
| 62 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 63 | namespace net { |
| 64 | |
| 65 | namespace { |
| 66 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 67 | const int kDefaultMaxSockets = 4; |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 68 | const int kDefaultMaxSocketsPerGroup = 2; |
Tarun Bansal | a763509 | 2019-02-20 10:00:59 | [diff] [blame] | 69 | constexpr base::TimeDelta kUnusedIdleSocketTimeout = |
| 70 | base::TimeDelta::FromSeconds(10); |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 71 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 72 | ClientSocketPool::GroupId TestGroupId(const std::string& host, |
| 73 | int port = 80, |
| 74 | ClientSocketPool::SocketType socket_type = |
| 75 | ClientSocketPool::SocketType::kHttp, |
| 76 | bool privacy_mode = false) { |
| 77 | return ClientSocketPool::GroupId(HostPortPair(host, port), socket_type, |
| 78 | privacy_mode); |
| 79 | } |
| 80 | |
Matt Menke | 84d11e56 | 2019-03-27 00:11:19 | [diff] [blame] | 81 | // Returns a ClientSocketPool::SocketParams that will never be used to |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 82 | // create a real TreansportConnectJob. |
Matt Menke | 84d11e56 | 2019-03-27 00:11:19 | [diff] [blame] | 83 | scoped_refptr<ClientSocketPool::SocketParams> CreateDummyParams() { |
| 84 | return ClientSocketPool::SocketParams::CreateFromTransportSocketParams( |
| 85 | base::MakeRefCounted<TransportSocketParams>(HostPortPair("ignored", 80), |
| 86 | OnHostResolutionCallback())); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 87 | } |
| 88 | |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 89 | // Make sure |handle| sets load times correctly when it has been assigned a |
| 90 | // reused socket. |
| 91 | void TestLoadTimingInfoConnectedReused(const ClientSocketHandle& handle) { |
| 92 | LoadTimingInfo load_timing_info; |
| 93 | // Only pass true in as |is_reused|, as in general, HttpStream types should |
| 94 | // have stricter concepts of reuse than socket pools. |
| 95 | EXPECT_TRUE(handle.GetLoadTimingInfo(true, &load_timing_info)); |
| 96 | |
| 97 | EXPECT_EQ(true, load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 98 | EXPECT_NE(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 99 | |
[email protected] | b258e079 | 2013-01-12 07:11:59 | [diff] [blame] | 100 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 101 | ExpectLoadTimingHasOnlyConnectionTimes(load_timing_info); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | // Make sure |handle| sets load times correctly when it has been assigned a |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 105 | // fresh socket. Also runs TestLoadTimingInfoConnectedReused, since the owner |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 106 | // of a connection where |is_reused| is false may consider the connection |
| 107 | // reused. |
| 108 | void TestLoadTimingInfoConnectedNotReused(const ClientSocketHandle& handle) { |
| 109 | EXPECT_FALSE(handle.is_reused()); |
| 110 | |
| 111 | LoadTimingInfo load_timing_info; |
| 112 | EXPECT_TRUE(handle.GetLoadTimingInfo(false, &load_timing_info)); |
| 113 | |
| 114 | EXPECT_FALSE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 115 | EXPECT_NE(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 116 | |
[email protected] | b258e079 | 2013-01-12 07:11:59 | [diff] [blame] | 117 | ExpectConnectTimingHasTimes(load_timing_info.connect_timing, |
| 118 | CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY); |
| 119 | ExpectLoadTimingHasOnlyConnectionTimes(load_timing_info); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 120 | |
| 121 | TestLoadTimingInfoConnectedReused(handle); |
| 122 | } |
| 123 | |
| 124 | // Make sure |handle| sets load times correctly, in the case that it does not |
| 125 | // currently have a socket. |
| 126 | void TestLoadTimingInfoNotConnected(const ClientSocketHandle& handle) { |
| 127 | // Should only be set to true once a socket is assigned, if at all. |
| 128 | EXPECT_FALSE(handle.is_reused()); |
| 129 | |
| 130 | LoadTimingInfo load_timing_info; |
| 131 | EXPECT_FALSE(handle.GetLoadTimingInfo(false, &load_timing_info)); |
| 132 | |
| 133 | EXPECT_FALSE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 134 | EXPECT_EQ(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 135 | |
[email protected] | b258e079 | 2013-01-12 07:11:59 | [diff] [blame] | 136 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 137 | ExpectLoadTimingHasOnlyConnectionTimes(load_timing_info); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 138 | } |
| 139 | |
[email protected] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame] | 140 | class MockClientSocket : public StreamSocket { |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 141 | public: |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 142 | explicit MockClientSocket(net::NetLog* net_log) |
| 143 | : connected_(false), |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 144 | has_unread_data_(false), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 145 | net_log_(NetLogWithSource::Make(net_log, NetLogSourceType::SOCKET)), |
Charlie Harrison | 3e4c062 | 2018-05-13 15:44:30 | [diff] [blame] | 146 | was_used_to_convey_data_(false) {} |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 147 | |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 148 | // Sets whether the socket has unread data. If true, the next call to Read() |
| 149 | // will return 1 byte and IsConnectedAndIdle() will return false. |
| 150 | void set_has_unread_data(bool has_unread_data) { |
| 151 | has_unread_data_ = has_unread_data; |
| 152 | } |
| 153 | |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 154 | // Socket implementation. |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 155 | int Read(IOBuffer* /* buf */, |
| 156 | int len, |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 157 | CompletionOnceCallback /* callback */) override { |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 158 | if (has_unread_data_ && len > 0) { |
| 159 | has_unread_data_ = false; |
| 160 | was_used_to_convey_data_ = true; |
| 161 | return 1; |
| 162 | } |
[email protected] | e86df8dc | 2013-03-30 13:18:28 | [diff] [blame] | 163 | return ERR_UNEXPECTED; |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 164 | } |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 165 | |
[email protected] | a2b2cfc | 2017-12-06 09:06:08 | [diff] [blame] | 166 | int Write( |
| 167 | IOBuffer* /* buf */, |
| 168 | int len, |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 169 | CompletionOnceCallback /* callback */, |
[email protected] | a2b2cfc | 2017-12-06 09:06:08 | [diff] [blame] | 170 | const NetworkTrafficAnnotationTag& /*traffic_annotation*/) override { |
[email protected] | 0f873e8 | 2010-09-02 16:09:01 | [diff] [blame] | 171 | was_used_to_convey_data_ = true; |
| 172 | return len; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 173 | } |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 174 | int SetReceiveBufferSize(int32_t size) override { return OK; } |
| 175 | int SetSendBufferSize(int32_t size) override { return OK; } |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 176 | |
[email protected] | dbf036f | 2011-12-06 23:33:24 | [diff] [blame] | 177 | // StreamSocket implementation. |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 178 | int Connect(CompletionOnceCallback callback) override { |
[email protected] | dbf036f | 2011-12-06 23:33:24 | [diff] [blame] | 179 | connected_ = true; |
| 180 | return OK; |
| 181 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 182 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 183 | void Disconnect() override { connected_ = false; } |
| 184 | bool IsConnected() const override { return connected_; } |
| 185 | bool IsConnectedAndIdle() const override { |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 186 | return connected_ && !has_unread_data_; |
| 187 | } |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 188 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 189 | int GetPeerAddress(IPEndPoint* /* address */) const override { |
[email protected] | 9f864b3 | 2010-01-20 15:01:16 | [diff] [blame] | 190 | return ERR_UNEXPECTED; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 191 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 192 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 193 | int GetLocalAddress(IPEndPoint* /* address */) const override { |
[email protected] | e7f74da | 2011-04-19 23:49:35 | [diff] [blame] | 194 | return ERR_UNEXPECTED; |
| 195 | } |
| 196 | |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 197 | const NetLogWithSource& NetLog() const override { return net_log_; } |
[email protected] | a2006ece | 2010-04-23 16:44:02 | [diff] [blame] | 198 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 199 | bool WasEverUsed() const override { return was_used_to_convey_data_; } |
tfarina | 2846404c | 2016-12-25 14:31:37 | [diff] [blame] | 200 | bool WasAlpnNegotiated() const override { return false; } |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 201 | NextProto GetNegotiatedProtocol() const override { return kProtoUnknown; } |
| 202 | bool GetSSLInfo(SSLInfo* ssl_info) override { return false; } |
ttuttle | 23fdb7b | 2015-05-15 01:28:03 | [diff] [blame] | 203 | void GetConnectionAttempts(ConnectionAttempts* out) const override { |
| 204 | out->clear(); |
| 205 | } |
| 206 | void ClearConnectionAttempts() override {} |
| 207 | void AddConnectionAttempts(const ConnectionAttempts& attempts) override {} |
tbansal | f82cc8e | 2015-10-14 20:05:49 | [diff] [blame] | 208 | int64_t GetTotalReceivedBytes() const override { |
| 209 | NOTIMPLEMENTED(); |
| 210 | return 0; |
| 211 | } |
Paul Jensen | 0f49dec | 2017-12-12 23:39:58 | [diff] [blame] | 212 | void ApplySocketTag(const SocketTag& tag) override {} |
[email protected] | 9b5614a | 2010-08-25 20:29:45 | [diff] [blame] | 213 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 214 | private: |
| 215 | bool connected_; |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 216 | bool has_unread_data_; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 217 | NetLogWithSource net_log_; |
[email protected] | 0f873e8 | 2010-09-02 16:09:01 | [diff] [blame] | 218 | bool was_used_to_convey_data_; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 219 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 220 | DISALLOW_COPY_AND_ASSIGN(MockClientSocket); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 221 | }; |
| 222 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 223 | class TestConnectJob; |
| 224 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 225 | class MockClientSocketFactory : public ClientSocketFactory { |
| 226 | public: |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 227 | MockClientSocketFactory() : allocation_count_(0) {} |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 228 | |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 229 | std::unique_ptr<DatagramClientSocket> CreateDatagramClientSocket( |
[email protected] | 5370c01 | 2011-06-29 03:47:04 | [diff] [blame] | 230 | DatagramSocket::BindType bind_type, |
[email protected] | 98b0e58 | 2011-06-22 14:31:41 | [diff] [blame] | 231 | NetLog* net_log, |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 232 | const NetLogSource& source) override { |
[email protected] | 98b0e58 | 2011-06-22 14:31:41 | [diff] [blame] | 233 | NOTREACHED(); |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 234 | return std::unique_ptr<DatagramClientSocket>(); |
[email protected] | 98b0e58 | 2011-06-22 14:31:41 | [diff] [blame] | 235 | } |
| 236 | |
Helen Li | d5bb922 | 2018-04-12 15:33:09 | [diff] [blame] | 237 | std::unique_ptr<TransportClientSocket> CreateTransportClientSocket( |
[email protected] | 0a0b768 | 2010-08-25 17:08:07 | [diff] [blame] | 238 | const AddressList& addresses, |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 239 | std::unique_ptr< |
| 240 | SocketPerformanceWatcher> /* socket_performance_watcher */, |
[email protected] | 0a0b768 | 2010-08-25 17:08:07 | [diff] [blame] | 241 | NetLog* /* net_log */, |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 242 | const NetLogSource& /*source*/) override { |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 243 | allocation_count_++; |
Helen Li | d5bb922 | 2018-04-12 15:33:09 | [diff] [blame] | 244 | return nullptr; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 245 | } |
| 246 | |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 247 | std::unique_ptr<SSLClientSocket> CreateSSLClientSocket( |
Matt Menke | 841fc41 | 2019-03-05 23:20:12 | [diff] [blame] | 248 | std::unique_ptr<StreamSocket> stream_socket, |
[email protected] | 4f4de7e6 | 2010-11-12 19:55:27 | [diff] [blame] | 249 | const HostPortPair& host_and_port, |
[email protected] | 7ab5bbd1 | 2010-10-19 13:33:21 | [diff] [blame] | 250 | const SSLConfig& ssl_config, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 251 | const SSLClientSocketContext& context) override { |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 252 | NOTIMPLEMENTED(); |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 253 | return std::unique_ptr<SSLClientSocket>(); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 254 | } |
Matt Menke | fd95692 | 2019-02-04 23:44:03 | [diff] [blame] | 255 | |
Matt Menke | 52cd95a | 2019-02-08 06:16:27 | [diff] [blame] | 256 | std::unique_ptr<ProxyClientSocket> CreateProxyClientSocket( |
| 257 | std::unique_ptr<StreamSocket> stream_socket, |
| 258 | const std::string& user_agent, |
| 259 | const HostPortPair& endpoint, |
| 260 | const ProxyServer& proxy_server, |
| 261 | HttpAuthController* http_auth_controller, |
| 262 | bool tunnel, |
| 263 | bool using_spdy, |
| 264 | NextProto negotiated_protocol, |
| 265 | ProxyDelegate* proxy_delegate, |
| 266 | bool is_https_proxy, |
| 267 | const NetworkTrafficAnnotationTag& traffic_annotation) override { |
| 268 | NOTIMPLEMENTED(); |
| 269 | return nullptr; |
| 270 | } |
| 271 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 272 | void WaitForSignal(TestConnectJob* job) { waiting_jobs_.push_back(job); } |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 273 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 274 | void SignalJobs(); |
| 275 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 276 | void SignalJob(size_t job); |
| 277 | |
| 278 | void SetJobLoadState(size_t job, LoadState load_state); |
| 279 | |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 280 | // Sets the HasConnectionEstablished value of the specified job to true, |
| 281 | // without invoking the callback. |
| 282 | void SetJobHasEstablishedConnection(size_t job); |
| 283 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 284 | int allocation_count() const { return allocation_count_; } |
| 285 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 286 | private: |
| 287 | int allocation_count_; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 288 | std::vector<TestConnectJob*> waiting_jobs_; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 289 | }; |
| 290 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 291 | class TestConnectJob : public ConnectJob { |
| 292 | public: |
| 293 | enum JobType { |
| 294 | kMockJob, |
| 295 | kMockFailingJob, |
| 296 | kMockPendingJob, |
| 297 | kMockPendingFailingJob, |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 298 | kMockWaitingJob, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 299 | |
| 300 | // Certificate errors return a socket in addition to an error code. |
| 301 | kMockCertErrorJob, |
| 302 | kMockPendingCertErrorJob, |
| 303 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 304 | kMockAdditionalErrorStateJob, |
| 305 | kMockPendingAdditionalErrorStateJob, |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 306 | kMockUnreadDataJob, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 307 | |
| 308 | kMockAuthChallengeOnceJob, |
| 309 | kMockAuthChallengeTwiceJob, |
| 310 | kMockAuthChallengeOnceFailingJob, |
| 311 | kMockAuthChallengeTwiceFailingJob, |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 312 | }; |
| 313 | |
[email protected] | 994d493 | 2010-07-12 17:55:13 | [diff] [blame] | 314 | // The kMockPendingJob uses a slight delay before allowing the connect |
| 315 | // to complete. |
| 316 | static const int kPendingConnectDelay = 2; |
| 317 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 318 | TestConnectJob(JobType job_type, |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 319 | RequestPriority request_priority, |
| 320 | SocketTag socket_tag, |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 321 | base::TimeDelta timeout_duration, |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 322 | const CommonConnectJobParams* common_connect_job_params, |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 323 | ConnectJob::Delegate* delegate, |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 324 | MockClientSocketFactory* client_socket_factory) |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 325 | : ConnectJob(request_priority, |
| 326 | socket_tag, |
Matt Menke | 1a6c92d | 2019-02-23 00:25:38 | [diff] [blame] | 327 | timeout_duration, |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 328 | common_connect_job_params, |
Matt Menke | 1a6c92d | 2019-02-23 00:25:38 | [diff] [blame] | 329 | delegate, |
| 330 | nullptr /* net_log */, |
| 331 | NetLogSourceType::TRANSPORT_CONNECT_JOB, |
| 332 | NetLogEventType::TRANSPORT_CONNECT_JOB_CONNECT), |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 333 | job_type_(job_type), |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 334 | client_socket_factory_(client_socket_factory), |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 335 | load_state_(LOAD_STATE_IDLE), |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 336 | has_established_connection_(false), |
[email protected] | d5492c5 | 2013-11-10 20:44:39 | [diff] [blame] | 337 | store_additional_error_state_(false), |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 338 | weak_factory_(this) {} |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 339 | |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 340 | void Signal() { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 341 | DoConnect(waiting_success_, true /* async */, false /* recoverable */); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 342 | } |
| 343 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 344 | void set_load_state(LoadState load_state) { load_state_ = load_state; } |
| 345 | |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 346 | void set_has_established_connection() { |
| 347 | DCHECK(!has_established_connection_); |
| 348 | has_established_connection_ = true; |
| 349 | } |
| 350 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 351 | // From ConnectJob: |
| 352 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 353 | LoadState GetLoadState() const override { return load_state_; } |
[email protected] | 4645135 | 2009-09-01 14:54:21 | [diff] [blame] | 354 | |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 355 | bool HasEstablishedConnection() const override { |
| 356 | return has_established_connection_; |
| 357 | } |
| 358 | |
Matt Menke | 6f84d1f1 | 2019-04-11 19:26:47 | [diff] [blame] | 359 | bool IsSSLError() const override { return store_additional_error_state_; } |
| 360 | |
| 361 | scoped_refptr<SSLCertRequestInfo> GetCertRequestInfo() override { |
| 362 | if (store_additional_error_state_) |
| 363 | return base::MakeRefCounted<SSLCertRequestInfo>(); |
| 364 | return nullptr; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 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); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 759 | handle.set_ssl_cert_request_info(base::MakeRefCounted<SSLCertRequestInfo>()); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 760 | EXPECT_EQ( |
| 761 | ERR_CONNECTION_FAILED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 762 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 763 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 764 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 765 | log.bound())); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 766 | EXPECT_FALSE(handle.socket()); |
| 767 | EXPECT_FALSE(handle.is_ssl_error()); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 768 | EXPECT_FALSE(handle.ssl_cert_request_info()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 769 | TestLoadTimingInfoNotConnected(handle); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 770 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 771 | TestNetLogEntry::List entries; |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 772 | log.GetEntries(&entries); |
| 773 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 774 | EXPECT_EQ(4u, entries.size()); |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 775 | EXPECT_TRUE(LogContainsEvent( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 776 | entries, 0, NetLogEventType::TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 777 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 778 | EXPECT_TRUE(LogContainsBeginEvent(entries, 1, NetLogEventType::SOCKET_POOL)); |
| 779 | EXPECT_TRUE(LogContainsEvent( |
| 780 | entries, 2, NetLogEventType::SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
| 781 | NetLogEventPhase::NONE)); |
| 782 | EXPECT_TRUE(LogContainsEndEvent(entries, 3, NetLogEventType::SOCKET_POOL)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 783 | } |
| 784 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 785 | // Make sure different groups do not share sockets. |
| 786 | TEST_F(ClientSocketPoolBaseTest, GroupSeparation) { |
| 787 | CreatePool(1000 /* max_sockets */, 2 /* max_sockets_per_group */); |
| 788 | |
| 789 | const HostPortPair kHostPortPairs[] = { |
| 790 | {"a", 80}, |
| 791 | {"a", 443}, |
| 792 | {"b", 80}, |
| 793 | }; |
| 794 | |
| 795 | const ClientSocketPool::SocketType kSocketTypes[] = { |
| 796 | ClientSocketPool::SocketType::kHttp, |
| 797 | ClientSocketPool::SocketType::kSsl, |
| 798 | ClientSocketPool::SocketType::kSslVersionInterferenceProbe, |
| 799 | ClientSocketPool::SocketType::kFtp, |
| 800 | }; |
| 801 | |
| 802 | const bool kPrivacyModes[] = {false, true}; |
| 803 | |
| 804 | int total_idle_sockets = 0; |
| 805 | |
| 806 | // Walk through each GroupId, making sure that requesting a socket for one |
| 807 | // group does not return a previously connected socket for another group. |
| 808 | for (const auto& host_port_pair : kHostPortPairs) { |
| 809 | SCOPED_TRACE(host_port_pair.ToString()); |
| 810 | for (const auto& socket_type : kSocketTypes) { |
| 811 | SCOPED_TRACE(static_cast<int>(socket_type)); |
| 812 | for (const auto& privacy_mode : kPrivacyModes) { |
| 813 | SCOPED_TRACE(privacy_mode); |
| 814 | |
| 815 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 816 | |
| 817 | ClientSocketPool::GroupId group_id(host_port_pair, socket_type, |
| 818 | privacy_mode); |
| 819 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 820 | EXPECT_FALSE(pool_->HasGroupForTesting(group_id)); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 821 | |
| 822 | TestCompletionCallback callback; |
| 823 | ClientSocketHandle handle; |
| 824 | |
| 825 | // Since the group is empty, requesting a socket should not complete |
| 826 | // synchronously. |
| 827 | EXPECT_THAT( |
| 828 | handle.Init(group_id, params_, DEFAULT_PRIORITY, SocketTag(), |
| 829 | ClientSocketPool::RespectLimits::ENABLED, |
| 830 | callback.callback(), |
| 831 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 832 | NetLogWithSource()), |
| 833 | IsError(ERR_IO_PENDING)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 834 | EXPECT_TRUE(pool_->HasGroupForTesting(group_id)); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 835 | EXPECT_EQ(total_idle_sockets, pool_->IdleSocketCount()); |
| 836 | |
| 837 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 838 | EXPECT_TRUE(handle.socket()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 839 | EXPECT_TRUE(pool_->HasGroupForTesting(group_id)); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 840 | EXPECT_EQ(total_idle_sockets, pool_->IdleSocketCount()); |
| 841 | |
| 842 | // Return socket to pool. |
| 843 | handle.Reset(); |
| 844 | EXPECT_EQ(total_idle_sockets + 1, pool_->IdleSocketCount()); |
| 845 | |
| 846 | // Requesting a socket again should return the same socket as before, so |
| 847 | // should complete synchronously. |
| 848 | EXPECT_THAT( |
| 849 | handle.Init(group_id, params_, DEFAULT_PRIORITY, SocketTag(), |
| 850 | ClientSocketPool::RespectLimits::ENABLED, |
| 851 | callback.callback(), |
| 852 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 853 | NetLogWithSource()), |
| 854 | IsOk()); |
| 855 | EXPECT_TRUE(handle.socket()); |
| 856 | EXPECT_EQ(total_idle_sockets, pool_->IdleSocketCount()); |
| 857 | |
| 858 | // Return socket to pool again. |
| 859 | handle.Reset(); |
| 860 | EXPECT_EQ(total_idle_sockets + 1, pool_->IdleSocketCount()); |
| 861 | |
| 862 | ++total_idle_sockets; |
| 863 | } |
| 864 | } |
| 865 | } |
| 866 | } |
| 867 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 868 | TEST_F(ClientSocketPoolBaseTest, TotalLimit) { |
| 869 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 870 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 871 | // TODO(eroman): Check that the NetLog contains this event. |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 872 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 873 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 874 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
| 875 | EXPECT_THAT(StartRequest(TestGroupId("c"), DEFAULT_PRIORITY), IsOk()); |
| 876 | EXPECT_THAT(StartRequest(TestGroupId("d"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 877 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 878 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 879 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 880 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 881 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 882 | EXPECT_THAT(StartRequest(TestGroupId("e"), DEFAULT_PRIORITY), |
| 883 | IsError(ERR_IO_PENDING)); |
| 884 | EXPECT_THAT(StartRequest(TestGroupId("f"), DEFAULT_PRIORITY), |
| 885 | IsError(ERR_IO_PENDING)); |
| 886 | EXPECT_THAT(StartRequest(TestGroupId("g"), DEFAULT_PRIORITY), |
| 887 | IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 888 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 889 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 890 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 891 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 892 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 893 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 894 | |
| 895 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 896 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 897 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 898 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 899 | EXPECT_EQ(5, GetOrderOfRequest(5)); |
| 900 | EXPECT_EQ(6, GetOrderOfRequest(6)); |
| 901 | EXPECT_EQ(7, GetOrderOfRequest(7)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 902 | |
| 903 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 904 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(8)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 905 | } |
| 906 | |
| 907 | TEST_F(ClientSocketPoolBaseTest, TotalLimitReachedNewGroup) { |
| 908 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 909 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 910 | // TODO(eroman): Check that the NetLog contains this event. |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 911 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 912 | // Reach all limits: max total sockets, and max sockets per group. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 913 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 914 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 915 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
| 916 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 917 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 918 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 919 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 920 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 921 | |
| 922 | // Now create a new group and verify that we don't starve it. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 923 | EXPECT_THAT(StartRequest(TestGroupId("c"), DEFAULT_PRIORITY), |
| 924 | IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 925 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 926 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 927 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 928 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 929 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 930 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 931 | |
| 932 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 933 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 934 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 935 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 936 | EXPECT_EQ(5, GetOrderOfRequest(5)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 937 | |
| 938 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 939 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(6)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 940 | } |
| 941 | |
| 942 | TEST_F(ClientSocketPoolBaseTest, TotalLimitRespectsPriority) { |
| 943 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 944 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 945 | EXPECT_THAT(StartRequest(TestGroupId("b"), LOWEST), IsOk()); |
| 946 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsOk()); |
| 947 | EXPECT_THAT(StartRequest(TestGroupId("b"), HIGHEST), IsOk()); |
| 948 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 949 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 950 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 951 | client_socket_factory_.allocation_count()); |
| 952 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 953 | EXPECT_THAT(StartRequest(TestGroupId("c"), LOWEST), IsError(ERR_IO_PENDING)); |
| 954 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 955 | EXPECT_THAT(StartRequest(TestGroupId("b"), HIGHEST), IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 956 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 957 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 958 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 959 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 960 | |
| 961 | // First 4 requests don't have to wait, and finish in order. |
| 962 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 963 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 964 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 965 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 966 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 967 | // Request ("b", HIGHEST) has the highest priority, then (TestGroupId("a"), |
| 968 | // MEDIUM), and then ("c", LOWEST). |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 969 | EXPECT_EQ(7, GetOrderOfRequest(5)); |
| 970 | EXPECT_EQ(6, GetOrderOfRequest(6)); |
| 971 | EXPECT_EQ(5, GetOrderOfRequest(7)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 972 | |
| 973 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 974 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(9)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 975 | } |
| 976 | |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 977 | // Test reprioritizing a request before completion doesn't interfere with |
| 978 | // its completion. |
| 979 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeOne) { |
| 980 | CreatePool(kDefaultMaxSockets, 1); |
| 981 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 982 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 983 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 984 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 985 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 986 | |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 987 | request(1)->handle()->SetPriority(HIGHEST); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 988 | |
| 989 | ReleaseOneConnection(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 990 | |
| 991 | EXPECT_TRUE(request(1)->handle()->socket()); |
| 992 | } |
| 993 | |
| 994 | // Reprioritize a request up past another one and make sure that changes the |
| 995 | // completion order. |
| 996 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeUpReorder) { |
| 997 | CreatePool(kDefaultMaxSockets, 1); |
| 998 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 999 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1000 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1001 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1002 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1003 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1004 | EXPECT_FALSE(request(2)->handle()->socket()); |
| 1005 | |
| 1006 | request(2)->handle()->SetPriority(HIGHEST); |
| 1007 | |
| 1008 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1009 | |
| 1010 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1011 | EXPECT_EQ(3, GetOrderOfRequest(2)); |
| 1012 | EXPECT_EQ(2, GetOrderOfRequest(3)); |
| 1013 | } |
| 1014 | |
| 1015 | // Reprioritize a request without changing relative priorities and check |
| 1016 | // that the order doesn't change. |
| 1017 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeUpNoReorder) { |
| 1018 | CreatePool(kDefaultMaxSockets, 1); |
| 1019 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1020 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1021 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1022 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1023 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1024 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1025 | EXPECT_FALSE(request(2)->handle()->socket()); |
| 1026 | |
| 1027 | request(2)->handle()->SetPriority(MEDIUM); |
| 1028 | |
| 1029 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1030 | |
| 1031 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1032 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1033 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 1034 | } |
| 1035 | |
| 1036 | // Reprioritize a request past down another one and make sure that changes the |
| 1037 | // completion order. |
| 1038 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeDownReorder) { |
| 1039 | CreatePool(kDefaultMaxSockets, 1); |
| 1040 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1041 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1042 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1043 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1044 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1045 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1046 | EXPECT_FALSE(request(2)->handle()->socket()); |
| 1047 | |
| 1048 | request(1)->handle()->SetPriority(LOW); |
| 1049 | |
| 1050 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1051 | |
| 1052 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1053 | EXPECT_EQ(3, GetOrderOfRequest(2)); |
| 1054 | EXPECT_EQ(2, GetOrderOfRequest(3)); |
| 1055 | } |
| 1056 | |
| 1057 | // Reprioritize a request to the same level as another and confirm it is |
| 1058 | // put after the old request. |
| 1059 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeResetFIFO) { |
| 1060 | CreatePool(kDefaultMaxSockets, 1); |
| 1061 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1062 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1063 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1064 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1065 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1066 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1067 | EXPECT_FALSE(request(2)->handle()->socket()); |
| 1068 | |
| 1069 | request(1)->handle()->SetPriority(MEDIUM); |
| 1070 | |
| 1071 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1072 | |
| 1073 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1074 | EXPECT_EQ(3, GetOrderOfRequest(2)); |
| 1075 | EXPECT_EQ(2, GetOrderOfRequest(3)); |
| 1076 | } |
| 1077 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1078 | TEST_F(ClientSocketPoolBaseTest, TotalLimitRespectsGroupLimit) { |
| 1079 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1080 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1081 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsOk()); |
| 1082 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsOk()); |
| 1083 | EXPECT_THAT(StartRequest(TestGroupId("b"), HIGHEST), IsOk()); |
| 1084 | EXPECT_THAT(StartRequest(TestGroupId("b"), MEDIUM), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1085 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1086 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1087 | client_socket_factory_.allocation_count()); |
| 1088 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1089 | EXPECT_THAT(StartRequest(TestGroupId("c"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1090 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 1091 | EXPECT_THAT(StartRequest(TestGroupId("b"), HIGHEST), IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1092 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1093 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1094 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1095 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1096 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1097 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1098 | |
| 1099 | // First 4 requests don't have to wait, and finish in order. |
| 1100 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1101 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1102 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 1103 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 1104 | |
| 1105 | // Request ("b", 7) has the highest priority, but we can't make new socket for |
| 1106 | // group "b", because it has reached the per-group limit. Then we make |
| 1107 | // socket for ("c", 6), because it has higher priority than ("a", 4), |
| 1108 | // and we still can't make a socket for group "b". |
| 1109 | EXPECT_EQ(5, GetOrderOfRequest(5)); |
| 1110 | EXPECT_EQ(6, GetOrderOfRequest(6)); |
| 1111 | EXPECT_EQ(7, GetOrderOfRequest(7)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1112 | |
| 1113 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1114 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(8)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1115 | } |
| 1116 | |
| 1117 | // Make sure that we count connecting sockets against the total limit. |
| 1118 | TEST_F(ClientSocketPoolBaseTest, TotalLimitCountsConnectingSockets) { |
| 1119 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1120 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1121 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1122 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
| 1123 | EXPECT_THAT(StartRequest(TestGroupId("c"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1124 | |
| 1125 | // Create one asynchronous request. |
| 1126 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1127 | EXPECT_THAT(StartRequest(TestGroupId("d"), DEFAULT_PRIORITY), |
| 1128 | IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1129 | |
[email protected] | 6b17538 | 2009-10-13 06:47:47 | [diff] [blame] | 1130 | // We post all of our delayed tasks with a 2ms delay. I.e. they don't |
| 1131 | // actually become pending until 2ms after they have been created. In order |
| 1132 | // to flush all tasks, we need to wait so that we know there are no |
| 1133 | // soon-to-be-pending tasks waiting. |
Alex Clarke | 0def209 | 2018-12-10 12:01:45 | [diff] [blame] | 1134 | FastForwardBy(base::TimeDelta::FromMilliseconds(10)); |
[email protected] | 6b17538 | 2009-10-13 06:47:47 | [diff] [blame] | 1135 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1136 | // The next synchronous request should wait for its turn. |
| 1137 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1138 | EXPECT_THAT(StartRequest(TestGroupId("e"), DEFAULT_PRIORITY), |
| 1139 | IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1140 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1141 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1142 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1143 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1144 | client_socket_factory_.allocation_count()); |
| 1145 | |
| 1146 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1147 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1148 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 1149 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1150 | EXPECT_EQ(5, GetOrderOfRequest(5)); |
| 1151 | |
| 1152 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1153 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(6)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1154 | } |
| 1155 | |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1156 | TEST_F(ClientSocketPoolBaseTest, CorrectlyCountStalledGroups) { |
| 1157 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 1158 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 1159 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1160 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1161 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1162 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1163 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1164 | |
| 1165 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 1166 | |
| 1167 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1168 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1169 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), |
| 1170 | IsError(ERR_IO_PENDING)); |
| 1171 | EXPECT_THAT(StartRequest(TestGroupId("c"), DEFAULT_PRIORITY), |
| 1172 | IsError(ERR_IO_PENDING)); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1173 | |
| 1174 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1175 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1176 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1177 | EXPECT_EQ(kDefaultMaxSockets + 1, client_socket_factory_.allocation_count()); |
[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 + 2, client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1180 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
| 1181 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1182 | EXPECT_EQ(kDefaultMaxSockets + 2, client_socket_factory_.allocation_count()); |
| 1183 | } |
| 1184 | |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 1185 | TEST_F(ClientSocketPoolBaseTest, StallAndThenCancelAndTriggerAvailableSocket) { |
| 1186 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 1187 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1188 | |
| 1189 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1190 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1191 | EXPECT_EQ( |
| 1192 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1193 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1194 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1195 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 1196 | NetLogWithSource())); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 1197 | |
| 1198 | ClientSocketHandle handles[4]; |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 1199 | for (size_t i = 0; i < base::size(handles); ++i) { |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1200 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1201 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1202 | handles[i].Init( |
| 1203 | TestGroupId("b"), params_, DEFAULT_PRIORITY, SocketTag(), |
| 1204 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1205 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 1206 | NetLogWithSource())); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 1207 | } |
| 1208 | |
| 1209 | // One will be stalled, cancel all the handles now. |
| 1210 | // This should hit the OnAvailableSocketSlot() code where we previously had |
| 1211 | // stalled groups, but no longer have any. |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 1212 | for (size_t i = 0; i < base::size(handles); ++i) |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 1213 | handles[i].Reset(); |
| 1214 | } |
| 1215 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1216 | TEST_F(ClientSocketPoolBaseTest, CancelStalledSocketAtSocketLimit) { |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1217 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1218 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 1219 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1220 | { |
| 1221 | ClientSocketHandle handles[kDefaultMaxSockets]; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1222 | TestCompletionCallback callbacks[kDefaultMaxSockets]; |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1223 | for (int i = 0; i < kDefaultMaxSockets; ++i) { |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1224 | EXPECT_EQ(OK, handles[i].Init(TestGroupId(base::NumberToString(i)), |
| 1225 | params_, DEFAULT_PRIORITY, SocketTag(), |
Paul Jensen | 8d6f87ec | 2018-01-13 00:46:54 | [diff] [blame] | 1226 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1227 | callbacks[i].callback(), |
| 1228 | ClientSocketPool::ProxyAuthCallback(), |
| 1229 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1230 | } |
| 1231 | |
| 1232 | // Force a stalled group. |
| 1233 | ClientSocketHandle stalled_handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1234 | TestCompletionCallback callback; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1235 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1236 | stalled_handle.Init( |
| 1237 | TestGroupId("foo"), params_, DEFAULT_PRIORITY, SocketTag(), |
| 1238 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1239 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 1240 | NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1241 | |
| 1242 | // Cancel the stalled request. |
| 1243 | stalled_handle.Reset(); |
| 1244 | |
| 1245 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1246 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 1247 | |
| 1248 | // Dropping out of scope will close all handles and return them to idle. |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1249 | } |
| 1250 | |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1251 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1252 | EXPECT_EQ(kDefaultMaxSockets, pool_->IdleSocketCount()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1253 | } |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1254 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1255 | TEST_F(ClientSocketPoolBaseTest, CancelPendingSocketAtSocketLimit) { |
| 1256 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1257 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 1258 | |
| 1259 | { |
| 1260 | ClientSocketHandle handles[kDefaultMaxSockets]; |
| 1261 | for (int i = 0; i < kDefaultMaxSockets; ++i) { |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1262 | TestCompletionCallback callback; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1263 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1264 | handles[i].Init(TestGroupId(base::NumberToString(i)), params_, |
| 1265 | DEFAULT_PRIORITY, SocketTag(), |
| 1266 | ClientSocketPool::RespectLimits::ENABLED, |
| 1267 | callback.callback(), |
| 1268 | ClientSocketPool::ProxyAuthCallback(), |
| 1269 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1270 | } |
| 1271 | |
| 1272 | // Force a stalled group. |
| 1273 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1274 | ClientSocketHandle stalled_handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1275 | TestCompletionCallback callback; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1276 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1277 | stalled_handle.Init( |
| 1278 | TestGroupId("foo"), params_, DEFAULT_PRIORITY, SocketTag(), |
| 1279 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1280 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 1281 | NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1282 | |
| 1283 | // Since it is stalled, it should have no connect jobs. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1284 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("foo"))); |
| 1285 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 1286 | TestGroupId("foo"))); |
| 1287 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroupForTesting( |
| 1288 | TestGroupId("foo"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1289 | |
| 1290 | // Cancel the stalled request. |
| 1291 | handles[0].Reset(); |
| 1292 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1293 | // Now we should have a connect job. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1294 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("foo"))); |
| 1295 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 1296 | TestGroupId("foo"))); |
| 1297 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroupForTesting( |
| 1298 | TestGroupId("foo"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1299 | |
| 1300 | // The stalled socket should connect. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1301 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1302 | |
| 1303 | EXPECT_EQ(kDefaultMaxSockets + 1, |
| 1304 | client_socket_factory_.allocation_count()); |
| 1305 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1306 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("foo"))); |
| 1307 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 1308 | TestGroupId("foo"))); |
| 1309 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroupForTesting( |
| 1310 | TestGroupId("foo"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1311 | |
| 1312 | // Dropping out of scope will close all handles and return them to idle. |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1313 | } |
| 1314 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1315 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 1316 | } |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1317 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1318 | TEST_F(ClientSocketPoolBaseTest, WaitForStalledSocketAtSocketLimit) { |
| 1319 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1320 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1321 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1322 | ClientSocketHandle stalled_handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1323 | TestCompletionCallback callback; |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1324 | { |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 1325 | EXPECT_FALSE(pool_->IsStalled()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1326 | ClientSocketHandle handles[kDefaultMaxSockets]; |
| 1327 | for (int i = 0; i < kDefaultMaxSockets; ++i) { |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1328 | TestCompletionCallback callback; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1329 | EXPECT_EQ( |
| 1330 | OK, handles[i].Init(TestGroupId(base::StringPrintf("Take 2: %d", i)), |
| 1331 | params_, DEFAULT_PRIORITY, SocketTag(), |
| 1332 | ClientSocketPool::RespectLimits::ENABLED, |
| 1333 | callback.callback(), |
| 1334 | ClientSocketPool::ProxyAuthCallback(), |
| 1335 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1336 | } |
| 1337 | |
| 1338 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1339 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 1340 | EXPECT_FALSE(pool_->IsStalled()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1341 | |
| 1342 | // Now we will hit the socket limit. |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1343 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1344 | stalled_handle.Init( |
| 1345 | TestGroupId("foo"), params_, DEFAULT_PRIORITY, SocketTag(), |
| 1346 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1347 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 1348 | NetLogWithSource())); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 1349 | EXPECT_TRUE(pool_->IsStalled()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1350 | |
| 1351 | // Dropping out of scope will close all handles and return them to idle. |
| 1352 | } |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1353 | |
| 1354 | // But if we wait for it, the released idle sockets will be closed in |
| 1355 | // preference of the waiting request. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1356 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1357 | |
| 1358 | EXPECT_EQ(kDefaultMaxSockets + 1, client_socket_factory_.allocation_count()); |
| 1359 | EXPECT_EQ(3, pool_->IdleSocketCount()); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1360 | } |
| 1361 | |
| 1362 | // Regression test for http://crbug.com/40952. |
| 1363 | TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketAtSocketLimitDeleteGroup) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1364 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 1365 | true /* enable_backup_connect_jobs */); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1366 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 1367 | |
| 1368 | for (int i = 0; i < kDefaultMaxSockets; ++i) { |
| 1369 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1370 | TestCompletionCallback callback; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1371 | EXPECT_EQ(OK, handle.Init(TestGroupId(base::NumberToString(i)), params_, |
| 1372 | DEFAULT_PRIORITY, SocketTag(), |
| 1373 | ClientSocketPool::RespectLimits::ENABLED, |
| 1374 | callback.callback(), |
| 1375 | ClientSocketPool::ProxyAuthCallback(), |
| 1376 | pool_.get(), NetLogWithSource())); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1377 | } |
| 1378 | |
| 1379 | // Flush all the DoReleaseSocket tasks. |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 1380 | base::RunLoop().RunUntilIdle(); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1381 | |
| 1382 | // Stall a group. Set a pending job so it'll trigger a backup job if we don't |
| 1383 | // reuse a socket. |
| 1384 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1385 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1386 | TestCompletionCallback callback; |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1387 | |
| 1388 | // "0" is special here, since it should be the first entry in the sorted map, |
| 1389 | // which is the one which we would close an idle socket for. We shouldn't |
| 1390 | // close an idle socket though, since we should reuse the idle socket. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1391 | EXPECT_EQ(OK, handle.Init( |
| 1392 | TestGroupId("0"), params_, DEFAULT_PRIORITY, SocketTag(), |
| 1393 | ClientSocketPool::RespectLimits::ENABLED, |
| 1394 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1395 | pool_.get(), NetLogWithSource())); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1396 | |
| 1397 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1398 | EXPECT_EQ(kDefaultMaxSockets - 1, pool_->IdleSocketCount()); |
| 1399 | } |
| 1400 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1401 | TEST_F(ClientSocketPoolBaseTest, PendingRequests) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1402 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1403 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1404 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1405 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1406 | EXPECT_THAT(StartRequest(TestGroupId("a"), IDLE), IsError(ERR_IO_PENDING)); |
| 1407 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
| 1408 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1409 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1410 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 1411 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1412 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1413 | ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1414 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
| 1415 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1416 | EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup, |
| 1417 | completion_count()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1418 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1419 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1420 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
[email protected] | c9c6f5c | 2010-07-31 01:30:03 | [diff] [blame] | 1421 | EXPECT_EQ(8, GetOrderOfRequest(3)); |
| 1422 | EXPECT_EQ(6, GetOrderOfRequest(4)); |
| 1423 | EXPECT_EQ(4, GetOrderOfRequest(5)); |
| 1424 | EXPECT_EQ(3, GetOrderOfRequest(6)); |
| 1425 | EXPECT_EQ(5, GetOrderOfRequest(7)); |
| 1426 | EXPECT_EQ(7, GetOrderOfRequest(8)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1427 | |
| 1428 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1429 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(9)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1430 | } |
| 1431 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1432 | TEST_F(ClientSocketPoolBaseTest, PendingRequests_NoKeepAlive) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1433 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1434 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1435 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1436 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1437 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
| 1438 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1439 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1440 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 1441 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1442 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1443 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1444 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1445 | for (size_t i = kDefaultMaxSocketsPerGroup; i < requests_size(); ++i) |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1446 | EXPECT_THAT(request(i)->WaitForResult(), IsOk()); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1447 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1448 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1449 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1450 | EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup, |
| 1451 | completion_count()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1452 | } |
| 1453 | |
| 1454 | // This test will start up a RequestSocket() and then immediately Cancel() it. |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1455 | // The pending connect job will be cancelled and should not call back into |
| 1456 | // ClientSocketPoolBase. |
| 1457 | TEST_F(ClientSocketPoolBaseTest, CancelRequestClearGroup) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1458 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1459 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1460 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1461 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1462 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1463 | EXPECT_EQ( |
| 1464 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1465 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1466 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1467 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 1468 | NetLogWithSource())); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1469 | handle.Reset(); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1470 | } |
| 1471 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1472 | TEST_F(ClientSocketPoolBaseTest, ConnectCancelConnect) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1473 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1474 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1475 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 1476 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1477 | TestCompletionCallback callback; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1478 | |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1479 | EXPECT_EQ( |
| 1480 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1481 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1482 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1483 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 1484 | NetLogWithSource())); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1485 | |
| 1486 | handle.Reset(); |
| 1487 | |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1488 | TestCompletionCallback callback2; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1489 | EXPECT_EQ( |
| 1490 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1491 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1492 | ClientSocketPool::RespectLimits::ENABLED, |
| 1493 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1494 | pool_.get(), NetLogWithSource())); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1495 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1496 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1497 | EXPECT_FALSE(callback.have_result()); |
| 1498 | |
| 1499 | handle.Reset(); |
| 1500 | } |
| 1501 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1502 | TEST_F(ClientSocketPoolBaseTest, CancelRequest) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1503 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1504 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1505 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1506 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1507 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
| 1508 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1509 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1510 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 1511 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1512 | |
| 1513 | // Cancel a request. |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1514 | size_t index_to_cancel = kDefaultMaxSocketsPerGroup + 2; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1515 | EXPECT_FALSE((*requests())[index_to_cancel]->handle()->is_initialized()); |
| 1516 | (*requests())[index_to_cancel]->handle()->Reset(); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1517 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1518 | ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1519 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1520 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
| 1521 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1522 | EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup - 1, |
| 1523 | completion_count()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1524 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1525 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1526 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1527 | EXPECT_EQ(5, GetOrderOfRequest(3)); |
| 1528 | EXPECT_EQ(3, GetOrderOfRequest(4)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1529 | EXPECT_EQ(ClientSocketPoolTest::kRequestNotFound, |
| 1530 | GetOrderOfRequest(5)); // Canceled request. |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1531 | EXPECT_EQ(4, GetOrderOfRequest(6)); |
| 1532 | EXPECT_EQ(6, GetOrderOfRequest(7)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1533 | |
| 1534 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1535 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(8)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1536 | } |
| 1537 | |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1538 | // Function to be used as a callback on socket request completion. It first |
| 1539 | // disconnects the successfully connected socket from the first request, and |
| 1540 | // then reuses the ClientSocketHandle to request another socket. |
| 1541 | // |
| 1542 | // |nested_callback| is called with the result of the second socket request. |
| 1543 | void RequestSocketOnComplete(ClientSocketHandle* handle, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1544 | TransportClientSocketPool* pool, |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1545 | TestConnectJobFactory* test_connect_job_factory, |
| 1546 | TestConnectJob::JobType next_job_type, |
Bence Béky | a4a5093 | 2018-08-10 13:39:41 | [diff] [blame] | 1547 | TestCompletionCallback* nested_callback, |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1548 | int first_request_result) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1549 | EXPECT_THAT(first_request_result, IsOk()); |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1550 | |
| 1551 | test_connect_job_factory->set_job_type(next_job_type); |
| 1552 | |
| 1553 | // Don't allow reuse of the socket. Disconnect it and then release it. |
| 1554 | if (handle->socket()) |
| 1555 | handle->socket()->Disconnect(); |
| 1556 | handle->Reset(); |
| 1557 | |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1558 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1559 | int rv = handle->Init( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1560 | TestGroupId("a"), CreateDummyParams(), LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1561 | ClientSocketPool::RespectLimits::ENABLED, nested_callback->callback(), |
| 1562 | ClientSocketPool::ProxyAuthCallback(), pool, NetLogWithSource()); |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1563 | if (rv != ERR_IO_PENDING) { |
| 1564 | DCHECK_EQ(TestConnectJob::kMockJob, next_job_type); |
Bence Béky | a4a5093 | 2018-08-10 13:39:41 | [diff] [blame] | 1565 | nested_callback->callback().Run(rv); |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1566 | } else { |
| 1567 | DCHECK_EQ(TestConnectJob::kMockPendingJob, next_job_type); |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1568 | } |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1569 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1570 | |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1571 | // Tests the case where a second socket is requested in a completion callback, |
| 1572 | // and the second socket connects asynchronously. Reuses the same |
| 1573 | // ClientSocketHandle for the second socket, after disconnecting the first. |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1574 | TEST_F(ClientSocketPoolBaseTest, RequestPendingJobTwice) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1575 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1576 | |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 1577 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 1578 | ClientSocketHandle handle; |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1579 | TestCompletionCallback second_result_callback; |
| 1580 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1581 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Paul Jensen | 8d6f87ec | 2018-01-13 00:46:54 | [diff] [blame] | 1582 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1583 | base::BindOnce(&RequestSocketOnComplete, &handle, pool_.get(), |
| 1584 | connect_job_factory_, TestConnectJob::kMockPendingJob, |
| 1585 | &second_result_callback), |
| 1586 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1587 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1588 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1589 | EXPECT_THAT(second_result_callback.WaitForResult(), IsOk()); |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1590 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1591 | |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1592 | // Tests the case where a second socket is requested in a completion callback, |
| 1593 | // and the second socket connects synchronously. Reuses the same |
| 1594 | // ClientSocketHandle for the second socket, after disconnecting the first. |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1595 | TEST_F(ClientSocketPoolBaseTest, RequestPendingJobThenSynchronous) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1596 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1597 | |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 1598 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 1599 | ClientSocketHandle handle; |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1600 | TestCompletionCallback second_result_callback; |
| 1601 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1602 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Paul Jensen | 8d6f87ec | 2018-01-13 00:46:54 | [diff] [blame] | 1603 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1604 | base::BindOnce(&RequestSocketOnComplete, &handle, pool_.get(), |
| 1605 | connect_job_factory_, TestConnectJob::kMockPendingJob, |
| 1606 | &second_result_callback), |
| 1607 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1608 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1609 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1610 | EXPECT_THAT(second_result_callback.WaitForResult(), IsOk()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1611 | } |
| 1612 | |
| 1613 | // Make sure that pending requests get serviced after active requests get |
| 1614 | // cancelled. |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1615 | TEST_F(ClientSocketPoolBaseTest, CancelActiveRequestWithPendingRequests) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1616 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1617 | |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 1618 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1619 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1620 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1621 | IsError(ERR_IO_PENDING)); |
| 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)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1634 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1635 | // Now, kDefaultMaxSocketsPerGroup requests should be active. |
| 1636 | // Let's cancel them. |
| 1637 | for (int i = 0; i < kDefaultMaxSocketsPerGroup; ++i) { |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1638 | ASSERT_FALSE(request(i)->handle()->is_initialized()); |
| 1639 | request(i)->handle()->Reset(); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1640 | } |
| 1641 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1642 | // Let's wait for the rest to complete now. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1643 | for (size_t i = kDefaultMaxSocketsPerGroup; i < requests_size(); ++i) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1644 | EXPECT_THAT(request(i)->WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1645 | request(i)->handle()->Reset(); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1646 | } |
| 1647 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1648 | EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup, |
| 1649 | completion_count()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1650 | } |
| 1651 | |
| 1652 | // Make sure that pending requests get serviced after active requests fail. |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1653 | TEST_F(ClientSocketPoolBaseTest, FailingActiveRequestWithPendingRequests) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1654 | const size_t kMaxSockets = 5; |
| 1655 | CreatePool(kMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1656 | |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 1657 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1658 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1659 | const size_t kNumberOfRequests = 2 * kDefaultMaxSocketsPerGroup + 1; |
| 1660 | ASSERT_LE(kNumberOfRequests, kMaxSockets); // Otherwise the test will hang. |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1661 | |
| 1662 | // Queue up all the requests |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1663 | for (size_t i = 0; i < kNumberOfRequests; ++i) |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1664 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1665 | IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1666 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1667 | for (size_t i = 0; i < kNumberOfRequests; ++i) |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1668 | EXPECT_THAT(request(i)->WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1669 | } |
| 1670 | |
mmenke | 9d72fe4 | 2017-05-18 22:36:07 | [diff] [blame] | 1671 | // Make sure that pending requests that complete synchronously get serviced |
| 1672 | // after active requests fail. See https://crbug.com/723748 |
| 1673 | TEST_F(ClientSocketPoolBaseTest, HandleMultipleSyncFailuresAfterAsyncFailure) { |
| 1674 | const size_t kNumberOfRequests = 10; |
| 1675 | const size_t kMaxSockets = 1; |
| 1676 | CreatePool(kMaxSockets, kMaxSockets); |
| 1677 | |
| 1678 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
| 1679 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1680 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1681 | IsError(ERR_IO_PENDING)); |
mmenke | 9d72fe4 | 2017-05-18 22:36:07 | [diff] [blame] | 1682 | |
| 1683 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
| 1684 | |
| 1685 | // Queue up all the other requests |
| 1686 | for (size_t i = 1; i < kNumberOfRequests; ++i) |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1687 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1688 | IsError(ERR_IO_PENDING)); |
mmenke | 9d72fe4 | 2017-05-18 22:36:07 | [diff] [blame] | 1689 | |
| 1690 | // Make sure all requests fail, instead of hanging. |
| 1691 | for (size_t i = 0; i < kNumberOfRequests; ++i) |
| 1692 | EXPECT_THAT(request(i)->WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 1693 | } |
| 1694 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1695 | TEST_F(ClientSocketPoolBaseTest, CancelActiveRequestThenRequestSocket) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1696 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1697 | |
| 1698 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1699 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1700 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1701 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1702 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1703 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1704 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1705 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1706 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1707 | |
| 1708 | // Cancel the active request. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1709 | handle.Reset(); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1710 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1711 | rv = handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 1712 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1713 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1714 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1715 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 1716 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1717 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1718 | EXPECT_FALSE(handle.is_reused()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 1719 | TestLoadTimingInfoConnectedNotReused(handle); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1720 | EXPECT_EQ(2, client_socket_factory_.allocation_count()); |
| 1721 | } |
| 1722 | |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1723 | TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketsForced) { |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1724 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1725 | ClientSocketHandle handle; |
| 1726 | TestCompletionCallback callback; |
| 1727 | BoundTestNetLog log; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1728 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1729 | TestGroupId("a"), params_, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1730 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1731 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1732 | EXPECT_THAT(rv, IsOk()); |
| 1733 | handle.Reset(); |
| 1734 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 1735 | pool_->CloseIdleSockets(); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1736 | } |
| 1737 | |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1738 | TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketsInGroupForced) { |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1739 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1740 | TestCompletionCallback callback; |
| 1741 | BoundTestNetLog log; |
| 1742 | ClientSocketHandle handle1; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1743 | int rv = handle1.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1744 | TestGroupId("a"), params_, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1745 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1746 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1747 | EXPECT_THAT(rv, IsOk()); |
| 1748 | ClientSocketHandle handle2; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1749 | rv = handle2.Init(TestGroupId("a"), params_, LOWEST, SocketTag(), |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1750 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1751 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1752 | pool_.get(), log.bound()); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1753 | ClientSocketHandle handle3; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1754 | rv = handle3.Init(TestGroupId("b"), params_, LOWEST, SocketTag(), |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1755 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1756 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1757 | pool_.get(), log.bound()); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1758 | EXPECT_THAT(rv, IsOk()); |
| 1759 | handle1.Reset(); |
| 1760 | handle2.Reset(); |
| 1761 | handle3.Reset(); |
| 1762 | EXPECT_EQ(3, pool_->IdleSocketCount()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1763 | pool_->CloseIdleSocketsInGroup(TestGroupId("a")); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1764 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1765 | } |
| 1766 | |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1767 | TEST_F(ClientSocketPoolBaseTest, CleanUpUnusableIdleSockets) { |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1768 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1769 | ClientSocketHandle handle; |
| 1770 | TestCompletionCallback callback; |
| 1771 | BoundTestNetLog log; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1772 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1773 | TestGroupId("a"), params_, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1774 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1775 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1776 | EXPECT_THAT(rv, IsOk()); |
| 1777 | StreamSocket* socket = handle.socket(); |
| 1778 | handle.Reset(); |
| 1779 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 1780 | |
| 1781 | // Disconnect socket now to make the socket unusable. |
| 1782 | socket->Disconnect(); |
| 1783 | ClientSocketHandle handle2; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1784 | rv = handle2.Init(TestGroupId("a"), params_, LOWEST, SocketTag(), |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1785 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1786 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1787 | pool_.get(), log.bound()); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1788 | EXPECT_THAT(rv, IsOk()); |
| 1789 | EXPECT_FALSE(handle2.is_reused()); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1790 | } |
| 1791 | |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1792 | // Regression test for http://crbug.com/17985. |
| 1793 | TEST_F(ClientSocketPoolBaseTest, GroupWithPendingRequestsIsNotEmpty) { |
| 1794 | const int kMaxSockets = 3; |
| 1795 | const int kMaxSocketsPerGroup = 2; |
| 1796 | CreatePool(kMaxSockets, kMaxSocketsPerGroup); |
| 1797 | |
[email protected] | ac790b4 | 2009-12-02 04:31:31 | [diff] [blame] | 1798 | const RequestPriority kHighPriority = HIGHEST; |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1799 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1800 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1801 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1802 | |
| 1803 | // This is going to be a pending request in an otherwise empty group. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1804 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1805 | IsError(ERR_IO_PENDING)); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1806 | |
| 1807 | // Reach the maximum socket limit. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1808 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1809 | |
| 1810 | // Create a stalled group with high priorities. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1811 | EXPECT_THAT(StartRequest(TestGroupId("c"), kHighPriority), |
| 1812 | IsError(ERR_IO_PENDING)); |
| 1813 | EXPECT_THAT(StartRequest(TestGroupId("c"), kHighPriority), |
| 1814 | IsError(ERR_IO_PENDING)); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1815 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1816 | // Release the first two sockets from TestGroupId("a"). Because this is a |
| 1817 | // keepalive, the first release will unblock the pending request for |
| 1818 | // TestGroupId("a"). The second release will unblock a request for "c", |
| 1819 | // because it is the next high priority socket. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1820 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
| 1821 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1822 | |
| 1823 | // Closing idle sockets should not get us into trouble, but in the bug |
| 1824 | // we were hitting a CHECK here. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1825 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1826 | pool_->CloseIdleSockets(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1827 | |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 1828 | // Run the released socket wakeups. |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 1829 | base::RunLoop().RunUntilIdle(); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1830 | } |
| 1831 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 1832 | TEST_F(ClientSocketPoolBaseTest, BasicAsynchronous) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1833 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1834 | |
| 1835 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1836 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1837 | TestCompletionCallback callback; |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 1838 | BoundTestNetLog log; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1839 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1840 | TestGroupId("a"), params_, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1841 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1842 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1843 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1844 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 1845 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 1846 | TestLoadTimingInfoNotConnected(handle); |
| 1847 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1848 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1849 | EXPECT_TRUE(handle.is_initialized()); |
| 1850 | EXPECT_TRUE(handle.socket()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 1851 | TestLoadTimingInfoConnectedNotReused(handle); |
| 1852 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1853 | handle.Reset(); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 1854 | TestLoadTimingInfoNotConnected(handle); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 1855 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 1856 | TestNetLogEntry::List entries; |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 1857 | log.GetEntries(&entries); |
| 1858 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1859 | EXPECT_EQ(5u, entries.size()); |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 1860 | EXPECT_TRUE(LogContainsEvent( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1861 | entries, 0, NetLogEventType::TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 1862 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1863 | EXPECT_TRUE(LogContainsBeginEvent(entries, 1, NetLogEventType::SOCKET_POOL)); |
| 1864 | EXPECT_TRUE(LogContainsEvent( |
| 1865 | entries, 2, NetLogEventType::SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
| 1866 | NetLogEventPhase::NONE)); |
| 1867 | EXPECT_TRUE(LogContainsEvent(entries, 3, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 1868 | NetLogEventType::SOCKET_POOL_BOUND_TO_SOCKET, |
| 1869 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1870 | EXPECT_TRUE(LogContainsEndEvent(entries, 4, NetLogEventType::SOCKET_POOL)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1871 | } |
| 1872 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 1873 | TEST_F(ClientSocketPoolBaseTest, |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1874 | InitConnectionAsynchronousFailure) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1875 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1876 | |
| 1877 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1878 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1879 | TestCompletionCallback callback; |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 1880 | BoundTestNetLog log; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 1881 | // Set the additional error state members to ensure that they get cleared. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1882 | handle.set_is_ssl_error(true); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 1883 | handle.set_ssl_cert_request_info(base::MakeRefCounted<SSLCertRequestInfo>()); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1884 | EXPECT_EQ( |
| 1885 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1886 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1887 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1888 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 1889 | log.bound())); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1890 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 1891 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1892 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1893 | EXPECT_FALSE(handle.is_ssl_error()); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 1894 | EXPECT_FALSE(handle.ssl_cert_request_info()); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 1895 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 1896 | TestNetLogEntry::List entries; |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 1897 | log.GetEntries(&entries); |
| 1898 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1899 | EXPECT_EQ(4u, entries.size()); |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 1900 | EXPECT_TRUE(LogContainsEvent( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1901 | entries, 0, NetLogEventType::TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 1902 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1903 | EXPECT_TRUE(LogContainsBeginEvent(entries, 1, NetLogEventType::SOCKET_POOL)); |
| 1904 | EXPECT_TRUE(LogContainsEvent( |
| 1905 | entries, 2, NetLogEventType::SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
| 1906 | NetLogEventPhase::NONE)); |
| 1907 | EXPECT_TRUE(LogContainsEndEvent(entries, 3, NetLogEventType::SOCKET_POOL)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1908 | } |
| 1909 | |
mmenke | 6be122f | 2015-03-09 22:22:47 | [diff] [blame] | 1910 | // Check that an async ConnectJob failure does not result in creation of a new |
| 1911 | // ConnectJob when there's another pending request also waiting on its own |
| 1912 | // ConnectJob. See http://crbug.com/463960. |
| 1913 | TEST_F(ClientSocketPoolBaseTest, AsyncFailureWithPendingRequestWithJob) { |
| 1914 | CreatePool(2, 2); |
| 1915 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
| 1916 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1917 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1918 | IsError(ERR_IO_PENDING)); |
| 1919 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1920 | IsError(ERR_IO_PENDING)); |
mmenke | 6be122f | 2015-03-09 22:22:47 | [diff] [blame] | 1921 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1922 | EXPECT_THAT(request(0)->WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 1923 | EXPECT_THAT(request(1)->WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
mmenke | 6be122f | 2015-03-09 22:22:47 | [diff] [blame] | 1924 | |
| 1925 | EXPECT_EQ(2, client_socket_factory_.allocation_count()); |
| 1926 | } |
| 1927 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 1928 | TEST_F(ClientSocketPoolBaseTest, TwoRequestsCancelOne) { |
[email protected] | b22b516 | 2010-03-16 07:53:10 | [diff] [blame] | 1929 | // TODO(eroman): Add back the log expectations! Removed them because the |
| 1930 | // ordering is difficult, and some may fire during destructor. |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1931 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1932 | |
| 1933 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1934 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1935 | TestCompletionCallback callback; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1936 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1937 | TestCompletionCallback callback2; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1938 | |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1939 | EXPECT_EQ( |
| 1940 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1941 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1942 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1943 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 1944 | NetLogWithSource())); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 1945 | BoundTestNetLog log2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1946 | EXPECT_EQ( |
| 1947 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1948 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1949 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1950 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1951 | pool_.get(), NetLogWithSource())); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1952 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1953 | handle.Reset(); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1954 | |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 1955 | |
| 1956 | // 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] | 1957 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1958 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1959 | handle2.Reset(); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 1960 | |
| 1961 | // Now request 2 has actually finished. |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 1962 | // TODO(eroman): Add back log expectations. |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1963 | } |
| 1964 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 1965 | TEST_F(ClientSocketPoolBaseTest, CancelRequestLimitsJobs) { |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 1966 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1967 | |
[email protected] | 17a0c6c | 2009-08-04 00:07:04 | [diff] [blame] | 1968 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1969 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1970 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
| 1971 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 1972 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1973 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 1974 | |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 1975 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1976 | static_cast<int>( |
| 1977 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1978 | (*requests())[2]->handle()->Reset(); |
| 1979 | (*requests())[3]->handle()->Reset(); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 1980 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1981 | static_cast<int>( |
| 1982 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 1983 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1984 | (*requests())[1]->handle()->Reset(); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 1985 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1986 | static_cast<int>( |
| 1987 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 1988 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1989 | (*requests())[0]->handle()->Reset(); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 1990 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1991 | static_cast<int>( |
| 1992 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 1993 | } |
| 1994 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1995 | // When requests and ConnectJobs are not coupled, the request will get serviced |
| 1996 | // by whatever comes first. |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 1997 | TEST_F(ClientSocketPoolBaseTest, ReleaseSockets) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1998 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1999 | |
| 2000 | // Start job 1 (async OK) |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 2001 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2002 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2003 | std::vector<TestSocketRequest*> request_order; |
| 2004 | size_t completion_count; // unused |
| 2005 | TestSocketRequest req1(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2006 | int rv = req1.handle()->Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2007 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2008 | ClientSocketPool::RespectLimits::ENABLED, req1.callback(), |
| 2009 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2010 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 2011 | EXPECT_THAT(req1.WaitForResult(), IsOk()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2012 | |
| 2013 | // Job 1 finished OK. Start job 2 (also async OK). Request 3 is pending |
| 2014 | // without a job. |
| 2015 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2016 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2017 | TestSocketRequest req2(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2018 | rv = req2.handle()->Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2019 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2020 | ClientSocketPool::RespectLimits::ENABLED, req2.callback(), |
| 2021 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2022 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2023 | TestSocketRequest req3(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2024 | rv = req3.handle()->Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2025 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2026 | ClientSocketPool::RespectLimits::ENABLED, req3.callback(), |
| 2027 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2028 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2029 | |
| 2030 | // Both Requests 2 and 3 are pending. We release socket 1 which should |
| 2031 | // service request 2. Request 3 should still be waiting. |
[email protected] | a6c59f6 | 2009-07-29 16:33:33 | [diff] [blame] | 2032 | req1.handle()->Reset(); |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 2033 | // Run the released socket wakeups. |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 2034 | base::RunLoop().RunUntilIdle(); |
[email protected] | a6c59f6 | 2009-07-29 16:33:33 | [diff] [blame] | 2035 | ASSERT_TRUE(req2.handle()->socket()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2036 | EXPECT_THAT(req2.WaitForResult(), IsOk()); |
[email protected] | a6c59f6 | 2009-07-29 16:33:33 | [diff] [blame] | 2037 | EXPECT_FALSE(req3.handle()->socket()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2038 | |
| 2039 | // Signal job 2, which should service request 3. |
| 2040 | |
| 2041 | client_socket_factory_.SignalJobs(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2042 | EXPECT_THAT(req3.WaitForResult(), IsOk()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2043 | |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2044 | ASSERT_EQ(3u, request_order.size()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2045 | EXPECT_EQ(&req1, request_order[0]); |
| 2046 | EXPECT_EQ(&req2, request_order[1]); |
| 2047 | EXPECT_EQ(&req3, request_order[2]); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2048 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2049 | } |
| 2050 | |
| 2051 | // The requests are not coupled to the jobs. So, the requests should finish in |
| 2052 | // their priority / insertion order. |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2053 | TEST_F(ClientSocketPoolBaseTest, PendingJobCompletionOrder) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2054 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2055 | // First two jobs are async. |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 2056 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2057 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2058 | std::vector<TestSocketRequest*> request_order; |
| 2059 | size_t completion_count; // unused |
| 2060 | TestSocketRequest req1(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2061 | int rv = req1.handle()->Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2062 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2063 | ClientSocketPool::RespectLimits::ENABLED, req1.callback(), |
| 2064 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2065 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2066 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2067 | TestSocketRequest req2(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2068 | rv = req2.handle()->Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2069 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2070 | ClientSocketPool::RespectLimits::ENABLED, req2.callback(), |
| 2071 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2072 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2073 | |
| 2074 | // The pending job is sync. |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 2075 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2076 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2077 | TestSocketRequest req3(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2078 | rv = req3.handle()->Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2079 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2080 | ClientSocketPool::RespectLimits::ENABLED, req3.callback(), |
| 2081 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2082 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2083 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2084 | EXPECT_THAT(req1.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 2085 | EXPECT_THAT(req2.WaitForResult(), IsOk()); |
| 2086 | EXPECT_THAT(req3.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2087 | |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2088 | ASSERT_EQ(3u, request_order.size()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2089 | EXPECT_EQ(&req1, request_order[0]); |
| 2090 | EXPECT_EQ(&req2, request_order[1]); |
| 2091 | EXPECT_EQ(&req3, request_order[2]); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2092 | } |
| 2093 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2094 | // Test GetLoadState in the case there's only one socket request. |
| 2095 | TEST_F(ClientSocketPoolBaseTest, LoadStateOneRequest) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2096 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2097 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2098 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2099 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2100 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2101 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2102 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2103 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2104 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2105 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2106 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2107 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2108 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE); |
| 2109 | EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle.GetLoadState()); |
| 2110 | |
| 2111 | // No point in completing the connection, since ClientSocketHandles only |
| 2112 | // expect the LoadState to be checked while connecting. |
| 2113 | } |
| 2114 | |
| 2115 | // Test GetLoadState in the case there are two socket requests. |
| 2116 | TEST_F(ClientSocketPoolBaseTest, LoadStateTwoRequests) { |
| 2117 | CreatePool(2, 2); |
| 2118 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2119 | |
| 2120 | ClientSocketHandle handle; |
| 2121 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2122 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2123 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2124 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2125 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2126 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2127 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_RESOLVING_HOST); |
| 2128 | |
| 2129 | ClientSocketHandle handle2; |
| 2130 | TestCompletionCallback callback2; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2131 | rv = handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 2132 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2133 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2134 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2135 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2136 | client_socket_factory_.SetJobLoadState(1, LOAD_STATE_RESOLVING_HOST); |
| 2137 | |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 2138 | // Each handle should reflect the state of its own job. |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2139 | EXPECT_EQ(LOAD_STATE_RESOLVING_HOST, handle.GetLoadState()); |
| 2140 | EXPECT_EQ(LOAD_STATE_RESOLVING_HOST, handle2.GetLoadState()); |
| 2141 | |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 2142 | // Update the state of the first job. |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2143 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_CONNECTING); |
| 2144 | |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 2145 | // Only the state of the first request should have changed. |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2146 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2147 | EXPECT_EQ(LOAD_STATE_RESOLVING_HOST, handle2.GetLoadState()); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 2148 | |
| 2149 | // Update the state of the second job. |
| 2150 | client_socket_factory_.SetJobLoadState(1, LOAD_STATE_SSL_HANDSHAKE); |
| 2151 | |
| 2152 | // Only the state of the second request should have changed. |
| 2153 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
| 2154 | EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle2.GetLoadState()); |
| 2155 | |
| 2156 | // Second job connects and the first request gets the socket. The |
| 2157 | // second handle switches to the state of the remaining ConnectJob. |
| 2158 | client_socket_factory_.SignalJob(1); |
| 2159 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 2160 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle2.GetLoadState()); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2161 | } |
| 2162 | |
| 2163 | // Test GetLoadState in the case the per-group limit is reached. |
| 2164 | TEST_F(ClientSocketPoolBaseTest, LoadStateGroupLimit) { |
| 2165 | CreatePool(2, 1); |
| 2166 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2167 | |
| 2168 | ClientSocketHandle handle; |
| 2169 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2170 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2171 | TestGroupId("a"), params_, MEDIUM, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2172 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2173 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2174 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2175 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
| 2176 | |
| 2177 | // Request another socket from the same pool, buth with a higher priority. |
| 2178 | // The first request should now be stalled at the socket group limit. |
| 2179 | ClientSocketHandle handle2; |
| 2180 | TestCompletionCallback callback2; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2181 | rv = handle2.Init(TestGroupId("a"), params_, HIGHEST, SocketTag(), |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 2182 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2183 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2184 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2185 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2186 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET, handle.GetLoadState()); |
| 2187 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle2.GetLoadState()); |
| 2188 | |
| 2189 | // The first handle should remain stalled as the other socket goes through |
| 2190 | // the connect process. |
| 2191 | |
| 2192 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE); |
| 2193 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET, handle.GetLoadState()); |
| 2194 | EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle2.GetLoadState()); |
| 2195 | |
| 2196 | client_socket_factory_.SignalJob(0); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2197 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2198 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET, handle.GetLoadState()); |
| 2199 | |
| 2200 | // Closing the second socket should cause the stalled handle to finally get a |
| 2201 | // ConnectJob. |
| 2202 | handle2.socket()->Disconnect(); |
| 2203 | handle2.Reset(); |
| 2204 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
| 2205 | } |
| 2206 | |
| 2207 | // Test GetLoadState in the case the per-pool limit is reached. |
| 2208 | TEST_F(ClientSocketPoolBaseTest, LoadStatePoolLimit) { |
| 2209 | CreatePool(2, 2); |
| 2210 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2211 | |
| 2212 | ClientSocketHandle handle; |
| 2213 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2214 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2215 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2216 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2217 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2218 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2219 | |
| 2220 | // Request for socket from another pool. |
| 2221 | ClientSocketHandle handle2; |
| 2222 | TestCompletionCallback callback2; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2223 | rv = handle2.Init(TestGroupId("b"), params_, DEFAULT_PRIORITY, SocketTag(), |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 2224 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2225 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2226 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2227 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2228 | |
| 2229 | // Request another socket from the first pool. Request should stall at the |
| 2230 | // socket pool limit. |
| 2231 | ClientSocketHandle handle3; |
| 2232 | TestCompletionCallback callback3; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2233 | rv = handle3.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 2234 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2235 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2236 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2237 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2238 | |
| 2239 | // The third handle should remain stalled as the other sockets in its group |
| 2240 | // goes through the connect process. |
| 2241 | |
| 2242 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
| 2243 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL, handle3.GetLoadState()); |
| 2244 | |
| 2245 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE); |
| 2246 | EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle.GetLoadState()); |
| 2247 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL, handle3.GetLoadState()); |
| 2248 | |
| 2249 | client_socket_factory_.SignalJob(0); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2250 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2251 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL, handle3.GetLoadState()); |
| 2252 | |
| 2253 | // Closing a socket should allow the stalled handle to finally get a new |
| 2254 | // ConnectJob. |
| 2255 | handle.socket()->Disconnect(); |
| 2256 | handle.Reset(); |
| 2257 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle3.GetLoadState()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2258 | } |
| 2259 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2260 | TEST_F(ClientSocketPoolBaseTest, CertError) { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2261 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2262 | connect_job_factory_->set_job_type(TestConnectJob::kMockCertErrorJob); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2263 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2264 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2265 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2266 | EXPECT_EQ( |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2267 | ERR_CERT_COMMON_NAME_INVALID, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2268 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2269 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2270 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2271 | NetLogWithSource())); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2272 | EXPECT_TRUE(handle.is_initialized()); |
| 2273 | EXPECT_TRUE(handle.socket()); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2274 | } |
| 2275 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2276 | TEST_F(ClientSocketPoolBaseTest, AsyncCertError) { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2277 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2278 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2279 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingCertErrorJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2280 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2281 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2282 | EXPECT_EQ( |
| 2283 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2284 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2285 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2286 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2287 | NetLogWithSource())); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2288 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2289 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2290 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_CERT_COMMON_NAME_INVALID)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2291 | EXPECT_TRUE(handle.is_initialized()); |
| 2292 | EXPECT_TRUE(handle.socket()); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2293 | } |
| 2294 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2295 | TEST_F(ClientSocketPoolBaseTest, AdditionalErrorStateSynchronous) { |
| 2296 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2297 | connect_job_factory_->set_job_type( |
| 2298 | TestConnectJob::kMockAdditionalErrorStateJob); |
| 2299 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2300 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2301 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2302 | EXPECT_EQ( |
| 2303 | ERR_CONNECTION_FAILED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2304 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2305 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2306 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2307 | NetLogWithSource())); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2308 | EXPECT_FALSE(handle.is_initialized()); |
| 2309 | EXPECT_FALSE(handle.socket()); |
| 2310 | EXPECT_TRUE(handle.is_ssl_error()); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 2311 | EXPECT_TRUE(handle.ssl_cert_request_info()); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2312 | } |
| 2313 | |
| 2314 | TEST_F(ClientSocketPoolBaseTest, AdditionalErrorStateAsynchronous) { |
| 2315 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2316 | |
| 2317 | connect_job_factory_->set_job_type( |
| 2318 | TestConnectJob::kMockPendingAdditionalErrorStateJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2319 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2320 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2321 | EXPECT_EQ( |
| 2322 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2323 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2324 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2325 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2326 | NetLogWithSource())); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2327 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2328 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2329 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2330 | EXPECT_FALSE(handle.is_initialized()); |
| 2331 | EXPECT_FALSE(handle.socket()); |
| 2332 | EXPECT_TRUE(handle.is_ssl_error()); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 2333 | EXPECT_TRUE(handle.ssl_cert_request_info()); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2334 | } |
| 2335 | |
martijn | 003cd61 | 2016-05-19 22:24:38 | [diff] [blame] | 2336 | // Make sure we can reuse sockets. |
| 2337 | TEST_F(ClientSocketPoolBaseTest, CleanupTimedOutIdleSocketsReuse) { |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2338 | CreatePoolWithIdleTimeouts( |
| 2339 | kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2340 | base::TimeDelta(), // Time out unused sockets immediately. |
| 2341 | base::TimeDelta::FromDays(1)); // Don't time out used sockets. |
| 2342 | |
| 2343 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2344 | |
| 2345 | ClientSocketHandle handle; |
| 2346 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2347 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2348 | TestGroupId("a"), params_, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2349 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2350 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2351 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2352 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2353 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2354 | ASSERT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2355 | |
| 2356 | // Use and release the socket. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 2357 | EXPECT_EQ(1, handle.socket()->Write(nullptr, 1, CompletionOnceCallback(), |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 2358 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 2359 | TestLoadTimingInfoConnectedNotReused(handle); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2360 | handle.Reset(); |
| 2361 | |
| 2362 | // Should now have one idle socket. |
| 2363 | ASSERT_EQ(1, pool_->IdleSocketCount()); |
| 2364 | |
| 2365 | // Request a new socket. This should reuse the old socket and complete |
| 2366 | // synchronously. |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 2367 | BoundTestNetLog log; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2368 | rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2369 | TestGroupId("a"), params_, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2370 | ClientSocketPool::RespectLimits::ENABLED, CompletionOnceCallback(), |
| 2371 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2372 | ASSERT_THAT(rv, IsOk()); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2373 | EXPECT_TRUE(handle.is_reused()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 2374 | TestLoadTimingInfoConnectedReused(handle); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2375 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2376 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2377 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2378 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2379 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 2380 | TestNetLogEntry::List entries; |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2381 | log.GetEntries(&entries); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2382 | EXPECT_TRUE(LogContainsEvent( |
| 2383 | entries, 0, NetLogEventType::TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET, |
| 2384 | NetLogEventPhase::NONE)); |
| 2385 | EXPECT_TRUE(LogContainsBeginEvent(entries, 1, NetLogEventType::SOCKET_POOL)); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2386 | EXPECT_TRUE(LogContainsEntryWithType( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2387 | entries, 2, NetLogEventType::SOCKET_POOL_REUSED_AN_EXISTING_SOCKET)); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2388 | } |
| 2389 | |
martijn | 003cd61 | 2016-05-19 22:24:38 | [diff] [blame] | 2390 | // Make sure we cleanup old unused sockets. |
Eric Roman | b49715e | 2018-04-24 22:41:17 | [diff] [blame] | 2391 | TEST_F(ClientSocketPoolBaseTest, CleanupTimedOutIdleSocketsNoReuse) { |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2392 | CreatePoolWithIdleTimeouts( |
| 2393 | kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 2394 | base::TimeDelta(), // Time out unused sockets immediately |
| 2395 | base::TimeDelta()); // Time out used sockets immediately |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2396 | |
| 2397 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2398 | |
| 2399 | // Startup two mock pending connect jobs, which will sit in the MessageLoop. |
| 2400 | |
| 2401 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2402 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2403 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2404 | TestGroupId("a"), params_, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2405 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2406 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2407 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2408 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2409 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2410 | |
| 2411 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2412 | TestCompletionCallback callback2; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2413 | rv = handle2.Init(TestGroupId("a"), params_, LOWEST, SocketTag(), |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 2414 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2415 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2416 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2417 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2418 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2419 | pool_->GetLoadState(TestGroupId("a"), &handle2)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2420 | |
| 2421 | // Cancel one of the requests. Wait for the other, which will get the first |
| 2422 | // job. Release the socket. Run the loop again to make sure the second |
| 2423 | // socket is sitting idle and the first one is released (since ReleaseSocket() |
| 2424 | // just posts a DoReleaseSocket() task). |
| 2425 | |
| 2426 | handle.Reset(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2427 | ASSERT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2428 | // Use the socket. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 2429 | EXPECT_EQ(1, handle2.socket()->Write(nullptr, 1, CompletionOnceCallback(), |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 2430 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2431 | handle2.Reset(); |
| 2432 | |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2433 | // We post all of our delayed tasks with a 2ms delay. I.e. they don't |
| 2434 | // actually become pending until 2ms after they have been created. In order |
| 2435 | // to flush all tasks, we need to wait so that we know there are no |
| 2436 | // soon-to-be-pending tasks waiting. |
Alex Clarke | 0def209 | 2018-12-10 12:01:45 | [diff] [blame] | 2437 | FastForwardBy(base::TimeDelta::FromMilliseconds(10)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2438 | |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2439 | // Both sockets should now be idle. |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2440 | ASSERT_EQ(2, pool_->IdleSocketCount()); |
| 2441 | |
| 2442 | // Request a new socket. This should cleanup the unused and timed out ones. |
| 2443 | // A new socket will be created rather than reusing the idle one. |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 2444 | BoundTestNetLog log; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2445 | TestCompletionCallback callback3; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2446 | rv = handle.Init(TestGroupId("a"), params_, LOWEST, SocketTag(), |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 2447 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2448 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2449 | pool_.get(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2450 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 2451 | ASSERT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2452 | EXPECT_FALSE(handle.is_reused()); |
| 2453 | |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2454 | // Make sure the idle socket is closed. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2455 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2456 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2457 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2458 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 2459 | TestNetLogEntry::List entries; |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2460 | log.GetEntries(&entries); |
| 2461 | EXPECT_FALSE(LogContainsEntryWithType( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 2462 | entries, 1, NetLogEventType::SOCKET_POOL_REUSED_AN_EXISTING_SOCKET)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2463 | } |
| 2464 | |
[email protected] | 2041cf34 | 2010-02-19 03:15:59 | [diff] [blame] | 2465 | // Make sure that we process all pending requests even when we're stalling |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2466 | // because of multiple releasing disconnected sockets. |
| 2467 | TEST_F(ClientSocketPoolBaseTest, MultipleReleasingDisconnectedSockets) { |
| 2468 | CreatePoolWithIdleTimeouts( |
| 2469 | kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 2470 | base::TimeDelta(), // Time out unused sockets immediately. |
| 2471 | base::TimeDelta::FromDays(1)); // Don't time out used sockets. |
| 2472 | |
| 2473 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 2474 | |
| 2475 | // Startup 4 connect jobs. Two of them will be pending. |
| 2476 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2477 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2478 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2479 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2480 | TestGroupId("a"), params_, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2481 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2482 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2483 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2484 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2485 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2486 | TestCompletionCallback callback2; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2487 | rv = handle2.Init(TestGroupId("a"), params_, LOWEST, SocketTag(), |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 2488 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2489 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2490 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2491 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2492 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2493 | ClientSocketHandle handle3; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2494 | TestCompletionCallback callback3; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2495 | rv = handle3.Init(TestGroupId("a"), params_, LOWEST, SocketTag(), |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 2496 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2497 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2498 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2499 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2500 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2501 | ClientSocketHandle handle4; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2502 | TestCompletionCallback callback4; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2503 | rv = handle4.Init(TestGroupId("a"), params_, LOWEST, SocketTag(), |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 2504 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2505 | callback4.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2506 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2507 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2508 | |
| 2509 | // Release two disconnected sockets. |
| 2510 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2511 | handle.socket()->Disconnect(); |
| 2512 | handle.Reset(); |
| 2513 | handle2.socket()->Disconnect(); |
| 2514 | handle2.Reset(); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2515 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2516 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2517 | EXPECT_FALSE(handle3.is_reused()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2518 | EXPECT_THAT(callback4.WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2519 | EXPECT_FALSE(handle4.is_reused()); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2520 | } |
| 2521 | |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2522 | // Regression test for http://crbug.com/42267. |
| 2523 | // When DoReleaseSocket() is processed for one socket, it is blocked because the |
| 2524 | // other stalled groups all have releasing sockets, so no progress can be made. |
| 2525 | TEST_F(ClientSocketPoolBaseTest, SocketLimitReleasingSockets) { |
| 2526 | CreatePoolWithIdleTimeouts( |
| 2527 | 4 /* socket limit */, 4 /* socket limit per group */, |
| 2528 | base::TimeDelta(), // Time out unused sockets immediately. |
| 2529 | base::TimeDelta::FromDays(1)); // Don't time out used sockets. |
| 2530 | |
| 2531 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 2532 | |
| 2533 | // Max out the socket limit with 2 per group. |
| 2534 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2535 | ClientSocketHandle handle_a[4]; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2536 | TestCompletionCallback callback_a[4]; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2537 | ClientSocketHandle handle_b[4]; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2538 | TestCompletionCallback callback_b[4]; |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2539 | |
| 2540 | for (int i = 0; i < 2; ++i) { |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2541 | EXPECT_EQ( |
| 2542 | OK, handle_a[i].Init(TestGroupId("a"), params_, LOWEST, SocketTag(), |
| 2543 | ClientSocketPool::RespectLimits::ENABLED, |
| 2544 | callback_a[i].callback(), |
| 2545 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2546 | NetLogWithSource())); |
| 2547 | EXPECT_EQ( |
| 2548 | OK, handle_b[i].Init(TestGroupId("b"), params_, LOWEST, SocketTag(), |
| 2549 | ClientSocketPool::RespectLimits::ENABLED, |
| 2550 | callback_b[i].callback(), |
| 2551 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2552 | NetLogWithSource())); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2553 | } |
[email protected] | b89f7e4 | 2010-05-20 20:37:00 | [diff] [blame] | 2554 | |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2555 | // Make 4 pending requests, 2 per group. |
| 2556 | |
| 2557 | for (int i = 2; i < 4; ++i) { |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 2558 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2559 | handle_a[i].Init(TestGroupId("a"), params_, LOWEST, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 2560 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2561 | callback_a[i].callback(), |
| 2562 | ClientSocketPool::ProxyAuthCallback(), |
| 2563 | pool_.get(), NetLogWithSource())); |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 2564 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2565 | handle_b[i].Init(TestGroupId("b"), params_, LOWEST, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 2566 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2567 | callback_b[i].callback(), |
| 2568 | ClientSocketPool::ProxyAuthCallback(), |
| 2569 | pool_.get(), NetLogWithSource())); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2570 | } |
| 2571 | |
| 2572 | // Release b's socket first. The order is important, because in |
| 2573 | // DoReleaseSocket(), we'll process b's released socket, and since both b and |
| 2574 | // a are stalled, but 'a' is lower lexicographically, we'll process group 'a' |
| 2575 | // first, which has a releasing socket, so it refuses to start up another |
| 2576 | // ConnectJob. So, we used to infinite loop on this. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2577 | handle_b[0].socket()->Disconnect(); |
| 2578 | handle_b[0].Reset(); |
| 2579 | handle_a[0].socket()->Disconnect(); |
| 2580 | handle_a[0].Reset(); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2581 | |
| 2582 | // Used to get stuck here. |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 2583 | base::RunLoop().RunUntilIdle(); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2584 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2585 | handle_b[1].socket()->Disconnect(); |
| 2586 | handle_b[1].Reset(); |
| 2587 | handle_a[1].socket()->Disconnect(); |
| 2588 | handle_a[1].Reset(); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2589 | |
| 2590 | for (int i = 2; i < 4; ++i) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2591 | EXPECT_THAT(callback_b[i].WaitForResult(), IsOk()); |
| 2592 | EXPECT_THAT(callback_a[i].WaitForResult(), IsOk()); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2593 | } |
| 2594 | } |
| 2595 | |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2596 | TEST_F(ClientSocketPoolBaseTest, |
| 2597 | ReleasingDisconnectedSocketsMaintainsPriorityOrder) { |
| 2598 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2599 | |
| 2600 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2601 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2602 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2603 | IsError(ERR_IO_PENDING)); |
| 2604 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2605 | IsError(ERR_IO_PENDING)); |
| 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)); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2610 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2611 | EXPECT_THAT((*requests())[0]->WaitForResult(), IsOk()); |
| 2612 | EXPECT_THAT((*requests())[1]->WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2613 | EXPECT_EQ(2u, completion_count()); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2614 | |
| 2615 | // Releases one connection. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2616 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::NO_KEEP_ALIVE)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2617 | EXPECT_THAT((*requests())[2]->WaitForResult(), IsOk()); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2618 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2619 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::NO_KEEP_ALIVE)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2620 | EXPECT_THAT((*requests())[3]->WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2621 | EXPECT_EQ(4u, completion_count()); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2622 | |
| 2623 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 2624 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 2625 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 2626 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 2627 | |
| 2628 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2629 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(5)); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2630 | } |
| 2631 | |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2632 | class TestReleasingSocketRequest : public TestCompletionCallbackBase { |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2633 | public: |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2634 | TestReleasingSocketRequest(TransportClientSocketPool* pool, |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2635 | int expected_result, |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2636 | bool reset_releasing_handle) |
| 2637 | : pool_(pool), |
| 2638 | expected_result_(expected_result), |
Bence Béky | 8ddc249 | 2018-06-13 01:02:04 | [diff] [blame] | 2639 | reset_releasing_handle_(reset_releasing_handle) {} |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2640 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 2641 | ~TestReleasingSocketRequest() override = default; |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2642 | |
| 2643 | ClientSocketHandle* handle() { return &handle_; } |
| 2644 | |
Bence Béky | 8ddc249 | 2018-06-13 01:02:04 | [diff] [blame] | 2645 | CompletionOnceCallback callback() { |
| 2646 | return base::BindOnce(&TestReleasingSocketRequest::OnComplete, |
| 2647 | base::Unretained(this)); |
| 2648 | } |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2649 | |
| 2650 | private: |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2651 | void OnComplete(int result) { |
| 2652 | SetResult(result); |
| 2653 | if (reset_releasing_handle_) |
| 2654 | handle_.Reset(); |
| 2655 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2656 | EXPECT_EQ( |
| 2657 | expected_result_, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2658 | handle2_.Init(TestGroupId("a"), CreateDummyParams(), DEFAULT_PRIORITY, |
| 2659 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2660 | CompletionOnceCallback(), |
| 2661 | ClientSocketPool::ProxyAuthCallback(), pool_, |
| 2662 | NetLogWithSource())); |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2663 | } |
| 2664 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2665 | TransportClientSocketPool* const pool_; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2666 | int expected_result_; |
| 2667 | bool reset_releasing_handle_; |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2668 | ClientSocketHandle handle_; |
| 2669 | ClientSocketHandle handle2_; |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2670 | }; |
| 2671 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2672 | |
| 2673 | TEST_F(ClientSocketPoolBaseTest, AdditionalErrorSocketsDontUseSlot) { |
| 2674 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2675 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2676 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
| 2677 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 2678 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2679 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2680 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2681 | client_socket_factory_.allocation_count()); |
| 2682 | |
| 2683 | connect_job_factory_->set_job_type( |
| 2684 | TestConnectJob::kMockPendingAdditionalErrorStateJob); |
| 2685 | TestReleasingSocketRequest req(pool_.get(), OK, false); |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 2686 | EXPECT_EQ( |
| 2687 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2688 | req.handle()->Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, |
| 2689 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2690 | req.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2691 | pool_.get(), NetLogWithSource())); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2692 | // The next job should complete synchronously |
| 2693 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 2694 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2695 | EXPECT_THAT(req.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2696 | EXPECT_FALSE(req.handle()->is_initialized()); |
| 2697 | EXPECT_FALSE(req.handle()->socket()); |
| 2698 | EXPECT_TRUE(req.handle()->is_ssl_error()); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 2699 | EXPECT_TRUE(req.handle()->ssl_cert_request_info()); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2700 | } |
| 2701 | |
[email protected] | b6501d3d | 2010-06-03 23:53:34 | [diff] [blame] | 2702 | // http://crbug.com/44724 regression test. |
| 2703 | // We start releasing the pool when we flush on network change. When that |
| 2704 | // happens, the only active references are in the ClientSocketHandles. When a |
| 2705 | // ConnectJob completes and calls back into the last ClientSocketHandle, that |
| 2706 | // callback can release the last reference and delete the pool. After the |
| 2707 | // callback finishes, we go back to the stack frame within the now-deleted pool. |
| 2708 | // Executing any code that refers to members of the now-deleted pool can cause |
| 2709 | // crashes. |
| 2710 | TEST_F(ClientSocketPoolBaseTest, CallbackThatReleasesPool) { |
| 2711 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2712 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
| 2713 | |
| 2714 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2715 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2716 | EXPECT_EQ( |
| 2717 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2718 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2719 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2720 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2721 | NetLogWithSource())); |
[email protected] | b6501d3d | 2010-06-03 23:53:34 | [diff] [blame] | 2722 | |
[email protected] | 7af985a | 2012-12-14 22:40:42 | [diff] [blame] | 2723 | pool_->FlushWithError(ERR_NETWORK_CHANGED); |
[email protected] | b6501d3d | 2010-06-03 23:53:34 | [diff] [blame] | 2724 | |
| 2725 | // We'll call back into this now. |
| 2726 | callback.WaitForResult(); |
| 2727 | } |
| 2728 | |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2729 | TEST_F(ClientSocketPoolBaseTest, DoNotReuseSocketAfterFlush) { |
| 2730 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2731 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2732 | |
| 2733 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2734 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2735 | EXPECT_EQ( |
| 2736 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2737 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2738 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2739 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2740 | NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2741 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2742 | EXPECT_EQ(ClientSocketHandle::UNUSED, handle.reuse_type()); |
| 2743 | |
[email protected] | 7af985a | 2012-12-14 22:40:42 | [diff] [blame] | 2744 | pool_->FlushWithError(ERR_NETWORK_CHANGED); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2745 | |
| 2746 | handle.Reset(); |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 2747 | base::RunLoop().RunUntilIdle(); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2748 | |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2749 | EXPECT_EQ( |
| 2750 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2751 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2752 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2753 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2754 | NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2755 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2756 | EXPECT_EQ(ClientSocketHandle::UNUSED, handle.reuse_type()); |
| 2757 | } |
| 2758 | |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2759 | class ConnectWithinCallback : public TestCompletionCallbackBase { |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2760 | public: |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2761 | ConnectWithinCallback( |
| 2762 | const ClientSocketPool::GroupId& group_id, |
Matt Menke | 84d11e56 | 2019-03-27 00:11:19 | [diff] [blame] | 2763 | const scoped_refptr<ClientSocketPool::SocketParams>& params, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2764 | TransportClientSocketPool* pool) |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2765 | : group_id_(group_id), params_(params), pool_(pool) {} |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2766 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 2767 | ~ConnectWithinCallback() override = default; |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2768 | |
| 2769 | int WaitForNestedResult() { |
| 2770 | return nested_callback_.WaitForResult(); |
| 2771 | } |
| 2772 | |
Bence Béky | 8ddc249 | 2018-06-13 01:02:04 | [diff] [blame] | 2773 | CompletionOnceCallback callback() { |
| 2774 | return base::BindOnce(&ConnectWithinCallback::OnComplete, |
| 2775 | base::Unretained(this)); |
| 2776 | } |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2777 | |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2778 | private: |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2779 | void OnComplete(int result) { |
| 2780 | SetResult(result); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2781 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2782 | handle_.Init(group_id_, params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2783 | ClientSocketPool::RespectLimits::ENABLED, |
| 2784 | nested_callback_.callback(), |
| 2785 | ClientSocketPool::ProxyAuthCallback(), pool_, |
| 2786 | NetLogWithSource())); |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2787 | } |
| 2788 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2789 | const ClientSocketPool::GroupId group_id_; |
Matt Menke | 84d11e56 | 2019-03-27 00:11:19 | [diff] [blame] | 2790 | const scoped_refptr<ClientSocketPool::SocketParams> params_; |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2791 | TransportClientSocketPool* const pool_; |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2792 | ClientSocketHandle handle_; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2793 | TestCompletionCallback nested_callback_; |
| 2794 | |
| 2795 | DISALLOW_COPY_AND_ASSIGN(ConnectWithinCallback); |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2796 | }; |
| 2797 | |
| 2798 | TEST_F(ClientSocketPoolBaseTest, AbortAllRequestsOnFlush) { |
| 2799 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2800 | |
| 2801 | // First job will be waiting until it gets aborted. |
| 2802 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2803 | |
| 2804 | ClientSocketHandle handle; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2805 | ConnectWithinCallback callback(TestGroupId("a"), params_, pool_.get()); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2806 | EXPECT_EQ( |
| 2807 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2808 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2809 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2810 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2811 | NetLogWithSource())); |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2812 | |
| 2813 | // Second job will be started during the first callback, and will |
| 2814 | // asynchronously complete with OK. |
| 2815 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 7af985a | 2012-12-14 22:40:42 | [diff] [blame] | 2816 | pool_->FlushWithError(ERR_NETWORK_CHANGED); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2817 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_NETWORK_CHANGED)); |
| 2818 | EXPECT_THAT(callback.WaitForNestedResult(), IsOk()); |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2819 | } |
| 2820 | |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 2821 | TEST_F(ClientSocketPoolBaseTest, BackupSocketWaitsForHostResolution) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2822 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets, |
| 2823 | true /* enable_backup_connect_jobs */); |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 2824 | |
| 2825 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2826 | ClientSocketHandle handle; |
| 2827 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2828 | EXPECT_EQ( |
| 2829 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2830 | handle.Init(TestGroupId("bar"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2831 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2832 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2833 | NetLogWithSource())); |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 2834 | // The backup timer fires but doesn't start a new ConnectJob while resolving |
| 2835 | // the hostname. |
| 2836 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_RESOLVING_HOST); |
| 2837 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
| 2838 | ClientSocketPool::kMaxConnectRetryIntervalMs * 100)); |
| 2839 | EXPECT_EQ(1, client_socket_factory_.allocation_count()); |
| 2840 | |
| 2841 | // Once the ConnectJob has finished resolving the hostname, the backup timer |
| 2842 | // will create a ConnectJob when it fires. |
| 2843 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_CONNECTING); |
| 2844 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
| 2845 | ClientSocketPool::kMaxConnectRetryIntervalMs)); |
| 2846 | EXPECT_EQ(2, client_socket_factory_.allocation_count()); |
| 2847 | } |
| 2848 | |
| 2849 | // Test that no backup socket is created when a ConnectJob connects before it |
| 2850 | // completes. |
| 2851 | TEST_F(ClientSocketPoolBaseTest, NoBackupSocketWhenConnected) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2852 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets, |
| 2853 | true /* enable_backup_connect_jobs */); |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 2854 | |
| 2855 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2856 | ClientSocketHandle handle; |
| 2857 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2858 | EXPECT_EQ( |
| 2859 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2860 | handle.Init(TestGroupId("bar"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2861 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2862 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2863 | NetLogWithSource())); |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 2864 | // The backup timer fires but doesn't start a new ConnectJob while resolving |
| 2865 | // the hostname. |
| 2866 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_RESOLVING_HOST); |
| 2867 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
| 2868 | ClientSocketPool::kMaxConnectRetryIntervalMs * 100)); |
| 2869 | EXPECT_EQ(1, client_socket_factory_.allocation_count()); |
| 2870 | |
| 2871 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE); |
| 2872 | client_socket_factory_.SetJobHasEstablishedConnection(0); |
| 2873 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
| 2874 | ClientSocketPool::kMaxConnectRetryIntervalMs * 100)); |
| 2875 | EXPECT_EQ(1, client_socket_factory_.allocation_count()); |
| 2876 | } |
| 2877 | |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 2878 | // Cancel a pending socket request while we're at max sockets, |
| 2879 | // and verify that the backup socket firing doesn't cause a crash. |
| 2880 | TEST_F(ClientSocketPoolBaseTest, BackupSocketCancelAtMaxSockets) { |
| 2881 | // Max 4 sockets globally, max 4 sockets per group. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2882 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets, |
| 2883 | true /* enable_backup_connect_jobs */); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 2884 | |
[email protected] | 4baaf9d | 2010-08-31 15:15:44 | [diff] [blame] | 2885 | // Create the first socket and set to ERR_IO_PENDING. This starts the backup |
| 2886 | // timer. |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 2887 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2888 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2889 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2890 | EXPECT_EQ( |
| 2891 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2892 | handle.Init(TestGroupId("bar"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2893 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2894 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2895 | NetLogWithSource())); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 2896 | |
| 2897 | // Start (MaxSockets - 1) connected sockets to reach max sockets. |
| 2898 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 2899 | ClientSocketHandle handles[kDefaultMaxSockets]; |
| 2900 | for (int i = 1; i < kDefaultMaxSockets; ++i) { |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2901 | TestCompletionCallback callback; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2902 | EXPECT_EQ(OK, handles[i].Init(TestGroupId("bar"), params_, DEFAULT_PRIORITY, |
| 2903 | SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 2904 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2905 | callback.callback(), |
| 2906 | ClientSocketPool::ProxyAuthCallback(), |
| 2907 | pool_.get(), NetLogWithSource())); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 2908 | } |
| 2909 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 2910 | base::RunLoop().RunUntilIdle(); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 2911 | |
| 2912 | // Cancel the pending request. |
| 2913 | handle.Reset(); |
| 2914 | |
| 2915 | // 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] | 2916 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
[email protected] | 26b997396 | 2012-01-28 00:57:00 | [diff] [blame] | 2917 | ClientSocketPool::kMaxConnectRetryIntervalMs / 2 * 3)); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 2918 | |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 2919 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 2920 | } |
| 2921 | |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 2922 | TEST_F(ClientSocketPoolBaseTest, CancelBackupSocketAfterCancelingAllRequests) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2923 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets, |
| 2924 | true /* enable_backup_connect_jobs */); |
[email protected] | 4baaf9d | 2010-08-31 15:15:44 | [diff] [blame] | 2925 | |
| 2926 | // Create the first socket and set to ERR_IO_PENDING. This starts the backup |
| 2927 | // timer. |
| 2928 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2929 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2930 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2931 | EXPECT_EQ( |
| 2932 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2933 | handle.Init(TestGroupId("bar"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2934 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2935 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2936 | NetLogWithSource())); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2937 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("bar"))); |
| 2938 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("bar"))); |
| 2939 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 2940 | TestGroupId("bar"))); |
| 2941 | EXPECT_EQ( |
| 2942 | 0u, pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("bar"))); |
[email protected] | 4baaf9d | 2010-08-31 15:15:44 | [diff] [blame] | 2943 | |
| 2944 | // Cancel the socket request. This should cancel the backup timer. Wait for |
| 2945 | // the backup time to see if it indeed got canceled. |
| 2946 | handle.Reset(); |
| 2947 | // 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] | 2948 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
[email protected] | 26b997396 | 2012-01-28 00:57:00 | [diff] [blame] | 2949 | ClientSocketPool::kMaxConnectRetryIntervalMs / 2 * 3)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2950 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("bar"))); |
| 2951 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("bar"))); |
[email protected] | 4baaf9d | 2010-08-31 15:15:44 | [diff] [blame] | 2952 | } |
| 2953 | |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 2954 | TEST_F(ClientSocketPoolBaseTest, CancelBackupSocketAfterFinishingAllRequests) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2955 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets, |
| 2956 | true /* enable_backup_connect_jobs */); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 2957 | |
| 2958 | // Create the first socket and set to ERR_IO_PENDING. This starts the backup |
| 2959 | // timer. |
| 2960 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2961 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2962 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2963 | EXPECT_EQ( |
| 2964 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2965 | handle.Init(TestGroupId("bar"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2966 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2967 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2968 | NetLogWithSource())); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 2969 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2970 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2971 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 2972 | EXPECT_EQ( |
| 2973 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2974 | handle2.Init(TestGroupId("bar"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 2975 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2976 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2977 | pool_.get(), NetLogWithSource())); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2978 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("bar"))); |
| 2979 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("bar"))); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 2980 | |
| 2981 | // Cancel request 1 and then complete request 2. With the requests finished, |
| 2982 | // the backup timer should be cancelled. |
| 2983 | handle.Reset(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2984 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 2985 | // 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] | 2986 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
[email protected] | 26b997396 | 2012-01-28 00:57:00 | [diff] [blame] | 2987 | ClientSocketPool::kMaxConnectRetryIntervalMs / 2 * 3)); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 2988 | } |
| 2989 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 2990 | // Test delayed socket binding for the case where we have two connects, |
| 2991 | // and while one is waiting on a connect, the other frees up. |
| 2992 | // The socket waiting on a connect should switch immediately to the freed |
| 2993 | // up socket. |
| 2994 | TEST_F(ClientSocketPoolBaseTest, DelayedSocketBindingWaitingForConnect) { |
| 2995 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2996 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2997 | |
| 2998 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2999 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3000 | EXPECT_EQ( |
| 3001 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3002 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3003 | ClientSocketPool::RespectLimits::ENABLED, |
| 3004 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3005 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3006 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3007 | |
| 3008 | // No idle sockets, no pending jobs. |
| 3009 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3010 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3011 | |
| 3012 | // Create a second socket to the same host, but this one will wait. |
| 3013 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3014 | ClientSocketHandle handle2; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3015 | EXPECT_EQ( |
| 3016 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3017 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3018 | ClientSocketPool::RespectLimits::ENABLED, |
| 3019 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3020 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3021 | // No idle sockets, and one connecting job. |
| 3022 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3023 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3024 | |
| 3025 | // Return the first handle to the pool. This will initiate the delayed |
| 3026 | // binding. |
| 3027 | handle1.Reset(); |
| 3028 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3029 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3030 | |
| 3031 | // Still no idle sockets, still one pending connect job. |
| 3032 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3033 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3034 | |
| 3035 | // The second socket connected, even though it was a Waiting Job. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3036 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3037 | |
| 3038 | // And we can see there is still one job waiting. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3039 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3040 | |
| 3041 | // Finally, signal the waiting Connect. |
| 3042 | client_socket_factory_.SignalJobs(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3043 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3044 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3045 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3046 | } |
| 3047 | |
| 3048 | // Test delayed socket binding when a group is at capacity and one |
| 3049 | // of the group's sockets frees up. |
| 3050 | TEST_F(ClientSocketPoolBaseTest, DelayedSocketBindingAtGroupCapacity) { |
| 3051 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3052 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3053 | |
| 3054 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3055 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3056 | EXPECT_EQ( |
| 3057 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3058 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3059 | ClientSocketPool::RespectLimits::ENABLED, |
| 3060 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3061 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3062 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3063 | |
| 3064 | // No idle sockets, no pending jobs. |
| 3065 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3066 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3067 | |
| 3068 | // Create a second socket to the same host, but this one will wait. |
| 3069 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3070 | ClientSocketHandle handle2; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3071 | EXPECT_EQ( |
| 3072 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3073 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3074 | ClientSocketPool::RespectLimits::ENABLED, |
| 3075 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3076 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3077 | // No idle sockets, and one connecting job. |
| 3078 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3079 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3080 | |
| 3081 | // Return the first handle to the pool. This will initiate the delayed |
| 3082 | // binding. |
| 3083 | handle1.Reset(); |
| 3084 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3085 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3086 | |
| 3087 | // Still no idle sockets, still one pending connect job. |
| 3088 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3089 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3090 | |
| 3091 | // The second socket connected, even though it was a Waiting Job. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3092 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3093 | |
| 3094 | // And we can see there is still one job waiting. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3095 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3096 | |
| 3097 | // Finally, signal the waiting Connect. |
| 3098 | client_socket_factory_.SignalJobs(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3099 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3100 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3101 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3102 | } |
| 3103 | |
| 3104 | // Test out the case where we have one socket connected, one |
| 3105 | // connecting, when the first socket finishes and goes idle. |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3106 | // Although the second connection is pending, the second request |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3107 | // should complete, by taking the first socket's idle socket. |
| 3108 | TEST_F(ClientSocketPoolBaseTest, DelayedSocketBindingAtStall) { |
| 3109 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3110 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3111 | |
| 3112 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3113 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3114 | EXPECT_EQ( |
| 3115 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3116 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3117 | ClientSocketPool::RespectLimits::ENABLED, |
| 3118 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3119 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3120 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3121 | |
| 3122 | // No idle sockets, no pending jobs. |
| 3123 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3124 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3125 | |
| 3126 | // Create a second socket to the same host, but this one will wait. |
| 3127 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3128 | ClientSocketHandle handle2; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3129 | EXPECT_EQ( |
| 3130 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3131 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3132 | ClientSocketPool::RespectLimits::ENABLED, |
| 3133 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3134 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3135 | // No idle sockets, and one connecting job. |
| 3136 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3137 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3138 | |
| 3139 | // Return the first handle to the pool. This will initiate the delayed |
| 3140 | // binding. |
| 3141 | handle1.Reset(); |
| 3142 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3143 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3144 | |
| 3145 | // Still no idle sockets, still one pending connect job. |
| 3146 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3147 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3148 | |
| 3149 | // The second socket connected, even though it was a Waiting Job. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3150 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3151 | |
| 3152 | // And we can see there is still one job waiting. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3153 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3154 | |
| 3155 | // Finally, signal the waiting Connect. |
| 3156 | client_socket_factory_.SignalJobs(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3157 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3158 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3159 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3160 | } |
| 3161 | |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3162 | // Cover the case where on an available socket slot, we have one pending |
| 3163 | // request that completes synchronously, thereby making the Group empty. |
| 3164 | TEST_F(ClientSocketPoolBaseTest, SynchronouslyProcessOnePendingRequest) { |
| 3165 | const int kUnlimitedSockets = 100; |
| 3166 | const int kOneSocketPerGroup = 1; |
| 3167 | CreatePool(kUnlimitedSockets, kOneSocketPerGroup); |
| 3168 | |
| 3169 | // Make the first request asynchronous fail. |
| 3170 | // This will free up a socket slot later. |
| 3171 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
| 3172 | |
| 3173 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3174 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3175 | EXPECT_EQ( |
| 3176 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3177 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3178 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3179 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3180 | pool_.get(), NetLogWithSource())); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3181 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3182 | |
| 3183 | // Make the second request synchronously fail. This should make the Group |
| 3184 | // empty. |
| 3185 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
| 3186 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3187 | TestCompletionCallback callback2; |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3188 | // It'll be ERR_IO_PENDING now, but the TestConnectJob will synchronously fail |
| 3189 | // when created. |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3190 | EXPECT_EQ( |
| 3191 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3192 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3193 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3194 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3195 | pool_.get(), NetLogWithSource())); |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3196 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3197 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3198 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3199 | EXPECT_THAT(callback1.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 3200 | EXPECT_THAT(callback2.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3201 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3202 | } |
| 3203 | |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3204 | TEST_F(ClientSocketPoolBaseTest, PreferUsedSocketToUnusedSocket) { |
| 3205 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 3206 | |
| 3207 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3208 | |
| 3209 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3210 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3211 | EXPECT_EQ( |
| 3212 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3213 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3214 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3215 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3216 | pool_.get(), NetLogWithSource())); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3217 | |
| 3218 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3219 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3220 | EXPECT_EQ( |
| 3221 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3222 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3223 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3224 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3225 | pool_.get(), NetLogWithSource())); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3226 | ClientSocketHandle handle3; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3227 | TestCompletionCallback callback3; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3228 | EXPECT_EQ( |
| 3229 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3230 | handle3.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3231 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3232 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3233 | pool_.get(), NetLogWithSource())); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3234 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3235 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3236 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
| 3237 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3238 | |
| 3239 | // Use the socket. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 3240 | EXPECT_EQ(1, handle1.socket()->Write(nullptr, 1, CompletionOnceCallback(), |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 3241 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 3242 | EXPECT_EQ(1, handle3.socket()->Write(nullptr, 1, CompletionOnceCallback(), |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 3243 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3244 | |
| 3245 | handle1.Reset(); |
| 3246 | handle2.Reset(); |
| 3247 | handle3.Reset(); |
| 3248 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3249 | EXPECT_EQ(OK, handle1.Init( |
| 3250 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
| 3251 | ClientSocketPool::RespectLimits::ENABLED, |
| 3252 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3253 | pool_.get(), NetLogWithSource())); |
| 3254 | EXPECT_EQ(OK, handle2.Init( |
| 3255 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
| 3256 | ClientSocketPool::RespectLimits::ENABLED, |
| 3257 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3258 | pool_.get(), NetLogWithSource())); |
| 3259 | EXPECT_EQ(OK, handle3.Init( |
| 3260 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
| 3261 | ClientSocketPool::RespectLimits::ENABLED, |
| 3262 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3263 | pool_.get(), NetLogWithSource())); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3264 | |
| 3265 | EXPECT_TRUE(handle1.socket()->WasEverUsed()); |
| 3266 | EXPECT_TRUE(handle2.socket()->WasEverUsed()); |
| 3267 | EXPECT_FALSE(handle3.socket()->WasEverUsed()); |
| 3268 | } |
| 3269 | |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3270 | TEST_F(ClientSocketPoolBaseTest, RequestSockets) { |
| 3271 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3272 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3273 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3274 | pool_->RequestSockets(TestGroupId("a"), params_, 2, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3275 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3276 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3277 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3278 | EXPECT_EQ(2u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3279 | TestGroupId("a"))); |
| 3280 | EXPECT_EQ(2u, |
| 3281 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3282 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3283 | |
| 3284 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3285 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3286 | EXPECT_EQ( |
| 3287 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3288 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3289 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3290 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3291 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3292 | |
| 3293 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3294 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3295 | EXPECT_EQ( |
| 3296 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3297 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3298 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3299 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3300 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3301 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3302 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3303 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3304 | TestGroupId("a"))); |
| 3305 | EXPECT_EQ(0u, |
| 3306 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3307 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3308 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3309 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3310 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3311 | handle1.Reset(); |
| 3312 | handle2.Reset(); |
| 3313 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3314 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3315 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3316 | TestGroupId("a"))); |
| 3317 | EXPECT_EQ(0u, |
| 3318 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3319 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3320 | } |
| 3321 | |
| 3322 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsWhenAlreadyHaveAConnectJob) { |
| 3323 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3324 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3325 | |
| 3326 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3327 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3328 | EXPECT_EQ( |
| 3329 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3330 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3331 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3332 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3333 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3334 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3335 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3336 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3337 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3338 | TestGroupId("a"))); |
| 3339 | EXPECT_EQ(0u, |
| 3340 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3341 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3342 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3343 | pool_->RequestSockets(TestGroupId("a"), params_, 2, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3344 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3345 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3346 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3347 | TestGroupId("a"))); |
| 3348 | EXPECT_EQ(1u, |
| 3349 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3350 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3351 | |
| 3352 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3353 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3354 | EXPECT_EQ( |
| 3355 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3356 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3357 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3358 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3359 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3360 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3361 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3362 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3363 | TestGroupId("a"))); |
| 3364 | EXPECT_EQ(0u, |
| 3365 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3366 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3367 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3368 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3369 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3370 | handle1.Reset(); |
| 3371 | handle2.Reset(); |
| 3372 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3373 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3374 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3375 | TestGroupId("a"))); |
| 3376 | EXPECT_EQ(0u, |
| 3377 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3378 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3379 | } |
| 3380 | |
| 3381 | TEST_F(ClientSocketPoolBaseTest, |
| 3382 | RequestSocketsWhenAlreadyHaveMultipleConnectJob) { |
| 3383 | CreatePool(4, 4); |
| 3384 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3385 | |
| 3386 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3387 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3388 | EXPECT_EQ( |
| 3389 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3390 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3391 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3392 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3393 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3394 | |
| 3395 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3396 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3397 | EXPECT_EQ( |
| 3398 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3399 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3400 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3401 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3402 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3403 | |
| 3404 | ClientSocketHandle handle3; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3405 | TestCompletionCallback callback3; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3406 | EXPECT_EQ( |
| 3407 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3408 | handle3.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3409 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3410 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3411 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3412 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3413 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3414 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3415 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3416 | TestGroupId("a"))); |
| 3417 | EXPECT_EQ(0u, |
| 3418 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3419 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3420 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3421 | pool_->RequestSockets(TestGroupId("a"), params_, 2, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3422 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3423 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3424 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3425 | TestGroupId("a"))); |
| 3426 | EXPECT_EQ(0u, |
| 3427 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3428 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3429 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3430 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3431 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
| 3432 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3433 | handle1.Reset(); |
| 3434 | handle2.Reset(); |
| 3435 | handle3.Reset(); |
| 3436 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3437 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3438 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3439 | TestGroupId("a"))); |
| 3440 | EXPECT_EQ(0u, |
| 3441 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3442 | EXPECT_EQ(3u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3443 | } |
| 3444 | |
| 3445 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsAtMaxSocketLimit) { |
| 3446 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 3447 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3448 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3449 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3450 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3451 | pool_->RequestSockets(TestGroupId("a"), params_, kDefaultMaxSockets, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3452 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3453 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3454 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3455 | EXPECT_EQ(kDefaultMaxSockets, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3456 | static_cast<int>( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3457 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
| 3458 | EXPECT_EQ( |
| 3459 | kDefaultMaxSockets, |
| 3460 | static_cast<int>(pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3461 | TestGroupId("a")))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3462 | EXPECT_EQ(kDefaultMaxSockets, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3463 | static_cast<int>(pool_->NumUnassignedConnectJobsInGroupForTesting( |
| 3464 | TestGroupId("a")))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3465 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3466 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("b"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3467 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3468 | pool_->RequestSockets(TestGroupId("b"), params_, kDefaultMaxSockets, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3469 | NetLogWithSource()); |
[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 | } |
| 3473 | |
| 3474 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsHitMaxSocketLimit) { |
| 3475 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 3476 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3477 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3478 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3479 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3480 | pool_->RequestSockets(TestGroupId("a"), params_, kDefaultMaxSockets - 1, |
Charlie Harrison | 55ce608 | 2018-05-14 02:25:57 | [diff] [blame] | 3481 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3482 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3483 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3484 | EXPECT_EQ(kDefaultMaxSockets - 1, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3485 | static_cast<int>( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3486 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
| 3487 | EXPECT_EQ( |
| 3488 | kDefaultMaxSockets - 1, |
| 3489 | static_cast<int>(pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3490 | TestGroupId("a")))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3491 | EXPECT_EQ(kDefaultMaxSockets - 1, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3492 | static_cast<int>(pool_->NumUnassignedConnectJobsInGroupForTesting( |
| 3493 | TestGroupId("a")))); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 3494 | EXPECT_FALSE(pool_->IsStalled()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3495 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3496 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("b"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3497 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3498 | pool_->RequestSockets(TestGroupId("b"), params_, kDefaultMaxSockets, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3499 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3500 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3501 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("b"))); |
| 3502 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 3503 | EXPECT_FALSE(pool_->IsStalled()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3504 | } |
| 3505 | |
| 3506 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsCountIdleSockets) { |
| 3507 | CreatePool(4, 4); |
| 3508 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3509 | |
| 3510 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3511 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3512 | EXPECT_EQ( |
| 3513 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3514 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3515 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3516 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3517 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3518 | ASSERT_THAT(callback1.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3519 | handle1.Reset(); |
| 3520 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3521 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3522 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3523 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3524 | TestGroupId("a"))); |
| 3525 | EXPECT_EQ(0u, |
| 3526 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3527 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3528 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3529 | pool_->RequestSockets(TestGroupId("a"), params_, 2, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3530 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3531 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3532 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3533 | TestGroupId("a"))); |
| 3534 | EXPECT_EQ(1u, |
| 3535 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3536 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3537 | } |
| 3538 | |
| 3539 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsCountActiveSockets) { |
| 3540 | CreatePool(4, 4); |
| 3541 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3542 | |
| 3543 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3544 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3545 | EXPECT_EQ( |
| 3546 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3547 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3548 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3549 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3550 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3551 | ASSERT_THAT(callback1.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3552 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3553 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3554 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3555 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3556 | TestGroupId("a"))); |
| 3557 | EXPECT_EQ(0u, |
| 3558 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3559 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3560 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3561 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3562 | pool_->RequestSockets(TestGroupId("a"), params_, 2, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3563 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3564 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3565 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3566 | TestGroupId("a"))); |
| 3567 | EXPECT_EQ(1u, |
| 3568 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3569 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3570 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3571 | } |
| 3572 | |
| 3573 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsSynchronous) { |
| 3574 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3575 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 3576 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3577 | pool_->RequestSockets(TestGroupId("a"), params_, kDefaultMaxSocketsPerGroup, |
Charlie Harrison | 55ce608 | 2018-05-14 02:25:57 | [diff] [blame] | 3578 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3579 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3580 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3581 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3582 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3583 | TestGroupId("a"))); |
| 3584 | EXPECT_EQ(0u, |
| 3585 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3586 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3587 | static_cast<int>(pool_->IdleSocketCountInGroup(TestGroupId("a")))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3588 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3589 | pool_->RequestSockets(TestGroupId("b"), params_, kDefaultMaxSocketsPerGroup, |
Charlie Harrison | 55ce608 | 2018-05-14 02:25:57 | [diff] [blame] | 3590 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3591 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3592 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 3593 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3594 | TestGroupId("b"))); |
| 3595 | EXPECT_EQ(0u, |
| 3596 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3597 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3598 | static_cast<int>(pool_->IdleSocketCountInGroup(TestGroupId("b")))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3599 | } |
| 3600 | |
[email protected] | 3c819f52 | 2010-12-02 02:03:12 | [diff] [blame] | 3601 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsSynchronousError) { |
| 3602 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3603 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
| 3604 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3605 | pool_->RequestSockets(TestGroupId("a"), params_, kDefaultMaxSocketsPerGroup, |
Charlie Harrison | 55ce608 | 2018-05-14 02:25:57 | [diff] [blame] | 3606 | NetLogWithSource()); |
[email protected] | 3c819f52 | 2010-12-02 02:03:12 | [diff] [blame] | 3607 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3608 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | fd2e53e | 2011-01-14 20:40:52 | [diff] [blame] | 3609 | |
| 3610 | connect_job_factory_->set_job_type( |
| 3611 | TestConnectJob::kMockAdditionalErrorStateJob); |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3612 | pool_->RequestSockets(TestGroupId("a"), params_, kDefaultMaxSocketsPerGroup, |
Charlie Harrison | 55ce608 | 2018-05-14 02:25:57 | [diff] [blame] | 3613 | NetLogWithSource()); |
[email protected] | fd2e53e | 2011-01-14 20:40:52 | [diff] [blame] | 3614 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3615 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | 3c819f52 | 2010-12-02 02:03:12 | [diff] [blame] | 3616 | } |
| 3617 | |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3618 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsMultipleTimesDoesNothing) { |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3619 | CreatePool(4, 4); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3620 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3621 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3622 | pool_->RequestSockets(TestGroupId("a"), params_, 2, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3623 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3624 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3625 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3626 | EXPECT_EQ(2u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3627 | TestGroupId("a"))); |
| 3628 | EXPECT_EQ(2u, |
| 3629 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3630 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3631 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3632 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3633 | pool_->RequestSockets(TestGroupId("a"), params_, 2, NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3634 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3635 | EXPECT_EQ(2u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3636 | TestGroupId("a"))); |
| 3637 | EXPECT_EQ(2u, |
| 3638 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3639 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3640 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3641 | |
| 3642 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3643 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3644 | EXPECT_EQ( |
| 3645 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3646 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3647 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3648 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3649 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3650 | |
| 3651 | client_socket_factory_.SignalJob(0); |
| 3652 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3653 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3654 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3655 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3656 | TestGroupId("a"))); |
| 3657 | EXPECT_EQ(1u, |
| 3658 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3659 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3660 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3661 | |
| 3662 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3663 | TestCompletionCallback callback2; |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3664 | EXPECT_EQ( |
| 3665 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3666 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3667 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3668 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3669 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3670 | client_socket_factory_.SignalJob(0); |
| 3671 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3672 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3673 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3674 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3675 | TestGroupId("a"))); |
| 3676 | EXPECT_EQ(0u, |
| 3677 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3678 | EXPECT_EQ(2, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3679 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3680 | |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3681 | handle1.Reset(); |
| 3682 | handle2.Reset(); |
| 3683 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3684 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3685 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3686 | TestGroupId("a"))); |
| 3687 | EXPECT_EQ(0u, |
| 3688 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3689 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3690 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3691 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3692 | pool_->RequestSockets(TestGroupId("a"), params_, 2, NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3693 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3694 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3695 | TestGroupId("a"))); |
| 3696 | EXPECT_EQ(0u, |
| 3697 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3698 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3699 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3700 | } |
| 3701 | |
| 3702 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsDifferentNumSockets) { |
| 3703 | CreatePool(4, 4); |
| 3704 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3705 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3706 | pool_->RequestSockets(TestGroupId("a"), params_, 1, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3707 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3708 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3709 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3710 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3711 | TestGroupId("a"))); |
| 3712 | EXPECT_EQ(1u, |
| 3713 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3714 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3715 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3716 | pool_->RequestSockets(TestGroupId("a"), params_, 2, NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3717 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3718 | EXPECT_EQ(2u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3719 | TestGroupId("a"))); |
| 3720 | EXPECT_EQ(2u, |
| 3721 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3722 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3723 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3724 | pool_->RequestSockets(TestGroupId("a"), params_, 3, NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3725 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3726 | EXPECT_EQ(3u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3727 | TestGroupId("a"))); |
| 3728 | EXPECT_EQ(3u, |
| 3729 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3730 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3731 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3732 | pool_->RequestSockets(TestGroupId("a"), params_, 1, NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3733 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3734 | EXPECT_EQ(3u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3735 | TestGroupId("a"))); |
| 3736 | EXPECT_EQ(3u, |
| 3737 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3738 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3739 | } |
| 3740 | |
| 3741 | TEST_F(ClientSocketPoolBaseTest, PreconnectJobsTakenByNormalRequests) { |
| 3742 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3743 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3744 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3745 | pool_->RequestSockets(TestGroupId("a"), params_, 1, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3746 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3747 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3748 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3749 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3750 | TestGroupId("a"))); |
| 3751 | EXPECT_EQ(1u, |
| 3752 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3753 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3754 | |
| 3755 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3756 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3757 | EXPECT_EQ( |
| 3758 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3759 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3760 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3761 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3762 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3763 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3764 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3765 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3766 | TestGroupId("a"))); |
| 3767 | EXPECT_EQ(0u, |
| 3768 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3769 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3770 | |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3771 | client_socket_factory_.SignalJobs(); |
| 3772 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3773 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3774 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3775 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3776 | TestGroupId("a"))); |
| 3777 | EXPECT_EQ(0u, |
| 3778 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3779 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3780 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3781 | |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 3782 | // Make sure if a preconnected socket is not fully connected when a request |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 3783 | // starts, it has a connect start time. |
| 3784 | TestLoadTimingInfoConnectedNotReused(handle1); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3785 | handle1.Reset(); |
| 3786 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3787 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3788 | } |
| 3789 | |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 3790 | // Checks that fully connected preconnect jobs have no connect times, and are |
| 3791 | // marked as reused. |
| 3792 | TEST_F(ClientSocketPoolBaseTest, ConnectedPreconnectJobsHaveNoConnectTimes) { |
| 3793 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3794 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3795 | pool_->RequestSockets(TestGroupId("a"), params_, 1, NetLogWithSource()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 3796 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3797 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3798 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3799 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3800 | TestGroupId("a"))); |
| 3801 | EXPECT_EQ(0u, |
| 3802 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3803 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 3804 | |
| 3805 | ClientSocketHandle handle; |
| 3806 | TestCompletionCallback callback; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3807 | EXPECT_EQ(OK, handle.Init( |
| 3808 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
| 3809 | ClientSocketPool::RespectLimits::ENABLED, |
| 3810 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3811 | pool_.get(), NetLogWithSource())); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 3812 | |
| 3813 | // Make sure the idle socket was used. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3814 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 3815 | |
| 3816 | TestLoadTimingInfoConnectedReused(handle); |
| 3817 | handle.Reset(); |
| 3818 | TestLoadTimingInfoNotConnected(handle); |
| 3819 | } |
| 3820 | |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3821 | // http://crbug.com/64940 regression test. |
| 3822 | TEST_F(ClientSocketPoolBaseTest, PreconnectClosesIdleSocketRemovesGroup) { |
| 3823 | const int kMaxTotalSockets = 3; |
| 3824 | const int kMaxSocketsPerGroup = 2; |
| 3825 | CreatePool(kMaxTotalSockets, kMaxSocketsPerGroup); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3826 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3827 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 3828 | // Note that group id ordering matters here. "a" comes before "b", so |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3829 | // CloseOneIdleSocket() will try to close "a"'s idle socket. |
| 3830 | |
| 3831 | // Set up one idle socket in "a". |
| 3832 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3833 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3834 | EXPECT_EQ( |
| 3835 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3836 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3837 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3838 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3839 | pool_.get(), NetLogWithSource())); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3840 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3841 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3842 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3843 | TestGroupId("a"))); |
| 3844 | EXPECT_EQ(0u, |
| 3845 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3846 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3847 | |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3848 | client_socket_factory_.SignalJobs(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3849 | ASSERT_THAT(callback1.WaitForResult(), IsOk()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3850 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3851 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3852 | TestGroupId("a"))); |
| 3853 | EXPECT_EQ(0u, |
| 3854 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3855 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3856 | |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3857 | handle1.Reset(); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3858 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3859 | |
| 3860 | // Set up two active sockets in "b". |
| 3861 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3862 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3863 | EXPECT_EQ( |
| 3864 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3865 | handle1.Init(TestGroupId("b"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3866 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3867 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3868 | pool_.get(), NetLogWithSource())); |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3869 | EXPECT_EQ( |
| 3870 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3871 | handle2.Init(TestGroupId("b"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3872 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3873 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3874 | pool_.get(), NetLogWithSource())); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3875 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3876 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("b"))); |
| 3877 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 3878 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3879 | TestGroupId("b"))); |
| 3880 | EXPECT_EQ(0u, |
| 3881 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3882 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3883 | |
| 3884 | client_socket_factory_.SignalJobs(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3885 | ASSERT_THAT(callback1.WaitForResult(), IsOk()); |
| 3886 | ASSERT_THAT(callback2.WaitForResult(), IsOk()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3887 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3888 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3889 | TestGroupId("b"))); |
| 3890 | EXPECT_EQ(0u, |
| 3891 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 3892 | EXPECT_EQ(2, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3893 | |
| 3894 | // Now we have 1 idle socket in "a" and 2 active sockets in "b". This means |
| 3895 | // we've maxed out on sockets, since we set |kMaxTotalSockets| to 3. |
| 3896 | // Requesting 2 preconnected sockets for "a" should fail to allocate any more |
| 3897 | // sockets for "a", and "b" should still have 2 active sockets. |
| 3898 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3899 | pool_->RequestSockets(TestGroupId("a"), params_, 2, NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3900 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3901 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3902 | TestGroupId("a"))); |
| 3903 | EXPECT_EQ(0u, |
| 3904 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3905 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3906 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 3907 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 3908 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3909 | TestGroupId("b"))); |
| 3910 | EXPECT_EQ(0u, |
| 3911 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3912 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3913 | EXPECT_EQ(2, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3914 | |
| 3915 | // Now release the 2 active sockets for "b". This will give us 1 idle socket |
| 3916 | // in "a" and 2 idle sockets in "b". Requesting 2 preconnected sockets for |
| 3917 | // "a" should result in closing 1 for "b". |
| 3918 | handle1.Reset(); |
| 3919 | handle2.Reset(); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3920 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3921 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3922 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3923 | pool_->RequestSockets(TestGroupId("a"), params_, 2, NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3924 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3925 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3926 | TestGroupId("a"))); |
| 3927 | EXPECT_EQ(1u, |
| 3928 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3929 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3930 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 3931 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 3932 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3933 | TestGroupId("b"))); |
| 3934 | EXPECT_EQ(0u, |
| 3935 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3936 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3937 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3938 | } |
| 3939 | |
[email protected] | b7b8be4 | 2011-07-12 12:46:41 | [diff] [blame] | 3940 | TEST_F(ClientSocketPoolBaseTest, PreconnectWithoutBackupJob) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3941 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 3942 | true /* enable_backup_connect_jobs */); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 3943 | |
| 3944 | // Make the ConnectJob hang until it times out, shorten the timeout. |
| 3945 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3946 | connect_job_factory_->set_timeout_duration( |
| 3947 | base::TimeDelta::FromMilliseconds(500)); |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3948 | pool_->RequestSockets(TestGroupId("a"), params_, 1, NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3949 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3950 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3951 | TestGroupId("a"))); |
| 3952 | EXPECT_EQ(1u, |
| 3953 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3954 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 3955 | |
[email protected] | b7b8be4 | 2011-07-12 12:46:41 | [diff] [blame] | 3956 | // Verify the backup timer doesn't create a backup job, by making |
| 3957 | // the backup job a pending job instead of a waiting job, so it |
| 3958 | // *would* complete if it were created. |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 3959 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 3960 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
Gabriel Charette | ea91801 | 2018-05-16 11:53:44 | [diff] [blame] | 3961 | FROM_HERE, base::RunLoop::QuitCurrentWhenIdleClosureDeprecated(), |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 3962 | base::TimeDelta::FromSeconds(1)); |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3963 | base::RunLoop().Run(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3964 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 3965 | } |
| 3966 | |
[email protected] | b7b8be4 | 2011-07-12 12:46:41 | [diff] [blame] | 3967 | TEST_F(ClientSocketPoolBaseTest, PreconnectWithBackupJob) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3968 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 3969 | true /* enable_backup_connect_jobs */); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 3970 | |
| 3971 | // Make the ConnectJob hang forever. |
| 3972 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3973 | pool_->RequestSockets(TestGroupId("a"), params_, 1, NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3974 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3975 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3976 | TestGroupId("a"))); |
| 3977 | EXPECT_EQ(1u, |
| 3978 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3979 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3980 | base::RunLoop().RunUntilIdle(); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 3981 | |
| 3982 | // Make the backup job be a pending job, so it completes normally. |
| 3983 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3984 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3985 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3986 | EXPECT_EQ( |
| 3987 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3988 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3989 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 3990 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 3991 | NetLogWithSource())); |
[email protected] | b7b8be4 | 2011-07-12 12:46:41 | [diff] [blame] | 3992 | // Timer has started, but the backup connect job shouldn't be created yet. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3993 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3994 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3995 | TestGroupId("a"))); |
| 3996 | EXPECT_EQ(0u, |
| 3997 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3998 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3999 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4000 | ASSERT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4001 | |
| 4002 | // The hung connect job should still be there, but everything else should be |
| 4003 | // complete. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4004 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4005 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4006 | TestGroupId("a"))); |
| 4007 | EXPECT_EQ(1u, |
| 4008 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4009 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4010 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4011 | } |
| 4012 | |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4013 | // Tests that a preconnect that starts out with unread data can still be used. |
| 4014 | // http://crbug.com/334467 |
| 4015 | TEST_F(ClientSocketPoolBaseTest, PreconnectWithUnreadData) { |
| 4016 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4017 | connect_job_factory_->set_job_type(TestConnectJob::kMockUnreadDataJob); |
| 4018 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 4019 | pool_->RequestSockets(TestGroupId("a"), params_, 1, NetLogWithSource()); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4020 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4021 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 4022 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4023 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4024 | TestGroupId("a"))); |
| 4025 | EXPECT_EQ(0u, |
| 4026 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4027 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4028 | |
| 4029 | // Fail future jobs to be sure that handle receives the preconnected socket |
| 4030 | // rather than closing it and making a new one. |
| 4031 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
| 4032 | ClientSocketHandle handle; |
| 4033 | TestCompletionCallback callback; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4034 | EXPECT_EQ(OK, handle.Init( |
| 4035 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
| 4036 | ClientSocketPool::RespectLimits::ENABLED, |
| 4037 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4038 | pool_.get(), NetLogWithSource())); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4039 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4040 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 4041 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4042 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4043 | TestGroupId("a"))); |
| 4044 | EXPECT_EQ(0u, |
| 4045 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4046 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4047 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4048 | |
| 4049 | // Drain the pending read. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 4050 | EXPECT_EQ(1, handle.socket()->Read(nullptr, 1, CompletionOnceCallback())); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4051 | |
| 4052 | TestLoadTimingInfoConnectedReused(handle); |
| 4053 | handle.Reset(); |
| 4054 | |
| 4055 | // The socket should be usable now that it's idle again. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4056 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4057 | } |
| 4058 | |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4059 | TEST_F(ClientSocketPoolBaseTest, RequestGetsAssignedJob) { |
| 4060 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4061 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4062 | |
| 4063 | ClientSocketHandle handle1; |
| 4064 | TestCompletionCallback callback1; |
| 4065 | EXPECT_EQ( |
| 4066 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4067 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4068 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4069 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4070 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4071 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4072 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4073 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4074 | TestGroupId("a"))); |
| 4075 | EXPECT_EQ(0u, |
| 4076 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4077 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4078 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4079 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4080 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4081 | } |
| 4082 | |
| 4083 | TEST_F(ClientSocketPoolBaseTest, MultipleRequestsGetAssignedJobs) { |
| 4084 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4085 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4086 | |
| 4087 | ClientSocketHandle handle1; |
| 4088 | TestCompletionCallback callback1; |
| 4089 | EXPECT_EQ( |
| 4090 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4091 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4092 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4093 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4094 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4095 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4096 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4097 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4098 | TestGroupId("a"))); |
| 4099 | EXPECT_EQ(0u, |
| 4100 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4101 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4102 | |
| 4103 | ClientSocketHandle handle2; |
| 4104 | TestCompletionCallback callback2; |
| 4105 | EXPECT_EQ( |
| 4106 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4107 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4108 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4109 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4110 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4111 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4112 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4113 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4114 | TestGroupId("a"))); |
| 4115 | EXPECT_EQ(0u, |
| 4116 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4117 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4118 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4119 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4120 | &handle1)); |
| 4121 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4122 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4123 | |
| 4124 | // One job completes. The other request should still have its job. |
| 4125 | client_socket_factory_.SignalJob(0); |
| 4126 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 4127 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4128 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4129 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4130 | TestGroupId("a"))); |
| 4131 | EXPECT_EQ(0u, |
| 4132 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4133 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4134 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4135 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4136 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4137 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4138 | } |
| 4139 | |
| 4140 | TEST_F(ClientSocketPoolBaseTest, PreconnectJobGetsAssignedToRequest) { |
| 4141 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4142 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4143 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 4144 | pool_->RequestSockets(TestGroupId("a"), params_, 1, NetLogWithSource()); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4145 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4146 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 4147 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4148 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4149 | TestGroupId("a"))); |
| 4150 | EXPECT_EQ(1u, |
| 4151 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4152 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4153 | |
| 4154 | ClientSocketHandle handle1; |
| 4155 | TestCompletionCallback callback1; |
| 4156 | EXPECT_EQ( |
| 4157 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4158 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4159 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4160 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4161 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4162 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4163 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4164 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4165 | TestGroupId("a"))); |
| 4166 | EXPECT_EQ(0u, |
| 4167 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4168 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4169 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4170 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4171 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4172 | } |
| 4173 | |
| 4174 | TEST_F(ClientSocketPoolBaseTest, HigherPriorityRequestStealsJob) { |
| 4175 | CreatePool(kDefaultMaxSockets, 1); |
| 4176 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4177 | |
| 4178 | ClientSocketHandle handle1; |
| 4179 | TestCompletionCallback callback1; |
| 4180 | EXPECT_EQ( |
| 4181 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4182 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4183 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4184 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4185 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4186 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4187 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4188 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4189 | TestGroupId("a"))); |
| 4190 | EXPECT_EQ(0u, |
| 4191 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4192 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4193 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4194 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4195 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4196 | |
| 4197 | // Insert a higher priority request |
| 4198 | ClientSocketHandle handle2; |
| 4199 | TestCompletionCallback callback2; |
| 4200 | EXPECT_EQ( |
| 4201 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4202 | handle2.Init(TestGroupId("a"), params_, HIGHEST, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4203 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4204 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4205 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4206 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4207 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4208 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4209 | TestGroupId("a"))); |
| 4210 | EXPECT_EQ(0u, |
| 4211 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4212 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4213 | |
| 4214 | // The highest priority request should steal the job from the default priority |
| 4215 | // request. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4216 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4217 | &handle2)); |
| 4218 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4219 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4220 | } |
| 4221 | |
| 4222 | TEST_F(ClientSocketPoolBaseTest, RequestStealsJobFromLowestRequestWithJob) { |
| 4223 | CreatePool(3, 3); |
| 4224 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4225 | |
| 4226 | ClientSocketHandle handle_lowest; |
| 4227 | TestCompletionCallback callback_lowest; |
| 4228 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4229 | handle_lowest.Init(TestGroupId("a"), params_, LOWEST, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4230 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4231 | callback_lowest.callback(), |
| 4232 | ClientSocketPool::ProxyAuthCallback(), |
| 4233 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4234 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4235 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4236 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4237 | TestGroupId("a"))); |
| 4238 | EXPECT_EQ(0u, |
| 4239 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4240 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4241 | |
| 4242 | ClientSocketHandle handle_highest; |
| 4243 | TestCompletionCallback callback_highest; |
| 4244 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4245 | handle_highest.Init(TestGroupId("a"), params_, HIGHEST, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4246 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4247 | callback_highest.callback(), |
| 4248 | ClientSocketPool::ProxyAuthCallback(), |
| 4249 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4250 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4251 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4252 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4253 | TestGroupId("a"))); |
| 4254 | EXPECT_EQ(0u, |
| 4255 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4256 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4257 | |
| 4258 | ClientSocketHandle handle_low; |
| 4259 | TestCompletionCallback callback_low; |
| 4260 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4261 | handle_low.Init(TestGroupId("a"), params_, LOW, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4262 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4263 | callback_low.callback(), |
| 4264 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4265 | NetLogWithSource())); |
| 4266 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4267 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4268 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4269 | TestGroupId("a"))); |
| 4270 | EXPECT_EQ(0u, |
| 4271 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4272 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4273 | |
| 4274 | ClientSocketHandle handle_lowest2; |
| 4275 | TestCompletionCallback callback_lowest2; |
| 4276 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4277 | handle_lowest2.Init(TestGroupId("a"), params_, LOWEST, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4278 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4279 | callback_lowest2.callback(), |
| 4280 | ClientSocketPool::ProxyAuthCallback(), |
| 4281 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4282 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4283 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4284 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4285 | TestGroupId("a"))); |
| 4286 | EXPECT_EQ(0u, |
| 4287 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4288 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4289 | |
| 4290 | // The top three requests in the queue should have jobs. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4291 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4292 | &handle_highest)); |
| 4293 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4294 | &handle_low)); |
| 4295 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4296 | &handle_lowest)); |
| 4297 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting( |
| 4298 | TestGroupId("a"), &handle_lowest2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4299 | |
| 4300 | // Add another request with medium priority. It should steal the job from the |
| 4301 | // lowest priority request with a job. |
| 4302 | ClientSocketHandle handle_medium; |
| 4303 | TestCompletionCallback callback_medium; |
| 4304 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4305 | handle_medium.Init(TestGroupId("a"), params_, MEDIUM, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4306 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4307 | callback_medium.callback(), |
| 4308 | ClientSocketPool::ProxyAuthCallback(), |
| 4309 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4310 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4311 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4312 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4313 | TestGroupId("a"))); |
| 4314 | EXPECT_EQ(0u, |
| 4315 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4316 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
| 4317 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4318 | &handle_highest)); |
| 4319 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4320 | &handle_medium)); |
| 4321 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4322 | &handle_low)); |
| 4323 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4324 | &handle_lowest)); |
| 4325 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting( |
| 4326 | TestGroupId("a"), &handle_lowest2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4327 | } |
| 4328 | |
| 4329 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeRequestStealsJob) { |
| 4330 | CreatePool(kDefaultMaxSockets, 1); |
| 4331 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4332 | |
| 4333 | ClientSocketHandle handle1; |
| 4334 | TestCompletionCallback callback1; |
| 4335 | EXPECT_EQ( |
| 4336 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4337 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4338 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4339 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4340 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4341 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4342 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4343 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4344 | TestGroupId("a"))); |
| 4345 | EXPECT_EQ(0u, |
| 4346 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4347 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4348 | |
| 4349 | ClientSocketHandle handle2; |
| 4350 | TestCompletionCallback callback2; |
| 4351 | EXPECT_EQ( |
| 4352 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4353 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4354 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4355 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4356 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4357 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4358 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4359 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4360 | TestGroupId("a"))); |
| 4361 | EXPECT_EQ(0u, |
| 4362 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4363 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4364 | |
| 4365 | // 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] | 4366 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4367 | &handle1)); |
| 4368 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4369 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4370 | |
| 4371 | // Reprioritizing the second request places it above the first, and it steals |
| 4372 | // the job from the first request. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4373 | pool_->SetPriority(TestGroupId("a"), &handle2, HIGHEST); |
| 4374 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4375 | &handle2)); |
| 4376 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4377 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4378 | } |
| 4379 | |
| 4380 | TEST_F(ClientSocketPoolBaseTest, CancelRequestReassignsJob) { |
| 4381 | CreatePool(kDefaultMaxSockets, 1); |
| 4382 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4383 | |
| 4384 | ClientSocketHandle handle1; |
| 4385 | TestCompletionCallback callback1; |
| 4386 | EXPECT_EQ( |
| 4387 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4388 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4389 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4390 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4391 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4392 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4393 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4394 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4395 | TestGroupId("a"))); |
| 4396 | EXPECT_EQ(0u, |
| 4397 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4398 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4399 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4400 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4401 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4402 | |
| 4403 | ClientSocketHandle handle2; |
| 4404 | TestCompletionCallback callback2; |
| 4405 | EXPECT_EQ( |
| 4406 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4407 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4408 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4409 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4410 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4411 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4412 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4413 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4414 | TestGroupId("a"))); |
| 4415 | EXPECT_EQ(0u, |
| 4416 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4417 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4418 | |
| 4419 | // The second request doesn't get a job because we are the limit. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4420 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4421 | &handle1)); |
| 4422 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4423 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4424 | |
| 4425 | // The second request should get a job upon cancelling the first request. |
| 4426 | handle1.Reset(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4427 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4428 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4429 | TestGroupId("a"))); |
| 4430 | EXPECT_EQ(0u, |
| 4431 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4432 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4433 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4434 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4435 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4436 | } |
| 4437 | |
| 4438 | TEST_F(ClientSocketPoolBaseTest, JobCompletionReassignsJob) { |
| 4439 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4440 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4441 | |
| 4442 | ClientSocketHandle handle1; |
| 4443 | TestCompletionCallback callback1; |
| 4444 | EXPECT_EQ( |
| 4445 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4446 | handle1.Init(TestGroupId("a"), params_, HIGHEST, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4447 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4448 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4449 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4450 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4451 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4452 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4453 | TestGroupId("a"))); |
| 4454 | EXPECT_EQ(0u, |
| 4455 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4456 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4457 | |
| 4458 | ClientSocketHandle handle2; |
| 4459 | TestCompletionCallback callback2; |
| 4460 | EXPECT_EQ( |
| 4461 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4462 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4463 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4464 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4465 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4466 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4467 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4468 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4469 | TestGroupId("a"))); |
| 4470 | EXPECT_EQ(0u, |
| 4471 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4472 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4473 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4474 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4475 | &handle1)); |
| 4476 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4477 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4478 | |
| 4479 | // The lower-priority job completes first. The higher-priority request should |
| 4480 | // get the socket, and the lower-priority request should get the remaining |
| 4481 | // job. |
| 4482 | client_socket_factory_.SignalJob(1); |
| 4483 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4484 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4485 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4486 | TestGroupId("a"))); |
| 4487 | EXPECT_EQ(0u, |
| 4488 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4489 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4490 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4491 | EXPECT_TRUE(handle1.socket()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4492 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4493 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4494 | } |
| 4495 | |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 4496 | class MockLayeredPool : public HigherLayeredPool { |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4497 | public: |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4498 | MockLayeredPool(TransportClientSocketPool* pool, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4499 | const ClientSocketPool::GroupId& group_id) |
| 4500 | : pool_(pool), group_id_(group_id), can_release_connection_(true) { |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 4501 | pool_->AddHigherLayeredPool(this); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4502 | } |
| 4503 | |
Daniel Cheng | 4496d082 | 2018-04-26 21:52:15 | [diff] [blame] | 4504 | ~MockLayeredPool() override { pool_->RemoveHigherLayeredPool(this); } |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4505 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4506 | int RequestSocket(TransportClientSocketPool* pool) { |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4507 | return handle_.Init( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4508 | group_id_, CreateDummyParams(), DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4509 | ClientSocketPool::RespectLimits::ENABLED, callback_.callback(), |
| 4510 | ClientSocketPool::ProxyAuthCallback(), pool, NetLogWithSource()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4511 | } |
| 4512 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4513 | int RequestSocketWithoutLimits(TransportClientSocketPool* pool) { |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4514 | return handle_.Init( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4515 | group_id_, CreateDummyParams(), MAXIMUM_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4516 | ClientSocketPool::RespectLimits::DISABLED, callback_.callback(), |
| 4517 | ClientSocketPool::ProxyAuthCallback(), pool, NetLogWithSource()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4518 | } |
| 4519 | |
| 4520 | bool ReleaseOneConnection() { |
| 4521 | if (!handle_.is_initialized() || !can_release_connection_) { |
| 4522 | return false; |
| 4523 | } |
| 4524 | handle_.socket()->Disconnect(); |
| 4525 | handle_.Reset(); |
| 4526 | return true; |
| 4527 | } |
| 4528 | |
| 4529 | void set_can_release_connection(bool can_release_connection) { |
| 4530 | can_release_connection_ = can_release_connection; |
| 4531 | } |
| 4532 | |
| 4533 | MOCK_METHOD0(CloseOneIdleConnection, bool()); |
| 4534 | |
| 4535 | private: |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4536 | TransportClientSocketPool* const pool_; |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4537 | ClientSocketHandle handle_; |
| 4538 | TestCompletionCallback callback_; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4539 | const ClientSocketPool::GroupId group_id_; |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4540 | bool can_release_connection_; |
| 4541 | }; |
| 4542 | |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4543 | // Tests the basic case of closing an idle socket in a higher layered pool when |
| 4544 | // a new request is issued and the lower layer pool is stalled. |
| 4545 | TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketsHeldByLayeredPoolWhenNeeded) { |
| 4546 | CreatePool(1, 1); |
| 4547 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 4548 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4549 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("foo")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4550 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4551 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4552 | .WillOnce(Invoke(&mock_layered_pool, |
| 4553 | &MockLayeredPool::ReleaseOneConnection)); |
| 4554 | ClientSocketHandle handle; |
| 4555 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4556 | EXPECT_EQ( |
| 4557 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4558 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4559 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 4560 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4561 | NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4562 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4563 | } |
| 4564 | |
Matt Menke | 83367864 | 2019-03-05 22:05:51 | [diff] [blame] | 4565 | // Tests the case that trying to close an idle socket in a higher layered pool |
| 4566 | // fails. |
| 4567 | TEST_F(ClientSocketPoolBaseTest, |
| 4568 | CloseIdleSocketsHeldByLayeredPoolWhenNeededFails) { |
| 4569 | CreatePool(1, 1); |
| 4570 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 4571 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4572 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("foo")); |
Matt Menke | 83367864 | 2019-03-05 22:05:51 | [diff] [blame] | 4573 | mock_layered_pool.set_can_release_connection(false); |
| 4574 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
| 4575 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4576 | .WillOnce(Invoke(&mock_layered_pool, |
| 4577 | &MockLayeredPool::ReleaseOneConnection)); |
| 4578 | ClientSocketHandle handle; |
| 4579 | TestCompletionCallback callback; |
| 4580 | EXPECT_EQ( |
| 4581 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4582 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 83367864 | 2019-03-05 22:05:51 | [diff] [blame] | 4583 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 4584 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4585 | NetLogWithSource())); |
| 4586 | base::RunLoop().RunUntilIdle(); |
| 4587 | EXPECT_FALSE(callback.have_result()); |
| 4588 | } |
| 4589 | |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4590 | // Same as above, but the idle socket is in the same group as the stalled |
| 4591 | // socket, and closes the only other request in its group when closing requests |
| 4592 | // in higher layered pools. This generally shouldn't happen, but it may be |
| 4593 | // possible if a higher level pool issues a request and the request is |
| 4594 | // subsequently cancelled. Even if it's not possible, best not to crash. |
| 4595 | TEST_F(ClientSocketPoolBaseTest, |
| 4596 | CloseIdleSocketsHeldByLayeredPoolWhenNeededSameGroup) { |
| 4597 | CreatePool(2, 2); |
| 4598 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 4599 | |
| 4600 | // Need a socket in another group for the pool to be stalled (If a group |
| 4601 | // has the maximum number of connections already, it's not stalled). |
| 4602 | ClientSocketHandle handle1; |
| 4603 | TestCompletionCallback callback1; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4604 | EXPECT_EQ(OK, handle1.Init( |
| 4605 | TestGroupId("group1"), params_, DEFAULT_PRIORITY, |
| 4606 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4607 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4608 | pool_.get(), NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4609 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4610 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("group2")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4611 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4612 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4613 | .WillOnce(Invoke(&mock_layered_pool, |
| 4614 | &MockLayeredPool::ReleaseOneConnection)); |
| 4615 | ClientSocketHandle handle; |
| 4616 | TestCompletionCallback callback2; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4617 | EXPECT_EQ( |
| 4618 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4619 | handle.Init(TestGroupId("group2"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4620 | ClientSocketPool::RespectLimits::ENABLED, |
| 4621 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4622 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4623 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4624 | } |
| 4625 | |
| 4626 | // Tests the case when an idle socket can be closed when a new request is |
| 4627 | // issued, and the new request belongs to a group that was previously stalled. |
| 4628 | TEST_F(ClientSocketPoolBaseTest, |
| 4629 | CloseIdleSocketsHeldByLayeredPoolInSameGroupWhenNeeded) { |
| 4630 | CreatePool(2, 2); |
| 4631 | std::list<TestConnectJob::JobType> job_types; |
| 4632 | job_types.push_back(TestConnectJob::kMockJob); |
| 4633 | job_types.push_back(TestConnectJob::kMockJob); |
| 4634 | job_types.push_back(TestConnectJob::kMockJob); |
| 4635 | job_types.push_back(TestConnectJob::kMockJob); |
| 4636 | connect_job_factory_->set_job_types(&job_types); |
| 4637 | |
| 4638 | ClientSocketHandle handle1; |
| 4639 | TestCompletionCallback callback1; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4640 | EXPECT_EQ(OK, handle1.Init( |
| 4641 | TestGroupId("group1"), params_, DEFAULT_PRIORITY, |
| 4642 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4643 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4644 | pool_.get(), NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4645 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4646 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("group2")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4647 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4648 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4649 | .WillRepeatedly(Invoke(&mock_layered_pool, |
| 4650 | &MockLayeredPool::ReleaseOneConnection)); |
| 4651 | mock_layered_pool.set_can_release_connection(false); |
| 4652 | |
| 4653 | // The third request is made when the socket pool is in a stalled state. |
| 4654 | ClientSocketHandle handle3; |
| 4655 | TestCompletionCallback callback3; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4656 | EXPECT_EQ( |
| 4657 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4658 | handle3.Init(TestGroupId("group3"), params_, DEFAULT_PRIORITY, |
| 4659 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4660 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4661 | pool_.get(), NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4662 | |
| 4663 | base::RunLoop().RunUntilIdle(); |
| 4664 | EXPECT_FALSE(callback3.have_result()); |
| 4665 | |
| 4666 | // The fourth request is made when the pool is no longer stalled. The third |
| 4667 | // request should be serviced first, since it was issued first and has the |
| 4668 | // same priority. |
| 4669 | mock_layered_pool.set_can_release_connection(true); |
| 4670 | ClientSocketHandle handle4; |
| 4671 | TestCompletionCallback callback4; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4672 | EXPECT_EQ( |
| 4673 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4674 | handle4.Init(TestGroupId("group3"), params_, DEFAULT_PRIORITY, |
| 4675 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4676 | callback4.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4677 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4678 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4679 | EXPECT_FALSE(callback4.have_result()); |
| 4680 | |
| 4681 | // Closing a handle should free up another socket slot. |
| 4682 | handle1.Reset(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4683 | EXPECT_THAT(callback4.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4684 | } |
| 4685 | |
| 4686 | // Tests the case when an idle socket can be closed when a new request is |
| 4687 | // issued, and the new request belongs to a group that was previously stalled. |
| 4688 | // |
| 4689 | // The two differences from the above test are that the stalled requests are not |
| 4690 | // in the same group as the layered pool's request, and the the fourth request |
| 4691 | // has a higher priority than the third one, so gets a socket first. |
| 4692 | TEST_F(ClientSocketPoolBaseTest, |
| 4693 | CloseIdleSocketsHeldByLayeredPoolInSameGroupWhenNeeded2) { |
| 4694 | CreatePool(2, 2); |
| 4695 | std::list<TestConnectJob::JobType> job_types; |
| 4696 | job_types.push_back(TestConnectJob::kMockJob); |
| 4697 | job_types.push_back(TestConnectJob::kMockJob); |
| 4698 | job_types.push_back(TestConnectJob::kMockJob); |
| 4699 | job_types.push_back(TestConnectJob::kMockJob); |
| 4700 | connect_job_factory_->set_job_types(&job_types); |
| 4701 | |
| 4702 | ClientSocketHandle handle1; |
| 4703 | TestCompletionCallback callback1; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4704 | EXPECT_EQ(OK, handle1.Init( |
| 4705 | TestGroupId("group1"), params_, DEFAULT_PRIORITY, |
| 4706 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4707 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4708 | pool_.get(), NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4709 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4710 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("group2")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4711 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4712 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4713 | .WillRepeatedly(Invoke(&mock_layered_pool, |
| 4714 | &MockLayeredPool::ReleaseOneConnection)); |
| 4715 | mock_layered_pool.set_can_release_connection(false); |
| 4716 | |
| 4717 | // The third request is made when the socket pool is in a stalled state. |
| 4718 | ClientSocketHandle handle3; |
| 4719 | TestCompletionCallback callback3; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4720 | EXPECT_EQ( |
| 4721 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4722 | handle3.Init(TestGroupId("group3"), params_, MEDIUM, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4723 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4724 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4725 | pool_.get(), NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4726 | |
| 4727 | base::RunLoop().RunUntilIdle(); |
| 4728 | EXPECT_FALSE(callback3.have_result()); |
| 4729 | |
| 4730 | // The fourth request is made when the pool is no longer stalled. This |
| 4731 | // request has a higher priority than the third request, so is serviced first. |
| 4732 | mock_layered_pool.set_can_release_connection(true); |
| 4733 | ClientSocketHandle handle4; |
| 4734 | TestCompletionCallback callback4; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4735 | EXPECT_EQ( |
| 4736 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4737 | handle4.Init(TestGroupId("group3"), params_, HIGHEST, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4738 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4739 | callback4.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4740 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4741 | EXPECT_THAT(callback4.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4742 | EXPECT_FALSE(callback3.have_result()); |
| 4743 | |
| 4744 | // Closing a handle should free up another socket slot. |
| 4745 | handle1.Reset(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4746 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4747 | } |
| 4748 | |
| 4749 | TEST_F(ClientSocketPoolBaseTest, |
| 4750 | CloseMultipleIdleSocketsHeldByLayeredPoolWhenNeeded) { |
| 4751 | CreatePool(1, 1); |
| 4752 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 4753 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4754 | MockLayeredPool mock_layered_pool1(pool_.get(), TestGroupId("foo")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4755 | EXPECT_THAT(mock_layered_pool1.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4756 | EXPECT_CALL(mock_layered_pool1, CloseOneIdleConnection()) |
| 4757 | .WillRepeatedly(Invoke(&mock_layered_pool1, |
| 4758 | &MockLayeredPool::ReleaseOneConnection)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4759 | MockLayeredPool mock_layered_pool2(pool_.get(), TestGroupId("bar")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4760 | EXPECT_THAT(mock_layered_pool2.RequestSocketWithoutLimits(pool_.get()), |
| 4761 | IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4762 | EXPECT_CALL(mock_layered_pool2, CloseOneIdleConnection()) |
| 4763 | .WillRepeatedly(Invoke(&mock_layered_pool2, |
| 4764 | &MockLayeredPool::ReleaseOneConnection)); |
| 4765 | ClientSocketHandle handle; |
| 4766 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4767 | EXPECT_EQ( |
| 4768 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4769 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4770 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 4771 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4772 | NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4773 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4774 | } |
| 4775 | |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4776 | // Test that when a socket pool and group are at their limits, a request |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4777 | // with RespectLimits::DISABLED triggers creation of a new socket, and gets the |
| 4778 | // socket instead of a request with the same priority that was issued earlier, |
| 4779 | // but has RespectLimits::ENABLED. |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4780 | TEST_F(ClientSocketPoolBaseTest, IgnoreLimits) { |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4781 | CreatePool(1, 1); |
| 4782 | |
| 4783 | // Issue a request to reach the socket pool limit. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4784 | EXPECT_EQ(OK, StartRequestWithIgnoreLimits( |
| 4785 | TestGroupId("a"), MAXIMUM_PRIORITY, |
| 4786 | ClientSocketPool::RespectLimits::ENABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4787 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4788 | |
| 4789 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 4790 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4791 | EXPECT_EQ(ERR_IO_PENDING, StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4792 | TestGroupId("a"), MAXIMUM_PRIORITY, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4793 | ClientSocketPool::RespectLimits::ENABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4794 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4795 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4796 | // Issue a request that ignores the limits, so a new ConnectJob is |
| 4797 | // created. |
| 4798 | EXPECT_EQ(ERR_IO_PENDING, StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4799 | TestGroupId("a"), MAXIMUM_PRIORITY, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4800 | ClientSocketPool::RespectLimits::DISABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4801 | ASSERT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4802 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4803 | EXPECT_THAT(request(2)->WaitForResult(), IsOk()); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4804 | EXPECT_FALSE(request(1)->have_result()); |
| 4805 | } |
| 4806 | |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 4807 | // Test that when a socket pool and group are at their limits, a ConnectJob |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4808 | // issued for a request with RespectLimits::DISABLED is not cancelled when a |
| 4809 | // request with RespectLimits::ENABLED issued to the same group is cancelled. |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 4810 | TEST_F(ClientSocketPoolBaseTest, IgnoreLimitsCancelOtherJob) { |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 4811 | CreatePool(1, 1); |
| 4812 | |
| 4813 | // Issue a request to reach the socket pool limit. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4814 | EXPECT_EQ(OK, StartRequestWithIgnoreLimits( |
| 4815 | TestGroupId("a"), MAXIMUM_PRIORITY, |
| 4816 | ClientSocketPool::RespectLimits::ENABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4817 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 4818 | |
| 4819 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 4820 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4821 | EXPECT_EQ(ERR_IO_PENDING, StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4822 | TestGroupId("a"), MAXIMUM_PRIORITY, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4823 | ClientSocketPool::RespectLimits::ENABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4824 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 4825 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4826 | // Issue a request with RespectLimits::DISABLED, so a new ConnectJob is |
| 4827 | // created. |
| 4828 | EXPECT_EQ(ERR_IO_PENDING, StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4829 | TestGroupId("a"), MAXIMUM_PRIORITY, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4830 | ClientSocketPool::RespectLimits::DISABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4831 | ASSERT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4832 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4833 | // Cancel the pending request with RespectLimits::ENABLED. The ConnectJob |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4834 | // should not be cancelled. |
| 4835 | request(1)->handle()->Reset(); |
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 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4838 | EXPECT_THAT(request(2)->WaitForResult(), IsOk()); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4839 | EXPECT_FALSE(request(1)->have_result()); |
| 4840 | } |
| 4841 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4842 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthNoAuthCallback) { |
| 4843 | CreatePool(1, 1); |
| 4844 | |
| 4845 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 4846 | |
| 4847 | ClientSocketHandle handle; |
| 4848 | TestCompletionCallback callback; |
| 4849 | EXPECT_EQ( |
| 4850 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4851 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4852 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 4853 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4854 | NetLogWithSource())); |
| 4855 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4856 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4857 | |
| 4858 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_PROXY_AUTH_REQUESTED)); |
| 4859 | EXPECT_FALSE(handle.is_initialized()); |
| 4860 | EXPECT_FALSE(handle.socket()); |
| 4861 | |
| 4862 | // The group should now be empty, and thus be deleted. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4863 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4864 | } |
| 4865 | |
| 4866 | class TestAuthHelper { |
| 4867 | public: |
| 4868 | TestAuthHelper() = default; |
| 4869 | ~TestAuthHelper() = default; |
| 4870 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4871 | void InitHandle( |
Matt Menke | 84d11e56 | 2019-03-27 00:11:19 | [diff] [blame] | 4872 | scoped_refptr<ClientSocketPool::SocketParams> params, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4873 | TransportClientSocketPool* pool, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4874 | RequestPriority priority = DEFAULT_PRIORITY, |
| 4875 | ClientSocketPool::RespectLimits respect_limits = |
| 4876 | ClientSocketPool::RespectLimits::ENABLED, |
| 4877 | const ClientSocketPool::GroupId& group_id_in = TestGroupId("a")) { |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4878 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4879 | handle_.Init(group_id_in, params, priority, SocketTag(), |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4880 | respect_limits, callback_.callback(), |
| 4881 | base::BindRepeating(&TestAuthHelper::AuthCallback, |
| 4882 | base::Unretained(this)), |
| 4883 | pool, NetLogWithSource())); |
| 4884 | } |
| 4885 | |
| 4886 | void WaitForAuth() { |
| 4887 | run_loop_ = std::make_unique<base::RunLoop>(); |
| 4888 | run_loop_->Run(); |
| 4889 | run_loop_.reset(); |
| 4890 | } |
| 4891 | |
| 4892 | void WaitForAuthAndRestartSync() { |
| 4893 | restart_sync_ = true; |
| 4894 | WaitForAuth(); |
| 4895 | restart_sync_ = false; |
| 4896 | } |
| 4897 | |
| 4898 | void WaitForAuthAndResetHandleSync() { |
| 4899 | reset_handle_sync_ = true; |
| 4900 | WaitForAuth(); |
| 4901 | reset_handle_sync_ = false; |
| 4902 | } |
| 4903 | |
| 4904 | void RestartWithAuth() { |
| 4905 | DCHECK(restart_with_auth_callback_); |
| 4906 | std::move(restart_with_auth_callback_).Run(); |
| 4907 | } |
| 4908 | |
| 4909 | int WaitForResult() { |
| 4910 | int result = callback_.WaitForResult(); |
| 4911 | // There shouldn't be any callback waiting to be invoked once the request is |
| 4912 | // complete. |
| 4913 | EXPECT_FALSE(restart_with_auth_callback_); |
| 4914 | // The socket should only be initialized on success. |
| 4915 | EXPECT_EQ(result == OK, handle_.is_initialized()); |
| 4916 | EXPECT_EQ(result == OK, handle_.socket() != nullptr); |
| 4917 | return result; |
| 4918 | } |
| 4919 | |
| 4920 | ClientSocketHandle* handle() { return &handle_; } |
| 4921 | int auth_count() const { return auth_count_; } |
| 4922 | int have_result() const { return callback_.have_result(); } |
| 4923 | |
| 4924 | private: |
| 4925 | void AuthCallback(const HttpResponseInfo& response, |
| 4926 | HttpAuthController* auth_controller, |
| 4927 | base::OnceClosure restart_with_auth_callback) { |
| 4928 | EXPECT_FALSE(restart_with_auth_callback_); |
| 4929 | EXPECT_TRUE(restart_with_auth_callback); |
| 4930 | |
| 4931 | // Once there's a result, this method shouldn't be invoked again. |
| 4932 | EXPECT_FALSE(callback_.have_result()); |
| 4933 | |
| 4934 | ++auth_count_; |
| 4935 | run_loop_->Quit(); |
| 4936 | if (restart_sync_) { |
| 4937 | std::move(restart_with_auth_callback).Run(); |
| 4938 | return; |
| 4939 | } |
| 4940 | |
| 4941 | restart_with_auth_callback_ = std::move(restart_with_auth_callback); |
| 4942 | |
| 4943 | if (reset_handle_sync_) { |
| 4944 | handle_.Reset(); |
| 4945 | return; |
| 4946 | } |
| 4947 | } |
| 4948 | |
| 4949 | std::unique_ptr<base::RunLoop> run_loop_; |
| 4950 | base::OnceClosure restart_with_auth_callback_; |
| 4951 | |
| 4952 | bool restart_sync_ = false; |
| 4953 | bool reset_handle_sync_ = false; |
| 4954 | |
| 4955 | ClientSocketHandle handle_; |
| 4956 | int auth_count_ = 0; |
| 4957 | TestCompletionCallback callback_; |
| 4958 | |
| 4959 | DISALLOW_COPY_AND_ASSIGN(TestAuthHelper); |
| 4960 | }; |
| 4961 | |
| 4962 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnce) { |
| 4963 | CreatePool(1, 1); |
| 4964 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 4965 | |
| 4966 | TestAuthHelper auth_helper; |
| 4967 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4968 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 4969 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4970 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4971 | |
| 4972 | auth_helper.WaitForAuth(); |
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_ESTABLISHING_PROXY_TUNNEL, |
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.RestartWithAuth(); |
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 | EXPECT_THAT(auth_helper.WaitForResult(), IsOk()); |
| 4983 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4984 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4985 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4986 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4987 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 4988 | } |
| 4989 | |
| 4990 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceSync) { |
| 4991 | CreatePool(1, 1); |
| 4992 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 4993 | |
| 4994 | TestAuthHelper auth_helper; |
| 4995 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4996 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 4997 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4998 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4999 | |
| 5000 | auth_helper.WaitForAuthAndRestartSync(); |
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_ESTABLISHING_PROXY_TUNNEL, |
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 | EXPECT_THAT(auth_helper.WaitForResult(), IsOk()); |
| 5006 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5007 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5008 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5009 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5010 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5011 | } |
| 5012 | |
| 5013 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceFails) { |
| 5014 | CreatePool(1, 1); |
| 5015 | connect_job_factory_->set_job_type( |
| 5016 | TestConnectJob::kMockAuthChallengeOnceFailingJob); |
| 5017 | |
| 5018 | TestAuthHelper auth_helper; |
| 5019 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5020 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5021 | |
| 5022 | auth_helper.WaitForAuth(); |
| 5023 | auth_helper.RestartWithAuth(); |
| 5024 | EXPECT_THAT(auth_helper.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 5025 | |
| 5026 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5027 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5028 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5029 | } |
| 5030 | |
| 5031 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceSyncFails) { |
| 5032 | CreatePool(1, 1); |
| 5033 | connect_job_factory_->set_job_type( |
| 5034 | TestConnectJob::kMockAuthChallengeOnceFailingJob); |
| 5035 | |
| 5036 | TestAuthHelper auth_helper; |
| 5037 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5038 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5039 | |
| 5040 | auth_helper.WaitForAuthAndRestartSync(); |
| 5041 | EXPECT_THAT(auth_helper.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 5042 | |
| 5043 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5044 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5045 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5046 | } |
| 5047 | |
| 5048 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceDeleteHandle) { |
| 5049 | CreatePool(1, 1); |
| 5050 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5051 | |
| 5052 | TestAuthHelper auth_helper; |
| 5053 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5054 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5055 | |
| 5056 | auth_helper.WaitForAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5057 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5058 | |
| 5059 | auth_helper.handle()->Reset(); |
| 5060 | |
| 5061 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5062 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5063 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5064 | EXPECT_FALSE(auth_helper.handle()->is_initialized()); |
| 5065 | EXPECT_FALSE(auth_helper.handle()->socket()); |
| 5066 | } |
| 5067 | |
| 5068 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceDeleteHandleSync) { |
| 5069 | CreatePool(1, 1); |
| 5070 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5071 | |
| 5072 | TestAuthHelper auth_helper; |
| 5073 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5074 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5075 | |
| 5076 | auth_helper.WaitForAuthAndResetHandleSync(); |
| 5077 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5078 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5079 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5080 | EXPECT_FALSE(auth_helper.handle()->is_initialized()); |
| 5081 | EXPECT_FALSE(auth_helper.handle()->socket()); |
| 5082 | } |
| 5083 | |
| 5084 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceFlushWithError) { |
| 5085 | CreatePool(1, 1); |
| 5086 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5087 | |
| 5088 | TestAuthHelper auth_helper; |
| 5089 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5090 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5091 | |
| 5092 | auth_helper.WaitForAuth(); |
| 5093 | |
| 5094 | pool_->FlushWithError(ERR_FAILED); |
| 5095 | base::RunLoop().RunUntilIdle(); |
| 5096 | |
| 5097 | // When flushing the socket pool, bound sockets should delay returning the |
| 5098 | // error until completion. |
| 5099 | EXPECT_FALSE(auth_helper.have_result()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5100 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5101 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5102 | |
| 5103 | auth_helper.RestartWithAuth(); |
| 5104 | // The callback should be called asynchronously. |
| 5105 | EXPECT_FALSE(auth_helper.have_result()); |
| 5106 | |
| 5107 | EXPECT_THAT(auth_helper.WaitForResult(), IsError(ERR_FAILED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5108 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5109 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5110 | } |
| 5111 | |
| 5112 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthTwice) { |
| 5113 | CreatePool(1, 1); |
| 5114 | connect_job_factory_->set_job_type( |
| 5115 | TestConnectJob::kMockAuthChallengeTwiceJob); |
| 5116 | |
| 5117 | TestAuthHelper auth_helper; |
| 5118 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5119 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5120 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5121 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5122 | |
| 5123 | auth_helper.WaitForAuth(); |
| 5124 | auth_helper.RestartWithAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5125 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5126 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5127 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5128 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5129 | |
| 5130 | auth_helper.WaitForAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5131 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5132 | EXPECT_EQ(2, auth_helper.auth_count()); |
| 5133 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5134 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5135 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5136 | auth_helper.RestartWithAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5137 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5138 | EXPECT_EQ(2, auth_helper.auth_count()); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5139 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5140 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5141 | |
| 5142 | EXPECT_THAT(auth_helper.WaitForResult(), IsOk()); |
| 5143 | EXPECT_EQ(2, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5144 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5145 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5146 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5147 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5148 | } |
| 5149 | |
| 5150 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthTwiceFails) { |
| 5151 | CreatePool(1, 1); |
| 5152 | connect_job_factory_->set_job_type( |
| 5153 | TestConnectJob::kMockAuthChallengeTwiceFailingJob); |
| 5154 | |
| 5155 | TestAuthHelper auth_helper; |
| 5156 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5157 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5158 | |
| 5159 | auth_helper.WaitForAuth(); |
| 5160 | auth_helper.RestartWithAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5161 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5162 | EXPECT_EQ(1, auth_helper.auth_count()); |
| 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(2, auth_helper.auth_count()); |
| 5168 | |
| 5169 | EXPECT_THAT(auth_helper.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 5170 | EXPECT_EQ(2, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5171 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5172 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5173 | } |
| 5174 | |
| 5175 | // Makes sure that when a bound request is destroyed, a new ConnectJob is |
| 5176 | // created, if needed. |
| 5177 | TEST_F(ClientSocketPoolBaseTest, |
| 5178 | ProxyAuthCreateNewConnectJobOnDestroyBoundRequest) { |
| 5179 | CreatePool(1 /* max_sockets */, 1 /* max_sockets_per_group */); |
| 5180 | connect_job_factory_->set_job_type( |
| 5181 | TestConnectJob::kMockAuthChallengeOnceFailingJob); |
| 5182 | |
| 5183 | // First request creates a ConnectJob. |
| 5184 | TestAuthHelper auth_helper1; |
| 5185 | auth_helper1.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5186 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5187 | |
| 5188 | // A second request come in, but no new ConnectJob is needed, since the limit |
| 5189 | // has been reached. |
| 5190 | TestAuthHelper auth_helper2; |
| 5191 | auth_helper2.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5192 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5193 | |
| 5194 | // Run until the auth callback for the first request is invoked. |
| 5195 | auth_helper1.WaitForAuth(); |
| 5196 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5197 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5198 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5199 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5200 | |
| 5201 | // Make connect jobs succeed, then cancel the first request, which should |
| 5202 | // destroy the bound ConnectJob, and cause a new ConnectJob to start. |
| 5203 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 5204 | auth_helper1.handle()->Reset(); |
| 5205 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5206 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5207 | |
| 5208 | // The second ConnectJob should succeed. |
| 5209 | EXPECT_THAT(auth_helper2.WaitForResult(), IsOk()); |
| 5210 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5211 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5212 | } |
| 5213 | |
| 5214 | // Makes sure that when a bound request is destroyed, a new ConnectJob is |
| 5215 | // created for another group, if needed. |
| 5216 | TEST_F(ClientSocketPoolBaseTest, |
| 5217 | ProxyAuthCreateNewConnectJobOnDestroyBoundRequestDifferentGroups) { |
| 5218 | CreatePool(1 /* max_sockets */, 1 /* max_sockets_per_group */); |
| 5219 | connect_job_factory_->set_job_type( |
| 5220 | TestConnectJob::kMockAuthChallengeOnceFailingJob); |
| 5221 | |
| 5222 | // First request creates a ConnectJob. |
| 5223 | TestAuthHelper auth_helper1; |
| 5224 | auth_helper1.InitHandle(params_, pool_.get(), DEFAULT_PRIORITY); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5225 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5226 | |
| 5227 | // A second request come in, but no new ConnectJob is needed, since the limit |
| 5228 | // has been reached. |
| 5229 | TestAuthHelper auth_helper2; |
| 5230 | auth_helper2.InitHandle(params_, pool_.get(), DEFAULT_PRIORITY, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5231 | ClientSocketPool::RespectLimits::ENABLED, |
| 5232 | TestGroupId("b")); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5233 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5234 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5235 | |
| 5236 | // Run until the auth callback for the first request is invoked. |
| 5237 | auth_helper1.WaitForAuth(); |
| 5238 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5239 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5240 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5241 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5242 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 5243 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5244 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5245 | |
| 5246 | // Make connect jobs succeed, then cancel the first request, which should |
| 5247 | // destroy the bound ConnectJob, and cause a new ConnectJob to start for the |
| 5248 | // other group. |
| 5249 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 5250 | auth_helper1.handle()->Reset(); |
| 5251 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5252 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 5253 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5254 | |
| 5255 | // The second ConnectJob should succeed. |
| 5256 | EXPECT_THAT(auth_helper2.WaitForResult(), IsOk()); |
| 5257 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5258 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 5259 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5260 | } |
| 5261 | |
| 5262 | // Test that once an auth challenge is bound, that's the request that gets all |
| 5263 | // subsequent calls and the socket itself. |
| 5264 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthStaysBound) { |
| 5265 | CreatePool(1, 1); |
| 5266 | connect_job_factory_->set_job_type( |
| 5267 | TestConnectJob::kMockAuthChallengeTwiceJob); |
| 5268 | |
| 5269 | // First request creates a ConnectJob. |
| 5270 | TestAuthHelper auth_helper1; |
| 5271 | auth_helper1.InitHandle(params_, pool_.get(), LOWEST); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5272 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5273 | |
| 5274 | // A second, higher priority request is made. |
| 5275 | TestAuthHelper auth_helper2; |
| 5276 | auth_helper2.InitHandle(params_, pool_.get(), LOW); |
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 | // Run until the auth callback for the second request is invoked. |
| 5280 | auth_helper2.WaitForAuth(); |
| 5281 | EXPECT_EQ(0, auth_helper1.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5282 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5283 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5284 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5285 | |
| 5286 | // Start a higher priority job. It shouldn't be able to steal |auth_helper2|'s |
| 5287 | // ConnectJob. |
| 5288 | TestAuthHelper auth_helper3; |
| 5289 | auth_helper3.InitHandle(params_, pool_.get(), HIGHEST); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5290 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5291 | |
| 5292 | // Start a higher job that ignores limits, creating a hanging socket. It |
| 5293 | // shouldn't be able to steal |auth_helper2|'s ConnectJob. |
| 5294 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 5295 | TestAuthHelper auth_helper4; |
| 5296 | auth_helper4.InitHandle(params_, pool_.get(), HIGHEST, |
| 5297 | ClientSocketPool::RespectLimits::DISABLED); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5298 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5299 | |
| 5300 | // Restart with auth, and |auth_helper2|'s auth method should be invoked |
| 5301 | // again. |
| 5302 | auth_helper2.RestartWithAuth(); |
| 5303 | auth_helper2.WaitForAuth(); |
| 5304 | EXPECT_EQ(0, auth_helper1.auth_count()); |
| 5305 | EXPECT_FALSE(auth_helper1.have_result()); |
| 5306 | EXPECT_EQ(2, auth_helper2.auth_count()); |
| 5307 | EXPECT_FALSE(auth_helper2.have_result()); |
| 5308 | EXPECT_EQ(0, auth_helper3.auth_count()); |
| 5309 | EXPECT_FALSE(auth_helper3.have_result()); |
| 5310 | EXPECT_EQ(0, auth_helper4.auth_count()); |
| 5311 | EXPECT_FALSE(auth_helper4.have_result()); |
| 5312 | |
| 5313 | // Advance auth again, and |auth_helper2| should get the socket. |
| 5314 | auth_helper2.RestartWithAuth(); |
| 5315 | EXPECT_THAT(auth_helper2.WaitForResult(), IsOk()); |
| 5316 | // The hung ConnectJob for the RespectLimits::DISABLED request is still in the |
| 5317 | // socket pool. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5318 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5319 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5320 | EXPECT_EQ(0, auth_helper1.auth_count()); |
| 5321 | EXPECT_FALSE(auth_helper1.have_result()); |
| 5322 | EXPECT_EQ(0, auth_helper3.auth_count()); |
| 5323 | EXPECT_FALSE(auth_helper3.have_result()); |
| 5324 | EXPECT_EQ(0, auth_helper4.auth_count()); |
| 5325 | EXPECT_FALSE(auth_helper4.have_result()); |
| 5326 | |
| 5327 | // If the socket is returned to the socket pool, the RespectLimits::DISABLED |
| 5328 | // socket request should be able to claim it. |
| 5329 | auth_helper2.handle()->Reset(); |
| 5330 | EXPECT_THAT(auth_helper4.WaitForResult(), IsOk()); |
| 5331 | EXPECT_EQ(0, auth_helper1.auth_count()); |
| 5332 | EXPECT_FALSE(auth_helper1.have_result()); |
| 5333 | EXPECT_EQ(0, auth_helper3.auth_count()); |
| 5334 | EXPECT_FALSE(auth_helper3.have_result()); |
| 5335 | EXPECT_EQ(0, auth_helper4.auth_count()); |
| 5336 | } |
| 5337 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 5338 | } // namespace |
| 5339 | |
| 5340 | } // namespace net |