QUIC - enable persisting of QUICServerInfo (server config) to disk
[chromium-blink-merge.git] / net / socket / transport_client_socket_pool_unittest.cc
blob1cc015a1fa1b4842f5fcc1d1b6568353e24e057f
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/socket/transport_client_socket_pool.h"
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/callback.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/threading/platform_thread.h"
12 #include "net/base/capturing_net_log.h"
13 #include "net/base/ip_endpoint.h"
14 #include "net/base/load_timing_info.h"
15 #include "net/base/load_timing_info_test_util.h"
16 #include "net/base/net_errors.h"
17 #include "net/base/net_util.h"
18 #include "net/base/test_completion_callback.h"
19 #include "net/dns/mock_host_resolver.h"
20 #include "net/socket/client_socket_handle.h"
21 #include "net/socket/client_socket_pool_histograms.h"
22 #include "net/socket/socket_test_util.h"
23 #include "net/socket/stream_socket.h"
24 #include "net/socket/transport_client_socket_pool_test_util.h"
25 #include "testing/gtest/include/gtest/gtest.h"
27 namespace net {
29 using internal::ClientSocketPoolBaseHelper;
31 namespace {
33 const int kMaxSockets = 32;
34 const int kMaxSocketsPerGroup = 6;
35 const RequestPriority kDefaultPriority = LOW;
37 class TransportClientSocketPoolTest : public testing::Test {
38 protected:
39 TransportClientSocketPoolTest()
40 : connect_backup_jobs_enabled_(
41 ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(true)),
42 params_(
43 new TransportSocketParams(HostPortPair("www.google.com", 80),
44 false, false,
45 OnHostResolutionCallback())),
46 histograms_(new ClientSocketPoolHistograms("TCPUnitTest")),
47 host_resolver_(new MockHostResolver),
48 client_socket_factory_(&net_log_),
49 pool_(kMaxSockets,
50 kMaxSocketsPerGroup,
51 histograms_.get(),
52 host_resolver_.get(),
53 &client_socket_factory_,
54 NULL) {
57 virtual ~TransportClientSocketPoolTest() {
58 internal::ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(
59 connect_backup_jobs_enabled_);
62 int StartRequest(const std::string& group_name, RequestPriority priority) {
63 scoped_refptr<TransportSocketParams> params(new TransportSocketParams(
64 HostPortPair("www.google.com", 80), false, false,
65 OnHostResolutionCallback()));
66 return test_base_.StartRequestUsingPool(
67 &pool_, group_name, priority, params);
70 int GetOrderOfRequest(size_t index) {
71 return test_base_.GetOrderOfRequest(index);
74 bool ReleaseOneConnection(ClientSocketPoolTest::KeepAlive keep_alive) {
75 return test_base_.ReleaseOneConnection(keep_alive);
78 void ReleaseAllConnections(ClientSocketPoolTest::KeepAlive keep_alive) {
79 test_base_.ReleaseAllConnections(keep_alive);
82 ScopedVector<TestSocketRequest>* requests() { return test_base_.requests(); }
83 size_t completion_count() const { return test_base_.completion_count(); }
85 bool connect_backup_jobs_enabled_;
86 CapturingNetLog net_log_;
87 scoped_refptr<TransportSocketParams> params_;
88 scoped_ptr<ClientSocketPoolHistograms> histograms_;
89 scoped_ptr<MockHostResolver> host_resolver_;
90 MockTransportClientSocketFactory client_socket_factory_;
91 TransportClientSocketPool pool_;
92 ClientSocketPoolTest test_base_;
94 private:
95 DISALLOW_COPY_AND_ASSIGN(TransportClientSocketPoolTest);
98 TEST(TransportConnectJobTest, MakeAddrListStartWithIPv4) {
99 IPAddressNumber ip_number;
100 ASSERT_TRUE(ParseIPLiteralToNumber("192.168.1.1", &ip_number));
101 IPEndPoint addrlist_v4_1(ip_number, 80);
102 ASSERT_TRUE(ParseIPLiteralToNumber("192.168.1.2", &ip_number));
103 IPEndPoint addrlist_v4_2(ip_number, 80);
104 ASSERT_TRUE(ParseIPLiteralToNumber("2001:4860:b006::64", &ip_number));
105 IPEndPoint addrlist_v6_1(ip_number, 80);
106 ASSERT_TRUE(ParseIPLiteralToNumber("2001:4860:b006::66", &ip_number));
107 IPEndPoint addrlist_v6_2(ip_number, 80);
109 AddressList addrlist;
111 // Test 1: IPv4 only. Expect no change.
112 addrlist.clear();
113 addrlist.push_back(addrlist_v4_1);
114 addrlist.push_back(addrlist_v4_2);
115 TransportConnectJob::MakeAddressListStartWithIPv4(&addrlist);
116 ASSERT_EQ(2u, addrlist.size());
117 EXPECT_EQ(ADDRESS_FAMILY_IPV4, addrlist[0].GetFamily());
118 EXPECT_EQ(ADDRESS_FAMILY_IPV4, addrlist[1].GetFamily());
120 // Test 2: IPv6 only. Expect no change.
121 addrlist.clear();
122 addrlist.push_back(addrlist_v6_1);
123 addrlist.push_back(addrlist_v6_2);
124 TransportConnectJob::MakeAddressListStartWithIPv4(&addrlist);
125 ASSERT_EQ(2u, addrlist.size());
126 EXPECT_EQ(ADDRESS_FAMILY_IPV6, addrlist[0].GetFamily());
127 EXPECT_EQ(ADDRESS_FAMILY_IPV6, addrlist[1].GetFamily());
129 // Test 3: IPv4 then IPv6. Expect no change.
130 addrlist.clear();
131 addrlist.push_back(addrlist_v4_1);
132 addrlist.push_back(addrlist_v4_2);
133 addrlist.push_back(addrlist_v6_1);
134 addrlist.push_back(addrlist_v6_2);
135 TransportConnectJob::MakeAddressListStartWithIPv4(&addrlist);
136 ASSERT_EQ(4u, addrlist.size());
137 EXPECT_EQ(ADDRESS_FAMILY_IPV4, addrlist[0].GetFamily());
138 EXPECT_EQ(ADDRESS_FAMILY_IPV4, addrlist[1].GetFamily());
139 EXPECT_EQ(ADDRESS_FAMILY_IPV6, addrlist[2].GetFamily());
140 EXPECT_EQ(ADDRESS_FAMILY_IPV6, addrlist[3].GetFamily());
142 // Test 4: IPv6, IPv4, IPv6, IPv4. Expect first IPv6 moved to the end.
143 addrlist.clear();
144 addrlist.push_back(addrlist_v6_1);
145 addrlist.push_back(addrlist_v4_1);
146 addrlist.push_back(addrlist_v6_2);
147 addrlist.push_back(addrlist_v4_2);
148 TransportConnectJob::MakeAddressListStartWithIPv4(&addrlist);
149 ASSERT_EQ(4u, addrlist.size());
150 EXPECT_EQ(ADDRESS_FAMILY_IPV4, addrlist[0].GetFamily());
151 EXPECT_EQ(ADDRESS_FAMILY_IPV6, addrlist[1].GetFamily());
152 EXPECT_EQ(ADDRESS_FAMILY_IPV4, addrlist[2].GetFamily());
153 EXPECT_EQ(ADDRESS_FAMILY_IPV6, addrlist[3].GetFamily());
155 // Test 5: IPv6, IPv6, IPv4, IPv4. Expect first two IPv6's moved to the end.
156 addrlist.clear();
157 addrlist.push_back(addrlist_v6_1);
158 addrlist.push_back(addrlist_v6_2);
159 addrlist.push_back(addrlist_v4_1);
160 addrlist.push_back(addrlist_v4_2);
161 TransportConnectJob::MakeAddressListStartWithIPv4(&addrlist);
162 ASSERT_EQ(4u, addrlist.size());
163 EXPECT_EQ(ADDRESS_FAMILY_IPV4, addrlist[0].GetFamily());
164 EXPECT_EQ(ADDRESS_FAMILY_IPV4, addrlist[1].GetFamily());
165 EXPECT_EQ(ADDRESS_FAMILY_IPV6, addrlist[2].GetFamily());
166 EXPECT_EQ(ADDRESS_FAMILY_IPV6, addrlist[3].GetFamily());
169 TEST_F(TransportClientSocketPoolTest, Basic) {
170 TestCompletionCallback callback;
171 ClientSocketHandle handle;
172 int rv = handle.Init("a", params_, LOW, callback.callback(), &pool_,
173 BoundNetLog());
174 EXPECT_EQ(ERR_IO_PENDING, rv);
175 EXPECT_FALSE(handle.is_initialized());
176 EXPECT_FALSE(handle.socket());
178 EXPECT_EQ(OK, callback.WaitForResult());
179 EXPECT_TRUE(handle.is_initialized());
180 EXPECT_TRUE(handle.socket());
181 TestLoadTimingInfoConnectedNotReused(handle);
184 // Make sure that TransportConnectJob passes on its priority to its
185 // HostResolver request on Init.
186 TEST_F(TransportClientSocketPoolTest, SetResolvePriorityOnInit) {
187 for (int i = MINIMUM_PRIORITY; i <= MAXIMUM_PRIORITY; ++i) {
188 RequestPriority priority = static_cast<RequestPriority>(i);
189 TestCompletionCallback callback;
190 ClientSocketHandle handle;
191 EXPECT_EQ(ERR_IO_PENDING,
192 handle.Init("a", params_, priority, callback.callback(), &pool_,
193 BoundNetLog()));
194 EXPECT_EQ(priority, host_resolver_->last_request_priority());
198 TEST_F(TransportClientSocketPoolTest, InitHostResolutionFailure) {
199 host_resolver_->rules()->AddSimulatedFailure("unresolvable.host.name");
200 TestCompletionCallback callback;
201 ClientSocketHandle handle;
202 HostPortPair host_port_pair("unresolvable.host.name", 80);
203 scoped_refptr<TransportSocketParams> dest(new TransportSocketParams(
204 host_port_pair, false, false,
205 OnHostResolutionCallback()));
206 EXPECT_EQ(ERR_IO_PENDING,
207 handle.Init("a", dest, kDefaultPriority, callback.callback(),
208 &pool_, BoundNetLog()));
209 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, callback.WaitForResult());
212 TEST_F(TransportClientSocketPoolTest, InitConnectionFailure) {
213 client_socket_factory_.set_default_client_socket_type(
214 MockTransportClientSocketFactory::MOCK_FAILING_CLIENT_SOCKET);
215 TestCompletionCallback callback;
216 ClientSocketHandle handle;
217 EXPECT_EQ(ERR_IO_PENDING,
218 handle.Init("a", params_, kDefaultPriority, callback.callback(),
219 &pool_, BoundNetLog()));
220 EXPECT_EQ(ERR_CONNECTION_FAILED, callback.WaitForResult());
222 // Make the host resolutions complete synchronously this time.
223 host_resolver_->set_synchronous_mode(true);
224 EXPECT_EQ(ERR_CONNECTION_FAILED,
225 handle.Init("a", params_, kDefaultPriority, callback.callback(),
226 &pool_, BoundNetLog()));
229 TEST_F(TransportClientSocketPoolTest, PendingRequests) {
230 // First request finishes asynchronously.
231 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
232 EXPECT_EQ(OK, (*requests())[0]->WaitForResult());
234 // Make all subsequent host resolutions complete synchronously.
235 host_resolver_->set_synchronous_mode(true);
237 // Rest of them finish synchronously, until we reach the per-group limit.
238 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority));
239 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority));
240 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority));
241 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority));
242 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority));
244 // The rest are pending since we've used all active sockets.
245 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", HIGHEST));
246 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOWEST));
247 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOWEST));
248 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", MEDIUM));
249 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOW));
250 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", HIGHEST));
251 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOWEST));
252 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", MEDIUM));
253 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", MEDIUM));
254 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", HIGHEST));
256 ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE);
258 EXPECT_EQ(kMaxSocketsPerGroup, client_socket_factory_.allocation_count());
260 // One initial asynchronous request and then 10 pending requests.
261 EXPECT_EQ(11U, completion_count());
263 // First part of requests, all with the same priority, finishes in FIFO order.
264 EXPECT_EQ(1, GetOrderOfRequest(1));
265 EXPECT_EQ(2, GetOrderOfRequest(2));
266 EXPECT_EQ(3, GetOrderOfRequest(3));
267 EXPECT_EQ(4, GetOrderOfRequest(4));
268 EXPECT_EQ(5, GetOrderOfRequest(5));
269 EXPECT_EQ(6, GetOrderOfRequest(6));
271 // Make sure that rest of the requests complete in the order of priority.
272 EXPECT_EQ(7, GetOrderOfRequest(7));
273 EXPECT_EQ(14, GetOrderOfRequest(8));
274 EXPECT_EQ(15, GetOrderOfRequest(9));
275 EXPECT_EQ(10, GetOrderOfRequest(10));
276 EXPECT_EQ(13, GetOrderOfRequest(11));
277 EXPECT_EQ(8, GetOrderOfRequest(12));
278 EXPECT_EQ(16, GetOrderOfRequest(13));
279 EXPECT_EQ(11, GetOrderOfRequest(14));
280 EXPECT_EQ(12, GetOrderOfRequest(15));
281 EXPECT_EQ(9, GetOrderOfRequest(16));
283 // Make sure we test order of all requests made.
284 EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(17));
287 TEST_F(TransportClientSocketPoolTest, PendingRequests_NoKeepAlive) {
288 // First request finishes asynchronously.
289 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
290 EXPECT_EQ(OK, (*requests())[0]->WaitForResult());
292 // Make all subsequent host resolutions complete synchronously.
293 host_resolver_->set_synchronous_mode(true);
295 // Rest of them finish synchronously, until we reach the per-group limit.
296 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority));
297 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority));
298 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority));
299 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority));
300 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority));
302 // The rest are pending since we've used all active sockets.
303 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
304 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
305 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
306 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
307 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
309 ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE);
311 // The pending requests should finish successfully.
312 EXPECT_EQ(OK, (*requests())[6]->WaitForResult());
313 EXPECT_EQ(OK, (*requests())[7]->WaitForResult());
314 EXPECT_EQ(OK, (*requests())[8]->WaitForResult());
315 EXPECT_EQ(OK, (*requests())[9]->WaitForResult());
316 EXPECT_EQ(OK, (*requests())[10]->WaitForResult());
318 EXPECT_EQ(static_cast<int>(requests()->size()),
319 client_socket_factory_.allocation_count());
321 // First asynchronous request, and then last 5 pending requests.
322 EXPECT_EQ(6U, completion_count());
325 // This test will start up a RequestSocket() and then immediately Cancel() it.
326 // The pending host resolution will eventually complete, and destroy the
327 // ClientSocketPool which will crash if the group was not cleared properly.
328 TEST_F(TransportClientSocketPoolTest, CancelRequestClearGroup) {
329 TestCompletionCallback callback;
330 ClientSocketHandle handle;
331 EXPECT_EQ(ERR_IO_PENDING,
332 handle.Init("a", params_, kDefaultPriority, callback.callback(),
333 &pool_, BoundNetLog()));
334 handle.Reset();
337 TEST_F(TransportClientSocketPoolTest, TwoRequestsCancelOne) {
338 ClientSocketHandle handle;
339 TestCompletionCallback callback;
340 ClientSocketHandle handle2;
341 TestCompletionCallback callback2;
343 EXPECT_EQ(ERR_IO_PENDING,
344 handle.Init("a", params_, kDefaultPriority, callback.callback(),
345 &pool_, BoundNetLog()));
346 EXPECT_EQ(ERR_IO_PENDING,
347 handle2.Init("a", params_, kDefaultPriority, callback2.callback(),
348 &pool_, BoundNetLog()));
350 handle.Reset();
352 EXPECT_EQ(OK, callback2.WaitForResult());
353 handle2.Reset();
356 TEST_F(TransportClientSocketPoolTest, ConnectCancelConnect) {
357 client_socket_factory_.set_default_client_socket_type(
358 MockTransportClientSocketFactory::MOCK_PENDING_CLIENT_SOCKET);
359 ClientSocketHandle handle;
360 TestCompletionCallback callback;
361 EXPECT_EQ(ERR_IO_PENDING,
362 handle.Init("a", params_, kDefaultPriority, callback.callback(),
363 &pool_, BoundNetLog()));
365 handle.Reset();
367 TestCompletionCallback callback2;
368 EXPECT_EQ(ERR_IO_PENDING,
369 handle.Init("a", params_, kDefaultPriority, callback2.callback(),
370 &pool_, BoundNetLog()));
372 host_resolver_->set_synchronous_mode(true);
373 // At this point, handle has two ConnectingSockets out for it. Due to the
374 // setting the mock resolver into synchronous mode, the host resolution for
375 // both will return in the same loop of the MessageLoop. The client socket
376 // is a pending socket, so the Connect() will asynchronously complete on the
377 // next loop of the MessageLoop. That means that the first
378 // ConnectingSocket will enter OnIOComplete, and then the second one will.
379 // If the first one is not cancelled, it will advance the load state, and
380 // then the second one will crash.
382 EXPECT_EQ(OK, callback2.WaitForResult());
383 EXPECT_FALSE(callback.have_result());
385 handle.Reset();
388 TEST_F(TransportClientSocketPoolTest, CancelRequest) {
389 // First request finishes asynchronously.
390 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
391 EXPECT_EQ(OK, (*requests())[0]->WaitForResult());
393 // Make all subsequent host resolutions complete synchronously.
394 host_resolver_->set_synchronous_mode(true);
396 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority));
397 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority));
398 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority));
399 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority));
400 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority));
402 // Reached per-group limit, queue up requests.
403 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOWEST));
404 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", HIGHEST));
405 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", HIGHEST));
406 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", MEDIUM));
407 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", MEDIUM));
408 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOW));
409 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", HIGHEST));
410 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOW));
411 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOW));
412 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOWEST));
414 // Cancel a request.
415 size_t index_to_cancel = kMaxSocketsPerGroup + 2;
416 EXPECT_FALSE((*requests())[index_to_cancel]->handle()->is_initialized());
417 (*requests())[index_to_cancel]->handle()->Reset();
419 ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE);
421 EXPECT_EQ(kMaxSocketsPerGroup,
422 client_socket_factory_.allocation_count());
423 EXPECT_EQ(requests()->size() - kMaxSocketsPerGroup, completion_count());
425 EXPECT_EQ(1, GetOrderOfRequest(1));
426 EXPECT_EQ(2, GetOrderOfRequest(2));
427 EXPECT_EQ(3, GetOrderOfRequest(3));
428 EXPECT_EQ(4, GetOrderOfRequest(4));
429 EXPECT_EQ(5, GetOrderOfRequest(5));
430 EXPECT_EQ(6, GetOrderOfRequest(6));
431 EXPECT_EQ(14, GetOrderOfRequest(7));
432 EXPECT_EQ(7, GetOrderOfRequest(8));
433 EXPECT_EQ(ClientSocketPoolTest::kRequestNotFound,
434 GetOrderOfRequest(9)); // Canceled request.
435 EXPECT_EQ(9, GetOrderOfRequest(10));
436 EXPECT_EQ(10, GetOrderOfRequest(11));
437 EXPECT_EQ(11, GetOrderOfRequest(12));
438 EXPECT_EQ(8, GetOrderOfRequest(13));
439 EXPECT_EQ(12, GetOrderOfRequest(14));
440 EXPECT_EQ(13, GetOrderOfRequest(15));
441 EXPECT_EQ(15, GetOrderOfRequest(16));
443 // Make sure we test order of all requests made.
444 EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(17));
447 class RequestSocketCallback : public TestCompletionCallbackBase {
448 public:
449 RequestSocketCallback(ClientSocketHandle* handle,
450 TransportClientSocketPool* pool)
451 : handle_(handle),
452 pool_(pool),
453 within_callback_(false),
454 callback_(base::Bind(&RequestSocketCallback::OnComplete,
455 base::Unretained(this))) {
458 virtual ~RequestSocketCallback() {}
460 const CompletionCallback& callback() const { return callback_; }
462 private:
463 void OnComplete(int result) {
464 SetResult(result);
465 ASSERT_EQ(OK, result);
467 if (!within_callback_) {
468 // Don't allow reuse of the socket. Disconnect it and then release it and
469 // run through the MessageLoop once to get it completely released.
470 handle_->socket()->Disconnect();
471 handle_->Reset();
473 base::MessageLoop::ScopedNestableTaskAllower allow(
474 base::MessageLoop::current());
475 base::MessageLoop::current()->RunUntilIdle();
477 within_callback_ = true;
478 scoped_refptr<TransportSocketParams> dest(new TransportSocketParams(
479 HostPortPair("www.google.com", 80), false, false,
480 OnHostResolutionCallback()));
481 int rv = handle_->Init("a", dest, LOWEST, callback(), pool_,
482 BoundNetLog());
483 EXPECT_EQ(OK, rv);
487 ClientSocketHandle* const handle_;
488 TransportClientSocketPool* const pool_;
489 bool within_callback_;
490 CompletionCallback callback_;
492 DISALLOW_COPY_AND_ASSIGN(RequestSocketCallback);
495 TEST_F(TransportClientSocketPoolTest, RequestTwice) {
496 ClientSocketHandle handle;
497 RequestSocketCallback callback(&handle, &pool_);
498 scoped_refptr<TransportSocketParams> dest(new TransportSocketParams(
499 HostPortPair("www.google.com", 80), false, false,
500 OnHostResolutionCallback()));
501 int rv = handle.Init("a", dest, LOWEST, callback.callback(), &pool_,
502 BoundNetLog());
503 ASSERT_EQ(ERR_IO_PENDING, rv);
505 // The callback is going to request "www.google.com". We want it to complete
506 // synchronously this time.
507 host_resolver_->set_synchronous_mode(true);
509 EXPECT_EQ(OK, callback.WaitForResult());
511 handle.Reset();
514 // Make sure that pending requests get serviced after active requests get
515 // cancelled.
516 TEST_F(TransportClientSocketPoolTest, CancelActiveRequestWithPendingRequests) {
517 client_socket_factory_.set_default_client_socket_type(
518 MockTransportClientSocketFactory::MOCK_PENDING_CLIENT_SOCKET);
520 // Queue up all the requests
521 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
522 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
523 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
524 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
525 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
526 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
527 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
528 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
529 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
531 // Now, kMaxSocketsPerGroup requests should be active. Let's cancel them.
532 ASSERT_LE(kMaxSocketsPerGroup, static_cast<int>(requests()->size()));
533 for (int i = 0; i < kMaxSocketsPerGroup; i++)
534 (*requests())[i]->handle()->Reset();
536 // Let's wait for the rest to complete now.
537 for (size_t i = kMaxSocketsPerGroup; i < requests()->size(); ++i) {
538 EXPECT_EQ(OK, (*requests())[i]->WaitForResult());
539 (*requests())[i]->handle()->Reset();
542 EXPECT_EQ(requests()->size() - kMaxSocketsPerGroup, completion_count());
545 // Make sure that pending requests get serviced after active requests fail.
546 TEST_F(TransportClientSocketPoolTest, FailingActiveRequestWithPendingRequests) {
547 client_socket_factory_.set_default_client_socket_type(
548 MockTransportClientSocketFactory::MOCK_PENDING_FAILING_CLIENT_SOCKET);
550 const int kNumRequests = 2 * kMaxSocketsPerGroup + 1;
551 ASSERT_LE(kNumRequests, kMaxSockets); // Otherwise the test will hang.
553 // Queue up all the requests
554 for (int i = 0; i < kNumRequests; i++)
555 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
557 for (int i = 0; i < kNumRequests; i++)
558 EXPECT_EQ(ERR_CONNECTION_FAILED, (*requests())[i]->WaitForResult());
561 TEST_F(TransportClientSocketPoolTest, IdleSocketLoadTiming) {
562 TestCompletionCallback callback;
563 ClientSocketHandle handle;
564 int rv = handle.Init("a", params_, LOW, callback.callback(), &pool_,
565 BoundNetLog());
566 EXPECT_EQ(ERR_IO_PENDING, rv);
567 EXPECT_FALSE(handle.is_initialized());
568 EXPECT_FALSE(handle.socket());
570 EXPECT_EQ(OK, callback.WaitForResult());
571 EXPECT_TRUE(handle.is_initialized());
572 EXPECT_TRUE(handle.socket());
573 TestLoadTimingInfoConnectedNotReused(handle);
575 handle.Reset();
576 // Need to run all pending to release the socket back to the pool.
577 base::MessageLoop::current()->RunUntilIdle();
579 // Now we should have 1 idle socket.
580 EXPECT_EQ(1, pool_.IdleSocketCount());
582 rv = handle.Init("a", params_, LOW, callback.callback(), &pool_,
583 BoundNetLog());
584 EXPECT_EQ(OK, rv);
585 EXPECT_EQ(0, pool_.IdleSocketCount());
586 TestLoadTimingInfoConnectedReused(handle);
589 TEST_F(TransportClientSocketPoolTest, ResetIdleSocketsOnIPAddressChange) {
590 TestCompletionCallback callback;
591 ClientSocketHandle handle;
592 int rv = handle.Init("a", params_, LOW, callback.callback(), &pool_,
593 BoundNetLog());
594 EXPECT_EQ(ERR_IO_PENDING, rv);
595 EXPECT_FALSE(handle.is_initialized());
596 EXPECT_FALSE(handle.socket());
598 EXPECT_EQ(OK, callback.WaitForResult());
599 EXPECT_TRUE(handle.is_initialized());
600 EXPECT_TRUE(handle.socket());
602 handle.Reset();
604 // Need to run all pending to release the socket back to the pool.
605 base::MessageLoop::current()->RunUntilIdle();
607 // Now we should have 1 idle socket.
608 EXPECT_EQ(1, pool_.IdleSocketCount());
610 // After an IP address change, we should have 0 idle sockets.
611 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests();
612 base::MessageLoop::current()->RunUntilIdle(); // Notification happens async.
614 EXPECT_EQ(0, pool_.IdleSocketCount());
617 TEST_F(TransportClientSocketPoolTest, BackupSocketConnect) {
618 // Case 1 tests the first socket stalling, and the backup connecting.
619 MockTransportClientSocketFactory::ClientSocketType case1_types[] = {
620 // The first socket will not connect.
621 MockTransportClientSocketFactory::MOCK_STALLED_CLIENT_SOCKET,
622 // The second socket will connect more quickly.
623 MockTransportClientSocketFactory::MOCK_CLIENT_SOCKET
626 // Case 2 tests the first socket being slow, so that we start the
627 // second connect, but the second connect stalls, and we still
628 // complete the first.
629 MockTransportClientSocketFactory::ClientSocketType case2_types[] = {
630 // The first socket will connect, although delayed.
631 MockTransportClientSocketFactory::MOCK_DELAYED_CLIENT_SOCKET,
632 // The second socket will not connect.
633 MockTransportClientSocketFactory::MOCK_STALLED_CLIENT_SOCKET
636 MockTransportClientSocketFactory::ClientSocketType* cases[2] = {
637 case1_types,
638 case2_types
641 for (size_t index = 0; index < arraysize(cases); ++index) {
642 client_socket_factory_.set_client_socket_types(cases[index], 2);
644 EXPECT_EQ(0, pool_.IdleSocketCount());
646 TestCompletionCallback callback;
647 ClientSocketHandle handle;
648 int rv = handle.Init("b", params_, LOW, callback.callback(), &pool_,
649 BoundNetLog());
650 EXPECT_EQ(ERR_IO_PENDING, rv);
651 EXPECT_FALSE(handle.is_initialized());
652 EXPECT_FALSE(handle.socket());
654 // Create the first socket, set the timer.
655 base::MessageLoop::current()->RunUntilIdle();
657 // Wait for the backup socket timer to fire.
658 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(
659 ClientSocketPool::kMaxConnectRetryIntervalMs + 50));
661 // Let the appropriate socket connect.
662 base::MessageLoop::current()->RunUntilIdle();
664 EXPECT_EQ(OK, callback.WaitForResult());
665 EXPECT_TRUE(handle.is_initialized());
666 EXPECT_TRUE(handle.socket());
668 // One socket is stalled, the other is active.
669 EXPECT_EQ(0, pool_.IdleSocketCount());
670 handle.Reset();
672 // Close all pending connect jobs and existing sockets.
673 pool_.FlushWithError(ERR_NETWORK_CHANGED);
677 // Test the case where a socket took long enough to start the creation
678 // of the backup socket, but then we cancelled the request after that.
679 TEST_F(TransportClientSocketPoolTest, BackupSocketCancel) {
680 client_socket_factory_.set_default_client_socket_type(
681 MockTransportClientSocketFactory::MOCK_STALLED_CLIENT_SOCKET);
683 enum { CANCEL_BEFORE_WAIT, CANCEL_AFTER_WAIT };
685 for (int index = CANCEL_BEFORE_WAIT; index < CANCEL_AFTER_WAIT; ++index) {
686 EXPECT_EQ(0, pool_.IdleSocketCount());
688 TestCompletionCallback callback;
689 ClientSocketHandle handle;
690 int rv = handle.Init("c", params_, LOW, callback.callback(), &pool_,
691 BoundNetLog());
692 EXPECT_EQ(ERR_IO_PENDING, rv);
693 EXPECT_FALSE(handle.is_initialized());
694 EXPECT_FALSE(handle.socket());
696 // Create the first socket, set the timer.
697 base::MessageLoop::current()->RunUntilIdle();
699 if (index == CANCEL_AFTER_WAIT) {
700 // Wait for the backup socket timer to fire.
701 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(
702 ClientSocketPool::kMaxConnectRetryIntervalMs));
705 // Let the appropriate socket connect.
706 base::MessageLoop::current()->RunUntilIdle();
708 handle.Reset();
710 EXPECT_FALSE(callback.have_result());
711 EXPECT_FALSE(handle.is_initialized());
712 EXPECT_FALSE(handle.socket());
714 // One socket is stalled, the other is active.
715 EXPECT_EQ(0, pool_.IdleSocketCount());
719 // Test the case where a socket took long enough to start the creation
720 // of the backup socket and never completes, and then the backup
721 // connection fails.
722 TEST_F(TransportClientSocketPoolTest, BackupSocketFailAfterStall) {
723 MockTransportClientSocketFactory::ClientSocketType case_types[] = {
724 // The first socket will not connect.
725 MockTransportClientSocketFactory::MOCK_STALLED_CLIENT_SOCKET,
726 // The second socket will fail immediately.
727 MockTransportClientSocketFactory::MOCK_FAILING_CLIENT_SOCKET
730 client_socket_factory_.set_client_socket_types(case_types, 2);
732 EXPECT_EQ(0, pool_.IdleSocketCount());
734 TestCompletionCallback callback;
735 ClientSocketHandle handle;
736 int rv = handle.Init("b", params_, LOW, callback.callback(), &pool_,
737 BoundNetLog());
738 EXPECT_EQ(ERR_IO_PENDING, rv);
739 EXPECT_FALSE(handle.is_initialized());
740 EXPECT_FALSE(handle.socket());
742 // Create the first socket, set the timer.
743 base::MessageLoop::current()->RunUntilIdle();
745 // Wait for the backup socket timer to fire.
746 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(
747 ClientSocketPool::kMaxConnectRetryIntervalMs));
749 // Let the second connect be synchronous. Otherwise, the emulated
750 // host resolution takes an extra trip through the message loop.
751 host_resolver_->set_synchronous_mode(true);
753 // Let the appropriate socket connect.
754 base::MessageLoop::current()->RunUntilIdle();
756 EXPECT_EQ(ERR_CONNECTION_FAILED, callback.WaitForResult());
757 EXPECT_FALSE(handle.is_initialized());
758 EXPECT_FALSE(handle.socket());
759 EXPECT_EQ(0, pool_.IdleSocketCount());
760 handle.Reset();
762 // Reset for the next case.
763 host_resolver_->set_synchronous_mode(false);
766 // Test the case where a socket took long enough to start the creation
767 // of the backup socket and eventually completes, but the backup socket
768 // fails.
769 TEST_F(TransportClientSocketPoolTest, BackupSocketFailAfterDelay) {
770 MockTransportClientSocketFactory::ClientSocketType case_types[] = {
771 // The first socket will connect, although delayed.
772 MockTransportClientSocketFactory::MOCK_DELAYED_CLIENT_SOCKET,
773 // The second socket will not connect.
774 MockTransportClientSocketFactory::MOCK_FAILING_CLIENT_SOCKET
777 client_socket_factory_.set_client_socket_types(case_types, 2);
778 client_socket_factory_.set_delay(base::TimeDelta::FromSeconds(5));
780 EXPECT_EQ(0, pool_.IdleSocketCount());
782 TestCompletionCallback callback;
783 ClientSocketHandle handle;
784 int rv = handle.Init("b", params_, LOW, callback.callback(), &pool_,
785 BoundNetLog());
786 EXPECT_EQ(ERR_IO_PENDING, rv);
787 EXPECT_FALSE(handle.is_initialized());
788 EXPECT_FALSE(handle.socket());
790 // Create the first socket, set the timer.
791 base::MessageLoop::current()->RunUntilIdle();
793 // Wait for the backup socket timer to fire.
794 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(
795 ClientSocketPool::kMaxConnectRetryIntervalMs));
797 // Let the second connect be synchronous. Otherwise, the emulated
798 // host resolution takes an extra trip through the message loop.
799 host_resolver_->set_synchronous_mode(true);
801 // Let the appropriate socket connect.
802 base::MessageLoop::current()->RunUntilIdle();
804 EXPECT_EQ(ERR_CONNECTION_FAILED, callback.WaitForResult());
805 EXPECT_FALSE(handle.is_initialized());
806 EXPECT_FALSE(handle.socket());
807 handle.Reset();
809 // Reset for the next case.
810 host_resolver_->set_synchronous_mode(false);
813 // Test the case of the IPv6 address stalling, and falling back to the IPv4
814 // socket which finishes first.
815 TEST_F(TransportClientSocketPoolTest, IPv6FallbackSocketIPv4FinishesFirst) {
816 // Create a pool without backup jobs.
817 ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(false);
818 TransportClientSocketPool pool(kMaxSockets,
819 kMaxSocketsPerGroup,
820 histograms_.get(),
821 host_resolver_.get(),
822 &client_socket_factory_,
823 NULL);
825 MockTransportClientSocketFactory::ClientSocketType case_types[] = {
826 // This is the IPv6 socket.
827 MockTransportClientSocketFactory::MOCK_STALLED_CLIENT_SOCKET,
828 // This is the IPv4 socket.
829 MockTransportClientSocketFactory::MOCK_PENDING_CLIENT_SOCKET
832 client_socket_factory_.set_client_socket_types(case_types, 2);
834 // Resolve an AddressList with a IPv6 address first and then a IPv4 address.
835 host_resolver_->rules()
836 ->AddIPLiteralRule("*", "2:abcd::3:4:ff,2.2.2.2", std::string());
838 TestCompletionCallback callback;
839 ClientSocketHandle handle;
840 int rv = handle.Init("a", params_, LOW, callback.callback(), &pool,
841 BoundNetLog());
842 EXPECT_EQ(ERR_IO_PENDING, rv);
843 EXPECT_FALSE(handle.is_initialized());
844 EXPECT_FALSE(handle.socket());
846 EXPECT_EQ(OK, callback.WaitForResult());
847 EXPECT_TRUE(handle.is_initialized());
848 EXPECT_TRUE(handle.socket());
849 IPEndPoint endpoint;
850 handle.socket()->GetLocalAddress(&endpoint);
851 EXPECT_EQ(kIPv4AddressSize, endpoint.address().size());
852 EXPECT_EQ(2, client_socket_factory_.allocation_count());
855 // Test the case of the IPv6 address being slow, thus falling back to trying to
856 // connect to the IPv4 address, but having the connect to the IPv6 address
857 // finish first.
858 TEST_F(TransportClientSocketPoolTest, IPv6FallbackSocketIPv6FinishesFirst) {
859 // Create a pool without backup jobs.
860 ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(false);
861 TransportClientSocketPool pool(kMaxSockets,
862 kMaxSocketsPerGroup,
863 histograms_.get(),
864 host_resolver_.get(),
865 &client_socket_factory_,
866 NULL);
868 MockTransportClientSocketFactory::ClientSocketType case_types[] = {
869 // This is the IPv6 socket.
870 MockTransportClientSocketFactory::MOCK_DELAYED_CLIENT_SOCKET,
871 // This is the IPv4 socket.
872 MockTransportClientSocketFactory::MOCK_STALLED_CLIENT_SOCKET
875 client_socket_factory_.set_client_socket_types(case_types, 2);
876 client_socket_factory_.set_delay(base::TimeDelta::FromMilliseconds(
877 TransportConnectJobHelper::kIPv6FallbackTimerInMs + 50));
879 // Resolve an AddressList with a IPv6 address first and then a IPv4 address.
880 host_resolver_->rules()
881 ->AddIPLiteralRule("*", "2:abcd::3:4:ff,2.2.2.2", std::string());
883 TestCompletionCallback callback;
884 ClientSocketHandle handle;
885 int rv = handle.Init("a", params_, LOW, callback.callback(), &pool,
886 BoundNetLog());
887 EXPECT_EQ(ERR_IO_PENDING, rv);
888 EXPECT_FALSE(handle.is_initialized());
889 EXPECT_FALSE(handle.socket());
891 EXPECT_EQ(OK, callback.WaitForResult());
892 EXPECT_TRUE(handle.is_initialized());
893 EXPECT_TRUE(handle.socket());
894 IPEndPoint endpoint;
895 handle.socket()->GetLocalAddress(&endpoint);
896 EXPECT_EQ(kIPv6AddressSize, endpoint.address().size());
897 EXPECT_EQ(2, client_socket_factory_.allocation_count());
900 TEST_F(TransportClientSocketPoolTest, IPv6NoIPv4AddressesToFallbackTo) {
901 // Create a pool without backup jobs.
902 ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(false);
903 TransportClientSocketPool pool(kMaxSockets,
904 kMaxSocketsPerGroup,
905 histograms_.get(),
906 host_resolver_.get(),
907 &client_socket_factory_,
908 NULL);
910 client_socket_factory_.set_default_client_socket_type(
911 MockTransportClientSocketFactory::MOCK_DELAYED_CLIENT_SOCKET);
913 // Resolve an AddressList with only IPv6 addresses.
914 host_resolver_->rules()
915 ->AddIPLiteralRule("*", "2:abcd::3:4:ff,3:abcd::3:4:ff", std::string());
917 TestCompletionCallback callback;
918 ClientSocketHandle handle;
919 int rv = handle.Init("a", params_, LOW, callback.callback(), &pool,
920 BoundNetLog());
921 EXPECT_EQ(ERR_IO_PENDING, rv);
922 EXPECT_FALSE(handle.is_initialized());
923 EXPECT_FALSE(handle.socket());
925 EXPECT_EQ(OK, callback.WaitForResult());
926 EXPECT_TRUE(handle.is_initialized());
927 EXPECT_TRUE(handle.socket());
928 IPEndPoint endpoint;
929 handle.socket()->GetLocalAddress(&endpoint);
930 EXPECT_EQ(kIPv6AddressSize, endpoint.address().size());
931 EXPECT_EQ(1, client_socket_factory_.allocation_count());
934 TEST_F(TransportClientSocketPoolTest, IPv4HasNoFallback) {
935 // Create a pool without backup jobs.
936 ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(false);
937 TransportClientSocketPool pool(kMaxSockets,
938 kMaxSocketsPerGroup,
939 histograms_.get(),
940 host_resolver_.get(),
941 &client_socket_factory_,
942 NULL);
944 client_socket_factory_.set_default_client_socket_type(
945 MockTransportClientSocketFactory::MOCK_DELAYED_CLIENT_SOCKET);
947 // Resolve an AddressList with only IPv4 addresses.
948 host_resolver_->rules()->AddIPLiteralRule("*", "1.1.1.1", std::string());
950 TestCompletionCallback callback;
951 ClientSocketHandle handle;
952 int rv = handle.Init("a", params_, LOW, callback.callback(), &pool,
953 BoundNetLog());
954 EXPECT_EQ(ERR_IO_PENDING, rv);
955 EXPECT_FALSE(handle.is_initialized());
956 EXPECT_FALSE(handle.socket());
958 EXPECT_EQ(OK, callback.WaitForResult());
959 EXPECT_TRUE(handle.is_initialized());
960 EXPECT_TRUE(handle.socket());
961 IPEndPoint endpoint;
962 handle.socket()->GetLocalAddress(&endpoint);
963 EXPECT_EQ(kIPv4AddressSize, endpoint.address().size());
964 EXPECT_EQ(1, client_socket_factory_.allocation_count());
967 } // namespace
969 } // namespace net