| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/socket/websocket_transport_client_socket_pool.h" | 5 #include "net/socket/websocket_transport_client_socket_pool.h" |
| 6 | 6 |
| 7 #include <queue> | 7 #include <queue> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 356 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 367 EXPECT_EQ(ClientSocketPoolTest::kRequestNotFound, | 367 EXPECT_EQ(ClientSocketPoolTest::kRequestNotFound, |
| 368 GetOrderOfRequest(3)); // Canceled request. | 368 GetOrderOfRequest(3)); // Canceled request. |
| 369 EXPECT_EQ(3, GetOrderOfRequest(4)); | 369 EXPECT_EQ(3, GetOrderOfRequest(4)); |
| 370 EXPECT_EQ(4, GetOrderOfRequest(5)); | 370 EXPECT_EQ(4, GetOrderOfRequest(5)); |
| 371 EXPECT_EQ(5, GetOrderOfRequest(6)); | 371 EXPECT_EQ(5, GetOrderOfRequest(6)); |
| 372 | 372 |
| 373 // Make sure we test order of all requests made. | 373 // Make sure we test order of all requests made. |
| 374 EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(7)); | 374 EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(7)); |
| 375 } | 375 } |
| 376 | 376 |
| 377 class RequestSocketCallback : public TestCompletionCallbackBase { | 377 // Function to be used as a callback on socket request completion. It first |
| 378 public: | 378 // disconnects the successfully connected socket from the first request, and |
| 379 RequestSocketCallback(ClientSocketHandle* handle, | 379 // then reuses the ClientSocketHandle to request another socket. The second |
| 380 WebSocketTransportClientSocketPool* pool) | 380 // request is expected to succeed asynchronously. |
| 381 : handle_(handle), | 381 // |
| 382 pool_(pool), | 382 // |nested_callback| is called with the result of the second socket request. |
| 383 within_callback_(false), | 383 void RequestSocketOnComplete(ClientSocketHandle* handle, |
| 384 callback_(base::Bind(&RequestSocketCallback::OnComplete, | 384 WebSocketTransportClientSocketPool* pool, |
| 385 base::Unretained(this))) {} | 385 const CompletionCallback& nested_callback, |
| 386 int first_request_result) { |
| 387 EXPECT_EQ(OK, first_request_result); |
| 386 | 388 |
| 387 ~RequestSocketCallback() override {} | 389 // Don't allow reuse of the socket. Disconnect it and then release it. |
| 390 handle->socket()->Disconnect(); |
| 391 handle->Reset(); |
| 388 | 392 |
| 389 const CompletionCallback& callback() const { return callback_; } | 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); |
| 402 } |
| 390 | 403 |
| 391 private: | 404 // Tests the case where a second socket is requested in a completion callback, |
| 392 void OnComplete(int result) { | 405 // and the second socket connects asynchronously. Reuses the same |
| 393 SetResult(result); | 406 // ClientSocketHandle for the second socket, after disconnecting the first. |
| 394 ASSERT_EQ(OK, result); | |
| 395 | |
| 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(); | |
| 401 { | |
| 402 base::MessageLoop::ScopedNestableTaskAllower allow( | |
| 403 base::MessageLoop::current()); | |
| 404 base::MessageLoop::current()->RunUntilIdle(); | |
| 405 } | |
| 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); | |
| 417 } | |
| 418 } | |
| 419 | |
| 420 ClientSocketHandle* const handle_; | |
| 421 WebSocketTransportClientSocketPool* const pool_; | |
| 422 bool within_callback_; | |
| 423 CompletionCallback callback_; | |
| 424 | |
| 425 DISALLOW_COPY_AND_ASSIGN(RequestSocketCallback); | |
| 426 }; | |
| 427 | |
| 428 TEST_F(WebSocketTransportClientSocketPoolTest, RequestTwice) { | 407 TEST_F(WebSocketTransportClientSocketPoolTest, RequestTwice) { |
| 429 ClientSocketHandle handle; | 408 ClientSocketHandle handle; |
| 430 RequestSocketCallback callback(&handle, &pool_); | |
| 431 scoped_refptr<TransportSocketParams> dest( | 409 scoped_refptr<TransportSocketParams> dest( |
| 432 new TransportSocketParams( | 410 new TransportSocketParams( |
| 433 HostPortPair("www.google.com", 80), | 411 HostPortPair("www.google.com", 80), |
| 434 false, | 412 false, |
| 435 false, | 413 false, |
| 436 OnHostResolutionCallback(), | 414 OnHostResolutionCallback(), |
| 437 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DEFAULT)); | 415 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DEFAULT)); |
| 438 int rv = handle.Init( | 416 TestCompletionCallback second_result_callback; |
| 439 "a", dest, LOWEST, callback.callback(), &pool_, BoundNetLog()); | 417 int rv = handle.Init("a", dest, LOWEST, |
| 418 base::Bind(&RequestSocketOnComplete, &handle, &pool_, |
| 419 second_result_callback.callback()), |
| 420 &pool_, BoundNetLog()); |
| 440 ASSERT_EQ(ERR_IO_PENDING, rv); | 421 ASSERT_EQ(ERR_IO_PENDING, rv); |
| 441 | 422 EXPECT_EQ(OK, second_result_callback.WaitForResult()); |
| 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); | |
| 445 | |
| 446 EXPECT_EQ(OK, callback.WaitForResult()); | |
| 447 | 423 |
| 448 handle.Reset(); | 424 handle.Reset(); |
| 449 } | 425 } |
| 450 | 426 |
| 451 // Make sure that pending requests get serviced after active requests get | 427 // Make sure that pending requests get serviced after active requests get |
| 452 // cancelled. | 428 // cancelled. |
| 453 TEST_F(WebSocketTransportClientSocketPoolTest, | 429 TEST_F(WebSocketTransportClientSocketPoolTest, |
| 454 CancelActiveRequestWithPendingRequests) { | 430 CancelActiveRequestWithPendingRequests) { |
| 455 client_socket_factory_.set_default_client_socket_type( | 431 client_socket_factory_.set_default_client_socket_type( |
| 456 MockTransportClientSocketFactory::MOCK_PENDING_CLIENT_SOCKET); | 432 MockTransportClientSocketFactory::MOCK_PENDING_CLIENT_SOCKET); |
| (...skipping 675 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1132 EXPECT_EQ(OK, request(1)->WaitForResult()); | 1108 EXPECT_EQ(OK, request(1)->WaitForResult()); |
| 1133 // Third socket should still be waiting for endpoint. | 1109 // Third socket should still be waiting for endpoint. |
| 1134 ASSERT_FALSE(request(2)->handle()->is_initialized()); | 1110 ASSERT_FALSE(request(2)->handle()->is_initialized()); |
| 1135 EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET, | 1111 EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET, |
| 1136 request(2)->handle()->GetLoadState()); | 1112 request(2)->handle()->GetLoadState()); |
| 1137 } | 1113 } |
| 1138 | 1114 |
| 1139 } // namespace | 1115 } // namespace |
| 1140 | 1116 |
| 1141 } // namespace net | 1117 } // namespace net |
| OLD | NEW |