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"
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/ip_endpoint.h"
13 #include "net/base/load_timing_info.h"
14 #include "net/base/load_timing_info_test_util.h"
15 #include "net/base/net_errors.h"
16 #include "net/base/net_util.h"
17 #include "net/base/test_completion_callback.h"
18 #include "net/dns/mock_host_resolver.h"
19 #include "net/log/capturing_net_log.h"
20 #include "net/socket/client_socket_handle.h"
21 #include "net/socket/socket_test_util.h"
22 #include "net/socket/stream_socket.h"
23 #include "net/socket/transport_client_socket_pool_test_util.h"
24 #include "testing/gtest/include/gtest/gtest.h"
28 using internal::ClientSocketPoolBaseHelper
;
32 const int kMaxSockets
= 32;
33 const int kMaxSocketsPerGroup
= 6;
34 const RequestPriority kDefaultPriority
= LOW
;
36 class TransportClientSocketPoolTest
: public testing::Test
{
38 TransportClientSocketPoolTest()
39 : connect_backup_jobs_enabled_(
40 ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(true)),
42 new TransportSocketParams(
43 HostPortPair("www.google.com", 80),
46 OnHostResolutionCallback(),
47 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DEFAULT
)),
48 host_resolver_(new MockHostResolver
),
49 client_socket_factory_(&net_log_
),
53 &client_socket_factory_
,
57 ~TransportClientSocketPoolTest() override
{
58 internal::ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(
59 connect_backup_jobs_enabled_
);
62 scoped_refptr
<TransportSocketParams
> CreateParamsForTCPFastOpen() {
63 return new TransportSocketParams(HostPortPair("www.google.com", 80),
64 false, false, OnHostResolutionCallback(),
65 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DESIRED
);
68 int StartRequest(const std::string
& group_name
, RequestPriority priority
) {
69 scoped_refptr
<TransportSocketParams
> params(new TransportSocketParams(
70 HostPortPair("www.google.com", 80), false, false,
71 OnHostResolutionCallback(),
72 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DEFAULT
));
73 return test_base_
.StartRequestUsingPool(
74 &pool_
, group_name
, priority
, params
);
77 int GetOrderOfRequest(size_t index
) {
78 return test_base_
.GetOrderOfRequest(index
);
81 bool ReleaseOneConnection(ClientSocketPoolTest::KeepAlive keep_alive
) {
82 return test_base_
.ReleaseOneConnection(keep_alive
);
85 void ReleaseAllConnections(ClientSocketPoolTest::KeepAlive keep_alive
) {
86 test_base_
.ReleaseAllConnections(keep_alive
);
89 ScopedVector
<TestSocketRequest
>* requests() { return test_base_
.requests(); }
90 size_t completion_count() const { return test_base_
.completion_count(); }
92 bool connect_backup_jobs_enabled_
;
93 CapturingNetLog net_log_
;
94 scoped_refptr
<TransportSocketParams
> params_
;
95 scoped_ptr
<MockHostResolver
> host_resolver_
;
96 MockTransportClientSocketFactory client_socket_factory_
;
97 TransportClientSocketPool pool_
;
98 ClientSocketPoolTest test_base_
;
101 DISALLOW_COPY_AND_ASSIGN(TransportClientSocketPoolTest
);
104 TEST(TransportConnectJobTest
, MakeAddrListStartWithIPv4
) {
105 IPAddressNumber ip_number
;
106 ASSERT_TRUE(ParseIPLiteralToNumber("192.168.1.1", &ip_number
));
107 IPEndPoint
addrlist_v4_1(ip_number
, 80);
108 ASSERT_TRUE(ParseIPLiteralToNumber("192.168.1.2", &ip_number
));
109 IPEndPoint
addrlist_v4_2(ip_number
, 80);
110 ASSERT_TRUE(ParseIPLiteralToNumber("2001:4860:b006::64", &ip_number
));
111 IPEndPoint
addrlist_v6_1(ip_number
, 80);
112 ASSERT_TRUE(ParseIPLiteralToNumber("2001:4860:b006::66", &ip_number
));
113 IPEndPoint
addrlist_v6_2(ip_number
, 80);
115 AddressList addrlist
;
117 // Test 1: IPv4 only. Expect no change.
119 addrlist
.push_back(addrlist_v4_1
);
120 addrlist
.push_back(addrlist_v4_2
);
121 TransportConnectJob::MakeAddressListStartWithIPv4(&addrlist
);
122 ASSERT_EQ(2u, addrlist
.size());
123 EXPECT_EQ(ADDRESS_FAMILY_IPV4
, addrlist
[0].GetFamily());
124 EXPECT_EQ(ADDRESS_FAMILY_IPV4
, addrlist
[1].GetFamily());
126 // Test 2: IPv6 only. Expect no change.
128 addrlist
.push_back(addrlist_v6_1
);
129 addrlist
.push_back(addrlist_v6_2
);
130 TransportConnectJob::MakeAddressListStartWithIPv4(&addrlist
);
131 ASSERT_EQ(2u, addrlist
.size());
132 EXPECT_EQ(ADDRESS_FAMILY_IPV6
, addrlist
[0].GetFamily());
133 EXPECT_EQ(ADDRESS_FAMILY_IPV6
, addrlist
[1].GetFamily());
135 // Test 3: IPv4 then IPv6. Expect no change.
137 addrlist
.push_back(addrlist_v4_1
);
138 addrlist
.push_back(addrlist_v4_2
);
139 addrlist
.push_back(addrlist_v6_1
);
140 addrlist
.push_back(addrlist_v6_2
);
141 TransportConnectJob::MakeAddressListStartWithIPv4(&addrlist
);
142 ASSERT_EQ(4u, addrlist
.size());
143 EXPECT_EQ(ADDRESS_FAMILY_IPV4
, addrlist
[0].GetFamily());
144 EXPECT_EQ(ADDRESS_FAMILY_IPV4
, addrlist
[1].GetFamily());
145 EXPECT_EQ(ADDRESS_FAMILY_IPV6
, addrlist
[2].GetFamily());
146 EXPECT_EQ(ADDRESS_FAMILY_IPV6
, addrlist
[3].GetFamily());
148 // Test 4: IPv6, IPv4, IPv6, IPv4. Expect first IPv6 moved to the end.
150 addrlist
.push_back(addrlist_v6_1
);
151 addrlist
.push_back(addrlist_v4_1
);
152 addrlist
.push_back(addrlist_v6_2
);
153 addrlist
.push_back(addrlist_v4_2
);
154 TransportConnectJob::MakeAddressListStartWithIPv4(&addrlist
);
155 ASSERT_EQ(4u, addrlist
.size());
156 EXPECT_EQ(ADDRESS_FAMILY_IPV4
, addrlist
[0].GetFamily());
157 EXPECT_EQ(ADDRESS_FAMILY_IPV6
, addrlist
[1].GetFamily());
158 EXPECT_EQ(ADDRESS_FAMILY_IPV4
, addrlist
[2].GetFamily());
159 EXPECT_EQ(ADDRESS_FAMILY_IPV6
, addrlist
[3].GetFamily());
161 // Test 5: IPv6, IPv6, IPv4, IPv4. Expect first two IPv6's moved to the end.
163 addrlist
.push_back(addrlist_v6_1
);
164 addrlist
.push_back(addrlist_v6_2
);
165 addrlist
.push_back(addrlist_v4_1
);
166 addrlist
.push_back(addrlist_v4_2
);
167 TransportConnectJob::MakeAddressListStartWithIPv4(&addrlist
);
168 ASSERT_EQ(4u, addrlist
.size());
169 EXPECT_EQ(ADDRESS_FAMILY_IPV4
, addrlist
[0].GetFamily());
170 EXPECT_EQ(ADDRESS_FAMILY_IPV4
, addrlist
[1].GetFamily());
171 EXPECT_EQ(ADDRESS_FAMILY_IPV6
, addrlist
[2].GetFamily());
172 EXPECT_EQ(ADDRESS_FAMILY_IPV6
, addrlist
[3].GetFamily());
175 TEST_F(TransportClientSocketPoolTest
, Basic
) {
176 TestCompletionCallback callback
;
177 ClientSocketHandle handle
;
178 int rv
= handle
.Init("a", params_
, LOW
, callback
.callback(), &pool_
,
180 EXPECT_EQ(ERR_IO_PENDING
, rv
);
181 EXPECT_FALSE(handle
.is_initialized());
182 EXPECT_FALSE(handle
.socket());
184 EXPECT_EQ(OK
, callback
.WaitForResult());
185 EXPECT_TRUE(handle
.is_initialized());
186 EXPECT_TRUE(handle
.socket());
187 TestLoadTimingInfoConnectedNotReused(handle
);
190 // Make sure that TransportConnectJob passes on its priority to its
191 // HostResolver request on Init.
192 TEST_F(TransportClientSocketPoolTest
, SetResolvePriorityOnInit
) {
193 for (int i
= MINIMUM_PRIORITY
; i
<= MAXIMUM_PRIORITY
; ++i
) {
194 RequestPriority priority
= static_cast<RequestPriority
>(i
);
195 TestCompletionCallback callback
;
196 ClientSocketHandle handle
;
197 EXPECT_EQ(ERR_IO_PENDING
,
198 handle
.Init("a", params_
, priority
, callback
.callback(), &pool_
,
200 EXPECT_EQ(priority
, host_resolver_
->last_request_priority());
204 TEST_F(TransportClientSocketPoolTest
, InitHostResolutionFailure
) {
205 host_resolver_
->rules()->AddSimulatedFailure("unresolvable.host.name");
206 TestCompletionCallback callback
;
207 ClientSocketHandle handle
;
208 HostPortPair
host_port_pair("unresolvable.host.name", 80);
209 scoped_refptr
<TransportSocketParams
> dest(new TransportSocketParams(
210 host_port_pair
, false, false, OnHostResolutionCallback(),
211 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DEFAULT
));
212 EXPECT_EQ(ERR_IO_PENDING
,
213 handle
.Init("a", dest
, kDefaultPriority
, callback
.callback(),
214 &pool_
, BoundNetLog()));
215 EXPECT_EQ(ERR_NAME_NOT_RESOLVED
, callback
.WaitForResult());
218 TEST_F(TransportClientSocketPoolTest
, InitConnectionFailure
) {
219 client_socket_factory_
.set_default_client_socket_type(
220 MockTransportClientSocketFactory::MOCK_FAILING_CLIENT_SOCKET
);
221 TestCompletionCallback callback
;
222 ClientSocketHandle handle
;
223 EXPECT_EQ(ERR_IO_PENDING
,
224 handle
.Init("a", params_
, kDefaultPriority
, callback
.callback(),
225 &pool_
, BoundNetLog()));
226 EXPECT_EQ(ERR_CONNECTION_FAILED
, callback
.WaitForResult());
228 // Make the host resolutions complete synchronously this time.
229 host_resolver_
->set_synchronous_mode(true);
230 EXPECT_EQ(ERR_CONNECTION_FAILED
,
231 handle
.Init("a", params_
, kDefaultPriority
, callback
.callback(),
232 &pool_
, BoundNetLog()));
235 TEST_F(TransportClientSocketPoolTest
, PendingRequests
) {
236 // First request finishes asynchronously.
237 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", kDefaultPriority
));
238 EXPECT_EQ(OK
, (*requests())[0]->WaitForResult());
240 // Make all subsequent host resolutions complete synchronously.
241 host_resolver_
->set_synchronous_mode(true);
243 // Rest of them finish synchronously, until we reach the per-group limit.
244 EXPECT_EQ(OK
, StartRequest("a", kDefaultPriority
));
245 EXPECT_EQ(OK
, StartRequest("a", kDefaultPriority
));
246 EXPECT_EQ(OK
, StartRequest("a", kDefaultPriority
));
247 EXPECT_EQ(OK
, StartRequest("a", kDefaultPriority
));
248 EXPECT_EQ(OK
, StartRequest("a", kDefaultPriority
));
250 // The rest are pending since we've used all active sockets.
251 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", HIGHEST
));
252 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", LOWEST
));
253 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", LOWEST
));
254 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", MEDIUM
));
255 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", LOW
));
256 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", HIGHEST
));
257 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", LOWEST
));
258 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", MEDIUM
));
259 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", MEDIUM
));
260 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", HIGHEST
));
262 ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE
);
264 EXPECT_EQ(kMaxSocketsPerGroup
, client_socket_factory_
.allocation_count());
266 // One initial asynchronous request and then 10 pending requests.
267 EXPECT_EQ(11U, completion_count());
269 // First part of requests, all with the same priority, finishes in FIFO order.
270 EXPECT_EQ(1, GetOrderOfRequest(1));
271 EXPECT_EQ(2, GetOrderOfRequest(2));
272 EXPECT_EQ(3, GetOrderOfRequest(3));
273 EXPECT_EQ(4, GetOrderOfRequest(4));
274 EXPECT_EQ(5, GetOrderOfRequest(5));
275 EXPECT_EQ(6, GetOrderOfRequest(6));
277 // Make sure that rest of the requests complete in the order of priority.
278 EXPECT_EQ(7, GetOrderOfRequest(7));
279 EXPECT_EQ(14, GetOrderOfRequest(8));
280 EXPECT_EQ(15, GetOrderOfRequest(9));
281 EXPECT_EQ(10, GetOrderOfRequest(10));
282 EXPECT_EQ(13, GetOrderOfRequest(11));
283 EXPECT_EQ(8, GetOrderOfRequest(12));
284 EXPECT_EQ(16, GetOrderOfRequest(13));
285 EXPECT_EQ(11, GetOrderOfRequest(14));
286 EXPECT_EQ(12, GetOrderOfRequest(15));
287 EXPECT_EQ(9, GetOrderOfRequest(16));
289 // Make sure we test order of all requests made.
290 EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds
, GetOrderOfRequest(17));
293 TEST_F(TransportClientSocketPoolTest
, PendingRequests_NoKeepAlive
) {
294 // First request finishes asynchronously.
295 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", kDefaultPriority
));
296 EXPECT_EQ(OK
, (*requests())[0]->WaitForResult());
298 // Make all subsequent host resolutions complete synchronously.
299 host_resolver_
->set_synchronous_mode(true);
301 // Rest of them finish synchronously, until we reach the per-group limit.
302 EXPECT_EQ(OK
, StartRequest("a", kDefaultPriority
));
303 EXPECT_EQ(OK
, StartRequest("a", kDefaultPriority
));
304 EXPECT_EQ(OK
, StartRequest("a", kDefaultPriority
));
305 EXPECT_EQ(OK
, StartRequest("a", kDefaultPriority
));
306 EXPECT_EQ(OK
, StartRequest("a", kDefaultPriority
));
308 // The rest are pending since we've used all active sockets.
309 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", kDefaultPriority
));
310 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", kDefaultPriority
));
311 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", kDefaultPriority
));
312 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", kDefaultPriority
));
313 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", kDefaultPriority
));
315 ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE
);
317 // The pending requests should finish successfully.
318 EXPECT_EQ(OK
, (*requests())[6]->WaitForResult());
319 EXPECT_EQ(OK
, (*requests())[7]->WaitForResult());
320 EXPECT_EQ(OK
, (*requests())[8]->WaitForResult());
321 EXPECT_EQ(OK
, (*requests())[9]->WaitForResult());
322 EXPECT_EQ(OK
, (*requests())[10]->WaitForResult());
324 EXPECT_EQ(static_cast<int>(requests()->size()),
325 client_socket_factory_
.allocation_count());
327 // First asynchronous request, and then last 5 pending requests.
328 EXPECT_EQ(6U, completion_count());
331 // This test will start up a RequestSocket() and then immediately Cancel() it.
332 // The pending host resolution will eventually complete, and destroy the
333 // ClientSocketPool which will crash if the group was not cleared properly.
334 TEST_F(TransportClientSocketPoolTest
, CancelRequestClearGroup
) {
335 TestCompletionCallback callback
;
336 ClientSocketHandle handle
;
337 EXPECT_EQ(ERR_IO_PENDING
,
338 handle
.Init("a", params_
, kDefaultPriority
, callback
.callback(),
339 &pool_
, BoundNetLog()));
343 TEST_F(TransportClientSocketPoolTest
, TwoRequestsCancelOne
) {
344 ClientSocketHandle handle
;
345 TestCompletionCallback callback
;
346 ClientSocketHandle handle2
;
347 TestCompletionCallback callback2
;
349 EXPECT_EQ(ERR_IO_PENDING
,
350 handle
.Init("a", params_
, kDefaultPriority
, callback
.callback(),
351 &pool_
, BoundNetLog()));
352 EXPECT_EQ(ERR_IO_PENDING
,
353 handle2
.Init("a", params_
, kDefaultPriority
, callback2
.callback(),
354 &pool_
, BoundNetLog()));
358 EXPECT_EQ(OK
, callback2
.WaitForResult());
362 TEST_F(TransportClientSocketPoolTest
, ConnectCancelConnect
) {
363 client_socket_factory_
.set_default_client_socket_type(
364 MockTransportClientSocketFactory::MOCK_PENDING_CLIENT_SOCKET
);
365 ClientSocketHandle handle
;
366 TestCompletionCallback callback
;
367 EXPECT_EQ(ERR_IO_PENDING
,
368 handle
.Init("a", params_
, kDefaultPriority
, callback
.callback(),
369 &pool_
, BoundNetLog()));
373 TestCompletionCallback callback2
;
374 EXPECT_EQ(ERR_IO_PENDING
,
375 handle
.Init("a", params_
, kDefaultPriority
, callback2
.callback(),
376 &pool_
, BoundNetLog()));
378 host_resolver_
->set_synchronous_mode(true);
379 // At this point, handle has two ConnectingSockets out for it. Due to the
380 // setting the mock resolver into synchronous mode, the host resolution for
381 // both will return in the same loop of the MessageLoop. The client socket
382 // is a pending socket, so the Connect() will asynchronously complete on the
383 // next loop of the MessageLoop. That means that the first
384 // ConnectingSocket will enter OnIOComplete, and then the second one will.
385 // If the first one is not cancelled, it will advance the load state, and
386 // then the second one will crash.
388 EXPECT_EQ(OK
, callback2
.WaitForResult());
389 EXPECT_FALSE(callback
.have_result());
394 TEST_F(TransportClientSocketPoolTest
, CancelRequest
) {
395 // First request finishes asynchronously.
396 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", kDefaultPriority
));
397 EXPECT_EQ(OK
, (*requests())[0]->WaitForResult());
399 // Make all subsequent host resolutions complete synchronously.
400 host_resolver_
->set_synchronous_mode(true);
402 EXPECT_EQ(OK
, StartRequest("a", kDefaultPriority
));
403 EXPECT_EQ(OK
, StartRequest("a", kDefaultPriority
));
404 EXPECT_EQ(OK
, StartRequest("a", kDefaultPriority
));
405 EXPECT_EQ(OK
, StartRequest("a", kDefaultPriority
));
406 EXPECT_EQ(OK
, StartRequest("a", kDefaultPriority
));
408 // Reached per-group limit, queue up requests.
409 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", LOWEST
));
410 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", HIGHEST
));
411 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", HIGHEST
));
412 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", MEDIUM
));
413 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", MEDIUM
));
414 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", LOW
));
415 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", HIGHEST
));
416 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", LOW
));
417 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", LOW
));
418 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", LOWEST
));
421 size_t index_to_cancel
= kMaxSocketsPerGroup
+ 2;
422 EXPECT_FALSE((*requests())[index_to_cancel
]->handle()->is_initialized());
423 (*requests())[index_to_cancel
]->handle()->Reset();
425 ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE
);
427 EXPECT_EQ(kMaxSocketsPerGroup
,
428 client_socket_factory_
.allocation_count());
429 EXPECT_EQ(requests()->size() - kMaxSocketsPerGroup
, completion_count());
431 EXPECT_EQ(1, GetOrderOfRequest(1));
432 EXPECT_EQ(2, GetOrderOfRequest(2));
433 EXPECT_EQ(3, GetOrderOfRequest(3));
434 EXPECT_EQ(4, GetOrderOfRequest(4));
435 EXPECT_EQ(5, GetOrderOfRequest(5));
436 EXPECT_EQ(6, GetOrderOfRequest(6));
437 EXPECT_EQ(14, GetOrderOfRequest(7));
438 EXPECT_EQ(7, GetOrderOfRequest(8));
439 EXPECT_EQ(ClientSocketPoolTest::kRequestNotFound
,
440 GetOrderOfRequest(9)); // Canceled request.
441 EXPECT_EQ(9, GetOrderOfRequest(10));
442 EXPECT_EQ(10, GetOrderOfRequest(11));
443 EXPECT_EQ(11, GetOrderOfRequest(12));
444 EXPECT_EQ(8, GetOrderOfRequest(13));
445 EXPECT_EQ(12, GetOrderOfRequest(14));
446 EXPECT_EQ(13, GetOrderOfRequest(15));
447 EXPECT_EQ(15, GetOrderOfRequest(16));
449 // Make sure we test order of all requests made.
450 EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds
, GetOrderOfRequest(17));
453 class RequestSocketCallback
: public TestCompletionCallbackBase
{
455 RequestSocketCallback(ClientSocketHandle
* handle
,
456 TransportClientSocketPool
* pool
)
459 within_callback_(false),
460 callback_(base::Bind(&RequestSocketCallback::OnComplete
,
461 base::Unretained(this))) {
464 ~RequestSocketCallback() override
{}
466 const CompletionCallback
& callback() const { return callback_
; }
469 void OnComplete(int result
) {
471 ASSERT_EQ(OK
, result
);
473 if (!within_callback_
) {
474 // Don't allow reuse of the socket. Disconnect it and then release it and
475 // run through the MessageLoop once to get it completely released.
476 handle_
->socket()->Disconnect();
479 base::MessageLoop::ScopedNestableTaskAllower
allow(
480 base::MessageLoop::current());
481 base::MessageLoop::current()->RunUntilIdle();
483 within_callback_
= true;
484 scoped_refptr
<TransportSocketParams
> dest(new TransportSocketParams(
485 HostPortPair("www.google.com", 80), false, false,
486 OnHostResolutionCallback(),
487 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DEFAULT
));
488 int rv
= handle_
->Init("a", dest
, LOWEST
, callback(), pool_
,
494 ClientSocketHandle
* const handle_
;
495 TransportClientSocketPool
* const pool_
;
496 bool within_callback_
;
497 CompletionCallback callback_
;
499 DISALLOW_COPY_AND_ASSIGN(RequestSocketCallback
);
502 TEST_F(TransportClientSocketPoolTest
, RequestTwice
) {
503 ClientSocketHandle handle
;
504 RequestSocketCallback
callback(&handle
, &pool_
);
505 scoped_refptr
<TransportSocketParams
> dest(new TransportSocketParams(
506 HostPortPair("www.google.com", 80), false, false,
507 OnHostResolutionCallback(),
508 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DEFAULT
));
509 int rv
= handle
.Init("a", dest
, LOWEST
, callback
.callback(), &pool_
,
511 ASSERT_EQ(ERR_IO_PENDING
, rv
);
513 // The callback is going to request "www.google.com". We want it to complete
514 // synchronously this time.
515 host_resolver_
->set_synchronous_mode(true);
517 EXPECT_EQ(OK
, callback
.WaitForResult());
522 // Make sure that pending requests get serviced after active requests get
524 TEST_F(TransportClientSocketPoolTest
, CancelActiveRequestWithPendingRequests
) {
525 client_socket_factory_
.set_default_client_socket_type(
526 MockTransportClientSocketFactory::MOCK_PENDING_CLIENT_SOCKET
);
528 // Queue up all the requests
529 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", kDefaultPriority
));
530 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", kDefaultPriority
));
531 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", kDefaultPriority
));
532 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", kDefaultPriority
));
533 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", kDefaultPriority
));
534 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", kDefaultPriority
));
535 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", kDefaultPriority
));
536 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", kDefaultPriority
));
537 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", kDefaultPriority
));
539 // Now, kMaxSocketsPerGroup requests should be active. Let's cancel them.
540 ASSERT_LE(kMaxSocketsPerGroup
, static_cast<int>(requests()->size()));
541 for (int i
= 0; i
< kMaxSocketsPerGroup
; i
++)
542 (*requests())[i
]->handle()->Reset();
544 // Let's wait for the rest to complete now.
545 for (size_t i
= kMaxSocketsPerGroup
; i
< requests()->size(); ++i
) {
546 EXPECT_EQ(OK
, (*requests())[i
]->WaitForResult());
547 (*requests())[i
]->handle()->Reset();
550 EXPECT_EQ(requests()->size() - kMaxSocketsPerGroup
, completion_count());
553 // Make sure that pending requests get serviced after active requests fail.
554 TEST_F(TransportClientSocketPoolTest
, FailingActiveRequestWithPendingRequests
) {
555 client_socket_factory_
.set_default_client_socket_type(
556 MockTransportClientSocketFactory::MOCK_PENDING_FAILING_CLIENT_SOCKET
);
558 const int kNumRequests
= 2 * kMaxSocketsPerGroup
+ 1;
559 ASSERT_LE(kNumRequests
, kMaxSockets
); // Otherwise the test will hang.
561 // Queue up all the requests
562 for (int i
= 0; i
< kNumRequests
; i
++)
563 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", kDefaultPriority
));
565 for (int i
= 0; i
< kNumRequests
; i
++)
566 EXPECT_EQ(ERR_CONNECTION_FAILED
, (*requests())[i
]->WaitForResult());
569 TEST_F(TransportClientSocketPoolTest
, IdleSocketLoadTiming
) {
570 TestCompletionCallback callback
;
571 ClientSocketHandle handle
;
572 int rv
= handle
.Init("a", params_
, LOW
, callback
.callback(), &pool_
,
574 EXPECT_EQ(ERR_IO_PENDING
, rv
);
575 EXPECT_FALSE(handle
.is_initialized());
576 EXPECT_FALSE(handle
.socket());
578 EXPECT_EQ(OK
, callback
.WaitForResult());
579 EXPECT_TRUE(handle
.is_initialized());
580 EXPECT_TRUE(handle
.socket());
581 TestLoadTimingInfoConnectedNotReused(handle
);
584 // Need to run all pending to release the socket back to the pool.
585 base::MessageLoop::current()->RunUntilIdle();
587 // Now we should have 1 idle socket.
588 EXPECT_EQ(1, pool_
.IdleSocketCount());
590 rv
= handle
.Init("a", params_
, LOW
, callback
.callback(), &pool_
,
593 EXPECT_EQ(0, pool_
.IdleSocketCount());
594 TestLoadTimingInfoConnectedReused(handle
);
597 TEST_F(TransportClientSocketPoolTest
, ResetIdleSocketsOnIPAddressChange
) {
598 TestCompletionCallback callback
;
599 ClientSocketHandle handle
;
600 int rv
= handle
.Init("a", params_
, LOW
, callback
.callback(), &pool_
,
602 EXPECT_EQ(ERR_IO_PENDING
, rv
);
603 EXPECT_FALSE(handle
.is_initialized());
604 EXPECT_FALSE(handle
.socket());
606 EXPECT_EQ(OK
, callback
.WaitForResult());
607 EXPECT_TRUE(handle
.is_initialized());
608 EXPECT_TRUE(handle
.socket());
612 // Need to run all pending to release the socket back to the pool.
613 base::MessageLoop::current()->RunUntilIdle();
615 // Now we should have 1 idle socket.
616 EXPECT_EQ(1, pool_
.IdleSocketCount());
618 // After an IP address change, we should have 0 idle sockets.
619 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests();
620 base::MessageLoop::current()->RunUntilIdle(); // Notification happens async.
622 EXPECT_EQ(0, pool_
.IdleSocketCount());
625 TEST_F(TransportClientSocketPoolTest
, BackupSocketConnect
) {
626 // Case 1 tests the first socket stalling, and the backup connecting.
627 MockTransportClientSocketFactory::ClientSocketType case1_types
[] = {
628 // The first socket will not connect.
629 MockTransportClientSocketFactory::MOCK_STALLED_CLIENT_SOCKET
,
630 // The second socket will connect more quickly.
631 MockTransportClientSocketFactory::MOCK_CLIENT_SOCKET
634 // Case 2 tests the first socket being slow, so that we start the
635 // second connect, but the second connect stalls, and we still
636 // complete the first.
637 MockTransportClientSocketFactory::ClientSocketType case2_types
[] = {
638 // The first socket will connect, although delayed.
639 MockTransportClientSocketFactory::MOCK_DELAYED_CLIENT_SOCKET
,
640 // The second socket will not connect.
641 MockTransportClientSocketFactory::MOCK_STALLED_CLIENT_SOCKET
644 MockTransportClientSocketFactory::ClientSocketType
* cases
[2] = {
649 for (size_t index
= 0; index
< arraysize(cases
); ++index
) {
650 client_socket_factory_
.set_client_socket_types(cases
[index
], 2);
652 EXPECT_EQ(0, pool_
.IdleSocketCount());
654 TestCompletionCallback callback
;
655 ClientSocketHandle handle
;
656 int rv
= handle
.Init("b", params_
, LOW
, callback
.callback(), &pool_
,
658 EXPECT_EQ(ERR_IO_PENDING
, rv
);
659 EXPECT_FALSE(handle
.is_initialized());
660 EXPECT_FALSE(handle
.socket());
662 // Create the first socket, set the timer.
663 base::MessageLoop::current()->RunUntilIdle();
665 // Wait for the backup socket timer to fire.
666 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(
667 ClientSocketPool::kMaxConnectRetryIntervalMs
+ 50));
669 // Let the appropriate socket connect.
670 base::MessageLoop::current()->RunUntilIdle();
672 EXPECT_EQ(OK
, callback
.WaitForResult());
673 EXPECT_TRUE(handle
.is_initialized());
674 EXPECT_TRUE(handle
.socket());
676 // One socket is stalled, the other is active.
677 EXPECT_EQ(0, pool_
.IdleSocketCount());
680 // Close all pending connect jobs and existing sockets.
681 pool_
.FlushWithError(ERR_NETWORK_CHANGED
);
685 // Test the case where a socket took long enough to start the creation
686 // of the backup socket, but then we cancelled the request after that.
687 TEST_F(TransportClientSocketPoolTest
, BackupSocketCancel
) {
688 client_socket_factory_
.set_default_client_socket_type(
689 MockTransportClientSocketFactory::MOCK_STALLED_CLIENT_SOCKET
);
691 enum { CANCEL_BEFORE_WAIT
, CANCEL_AFTER_WAIT
};
693 for (int index
= CANCEL_BEFORE_WAIT
; index
< CANCEL_AFTER_WAIT
; ++index
) {
694 EXPECT_EQ(0, pool_
.IdleSocketCount());
696 TestCompletionCallback callback
;
697 ClientSocketHandle handle
;
698 int rv
= handle
.Init("c", params_
, LOW
, callback
.callback(), &pool_
,
700 EXPECT_EQ(ERR_IO_PENDING
, rv
);
701 EXPECT_FALSE(handle
.is_initialized());
702 EXPECT_FALSE(handle
.socket());
704 // Create the first socket, set the timer.
705 base::MessageLoop::current()->RunUntilIdle();
707 if (index
== CANCEL_AFTER_WAIT
) {
708 // Wait for the backup socket timer to fire.
709 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(
710 ClientSocketPool::kMaxConnectRetryIntervalMs
));
713 // Let the appropriate socket connect.
714 base::MessageLoop::current()->RunUntilIdle();
718 EXPECT_FALSE(callback
.have_result());
719 EXPECT_FALSE(handle
.is_initialized());
720 EXPECT_FALSE(handle
.socket());
722 // One socket is stalled, the other is active.
723 EXPECT_EQ(0, pool_
.IdleSocketCount());
727 // Test the case where a socket took long enough to start the creation
728 // of the backup socket and never completes, and then the backup
730 TEST_F(TransportClientSocketPoolTest
, BackupSocketFailAfterStall
) {
731 MockTransportClientSocketFactory::ClientSocketType case_types
[] = {
732 // The first socket will not connect.
733 MockTransportClientSocketFactory::MOCK_STALLED_CLIENT_SOCKET
,
734 // The second socket will fail immediately.
735 MockTransportClientSocketFactory::MOCK_FAILING_CLIENT_SOCKET
738 client_socket_factory_
.set_client_socket_types(case_types
, 2);
740 EXPECT_EQ(0, pool_
.IdleSocketCount());
742 TestCompletionCallback callback
;
743 ClientSocketHandle handle
;
744 int rv
= handle
.Init("b", params_
, LOW
, callback
.callback(), &pool_
,
746 EXPECT_EQ(ERR_IO_PENDING
, rv
);
747 EXPECT_FALSE(handle
.is_initialized());
748 EXPECT_FALSE(handle
.socket());
750 // Create the first socket, set the timer.
751 base::MessageLoop::current()->RunUntilIdle();
753 // Wait for the backup socket timer to fire.
754 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(
755 ClientSocketPool::kMaxConnectRetryIntervalMs
));
757 // Let the second connect be synchronous. Otherwise, the emulated
758 // host resolution takes an extra trip through the message loop.
759 host_resolver_
->set_synchronous_mode(true);
761 // Let the appropriate socket connect.
762 base::MessageLoop::current()->RunUntilIdle();
764 EXPECT_EQ(ERR_CONNECTION_FAILED
, callback
.WaitForResult());
765 EXPECT_FALSE(handle
.is_initialized());
766 EXPECT_FALSE(handle
.socket());
767 EXPECT_EQ(0, pool_
.IdleSocketCount());
770 // Reset for the next case.
771 host_resolver_
->set_synchronous_mode(false);
774 // Test the case where a socket took long enough to start the creation
775 // of the backup socket and eventually completes, but the backup socket
777 TEST_F(TransportClientSocketPoolTest
, BackupSocketFailAfterDelay
) {
778 MockTransportClientSocketFactory::ClientSocketType case_types
[] = {
779 // The first socket will connect, although delayed.
780 MockTransportClientSocketFactory::MOCK_DELAYED_CLIENT_SOCKET
,
781 // The second socket will not connect.
782 MockTransportClientSocketFactory::MOCK_FAILING_CLIENT_SOCKET
785 client_socket_factory_
.set_client_socket_types(case_types
, 2);
786 client_socket_factory_
.set_delay(base::TimeDelta::FromSeconds(5));
788 EXPECT_EQ(0, pool_
.IdleSocketCount());
790 TestCompletionCallback callback
;
791 ClientSocketHandle handle
;
792 int rv
= handle
.Init("b", params_
, LOW
, callback
.callback(), &pool_
,
794 EXPECT_EQ(ERR_IO_PENDING
, rv
);
795 EXPECT_FALSE(handle
.is_initialized());
796 EXPECT_FALSE(handle
.socket());
798 // Create the first socket, set the timer.
799 base::MessageLoop::current()->RunUntilIdle();
801 // Wait for the backup socket timer to fire.
802 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(
803 ClientSocketPool::kMaxConnectRetryIntervalMs
));
805 // Let the second connect be synchronous. Otherwise, the emulated
806 // host resolution takes an extra trip through the message loop.
807 host_resolver_
->set_synchronous_mode(true);
809 // Let the appropriate socket connect.
810 base::MessageLoop::current()->RunUntilIdle();
812 EXPECT_EQ(ERR_CONNECTION_FAILED
, callback
.WaitForResult());
813 EXPECT_FALSE(handle
.is_initialized());
814 EXPECT_FALSE(handle
.socket());
817 // Reset for the next case.
818 host_resolver_
->set_synchronous_mode(false);
821 // Test the case of the IPv6 address stalling, and falling back to the IPv4
822 // socket which finishes first.
823 TEST_F(TransportClientSocketPoolTest
, IPv6FallbackSocketIPv4FinishesFirst
) {
824 // Create a pool without backup jobs.
825 ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(false);
826 TransportClientSocketPool
pool(kMaxSockets
,
828 host_resolver_
.get(),
829 &client_socket_factory_
,
832 MockTransportClientSocketFactory::ClientSocketType case_types
[] = {
833 // This is the IPv6 socket.
834 MockTransportClientSocketFactory::MOCK_STALLED_CLIENT_SOCKET
,
835 // This is the IPv4 socket.
836 MockTransportClientSocketFactory::MOCK_PENDING_CLIENT_SOCKET
839 client_socket_factory_
.set_client_socket_types(case_types
, 2);
841 // Resolve an AddressList with a IPv6 address first and then a IPv4 address.
842 host_resolver_
->rules()
843 ->AddIPLiteralRule("*", "2:abcd::3:4:ff,2.2.2.2", std::string());
845 TestCompletionCallback callback
;
846 ClientSocketHandle handle
;
847 int rv
= handle
.Init("a", params_
, LOW
, callback
.callback(), &pool
,
849 EXPECT_EQ(ERR_IO_PENDING
, rv
);
850 EXPECT_FALSE(handle
.is_initialized());
851 EXPECT_FALSE(handle
.socket());
853 EXPECT_EQ(OK
, callback
.WaitForResult());
854 EXPECT_TRUE(handle
.is_initialized());
855 EXPECT_TRUE(handle
.socket());
857 handle
.socket()->GetLocalAddress(&endpoint
);
858 EXPECT_EQ(kIPv4AddressSize
, endpoint
.address().size());
859 EXPECT_EQ(2, client_socket_factory_
.allocation_count());
862 // Test the case of the IPv6 address being slow, thus falling back to trying to
863 // connect to the IPv4 address, but having the connect to the IPv6 address
865 TEST_F(TransportClientSocketPoolTest
, IPv6FallbackSocketIPv6FinishesFirst
) {
866 // Create a pool without backup jobs.
867 ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(false);
868 TransportClientSocketPool
pool(kMaxSockets
,
870 host_resolver_
.get(),
871 &client_socket_factory_
,
874 MockTransportClientSocketFactory::ClientSocketType case_types
[] = {
875 // This is the IPv6 socket.
876 MockTransportClientSocketFactory::MOCK_DELAYED_CLIENT_SOCKET
,
877 // This is the IPv4 socket.
878 MockTransportClientSocketFactory::MOCK_STALLED_CLIENT_SOCKET
881 client_socket_factory_
.set_client_socket_types(case_types
, 2);
882 client_socket_factory_
.set_delay(base::TimeDelta::FromMilliseconds(
883 TransportConnectJobHelper::kIPv6FallbackTimerInMs
+ 50));
885 // Resolve an AddressList with a IPv6 address first and then a IPv4 address.
886 host_resolver_
->rules()
887 ->AddIPLiteralRule("*", "2:abcd::3:4:ff,2.2.2.2", std::string());
889 TestCompletionCallback callback
;
890 ClientSocketHandle handle
;
891 int rv
= handle
.Init("a", params_
, LOW
, callback
.callback(), &pool
,
893 EXPECT_EQ(ERR_IO_PENDING
, rv
);
894 EXPECT_FALSE(handle
.is_initialized());
895 EXPECT_FALSE(handle
.socket());
897 EXPECT_EQ(OK
, callback
.WaitForResult());
898 EXPECT_TRUE(handle
.is_initialized());
899 EXPECT_TRUE(handle
.socket());
901 handle
.socket()->GetLocalAddress(&endpoint
);
902 EXPECT_EQ(kIPv6AddressSize
, endpoint
.address().size());
903 EXPECT_EQ(2, client_socket_factory_
.allocation_count());
906 TEST_F(TransportClientSocketPoolTest
, IPv6NoIPv4AddressesToFallbackTo
) {
907 // Create a pool without backup jobs.
908 ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(false);
909 TransportClientSocketPool
pool(kMaxSockets
,
911 host_resolver_
.get(),
912 &client_socket_factory_
,
915 client_socket_factory_
.set_default_client_socket_type(
916 MockTransportClientSocketFactory::MOCK_DELAYED_CLIENT_SOCKET
);
918 // Resolve an AddressList with only IPv6 addresses.
919 host_resolver_
->rules()
920 ->AddIPLiteralRule("*", "2:abcd::3:4:ff,3:abcd::3:4:ff", std::string());
922 TestCompletionCallback callback
;
923 ClientSocketHandle handle
;
924 int rv
= handle
.Init("a", params_
, LOW
, callback
.callback(), &pool
,
926 EXPECT_EQ(ERR_IO_PENDING
, rv
);
927 EXPECT_FALSE(handle
.is_initialized());
928 EXPECT_FALSE(handle
.socket());
930 EXPECT_EQ(OK
, callback
.WaitForResult());
931 EXPECT_TRUE(handle
.is_initialized());
932 EXPECT_TRUE(handle
.socket());
934 handle
.socket()->GetLocalAddress(&endpoint
);
935 EXPECT_EQ(kIPv6AddressSize
, endpoint
.address().size());
936 EXPECT_EQ(1, client_socket_factory_
.allocation_count());
939 TEST_F(TransportClientSocketPoolTest
, IPv4HasNoFallback
) {
940 // Create a pool without backup jobs.
941 ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(false);
942 TransportClientSocketPool
pool(kMaxSockets
,
944 host_resolver_
.get(),
945 &client_socket_factory_
,
948 client_socket_factory_
.set_default_client_socket_type(
949 MockTransportClientSocketFactory::MOCK_DELAYED_CLIENT_SOCKET
);
951 // Resolve an AddressList with only IPv4 addresses.
952 host_resolver_
->rules()->AddIPLiteralRule("*", "1.1.1.1", std::string());
954 TestCompletionCallback callback
;
955 ClientSocketHandle handle
;
956 int rv
= handle
.Init("a", params_
, LOW
, callback
.callback(), &pool
,
958 EXPECT_EQ(ERR_IO_PENDING
, rv
);
959 EXPECT_FALSE(handle
.is_initialized());
960 EXPECT_FALSE(handle
.socket());
962 EXPECT_EQ(OK
, callback
.WaitForResult());
963 EXPECT_TRUE(handle
.is_initialized());
964 EXPECT_TRUE(handle
.socket());
966 handle
.socket()->GetLocalAddress(&endpoint
);
967 EXPECT_EQ(kIPv4AddressSize
, endpoint
.address().size());
968 EXPECT_EQ(1, client_socket_factory_
.allocation_count());
971 // Test that if TCP FastOpen is enabled, it is set on the socket
972 // when we have only an IPv4 address.
973 TEST_F(TransportClientSocketPoolTest
, TCPFastOpenOnIPv4WithNoFallback
) {
974 // Create a pool without backup jobs.
975 ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(false);
976 TransportClientSocketPool
pool(kMaxSockets
,
978 host_resolver_
.get(),
979 &client_socket_factory_
,
981 client_socket_factory_
.set_default_client_socket_type(
982 MockTransportClientSocketFactory::MOCK_DELAYED_CLIENT_SOCKET
);
983 // Resolve an AddressList with only IPv4 addresses.
984 host_resolver_
->rules()->AddIPLiteralRule("*", "1.1.1.1", std::string());
986 TestCompletionCallback callback
;
987 ClientSocketHandle handle
;
988 // Enable TCP FastOpen in TransportSocketParams.
989 scoped_refptr
<TransportSocketParams
> params
= CreateParamsForTCPFastOpen();
990 handle
.Init("a", params
, LOW
, callback
.callback(), &pool
, BoundNetLog());
991 EXPECT_EQ(OK
, callback
.WaitForResult());
992 EXPECT_TRUE(handle
.socket()->UsingTCPFastOpen());
995 // Test that if TCP FastOpen is enabled, it is set on the socket
996 // when we have only IPv6 addresses.
997 TEST_F(TransportClientSocketPoolTest
, TCPFastOpenOnIPv6WithNoFallback
) {
998 // Create a pool without backup jobs.
999 ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(false);
1000 TransportClientSocketPool
pool(kMaxSockets
,
1001 kMaxSocketsPerGroup
,
1002 host_resolver_
.get(),
1003 &client_socket_factory_
,
1005 client_socket_factory_
.set_default_client_socket_type(
1006 MockTransportClientSocketFactory::MOCK_DELAYED_CLIENT_SOCKET
);
1007 // Resolve an AddressList with only IPv6 addresses.
1008 host_resolver_
->rules()
1009 ->AddIPLiteralRule("*", "2:abcd::3:4:ff,3:abcd::3:4:ff", std::string());
1011 TestCompletionCallback callback
;
1012 ClientSocketHandle handle
;
1013 // Enable TCP FastOpen in TransportSocketParams.
1014 scoped_refptr
<TransportSocketParams
> params
= CreateParamsForTCPFastOpen();
1015 handle
.Init("a", params
, LOW
, callback
.callback(), &pool
, BoundNetLog());
1016 EXPECT_EQ(OK
, callback
.WaitForResult());
1017 EXPECT_TRUE(handle
.socket()->UsingTCPFastOpen());
1020 // Test that if TCP FastOpen is enabled, it does not do anything when there
1021 // is a IPv6 address with fallback to an IPv4 address. This test tests the case
1022 // when the IPv6 connect fails and the IPv4 one succeeds.
1023 TEST_F(TransportClientSocketPoolTest
,
1024 NoTCPFastOpenOnIPv6FailureWithIPv4Fallback
) {
1025 // Create a pool without backup jobs.
1026 ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(false);
1027 TransportClientSocketPool
pool(kMaxSockets
,
1028 kMaxSocketsPerGroup
,
1029 host_resolver_
.get(),
1030 &client_socket_factory_
,
1033 MockTransportClientSocketFactory::ClientSocketType case_types
[] = {
1034 // This is the IPv6 socket.
1035 MockTransportClientSocketFactory::MOCK_STALLED_CLIENT_SOCKET
,
1036 // This is the IPv4 socket.
1037 MockTransportClientSocketFactory::MOCK_PENDING_CLIENT_SOCKET
1039 client_socket_factory_
.set_client_socket_types(case_types
, 2);
1040 // Resolve an AddressList with a IPv6 address first and then a IPv4 address.
1041 host_resolver_
->rules()
1042 ->AddIPLiteralRule("*", "2:abcd::3:4:ff,2.2.2.2", std::string());
1044 TestCompletionCallback callback
;
1045 ClientSocketHandle handle
;
1046 // Enable TCP FastOpen in TransportSocketParams.
1047 scoped_refptr
<TransportSocketParams
> params
= CreateParamsForTCPFastOpen();
1048 handle
.Init("a", params
, LOW
, callback
.callback(), &pool
, BoundNetLog());
1049 EXPECT_EQ(OK
, callback
.WaitForResult());
1050 // Verify that the socket used is connected to the fallback IPv4 address.
1051 IPEndPoint endpoint
;
1052 handle
.socket()->GetLocalAddress(&endpoint
);
1053 EXPECT_EQ(kIPv4AddressSize
, endpoint
.address().size());
1054 EXPECT_EQ(2, client_socket_factory_
.allocation_count());
1055 // Verify that TCP FastOpen was not turned on for the socket.
1056 EXPECT_FALSE(handle
.socket()->UsingTCPFastOpen());
1059 // Test that if TCP FastOpen is enabled, it does not do anything when there
1060 // is a IPv6 address with fallback to an IPv4 address. This test tests the case
1061 // when the IPv6 connect succeeds.
1062 TEST_F(TransportClientSocketPoolTest
,
1063 NoTCPFastOpenOnIPv6SuccessWithIPv4Fallback
) {
1064 // Create a pool without backup jobs.
1065 ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(false);
1066 TransportClientSocketPool
pool(kMaxSockets
,
1067 kMaxSocketsPerGroup
,
1068 host_resolver_
.get(),
1069 &client_socket_factory_
,
1072 MockTransportClientSocketFactory::ClientSocketType case_types
[] = {
1073 // This is the IPv6 socket.
1074 MockTransportClientSocketFactory::MOCK_PENDING_CLIENT_SOCKET
,
1075 // This is the IPv4 socket.
1076 MockTransportClientSocketFactory::MOCK_PENDING_CLIENT_SOCKET
1078 client_socket_factory_
.set_client_socket_types(case_types
, 2);
1079 // Resolve an AddressList with a IPv6 address first and then a IPv4 address.
1080 host_resolver_
->rules()
1081 ->AddIPLiteralRule("*", "2:abcd::3:4:ff,2.2.2.2", std::string());
1083 TestCompletionCallback callback
;
1084 ClientSocketHandle handle
;
1085 // Enable TCP FastOpen in TransportSocketParams.
1086 scoped_refptr
<TransportSocketParams
> params
= CreateParamsForTCPFastOpen();
1087 handle
.Init("a", params
, LOW
, callback
.callback(), &pool
, BoundNetLog());
1088 EXPECT_EQ(OK
, callback
.WaitForResult());
1089 // Verify that the socket used is connected to the IPv6 address.
1090 IPEndPoint endpoint
;
1091 handle
.socket()->GetLocalAddress(&endpoint
);
1092 EXPECT_EQ(kIPv6AddressSize
, endpoint
.address().size());
1093 EXPECT_EQ(1, client_socket_factory_
.allocation_count());
1094 // Verify that TCP FastOpen was not turned on for the socket.
1095 EXPECT_FALSE(handle
.socket()->UsingTCPFastOpen());