OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/tcp_client_socket_pool.h" | 5 #include "net/socket/tcp_client_socket_pool.h" |
6 | 6 |
7 #include "base/callback.h" | 7 #include "base/callback.h" |
8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "net/base/mock_host_resolver.h" | 10 #include "net/base/mock_host_resolver.h" |
(...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
290 host_resolver_(new MockHostResolver), | 290 host_resolver_(new MockHostResolver), |
291 pool_(kMaxSockets, | 291 pool_(kMaxSockets, |
292 kMaxSocketsPerGroup, | 292 kMaxSocketsPerGroup, |
293 histograms_.get(), | 293 histograms_.get(), |
294 host_resolver_.get(), | 294 host_resolver_.get(), |
295 &client_socket_factory_, | 295 &client_socket_factory_, |
296 NULL) { | 296 NULL) { |
297 } | 297 } |
298 | 298 |
299 int StartRequest(const std::string& group_name, RequestPriority priority) { | 299 int StartRequest(const std::string& group_name, RequestPriority priority) { |
300 scoped_refptr<TCPSocketParams> params = new TCPSocketParams( | 300 scoped_refptr<TCPSocketParams> params(new TCPSocketParams( |
301 HostPortPair("www.google.com", 80), MEDIUM, GURL(), false); | 301 HostPortPair("www.google.com", 80), MEDIUM, GURL(), false)); |
302 return test_base_.StartRequestUsingPool( | 302 return test_base_.StartRequestUsingPool( |
303 &pool_, group_name, priority, params); | 303 &pool_, group_name, priority, params); |
304 } | 304 } |
305 | 305 |
306 int GetOrderOfRequest(size_t index) { | 306 int GetOrderOfRequest(size_t index) { |
307 return test_base_.GetOrderOfRequest(index); | 307 return test_base_.GetOrderOfRequest(index); |
308 } | 308 } |
309 | 309 |
310 bool ReleaseOneConnection(ClientSocketPoolTest::KeepAlive keep_alive) { | 310 bool ReleaseOneConnection(ClientSocketPoolTest::KeepAlive keep_alive) { |
311 return test_base_.ReleaseOneConnection(keep_alive); | 311 return test_base_.ReleaseOneConnection(keep_alive); |
(...skipping 27 matching lines...) Expand all Loading... |
339 EXPECT_TRUE(handle.is_initialized()); | 339 EXPECT_TRUE(handle.is_initialized()); |
340 EXPECT_TRUE(handle.socket()); | 340 EXPECT_TRUE(handle.socket()); |
341 | 341 |
342 handle.Reset(); | 342 handle.Reset(); |
343 } | 343 } |
344 | 344 |
345 TEST_F(TCPClientSocketPoolTest, InitHostResolutionFailure) { | 345 TEST_F(TCPClientSocketPoolTest, InitHostResolutionFailure) { |
346 host_resolver_->rules()->AddSimulatedFailure("unresolvable.host.name"); | 346 host_resolver_->rules()->AddSimulatedFailure("unresolvable.host.name"); |
347 TestCompletionCallback callback; | 347 TestCompletionCallback callback; |
348 ClientSocketHandle handle; | 348 ClientSocketHandle handle; |
349 scoped_refptr<TCPSocketParams> dest = new TCPSocketParams( | 349 scoped_refptr<TCPSocketParams> dest(new TCPSocketParams( |
350 "unresolvable.host.name", 80, kDefaultPriority, GURL(), false); | 350 "unresolvable.host.name", 80, kDefaultPriority, GURL(), false)); |
351 EXPECT_EQ(ERR_IO_PENDING, | 351 EXPECT_EQ(ERR_IO_PENDING, |
352 handle.Init("a", dest, kDefaultPriority, &callback, &pool_, | 352 handle.Init("a", dest, kDefaultPriority, &callback, &pool_, |
353 BoundNetLog())); | 353 BoundNetLog())); |
354 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, callback.WaitForResult()); | 354 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, callback.WaitForResult()); |
355 } | 355 } |
356 | 356 |
357 TEST_F(TCPClientSocketPoolTest, InitConnectionFailure) { | 357 TEST_F(TCPClientSocketPoolTest, InitConnectionFailure) { |
358 client_socket_factory_.set_client_socket_type( | 358 client_socket_factory_.set_client_socket_type( |
359 MockClientSocketFactory::MOCK_FAILING_CLIENT_SOCKET); | 359 MockClientSocketFactory::MOCK_FAILING_CLIENT_SOCKET); |
360 TestCompletionCallback callback; | 360 TestCompletionCallback callback; |
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
598 // Don't allow reuse of the socket. Disconnect it and then release it and | 598 // Don't allow reuse of the socket. Disconnect it and then release it and |
599 // run through the MessageLoop once to get it completely released. | 599 // run through the MessageLoop once to get it completely released. |
600 handle_->socket()->Disconnect(); | 600 handle_->socket()->Disconnect(); |
601 handle_->Reset(); | 601 handle_->Reset(); |
602 { | 602 { |
603 MessageLoop::ScopedNestableTaskAllower nestable( | 603 MessageLoop::ScopedNestableTaskAllower nestable( |
604 MessageLoop::current()); | 604 MessageLoop::current()); |
605 MessageLoop::current()->RunAllPending(); | 605 MessageLoop::current()->RunAllPending(); |
606 } | 606 } |
607 within_callback_ = true; | 607 within_callback_ = true; |
608 scoped_refptr<TCPSocketParams> dest = new TCPSocketParams( | 608 scoped_refptr<TCPSocketParams> dest(new TCPSocketParams( |
609 HostPortPair("www.google.com", 80), LOWEST, GURL(), false); | 609 HostPortPair("www.google.com", 80), LOWEST, GURL(), false)); |
610 int rv = handle_->Init("a", dest, LOWEST, this, pool_, BoundNetLog()); | 610 int rv = handle_->Init("a", dest, LOWEST, this, pool_, BoundNetLog()); |
611 EXPECT_EQ(OK, rv); | 611 EXPECT_EQ(OK, rv); |
612 } | 612 } |
613 } | 613 } |
614 | 614 |
615 int WaitForResult() { | 615 int WaitForResult() { |
616 return callback_.WaitForResult(); | 616 return callback_.WaitForResult(); |
617 } | 617 } |
618 | 618 |
619 private: | 619 private: |
620 ClientSocketHandle* const handle_; | 620 ClientSocketHandle* const handle_; |
621 TCPClientSocketPool* const pool_; | 621 TCPClientSocketPool* const pool_; |
622 bool within_callback_; | 622 bool within_callback_; |
623 TestCompletionCallback callback_; | 623 TestCompletionCallback callback_; |
624 }; | 624 }; |
625 | 625 |
626 TEST_F(TCPClientSocketPoolTest, RequestTwice) { | 626 TEST_F(TCPClientSocketPoolTest, RequestTwice) { |
627 ClientSocketHandle handle; | 627 ClientSocketHandle handle; |
628 RequestSocketCallback callback(&handle, &pool_); | 628 RequestSocketCallback callback(&handle, &pool_); |
629 scoped_refptr<TCPSocketParams> dest = new TCPSocketParams( | 629 scoped_refptr<TCPSocketParams> dest(new TCPSocketParams( |
630 HostPortPair("www.google.com", 80), LOWEST, GURL(), false); | 630 HostPortPair("www.google.com", 80), LOWEST, GURL(), false)); |
631 int rv = handle.Init("a", dest, LOWEST, &callback, &pool_, | 631 int rv = handle.Init("a", dest, LOWEST, &callback, &pool_, |
632 BoundNetLog()); | 632 BoundNetLog()); |
633 ASSERT_EQ(ERR_IO_PENDING, rv); | 633 ASSERT_EQ(ERR_IO_PENDING, rv); |
634 | 634 |
635 // The callback is going to request "www.google.com". We want it to complete | 635 // The callback is going to request "www.google.com". We want it to complete |
636 // synchronously this time. | 636 // synchronously this time. |
637 host_resolver_->set_synchronous_mode(true); | 637 host_resolver_->set_synchronous_mode(true); |
638 | 638 |
639 EXPECT_EQ(OK, callback.WaitForResult()); | 639 EXPECT_EQ(OK, callback.WaitForResult()); |
640 | 640 |
(...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
900 EXPECT_FALSE(handle.socket()); | 900 EXPECT_FALSE(handle.socket()); |
901 handle.Reset(); | 901 handle.Reset(); |
902 | 902 |
903 // Reset for the next case. | 903 // Reset for the next case. |
904 host_resolver_->set_synchronous_mode(false); | 904 host_resolver_->set_synchronous_mode(false); |
905 } | 905 } |
906 | 906 |
907 } // namespace | 907 } // namespace |
908 | 908 |
909 } // namespace net | 909 } // namespace net |
OLD | NEW |