Convert raw pointers to scoped_ptr in net module.
[chromium-blink-merge.git] / net / socket / websocket_transport_client_socket_pool_unittest.cc
blob47a255878b608d7e438b7d270f7cfc8220daff14
1 // Copyright 2014 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/websocket_transport_client_socket_pool.h"
7 #include <queue>
8 #include <vector>
10 #include "base/bind.h"
11 #include "base/bind_helpers.h"
12 #include "base/callback.h"
13 #include "base/macros.h"
14 #include "base/message_loop/message_loop.h"
15 #include "base/run_loop.h"
16 #include "base/strings/stringprintf.h"
17 #include "base/time/time.h"
18 #include "net/base/ip_endpoint.h"
19 #include "net/base/load_timing_info.h"
20 #include "net/base/load_timing_info_test_util.h"
21 #include "net/base/net_errors.h"
22 #include "net/base/net_util.h"
23 #include "net/base/test_completion_callback.h"
24 #include "net/dns/mock_host_resolver.h"
25 #include "net/log/test_net_log.h"
26 #include "net/socket/client_socket_handle.h"
27 #include "net/socket/socket_test_util.h"
28 #include "net/socket/stream_socket.h"
29 #include "net/socket/transport_client_socket_pool_test_util.h"
30 #include "net/socket/websocket_endpoint_lock_manager.h"
31 #include "testing/gtest/include/gtest/gtest.h"
33 namespace net {
35 namespace {
37 const int kMaxSockets = 32;
38 const int kMaxSocketsPerGroup = 6;
39 const RequestPriority kDefaultPriority = LOW;
41 // RunLoop doesn't support this natively but it is easy to emulate.
42 void RunLoopForTimePeriod(base::TimeDelta period) {
43 base::RunLoop run_loop;
44 base::Closure quit_closure(run_loop.QuitClosure());
45 base::MessageLoop::current()->PostDelayedTask(
46 FROM_HERE, quit_closure, period);
47 run_loop.Run();
50 class WebSocketTransportClientSocketPoolTest : public ::testing::Test {
51 protected:
52 WebSocketTransportClientSocketPoolTest()
53 : params_(new TransportSocketParams(
54 HostPortPair("www.google.com", 80),
55 false,
56 false,
57 OnHostResolutionCallback(),
58 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DEFAULT)),
59 host_resolver_(new MockHostResolver),
60 client_socket_factory_(&net_log_),
61 pool_(kMaxSockets,
62 kMaxSocketsPerGroup,
63 host_resolver_.get(),
64 &client_socket_factory_,
65 NULL) {}
67 ~WebSocketTransportClientSocketPoolTest() override {
68 RunUntilIdle();
69 // ReleaseAllConnections() calls RunUntilIdle() after releasing each
70 // connection.
71 ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE);
72 EXPECT_TRUE(WebSocketEndpointLockManager::GetInstance()->IsEmpty());
75 static void RunUntilIdle() { base::RunLoop().RunUntilIdle(); }
77 int StartRequest(const std::string& group_name, RequestPriority priority) {
78 scoped_refptr<TransportSocketParams> params(
79 new TransportSocketParams(
80 HostPortPair("www.google.com", 80),
81 false,
82 false,
83 OnHostResolutionCallback(),
84 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DEFAULT));
85 return test_base_.StartRequestUsingPool(
86 &pool_, group_name, priority, params);
89 int GetOrderOfRequest(size_t index) {
90 return test_base_.GetOrderOfRequest(index);
93 bool ReleaseOneConnection(ClientSocketPoolTest::KeepAlive keep_alive) {
94 return test_base_.ReleaseOneConnection(keep_alive);
97 void ReleaseAllConnections(ClientSocketPoolTest::KeepAlive keep_alive) {
98 test_base_.ReleaseAllConnections(keep_alive);
101 TestSocketRequest* request(int i) { return test_base_.request(i); }
103 ScopedVector<TestSocketRequest>* requests() { return test_base_.requests(); }
104 size_t completion_count() const { return test_base_.completion_count(); }
106 TestNetLog net_log_;
107 scoped_refptr<TransportSocketParams> params_;
108 scoped_ptr<MockHostResolver> host_resolver_;
109 MockTransportClientSocketFactory client_socket_factory_;
110 WebSocketTransportClientSocketPool pool_;
111 ClientSocketPoolTest test_base_;
112 ScopedWebSocketEndpointZeroUnlockDelay zero_unlock_delay_;
114 private:
115 DISALLOW_COPY_AND_ASSIGN(WebSocketTransportClientSocketPoolTest);
118 TEST_F(WebSocketTransportClientSocketPoolTest, Basic) {
119 TestCompletionCallback callback;
120 ClientSocketHandle handle;
121 int rv = handle.Init(
122 "a", params_, LOW, callback.callback(), &pool_, BoundNetLog());
123 EXPECT_EQ(ERR_IO_PENDING, rv);
124 EXPECT_FALSE(handle.is_initialized());
125 EXPECT_FALSE(handle.socket());
127 EXPECT_EQ(OK, callback.WaitForResult());
128 EXPECT_TRUE(handle.is_initialized());
129 EXPECT_TRUE(handle.socket());
130 TestLoadTimingInfoConnectedNotReused(handle);
133 // Make sure that WebSocketTransportConnectJob passes on its priority to its
134 // HostResolver request on Init.
135 TEST_F(WebSocketTransportClientSocketPoolTest, SetResolvePriorityOnInit) {
136 for (int i = MINIMUM_PRIORITY; i <= MAXIMUM_PRIORITY; ++i) {
137 RequestPriority priority = static_cast<RequestPriority>(i);
138 TestCompletionCallback callback;
139 ClientSocketHandle handle;
140 EXPECT_EQ(ERR_IO_PENDING,
141 handle.Init("a",
142 params_,
143 priority,
144 callback.callback(),
145 &pool_,
146 BoundNetLog()));
147 EXPECT_EQ(priority, host_resolver_->last_request_priority());
151 TEST_F(WebSocketTransportClientSocketPoolTest, InitHostResolutionFailure) {
152 host_resolver_->rules()->AddSimulatedFailure("unresolvable.host.name");
153 TestCompletionCallback callback;
154 ClientSocketHandle handle;
155 HostPortPair host_port_pair("unresolvable.host.name", 80);
156 scoped_refptr<TransportSocketParams> dest(new TransportSocketParams(
157 host_port_pair, false, false, OnHostResolutionCallback(),
158 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DEFAULT));
159 EXPECT_EQ(ERR_IO_PENDING,
160 handle.Init("a",
161 dest,
162 kDefaultPriority,
163 callback.callback(),
164 &pool_,
165 BoundNetLog()));
166 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, callback.WaitForResult());
169 TEST_F(WebSocketTransportClientSocketPoolTest, InitConnectionFailure) {
170 client_socket_factory_.set_default_client_socket_type(
171 MockTransportClientSocketFactory::MOCK_FAILING_CLIENT_SOCKET);
172 TestCompletionCallback callback;
173 ClientSocketHandle handle;
174 EXPECT_EQ(ERR_IO_PENDING,
175 handle.Init("a",
176 params_,
177 kDefaultPriority,
178 callback.callback(),
179 &pool_,
180 BoundNetLog()));
181 EXPECT_EQ(ERR_CONNECTION_FAILED, callback.WaitForResult());
183 // Make the host resolutions complete synchronously this time.
184 host_resolver_->set_synchronous_mode(true);
185 EXPECT_EQ(ERR_CONNECTION_FAILED,
186 handle.Init("a",
187 params_,
188 kDefaultPriority,
189 callback.callback(),
190 &pool_,
191 BoundNetLog()));
194 TEST_F(WebSocketTransportClientSocketPoolTest, PendingRequestsFinishFifo) {
195 // First request finishes asynchronously.
196 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
197 EXPECT_EQ(OK, request(0)->WaitForResult());
199 // Make all subsequent host resolutions complete synchronously.
200 host_resolver_->set_synchronous_mode(true);
202 // Rest of them wait for the first socket to be released.
203 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
204 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
205 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
206 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
207 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
209 ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE);
211 EXPECT_EQ(6, client_socket_factory_.allocation_count());
213 // One initial asynchronous request and then 5 pending requests.
214 EXPECT_EQ(6U, completion_count());
216 // The requests finish in FIFO order.
217 EXPECT_EQ(1, GetOrderOfRequest(1));
218 EXPECT_EQ(2, GetOrderOfRequest(2));
219 EXPECT_EQ(3, GetOrderOfRequest(3));
220 EXPECT_EQ(4, GetOrderOfRequest(4));
221 EXPECT_EQ(5, GetOrderOfRequest(5));
222 EXPECT_EQ(6, GetOrderOfRequest(6));
224 // Make sure we test order of all requests made.
225 EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(7));
228 TEST_F(WebSocketTransportClientSocketPoolTest, PendingRequests_NoKeepAlive) {
229 // First request finishes asynchronously.
230 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
231 EXPECT_EQ(OK, request(0)->WaitForResult());
233 // Make all subsequent host resolutions complete synchronously.
234 host_resolver_->set_synchronous_mode(true);
236 // Rest of them wait for the first socket to be released.
237 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
238 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
239 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
240 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
241 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
243 ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE);
245 // The pending requests should finish successfully.
246 EXPECT_EQ(OK, request(1)->WaitForResult());
247 EXPECT_EQ(OK, request(2)->WaitForResult());
248 EXPECT_EQ(OK, request(3)->WaitForResult());
249 EXPECT_EQ(OK, request(4)->WaitForResult());
250 EXPECT_EQ(OK, request(5)->WaitForResult());
252 EXPECT_EQ(static_cast<int>(requests()->size()),
253 client_socket_factory_.allocation_count());
255 // First asynchronous request, and then last 5 pending requests.
256 EXPECT_EQ(6U, completion_count());
259 // This test will start up a RequestSocket() and then immediately Cancel() it.
260 // The pending host resolution will eventually complete, and destroy the
261 // ClientSocketPool which will crash if the group was not cleared properly.
262 TEST_F(WebSocketTransportClientSocketPoolTest, CancelRequestClearGroup) {
263 TestCompletionCallback callback;
264 ClientSocketHandle handle;
265 EXPECT_EQ(ERR_IO_PENDING,
266 handle.Init("a",
267 params_,
268 kDefaultPriority,
269 callback.callback(),
270 &pool_,
271 BoundNetLog()));
272 handle.Reset();
275 TEST_F(WebSocketTransportClientSocketPoolTest, TwoRequestsCancelOne) {
276 ClientSocketHandle handle;
277 TestCompletionCallback callback;
278 ClientSocketHandle handle2;
279 TestCompletionCallback callback2;
281 EXPECT_EQ(ERR_IO_PENDING,
282 handle.Init("a",
283 params_,
284 kDefaultPriority,
285 callback.callback(),
286 &pool_,
287 BoundNetLog()));
288 EXPECT_EQ(ERR_IO_PENDING,
289 handle2.Init("a",
290 params_,
291 kDefaultPriority,
292 callback2.callback(),
293 &pool_,
294 BoundNetLog()));
296 handle.Reset();
298 EXPECT_EQ(OK, callback2.WaitForResult());
299 handle2.Reset();
302 TEST_F(WebSocketTransportClientSocketPoolTest, ConnectCancelConnect) {
303 client_socket_factory_.set_default_client_socket_type(
304 MockTransportClientSocketFactory::MOCK_PENDING_CLIENT_SOCKET);
305 ClientSocketHandle handle;
306 TestCompletionCallback callback;
307 EXPECT_EQ(ERR_IO_PENDING,
308 handle.Init("a",
309 params_,
310 kDefaultPriority,
311 callback.callback(),
312 &pool_,
313 BoundNetLog()));
315 handle.Reset();
317 TestCompletionCallback callback2;
318 EXPECT_EQ(ERR_IO_PENDING,
319 handle.Init("a",
320 params_,
321 kDefaultPriority,
322 callback2.callback(),
323 &pool_,
324 BoundNetLog()));
326 host_resolver_->set_synchronous_mode(true);
327 // At this point, handle has two ConnectingSockets out for it. Due to the
328 // setting the mock resolver into synchronous mode, the host resolution for
329 // both will return in the same loop of the MessageLoop. The client socket
330 // is a pending socket, so the Connect() will asynchronously complete on the
331 // next loop of the MessageLoop. That means that the first
332 // ConnectingSocket will enter OnIOComplete, and then the second one will.
333 // If the first one is not cancelled, it will advance the load state, and
334 // then the second one will crash.
336 EXPECT_EQ(OK, callback2.WaitForResult());
337 EXPECT_FALSE(callback.have_result());
339 handle.Reset();
342 TEST_F(WebSocketTransportClientSocketPoolTest, CancelRequest) {
343 // First request finishes asynchronously.
344 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
345 EXPECT_EQ(OK, request(0)->WaitForResult());
347 // Make all subsequent host resolutions complete synchronously.
348 host_resolver_->set_synchronous_mode(true);
350 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
351 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
352 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
353 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
354 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
356 // Cancel a request.
357 const size_t index_to_cancel = 2;
358 EXPECT_FALSE(request(index_to_cancel)->handle()->is_initialized());
359 request(index_to_cancel)->handle()->Reset();
361 ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE);
363 EXPECT_EQ(5, client_socket_factory_.allocation_count());
365 EXPECT_EQ(1, GetOrderOfRequest(1));
366 EXPECT_EQ(2, GetOrderOfRequest(2));
367 EXPECT_EQ(ClientSocketPoolTest::kRequestNotFound,
368 GetOrderOfRequest(3)); // Canceled request.
369 EXPECT_EQ(3, GetOrderOfRequest(4));
370 EXPECT_EQ(4, GetOrderOfRequest(5));
371 EXPECT_EQ(5, GetOrderOfRequest(6));
373 // Make sure we test order of all requests made.
374 EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(7));
377 // Function to be used as a callback on socket request completion. It first
378 // disconnects the successfully connected socket from the first request, and
379 // then reuses the ClientSocketHandle to request another socket. The second
380 // request is expected to succeed asynchronously.
382 // |nested_callback| is called with the result of the second socket request.
383 void RequestSocketOnComplete(ClientSocketHandle* handle,
384 WebSocketTransportClientSocketPool* pool,
385 const CompletionCallback& nested_callback,
386 int first_request_result) {
387 EXPECT_EQ(OK, first_request_result);
389 // Don't allow reuse of the socket. Disconnect it and then release it.
390 handle->socket()->Disconnect();
391 handle->Reset();
393 scoped_refptr<TransportSocketParams> dest(new TransportSocketParams(
394 HostPortPair("www.google.com", 80), false, false,
395 OnHostResolutionCallback(),
396 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DEFAULT));
397 int rv =
398 handle->Init("a", dest, LOWEST, nested_callback, pool, BoundNetLog());
399 EXPECT_EQ(ERR_IO_PENDING, rv);
400 if (ERR_IO_PENDING != rv)
401 nested_callback.Run(rv);
404 // Tests the case where a second socket is requested in a completion callback,
405 // and the second socket connects asynchronously. Reuses the same
406 // ClientSocketHandle for the second socket, after disconnecting the first.
407 TEST_F(WebSocketTransportClientSocketPoolTest, RequestTwice) {
408 ClientSocketHandle handle;
409 scoped_refptr<TransportSocketParams> dest(
410 new TransportSocketParams(
411 HostPortPair("www.google.com", 80),
412 false,
413 false,
414 OnHostResolutionCallback(),
415 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DEFAULT));
416 TestCompletionCallback second_result_callback;
417 int rv = handle.Init("a", dest, LOWEST,
418 base::Bind(&RequestSocketOnComplete, &handle, &pool_,
419 second_result_callback.callback()),
420 &pool_, BoundNetLog());
421 ASSERT_EQ(ERR_IO_PENDING, rv);
422 EXPECT_EQ(OK, second_result_callback.WaitForResult());
424 handle.Reset();
427 // Make sure that pending requests get serviced after active requests get
428 // cancelled.
429 TEST_F(WebSocketTransportClientSocketPoolTest,
430 CancelActiveRequestWithPendingRequests) {
431 client_socket_factory_.set_default_client_socket_type(
432 MockTransportClientSocketFactory::MOCK_PENDING_CLIENT_SOCKET);
434 // Queue up all the requests
435 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
436 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
437 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
438 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
439 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
440 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
441 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
442 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
443 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
445 // Now, kMaxSocketsPerGroup requests should be active. Let's cancel them.
446 ASSERT_LE(kMaxSocketsPerGroup, static_cast<int>(requests()->size()));
447 for (int i = 0; i < kMaxSocketsPerGroup; i++)
448 request(i)->handle()->Reset();
450 // Let's wait for the rest to complete now.
451 for (size_t i = kMaxSocketsPerGroup; i < requests()->size(); ++i) {
452 EXPECT_EQ(OK, request(i)->WaitForResult());
453 request(i)->handle()->Reset();
456 EXPECT_EQ(requests()->size() - kMaxSocketsPerGroup, completion_count());
459 // Make sure that pending requests get serviced after active requests fail.
460 TEST_F(WebSocketTransportClientSocketPoolTest,
461 FailingActiveRequestWithPendingRequests) {
462 client_socket_factory_.set_default_client_socket_type(
463 MockTransportClientSocketFactory::MOCK_PENDING_FAILING_CLIENT_SOCKET);
465 const int kNumRequests = 2 * kMaxSocketsPerGroup + 1;
466 ASSERT_LE(kNumRequests, kMaxSockets); // Otherwise the test will hang.
468 // Queue up all the requests
469 for (int i = 0; i < kNumRequests; i++)
470 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
472 for (int i = 0; i < kNumRequests; i++)
473 EXPECT_EQ(ERR_CONNECTION_FAILED, request(i)->WaitForResult());
476 // The lock on the endpoint is released when a ClientSocketHandle is reset.
477 TEST_F(WebSocketTransportClientSocketPoolTest, LockReleasedOnHandleReset) {
478 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
479 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
480 EXPECT_EQ(OK, request(0)->WaitForResult());
481 EXPECT_FALSE(request(1)->handle()->is_initialized());
482 request(0)->handle()->Reset();
483 RunUntilIdle();
484 EXPECT_TRUE(request(1)->handle()->is_initialized());
487 // The lock on the endpoint is released when a ClientSocketHandle is deleted.
488 TEST_F(WebSocketTransportClientSocketPoolTest, LockReleasedOnHandleDelete) {
489 TestCompletionCallback callback;
490 scoped_ptr<ClientSocketHandle> handle(new ClientSocketHandle);
491 int rv = handle->Init(
492 "a", params_, LOW, callback.callback(), &pool_, BoundNetLog());
493 EXPECT_EQ(ERR_IO_PENDING, rv);
495 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
496 EXPECT_EQ(OK, callback.WaitForResult());
497 EXPECT_FALSE(request(0)->handle()->is_initialized());
498 handle.reset();
499 RunUntilIdle();
500 EXPECT_TRUE(request(0)->handle()->is_initialized());
503 // A new connection is performed when the lock on the previous connection is
504 // explicitly released.
505 TEST_F(WebSocketTransportClientSocketPoolTest,
506 ConnectionProceedsOnExplicitRelease) {
507 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
508 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
509 EXPECT_EQ(OK, request(0)->WaitForResult());
510 EXPECT_FALSE(request(1)->handle()->is_initialized());
511 WebSocketTransportClientSocketPool::UnlockEndpoint(request(0)->handle());
512 RunUntilIdle();
513 EXPECT_TRUE(request(1)->handle()->is_initialized());
516 // A connection which is cancelled before completion does not block subsequent
517 // connections.
518 TEST_F(WebSocketTransportClientSocketPoolTest,
519 CancelDuringConnectionReleasesLock) {
520 MockTransportClientSocketFactory::ClientSocketType case_types[] = {
521 MockTransportClientSocketFactory::MOCK_STALLED_CLIENT_SOCKET,
522 MockTransportClientSocketFactory::MOCK_PENDING_CLIENT_SOCKET};
524 client_socket_factory_.set_client_socket_types(case_types,
525 arraysize(case_types));
527 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
528 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
529 RunUntilIdle();
530 pool_.CancelRequest("a", request(0)->handle());
531 EXPECT_EQ(OK, request(1)->WaitForResult());
534 // Test the case of the IPv6 address stalling, and falling back to the IPv4
535 // socket which finishes first.
536 TEST_F(WebSocketTransportClientSocketPoolTest,
537 IPv6FallbackSocketIPv4FinishesFirst) {
538 WebSocketTransportClientSocketPool pool(kMaxSockets,
539 kMaxSocketsPerGroup,
540 host_resolver_.get(),
541 &client_socket_factory_,
542 NULL);
544 MockTransportClientSocketFactory::ClientSocketType case_types[] = {
545 // This is the IPv6 socket.
546 MockTransportClientSocketFactory::MOCK_STALLED_CLIENT_SOCKET,
547 // This is the IPv4 socket.
548 MockTransportClientSocketFactory::MOCK_PENDING_CLIENT_SOCKET};
550 client_socket_factory_.set_client_socket_types(case_types, 2);
552 // Resolve an AddressList with an IPv6 address first and then an IPv4 address.
553 host_resolver_->rules()->AddIPLiteralRule(
554 "*", "2:abcd::3:4:ff,2.2.2.2", std::string());
556 TestCompletionCallback callback;
557 ClientSocketHandle handle;
558 int rv =
559 handle.Init("a", params_, LOW, callback.callback(), &pool, BoundNetLog());
560 EXPECT_EQ(ERR_IO_PENDING, rv);
561 EXPECT_FALSE(handle.is_initialized());
562 EXPECT_FALSE(handle.socket());
564 EXPECT_EQ(OK, callback.WaitForResult());
565 EXPECT_TRUE(handle.is_initialized());
566 EXPECT_TRUE(handle.socket());
567 IPEndPoint endpoint;
568 handle.socket()->GetLocalAddress(&endpoint);
569 EXPECT_EQ(kIPv4AddressSize, endpoint.address().size());
570 EXPECT_EQ(2, client_socket_factory_.allocation_count());
573 // Test the case of the IPv6 address being slow, thus falling back to trying to
574 // connect to the IPv4 address, but having the connect to the IPv6 address
575 // finish first.
576 TEST_F(WebSocketTransportClientSocketPoolTest,
577 IPv6FallbackSocketIPv6FinishesFirst) {
578 WebSocketTransportClientSocketPool pool(kMaxSockets,
579 kMaxSocketsPerGroup,
580 host_resolver_.get(),
581 &client_socket_factory_,
582 NULL);
584 MockTransportClientSocketFactory::ClientSocketType case_types[] = {
585 // This is the IPv6 socket.
586 MockTransportClientSocketFactory::MOCK_DELAYED_CLIENT_SOCKET,
587 // This is the IPv4 socket.
588 MockTransportClientSocketFactory::MOCK_STALLED_CLIENT_SOCKET};
590 client_socket_factory_.set_client_socket_types(case_types, 2);
591 client_socket_factory_.set_delay(base::TimeDelta::FromMilliseconds(
592 TransportConnectJobHelper::kIPv6FallbackTimerInMs + 50));
594 // Resolve an AddressList with an IPv6 address first and then an IPv4 address.
595 host_resolver_->rules()->AddIPLiteralRule(
596 "*", "2:abcd::3:4:ff,2.2.2.2", std::string());
598 TestCompletionCallback callback;
599 ClientSocketHandle handle;
600 int rv =
601 handle.Init("a", params_, LOW, callback.callback(), &pool, BoundNetLog());
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());
609 IPEndPoint endpoint;
610 handle.socket()->GetLocalAddress(&endpoint);
611 EXPECT_EQ(kIPv6AddressSize, endpoint.address().size());
612 EXPECT_EQ(2, client_socket_factory_.allocation_count());
615 TEST_F(WebSocketTransportClientSocketPoolTest,
616 IPv6NoIPv4AddressesToFallbackTo) {
617 WebSocketTransportClientSocketPool pool(kMaxSockets,
618 kMaxSocketsPerGroup,
619 host_resolver_.get(),
620 &client_socket_factory_,
621 NULL);
623 client_socket_factory_.set_default_client_socket_type(
624 MockTransportClientSocketFactory::MOCK_DELAYED_CLIENT_SOCKET);
626 // Resolve an AddressList with only IPv6 addresses.
627 host_resolver_->rules()->AddIPLiteralRule(
628 "*", "2:abcd::3:4:ff,3:abcd::3:4:ff", std::string());
630 TestCompletionCallback callback;
631 ClientSocketHandle handle;
632 int rv =
633 handle.Init("a", params_, LOW, callback.callback(), &pool, BoundNetLog());
634 EXPECT_EQ(ERR_IO_PENDING, rv);
635 EXPECT_FALSE(handle.is_initialized());
636 EXPECT_FALSE(handle.socket());
638 EXPECT_EQ(OK, callback.WaitForResult());
639 EXPECT_TRUE(handle.is_initialized());
640 EXPECT_TRUE(handle.socket());
641 IPEndPoint endpoint;
642 handle.socket()->GetLocalAddress(&endpoint);
643 EXPECT_EQ(kIPv6AddressSize, endpoint.address().size());
644 EXPECT_EQ(1, client_socket_factory_.allocation_count());
647 TEST_F(WebSocketTransportClientSocketPoolTest, IPv4HasNoFallback) {
648 WebSocketTransportClientSocketPool pool(kMaxSockets,
649 kMaxSocketsPerGroup,
650 host_resolver_.get(),
651 &client_socket_factory_,
652 NULL);
654 client_socket_factory_.set_default_client_socket_type(
655 MockTransportClientSocketFactory::MOCK_DELAYED_CLIENT_SOCKET);
657 // Resolve an AddressList with only IPv4 addresses.
658 host_resolver_->rules()->AddIPLiteralRule("*", "1.1.1.1", std::string());
660 TestCompletionCallback callback;
661 ClientSocketHandle handle;
662 int rv =
663 handle.Init("a", params_, LOW, callback.callback(), &pool, BoundNetLog());
664 EXPECT_EQ(ERR_IO_PENDING, rv);
665 EXPECT_FALSE(handle.is_initialized());
666 EXPECT_FALSE(handle.socket());
668 EXPECT_EQ(OK, callback.WaitForResult());
669 EXPECT_TRUE(handle.is_initialized());
670 EXPECT_TRUE(handle.socket());
671 IPEndPoint endpoint;
672 handle.socket()->GetLocalAddress(&endpoint);
673 EXPECT_EQ(kIPv4AddressSize, endpoint.address().size());
674 EXPECT_EQ(1, client_socket_factory_.allocation_count());
677 // If all IPv6 addresses fail to connect synchronously, then IPv4 connections
678 // proceeed immediately.
679 TEST_F(WebSocketTransportClientSocketPoolTest, IPv6InstantFail) {
680 WebSocketTransportClientSocketPool pool(kMaxSockets,
681 kMaxSocketsPerGroup,
682 host_resolver_.get(),
683 &client_socket_factory_,
684 NULL);
686 MockTransportClientSocketFactory::ClientSocketType case_types[] = {
687 // First IPv6 socket.
688 MockTransportClientSocketFactory::MOCK_FAILING_CLIENT_SOCKET,
689 // Second IPv6 socket.
690 MockTransportClientSocketFactory::MOCK_FAILING_CLIENT_SOCKET,
691 // This is the IPv4 socket.
692 MockTransportClientSocketFactory::MOCK_CLIENT_SOCKET};
694 client_socket_factory_.set_client_socket_types(case_types,
695 arraysize(case_types));
697 // Resolve an AddressList with two IPv6 addresses and then an IPv4 address.
698 host_resolver_->rules()->AddIPLiteralRule(
699 "*", "2:abcd::3:4:ff,2:abcd::3:5:ff,2.2.2.2", std::string());
700 host_resolver_->set_synchronous_mode(true);
701 TestCompletionCallback callback;
702 ClientSocketHandle handle;
703 int rv =
704 handle.Init("a", params_, LOW, callback.callback(), &pool, BoundNetLog());
705 EXPECT_EQ(OK, rv);
706 ASSERT_TRUE(handle.socket());
708 IPEndPoint endpoint;
709 handle.socket()->GetPeerAddress(&endpoint);
710 EXPECT_EQ("2.2.2.2", endpoint.ToStringWithoutPort());
713 // If all IPv6 addresses fail before the IPv4 fallback timeout, then the IPv4
714 // connections proceed immediately.
715 TEST_F(WebSocketTransportClientSocketPoolTest, IPv6RapidFail) {
716 WebSocketTransportClientSocketPool pool(kMaxSockets,
717 kMaxSocketsPerGroup,
718 host_resolver_.get(),
719 &client_socket_factory_,
720 NULL);
722 MockTransportClientSocketFactory::ClientSocketType case_types[] = {
723 // First IPv6 socket.
724 MockTransportClientSocketFactory::MOCK_PENDING_FAILING_CLIENT_SOCKET,
725 // Second IPv6 socket.
726 MockTransportClientSocketFactory::MOCK_PENDING_FAILING_CLIENT_SOCKET,
727 // This is the IPv4 socket.
728 MockTransportClientSocketFactory::MOCK_CLIENT_SOCKET};
730 client_socket_factory_.set_client_socket_types(case_types,
731 arraysize(case_types));
733 // Resolve an AddressList with two IPv6 addresses and then an IPv4 address.
734 host_resolver_->rules()->AddIPLiteralRule(
735 "*", "2:abcd::3:4:ff,2:abcd::3:5:ff,2.2.2.2", std::string());
737 TestCompletionCallback callback;
738 ClientSocketHandle handle;
739 int rv =
740 handle.Init("a", params_, LOW, callback.callback(), &pool, BoundNetLog());
741 EXPECT_EQ(ERR_IO_PENDING, rv);
742 EXPECT_FALSE(handle.socket());
744 base::TimeTicks start(base::TimeTicks::Now());
745 EXPECT_EQ(OK, callback.WaitForResult());
746 EXPECT_LT(base::TimeTicks::Now() - start,
747 base::TimeDelta::FromMilliseconds(
748 TransportConnectJobHelper::kIPv6FallbackTimerInMs));
749 ASSERT_TRUE(handle.socket());
751 IPEndPoint endpoint;
752 handle.socket()->GetPeerAddress(&endpoint);
753 EXPECT_EQ("2.2.2.2", endpoint.ToStringWithoutPort());
756 // If two sockets connect successfully, the one which connected first wins (this
757 // can only happen if the sockets are different types, since sockets of the same
758 // type do not race).
759 TEST_F(WebSocketTransportClientSocketPoolTest, FirstSuccessWins) {
760 WebSocketTransportClientSocketPool pool(kMaxSockets,
761 kMaxSocketsPerGroup,
762 host_resolver_.get(),
763 &client_socket_factory_,
764 NULL);
766 client_socket_factory_.set_default_client_socket_type(
767 MockTransportClientSocketFactory::MOCK_TRIGGERABLE_CLIENT_SOCKET);
769 // Resolve an AddressList with an IPv6 addresses and an IPv4 address.
770 host_resolver_->rules()->AddIPLiteralRule(
771 "*", "2:abcd::3:4:ff,2.2.2.2", std::string());
773 TestCompletionCallback callback;
774 ClientSocketHandle handle;
775 int rv =
776 handle.Init("a", params_, LOW, callback.callback(), &pool, BoundNetLog());
777 EXPECT_EQ(ERR_IO_PENDING, rv);
778 ASSERT_FALSE(handle.socket());
780 base::Closure ipv6_connect_trigger =
781 client_socket_factory_.WaitForTriggerableSocketCreation();
782 base::Closure ipv4_connect_trigger =
783 client_socket_factory_.WaitForTriggerableSocketCreation();
785 ipv4_connect_trigger.Run();
786 ipv6_connect_trigger.Run();
788 EXPECT_EQ(OK, callback.WaitForResult());
789 ASSERT_TRUE(handle.socket());
791 IPEndPoint endpoint;
792 handle.socket()->GetPeerAddress(&endpoint);
793 EXPECT_EQ("2.2.2.2", endpoint.ToStringWithoutPort());
796 // We should not report failure until all connections have failed.
797 TEST_F(WebSocketTransportClientSocketPoolTest, LastFailureWins) {
798 WebSocketTransportClientSocketPool pool(kMaxSockets,
799 kMaxSocketsPerGroup,
800 host_resolver_.get(),
801 &client_socket_factory_,
802 NULL);
804 client_socket_factory_.set_default_client_socket_type(
805 MockTransportClientSocketFactory::MOCK_DELAYED_FAILING_CLIENT_SOCKET);
806 base::TimeDelta delay = base::TimeDelta::FromMilliseconds(
807 TransportConnectJobHelper::kIPv6FallbackTimerInMs / 3);
808 client_socket_factory_.set_delay(delay);
810 // Resolve an AddressList with 4 IPv6 addresses and 2 IPv4 addresses.
811 host_resolver_->rules()->AddIPLiteralRule("*",
812 "1:abcd::3:4:ff,2:abcd::3:4:ff,"
813 "3:abcd::3:4:ff,4:abcd::3:4:ff,"
814 "1.1.1.1,2.2.2.2",
815 std::string());
817 // Expected order of events:
818 // After 100ms: Connect to 1:abcd::3:4:ff times out
819 // After 200ms: Connect to 2:abcd::3:4:ff times out
820 // After 300ms: Connect to 3:abcd::3:4:ff times out, IPv4 fallback starts
821 // After 400ms: Connect to 4:abcd::3:4:ff and 1.1.1.1 time out
822 // After 500ms: Connect to 2.2.2.2 times out
824 TestCompletionCallback callback;
825 ClientSocketHandle handle;
826 base::TimeTicks start(base::TimeTicks::Now());
827 int rv =
828 handle.Init("a", params_, LOW, callback.callback(), &pool, BoundNetLog());
829 EXPECT_EQ(ERR_IO_PENDING, rv);
831 EXPECT_EQ(ERR_CONNECTION_FAILED, callback.WaitForResult());
833 EXPECT_GE(base::TimeTicks::Now() - start, delay * 5);
836 // Global timeout for all connects applies. This test is disabled by default
837 // because it takes 4 minutes. Run with --gtest_also_run_disabled_tests if you
838 // want to run it.
839 TEST_F(WebSocketTransportClientSocketPoolTest, DISABLED_OverallTimeoutApplies) {
840 WebSocketTransportClientSocketPool pool(kMaxSockets,
841 kMaxSocketsPerGroup,
842 host_resolver_.get(),
843 &client_socket_factory_,
844 NULL);
845 const base::TimeDelta connect_job_timeout = pool.ConnectionTimeout();
847 client_socket_factory_.set_default_client_socket_type(
848 MockTransportClientSocketFactory::MOCK_DELAYED_FAILING_CLIENT_SOCKET);
849 client_socket_factory_.set_delay(base::TimeDelta::FromSeconds(1) +
850 connect_job_timeout / 6);
852 // Resolve an AddressList with 6 IPv6 addresses and 6 IPv4 addresses.
853 host_resolver_->rules()->AddIPLiteralRule("*",
854 "1:abcd::3:4:ff,2:abcd::3:4:ff,"
855 "3:abcd::3:4:ff,4:abcd::3:4:ff,"
856 "5:abcd::3:4:ff,6:abcd::3:4:ff,"
857 "1.1.1.1,2.2.2.2,3.3.3.3,"
858 "4.4.4.4,5.5.5.5,6.6.6.6",
859 std::string());
861 TestCompletionCallback callback;
862 ClientSocketHandle handle;
864 int rv =
865 handle.Init("a", params_, LOW, callback.callback(), &pool, BoundNetLog());
866 EXPECT_EQ(ERR_IO_PENDING, rv);
868 EXPECT_EQ(ERR_TIMED_OUT, callback.WaitForResult());
871 TEST_F(WebSocketTransportClientSocketPoolTest, MaxSocketsEnforced) {
872 host_resolver_->set_synchronous_mode(true);
873 for (int i = 0; i < kMaxSockets; ++i) {
874 ASSERT_EQ(OK, StartRequest("a", kDefaultPriority));
875 WebSocketTransportClientSocketPool::UnlockEndpoint(request(i)->handle());
876 RunUntilIdle();
878 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
881 TEST_F(WebSocketTransportClientSocketPoolTest, MaxSocketsEnforcedWhenPending) {
882 for (int i = 0; i < kMaxSockets + 1; ++i) {
883 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
885 // Now there are 32 sockets waiting to connect, and one stalled.
886 for (int i = 0; i < kMaxSockets; ++i) {
887 RunUntilIdle();
888 EXPECT_TRUE(request(i)->handle()->is_initialized());
889 EXPECT_TRUE(request(i)->handle()->socket());
890 WebSocketTransportClientSocketPool::UnlockEndpoint(request(i)->handle());
892 // Now there are 32 sockets connected, and one stalled.
893 RunUntilIdle();
894 EXPECT_FALSE(request(kMaxSockets)->handle()->is_initialized());
895 EXPECT_FALSE(request(kMaxSockets)->handle()->socket());
898 TEST_F(WebSocketTransportClientSocketPoolTest, StalledSocketReleased) {
899 host_resolver_->set_synchronous_mode(true);
900 for (int i = 0; i < kMaxSockets; ++i) {
901 ASSERT_EQ(OK, StartRequest("a", kDefaultPriority));
902 WebSocketTransportClientSocketPool::UnlockEndpoint(request(i)->handle());
903 RunUntilIdle();
906 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
907 ReleaseOneConnection(ClientSocketPoolTest::NO_KEEP_ALIVE);
908 EXPECT_TRUE(request(kMaxSockets)->handle()->is_initialized());
909 EXPECT_TRUE(request(kMaxSockets)->handle()->socket());
912 TEST_F(WebSocketTransportClientSocketPoolTest, IsStalledTrueWhenStalled) {
913 for (int i = 0; i < kMaxSockets + 1; ++i) {
914 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
916 EXPECT_EQ(OK, request(0)->WaitForResult());
917 EXPECT_TRUE(pool_.IsStalled());
920 TEST_F(WebSocketTransportClientSocketPoolTest,
921 CancellingPendingSocketUnstallsStalledSocket) {
922 for (int i = 0; i < kMaxSockets + 1; ++i) {
923 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
925 EXPECT_EQ(OK, request(0)->WaitForResult());
926 request(1)->handle()->Reset();
927 RunUntilIdle();
928 EXPECT_FALSE(pool_.IsStalled());
931 TEST_F(WebSocketTransportClientSocketPoolTest,
932 LoadStateOfStalledSocketIsWaitingForAvailableSocket) {
933 for (int i = 0; i < kMaxSockets + 1; ++i) {
934 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
936 EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET,
937 pool_.GetLoadState("a", request(kMaxSockets)->handle()));
940 TEST_F(WebSocketTransportClientSocketPoolTest,
941 CancellingStalledSocketUnstallsPool) {
942 for (int i = 0; i < kMaxSockets + 1; ++i) {
943 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
945 request(kMaxSockets)->handle()->Reset();
946 RunUntilIdle();
947 EXPECT_FALSE(pool_.IsStalled());
950 TEST_F(WebSocketTransportClientSocketPoolTest,
951 FlushWithErrorFlushesPendingConnections) {
952 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
953 pool_.FlushWithError(ERR_FAILED);
954 EXPECT_EQ(ERR_FAILED, request(0)->WaitForResult());
957 TEST_F(WebSocketTransportClientSocketPoolTest,
958 FlushWithErrorFlushesStalledConnections) {
959 for (int i = 0; i < kMaxSockets + 1; ++i) {
960 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
962 pool_.FlushWithError(ERR_FAILED);
963 EXPECT_EQ(ERR_FAILED, request(kMaxSockets)->WaitForResult());
966 TEST_F(WebSocketTransportClientSocketPoolTest,
967 AfterFlushWithErrorCanMakeNewConnections) {
968 for (int i = 0; i < kMaxSockets + 1; ++i) {
969 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
971 pool_.FlushWithError(ERR_FAILED);
972 host_resolver_->set_synchronous_mode(true);
973 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority));
976 // Deleting pending connections can release the lock on the endpoint, which can
977 // in principle lead to other pending connections succeeding. However, when we
978 // call FlushWithError(), everything should fail.
979 TEST_F(WebSocketTransportClientSocketPoolTest,
980 FlushWithErrorDoesNotCauseSuccessfulConnections) {
981 host_resolver_->set_synchronous_mode(true);
982 MockTransportClientSocketFactory::ClientSocketType first_type[] = {
983 // First socket
984 MockTransportClientSocketFactory::MOCK_PENDING_CLIENT_SOCKET
986 client_socket_factory_.set_client_socket_types(first_type,
987 arraysize(first_type));
988 // The rest of the sockets will connect synchronously.
989 client_socket_factory_.set_default_client_socket_type(
990 MockTransportClientSocketFactory::MOCK_CLIENT_SOCKET);
991 for (int i = 0; i < kMaxSockets; ++i) {
992 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
994 // Now we have one socket in STATE_TRANSPORT_CONNECT and the rest in
995 // STATE_OBTAIN_LOCK. If any of the sockets in STATE_OBTAIN_LOCK is given the
996 // lock, they will synchronously connect.
997 pool_.FlushWithError(ERR_FAILED);
998 for (int i = 0; i < kMaxSockets; ++i) {
999 EXPECT_EQ(ERR_FAILED, request(i)->WaitForResult());
1003 // This is a regression test for the first attempted fix for
1004 // FlushWithErrorDoesNotCauseSuccessfulConnections. Because a ConnectJob can
1005 // have both IPv4 and IPv6 subjobs, it can be both connecting and waiting for
1006 // the lock at the same time.
1007 TEST_F(WebSocketTransportClientSocketPoolTest,
1008 FlushWithErrorDoesNotCauseSuccessfulConnectionsMultipleAddressTypes) {
1009 host_resolver_->set_synchronous_mode(true);
1010 // The first |kMaxSockets| sockets to connect will be IPv6. Then we will have
1011 // one IPv4.
1012 std::vector<MockTransportClientSocketFactory::ClientSocketType> socket_types(
1013 kMaxSockets + 1,
1014 MockTransportClientSocketFactory::MOCK_STALLED_CLIENT_SOCKET);
1015 client_socket_factory_.set_client_socket_types(&socket_types[0],
1016 socket_types.size());
1017 // The rest of the sockets will connect synchronously.
1018 client_socket_factory_.set_default_client_socket_type(
1019 MockTransportClientSocketFactory::MOCK_CLIENT_SOCKET);
1020 for (int i = 0; i < kMaxSockets; ++i) {
1021 host_resolver_->rules()->ClearRules();
1022 // Each connect job has a different IPv6 address but the same IPv4 address.
1023 // So the IPv6 connections happen in parallel but the IPv4 ones are
1024 // serialised.
1025 host_resolver_->rules()->AddIPLiteralRule("*",
1026 base::StringPrintf(
1027 "%x:abcd::3:4:ff,"
1028 "1.1.1.1",
1029 i + 1),
1030 std::string());
1031 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
1033 // Now we have |kMaxSockets| IPv6 sockets stalled in connect. No IPv4 sockets
1034 // are started yet.
1035 RunLoopForTimePeriod(base::TimeDelta::FromMilliseconds(
1036 TransportConnectJobHelper::kIPv6FallbackTimerInMs));
1037 // Now we have |kMaxSockets| IPv6 sockets and one IPv4 socket stalled in
1038 // connect, and |kMaxSockets - 1| IPv4 sockets waiting for the endpoint lock.
1039 pool_.FlushWithError(ERR_FAILED);
1040 for (int i = 0; i < kMaxSockets; ++i) {
1041 EXPECT_EQ(ERR_FAILED, request(i)->WaitForResult());
1045 // Sockets that have had ownership transferred to a ClientSocketHandle should
1046 // not be affected by FlushWithError.
1047 TEST_F(WebSocketTransportClientSocketPoolTest,
1048 FlushWithErrorDoesNotAffectHandedOutSockets) {
1049 host_resolver_->set_synchronous_mode(true);
1050 MockTransportClientSocketFactory::ClientSocketType socket_types[] = {
1051 MockTransportClientSocketFactory::MOCK_CLIENT_SOCKET,
1052 MockTransportClientSocketFactory::MOCK_STALLED_CLIENT_SOCKET};
1053 client_socket_factory_.set_client_socket_types(socket_types,
1054 arraysize(socket_types));
1055 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority));
1056 // Socket has been "handed out".
1057 EXPECT_TRUE(request(0)->handle()->socket());
1059 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
1060 // Now we have one socket handed out, and one pending.
1061 pool_.FlushWithError(ERR_FAILED);
1062 EXPECT_EQ(ERR_FAILED, request(1)->WaitForResult());
1063 // Socket owned by ClientSocketHandle is unaffected:
1064 EXPECT_TRUE(request(0)->handle()->socket());
1065 // Return it to the pool (which deletes it).
1066 request(0)->handle()->Reset();
1069 // Sockets should not be leaked if CancelRequest() is called in between
1070 // SetSocket() being called on the ClientSocketHandle and InvokeUserCallback().
1071 TEST_F(WebSocketTransportClientSocketPoolTest, CancelRequestReclaimsSockets) {
1072 host_resolver_->set_synchronous_mode(true);
1073 MockTransportClientSocketFactory::ClientSocketType socket_types[] = {
1074 MockTransportClientSocketFactory::MOCK_TRIGGERABLE_CLIENT_SOCKET,
1075 MockTransportClientSocketFactory::MOCK_CLIENT_SOCKET};
1077 client_socket_factory_.set_client_socket_types(socket_types,
1078 arraysize(socket_types));
1080 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
1082 base::Closure connect_trigger =
1083 client_socket_factory_.WaitForTriggerableSocketCreation();
1085 connect_trigger.Run(); // Calls InvokeUserCallbackLater()
1087 request(0)->handle()->Reset(); // calls CancelRequest()
1089 RunUntilIdle();
1090 // We should now be able to create a new connection without blocking on the
1091 // endpoint lock.
1092 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority));
1095 // A handshake completing and then the WebSocket closing should only release one
1096 // Endpoint, not two.
1097 TEST_F(WebSocketTransportClientSocketPoolTest, EndpointLockIsOnlyReleasedOnce) {
1098 host_resolver_->set_synchronous_mode(true);
1099 ASSERT_EQ(OK, StartRequest("a", kDefaultPriority));
1100 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
1101 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
1102 // First socket completes handshake.
1103 WebSocketTransportClientSocketPool::UnlockEndpoint(request(0)->handle());
1104 RunUntilIdle();
1105 // First socket is closed.
1106 request(0)->handle()->Reset();
1107 // Second socket should have been released.
1108 EXPECT_EQ(OK, request(1)->WaitForResult());
1109 // Third socket should still be waiting for endpoint.
1110 ASSERT_FALSE(request(2)->handle()->is_initialized());
1111 EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET,
1112 request(2)->handle()->GetLoadState());
1115 } // namespace
1117 } // namespace net