TestGuestViewManager: Remove unnecessary includes
[chromium-blink-merge.git] / net / socket / websocket_transport_client_socket_pool_unittest.cc
blob2df660633168450d7c601f566b0eb9452831f3dc
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 class RequestSocketCallback : public TestCompletionCallbackBase {
378 public:
379 RequestSocketCallback(ClientSocketHandle* handle,
380 WebSocketTransportClientSocketPool* pool)
381 : handle_(handle),
382 pool_(pool),
383 within_callback_(false),
384 callback_(base::Bind(&RequestSocketCallback::OnComplete,
385 base::Unretained(this))) {}
387 ~RequestSocketCallback() override {}
389 const CompletionCallback& callback() const { return callback_; }
391 private:
392 void OnComplete(int result) {
393 SetResult(result);
394 ASSERT_EQ(OK, result);
396 if (!within_callback_) {
397 // Don't allow reuse of the socket. Disconnect it and then release it and
398 // run through the MessageLoop once to get it completely released.
399 handle_->socket()->Disconnect();
400 handle_->Reset();
402 base::MessageLoop::ScopedNestableTaskAllower allow(
403 base::MessageLoop::current());
404 base::MessageLoop::current()->RunUntilIdle();
406 within_callback_ = true;
407 scoped_refptr<TransportSocketParams> dest(
408 new TransportSocketParams(
409 HostPortPair("www.google.com", 80),
410 false,
411 false,
412 OnHostResolutionCallback(),
413 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DEFAULT));
414 int rv =
415 handle_->Init("a", dest, LOWEST, callback(), pool_, BoundNetLog());
416 EXPECT_EQ(OK, rv);
420 ClientSocketHandle* const handle_;
421 WebSocketTransportClientSocketPool* const pool_;
422 bool within_callback_;
423 CompletionCallback callback_;
425 DISALLOW_COPY_AND_ASSIGN(RequestSocketCallback);
428 TEST_F(WebSocketTransportClientSocketPoolTest, RequestTwice) {
429 ClientSocketHandle handle;
430 RequestSocketCallback callback(&handle, &pool_);
431 scoped_refptr<TransportSocketParams> dest(
432 new TransportSocketParams(
433 HostPortPair("www.google.com", 80),
434 false,
435 false,
436 OnHostResolutionCallback(),
437 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DEFAULT));
438 int rv = handle.Init(
439 "a", dest, LOWEST, callback.callback(), &pool_, BoundNetLog());
440 ASSERT_EQ(ERR_IO_PENDING, rv);
442 // The callback is going to request "www.google.com". We want it to complete
443 // synchronously this time.
444 host_resolver_->set_synchronous_mode(true);
446 EXPECT_EQ(OK, callback.WaitForResult());
448 handle.Reset();
451 // Make sure that pending requests get serviced after active requests get
452 // cancelled.
453 TEST_F(WebSocketTransportClientSocketPoolTest,
454 CancelActiveRequestWithPendingRequests) {
455 client_socket_factory_.set_default_client_socket_type(
456 MockTransportClientSocketFactory::MOCK_PENDING_CLIENT_SOCKET);
458 // Queue up all the requests
459 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
460 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
461 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
462 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
463 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
464 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
465 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
466 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
467 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
469 // Now, kMaxSocketsPerGroup requests should be active. Let's cancel them.
470 ASSERT_LE(kMaxSocketsPerGroup, static_cast<int>(requests()->size()));
471 for (int i = 0; i < kMaxSocketsPerGroup; i++)
472 request(i)->handle()->Reset();
474 // Let's wait for the rest to complete now.
475 for (size_t i = kMaxSocketsPerGroup; i < requests()->size(); ++i) {
476 EXPECT_EQ(OK, request(i)->WaitForResult());
477 request(i)->handle()->Reset();
480 EXPECT_EQ(requests()->size() - kMaxSocketsPerGroup, completion_count());
483 // Make sure that pending requests get serviced after active requests fail.
484 TEST_F(WebSocketTransportClientSocketPoolTest,
485 FailingActiveRequestWithPendingRequests) {
486 client_socket_factory_.set_default_client_socket_type(
487 MockTransportClientSocketFactory::MOCK_PENDING_FAILING_CLIENT_SOCKET);
489 const int kNumRequests = 2 * kMaxSocketsPerGroup + 1;
490 ASSERT_LE(kNumRequests, kMaxSockets); // Otherwise the test will hang.
492 // Queue up all the requests
493 for (int i = 0; i < kNumRequests; i++)
494 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
496 for (int i = 0; i < kNumRequests; i++)
497 EXPECT_EQ(ERR_CONNECTION_FAILED, request(i)->WaitForResult());
500 // The lock on the endpoint is released when a ClientSocketHandle is reset.
501 TEST_F(WebSocketTransportClientSocketPoolTest, LockReleasedOnHandleReset) {
502 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
503 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
504 EXPECT_EQ(OK, request(0)->WaitForResult());
505 EXPECT_FALSE(request(1)->handle()->is_initialized());
506 request(0)->handle()->Reset();
507 RunUntilIdle();
508 EXPECT_TRUE(request(1)->handle()->is_initialized());
511 // The lock on the endpoint is released when a ClientSocketHandle is deleted.
512 TEST_F(WebSocketTransportClientSocketPoolTest, LockReleasedOnHandleDelete) {
513 TestCompletionCallback callback;
514 scoped_ptr<ClientSocketHandle> handle(new ClientSocketHandle);
515 int rv = handle->Init(
516 "a", params_, LOW, callback.callback(), &pool_, BoundNetLog());
517 EXPECT_EQ(ERR_IO_PENDING, rv);
519 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
520 EXPECT_EQ(OK, callback.WaitForResult());
521 EXPECT_FALSE(request(0)->handle()->is_initialized());
522 handle.reset();
523 RunUntilIdle();
524 EXPECT_TRUE(request(0)->handle()->is_initialized());
527 // A new connection is performed when the lock on the previous connection is
528 // explicitly released.
529 TEST_F(WebSocketTransportClientSocketPoolTest,
530 ConnectionProceedsOnExplicitRelease) {
531 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
532 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
533 EXPECT_EQ(OK, request(0)->WaitForResult());
534 EXPECT_FALSE(request(1)->handle()->is_initialized());
535 WebSocketTransportClientSocketPool::UnlockEndpoint(request(0)->handle());
536 RunUntilIdle();
537 EXPECT_TRUE(request(1)->handle()->is_initialized());
540 // A connection which is cancelled before completion does not block subsequent
541 // connections.
542 TEST_F(WebSocketTransportClientSocketPoolTest,
543 CancelDuringConnectionReleasesLock) {
544 MockTransportClientSocketFactory::ClientSocketType case_types[] = {
545 MockTransportClientSocketFactory::MOCK_STALLED_CLIENT_SOCKET,
546 MockTransportClientSocketFactory::MOCK_PENDING_CLIENT_SOCKET};
548 client_socket_factory_.set_client_socket_types(case_types,
549 arraysize(case_types));
551 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
552 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
553 RunUntilIdle();
554 pool_.CancelRequest("a", request(0)->handle());
555 EXPECT_EQ(OK, request(1)->WaitForResult());
558 // Test the case of the IPv6 address stalling, and falling back to the IPv4
559 // socket which finishes first.
560 TEST_F(WebSocketTransportClientSocketPoolTest,
561 IPv6FallbackSocketIPv4FinishesFirst) {
562 WebSocketTransportClientSocketPool pool(kMaxSockets,
563 kMaxSocketsPerGroup,
564 host_resolver_.get(),
565 &client_socket_factory_,
566 NULL);
568 MockTransportClientSocketFactory::ClientSocketType case_types[] = {
569 // This is the IPv6 socket.
570 MockTransportClientSocketFactory::MOCK_STALLED_CLIENT_SOCKET,
571 // This is the IPv4 socket.
572 MockTransportClientSocketFactory::MOCK_PENDING_CLIENT_SOCKET};
574 client_socket_factory_.set_client_socket_types(case_types, 2);
576 // Resolve an AddressList with an IPv6 address first and then an IPv4 address.
577 host_resolver_->rules()->AddIPLiteralRule(
578 "*", "2:abcd::3:4:ff,2.2.2.2", std::string());
580 TestCompletionCallback callback;
581 ClientSocketHandle handle;
582 int rv =
583 handle.Init("a", params_, LOW, callback.callback(), &pool, BoundNetLog());
584 EXPECT_EQ(ERR_IO_PENDING, rv);
585 EXPECT_FALSE(handle.is_initialized());
586 EXPECT_FALSE(handle.socket());
588 EXPECT_EQ(OK, callback.WaitForResult());
589 EXPECT_TRUE(handle.is_initialized());
590 EXPECT_TRUE(handle.socket());
591 IPEndPoint endpoint;
592 handle.socket()->GetLocalAddress(&endpoint);
593 EXPECT_EQ(kIPv4AddressSize, endpoint.address().size());
594 EXPECT_EQ(2, client_socket_factory_.allocation_count());
597 // Test the case of the IPv6 address being slow, thus falling back to trying to
598 // connect to the IPv4 address, but having the connect to the IPv6 address
599 // finish first.
600 TEST_F(WebSocketTransportClientSocketPoolTest,
601 IPv6FallbackSocketIPv6FinishesFirst) {
602 WebSocketTransportClientSocketPool pool(kMaxSockets,
603 kMaxSocketsPerGroup,
604 host_resolver_.get(),
605 &client_socket_factory_,
606 NULL);
608 MockTransportClientSocketFactory::ClientSocketType case_types[] = {
609 // This is the IPv6 socket.
610 MockTransportClientSocketFactory::MOCK_DELAYED_CLIENT_SOCKET,
611 // This is the IPv4 socket.
612 MockTransportClientSocketFactory::MOCK_STALLED_CLIENT_SOCKET};
614 client_socket_factory_.set_client_socket_types(case_types, 2);
615 client_socket_factory_.set_delay(base::TimeDelta::FromMilliseconds(
616 TransportConnectJobHelper::kIPv6FallbackTimerInMs + 50));
618 // Resolve an AddressList with an IPv6 address first and then an IPv4 address.
619 host_resolver_->rules()->AddIPLiteralRule(
620 "*", "2:abcd::3:4:ff,2.2.2.2", std::string());
622 TestCompletionCallback callback;
623 ClientSocketHandle handle;
624 int rv =
625 handle.Init("a", params_, LOW, callback.callback(), &pool, BoundNetLog());
626 EXPECT_EQ(ERR_IO_PENDING, rv);
627 EXPECT_FALSE(handle.is_initialized());
628 EXPECT_FALSE(handle.socket());
630 EXPECT_EQ(OK, callback.WaitForResult());
631 EXPECT_TRUE(handle.is_initialized());
632 EXPECT_TRUE(handle.socket());
633 IPEndPoint endpoint;
634 handle.socket()->GetLocalAddress(&endpoint);
635 EXPECT_EQ(kIPv6AddressSize, endpoint.address().size());
636 EXPECT_EQ(2, client_socket_factory_.allocation_count());
639 TEST_F(WebSocketTransportClientSocketPoolTest,
640 IPv6NoIPv4AddressesToFallbackTo) {
641 WebSocketTransportClientSocketPool pool(kMaxSockets,
642 kMaxSocketsPerGroup,
643 host_resolver_.get(),
644 &client_socket_factory_,
645 NULL);
647 client_socket_factory_.set_default_client_socket_type(
648 MockTransportClientSocketFactory::MOCK_DELAYED_CLIENT_SOCKET);
650 // Resolve an AddressList with only IPv6 addresses.
651 host_resolver_->rules()->AddIPLiteralRule(
652 "*", "2:abcd::3:4:ff,3:abcd::3:4:ff", std::string());
654 TestCompletionCallback callback;
655 ClientSocketHandle handle;
656 int rv =
657 handle.Init("a", params_, LOW, callback.callback(), &pool, BoundNetLog());
658 EXPECT_EQ(ERR_IO_PENDING, rv);
659 EXPECT_FALSE(handle.is_initialized());
660 EXPECT_FALSE(handle.socket());
662 EXPECT_EQ(OK, callback.WaitForResult());
663 EXPECT_TRUE(handle.is_initialized());
664 EXPECT_TRUE(handle.socket());
665 IPEndPoint endpoint;
666 handle.socket()->GetLocalAddress(&endpoint);
667 EXPECT_EQ(kIPv6AddressSize, endpoint.address().size());
668 EXPECT_EQ(1, client_socket_factory_.allocation_count());
671 TEST_F(WebSocketTransportClientSocketPoolTest, IPv4HasNoFallback) {
672 WebSocketTransportClientSocketPool pool(kMaxSockets,
673 kMaxSocketsPerGroup,
674 host_resolver_.get(),
675 &client_socket_factory_,
676 NULL);
678 client_socket_factory_.set_default_client_socket_type(
679 MockTransportClientSocketFactory::MOCK_DELAYED_CLIENT_SOCKET);
681 // Resolve an AddressList with only IPv4 addresses.
682 host_resolver_->rules()->AddIPLiteralRule("*", "1.1.1.1", std::string());
684 TestCompletionCallback callback;
685 ClientSocketHandle handle;
686 int rv =
687 handle.Init("a", params_, LOW, callback.callback(), &pool, BoundNetLog());
688 EXPECT_EQ(ERR_IO_PENDING, rv);
689 EXPECT_FALSE(handle.is_initialized());
690 EXPECT_FALSE(handle.socket());
692 EXPECT_EQ(OK, callback.WaitForResult());
693 EXPECT_TRUE(handle.is_initialized());
694 EXPECT_TRUE(handle.socket());
695 IPEndPoint endpoint;
696 handle.socket()->GetLocalAddress(&endpoint);
697 EXPECT_EQ(kIPv4AddressSize, endpoint.address().size());
698 EXPECT_EQ(1, client_socket_factory_.allocation_count());
701 // If all IPv6 addresses fail to connect synchronously, then IPv4 connections
702 // proceeed immediately.
703 TEST_F(WebSocketTransportClientSocketPoolTest, IPv6InstantFail) {
704 WebSocketTransportClientSocketPool pool(kMaxSockets,
705 kMaxSocketsPerGroup,
706 host_resolver_.get(),
707 &client_socket_factory_,
708 NULL);
710 MockTransportClientSocketFactory::ClientSocketType case_types[] = {
711 // First IPv6 socket.
712 MockTransportClientSocketFactory::MOCK_FAILING_CLIENT_SOCKET,
713 // Second IPv6 socket.
714 MockTransportClientSocketFactory::MOCK_FAILING_CLIENT_SOCKET,
715 // This is the IPv4 socket.
716 MockTransportClientSocketFactory::MOCK_CLIENT_SOCKET};
718 client_socket_factory_.set_client_socket_types(case_types,
719 arraysize(case_types));
721 // Resolve an AddressList with two IPv6 addresses and then an IPv4 address.
722 host_resolver_->rules()->AddIPLiteralRule(
723 "*", "2:abcd::3:4:ff,2:abcd::3:5:ff,2.2.2.2", std::string());
724 host_resolver_->set_synchronous_mode(true);
725 TestCompletionCallback callback;
726 ClientSocketHandle handle;
727 int rv =
728 handle.Init("a", params_, LOW, callback.callback(), &pool, BoundNetLog());
729 EXPECT_EQ(OK, rv);
730 ASSERT_TRUE(handle.socket());
732 IPEndPoint endpoint;
733 handle.socket()->GetPeerAddress(&endpoint);
734 EXPECT_EQ("2.2.2.2", endpoint.ToStringWithoutPort());
737 // If all IPv6 addresses fail before the IPv4 fallback timeout, then the IPv4
738 // connections proceed immediately.
739 TEST_F(WebSocketTransportClientSocketPoolTest, IPv6RapidFail) {
740 WebSocketTransportClientSocketPool pool(kMaxSockets,
741 kMaxSocketsPerGroup,
742 host_resolver_.get(),
743 &client_socket_factory_,
744 NULL);
746 MockTransportClientSocketFactory::ClientSocketType case_types[] = {
747 // First IPv6 socket.
748 MockTransportClientSocketFactory::MOCK_PENDING_FAILING_CLIENT_SOCKET,
749 // Second IPv6 socket.
750 MockTransportClientSocketFactory::MOCK_PENDING_FAILING_CLIENT_SOCKET,
751 // This is the IPv4 socket.
752 MockTransportClientSocketFactory::MOCK_CLIENT_SOCKET};
754 client_socket_factory_.set_client_socket_types(case_types,
755 arraysize(case_types));
757 // Resolve an AddressList with two IPv6 addresses and then an IPv4 address.
758 host_resolver_->rules()->AddIPLiteralRule(
759 "*", "2:abcd::3:4:ff,2:abcd::3:5:ff,2.2.2.2", std::string());
761 TestCompletionCallback callback;
762 ClientSocketHandle handle;
763 int rv =
764 handle.Init("a", params_, LOW, callback.callback(), &pool, BoundNetLog());
765 EXPECT_EQ(ERR_IO_PENDING, rv);
766 EXPECT_FALSE(handle.socket());
768 base::TimeTicks start(base::TimeTicks::Now());
769 EXPECT_EQ(OK, callback.WaitForResult());
770 EXPECT_LT(base::TimeTicks::Now() - start,
771 base::TimeDelta::FromMilliseconds(
772 TransportConnectJobHelper::kIPv6FallbackTimerInMs));
773 ASSERT_TRUE(handle.socket());
775 IPEndPoint endpoint;
776 handle.socket()->GetPeerAddress(&endpoint);
777 EXPECT_EQ("2.2.2.2", endpoint.ToStringWithoutPort());
780 // If two sockets connect successfully, the one which connected first wins (this
781 // can only happen if the sockets are different types, since sockets of the same
782 // type do not race).
783 TEST_F(WebSocketTransportClientSocketPoolTest, FirstSuccessWins) {
784 WebSocketTransportClientSocketPool pool(kMaxSockets,
785 kMaxSocketsPerGroup,
786 host_resolver_.get(),
787 &client_socket_factory_,
788 NULL);
790 client_socket_factory_.set_default_client_socket_type(
791 MockTransportClientSocketFactory::MOCK_TRIGGERABLE_CLIENT_SOCKET);
793 // Resolve an AddressList with an IPv6 addresses and an IPv4 address.
794 host_resolver_->rules()->AddIPLiteralRule(
795 "*", "2:abcd::3:4:ff,2.2.2.2", std::string());
797 TestCompletionCallback callback;
798 ClientSocketHandle handle;
799 int rv =
800 handle.Init("a", params_, LOW, callback.callback(), &pool, BoundNetLog());
801 EXPECT_EQ(ERR_IO_PENDING, rv);
802 ASSERT_FALSE(handle.socket());
804 base::Closure ipv6_connect_trigger =
805 client_socket_factory_.WaitForTriggerableSocketCreation();
806 base::Closure ipv4_connect_trigger =
807 client_socket_factory_.WaitForTriggerableSocketCreation();
809 ipv4_connect_trigger.Run();
810 ipv6_connect_trigger.Run();
812 EXPECT_EQ(OK, callback.WaitForResult());
813 ASSERT_TRUE(handle.socket());
815 IPEndPoint endpoint;
816 handle.socket()->GetPeerAddress(&endpoint);
817 EXPECT_EQ("2.2.2.2", endpoint.ToStringWithoutPort());
820 // We should not report failure until all connections have failed.
821 TEST_F(WebSocketTransportClientSocketPoolTest, LastFailureWins) {
822 WebSocketTransportClientSocketPool pool(kMaxSockets,
823 kMaxSocketsPerGroup,
824 host_resolver_.get(),
825 &client_socket_factory_,
826 NULL);
828 client_socket_factory_.set_default_client_socket_type(
829 MockTransportClientSocketFactory::MOCK_DELAYED_FAILING_CLIENT_SOCKET);
830 base::TimeDelta delay = base::TimeDelta::FromMilliseconds(
831 TransportConnectJobHelper::kIPv6FallbackTimerInMs / 3);
832 client_socket_factory_.set_delay(delay);
834 // Resolve an AddressList with 4 IPv6 addresses and 2 IPv4 addresses.
835 host_resolver_->rules()->AddIPLiteralRule("*",
836 "1:abcd::3:4:ff,2:abcd::3:4:ff,"
837 "3:abcd::3:4:ff,4:abcd::3:4:ff,"
838 "1.1.1.1,2.2.2.2",
839 std::string());
841 // Expected order of events:
842 // After 100ms: Connect to 1:abcd::3:4:ff times out
843 // After 200ms: Connect to 2:abcd::3:4:ff times out
844 // After 300ms: Connect to 3:abcd::3:4:ff times out, IPv4 fallback starts
845 // After 400ms: Connect to 4:abcd::3:4:ff and 1.1.1.1 time out
846 // After 500ms: Connect to 2.2.2.2 times out
848 TestCompletionCallback callback;
849 ClientSocketHandle handle;
850 base::TimeTicks start(base::TimeTicks::Now());
851 int rv =
852 handle.Init("a", params_, LOW, callback.callback(), &pool, BoundNetLog());
853 EXPECT_EQ(ERR_IO_PENDING, rv);
855 EXPECT_EQ(ERR_CONNECTION_FAILED, callback.WaitForResult());
857 EXPECT_GE(base::TimeTicks::Now() - start, delay * 5);
860 // Global timeout for all connects applies. This test is disabled by default
861 // because it takes 4 minutes. Run with --gtest_also_run_disabled_tests if you
862 // want to run it.
863 TEST_F(WebSocketTransportClientSocketPoolTest, DISABLED_OverallTimeoutApplies) {
864 WebSocketTransportClientSocketPool pool(kMaxSockets,
865 kMaxSocketsPerGroup,
866 host_resolver_.get(),
867 &client_socket_factory_,
868 NULL);
869 const base::TimeDelta connect_job_timeout = pool.ConnectionTimeout();
871 client_socket_factory_.set_default_client_socket_type(
872 MockTransportClientSocketFactory::MOCK_DELAYED_FAILING_CLIENT_SOCKET);
873 client_socket_factory_.set_delay(base::TimeDelta::FromSeconds(1) +
874 connect_job_timeout / 6);
876 // Resolve an AddressList with 6 IPv6 addresses and 6 IPv4 addresses.
877 host_resolver_->rules()->AddIPLiteralRule("*",
878 "1:abcd::3:4:ff,2:abcd::3:4:ff,"
879 "3:abcd::3:4:ff,4:abcd::3:4:ff,"
880 "5:abcd::3:4:ff,6:abcd::3:4:ff,"
881 "1.1.1.1,2.2.2.2,3.3.3.3,"
882 "4.4.4.4,5.5.5.5,6.6.6.6",
883 std::string());
885 TestCompletionCallback callback;
886 ClientSocketHandle handle;
888 int rv =
889 handle.Init("a", params_, LOW, callback.callback(), &pool, BoundNetLog());
890 EXPECT_EQ(ERR_IO_PENDING, rv);
892 EXPECT_EQ(ERR_TIMED_OUT, callback.WaitForResult());
895 TEST_F(WebSocketTransportClientSocketPoolTest, MaxSocketsEnforced) {
896 host_resolver_->set_synchronous_mode(true);
897 for (int i = 0; i < kMaxSockets; ++i) {
898 ASSERT_EQ(OK, StartRequest("a", kDefaultPriority));
899 WebSocketTransportClientSocketPool::UnlockEndpoint(request(i)->handle());
900 RunUntilIdle();
902 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
905 TEST_F(WebSocketTransportClientSocketPoolTest, MaxSocketsEnforcedWhenPending) {
906 for (int i = 0; i < kMaxSockets + 1; ++i) {
907 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
909 // Now there are 32 sockets waiting to connect, and one stalled.
910 for (int i = 0; i < kMaxSockets; ++i) {
911 RunUntilIdle();
912 EXPECT_TRUE(request(i)->handle()->is_initialized());
913 EXPECT_TRUE(request(i)->handle()->socket());
914 WebSocketTransportClientSocketPool::UnlockEndpoint(request(i)->handle());
916 // Now there are 32 sockets connected, and one stalled.
917 RunUntilIdle();
918 EXPECT_FALSE(request(kMaxSockets)->handle()->is_initialized());
919 EXPECT_FALSE(request(kMaxSockets)->handle()->socket());
922 TEST_F(WebSocketTransportClientSocketPoolTest, StalledSocketReleased) {
923 host_resolver_->set_synchronous_mode(true);
924 for (int i = 0; i < kMaxSockets; ++i) {
925 ASSERT_EQ(OK, StartRequest("a", kDefaultPriority));
926 WebSocketTransportClientSocketPool::UnlockEndpoint(request(i)->handle());
927 RunUntilIdle();
930 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
931 ReleaseOneConnection(ClientSocketPoolTest::NO_KEEP_ALIVE);
932 EXPECT_TRUE(request(kMaxSockets)->handle()->is_initialized());
933 EXPECT_TRUE(request(kMaxSockets)->handle()->socket());
936 TEST_F(WebSocketTransportClientSocketPoolTest, IsStalledTrueWhenStalled) {
937 for (int i = 0; i < kMaxSockets + 1; ++i) {
938 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
940 EXPECT_EQ(OK, request(0)->WaitForResult());
941 EXPECT_TRUE(pool_.IsStalled());
944 TEST_F(WebSocketTransportClientSocketPoolTest,
945 CancellingPendingSocketUnstallsStalledSocket) {
946 for (int i = 0; i < kMaxSockets + 1; ++i) {
947 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
949 EXPECT_EQ(OK, request(0)->WaitForResult());
950 request(1)->handle()->Reset();
951 RunUntilIdle();
952 EXPECT_FALSE(pool_.IsStalled());
955 TEST_F(WebSocketTransportClientSocketPoolTest,
956 LoadStateOfStalledSocketIsWaitingForAvailableSocket) {
957 for (int i = 0; i < kMaxSockets + 1; ++i) {
958 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
960 EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET,
961 pool_.GetLoadState("a", request(kMaxSockets)->handle()));
964 TEST_F(WebSocketTransportClientSocketPoolTest,
965 CancellingStalledSocketUnstallsPool) {
966 for (int i = 0; i < kMaxSockets + 1; ++i) {
967 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
969 request(kMaxSockets)->handle()->Reset();
970 RunUntilIdle();
971 EXPECT_FALSE(pool_.IsStalled());
974 TEST_F(WebSocketTransportClientSocketPoolTest,
975 FlushWithErrorFlushesPendingConnections) {
976 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
977 pool_.FlushWithError(ERR_FAILED);
978 EXPECT_EQ(ERR_FAILED, request(0)->WaitForResult());
981 TEST_F(WebSocketTransportClientSocketPoolTest,
982 FlushWithErrorFlushesStalledConnections) {
983 for (int i = 0; i < kMaxSockets + 1; ++i) {
984 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
986 pool_.FlushWithError(ERR_FAILED);
987 EXPECT_EQ(ERR_FAILED, request(kMaxSockets)->WaitForResult());
990 TEST_F(WebSocketTransportClientSocketPoolTest,
991 AfterFlushWithErrorCanMakeNewConnections) {
992 for (int i = 0; i < kMaxSockets + 1; ++i) {
993 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
995 pool_.FlushWithError(ERR_FAILED);
996 host_resolver_->set_synchronous_mode(true);
997 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority));
1000 // Deleting pending connections can release the lock on the endpoint, which can
1001 // in principle lead to other pending connections succeeding. However, when we
1002 // call FlushWithError(), everything should fail.
1003 TEST_F(WebSocketTransportClientSocketPoolTest,
1004 FlushWithErrorDoesNotCauseSuccessfulConnections) {
1005 host_resolver_->set_synchronous_mode(true);
1006 MockTransportClientSocketFactory::ClientSocketType first_type[] = {
1007 // First socket
1008 MockTransportClientSocketFactory::MOCK_PENDING_CLIENT_SOCKET
1010 client_socket_factory_.set_client_socket_types(first_type,
1011 arraysize(first_type));
1012 // The rest of the sockets will connect synchronously.
1013 client_socket_factory_.set_default_client_socket_type(
1014 MockTransportClientSocketFactory::MOCK_CLIENT_SOCKET);
1015 for (int i = 0; i < kMaxSockets; ++i) {
1016 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
1018 // Now we have one socket in STATE_TRANSPORT_CONNECT and the rest in
1019 // STATE_OBTAIN_LOCK. If any of the sockets in STATE_OBTAIN_LOCK is given the
1020 // lock, they will synchronously connect.
1021 pool_.FlushWithError(ERR_FAILED);
1022 for (int i = 0; i < kMaxSockets; ++i) {
1023 EXPECT_EQ(ERR_FAILED, request(i)->WaitForResult());
1027 // This is a regression test for the first attempted fix for
1028 // FlushWithErrorDoesNotCauseSuccessfulConnections. Because a ConnectJob can
1029 // have both IPv4 and IPv6 subjobs, it can be both connecting and waiting for
1030 // the lock at the same time.
1031 TEST_F(WebSocketTransportClientSocketPoolTest,
1032 FlushWithErrorDoesNotCauseSuccessfulConnectionsMultipleAddressTypes) {
1033 host_resolver_->set_synchronous_mode(true);
1034 // The first |kMaxSockets| sockets to connect will be IPv6. Then we will have
1035 // one IPv4.
1036 std::vector<MockTransportClientSocketFactory::ClientSocketType> socket_types(
1037 kMaxSockets + 1,
1038 MockTransportClientSocketFactory::MOCK_STALLED_CLIENT_SOCKET);
1039 client_socket_factory_.set_client_socket_types(&socket_types[0],
1040 socket_types.size());
1041 // The rest of the sockets will connect synchronously.
1042 client_socket_factory_.set_default_client_socket_type(
1043 MockTransportClientSocketFactory::MOCK_CLIENT_SOCKET);
1044 for (int i = 0; i < kMaxSockets; ++i) {
1045 host_resolver_->rules()->ClearRules();
1046 // Each connect job has a different IPv6 address but the same IPv4 address.
1047 // So the IPv6 connections happen in parallel but the IPv4 ones are
1048 // serialised.
1049 host_resolver_->rules()->AddIPLiteralRule("*",
1050 base::StringPrintf(
1051 "%x:abcd::3:4:ff,"
1052 "1.1.1.1",
1053 i + 1),
1054 std::string());
1055 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
1057 // Now we have |kMaxSockets| IPv6 sockets stalled in connect. No IPv4 sockets
1058 // are started yet.
1059 RunLoopForTimePeriod(base::TimeDelta::FromMilliseconds(
1060 TransportConnectJobHelper::kIPv6FallbackTimerInMs));
1061 // Now we have |kMaxSockets| IPv6 sockets and one IPv4 socket stalled in
1062 // connect, and |kMaxSockets - 1| IPv4 sockets waiting for the endpoint lock.
1063 pool_.FlushWithError(ERR_FAILED);
1064 for (int i = 0; i < kMaxSockets; ++i) {
1065 EXPECT_EQ(ERR_FAILED, request(i)->WaitForResult());
1069 // Sockets that have had ownership transferred to a ClientSocketHandle should
1070 // not be affected by FlushWithError.
1071 TEST_F(WebSocketTransportClientSocketPoolTest,
1072 FlushWithErrorDoesNotAffectHandedOutSockets) {
1073 host_resolver_->set_synchronous_mode(true);
1074 MockTransportClientSocketFactory::ClientSocketType socket_types[] = {
1075 MockTransportClientSocketFactory::MOCK_CLIENT_SOCKET,
1076 MockTransportClientSocketFactory::MOCK_STALLED_CLIENT_SOCKET};
1077 client_socket_factory_.set_client_socket_types(socket_types,
1078 arraysize(socket_types));
1079 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority));
1080 // Socket has been "handed out".
1081 EXPECT_TRUE(request(0)->handle()->socket());
1083 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
1084 // Now we have one socket handed out, and one pending.
1085 pool_.FlushWithError(ERR_FAILED);
1086 EXPECT_EQ(ERR_FAILED, request(1)->WaitForResult());
1087 // Socket owned by ClientSocketHandle is unaffected:
1088 EXPECT_TRUE(request(0)->handle()->socket());
1089 // Return it to the pool (which deletes it).
1090 request(0)->handle()->Reset();
1093 // Sockets should not be leaked if CancelRequest() is called in between
1094 // SetSocket() being called on the ClientSocketHandle and InvokeUserCallback().
1095 TEST_F(WebSocketTransportClientSocketPoolTest, CancelRequestReclaimsSockets) {
1096 host_resolver_->set_synchronous_mode(true);
1097 MockTransportClientSocketFactory::ClientSocketType socket_types[] = {
1098 MockTransportClientSocketFactory::MOCK_TRIGGERABLE_CLIENT_SOCKET,
1099 MockTransportClientSocketFactory::MOCK_CLIENT_SOCKET};
1101 client_socket_factory_.set_client_socket_types(socket_types,
1102 arraysize(socket_types));
1104 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
1106 base::Closure connect_trigger =
1107 client_socket_factory_.WaitForTriggerableSocketCreation();
1109 connect_trigger.Run(); // Calls InvokeUserCallbackLater()
1111 request(0)->handle()->Reset(); // calls CancelRequest()
1113 RunUntilIdle();
1114 // We should now be able to create a new connection without blocking on the
1115 // endpoint lock.
1116 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority));
1119 // A handshake completing and then the WebSocket closing should only release one
1120 // Endpoint, not two.
1121 TEST_F(WebSocketTransportClientSocketPoolTest, EndpointLockIsOnlyReleasedOnce) {
1122 host_resolver_->set_synchronous_mode(true);
1123 ASSERT_EQ(OK, StartRequest("a", kDefaultPriority));
1124 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
1125 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority));
1126 // First socket completes handshake.
1127 WebSocketTransportClientSocketPool::UnlockEndpoint(request(0)->handle());
1128 RunUntilIdle();
1129 // First socket is closed.
1130 request(0)->handle()->Reset();
1131 // Second socket should have been released.
1132 EXPECT_EQ(OK, request(1)->WaitForResult());
1133 // Third socket should still be waiting for endpoint.
1134 ASSERT_FALSE(request(2)->handle()->is_initialized());
1135 EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET,
1136 request(2)->handle()->GetLoadState());
1139 } // namespace
1141 } // namespace net