[email protected] | e34400c3 | 2012-01-24 02:49:33 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5 | #include "net/socket/transport_client_socket_pool.h" |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 6 | |
tbansal | f82cc8e | 2015-10-14 20:05:49 | [diff] [blame] | 7 | #include <stdint.h> |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 8 | #include <utility> |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 9 | #include <vector> |
| 10 | |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 11 | #include "base/bind.h" |
| 12 | #include "base/bind_helpers.h" |
[email protected] | 2041cf34 | 2010-02-19 03:15:59 | [diff] [blame] | 13 | #include "base/callback.h" |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 14 | #include "base/location.h" |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 15 | #include "base/logging.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 16 | #include "base/memory/ref_counted.h" |
[email protected] | 6ea7b15 | 2011-12-21 21:21:13 | [diff] [blame] | 17 | #include "base/memory/weak_ptr.h" |
Alexander Timin | 4f9c35c | 2018-11-01 20:15:20 | [diff] [blame] | 18 | #include "base/message_loop/message_loop.h" |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 19 | #include "base/optional.h" |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 20 | #include "base/run_loop.h" |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 21 | #include "base/single_thread_task_runner.h" |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 22 | #include "base/stl_util.h" |
[email protected] | fc9be580 | 2013-06-11 10:56:51 | [diff] [blame] | 23 | #include "base/strings/string_number_conversions.h" |
[email protected] | 18b57741 | 2013-07-18 04:19:15 | [diff] [blame] | 24 | #include "base/strings/stringprintf.h" |
[email protected] | f214f879 | 2011-01-01 02:17:08 | [diff] [blame] | 25 | #include "base/threading/platform_thread.h" |
gab | f767595f | 2016-05-11 18:50:35 | [diff] [blame] | 26 | #include "base/threading/thread_task_runner_handle.h" |
[email protected] | f3a1c64 | 2011-07-12 19:15:03 | [diff] [blame] | 27 | #include "base/values.h" |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 28 | #include "net/base/load_timing_info.h" |
[email protected] | b258e079 | 2013-01-12 07:11:59 | [diff] [blame] | 29 | #include "net/base/load_timing_info_test_util.h" |
[email protected] | d8eb8424 | 2010-09-25 02:25:06 | [diff] [blame] | 30 | #include "net/base/net_errors.h" |
Matt Menke | bdf77780 | 2019-04-22 19:38:59 | [diff] [blame] | 31 | #include "net/base/privacy_mode.h" |
Matt Menke | aafff54 | 2019-04-22 22:09:36 | [diff] [blame] | 32 | #include "net/base/proxy_server.h" |
[email protected] | ac790b4 | 2009-12-02 04:31:31 | [diff] [blame] | 33 | #include "net/base/request_priority.h" |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 34 | #include "net/base/test_completion_callback.h" |
[email protected] | 277d594 | 2010-08-11 21:02:35 | [diff] [blame] | 35 | #include "net/http/http_response_headers.h" |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 36 | #include "net/http/http_response_info.h" |
eroman | 87c53d6 | 2015-04-02 06:51:07 | [diff] [blame] | 37 | #include "net/log/net_log.h" |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 38 | #include "net/log/net_log_event_type.h" |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 39 | #include "net/log/net_log_source.h" |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 40 | #include "net/log/net_log_source_type.h" |
mmenke | 16a7cbdd | 2015-04-24 23:00:56 | [diff] [blame] | 41 | #include "net/log/test_net_log.h" |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 42 | #include "net/log/test_net_log_entry.h" |
| 43 | #include "net/log/test_net_log_util.h" |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 44 | #include "net/socket/client_socket_factory.h" |
| 45 | #include "net/socket/client_socket_handle.h" |
tfarina | 5dd13c2 | 2016-11-16 12:08:26 | [diff] [blame] | 46 | #include "net/socket/datagram_client_socket.h" |
tbansal | ca83c00 | 2016-04-28 20:56:28 | [diff] [blame] | 47 | #include "net/socket/socket_performance_watcher.h" |
Paul Jensen | 8d6f87ec | 2018-01-13 00:46:54 | [diff] [blame] | 48 | #include "net/socket/socket_tag.h" |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 49 | #include "net/socket/socket_test_util.h" |
[email protected] | 18ccfdb | 2013-08-15 00:13:44 | [diff] [blame] | 50 | #include "net/socket/ssl_client_socket.h" |
[email protected] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame] | 51 | #include "net/socket/stream_socket.h" |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 52 | #include "net/socket/transport_connect_job.h" |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 53 | #include "net/ssl/ssl_cert_request_info.h" |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 54 | #include "net/test/gtest_util.h" |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 55 | #include "net/test/test_with_scoped_task_environment.h" |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 56 | #include "net/traffic_annotation/network_traffic_annotation.h" |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 57 | #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 58 | #include "testing/gmock/include/gmock/gmock.h" |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 59 | #include "testing/gtest/include/gtest/gtest.h" |
| 60 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 61 | using net::test::IsError; |
| 62 | using net::test::IsOk; |
| 63 | |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 64 | using ::testing::Invoke; |
| 65 | using ::testing::Return; |
| 66 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 67 | namespace net { |
| 68 | |
| 69 | namespace { |
| 70 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 71 | const int kDefaultMaxSockets = 4; |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 72 | const int kDefaultMaxSocketsPerGroup = 2; |
Tarun Bansal | a763509 | 2019-02-20 10:00:59 | [diff] [blame] | 73 | constexpr base::TimeDelta kUnusedIdleSocketTimeout = |
| 74 | base::TimeDelta::FromSeconds(10); |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 75 | |
Matt Menke | bdf77780 | 2019-04-22 19:38:59 | [diff] [blame] | 76 | ClientSocketPool::GroupId TestGroupId( |
| 77 | const std::string& host, |
| 78 | int port = 80, |
| 79 | ClientSocketPool::SocketType socket_type = |
| 80 | ClientSocketPool::SocketType::kHttp, |
| 81 | PrivacyMode privacy_mode = PrivacyMode::PRIVACY_MODE_DISABLED) { |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 82 | return ClientSocketPool::GroupId(HostPortPair(host, port), socket_type, |
| 83 | privacy_mode); |
| 84 | } |
| 85 | |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 86 | // Make sure |handle| sets load times correctly when it has been assigned a |
| 87 | // reused socket. |
| 88 | void TestLoadTimingInfoConnectedReused(const ClientSocketHandle& handle) { |
| 89 | LoadTimingInfo load_timing_info; |
| 90 | // Only pass true in as |is_reused|, as in general, HttpStream types should |
| 91 | // have stricter concepts of reuse than socket pools. |
| 92 | EXPECT_TRUE(handle.GetLoadTimingInfo(true, &load_timing_info)); |
| 93 | |
| 94 | EXPECT_EQ(true, load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 95 | EXPECT_NE(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 96 | |
[email protected] | b258e079 | 2013-01-12 07:11:59 | [diff] [blame] | 97 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 98 | ExpectLoadTimingHasOnlyConnectionTimes(load_timing_info); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | // Make sure |handle| sets load times correctly when it has been assigned a |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 102 | // fresh socket. Also runs TestLoadTimingInfoConnectedReused, since the owner |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 103 | // of a connection where |is_reused| is false may consider the connection |
| 104 | // reused. |
| 105 | void TestLoadTimingInfoConnectedNotReused(const ClientSocketHandle& handle) { |
| 106 | EXPECT_FALSE(handle.is_reused()); |
| 107 | |
| 108 | LoadTimingInfo load_timing_info; |
| 109 | EXPECT_TRUE(handle.GetLoadTimingInfo(false, &load_timing_info)); |
| 110 | |
| 111 | EXPECT_FALSE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 112 | EXPECT_NE(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 113 | |
[email protected] | b258e079 | 2013-01-12 07:11:59 | [diff] [blame] | 114 | ExpectConnectTimingHasTimes(load_timing_info.connect_timing, |
| 115 | CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY); |
| 116 | ExpectLoadTimingHasOnlyConnectionTimes(load_timing_info); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 117 | |
| 118 | TestLoadTimingInfoConnectedReused(handle); |
| 119 | } |
| 120 | |
| 121 | // Make sure |handle| sets load times correctly, in the case that it does not |
| 122 | // currently have a socket. |
| 123 | void TestLoadTimingInfoNotConnected(const ClientSocketHandle& handle) { |
| 124 | // Should only be set to true once a socket is assigned, if at all. |
| 125 | EXPECT_FALSE(handle.is_reused()); |
| 126 | |
| 127 | LoadTimingInfo load_timing_info; |
| 128 | EXPECT_FALSE(handle.GetLoadTimingInfo(false, &load_timing_info)); |
| 129 | |
| 130 | EXPECT_FALSE(load_timing_info.socket_reused); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 131 | EXPECT_EQ(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 132 | |
[email protected] | b258e079 | 2013-01-12 07:11:59 | [diff] [blame] | 133 | ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); |
| 134 | ExpectLoadTimingHasOnlyConnectionTimes(load_timing_info); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 135 | } |
| 136 | |
[email protected] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame] | 137 | class MockClientSocket : public StreamSocket { |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 138 | public: |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 139 | explicit MockClientSocket(net::NetLog* net_log) |
| 140 | : connected_(false), |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 141 | has_unread_data_(false), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 142 | net_log_(NetLogWithSource::Make(net_log, NetLogSourceType::SOCKET)), |
Charlie Harrison | 3e4c062 | 2018-05-13 15:44:30 | [diff] [blame] | 143 | was_used_to_convey_data_(false) {} |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 144 | |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 145 | // Sets whether the socket has unread data. If true, the next call to Read() |
| 146 | // will return 1 byte and IsConnectedAndIdle() will return false. |
| 147 | void set_has_unread_data(bool has_unread_data) { |
| 148 | has_unread_data_ = has_unread_data; |
| 149 | } |
| 150 | |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 151 | // Socket implementation. |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 152 | int Read(IOBuffer* /* buf */, |
| 153 | int len, |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 154 | CompletionOnceCallback /* callback */) override { |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 155 | if (has_unread_data_ && len > 0) { |
| 156 | has_unread_data_ = false; |
| 157 | was_used_to_convey_data_ = true; |
| 158 | return 1; |
| 159 | } |
[email protected] | e86df8dc | 2013-03-30 13:18:28 | [diff] [blame] | 160 | return ERR_UNEXPECTED; |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 161 | } |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 162 | |
[email protected] | a2b2cfc | 2017-12-06 09:06:08 | [diff] [blame] | 163 | int Write( |
| 164 | IOBuffer* /* buf */, |
| 165 | int len, |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 166 | CompletionOnceCallback /* callback */, |
[email protected] | a2b2cfc | 2017-12-06 09:06:08 | [diff] [blame] | 167 | const NetworkTrafficAnnotationTag& /*traffic_annotation*/) override { |
[email protected] | 0f873e8 | 2010-09-02 16:09:01 | [diff] [blame] | 168 | was_used_to_convey_data_ = true; |
| 169 | return len; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 170 | } |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 171 | int SetReceiveBufferSize(int32_t size) override { return OK; } |
| 172 | int SetSendBufferSize(int32_t size) override { return OK; } |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 173 | |
[email protected] | dbf036f | 2011-12-06 23:33:24 | [diff] [blame] | 174 | // StreamSocket implementation. |
Brad Lassey | 3a81417 | 2018-04-26 03:30:21 | [diff] [blame] | 175 | int Connect(CompletionOnceCallback callback) override { |
[email protected] | dbf036f | 2011-12-06 23:33:24 | [diff] [blame] | 176 | connected_ = true; |
| 177 | return OK; |
| 178 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 179 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 180 | void Disconnect() override { connected_ = false; } |
| 181 | bool IsConnected() const override { return connected_; } |
| 182 | bool IsConnectedAndIdle() const override { |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 183 | return connected_ && !has_unread_data_; |
| 184 | } |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 185 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 186 | int GetPeerAddress(IPEndPoint* /* address */) const override { |
[email protected] | 9f864b3 | 2010-01-20 15:01:16 | [diff] [blame] | 187 | return ERR_UNEXPECTED; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 188 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 189 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 190 | int GetLocalAddress(IPEndPoint* /* address */) const override { |
[email protected] | e7f74da | 2011-04-19 23:49:35 | [diff] [blame] | 191 | return ERR_UNEXPECTED; |
| 192 | } |
| 193 | |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 194 | const NetLogWithSource& NetLog() const override { return net_log_; } |
[email protected] | a2006ece | 2010-04-23 16:44:02 | [diff] [blame] | 195 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 196 | bool WasEverUsed() const override { return was_used_to_convey_data_; } |
tfarina | 2846404c | 2016-12-25 14:31:37 | [diff] [blame] | 197 | bool WasAlpnNegotiated() const override { return false; } |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 198 | NextProto GetNegotiatedProtocol() const override { return kProtoUnknown; } |
| 199 | bool GetSSLInfo(SSLInfo* ssl_info) override { return false; } |
ttuttle | 23fdb7b | 2015-05-15 01:28:03 | [diff] [blame] | 200 | void GetConnectionAttempts(ConnectionAttempts* out) const override { |
| 201 | out->clear(); |
| 202 | } |
| 203 | void ClearConnectionAttempts() override {} |
| 204 | void AddConnectionAttempts(const ConnectionAttempts& attempts) override {} |
tbansal | f82cc8e | 2015-10-14 20:05:49 | [diff] [blame] | 205 | int64_t GetTotalReceivedBytes() const override { |
| 206 | NOTIMPLEMENTED(); |
| 207 | return 0; |
| 208 | } |
Paul Jensen | 0f49dec | 2017-12-12 23:39:58 | [diff] [blame] | 209 | void ApplySocketTag(const SocketTag& tag) override {} |
[email protected] | 9b5614a | 2010-08-25 20:29:45 | [diff] [blame] | 210 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 211 | private: |
| 212 | bool connected_; |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 213 | bool has_unread_data_; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 214 | NetLogWithSource net_log_; |
[email protected] | 0f873e8 | 2010-09-02 16:09:01 | [diff] [blame] | 215 | bool was_used_to_convey_data_; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 216 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 217 | DISALLOW_COPY_AND_ASSIGN(MockClientSocket); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 218 | }; |
| 219 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 220 | class TestConnectJob; |
| 221 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 222 | class MockClientSocketFactory : public ClientSocketFactory { |
| 223 | public: |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 224 | MockClientSocketFactory() : allocation_count_(0) {} |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 225 | |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 226 | std::unique_ptr<DatagramClientSocket> CreateDatagramClientSocket( |
[email protected] | 5370c01 | 2011-06-29 03:47:04 | [diff] [blame] | 227 | DatagramSocket::BindType bind_type, |
[email protected] | 98b0e58 | 2011-06-22 14:31:41 | [diff] [blame] | 228 | NetLog* net_log, |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 229 | const NetLogSource& source) override { |
[email protected] | 98b0e58 | 2011-06-22 14:31:41 | [diff] [blame] | 230 | NOTREACHED(); |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 231 | return std::unique_ptr<DatagramClientSocket>(); |
[email protected] | 98b0e58 | 2011-06-22 14:31:41 | [diff] [blame] | 232 | } |
| 233 | |
Helen Li | d5bb922 | 2018-04-12 15:33:09 | [diff] [blame] | 234 | std::unique_ptr<TransportClientSocket> CreateTransportClientSocket( |
[email protected] | 0a0b768 | 2010-08-25 17:08:07 | [diff] [blame] | 235 | const AddressList& addresses, |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 236 | std::unique_ptr< |
| 237 | SocketPerformanceWatcher> /* socket_performance_watcher */, |
[email protected] | 0a0b768 | 2010-08-25 17:08:07 | [diff] [blame] | 238 | NetLog* /* net_log */, |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 239 | const NetLogSource& /*source*/) override { |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 240 | allocation_count_++; |
Helen Li | d5bb922 | 2018-04-12 15:33:09 | [diff] [blame] | 241 | return nullptr; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 242 | } |
| 243 | |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 244 | std::unique_ptr<SSLClientSocket> CreateSSLClientSocket( |
Matt Menke | 841fc41 | 2019-03-05 23:20:12 | [diff] [blame] | 245 | std::unique_ptr<StreamSocket> stream_socket, |
[email protected] | 4f4de7e6 | 2010-11-12 19:55:27 | [diff] [blame] | 246 | const HostPortPair& host_and_port, |
[email protected] | 7ab5bbd1 | 2010-10-19 13:33:21 | [diff] [blame] | 247 | const SSLConfig& ssl_config, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 248 | const SSLClientSocketContext& context) override { |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 249 | NOTIMPLEMENTED(); |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 250 | return std::unique_ptr<SSLClientSocket>(); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 251 | } |
Matt Menke | fd95692 | 2019-02-04 23:44:03 | [diff] [blame] | 252 | |
Matt Menke | 52cd95a | 2019-02-08 06:16:27 | [diff] [blame] | 253 | std::unique_ptr<ProxyClientSocket> CreateProxyClientSocket( |
| 254 | std::unique_ptr<StreamSocket> stream_socket, |
| 255 | const std::string& user_agent, |
| 256 | const HostPortPair& endpoint, |
| 257 | const ProxyServer& proxy_server, |
| 258 | HttpAuthController* http_auth_controller, |
| 259 | bool tunnel, |
| 260 | bool using_spdy, |
| 261 | NextProto negotiated_protocol, |
| 262 | ProxyDelegate* proxy_delegate, |
Matt Menke | 52cd95a | 2019-02-08 06:16:27 | [diff] [blame] | 263 | const NetworkTrafficAnnotationTag& traffic_annotation) override { |
| 264 | NOTIMPLEMENTED(); |
| 265 | return nullptr; |
| 266 | } |
| 267 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 268 | void WaitForSignal(TestConnectJob* job) { waiting_jobs_.push_back(job); } |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 269 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 270 | void SignalJobs(); |
| 271 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 272 | void SignalJob(size_t job); |
| 273 | |
| 274 | void SetJobLoadState(size_t job, LoadState load_state); |
| 275 | |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 276 | // Sets the HasConnectionEstablished value of the specified job to true, |
| 277 | // without invoking the callback. |
| 278 | void SetJobHasEstablishedConnection(size_t job); |
| 279 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 280 | int allocation_count() const { return allocation_count_; } |
| 281 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 282 | private: |
| 283 | int allocation_count_; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 284 | std::vector<TestConnectJob*> waiting_jobs_; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 285 | }; |
| 286 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 287 | class TestConnectJob : public ConnectJob { |
| 288 | public: |
| 289 | enum JobType { |
| 290 | kMockJob, |
| 291 | kMockFailingJob, |
| 292 | kMockPendingJob, |
| 293 | kMockPendingFailingJob, |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 294 | kMockWaitingJob, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 295 | |
| 296 | // Certificate errors return a socket in addition to an error code. |
| 297 | kMockCertErrorJob, |
| 298 | kMockPendingCertErrorJob, |
| 299 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 300 | kMockAdditionalErrorStateJob, |
| 301 | kMockPendingAdditionalErrorStateJob, |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 302 | kMockUnreadDataJob, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 303 | |
| 304 | kMockAuthChallengeOnceJob, |
| 305 | kMockAuthChallengeTwiceJob, |
| 306 | kMockAuthChallengeOnceFailingJob, |
| 307 | kMockAuthChallengeTwiceFailingJob, |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 308 | }; |
| 309 | |
[email protected] | 994d493 | 2010-07-12 17:55:13 | [diff] [blame] | 310 | // The kMockPendingJob uses a slight delay before allowing the connect |
| 311 | // to complete. |
| 312 | static const int kPendingConnectDelay = 2; |
| 313 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 314 | TestConnectJob(JobType job_type, |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 315 | RequestPriority request_priority, |
| 316 | SocketTag socket_tag, |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 317 | base::TimeDelta timeout_duration, |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 318 | const CommonConnectJobParams* common_connect_job_params, |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 319 | ConnectJob::Delegate* delegate, |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 320 | MockClientSocketFactory* client_socket_factory) |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 321 | : ConnectJob(request_priority, |
| 322 | socket_tag, |
Matt Menke | 1a6c92d | 2019-02-23 00:25:38 | [diff] [blame] | 323 | timeout_duration, |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 324 | common_connect_job_params, |
Matt Menke | 1a6c92d | 2019-02-23 00:25:38 | [diff] [blame] | 325 | delegate, |
| 326 | nullptr /* net_log */, |
| 327 | NetLogSourceType::TRANSPORT_CONNECT_JOB, |
| 328 | NetLogEventType::TRANSPORT_CONNECT_JOB_CONNECT), |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 329 | job_type_(job_type), |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 330 | client_socket_factory_(client_socket_factory), |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 331 | load_state_(LOAD_STATE_IDLE), |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 332 | has_established_connection_(false), |
[email protected] | d5492c5 | 2013-11-10 20:44:39 | [diff] [blame] | 333 | store_additional_error_state_(false), |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 334 | weak_factory_(this) {} |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 335 | |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 336 | void Signal() { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 337 | DoConnect(waiting_success_, true /* async */, false /* recoverable */); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 338 | } |
| 339 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 340 | void set_load_state(LoadState load_state) { load_state_ = load_state; } |
| 341 | |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 342 | void set_has_established_connection() { |
| 343 | DCHECK(!has_established_connection_); |
| 344 | has_established_connection_ = true; |
| 345 | } |
| 346 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 347 | // From ConnectJob: |
| 348 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 349 | LoadState GetLoadState() const override { return load_state_; } |
[email protected] | 4645135 | 2009-09-01 14:54:21 | [diff] [blame] | 350 | |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 351 | bool HasEstablishedConnection() const override { |
| 352 | return has_established_connection_; |
| 353 | } |
| 354 | |
Matt Menke | 6f84d1f1 | 2019-04-11 19:26:47 | [diff] [blame] | 355 | bool IsSSLError() const override { return store_additional_error_state_; } |
| 356 | |
| 357 | scoped_refptr<SSLCertRequestInfo> GetCertRequestInfo() override { |
| 358 | if (store_additional_error_state_) |
| 359 | return base::MakeRefCounted<SSLCertRequestInfo>(); |
| 360 | return nullptr; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 361 | } |
| 362 | |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 363 | private: |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 364 | // From ConnectJob: |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 365 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 366 | int ConnectInternal() override { |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 367 | AddressList ignored; |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 368 | client_socket_factory_->CreateTransportClientSocket( |
| 369 | ignored, nullptr, nullptr, NetLogSource()); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 370 | switch (job_type_) { |
| 371 | case kMockJob: |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 372 | return DoConnect(true /* successful */, false /* sync */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 373 | false /* cert_error */); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 374 | case kMockFailingJob: |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 375 | return DoConnect(false /* error */, false /* sync */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 376 | false /* cert_error */); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 377 | case kMockPendingJob: |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 378 | set_load_state(LOAD_STATE_CONNECTING); |
[email protected] | 6b17538 | 2009-10-13 06:47:47 | [diff] [blame] | 379 | |
| 380 | // Depending on execution timings, posting a delayed task can result |
| 381 | // in the task getting executed the at the earliest possible |
| 382 | // opportunity or only after returning once from the message loop and |
| 383 | // then a second call into the message loop. In order to make behavior |
| 384 | // more deterministic, we change the default delay to 2ms. This should |
| 385 | // always require us to wait for the second call into the message loop. |
| 386 | // |
| 387 | // N.B. The correct fix for this and similar timing problems is to |
| 388 | // abstract time for the purpose of unittests. Unfortunately, we have |
| 389 | // a lot of third-party components that directly call the various |
| 390 | // time functions, so this change would be rather invasive. |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 391 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 392 | FROM_HERE, |
kylechar | f4fe517 | 2019-02-15 18:53:49 | [diff] [blame] | 393 | base::BindOnce(base::IgnoreResult(&TestConnectJob::DoConnect), |
| 394 | weak_factory_.GetWeakPtr(), true /* successful */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 395 | true /* async */, false /* cert_error */), |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 396 | base::TimeDelta::FromMilliseconds(kPendingConnectDelay)); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 397 | return ERR_IO_PENDING; |
| 398 | case kMockPendingFailingJob: |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 399 | set_load_state(LOAD_STATE_CONNECTING); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 400 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 401 | FROM_HERE, |
kylechar | f4fe517 | 2019-02-15 18:53:49 | [diff] [blame] | 402 | base::BindOnce(base::IgnoreResult(&TestConnectJob::DoConnect), |
| 403 | weak_factory_.GetWeakPtr(), false /* error */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 404 | true /* async */, false /* cert_error */), |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 405 | base::TimeDelta::FromMilliseconds(2)); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 406 | return ERR_IO_PENDING; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 407 | case kMockWaitingJob: |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 408 | set_load_state(LOAD_STATE_CONNECTING); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 409 | client_socket_factory_->WaitForSignal(this); |
| 410 | waiting_success_ = true; |
| 411 | return ERR_IO_PENDING; |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 412 | case kMockCertErrorJob: |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 413 | return DoConnect(false /* error */, false /* sync */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 414 | true /* cert_error */); |
| 415 | case kMockPendingCertErrorJob: |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 416 | set_load_state(LOAD_STATE_CONNECTING); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 417 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 418 | FROM_HERE, |
kylechar | f4fe517 | 2019-02-15 18:53:49 | [diff] [blame] | 419 | base::BindOnce(base::IgnoreResult(&TestConnectJob::DoConnect), |
| 420 | weak_factory_.GetWeakPtr(), false /* error */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 421 | true /* async */, true /* cert_error */), |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 422 | base::TimeDelta::FromMilliseconds(2)); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 423 | return ERR_IO_PENDING; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 424 | case kMockAdditionalErrorStateJob: |
| 425 | store_additional_error_state_ = true; |
| 426 | return DoConnect(false /* error */, false /* sync */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 427 | false /* cert_error */); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 428 | case kMockPendingAdditionalErrorStateJob: |
| 429 | set_load_state(LOAD_STATE_CONNECTING); |
| 430 | store_additional_error_state_ = true; |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 431 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 432 | FROM_HERE, |
kylechar | f4fe517 | 2019-02-15 18:53:49 | [diff] [blame] | 433 | base::BindOnce(base::IgnoreResult(&TestConnectJob::DoConnect), |
| 434 | weak_factory_.GetWeakPtr(), false /* error */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 435 | true /* async */, false /* cert_error */), |
[email protected] | 5761ab9c | 2012-02-04 16:44:53 | [diff] [blame] | 436 | base::TimeDelta::FromMilliseconds(2)); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 437 | return ERR_IO_PENDING; |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 438 | case kMockUnreadDataJob: { |
| 439 | int ret = DoConnect(true /* successful */, false /* sync */, |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 440 | false /* cert_error */); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 441 | static_cast<MockClientSocket*>(socket())->set_has_unread_data(true); |
| 442 | return ret; |
| 443 | } |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 444 | case kMockAuthChallengeOnceJob: |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 445 | set_load_state(LOAD_STATE_CONNECTING); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 446 | DoAdvanceAuthChallenge(1, true /* succeed_after_last_challenge */); |
| 447 | return ERR_IO_PENDING; |
| 448 | case kMockAuthChallengeTwiceJob: |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 449 | set_load_state(LOAD_STATE_CONNECTING); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 450 | DoAdvanceAuthChallenge(2, true /* succeed_after_last_challenge */); |
| 451 | return ERR_IO_PENDING; |
| 452 | case kMockAuthChallengeOnceFailingJob: |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 453 | set_load_state(LOAD_STATE_CONNECTING); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 454 | DoAdvanceAuthChallenge(1, false /* succeed_after_last_challenge */); |
| 455 | return ERR_IO_PENDING; |
| 456 | case kMockAuthChallengeTwiceFailingJob: |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 457 | set_load_state(LOAD_STATE_CONNECTING); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 458 | DoAdvanceAuthChallenge(2, false /* succeed_after_last_challenge */); |
| 459 | return ERR_IO_PENDING; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 460 | default: |
| 461 | NOTREACHED(); |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 462 | SetSocket(std::unique_ptr<StreamSocket>()); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 463 | return ERR_FAILED; |
| 464 | } |
| 465 | } |
| 466 | |
Lily Chen | 02ef29a | 2018-11-30 16:31:43 | [diff] [blame] | 467 | void ChangePriorityInternal(RequestPriority priority) override {} |
| 468 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 469 | int DoConnect(bool succeed, bool was_async, bool cert_error) { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 470 | int result = OK; |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 471 | has_established_connection_ = true; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 472 | if (succeed) { |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 473 | SetSocket(std::make_unique<MockClientSocket>(net_log().net_log())); |
Bence Béky | bdbb0e7 | 2018-08-07 21:42:59 | [diff] [blame] | 474 | socket()->Connect(CompletionOnceCallback()); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 475 | } else if (cert_error) { |
| 476 | SetSocket(std::make_unique<MockClientSocket>(net_log().net_log())); |
| 477 | result = ERR_CERT_COMMON_NAME_INVALID; |
[email protected] | 6e713f0 | 2009-08-06 02:56:40 | [diff] [blame] | 478 | } else { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 479 | result = ERR_CONNECTION_FAILED; |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 480 | SetSocket(std::unique_ptr<StreamSocket>()); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 481 | } |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 482 | |
| 483 | if (was_async) |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 484 | NotifyDelegateOfCompletion(result); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 485 | return result; |
| 486 | } |
| 487 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 488 | void DoAdvanceAuthChallenge(int remaining_challenges, |
| 489 | bool succeed_after_last_challenge) { |
| 490 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 491 | FROM_HERE, |
| 492 | base::BindOnce(&TestConnectJob::InvokeNextProxyAuthCallback, |
| 493 | weak_factory_.GetWeakPtr(), remaining_challenges, |
| 494 | succeed_after_last_challenge)); |
| 495 | } |
| 496 | |
| 497 | void InvokeNextProxyAuthCallback(int remaining_challenges, |
| 498 | bool succeed_after_last_challenge) { |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 499 | set_load_state(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 500 | if (remaining_challenges == 0) { |
| 501 | DoConnect(succeed_after_last_challenge, true /* was_async */, |
| 502 | false /* cert_error */); |
| 503 | return; |
| 504 | } |
| 505 | |
| 506 | // Integration tests make sure HttpResponseInfo and HttpAuthController work. |
| 507 | // The auth tests here are just focused on ConnectJob bookkeeping. |
| 508 | HttpResponseInfo info; |
| 509 | NotifyDelegateOfProxyAuth( |
| 510 | info, nullptr /* http_auth_controller */, |
| 511 | base::BindOnce(&TestConnectJob::DoAdvanceAuthChallenge, |
| 512 | weak_factory_.GetWeakPtr(), remaining_challenges - 1, |
| 513 | succeed_after_last_challenge)); |
| 514 | } |
| 515 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 516 | bool waiting_success_; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 517 | const JobType job_type_; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 518 | MockClientSocketFactory* const client_socket_factory_; |
[email protected] | 4645135 | 2009-09-01 14:54:21 | [diff] [blame] | 519 | LoadState load_state_; |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 520 | bool has_established_connection_; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 521 | bool store_additional_error_state_; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 522 | |
[email protected] | d5492c5 | 2013-11-10 20:44:39 | [diff] [blame] | 523 | base::WeakPtrFactory<TestConnectJob> weak_factory_; |
| 524 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 525 | DISALLOW_COPY_AND_ASSIGN(TestConnectJob); |
| 526 | }; |
| 527 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 528 | class TestConnectJobFactory |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 529 | : public TransportClientSocketPool::ConnectJobFactory { |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 530 | public: |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 531 | TestConnectJobFactory(MockClientSocketFactory* client_socket_factory, |
| 532 | NetLog* net_log) |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 533 | : common_connect_job_params_( |
| 534 | nullptr /* client_socket_factory */, |
| 535 | nullptr /* host_resolver */, |
Matt Menke | b88837e | 2019-03-20 11:50:40 | [diff] [blame] | 536 | nullptr /* http_auth_cache */, |
| 537 | nullptr /* http_auth_handler_factory */, |
| 538 | nullptr /* spdy_session_pool */, |
Matt Menke | b5fb42b | 2019-03-22 17:26:13 | [diff] [blame] | 539 | nullptr /* quic_supported_versions */, |
Matt Menke | b88837e | 2019-03-20 11:50:40 | [diff] [blame] | 540 | nullptr /* quic_stream_factory */, |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 541 | nullptr /* proxy_delegate */, |
Matt Menke | d732ea4 | 2019-03-08 12:05:00 | [diff] [blame] | 542 | nullptr /* http_user_agent_settings */, |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 543 | SSLClientSocketContext(), |
| 544 | SSLClientSocketContext(), |
| 545 | nullptr /* socket_performance_watcher_factory */, |
| 546 | nullptr /* network_quality_estimator */, |
| 547 | net_log, |
| 548 | nullptr /* websocket_endpoint_lock_manager */), |
| 549 | job_type_(TestConnectJob::kMockJob), |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 550 | job_types_(nullptr), |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 551 | client_socket_factory_(client_socket_factory) {} |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 552 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 553 | ~TestConnectJobFactory() override = default; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 554 | |
| 555 | void set_job_type(TestConnectJob::JobType job_type) { job_type_ = job_type; } |
| 556 | |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 557 | void set_job_types(std::list<TestConnectJob::JobType>* job_types) { |
| 558 | job_types_ = job_types; |
| 559 | CHECK(!job_types_->empty()); |
| 560 | } |
| 561 | |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 562 | void set_timeout_duration(base::TimeDelta timeout_duration) { |
| 563 | timeout_duration_ = timeout_duration; |
| 564 | } |
| 565 | |
[email protected] | 3f55aa1 | 2011-12-07 02:03:33 | [diff] [blame] | 566 | // ConnectJobFactory implementation. |
[email protected] | 83039bb | 2011-12-09 18:43:55 | [diff] [blame] | 567 | |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 568 | std::unique_ptr<ConnectJob> NewConnectJob( |
Matt Menke | aafff54 | 2019-04-22 22:09:36 | [diff] [blame] | 569 | ClientSocketPool::GroupId group_id, |
| 570 | scoped_refptr<ClientSocketPool::SocketParams> socket_params, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 571 | const base::Optional<NetworkTrafficAnnotationTag>& proxy_annotation_tag, |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 572 | RequestPriority request_priority, |
| 573 | SocketTag socket_tag, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 574 | ConnectJob::Delegate* delegate) const override { |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 575 | EXPECT_TRUE(!job_types_ || !job_types_->empty()); |
| 576 | TestConnectJob::JobType job_type = job_type_; |
| 577 | if (job_types_ && !job_types_->empty()) { |
| 578 | job_type = job_types_->front(); |
| 579 | job_types_->pop_front(); |
| 580 | } |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 581 | return std::make_unique<TestConnectJob>( |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 582 | job_type, request_priority, socket_tag, timeout_duration_, |
| 583 | &common_connect_job_params_, delegate, client_socket_factory_); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | private: |
Matt Menke | a6f99ad | 2019-03-08 02:26:43 | [diff] [blame] | 587 | const CommonConnectJobParams common_connect_job_params_; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 588 | TestConnectJob::JobType job_type_; |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 589 | std::list<TestConnectJob::JobType>* job_types_; |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 590 | base::TimeDelta timeout_duration_; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 591 | MockClientSocketFactory* const client_socket_factory_; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 592 | |
| 593 | DISALLOW_COPY_AND_ASSIGN(TestConnectJobFactory); |
| 594 | }; |
| 595 | |
[email protected] | a937a06d | 2009-08-19 21:19:24 | [diff] [blame] | 596 | } // namespace |
| 597 | |
[email protected] | a937a06d | 2009-08-19 21:19:24 | [diff] [blame] | 598 | namespace { |
| 599 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 600 | void MockClientSocketFactory::SignalJobs() { |
jdoerrie | 22a91d8b9 | 2018-10-05 08:43:26 | [diff] [blame] | 601 | for (auto it = waiting_jobs_.begin(); it != waiting_jobs_.end(); ++it) { |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 602 | (*it)->Signal(); |
| 603 | } |
| 604 | waiting_jobs_.clear(); |
| 605 | } |
| 606 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 607 | void MockClientSocketFactory::SignalJob(size_t job) { |
| 608 | ASSERT_LT(job, waiting_jobs_.size()); |
| 609 | waiting_jobs_[job]->Signal(); |
| 610 | waiting_jobs_.erase(waiting_jobs_.begin() + job); |
| 611 | } |
| 612 | |
| 613 | void MockClientSocketFactory::SetJobLoadState(size_t job, |
| 614 | LoadState load_state) { |
| 615 | ASSERT_LT(job, waiting_jobs_.size()); |
| 616 | waiting_jobs_[job]->set_load_state(load_state); |
| 617 | } |
| 618 | |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 619 | void MockClientSocketFactory::SetJobHasEstablishedConnection(size_t job) { |
| 620 | ASSERT_LT(job, waiting_jobs_.size()); |
| 621 | waiting_jobs_[job]->set_has_established_connection(); |
| 622 | } |
| 623 | |
Bence Béky | 98447b1 | 2018-05-08 03:14:01 | [diff] [blame] | 624 | class ClientSocketPoolBaseTest : public TestWithScopedTaskEnvironment { |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 625 | protected: |
Alex Clarke | 0def209 | 2018-12-10 12:01:45 | [diff] [blame] | 626 | ClientSocketPoolBaseTest() |
| 627 | : TestWithScopedTaskEnvironment( |
| 628 | base::test::ScopedTaskEnvironment::MainThreadType::MOCK_TIME), |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 629 | params_(ClientSocketPool::SocketParams::CreateForHttpForTesting()) { |
[email protected] | 636b825 | 2011-04-08 19:56:54 | [diff] [blame] | 630 | connect_backup_jobs_enabled_ = |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 631 | TransportClientSocketPool::connect_backup_jobs_enabled(); |
| 632 | TransportClientSocketPool::set_connect_backup_jobs_enabled(true); |
[email protected] | 636b825 | 2011-04-08 19:56:54 | [diff] [blame] | 633 | } |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 634 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 635 | ~ClientSocketPoolBaseTest() override { |
Matt Menke | 16f5c2e5 | 2019-03-25 21:50:40 | [diff] [blame] | 636 | TransportClientSocketPool::set_connect_backup_jobs_enabled( |
[email protected] | 636b825 | 2011-04-08 19:56:54 | [diff] [blame] | 637 | connect_backup_jobs_enabled_); |
| 638 | } |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 639 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 640 | void CreatePool(int max_sockets, |
| 641 | int max_sockets_per_group, |
| 642 | bool enable_backup_connect_jobs = false) { |
Tarun Bansal | a763509 | 2019-02-20 10:00:59 | [diff] [blame] | 643 | CreatePoolWithIdleTimeouts(max_sockets, max_sockets_per_group, |
| 644 | kUnusedIdleSocketTimeout, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 645 | ClientSocketPool::used_idle_socket_timeout(), |
| 646 | enable_backup_connect_jobs); |
[email protected] | 9bf28db | 2009-08-29 01:35:16 | [diff] [blame] | 647 | } |
| 648 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 649 | void CreatePoolWithIdleTimeouts(int max_sockets, |
| 650 | int max_sockets_per_group, |
| 651 | base::TimeDelta unused_idle_socket_timeout, |
| 652 | base::TimeDelta used_idle_socket_timeout, |
| 653 | bool enable_backup_connect_jobs = false) { |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 654 | DCHECK(!pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 655 | std::unique_ptr<TestConnectJobFactory> connect_job_factory = |
| 656 | std::make_unique<TestConnectJobFactory>(&client_socket_factory_, |
| 657 | &net_log_); |
| 658 | connect_job_factory_ = connect_job_factory.get(); |
| 659 | pool_ = TransportClientSocketPool::CreateForTesting( |
| 660 | max_sockets, max_sockets_per_group, unused_idle_socket_timeout, |
| 661 | used_idle_socket_timeout, std::move(connect_job_factory), |
| 662 | nullptr /* ssl_config_service */, enable_backup_connect_jobs); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 663 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 664 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 665 | int StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 666 | const ClientSocketPool::GroupId& group_id, |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 667 | RequestPriority priority, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 668 | ClientSocketPool::RespectLimits respect_limits) { |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 669 | return test_base_.StartRequestUsingPool(pool_.get(), group_id, priority, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 670 | respect_limits, params_); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 671 | } |
| 672 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 673 | int StartRequest(const ClientSocketPool::GroupId& group_id, |
| 674 | RequestPriority priority) { |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 675 | return StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 676 | group_id, priority, ClientSocketPool::RespectLimits::ENABLED); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 677 | } |
| 678 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 679 | int GetOrderOfRequest(size_t index) const { |
| 680 | return test_base_.GetOrderOfRequest(index); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 681 | } |
| 682 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 683 | bool ReleaseOneConnection(ClientSocketPoolTest::KeepAlive keep_alive) { |
| 684 | return test_base_.ReleaseOneConnection(keep_alive); |
| 685 | } |
| 686 | |
| 687 | void ReleaseAllConnections(ClientSocketPoolTest::KeepAlive keep_alive) { |
| 688 | test_base_.ReleaseAllConnections(keep_alive); |
| 689 | } |
| 690 | |
| 691 | TestSocketRequest* request(int i) { return test_base_.request(i); } |
| 692 | size_t requests_size() const { return test_base_.requests_size(); } |
danakj | 655b66c | 2016-04-16 00:51:38 | [diff] [blame] | 693 | std::vector<std::unique_ptr<TestSocketRequest>>* requests() { |
olli.raula | 9d66b7d | 2015-11-23 08:30:42 | [diff] [blame] | 694 | return test_base_.requests(); |
| 695 | } |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 696 | // Only counts the requests that get sockets asynchronously; |
| 697 | // synchronous completions are not registered by this count. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 698 | size_t completion_count() const { return test_base_.completion_count(); } |
| 699 | |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 700 | TestNetLog net_log_; |
[email protected] | 636b825 | 2011-04-08 19:56:54 | [diff] [blame] | 701 | bool connect_backup_jobs_enabled_; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 702 | MockClientSocketFactory client_socket_factory_; |
[email protected] | 17a0c6c | 2009-08-04 00:07:04 | [diff] [blame] | 703 | TestConnectJobFactory* connect_job_factory_; |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 704 | // These parameters are never actually used to create a TransportConnectJob. |
Matt Menke | 84d11e56 | 2019-03-27 00:11:19 | [diff] [blame] | 705 | scoped_refptr<ClientSocketPool::SocketParams> params_; |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 706 | std::unique_ptr<TransportClientSocketPool> pool_; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 707 | ClientSocketPoolTest test_base_; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 708 | }; |
| 709 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 710 | TEST_F(ClientSocketPoolBaseTest, BasicSynchronous) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 711 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 712 | |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 713 | TestCompletionCallback callback; |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 714 | ClientSocketHandle handle; |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 715 | BoundTestNetLog log; |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 716 | TestLoadTimingInfoNotConnected(handle); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 717 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 718 | EXPECT_EQ(OK, handle.Init( |
| 719 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 720 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 721 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 722 | pool_.get(), log.bound())); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 723 | EXPECT_TRUE(handle.is_initialized()); |
| 724 | EXPECT_TRUE(handle.socket()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 725 | TestLoadTimingInfoConnectedNotReused(handle); |
| 726 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 727 | handle.Reset(); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 728 | TestLoadTimingInfoNotConnected(handle); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 729 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 730 | TestNetLogEntry::List entries; |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 731 | log.GetEntries(&entries); |
| 732 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 733 | EXPECT_EQ(5u, entries.size()); |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 734 | EXPECT_TRUE(LogContainsEvent( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 735 | entries, 0, NetLogEventType::TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 736 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 737 | EXPECT_TRUE(LogContainsBeginEvent(entries, 1, NetLogEventType::SOCKET_POOL)); |
| 738 | EXPECT_TRUE(LogContainsEvent( |
| 739 | entries, 2, NetLogEventType::SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
| 740 | NetLogEventPhase::NONE)); |
| 741 | EXPECT_TRUE(LogContainsEvent(entries, 3, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 742 | NetLogEventType::SOCKET_POOL_BOUND_TO_SOCKET, |
| 743 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 744 | EXPECT_TRUE(LogContainsEndEvent(entries, 4, NetLogEventType::SOCKET_POOL)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 745 | } |
| 746 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 747 | TEST_F(ClientSocketPoolBaseTest, InitConnectionFailure) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 748 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 749 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 750 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 751 | BoundTestNetLog log; |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 752 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 753 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 754 | TestCompletionCallback callback; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 755 | // Set the additional error state members to ensure that they get cleared. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 756 | handle.set_is_ssl_error(true); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 757 | handle.set_ssl_cert_request_info(base::MakeRefCounted<SSLCertRequestInfo>()); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 758 | EXPECT_EQ( |
| 759 | ERR_CONNECTION_FAILED, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 760 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 761 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 762 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 763 | pool_.get(), log.bound())); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 764 | EXPECT_FALSE(handle.socket()); |
| 765 | EXPECT_FALSE(handle.is_ssl_error()); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 766 | EXPECT_FALSE(handle.ssl_cert_request_info()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 767 | TestLoadTimingInfoNotConnected(handle); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 768 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 769 | TestNetLogEntry::List entries; |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 770 | log.GetEntries(&entries); |
| 771 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 772 | EXPECT_EQ(4u, entries.size()); |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 773 | EXPECT_TRUE(LogContainsEvent( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 774 | entries, 0, NetLogEventType::TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 775 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 776 | EXPECT_TRUE(LogContainsBeginEvent(entries, 1, NetLogEventType::SOCKET_POOL)); |
| 777 | EXPECT_TRUE(LogContainsEvent( |
| 778 | entries, 2, NetLogEventType::SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
| 779 | NetLogEventPhase::NONE)); |
| 780 | EXPECT_TRUE(LogContainsEndEvent(entries, 3, NetLogEventType::SOCKET_POOL)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 781 | } |
| 782 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 783 | // Make sure different groups do not share sockets. |
| 784 | TEST_F(ClientSocketPoolBaseTest, GroupSeparation) { |
| 785 | CreatePool(1000 /* max_sockets */, 2 /* max_sockets_per_group */); |
| 786 | |
| 787 | const HostPortPair kHostPortPairs[] = { |
| 788 | {"a", 80}, |
| 789 | {"a", 443}, |
| 790 | {"b", 80}, |
| 791 | }; |
| 792 | |
| 793 | const ClientSocketPool::SocketType kSocketTypes[] = { |
| 794 | ClientSocketPool::SocketType::kHttp, |
| 795 | ClientSocketPool::SocketType::kSsl, |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 796 | ClientSocketPool::SocketType::kFtp, |
| 797 | }; |
| 798 | |
Matt Menke | bdf77780 | 2019-04-22 19:38:59 | [diff] [blame] | 799 | const PrivacyMode kPrivacyModes[] = {PrivacyMode::PRIVACY_MODE_DISABLED, |
| 800 | PrivacyMode::PRIVACY_MODE_ENABLED}; |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 801 | |
| 802 | int total_idle_sockets = 0; |
| 803 | |
| 804 | // Walk through each GroupId, making sure that requesting a socket for one |
| 805 | // group does not return a previously connected socket for another group. |
| 806 | for (const auto& host_port_pair : kHostPortPairs) { |
| 807 | SCOPED_TRACE(host_port_pair.ToString()); |
| 808 | for (const auto& socket_type : kSocketTypes) { |
| 809 | SCOPED_TRACE(static_cast<int>(socket_type)); |
| 810 | for (const auto& privacy_mode : kPrivacyModes) { |
| 811 | SCOPED_TRACE(privacy_mode); |
| 812 | |
| 813 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 814 | |
| 815 | ClientSocketPool::GroupId group_id(host_port_pair, socket_type, |
| 816 | privacy_mode); |
| 817 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 818 | EXPECT_FALSE(pool_->HasGroupForTesting(group_id)); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 819 | |
| 820 | TestCompletionCallback callback; |
| 821 | ClientSocketHandle handle; |
| 822 | |
| 823 | // Since the group is empty, requesting a socket should not complete |
| 824 | // synchronously. |
| 825 | EXPECT_THAT( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 826 | handle.Init(group_id, params_, base::nullopt, DEFAULT_PRIORITY, |
| 827 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 828 | callback.callback(), |
| 829 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 830 | NetLogWithSource()), |
| 831 | IsError(ERR_IO_PENDING)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 832 | EXPECT_TRUE(pool_->HasGroupForTesting(group_id)); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 833 | EXPECT_EQ(total_idle_sockets, pool_->IdleSocketCount()); |
| 834 | |
| 835 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 836 | EXPECT_TRUE(handle.socket()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 837 | EXPECT_TRUE(pool_->HasGroupForTesting(group_id)); |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 838 | EXPECT_EQ(total_idle_sockets, pool_->IdleSocketCount()); |
| 839 | |
| 840 | // Return socket to pool. |
| 841 | handle.Reset(); |
| 842 | EXPECT_EQ(total_idle_sockets + 1, pool_->IdleSocketCount()); |
| 843 | |
| 844 | // Requesting a socket again should return the same socket as before, so |
| 845 | // should complete synchronously. |
| 846 | EXPECT_THAT( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 847 | handle.Init(group_id, params_, base::nullopt, DEFAULT_PRIORITY, |
| 848 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 849 | callback.callback(), |
| 850 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 851 | NetLogWithSource()), |
| 852 | IsOk()); |
| 853 | EXPECT_TRUE(handle.socket()); |
| 854 | EXPECT_EQ(total_idle_sockets, pool_->IdleSocketCount()); |
| 855 | |
| 856 | // Return socket to pool again. |
| 857 | handle.Reset(); |
| 858 | EXPECT_EQ(total_idle_sockets + 1, pool_->IdleSocketCount()); |
| 859 | |
| 860 | ++total_idle_sockets; |
| 861 | } |
| 862 | } |
| 863 | } |
| 864 | } |
| 865 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 866 | TEST_F(ClientSocketPoolBaseTest, TotalLimit) { |
| 867 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 868 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 869 | // TODO(eroman): Check that the NetLog contains this event. |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 870 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 871 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 872 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
| 873 | EXPECT_THAT(StartRequest(TestGroupId("c"), DEFAULT_PRIORITY), IsOk()); |
| 874 | EXPECT_THAT(StartRequest(TestGroupId("d"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 875 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 876 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 877 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 878 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 879 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 880 | EXPECT_THAT(StartRequest(TestGroupId("e"), DEFAULT_PRIORITY), |
| 881 | IsError(ERR_IO_PENDING)); |
| 882 | EXPECT_THAT(StartRequest(TestGroupId("f"), DEFAULT_PRIORITY), |
| 883 | IsError(ERR_IO_PENDING)); |
| 884 | EXPECT_THAT(StartRequest(TestGroupId("g"), DEFAULT_PRIORITY), |
| 885 | IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 886 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 887 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 888 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 889 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 890 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 891 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 892 | |
| 893 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 894 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 895 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 896 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 897 | EXPECT_EQ(5, GetOrderOfRequest(5)); |
| 898 | EXPECT_EQ(6, GetOrderOfRequest(6)); |
| 899 | EXPECT_EQ(7, GetOrderOfRequest(7)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 900 | |
| 901 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 902 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(8)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 903 | } |
| 904 | |
| 905 | TEST_F(ClientSocketPoolBaseTest, TotalLimitReachedNewGroup) { |
| 906 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 907 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 908 | // TODO(eroman): Check that the NetLog contains this event. |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 909 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 910 | // Reach all limits: max total sockets, and max sockets per group. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 911 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 912 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 913 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
| 914 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 915 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 916 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 917 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 918 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 919 | |
| 920 | // Now create a new group and verify that we don't starve it. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 921 | EXPECT_THAT(StartRequest(TestGroupId("c"), DEFAULT_PRIORITY), |
| 922 | IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 923 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 924 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 925 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 926 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 927 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 928 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 929 | |
| 930 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 931 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 932 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 933 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 934 | EXPECT_EQ(5, GetOrderOfRequest(5)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 935 | |
| 936 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 937 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(6)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 938 | } |
| 939 | |
| 940 | TEST_F(ClientSocketPoolBaseTest, TotalLimitRespectsPriority) { |
| 941 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 942 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 943 | EXPECT_THAT(StartRequest(TestGroupId("b"), LOWEST), IsOk()); |
| 944 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsOk()); |
| 945 | EXPECT_THAT(StartRequest(TestGroupId("b"), HIGHEST), IsOk()); |
| 946 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 947 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 948 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 949 | client_socket_factory_.allocation_count()); |
| 950 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 951 | EXPECT_THAT(StartRequest(TestGroupId("c"), LOWEST), IsError(ERR_IO_PENDING)); |
| 952 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 953 | EXPECT_THAT(StartRequest(TestGroupId("b"), HIGHEST), IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 954 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 955 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 956 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 957 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 958 | |
| 959 | // First 4 requests don't have to wait, and finish in order. |
| 960 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 961 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 962 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 963 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 964 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 965 | // Request ("b", HIGHEST) has the highest priority, then (TestGroupId("a"), |
| 966 | // MEDIUM), and then ("c", LOWEST). |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 967 | EXPECT_EQ(7, GetOrderOfRequest(5)); |
| 968 | EXPECT_EQ(6, GetOrderOfRequest(6)); |
| 969 | EXPECT_EQ(5, GetOrderOfRequest(7)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 970 | |
| 971 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 972 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(9)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 973 | } |
| 974 | |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 975 | // Test reprioritizing a request before completion doesn't interfere with |
| 976 | // its completion. |
| 977 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeOne) { |
| 978 | CreatePool(kDefaultMaxSockets, 1); |
| 979 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 980 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 981 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 982 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 983 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 984 | |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 985 | request(1)->handle()->SetPriority(HIGHEST); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 986 | |
| 987 | ReleaseOneConnection(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 988 | |
| 989 | EXPECT_TRUE(request(1)->handle()->socket()); |
| 990 | } |
| 991 | |
| 992 | // Reprioritize a request up past another one and make sure that changes the |
| 993 | // completion order. |
| 994 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeUpReorder) { |
| 995 | CreatePool(kDefaultMaxSockets, 1); |
| 996 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 997 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 998 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 999 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1000 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1001 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1002 | EXPECT_FALSE(request(2)->handle()->socket()); |
| 1003 | |
| 1004 | request(2)->handle()->SetPriority(HIGHEST); |
| 1005 | |
| 1006 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1007 | |
| 1008 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1009 | EXPECT_EQ(3, GetOrderOfRequest(2)); |
| 1010 | EXPECT_EQ(2, GetOrderOfRequest(3)); |
| 1011 | } |
| 1012 | |
| 1013 | // Reprioritize a request without changing relative priorities and check |
| 1014 | // that the order doesn't change. |
| 1015 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeUpNoReorder) { |
| 1016 | CreatePool(kDefaultMaxSockets, 1); |
| 1017 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1018 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1019 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1020 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1021 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1022 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1023 | EXPECT_FALSE(request(2)->handle()->socket()); |
| 1024 | |
| 1025 | request(2)->handle()->SetPriority(MEDIUM); |
| 1026 | |
| 1027 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1028 | |
| 1029 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1030 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1031 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 1032 | } |
| 1033 | |
| 1034 | // Reprioritize a request past down another one and make sure that changes the |
| 1035 | // completion order. |
| 1036 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeDownReorder) { |
| 1037 | CreatePool(kDefaultMaxSockets, 1); |
| 1038 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1039 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1040 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1041 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1042 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1043 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1044 | EXPECT_FALSE(request(2)->handle()->socket()); |
| 1045 | |
| 1046 | request(1)->handle()->SetPriority(LOW); |
| 1047 | |
| 1048 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1049 | |
| 1050 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1051 | EXPECT_EQ(3, GetOrderOfRequest(2)); |
| 1052 | EXPECT_EQ(2, GetOrderOfRequest(3)); |
| 1053 | } |
| 1054 | |
| 1055 | // Reprioritize a request to the same level as another and confirm it is |
| 1056 | // put after the old request. |
| 1057 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeResetFIFO) { |
| 1058 | CreatePool(kDefaultMaxSockets, 1); |
| 1059 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1060 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(OK)); |
| 1061 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1062 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
rdsmith | 29dbad1 | 2017-02-17 02:22:18 | [diff] [blame] | 1063 | EXPECT_TRUE(request(0)->handle()->socket()); |
| 1064 | EXPECT_FALSE(request(1)->handle()->socket()); |
| 1065 | EXPECT_FALSE(request(2)->handle()->socket()); |
| 1066 | |
| 1067 | request(1)->handle()->SetPriority(MEDIUM); |
| 1068 | |
| 1069 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 1070 | |
| 1071 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1072 | EXPECT_EQ(3, GetOrderOfRequest(2)); |
| 1073 | EXPECT_EQ(2, GetOrderOfRequest(3)); |
| 1074 | } |
| 1075 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1076 | TEST_F(ClientSocketPoolBaseTest, TotalLimitRespectsGroupLimit) { |
| 1077 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1078 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1079 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsOk()); |
| 1080 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsOk()); |
| 1081 | EXPECT_THAT(StartRequest(TestGroupId("b"), HIGHEST), IsOk()); |
| 1082 | EXPECT_THAT(StartRequest(TestGroupId("b"), MEDIUM), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1083 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1084 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1085 | client_socket_factory_.allocation_count()); |
| 1086 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1087 | EXPECT_THAT(StartRequest(TestGroupId("c"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1088 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 1089 | EXPECT_THAT(StartRequest(TestGroupId("b"), HIGHEST), IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1090 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1091 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1092 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1093 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1094 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1095 | EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1096 | |
| 1097 | // First 4 requests don't have to wait, and finish in order. |
| 1098 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1099 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1100 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 1101 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 1102 | |
| 1103 | // Request ("b", 7) has the highest priority, but we can't make new socket for |
| 1104 | // group "b", because it has reached the per-group limit. Then we make |
| 1105 | // socket for ("c", 6), because it has higher priority than ("a", 4), |
| 1106 | // and we still can't make a socket for group "b". |
| 1107 | EXPECT_EQ(5, GetOrderOfRequest(5)); |
| 1108 | EXPECT_EQ(6, GetOrderOfRequest(6)); |
| 1109 | EXPECT_EQ(7, GetOrderOfRequest(7)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1110 | |
| 1111 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1112 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(8)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1113 | } |
| 1114 | |
| 1115 | // Make sure that we count connecting sockets against the total limit. |
| 1116 | TEST_F(ClientSocketPoolBaseTest, TotalLimitCountsConnectingSockets) { |
| 1117 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1118 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1119 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1120 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
| 1121 | EXPECT_THAT(StartRequest(TestGroupId("c"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1122 | |
| 1123 | // Create one asynchronous request. |
| 1124 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1125 | EXPECT_THAT(StartRequest(TestGroupId("d"), DEFAULT_PRIORITY), |
| 1126 | IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1127 | |
[email protected] | 6b17538 | 2009-10-13 06:47:47 | [diff] [blame] | 1128 | // We post all of our delayed tasks with a 2ms delay. I.e. they don't |
| 1129 | // actually become pending until 2ms after they have been created. In order |
| 1130 | // to flush all tasks, we need to wait so that we know there are no |
| 1131 | // soon-to-be-pending tasks waiting. |
Alex Clarke | 0def209 | 2018-12-10 12:01:45 | [diff] [blame] | 1132 | FastForwardBy(base::TimeDelta::FromMilliseconds(10)); |
[email protected] | 6b17538 | 2009-10-13 06:47:47 | [diff] [blame] | 1133 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1134 | // The next synchronous request should wait for its turn. |
| 1135 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1136 | EXPECT_THAT(StartRequest(TestGroupId("e"), DEFAULT_PRIORITY), |
| 1137 | IsError(ERR_IO_PENDING)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1138 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1139 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1140 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1141 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1142 | client_socket_factory_.allocation_count()); |
| 1143 | |
| 1144 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1145 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1146 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 1147 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1148 | EXPECT_EQ(5, GetOrderOfRequest(5)); |
| 1149 | |
| 1150 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1151 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(6)); |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1152 | } |
| 1153 | |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1154 | TEST_F(ClientSocketPoolBaseTest, CorrectlyCountStalledGroups) { |
| 1155 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 1156 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 1157 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1158 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1159 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1160 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1161 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1162 | |
| 1163 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 1164 | |
| 1165 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1166 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1167 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), |
| 1168 | IsError(ERR_IO_PENDING)); |
| 1169 | EXPECT_THAT(StartRequest(TestGroupId("c"), DEFAULT_PRIORITY), |
| 1170 | IsError(ERR_IO_PENDING)); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1171 | |
| 1172 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1173 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1174 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1175 | EXPECT_EQ(kDefaultMaxSockets + 1, client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1176 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1177 | EXPECT_EQ(kDefaultMaxSockets + 2, client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1178 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
| 1179 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 1180 | EXPECT_EQ(kDefaultMaxSockets + 2, client_socket_factory_.allocation_count()); |
| 1181 | } |
| 1182 | |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 1183 | TEST_F(ClientSocketPoolBaseTest, StallAndThenCancelAndTriggerAvailableSocket) { |
| 1184 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 1185 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1186 | |
| 1187 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1188 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1189 | EXPECT_EQ( |
| 1190 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1191 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1192 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1193 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1194 | pool_.get(), NetLogWithSource())); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 1195 | |
| 1196 | ClientSocketHandle handles[4]; |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 1197 | for (size_t i = 0; i < base::size(handles); ++i) { |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1198 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1199 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1200 | handles[i].Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1201 | TestGroupId("b"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1202 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1203 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1204 | pool_.get(), NetLogWithSource())); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 1205 | } |
| 1206 | |
| 1207 | // One will be stalled, cancel all the handles now. |
| 1208 | // This should hit the OnAvailableSocketSlot() code where we previously had |
| 1209 | // stalled groups, but no longer have any. |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 1210 | for (size_t i = 0; i < base::size(handles); ++i) |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 1211 | handles[i].Reset(); |
| 1212 | } |
| 1213 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1214 | TEST_F(ClientSocketPoolBaseTest, CancelStalledSocketAtSocketLimit) { |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1215 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1216 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 1217 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1218 | { |
| 1219 | ClientSocketHandle handles[kDefaultMaxSockets]; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1220 | TestCompletionCallback callbacks[kDefaultMaxSockets]; |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1221 | for (int i = 0; i < kDefaultMaxSockets; ++i) { |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1222 | EXPECT_EQ(OK, |
| 1223 | handles[i].Init(TestGroupId(base::NumberToString(i)), params_, |
| 1224 | base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
| 1225 | ClientSocketPool::RespectLimits::ENABLED, |
| 1226 | callbacks[i].callback(), |
| 1227 | ClientSocketPool::ProxyAuthCallback(), |
| 1228 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1229 | } |
| 1230 | |
| 1231 | // Force a stalled group. |
| 1232 | ClientSocketHandle stalled_handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1233 | TestCompletionCallback callback; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1234 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1235 | stalled_handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1236 | TestGroupId("foo"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1237 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1238 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1239 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1240 | |
| 1241 | // Cancel the stalled request. |
| 1242 | stalled_handle.Reset(); |
| 1243 | |
| 1244 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1245 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 1246 | |
| 1247 | // Dropping out of scope will close all handles and return them to idle. |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1248 | } |
| 1249 | |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1250 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1251 | EXPECT_EQ(kDefaultMaxSockets, pool_->IdleSocketCount()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1252 | } |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1253 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1254 | TEST_F(ClientSocketPoolBaseTest, CancelPendingSocketAtSocketLimit) { |
| 1255 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1256 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 1257 | |
| 1258 | { |
| 1259 | ClientSocketHandle handles[kDefaultMaxSockets]; |
| 1260 | for (int i = 0; i < kDefaultMaxSockets; ++i) { |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1261 | TestCompletionCallback callback; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1262 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1263 | handles[i].Init(TestGroupId(base::NumberToString(i)), params_, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1264 | base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1265 | ClientSocketPool::RespectLimits::ENABLED, |
| 1266 | callback.callback(), |
| 1267 | ClientSocketPool::ProxyAuthCallback(), |
| 1268 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1269 | } |
| 1270 | |
| 1271 | // Force a stalled group. |
| 1272 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1273 | ClientSocketHandle stalled_handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1274 | TestCompletionCallback callback; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1275 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1276 | stalled_handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1277 | TestGroupId("foo"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1278 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1279 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1280 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1281 | |
| 1282 | // Since it is stalled, it should have no connect jobs. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1283 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("foo"))); |
| 1284 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 1285 | TestGroupId("foo"))); |
| 1286 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroupForTesting( |
| 1287 | TestGroupId("foo"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1288 | |
| 1289 | // Cancel the stalled request. |
| 1290 | handles[0].Reset(); |
| 1291 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1292 | // Now we should have a connect job. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1293 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("foo"))); |
| 1294 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 1295 | TestGroupId("foo"))); |
| 1296 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroupForTesting( |
| 1297 | TestGroupId("foo"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1298 | |
| 1299 | // The stalled socket should connect. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1300 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1301 | |
| 1302 | EXPECT_EQ(kDefaultMaxSockets + 1, |
| 1303 | client_socket_factory_.allocation_count()); |
| 1304 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1305 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("foo"))); |
| 1306 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 1307 | TestGroupId("foo"))); |
| 1308 | EXPECT_EQ(0u, pool_->NumUnassignedConnectJobsInGroupForTesting( |
| 1309 | TestGroupId("foo"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1310 | |
| 1311 | // Dropping out of scope will close all handles and return them to idle. |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1312 | } |
| 1313 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1314 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 1315 | } |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1316 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1317 | TEST_F(ClientSocketPoolBaseTest, WaitForStalledSocketAtSocketLimit) { |
| 1318 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1319 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1320 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1321 | ClientSocketHandle stalled_handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1322 | TestCompletionCallback callback; |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1323 | { |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 1324 | EXPECT_FALSE(pool_->IsStalled()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1325 | ClientSocketHandle handles[kDefaultMaxSockets]; |
| 1326 | for (int i = 0; i < kDefaultMaxSockets; ++i) { |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1327 | TestCompletionCallback callback; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1328 | EXPECT_EQ( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1329 | OK, handles[i].Init( |
| 1330 | TestGroupId(base::StringPrintf("Take 2: %d", i)), params_, |
| 1331 | base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
| 1332 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1333 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 1334 | NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1335 | } |
| 1336 | |
| 1337 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1338 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 1339 | EXPECT_FALSE(pool_->IsStalled()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1340 | |
| 1341 | // Now we will hit the socket limit. |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 1342 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1343 | stalled_handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1344 | TestGroupId("foo"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1345 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1346 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1347 | pool_.get(), NetLogWithSource())); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 1348 | EXPECT_TRUE(pool_->IsStalled()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1349 | |
| 1350 | // Dropping out of scope will close all handles and return them to idle. |
| 1351 | } |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1352 | |
| 1353 | // But if we wait for it, the released idle sockets will be closed in |
| 1354 | // preference of the waiting request. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1355 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1356 | |
| 1357 | EXPECT_EQ(kDefaultMaxSockets + 1, client_socket_factory_.allocation_count()); |
| 1358 | EXPECT_EQ(3, pool_->IdleSocketCount()); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1359 | } |
| 1360 | |
| 1361 | // Regression test for http://crbug.com/40952. |
| 1362 | TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketAtSocketLimitDeleteGroup) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1363 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 1364 | true /* enable_backup_connect_jobs */); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1365 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 1366 | |
| 1367 | for (int i = 0; i < kDefaultMaxSockets; ++i) { |
| 1368 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1369 | TestCompletionCallback callback; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1370 | EXPECT_EQ(OK, handle.Init(TestGroupId(base::NumberToString(i)), params_, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1371 | base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1372 | ClientSocketPool::RespectLimits::ENABLED, |
| 1373 | callback.callback(), |
| 1374 | ClientSocketPool::ProxyAuthCallback(), |
| 1375 | pool_.get(), NetLogWithSource())); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1376 | } |
| 1377 | |
| 1378 | // Flush all the DoReleaseSocket tasks. |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 1379 | base::RunLoop().RunUntilIdle(); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1380 | |
| 1381 | // Stall a group. Set a pending job so it'll trigger a backup job if we don't |
| 1382 | // reuse a socket. |
| 1383 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1384 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1385 | TestCompletionCallback callback; |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1386 | |
| 1387 | // "0" is special here, since it should be the first entry in the sorted map, |
| 1388 | // which is the one which we would close an idle socket for. We shouldn't |
| 1389 | // close an idle socket though, since we should reuse the idle socket. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1390 | EXPECT_EQ(OK, handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1391 | TestGroupId("0"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1392 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1393 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1394 | pool_.get(), NetLogWithSource())); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1395 | |
| 1396 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1397 | EXPECT_EQ(kDefaultMaxSockets - 1, pool_->IdleSocketCount()); |
| 1398 | } |
| 1399 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1400 | TEST_F(ClientSocketPoolBaseTest, PendingRequests) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1401 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1402 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1403 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1404 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1405 | EXPECT_THAT(StartRequest(TestGroupId("a"), IDLE), IsError(ERR_IO_PENDING)); |
| 1406 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
| 1407 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1408 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1409 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 1410 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1411 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1412 | ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1413 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
| 1414 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1415 | EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup, |
| 1416 | completion_count()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1417 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1418 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1419 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
[email protected] | c9c6f5c | 2010-07-31 01:30:03 | [diff] [blame] | 1420 | EXPECT_EQ(8, GetOrderOfRequest(3)); |
| 1421 | EXPECT_EQ(6, GetOrderOfRequest(4)); |
| 1422 | EXPECT_EQ(4, GetOrderOfRequest(5)); |
| 1423 | EXPECT_EQ(3, GetOrderOfRequest(6)); |
| 1424 | EXPECT_EQ(5, GetOrderOfRequest(7)); |
| 1425 | EXPECT_EQ(7, GetOrderOfRequest(8)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1426 | |
| 1427 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1428 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(9)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1429 | } |
| 1430 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1431 | TEST_F(ClientSocketPoolBaseTest, PendingRequests_NoKeepAlive) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1432 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1433 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1434 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1435 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1436 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
| 1437 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1438 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1439 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 1440 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1441 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1442 | ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1443 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1444 | for (size_t i = kDefaultMaxSocketsPerGroup; i < requests_size(); ++i) |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1445 | EXPECT_THAT(request(i)->WaitForResult(), IsOk()); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1446 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1447 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1448 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1449 | EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup, |
| 1450 | completion_count()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1451 | } |
| 1452 | |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1453 | TEST_F(ClientSocketPoolBaseTest, ResetAndCloseSocket) { |
| 1454 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1455 | |
| 1456 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1457 | ClientSocketHandle handle; |
| 1458 | TestCompletionCallback callback; |
| 1459 | EXPECT_EQ( |
| 1460 | ERR_IO_PENDING, |
| 1461 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1462 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1463 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1464 | pool_.get(), NetLogWithSource())); |
| 1465 | |
| 1466 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 1467 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1468 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1469 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
| 1470 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 1471 | |
| 1472 | handle.ResetAndCloseSocket(); |
| 1473 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1474 | } |
| 1475 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1476 | // This test will start up a RequestSocket() and then immediately Cancel() it. |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1477 | // The pending ConnectJob will not be cancelled. |
| 1478 | TEST_F(ClientSocketPoolBaseTest, CancelRequestKeepsConnectJob) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1479 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1480 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1481 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1482 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1483 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1484 | EXPECT_EQ( |
| 1485 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1486 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1487 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1488 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1489 | pool_.get(), NetLogWithSource())); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1490 | handle.Reset(); |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1491 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1492 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1493 | } |
| 1494 | |
| 1495 | // This test will start up a RequestSocket() and then immediately Cancel() it |
| 1496 | // and kill the ConnectJob. The pending ConnectJob should be destroyed. |
| 1497 | TEST_F(ClientSocketPoolBaseTest, CancelRequestAndCloseSocket) { |
| 1498 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1499 | |
| 1500 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1501 | ClientSocketHandle handle; |
| 1502 | TestCompletionCallback callback; |
| 1503 | EXPECT_EQ( |
| 1504 | ERR_IO_PENDING, |
| 1505 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1506 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1507 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1508 | pool_.get(), NetLogWithSource())); |
| 1509 | handle.ResetAndCloseSocket(); |
| 1510 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1511 | } |
| 1512 | |
| 1513 | TEST_F(ClientSocketPoolBaseTest, |
| 1514 | CancelRequestAndCloseSocketWhenMoreRequestsThanConnectJobs) { |
| 1515 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1516 | |
| 1517 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1518 | |
| 1519 | std::vector<std::unique_ptr<ClientSocketHandle>> handles; |
| 1520 | TestCompletionCallback callback; |
| 1521 | // Make |kDefaultMaxSockets + 1| socket requests. |
| 1522 | for (int i = 0; i < kDefaultMaxSocketsPerGroup + 1; ++i) { |
| 1523 | std::unique_ptr<ClientSocketHandle> handle = |
| 1524 | std::make_unique<ClientSocketHandle>(); |
| 1525 | EXPECT_EQ( |
| 1526 | ERR_IO_PENDING, |
| 1527 | handle->Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1528 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1529 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1530 | pool_.get(), NetLogWithSource())); |
| 1531 | handles.push_back(std::move(handle)); |
| 1532 | if (i < kDefaultMaxSocketsPerGroup) { |
| 1533 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1534 | EXPECT_EQ(static_cast<size_t>(i + 1), |
| 1535 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1536 | } |
| 1537 | } |
| 1538 | |
| 1539 | // There should now be exactly |kDefaultMaxSockets| sockets. |
| 1540 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1541 | EXPECT_EQ(static_cast<size_t>(kDefaultMaxSocketsPerGroup), |
| 1542 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1543 | |
| 1544 | // Calling ResetAndCloseSocket() on a handle should not cancel a ConnectJob, |
| 1545 | // since there are more requests than ConnectJobs. |
| 1546 | handles[0]->ResetAndCloseSocket(); |
| 1547 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1548 | EXPECT_EQ(static_cast<size_t>(kDefaultMaxSocketsPerGroup), |
| 1549 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1550 | |
| 1551 | // Calling ResetAndCloseSocket() on another handle should cancel a ConnectJob, |
| 1552 | // since there are just as many requests as ConnectJobs. |
| 1553 | handles[1]->ResetAndCloseSocket(); |
| 1554 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1555 | EXPECT_EQ(static_cast<size_t>(kDefaultMaxSocketsPerGroup - 1), |
| 1556 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1557 | } |
| 1558 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1559 | TEST_F(ClientSocketPoolBaseTest, ConnectCancelConnect) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1560 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1561 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1562 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 1563 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1564 | TestCompletionCallback callback; |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1565 | |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1566 | EXPECT_EQ( |
| 1567 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1568 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1569 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1570 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1571 | pool_.get(), NetLogWithSource())); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1572 | |
| 1573 | handle.Reset(); |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1574 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1575 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1576 | |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1577 | // This will create a second ConnectJob, since the other ConnectJob was |
| 1578 | // previously assigned to a request. |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1579 | TestCompletionCallback callback2; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1580 | EXPECT_EQ( |
| 1581 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1582 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1583 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1584 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1585 | pool_.get(), NetLogWithSource())); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1586 | |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1587 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1588 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 1589 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1590 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1591 | EXPECT_FALSE(callback.have_result()); |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1592 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1593 | // One ConnectJob completed, and its socket is now assigned to |handle|. |
| 1594 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 1595 | // The other ConnectJob should have either completed, or still be connecting. |
| 1596 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")) + |
| 1597 | pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1598 | |
| 1599 | handle.Reset(); |
Matt Menke | 7eb405e | 2019-04-25 20:48:21 | [diff] [blame] | 1600 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 1601 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")) + |
| 1602 | pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
| 1603 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1604 | } |
| 1605 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1606 | TEST_F(ClientSocketPoolBaseTest, CancelRequest) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1607 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1608 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1609 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1610 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1611 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
| 1612 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 1613 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
| 1614 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 1615 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1616 | |
| 1617 | // Cancel a request. |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1618 | size_t index_to_cancel = kDefaultMaxSocketsPerGroup + 2; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1619 | EXPECT_FALSE((*requests())[index_to_cancel]->handle()->is_initialized()); |
| 1620 | (*requests())[index_to_cancel]->handle()->Reset(); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1621 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1622 | ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1623 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1624 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
| 1625 | client_socket_factory_.allocation_count()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1626 | EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup - 1, |
| 1627 | completion_count()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1628 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1629 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 1630 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 1631 | EXPECT_EQ(5, GetOrderOfRequest(3)); |
| 1632 | EXPECT_EQ(3, GetOrderOfRequest(4)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1633 | EXPECT_EQ(ClientSocketPoolTest::kRequestNotFound, |
| 1634 | GetOrderOfRequest(5)); // Canceled request. |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1635 | EXPECT_EQ(4, GetOrderOfRequest(6)); |
| 1636 | EXPECT_EQ(6, GetOrderOfRequest(7)); |
[email protected] | 75439d3b | 2009-07-23 22:11:17 | [diff] [blame] | 1637 | |
| 1638 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1639 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(8)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1640 | } |
| 1641 | |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1642 | // Function to be used as a callback on socket request completion. It first |
| 1643 | // disconnects the successfully connected socket from the first request, and |
| 1644 | // then reuses the ClientSocketHandle to request another socket. |
| 1645 | // |
| 1646 | // |nested_callback| is called with the result of the second socket request. |
| 1647 | void RequestSocketOnComplete(ClientSocketHandle* handle, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1648 | TransportClientSocketPool* pool, |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1649 | TestConnectJobFactory* test_connect_job_factory, |
| 1650 | TestConnectJob::JobType next_job_type, |
Bence Béky | a4a5093 | 2018-08-10 13:39:41 | [diff] [blame] | 1651 | TestCompletionCallback* nested_callback, |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1652 | int first_request_result) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1653 | EXPECT_THAT(first_request_result, IsOk()); |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1654 | |
| 1655 | test_connect_job_factory->set_job_type(next_job_type); |
| 1656 | |
| 1657 | // Don't allow reuse of the socket. Disconnect it and then release it. |
| 1658 | if (handle->socket()) |
| 1659 | handle->socket()->Disconnect(); |
| 1660 | handle->Reset(); |
| 1661 | |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1662 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1663 | int rv = handle->Init( |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 1664 | TestGroupId("a"), |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1665 | ClientSocketPool::SocketParams::CreateForHttpForTesting(), base::nullopt, |
| 1666 | LOWEST, SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 1667 | nested_callback->callback(), ClientSocketPool::ProxyAuthCallback(), pool, |
| 1668 | NetLogWithSource()); |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1669 | if (rv != ERR_IO_PENDING) { |
| 1670 | DCHECK_EQ(TestConnectJob::kMockJob, next_job_type); |
Bence Béky | a4a5093 | 2018-08-10 13:39:41 | [diff] [blame] | 1671 | nested_callback->callback().Run(rv); |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1672 | } else { |
| 1673 | DCHECK_EQ(TestConnectJob::kMockPendingJob, next_job_type); |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1674 | } |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1675 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1676 | |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1677 | // Tests the case where a second socket is requested in a completion callback, |
| 1678 | // and the second socket connects asynchronously. Reuses the same |
| 1679 | // ClientSocketHandle for the second socket, after disconnecting the first. |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1680 | TEST_F(ClientSocketPoolBaseTest, RequestPendingJobTwice) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1681 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1682 | |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 1683 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 1684 | ClientSocketHandle handle; |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1685 | TestCompletionCallback second_result_callback; |
| 1686 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1687 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Paul Jensen | 8d6f87ec | 2018-01-13 00:46:54 | [diff] [blame] | 1688 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1689 | base::BindOnce(&RequestSocketOnComplete, &handle, pool_.get(), |
| 1690 | connect_job_factory_, TestConnectJob::kMockPendingJob, |
| 1691 | &second_result_callback), |
| 1692 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1693 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1694 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1695 | EXPECT_THAT(second_result_callback.WaitForResult(), IsOk()); |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1696 | } |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1697 | |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1698 | // Tests the case where a second socket is requested in a completion callback, |
| 1699 | // and the second socket connects synchronously. Reuses the same |
| 1700 | // ClientSocketHandle for the second socket, after disconnecting the first. |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1701 | TEST_F(ClientSocketPoolBaseTest, RequestPendingJobThenSynchronous) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1702 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1703 | |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 1704 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | a512f598 | 2009-08-18 16:01:06 | [diff] [blame] | 1705 | ClientSocketHandle handle; |
mmenke | 33d24423d | 2015-05-19 19:41:09 | [diff] [blame] | 1706 | TestCompletionCallback second_result_callback; |
| 1707 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1708 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Paul Jensen | 8d6f87ec | 2018-01-13 00:46:54 | [diff] [blame] | 1709 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1710 | base::BindOnce(&RequestSocketOnComplete, &handle, pool_.get(), |
| 1711 | connect_job_factory_, TestConnectJob::kMockPendingJob, |
| 1712 | &second_result_callback), |
| 1713 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1714 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 1715 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1716 | EXPECT_THAT(second_result_callback.WaitForResult(), IsOk()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1717 | } |
| 1718 | |
| 1719 | // Make sure that pending requests get serviced after active requests get |
| 1720 | // cancelled. |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1721 | TEST_F(ClientSocketPoolBaseTest, CancelActiveRequestWithPendingRequests) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1722 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1723 | |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 1724 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1725 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1726 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1727 | IsError(ERR_IO_PENDING)); |
| 1728 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1729 | IsError(ERR_IO_PENDING)); |
| 1730 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1731 | IsError(ERR_IO_PENDING)); |
| 1732 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1733 | IsError(ERR_IO_PENDING)); |
| 1734 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1735 | IsError(ERR_IO_PENDING)); |
| 1736 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1737 | IsError(ERR_IO_PENDING)); |
| 1738 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1739 | IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1740 | |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1741 | // Now, kDefaultMaxSocketsPerGroup requests should be active. |
| 1742 | // Let's cancel them. |
| 1743 | for (int i = 0; i < kDefaultMaxSocketsPerGroup; ++i) { |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1744 | ASSERT_FALSE(request(i)->handle()->is_initialized()); |
| 1745 | request(i)->handle()->Reset(); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1746 | } |
| 1747 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1748 | // Let's wait for the rest to complete now. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1749 | for (size_t i = kDefaultMaxSocketsPerGroup; i < requests_size(); ++i) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1750 | EXPECT_THAT(request(i)->WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1751 | request(i)->handle()->Reset(); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1752 | } |
| 1753 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1754 | EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup, |
| 1755 | completion_count()); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1756 | } |
| 1757 | |
| 1758 | // Make sure that pending requests get serviced after active requests fail. |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 1759 | TEST_F(ClientSocketPoolBaseTest, FailingActiveRequestWithPendingRequests) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1760 | const size_t kMaxSockets = 5; |
| 1761 | CreatePool(kMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | c9d6a1d | 2009-07-14 16:15:20 | [diff] [blame] | 1762 | |
[email protected] | 0b7648c | 2009-07-06 20:14:01 | [diff] [blame] | 1763 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1764 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1765 | const size_t kNumberOfRequests = 2 * kDefaultMaxSocketsPerGroup + 1; |
| 1766 | ASSERT_LE(kNumberOfRequests, kMaxSockets); // Otherwise the test will hang. |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1767 | |
| 1768 | // Queue up all the requests |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1769 | for (size_t i = 0; i < kNumberOfRequests; ++i) |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1770 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1771 | IsError(ERR_IO_PENDING)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1772 | |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1773 | for (size_t i = 0; i < kNumberOfRequests; ++i) |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1774 | EXPECT_THAT(request(i)->WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 1775 | } |
| 1776 | |
mmenke | 9d72fe4 | 2017-05-18 22:36:07 | [diff] [blame] | 1777 | // Make sure that pending requests that complete synchronously get serviced |
| 1778 | // after active requests fail. See https://crbug.com/723748 |
| 1779 | TEST_F(ClientSocketPoolBaseTest, HandleMultipleSyncFailuresAfterAsyncFailure) { |
| 1780 | const size_t kNumberOfRequests = 10; |
| 1781 | const size_t kMaxSockets = 1; |
| 1782 | CreatePool(kMaxSockets, kMaxSockets); |
| 1783 | |
| 1784 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
| 1785 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1786 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1787 | IsError(ERR_IO_PENDING)); |
mmenke | 9d72fe4 | 2017-05-18 22:36:07 | [diff] [blame] | 1788 | |
| 1789 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
| 1790 | |
| 1791 | // Queue up all the other requests |
| 1792 | for (size_t i = 1; i < kNumberOfRequests; ++i) |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1793 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1794 | IsError(ERR_IO_PENDING)); |
mmenke | 9d72fe4 | 2017-05-18 22:36:07 | [diff] [blame] | 1795 | |
| 1796 | // Make sure all requests fail, instead of hanging. |
| 1797 | for (size_t i = 0; i < kNumberOfRequests; ++i) |
| 1798 | EXPECT_THAT(request(i)->WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 1799 | } |
| 1800 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1801 | TEST_F(ClientSocketPoolBaseTest, CancelActiveRequestThenRequestSocket) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1802 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1803 | |
| 1804 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 1805 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1806 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1807 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1808 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1809 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1810 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1811 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1812 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1813 | |
| 1814 | // Cancel the active request. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1815 | handle.Reset(); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1816 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1817 | rv = handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1818 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1819 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1820 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1821 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 1822 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1823 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1824 | EXPECT_FALSE(handle.is_reused()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 1825 | TestLoadTimingInfoConnectedNotReused(handle); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1826 | EXPECT_EQ(2, client_socket_factory_.allocation_count()); |
| 1827 | } |
| 1828 | |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1829 | TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketsForced) { |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1830 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1831 | ClientSocketHandle handle; |
| 1832 | TestCompletionCallback callback; |
| 1833 | BoundTestNetLog log; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1834 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1835 | TestGroupId("a"), params_, base::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1836 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1837 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1838 | EXPECT_THAT(rv, IsOk()); |
| 1839 | handle.Reset(); |
| 1840 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 1841 | pool_->CloseIdleSockets(); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1842 | } |
| 1843 | |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1844 | TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketsInGroupForced) { |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1845 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1846 | TestCompletionCallback callback; |
| 1847 | BoundTestNetLog log; |
| 1848 | ClientSocketHandle handle1; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1849 | int rv = handle1.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1850 | TestGroupId("a"), params_, base::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1851 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1852 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1853 | EXPECT_THAT(rv, IsOk()); |
| 1854 | ClientSocketHandle handle2; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1855 | rv = handle2.Init(TestGroupId("a"), params_, base::nullopt, LOWEST, |
| 1856 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1857 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1858 | pool_.get(), log.bound()); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1859 | ClientSocketHandle handle3; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1860 | rv = handle3.Init(TestGroupId("b"), params_, base::nullopt, LOWEST, |
| 1861 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1862 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1863 | pool_.get(), log.bound()); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1864 | EXPECT_THAT(rv, IsOk()); |
| 1865 | handle1.Reset(); |
| 1866 | handle2.Reset(); |
| 1867 | handle3.Reset(); |
| 1868 | EXPECT_EQ(3, pool_->IdleSocketCount()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1869 | pool_->CloseIdleSocketsInGroup(TestGroupId("a")); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1870 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
xunjieli | 92feb33 | 2017-03-03 17:19:23 | [diff] [blame] | 1871 | } |
| 1872 | |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1873 | TEST_F(ClientSocketPoolBaseTest, CleanUpUnusableIdleSockets) { |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1874 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1875 | ClientSocketHandle handle; |
| 1876 | TestCompletionCallback callback; |
| 1877 | BoundTestNetLog log; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1878 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1879 | TestGroupId("a"), params_, base::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1880 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1881 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1882 | EXPECT_THAT(rv, IsOk()); |
| 1883 | StreamSocket* socket = handle.socket(); |
| 1884 | handle.Reset(); |
| 1885 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 1886 | |
| 1887 | // Disconnect socket now to make the socket unusable. |
| 1888 | socket->Disconnect(); |
| 1889 | ClientSocketHandle handle2; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1890 | rv = handle2.Init(TestGroupId("a"), params_, base::nullopt, LOWEST, |
| 1891 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1892 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1893 | pool_.get(), log.bound()); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1894 | EXPECT_THAT(rv, IsOk()); |
| 1895 | EXPECT_FALSE(handle2.is_reused()); |
xunjieli | 26619e7 | 2016-11-23 19:39:55 | [diff] [blame] | 1896 | } |
| 1897 | |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1898 | // Regression test for http://crbug.com/17985. |
| 1899 | TEST_F(ClientSocketPoolBaseTest, GroupWithPendingRequestsIsNotEmpty) { |
| 1900 | const int kMaxSockets = 3; |
| 1901 | const int kMaxSocketsPerGroup = 2; |
| 1902 | CreatePool(kMaxSockets, kMaxSocketsPerGroup); |
| 1903 | |
[email protected] | ac790b4 | 2009-12-02 04:31:31 | [diff] [blame] | 1904 | const RequestPriority kHighPriority = HIGHEST; |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1905 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1906 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 1907 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1908 | |
| 1909 | // This is going to be a pending request in an otherwise empty group. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1910 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 1911 | IsError(ERR_IO_PENDING)); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1912 | |
| 1913 | // Reach the maximum socket limit. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1914 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1915 | |
| 1916 | // Create a stalled group with high priorities. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1917 | EXPECT_THAT(StartRequest(TestGroupId("c"), kHighPriority), |
| 1918 | IsError(ERR_IO_PENDING)); |
| 1919 | EXPECT_THAT(StartRequest(TestGroupId("c"), kHighPriority), |
| 1920 | IsError(ERR_IO_PENDING)); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1921 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1922 | // Release the first two sockets from TestGroupId("a"). Because this is a |
| 1923 | // keepalive, the first release will unblock the pending request for |
| 1924 | // TestGroupId("a"). The second release will unblock a request for "c", |
| 1925 | // because it is the next high priority socket. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1926 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
| 1927 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE)); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1928 | |
| 1929 | // Closing idle sockets should not get us into trouble, but in the bug |
| 1930 | // we were hitting a CHECK here. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1931 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 1932 | pool_->CloseIdleSockets(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 1933 | |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 1934 | // Run the released socket wakeups. |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 1935 | base::RunLoop().RunUntilIdle(); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 1936 | } |
| 1937 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 1938 | TEST_F(ClientSocketPoolBaseTest, BasicAsynchronous) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1939 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1940 | |
| 1941 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1942 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1943 | TestCompletionCallback callback; |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 1944 | BoundTestNetLog log; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1945 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1946 | TestGroupId("a"), params_, base::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1947 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 1948 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1949 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1950 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 1951 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 1952 | TestLoadTimingInfoNotConnected(handle); |
| 1953 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1954 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1955 | EXPECT_TRUE(handle.is_initialized()); |
| 1956 | EXPECT_TRUE(handle.socket()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 1957 | TestLoadTimingInfoConnectedNotReused(handle); |
| 1958 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1959 | handle.Reset(); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 1960 | TestLoadTimingInfoNotConnected(handle); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 1961 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 1962 | TestNetLogEntry::List entries; |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 1963 | log.GetEntries(&entries); |
| 1964 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1965 | EXPECT_EQ(5u, entries.size()); |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 1966 | EXPECT_TRUE(LogContainsEvent( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1967 | entries, 0, NetLogEventType::TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 1968 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1969 | EXPECT_TRUE(LogContainsBeginEvent(entries, 1, NetLogEventType::SOCKET_POOL)); |
| 1970 | EXPECT_TRUE(LogContainsEvent( |
| 1971 | entries, 2, NetLogEventType::SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
| 1972 | NetLogEventPhase::NONE)); |
| 1973 | EXPECT_TRUE(LogContainsEvent(entries, 3, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 1974 | NetLogEventType::SOCKET_POOL_BOUND_TO_SOCKET, |
| 1975 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 1976 | EXPECT_TRUE(LogContainsEndEvent(entries, 4, NetLogEventType::SOCKET_POOL)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1977 | } |
| 1978 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 1979 | TEST_F(ClientSocketPoolBaseTest, |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1980 | InitConnectionAsynchronousFailure) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 1981 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 1982 | |
| 1983 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1984 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 1985 | TestCompletionCallback callback; |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 1986 | BoundTestNetLog log; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 1987 | // Set the additional error state members to ensure that they get cleared. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1988 | handle.set_is_ssl_error(true); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 1989 | handle.set_ssl_cert_request_info(base::MakeRefCounted<SSLCertRequestInfo>()); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 1990 | EXPECT_EQ( |
| 1991 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 1992 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 1993 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 1994 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 1995 | pool_.get(), log.bound())); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 1996 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 1997 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 1998 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 1999 | EXPECT_FALSE(handle.is_ssl_error()); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 2000 | EXPECT_FALSE(handle.ssl_cert_request_info()); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 2001 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 2002 | TestNetLogEntry::List entries; |
[email protected] | b2fcd0e | 2010-12-01 15:19:40 | [diff] [blame] | 2003 | log.GetEntries(&entries); |
| 2004 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2005 | EXPECT_EQ(4u, entries.size()); |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 2006 | EXPECT_TRUE(LogContainsEvent( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2007 | entries, 0, NetLogEventType::TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET, |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 2008 | NetLogEventPhase::NONE)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2009 | EXPECT_TRUE(LogContainsBeginEvent(entries, 1, NetLogEventType::SOCKET_POOL)); |
| 2010 | EXPECT_TRUE(LogContainsEvent( |
| 2011 | entries, 2, NetLogEventType::SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
| 2012 | NetLogEventPhase::NONE)); |
| 2013 | EXPECT_TRUE(LogContainsEndEvent(entries, 3, NetLogEventType::SOCKET_POOL)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2014 | } |
| 2015 | |
mmenke | 6be122f | 2015-03-09 22:22:47 | [diff] [blame] | 2016 | // Check that an async ConnectJob failure does not result in creation of a new |
| 2017 | // ConnectJob when there's another pending request also waiting on its own |
| 2018 | // ConnectJob. See http://crbug.com/463960. |
| 2019 | TEST_F(ClientSocketPoolBaseTest, AsyncFailureWithPendingRequestWithJob) { |
| 2020 | CreatePool(2, 2); |
| 2021 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
| 2022 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2023 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2024 | IsError(ERR_IO_PENDING)); |
| 2025 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2026 | IsError(ERR_IO_PENDING)); |
mmenke | 6be122f | 2015-03-09 22:22:47 | [diff] [blame] | 2027 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2028 | EXPECT_THAT(request(0)->WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 2029 | EXPECT_THAT(request(1)->WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
mmenke | 6be122f | 2015-03-09 22:22:47 | [diff] [blame] | 2030 | |
| 2031 | EXPECT_EQ(2, client_socket_factory_.allocation_count()); |
| 2032 | } |
| 2033 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2034 | TEST_F(ClientSocketPoolBaseTest, TwoRequestsCancelOne) { |
[email protected] | b22b516 | 2010-03-16 07:53:10 | [diff] [blame] | 2035 | // TODO(eroman): Add back the log expectations! Removed them because the |
| 2036 | // ordering is difficult, and some may fire during destructor. |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2037 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2038 | |
| 2039 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2040 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2041 | TestCompletionCallback callback; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2042 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2043 | TestCompletionCallback callback2; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2044 | |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2045 | EXPECT_EQ( |
| 2046 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2047 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2048 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2049 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2050 | pool_.get(), NetLogWithSource())); |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 2051 | BoundTestNetLog log2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 2052 | EXPECT_EQ( |
| 2053 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2054 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2055 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2056 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2057 | pool_.get(), NetLogWithSource())); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2058 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2059 | handle.Reset(); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2060 | |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 2061 | |
| 2062 | // 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] | 2063 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2064 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2065 | handle2.Reset(); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 2066 | |
| 2067 | // Now request 2 has actually finished. |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 2068 | // TODO(eroman): Add back log expectations. |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2069 | } |
| 2070 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2071 | TEST_F(ClientSocketPoolBaseTest, CancelRequestLimitsJobs) { |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 2072 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2073 | |
[email protected] | 17a0c6c | 2009-08-04 00:07:04 | [diff] [blame] | 2074 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2075 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2076 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOWEST), IsError(ERR_IO_PENDING)); |
| 2077 | EXPECT_THAT(StartRequest(TestGroupId("a"), LOW), IsError(ERR_IO_PENDING)); |
| 2078 | EXPECT_THAT(StartRequest(TestGroupId("a"), MEDIUM), IsError(ERR_IO_PENDING)); |
| 2079 | EXPECT_THAT(StartRequest(TestGroupId("a"), HIGHEST), IsError(ERR_IO_PENDING)); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 2080 | |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2081 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2082 | static_cast<int>( |
| 2083 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2084 | (*requests())[2]->handle()->Reset(); |
| 2085 | (*requests())[3]->handle()->Reset(); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2086 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2087 | static_cast<int>( |
| 2088 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 2089 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2090 | (*requests())[1]->handle()->Reset(); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2091 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2092 | static_cast<int>( |
| 2093 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 2094 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2095 | (*requests())[0]->handle()->Reset(); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2096 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2097 | static_cast<int>( |
| 2098 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 2099 | } |
| 2100 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2101 | // When requests and ConnectJobs are not coupled, the request will get serviced |
| 2102 | // by whatever comes first. |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2103 | TEST_F(ClientSocketPoolBaseTest, ReleaseSockets) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2104 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2105 | |
| 2106 | // Start job 1 (async OK) |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 2107 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2108 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2109 | std::vector<TestSocketRequest*> request_order; |
| 2110 | size_t completion_count; // unused |
| 2111 | TestSocketRequest req1(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2112 | int rv = req1.handle()->Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2113 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2114 | ClientSocketPool::RespectLimits::ENABLED, req1.callback(), |
| 2115 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2116 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 2117 | EXPECT_THAT(req1.WaitForResult(), IsOk()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2118 | |
| 2119 | // Job 1 finished OK. Start job 2 (also async OK). Request 3 is pending |
| 2120 | // without a job. |
| 2121 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2122 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2123 | TestSocketRequest req2(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2124 | rv = req2.handle()->Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2125 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2126 | ClientSocketPool::RespectLimits::ENABLED, req2.callback(), |
| 2127 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2128 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2129 | TestSocketRequest req3(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2130 | rv = req3.handle()->Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2131 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2132 | ClientSocketPool::RespectLimits::ENABLED, req3.callback(), |
| 2133 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2134 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2135 | |
| 2136 | // Both Requests 2 and 3 are pending. We release socket 1 which should |
| 2137 | // service request 2. Request 3 should still be waiting. |
[email protected] | a6c59f6 | 2009-07-29 16:33:33 | [diff] [blame] | 2138 | req1.handle()->Reset(); |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 2139 | // Run the released socket wakeups. |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 2140 | base::RunLoop().RunUntilIdle(); |
[email protected] | a6c59f6 | 2009-07-29 16:33:33 | [diff] [blame] | 2141 | ASSERT_TRUE(req2.handle()->socket()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2142 | EXPECT_THAT(req2.WaitForResult(), IsOk()); |
[email protected] | a6c59f6 | 2009-07-29 16:33:33 | [diff] [blame] | 2143 | EXPECT_FALSE(req3.handle()->socket()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2144 | |
| 2145 | // Signal job 2, which should service request 3. |
| 2146 | |
| 2147 | client_socket_factory_.SignalJobs(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2148 | EXPECT_THAT(req3.WaitForResult(), IsOk()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2149 | |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2150 | ASSERT_EQ(3u, request_order.size()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2151 | EXPECT_EQ(&req1, request_order[0]); |
| 2152 | EXPECT_EQ(&req2, request_order[1]); |
| 2153 | EXPECT_EQ(&req3, request_order[2]); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2154 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2155 | } |
| 2156 | |
| 2157 | // The requests are not coupled to the jobs. So, the requests should finish in |
| 2158 | // their priority / insertion order. |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 2159 | TEST_F(ClientSocketPoolBaseTest, PendingJobCompletionOrder) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2160 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2161 | // First two jobs are async. |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 2162 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2163 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2164 | std::vector<TestSocketRequest*> request_order; |
| 2165 | size_t completion_count; // unused |
| 2166 | TestSocketRequest req1(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2167 | int rv = req1.handle()->Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2168 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2169 | ClientSocketPool::RespectLimits::ENABLED, req1.callback(), |
| 2170 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2171 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2172 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2173 | TestSocketRequest req2(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2174 | rv = req2.handle()->Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2175 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2176 | ClientSocketPool::RespectLimits::ENABLED, req2.callback(), |
| 2177 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2178 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2179 | |
| 2180 | // The pending job is sync. |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 2181 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2182 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2183 | TestSocketRequest req3(&request_order, &completion_count); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2184 | rv = req3.handle()->Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2185 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2186 | ClientSocketPool::RespectLimits::ENABLED, req3.callback(), |
| 2187 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2188 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2189 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2190 | EXPECT_THAT(req1.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 2191 | EXPECT_THAT(req2.WaitForResult(), IsOk()); |
| 2192 | EXPECT_THAT(req3.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2193 | |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 2194 | ASSERT_EQ(3u, request_order.size()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2195 | EXPECT_EQ(&req1, request_order[0]); |
| 2196 | EXPECT_EQ(&req2, request_order[1]); |
| 2197 | EXPECT_EQ(&req3, request_order[2]); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2198 | } |
| 2199 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2200 | // Test GetLoadState in the case there's only one socket request. |
| 2201 | TEST_F(ClientSocketPoolBaseTest, LoadStateOneRequest) { |
[email protected] | 211d2172 | 2009-07-22 15:48:53 | [diff] [blame] | 2202 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2203 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2204 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2205 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2206 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2207 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2208 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2209 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2210 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2211 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2212 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2213 | |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2214 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE); |
| 2215 | EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle.GetLoadState()); |
| 2216 | |
| 2217 | // No point in completing the connection, since ClientSocketHandles only |
| 2218 | // expect the LoadState to be checked while connecting. |
| 2219 | } |
| 2220 | |
| 2221 | // Test GetLoadState in the case there are two socket requests. |
| 2222 | TEST_F(ClientSocketPoolBaseTest, LoadStateTwoRequests) { |
| 2223 | CreatePool(2, 2); |
| 2224 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2225 | |
| 2226 | ClientSocketHandle handle; |
| 2227 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2228 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2229 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2230 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2231 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2232 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2233 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_RESOLVING_HOST); |
| 2234 | |
| 2235 | ClientSocketHandle handle2; |
| 2236 | TestCompletionCallback callback2; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2237 | rv = handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2238 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2239 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2240 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2241 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2242 | client_socket_factory_.SetJobLoadState(1, LOAD_STATE_RESOLVING_HOST); |
| 2243 | |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 2244 | // Each handle should reflect the state of its own job. |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2245 | EXPECT_EQ(LOAD_STATE_RESOLVING_HOST, handle.GetLoadState()); |
| 2246 | EXPECT_EQ(LOAD_STATE_RESOLVING_HOST, handle2.GetLoadState()); |
| 2247 | |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 2248 | // Update the state of the first job. |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2249 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_CONNECTING); |
| 2250 | |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 2251 | // Only the state of the first request should have changed. |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2252 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
haavardm | 835c1d6 | 2015-04-22 08:18:00 | [diff] [blame] | 2253 | EXPECT_EQ(LOAD_STATE_RESOLVING_HOST, handle2.GetLoadState()); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 2254 | |
| 2255 | // Update the state of the second job. |
| 2256 | client_socket_factory_.SetJobLoadState(1, LOAD_STATE_SSL_HANDSHAKE); |
| 2257 | |
| 2258 | // Only the state of the second request should have changed. |
| 2259 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
| 2260 | EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle2.GetLoadState()); |
| 2261 | |
| 2262 | // Second job connects and the first request gets the socket. The |
| 2263 | // second handle switches to the state of the remaining ConnectJob. |
| 2264 | client_socket_factory_.SignalJob(1); |
| 2265 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 2266 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle2.GetLoadState()); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2267 | } |
| 2268 | |
| 2269 | // Test GetLoadState in the case the per-group limit is reached. |
| 2270 | TEST_F(ClientSocketPoolBaseTest, LoadStateGroupLimit) { |
| 2271 | CreatePool(2, 1); |
| 2272 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2273 | |
| 2274 | ClientSocketHandle handle; |
| 2275 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2276 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2277 | TestGroupId("a"), params_, base::nullopt, MEDIUM, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2278 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2279 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2280 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2281 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
| 2282 | |
| 2283 | // Request another socket from the same pool, buth with a higher priority. |
| 2284 | // The first request should now be stalled at the socket group limit. |
| 2285 | ClientSocketHandle handle2; |
| 2286 | TestCompletionCallback callback2; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2287 | rv = handle2.Init(TestGroupId("a"), params_, base::nullopt, HIGHEST, |
| 2288 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2289 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2290 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2291 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2292 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET, handle.GetLoadState()); |
| 2293 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle2.GetLoadState()); |
| 2294 | |
| 2295 | // The first handle should remain stalled as the other socket goes through |
| 2296 | // the connect process. |
| 2297 | |
| 2298 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE); |
| 2299 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET, handle.GetLoadState()); |
| 2300 | EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle2.GetLoadState()); |
| 2301 | |
| 2302 | client_socket_factory_.SignalJob(0); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2303 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2304 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET, handle.GetLoadState()); |
| 2305 | |
| 2306 | // Closing the second socket should cause the stalled handle to finally get a |
| 2307 | // ConnectJob. |
| 2308 | handle2.socket()->Disconnect(); |
| 2309 | handle2.Reset(); |
| 2310 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
| 2311 | } |
| 2312 | |
| 2313 | // Test GetLoadState in the case the per-pool limit is reached. |
| 2314 | TEST_F(ClientSocketPoolBaseTest, LoadStatePoolLimit) { |
| 2315 | CreatePool(2, 2); |
| 2316 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2317 | |
| 2318 | ClientSocketHandle handle; |
| 2319 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2320 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2321 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2322 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2323 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2324 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2325 | |
| 2326 | // Request for socket from another pool. |
| 2327 | ClientSocketHandle handle2; |
| 2328 | TestCompletionCallback callback2; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2329 | rv = handle2.Init(TestGroupId("b"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2330 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2331 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2332 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2333 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2334 | |
| 2335 | // Request another socket from the first pool. Request should stall at the |
| 2336 | // socket pool limit. |
| 2337 | ClientSocketHandle handle3; |
| 2338 | TestCompletionCallback callback3; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2339 | rv = handle3.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2340 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2341 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2342 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2343 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2344 | |
| 2345 | // The third handle should remain stalled as the other sockets in its group |
| 2346 | // goes through the connect process. |
| 2347 | |
| 2348 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState()); |
| 2349 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL, handle3.GetLoadState()); |
| 2350 | |
| 2351 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE); |
| 2352 | EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle.GetLoadState()); |
| 2353 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL, handle3.GetLoadState()); |
| 2354 | |
| 2355 | client_socket_factory_.SignalJob(0); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2356 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 03b7c8c | 2013-07-20 04:38:55 | [diff] [blame] | 2357 | EXPECT_EQ(LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL, handle3.GetLoadState()); |
| 2358 | |
| 2359 | // Closing a socket should allow the stalled handle to finally get a new |
| 2360 | // ConnectJob. |
| 2361 | handle.socket()->Disconnect(); |
| 2362 | handle.Reset(); |
| 2363 | EXPECT_EQ(LOAD_STATE_CONNECTING, handle3.GetLoadState()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 2364 | } |
| 2365 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2366 | TEST_F(ClientSocketPoolBaseTest, CertError) { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2367 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2368 | connect_job_factory_->set_job_type(TestConnectJob::kMockCertErrorJob); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2369 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2370 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2371 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2372 | EXPECT_EQ( |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2373 | ERR_CERT_COMMON_NAME_INVALID, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2374 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2375 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2376 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2377 | pool_.get(), NetLogWithSource())); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2378 | EXPECT_TRUE(handle.is_initialized()); |
| 2379 | EXPECT_TRUE(handle.socket()); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2380 | } |
| 2381 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2382 | TEST_F(ClientSocketPoolBaseTest, AsyncCertError) { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2383 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2384 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2385 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingCertErrorJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2386 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2387 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2388 | EXPECT_EQ( |
| 2389 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2390 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2391 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2392 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2393 | pool_.get(), NetLogWithSource())); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2394 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2395 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 2396 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_CERT_COMMON_NAME_INVALID)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2397 | EXPECT_TRUE(handle.is_initialized()); |
| 2398 | EXPECT_TRUE(handle.socket()); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 2399 | } |
| 2400 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2401 | TEST_F(ClientSocketPoolBaseTest, AdditionalErrorStateSynchronous) { |
| 2402 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2403 | connect_job_factory_->set_job_type( |
| 2404 | TestConnectJob::kMockAdditionalErrorStateJob); |
| 2405 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 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 | EXPECT_EQ( |
| 2409 | ERR_CONNECTION_FAILED, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2410 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2411 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2412 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2413 | pool_.get(), NetLogWithSource())); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2414 | EXPECT_FALSE(handle.is_initialized()); |
| 2415 | EXPECT_FALSE(handle.socket()); |
| 2416 | EXPECT_TRUE(handle.is_ssl_error()); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 2417 | EXPECT_TRUE(handle.ssl_cert_request_info()); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2418 | } |
| 2419 | |
| 2420 | TEST_F(ClientSocketPoolBaseTest, AdditionalErrorStateAsynchronous) { |
| 2421 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2422 | |
| 2423 | connect_job_factory_->set_job_type( |
| 2424 | TestConnectJob::kMockPendingAdditionalErrorStateJob); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2425 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2426 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2427 | EXPECT_EQ( |
| 2428 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2429 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2430 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2431 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2432 | pool_.get(), NetLogWithSource())); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2433 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2434 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2435 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2436 | EXPECT_FALSE(handle.is_initialized()); |
| 2437 | EXPECT_FALSE(handle.socket()); |
| 2438 | EXPECT_TRUE(handle.is_ssl_error()); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 2439 | EXPECT_TRUE(handle.ssl_cert_request_info()); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2440 | } |
| 2441 | |
martijn | 003cd61 | 2016-05-19 22:24:38 | [diff] [blame] | 2442 | // Make sure we can reuse sockets. |
| 2443 | TEST_F(ClientSocketPoolBaseTest, CleanupTimedOutIdleSocketsReuse) { |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2444 | CreatePoolWithIdleTimeouts( |
| 2445 | kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2446 | base::TimeDelta(), // Time out unused sockets immediately. |
| 2447 | base::TimeDelta::FromDays(1)); // Don't time out used sockets. |
| 2448 | |
| 2449 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2450 | |
| 2451 | ClientSocketHandle handle; |
| 2452 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2453 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2454 | TestGroupId("a"), params_, base::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2455 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2456 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2457 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2458 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2459 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2460 | ASSERT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2461 | |
| 2462 | // Use and release the socket. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 2463 | EXPECT_EQ(1, handle.socket()->Write(nullptr, 1, CompletionOnceCallback(), |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 2464 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 2465 | TestLoadTimingInfoConnectedNotReused(handle); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2466 | handle.Reset(); |
| 2467 | |
| 2468 | // Should now have one idle socket. |
| 2469 | ASSERT_EQ(1, pool_->IdleSocketCount()); |
| 2470 | |
| 2471 | // Request a new socket. This should reuse the old socket and complete |
| 2472 | // synchronously. |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 2473 | BoundTestNetLog log; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2474 | rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2475 | TestGroupId("a"), params_, base::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2476 | ClientSocketPool::RespectLimits::ENABLED, CompletionOnceCallback(), |
| 2477 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2478 | ASSERT_THAT(rv, IsOk()); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2479 | EXPECT_TRUE(handle.is_reused()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 2480 | TestLoadTimingInfoConnectedReused(handle); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2481 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2482 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2483 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2484 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2485 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 2486 | TestNetLogEntry::List entries; |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2487 | log.GetEntries(&entries); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2488 | EXPECT_TRUE(LogContainsEvent( |
| 2489 | entries, 0, NetLogEventType::TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET, |
| 2490 | NetLogEventPhase::NONE)); |
| 2491 | EXPECT_TRUE(LogContainsBeginEvent(entries, 1, NetLogEventType::SOCKET_POOL)); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2492 | EXPECT_TRUE(LogContainsEntryWithType( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2493 | entries, 2, NetLogEventType::SOCKET_POOL_REUSED_AN_EXISTING_SOCKET)); |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2494 | } |
| 2495 | |
martijn | 003cd61 | 2016-05-19 22:24:38 | [diff] [blame] | 2496 | // Make sure we cleanup old unused sockets. |
Eric Roman | b49715e | 2018-04-24 22:41:17 | [diff] [blame] | 2497 | TEST_F(ClientSocketPoolBaseTest, CleanupTimedOutIdleSocketsNoReuse) { |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2498 | CreatePoolWithIdleTimeouts( |
| 2499 | kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 2500 | base::TimeDelta(), // Time out unused sockets immediately |
| 2501 | base::TimeDelta()); // Time out used sockets immediately |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2502 | |
| 2503 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2504 | |
| 2505 | // Startup two mock pending connect jobs, which will sit in the MessageLoop. |
| 2506 | |
| 2507 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2508 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2509 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2510 | TestGroupId("a"), params_, base::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2511 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2512 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2513 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2514 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2515 | pool_->GetLoadState(TestGroupId("a"), &handle)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2516 | |
| 2517 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2518 | TestCompletionCallback callback2; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2519 | rv = handle2.Init(TestGroupId("a"), params_, base::nullopt, LOWEST, |
| 2520 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2521 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2522 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2523 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2524 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
| 2525 | pool_->GetLoadState(TestGroupId("a"), &handle2)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2526 | |
| 2527 | // Cancel one of the requests. Wait for the other, which will get the first |
| 2528 | // job. Release the socket. Run the loop again to make sure the second |
| 2529 | // socket is sitting idle and the first one is released (since ReleaseSocket() |
| 2530 | // just posts a DoReleaseSocket() task). |
| 2531 | |
| 2532 | handle.Reset(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2533 | ASSERT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2534 | // Use the socket. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 2535 | EXPECT_EQ(1, handle2.socket()->Write(nullptr, 1, CompletionOnceCallback(), |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 2536 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2537 | handle2.Reset(); |
| 2538 | |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2539 | // We post all of our delayed tasks with a 2ms delay. I.e. they don't |
| 2540 | // actually become pending until 2ms after they have been created. In order |
| 2541 | // to flush all tasks, we need to wait so that we know there are no |
| 2542 | // soon-to-be-pending tasks waiting. |
Alex Clarke | 0def209 | 2018-12-10 12:01:45 | [diff] [blame] | 2543 | FastForwardBy(base::TimeDelta::FromMilliseconds(10)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2544 | |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2545 | // Both sockets should now be idle. |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2546 | ASSERT_EQ(2, pool_->IdleSocketCount()); |
| 2547 | |
| 2548 | // Request a new socket. This should cleanup the unused and timed out ones. |
| 2549 | // A new socket will be created rather than reusing the idle one. |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 2550 | BoundTestNetLog log; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2551 | TestCompletionCallback callback3; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2552 | rv = handle.Init(TestGroupId("a"), params_, base::nullopt, LOWEST, |
| 2553 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2554 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2555 | pool_.get(), log.bound()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2556 | ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 2557 | ASSERT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2558 | EXPECT_FALSE(handle.is_reused()); |
| 2559 | |
[email protected] | e7b1c6d2c | 2012-05-05 00:54:03 | [diff] [blame] | 2560 | // Make sure the idle socket is closed. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2561 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2562 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2563 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2564 | |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 2565 | TestNetLogEntry::List entries; |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2566 | log.GetEntries(&entries); |
| 2567 | EXPECT_FALSE(LogContainsEntryWithType( |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 2568 | entries, 1, NetLogEventType::SOCKET_POOL_REUSED_AN_EXISTING_SOCKET)); |
[email protected] | 64770b7d | 2011-11-16 04:30:41 | [diff] [blame] | 2569 | } |
| 2570 | |
[email protected] | 2041cf34 | 2010-02-19 03:15:59 | [diff] [blame] | 2571 | // Make sure that we process all pending requests even when we're stalling |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2572 | // because of multiple releasing disconnected sockets. |
| 2573 | TEST_F(ClientSocketPoolBaseTest, MultipleReleasingDisconnectedSockets) { |
| 2574 | CreatePoolWithIdleTimeouts( |
| 2575 | kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 2576 | base::TimeDelta(), // Time out unused sockets immediately. |
| 2577 | base::TimeDelta::FromDays(1)); // Don't time out used sockets. |
| 2578 | |
| 2579 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 2580 | |
| 2581 | // Startup 4 connect jobs. Two of them will be pending. |
| 2582 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2583 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2584 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2585 | int rv = handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2586 | TestGroupId("a"), params_, base::nullopt, LOWEST, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2587 | ClientSocketPool::RespectLimits::ENABLED, callback.callback(), |
| 2588 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2589 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2590 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2591 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2592 | TestCompletionCallback callback2; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2593 | rv = handle2.Init(TestGroupId("a"), params_, base::nullopt, LOWEST, |
| 2594 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2595 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2596 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2597 | EXPECT_THAT(rv, IsOk()); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2598 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2599 | ClientSocketHandle handle3; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2600 | TestCompletionCallback callback3; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2601 | rv = handle3.Init(TestGroupId("a"), params_, base::nullopt, LOWEST, |
| 2602 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2603 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2604 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2605 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2606 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2607 | ClientSocketHandle handle4; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2608 | TestCompletionCallback callback4; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2609 | rv = handle4.Init(TestGroupId("a"), params_, base::nullopt, LOWEST, |
| 2610 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2611 | callback4.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2612 | pool_.get(), NetLogWithSource()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2613 | EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2614 | |
| 2615 | // Release two disconnected sockets. |
| 2616 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2617 | handle.socket()->Disconnect(); |
| 2618 | handle.Reset(); |
| 2619 | handle2.socket()->Disconnect(); |
| 2620 | handle2.Reset(); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2621 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2622 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2623 | EXPECT_FALSE(handle3.is_reused()); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2624 | EXPECT_THAT(callback4.WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2625 | EXPECT_FALSE(handle4.is_reused()); |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 2626 | } |
| 2627 | |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2628 | // Regression test for http://crbug.com/42267. |
| 2629 | // When DoReleaseSocket() is processed for one socket, it is blocked because the |
| 2630 | // other stalled groups all have releasing sockets, so no progress can be made. |
| 2631 | TEST_F(ClientSocketPoolBaseTest, SocketLimitReleasingSockets) { |
| 2632 | CreatePoolWithIdleTimeouts( |
| 2633 | 4 /* socket limit */, 4 /* socket limit per group */, |
| 2634 | base::TimeDelta(), // Time out unused sockets immediately. |
| 2635 | base::TimeDelta::FromDays(1)); // Don't time out used sockets. |
| 2636 | |
| 2637 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 2638 | |
| 2639 | // Max out the socket limit with 2 per group. |
| 2640 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2641 | ClientSocketHandle handle_a[4]; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2642 | TestCompletionCallback callback_a[4]; |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2643 | ClientSocketHandle handle_b[4]; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2644 | TestCompletionCallback callback_b[4]; |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2645 | |
| 2646 | for (int i = 0; i < 2; ++i) { |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2647 | EXPECT_EQ(OK, handle_a[i].Init(TestGroupId("a"), params_, base::nullopt, |
| 2648 | LOWEST, SocketTag(), |
| 2649 | ClientSocketPool::RespectLimits::ENABLED, |
| 2650 | callback_a[i].callback(), |
| 2651 | ClientSocketPool::ProxyAuthCallback(), |
| 2652 | pool_.get(), NetLogWithSource())); |
| 2653 | EXPECT_EQ(OK, handle_b[i].Init(TestGroupId("b"), params_, base::nullopt, |
| 2654 | LOWEST, SocketTag(), |
| 2655 | ClientSocketPool::RespectLimits::ENABLED, |
| 2656 | callback_b[i].callback(), |
| 2657 | ClientSocketPool::ProxyAuthCallback(), |
| 2658 | pool_.get(), NetLogWithSource())); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2659 | } |
[email protected] | b89f7e4 | 2010-05-20 20:37:00 | [diff] [blame] | 2660 | |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2661 | // Make 4 pending requests, 2 per group. |
| 2662 | |
| 2663 | for (int i = 2; i < 4; ++i) { |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2664 | EXPECT_EQ( |
| 2665 | ERR_IO_PENDING, |
| 2666 | handle_a[i].Init(TestGroupId("a"), params_, base::nullopt, LOWEST, |
| 2667 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2668 | callback_a[i].callback(), |
| 2669 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2670 | NetLogWithSource())); |
| 2671 | EXPECT_EQ( |
| 2672 | ERR_IO_PENDING, |
| 2673 | handle_b[i].Init(TestGroupId("b"), params_, base::nullopt, LOWEST, |
| 2674 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2675 | callback_b[i].callback(), |
| 2676 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 2677 | NetLogWithSource())); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2678 | } |
| 2679 | |
| 2680 | // Release b's socket first. The order is important, because in |
| 2681 | // DoReleaseSocket(), we'll process b's released socket, and since both b and |
| 2682 | // a are stalled, but 'a' is lower lexicographically, we'll process group 'a' |
| 2683 | // first, which has a releasing socket, so it refuses to start up another |
| 2684 | // ConnectJob. So, we used to infinite loop on this. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2685 | handle_b[0].socket()->Disconnect(); |
| 2686 | handle_b[0].Reset(); |
| 2687 | handle_a[0].socket()->Disconnect(); |
| 2688 | handle_a[0].Reset(); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2689 | |
| 2690 | // Used to get stuck here. |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 2691 | base::RunLoop().RunUntilIdle(); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2692 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2693 | handle_b[1].socket()->Disconnect(); |
| 2694 | handle_b[1].Reset(); |
| 2695 | handle_a[1].socket()->Disconnect(); |
| 2696 | handle_a[1].Reset(); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2697 | |
| 2698 | for (int i = 2; i < 4; ++i) { |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2699 | EXPECT_THAT(callback_b[i].WaitForResult(), IsOk()); |
| 2700 | EXPECT_THAT(callback_a[i].WaitForResult(), IsOk()); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 2701 | } |
| 2702 | } |
| 2703 | |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2704 | TEST_F(ClientSocketPoolBaseTest, |
| 2705 | ReleasingDisconnectedSocketsMaintainsPriorityOrder) { |
| 2706 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2707 | |
| 2708 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2709 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2710 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2711 | IsError(ERR_IO_PENDING)); |
| 2712 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2713 | IsError(ERR_IO_PENDING)); |
| 2714 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2715 | IsError(ERR_IO_PENDING)); |
| 2716 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), |
| 2717 | IsError(ERR_IO_PENDING)); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2718 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2719 | EXPECT_THAT((*requests())[0]->WaitForResult(), IsOk()); |
| 2720 | EXPECT_THAT((*requests())[1]->WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2721 | EXPECT_EQ(2u, completion_count()); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2722 | |
| 2723 | // Releases one connection. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2724 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::NO_KEEP_ALIVE)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2725 | EXPECT_THAT((*requests())[2]->WaitForResult(), IsOk()); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2726 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2727 | EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::NO_KEEP_ALIVE)); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2728 | EXPECT_THAT((*requests())[3]->WaitForResult(), IsOk()); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2729 | EXPECT_EQ(4u, completion_count()); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2730 | |
| 2731 | EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 2732 | EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 2733 | EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 2734 | EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 2735 | |
| 2736 | // Make sure we test order of all requests made. |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2737 | EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(5)); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 2738 | } |
| 2739 | |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2740 | class TestReleasingSocketRequest : public TestCompletionCallbackBase { |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2741 | public: |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2742 | TestReleasingSocketRequest(TransportClientSocketPool* pool, |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2743 | int expected_result, |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2744 | bool reset_releasing_handle) |
| 2745 | : pool_(pool), |
| 2746 | expected_result_(expected_result), |
Bence Béky | 8ddc249 | 2018-06-13 01:02:04 | [diff] [blame] | 2747 | reset_releasing_handle_(reset_releasing_handle) {} |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2748 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 2749 | ~TestReleasingSocketRequest() override = default; |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2750 | |
| 2751 | ClientSocketHandle* handle() { return &handle_; } |
| 2752 | |
Bence Béky | 8ddc249 | 2018-06-13 01:02:04 | [diff] [blame] | 2753 | CompletionOnceCallback callback() { |
| 2754 | return base::BindOnce(&TestReleasingSocketRequest::OnComplete, |
| 2755 | base::Unretained(this)); |
| 2756 | } |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2757 | |
| 2758 | private: |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2759 | void OnComplete(int result) { |
| 2760 | SetResult(result); |
| 2761 | if (reset_releasing_handle_) |
| 2762 | handle_.Reset(); |
| 2763 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2764 | EXPECT_EQ( |
| 2765 | expected_result_, |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 2766 | handle2_.Init( |
| 2767 | TestGroupId("a"), |
| 2768 | ClientSocketPool::SocketParams::CreateForHttpForTesting(), |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2769 | base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 2770 | ClientSocketPool::RespectLimits::ENABLED, CompletionOnceCallback(), |
| 2771 | ClientSocketPool::ProxyAuthCallback(), pool_, NetLogWithSource())); |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2772 | } |
| 2773 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2774 | TransportClientSocketPool* const pool_; |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2775 | int expected_result_; |
| 2776 | bool reset_releasing_handle_; |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2777 | ClientSocketHandle handle_; |
| 2778 | ClientSocketHandle handle2_; |
[email protected] | 4f1e498 | 2010-03-02 18:31:04 | [diff] [blame] | 2779 | }; |
| 2780 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2781 | |
| 2782 | TEST_F(ClientSocketPoolBaseTest, AdditionalErrorSocketsDontUseSlot) { |
| 2783 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2784 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2785 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
| 2786 | EXPECT_THAT(StartRequest(TestGroupId("a"), DEFAULT_PRIORITY), IsOk()); |
| 2787 | EXPECT_THAT(StartRequest(TestGroupId("b"), DEFAULT_PRIORITY), IsOk()); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2788 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 2789 | EXPECT_EQ(static_cast<int>(requests_size()), |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2790 | client_socket_factory_.allocation_count()); |
| 2791 | |
| 2792 | connect_job_factory_->set_job_type( |
| 2793 | TestConnectJob::kMockPendingAdditionalErrorStateJob); |
| 2794 | TestReleasingSocketRequest req(pool_.get(), OK, false); |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2795 | EXPECT_EQ(ERR_IO_PENDING, |
| 2796 | req.handle()->Init( |
| 2797 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2798 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2799 | req.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2800 | pool_.get(), NetLogWithSource())); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2801 | // The next job should complete synchronously |
| 2802 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 2803 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2804 | EXPECT_THAT(req.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2805 | EXPECT_FALSE(req.handle()->is_initialized()); |
| 2806 | EXPECT_FALSE(req.handle()->socket()); |
| 2807 | EXPECT_TRUE(req.handle()->is_ssl_error()); |
Matt Menke | 39b7c5a | 2019-04-10 19:47:51 | [diff] [blame] | 2808 | EXPECT_TRUE(req.handle()->ssl_cert_request_info()); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 2809 | } |
| 2810 | |
[email protected] | b6501d3d | 2010-06-03 23:53:34 | [diff] [blame] | 2811 | // http://crbug.com/44724 regression test. |
| 2812 | // We start releasing the pool when we flush on network change. When that |
| 2813 | // happens, the only active references are in the ClientSocketHandles. When a |
| 2814 | // ConnectJob completes and calls back into the last ClientSocketHandle, that |
| 2815 | // callback can release the last reference and delete the pool. After the |
| 2816 | // callback finishes, we go back to the stack frame within the now-deleted pool. |
| 2817 | // Executing any code that refers to members of the now-deleted pool can cause |
| 2818 | // crashes. |
| 2819 | TEST_F(ClientSocketPoolBaseTest, CallbackThatReleasesPool) { |
| 2820 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2821 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
| 2822 | |
| 2823 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2824 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2825 | EXPECT_EQ( |
| 2826 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2827 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2828 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2829 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2830 | pool_.get(), NetLogWithSource())); |
[email protected] | b6501d3d | 2010-06-03 23:53:34 | [diff] [blame] | 2831 | |
[email protected] | 7af985a | 2012-12-14 22:40:42 | [diff] [blame] | 2832 | pool_->FlushWithError(ERR_NETWORK_CHANGED); |
[email protected] | b6501d3d | 2010-06-03 23:53:34 | [diff] [blame] | 2833 | |
| 2834 | // We'll call back into this now. |
| 2835 | callback.WaitForResult(); |
| 2836 | } |
| 2837 | |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2838 | TEST_F(ClientSocketPoolBaseTest, DoNotReuseSocketAfterFlush) { |
| 2839 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2840 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 2841 | |
| 2842 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2843 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2844 | EXPECT_EQ( |
| 2845 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2846 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2847 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2848 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2849 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2850 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2851 | EXPECT_EQ(ClientSocketHandle::UNUSED, handle.reuse_type()); |
| 2852 | |
[email protected] | 7af985a | 2012-12-14 22:40:42 | [diff] [blame] | 2853 | pool_->FlushWithError(ERR_NETWORK_CHANGED); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2854 | |
| 2855 | handle.Reset(); |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 2856 | base::RunLoop().RunUntilIdle(); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2857 | |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2858 | EXPECT_EQ( |
| 2859 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2860 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2861 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2862 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2863 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2864 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 2865 | EXPECT_EQ(ClientSocketHandle::UNUSED, handle.reuse_type()); |
| 2866 | } |
| 2867 | |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2868 | class ConnectWithinCallback : public TestCompletionCallbackBase { |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2869 | public: |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2870 | ConnectWithinCallback( |
| 2871 | const ClientSocketPool::GroupId& group_id, |
Matt Menke | 84d11e56 | 2019-03-27 00:11:19 | [diff] [blame] | 2872 | const scoped_refptr<ClientSocketPool::SocketParams>& params, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2873 | TransportClientSocketPool* pool) |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2874 | : group_id_(group_id), params_(params), pool_(pool) {} |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2875 | |
Chris Watkins | 7a41d355 | 2017-12-01 02:13:27 | [diff] [blame] | 2876 | ~ConnectWithinCallback() override = default; |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2877 | |
| 2878 | int WaitForNestedResult() { |
| 2879 | return nested_callback_.WaitForResult(); |
| 2880 | } |
| 2881 | |
Bence Béky | 8ddc249 | 2018-06-13 01:02:04 | [diff] [blame] | 2882 | CompletionOnceCallback callback() { |
| 2883 | return base::BindOnce(&ConnectWithinCallback::OnComplete, |
| 2884 | base::Unretained(this)); |
| 2885 | } |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2886 | |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2887 | private: |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2888 | void OnComplete(int result) { |
| 2889 | SetResult(result); |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2890 | EXPECT_EQ( |
| 2891 | ERR_IO_PENDING, |
| 2892 | handle_.Init(group_id_, params_, base::nullopt, DEFAULT_PRIORITY, |
| 2893 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2894 | nested_callback_.callback(), |
| 2895 | ClientSocketPool::ProxyAuthCallback(), pool_, |
| 2896 | NetLogWithSource())); |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2897 | } |
| 2898 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2899 | const ClientSocketPool::GroupId group_id_; |
Matt Menke | 84d11e56 | 2019-03-27 00:11:19 | [diff] [blame] | 2900 | const scoped_refptr<ClientSocketPool::SocketParams> params_; |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2901 | TransportClientSocketPool* const pool_; |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2902 | ClientSocketHandle handle_; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2903 | TestCompletionCallback nested_callback_; |
| 2904 | |
| 2905 | DISALLOW_COPY_AND_ASSIGN(ConnectWithinCallback); |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2906 | }; |
| 2907 | |
| 2908 | TEST_F(ClientSocketPoolBaseTest, AbortAllRequestsOnFlush) { |
| 2909 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 2910 | |
| 2911 | // First job will be waiting until it gets aborted. |
| 2912 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2913 | |
| 2914 | ClientSocketHandle handle; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 2915 | ConnectWithinCallback callback(TestGroupId("a"), params_, pool_.get()); |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2916 | EXPECT_EQ( |
| 2917 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2918 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2919 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2920 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2921 | pool_.get(), NetLogWithSource())); |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2922 | |
| 2923 | // Second job will be started during the first callback, and will |
| 2924 | // asynchronously complete with OK. |
| 2925 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
[email protected] | 7af985a | 2012-12-14 22:40:42 | [diff] [blame] | 2926 | pool_->FlushWithError(ERR_NETWORK_CHANGED); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 2927 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_NETWORK_CHANGED)); |
| 2928 | EXPECT_THAT(callback.WaitForNestedResult(), IsOk()); |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 2929 | } |
| 2930 | |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 2931 | TEST_F(ClientSocketPoolBaseTest, BackupSocketWaitsForHostResolution) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2932 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets, |
| 2933 | true /* enable_backup_connect_jobs */); |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 2934 | |
| 2935 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2936 | ClientSocketHandle handle; |
| 2937 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2938 | EXPECT_EQ( |
| 2939 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2940 | handle.Init(TestGroupId("bar"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2941 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2942 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2943 | pool_.get(), NetLogWithSource())); |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 2944 | // The backup timer fires but doesn't start a new ConnectJob while resolving |
| 2945 | // the hostname. |
| 2946 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_RESOLVING_HOST); |
| 2947 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
| 2948 | ClientSocketPool::kMaxConnectRetryIntervalMs * 100)); |
| 2949 | EXPECT_EQ(1, client_socket_factory_.allocation_count()); |
| 2950 | |
| 2951 | // Once the ConnectJob has finished resolving the hostname, the backup timer |
| 2952 | // will create a ConnectJob when it fires. |
| 2953 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_CONNECTING); |
| 2954 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
| 2955 | ClientSocketPool::kMaxConnectRetryIntervalMs)); |
| 2956 | EXPECT_EQ(2, client_socket_factory_.allocation_count()); |
| 2957 | } |
| 2958 | |
| 2959 | // Test that no backup socket is created when a ConnectJob connects before it |
| 2960 | // completes. |
| 2961 | TEST_F(ClientSocketPoolBaseTest, NoBackupSocketWhenConnected) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2962 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets, |
| 2963 | true /* enable_backup_connect_jobs */); |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 2964 | |
| 2965 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2966 | ClientSocketHandle handle; |
| 2967 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 2968 | EXPECT_EQ( |
| 2969 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 2970 | handle.Init(TestGroupId("bar"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 2971 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 2972 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 2973 | pool_.get(), NetLogWithSource())); |
Matt Menke | 141b87f2 | 2019-01-30 02:43:03 | [diff] [blame] | 2974 | // The backup timer fires but doesn't start a new ConnectJob while resolving |
| 2975 | // the hostname. |
| 2976 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_RESOLVING_HOST); |
| 2977 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
| 2978 | ClientSocketPool::kMaxConnectRetryIntervalMs * 100)); |
| 2979 | EXPECT_EQ(1, client_socket_factory_.allocation_count()); |
| 2980 | |
| 2981 | client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE); |
| 2982 | client_socket_factory_.SetJobHasEstablishedConnection(0); |
| 2983 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
| 2984 | ClientSocketPool::kMaxConnectRetryIntervalMs * 100)); |
| 2985 | EXPECT_EQ(1, client_socket_factory_.allocation_count()); |
| 2986 | } |
| 2987 | |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 2988 | // Cancel a pending socket request while we're at max sockets, |
| 2989 | // and verify that the backup socket firing doesn't cause a crash. |
| 2990 | TEST_F(ClientSocketPoolBaseTest, BackupSocketCancelAtMaxSockets) { |
| 2991 | // Max 4 sockets globally, max 4 sockets per group. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 2992 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets, |
| 2993 | true /* enable_backup_connect_jobs */); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 2994 | |
[email protected] | 4baaf9d | 2010-08-31 15:15:44 | [diff] [blame] | 2995 | // Create the first socket and set to ERR_IO_PENDING. This starts the backup |
| 2996 | // timer. |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 2997 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 2998 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 2999 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3000 | EXPECT_EQ( |
| 3001 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3002 | handle.Init(TestGroupId("bar"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3003 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 3004 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3005 | pool_.get(), NetLogWithSource())); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3006 | |
| 3007 | // Start (MaxSockets - 1) connected sockets to reach max sockets. |
| 3008 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 3009 | ClientSocketHandle handles[kDefaultMaxSockets]; |
| 3010 | for (int i = 1; i < kDefaultMaxSockets; ++i) { |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3011 | TestCompletionCallback callback; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3012 | EXPECT_EQ(OK, handles[i].Init(TestGroupId("bar"), params_, base::nullopt, |
| 3013 | DEFAULT_PRIORITY, SocketTag(), |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3014 | ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3015 | callback.callback(), |
| 3016 | ClientSocketPool::ProxyAuthCallback(), |
| 3017 | pool_.get(), NetLogWithSource())); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3018 | } |
| 3019 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3020 | base::RunLoop().RunUntilIdle(); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3021 | |
| 3022 | // Cancel the pending request. |
| 3023 | handle.Reset(); |
| 3024 | |
| 3025 | // 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] | 3026 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
[email protected] | 26b997396 | 2012-01-28 00:57:00 | [diff] [blame] | 3027 | ClientSocketPool::kMaxConnectRetryIntervalMs / 2 * 3)); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3028 | |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 3029 | EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 3030 | } |
| 3031 | |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3032 | TEST_F(ClientSocketPoolBaseTest, CancelBackupSocketAfterCancelingAllRequests) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3033 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets, |
| 3034 | true /* enable_backup_connect_jobs */); |
[email protected] | 4baaf9d | 2010-08-31 15:15:44 | [diff] [blame] | 3035 | |
| 3036 | // Create the first socket and set to ERR_IO_PENDING. This starts the backup |
| 3037 | // timer. |
| 3038 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3039 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3040 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3041 | EXPECT_EQ( |
| 3042 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3043 | handle.Init(TestGroupId("bar"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3044 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 3045 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3046 | pool_.get(), NetLogWithSource())); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3047 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("bar"))); |
| 3048 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("bar"))); |
| 3049 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3050 | TestGroupId("bar"))); |
| 3051 | EXPECT_EQ( |
| 3052 | 0u, pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("bar"))); |
[email protected] | 4baaf9d | 2010-08-31 15:15:44 | [diff] [blame] | 3053 | |
| 3054 | // Cancel the socket request. This should cancel the backup timer. Wait for |
| 3055 | // the backup time to see if it indeed got canceled. |
| 3056 | handle.Reset(); |
| 3057 | // 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] | 3058 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
[email protected] | 26b997396 | 2012-01-28 00:57:00 | [diff] [blame] | 3059 | ClientSocketPool::kMaxConnectRetryIntervalMs / 2 * 3)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3060 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("bar"))); |
| 3061 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("bar"))); |
[email protected] | 4baaf9d | 2010-08-31 15:15:44 | [diff] [blame] | 3062 | } |
| 3063 | |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3064 | TEST_F(ClientSocketPoolBaseTest, CancelBackupSocketAfterFinishingAllRequests) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3065 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets, |
| 3066 | true /* enable_backup_connect_jobs */); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3067 | |
| 3068 | // Create the first socket and set to ERR_IO_PENDING. This starts the backup |
| 3069 | // timer. |
| 3070 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3071 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3072 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3073 | EXPECT_EQ( |
| 3074 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3075 | handle.Init(TestGroupId("bar"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3076 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 3077 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3078 | pool_.get(), NetLogWithSource())); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3079 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3080 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3081 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3082 | EXPECT_EQ( |
| 3083 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3084 | handle2.Init(TestGroupId("bar"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3085 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3086 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3087 | pool_.get(), NetLogWithSource())); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3088 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("bar"))); |
| 3089 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("bar"))); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3090 | |
| 3091 | // Cancel request 1 and then complete request 2. With the requests finished, |
| 3092 | // the backup timer should be cancelled. |
| 3093 | handle.Reset(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3094 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3095 | // 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] | 3096 | FastForwardBy(base::TimeDelta::FromMilliseconds( |
[email protected] | 26b997396 | 2012-01-28 00:57:00 | [diff] [blame] | 3097 | ClientSocketPool::kMaxConnectRetryIntervalMs / 2 * 3)); |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 3098 | } |
| 3099 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3100 | // Test delayed socket binding for the case where we have two connects, |
| 3101 | // and while one is waiting on a connect, the other frees up. |
| 3102 | // The socket waiting on a connect should switch immediately to the freed |
| 3103 | // up socket. |
| 3104 | TEST_F(ClientSocketPoolBaseTest, DelayedSocketBindingWaitingForConnect) { |
| 3105 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3106 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3107 | |
| 3108 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3109 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3110 | EXPECT_EQ( |
| 3111 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3112 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3113 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3114 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3115 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3116 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3117 | |
| 3118 | // No idle sockets, no pending jobs. |
| 3119 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3120 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3121 | |
| 3122 | // Create a second socket to the same host, but this one will wait. |
| 3123 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3124 | ClientSocketHandle handle2; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3125 | EXPECT_EQ( |
| 3126 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3127 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3128 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3129 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3130 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3131 | // No idle sockets, and one connecting job. |
| 3132 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3133 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3134 | |
| 3135 | // Return the first handle to the pool. This will initiate the delayed |
| 3136 | // binding. |
| 3137 | handle1.Reset(); |
| 3138 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3139 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3140 | |
| 3141 | // Still no idle sockets, still one pending connect job. |
| 3142 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3143 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3144 | |
| 3145 | // The second socket connected, even though it was a Waiting Job. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3146 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3147 | |
| 3148 | // And we can see there is still one job waiting. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3149 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3150 | |
| 3151 | // Finally, signal the waiting Connect. |
| 3152 | client_socket_factory_.SignalJobs(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3153 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3154 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3155 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3156 | } |
| 3157 | |
| 3158 | // Test delayed socket binding when a group is at capacity and one |
| 3159 | // of the group's sockets frees up. |
| 3160 | TEST_F(ClientSocketPoolBaseTest, DelayedSocketBindingAtGroupCapacity) { |
| 3161 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3162 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3163 | |
| 3164 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3165 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3166 | EXPECT_EQ( |
| 3167 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3168 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3169 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3170 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3171 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3172 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3173 | |
| 3174 | // No idle sockets, no pending jobs. |
| 3175 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3176 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3177 | |
| 3178 | // Create a second socket to the same host, but this one will wait. |
| 3179 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3180 | ClientSocketHandle handle2; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3181 | EXPECT_EQ( |
| 3182 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3183 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3184 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3185 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3186 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3187 | // No idle sockets, and one connecting job. |
| 3188 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3189 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3190 | |
| 3191 | // Return the first handle to the pool. This will initiate the delayed |
| 3192 | // binding. |
| 3193 | handle1.Reset(); |
| 3194 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3195 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3196 | |
| 3197 | // Still no idle sockets, still one pending connect job. |
| 3198 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3199 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3200 | |
| 3201 | // The second socket connected, even though it was a Waiting Job. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3202 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3203 | |
| 3204 | // And we can see there is still one job waiting. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3205 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3206 | |
| 3207 | // Finally, signal the waiting Connect. |
| 3208 | client_socket_factory_.SignalJobs(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3209 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3210 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3211 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3212 | } |
| 3213 | |
| 3214 | // Test out the case where we have one socket connected, one |
| 3215 | // connecting, when the first socket finishes and goes idle. |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3216 | // Although the second connection is pending, the second request |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3217 | // should complete, by taking the first socket's idle socket. |
| 3218 | TEST_F(ClientSocketPoolBaseTest, DelayedSocketBindingAtStall) { |
| 3219 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3220 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3221 | |
| 3222 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3223 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3224 | EXPECT_EQ( |
| 3225 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3226 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3227 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3228 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3229 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3230 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3231 | |
| 3232 | // No idle sockets, no pending jobs. |
| 3233 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3234 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3235 | |
| 3236 | // Create a second socket to the same host, but this one will wait. |
| 3237 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 3238 | ClientSocketHandle handle2; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3239 | EXPECT_EQ( |
| 3240 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3241 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3242 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3243 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3244 | pool_.get(), NetLogWithSource())); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3245 | // No idle sockets, and one connecting job. |
| 3246 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3247 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3248 | |
| 3249 | // Return the first handle to the pool. This will initiate the delayed |
| 3250 | // binding. |
| 3251 | handle1.Reset(); |
| 3252 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3253 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3254 | |
| 3255 | // Still no idle sockets, still one pending connect job. |
| 3256 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3257 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3258 | |
| 3259 | // The second socket connected, even though it was a Waiting Job. |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3260 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3261 | |
| 3262 | // And we can see there is still one job waiting. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3263 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3264 | |
| 3265 | // Finally, signal the waiting Connect. |
| 3266 | client_socket_factory_.SignalJobs(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3267 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3268 | |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 3269 | base::RunLoop().RunUntilIdle(); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 3270 | } |
| 3271 | |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3272 | // Cover the case where on an available socket slot, we have one pending |
| 3273 | // request that completes synchronously, thereby making the Group empty. |
| 3274 | TEST_F(ClientSocketPoolBaseTest, SynchronouslyProcessOnePendingRequest) { |
| 3275 | const int kUnlimitedSockets = 100; |
| 3276 | const int kOneSocketPerGroup = 1; |
| 3277 | CreatePool(kUnlimitedSockets, kOneSocketPerGroup); |
| 3278 | |
| 3279 | // Make the first request asynchronous fail. |
| 3280 | // This will free up a socket slot later. |
| 3281 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); |
| 3282 | |
| 3283 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3284 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3285 | EXPECT_EQ( |
| 3286 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3287 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3288 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3289 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3290 | pool_.get(), NetLogWithSource())); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3291 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3292 | |
| 3293 | // Make the second request synchronously fail. This should make the Group |
| 3294 | // empty. |
| 3295 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
| 3296 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3297 | TestCompletionCallback callback2; |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3298 | // It'll be ERR_IO_PENDING now, but the TestConnectJob will synchronously fail |
| 3299 | // when created. |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3300 | EXPECT_EQ( |
| 3301 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3302 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3303 | SocketTag(), 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] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3306 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3307 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3308 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3309 | EXPECT_THAT(callback1.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 3310 | EXPECT_THAT(callback2.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3311 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 3312 | } |
| 3313 | |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3314 | TEST_F(ClientSocketPoolBaseTest, PreferUsedSocketToUnusedSocket) { |
| 3315 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 3316 | |
| 3317 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3318 | |
| 3319 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3320 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3321 | EXPECT_EQ( |
| 3322 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3323 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3324 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3325 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3326 | pool_.get(), NetLogWithSource())); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3327 | |
| 3328 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3329 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3330 | EXPECT_EQ( |
| 3331 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3332 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3333 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3334 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3335 | pool_.get(), NetLogWithSource())); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3336 | ClientSocketHandle handle3; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3337 | TestCompletionCallback callback3; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3338 | EXPECT_EQ( |
| 3339 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3340 | handle3.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3341 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3342 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3343 | pool_.get(), NetLogWithSource())); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3344 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3345 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3346 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
| 3347 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3348 | |
| 3349 | // Use the socket. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 3350 | EXPECT_EQ(1, handle1.socket()->Write(nullptr, 1, CompletionOnceCallback(), |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 3351 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 3352 | EXPECT_EQ(1, handle3.socket()->Write(nullptr, 1, CompletionOnceCallback(), |
Ramin Halavati | 0a08cc8 | 2018-02-06 07:46:38 | [diff] [blame] | 3353 | TRAFFIC_ANNOTATION_FOR_TESTS)); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3354 | |
| 3355 | handle1.Reset(); |
| 3356 | handle2.Reset(); |
| 3357 | handle3.Reset(); |
| 3358 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3359 | EXPECT_EQ(OK, handle1.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3360 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3361 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3362 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3363 | pool_.get(), NetLogWithSource())); |
| 3364 | EXPECT_EQ(OK, handle2.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3365 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3366 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3367 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3368 | pool_.get(), NetLogWithSource())); |
| 3369 | EXPECT_EQ(OK, handle3.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3370 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3371 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3372 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3373 | pool_.get(), NetLogWithSource())); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 3374 | |
| 3375 | EXPECT_TRUE(handle1.socket()->WasEverUsed()); |
| 3376 | EXPECT_TRUE(handle2.socket()->WasEverUsed()); |
| 3377 | EXPECT_FALSE(handle3.socket()->WasEverUsed()); |
| 3378 | } |
| 3379 | |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3380 | TEST_F(ClientSocketPoolBaseTest, RequestSockets) { |
| 3381 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3382 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3383 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3384 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 3385 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3386 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3387 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3388 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3389 | EXPECT_EQ(2u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3390 | TestGroupId("a"))); |
| 3391 | EXPECT_EQ(2u, |
| 3392 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3393 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3394 | |
| 3395 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3396 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3397 | EXPECT_EQ( |
| 3398 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3399 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3400 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3401 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3402 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3403 | |
| 3404 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3405 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3406 | EXPECT_EQ( |
| 3407 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3408 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3409 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3410 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3411 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3412 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3413 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3414 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3415 | TestGroupId("a"))); |
| 3416 | EXPECT_EQ(0u, |
| 3417 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3418 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3419 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3420 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3421 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3422 | handle1.Reset(); |
| 3423 | handle2.Reset(); |
| 3424 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3425 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3426 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3427 | TestGroupId("a"))); |
| 3428 | EXPECT_EQ(0u, |
| 3429 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3430 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3431 | } |
| 3432 | |
| 3433 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsWhenAlreadyHaveAConnectJob) { |
| 3434 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3435 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3436 | |
| 3437 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3438 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3439 | EXPECT_EQ( |
| 3440 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3441 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3442 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3443 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3444 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3445 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3446 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3447 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3448 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3449 | TestGroupId("a"))); |
| 3450 | EXPECT_EQ(0u, |
| 3451 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3452 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3453 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3454 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 3455 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3456 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3457 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3458 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3459 | TestGroupId("a"))); |
| 3460 | EXPECT_EQ(1u, |
| 3461 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3462 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3463 | |
| 3464 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3465 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3466 | EXPECT_EQ( |
| 3467 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3468 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3469 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3470 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3471 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3472 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3473 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3474 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3475 | TestGroupId("a"))); |
| 3476 | EXPECT_EQ(0u, |
| 3477 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3478 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3479 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3480 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3481 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3482 | handle1.Reset(); |
| 3483 | handle2.Reset(); |
| 3484 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3485 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3486 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3487 | TestGroupId("a"))); |
| 3488 | EXPECT_EQ(0u, |
| 3489 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3490 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3491 | } |
| 3492 | |
| 3493 | TEST_F(ClientSocketPoolBaseTest, |
| 3494 | RequestSocketsWhenAlreadyHaveMultipleConnectJob) { |
| 3495 | CreatePool(4, 4); |
| 3496 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3497 | |
| 3498 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3499 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3500 | EXPECT_EQ( |
| 3501 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3502 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3503 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3504 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3505 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3506 | |
| 3507 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3508 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3509 | EXPECT_EQ( |
| 3510 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3511 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3512 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3513 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3514 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3515 | |
| 3516 | ClientSocketHandle handle3; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3517 | TestCompletionCallback callback3; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3518 | EXPECT_EQ( |
| 3519 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3520 | handle3.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3521 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3522 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3523 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3524 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3525 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3526 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3527 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3528 | TestGroupId("a"))); |
| 3529 | EXPECT_EQ(0u, |
| 3530 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3531 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3532 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3533 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 3534 | 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(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3537 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3538 | TestGroupId("a"))); |
| 3539 | EXPECT_EQ(0u, |
| 3540 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3541 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3542 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3543 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3544 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
| 3545 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3546 | handle1.Reset(); |
| 3547 | handle2.Reset(); |
| 3548 | handle3.Reset(); |
| 3549 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3550 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3551 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3552 | TestGroupId("a"))); |
| 3553 | EXPECT_EQ(0u, |
| 3554 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3555 | EXPECT_EQ(3u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3556 | } |
| 3557 | |
| 3558 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsAtMaxSocketLimit) { |
| 3559 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 3560 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3561 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3562 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3563 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3564 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, |
| 3565 | kDefaultMaxSockets, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3566 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3567 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3568 | EXPECT_EQ(kDefaultMaxSockets, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3569 | static_cast<int>( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3570 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
| 3571 | EXPECT_EQ( |
| 3572 | kDefaultMaxSockets, |
| 3573 | static_cast<int>(pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3574 | TestGroupId("a")))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3575 | EXPECT_EQ(kDefaultMaxSockets, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3576 | static_cast<int>(pool_->NumUnassignedConnectJobsInGroupForTesting( |
| 3577 | TestGroupId("a")))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3578 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3579 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("b"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3580 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3581 | pool_->RequestSockets(TestGroupId("b"), params_, base::nullopt, |
| 3582 | kDefaultMaxSockets, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3583 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3584 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("b"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3585 | } |
| 3586 | |
| 3587 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsHitMaxSocketLimit) { |
| 3588 | CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 3589 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3590 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3591 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3592 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3593 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, |
| 3594 | kDefaultMaxSockets - 1, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3595 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3596 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3597 | EXPECT_EQ(kDefaultMaxSockets - 1, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3598 | static_cast<int>( |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3599 | pool_->NumConnectJobsInGroupForTesting(TestGroupId("a")))); |
| 3600 | EXPECT_EQ( |
| 3601 | kDefaultMaxSockets - 1, |
| 3602 | static_cast<int>(pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3603 | TestGroupId("a")))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3604 | EXPECT_EQ(kDefaultMaxSockets - 1, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3605 | static_cast<int>(pool_->NumUnassignedConnectJobsInGroupForTesting( |
| 3606 | TestGroupId("a")))); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 3607 | EXPECT_FALSE(pool_->IsStalled()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3608 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3609 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("b"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3610 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3611 | pool_->RequestSockets(TestGroupId("b"), params_, base::nullopt, |
| 3612 | kDefaultMaxSockets, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3613 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3614 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("b"))); |
| 3615 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
[email protected] | 51fdc7c | 2012-04-10 19:19:48 | [diff] [blame] | 3616 | EXPECT_FALSE(pool_->IsStalled()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3617 | } |
| 3618 | |
| 3619 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsCountIdleSockets) { |
| 3620 | CreatePool(4, 4); |
| 3621 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3622 | |
| 3623 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3624 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3625 | EXPECT_EQ( |
| 3626 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3627 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3628 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3629 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3630 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3631 | ASSERT_THAT(callback1.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3632 | handle1.Reset(); |
| 3633 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3634 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3635 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3636 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3637 | TestGroupId("a"))); |
| 3638 | EXPECT_EQ(0u, |
| 3639 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3640 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3641 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3642 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 3643 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3644 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3645 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3646 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3647 | TestGroupId("a"))); |
| 3648 | EXPECT_EQ(1u, |
| 3649 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3650 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3651 | } |
| 3652 | |
| 3653 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsCountActiveSockets) { |
| 3654 | CreatePool(4, 4); |
| 3655 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3656 | |
| 3657 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3658 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3659 | EXPECT_EQ( |
| 3660 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3661 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3662 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3663 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3664 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3665 | ASSERT_THAT(callback1.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3666 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3667 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3668 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3669 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3670 | TestGroupId("a"))); |
| 3671 | EXPECT_EQ(0u, |
| 3672 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3673 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3674 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3675 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3676 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 3677 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3678 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3679 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3680 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3681 | TestGroupId("a"))); |
| 3682 | EXPECT_EQ(1u, |
| 3683 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3684 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3685 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3686 | } |
| 3687 | |
| 3688 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsSynchronous) { |
| 3689 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3690 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 3691 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3692 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, |
| 3693 | kDefaultMaxSocketsPerGroup, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3694 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3695 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3696 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3697 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3698 | TestGroupId("a"))); |
| 3699 | EXPECT_EQ(0u, |
| 3700 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3701 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3702 | static_cast<int>(pool_->IdleSocketCountInGroup(TestGroupId("a")))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3703 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3704 | pool_->RequestSockets(TestGroupId("b"), params_, base::nullopt, |
| 3705 | kDefaultMaxSocketsPerGroup, NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3706 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3707 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 3708 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3709 | TestGroupId("b"))); |
| 3710 | EXPECT_EQ(0u, |
| 3711 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
Raul Tambre | 8335a6d | 2019-02-21 16:57:43 | [diff] [blame] | 3712 | EXPECT_EQ(kDefaultMaxSocketsPerGroup, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3713 | static_cast<int>(pool_->IdleSocketCountInGroup(TestGroupId("b")))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3714 | } |
| 3715 | |
[email protected] | 3c819f52 | 2010-12-02 02:03:12 | [diff] [blame] | 3716 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsSynchronousError) { |
| 3717 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3718 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
| 3719 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3720 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, |
| 3721 | kDefaultMaxSocketsPerGroup, NetLogWithSource()); |
[email protected] | 3c819f52 | 2010-12-02 02:03:12 | [diff] [blame] | 3722 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3723 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | fd2e53e | 2011-01-14 20:40:52 | [diff] [blame] | 3724 | |
| 3725 | connect_job_factory_->set_job_type( |
| 3726 | TestConnectJob::kMockAdditionalErrorStateJob); |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3727 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, |
| 3728 | kDefaultMaxSocketsPerGroup, NetLogWithSource()); |
[email protected] | fd2e53e | 2011-01-14 20:40:52 | [diff] [blame] | 3729 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3730 | ASSERT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | 3c819f52 | 2010-12-02 02:03:12 | [diff] [blame] | 3731 | } |
| 3732 | |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3733 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsMultipleTimesDoesNothing) { |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3734 | CreatePool(4, 4); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3735 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3736 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3737 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 3738 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3739 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3740 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3741 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3742 | EXPECT_EQ(2u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3743 | TestGroupId("a"))); |
| 3744 | EXPECT_EQ(2u, |
| 3745 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3746 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3747 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3748 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3749 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 3750 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3751 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3752 | EXPECT_EQ(2u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3753 | TestGroupId("a"))); |
| 3754 | EXPECT_EQ(2u, |
| 3755 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3756 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3757 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3758 | |
| 3759 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3760 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3761 | EXPECT_EQ( |
| 3762 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3763 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3764 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3765 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3766 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3767 | |
| 3768 | client_socket_factory_.SignalJob(0); |
| 3769 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3770 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3771 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3772 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3773 | TestGroupId("a"))); |
| 3774 | EXPECT_EQ(1u, |
| 3775 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3776 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3777 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3778 | |
| 3779 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3780 | TestCompletionCallback callback2; |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3781 | EXPECT_EQ( |
| 3782 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3783 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3784 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3785 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3786 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3787 | client_socket_factory_.SignalJob(0); |
| 3788 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3789 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3790 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3791 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3792 | TestGroupId("a"))); |
| 3793 | EXPECT_EQ(0u, |
| 3794 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3795 | EXPECT_EQ(2, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3796 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 8159a1c | 2012-06-07 00:00:10 | [diff] [blame] | 3797 | |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3798 | handle1.Reset(); |
| 3799 | handle2.Reset(); |
| 3800 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3801 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3802 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3803 | TestGroupId("a"))); |
| 3804 | EXPECT_EQ(0u, |
| 3805 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3806 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3807 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3808 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3809 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 3810 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3811 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3812 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3813 | TestGroupId("a"))); |
| 3814 | EXPECT_EQ(0u, |
| 3815 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3816 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3817 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3818 | } |
| 3819 | |
| 3820 | TEST_F(ClientSocketPoolBaseTest, RequestSocketsDifferentNumSockets) { |
| 3821 | CreatePool(4, 4); |
| 3822 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3823 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3824 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 1, |
| 3825 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3826 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3827 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3828 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3829 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3830 | TestGroupId("a"))); |
| 3831 | EXPECT_EQ(1u, |
| 3832 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3833 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3834 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3835 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 3836 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3837 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3838 | EXPECT_EQ(2u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3839 | TestGroupId("a"))); |
| 3840 | EXPECT_EQ(2u, |
| 3841 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3842 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3843 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3844 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 3, |
| 3845 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3846 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3847 | EXPECT_EQ(3u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3848 | TestGroupId("a"))); |
| 3849 | EXPECT_EQ(3u, |
| 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] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3852 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3853 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 1, |
| 3854 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3855 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3856 | EXPECT_EQ(3u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3857 | TestGroupId("a"))); |
| 3858 | EXPECT_EQ(3u, |
| 3859 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3860 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3861 | } |
| 3862 | |
| 3863 | TEST_F(ClientSocketPoolBaseTest, PreconnectJobsTakenByNormalRequests) { |
| 3864 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3865 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3866 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3867 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 1, |
| 3868 | NetLogWithSource()); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3869 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3870 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3871 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3872 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3873 | TestGroupId("a"))); |
| 3874 | EXPECT_EQ(1u, |
| 3875 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3876 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3877 | |
| 3878 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3879 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3880 | EXPECT_EQ( |
| 3881 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3882 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3883 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3884 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3885 | pool_.get(), NetLogWithSource())); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3886 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3887 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3888 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3889 | TestGroupId("a"))); |
| 3890 | EXPECT_EQ(0u, |
| 3891 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3892 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3893 | |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3894 | client_socket_factory_.SignalJobs(); |
| 3895 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 3896 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3897 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3898 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3899 | TestGroupId("a"))); |
| 3900 | EXPECT_EQ(0u, |
| 3901 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3902 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3903 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3904 | |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 3905 | // Make sure if a preconnected socket is not fully connected when a request |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 3906 | // starts, it has a connect start time. |
| 3907 | TestLoadTimingInfoConnectedNotReused(handle1); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3908 | handle1.Reset(); |
| 3909 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3910 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 3911 | } |
| 3912 | |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 3913 | // Checks that fully connected preconnect jobs have no connect times, and are |
| 3914 | // marked as reused. |
| 3915 | TEST_F(ClientSocketPoolBaseTest, ConnectedPreconnectJobsHaveNoConnectTimes) { |
| 3916 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 3917 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3918 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 1, |
| 3919 | NetLogWithSource()); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 3920 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3921 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3922 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3923 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3924 | TestGroupId("a"))); |
| 3925 | EXPECT_EQ(0u, |
| 3926 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3927 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 3928 | |
| 3929 | ClientSocketHandle handle; |
| 3930 | TestCompletionCallback callback; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3931 | EXPECT_EQ(OK, handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3932 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3933 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3934 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3935 | pool_.get(), NetLogWithSource())); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 3936 | |
| 3937 | // Make sure the idle socket was used. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3938 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 034df0f3 | 2013-01-07 23:17:48 | [diff] [blame] | 3939 | |
| 3940 | TestLoadTimingInfoConnectedReused(handle); |
| 3941 | handle.Reset(); |
| 3942 | TestLoadTimingInfoNotConnected(handle); |
| 3943 | } |
| 3944 | |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3945 | // http://crbug.com/64940 regression test. |
| 3946 | TEST_F(ClientSocketPoolBaseTest, PreconnectClosesIdleSocketRemovesGroup) { |
| 3947 | const int kMaxTotalSockets = 3; |
| 3948 | const int kMaxSocketsPerGroup = 2; |
| 3949 | CreatePool(kMaxTotalSockets, kMaxSocketsPerGroup); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3950 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3951 | |
Matt Menke | f6edce75 | 2019-03-19 17:21:56 | [diff] [blame] | 3952 | // Note that group id ordering matters here. "a" comes before "b", so |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3953 | // CloseOneIdleSocket() will try to close "a"'s idle socket. |
| 3954 | |
| 3955 | // Set up one idle socket in "a". |
| 3956 | ClientSocketHandle handle1; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3957 | TestCompletionCallback callback1; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3958 | EXPECT_EQ( |
| 3959 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3960 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3961 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3962 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3963 | pool_.get(), NetLogWithSource())); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3964 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 3965 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3966 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3967 | TestGroupId("a"))); |
| 3968 | EXPECT_EQ(0u, |
| 3969 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3970 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3971 | |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3972 | client_socket_factory_.SignalJobs(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 3973 | ASSERT_THAT(callback1.WaitForResult(), IsOk()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 3974 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3975 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 3976 | TestGroupId("a"))); |
| 3977 | EXPECT_EQ(0u, |
| 3978 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 3979 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 3980 | |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3981 | handle1.Reset(); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 3982 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3983 | |
| 3984 | // Set up two active sockets in "b". |
| 3985 | ClientSocketHandle handle2; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 3986 | TestCompletionCallback callback2; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3987 | EXPECT_EQ( |
| 3988 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3989 | handle1.Init(TestGroupId("b"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3990 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3991 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3992 | pool_.get(), NetLogWithSource())); |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 3993 | EXPECT_EQ( |
| 3994 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 3995 | handle2.Init(TestGroupId("b"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 3996 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 3997 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 3998 | pool_.get(), NetLogWithSource())); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 3999 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4000 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("b"))); |
| 4001 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 4002 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4003 | TestGroupId("b"))); |
| 4004 | EXPECT_EQ(0u, |
| 4005 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4006 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4007 | |
| 4008 | client_socket_factory_.SignalJobs(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4009 | ASSERT_THAT(callback1.WaitForResult(), IsOk()); |
| 4010 | ASSERT_THAT(callback2.WaitForResult(), IsOk()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4011 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4012 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4013 | TestGroupId("b"))); |
| 4014 | EXPECT_EQ(0u, |
| 4015 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 4016 | EXPECT_EQ(2, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4017 | |
| 4018 | // Now we have 1 idle socket in "a" and 2 active sockets in "b". This means |
| 4019 | // we've maxed out on sockets, since we set |kMaxTotalSockets| to 3. |
| 4020 | // Requesting 2 preconnected sockets for "a" should fail to allocate any more |
| 4021 | // sockets for "a", and "b" should still have 2 active sockets. |
| 4022 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4023 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 4024 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4025 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4026 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4027 | TestGroupId("a"))); |
| 4028 | EXPECT_EQ(0u, |
| 4029 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4030 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4031 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 4032 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 4033 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4034 | TestGroupId("b"))); |
| 4035 | EXPECT_EQ(0u, |
| 4036 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4037 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4038 | EXPECT_EQ(2, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4039 | |
| 4040 | // Now release the 2 active sockets for "b". This will give us 1 idle socket |
| 4041 | // in "a" and 2 idle sockets in "b". Requesting 2 preconnected sockets for |
| 4042 | // "a" should result in closing 1 for "b". |
| 4043 | handle1.Reset(); |
| 4044 | handle2.Reset(); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4045 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4046 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4047 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4048 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 2, |
| 4049 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4050 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4051 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4052 | TestGroupId("a"))); |
| 4053 | EXPECT_EQ(1u, |
| 4054 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4055 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4056 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
| 4057 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 4058 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4059 | TestGroupId("b"))); |
| 4060 | EXPECT_EQ(0u, |
| 4061 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4062 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4063 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 4064 | } |
| 4065 | |
[email protected] | b7b8be4 | 2011-07-12 12:46:41 | [diff] [blame] | 4066 | TEST_F(ClientSocketPoolBaseTest, PreconnectWithoutBackupJob) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4067 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 4068 | true /* enable_backup_connect_jobs */); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4069 | |
| 4070 | // Make the ConnectJob hang until it times out, shorten the timeout. |
| 4071 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4072 | connect_job_factory_->set_timeout_duration( |
| 4073 | base::TimeDelta::FromMilliseconds(500)); |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4074 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 1, |
| 4075 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4076 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4077 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4078 | TestGroupId("a"))); |
| 4079 | EXPECT_EQ(1u, |
| 4080 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4081 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4082 | |
[email protected] | b7b8be4 | 2011-07-12 12:46:41 | [diff] [blame] | 4083 | // Verify the backup timer doesn't create a backup job, by making |
| 4084 | // the backup job a pending job instead of a waiting job, so it |
| 4085 | // *would* complete if it were created. |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4086 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 4087 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
Gabriel Charette | ea91801 | 2018-05-16 11:53:44 | [diff] [blame] | 4088 | FROM_HERE, base::RunLoop::QuitCurrentWhenIdleClosureDeprecated(), |
[email protected] | 2da659e | 2013-05-23 20:51:34 | [diff] [blame] | 4089 | base::TimeDelta::FromSeconds(1)); |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 4090 | base::RunLoop().Run(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4091 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4092 | } |
| 4093 | |
[email protected] | b7b8be4 | 2011-07-12 12:46:41 | [diff] [blame] | 4094 | TEST_F(ClientSocketPoolBaseTest, PreconnectWithBackupJob) { |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4095 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup, |
| 4096 | true /* enable_backup_connect_jobs */); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4097 | |
| 4098 | // Make the ConnectJob hang forever. |
| 4099 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4100 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 1, |
| 4101 | NetLogWithSource()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4102 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4103 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4104 | TestGroupId("a"))); |
| 4105 | EXPECT_EQ(1u, |
| 4106 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4107 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
fdoray | 5eeb764 | 2016-06-22 16:11:28 | [diff] [blame] | 4108 | base::RunLoop().RunUntilIdle(); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4109 | |
| 4110 | // Make the backup job be a pending job, so it completes normally. |
| 4111 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 4112 | ClientSocketHandle handle; |
[email protected] | 6ecf2b9 | 2011-12-15 01:14:52 | [diff] [blame] | 4113 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4114 | EXPECT_EQ( |
| 4115 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4116 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4117 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4118 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4119 | pool_.get(), NetLogWithSource())); |
[email protected] | b7b8be4 | 2011-07-12 12:46:41 | [diff] [blame] | 4120 | // Timer has started, but the backup connect job shouldn't be created yet. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4121 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4122 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4123 | TestGroupId("a"))); |
| 4124 | EXPECT_EQ(0u, |
| 4125 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4126 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4127 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4128 | ASSERT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4129 | |
| 4130 | // The hung connect job should still be there, but everything else should be |
| 4131 | // complete. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4132 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4133 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4134 | TestGroupId("a"))); |
| 4135 | EXPECT_EQ(1u, |
| 4136 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4137 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4138 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | a9fc8fc | 2011-05-10 02:41:07 | [diff] [blame] | 4139 | } |
| 4140 | |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4141 | // Tests that a preconnect that starts out with unread data can still be used. |
| 4142 | // http://crbug.com/334467 |
| 4143 | TEST_F(ClientSocketPoolBaseTest, PreconnectWithUnreadData) { |
| 4144 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4145 | connect_job_factory_->set_job_type(TestConnectJob::kMockUnreadDataJob); |
| 4146 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4147 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 1, |
| 4148 | NetLogWithSource()); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4149 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4150 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 4151 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4152 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4153 | TestGroupId("a"))); |
| 4154 | EXPECT_EQ(0u, |
| 4155 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4156 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4157 | |
| 4158 | // Fail future jobs to be sure that handle receives the preconnected socket |
| 4159 | // rather than closing it and making a new one. |
| 4160 | connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); |
| 4161 | ClientSocketHandle handle; |
| 4162 | TestCompletionCallback callback; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4163 | EXPECT_EQ(OK, handle.Init( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4164 | TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4165 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4166 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4167 | pool_.get(), NetLogWithSource())); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4168 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4169 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 4170 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4171 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4172 | TestGroupId("a"))); |
| 4173 | EXPECT_EQ(0u, |
| 4174 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4175 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4176 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4177 | |
| 4178 | // Drain the pending read. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 4179 | EXPECT_EQ(1, handle.socket()->Read(nullptr, 1, CompletionOnceCallback())); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4180 | |
| 4181 | TestLoadTimingInfoConnectedReused(handle); |
| 4182 | handle.Reset(); |
| 4183 | |
| 4184 | // The socket should be usable now that it's idle again. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4185 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
[email protected] | 0dc88b3 | 2014-03-26 20:12:28 | [diff] [blame] | 4186 | } |
| 4187 | |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4188 | TEST_F(ClientSocketPoolBaseTest, RequestGetsAssignedJob) { |
| 4189 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4190 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4191 | |
| 4192 | ClientSocketHandle handle1; |
| 4193 | TestCompletionCallback callback1; |
| 4194 | EXPECT_EQ( |
| 4195 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4196 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4197 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4198 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4199 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4200 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4201 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4202 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4203 | TestGroupId("a"))); |
| 4204 | EXPECT_EQ(0u, |
| 4205 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4206 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4207 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4208 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4209 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4210 | } |
| 4211 | |
| 4212 | TEST_F(ClientSocketPoolBaseTest, MultipleRequestsGetAssignedJobs) { |
| 4213 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4214 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4215 | |
| 4216 | ClientSocketHandle handle1; |
| 4217 | TestCompletionCallback callback1; |
| 4218 | EXPECT_EQ( |
| 4219 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4220 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4221 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4222 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4223 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4224 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4225 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4226 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4227 | TestGroupId("a"))); |
| 4228 | EXPECT_EQ(0u, |
| 4229 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4230 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4231 | |
| 4232 | ClientSocketHandle handle2; |
| 4233 | TestCompletionCallback callback2; |
| 4234 | EXPECT_EQ( |
| 4235 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4236 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4237 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4238 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4239 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4240 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4241 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4242 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4243 | TestGroupId("a"))); |
| 4244 | EXPECT_EQ(0u, |
| 4245 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4246 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4247 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4248 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4249 | &handle1)); |
| 4250 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4251 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4252 | |
| 4253 | // One job completes. The other request should still have its job. |
| 4254 | client_socket_factory_.SignalJob(0); |
| 4255 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
| 4256 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4257 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4258 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4259 | TestGroupId("a"))); |
| 4260 | EXPECT_EQ(0u, |
| 4261 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4262 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4263 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4264 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4265 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4266 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4267 | } |
| 4268 | |
| 4269 | TEST_F(ClientSocketPoolBaseTest, PreconnectJobGetsAssignedToRequest) { |
| 4270 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4271 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4272 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4273 | pool_->RequestSockets(TestGroupId("a"), params_, base::nullopt, 1, |
| 4274 | NetLogWithSource()); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4275 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4276 | ASSERT_TRUE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 4277 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4278 | EXPECT_EQ(1u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4279 | TestGroupId("a"))); |
| 4280 | EXPECT_EQ(1u, |
| 4281 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4282 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4283 | |
| 4284 | ClientSocketHandle handle1; |
| 4285 | TestCompletionCallback callback1; |
| 4286 | EXPECT_EQ( |
| 4287 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4288 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4289 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4290 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4291 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4292 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4293 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4294 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4295 | TestGroupId("a"))); |
| 4296 | EXPECT_EQ(0u, |
| 4297 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4298 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4299 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4300 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4301 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4302 | } |
| 4303 | |
| 4304 | TEST_F(ClientSocketPoolBaseTest, HigherPriorityRequestStealsJob) { |
| 4305 | CreatePool(kDefaultMaxSockets, 1); |
| 4306 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4307 | |
| 4308 | ClientSocketHandle handle1; |
| 4309 | TestCompletionCallback callback1; |
| 4310 | EXPECT_EQ( |
| 4311 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4312 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4313 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4314 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4315 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4316 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4317 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4318 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4319 | TestGroupId("a"))); |
| 4320 | EXPECT_EQ(0u, |
| 4321 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4322 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4323 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4324 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4325 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4326 | |
| 4327 | // Insert a higher priority request |
| 4328 | ClientSocketHandle handle2; |
| 4329 | TestCompletionCallback callback2; |
| 4330 | EXPECT_EQ( |
| 4331 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4332 | handle2.Init(TestGroupId("a"), params_, base::nullopt, HIGHEST, |
| 4333 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4334 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4335 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4336 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4337 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4338 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4339 | TestGroupId("a"))); |
| 4340 | EXPECT_EQ(0u, |
| 4341 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4342 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4343 | |
| 4344 | // The highest priority request should steal the job from the default priority |
| 4345 | // request. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4346 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4347 | &handle2)); |
| 4348 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4349 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4350 | } |
| 4351 | |
| 4352 | TEST_F(ClientSocketPoolBaseTest, RequestStealsJobFromLowestRequestWithJob) { |
| 4353 | CreatePool(3, 3); |
| 4354 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4355 | |
| 4356 | ClientSocketHandle handle_lowest; |
| 4357 | TestCompletionCallback callback_lowest; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4358 | EXPECT_EQ( |
| 4359 | ERR_IO_PENDING, |
| 4360 | handle_lowest.Init(TestGroupId("a"), params_, base::nullopt, LOWEST, |
| 4361 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4362 | callback_lowest.callback(), |
| 4363 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4364 | NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4365 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4366 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4367 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4368 | TestGroupId("a"))); |
| 4369 | EXPECT_EQ(0u, |
| 4370 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4371 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4372 | |
| 4373 | ClientSocketHandle handle_highest; |
| 4374 | TestCompletionCallback callback_highest; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4375 | EXPECT_EQ( |
| 4376 | ERR_IO_PENDING, |
| 4377 | handle_highest.Init(TestGroupId("a"), params_, base::nullopt, HIGHEST, |
| 4378 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4379 | callback_highest.callback(), |
| 4380 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4381 | NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4382 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4383 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4384 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4385 | TestGroupId("a"))); |
| 4386 | EXPECT_EQ(0u, |
| 4387 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4388 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4389 | |
| 4390 | ClientSocketHandle handle_low; |
| 4391 | TestCompletionCallback callback_low; |
| 4392 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4393 | handle_low.Init( |
| 4394 | TestGroupId("a"), params_, base::nullopt, LOW, SocketTag(), |
| 4395 | ClientSocketPool::RespectLimits::ENABLED, |
| 4396 | callback_low.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4397 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4398 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4399 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4400 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4401 | TestGroupId("a"))); |
| 4402 | EXPECT_EQ(0u, |
| 4403 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4404 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4405 | |
| 4406 | ClientSocketHandle handle_lowest2; |
| 4407 | TestCompletionCallback callback_lowest2; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4408 | EXPECT_EQ( |
| 4409 | ERR_IO_PENDING, |
| 4410 | handle_lowest2.Init(TestGroupId("a"), params_, base::nullopt, LOWEST, |
| 4411 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4412 | callback_lowest2.callback(), |
| 4413 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4414 | NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4415 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4416 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4417 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4418 | TestGroupId("a"))); |
| 4419 | EXPECT_EQ(0u, |
| 4420 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4421 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4422 | |
| 4423 | // The top three requests in the queue should have jobs. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4424 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4425 | &handle_highest)); |
| 4426 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4427 | &handle_low)); |
| 4428 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4429 | &handle_lowest)); |
| 4430 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting( |
| 4431 | TestGroupId("a"), &handle_lowest2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4432 | |
| 4433 | // Add another request with medium priority. It should steal the job from the |
| 4434 | // lowest priority request with a job. |
| 4435 | ClientSocketHandle handle_medium; |
| 4436 | TestCompletionCallback callback_medium; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4437 | EXPECT_EQ( |
| 4438 | ERR_IO_PENDING, |
| 4439 | handle_medium.Init(TestGroupId("a"), params_, base::nullopt, MEDIUM, |
| 4440 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4441 | callback_medium.callback(), |
| 4442 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4443 | NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4444 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4445 | EXPECT_EQ(3u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4446 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4447 | TestGroupId("a"))); |
| 4448 | EXPECT_EQ(0u, |
| 4449 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4450 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
| 4451 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4452 | &handle_highest)); |
| 4453 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4454 | &handle_medium)); |
| 4455 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4456 | &handle_low)); |
| 4457 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4458 | &handle_lowest)); |
| 4459 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting( |
| 4460 | TestGroupId("a"), &handle_lowest2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4461 | } |
| 4462 | |
| 4463 | TEST_F(ClientSocketPoolBaseTest, ReprioritizeRequestStealsJob) { |
| 4464 | CreatePool(kDefaultMaxSockets, 1); |
| 4465 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4466 | |
| 4467 | ClientSocketHandle handle1; |
| 4468 | TestCompletionCallback callback1; |
| 4469 | EXPECT_EQ( |
| 4470 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4471 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4472 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4473 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4474 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4475 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4476 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4477 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4478 | TestGroupId("a"))); |
| 4479 | EXPECT_EQ(0u, |
| 4480 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4481 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4482 | |
| 4483 | ClientSocketHandle handle2; |
| 4484 | TestCompletionCallback callback2; |
| 4485 | EXPECT_EQ( |
| 4486 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4487 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4488 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4489 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4490 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4491 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4492 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4493 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4494 | TestGroupId("a"))); |
| 4495 | EXPECT_EQ(0u, |
| 4496 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4497 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4498 | |
| 4499 | // 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] | 4500 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4501 | &handle1)); |
| 4502 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4503 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4504 | |
| 4505 | // Reprioritizing the second request places it above the first, and it steals |
| 4506 | // the job from the first request. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4507 | pool_->SetPriority(TestGroupId("a"), &handle2, HIGHEST); |
| 4508 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4509 | &handle2)); |
| 4510 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4511 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4512 | } |
| 4513 | |
| 4514 | TEST_F(ClientSocketPoolBaseTest, CancelRequestReassignsJob) { |
| 4515 | CreatePool(kDefaultMaxSockets, 1); |
| 4516 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4517 | |
| 4518 | ClientSocketHandle handle1; |
| 4519 | TestCompletionCallback callback1; |
| 4520 | EXPECT_EQ( |
| 4521 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4522 | handle1.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4523 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4524 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4525 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4526 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4527 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4528 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4529 | TestGroupId("a"))); |
| 4530 | EXPECT_EQ(0u, |
| 4531 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4532 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4533 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4534 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4535 | &handle1)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4536 | |
| 4537 | ClientSocketHandle handle2; |
| 4538 | TestCompletionCallback callback2; |
| 4539 | EXPECT_EQ( |
| 4540 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4541 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4542 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4543 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4544 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4545 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4546 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4547 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4548 | TestGroupId("a"))); |
| 4549 | EXPECT_EQ(0u, |
| 4550 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4551 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4552 | |
| 4553 | // The second request doesn't get a job because we are the limit. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4554 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4555 | &handle1)); |
| 4556 | EXPECT_FALSE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4557 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4558 | |
| 4559 | // The second request should get a job upon cancelling the first request. |
| 4560 | handle1.Reset(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4561 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4562 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4563 | TestGroupId("a"))); |
| 4564 | EXPECT_EQ(0u, |
| 4565 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4566 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4567 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4568 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4569 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4570 | } |
| 4571 | |
| 4572 | TEST_F(ClientSocketPoolBaseTest, JobCompletionReassignsJob) { |
| 4573 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 4574 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 4575 | |
| 4576 | ClientSocketHandle handle1; |
| 4577 | TestCompletionCallback callback1; |
| 4578 | EXPECT_EQ( |
| 4579 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4580 | handle1.Init(TestGroupId("a"), params_, base::nullopt, HIGHEST, |
| 4581 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4582 | callback1.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4583 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4584 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4585 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4586 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4587 | TestGroupId("a"))); |
| 4588 | EXPECT_EQ(0u, |
| 4589 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4590 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4591 | |
| 4592 | ClientSocketHandle handle2; |
| 4593 | TestCompletionCallback callback2; |
| 4594 | EXPECT_EQ( |
| 4595 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4596 | handle2.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4597 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4598 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4599 | pool_.get(), NetLogWithSource())); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4600 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4601 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4602 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4603 | TestGroupId("a"))); |
| 4604 | EXPECT_EQ(0u, |
| 4605 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4606 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4607 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4608 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4609 | &handle1)); |
| 4610 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4611 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4612 | |
| 4613 | // The lower-priority job completes first. The higher-priority request should |
| 4614 | // get the socket, and the lower-priority request should get the remaining |
| 4615 | // job. |
| 4616 | client_socket_factory_.SignalJob(1); |
| 4617 | EXPECT_THAT(callback1.WaitForResult(), IsOk()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4618 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4619 | EXPECT_EQ(0u, pool_->NumNeverAssignedConnectJobsInGroupForTesting( |
| 4620 | TestGroupId("a"))); |
| 4621 | EXPECT_EQ(0u, |
| 4622 | pool_->NumUnassignedConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 4623 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4624 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4625 | EXPECT_TRUE(handle1.socket()); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4626 | EXPECT_TRUE(pool_->RequestInGroupWithHandleHasJobForTesting(TestGroupId("a"), |
| 4627 | &handle2)); |
Lily Chen | ecebf93 | 2018-11-02 17:15:43 | [diff] [blame] | 4628 | } |
| 4629 | |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 4630 | class MockLayeredPool : public HigherLayeredPool { |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4631 | public: |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4632 | MockLayeredPool(TransportClientSocketPool* pool, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4633 | const ClientSocketPool::GroupId& group_id) |
| 4634 | : pool_(pool), group_id_(group_id), can_release_connection_(true) { |
[email protected] | 043b68c8 | 2013-08-22 23:41:52 | [diff] [blame] | 4635 | pool_->AddHigherLayeredPool(this); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4636 | } |
| 4637 | |
Daniel Cheng | 4496d082 | 2018-04-26 21:52:15 | [diff] [blame] | 4638 | ~MockLayeredPool() override { pool_->RemoveHigherLayeredPool(this); } |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4639 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4640 | int RequestSocket(TransportClientSocketPool* pool) { |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4641 | return handle_.Init( |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 4642 | group_id_, ClientSocketPool::SocketParams::CreateForHttpForTesting(), |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4643 | base::nullopt, DEFAULT_PRIORITY, SocketTag(), |
| 4644 | ClientSocketPool::RespectLimits::ENABLED, callback_.callback(), |
| 4645 | ClientSocketPool::ProxyAuthCallback(), pool, NetLogWithSource()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4646 | } |
| 4647 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4648 | int RequestSocketWithoutLimits(TransportClientSocketPool* pool) { |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4649 | return handle_.Init( |
Matt Menke | 870e19ab | 2019-04-23 16:23:03 | [diff] [blame] | 4650 | group_id_, ClientSocketPool::SocketParams::CreateForHttpForTesting(), |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4651 | base::nullopt, MAXIMUM_PRIORITY, SocketTag(), |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4652 | ClientSocketPool::RespectLimits::DISABLED, callback_.callback(), |
| 4653 | ClientSocketPool::ProxyAuthCallback(), pool, NetLogWithSource()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4654 | } |
| 4655 | |
| 4656 | bool ReleaseOneConnection() { |
| 4657 | if (!handle_.is_initialized() || !can_release_connection_) { |
| 4658 | return false; |
| 4659 | } |
| 4660 | handle_.socket()->Disconnect(); |
| 4661 | handle_.Reset(); |
| 4662 | return true; |
| 4663 | } |
| 4664 | |
| 4665 | void set_can_release_connection(bool can_release_connection) { |
| 4666 | can_release_connection_ = can_release_connection; |
| 4667 | } |
| 4668 | |
| 4669 | MOCK_METHOD0(CloseOneIdleConnection, bool()); |
| 4670 | |
| 4671 | private: |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4672 | TransportClientSocketPool* const pool_; |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4673 | ClientSocketHandle handle_; |
| 4674 | TestCompletionCallback callback_; |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4675 | const ClientSocketPool::GroupId group_id_; |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4676 | bool can_release_connection_; |
| 4677 | }; |
| 4678 | |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4679 | // Tests the basic case of closing an idle socket in a higher layered pool when |
| 4680 | // a new request is issued and the lower layer pool is stalled. |
| 4681 | TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketsHeldByLayeredPoolWhenNeeded) { |
| 4682 | CreatePool(1, 1); |
| 4683 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 4684 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4685 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("foo")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4686 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4687 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4688 | .WillOnce(Invoke(&mock_layered_pool, |
| 4689 | &MockLayeredPool::ReleaseOneConnection)); |
| 4690 | ClientSocketHandle handle; |
| 4691 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4692 | EXPECT_EQ( |
| 4693 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4694 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4695 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4696 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4697 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4698 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4699 | } |
| 4700 | |
Matt Menke | 83367864 | 2019-03-05 22:05:51 | [diff] [blame] | 4701 | // Tests the case that trying to close an idle socket in a higher layered pool |
| 4702 | // fails. |
| 4703 | TEST_F(ClientSocketPoolBaseTest, |
| 4704 | CloseIdleSocketsHeldByLayeredPoolWhenNeededFails) { |
| 4705 | CreatePool(1, 1); |
| 4706 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 4707 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4708 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("foo")); |
Matt Menke | 83367864 | 2019-03-05 22:05:51 | [diff] [blame] | 4709 | mock_layered_pool.set_can_release_connection(false); |
| 4710 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
| 4711 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4712 | .WillOnce(Invoke(&mock_layered_pool, |
| 4713 | &MockLayeredPool::ReleaseOneConnection)); |
| 4714 | ClientSocketHandle handle; |
| 4715 | TestCompletionCallback callback; |
| 4716 | EXPECT_EQ( |
| 4717 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4718 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4719 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4720 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4721 | pool_.get(), NetLogWithSource())); |
Matt Menke | 83367864 | 2019-03-05 22:05:51 | [diff] [blame] | 4722 | base::RunLoop().RunUntilIdle(); |
| 4723 | EXPECT_FALSE(callback.have_result()); |
| 4724 | } |
| 4725 | |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4726 | // Same as above, but the idle socket is in the same group as the stalled |
| 4727 | // socket, and closes the only other request in its group when closing requests |
| 4728 | // in higher layered pools. This generally shouldn't happen, but it may be |
| 4729 | // possible if a higher level pool issues a request and the request is |
| 4730 | // subsequently cancelled. Even if it's not possible, best not to crash. |
| 4731 | TEST_F(ClientSocketPoolBaseTest, |
| 4732 | CloseIdleSocketsHeldByLayeredPoolWhenNeededSameGroup) { |
| 4733 | CreatePool(2, 2); |
| 4734 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 4735 | |
| 4736 | // Need a socket in another group for the pool to be stalled (If a group |
| 4737 | // has the maximum number of connections already, it's not stalled). |
| 4738 | ClientSocketHandle handle1; |
| 4739 | TestCompletionCallback callback1; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4740 | EXPECT_EQ(OK, handle1.Init(TestGroupId("group1"), params_, base::nullopt, |
| 4741 | DEFAULT_PRIORITY, SocketTag(), |
| 4742 | ClientSocketPool::RespectLimits::ENABLED, |
| 4743 | callback1.callback(), |
| 4744 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4745 | NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4746 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4747 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("group2")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4748 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4749 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4750 | .WillOnce(Invoke(&mock_layered_pool, |
| 4751 | &MockLayeredPool::ReleaseOneConnection)); |
| 4752 | ClientSocketHandle handle; |
| 4753 | TestCompletionCallback callback2; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4754 | EXPECT_EQ(ERR_IO_PENDING, |
| 4755 | handle.Init( |
| 4756 | TestGroupId("group2"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4757 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4758 | callback2.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4759 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4760 | EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4761 | } |
| 4762 | |
| 4763 | // Tests the case when an idle socket can be closed when a new request is |
| 4764 | // issued, and the new request belongs to a group that was previously stalled. |
| 4765 | TEST_F(ClientSocketPoolBaseTest, |
| 4766 | CloseIdleSocketsHeldByLayeredPoolInSameGroupWhenNeeded) { |
| 4767 | CreatePool(2, 2); |
| 4768 | std::list<TestConnectJob::JobType> job_types; |
| 4769 | job_types.push_back(TestConnectJob::kMockJob); |
| 4770 | job_types.push_back(TestConnectJob::kMockJob); |
| 4771 | job_types.push_back(TestConnectJob::kMockJob); |
| 4772 | job_types.push_back(TestConnectJob::kMockJob); |
| 4773 | connect_job_factory_->set_job_types(&job_types); |
| 4774 | |
| 4775 | ClientSocketHandle handle1; |
| 4776 | TestCompletionCallback callback1; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4777 | EXPECT_EQ(OK, handle1.Init(TestGroupId("group1"), params_, base::nullopt, |
| 4778 | DEFAULT_PRIORITY, SocketTag(), |
| 4779 | ClientSocketPool::RespectLimits::ENABLED, |
| 4780 | callback1.callback(), |
| 4781 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4782 | NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4783 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4784 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("group2")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4785 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4786 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4787 | .WillRepeatedly(Invoke(&mock_layered_pool, |
| 4788 | &MockLayeredPool::ReleaseOneConnection)); |
| 4789 | mock_layered_pool.set_can_release_connection(false); |
| 4790 | |
| 4791 | // The third request is made when the socket pool is in a stalled state. |
| 4792 | ClientSocketHandle handle3; |
| 4793 | TestCompletionCallback callback3; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4794 | EXPECT_EQ(ERR_IO_PENDING, |
| 4795 | handle3.Init( |
| 4796 | TestGroupId("group3"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4797 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4798 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4799 | pool_.get(), NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4800 | |
| 4801 | base::RunLoop().RunUntilIdle(); |
| 4802 | EXPECT_FALSE(callback3.have_result()); |
| 4803 | |
| 4804 | // The fourth request is made when the pool is no longer stalled. The third |
| 4805 | // request should be serviced first, since it was issued first and has the |
| 4806 | // same priority. |
| 4807 | mock_layered_pool.set_can_release_connection(true); |
| 4808 | ClientSocketHandle handle4; |
| 4809 | TestCompletionCallback callback4; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4810 | EXPECT_EQ(ERR_IO_PENDING, |
| 4811 | handle4.Init( |
| 4812 | TestGroupId("group3"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4813 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4814 | callback4.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4815 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4816 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4817 | EXPECT_FALSE(callback4.have_result()); |
| 4818 | |
| 4819 | // Closing a handle should free up another socket slot. |
| 4820 | handle1.Reset(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4821 | EXPECT_THAT(callback4.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4822 | } |
| 4823 | |
| 4824 | // Tests the case when an idle socket can be closed when a new request is |
| 4825 | // issued, and the new request belongs to a group that was previously stalled. |
| 4826 | // |
| 4827 | // The two differences from the above test are that the stalled requests are not |
| 4828 | // in the same group as the layered pool's request, and the the fourth request |
| 4829 | // has a higher priority than the third one, so gets a socket first. |
| 4830 | TEST_F(ClientSocketPoolBaseTest, |
| 4831 | CloseIdleSocketsHeldByLayeredPoolInSameGroupWhenNeeded2) { |
| 4832 | CreatePool(2, 2); |
| 4833 | std::list<TestConnectJob::JobType> job_types; |
| 4834 | job_types.push_back(TestConnectJob::kMockJob); |
| 4835 | job_types.push_back(TestConnectJob::kMockJob); |
| 4836 | job_types.push_back(TestConnectJob::kMockJob); |
| 4837 | job_types.push_back(TestConnectJob::kMockJob); |
| 4838 | connect_job_factory_->set_job_types(&job_types); |
| 4839 | |
| 4840 | ClientSocketHandle handle1; |
| 4841 | TestCompletionCallback callback1; |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4842 | EXPECT_EQ(OK, handle1.Init(TestGroupId("group1"), params_, base::nullopt, |
| 4843 | DEFAULT_PRIORITY, SocketTag(), |
| 4844 | ClientSocketPool::RespectLimits::ENABLED, |
| 4845 | callback1.callback(), |
| 4846 | ClientSocketPool::ProxyAuthCallback(), pool_.get(), |
| 4847 | NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4848 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4849 | MockLayeredPool mock_layered_pool(pool_.get(), TestGroupId("group2")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4850 | EXPECT_THAT(mock_layered_pool.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4851 | EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) |
| 4852 | .WillRepeatedly(Invoke(&mock_layered_pool, |
| 4853 | &MockLayeredPool::ReleaseOneConnection)); |
| 4854 | mock_layered_pool.set_can_release_connection(false); |
| 4855 | |
| 4856 | // The third request is made when the socket pool is in a stalled state. |
| 4857 | ClientSocketHandle handle3; |
| 4858 | TestCompletionCallback callback3; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4859 | EXPECT_EQ( |
| 4860 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4861 | handle3.Init(TestGroupId("group3"), params_, base::nullopt, MEDIUM, |
| 4862 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4863 | callback3.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4864 | pool_.get(), NetLogWithSource())); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4865 | |
| 4866 | base::RunLoop().RunUntilIdle(); |
| 4867 | EXPECT_FALSE(callback3.have_result()); |
| 4868 | |
| 4869 | // The fourth request is made when the pool is no longer stalled. This |
| 4870 | // request has a higher priority than the third request, so is serviced first. |
| 4871 | mock_layered_pool.set_can_release_connection(true); |
| 4872 | ClientSocketHandle handle4; |
| 4873 | TestCompletionCallback callback4; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 4874 | EXPECT_EQ( |
| 4875 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4876 | handle4.Init(TestGroupId("group3"), params_, base::nullopt, HIGHEST, |
| 4877 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4878 | callback4.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4879 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4880 | EXPECT_THAT(callback4.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4881 | EXPECT_FALSE(callback3.have_result()); |
| 4882 | |
| 4883 | // Closing a handle should free up another socket slot. |
| 4884 | handle1.Reset(); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4885 | EXPECT_THAT(callback3.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4886 | } |
| 4887 | |
| 4888 | TEST_F(ClientSocketPoolBaseTest, |
| 4889 | CloseMultipleIdleSocketsHeldByLayeredPoolWhenNeeded) { |
| 4890 | CreatePool(1, 1); |
| 4891 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 4892 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4893 | MockLayeredPool mock_layered_pool1(pool_.get(), TestGroupId("foo")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4894 | EXPECT_THAT(mock_layered_pool1.RequestSocket(pool_.get()), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4895 | EXPECT_CALL(mock_layered_pool1, CloseOneIdleConnection()) |
| 4896 | .WillRepeatedly(Invoke(&mock_layered_pool1, |
| 4897 | &MockLayeredPool::ReleaseOneConnection)); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4898 | MockLayeredPool mock_layered_pool2(pool_.get(), TestGroupId("bar")); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4899 | EXPECT_THAT(mock_layered_pool2.RequestSocketWithoutLimits(pool_.get()), |
| 4900 | IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4901 | EXPECT_CALL(mock_layered_pool2, CloseOneIdleConnection()) |
| 4902 | .WillRepeatedly(Invoke(&mock_layered_pool2, |
| 4903 | &MockLayeredPool::ReleaseOneConnection)); |
| 4904 | ClientSocketHandle handle; |
| 4905 | TestCompletionCallback callback; |
Matt Menke | 28ac03e | 2019-02-25 22:25:50 | [diff] [blame] | 4906 | EXPECT_EQ( |
| 4907 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4908 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4909 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4910 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4911 | pool_.get(), NetLogWithSource())); |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4912 | EXPECT_THAT(callback.WaitForResult(), IsOk()); |
[email protected] | 58e562f | 2013-04-22 17:32:20 | [diff] [blame] | 4913 | } |
| 4914 | |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4915 | // Test that when a socket pool and group are at their limits, a request |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4916 | // with RespectLimits::DISABLED triggers creation of a new socket, and gets the |
| 4917 | // socket instead of a request with the same priority that was issued earlier, |
| 4918 | // but has RespectLimits::ENABLED. |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4919 | TEST_F(ClientSocketPoolBaseTest, IgnoreLimits) { |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4920 | CreatePool(1, 1); |
| 4921 | |
| 4922 | // Issue a request to reach the socket pool limit. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4923 | EXPECT_EQ(OK, StartRequestWithIgnoreLimits( |
| 4924 | TestGroupId("a"), MAXIMUM_PRIORITY, |
| 4925 | ClientSocketPool::RespectLimits::ENABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4926 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4927 | |
| 4928 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 4929 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4930 | EXPECT_EQ(ERR_IO_PENDING, StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4931 | TestGroupId("a"), MAXIMUM_PRIORITY, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4932 | ClientSocketPool::RespectLimits::ENABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4933 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4934 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4935 | // Issue a request that ignores the limits, so a new ConnectJob is |
| 4936 | // created. |
| 4937 | EXPECT_EQ(ERR_IO_PENDING, StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4938 | TestGroupId("a"), MAXIMUM_PRIORITY, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4939 | ClientSocketPool::RespectLimits::DISABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4940 | ASSERT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4941 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4942 | EXPECT_THAT(request(2)->WaitForResult(), IsOk()); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4943 | EXPECT_FALSE(request(1)->have_result()); |
| 4944 | } |
| 4945 | |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 4946 | // Test that when a socket pool and group are at their limits, a ConnectJob |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4947 | // issued for a request with RespectLimits::DISABLED is not cancelled when a |
| 4948 | // request with RespectLimits::ENABLED issued to the same group is cancelled. |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 4949 | TEST_F(ClientSocketPoolBaseTest, IgnoreLimitsCancelOtherJob) { |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 4950 | CreatePool(1, 1); |
| 4951 | |
| 4952 | // Issue a request to reach the socket pool limit. |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4953 | EXPECT_EQ(OK, StartRequestWithIgnoreLimits( |
| 4954 | TestGroupId("a"), MAXIMUM_PRIORITY, |
| 4955 | ClientSocketPool::RespectLimits::ENABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4956 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 4957 | |
| 4958 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 4959 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4960 | EXPECT_EQ(ERR_IO_PENDING, StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4961 | TestGroupId("a"), MAXIMUM_PRIORITY, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4962 | ClientSocketPool::RespectLimits::ENABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4963 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | c55fabd | 2013-11-04 23:26:56 | [diff] [blame] | 4964 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4965 | // Issue a request with RespectLimits::DISABLED, so a new ConnectJob is |
| 4966 | // created. |
| 4967 | EXPECT_EQ(ERR_IO_PENDING, StartRequestWithIgnoreLimits( |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 4968 | TestGroupId("a"), MAXIMUM_PRIORITY, |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4969 | ClientSocketPool::RespectLimits::DISABLED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4970 | ASSERT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4971 | |
mmenke | d3641e1 | 2016-01-28 16:06:15 | [diff] [blame] | 4972 | // Cancel the pending request with RespectLimits::ENABLED. The ConnectJob |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4973 | // should not be cancelled. |
| 4974 | request(1)->handle()->Reset(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4975 | ASSERT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4976 | |
robpercival | 214763f | 2016-07-01 23:27:01 | [diff] [blame] | 4977 | EXPECT_THAT(request(2)->WaitForResult(), IsOk()); |
[email protected] | b021ece6 | 2013-06-11 11:06:33 | [diff] [blame] | 4978 | EXPECT_FALSE(request(1)->have_result()); |
| 4979 | } |
| 4980 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4981 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthNoAuthCallback) { |
| 4982 | CreatePool(1, 1); |
| 4983 | |
| 4984 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 4985 | |
| 4986 | ClientSocketHandle handle; |
| 4987 | TestCompletionCallback callback; |
| 4988 | EXPECT_EQ( |
| 4989 | ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 4990 | handle.Init(TestGroupId("a"), params_, base::nullopt, DEFAULT_PRIORITY, |
| 4991 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 4992 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 4993 | pool_.get(), NetLogWithSource())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4994 | |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 4995 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 4996 | |
| 4997 | EXPECT_THAT(callback.WaitForResult(), IsError(ERR_PROXY_AUTH_REQUESTED)); |
| 4998 | EXPECT_FALSE(handle.is_initialized()); |
| 4999 | EXPECT_FALSE(handle.socket()); |
| 5000 | |
| 5001 | // The group should now be empty, and thus be deleted. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5002 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5003 | } |
| 5004 | |
| 5005 | class TestAuthHelper { |
| 5006 | public: |
| 5007 | TestAuthHelper() = default; |
| 5008 | ~TestAuthHelper() = default; |
| 5009 | |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5010 | void InitHandle( |
Matt Menke | 84d11e56 | 2019-03-27 00:11:19 | [diff] [blame] | 5011 | scoped_refptr<ClientSocketPool::SocketParams> params, |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5012 | TransportClientSocketPool* pool, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5013 | RequestPriority priority = DEFAULT_PRIORITY, |
| 5014 | ClientSocketPool::RespectLimits respect_limits = |
| 5015 | ClientSocketPool::RespectLimits::ENABLED, |
| 5016 | const ClientSocketPool::GroupId& group_id_in = TestGroupId("a")) { |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5017 | EXPECT_EQ(ERR_IO_PENDING, |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5018 | handle_.Init(group_id_in, params, base::nullopt, priority, |
| 5019 | SocketTag(), respect_limits, callback_.callback(), |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5020 | base::BindRepeating(&TestAuthHelper::AuthCallback, |
| 5021 | base::Unretained(this)), |
| 5022 | pool, NetLogWithSource())); |
| 5023 | } |
| 5024 | |
| 5025 | void WaitForAuth() { |
| 5026 | run_loop_ = std::make_unique<base::RunLoop>(); |
| 5027 | run_loop_->Run(); |
| 5028 | run_loop_.reset(); |
| 5029 | } |
| 5030 | |
| 5031 | void WaitForAuthAndRestartSync() { |
| 5032 | restart_sync_ = true; |
| 5033 | WaitForAuth(); |
| 5034 | restart_sync_ = false; |
| 5035 | } |
| 5036 | |
| 5037 | void WaitForAuthAndResetHandleSync() { |
| 5038 | reset_handle_sync_ = true; |
| 5039 | WaitForAuth(); |
| 5040 | reset_handle_sync_ = false; |
| 5041 | } |
| 5042 | |
| 5043 | void RestartWithAuth() { |
| 5044 | DCHECK(restart_with_auth_callback_); |
| 5045 | std::move(restart_with_auth_callback_).Run(); |
| 5046 | } |
| 5047 | |
| 5048 | int WaitForResult() { |
| 5049 | int result = callback_.WaitForResult(); |
| 5050 | // There shouldn't be any callback waiting to be invoked once the request is |
| 5051 | // complete. |
| 5052 | EXPECT_FALSE(restart_with_auth_callback_); |
| 5053 | // The socket should only be initialized on success. |
| 5054 | EXPECT_EQ(result == OK, handle_.is_initialized()); |
| 5055 | EXPECT_EQ(result == OK, handle_.socket() != nullptr); |
| 5056 | return result; |
| 5057 | } |
| 5058 | |
| 5059 | ClientSocketHandle* handle() { return &handle_; } |
| 5060 | int auth_count() const { return auth_count_; } |
| 5061 | int have_result() const { return callback_.have_result(); } |
| 5062 | |
| 5063 | private: |
| 5064 | void AuthCallback(const HttpResponseInfo& response, |
| 5065 | HttpAuthController* auth_controller, |
| 5066 | base::OnceClosure restart_with_auth_callback) { |
| 5067 | EXPECT_FALSE(restart_with_auth_callback_); |
| 5068 | EXPECT_TRUE(restart_with_auth_callback); |
| 5069 | |
| 5070 | // Once there's a result, this method shouldn't be invoked again. |
| 5071 | EXPECT_FALSE(callback_.have_result()); |
| 5072 | |
| 5073 | ++auth_count_; |
| 5074 | run_loop_->Quit(); |
| 5075 | if (restart_sync_) { |
| 5076 | std::move(restart_with_auth_callback).Run(); |
| 5077 | return; |
| 5078 | } |
| 5079 | |
| 5080 | restart_with_auth_callback_ = std::move(restart_with_auth_callback); |
| 5081 | |
| 5082 | if (reset_handle_sync_) { |
| 5083 | handle_.Reset(); |
| 5084 | return; |
| 5085 | } |
| 5086 | } |
| 5087 | |
| 5088 | std::unique_ptr<base::RunLoop> run_loop_; |
| 5089 | base::OnceClosure restart_with_auth_callback_; |
| 5090 | |
| 5091 | bool restart_sync_ = false; |
| 5092 | bool reset_handle_sync_ = false; |
| 5093 | |
| 5094 | ClientSocketHandle handle_; |
| 5095 | int auth_count_ = 0; |
| 5096 | TestCompletionCallback callback_; |
| 5097 | |
| 5098 | DISALLOW_COPY_AND_ASSIGN(TestAuthHelper); |
| 5099 | }; |
| 5100 | |
| 5101 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnce) { |
| 5102 | CreatePool(1, 1); |
| 5103 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5104 | |
| 5105 | TestAuthHelper auth_helper; |
| 5106 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5107 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5108 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5109 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5110 | |
| 5111 | auth_helper.WaitForAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5112 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5113 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5114 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5115 | |
| 5116 | auth_helper.RestartWithAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5117 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5118 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5119 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5120 | |
| 5121 | EXPECT_THAT(auth_helper.WaitForResult(), IsOk()); |
| 5122 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5123 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5124 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5125 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5126 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5127 | } |
| 5128 | |
| 5129 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceSync) { |
| 5130 | CreatePool(1, 1); |
| 5131 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5132 | |
| 5133 | TestAuthHelper auth_helper; |
| 5134 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5135 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5136 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5137 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5138 | |
| 5139 | auth_helper.WaitForAuthAndRestartSync(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5140 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5141 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5142 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5143 | |
| 5144 | EXPECT_THAT(auth_helper.WaitForResult(), IsOk()); |
| 5145 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5146 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5147 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5148 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5149 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5150 | } |
| 5151 | |
| 5152 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceFails) { |
| 5153 | CreatePool(1, 1); |
| 5154 | connect_job_factory_->set_job_type( |
| 5155 | TestConnectJob::kMockAuthChallengeOnceFailingJob); |
| 5156 | |
| 5157 | TestAuthHelper auth_helper; |
| 5158 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5159 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5160 | |
| 5161 | auth_helper.WaitForAuth(); |
| 5162 | auth_helper.RestartWithAuth(); |
| 5163 | EXPECT_THAT(auth_helper.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 5164 | |
| 5165 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5166 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5167 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5168 | } |
| 5169 | |
| 5170 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceSyncFails) { |
| 5171 | CreatePool(1, 1); |
| 5172 | connect_job_factory_->set_job_type( |
| 5173 | TestConnectJob::kMockAuthChallengeOnceFailingJob); |
| 5174 | |
| 5175 | TestAuthHelper auth_helper; |
| 5176 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5177 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5178 | |
| 5179 | auth_helper.WaitForAuthAndRestartSync(); |
| 5180 | EXPECT_THAT(auth_helper.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 5181 | |
| 5182 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5183 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5184 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5185 | } |
| 5186 | |
| 5187 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceDeleteHandle) { |
| 5188 | CreatePool(1, 1); |
| 5189 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5190 | |
| 5191 | TestAuthHelper auth_helper; |
| 5192 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5193 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5194 | |
| 5195 | auth_helper.WaitForAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5196 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5197 | |
| 5198 | auth_helper.handle()->Reset(); |
| 5199 | |
| 5200 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5201 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5202 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5203 | EXPECT_FALSE(auth_helper.handle()->is_initialized()); |
| 5204 | EXPECT_FALSE(auth_helper.handle()->socket()); |
| 5205 | } |
| 5206 | |
| 5207 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceDeleteHandleSync) { |
| 5208 | CreatePool(1, 1); |
| 5209 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5210 | |
| 5211 | TestAuthHelper auth_helper; |
| 5212 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5213 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5214 | |
| 5215 | auth_helper.WaitForAuthAndResetHandleSync(); |
| 5216 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5217 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5218 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5219 | EXPECT_FALSE(auth_helper.handle()->is_initialized()); |
| 5220 | EXPECT_FALSE(auth_helper.handle()->socket()); |
| 5221 | } |
| 5222 | |
| 5223 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthOnceFlushWithError) { |
| 5224 | CreatePool(1, 1); |
| 5225 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5226 | |
| 5227 | TestAuthHelper auth_helper; |
| 5228 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5229 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5230 | |
| 5231 | auth_helper.WaitForAuth(); |
| 5232 | |
| 5233 | pool_->FlushWithError(ERR_FAILED); |
| 5234 | base::RunLoop().RunUntilIdle(); |
| 5235 | |
| 5236 | // When flushing the socket pool, bound sockets should delay returning the |
| 5237 | // error until completion. |
| 5238 | EXPECT_FALSE(auth_helper.have_result()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5239 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5240 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5241 | |
| 5242 | auth_helper.RestartWithAuth(); |
| 5243 | // The callback should be called asynchronously. |
| 5244 | EXPECT_FALSE(auth_helper.have_result()); |
| 5245 | |
| 5246 | EXPECT_THAT(auth_helper.WaitForResult(), IsError(ERR_FAILED)); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5247 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5248 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5249 | } |
| 5250 | |
| 5251 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthTwice) { |
| 5252 | CreatePool(1, 1); |
| 5253 | connect_job_factory_->set_job_type( |
| 5254 | TestConnectJob::kMockAuthChallengeTwiceJob); |
| 5255 | |
| 5256 | TestAuthHelper auth_helper; |
| 5257 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5258 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5259 | EXPECT_EQ(LOAD_STATE_CONNECTING, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5260 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5261 | |
| 5262 | auth_helper.WaitForAuth(); |
| 5263 | auth_helper.RestartWithAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5264 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5265 | EXPECT_EQ(1, auth_helper.auth_count()); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5266 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5267 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5268 | |
| 5269 | auth_helper.WaitForAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5270 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5271 | EXPECT_EQ(2, auth_helper.auth_count()); |
| 5272 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5273 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5274 | |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5275 | auth_helper.RestartWithAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5276 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5277 | EXPECT_EQ(2, auth_helper.auth_count()); |
Matt Menke | 4b69f93 | 2019-03-04 16:20:01 | [diff] [blame] | 5278 | EXPECT_EQ(LOAD_STATE_ESTABLISHING_PROXY_TUNNEL, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5279 | pool_->GetLoadState(TestGroupId("a"), auth_helper.handle())); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5280 | |
| 5281 | EXPECT_THAT(auth_helper.WaitForResult(), IsOk()); |
| 5282 | EXPECT_EQ(2, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5283 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5284 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5285 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5286 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5287 | } |
| 5288 | |
| 5289 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthTwiceFails) { |
| 5290 | CreatePool(1, 1); |
| 5291 | connect_job_factory_->set_job_type( |
| 5292 | TestConnectJob::kMockAuthChallengeTwiceFailingJob); |
| 5293 | |
| 5294 | TestAuthHelper auth_helper; |
| 5295 | auth_helper.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5296 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5297 | |
| 5298 | auth_helper.WaitForAuth(); |
| 5299 | auth_helper.RestartWithAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5300 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5301 | EXPECT_EQ(1, auth_helper.auth_count()); |
| 5302 | |
| 5303 | auth_helper.WaitForAuth(); |
| 5304 | auth_helper.RestartWithAuth(); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5305 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5306 | EXPECT_EQ(2, auth_helper.auth_count()); |
| 5307 | |
| 5308 | EXPECT_THAT(auth_helper.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 5309 | EXPECT_EQ(2, auth_helper.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5310 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5311 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5312 | } |
| 5313 | |
| 5314 | // Makes sure that when a bound request is destroyed, a new ConnectJob is |
| 5315 | // created, if needed. |
| 5316 | TEST_F(ClientSocketPoolBaseTest, |
| 5317 | ProxyAuthCreateNewConnectJobOnDestroyBoundRequest) { |
| 5318 | CreatePool(1 /* max_sockets */, 1 /* max_sockets_per_group */); |
| 5319 | connect_job_factory_->set_job_type( |
| 5320 | TestConnectJob::kMockAuthChallengeOnceFailingJob); |
| 5321 | |
| 5322 | // First request creates a ConnectJob. |
| 5323 | TestAuthHelper auth_helper1; |
| 5324 | auth_helper1.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5325 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5326 | |
| 5327 | // A second request come in, but no new ConnectJob is needed, since the limit |
| 5328 | // has been reached. |
| 5329 | TestAuthHelper auth_helper2; |
| 5330 | auth_helper2.InitHandle(params_, pool_.get()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5331 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5332 | |
| 5333 | // Run until the auth callback for the first request is invoked. |
| 5334 | auth_helper1.WaitForAuth(); |
| 5335 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5336 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5337 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5338 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5339 | |
| 5340 | // Make connect jobs succeed, then cancel the first request, which should |
| 5341 | // destroy the bound ConnectJob, and cause a new ConnectJob to start. |
| 5342 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 5343 | auth_helper1.handle()->Reset(); |
| 5344 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5345 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5346 | |
| 5347 | // The second ConnectJob should succeed. |
| 5348 | EXPECT_THAT(auth_helper2.WaitForResult(), IsOk()); |
| 5349 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5350 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5351 | } |
| 5352 | |
| 5353 | // Makes sure that when a bound request is destroyed, a new ConnectJob is |
| 5354 | // created for another group, if needed. |
| 5355 | TEST_F(ClientSocketPoolBaseTest, |
| 5356 | ProxyAuthCreateNewConnectJobOnDestroyBoundRequestDifferentGroups) { |
| 5357 | CreatePool(1 /* max_sockets */, 1 /* max_sockets_per_group */); |
| 5358 | connect_job_factory_->set_job_type( |
| 5359 | TestConnectJob::kMockAuthChallengeOnceFailingJob); |
| 5360 | |
| 5361 | // First request creates a ConnectJob. |
| 5362 | TestAuthHelper auth_helper1; |
| 5363 | auth_helper1.InitHandle(params_, pool_.get(), DEFAULT_PRIORITY); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5364 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5365 | |
| 5366 | // A second request come in, but no new ConnectJob is needed, since the limit |
| 5367 | // has been reached. |
| 5368 | TestAuthHelper auth_helper2; |
| 5369 | auth_helper2.InitHandle(params_, pool_.get(), DEFAULT_PRIORITY, |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5370 | ClientSocketPool::RespectLimits::ENABLED, |
| 5371 | TestGroupId("b")); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5372 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5373 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5374 | |
| 5375 | // Run until the auth callback for the first request is invoked. |
| 5376 | auth_helper1.WaitForAuth(); |
| 5377 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5378 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5379 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5380 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5381 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
| 5382 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5383 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("b"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5384 | |
| 5385 | // Make connect jobs succeed, then cancel the first request, which should |
| 5386 | // destroy the bound ConnectJob, and cause a new ConnectJob to start for the |
| 5387 | // other group. |
| 5388 | connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 5389 | auth_helper1.handle()->Reset(); |
| 5390 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5391 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 5392 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5393 | |
| 5394 | // The second ConnectJob should succeed. |
| 5395 | EXPECT_THAT(auth_helper2.WaitForResult(), IsOk()); |
| 5396 | EXPECT_EQ(0, auth_helper2.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5397 | EXPECT_FALSE(pool_->HasGroupForTesting(TestGroupId("a"))); |
| 5398 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("b"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5399 | } |
| 5400 | |
| 5401 | // Test that once an auth challenge is bound, that's the request that gets all |
| 5402 | // subsequent calls and the socket itself. |
| 5403 | TEST_F(ClientSocketPoolBaseTest, ProxyAuthStaysBound) { |
| 5404 | CreatePool(1, 1); |
| 5405 | connect_job_factory_->set_job_type( |
| 5406 | TestConnectJob::kMockAuthChallengeTwiceJob); |
| 5407 | |
| 5408 | // First request creates a ConnectJob. |
| 5409 | TestAuthHelper auth_helper1; |
| 5410 | auth_helper1.InitHandle(params_, pool_.get(), LOWEST); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5411 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5412 | |
| 5413 | // A second, higher priority request is made. |
| 5414 | TestAuthHelper auth_helper2; |
| 5415 | auth_helper2.InitHandle(params_, pool_.get(), LOW); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5416 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5417 | |
| 5418 | // Run until the auth callback for the second request is invoked. |
| 5419 | auth_helper2.WaitForAuth(); |
| 5420 | EXPECT_EQ(0, auth_helper1.auth_count()); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5421 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5422 | EXPECT_EQ(0, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | c6b3edf7 | 2019-03-19 17:00:39 | [diff] [blame] | 5423 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5424 | |
| 5425 | // Start a higher priority job. It shouldn't be able to steal |auth_helper2|'s |
| 5426 | // ConnectJob. |
| 5427 | TestAuthHelper auth_helper3; |
| 5428 | auth_helper3.InitHandle(params_, pool_.get(), HIGHEST); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5429 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5430 | |
| 5431 | // Start a higher job that ignores limits, creating a hanging socket. It |
| 5432 | // shouldn't be able to steal |auth_helper2|'s ConnectJob. |
| 5433 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 5434 | TestAuthHelper auth_helper4; |
| 5435 | auth_helper4.InitHandle(params_, pool_.get(), HIGHEST, |
| 5436 | ClientSocketPool::RespectLimits::DISABLED); |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5437 | EXPECT_EQ(2u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5438 | |
| 5439 | // Restart with auth, and |auth_helper2|'s auth method should be invoked |
| 5440 | // again. |
| 5441 | auth_helper2.RestartWithAuth(); |
| 5442 | auth_helper2.WaitForAuth(); |
| 5443 | EXPECT_EQ(0, auth_helper1.auth_count()); |
| 5444 | EXPECT_FALSE(auth_helper1.have_result()); |
| 5445 | EXPECT_EQ(2, auth_helper2.auth_count()); |
| 5446 | EXPECT_FALSE(auth_helper2.have_result()); |
| 5447 | EXPECT_EQ(0, auth_helper3.auth_count()); |
| 5448 | EXPECT_FALSE(auth_helper3.have_result()); |
| 5449 | EXPECT_EQ(0, auth_helper4.auth_count()); |
| 5450 | EXPECT_FALSE(auth_helper4.have_result()); |
| 5451 | |
| 5452 | // Advance auth again, and |auth_helper2| should get the socket. |
| 5453 | auth_helper2.RestartWithAuth(); |
| 5454 | EXPECT_THAT(auth_helper2.WaitForResult(), IsOk()); |
| 5455 | // The hung ConnectJob for the RespectLimits::DISABLED request is still in the |
| 5456 | // socket pool. |
Matt Menke | 9fa17d5 | 2019-03-25 19:12:26 | [diff] [blame] | 5457 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(TestGroupId("a"))); |
| 5458 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(TestGroupId("a"))); |
Matt Menke | b57663b3 | 2019-03-01 17:17:10 | [diff] [blame] | 5459 | EXPECT_EQ(0, auth_helper1.auth_count()); |
| 5460 | EXPECT_FALSE(auth_helper1.have_result()); |
| 5461 | EXPECT_EQ(0, auth_helper3.auth_count()); |
| 5462 | EXPECT_FALSE(auth_helper3.have_result()); |
| 5463 | EXPECT_EQ(0, auth_helper4.auth_count()); |
| 5464 | EXPECT_FALSE(auth_helper4.have_result()); |
| 5465 | |
| 5466 | // If the socket is returned to the socket pool, the RespectLimits::DISABLED |
| 5467 | // socket request should be able to claim it. |
| 5468 | auth_helper2.handle()->Reset(); |
| 5469 | EXPECT_THAT(auth_helper4.WaitForResult(), IsOk()); |
| 5470 | EXPECT_EQ(0, auth_helper1.auth_count()); |
| 5471 | EXPECT_FALSE(auth_helper1.have_result()); |
| 5472 | EXPECT_EQ(0, auth_helper3.auth_count()); |
| 5473 | EXPECT_FALSE(auth_helper3.have_result()); |
| 5474 | EXPECT_EQ(0, auth_helper4.auth_count()); |
| 5475 | } |
| 5476 | |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5477 | TEST_F(ClientSocketPoolBaseTest, RefreshGroupCreatesNewConnectJobs) { |
| 5478 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 5479 | const ClientSocketPool::GroupId kGroupId = TestGroupId("a"); |
| 5480 | |
| 5481 | // First job will be waiting until it gets aborted. |
| 5482 | connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
| 5483 | |
| 5484 | ClientSocketHandle handle; |
| 5485 | TestCompletionCallback callback; |
| 5486 | EXPECT_THAT( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5487 | handle.Init(kGroupId, params_, base::nullopt, DEFAULT_PRIORITY, |
| 5488 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5489 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5490 | pool_.get(), NetLogWithSource()), |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5491 | IsError(ERR_IO_PENDING)); |
| 5492 | |
| 5493 | // Switch connect job types, so creating a new ConnectJob will result in |
| 5494 | // success. |
| 5495 | connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 5496 | |
| 5497 | pool_->RefreshGroupForTesting(kGroupId); |
| 5498 | EXPECT_EQ(OK, callback.WaitForResult()); |
| 5499 | ASSERT_TRUE(handle.socket()); |
| 5500 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5501 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId)); |
| 5502 | EXPECT_EQ(0u, pool_->IdleSocketCountInGroup(kGroupId)); |
| 5503 | EXPECT_EQ(0u, pool_->NumConnectJobsInGroupForTesting(kGroupId)); |
| 5504 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId)); |
| 5505 | } |
| 5506 | |
| 5507 | TEST_F(ClientSocketPoolBaseTest, RefreshGroupClosesIdleConnectJobs) { |
| 5508 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 5509 | const ClientSocketPool::GroupId kGroupId = TestGroupId("a"); |
| 5510 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5511 | pool_->RequestSockets(kGroupId, params_, base::nullopt, 2, |
| 5512 | NetLogWithSource()); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5513 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId)); |
| 5514 | EXPECT_EQ(2, pool_->IdleSocketCount()); |
| 5515 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(kGroupId)); |
| 5516 | |
| 5517 | pool_->RefreshGroupForTesting(kGroupId); |
| 5518 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5519 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupId)); |
| 5520 | } |
| 5521 | |
| 5522 | TEST_F(ClientSocketPoolBaseTest, |
| 5523 | RefreshGroupDoesNotCloseIdleConnectJobsInOtherGroup) { |
| 5524 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 5525 | const ClientSocketPool::GroupId kGroupId = TestGroupId("a"); |
| 5526 | const ClientSocketPool::GroupId kOtherGroupId = TestGroupId("b"); |
| 5527 | |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5528 | pool_->RequestSockets(kOtherGroupId, params_, base::nullopt, 2, |
| 5529 | NetLogWithSource()); |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5530 | ASSERT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 5531 | EXPECT_EQ(2, pool_->IdleSocketCount()); |
| 5532 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(kOtherGroupId)); |
| 5533 | |
| 5534 | pool_->RefreshGroupForTesting(kGroupId); |
| 5535 | ASSERT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 5536 | EXPECT_EQ(2, pool_->IdleSocketCount()); |
| 5537 | EXPECT_EQ(2u, pool_->IdleSocketCountInGroup(kOtherGroupId)); |
| 5538 | } |
| 5539 | |
| 5540 | TEST_F(ClientSocketPoolBaseTest, RefreshGroupPreventsSocketReuse) { |
| 5541 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 5542 | const ClientSocketPool::GroupId kGroupId = TestGroupId("a"); |
| 5543 | |
| 5544 | ClientSocketHandle handle; |
| 5545 | TestCompletionCallback callback; |
| 5546 | EXPECT_THAT( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5547 | handle.Init(kGroupId, params_, base::nullopt, DEFAULT_PRIORITY, |
| 5548 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5549 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5550 | pool_.get(), NetLogWithSource()), |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5551 | IsOk()); |
| 5552 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId)); |
| 5553 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kGroupId)); |
| 5554 | |
| 5555 | pool_->RefreshGroupForTesting(kGroupId); |
| 5556 | |
| 5557 | handle.Reset(); |
| 5558 | EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 5559 | EXPECT_FALSE(pool_->HasGroupForTesting(kGroupId)); |
| 5560 | } |
| 5561 | |
| 5562 | TEST_F(ClientSocketPoolBaseTest, |
| 5563 | RefreshGroupDoesNotPreventSocketReuseInOtherGroup) { |
| 5564 | CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 5565 | const ClientSocketPool::GroupId kGroupId = TestGroupId("a"); |
| 5566 | const ClientSocketPool::GroupId kOtherGroupId = TestGroupId("b"); |
| 5567 | |
| 5568 | ClientSocketHandle handle; |
| 5569 | TestCompletionCallback callback; |
| 5570 | EXPECT_THAT( |
Matt Menke | f09e64c | 2019-04-23 22:16:28 | [diff] [blame] | 5571 | handle.Init(kOtherGroupId, params_, base::nullopt, DEFAULT_PRIORITY, |
| 5572 | SocketTag(), ClientSocketPool::RespectLimits::ENABLED, |
| 5573 | callback.callback(), ClientSocketPool::ProxyAuthCallback(), |
| 5574 | pool_.get(), NetLogWithSource()), |
Matt Menke | bf3c767d | 2019-04-15 23:28:24 | [diff] [blame] | 5575 | IsOk()); |
| 5576 | ASSERT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 5577 | EXPECT_EQ(1, pool_->NumActiveSocketsInGroupForTesting(kOtherGroupId)); |
| 5578 | |
| 5579 | pool_->RefreshGroupForTesting(kGroupId); |
| 5580 | |
| 5581 | handle.Reset(); |
| 5582 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 5583 | ASSERT_TRUE(pool_->HasGroupForTesting(kOtherGroupId)); |
| 5584 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(kOtherGroupId)); |
| 5585 | } |
| 5586 | |
| 5587 | TEST_F(ClientSocketPoolBaseTest, RefreshGroupReplacesBoundConnectJobOnConnect) { |
| 5588 | CreatePool(1, 1); |
| 5589 | const ClientSocketPool::GroupId kGroupId = TestGroupId("a"); |
| 5590 | connect_job_factory_->set_job_type(TestConnectJob::kMockAuthChallengeOnceJob); |
| 5591 | |
| 5592 | TestAuthHelper auth_helper; |
| 5593 | auth_helper.InitHandle(params_, pool_.get(), DEFAULT_PRIORITY, |
| 5594 | ClientSocketPool::RespectLimits::ENABLED, kGroupId); |
| 5595 | EXPECT_EQ(1u, pool_->NumConnectJobsInGroupForTesting(kGroupId)); |
| 5596 | |
| 5597 | auth_helper.WaitForAuth(); |
| 5598 | |
| 5599 | // This should update the generation, but not cancel the old ConnectJob - it's |
| 5600 | // not safe to do anything while waiting on the original ConnectJob. |
| 5601 | pool_->RefreshGroupForTesting(kGroupId); |
| 5602 | |
| 5603 | // Providing auth credentials and restarting the request with them will cause |
| 5604 | // the ConnectJob to complete successfully, but the result will be discarded |
| 5605 | // because of the generation mismatch. |
| 5606 | auth_helper.RestartWithAuth(); |
| 5607 | |
| 5608 | // Despite using ConnectJobs that simulate a single challenge, a second |
| 5609 | // challenge will be seen, due to using a new ConnectJob. |
| 5610 | auth_helper.WaitForAuth(); |
| 5611 | auth_helper.RestartWithAuth(); |
| 5612 | |
| 5613 | EXPECT_THAT(auth_helper.WaitForResult(), IsOk()); |
| 5614 | EXPECT_TRUE(auth_helper.handle()->socket()); |
| 5615 | EXPECT_EQ(2, auth_helper.auth_count()); |
| 5616 | |
| 5617 | // When released, the socket will be returned to the socket pool, and |
| 5618 | // available for reuse. |
| 5619 | auth_helper.handle()->Reset(); |
| 5620 | EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 5621 | ASSERT_TRUE(pool_->HasGroupForTesting(kGroupId)); |
| 5622 | EXPECT_EQ(1u, pool_->IdleSocketCountInGroup(kGroupId)); |
| 5623 | } |
| 5624 | |
[email protected] | f6d1d6eb | 2009-06-24 20:16:09 | [diff] [blame] | 5625 | } // namespace |
| 5626 | |
| 5627 | } // namespace net |