[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" |
Matt Menke | bdf77780 | 2019-04-22 19:38:59 | [diff] [blame] | 30 | #include "net/base/privacy_mode.h" |
Matt Menke | aafff54 | 2019-04-22 22:09:36 | [diff] [blame^] | 31 | #include "net/base/proxy_server.h" |
[email protected] | ac790b4 | 2009-12-02 04:31:31 | [diff] [blame] | 32 | #include "net/base/request_priority.h" |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 33 | #include "net/base/test_completion_callback.h" |
[email protected] | 277d594 | 2010-08-11 21:02:35 | [diff] [blame] | 34 | #include "net/http/http_response_headers.h" |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 35 | #include "net/http/http_response_info.h" |
eroman | 87c53d6 | 2015-04-02 06:51:07 | [diff] [blame] | 36 | #include "net/log/net_log.h" |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 37 | #include "net/log/net_log_event_type.h" |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 38 | #include "net/log/net_log_source.h" |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 39 | #include "net/log/net_log_source_type.h" |
mmenke | 16a7cbdd | 2015-04-24 23:00:56 | [diff] [blame] | 40 | #include "net/log/test_net_log.h" |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 41 | #include "net/log/test_net_log_entry.h" |
| 42 | #include "net/log/test_net_log_util.h" |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 43 | #include "net/socket/client_socket_factory.h" |
| 44 | #include "net/socket/client_socket_handle.h" |
tfarina | 5dd13c2 | 2016-11-16 12:08:26 | [diff] [blame] | 45 | #include "net/socket/datagram_client_socket.h" |
tbansal | ca83c00 | 2016-04-28 20:56:28 | [diff] [blame] | 46 | #include "net/socket/socket_performance_watcher.h" |
Paul Jensen | 8d6f87ec | 2018-01-13 00:46:54 | [diff] [blame] | 47 | #include "net/socket/socket_tag.h" |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 48 | #include "net/socket/socket_test_util.h" |
[email protected] | 18ccfdb | 2013-08-15 00:13:44 | [diff] [blame] | 49 | #include "net/socket/ssl_client_socket.h" |
[email protected] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame] | 50 | #include "net/socket/stream_socket.h" |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 51 | #include "net/socket/transport_connect_job.h" |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 52 | #include "net/ssl/ssl_cert_request_info.h" |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 53 | #include "net/test/gtest_util.h" |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 54 | #include "net/test/test_with_scoped_task_environment.h" |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 55 | #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 56 | #include "testing/gmock/include/gmock/gmock.h" |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 57 | #include "testing/gtest/include/gtest/gtest.h" |
| 58 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 59 | using net::test::IsError; |
| 60 | using net::test::IsOk; |
| 61 | |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 62 | using ::testing::Invoke; |
| 63 | using ::testing::Return; |
| 64 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 65 | namespace net { |
| 66 | |
| 67 | namespace { |
| 68 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 69 | const int kDefaultMaxSockets = 4; |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 70 | const int kDefaultMaxSocketsPerGroup = 2; |
Tarun Bansal | a763509 | 2019-02-20 10:00:59 | [diff] [blame] | 71 | constexpr base::TimeDelta kUnusedIdleSocketTimeout = |
| 72 | base::TimeDelta::FromSeconds(10); |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 73 | |
Matt Menke | bdf77780 | 2019-04-22 19:38:59 | [diff] [blame] | 74 | ClientSocketPool::GroupId TestGroupId( |
| 75 | const std::string& host, |
| 76 | int port = 80, |
| 77 | ClientSocketPool::SocketType socket_type = |
| 78 | ClientSocketPool::SocketType::kHttp, |
| 79 | PrivacyMode privacy_mode = PrivacyMode::PRIVACY_MODE_DISABLED) { |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 80 | return ClientSocketPool::GroupId(HostPortPair(host, port), socket_type, |
| 81 | privacy_mode); |
| 82 | } |
| 83 | |
Matt Menke | 84d11e56 | 2019-03-27 00:11:19 | [diff] [blame] | 84 | // Returns a ClientSocketPool::SocketParams that will never be used to |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 85 | // create a real TreansportConnectJob. |
Matt Menke | 84d11e56 | 2019-03-27 00:11:19 | [diff] [blame] | 86 | scoped_refptr<ClientSocketPool::SocketParams> CreateDummyParams() { |
| 87 | return ClientSocketPool::SocketParams::CreateFromTransportSocketParams( |
| 88 | base::MakeRefCounted<TransportSocketParams>(HostPortPair("ignored", 80), |
| 89 | OnHostResolutionCallback())); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 90 | } |
| 91 | |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 92 | // Make sure |handle| sets load times correctly when it has been assigned a |
| 93 | // reused socket. |
| 94 | void TestLoadTimingInfoConnectedReused(const ClientSocketHandle& handle) { |
| 95 | LoadTimingInfo load_timing_info; |
| 96 | // Only pass true in as |is_reused|, as in general, HttpStream types should |
| 97 | // have stricter concepts of reuse than socket pools. |
| 98 | EXPECT_TRUE(handle.GetLoadTimingInfo(true, &load_timing_info)); |
| 99 | |
| 100 | EXPECT_EQ(true, load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 101 | EXPECT_NE(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 102 | |
[email protected] | b258e079 | 2013-01-12 07:11:59 | [diff] [blame] | 103 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 104 | ExpectLoadTimingHasOnlyConnectionTimes(load_timing_info); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | // Make sure |handle| sets load times correctly when it has been assigned a |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 108 | // fresh socket. Also runs TestLoadTimingInfoConnectedReused, since the owner |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 109 | // of a connection where |is_reused| is false may consider the connection |
| 110 | // reused. |
| 111 | void TestLoadTimingInfoConnectedNotReused(const ClientSocketHandle& handle) { |
| 112 | EXPECT_FALSE(handle.is_reused()); |
| 113 | |
| 114 | LoadTimingInfo load_timing_info; |
| 115 | EXPECT_TRUE(handle.GetLoadTimingInfo(false, &load_timing_info)); |
| 116 | |
| 117 | EXPECT_FALSE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 118 | EXPECT_NE(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 119 | |
[email protected] | b258e079 | 2013-01-12 07:11:59 | [diff] [blame] | 120 | ExpectConnectTimingHasTimes(load_timing_info.connect_timing, |
| 121 | CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY); |
| 122 | ExpectLoadTimingHasOnlyConnectionTimes(load_timing_info); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 123 | |
| 124 | TestLoadTimingInfoConnectedReused(handle); |
| 125 | } |
| 126 | |
| 127 | // Make sure |handle| sets load times correctly, in the case that it does not |
| 128 | // currently have a socket. |
| 129 | void TestLoadTimingInfoNotConnected(const ClientSocketHandle& handle) { |
| 130 | // Should only be set to true once a socket is assigned, if at all. |
| 131 | EXPECT_FALSE(handle.is_reused()); |
| 132 | |
| 133 | LoadTimingInfo load_timing_info; |
| 134 | EXPECT_FALSE(handle.GetLoadTimingInfo(false, &load_timing_info)); |
| 135 | |
| 136 | EXPECT_FALSE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 137 | EXPECT_EQ(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 138 | |
[email protected] | b258e079 | 2013-01-12 07:11:59 | [diff] [blame] | 139 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 140 | ExpectLoadTimingHasOnlyConnectionTimes(load_timing_info); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 141 | } |
| 142 | |
[email protected] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame] | 143 | class MockClientSocket : public StreamSocket { |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 144 | public: |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 145 | explicit MockClientSocket(net::NetLog* net_log) |
| 146 | : connected_(false), |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 147 | has_unread_data_(false), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 148 | net_log_(NetLogWithSource::Make(net_log, NetLogSourceType::SOCKET)), |
Charlie Harrison | 3e4c062 | 2018-05-13 15:44:30 | [diff] [blame] | 149 | was_used_to_convey_data_(false) {} |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 150 | |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 151 | // Sets whether the socket has unread data. If true, the next call to Read() |
| 152 | // will return 1 byte and IsConnectedAndIdle() will return false. |
| 153 | void set_has_unread_data(bool has_unread_data) { |
| 154 | has_unread_data_ = has_unread_data; |
| 155 | } |
| 156 | |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 157 | // Socket implementation. |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 158 | int Read(IOBuffer* /* buf */, |
| 159 | int len, |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 160 | CompletionOnceCallback /* callback */) override { |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 161 | if (has_unread_data_ && len > 0) { |
| 162 | has_unread_data_ = false; |
| 163 | was_used_to_convey_data_ = true; |
| 164 | return 1; |
| 165 | } |
[email protected] | e86df8dc | 2013-03-30 13:18:28 | [diff] [blame] | 166 | return ERR_UNEXPECTED; |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 167 | } |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 168 | |
[email protected] | a2b2cfc | 2017-12-06 09:06:08 | [diff] [blame] | 169 | int Write( |
| 170 | IOBuffer* /* buf */, |
| 171 | int len, |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 172 | CompletionOnceCallback /* callback */, |
[email protected] | a2b2cfc | 2017-12-06 09:06:08 | [diff] [blame] | 173 | const NetworkTrafficAnnotationTag& /*traffic_annotation*/) override { |
[email protected] | 0f873e8 | 2010-09-02 16:09:01 | [diff] [blame] | 174 | was_used_to_convey_data_ = true; |
| 175 | return len; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 176 | } |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 177 | int SetReceiveBufferSize(int32_t size) override { return OK; } |
| 178 | int SetSendBufferSize(int32_t size) override { return OK; } |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 179 | |
[email protected] | dbf036f | 2011-12-06 23:33:24 | [diff] [blame] | 180 | // StreamSocket implementation. |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 181 | int Connect(CompletionOnceCallback callback) override { |
[email protected] | dbf036f | 2011-12-06 23:33:24 | [diff] [blame] | 182 | connected_ = true; |
| 183 | return OK; |
| 184 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 185 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 186 | void Disconnect() override { connected_ = false; } |
| 187 | bool IsConnected() const override { return connected_; } |
| 188 | bool IsConnectedAndIdle() const override { |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 189 | return connected_ && !has_unread_data_; |
| 190 | } |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 191 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 192 | int GetPeerAddress(IPEndPoint* /* address */) const override { |
[email protected] | 9f864b3 | 2010-01-20 15:01:16 | [diff] [blame] | 193 | return ERR_UNEXPECTED; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 194 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 195 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 196 | int GetLocalAddress(IPEndPoint* /* address */) const override { |
[email protected] | e7f74da | 2011-04-19 23:49:35 | [diff] [blame] | 197 | return ERR_UNEXPECTED; |
| 198 | } |
| 199 | |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 200 | const NetLogWithSource& NetLog() const override { return net_log_; } |
[email protected] | a2006ece | 2010-04-23 16:44:02 | [diff] [blame] | 201 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 202 | bool WasEverUsed() const override { return was_used_to_convey_data_; } |
tfarina | 2846404c | 2016-12-25 14:31:37 | [diff] [blame] | 203 | bool WasAlpnNegotiated() const override { return false; } |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 204 | NextProto GetNegotiatedProtocol() const override { return kProtoUnknown; } |
| 205 | bool GetSSLInfo(SSLInfo* ssl_info) override { return false; } |
ttuttle | 23fdb7b | 2015-05-15 01:28:03 | [diff] [blame] | 206 | void GetConnectionAttempts(ConnectionAttempts* out) const override { |
| 207 | out->clear(); |
| 208 | } |
| 209 | void ClearConnectionAttempts() override {} |
| 210 | void AddConnectionAttempts(const ConnectionAttempts& attempts) override {} |
tbansal | f82cc8e | 2015-10-14 20:05:49 | [diff] [blame] | 211 | int64_t GetTotalReceivedBytes() const override { |
| 212 | NOTIMPLEMENTED(); |
| 213 | return 0; |
| 214 | } |
Paul Jensen | 0f49dec | 2017-12-12 23:39:58 | [diff] [blame] | 215 | void ApplySocketTag(const SocketTag& tag) override {} |
[email protected] | 9b5614a | 2010-08-25 20:29:45 | [diff] [blame] | 216 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 217 | private: |
| 218 | bool connected_; |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 219 | bool has_unread_data_; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 220 | NetLogWithSource net_log_; |
[email protected] | 0f873e8 | 2010-09-02 16:09:01 | [diff] [blame] | 221 | bool was_used_to_convey_data_; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 222 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 223 | DISALLOW_COPY_AND_ASSIGN(MockClientSocket); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 224 | }; |
| 225 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 226 | class TestConnectJob; |
| 227 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 228 | class MockClientSocketFactory : public ClientSocketFactory { |
| 229 | public: |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 230 | MockClientSocketFactory() : allocation_count_(0) {} |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 231 | |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 232 | std::unique_ptr<DatagramClientSocket> CreateDatagramClientSocket( |
[email protected] | 5370c01 | 2011-06-29 03:47:04 | [diff] [blame] | 233 | DatagramSocket::BindType bind_type, |
[email protected] | 98b0e58 | 2011-06-22 14:31:41 | [diff] [blame] | 234 | NetLog* net_log, |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 235 | const NetLogSource& source) override { |
[email protected] | 98b0e58 | 2011-06-22 14:31:41 | [diff] [blame] | 236 | NOTREACHED(); |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 237 | return std::unique_ptr<DatagramClientSocket>(); |
[email protected] | 98b0e58 | 2011-06-22 14:31:41 | [diff] [blame] | 238 | } |
| 239 | |
Helen Li | d5bb922 | 2018-04-12 15:33:09 | [diff] [blame] | 240 | std::unique_ptr<TransportClientSocket> CreateTransportClientSocket( |
[email protected] | 0a0b768 | 2010-08-25 17:08:07 | [diff] [blame] | 241 | const AddressList& addresses, |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 242 | std::unique_ptr< |
| 243 | SocketPerformanceWatcher> /* socket_performance_watcher */, |
[email protected] | 0a0b768 | 2010-08-25 17:08:07 | [diff] [blame] | 244 | NetLog* /* net_log */, |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 245 | const NetLogSource& /*source*/) override { |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 246 | allocation_count_++; |
Helen Li | d5bb922 | 2018-04-12 15:33:09 | [diff] [blame] | 247 | return nullptr; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 248 | } |
| 249 | |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 250 | std::unique_ptr<SSLClientSocket> CreateSSLClientSocket( |
Matt Menke | 841fc41 | 2019-03-05 23:20:12 | [diff] [blame] | 251 | std::unique_ptr<StreamSocket> stream_socket, |
[email protected] | 4f4de7e6 | 2010-11-12 19:55:27 | [diff] [blame] | 252 | const HostPortPair& host_and_port, |
[email protected] | 7ab5bbd1 | 2010-10-19 13:33:21 | [diff] [blame] | 253 | const SSLConfig& ssl_config, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 254 | const SSLClientSocketContext& context) override { |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 255 | NOTIMPLEMENTED(); |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 256 | return std::unique_ptr<SSLClientSocket>(); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 257 | } |
Matt Menke | fd95692 | 2019-02-04 23:44:03 | [diff] [blame] | 258 | |
Matt Menke | 52cd95a | 2019-02-08 06:16:27 | [diff] [blame] | 259 | std::unique_ptr<ProxyClientSocket> CreateProxyClientSocket( |
| 260 | std::unique_ptr<StreamSocket> stream_socket, |
| 261 | const std::string& user_agent, |
| 262 | const HostPortPair& endpoint, |
| 263 | const ProxyServer& proxy_server, |
| 264 | HttpAuthController* http_auth_controller, |
| 265 | bool tunnel, |
| 266 | bool using_spdy, |
| 267 | NextProto negotiated_protocol, |
| 268 | ProxyDelegate* proxy_delegate, |
| 269 | bool is_https_proxy, |
| 270 | const NetworkTrafficAnnotationTag& traffic_annotation) override { |
| 271 | NOTIMPLEMENTED(); |
| 272 | return nullptr; |
| 273 | } |
| 274 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 275 | void WaitForSignal(TestConnectJob* job) { waiting_jobs_.push_back(job); } |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 276 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 277 | void SignalJobs(); |
| 278 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 279 | void SignalJob(size_t job); |
| 280 | |
| 281 | void SetJobLoadState(size_t job, LoadState load_state); |
| 282 | |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 283 | // Sets the HasConnectionEstablished value of the specified job to true, |
| 284 | // without invoking the callback. |
| 285 | void SetJobHasEstablishedConnection(size_t job); |
| 286 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 287 | int allocation_count() const { return allocation_count_; } |
| 288 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 289 | private: |
| 290 | int allocation_count_; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 291 | std::vector<TestConnectJob*> waiting_jobs_; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 292 | }; |
| 293 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 294 | class TestConnectJob : public ConnectJob { |
| 295 | public: |
| 296 | enum JobType { |
| 297 | kMockJob, |
| 298 | kMockFailingJob, |
| 299 | kMockPendingJob, |
| 300 | kMockPendingFailingJob, |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 301 | kMockWaitingJob, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 302 | |
| 303 | // Certificate errors return a socket in addition to an error code. |
| 304 | kMockCertErrorJob, |
| 305 | kMockPendingCertErrorJob, |
| 306 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 307 | kMockAdditionalErrorStateJob, |
| 308 | kMockPendingAdditionalErrorStateJob, |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 309 | kMockUnreadDataJob, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 310 | |
| 311 | kMockAuthChallengeOnceJob, |
| 312 | kMockAuthChallengeTwiceJob, |
| 313 | kMockAuthChallengeOnceFailingJob, |
| 314 | kMockAuthChallengeTwiceFailingJob, |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 315 | }; |
| 316 | |
[email protected] | 994d493 | 2010-07-12 17:55:13 | [diff] [blame] | 317 | // The kMockPendingJob uses a slight delay before allowing the connect |
| 318 | // to complete. |
| 319 | static const int kPendingConnectDelay = 2; |
| 320 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 321 | TestConnectJob(JobType job_type, |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 322 | RequestPriority request_priority, |
| 323 | SocketTag socket_tag, |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 324 | base::TimeDelta timeout_duration, |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 325 | const CommonConnectJobParams* common_connect_job_params, |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 326 | ConnectJob::Delegate* delegate, |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 327 | MockClientSocketFactory* client_socket_factory) |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 328 | : ConnectJob(request_priority, |
| 329 | socket_tag, |
Matt Menke | 1a6c92d | 2019-02-23 00:25:38 | [diff] [blame] | 330 | timeout_duration, |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 331 | common_connect_job_params, |
Matt Menke | 1a6c92d | 2019-02-23 00:25:38 | [diff] [blame] | 332 | delegate, |
| 333 | nullptr /* net_log */, |
| 334 | NetLogSourceType::TRANSPORT_CONNECT_JOB, |
| 335 | NetLogEventType::TRANSPORT_CONNECT_JOB_CONNECT), |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 336 | job_type_(job_type), |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 337 | client_socket_factory_(client_socket_factory), |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 338 | load_state_(LOAD_STATE_IDLE), |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 339 | has_established_connection_(false), |
[email protected] | d5492c5 | 2013-11-10 20:44:39 | [diff] [blame] | 340 | store_additional_error_state_(false), |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 341 | weak_factory_(this) {} |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 342 | |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 343 | void Signal() { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 344 | DoConnect(waiting_success_, true /* async */, false /* recoverable */); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 345 | } |
| 346 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 347 | void set_load_state(LoadState load_state) { load_state_ = load_state; } |
| 348 | |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 349 | void set_has_established_connection() { |
| 350 | DCHECK(!has_established_connection_); |
| 351 | has_established_connection_ = true; |
| 352 | } |
| 353 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 354 | // From ConnectJob: |
| 355 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 356 | LoadState GetLoadState() const override { return load_state_; } |
[email protected] | 4645135 | 2009-09-01 14:54:21 | [diff] [blame] | 357 | |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 358 | bool HasEstablishedConnection() const override { |
| 359 | return has_established_connection_; |
| 360 | } |
| 361 | |
Matt Menke | 6f84d1f1 | 2019-04-11 19:26:47 | [diff] [blame] | 362 | bool IsSSLError() const override { return store_additional_error_state_; } |
| 363 | |
| 364 | scoped_refptr<SSLCertRequestInfo> GetCertRequestInfo() override { |
| 365 | if (store_additional_error_state_) |
| 366 | return base::MakeRefCounted<SSLCertRequestInfo>(); |
| 367 | return nullptr; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 368 | } |
| 369 | |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 370 | private: |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 371 | // From ConnectJob: |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 372 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 373 | int ConnectInternal() override { |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 374 | AddressList ignored; |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 375 | client_socket_factory_->CreateTransportClientSocket( |
| 376 | ignored, nullptr, nullptr, NetLogSource()); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 377 | switch (job_type_) { |
| 378 | case kMockJob: |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 379 | return DoConnect(true /* successful */, 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 kMockFailingJob: |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 382 | return DoConnect(false /* error */, false /* sync */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 383 | false /* cert_error */); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 384 | case kMockPendingJob: |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 385 | set_load_state(LOAD_STATE_CONNECTING); |
[email protected] | 6b17538 | 2009-10-13 06:47:47 | [diff] [blame] | 386 | |
| 387 | // Depending on execution timings, posting a delayed task can result |
| 388 | // in the task getting executed the at the earliest possible |
| 389 | // opportunity or only after returning once from the message loop and |
| 390 | // then a second call into the message loop. In order to make behavior |
| 391 | // more deterministic, we change the default delay to 2ms. This should |
| 392 | // always require us to wait for the second call into the message loop. |
| 393 | // |
| 394 | // N.B. The correct fix for this and similar timing problems is to |
| 395 | // abstract time for the purpose of unittests. Unfortunately, we have |
| 396 | // a lot of third-party components that directly call the various |
| 397 | // time functions, so this change would be rather invasive. |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 398 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 399 | FROM_HERE, |
kylechar | f4fe517 | 2019-02-15 18:53:49 | [diff] [blame] | 400 | base::BindOnce(base::IgnoreResult(&TestConnectJob::DoConnect), |
| 401 | weak_factory_.GetWeakPtr(), true /* successful */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 402 | true /* async */, false /* cert_error */), |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 403 | base::TimeDelta::FromMilliseconds(kPendingConnectDelay)); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 404 | return ERR_IO_PENDING; |
| 405 | case kMockPendingFailingJob: |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 406 | set_load_state(LOAD_STATE_CONNECTING); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 407 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 408 | FROM_HERE, |
kylechar | f4fe517 | 2019-02-15 18:53:49 | [diff] [blame] | 409 | base::BindOnce(base::IgnoreResult(&TestConnectJob::DoConnect), |
| 410 | weak_factory_.GetWeakPtr(), false /* error */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 411 | true /* async */, false /* cert_error */), |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 412 | base::TimeDelta::FromMilliseconds(2)); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 413 | return ERR_IO_PENDING; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 414 | case kMockWaitingJob: |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 415 | set_load_state(LOAD_STATE_CONNECTING); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 416 | client_socket_factory_->WaitForSignal(this); |
| 417 | waiting_success_ = true; |
| 418 | return ERR_IO_PENDING; |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 419 | case kMockCertErrorJob: |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 420 | return DoConnect(false /* error */, false /* sync */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 421 | true /* cert_error */); |
| 422 | case kMockPendingCertErrorJob: |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 423 | set_load_state(LOAD_STATE_CONNECTING); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 424 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 425 | FROM_HERE, |
kylechar | f4fe517 | 2019-02-15 18:53:49 | [diff] [blame] | 426 | base::BindOnce(base::IgnoreResult(&TestConnectJob::DoConnect), |
| 427 | weak_factory_.GetWeakPtr(), false /* error */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 428 | true /* async */, true /* cert_error */), |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 429 | base::TimeDelta::FromMilliseconds(2)); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 430 | return ERR_IO_PENDING; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 431 | case kMockAdditionalErrorStateJob: |
| 432 | store_additional_error_state_ = true; |
| 433 | return DoConnect(false /* error */, false /* sync */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 434 | false /* cert_error */); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 435 | case kMockPendingAdditionalErrorStateJob: |
| 436 | set_load_state(LOAD_STATE_CONNECTING); |
| 437 | store_additional_error_state_ = true; |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 438 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 439 | FROM_HERE, |
kylechar | f4fe517 | 2019-02-15 18:53:49 | [diff] [blame] | 440 | base::BindOnce(base::IgnoreResult(&TestConnectJob::DoConnect), |
| 441 | weak_factory_.GetWeakPtr(), false /* error */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 442 | true /* async */, false /* cert_error */), |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 443 | base::TimeDelta::FromMilliseconds(2)); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 444 | return ERR_IO_PENDING; |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 445 | case kMockUnreadDataJob: { |
| 446 | int ret = DoConnect(true /* successful */, false /* sync */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 447 | false /* cert_error */); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 448 | static_cast<MockClientSocket*>(socket())->set_has_unread_data(true); |
| 449 | return ret; |
| 450 | } |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 451 | case kMockAuthChallengeOnceJob: |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 452 | set_load_state(LOAD_STATE_CONNECTING); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 453 | DoAdvanceAuthChallenge(1, true /* succeed_after_last_challenge */); |
| 454 | return ERR_IO_PENDING; |
| 455 | case kMockAuthChallengeTwiceJob: |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 456 | set_load_state(LOAD_STATE_CONNECTING); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 457 | DoAdvanceAuthChallenge(2, true /* succeed_after_last_challenge */); |
| 458 | return ERR_IO_PENDING; |
| 459 | case kMockAuthChallengeOnceFailingJob: |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 460 | set_load_state(LOAD_STATE_CONNECTING); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 461 | DoAdvanceAuthChallenge(1, false /* succeed_after_last_challenge */); |
| 462 | return ERR_IO_PENDING; |
| 463 | case kMockAuthChallengeTwiceFailingJob: |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 464 | set_load_state(LOAD_STATE_CONNECTING); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 465 | DoAdvanceAuthChallenge(2, false /* succeed_after_last_challenge */); |
| 466 | return ERR_IO_PENDING; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 467 | default: |
| 468 | NOTREACHED(); |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 469 | SetSocket(std::unique_ptr<StreamSocket>()); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 470 | return ERR_FAILED; |
| 471 | } |
| 472 | } |
| 473 | |
Lily Chen | 02ef29a | 2018-11-30 16:31:43 | [diff] [blame] | 474 | void ChangePriorityInternal(RequestPriority priority) override {} |
| 475 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 476 | int DoConnect(bool succeed, bool was_async, bool cert_error) { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 477 | int result = OK; |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 478 | has_established_connection_ = true; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 479 | if (succeed) { |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 480 | SetSocket(std::make_unique<MockClientSocket>(net_log().net_log())); |
Bence Béky | bdbb0e7 | 2018-08-07 21:42:59 | [diff] [blame] | 481 | socket()->Connect(CompletionOnceCallback()); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 482 | } else if (cert_error) { |
| 483 | SetSocket(std::make_unique<MockClientSocket>(net_log().net_log())); |
| 484 | result = ERR_CERT_COMMON_NAME_INVALID; |
[email protected] | 6e713f0 | 2009-08-06 02:56:40 | [diff] [blame] | 485 | } else { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 486 | result = ERR_CONNECTION_FAILED; |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 487 | SetSocket(std::unique_ptr<StreamSocket>()); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 488 | } |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 489 | |
| 490 | if (was_async) |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 491 | NotifyDelegateOfCompletion(result); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 492 | return result; |
| 493 | } |
| 494 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 495 | void DoAdvanceAuthChallenge(int remaining_challenges, |
| 496 | bool succeed_after_last_challenge) { |
| 497 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 498 | FROM_HERE, |
| 499 | base::BindOnce(&TestConnectJob::InvokeNextProxyAuthCallback, |
| 500 | weak_factory_.GetWeakPtr(), remaining_challenges, |
| 501 | succeed_after_last_challenge)); |
| 502 | } |
| 503 | |
| 504 | void InvokeNextProxyAuthCallback(int remaining_challenges, |
| 505 | bool succeed_after_last_challenge) { |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 506 | set_load_state(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 507 | if (remaining_challenges == 0) { |
| 508 | DoConnect(succeed_after_last_challenge, true /* was_async */, |
| 509 | false /* cert_error */); |
| 510 | return; |
| 511 | } |
| 512 | |
| 513 | // Integration tests make sure HttpResponseInfo and HttpAuthController work. |
| 514 | // The auth tests here are just focused on ConnectJob bookkeeping. |
| 515 | HttpResponseInfo info; |
| 516 | NotifyDelegateOfProxyAuth( |
| 517 | info, nullptr /* http_auth_controller */, |
| 518 | base::BindOnce(&TestConnectJob::DoAdvanceAuthChallenge, |
| 519 | weak_factory_.GetWeakPtr(), remaining_challenges - 1, |
| 520 | succeed_after_last_challenge)); |
| 521 | } |
| 522 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 523 | bool waiting_success_; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 524 | const JobType job_type_; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 525 | MockClientSocketFactory* const client_socket_factory_; |
[email protected] | 4645135 | 2009-09-01 14:54:21 | [diff] [blame] | 526 | LoadState load_state_; |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 527 | bool has_established_connection_; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 528 | bool store_additional_error_state_; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 529 | |
[email protected] | d5492c5 | 2013-11-10 20:44:39 | [diff] [blame] | 530 | base::WeakPtrFactory<TestConnectJob> weak_factory_; |
| 531 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 532 | DISALLOW_COPY_AND_ASSIGN(TestConnectJob); |
| 533 | }; |
| 534 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 535 | class TestConnectJobFactory |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 536 | : public TransportClientSocketPool::ConnectJobFactory { |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 537 | public: |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 538 | TestConnectJobFactory(MockClientSocketFactory* client_socket_factory, |
| 539 | NetLog* net_log) |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 540 | : common_connect_job_params_( |
| 541 | nullptr /* client_socket_factory */, |
| 542 | nullptr /* host_resolver */, |
Matt Menke | b88837e | 2019-03-20 11:50:40 | [diff] [blame] | 543 | nullptr /* http_auth_cache */, |
| 544 | nullptr /* http_auth_handler_factory */, |
| 545 | nullptr /* spdy_session_pool */, |
Matt Menke | b5fb42b | 2019-03-22 17:26:13 | [diff] [blame] | 546 | nullptr /* quic_supported_versions */, |
Matt Menke | b88837e | 2019-03-20 11:50:40 | [diff] [blame] | 547 | nullptr /* quic_stream_factory */, |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 548 | nullptr /* proxy_delegate */, |
Matt Menke | d732ea4 | 2019-03-08 12:05:00 | [diff] [blame] | 549 | nullptr /* http_user_agent_settings */, |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 550 | SSLClientSocketContext(), |
| 551 | SSLClientSocketContext(), |
| 552 | nullptr /* socket_performance_watcher_factory */, |
| 553 | nullptr /* network_quality_estimator */, |
| 554 | net_log, |
| 555 | nullptr /* websocket_endpoint_lock_manager */), |
| 556 | job_type_(TestConnectJob::kMockJob), |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 557 | job_types_(nullptr), |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 558 | client_socket_factory_(client_socket_factory) {} |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 559 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 560 | ~TestConnectJobFactory() override = default; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 561 | |
| 562 | void set_job_type(TestConnectJob::JobType job_type) { job_type_ = job_type; } |
| 563 | |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 564 | void set_job_types(std::list<TestConnectJob::JobType>* job_types) { |
| 565 | job_types_ = job_types; |
| 566 | CHECK(!job_types_->empty()); |
| 567 | } |
| 568 | |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 569 | void set_timeout_duration(base::TimeDelta timeout_duration) { |
| 570 | timeout_duration_ = timeout_duration; |
| 571 | } |
| 572 | |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 573 | // ConnectJobFactory implementation. |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 574 | |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 575 | std::unique_ptr<ConnectJob> NewConnectJob( |
Matt Menke | aafff54 | 2019-04-22 22:09:36 | [diff] [blame^] | 576 | ClientSocketPool::GroupId group_id, |
| 577 | scoped_refptr<ClientSocketPool::SocketParams> socket_params, |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 578 | RequestPriority request_priority, |
| 579 | SocketTag socket_tag, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 580 | ConnectJob::Delegate* delegate) const override { |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 581 | EXPECT_TRUE(!job_types_ || !job_types_->empty()); |
| 582 | TestConnectJob::JobType job_type = job_type_; |
| 583 | if (job_types_ && !job_types_->empty()) { |
| 584 | job_type = job_types_->front(); |
| 585 | job_types_->pop_front(); |
| 586 | } |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 587 | return std::make_unique<TestConnectJob>( |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 588 | job_type, request_priority, socket_tag, timeout_duration_, |
| 589 | &common_connect_job_params_, delegate, client_socket_factory_); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | private: |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 593 | const CommonConnectJobParams common_connect_job_params_; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 594 | TestConnectJob::JobType job_type_; |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 595 | std::list<TestConnectJob::JobType>* job_types_; |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 596 | base::TimeDelta timeout_duration_; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 597 | MockClientSocketFactory* const client_socket_factory_; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 598 | |
| 599 | DISALLOW_COPY_AND_ASSIGN(TestConnectJobFactory); |
| 600 | }; |
| 601 | |
[email protected] | a937a06d | 2009-08-19 21:19:24 | [diff] [blame] | 602 | } // namespace |
| 603 | |
[email protected] | a937a06d | 2009-08-19 21:19:24 | [diff] [blame] | 604 | namespace { |
| 605 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 606 | void MockClientSocketFactory::SignalJobs() { |
jdoerrie | 22a91d8b9 | 2018-10-05 08:43:26 | [diff] [blame] | 607 | for (auto it = waiting_jobs_.begin(); it != waiting_jobs_.end(); ++it) { |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 608 | (*it)->Signal(); |
| 609 | } |
| 610 | waiting_jobs_.clear(); |
| 611 | } |
| 612 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 613 | void MockClientSocketFactory::SignalJob(size_t job) { |
| 614 | ASSERT_LT(job, waiting_jobs_.size()); |
| 615 | waiting_jobs_[job]->Signal(); |
| 616 | waiting_jobs_.erase(waiting_jobs_.begin() + job); |
| 617 | } |
| 618 | |
| 619 | void MockClientSocketFactory::SetJobLoadState(size_t job, |
| 620 | LoadState load_state) { |
| 621 | ASSERT_LT(job, waiting_jobs_.size()); |
| 622 | waiting_jobs_[job]->set_load_state(load_state); |
| 623 | } |
| 624 | |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 625 | void MockClientSocketFactory::SetJobHasEstablishedConnection(size_t job) { |
| 626 | ASSERT_LT(job, waiting_jobs_.size()); |
| 627 | waiting_jobs_[job]->set_has_established_connection(); |
| 628 | } |
| 629 | |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 630 | class ClientSocketPoolBaseTest : public TestWithScopedTaskEnvironment { |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 631 | protected: |
Alex Clarke | 0def209 | 2018-12-10 12:01:45 | [diff] [blame] | 632 | ClientSocketPoolBaseTest() |
| 633 | : TestWithScopedTaskEnvironment( |
| 634 | base::test::ScopedTaskEnvironment::MainThreadType::MOCK_TIME), |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 635 | params_(CreateDummyParams()) { |
[email protected] | 636b825 | 2011-04-08 19:56:54 | [diff] [blame] | 636 | connect_backup_jobs_enabled_ = |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 637 | TransportClientSocketPool::connect_backup_jobs_enabled(); |
| 638 | TransportClientSocketPool::set_connect_backup_jobs_enabled(true); |
[email protected] | 636b825 | 2011-04-08 19:56:54 | [diff] [blame] | 639 | } |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 640 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 641 | ~ClientSocketPoolBaseTest() override { |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 642 | TransportClientSocketPool::set_connect_backup_jobs_enabled( |
[email protected] | 636b825 | 2011-04-08 19:56:54 | [diff] [blame] | 643 | connect_backup_jobs_enabled_); |
| 644 | } |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 645 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 646 | void CreatePool(int max_sockets, |
| 647 | int max_sockets_per_group, |
| 648 | bool enable_backup_connect_jobs = false) { |
Tarun Bansal | a763509 | 2019-02-20 10:00:59 | [diff] [blame] | 649 | CreatePoolWithIdleTimeouts(max_sockets, max_sockets_per_group, |
| 650 | kUnusedIdleSocketTimeout, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 651 | ClientSocketPool::used_idle_socket_timeout(), |
| 652 | enable_backup_connect_jobs); |
[email protected] | 9bf28db | 2009-08-29 01:35:16 | [diff] [blame] | 653 | } |
| 654 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 655 | void CreatePoolWithIdleTimeouts(int max_sockets, |
| 656 | int max_sockets_per_group, |
| 657 | base::TimeDelta unused_idle_socket_timeout, |
| 658 | base::TimeDelta used_idle_socket_timeout, |
| 659 | bool enable_backup_connect_jobs = false) { |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 660 | DCHECK(!pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 661 | std::unique_ptr<TestConnectJobFactory> connect_job_factory = |
| 662 | std::make_unique<TestConnectJobFactory>(&client_socket_factory_, |
| 663 | &net_log_); |
| 664 | connect_job_factory_ = connect_job_factory.get(); |
| 665 | pool_ = TransportClientSocketPool::CreateForTesting( |
| 666 | max_sockets, max_sockets_per_group, unused_idle_socket_timeout, |
| 667 | used_idle_socket_timeout, std::move(connect_job_factory), |
| 668 | nullptr /* ssl_config_service */, enable_backup_connect_jobs); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 669 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 670 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 671 | int StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 672 | const ClientSocketPool::GroupId& group_id, |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 673 | RequestPriority priority, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 674 | ClientSocketPool::RespectLimits respect_limits) { |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 675 | return test_base_.StartRequestUsingPool(pool_.get(), group_id, priority, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 676 | respect_limits, params_); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 677 | } |
| 678 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 679 | int StartRequest(const ClientSocketPool::GroupId& group_id, |
| 680 | RequestPriority priority) { |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 681 | return StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 682 | group_id, priority, ClientSocketPool::RespectLimits::ENABLED); |
[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 | int GetOrderOfRequest(size_t index) const { |
| 686 | return test_base_.GetOrderOfRequest(index); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 687 | } |
| 688 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 689 | bool ReleaseOneConnection(ClientSocketPoolTest::KeepAlive keep_alive) { |
| 690 | return test_base_.ReleaseOneConnection(keep_alive); |
| 691 | } |
| 692 | |
| 693 | void ReleaseAllConnections(ClientSocketPoolTest::KeepAlive keep_alive) { |
| 694 | test_base_.ReleaseAllConnections(keep_alive); |
| 695 | } |
| 696 | |
| 697 | TestSocketRequest* request(int i) { return test_base_.request(i); } |
| 698 | size_t requests_size() const { return test_base_.requests_size(); } |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 699 | std::vector<std::unique_ptr<TestSocketRequest>>* requests() { |
olli.raula | 9d66b7d | 2015-11-23 08:30:42 | [diff] [blame] | 700 | return test_base_.requests(); |
| 701 | } |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 702 | // Only counts the requests that get sockets asynchronously; |
| 703 | // synchronous completions are not registered by this count. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 704 | size_t completion_count() const { return test_base_.completion_count(); } |
| 705 | |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 706 | TestNetLog net_log_; |
[email protected] | 636b825 | 2011-04-08 19:56:54 | [diff] [blame] | 707 | bool connect_backup_jobs_enabled_; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 708 | MockClientSocketFactory client_socket_factory_; |
[email protected] | 17a0c6c | 2009-08-04 00:07:04 | [diff] [blame] | 709 | TestConnectJobFactory* connect_job_factory_; |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 710 | // These parameters are never actually used to create a TransportConnectJob. |
Matt Menke | 84d11e56 | 2019-03-27 00:11:19 | [diff] [blame] | 711 | scoped_refptr<ClientSocketPool::SocketParams> params_; |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 712 | std::unique_ptr<TransportClientSocketPool> pool_; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 713 | ClientSocketPoolTest test_base_; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 714 | }; |
| 715 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 716 | TEST_F(ClientSocketPoolBaseTest, BasicSynchronous) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 717 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 718 | |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 719 | TestCompletionCallback callback; |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 720 | ClientSocketHandle handle; |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 721 | BoundTestNetLog log; |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 722 | TestLoadTimingInfoNotConnected(handle); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 723 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 724 | EXPECT_EQ( |
| 725 | OK, handle.Init( |
| 726 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
| 727 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 728 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound())); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 729 | EXPECT_TRUE(handle.is_initialized()); |
| 730 | EXPECT_TRUE(handle.socket()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 731 | TestLoadTimingInfoConnectedNotReused(handle); |
| 732 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 733 | handle.Reset(); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 734 | TestLoadTimingInfoNotConnected(handle); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 735 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 736 | TestNetLogEntry::List entries; |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 737 | log.GetEntries(&entries); |
| 738 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 739 | EXPECT_EQ(5u, entries.size()); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 740 | EXPECT_TRUE(LogContainsEvent( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 741 | entries, 0, NetLogEventType::TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 742 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 743 | EXPECT_TRUE(LogContainsBeginEvent(entries, 1, NetLogEventType::SOCKET_POOL)); |
| 744 | EXPECT_TRUE(LogContainsEvent( |
| 745 | entries, 2, NetLogEventType::SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
| 746 | NetLogEventPhase::NONE)); |
| 747 | EXPECT_TRUE(LogContainsEvent(entries, 3, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 748 | NetLogEventType::SOCKET_POOL_BOUND_TO_SOCKET, |
| 749 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 750 | EXPECT_TRUE(LogContainsEndEvent(entries, 4, NetLogEventType::SOCKET_POOL)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 751 | } |
| 752 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 753 | TEST_F(ClientSocketPoolBaseTest, InitConnectionFailure) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 754 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 755 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 756 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 757 | BoundTestNetLog log; |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 758 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 759 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 760 | TestCompletionCallback callback; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 761 | // Set the additional error state members to ensure that they get cleared. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 762 | handle.set_is_ssl_error(true); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 763 | handle.set_ssl_cert_request_info(base::MakeRefCounted<SSLCertRequestInfo>()); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 764 | EXPECT_EQ( |
| 765 | ERR_CONNECTION_FAILED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 766 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 767 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 768 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 769 | log.bound())); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 770 | EXPECT_FALSE(handle.socket()); |
| 771 | EXPECT_FALSE(handle.is_ssl_error()); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 772 | EXPECT_FALSE(handle.ssl_cert_request_info()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 773 | TestLoadTimingInfoNotConnected(handle); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 774 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 775 | TestNetLogEntry::List entries; |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 776 | log.GetEntries(&entries); |
| 777 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 778 | EXPECT_EQ(4u, entries.size()); |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 779 | EXPECT_TRUE(LogContainsEvent( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 780 | entries, 0, NetLogEventType::TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 781 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 782 | EXPECT_TRUE(LogContainsBeginEvent(entries, 1, NetLogEventType::SOCKET_POOL)); |
| 783 | EXPECT_TRUE(LogContainsEvent( |
| 784 | entries, 2, NetLogEventType::SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
| 785 | NetLogEventPhase::NONE)); |
| 786 | EXPECT_TRUE(LogContainsEndEvent(entries, 3, NetLogEventType::SOCKET_POOL)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 787 | } |
| 788 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 789 | // Make sure different groups do not share sockets. |
| 790 | TEST_F(ClientSocketPoolBaseTest, GroupSeparation) { |
| 791 | CreatePool(1000 /* max_sockets */, 2 /* max_sockets_per_group */); |
| 792 | |
| 793 | const HostPortPair kHostPortPairs[] = { |
| 794 | {"a", 80}, |
| 795 | {"a", 443}, |
| 796 | {"b", 80}, |
| 797 | }; |
| 798 | |
| 799 | const ClientSocketPool::SocketType kSocketTypes[] = { |
| 800 | ClientSocketPool::SocketType::kHttp, |
| 801 | ClientSocketPool::SocketType::kSsl, |
| 802 | ClientSocketPool::SocketType::kSslVersionInterferenceProbe, |
| 803 | ClientSocketPool::SocketType::kFtp, |
| 804 | }; |
| 805 | |
Matt Menke | bdf77780 | 2019-04-22 19:38:59 | [diff] [blame] | 806 | const PrivacyMode kPrivacyModes[] = {PrivacyMode::PRIVACY_MODE_DISABLED, |
| 807 | PrivacyMode::PRIVACY_MODE_ENABLED}; |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 808 | |
| 809 | int total_idle_sockets = 0; |
| 810 | |
| 811 | // Walk through each GroupId, making sure that requesting a socket for one |
| 812 | // group does not return a previously connected socket for another group. |
| 813 | for (const auto& host_port_pair : kHostPortPairs) { |
| 814 | SCOPED_TRACE(host_port_pair.ToString()); |
| 815 | for (const auto& socket_type : kSocketTypes) { |
| 816 | SCOPED_TRACE(static_cast<int>(socket_type)); |
| 817 | for (const auto& privacy_mode : kPrivacyModes) { |
| 818 | SCOPED_TRACE(privacy_mode); |
| 819 | |
| 820 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 821 | |
| 822 | ClientSocketPool::GroupId group_id(host_port_pair, socket_type, |
| 823 | privacy_mode); |
| 824 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 825 | EXPECT_FALSE(pool_->HasGroupForTesting(group_id)); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 826 | |
| 827 | TestCompletionCallback callback; |
| 828 | ClientSocketHandle handle; |
| 829 | |
| 830 | // Since the group is empty, requesting a socket should not complete |
| 831 | // synchronously. |
| 832 | EXPECT_THAT( |
| 833 | handle.Init(group_id, params_, DEFAULT_PRIORITY, SocketTag(), |
| 834 | ClientSocketPool::RespectLimits::ENABLED, |
| 835 | callback.callback(), |
| 836 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 837 | NetLogWithSource()), |
| 838 | IsError(ERR_IO_PENDING)); |
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 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 843 | EXPECT_TRUE(handle.socket()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 844 | EXPECT_TRUE(pool_->HasGroupForTesting(group_id)); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 845 | EXPECT_EQ(total_idle_sockets, pool_->IdleSocketCount()); |
| 846 | |
| 847 | // Return socket to pool. |
| 848 | handle.Reset(); |
| 849 | EXPECT_EQ(total_idle_sockets + 1, pool_->IdleSocketCount()); |
| 850 | |
| 851 | // Requesting a socket again should return the same socket as before, so |
| 852 | // should complete synchronously. |
| 853 | EXPECT_THAT( |
| 854 | handle.Init(group_id, params_, DEFAULT_PRIORITY, SocketTag(), |
| 855 | ClientSocketPool::RespectLimits::ENABLED, |
| 856 | callback.callback(), |
| 857 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 858 | NetLogWithSource()), |
| 859 | IsOk()); |
| 860 | EXPECT_TRUE(handle.socket()); |
| 861 | EXPECT_EQ(total_idle_sockets, pool_->IdleSocketCount()); |
| 862 | |
| 863 | // Return socket to pool again. |
| 864 | handle.Reset(); |
| 865 | EXPECT_EQ(total_idle_sockets + 1, pool_->IdleSocketCount()); |
| 866 | |
| 867 | ++total_idle_sockets; |
| 868 | } |
| 869 | } |
| 870 | } |
| 871 | } |
| 872 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 873 | TEST_F(ClientSocketPoolBaseTest, TotalLimit) { |
| 874 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 875 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 876 | // TODO(eroman): Check that the NetLog contains this event. |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 877 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 878 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 879 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
| 880 | EXPECT_THAT(StartRequest(TestGroupId("c"), DEFAULT_PRIORITY), IsOk()); |
| 881 | EXPECT_THAT(StartRequest(TestGroupId("d"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 882 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 883 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 884 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 885 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 886 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 887 | EXPECT_THAT(StartRequest(TestGroupId("e"), DEFAULT_PRIORITY), |
| 888 | IsError(ERR_IO_PENDING)); |
| 889 | EXPECT_THAT(StartRequest(TestGroupId("f"), DEFAULT_PRIORITY), |
| 890 | IsError(ERR_IO_PENDING)); |
| 891 | EXPECT_THAT(StartRequest(TestGroupId("g"), DEFAULT_PRIORITY), |
| 892 | IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 893 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 894 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 895 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 896 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 897 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 898 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 899 | |
| 900 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 901 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 902 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 903 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 904 | EXPECT_EQ(5, GetOrderOfRequest(5)); |
| 905 | EXPECT_EQ(6, GetOrderOfRequest(6)); |
| 906 | EXPECT_EQ(7, GetOrderOfRequest(7)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 907 | |
| 908 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 909 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(8)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 910 | } |
| 911 | |
| 912 | TEST_F(ClientSocketPoolBaseTest, TotalLimitReachedNewGroup) { |
| 913 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 914 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 915 | // TODO(eroman): Check that the NetLog contains this event. |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 916 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 917 | // Reach all limits: max total sockets, and max sockets per group. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 918 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 919 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 920 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
| 921 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 922 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 923 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 924 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 925 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 926 | |
| 927 | // Now create a new group and verify that we don't starve it. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 928 | EXPECT_THAT(StartRequest(TestGroupId("c"), DEFAULT_PRIORITY), |
| 929 | IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 930 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 931 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 932 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 933 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 934 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 935 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 936 | |
| 937 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 938 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 939 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 940 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 941 | EXPECT_EQ(5, GetOrderOfRequest(5)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 942 | |
| 943 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 944 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(6)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 945 | } |
| 946 | |
| 947 | TEST_F(ClientSocketPoolBaseTest, TotalLimitRespectsPriority) { |
| 948 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 949 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 950 | EXPECT_THAT(StartRequest(TestGroupId("b"), LOWEST), IsOk()); |
| 951 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsOk()); |
| 952 | EXPECT_THAT(StartRequest(TestGroupId("b"), HIGHEST), IsOk()); |
| 953 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 954 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 955 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 956 | client_socket_factory_.allocation_count()); |
| 957 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 958 | EXPECT_THAT(StartRequest(TestGroupId("c"), LOWEST), IsError(ERR_IO_PENDING)); |
| 959 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 960 | EXPECT_THAT(StartRequest(TestGroupId("b"), HIGHEST), IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 961 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 962 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 963 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 964 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 965 | |
| 966 | // First 4 requests don't have to wait, and finish in order. |
| 967 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 968 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 969 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 970 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 971 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 972 | // Request ("b", HIGHEST) has the highest priority, then (TestGroupId("a"), |
| 973 | // MEDIUM), and then ("c", LOWEST). |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 974 | EXPECT_EQ(7, GetOrderOfRequest(5)); |
| 975 | EXPECT_EQ(6, GetOrderOfRequest(6)); |
| 976 | EXPECT_EQ(5, GetOrderOfRequest(7)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 977 | |
| 978 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 979 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(9)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 980 | } |
| 981 | |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 982 | // Test reprioritizing a request before completion doesn't interfere with |
| 983 | // its completion. |
| 984 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeOne) { |
| 985 | CreatePool(kDefaultMaxSockets, 1); |
| 986 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 987 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 988 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 989 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 990 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 991 | |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 992 | request(1)->handle()->SetPriority(HIGHEST); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 993 | |
| 994 | ReleaseOneConnection(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 995 | |
| 996 | EXPECT_TRUE(request(1)->handle()->socket()); |
| 997 | } |
| 998 | |
| 999 | // Reprioritize a request up past another one and make sure that changes the |
| 1000 | // completion order. |
| 1001 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeUpReorder) { |
| 1002 | CreatePool(kDefaultMaxSockets, 1); |
| 1003 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1004 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1005 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1006 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1007 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1008 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1009 | EXPECT_FALSE(request(2)->handle()->socket()); |
| 1010 | |
| 1011 | request(2)->handle()->SetPriority(HIGHEST); |
| 1012 | |
| 1013 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1014 | |
| 1015 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1016 | EXPECT_EQ(3, GetOrderOfRequest(2)); |
| 1017 | EXPECT_EQ(2, GetOrderOfRequest(3)); |
| 1018 | } |
| 1019 | |
| 1020 | // Reprioritize a request without changing relative priorities and check |
| 1021 | // that the order doesn't change. |
| 1022 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeUpNoReorder) { |
| 1023 | CreatePool(kDefaultMaxSockets, 1); |
| 1024 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1025 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1026 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1027 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1028 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1029 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1030 | EXPECT_FALSE(request(2)->handle()->socket()); |
| 1031 | |
| 1032 | request(2)->handle()->SetPriority(MEDIUM); |
| 1033 | |
| 1034 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1035 | |
| 1036 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1037 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1038 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 1039 | } |
| 1040 | |
| 1041 | // Reprioritize a request past down another one and make sure that changes the |
| 1042 | // completion order. |
| 1043 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeDownReorder) { |
| 1044 | CreatePool(kDefaultMaxSockets, 1); |
| 1045 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1046 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1047 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1048 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1049 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1050 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1051 | EXPECT_FALSE(request(2)->handle()->socket()); |
| 1052 | |
| 1053 | request(1)->handle()->SetPriority(LOW); |
| 1054 | |
| 1055 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1056 | |
| 1057 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1058 | EXPECT_EQ(3, GetOrderOfRequest(2)); |
| 1059 | EXPECT_EQ(2, GetOrderOfRequest(3)); |
| 1060 | } |
| 1061 | |
| 1062 | // Reprioritize a request to the same level as another and confirm it is |
| 1063 | // put after the old request. |
| 1064 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeResetFIFO) { |
| 1065 | CreatePool(kDefaultMaxSockets, 1); |
| 1066 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1067 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1068 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1069 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1070 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1071 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1072 | EXPECT_FALSE(request(2)->handle()->socket()); |
| 1073 | |
| 1074 | request(1)->handle()->SetPriority(MEDIUM); |
| 1075 | |
| 1076 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1077 | |
| 1078 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1079 | EXPECT_EQ(3, GetOrderOfRequest(2)); |
| 1080 | EXPECT_EQ(2, GetOrderOfRequest(3)); |
| 1081 | } |
| 1082 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1083 | TEST_F(ClientSocketPoolBaseTest, TotalLimitRespectsGroupLimit) { |
| 1084 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1085 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1086 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsOk()); |
| 1087 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsOk()); |
| 1088 | EXPECT_THAT(StartRequest(TestGroupId("b"), HIGHEST), IsOk()); |
| 1089 | EXPECT_THAT(StartRequest(TestGroupId("b"), MEDIUM), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1090 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1091 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1092 | client_socket_factory_.allocation_count()); |
| 1093 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1094 | EXPECT_THAT(StartRequest(TestGroupId("c"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1095 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 1096 | EXPECT_THAT(StartRequest(TestGroupId("b"), HIGHEST), IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1097 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1098 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1099 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1100 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1101 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1102 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1103 | |
| 1104 | // First 4 requests don't have to wait, and finish in order. |
| 1105 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1106 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1107 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 1108 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 1109 | |
| 1110 | // Request ("b", 7) has the highest priority, but we can't make new socket for |
| 1111 | // group "b", because it has reached the per-group limit. Then we make |
| 1112 | // socket for ("c", 6), because it has higher priority than ("a", 4), |
| 1113 | // and we still can't make a socket for group "b". |
| 1114 | EXPECT_EQ(5, GetOrderOfRequest(5)); |
| 1115 | EXPECT_EQ(6, GetOrderOfRequest(6)); |
| 1116 | EXPECT_EQ(7, GetOrderOfRequest(7)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1117 | |
| 1118 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1119 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(8)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1120 | } |
| 1121 | |
| 1122 | // Make sure that we count connecting sockets against the total limit. |
| 1123 | TEST_F(ClientSocketPoolBaseTest, TotalLimitCountsConnectingSockets) { |
| 1124 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1125 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1126 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1127 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
| 1128 | EXPECT_THAT(StartRequest(TestGroupId("c"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1129 | |
| 1130 | // Create one asynchronous request. |
| 1131 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1132 | EXPECT_THAT(StartRequest(TestGroupId("d"), DEFAULT_PRIORITY), |
| 1133 | IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1134 | |
[email protected] | 6b17538 | 2009-10-13 06:47:47 | [diff] [blame] | 1135 | // We post all of our delayed tasks with a 2ms delay. I.e. they don't |
| 1136 | // actually become pending until 2ms after they have been created. In order |
| 1137 | // to flush all tasks, we need to wait so that we know there are no |
| 1138 | // soon-to-be-pending tasks waiting. |
Alex Clarke | 0def209 | 2018-12-10 12:01:45 | [diff] [blame] | 1139 | FastForwardBy(base::TimeDelta::FromMilliseconds(10)); |
[email protected] | 6b17538 | 2009-10-13 06:47:47 | [diff] [blame] | 1140 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1141 | // The next synchronous request should wait for its turn. |
| 1142 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1143 | EXPECT_THAT(StartRequest(TestGroupId("e"), DEFAULT_PRIORITY), |
| 1144 | IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1145 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1146 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1147 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1148 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1149 | client_socket_factory_.allocation_count()); |
| 1150 | |
| 1151 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1152 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1153 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 1154 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1155 | EXPECT_EQ(5, GetOrderOfRequest(5)); |
| 1156 | |
| 1157 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1158 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(6)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1159 | } |
| 1160 | |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1161 | TEST_F(ClientSocketPoolBaseTest, CorrectlyCountStalledGroups) { |
| 1162 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 1163 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 1164 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1165 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1166 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1167 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1168 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1169 | |
| 1170 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 1171 | |
| 1172 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1173 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1174 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), |
| 1175 | IsError(ERR_IO_PENDING)); |
| 1176 | EXPECT_THAT(StartRequest(TestGroupId("c"), DEFAULT_PRIORITY), |
| 1177 | IsError(ERR_IO_PENDING)); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1178 | |
| 1179 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1180 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1181 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1182 | EXPECT_EQ(kDefaultMaxSockets + 1, client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1183 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1184 | EXPECT_EQ(kDefaultMaxSockets + 2, client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1185 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
| 1186 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1187 | EXPECT_EQ(kDefaultMaxSockets + 2, client_socket_factory_.allocation_count()); |
| 1188 | } |
| 1189 | |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 1190 | TEST_F(ClientSocketPoolBaseTest, StallAndThenCancelAndTriggerAvailableSocket) { |
| 1191 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 1192 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1193 | |
| 1194 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1195 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1196 | EXPECT_EQ( |
| 1197 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1198 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1199 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1200 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 1201 | NetLogWithSource())); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 1202 | |
| 1203 | ClientSocketHandle handles[4]; |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 1204 | for (size_t i = 0; i < base::size(handles); ++i) { |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1205 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1206 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1207 | handles[i].Init( |
| 1208 | TestGroupId("b"), params_, DEFAULT_PRIORITY, SocketTag(), |
| 1209 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1210 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 1211 | NetLogWithSource())); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 1212 | } |
| 1213 | |
| 1214 | // One will be stalled, cancel all the handles now. |
| 1215 | // This should hit the OnAvailableSocketSlot() code where we previously had |
| 1216 | // stalled groups, but no longer have any. |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 1217 | for (size_t i = 0; i < base::size(handles); ++i) |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 1218 | handles[i].Reset(); |
| 1219 | } |
| 1220 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1221 | TEST_F(ClientSocketPoolBaseTest, CancelStalledSocketAtSocketLimit) { |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1222 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1223 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 1224 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1225 | { |
| 1226 | ClientSocketHandle handles[kDefaultMaxSockets]; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1227 | TestCompletionCallback callbacks[kDefaultMaxSockets]; |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1228 | for (int i = 0; i < kDefaultMaxSockets; ++i) { |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1229 | EXPECT_EQ(OK, handles[i].Init(TestGroupId(base::NumberToString(i)), |
| 1230 | params_, DEFAULT_PRIORITY, SocketTag(), |
Paul Jensen | 8d6f87ec | 2018-01-13 00:46:54 | [diff] [blame] | 1231 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1232 | callbacks[i].callback(), |
| 1233 | ClientSocketPool::ProxyAuthCallback(), |
| 1234 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1235 | } |
| 1236 | |
| 1237 | // Force a stalled group. |
| 1238 | ClientSocketHandle stalled_handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1239 | TestCompletionCallback callback; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1240 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1241 | stalled_handle.Init( |
| 1242 | TestGroupId("foo"), params_, DEFAULT_PRIORITY, SocketTag(), |
| 1243 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1244 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 1245 | NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1246 | |
| 1247 | // Cancel the stalled request. |
| 1248 | stalled_handle.Reset(); |
| 1249 | |
| 1250 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1251 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 1252 | |
| 1253 | // Dropping out of scope will close all handles and return them to idle. |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1254 | } |
| 1255 | |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1256 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1257 | EXPECT_EQ(kDefaultMaxSockets, pool_->IdleSocketCount()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1258 | } |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1259 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1260 | TEST_F(ClientSocketPoolBaseTest, CancelPendingSocketAtSocketLimit) { |
| 1261 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1262 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 1263 | |
| 1264 | { |
| 1265 | ClientSocketHandle handles[kDefaultMaxSockets]; |
| 1266 | for (int i = 0; i < kDefaultMaxSockets; ++i) { |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1267 | TestCompletionCallback callback; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1268 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1269 | handles[i].Init(TestGroupId(base::NumberToString(i)), params_, |
| 1270 | DEFAULT_PRIORITY, SocketTag(), |
| 1271 | ClientSocketPool::RespectLimits::ENABLED, |
| 1272 | callback.callback(), |
| 1273 | ClientSocketPool::ProxyAuthCallback(), |
| 1274 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1275 | } |
| 1276 | |
| 1277 | // Force a stalled group. |
| 1278 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1279 | ClientSocketHandle stalled_handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1280 | TestCompletionCallback callback; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1281 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1282 | stalled_handle.Init( |
| 1283 | TestGroupId("foo"), params_, DEFAULT_PRIORITY, SocketTag(), |
| 1284 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1285 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 1286 | NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1287 | |
| 1288 | // Since it is stalled, it should have no connect jobs. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1289 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("foo"))); |
| 1290 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 1291 | TestGroupId("foo"))); |
| 1292 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroupForTesting( |
| 1293 | TestGroupId("foo"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1294 | |
| 1295 | // Cancel the stalled request. |
| 1296 | handles[0].Reset(); |
| 1297 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1298 | // Now we should have a connect job. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1299 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("foo"))); |
| 1300 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 1301 | TestGroupId("foo"))); |
| 1302 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroupForTesting( |
| 1303 | TestGroupId("foo"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1304 | |
| 1305 | // The stalled socket should connect. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1306 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1307 | |
| 1308 | EXPECT_EQ(kDefaultMaxSockets + 1, |
| 1309 | client_socket_factory_.allocation_count()); |
| 1310 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1311 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("foo"))); |
| 1312 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 1313 | TestGroupId("foo"))); |
| 1314 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroupForTesting( |
| 1315 | TestGroupId("foo"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1316 | |
| 1317 | // Dropping out of scope will close all handles and return them to idle. |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1318 | } |
| 1319 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1320 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 1321 | } |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1322 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1323 | TEST_F(ClientSocketPoolBaseTest, WaitForStalledSocketAtSocketLimit) { |
| 1324 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1325 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1326 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1327 | ClientSocketHandle stalled_handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1328 | TestCompletionCallback callback; |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1329 | { |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 1330 | EXPECT_FALSE(pool_->IsStalled()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1331 | ClientSocketHandle handles[kDefaultMaxSockets]; |
| 1332 | for (int i = 0; i < kDefaultMaxSockets; ++i) { |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1333 | TestCompletionCallback callback; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1334 | EXPECT_EQ( |
| 1335 | OK, handles[i].Init(TestGroupId(base::StringPrintf("Take 2: %d", i)), |
| 1336 | params_, DEFAULT_PRIORITY, SocketTag(), |
| 1337 | ClientSocketPool::RespectLimits::ENABLED, |
| 1338 | callback.callback(), |
| 1339 | ClientSocketPool::ProxyAuthCallback(), |
| 1340 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1341 | } |
| 1342 | |
| 1343 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1344 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 1345 | EXPECT_FALSE(pool_->IsStalled()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1346 | |
| 1347 | // Now we will hit the socket limit. |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1348 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1349 | stalled_handle.Init( |
| 1350 | TestGroupId("foo"), params_, DEFAULT_PRIORITY, SocketTag(), |
| 1351 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1352 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 1353 | NetLogWithSource())); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 1354 | EXPECT_TRUE(pool_->IsStalled()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1355 | |
| 1356 | // Dropping out of scope will close all handles and return them to idle. |
| 1357 | } |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1358 | |
| 1359 | // But if we wait for it, the released idle sockets will be closed in |
| 1360 | // preference of the waiting request. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1361 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1362 | |
| 1363 | EXPECT_EQ(kDefaultMaxSockets + 1, client_socket_factory_.allocation_count()); |
| 1364 | EXPECT_EQ(3, pool_->IdleSocketCount()); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1365 | } |
| 1366 | |
| 1367 | // Regression test for http://crbug.com/40952. |
| 1368 | TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketAtSocketLimitDeleteGroup) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1369 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 1370 | true /* enable_backup_connect_jobs */); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1371 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 1372 | |
| 1373 | for (int i = 0; i < kDefaultMaxSockets; ++i) { |
| 1374 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1375 | TestCompletionCallback callback; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1376 | EXPECT_EQ(OK, handle.Init(TestGroupId(base::NumberToString(i)), params_, |
| 1377 | DEFAULT_PRIORITY, SocketTag(), |
| 1378 | ClientSocketPool::RespectLimits::ENABLED, |
| 1379 | callback.callback(), |
| 1380 | ClientSocketPool::ProxyAuthCallback(), |
| 1381 | pool_.get(), NetLogWithSource())); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1382 | } |
| 1383 | |
| 1384 | // Flush all the DoReleaseSocket tasks. |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 1385 | base::RunLoop().RunUntilIdle(); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1386 | |
| 1387 | // Stall a group. Set a pending job so it'll trigger a backup job if we don't |
| 1388 | // reuse a socket. |
| 1389 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1390 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1391 | TestCompletionCallback callback; |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1392 | |
| 1393 | // "0" is special here, since it should be the first entry in the sorted map, |
| 1394 | // which is the one which we would close an idle socket for. We shouldn't |
| 1395 | // close an idle socket though, since we should reuse the idle socket. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1396 | EXPECT_EQ(OK, handle.Init( |
| 1397 | TestGroupId("0"), params_, DEFAULT_PRIORITY, SocketTag(), |
| 1398 | ClientSocketPool::RespectLimits::ENABLED, |
| 1399 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1400 | pool_.get(), NetLogWithSource())); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1401 | |
| 1402 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1403 | EXPECT_EQ(kDefaultMaxSockets - 1, pool_->IdleSocketCount()); |
| 1404 | } |
| 1405 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1406 | TEST_F(ClientSocketPoolBaseTest, PendingRequests) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1407 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1408 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1409 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1410 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1411 | EXPECT_THAT(StartRequest(TestGroupId("a"), IDLE), IsError(ERR_IO_PENDING)); |
| 1412 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
| 1413 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1414 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1415 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 1416 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1417 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1418 | ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1419 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
| 1420 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1421 | EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup, |
| 1422 | completion_count()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1423 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1424 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1425 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
[email protected] | c9c6f5c | 2010-07-31 01:30:03 | [diff] [blame] | 1426 | EXPECT_EQ(8, GetOrderOfRequest(3)); |
| 1427 | EXPECT_EQ(6, GetOrderOfRequest(4)); |
| 1428 | EXPECT_EQ(4, GetOrderOfRequest(5)); |
| 1429 | EXPECT_EQ(3, GetOrderOfRequest(6)); |
| 1430 | EXPECT_EQ(5, GetOrderOfRequest(7)); |
| 1431 | EXPECT_EQ(7, GetOrderOfRequest(8)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1432 | |
| 1433 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1434 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(9)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1435 | } |
| 1436 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1437 | TEST_F(ClientSocketPoolBaseTest, PendingRequests_NoKeepAlive) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1438 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1439 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1440 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1441 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1442 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
| 1443 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1444 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1445 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 1446 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1447 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1448 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1449 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1450 | for (size_t i = kDefaultMaxSocketsPerGroup; i < requests_size(); ++i) |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1451 | EXPECT_THAT(request(i)->WaitForResult(), IsOk()); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1452 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1453 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1454 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1455 | EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup, |
| 1456 | completion_count()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1457 | } |
| 1458 | |
| 1459 | // This test will start up a RequestSocket() and then immediately Cancel() it. |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1460 | // The pending connect job will be cancelled and should not call back into |
| 1461 | // ClientSocketPoolBase. |
| 1462 | TEST_F(ClientSocketPoolBaseTest, CancelRequestClearGroup) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1463 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1464 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1465 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1466 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1467 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1468 | EXPECT_EQ( |
| 1469 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1470 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1471 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1472 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 1473 | NetLogWithSource())); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1474 | handle.Reset(); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1475 | } |
| 1476 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1477 | TEST_F(ClientSocketPoolBaseTest, ConnectCancelConnect) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1478 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1479 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1480 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 1481 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1482 | TestCompletionCallback callback; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1483 | |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1484 | EXPECT_EQ( |
| 1485 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1486 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1487 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1488 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 1489 | NetLogWithSource())); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1490 | |
| 1491 | handle.Reset(); |
| 1492 | |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1493 | TestCompletionCallback callback2; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1494 | EXPECT_EQ( |
| 1495 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1496 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1497 | ClientSocketPool::RespectLimits::ENABLED, |
| 1498 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1499 | pool_.get(), NetLogWithSource())); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1500 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1501 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1502 | EXPECT_FALSE(callback.have_result()); |
| 1503 | |
| 1504 | handle.Reset(); |
| 1505 | } |
| 1506 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1507 | TEST_F(ClientSocketPoolBaseTest, CancelRequest) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1508 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1509 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1510 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1511 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1512 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
| 1513 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1514 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1515 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 1516 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1517 | |
| 1518 | // Cancel a request. |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1519 | size_t index_to_cancel = kDefaultMaxSocketsPerGroup + 2; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1520 | EXPECT_FALSE((*requests())[index_to_cancel]->handle()->is_initialized()); |
| 1521 | (*requests())[index_to_cancel]->handle()->Reset(); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1522 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1523 | ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE); |
[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(kDefaultMaxSocketsPerGroup, |
| 1526 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1527 | EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup - 1, |
| 1528 | completion_count()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1529 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1530 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1531 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1532 | EXPECT_EQ(5, GetOrderOfRequest(3)); |
| 1533 | EXPECT_EQ(3, GetOrderOfRequest(4)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1534 | EXPECT_EQ(ClientSocketPoolTest::kRequestNotFound, |
| 1535 | GetOrderOfRequest(5)); // Canceled request. |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1536 | EXPECT_EQ(4, GetOrderOfRequest(6)); |
| 1537 | EXPECT_EQ(6, GetOrderOfRequest(7)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1538 | |
| 1539 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1540 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(8)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1541 | } |
| 1542 | |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1543 | // Function to be used as a callback on socket request completion. It first |
| 1544 | // disconnects the successfully connected socket from the first request, and |
| 1545 | // then reuses the ClientSocketHandle to request another socket. |
| 1546 | // |
| 1547 | // |nested_callback| is called with the result of the second socket request. |
| 1548 | void RequestSocketOnComplete(ClientSocketHandle* handle, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1549 | TransportClientSocketPool* pool, |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1550 | TestConnectJobFactory* test_connect_job_factory, |
| 1551 | TestConnectJob::JobType next_job_type, |
Bence Béky | a4a5093 | 2018-08-10 13:39:41 | [diff] [blame] | 1552 | TestCompletionCallback* nested_callback, |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1553 | int first_request_result) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1554 | EXPECT_THAT(first_request_result, IsOk()); |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1555 | |
| 1556 | test_connect_job_factory->set_job_type(next_job_type); |
| 1557 | |
| 1558 | // Don't allow reuse of the socket. Disconnect it and then release it. |
| 1559 | if (handle->socket()) |
| 1560 | handle->socket()->Disconnect(); |
| 1561 | handle->Reset(); |
| 1562 | |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1563 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1564 | int rv = handle->Init( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1565 | TestGroupId("a"), CreateDummyParams(), LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1566 | ClientSocketPool::RespectLimits::ENABLED, nested_callback->callback(), |
| 1567 | ClientSocketPool::ProxyAuthCallback(), pool, NetLogWithSource()); |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1568 | if (rv != ERR_IO_PENDING) { |
| 1569 | DCHECK_EQ(TestConnectJob::kMockJob, next_job_type); |
Bence Béky | a4a5093 | 2018-08-10 13:39:41 | [diff] [blame] | 1570 | nested_callback->callback().Run(rv); |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1571 | } else { |
| 1572 | DCHECK_EQ(TestConnectJob::kMockPendingJob, next_job_type); |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1573 | } |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1574 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1575 | |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1576 | // Tests the case where a second socket is requested in a completion callback, |
| 1577 | // and the second socket connects asynchronously. Reuses the same |
| 1578 | // ClientSocketHandle for the second socket, after disconnecting the first. |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1579 | TEST_F(ClientSocketPoolBaseTest, RequestPendingJobTwice) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1580 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1581 | |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 1582 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 1583 | ClientSocketHandle handle; |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1584 | TestCompletionCallback second_result_callback; |
| 1585 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1586 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Paul Jensen | 8d6f87ec | 2018-01-13 00:46:54 | [diff] [blame] | 1587 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1588 | base::BindOnce(&RequestSocketOnComplete, &handle, pool_.get(), |
| 1589 | connect_job_factory_, TestConnectJob::kMockPendingJob, |
| 1590 | &second_result_callback), |
| 1591 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1592 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1593 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1594 | EXPECT_THAT(second_result_callback.WaitForResult(), IsOk()); |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1595 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1596 | |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1597 | // Tests the case where a second socket is requested in a completion callback, |
| 1598 | // and the second socket connects synchronously. Reuses the same |
| 1599 | // ClientSocketHandle for the second socket, after disconnecting the first. |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1600 | TEST_F(ClientSocketPoolBaseTest, RequestPendingJobThenSynchronous) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1601 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1602 | |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 1603 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 1604 | ClientSocketHandle handle; |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1605 | TestCompletionCallback second_result_callback; |
| 1606 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1607 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Paul Jensen | 8d6f87ec | 2018-01-13 00:46:54 | [diff] [blame] | 1608 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1609 | base::BindOnce(&RequestSocketOnComplete, &handle, pool_.get(), |
| 1610 | connect_job_factory_, TestConnectJob::kMockPendingJob, |
| 1611 | &second_result_callback), |
| 1612 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1613 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1614 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1615 | EXPECT_THAT(second_result_callback.WaitForResult(), IsOk()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1616 | } |
| 1617 | |
| 1618 | // Make sure that pending requests get serviced after active requests get |
| 1619 | // cancelled. |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1620 | TEST_F(ClientSocketPoolBaseTest, CancelActiveRequestWithPendingRequests) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1621 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1622 | |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 1623 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1624 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1625 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1626 | IsError(ERR_IO_PENDING)); |
| 1627 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1628 | IsError(ERR_IO_PENDING)); |
| 1629 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1630 | IsError(ERR_IO_PENDING)); |
| 1631 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1632 | IsError(ERR_IO_PENDING)); |
| 1633 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1634 | IsError(ERR_IO_PENDING)); |
| 1635 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1636 | IsError(ERR_IO_PENDING)); |
| 1637 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1638 | IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1639 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1640 | // Now, kDefaultMaxSocketsPerGroup requests should be active. |
| 1641 | // Let's cancel them. |
| 1642 | for (int i = 0; i < kDefaultMaxSocketsPerGroup; ++i) { |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1643 | ASSERT_FALSE(request(i)->handle()->is_initialized()); |
| 1644 | request(i)->handle()->Reset(); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1645 | } |
| 1646 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1647 | // Let's wait for the rest to complete now. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1648 | for (size_t i = kDefaultMaxSocketsPerGroup; i < requests_size(); ++i) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1649 | EXPECT_THAT(request(i)->WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1650 | request(i)->handle()->Reset(); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1651 | } |
| 1652 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1653 | EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup, |
| 1654 | completion_count()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1655 | } |
| 1656 | |
| 1657 | // Make sure that pending requests get serviced after active requests fail. |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1658 | TEST_F(ClientSocketPoolBaseTest, FailingActiveRequestWithPendingRequests) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1659 | const size_t kMaxSockets = 5; |
| 1660 | CreatePool(kMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1661 | |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 1662 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1663 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1664 | const size_t kNumberOfRequests = 2 * kDefaultMaxSocketsPerGroup + 1; |
| 1665 | ASSERT_LE(kNumberOfRequests, kMaxSockets); // Otherwise the test will hang. |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1666 | |
| 1667 | // Queue up all the requests |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1668 | for (size_t i = 0; i < kNumberOfRequests; ++i) |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1669 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1670 | IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1671 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1672 | for (size_t i = 0; i < kNumberOfRequests; ++i) |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1673 | EXPECT_THAT(request(i)->WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1674 | } |
| 1675 | |
mmenke | 9d72fe4 | 2017-05-18 22:36:07 | [diff] [blame] | 1676 | // Make sure that pending requests that complete synchronously get serviced |
| 1677 | // after active requests fail. See https://crbug.com/723748 |
| 1678 | TEST_F(ClientSocketPoolBaseTest, HandleMultipleSyncFailuresAfterAsyncFailure) { |
| 1679 | const size_t kNumberOfRequests = 10; |
| 1680 | const size_t kMaxSockets = 1; |
| 1681 | CreatePool(kMaxSockets, kMaxSockets); |
| 1682 | |
| 1683 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
| 1684 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1685 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1686 | IsError(ERR_IO_PENDING)); |
mmenke | 9d72fe4 | 2017-05-18 22:36:07 | [diff] [blame] | 1687 | |
| 1688 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
| 1689 | |
| 1690 | // Queue up all the other requests |
| 1691 | for (size_t i = 1; i < kNumberOfRequests; ++i) |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1692 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1693 | IsError(ERR_IO_PENDING)); |
mmenke | 9d72fe4 | 2017-05-18 22:36:07 | [diff] [blame] | 1694 | |
| 1695 | // Make sure all requests fail, instead of hanging. |
| 1696 | for (size_t i = 0; i < kNumberOfRequests; ++i) |
| 1697 | EXPECT_THAT(request(i)->WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 1698 | } |
| 1699 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1700 | TEST_F(ClientSocketPoolBaseTest, CancelActiveRequestThenRequestSocket) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1701 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1702 | |
| 1703 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1704 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1705 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1706 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1707 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1708 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1709 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1710 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1711 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1712 | |
| 1713 | // Cancel the active request. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1714 | handle.Reset(); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1715 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1716 | rv = handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 1717 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1718 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1719 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1720 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 1721 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1722 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1723 | EXPECT_FALSE(handle.is_reused()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 1724 | TestLoadTimingInfoConnectedNotReused(handle); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1725 | EXPECT_EQ(2, client_socket_factory_.allocation_count()); |
| 1726 | } |
| 1727 | |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1728 | TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketsForced) { |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1729 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1730 | ClientSocketHandle handle; |
| 1731 | TestCompletionCallback callback; |
| 1732 | BoundTestNetLog log; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1733 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1734 | TestGroupId("a"), params_, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1735 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1736 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1737 | EXPECT_THAT(rv, IsOk()); |
| 1738 | handle.Reset(); |
| 1739 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 1740 | pool_->CloseIdleSockets(); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1741 | } |
| 1742 | |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1743 | TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketsInGroupForced) { |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1744 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1745 | TestCompletionCallback callback; |
| 1746 | BoundTestNetLog log; |
| 1747 | ClientSocketHandle handle1; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1748 | int rv = handle1.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1749 | TestGroupId("a"), params_, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1750 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1751 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1752 | EXPECT_THAT(rv, IsOk()); |
| 1753 | ClientSocketHandle handle2; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1754 | rv = handle2.Init(TestGroupId("a"), 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 | ClientSocketHandle handle3; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1759 | rv = handle3.Init(TestGroupId("b"), params_, LOWEST, SocketTag(), |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1760 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1761 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1762 | pool_.get(), log.bound()); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1763 | EXPECT_THAT(rv, IsOk()); |
| 1764 | handle1.Reset(); |
| 1765 | handle2.Reset(); |
| 1766 | handle3.Reset(); |
| 1767 | EXPECT_EQ(3, pool_->IdleSocketCount()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1768 | pool_->CloseIdleSocketsInGroup(TestGroupId("a")); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1769 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1770 | } |
| 1771 | |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1772 | TEST_F(ClientSocketPoolBaseTest, CleanUpUnusableIdleSockets) { |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1773 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1774 | ClientSocketHandle handle; |
| 1775 | TestCompletionCallback callback; |
| 1776 | BoundTestNetLog log; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1777 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1778 | TestGroupId("a"), params_, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1779 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1780 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1781 | EXPECT_THAT(rv, IsOk()); |
| 1782 | StreamSocket* socket = handle.socket(); |
| 1783 | handle.Reset(); |
| 1784 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 1785 | |
| 1786 | // Disconnect socket now to make the socket unusable. |
| 1787 | socket->Disconnect(); |
| 1788 | ClientSocketHandle handle2; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1789 | rv = handle2.Init(TestGroupId("a"), params_, LOWEST, SocketTag(), |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1790 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1791 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1792 | pool_.get(), log.bound()); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1793 | EXPECT_THAT(rv, IsOk()); |
| 1794 | EXPECT_FALSE(handle2.is_reused()); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1795 | } |
| 1796 | |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1797 | // Regression test for http://crbug.com/17985. |
| 1798 | TEST_F(ClientSocketPoolBaseTest, GroupWithPendingRequestsIsNotEmpty) { |
| 1799 | const int kMaxSockets = 3; |
| 1800 | const int kMaxSocketsPerGroup = 2; |
| 1801 | CreatePool(kMaxSockets, kMaxSocketsPerGroup); |
| 1802 | |
[email protected] | ac790b4 | 2009-12-02 04:31:31 | [diff] [blame] | 1803 | const RequestPriority kHighPriority = HIGHEST; |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1804 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1805 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1806 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1807 | |
| 1808 | // This is going to be a pending request in an otherwise empty group. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1809 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1810 | IsError(ERR_IO_PENDING)); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1811 | |
| 1812 | // Reach the maximum socket limit. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1813 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1814 | |
| 1815 | // Create a stalled group with high priorities. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1816 | EXPECT_THAT(StartRequest(TestGroupId("c"), kHighPriority), |
| 1817 | IsError(ERR_IO_PENDING)); |
| 1818 | EXPECT_THAT(StartRequest(TestGroupId("c"), kHighPriority), |
| 1819 | IsError(ERR_IO_PENDING)); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1820 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1821 | // Release the first two sockets from TestGroupId("a"). Because this is a |
| 1822 | // keepalive, the first release will unblock the pending request for |
| 1823 | // TestGroupId("a"). The second release will unblock a request for "c", |
| 1824 | // because it is the next high priority socket. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1825 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
| 1826 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1827 | |
| 1828 | // Closing idle sockets should not get us into trouble, but in the bug |
| 1829 | // we were hitting a CHECK here. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1830 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1831 | pool_->CloseIdleSockets(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1832 | |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 1833 | // Run the released socket wakeups. |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 1834 | base::RunLoop().RunUntilIdle(); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1835 | } |
| 1836 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 1837 | TEST_F(ClientSocketPoolBaseTest, BasicAsynchronous) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1838 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1839 | |
| 1840 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1841 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1842 | TestCompletionCallback callback; |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 1843 | BoundTestNetLog log; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1844 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1845 | TestGroupId("a"), params_, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1846 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1847 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1848 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1849 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 1850 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 1851 | TestLoadTimingInfoNotConnected(handle); |
| 1852 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1853 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1854 | EXPECT_TRUE(handle.is_initialized()); |
| 1855 | EXPECT_TRUE(handle.socket()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 1856 | TestLoadTimingInfoConnectedNotReused(handle); |
| 1857 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1858 | handle.Reset(); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 1859 | TestLoadTimingInfoNotConnected(handle); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 1860 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 1861 | TestNetLogEntry::List entries; |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 1862 | log.GetEntries(&entries); |
| 1863 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1864 | EXPECT_EQ(5u, entries.size()); |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 1865 | EXPECT_TRUE(LogContainsEvent( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1866 | entries, 0, NetLogEventType::TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 1867 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1868 | EXPECT_TRUE(LogContainsBeginEvent(entries, 1, NetLogEventType::SOCKET_POOL)); |
| 1869 | EXPECT_TRUE(LogContainsEvent( |
| 1870 | entries, 2, NetLogEventType::SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
| 1871 | NetLogEventPhase::NONE)); |
| 1872 | EXPECT_TRUE(LogContainsEvent(entries, 3, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 1873 | NetLogEventType::SOCKET_POOL_BOUND_TO_SOCKET, |
| 1874 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1875 | EXPECT_TRUE(LogContainsEndEvent(entries, 4, NetLogEventType::SOCKET_POOL)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1876 | } |
| 1877 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 1878 | TEST_F(ClientSocketPoolBaseTest, |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1879 | InitConnectionAsynchronousFailure) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1880 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1881 | |
| 1882 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1883 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1884 | TestCompletionCallback callback; |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 1885 | BoundTestNetLog log; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 1886 | // Set the additional error state members to ensure that they get cleared. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1887 | handle.set_is_ssl_error(true); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 1888 | handle.set_ssl_cert_request_info(base::MakeRefCounted<SSLCertRequestInfo>()); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1889 | EXPECT_EQ( |
| 1890 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1891 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1892 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1893 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 1894 | log.bound())); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1895 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 1896 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1897 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1898 | EXPECT_FALSE(handle.is_ssl_error()); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 1899 | EXPECT_FALSE(handle.ssl_cert_request_info()); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 1900 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 1901 | TestNetLogEntry::List entries; |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 1902 | log.GetEntries(&entries); |
| 1903 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1904 | EXPECT_EQ(4u, entries.size()); |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 1905 | EXPECT_TRUE(LogContainsEvent( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1906 | entries, 0, NetLogEventType::TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 1907 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1908 | EXPECT_TRUE(LogContainsBeginEvent(entries, 1, NetLogEventType::SOCKET_POOL)); |
| 1909 | EXPECT_TRUE(LogContainsEvent( |
| 1910 | entries, 2, NetLogEventType::SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
| 1911 | NetLogEventPhase::NONE)); |
| 1912 | EXPECT_TRUE(LogContainsEndEvent(entries, 3, NetLogEventType::SOCKET_POOL)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1913 | } |
| 1914 | |
mmenke | 6be122f | 2015-03-09 22:22:47 | [diff] [blame] | 1915 | // Check that an async ConnectJob failure does not result in creation of a new |
| 1916 | // ConnectJob when there's another pending request also waiting on its own |
| 1917 | // ConnectJob. See http://crbug.com/463960. |
| 1918 | TEST_F(ClientSocketPoolBaseTest, AsyncFailureWithPendingRequestWithJob) { |
| 1919 | CreatePool(2, 2); |
| 1920 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
| 1921 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1922 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1923 | IsError(ERR_IO_PENDING)); |
| 1924 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1925 | IsError(ERR_IO_PENDING)); |
mmenke | 6be122f | 2015-03-09 22:22:47 | [diff] [blame] | 1926 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1927 | EXPECT_THAT(request(0)->WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 1928 | EXPECT_THAT(request(1)->WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
mmenke | 6be122f | 2015-03-09 22:22:47 | [diff] [blame] | 1929 | |
| 1930 | EXPECT_EQ(2, client_socket_factory_.allocation_count()); |
| 1931 | } |
| 1932 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 1933 | TEST_F(ClientSocketPoolBaseTest, TwoRequestsCancelOne) { |
[email protected] | b22b516 | 2010-03-16 07:53:10 | [diff] [blame] | 1934 | // TODO(eroman): Add back the log expectations! Removed them because the |
| 1935 | // ordering is difficult, and some may fire during destructor. |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1936 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1937 | |
| 1938 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1939 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1940 | TestCompletionCallback callback; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1941 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1942 | TestCompletionCallback callback2; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1943 | |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1944 | EXPECT_EQ( |
| 1945 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1946 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1947 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1948 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 1949 | NetLogWithSource())); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 1950 | BoundTestNetLog log2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1951 | EXPECT_EQ( |
| 1952 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1953 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1954 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1955 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1956 | pool_.get(), NetLogWithSource())); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1957 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1958 | handle.Reset(); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1959 | |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 1960 | |
| 1961 | // 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] | 1962 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1963 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1964 | handle2.Reset(); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 1965 | |
| 1966 | // Now request 2 has actually finished. |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 1967 | // TODO(eroman): Add back log expectations. |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1968 | } |
| 1969 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 1970 | TEST_F(ClientSocketPoolBaseTest, CancelRequestLimitsJobs) { |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 1971 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1972 | |
[email protected] | 17a0c6c | 2009-08-04 00:07:04 | [diff] [blame] | 1973 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1974 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1975 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
| 1976 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 1977 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1978 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 1979 | |
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] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1983 | (*requests())[2]->handle()->Reset(); |
| 1984 | (*requests())[3]->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())[1]->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 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1994 | (*requests())[0]->handle()->Reset(); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 1995 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1996 | static_cast<int>( |
| 1997 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 1998 | } |
| 1999 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2000 | // When requests and ConnectJobs are not coupled, the request will get serviced |
| 2001 | // by whatever comes first. |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2002 | TEST_F(ClientSocketPoolBaseTest, ReleaseSockets) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2003 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2004 | |
| 2005 | // Start job 1 (async OK) |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 2006 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2007 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2008 | std::vector<TestSocketRequest*> request_order; |
| 2009 | size_t completion_count; // unused |
| 2010 | TestSocketRequest req1(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2011 | int rv = req1.handle()->Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2012 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2013 | ClientSocketPool::RespectLimits::ENABLED, req1.callback(), |
| 2014 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2015 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 2016 | EXPECT_THAT(req1.WaitForResult(), IsOk()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2017 | |
| 2018 | // Job 1 finished OK. Start job 2 (also async OK). Request 3 is pending |
| 2019 | // without a job. |
| 2020 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2021 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2022 | TestSocketRequest req2(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2023 | rv = req2.handle()->Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2024 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2025 | ClientSocketPool::RespectLimits::ENABLED, req2.callback(), |
| 2026 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2027 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2028 | TestSocketRequest req3(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2029 | rv = req3.handle()->Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2030 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2031 | ClientSocketPool::RespectLimits::ENABLED, req3.callback(), |
| 2032 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2033 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2034 | |
| 2035 | // Both Requests 2 and 3 are pending. We release socket 1 which should |
| 2036 | // service request 2. Request 3 should still be waiting. |
[email protected] | a6c59f6 | 2009-07-29 16:33:33 | [diff] [blame] | 2037 | req1.handle()->Reset(); |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 2038 | // Run the released socket wakeups. |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 2039 | base::RunLoop().RunUntilIdle(); |
[email protected] | a6c59f6 | 2009-07-29 16:33:33 | [diff] [blame] | 2040 | ASSERT_TRUE(req2.handle()->socket()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2041 | EXPECT_THAT(req2.WaitForResult(), IsOk()); |
[email protected] | a6c59f6 | 2009-07-29 16:33:33 | [diff] [blame] | 2042 | EXPECT_FALSE(req3.handle()->socket()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2043 | |
| 2044 | // Signal job 2, which should service request 3. |
| 2045 | |
| 2046 | client_socket_factory_.SignalJobs(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2047 | EXPECT_THAT(req3.WaitForResult(), IsOk()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2048 | |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2049 | ASSERT_EQ(3u, request_order.size()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2050 | EXPECT_EQ(&req1, request_order[0]); |
| 2051 | EXPECT_EQ(&req2, request_order[1]); |
| 2052 | EXPECT_EQ(&req3, request_order[2]); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2053 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2054 | } |
| 2055 | |
| 2056 | // The requests are not coupled to the jobs. So, the requests should finish in |
| 2057 | // their priority / insertion order. |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2058 | TEST_F(ClientSocketPoolBaseTest, PendingJobCompletionOrder) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2059 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2060 | // First two jobs are async. |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 2061 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2062 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2063 | std::vector<TestSocketRequest*> request_order; |
| 2064 | size_t completion_count; // unused |
| 2065 | TestSocketRequest req1(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2066 | int rv = req1.handle()->Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2067 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2068 | ClientSocketPool::RespectLimits::ENABLED, req1.callback(), |
| 2069 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2070 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2071 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2072 | TestSocketRequest req2(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2073 | rv = req2.handle()->Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2074 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2075 | ClientSocketPool::RespectLimits::ENABLED, req2.callback(), |
| 2076 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2077 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2078 | |
| 2079 | // The pending job is sync. |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 2080 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2081 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2082 | TestSocketRequest req3(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2083 | rv = req3.handle()->Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2084 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2085 | ClientSocketPool::RespectLimits::ENABLED, req3.callback(), |
| 2086 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2087 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2088 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2089 | EXPECT_THAT(req1.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 2090 | EXPECT_THAT(req2.WaitForResult(), IsOk()); |
| 2091 | EXPECT_THAT(req3.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2092 | |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2093 | ASSERT_EQ(3u, request_order.size()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2094 | EXPECT_EQ(&req1, request_order[0]); |
| 2095 | EXPECT_EQ(&req2, request_order[1]); |
| 2096 | EXPECT_EQ(&req3, request_order[2]); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2097 | } |
| 2098 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2099 | // Test GetLoadState in the case there's only one socket request. |
| 2100 | TEST_F(ClientSocketPoolBaseTest, LoadStateOneRequest) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2101 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2102 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2103 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2104 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2105 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2106 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2107 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2108 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2109 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2110 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2111 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2112 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2113 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE); |
| 2114 | EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle.GetLoadState()); |
| 2115 | |
| 2116 | // No point in completing the connection, since ClientSocketHandles only |
| 2117 | // expect the LoadState to be checked while connecting. |
| 2118 | } |
| 2119 | |
| 2120 | // Test GetLoadState in the case there are two socket requests. |
| 2121 | TEST_F(ClientSocketPoolBaseTest, LoadStateTwoRequests) { |
| 2122 | CreatePool(2, 2); |
| 2123 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2124 | |
| 2125 | ClientSocketHandle handle; |
| 2126 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2127 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2128 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2129 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2130 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2131 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2132 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_RESOLVING_HOST); |
| 2133 | |
| 2134 | ClientSocketHandle handle2; |
| 2135 | TestCompletionCallback callback2; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2136 | rv = handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 2137 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2138 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2139 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2140 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2141 | client_socket_factory_.SetJobLoadState(1, LOAD_STATE_RESOLVING_HOST); |
| 2142 | |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 2143 | // Each handle should reflect the state of its own job. |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2144 | EXPECT_EQ(LOAD_STATE_RESOLVING_HOST, handle.GetLoadState()); |
| 2145 | EXPECT_EQ(LOAD_STATE_RESOLVING_HOST, handle2.GetLoadState()); |
| 2146 | |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 2147 | // Update the state of the first job. |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2148 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_CONNECTING); |
| 2149 | |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 2150 | // Only the state of the first request should have changed. |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2151 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2152 | EXPECT_EQ(LOAD_STATE_RESOLVING_HOST, handle2.GetLoadState()); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 2153 | |
| 2154 | // Update the state of the second job. |
| 2155 | client_socket_factory_.SetJobLoadState(1, LOAD_STATE_SSL_HANDSHAKE); |
| 2156 | |
| 2157 | // Only the state of the second request should have changed. |
| 2158 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
| 2159 | EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle2.GetLoadState()); |
| 2160 | |
| 2161 | // Second job connects and the first request gets the socket. The |
| 2162 | // second handle switches to the state of the remaining ConnectJob. |
| 2163 | client_socket_factory_.SignalJob(1); |
| 2164 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 2165 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle2.GetLoadState()); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2166 | } |
| 2167 | |
| 2168 | // Test GetLoadState in the case the per-group limit is reached. |
| 2169 | TEST_F(ClientSocketPoolBaseTest, LoadStateGroupLimit) { |
| 2170 | CreatePool(2, 1); |
| 2171 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2172 | |
| 2173 | ClientSocketHandle handle; |
| 2174 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2175 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2176 | TestGroupId("a"), params_, MEDIUM, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2177 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2178 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2179 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2180 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
| 2181 | |
| 2182 | // Request another socket from the same pool, buth with a higher priority. |
| 2183 | // The first request should now be stalled at the socket group limit. |
| 2184 | ClientSocketHandle handle2; |
| 2185 | TestCompletionCallback callback2; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2186 | rv = handle2.Init(TestGroupId("a"), params_, HIGHEST, SocketTag(), |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 2187 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2188 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2189 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2190 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2191 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET, handle.GetLoadState()); |
| 2192 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle2.GetLoadState()); |
| 2193 | |
| 2194 | // The first handle should remain stalled as the other socket goes through |
| 2195 | // the connect process. |
| 2196 | |
| 2197 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE); |
| 2198 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET, handle.GetLoadState()); |
| 2199 | EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle2.GetLoadState()); |
| 2200 | |
| 2201 | client_socket_factory_.SignalJob(0); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2202 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2203 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET, handle.GetLoadState()); |
| 2204 | |
| 2205 | // Closing the second socket should cause the stalled handle to finally get a |
| 2206 | // ConnectJob. |
| 2207 | handle2.socket()->Disconnect(); |
| 2208 | handle2.Reset(); |
| 2209 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
| 2210 | } |
| 2211 | |
| 2212 | // Test GetLoadState in the case the per-pool limit is reached. |
| 2213 | TEST_F(ClientSocketPoolBaseTest, LoadStatePoolLimit) { |
| 2214 | CreatePool(2, 2); |
| 2215 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2216 | |
| 2217 | ClientSocketHandle handle; |
| 2218 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2219 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2220 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2221 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2222 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2223 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2224 | |
| 2225 | // Request for socket from another pool. |
| 2226 | ClientSocketHandle handle2; |
| 2227 | TestCompletionCallback callback2; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2228 | rv = handle2.Init(TestGroupId("b"), params_, DEFAULT_PRIORITY, SocketTag(), |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 2229 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2230 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2231 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2232 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2233 | |
| 2234 | // Request another socket from the first pool. Request should stall at the |
| 2235 | // socket pool limit. |
| 2236 | ClientSocketHandle handle3; |
| 2237 | TestCompletionCallback callback3; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2238 | rv = handle3.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 2239 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2240 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2241 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2242 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2243 | |
| 2244 | // The third handle should remain stalled as the other sockets in its group |
| 2245 | // goes through the connect process. |
| 2246 | |
| 2247 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
| 2248 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL, handle3.GetLoadState()); |
| 2249 | |
| 2250 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE); |
| 2251 | EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle.GetLoadState()); |
| 2252 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL, handle3.GetLoadState()); |
| 2253 | |
| 2254 | client_socket_factory_.SignalJob(0); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2255 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2256 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL, handle3.GetLoadState()); |
| 2257 | |
| 2258 | // Closing a socket should allow the stalled handle to finally get a new |
| 2259 | // ConnectJob. |
| 2260 | handle.socket()->Disconnect(); |
| 2261 | handle.Reset(); |
| 2262 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle3.GetLoadState()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2263 | } |
| 2264 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2265 | TEST_F(ClientSocketPoolBaseTest, CertError) { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2266 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2267 | connect_job_factory_->set_job_type(TestConnectJob::kMockCertErrorJob); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2268 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2269 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2270 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2271 | EXPECT_EQ( |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2272 | ERR_CERT_COMMON_NAME_INVALID, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2273 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2274 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2275 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2276 | NetLogWithSource())); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2277 | EXPECT_TRUE(handle.is_initialized()); |
| 2278 | EXPECT_TRUE(handle.socket()); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2279 | } |
| 2280 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2281 | TEST_F(ClientSocketPoolBaseTest, AsyncCertError) { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2282 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2283 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2284 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingCertErrorJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2285 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2286 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2287 | EXPECT_EQ( |
| 2288 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2289 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2290 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2291 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2292 | NetLogWithSource())); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2293 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2294 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2295 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_CERT_COMMON_NAME_INVALID)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2296 | EXPECT_TRUE(handle.is_initialized()); |
| 2297 | EXPECT_TRUE(handle.socket()); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2298 | } |
| 2299 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2300 | TEST_F(ClientSocketPoolBaseTest, AdditionalErrorStateSynchronous) { |
| 2301 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2302 | connect_job_factory_->set_job_type( |
| 2303 | TestConnectJob::kMockAdditionalErrorStateJob); |
| 2304 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2305 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2306 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2307 | EXPECT_EQ( |
| 2308 | ERR_CONNECTION_FAILED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2309 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2310 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2311 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2312 | NetLogWithSource())); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2313 | EXPECT_FALSE(handle.is_initialized()); |
| 2314 | EXPECT_FALSE(handle.socket()); |
| 2315 | EXPECT_TRUE(handle.is_ssl_error()); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 2316 | EXPECT_TRUE(handle.ssl_cert_request_info()); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2317 | } |
| 2318 | |
| 2319 | TEST_F(ClientSocketPoolBaseTest, AdditionalErrorStateAsynchronous) { |
| 2320 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2321 | |
| 2322 | connect_job_factory_->set_job_type( |
| 2323 | TestConnectJob::kMockPendingAdditionalErrorStateJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2324 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2325 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2326 | EXPECT_EQ( |
| 2327 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2328 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2329 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2330 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2331 | NetLogWithSource())); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2332 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2333 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2334 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2335 | EXPECT_FALSE(handle.is_initialized()); |
| 2336 | EXPECT_FALSE(handle.socket()); |
| 2337 | EXPECT_TRUE(handle.is_ssl_error()); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 2338 | EXPECT_TRUE(handle.ssl_cert_request_info()); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2339 | } |
| 2340 | |
martijn | 003cd61 | 2016-05-19 22:24:38 | [diff] [blame] | 2341 | // Make sure we can reuse sockets. |
| 2342 | TEST_F(ClientSocketPoolBaseTest, CleanupTimedOutIdleSocketsReuse) { |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2343 | CreatePoolWithIdleTimeouts( |
| 2344 | kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2345 | base::TimeDelta(), // Time out unused sockets immediately. |
| 2346 | base::TimeDelta::FromDays(1)); // Don't time out used sockets. |
| 2347 | |
| 2348 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2349 | |
| 2350 | ClientSocketHandle handle; |
| 2351 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2352 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2353 | TestGroupId("a"), params_, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2354 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2355 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2356 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2357 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2358 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2359 | ASSERT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2360 | |
| 2361 | // Use and release the socket. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 2362 | EXPECT_EQ(1, handle.socket()->Write(nullptr, 1, CompletionOnceCallback(), |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 2363 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 2364 | TestLoadTimingInfoConnectedNotReused(handle); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2365 | handle.Reset(); |
| 2366 | |
| 2367 | // Should now have one idle socket. |
| 2368 | ASSERT_EQ(1, pool_->IdleSocketCount()); |
| 2369 | |
| 2370 | // Request a new socket. This should reuse the old socket and complete |
| 2371 | // synchronously. |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 2372 | BoundTestNetLog log; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2373 | rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2374 | TestGroupId("a"), params_, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2375 | ClientSocketPool::RespectLimits::ENABLED, CompletionOnceCallback(), |
| 2376 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2377 | ASSERT_THAT(rv, IsOk()); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2378 | EXPECT_TRUE(handle.is_reused()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 2379 | TestLoadTimingInfoConnectedReused(handle); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2380 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2381 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2382 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2383 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2384 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 2385 | TestNetLogEntry::List entries; |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2386 | log.GetEntries(&entries); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2387 | EXPECT_TRUE(LogContainsEvent( |
| 2388 | entries, 0, NetLogEventType::TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET, |
| 2389 | NetLogEventPhase::NONE)); |
| 2390 | EXPECT_TRUE(LogContainsBeginEvent(entries, 1, NetLogEventType::SOCKET_POOL)); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2391 | EXPECT_TRUE(LogContainsEntryWithType( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2392 | entries, 2, NetLogEventType::SOCKET_POOL_REUSED_AN_EXISTING_SOCKET)); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2393 | } |
| 2394 | |
martijn | 003cd61 | 2016-05-19 22:24:38 | [diff] [blame] | 2395 | // Make sure we cleanup old unused sockets. |
Eric Roman | b49715e | 2018-04-24 22:41:17 | [diff] [blame] | 2396 | TEST_F(ClientSocketPoolBaseTest, CleanupTimedOutIdleSocketsNoReuse) { |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2397 | CreatePoolWithIdleTimeouts( |
| 2398 | kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 2399 | base::TimeDelta(), // Time out unused sockets immediately |
| 2400 | base::TimeDelta()); // Time out used sockets immediately |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2401 | |
| 2402 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2403 | |
| 2404 | // Startup two mock pending connect jobs, which will sit in the MessageLoop. |
| 2405 | |
| 2406 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2407 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2408 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2409 | TestGroupId("a"), params_, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2410 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2411 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2412 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2413 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2414 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2415 | |
| 2416 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2417 | TestCompletionCallback callback2; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2418 | rv = handle2.Init(TestGroupId("a"), params_, LOWEST, SocketTag(), |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 2419 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2420 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2421 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2422 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2423 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2424 | pool_->GetLoadState(TestGroupId("a"), &handle2)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2425 | |
| 2426 | // Cancel one of the requests. Wait for the other, which will get the first |
| 2427 | // job. Release the socket. Run the loop again to make sure the second |
| 2428 | // socket is sitting idle and the first one is released (since ReleaseSocket() |
| 2429 | // just posts a DoReleaseSocket() task). |
| 2430 | |
| 2431 | handle.Reset(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2432 | ASSERT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2433 | // Use the socket. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 2434 | EXPECT_EQ(1, handle2.socket()->Write(nullptr, 1, CompletionOnceCallback(), |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 2435 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2436 | handle2.Reset(); |
| 2437 | |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2438 | // We post all of our delayed tasks with a 2ms delay. I.e. they don't |
| 2439 | // actually become pending until 2ms after they have been created. In order |
| 2440 | // to flush all tasks, we need to wait so that we know there are no |
| 2441 | // soon-to-be-pending tasks waiting. |
Alex Clarke | 0def209 | 2018-12-10 12:01:45 | [diff] [blame] | 2442 | FastForwardBy(base::TimeDelta::FromMilliseconds(10)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2443 | |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2444 | // Both sockets should now be idle. |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2445 | ASSERT_EQ(2, pool_->IdleSocketCount()); |
| 2446 | |
| 2447 | // Request a new socket. This should cleanup the unused and timed out ones. |
| 2448 | // A new socket will be created rather than reusing the idle one. |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 2449 | BoundTestNetLog log; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2450 | TestCompletionCallback callback3; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2451 | rv = handle.Init(TestGroupId("a"), params_, LOWEST, SocketTag(), |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 2452 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2453 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2454 | pool_.get(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2455 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 2456 | ASSERT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2457 | EXPECT_FALSE(handle.is_reused()); |
| 2458 | |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2459 | // Make sure the idle socket is closed. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2460 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2461 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2462 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2463 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 2464 | TestNetLogEntry::List entries; |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2465 | log.GetEntries(&entries); |
| 2466 | EXPECT_FALSE(LogContainsEntryWithType( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 2467 | entries, 1, NetLogEventType::SOCKET_POOL_REUSED_AN_EXISTING_SOCKET)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2468 | } |
| 2469 | |
[email protected] | 2041cf34 | 2010-02-19 03:15:59 | [diff] [blame] | 2470 | // Make sure that we process all pending requests even when we're stalling |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2471 | // because of multiple releasing disconnected sockets. |
| 2472 | TEST_F(ClientSocketPoolBaseTest, MultipleReleasingDisconnectedSockets) { |
| 2473 | CreatePoolWithIdleTimeouts( |
| 2474 | kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 2475 | base::TimeDelta(), // Time out unused sockets immediately. |
| 2476 | base::TimeDelta::FromDays(1)); // Don't time out used sockets. |
| 2477 | |
| 2478 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 2479 | |
| 2480 | // Startup 4 connect jobs. Two of them will be pending. |
| 2481 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2482 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2483 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2484 | int rv = handle.Init( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2485 | TestGroupId("a"), params_, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2486 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2487 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2488 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2489 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2490 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2491 | TestCompletionCallback callback2; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2492 | rv = handle2.Init(TestGroupId("a"), params_, LOWEST, SocketTag(), |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 2493 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2494 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2495 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2496 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2497 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2498 | ClientSocketHandle handle3; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2499 | TestCompletionCallback callback3; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2500 | rv = handle3.Init(TestGroupId("a"), params_, LOWEST, SocketTag(), |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 2501 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2502 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2503 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2504 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2505 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2506 | ClientSocketHandle handle4; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2507 | TestCompletionCallback callback4; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2508 | rv = handle4.Init(TestGroupId("a"), params_, LOWEST, SocketTag(), |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 2509 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2510 | callback4.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2511 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2512 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2513 | |
| 2514 | // Release two disconnected sockets. |
| 2515 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2516 | handle.socket()->Disconnect(); |
| 2517 | handle.Reset(); |
| 2518 | handle2.socket()->Disconnect(); |
| 2519 | handle2.Reset(); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2520 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2521 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2522 | EXPECT_FALSE(handle3.is_reused()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2523 | EXPECT_THAT(callback4.WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2524 | EXPECT_FALSE(handle4.is_reused()); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2525 | } |
| 2526 | |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2527 | // Regression test for http://crbug.com/42267. |
| 2528 | // When DoReleaseSocket() is processed for one socket, it is blocked because the |
| 2529 | // other stalled groups all have releasing sockets, so no progress can be made. |
| 2530 | TEST_F(ClientSocketPoolBaseTest, SocketLimitReleasingSockets) { |
| 2531 | CreatePoolWithIdleTimeouts( |
| 2532 | 4 /* socket limit */, 4 /* socket limit per group */, |
| 2533 | base::TimeDelta(), // Time out unused sockets immediately. |
| 2534 | base::TimeDelta::FromDays(1)); // Don't time out used sockets. |
| 2535 | |
| 2536 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 2537 | |
| 2538 | // Max out the socket limit with 2 per group. |
| 2539 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2540 | ClientSocketHandle handle_a[4]; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2541 | TestCompletionCallback callback_a[4]; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2542 | ClientSocketHandle handle_b[4]; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2543 | TestCompletionCallback callback_b[4]; |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2544 | |
| 2545 | for (int i = 0; i < 2; ++i) { |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2546 | EXPECT_EQ( |
| 2547 | OK, handle_a[i].Init(TestGroupId("a"), params_, LOWEST, SocketTag(), |
| 2548 | ClientSocketPool::RespectLimits::ENABLED, |
| 2549 | callback_a[i].callback(), |
| 2550 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2551 | NetLogWithSource())); |
| 2552 | EXPECT_EQ( |
| 2553 | OK, handle_b[i].Init(TestGroupId("b"), params_, LOWEST, SocketTag(), |
| 2554 | ClientSocketPool::RespectLimits::ENABLED, |
| 2555 | callback_b[i].callback(), |
| 2556 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2557 | NetLogWithSource())); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2558 | } |
[email protected] | b89f7e4 | 2010-05-20 20:37:00 | [diff] [blame] | 2559 | |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2560 | // Make 4 pending requests, 2 per group. |
| 2561 | |
| 2562 | for (int i = 2; i < 4; ++i) { |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 2563 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2564 | handle_a[i].Init(TestGroupId("a"), params_, LOWEST, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 2565 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2566 | callback_a[i].callback(), |
| 2567 | ClientSocketPool::ProxyAuthCallback(), |
| 2568 | pool_.get(), NetLogWithSource())); |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 2569 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2570 | handle_b[i].Init(TestGroupId("b"), params_, LOWEST, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 2571 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2572 | callback_b[i].callback(), |
| 2573 | ClientSocketPool::ProxyAuthCallback(), |
| 2574 | pool_.get(), NetLogWithSource())); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2575 | } |
| 2576 | |
| 2577 | // Release b's socket first. The order is important, because in |
| 2578 | // DoReleaseSocket(), we'll process b's released socket, and since both b and |
| 2579 | // a are stalled, but 'a' is lower lexicographically, we'll process group 'a' |
| 2580 | // first, which has a releasing socket, so it refuses to start up another |
| 2581 | // ConnectJob. So, we used to infinite loop on this. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2582 | handle_b[0].socket()->Disconnect(); |
| 2583 | handle_b[0].Reset(); |
| 2584 | handle_a[0].socket()->Disconnect(); |
| 2585 | handle_a[0].Reset(); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2586 | |
| 2587 | // Used to get stuck here. |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 2588 | base::RunLoop().RunUntilIdle(); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2589 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2590 | handle_b[1].socket()->Disconnect(); |
| 2591 | handle_b[1].Reset(); |
| 2592 | handle_a[1].socket()->Disconnect(); |
| 2593 | handle_a[1].Reset(); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2594 | |
| 2595 | for (int i = 2; i < 4; ++i) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2596 | EXPECT_THAT(callback_b[i].WaitForResult(), IsOk()); |
| 2597 | EXPECT_THAT(callback_a[i].WaitForResult(), IsOk()); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2598 | } |
| 2599 | } |
| 2600 | |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2601 | TEST_F(ClientSocketPoolBaseTest, |
| 2602 | ReleasingDisconnectedSocketsMaintainsPriorityOrder) { |
| 2603 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2604 | |
| 2605 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2606 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2607 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2608 | IsError(ERR_IO_PENDING)); |
| 2609 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2610 | IsError(ERR_IO_PENDING)); |
| 2611 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2612 | IsError(ERR_IO_PENDING)); |
| 2613 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2614 | IsError(ERR_IO_PENDING)); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2615 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2616 | EXPECT_THAT((*requests())[0]->WaitForResult(), IsOk()); |
| 2617 | EXPECT_THAT((*requests())[1]->WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2618 | EXPECT_EQ(2u, completion_count()); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2619 | |
| 2620 | // Releases one connection. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2621 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::NO_KEEP_ALIVE)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2622 | EXPECT_THAT((*requests())[2]->WaitForResult(), IsOk()); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2623 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2624 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::NO_KEEP_ALIVE)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2625 | EXPECT_THAT((*requests())[3]->WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2626 | EXPECT_EQ(4u, completion_count()); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2627 | |
| 2628 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 2629 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 2630 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 2631 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 2632 | |
| 2633 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2634 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(5)); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2635 | } |
| 2636 | |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2637 | class TestReleasingSocketRequest : public TestCompletionCallbackBase { |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2638 | public: |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2639 | TestReleasingSocketRequest(TransportClientSocketPool* pool, |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2640 | int expected_result, |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2641 | bool reset_releasing_handle) |
| 2642 | : pool_(pool), |
| 2643 | expected_result_(expected_result), |
Bence Béky | 8ddc249 | 2018-06-13 01:02:04 | [diff] [blame] | 2644 | reset_releasing_handle_(reset_releasing_handle) {} |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2645 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 2646 | ~TestReleasingSocketRequest() override = default; |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2647 | |
| 2648 | ClientSocketHandle* handle() { return &handle_; } |
| 2649 | |
Bence Béky | 8ddc249 | 2018-06-13 01:02:04 | [diff] [blame] | 2650 | CompletionOnceCallback callback() { |
| 2651 | return base::BindOnce(&TestReleasingSocketRequest::OnComplete, |
| 2652 | base::Unretained(this)); |
| 2653 | } |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2654 | |
| 2655 | private: |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2656 | void OnComplete(int result) { |
| 2657 | SetResult(result); |
| 2658 | if (reset_releasing_handle_) |
| 2659 | handle_.Reset(); |
| 2660 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2661 | EXPECT_EQ( |
| 2662 | expected_result_, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2663 | handle2_.Init(TestGroupId("a"), CreateDummyParams(), DEFAULT_PRIORITY, |
| 2664 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2665 | CompletionOnceCallback(), |
| 2666 | ClientSocketPool::ProxyAuthCallback(), pool_, |
| 2667 | NetLogWithSource())); |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2668 | } |
| 2669 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2670 | TransportClientSocketPool* const pool_; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2671 | int expected_result_; |
| 2672 | bool reset_releasing_handle_; |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2673 | ClientSocketHandle handle_; |
| 2674 | ClientSocketHandle handle2_; |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2675 | }; |
| 2676 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2677 | |
| 2678 | TEST_F(ClientSocketPoolBaseTest, AdditionalErrorSocketsDontUseSlot) { |
| 2679 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2680 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2681 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
| 2682 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 2683 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2684 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2685 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2686 | client_socket_factory_.allocation_count()); |
| 2687 | |
| 2688 | connect_job_factory_->set_job_type( |
| 2689 | TestConnectJob::kMockPendingAdditionalErrorStateJob); |
| 2690 | TestReleasingSocketRequest req(pool_.get(), OK, false); |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 2691 | EXPECT_EQ( |
| 2692 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2693 | req.handle()->Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, |
| 2694 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2695 | req.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2696 | pool_.get(), NetLogWithSource())); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2697 | // The next job should complete synchronously |
| 2698 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 2699 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2700 | EXPECT_THAT(req.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2701 | EXPECT_FALSE(req.handle()->is_initialized()); |
| 2702 | EXPECT_FALSE(req.handle()->socket()); |
| 2703 | EXPECT_TRUE(req.handle()->is_ssl_error()); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 2704 | EXPECT_TRUE(req.handle()->ssl_cert_request_info()); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2705 | } |
| 2706 | |
[email protected] | b6501d3d | 2010-06-03 23:53:34 | [diff] [blame] | 2707 | // http://crbug.com/44724 regression test. |
| 2708 | // We start releasing the pool when we flush on network change. When that |
| 2709 | // happens, the only active references are in the ClientSocketHandles. When a |
| 2710 | // ConnectJob completes and calls back into the last ClientSocketHandle, that |
| 2711 | // callback can release the last reference and delete the pool. After the |
| 2712 | // callback finishes, we go back to the stack frame within the now-deleted pool. |
| 2713 | // Executing any code that refers to members of the now-deleted pool can cause |
| 2714 | // crashes. |
| 2715 | TEST_F(ClientSocketPoolBaseTest, CallbackThatReleasesPool) { |
| 2716 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2717 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
| 2718 | |
| 2719 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2720 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2721 | EXPECT_EQ( |
| 2722 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2723 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2724 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2725 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2726 | NetLogWithSource())); |
[email protected] | b6501d3d | 2010-06-03 23:53:34 | [diff] [blame] | 2727 | |
[email protected] | 7af985a | 2012-12-14 22:40:42 | [diff] [blame] | 2728 | pool_->FlushWithError(ERR_NETWORK_CHANGED); |
[email protected] | b6501d3d | 2010-06-03 23:53:34 | [diff] [blame] | 2729 | |
| 2730 | // We'll call back into this now. |
| 2731 | callback.WaitForResult(); |
| 2732 | } |
| 2733 | |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2734 | TEST_F(ClientSocketPoolBaseTest, DoNotReuseSocketAfterFlush) { |
| 2735 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2736 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2737 | |
| 2738 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2739 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2740 | EXPECT_EQ( |
| 2741 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2742 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2743 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2744 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2745 | NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2746 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2747 | EXPECT_EQ(ClientSocketHandle::UNUSED, handle.reuse_type()); |
| 2748 | |
[email protected] | 7af985a | 2012-12-14 22:40:42 | [diff] [blame] | 2749 | pool_->FlushWithError(ERR_NETWORK_CHANGED); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2750 | |
| 2751 | handle.Reset(); |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 2752 | base::RunLoop().RunUntilIdle(); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2753 | |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2754 | EXPECT_EQ( |
| 2755 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2756 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2757 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2758 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2759 | NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2760 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2761 | EXPECT_EQ(ClientSocketHandle::UNUSED, handle.reuse_type()); |
| 2762 | } |
| 2763 | |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2764 | class ConnectWithinCallback : public TestCompletionCallbackBase { |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2765 | public: |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2766 | ConnectWithinCallback( |
| 2767 | const ClientSocketPool::GroupId& group_id, |
Matt Menke | 84d11e56 | 2019-03-27 00:11:19 | [diff] [blame] | 2768 | const scoped_refptr<ClientSocketPool::SocketParams>& params, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2769 | TransportClientSocketPool* pool) |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2770 | : group_id_(group_id), params_(params), pool_(pool) {} |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2771 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 2772 | ~ConnectWithinCallback() override = default; |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2773 | |
| 2774 | int WaitForNestedResult() { |
| 2775 | return nested_callback_.WaitForResult(); |
| 2776 | } |
| 2777 | |
Bence Béky | 8ddc249 | 2018-06-13 01:02:04 | [diff] [blame] | 2778 | CompletionOnceCallback callback() { |
| 2779 | return base::BindOnce(&ConnectWithinCallback::OnComplete, |
| 2780 | base::Unretained(this)); |
| 2781 | } |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2782 | |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2783 | private: |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2784 | void OnComplete(int result) { |
| 2785 | SetResult(result); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2786 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2787 | handle_.Init(group_id_, params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2788 | ClientSocketPool::RespectLimits::ENABLED, |
| 2789 | nested_callback_.callback(), |
| 2790 | ClientSocketPool::ProxyAuthCallback(), pool_, |
| 2791 | NetLogWithSource())); |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2792 | } |
| 2793 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2794 | const ClientSocketPool::GroupId group_id_; |
Matt Menke | 84d11e56 | 2019-03-27 00:11:19 | [diff] [blame] | 2795 | const scoped_refptr<ClientSocketPool::SocketParams> params_; |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2796 | TransportClientSocketPool* const pool_; |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2797 | ClientSocketHandle handle_; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2798 | TestCompletionCallback nested_callback_; |
| 2799 | |
| 2800 | DISALLOW_COPY_AND_ASSIGN(ConnectWithinCallback); |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2801 | }; |
| 2802 | |
| 2803 | TEST_F(ClientSocketPoolBaseTest, AbortAllRequestsOnFlush) { |
| 2804 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2805 | |
| 2806 | // First job will be waiting until it gets aborted. |
| 2807 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2808 | |
| 2809 | ClientSocketHandle handle; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2810 | ConnectWithinCallback callback(TestGroupId("a"), params_, pool_.get()); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2811 | EXPECT_EQ( |
| 2812 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2813 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2814 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2815 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2816 | NetLogWithSource())); |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2817 | |
| 2818 | // Second job will be started during the first callback, and will |
| 2819 | // asynchronously complete with OK. |
| 2820 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 7af985a | 2012-12-14 22:40:42 | [diff] [blame] | 2821 | pool_->FlushWithError(ERR_NETWORK_CHANGED); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2822 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_NETWORK_CHANGED)); |
| 2823 | EXPECT_THAT(callback.WaitForNestedResult(), IsOk()); |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2824 | } |
| 2825 | |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 2826 | TEST_F(ClientSocketPoolBaseTest, BackupSocketWaitsForHostResolution) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2827 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets, |
| 2828 | true /* enable_backup_connect_jobs */); |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 2829 | |
| 2830 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2831 | ClientSocketHandle handle; |
| 2832 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2833 | EXPECT_EQ( |
| 2834 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2835 | handle.Init(TestGroupId("bar"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2836 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2837 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2838 | NetLogWithSource())); |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 2839 | // The backup timer fires but doesn't start a new ConnectJob while resolving |
| 2840 | // the hostname. |
| 2841 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_RESOLVING_HOST); |
| 2842 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
| 2843 | ClientSocketPool::kMaxConnectRetryIntervalMs * 100)); |
| 2844 | EXPECT_EQ(1, client_socket_factory_.allocation_count()); |
| 2845 | |
| 2846 | // Once the ConnectJob has finished resolving the hostname, the backup timer |
| 2847 | // will create a ConnectJob when it fires. |
| 2848 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_CONNECTING); |
| 2849 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
| 2850 | ClientSocketPool::kMaxConnectRetryIntervalMs)); |
| 2851 | EXPECT_EQ(2, client_socket_factory_.allocation_count()); |
| 2852 | } |
| 2853 | |
| 2854 | // Test that no backup socket is created when a ConnectJob connects before it |
| 2855 | // completes. |
| 2856 | TEST_F(ClientSocketPoolBaseTest, NoBackupSocketWhenConnected) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2857 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets, |
| 2858 | true /* enable_backup_connect_jobs */); |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 2859 | |
| 2860 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2861 | ClientSocketHandle handle; |
| 2862 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2863 | EXPECT_EQ( |
| 2864 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2865 | handle.Init(TestGroupId("bar"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2866 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2867 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2868 | NetLogWithSource())); |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 2869 | // The backup timer fires but doesn't start a new ConnectJob while resolving |
| 2870 | // the hostname. |
| 2871 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_RESOLVING_HOST); |
| 2872 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
| 2873 | ClientSocketPool::kMaxConnectRetryIntervalMs * 100)); |
| 2874 | EXPECT_EQ(1, client_socket_factory_.allocation_count()); |
| 2875 | |
| 2876 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE); |
| 2877 | client_socket_factory_.SetJobHasEstablishedConnection(0); |
| 2878 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
| 2879 | ClientSocketPool::kMaxConnectRetryIntervalMs * 100)); |
| 2880 | EXPECT_EQ(1, client_socket_factory_.allocation_count()); |
| 2881 | } |
| 2882 | |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 2883 | // Cancel a pending socket request while we're at max sockets, |
| 2884 | // and verify that the backup socket firing doesn't cause a crash. |
| 2885 | TEST_F(ClientSocketPoolBaseTest, BackupSocketCancelAtMaxSockets) { |
| 2886 | // Max 4 sockets globally, max 4 sockets per group. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2887 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets, |
| 2888 | true /* enable_backup_connect_jobs */); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 2889 | |
[email protected] | 4baaf9d | 2010-08-31 15:15:44 | [diff] [blame] | 2890 | // Create the first socket and set to ERR_IO_PENDING. This starts the backup |
| 2891 | // timer. |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 2892 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2893 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2894 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2895 | EXPECT_EQ( |
| 2896 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2897 | handle.Init(TestGroupId("bar"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2898 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2899 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2900 | NetLogWithSource())); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 2901 | |
| 2902 | // Start (MaxSockets - 1) connected sockets to reach max sockets. |
| 2903 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 2904 | ClientSocketHandle handles[kDefaultMaxSockets]; |
| 2905 | for (int i = 1; i < kDefaultMaxSockets; ++i) { |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2906 | TestCompletionCallback callback; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2907 | EXPECT_EQ(OK, handles[i].Init(TestGroupId("bar"), params_, DEFAULT_PRIORITY, |
| 2908 | SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 2909 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2910 | callback.callback(), |
| 2911 | ClientSocketPool::ProxyAuthCallback(), |
| 2912 | pool_.get(), NetLogWithSource())); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 2913 | } |
| 2914 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 2915 | base::RunLoop().RunUntilIdle(); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 2916 | |
| 2917 | // Cancel the pending request. |
| 2918 | handle.Reset(); |
| 2919 | |
| 2920 | // Wait for the backup timer to fire (add some slop to ensure it fires) |
Alex Clarke | 0def209 | 2018-12-10 12:01:45 | [diff] [blame] | 2921 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
[email protected] | 26b997396 | 2012-01-28 00:57:00 | [diff] [blame] | 2922 | ClientSocketPool::kMaxConnectRetryIntervalMs / 2 * 3)); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 2923 | |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 2924 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 2925 | } |
| 2926 | |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 2927 | TEST_F(ClientSocketPoolBaseTest, CancelBackupSocketAfterCancelingAllRequests) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2928 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets, |
| 2929 | true /* enable_backup_connect_jobs */); |
[email protected] | 4baaf9d | 2010-08-31 15:15:44 | [diff] [blame] | 2930 | |
| 2931 | // Create the first socket and set to ERR_IO_PENDING. This starts the backup |
| 2932 | // timer. |
| 2933 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2934 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2935 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2936 | EXPECT_EQ( |
| 2937 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2938 | handle.Init(TestGroupId("bar"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2939 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2940 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2941 | NetLogWithSource())); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2942 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("bar"))); |
| 2943 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("bar"))); |
| 2944 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 2945 | TestGroupId("bar"))); |
| 2946 | EXPECT_EQ( |
| 2947 | 0u, pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("bar"))); |
[email protected] | 4baaf9d | 2010-08-31 15:15:44 | [diff] [blame] | 2948 | |
| 2949 | // Cancel the socket request. This should cancel the backup timer. Wait for |
| 2950 | // the backup time to see if it indeed got canceled. |
| 2951 | handle.Reset(); |
| 2952 | // Wait for the backup timer to fire (add some slop to ensure it fires) |
Alex Clarke | 0def209 | 2018-12-10 12:01:45 | [diff] [blame] | 2953 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
[email protected] | 26b997396 | 2012-01-28 00:57:00 | [diff] [blame] | 2954 | ClientSocketPool::kMaxConnectRetryIntervalMs / 2 * 3)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2955 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("bar"))); |
| 2956 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("bar"))); |
[email protected] | 4baaf9d | 2010-08-31 15:15:44 | [diff] [blame] | 2957 | } |
| 2958 | |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 2959 | TEST_F(ClientSocketPoolBaseTest, CancelBackupSocketAfterFinishingAllRequests) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2960 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets, |
| 2961 | true /* enable_backup_connect_jobs */); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 2962 | |
| 2963 | // Create the first socket and set to ERR_IO_PENDING. This starts the backup |
| 2964 | // timer. |
| 2965 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2966 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2967 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2968 | EXPECT_EQ( |
| 2969 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2970 | handle.Init(TestGroupId("bar"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2971 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2972 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2973 | NetLogWithSource())); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 2974 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2975 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2976 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 2977 | EXPECT_EQ( |
| 2978 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2979 | handle2.Init(TestGroupId("bar"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 2980 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2981 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2982 | pool_.get(), NetLogWithSource())); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2983 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("bar"))); |
| 2984 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("bar"))); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 2985 | |
| 2986 | // Cancel request 1 and then complete request 2. With the requests finished, |
| 2987 | // the backup timer should be cancelled. |
| 2988 | handle.Reset(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2989 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 2990 | // Wait for the backup timer to fire (add some slop to ensure it fires) |
Alex Clarke | 0def209 | 2018-12-10 12:01:45 | [diff] [blame] | 2991 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
[email protected] | 26b997396 | 2012-01-28 00:57:00 | [diff] [blame] | 2992 | ClientSocketPool::kMaxConnectRetryIntervalMs / 2 * 3)); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 2993 | } |
| 2994 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 2995 | // Test delayed socket binding for the case where we have two connects, |
| 2996 | // and while one is waiting on a connect, the other frees up. |
| 2997 | // The socket waiting on a connect should switch immediately to the freed |
| 2998 | // up socket. |
| 2999 | TEST_F(ClientSocketPoolBaseTest, DelayedSocketBindingWaitingForConnect) { |
| 3000 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3001 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3002 | |
| 3003 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3004 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3005 | EXPECT_EQ( |
| 3006 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3007 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3008 | ClientSocketPool::RespectLimits::ENABLED, |
| 3009 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3010 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3011 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3012 | |
| 3013 | // No idle sockets, no pending jobs. |
| 3014 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3015 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3016 | |
| 3017 | // Create a second socket to the same host, but this one will wait. |
| 3018 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3019 | ClientSocketHandle handle2; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3020 | EXPECT_EQ( |
| 3021 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3022 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3023 | ClientSocketPool::RespectLimits::ENABLED, |
| 3024 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3025 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3026 | // No idle sockets, and one connecting job. |
| 3027 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3028 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3029 | |
| 3030 | // Return the first handle to the pool. This will initiate the delayed |
| 3031 | // binding. |
| 3032 | handle1.Reset(); |
| 3033 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3034 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3035 | |
| 3036 | // Still no idle sockets, still one pending connect job. |
| 3037 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3038 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3039 | |
| 3040 | // The second socket connected, even though it was a Waiting Job. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3041 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3042 | |
| 3043 | // And we can see there is still one job waiting. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3044 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3045 | |
| 3046 | // Finally, signal the waiting Connect. |
| 3047 | client_socket_factory_.SignalJobs(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3048 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3049 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3050 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3051 | } |
| 3052 | |
| 3053 | // Test delayed socket binding when a group is at capacity and one |
| 3054 | // of the group's sockets frees up. |
| 3055 | TEST_F(ClientSocketPoolBaseTest, DelayedSocketBindingAtGroupCapacity) { |
| 3056 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3057 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3058 | |
| 3059 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3060 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3061 | EXPECT_EQ( |
| 3062 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3063 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3064 | ClientSocketPool::RespectLimits::ENABLED, |
| 3065 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3066 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3067 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3068 | |
| 3069 | // No idle sockets, no pending jobs. |
| 3070 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3071 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3072 | |
| 3073 | // Create a second socket to the same host, but this one will wait. |
| 3074 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3075 | ClientSocketHandle handle2; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3076 | EXPECT_EQ( |
| 3077 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3078 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3079 | ClientSocketPool::RespectLimits::ENABLED, |
| 3080 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3081 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3082 | // No idle sockets, and one connecting job. |
| 3083 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3084 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3085 | |
| 3086 | // Return the first handle to the pool. This will initiate the delayed |
| 3087 | // binding. |
| 3088 | handle1.Reset(); |
| 3089 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3090 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3091 | |
| 3092 | // Still no idle sockets, still one pending connect job. |
| 3093 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3094 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3095 | |
| 3096 | // The second socket connected, even though it was a Waiting Job. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3097 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3098 | |
| 3099 | // And we can see there is still one job waiting. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3100 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3101 | |
| 3102 | // Finally, signal the waiting Connect. |
| 3103 | client_socket_factory_.SignalJobs(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3104 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3105 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3106 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3107 | } |
| 3108 | |
| 3109 | // Test out the case where we have one socket connected, one |
| 3110 | // connecting, when the first socket finishes and goes idle. |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3111 | // Although the second connection is pending, the second request |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3112 | // should complete, by taking the first socket's idle socket. |
| 3113 | TEST_F(ClientSocketPoolBaseTest, DelayedSocketBindingAtStall) { |
| 3114 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3115 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3116 | |
| 3117 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3118 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3119 | EXPECT_EQ( |
| 3120 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3121 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3122 | ClientSocketPool::RespectLimits::ENABLED, |
| 3123 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3124 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3125 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3126 | |
| 3127 | // No idle sockets, no pending jobs. |
| 3128 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3129 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3130 | |
| 3131 | // Create a second socket to the same host, but this one will wait. |
| 3132 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3133 | ClientSocketHandle handle2; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3134 | EXPECT_EQ( |
| 3135 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3136 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3137 | ClientSocketPool::RespectLimits::ENABLED, |
| 3138 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3139 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3140 | // No idle sockets, and one connecting job. |
| 3141 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3142 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3143 | |
| 3144 | // Return the first handle to the pool. This will initiate the delayed |
| 3145 | // binding. |
| 3146 | handle1.Reset(); |
| 3147 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3148 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3149 | |
| 3150 | // Still no idle sockets, still one pending connect job. |
| 3151 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3152 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3153 | |
| 3154 | // The second socket connected, even though it was a Waiting Job. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3155 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3156 | |
| 3157 | // And we can see there is still one job waiting. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3158 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3159 | |
| 3160 | // Finally, signal the waiting Connect. |
| 3161 | client_socket_factory_.SignalJobs(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3162 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3163 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3164 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3165 | } |
| 3166 | |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3167 | // Cover the case where on an available socket slot, we have one pending |
| 3168 | // request that completes synchronously, thereby making the Group empty. |
| 3169 | TEST_F(ClientSocketPoolBaseTest, SynchronouslyProcessOnePendingRequest) { |
| 3170 | const int kUnlimitedSockets = 100; |
| 3171 | const int kOneSocketPerGroup = 1; |
| 3172 | CreatePool(kUnlimitedSockets, kOneSocketPerGroup); |
| 3173 | |
| 3174 | // Make the first request asynchronous fail. |
| 3175 | // This will free up a socket slot later. |
| 3176 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
| 3177 | |
| 3178 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3179 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3180 | EXPECT_EQ( |
| 3181 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3182 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3183 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3184 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3185 | pool_.get(), NetLogWithSource())); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3186 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3187 | |
| 3188 | // Make the second request synchronously fail. This should make the Group |
| 3189 | // empty. |
| 3190 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
| 3191 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3192 | TestCompletionCallback callback2; |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3193 | // It'll be ERR_IO_PENDING now, but the TestConnectJob will synchronously fail |
| 3194 | // when created. |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3195 | EXPECT_EQ( |
| 3196 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3197 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3198 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3199 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3200 | pool_.get(), NetLogWithSource())); |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3201 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3202 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3203 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3204 | EXPECT_THAT(callback1.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 3205 | EXPECT_THAT(callback2.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3206 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3207 | } |
| 3208 | |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3209 | TEST_F(ClientSocketPoolBaseTest, PreferUsedSocketToUnusedSocket) { |
| 3210 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 3211 | |
| 3212 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3213 | |
| 3214 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3215 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3216 | EXPECT_EQ( |
| 3217 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3218 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3219 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3220 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3221 | pool_.get(), NetLogWithSource())); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3222 | |
| 3223 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3224 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3225 | EXPECT_EQ( |
| 3226 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3227 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3228 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3229 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3230 | pool_.get(), NetLogWithSource())); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3231 | ClientSocketHandle handle3; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3232 | TestCompletionCallback callback3; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3233 | EXPECT_EQ( |
| 3234 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3235 | handle3.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3236 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3237 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3238 | pool_.get(), NetLogWithSource())); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3239 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3240 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3241 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
| 3242 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3243 | |
| 3244 | // Use the socket. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 3245 | EXPECT_EQ(1, handle1.socket()->Write(nullptr, 1, CompletionOnceCallback(), |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 3246 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 3247 | EXPECT_EQ(1, handle3.socket()->Write(nullptr, 1, CompletionOnceCallback(), |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 3248 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3249 | |
| 3250 | handle1.Reset(); |
| 3251 | handle2.Reset(); |
| 3252 | handle3.Reset(); |
| 3253 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3254 | EXPECT_EQ(OK, handle1.Init( |
| 3255 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
| 3256 | ClientSocketPool::RespectLimits::ENABLED, |
| 3257 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3258 | pool_.get(), NetLogWithSource())); |
| 3259 | EXPECT_EQ(OK, handle2.Init( |
| 3260 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
| 3261 | ClientSocketPool::RespectLimits::ENABLED, |
| 3262 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3263 | pool_.get(), NetLogWithSource())); |
| 3264 | EXPECT_EQ(OK, handle3.Init( |
| 3265 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
| 3266 | ClientSocketPool::RespectLimits::ENABLED, |
| 3267 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3268 | pool_.get(), NetLogWithSource())); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3269 | |
| 3270 | EXPECT_TRUE(handle1.socket()->WasEverUsed()); |
| 3271 | EXPECT_TRUE(handle2.socket()->WasEverUsed()); |
| 3272 | EXPECT_FALSE(handle3.socket()->WasEverUsed()); |
| 3273 | } |
| 3274 | |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3275 | TEST_F(ClientSocketPoolBaseTest, RequestSockets) { |
| 3276 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3277 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3278 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3279 | pool_->RequestSockets(TestGroupId("a"), params_, 2, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3280 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3281 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3282 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3283 | EXPECT_EQ(2u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3284 | TestGroupId("a"))); |
| 3285 | EXPECT_EQ(2u, |
| 3286 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3287 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3288 | |
| 3289 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3290 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3291 | EXPECT_EQ( |
| 3292 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3293 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3294 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3295 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3296 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3297 | |
| 3298 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3299 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3300 | EXPECT_EQ( |
| 3301 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3302 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3303 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3304 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3305 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3306 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3307 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3308 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3309 | TestGroupId("a"))); |
| 3310 | EXPECT_EQ(0u, |
| 3311 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3312 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3313 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3314 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3315 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3316 | handle1.Reset(); |
| 3317 | handle2.Reset(); |
| 3318 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3319 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3320 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3321 | TestGroupId("a"))); |
| 3322 | EXPECT_EQ(0u, |
| 3323 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3324 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3325 | } |
| 3326 | |
| 3327 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsWhenAlreadyHaveAConnectJob) { |
| 3328 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3329 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3330 | |
| 3331 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3332 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3333 | EXPECT_EQ( |
| 3334 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3335 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3336 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3337 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3338 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3339 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3340 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3341 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3342 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3343 | TestGroupId("a"))); |
| 3344 | EXPECT_EQ(0u, |
| 3345 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3346 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3347 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3348 | pool_->RequestSockets(TestGroupId("a"), params_, 2, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3349 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3350 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3351 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3352 | TestGroupId("a"))); |
| 3353 | EXPECT_EQ(1u, |
| 3354 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3355 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3356 | |
| 3357 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3358 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3359 | EXPECT_EQ( |
| 3360 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3361 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3362 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3363 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3364 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3365 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3366 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3367 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3368 | TestGroupId("a"))); |
| 3369 | EXPECT_EQ(0u, |
| 3370 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3371 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3372 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3373 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3374 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3375 | handle1.Reset(); |
| 3376 | handle2.Reset(); |
| 3377 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3378 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3379 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3380 | TestGroupId("a"))); |
| 3381 | EXPECT_EQ(0u, |
| 3382 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3383 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3384 | } |
| 3385 | |
| 3386 | TEST_F(ClientSocketPoolBaseTest, |
| 3387 | RequestSocketsWhenAlreadyHaveMultipleConnectJob) { |
| 3388 | CreatePool(4, 4); |
| 3389 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3390 | |
| 3391 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3392 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3393 | EXPECT_EQ( |
| 3394 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3395 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3396 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3397 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3398 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3399 | |
| 3400 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3401 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3402 | EXPECT_EQ( |
| 3403 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3404 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3405 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3406 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3407 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3408 | |
| 3409 | ClientSocketHandle handle3; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3410 | TestCompletionCallback callback3; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3411 | EXPECT_EQ( |
| 3412 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3413 | handle3.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3414 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3415 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3416 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3417 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3418 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3419 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3420 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3421 | TestGroupId("a"))); |
| 3422 | EXPECT_EQ(0u, |
| 3423 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3424 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3425 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3426 | pool_->RequestSockets(TestGroupId("a"), params_, 2, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3427 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3428 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3429 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3430 | TestGroupId("a"))); |
| 3431 | EXPECT_EQ(0u, |
| 3432 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3433 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3434 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3435 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3436 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
| 3437 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3438 | handle1.Reset(); |
| 3439 | handle2.Reset(); |
| 3440 | handle3.Reset(); |
| 3441 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3442 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3443 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3444 | TestGroupId("a"))); |
| 3445 | EXPECT_EQ(0u, |
| 3446 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3447 | EXPECT_EQ(3u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3448 | } |
| 3449 | |
| 3450 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsAtMaxSocketLimit) { |
| 3451 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 3452 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3453 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3454 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3455 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3456 | pool_->RequestSockets(TestGroupId("a"), params_, kDefaultMaxSockets, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3457 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3458 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3459 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3460 | EXPECT_EQ(kDefaultMaxSockets, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3461 | static_cast<int>( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3462 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
| 3463 | EXPECT_EQ( |
| 3464 | kDefaultMaxSockets, |
| 3465 | static_cast<int>(pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3466 | TestGroupId("a")))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3467 | EXPECT_EQ(kDefaultMaxSockets, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3468 | static_cast<int>(pool_->NumUnassignedConnectJobsInGroupForTesting( |
| 3469 | TestGroupId("a")))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3470 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3471 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("b"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3472 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3473 | pool_->RequestSockets(TestGroupId("b"), params_, kDefaultMaxSockets, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3474 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3475 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3476 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("b"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3477 | } |
| 3478 | |
| 3479 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsHitMaxSocketLimit) { |
| 3480 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 3481 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3482 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3483 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3484 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3485 | pool_->RequestSockets(TestGroupId("a"), params_, kDefaultMaxSockets - 1, |
Charlie Harrison | 55ce608 | 2018-05-14 02:25:57 | [diff] [blame] | 3486 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3487 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3488 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3489 | EXPECT_EQ(kDefaultMaxSockets - 1, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3490 | static_cast<int>( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3491 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
| 3492 | EXPECT_EQ( |
| 3493 | kDefaultMaxSockets - 1, |
| 3494 | static_cast<int>(pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3495 | TestGroupId("a")))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3496 | EXPECT_EQ(kDefaultMaxSockets - 1, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3497 | static_cast<int>(pool_->NumUnassignedConnectJobsInGroupForTesting( |
| 3498 | TestGroupId("a")))); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 3499 | EXPECT_FALSE(pool_->IsStalled()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3500 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3501 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("b"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3502 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3503 | pool_->RequestSockets(TestGroupId("b"), params_, kDefaultMaxSockets, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3504 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3505 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3506 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("b"))); |
| 3507 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 3508 | EXPECT_FALSE(pool_->IsStalled()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3509 | } |
| 3510 | |
| 3511 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsCountIdleSockets) { |
| 3512 | CreatePool(4, 4); |
| 3513 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3514 | |
| 3515 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3516 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3517 | EXPECT_EQ( |
| 3518 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3519 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3520 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3521 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3522 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3523 | ASSERT_THAT(callback1.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3524 | handle1.Reset(); |
| 3525 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3526 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3527 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3528 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3529 | TestGroupId("a"))); |
| 3530 | EXPECT_EQ(0u, |
| 3531 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3532 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3533 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3534 | pool_->RequestSockets(TestGroupId("a"), params_, 2, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3535 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3536 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3537 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3538 | TestGroupId("a"))); |
| 3539 | EXPECT_EQ(1u, |
| 3540 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3541 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3542 | } |
| 3543 | |
| 3544 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsCountActiveSockets) { |
| 3545 | CreatePool(4, 4); |
| 3546 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3547 | |
| 3548 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3549 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3550 | EXPECT_EQ( |
| 3551 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3552 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3553 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3554 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3555 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3556 | ASSERT_THAT(callback1.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3557 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3558 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3559 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3560 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3561 | TestGroupId("a"))); |
| 3562 | EXPECT_EQ(0u, |
| 3563 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3564 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3565 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3566 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3567 | pool_->RequestSockets(TestGroupId("a"), params_, 2, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3568 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3569 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3570 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3571 | TestGroupId("a"))); |
| 3572 | EXPECT_EQ(1u, |
| 3573 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3574 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3575 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3576 | } |
| 3577 | |
| 3578 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsSynchronous) { |
| 3579 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3580 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 3581 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3582 | pool_->RequestSockets(TestGroupId("a"), params_, kDefaultMaxSocketsPerGroup, |
Charlie Harrison | 55ce608 | 2018-05-14 02:25:57 | [diff] [blame] | 3583 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3584 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3585 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3586 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3587 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3588 | TestGroupId("a"))); |
| 3589 | EXPECT_EQ(0u, |
| 3590 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3591 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3592 | static_cast<int>(pool_->IdleSocketCountInGroup(TestGroupId("a")))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3593 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3594 | pool_->RequestSockets(TestGroupId("b"), params_, kDefaultMaxSocketsPerGroup, |
Charlie Harrison | 55ce608 | 2018-05-14 02:25:57 | [diff] [blame] | 3595 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3596 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3597 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 3598 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3599 | TestGroupId("b"))); |
| 3600 | EXPECT_EQ(0u, |
| 3601 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3602 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3603 | static_cast<int>(pool_->IdleSocketCountInGroup(TestGroupId("b")))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3604 | } |
| 3605 | |
[email protected] | 3c819f52 | 2010-12-02 02:03:12 | [diff] [blame] | 3606 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsSynchronousError) { |
| 3607 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3608 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
| 3609 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3610 | pool_->RequestSockets(TestGroupId("a"), params_, kDefaultMaxSocketsPerGroup, |
Charlie Harrison | 55ce608 | 2018-05-14 02:25:57 | [diff] [blame] | 3611 | NetLogWithSource()); |
[email protected] | 3c819f52 | 2010-12-02 02:03:12 | [diff] [blame] | 3612 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3613 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | fd2e53e | 2011-01-14 20:40:52 | [diff] [blame] | 3614 | |
| 3615 | connect_job_factory_->set_job_type( |
| 3616 | TestConnectJob::kMockAdditionalErrorStateJob); |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3617 | pool_->RequestSockets(TestGroupId("a"), params_, kDefaultMaxSocketsPerGroup, |
Charlie Harrison | 55ce608 | 2018-05-14 02:25:57 | [diff] [blame] | 3618 | NetLogWithSource()); |
[email protected] | fd2e53e | 2011-01-14 20:40:52 | [diff] [blame] | 3619 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3620 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | 3c819f52 | 2010-12-02 02:03:12 | [diff] [blame] | 3621 | } |
| 3622 | |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3623 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsMultipleTimesDoesNothing) { |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3624 | CreatePool(4, 4); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3625 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3626 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3627 | pool_->RequestSockets(TestGroupId("a"), params_, 2, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3628 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3629 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3630 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3631 | EXPECT_EQ(2u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3632 | TestGroupId("a"))); |
| 3633 | EXPECT_EQ(2u, |
| 3634 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3635 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3636 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3637 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3638 | pool_->RequestSockets(TestGroupId("a"), params_, 2, NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3639 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3640 | EXPECT_EQ(2u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3641 | TestGroupId("a"))); |
| 3642 | EXPECT_EQ(2u, |
| 3643 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3644 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3645 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3646 | |
| 3647 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3648 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3649 | EXPECT_EQ( |
| 3650 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3651 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3652 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3653 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3654 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3655 | |
| 3656 | client_socket_factory_.SignalJob(0); |
| 3657 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3658 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3659 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3660 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3661 | TestGroupId("a"))); |
| 3662 | EXPECT_EQ(1u, |
| 3663 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3664 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3665 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3666 | |
| 3667 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3668 | TestCompletionCallback callback2; |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3669 | EXPECT_EQ( |
| 3670 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3671 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3672 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3673 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3674 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3675 | client_socket_factory_.SignalJob(0); |
| 3676 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3677 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3678 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3679 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3680 | TestGroupId("a"))); |
| 3681 | EXPECT_EQ(0u, |
| 3682 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3683 | EXPECT_EQ(2, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3684 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3685 | |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3686 | handle1.Reset(); |
| 3687 | handle2.Reset(); |
| 3688 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3689 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3690 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3691 | TestGroupId("a"))); |
| 3692 | EXPECT_EQ(0u, |
| 3693 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3694 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3695 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3696 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3697 | pool_->RequestSockets(TestGroupId("a"), params_, 2, NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3698 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3699 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3700 | TestGroupId("a"))); |
| 3701 | EXPECT_EQ(0u, |
| 3702 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3703 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3704 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3705 | } |
| 3706 | |
| 3707 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsDifferentNumSockets) { |
| 3708 | CreatePool(4, 4); |
| 3709 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3710 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3711 | pool_->RequestSockets(TestGroupId("a"), params_, 1, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3712 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3713 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3714 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3715 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3716 | TestGroupId("a"))); |
| 3717 | EXPECT_EQ(1u, |
| 3718 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3719 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3720 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3721 | pool_->RequestSockets(TestGroupId("a"), params_, 2, NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3722 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3723 | EXPECT_EQ(2u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3724 | TestGroupId("a"))); |
| 3725 | EXPECT_EQ(2u, |
| 3726 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3727 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3728 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3729 | pool_->RequestSockets(TestGroupId("a"), params_, 3, NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3730 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3731 | EXPECT_EQ(3u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3732 | TestGroupId("a"))); |
| 3733 | EXPECT_EQ(3u, |
| 3734 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3735 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3736 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3737 | pool_->RequestSockets(TestGroupId("a"), params_, 1, NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3738 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3739 | EXPECT_EQ(3u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3740 | TestGroupId("a"))); |
| 3741 | EXPECT_EQ(3u, |
| 3742 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3743 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3744 | } |
| 3745 | |
| 3746 | TEST_F(ClientSocketPoolBaseTest, PreconnectJobsTakenByNormalRequests) { |
| 3747 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3748 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3749 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3750 | pool_->RequestSockets(TestGroupId("a"), params_, 1, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3751 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3752 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3753 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3754 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3755 | TestGroupId("a"))); |
| 3756 | EXPECT_EQ(1u, |
| 3757 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3758 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3759 | |
| 3760 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3761 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3762 | EXPECT_EQ( |
| 3763 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3764 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3765 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3766 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3767 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3768 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3769 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3770 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3771 | TestGroupId("a"))); |
| 3772 | EXPECT_EQ(0u, |
| 3773 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3774 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3775 | |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3776 | client_socket_factory_.SignalJobs(); |
| 3777 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3778 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3779 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3780 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3781 | TestGroupId("a"))); |
| 3782 | EXPECT_EQ(0u, |
| 3783 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3784 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3785 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3786 | |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 3787 | // Make sure if a preconnected socket is not fully connected when a request |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 3788 | // starts, it has a connect start time. |
| 3789 | TestLoadTimingInfoConnectedNotReused(handle1); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3790 | handle1.Reset(); |
| 3791 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3792 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3793 | } |
| 3794 | |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 3795 | // Checks that fully connected preconnect jobs have no connect times, and are |
| 3796 | // marked as reused. |
| 3797 | TEST_F(ClientSocketPoolBaseTest, ConnectedPreconnectJobsHaveNoConnectTimes) { |
| 3798 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3799 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3800 | pool_->RequestSockets(TestGroupId("a"), params_, 1, NetLogWithSource()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 3801 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3802 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3803 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3804 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3805 | TestGroupId("a"))); |
| 3806 | EXPECT_EQ(0u, |
| 3807 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3808 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 3809 | |
| 3810 | ClientSocketHandle handle; |
| 3811 | TestCompletionCallback callback; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3812 | EXPECT_EQ(OK, handle.Init( |
| 3813 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
| 3814 | ClientSocketPool::RespectLimits::ENABLED, |
| 3815 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3816 | pool_.get(), NetLogWithSource())); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 3817 | |
| 3818 | // Make sure the idle socket was used. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3819 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 3820 | |
| 3821 | TestLoadTimingInfoConnectedReused(handle); |
| 3822 | handle.Reset(); |
| 3823 | TestLoadTimingInfoNotConnected(handle); |
| 3824 | } |
| 3825 | |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3826 | // http://crbug.com/64940 regression test. |
| 3827 | TEST_F(ClientSocketPoolBaseTest, PreconnectClosesIdleSocketRemovesGroup) { |
| 3828 | const int kMaxTotalSockets = 3; |
| 3829 | const int kMaxSocketsPerGroup = 2; |
| 3830 | CreatePool(kMaxTotalSockets, kMaxSocketsPerGroup); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3831 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3832 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 3833 | // Note that group id ordering matters here. "a" comes before "b", so |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3834 | // CloseOneIdleSocket() will try to close "a"'s idle socket. |
| 3835 | |
| 3836 | // Set up one idle socket in "a". |
| 3837 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3838 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3839 | EXPECT_EQ( |
| 3840 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3841 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3842 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3843 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3844 | pool_.get(), NetLogWithSource())); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3845 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3846 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3847 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3848 | TestGroupId("a"))); |
| 3849 | EXPECT_EQ(0u, |
| 3850 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3851 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3852 | |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3853 | client_socket_factory_.SignalJobs(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3854 | ASSERT_THAT(callback1.WaitForResult(), IsOk()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3855 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3856 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3857 | TestGroupId("a"))); |
| 3858 | EXPECT_EQ(0u, |
| 3859 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3860 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3861 | |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3862 | handle1.Reset(); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3863 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3864 | |
| 3865 | // Set up two active sockets in "b". |
| 3866 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3867 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3868 | EXPECT_EQ( |
| 3869 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3870 | handle1.Init(TestGroupId("b"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3871 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3872 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3873 | pool_.get(), NetLogWithSource())); |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3874 | EXPECT_EQ( |
| 3875 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3876 | handle2.Init(TestGroupId("b"), params_, DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3877 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3878 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3879 | pool_.get(), NetLogWithSource())); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3880 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3881 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("b"))); |
| 3882 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 3883 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3884 | TestGroupId("b"))); |
| 3885 | EXPECT_EQ(0u, |
| 3886 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3887 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3888 | |
| 3889 | client_socket_factory_.SignalJobs(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3890 | ASSERT_THAT(callback1.WaitForResult(), IsOk()); |
| 3891 | ASSERT_THAT(callback2.WaitForResult(), IsOk()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3892 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3893 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3894 | TestGroupId("b"))); |
| 3895 | EXPECT_EQ(0u, |
| 3896 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 3897 | EXPECT_EQ(2, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3898 | |
| 3899 | // Now we have 1 idle socket in "a" and 2 active sockets in "b". This means |
| 3900 | // we've maxed out on sockets, since we set |kMaxTotalSockets| to 3. |
| 3901 | // Requesting 2 preconnected sockets for "a" should fail to allocate any more |
| 3902 | // sockets for "a", and "b" should still have 2 active sockets. |
| 3903 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3904 | pool_->RequestSockets(TestGroupId("a"), params_, 2, NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3905 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3906 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3907 | TestGroupId("a"))); |
| 3908 | EXPECT_EQ(0u, |
| 3909 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3910 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3911 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 3912 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 3913 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3914 | TestGroupId("b"))); |
| 3915 | EXPECT_EQ(0u, |
| 3916 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3917 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3918 | EXPECT_EQ(2, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3919 | |
| 3920 | // Now release the 2 active sockets for "b". This will give us 1 idle socket |
| 3921 | // in "a" and 2 idle sockets in "b". Requesting 2 preconnected sockets for |
| 3922 | // "a" should result in closing 1 for "b". |
| 3923 | handle1.Reset(); |
| 3924 | handle2.Reset(); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3925 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3926 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3927 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3928 | pool_->RequestSockets(TestGroupId("a"), params_, 2, NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3929 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3930 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3931 | TestGroupId("a"))); |
| 3932 | EXPECT_EQ(1u, |
| 3933 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3934 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3935 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 3936 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 3937 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3938 | TestGroupId("b"))); |
| 3939 | EXPECT_EQ(0u, |
| 3940 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3941 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3942 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3943 | } |
| 3944 | |
[email protected] | b7b8be4 | 2011-07-12 12:46:41 | [diff] [blame] | 3945 | TEST_F(ClientSocketPoolBaseTest, PreconnectWithoutBackupJob) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3946 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 3947 | true /* enable_backup_connect_jobs */); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 3948 | |
| 3949 | // Make the ConnectJob hang until it times out, shorten the timeout. |
| 3950 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3951 | connect_job_factory_->set_timeout_duration( |
| 3952 | base::TimeDelta::FromMilliseconds(500)); |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3953 | pool_->RequestSockets(TestGroupId("a"), params_, 1, NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3954 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3955 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3956 | TestGroupId("a"))); |
| 3957 | EXPECT_EQ(1u, |
| 3958 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3959 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 3960 | |
[email protected] | b7b8be4 | 2011-07-12 12:46:41 | [diff] [blame] | 3961 | // Verify the backup timer doesn't create a backup job, by making |
| 3962 | // the backup job a pending job instead of a waiting job, so it |
| 3963 | // *would* complete if it were created. |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 3964 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 3965 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
Gabriel Charette | ea91801 | 2018-05-16 11:53:44 | [diff] [blame] | 3966 | FROM_HERE, base::RunLoop::QuitCurrentWhenIdleClosureDeprecated(), |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 3967 | base::TimeDelta::FromSeconds(1)); |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3968 | base::RunLoop().Run(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3969 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 3970 | } |
| 3971 | |
[email protected] | b7b8be4 | 2011-07-12 12:46:41 | [diff] [blame] | 3972 | TEST_F(ClientSocketPoolBaseTest, PreconnectWithBackupJob) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3973 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 3974 | true /* enable_backup_connect_jobs */); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 3975 | |
| 3976 | // Make the ConnectJob hang forever. |
| 3977 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 3978 | pool_->RequestSockets(TestGroupId("a"), params_, 1, NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3979 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3980 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3981 | TestGroupId("a"))); |
| 3982 | EXPECT_EQ(1u, |
| 3983 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3984 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3985 | base::RunLoop().RunUntilIdle(); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 3986 | |
| 3987 | // Make the backup job be a pending job, so it completes normally. |
| 3988 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3989 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3990 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3991 | EXPECT_EQ( |
| 3992 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3993 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3994 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 3995 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 3996 | NetLogWithSource())); |
[email protected] | b7b8be4 | 2011-07-12 12:46:41 | [diff] [blame] | 3997 | // Timer has started, but the backup connect job shouldn't be created yet. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3998 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3999 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4000 | TestGroupId("a"))); |
| 4001 | EXPECT_EQ(0u, |
| 4002 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4003 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4004 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4005 | ASSERT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4006 | |
| 4007 | // The hung connect job should still be there, but everything else should be |
| 4008 | // complete. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4009 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4010 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4011 | TestGroupId("a"))); |
| 4012 | EXPECT_EQ(1u, |
| 4013 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4014 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4015 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4016 | } |
| 4017 | |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4018 | // Tests that a preconnect that starts out with unread data can still be used. |
| 4019 | // http://crbug.com/334467 |
| 4020 | TEST_F(ClientSocketPoolBaseTest, PreconnectWithUnreadData) { |
| 4021 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4022 | connect_job_factory_->set_job_type(TestConnectJob::kMockUnreadDataJob); |
| 4023 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 4024 | pool_->RequestSockets(TestGroupId("a"), params_, 1, NetLogWithSource()); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4025 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4026 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 4027 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4028 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4029 | TestGroupId("a"))); |
| 4030 | EXPECT_EQ(0u, |
| 4031 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4032 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4033 | |
| 4034 | // Fail future jobs to be sure that handle receives the preconnected socket |
| 4035 | // rather than closing it and making a new one. |
| 4036 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
| 4037 | ClientSocketHandle handle; |
| 4038 | TestCompletionCallback callback; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4039 | EXPECT_EQ(OK, handle.Init( |
| 4040 | TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
| 4041 | ClientSocketPool::RespectLimits::ENABLED, |
| 4042 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4043 | pool_.get(), NetLogWithSource())); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4044 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4045 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 4046 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4047 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4048 | TestGroupId("a"))); |
| 4049 | EXPECT_EQ(0u, |
| 4050 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4051 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4052 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4053 | |
| 4054 | // Drain the pending read. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 4055 | EXPECT_EQ(1, handle.socket()->Read(nullptr, 1, CompletionOnceCallback())); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4056 | |
| 4057 | TestLoadTimingInfoConnectedReused(handle); |
| 4058 | handle.Reset(); |
| 4059 | |
| 4060 | // The socket should be usable now that it's idle again. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4061 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4062 | } |
| 4063 | |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4064 | TEST_F(ClientSocketPoolBaseTest, RequestGetsAssignedJob) { |
| 4065 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4066 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4067 | |
| 4068 | ClientSocketHandle handle1; |
| 4069 | TestCompletionCallback callback1; |
| 4070 | EXPECT_EQ( |
| 4071 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4072 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4073 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4074 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4075 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4076 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4077 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4078 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4079 | TestGroupId("a"))); |
| 4080 | EXPECT_EQ(0u, |
| 4081 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4082 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4083 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4084 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4085 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4086 | } |
| 4087 | |
| 4088 | TEST_F(ClientSocketPoolBaseTest, MultipleRequestsGetAssignedJobs) { |
| 4089 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4090 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4091 | |
| 4092 | ClientSocketHandle handle1; |
| 4093 | TestCompletionCallback callback1; |
| 4094 | EXPECT_EQ( |
| 4095 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4096 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4097 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4098 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4099 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4100 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4101 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4102 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4103 | TestGroupId("a"))); |
| 4104 | EXPECT_EQ(0u, |
| 4105 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4106 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4107 | |
| 4108 | ClientSocketHandle handle2; |
| 4109 | TestCompletionCallback callback2; |
| 4110 | EXPECT_EQ( |
| 4111 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4112 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4113 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4114 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4115 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4116 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4117 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4118 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4119 | TestGroupId("a"))); |
| 4120 | EXPECT_EQ(0u, |
| 4121 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4122 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4123 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4124 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4125 | &handle1)); |
| 4126 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4127 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4128 | |
| 4129 | // One job completes. The other request should still have its job. |
| 4130 | client_socket_factory_.SignalJob(0); |
| 4131 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 4132 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4133 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4134 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4135 | TestGroupId("a"))); |
| 4136 | EXPECT_EQ(0u, |
| 4137 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4138 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4139 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4140 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4141 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4142 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4143 | } |
| 4144 | |
| 4145 | TEST_F(ClientSocketPoolBaseTest, PreconnectJobGetsAssignedToRequest) { |
| 4146 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4147 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4148 | |
Matt Menke | bd12b7e | 2019-03-25 21:12:03 | [diff] [blame] | 4149 | pool_->RequestSockets(TestGroupId("a"), params_, 1, NetLogWithSource()); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4150 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4151 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 4152 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4153 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4154 | TestGroupId("a"))); |
| 4155 | EXPECT_EQ(1u, |
| 4156 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4157 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4158 | |
| 4159 | ClientSocketHandle handle1; |
| 4160 | TestCompletionCallback callback1; |
| 4161 | EXPECT_EQ( |
| 4162 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4163 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4164 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4165 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4166 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4167 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4168 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4169 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4170 | TestGroupId("a"))); |
| 4171 | EXPECT_EQ(0u, |
| 4172 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4173 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4174 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4175 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4176 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4177 | } |
| 4178 | |
| 4179 | TEST_F(ClientSocketPoolBaseTest, HigherPriorityRequestStealsJob) { |
| 4180 | CreatePool(kDefaultMaxSockets, 1); |
| 4181 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4182 | |
| 4183 | ClientSocketHandle handle1; |
| 4184 | TestCompletionCallback callback1; |
| 4185 | EXPECT_EQ( |
| 4186 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4187 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4188 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4189 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4190 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4191 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4192 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4193 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4194 | TestGroupId("a"))); |
| 4195 | EXPECT_EQ(0u, |
| 4196 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4197 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4198 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4199 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4200 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4201 | |
| 4202 | // Insert a higher priority request |
| 4203 | ClientSocketHandle handle2; |
| 4204 | TestCompletionCallback callback2; |
| 4205 | EXPECT_EQ( |
| 4206 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4207 | handle2.Init(TestGroupId("a"), params_, HIGHEST, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4208 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4209 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4210 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4211 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4212 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4213 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4214 | TestGroupId("a"))); |
| 4215 | EXPECT_EQ(0u, |
| 4216 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4217 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4218 | |
| 4219 | // The highest priority request should steal the job from the default priority |
| 4220 | // request. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4221 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4222 | &handle2)); |
| 4223 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4224 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4225 | } |
| 4226 | |
| 4227 | TEST_F(ClientSocketPoolBaseTest, RequestStealsJobFromLowestRequestWithJob) { |
| 4228 | CreatePool(3, 3); |
| 4229 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4230 | |
| 4231 | ClientSocketHandle handle_lowest; |
| 4232 | TestCompletionCallback callback_lowest; |
| 4233 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4234 | handle_lowest.Init(TestGroupId("a"), params_, LOWEST, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4235 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4236 | callback_lowest.callback(), |
| 4237 | ClientSocketPool::ProxyAuthCallback(), |
| 4238 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4239 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4240 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4241 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4242 | TestGroupId("a"))); |
| 4243 | EXPECT_EQ(0u, |
| 4244 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4245 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4246 | |
| 4247 | ClientSocketHandle handle_highest; |
| 4248 | TestCompletionCallback callback_highest; |
| 4249 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4250 | handle_highest.Init(TestGroupId("a"), params_, HIGHEST, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4251 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4252 | callback_highest.callback(), |
| 4253 | ClientSocketPool::ProxyAuthCallback(), |
| 4254 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4255 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4256 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4257 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4258 | TestGroupId("a"))); |
| 4259 | EXPECT_EQ(0u, |
| 4260 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4261 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4262 | |
| 4263 | ClientSocketHandle handle_low; |
| 4264 | TestCompletionCallback callback_low; |
| 4265 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4266 | handle_low.Init(TestGroupId("a"), params_, LOW, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4267 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4268 | callback_low.callback(), |
| 4269 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4270 | NetLogWithSource())); |
| 4271 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4272 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4273 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4274 | TestGroupId("a"))); |
| 4275 | EXPECT_EQ(0u, |
| 4276 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4277 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4278 | |
| 4279 | ClientSocketHandle handle_lowest2; |
| 4280 | TestCompletionCallback callback_lowest2; |
| 4281 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4282 | handle_lowest2.Init(TestGroupId("a"), params_, LOWEST, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4283 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4284 | callback_lowest2.callback(), |
| 4285 | ClientSocketPool::ProxyAuthCallback(), |
| 4286 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4287 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4288 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4289 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4290 | TestGroupId("a"))); |
| 4291 | EXPECT_EQ(0u, |
| 4292 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4293 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4294 | |
| 4295 | // The top three requests in the queue should have jobs. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4296 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4297 | &handle_highest)); |
| 4298 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4299 | &handle_low)); |
| 4300 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4301 | &handle_lowest)); |
| 4302 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting( |
| 4303 | TestGroupId("a"), &handle_lowest2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4304 | |
| 4305 | // Add another request with medium priority. It should steal the job from the |
| 4306 | // lowest priority request with a job. |
| 4307 | ClientSocketHandle handle_medium; |
| 4308 | TestCompletionCallback callback_medium; |
| 4309 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4310 | handle_medium.Init(TestGroupId("a"), params_, MEDIUM, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4311 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4312 | callback_medium.callback(), |
| 4313 | ClientSocketPool::ProxyAuthCallback(), |
| 4314 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4315 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4316 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4317 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4318 | TestGroupId("a"))); |
| 4319 | EXPECT_EQ(0u, |
| 4320 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4321 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
| 4322 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4323 | &handle_highest)); |
| 4324 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4325 | &handle_medium)); |
| 4326 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4327 | &handle_low)); |
| 4328 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4329 | &handle_lowest)); |
| 4330 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting( |
| 4331 | TestGroupId("a"), &handle_lowest2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4332 | } |
| 4333 | |
| 4334 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeRequestStealsJob) { |
| 4335 | CreatePool(kDefaultMaxSockets, 1); |
| 4336 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4337 | |
| 4338 | ClientSocketHandle handle1; |
| 4339 | TestCompletionCallback callback1; |
| 4340 | EXPECT_EQ( |
| 4341 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4342 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4343 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4344 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4345 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4346 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4347 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4348 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4349 | TestGroupId("a"))); |
| 4350 | EXPECT_EQ(0u, |
| 4351 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4352 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4353 | |
| 4354 | ClientSocketHandle handle2; |
| 4355 | TestCompletionCallback callback2; |
| 4356 | EXPECT_EQ( |
| 4357 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4358 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4359 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4360 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4361 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4362 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4363 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4364 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4365 | TestGroupId("a"))); |
| 4366 | EXPECT_EQ(0u, |
| 4367 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4368 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4369 | |
| 4370 | // The second request doesn't get a job because we are at the limit. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4371 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4372 | &handle1)); |
| 4373 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4374 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4375 | |
| 4376 | // Reprioritizing the second request places it above the first, and it steals |
| 4377 | // the job from the first request. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4378 | pool_->SetPriority(TestGroupId("a"), &handle2, HIGHEST); |
| 4379 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4380 | &handle2)); |
| 4381 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4382 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4383 | } |
| 4384 | |
| 4385 | TEST_F(ClientSocketPoolBaseTest, CancelRequestReassignsJob) { |
| 4386 | CreatePool(kDefaultMaxSockets, 1); |
| 4387 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4388 | |
| 4389 | ClientSocketHandle handle1; |
| 4390 | TestCompletionCallback callback1; |
| 4391 | EXPECT_EQ( |
| 4392 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4393 | handle1.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4394 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4395 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4396 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4397 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4398 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4399 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4400 | TestGroupId("a"))); |
| 4401 | EXPECT_EQ(0u, |
| 4402 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4403 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4404 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4405 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4406 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4407 | |
| 4408 | ClientSocketHandle handle2; |
| 4409 | TestCompletionCallback callback2; |
| 4410 | EXPECT_EQ( |
| 4411 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4412 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4413 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4414 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4415 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4416 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4417 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4418 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4419 | TestGroupId("a"))); |
| 4420 | EXPECT_EQ(0u, |
| 4421 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4422 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4423 | |
| 4424 | // The second request doesn't get a job because we are the limit. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4425 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4426 | &handle1)); |
| 4427 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4428 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4429 | |
| 4430 | // The second request should get a job upon cancelling the first request. |
| 4431 | handle1.Reset(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4432 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4433 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4434 | TestGroupId("a"))); |
| 4435 | EXPECT_EQ(0u, |
| 4436 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4437 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4438 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4439 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4440 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4441 | } |
| 4442 | |
| 4443 | TEST_F(ClientSocketPoolBaseTest, JobCompletionReassignsJob) { |
| 4444 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4445 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4446 | |
| 4447 | ClientSocketHandle handle1; |
| 4448 | TestCompletionCallback callback1; |
| 4449 | EXPECT_EQ( |
| 4450 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4451 | handle1.Init(TestGroupId("a"), params_, HIGHEST, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4452 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4453 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4454 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4455 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4456 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4457 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4458 | TestGroupId("a"))); |
| 4459 | EXPECT_EQ(0u, |
| 4460 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4461 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4462 | |
| 4463 | ClientSocketHandle handle2; |
| 4464 | TestCompletionCallback callback2; |
| 4465 | EXPECT_EQ( |
| 4466 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4467 | handle2.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4468 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4469 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4470 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4471 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4472 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4473 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4474 | TestGroupId("a"))); |
| 4475 | EXPECT_EQ(0u, |
| 4476 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4477 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4478 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4479 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4480 | &handle1)); |
| 4481 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4482 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4483 | |
| 4484 | // The lower-priority job completes first. The higher-priority request should |
| 4485 | // get the socket, and the lower-priority request should get the remaining |
| 4486 | // job. |
| 4487 | client_socket_factory_.SignalJob(1); |
| 4488 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4489 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4490 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4491 | TestGroupId("a"))); |
| 4492 | EXPECT_EQ(0u, |
| 4493 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4494 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4495 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4496 | EXPECT_TRUE(handle1.socket()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4497 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4498 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4499 | } |
| 4500 | |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 4501 | class MockLayeredPool : public HigherLayeredPool { |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4502 | public: |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4503 | MockLayeredPool(TransportClientSocketPool* pool, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4504 | const ClientSocketPool::GroupId& group_id) |
| 4505 | : pool_(pool), group_id_(group_id), can_release_connection_(true) { |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 4506 | pool_->AddHigherLayeredPool(this); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4507 | } |
| 4508 | |
Daniel Cheng | 4496d082 | 2018-04-26 21:52:15 | [diff] [blame] | 4509 | ~MockLayeredPool() override { pool_->RemoveHigherLayeredPool(this); } |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4510 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4511 | int RequestSocket(TransportClientSocketPool* pool) { |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4512 | return handle_.Init( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4513 | group_id_, CreateDummyParams(), DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4514 | ClientSocketPool::RespectLimits::ENABLED, callback_.callback(), |
| 4515 | ClientSocketPool::ProxyAuthCallback(), pool, NetLogWithSource()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4516 | } |
| 4517 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4518 | int RequestSocketWithoutLimits(TransportClientSocketPool* pool) { |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4519 | return handle_.Init( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4520 | group_id_, CreateDummyParams(), MAXIMUM_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4521 | ClientSocketPool::RespectLimits::DISABLED, callback_.callback(), |
| 4522 | ClientSocketPool::ProxyAuthCallback(), pool, NetLogWithSource()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4523 | } |
| 4524 | |
| 4525 | bool ReleaseOneConnection() { |
| 4526 | if (!handle_.is_initialized() || !can_release_connection_) { |
| 4527 | return false; |
| 4528 | } |
| 4529 | handle_.socket()->Disconnect(); |
| 4530 | handle_.Reset(); |
| 4531 | return true; |
| 4532 | } |
| 4533 | |
| 4534 | void set_can_release_connection(bool can_release_connection) { |
| 4535 | can_release_connection_ = can_release_connection; |
| 4536 | } |
| 4537 | |
| 4538 | MOCK_METHOD0(CloseOneIdleConnection, bool()); |
| 4539 | |
| 4540 | private: |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4541 | TransportClientSocketPool* const pool_; |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4542 | ClientSocketHandle handle_; |
| 4543 | TestCompletionCallback callback_; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4544 | const ClientSocketPool::GroupId group_id_; |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4545 | bool can_release_connection_; |
| 4546 | }; |
| 4547 | |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4548 | // Tests the basic case of closing an idle socket in a higher layered pool when |
| 4549 | // a new request is issued and the lower layer pool is stalled. |
| 4550 | TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketsHeldByLayeredPoolWhenNeeded) { |
| 4551 | CreatePool(1, 1); |
| 4552 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 4553 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4554 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("foo")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4555 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4556 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4557 | .WillOnce(Invoke(&mock_layered_pool, |
| 4558 | &MockLayeredPool::ReleaseOneConnection)); |
| 4559 | ClientSocketHandle handle; |
| 4560 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4561 | EXPECT_EQ( |
| 4562 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4563 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4564 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 4565 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4566 | NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4567 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4568 | } |
| 4569 | |
Matt Menke | 83367864 | 2019-03-05 22:05:51 | [diff] [blame] | 4570 | // Tests the case that trying to close an idle socket in a higher layered pool |
| 4571 | // fails. |
| 4572 | TEST_F(ClientSocketPoolBaseTest, |
| 4573 | CloseIdleSocketsHeldByLayeredPoolWhenNeededFails) { |
| 4574 | CreatePool(1, 1); |
| 4575 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 4576 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4577 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("foo")); |
Matt Menke | 83367864 | 2019-03-05 22:05:51 | [diff] [blame] | 4578 | mock_layered_pool.set_can_release_connection(false); |
| 4579 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
| 4580 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4581 | .WillOnce(Invoke(&mock_layered_pool, |
| 4582 | &MockLayeredPool::ReleaseOneConnection)); |
| 4583 | ClientSocketHandle handle; |
| 4584 | TestCompletionCallback callback; |
| 4585 | EXPECT_EQ( |
| 4586 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4587 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 83367864 | 2019-03-05 22:05:51 | [diff] [blame] | 4588 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 4589 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4590 | NetLogWithSource())); |
| 4591 | base::RunLoop().RunUntilIdle(); |
| 4592 | EXPECT_FALSE(callback.have_result()); |
| 4593 | } |
| 4594 | |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4595 | // Same as above, but the idle socket is in the same group as the stalled |
| 4596 | // socket, and closes the only other request in its group when closing requests |
| 4597 | // in higher layered pools. This generally shouldn't happen, but it may be |
| 4598 | // possible if a higher level pool issues a request and the request is |
| 4599 | // subsequently cancelled. Even if it's not possible, best not to crash. |
| 4600 | TEST_F(ClientSocketPoolBaseTest, |
| 4601 | CloseIdleSocketsHeldByLayeredPoolWhenNeededSameGroup) { |
| 4602 | CreatePool(2, 2); |
| 4603 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 4604 | |
| 4605 | // Need a socket in another group for the pool to be stalled (If a group |
| 4606 | // has the maximum number of connections already, it's not stalled). |
| 4607 | ClientSocketHandle handle1; |
| 4608 | TestCompletionCallback callback1; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4609 | EXPECT_EQ(OK, handle1.Init( |
| 4610 | TestGroupId("group1"), params_, DEFAULT_PRIORITY, |
| 4611 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4612 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4613 | pool_.get(), NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4614 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4615 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("group2")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4616 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4617 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4618 | .WillOnce(Invoke(&mock_layered_pool, |
| 4619 | &MockLayeredPool::ReleaseOneConnection)); |
| 4620 | ClientSocketHandle handle; |
| 4621 | TestCompletionCallback callback2; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4622 | EXPECT_EQ( |
| 4623 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4624 | handle.Init(TestGroupId("group2"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4625 | ClientSocketPool::RespectLimits::ENABLED, |
| 4626 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4627 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4628 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4629 | } |
| 4630 | |
| 4631 | // Tests the case when an idle socket can be closed when a new request is |
| 4632 | // issued, and the new request belongs to a group that was previously stalled. |
| 4633 | TEST_F(ClientSocketPoolBaseTest, |
| 4634 | CloseIdleSocketsHeldByLayeredPoolInSameGroupWhenNeeded) { |
| 4635 | CreatePool(2, 2); |
| 4636 | std::list<TestConnectJob::JobType> job_types; |
| 4637 | job_types.push_back(TestConnectJob::kMockJob); |
| 4638 | job_types.push_back(TestConnectJob::kMockJob); |
| 4639 | job_types.push_back(TestConnectJob::kMockJob); |
| 4640 | job_types.push_back(TestConnectJob::kMockJob); |
| 4641 | connect_job_factory_->set_job_types(&job_types); |
| 4642 | |
| 4643 | ClientSocketHandle handle1; |
| 4644 | TestCompletionCallback callback1; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4645 | EXPECT_EQ(OK, handle1.Init( |
| 4646 | TestGroupId("group1"), params_, DEFAULT_PRIORITY, |
| 4647 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4648 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4649 | pool_.get(), NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4650 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4651 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("group2")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4652 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4653 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4654 | .WillRepeatedly(Invoke(&mock_layered_pool, |
| 4655 | &MockLayeredPool::ReleaseOneConnection)); |
| 4656 | mock_layered_pool.set_can_release_connection(false); |
| 4657 | |
| 4658 | // The third request is made when the socket pool is in a stalled state. |
| 4659 | ClientSocketHandle handle3; |
| 4660 | TestCompletionCallback callback3; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4661 | EXPECT_EQ( |
| 4662 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4663 | handle3.Init(TestGroupId("group3"), params_, DEFAULT_PRIORITY, |
| 4664 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4665 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4666 | pool_.get(), NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4667 | |
| 4668 | base::RunLoop().RunUntilIdle(); |
| 4669 | EXPECT_FALSE(callback3.have_result()); |
| 4670 | |
| 4671 | // The fourth request is made when the pool is no longer stalled. The third |
| 4672 | // request should be serviced first, since it was issued first and has the |
| 4673 | // same priority. |
| 4674 | mock_layered_pool.set_can_release_connection(true); |
| 4675 | ClientSocketHandle handle4; |
| 4676 | TestCompletionCallback callback4; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4677 | EXPECT_EQ( |
| 4678 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4679 | handle4.Init(TestGroupId("group3"), params_, DEFAULT_PRIORITY, |
| 4680 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4681 | callback4.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4682 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4683 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4684 | EXPECT_FALSE(callback4.have_result()); |
| 4685 | |
| 4686 | // Closing a handle should free up another socket slot. |
| 4687 | handle1.Reset(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4688 | EXPECT_THAT(callback4.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4689 | } |
| 4690 | |
| 4691 | // Tests the case when an idle socket can be closed when a new request is |
| 4692 | // issued, and the new request belongs to a group that was previously stalled. |
| 4693 | // |
| 4694 | // The two differences from the above test are that the stalled requests are not |
| 4695 | // in the same group as the layered pool's request, and the the fourth request |
| 4696 | // has a higher priority than the third one, so gets a socket first. |
| 4697 | TEST_F(ClientSocketPoolBaseTest, |
| 4698 | CloseIdleSocketsHeldByLayeredPoolInSameGroupWhenNeeded2) { |
| 4699 | CreatePool(2, 2); |
| 4700 | std::list<TestConnectJob::JobType> job_types; |
| 4701 | job_types.push_back(TestConnectJob::kMockJob); |
| 4702 | job_types.push_back(TestConnectJob::kMockJob); |
| 4703 | job_types.push_back(TestConnectJob::kMockJob); |
| 4704 | job_types.push_back(TestConnectJob::kMockJob); |
| 4705 | connect_job_factory_->set_job_types(&job_types); |
| 4706 | |
| 4707 | ClientSocketHandle handle1; |
| 4708 | TestCompletionCallback callback1; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4709 | EXPECT_EQ(OK, handle1.Init( |
| 4710 | TestGroupId("group1"), params_, DEFAULT_PRIORITY, |
| 4711 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4712 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4713 | pool_.get(), NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4714 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4715 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("group2")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4716 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4717 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4718 | .WillRepeatedly(Invoke(&mock_layered_pool, |
| 4719 | &MockLayeredPool::ReleaseOneConnection)); |
| 4720 | mock_layered_pool.set_can_release_connection(false); |
| 4721 | |
| 4722 | // The third request is made when the socket pool is in a stalled state. |
| 4723 | ClientSocketHandle handle3; |
| 4724 | TestCompletionCallback callback3; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4725 | EXPECT_EQ( |
| 4726 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4727 | handle3.Init(TestGroupId("group3"), params_, MEDIUM, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4728 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4729 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4730 | pool_.get(), NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4731 | |
| 4732 | base::RunLoop().RunUntilIdle(); |
| 4733 | EXPECT_FALSE(callback3.have_result()); |
| 4734 | |
| 4735 | // The fourth request is made when the pool is no longer stalled. This |
| 4736 | // request has a higher priority than the third request, so is serviced first. |
| 4737 | mock_layered_pool.set_can_release_connection(true); |
| 4738 | ClientSocketHandle handle4; |
| 4739 | TestCompletionCallback callback4; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4740 | EXPECT_EQ( |
| 4741 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4742 | handle4.Init(TestGroupId("group3"), params_, HIGHEST, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4743 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4744 | callback4.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4745 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4746 | EXPECT_THAT(callback4.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4747 | EXPECT_FALSE(callback3.have_result()); |
| 4748 | |
| 4749 | // Closing a handle should free up another socket slot. |
| 4750 | handle1.Reset(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4751 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4752 | } |
| 4753 | |
| 4754 | TEST_F(ClientSocketPoolBaseTest, |
| 4755 | CloseMultipleIdleSocketsHeldByLayeredPoolWhenNeeded) { |
| 4756 | CreatePool(1, 1); |
| 4757 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 4758 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4759 | MockLayeredPool mock_layered_pool1(pool_.get(), TestGroupId("foo")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4760 | EXPECT_THAT(mock_layered_pool1.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4761 | EXPECT_CALL(mock_layered_pool1, CloseOneIdleConnection()) |
| 4762 | .WillRepeatedly(Invoke(&mock_layered_pool1, |
| 4763 | &MockLayeredPool::ReleaseOneConnection)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4764 | MockLayeredPool mock_layered_pool2(pool_.get(), TestGroupId("bar")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4765 | EXPECT_THAT(mock_layered_pool2.RequestSocketWithoutLimits(pool_.get()), |
| 4766 | IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4767 | EXPECT_CALL(mock_layered_pool2, CloseOneIdleConnection()) |
| 4768 | .WillRepeatedly(Invoke(&mock_layered_pool2, |
| 4769 | &MockLayeredPool::ReleaseOneConnection)); |
| 4770 | ClientSocketHandle handle; |
| 4771 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4772 | EXPECT_EQ( |
| 4773 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4774 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4775 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 4776 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4777 | NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4778 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4779 | } |
| 4780 | |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4781 | // Test that when a socket pool and group are at their limits, a request |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4782 | // with RespectLimits::DISABLED triggers creation of a new socket, and gets the |
| 4783 | // socket instead of a request with the same priority that was issued earlier, |
| 4784 | // but has RespectLimits::ENABLED. |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4785 | TEST_F(ClientSocketPoolBaseTest, IgnoreLimits) { |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4786 | CreatePool(1, 1); |
| 4787 | |
| 4788 | // Issue a request to reach the socket pool limit. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4789 | EXPECT_EQ(OK, StartRequestWithIgnoreLimits( |
| 4790 | TestGroupId("a"), MAXIMUM_PRIORITY, |
| 4791 | ClientSocketPool::RespectLimits::ENABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4792 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4793 | |
| 4794 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 4795 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4796 | EXPECT_EQ(ERR_IO_PENDING, StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4797 | TestGroupId("a"), MAXIMUM_PRIORITY, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4798 | ClientSocketPool::RespectLimits::ENABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4799 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4800 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4801 | // Issue a request that ignores the limits, so a new ConnectJob is |
| 4802 | // created. |
| 4803 | EXPECT_EQ(ERR_IO_PENDING, StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4804 | TestGroupId("a"), MAXIMUM_PRIORITY, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4805 | ClientSocketPool::RespectLimits::DISABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4806 | ASSERT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4807 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4808 | EXPECT_THAT(request(2)->WaitForResult(), IsOk()); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4809 | EXPECT_FALSE(request(1)->have_result()); |
| 4810 | } |
| 4811 | |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 4812 | // Test that when a socket pool and group are at their limits, a ConnectJob |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4813 | // issued for a request with RespectLimits::DISABLED is not cancelled when a |
| 4814 | // request with RespectLimits::ENABLED issued to the same group is cancelled. |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 4815 | TEST_F(ClientSocketPoolBaseTest, IgnoreLimitsCancelOtherJob) { |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 4816 | CreatePool(1, 1); |
| 4817 | |
| 4818 | // Issue a request to reach the socket pool limit. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4819 | EXPECT_EQ(OK, StartRequestWithIgnoreLimits( |
| 4820 | TestGroupId("a"), MAXIMUM_PRIORITY, |
| 4821 | ClientSocketPool::RespectLimits::ENABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4822 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 4823 | |
| 4824 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 4825 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4826 | EXPECT_EQ(ERR_IO_PENDING, StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4827 | TestGroupId("a"), MAXIMUM_PRIORITY, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4828 | ClientSocketPool::RespectLimits::ENABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4829 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 4830 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4831 | // Issue a request with RespectLimits::DISABLED, so a new ConnectJob is |
| 4832 | // created. |
| 4833 | EXPECT_EQ(ERR_IO_PENDING, StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4834 | TestGroupId("a"), MAXIMUM_PRIORITY, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4835 | ClientSocketPool::RespectLimits::DISABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4836 | ASSERT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4837 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4838 | // Cancel the pending request with RespectLimits::ENABLED. The ConnectJob |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4839 | // should not be cancelled. |
| 4840 | request(1)->handle()->Reset(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4841 | ASSERT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4842 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4843 | EXPECT_THAT(request(2)->WaitForResult(), IsOk()); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4844 | EXPECT_FALSE(request(1)->have_result()); |
| 4845 | } |
| 4846 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4847 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthNoAuthCallback) { |
| 4848 | CreatePool(1, 1); |
| 4849 | |
| 4850 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 4851 | |
| 4852 | ClientSocketHandle handle; |
| 4853 | TestCompletionCallback callback; |
| 4854 | EXPECT_EQ( |
| 4855 | ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4856 | handle.Init(TestGroupId("a"), params_, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4857 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 4858 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4859 | NetLogWithSource())); |
| 4860 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4861 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4862 | |
| 4863 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_PROXY_AUTH_REQUESTED)); |
| 4864 | EXPECT_FALSE(handle.is_initialized()); |
| 4865 | EXPECT_FALSE(handle.socket()); |
| 4866 | |
| 4867 | // The group should now be empty, and thus be deleted. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4868 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4869 | } |
| 4870 | |
| 4871 | class TestAuthHelper { |
| 4872 | public: |
| 4873 | TestAuthHelper() = default; |
| 4874 | ~TestAuthHelper() = default; |
| 4875 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4876 | void InitHandle( |
Matt Menke | 84d11e56 | 2019-03-27 00:11:19 | [diff] [blame] | 4877 | scoped_refptr<ClientSocketPool::SocketParams> params, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4878 | TransportClientSocketPool* pool, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4879 | RequestPriority priority = DEFAULT_PRIORITY, |
| 4880 | ClientSocketPool::RespectLimits respect_limits = |
| 4881 | ClientSocketPool::RespectLimits::ENABLED, |
| 4882 | const ClientSocketPool::GroupId& group_id_in = TestGroupId("a")) { |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4883 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4884 | handle_.Init(group_id_in, params, priority, SocketTag(), |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4885 | respect_limits, callback_.callback(), |
| 4886 | base::BindRepeating(&TestAuthHelper::AuthCallback, |
| 4887 | base::Unretained(this)), |
| 4888 | pool, NetLogWithSource())); |
| 4889 | } |
| 4890 | |
| 4891 | void WaitForAuth() { |
| 4892 | run_loop_ = std::make_unique<base::RunLoop>(); |
| 4893 | run_loop_->Run(); |
| 4894 | run_loop_.reset(); |
| 4895 | } |
| 4896 | |
| 4897 | void WaitForAuthAndRestartSync() { |
| 4898 | restart_sync_ = true; |
| 4899 | WaitForAuth(); |
| 4900 | restart_sync_ = false; |
| 4901 | } |
| 4902 | |
| 4903 | void WaitForAuthAndResetHandleSync() { |
| 4904 | reset_handle_sync_ = true; |
| 4905 | WaitForAuth(); |
| 4906 | reset_handle_sync_ = false; |
| 4907 | } |
| 4908 | |
| 4909 | void RestartWithAuth() { |
| 4910 | DCHECK(restart_with_auth_callback_); |
| 4911 | std::move(restart_with_auth_callback_).Run(); |
| 4912 | } |
| 4913 | |
| 4914 | int WaitForResult() { |
| 4915 | int result = callback_.WaitForResult(); |
| 4916 | // There shouldn't be any callback waiting to be invoked once the request is |
| 4917 | // complete. |
| 4918 | EXPECT_FALSE(restart_with_auth_callback_); |
| 4919 | // The socket should only be initialized on success. |
| 4920 | EXPECT_EQ(result == OK, handle_.is_initialized()); |
| 4921 | EXPECT_EQ(result == OK, handle_.socket() != nullptr); |
| 4922 | return result; |
| 4923 | } |
| 4924 | |
| 4925 | ClientSocketHandle* handle() { return &handle_; } |
| 4926 | int auth_count() const { return auth_count_; } |
| 4927 | int have_result() const { return callback_.have_result(); } |
| 4928 | |
| 4929 | private: |
| 4930 | void AuthCallback(const HttpResponseInfo& response, |
| 4931 | HttpAuthController* auth_controller, |
| 4932 | base::OnceClosure restart_with_auth_callback) { |
| 4933 | EXPECT_FALSE(restart_with_auth_callback_); |
| 4934 | EXPECT_TRUE(restart_with_auth_callback); |
| 4935 | |
| 4936 | // Once there's a result, this method shouldn't be invoked again. |
| 4937 | EXPECT_FALSE(callback_.have_result()); |
| 4938 | |
| 4939 | ++auth_count_; |
| 4940 | run_loop_->Quit(); |
| 4941 | if (restart_sync_) { |
| 4942 | std::move(restart_with_auth_callback).Run(); |
| 4943 | return; |
| 4944 | } |
| 4945 | |
| 4946 | restart_with_auth_callback_ = std::move(restart_with_auth_callback); |
| 4947 | |
| 4948 | if (reset_handle_sync_) { |
| 4949 | handle_.Reset(); |
| 4950 | return; |
| 4951 | } |
| 4952 | } |
| 4953 | |
| 4954 | std::unique_ptr<base::RunLoop> run_loop_; |
| 4955 | base::OnceClosure restart_with_auth_callback_; |
| 4956 | |
| 4957 | bool restart_sync_ = false; |
| 4958 | bool reset_handle_sync_ = false; |
| 4959 | |
| 4960 | ClientSocketHandle handle_; |
| 4961 | int auth_count_ = 0; |
| 4962 | TestCompletionCallback callback_; |
| 4963 | |
| 4964 | DISALLOW_COPY_AND_ASSIGN(TestAuthHelper); |
| 4965 | }; |
| 4966 | |
| 4967 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnce) { |
| 4968 | CreatePool(1, 1); |
| 4969 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 4970 | |
| 4971 | TestAuthHelper auth_helper; |
| 4972 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4973 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 4974 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4975 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4976 | |
| 4977 | auth_helper.WaitForAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4978 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 4979 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4980 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4981 | |
| 4982 | auth_helper.RestartWithAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4983 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 4984 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4985 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4986 | |
| 4987 | EXPECT_THAT(auth_helper.WaitForResult(), IsOk()); |
| 4988 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4989 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4990 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4991 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4992 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 4993 | } |
| 4994 | |
| 4995 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceSync) { |
| 4996 | CreatePool(1, 1); |
| 4997 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 4998 | |
| 4999 | TestAuthHelper auth_helper; |
| 5000 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5001 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5002 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5003 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5004 | |
| 5005 | auth_helper.WaitForAuthAndRestartSync(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5006 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5007 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5008 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5009 | |
| 5010 | EXPECT_THAT(auth_helper.WaitForResult(), IsOk()); |
| 5011 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5012 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5013 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5014 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5015 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5016 | } |
| 5017 | |
| 5018 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceFails) { |
| 5019 | CreatePool(1, 1); |
| 5020 | connect_job_factory_->set_job_type( |
| 5021 | TestConnectJob::kMockAuthChallengeOnceFailingJob); |
| 5022 | |
| 5023 | TestAuthHelper auth_helper; |
| 5024 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5025 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5026 | |
| 5027 | auth_helper.WaitForAuth(); |
| 5028 | auth_helper.RestartWithAuth(); |
| 5029 | EXPECT_THAT(auth_helper.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 5030 | |
| 5031 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5032 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5033 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5034 | } |
| 5035 | |
| 5036 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceSyncFails) { |
| 5037 | CreatePool(1, 1); |
| 5038 | connect_job_factory_->set_job_type( |
| 5039 | TestConnectJob::kMockAuthChallengeOnceFailingJob); |
| 5040 | |
| 5041 | TestAuthHelper auth_helper; |
| 5042 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5043 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5044 | |
| 5045 | auth_helper.WaitForAuthAndRestartSync(); |
| 5046 | EXPECT_THAT(auth_helper.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 5047 | |
| 5048 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5049 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5050 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5051 | } |
| 5052 | |
| 5053 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceDeleteHandle) { |
| 5054 | CreatePool(1, 1); |
| 5055 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5056 | |
| 5057 | TestAuthHelper auth_helper; |
| 5058 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5059 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5060 | |
| 5061 | auth_helper.WaitForAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5062 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5063 | |
| 5064 | auth_helper.handle()->Reset(); |
| 5065 | |
| 5066 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5067 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5068 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5069 | EXPECT_FALSE(auth_helper.handle()->is_initialized()); |
| 5070 | EXPECT_FALSE(auth_helper.handle()->socket()); |
| 5071 | } |
| 5072 | |
| 5073 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceDeleteHandleSync) { |
| 5074 | CreatePool(1, 1); |
| 5075 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5076 | |
| 5077 | TestAuthHelper auth_helper; |
| 5078 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5079 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5080 | |
| 5081 | auth_helper.WaitForAuthAndResetHandleSync(); |
| 5082 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5083 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5084 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5085 | EXPECT_FALSE(auth_helper.handle()->is_initialized()); |
| 5086 | EXPECT_FALSE(auth_helper.handle()->socket()); |
| 5087 | } |
| 5088 | |
| 5089 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceFlushWithError) { |
| 5090 | CreatePool(1, 1); |
| 5091 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5092 | |
| 5093 | TestAuthHelper auth_helper; |
| 5094 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5095 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5096 | |
| 5097 | auth_helper.WaitForAuth(); |
| 5098 | |
| 5099 | pool_->FlushWithError(ERR_FAILED); |
| 5100 | base::RunLoop().RunUntilIdle(); |
| 5101 | |
| 5102 | // When flushing the socket pool, bound sockets should delay returning the |
| 5103 | // error until completion. |
| 5104 | EXPECT_FALSE(auth_helper.have_result()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5105 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5106 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5107 | |
| 5108 | auth_helper.RestartWithAuth(); |
| 5109 | // The callback should be called asynchronously. |
| 5110 | EXPECT_FALSE(auth_helper.have_result()); |
| 5111 | |
| 5112 | EXPECT_THAT(auth_helper.WaitForResult(), IsError(ERR_FAILED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5113 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5114 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5115 | } |
| 5116 | |
| 5117 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthTwice) { |
| 5118 | CreatePool(1, 1); |
| 5119 | connect_job_factory_->set_job_type( |
| 5120 | TestConnectJob::kMockAuthChallengeTwiceJob); |
| 5121 | |
| 5122 | TestAuthHelper auth_helper; |
| 5123 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5124 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5125 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5126 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5127 | |
| 5128 | auth_helper.WaitForAuth(); |
| 5129 | auth_helper.RestartWithAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5130 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5131 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5132 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5133 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5134 | |
| 5135 | auth_helper.WaitForAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5136 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5137 | EXPECT_EQ(2, auth_helper.auth_count()); |
| 5138 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5139 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5140 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5141 | auth_helper.RestartWithAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5142 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5143 | EXPECT_EQ(2, auth_helper.auth_count()); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5144 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5145 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5146 | |
| 5147 | EXPECT_THAT(auth_helper.WaitForResult(), IsOk()); |
| 5148 | EXPECT_EQ(2, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5149 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5150 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5151 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5152 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5153 | } |
| 5154 | |
| 5155 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthTwiceFails) { |
| 5156 | CreatePool(1, 1); |
| 5157 | connect_job_factory_->set_job_type( |
| 5158 | TestConnectJob::kMockAuthChallengeTwiceFailingJob); |
| 5159 | |
| 5160 | TestAuthHelper auth_helper; |
| 5161 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5162 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5163 | |
| 5164 | auth_helper.WaitForAuth(); |
| 5165 | auth_helper.RestartWithAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5166 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5167 | EXPECT_EQ(1, auth_helper.auth_count()); |
| 5168 | |
| 5169 | auth_helper.WaitForAuth(); |
| 5170 | auth_helper.RestartWithAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5171 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5172 | EXPECT_EQ(2, auth_helper.auth_count()); |
| 5173 | |
| 5174 | EXPECT_THAT(auth_helper.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 5175 | EXPECT_EQ(2, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5176 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5177 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5178 | } |
| 5179 | |
| 5180 | // Makes sure that when a bound request is destroyed, a new ConnectJob is |
| 5181 | // created, if needed. |
| 5182 | TEST_F(ClientSocketPoolBaseTest, |
| 5183 | ProxyAuthCreateNewConnectJobOnDestroyBoundRequest) { |
| 5184 | CreatePool(1 /* max_sockets */, 1 /* max_sockets_per_group */); |
| 5185 | connect_job_factory_->set_job_type( |
| 5186 | TestConnectJob::kMockAuthChallengeOnceFailingJob); |
| 5187 | |
| 5188 | // First request creates a ConnectJob. |
| 5189 | TestAuthHelper auth_helper1; |
| 5190 | auth_helper1.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5191 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5192 | |
| 5193 | // A second request come in, but no new ConnectJob is needed, since the limit |
| 5194 | // has been reached. |
| 5195 | TestAuthHelper auth_helper2; |
| 5196 | auth_helper2.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5197 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5198 | |
| 5199 | // Run until the auth callback for the first request is invoked. |
| 5200 | auth_helper1.WaitForAuth(); |
| 5201 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5202 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5203 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5204 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5205 | |
| 5206 | // Make connect jobs succeed, then cancel the first request, which should |
| 5207 | // destroy the bound ConnectJob, and cause a new ConnectJob to start. |
| 5208 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 5209 | auth_helper1.handle()->Reset(); |
| 5210 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5211 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5212 | |
| 5213 | // The second ConnectJob should succeed. |
| 5214 | EXPECT_THAT(auth_helper2.WaitForResult(), IsOk()); |
| 5215 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5216 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5217 | } |
| 5218 | |
| 5219 | // Makes sure that when a bound request is destroyed, a new ConnectJob is |
| 5220 | // created for another group, if needed. |
| 5221 | TEST_F(ClientSocketPoolBaseTest, |
| 5222 | ProxyAuthCreateNewConnectJobOnDestroyBoundRequestDifferentGroups) { |
| 5223 | CreatePool(1 /* max_sockets */, 1 /* max_sockets_per_group */); |
| 5224 | connect_job_factory_->set_job_type( |
| 5225 | TestConnectJob::kMockAuthChallengeOnceFailingJob); |
| 5226 | |
| 5227 | // First request creates a ConnectJob. |
| 5228 | TestAuthHelper auth_helper1; |
| 5229 | auth_helper1.InitHandle(params_, pool_.get(), DEFAULT_PRIORITY); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5230 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5231 | |
| 5232 | // A second request come in, but no new ConnectJob is needed, since the limit |
| 5233 | // has been reached. |
| 5234 | TestAuthHelper auth_helper2; |
| 5235 | auth_helper2.InitHandle(params_, pool_.get(), DEFAULT_PRIORITY, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5236 | ClientSocketPool::RespectLimits::ENABLED, |
| 5237 | TestGroupId("b")); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5238 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5239 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5240 | |
| 5241 | // Run until the auth callback for the first request is invoked. |
| 5242 | auth_helper1.WaitForAuth(); |
| 5243 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5244 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5245 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5246 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5247 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 5248 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5249 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5250 | |
| 5251 | // Make connect jobs succeed, then cancel the first request, which should |
| 5252 | // destroy the bound ConnectJob, and cause a new ConnectJob to start for the |
| 5253 | // other group. |
| 5254 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 5255 | auth_helper1.handle()->Reset(); |
| 5256 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5257 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 5258 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5259 | |
| 5260 | // The second ConnectJob should succeed. |
| 5261 | EXPECT_THAT(auth_helper2.WaitForResult(), IsOk()); |
| 5262 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5263 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 5264 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5265 | } |
| 5266 | |
| 5267 | // Test that once an auth challenge is bound, that's the request that gets all |
| 5268 | // subsequent calls and the socket itself. |
| 5269 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthStaysBound) { |
| 5270 | CreatePool(1, 1); |
| 5271 | connect_job_factory_->set_job_type( |
| 5272 | TestConnectJob::kMockAuthChallengeTwiceJob); |
| 5273 | |
| 5274 | // First request creates a ConnectJob. |
| 5275 | TestAuthHelper auth_helper1; |
| 5276 | auth_helper1.InitHandle(params_, pool_.get(), LOWEST); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5277 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5278 | |
| 5279 | // A second, higher priority request is made. |
| 5280 | TestAuthHelper auth_helper2; |
| 5281 | auth_helper2.InitHandle(params_, pool_.get(), LOW); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5282 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5283 | |
| 5284 | // Run until the auth callback for the second request is invoked. |
| 5285 | auth_helper2.WaitForAuth(); |
| 5286 | EXPECT_EQ(0, auth_helper1.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5287 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5288 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5289 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5290 | |
| 5291 | // Start a higher priority job. It shouldn't be able to steal |auth_helper2|'s |
| 5292 | // ConnectJob. |
| 5293 | TestAuthHelper auth_helper3; |
| 5294 | auth_helper3.InitHandle(params_, pool_.get(), HIGHEST); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5295 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5296 | |
| 5297 | // Start a higher job that ignores limits, creating a hanging socket. It |
| 5298 | // shouldn't be able to steal |auth_helper2|'s ConnectJob. |
| 5299 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 5300 | TestAuthHelper auth_helper4; |
| 5301 | auth_helper4.InitHandle(params_, pool_.get(), HIGHEST, |
| 5302 | ClientSocketPool::RespectLimits::DISABLED); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5303 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5304 | |
| 5305 | // Restart with auth, and |auth_helper2|'s auth method should be invoked |
| 5306 | // again. |
| 5307 | auth_helper2.RestartWithAuth(); |
| 5308 | auth_helper2.WaitForAuth(); |
| 5309 | EXPECT_EQ(0, auth_helper1.auth_count()); |
| 5310 | EXPECT_FALSE(auth_helper1.have_result()); |
| 5311 | EXPECT_EQ(2, auth_helper2.auth_count()); |
| 5312 | EXPECT_FALSE(auth_helper2.have_result()); |
| 5313 | EXPECT_EQ(0, auth_helper3.auth_count()); |
| 5314 | EXPECT_FALSE(auth_helper3.have_result()); |
| 5315 | EXPECT_EQ(0, auth_helper4.auth_count()); |
| 5316 | EXPECT_FALSE(auth_helper4.have_result()); |
| 5317 | |
| 5318 | // Advance auth again, and |auth_helper2| should get the socket. |
| 5319 | auth_helper2.RestartWithAuth(); |
| 5320 | EXPECT_THAT(auth_helper2.WaitForResult(), IsOk()); |
| 5321 | // The hung ConnectJob for the RespectLimits::DISABLED request is still in the |
| 5322 | // socket pool. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5323 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5324 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5325 | EXPECT_EQ(0, auth_helper1.auth_count()); |
| 5326 | EXPECT_FALSE(auth_helper1.have_result()); |
| 5327 | EXPECT_EQ(0, auth_helper3.auth_count()); |
| 5328 | EXPECT_FALSE(auth_helper3.have_result()); |
| 5329 | EXPECT_EQ(0, auth_helper4.auth_count()); |
| 5330 | EXPECT_FALSE(auth_helper4.have_result()); |
| 5331 | |
| 5332 | // If the socket is returned to the socket pool, the RespectLimits::DISABLED |
| 5333 | // socket request should be able to claim it. |
| 5334 | auth_helper2.handle()->Reset(); |
| 5335 | EXPECT_THAT(auth_helper4.WaitForResult(), IsOk()); |
| 5336 | EXPECT_EQ(0, auth_helper1.auth_count()); |
| 5337 | EXPECT_FALSE(auth_helper1.have_result()); |
| 5338 | EXPECT_EQ(0, auth_helper3.auth_count()); |
| 5339 | EXPECT_FALSE(auth_helper3.have_result()); |
| 5340 | EXPECT_EQ(0, auth_helper4.auth_count()); |
| 5341 | } |
| 5342 | |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5343 | TEST_F(ClientSocketPoolBaseTest, RefreshGroupCreatesNewConnectJobs) { |
| 5344 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 5345 | const ClientSocketPool::GroupId kGroupId = TestGroupId("a"); |
| 5346 | |
| 5347 | // First job will be waiting until it gets aborted. |
| 5348 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 5349 | |
| 5350 | ClientSocketHandle handle; |
| 5351 | TestCompletionCallback callback; |
| 5352 | EXPECT_THAT( |
| 5353 | handle.Init(kGroupId, params_, DEFAULT_PRIORITY, SocketTag(), |
| 5354 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 5355 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 5356 | NetLogWithSource()), |
| 5357 | IsError(ERR_IO_PENDING)); |
| 5358 | |
| 5359 | // Switch connect job types, so creating a new ConnectJob will result in |
| 5360 | // success. |
| 5361 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 5362 | |
| 5363 | pool_->RefreshGroupForTesting(kGroupId); |
| 5364 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 5365 | ASSERT_TRUE(handle.socket()); |
| 5366 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5367 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId)); |
| 5368 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(kGroupId)); |
| 5369 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(kGroupId)); |
| 5370 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId)); |
| 5371 | } |
| 5372 | |
| 5373 | TEST_F(ClientSocketPoolBaseTest, RefreshGroupClosesIdleConnectJobs) { |
| 5374 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 5375 | const ClientSocketPool::GroupId kGroupId = TestGroupId("a"); |
| 5376 | |
| 5377 | pool_->RequestSockets(kGroupId, params_, 2, NetLogWithSource()); |
| 5378 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId)); |
| 5379 | EXPECT_EQ(2, pool_->IdleSocketCount()); |
| 5380 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(kGroupId)); |
| 5381 | |
| 5382 | pool_->RefreshGroupForTesting(kGroupId); |
| 5383 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5384 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupId)); |
| 5385 | } |
| 5386 | |
| 5387 | TEST_F(ClientSocketPoolBaseTest, |
| 5388 | RefreshGroupDoesNotCloseIdleConnectJobsInOtherGroup) { |
| 5389 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 5390 | const ClientSocketPool::GroupId kGroupId = TestGroupId("a"); |
| 5391 | const ClientSocketPool::GroupId kOtherGroupId = TestGroupId("b"); |
| 5392 | |
| 5393 | pool_->RequestSockets(kOtherGroupId, params_, 2, NetLogWithSource()); |
| 5394 | ASSERT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 5395 | EXPECT_EQ(2, pool_->IdleSocketCount()); |
| 5396 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(kOtherGroupId)); |
| 5397 | |
| 5398 | pool_->RefreshGroupForTesting(kGroupId); |
| 5399 | ASSERT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 5400 | EXPECT_EQ(2, pool_->IdleSocketCount()); |
| 5401 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(kOtherGroupId)); |
| 5402 | } |
| 5403 | |
| 5404 | TEST_F(ClientSocketPoolBaseTest, RefreshGroupPreventsSocketReuse) { |
| 5405 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 5406 | const ClientSocketPool::GroupId kGroupId = TestGroupId("a"); |
| 5407 | |
| 5408 | ClientSocketHandle handle; |
| 5409 | TestCompletionCallback callback; |
| 5410 | EXPECT_THAT( |
| 5411 | handle.Init(kGroupId, params_, DEFAULT_PRIORITY, SocketTag(), |
| 5412 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 5413 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 5414 | NetLogWithSource()), |
| 5415 | IsOk()); |
| 5416 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId)); |
| 5417 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId)); |
| 5418 | |
| 5419 | pool_->RefreshGroupForTesting(kGroupId); |
| 5420 | |
| 5421 | handle.Reset(); |
| 5422 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5423 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupId)); |
| 5424 | } |
| 5425 | |
| 5426 | TEST_F(ClientSocketPoolBaseTest, |
| 5427 | RefreshGroupDoesNotPreventSocketReuseInOtherGroup) { |
| 5428 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 5429 | const ClientSocketPool::GroupId kGroupId = TestGroupId("a"); |
| 5430 | const ClientSocketPool::GroupId kOtherGroupId = TestGroupId("b"); |
| 5431 | |
| 5432 | ClientSocketHandle handle; |
| 5433 | TestCompletionCallback callback; |
| 5434 | EXPECT_THAT( |
| 5435 | handle.Init(kOtherGroupId, params_, DEFAULT_PRIORITY, SocketTag(), |
| 5436 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 5437 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 5438 | NetLogWithSource()), |
| 5439 | IsOk()); |
| 5440 | ASSERT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 5441 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kOtherGroupId)); |
| 5442 | |
| 5443 | pool_->RefreshGroupForTesting(kGroupId); |
| 5444 | |
| 5445 | handle.Reset(); |
| 5446 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 5447 | ASSERT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 5448 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(kOtherGroupId)); |
| 5449 | } |
| 5450 | |
| 5451 | TEST_F(ClientSocketPoolBaseTest, RefreshGroupReplacesBoundConnectJobOnConnect) { |
| 5452 | CreatePool(1, 1); |
| 5453 | const ClientSocketPool::GroupId kGroupId = TestGroupId("a"); |
| 5454 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5455 | |
| 5456 | TestAuthHelper auth_helper; |
| 5457 | auth_helper.InitHandle(params_, pool_.get(), DEFAULT_PRIORITY, |
| 5458 | ClientSocketPool::RespectLimits::ENABLED, kGroupId); |
| 5459 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(kGroupId)); |
| 5460 | |
| 5461 | auth_helper.WaitForAuth(); |
| 5462 | |
| 5463 | // This should update the generation, but not cancel the old ConnectJob - it's |
| 5464 | // not safe to do anything while waiting on the original ConnectJob. |
| 5465 | pool_->RefreshGroupForTesting(kGroupId); |
| 5466 | |
| 5467 | // Providing auth credentials and restarting the request with them will cause |
| 5468 | // the ConnectJob to complete successfully, but the result will be discarded |
| 5469 | // because of the generation mismatch. |
| 5470 | auth_helper.RestartWithAuth(); |
| 5471 | |
| 5472 | // Despite using ConnectJobs that simulate a single challenge, a second |
| 5473 | // challenge will be seen, due to using a new ConnectJob. |
| 5474 | auth_helper.WaitForAuth(); |
| 5475 | auth_helper.RestartWithAuth(); |
| 5476 | |
| 5477 | EXPECT_THAT(auth_helper.WaitForResult(), IsOk()); |
| 5478 | EXPECT_TRUE(auth_helper.handle()->socket()); |
| 5479 | EXPECT_EQ(2, auth_helper.auth_count()); |
| 5480 | |
| 5481 | // When released, the socket will be returned to the socket pool, and |
| 5482 | // available for reuse. |
| 5483 | auth_helper.handle()->Reset(); |
| 5484 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 5485 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId)); |
| 5486 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(kGroupId)); |
| 5487 | } |
| 5488 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 5489 | } // namespace |
| 5490 | |
| 5491 | } // namespace net |