| 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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 44 base::RunLoop run_loop; | 44 base::RunLoop run_loop; |
| 45 base::Closure quit_closure(run_loop.QuitClosure()); | 45 base::Closure quit_closure(run_loop.QuitClosure()); |
| 46 base::MessageLoop::current()->PostDelayedTask( | 46 base::MessageLoop::current()->PostDelayedTask( |
| 47 FROM_HERE, quit_closure, period); | 47 FROM_HERE, quit_closure, period); |
| 48 run_loop.Run(); | 48 run_loop.Run(); |
| 49 } | 49 } |
| 50 | 50 |
| 51 class WebSocketTransportClientSocketPoolTest : public testing::Test { | 51 class WebSocketTransportClientSocketPoolTest : public testing::Test { |
| 52 protected: | 52 protected: |
| 53 WebSocketTransportClientSocketPoolTest() | 53 WebSocketTransportClientSocketPoolTest() |
| 54 : params_(new TransportSocketParams(HostPortPair("www.google.com", 80), | 54 : params_(new TransportSocketParams( |
| 55 false, | 55 HostPortPair("www.google.com", 80), |
| 56 false, | 56 false, |
| 57 OnHostResolutionCallback())), | 57 false, |
| 58 OnHostResolutionCallback(), |
| 59 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DEFAULT)), |
| 58 histograms_(new ClientSocketPoolHistograms("TCPUnitTest")), | 60 histograms_(new ClientSocketPoolHistograms("TCPUnitTest")), |
| 59 host_resolver_(new MockHostResolver), | 61 host_resolver_(new MockHostResolver), |
| 60 client_socket_factory_(&net_log_), | 62 client_socket_factory_(&net_log_), |
| 61 pool_(kMaxSockets, | 63 pool_(kMaxSockets, |
| 62 kMaxSocketsPerGroup, | 64 kMaxSocketsPerGroup, |
| 63 histograms_.get(), | 65 histograms_.get(), |
| 64 host_resolver_.get(), | 66 host_resolver_.get(), |
| 65 &client_socket_factory_, | 67 &client_socket_factory_, |
| 66 NULL) {} | 68 NULL) {} |
| 67 | 69 |
| 68 virtual ~WebSocketTransportClientSocketPoolTest() { | 70 virtual ~WebSocketTransportClientSocketPoolTest() { |
| 69 ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); | 71 ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 70 EXPECT_TRUE(WebSocketEndpointLockManager::GetInstance()->IsEmpty()); | 72 EXPECT_TRUE(WebSocketEndpointLockManager::GetInstance()->IsEmpty()); |
| 71 } | 73 } |
| 72 | 74 |
| 73 int StartRequest(const std::string& group_name, RequestPriority priority) { | 75 int StartRequest(const std::string& group_name, RequestPriority priority) { |
| 74 scoped_refptr<TransportSocketParams> params( | 76 scoped_refptr<TransportSocketParams> params( |
| 75 new TransportSocketParams(HostPortPair("www.google.com", 80), | 77 new TransportSocketParams( |
| 76 false, | 78 HostPortPair("www.google.com", 80), |
| 77 false, | 79 false, |
| 78 OnHostResolutionCallback())); | 80 false, |
| 81 OnHostResolutionCallback(), |
| 82 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DEFAULT)); |
| 79 return test_base_.StartRequestUsingPool( | 83 return test_base_.StartRequestUsingPool( |
| 80 &pool_, group_name, priority, params); | 84 &pool_, group_name, priority, params); |
| 81 } | 85 } |
| 82 | 86 |
| 83 int GetOrderOfRequest(size_t index) { | 87 int GetOrderOfRequest(size_t index) { |
| 84 return test_base_.GetOrderOfRequest(index); | 88 return test_base_.GetOrderOfRequest(index); |
| 85 } | 89 } |
| 86 | 90 |
| 87 bool ReleaseOneConnection(ClientSocketPoolTest::KeepAlive keep_alive) { | 91 bool ReleaseOneConnection(ClientSocketPoolTest::KeepAlive keep_alive) { |
| 88 return test_base_.ReleaseOneConnection(keep_alive); | 92 return test_base_.ReleaseOneConnection(keep_alive); |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 141 EXPECT_EQ(priority, host_resolver_->last_request_priority()); | 145 EXPECT_EQ(priority, host_resolver_->last_request_priority()); |
| 142 } | 146 } |
| 143 } | 147 } |
| 144 | 148 |
| 145 TEST_F(WebSocketTransportClientSocketPoolTest, InitHostResolutionFailure) { | 149 TEST_F(WebSocketTransportClientSocketPoolTest, InitHostResolutionFailure) { |
| 146 host_resolver_->rules()->AddSimulatedFailure("unresolvable.host.name"); | 150 host_resolver_->rules()->AddSimulatedFailure("unresolvable.host.name"); |
| 147 TestCompletionCallback callback; | 151 TestCompletionCallback callback; |
| 148 ClientSocketHandle handle; | 152 ClientSocketHandle handle; |
| 149 HostPortPair host_port_pair("unresolvable.host.name", 80); | 153 HostPortPair host_port_pair("unresolvable.host.name", 80); |
| 150 scoped_refptr<TransportSocketParams> dest(new TransportSocketParams( | 154 scoped_refptr<TransportSocketParams> dest(new TransportSocketParams( |
| 151 host_port_pair, false, false, OnHostResolutionCallback())); | 155 host_port_pair, false, false, OnHostResolutionCallback(), |
| 156 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DEFAULT)); |
| 152 EXPECT_EQ(ERR_IO_PENDING, | 157 EXPECT_EQ(ERR_IO_PENDING, |
| 153 handle.Init("a", | 158 handle.Init("a", |
| 154 dest, | 159 dest, |
| 155 kDefaultPriority, | 160 kDefaultPriority, |
| 156 callback.callback(), | 161 callback.callback(), |
| 157 &pool_, | 162 &pool_, |
| 158 BoundNetLog())); | 163 BoundNetLog())); |
| 159 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, callback.WaitForResult()); | 164 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, callback.WaitForResult()); |
| 160 } | 165 } |
| 161 | 166 |
| (...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 391 // run through the MessageLoop once to get it completely released. | 396 // run through the MessageLoop once to get it completely released. |
| 392 handle_->socket()->Disconnect(); | 397 handle_->socket()->Disconnect(); |
| 393 handle_->Reset(); | 398 handle_->Reset(); |
| 394 { | 399 { |
| 395 base::MessageLoop::ScopedNestableTaskAllower allow( | 400 base::MessageLoop::ScopedNestableTaskAllower allow( |
| 396 base::MessageLoop::current()); | 401 base::MessageLoop::current()); |
| 397 base::MessageLoop::current()->RunUntilIdle(); | 402 base::MessageLoop::current()->RunUntilIdle(); |
| 398 } | 403 } |
| 399 within_callback_ = true; | 404 within_callback_ = true; |
| 400 scoped_refptr<TransportSocketParams> dest( | 405 scoped_refptr<TransportSocketParams> dest( |
| 401 new TransportSocketParams(HostPortPair("www.google.com", 80), | 406 new TransportSocketParams( |
| 402 false, | 407 HostPortPair("www.google.com", 80), |
| 403 false, | 408 false, |
| 404 OnHostResolutionCallback())); | 409 false, |
| 410 OnHostResolutionCallback(), |
| 411 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DEFAULT)); |
| 405 int rv = | 412 int rv = |
| 406 handle_->Init("a", dest, LOWEST, callback(), pool_, BoundNetLog()); | 413 handle_->Init("a", dest, LOWEST, callback(), pool_, BoundNetLog()); |
| 407 EXPECT_EQ(OK, rv); | 414 EXPECT_EQ(OK, rv); |
| 408 } | 415 } |
| 409 } | 416 } |
| 410 | 417 |
| 411 ClientSocketHandle* const handle_; | 418 ClientSocketHandle* const handle_; |
| 412 WebSocketTransportClientSocketPool* const pool_; | 419 WebSocketTransportClientSocketPool* const pool_; |
| 413 bool within_callback_; | 420 bool within_callback_; |
| 414 CompletionCallback callback_; | 421 CompletionCallback callback_; |
| 415 | 422 |
| 416 DISALLOW_COPY_AND_ASSIGN(RequestSocketCallback); | 423 DISALLOW_COPY_AND_ASSIGN(RequestSocketCallback); |
| 417 }; | 424 }; |
| 418 | 425 |
| 419 TEST_F(WebSocketTransportClientSocketPoolTest, RequestTwice) { | 426 TEST_F(WebSocketTransportClientSocketPoolTest, RequestTwice) { |
| 420 ClientSocketHandle handle; | 427 ClientSocketHandle handle; |
| 421 RequestSocketCallback callback(&handle, &pool_); | 428 RequestSocketCallback callback(&handle, &pool_); |
| 422 scoped_refptr<TransportSocketParams> dest( | 429 scoped_refptr<TransportSocketParams> dest( |
| 423 new TransportSocketParams(HostPortPair("www.google.com", 80), | 430 new TransportSocketParams( |
| 424 false, | 431 HostPortPair("www.google.com", 80), |
| 425 false, | 432 false, |
| 426 OnHostResolutionCallback())); | 433 false, |
| 434 OnHostResolutionCallback(), |
| 435 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DEFAULT)); |
| 427 int rv = handle.Init( | 436 int rv = handle.Init( |
| 428 "a", dest, LOWEST, callback.callback(), &pool_, BoundNetLog()); | 437 "a", dest, LOWEST, callback.callback(), &pool_, BoundNetLog()); |
| 429 ASSERT_EQ(ERR_IO_PENDING, rv); | 438 ASSERT_EQ(ERR_IO_PENDING, rv); |
| 430 | 439 |
| 431 // The callback is going to request "www.google.com". We want it to complete | 440 // The callback is going to request "www.google.com". We want it to complete |
| 432 // synchronously this time. | 441 // synchronously this time. |
| 433 host_resolver_->set_synchronous_mode(true); | 442 host_resolver_->set_synchronous_mode(true); |
| 434 | 443 |
| 435 EXPECT_EQ(OK, callback.WaitForResult()); | 444 EXPECT_EQ(OK, callback.WaitForResult()); |
| 436 | 445 |
| (...skipping 688 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1125 EXPECT_EQ(OK, request(1)->WaitForResult()); | 1134 EXPECT_EQ(OK, request(1)->WaitForResult()); |
| 1126 // Third socket should still be waiting for endpoint. | 1135 // Third socket should still be waiting for endpoint. |
| 1127 ASSERT_FALSE(request(2)->handle()->is_initialized()); | 1136 ASSERT_FALSE(request(2)->handle()->is_initialized()); |
| 1128 EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET, | 1137 EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET, |
| 1129 request(2)->handle()->GetLoadState()); | 1138 request(2)->handle()->GetLoadState()); |
| 1130 } | 1139 } |
| 1131 | 1140 |
| 1132 } // namespace | 1141 } // namespace |
| 1133 | 1142 |
| 1134 } // namespace net | 1143 } // namespace net |
| OLD | NEW |