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" |
| 11 #include "net/base/mock_network_change_notifier.h" |
11 #include "net/base/net_errors.h" | 12 #include "net/base/net_errors.h" |
12 #include "net/base/test_completion_callback.h" | 13 #include "net/base/test_completion_callback.h" |
13 #include "net/socket/client_socket.h" | 14 #include "net/socket/client_socket.h" |
14 #include "net/socket/client_socket_factory.h" | 15 #include "net/socket/client_socket_factory.h" |
15 #include "net/socket/client_socket_handle.h" | 16 #include "net/socket/client_socket_handle.h" |
16 #include "net/socket/socket_test_util.h" | 17 #include "net/socket/socket_test_util.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
18 | 19 |
19 namespace net { | 20 namespace net { |
20 | 21 |
(...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
261 class TCPClientSocketPoolTest : public ClientSocketPoolTest { | 262 class TCPClientSocketPoolTest : public ClientSocketPoolTest { |
262 protected: | 263 protected: |
263 TCPClientSocketPoolTest() | 264 TCPClientSocketPoolTest() |
264 : ignored_socket_params_( | 265 : ignored_socket_params_( |
265 HostPortPair("ignored", 80), MEDIUM, GURL(), false), | 266 HostPortPair("ignored", 80), MEDIUM, GURL(), false), |
266 host_resolver_(new MockHostResolver), | 267 host_resolver_(new MockHostResolver), |
267 pool_(new TCPClientSocketPool(kMaxSockets, | 268 pool_(new TCPClientSocketPool(kMaxSockets, |
268 kMaxSocketsPerGroup, | 269 kMaxSocketsPerGroup, |
269 "TCPUnitTest", | 270 "TCPUnitTest", |
270 host_resolver_, | 271 host_resolver_, |
271 &client_socket_factory_)) {} | 272 &client_socket_factory_, |
| 273 ¬ifier_)) { |
| 274 } |
272 | 275 |
273 int StartRequest(const std::string& group_name, RequestPriority priority) { | 276 int StartRequest(const std::string& group_name, RequestPriority priority) { |
274 return StartRequestUsingPool( | 277 return StartRequestUsingPool( |
275 pool_, group_name, priority, ignored_socket_params_); | 278 pool_, group_name, priority, ignored_socket_params_); |
276 } | 279 } |
277 | 280 |
278 TCPSocketParams ignored_socket_params_; | 281 TCPSocketParams ignored_socket_params_; |
279 scoped_refptr<MockHostResolver> host_resolver_; | 282 scoped_refptr<MockHostResolver> host_resolver_; |
280 MockClientSocketFactory client_socket_factory_; | 283 MockClientSocketFactory client_socket_factory_; |
| 284 MockNetworkChangeNotifier notifier_; |
281 scoped_refptr<TCPClientSocketPool> pool_; | 285 scoped_refptr<TCPClientSocketPool> pool_; |
282 }; | 286 }; |
283 | 287 |
284 TEST_F(TCPClientSocketPoolTest, Basic) { | 288 TEST_F(TCPClientSocketPoolTest, Basic) { |
285 TestCompletionCallback callback; | 289 TestCompletionCallback callback; |
286 ClientSocketHandle handle; | 290 ClientSocketHandle handle; |
287 TCPSocketParams dest(HostPortPair("www.google.com", 80), LOW, GURL(), false); | 291 TCPSocketParams dest(HostPortPair("www.google.com", 80), LOW, GURL(), false); |
288 int rv = handle.Init("a", dest, LOW, &callback, pool_, BoundNetLog()); | 292 int rv = handle.Init("a", dest, LOW, &callback, pool_, BoundNetLog()); |
289 EXPECT_EQ(ERR_IO_PENDING, rv); | 293 EXPECT_EQ(ERR_IO_PENDING, rv); |
290 EXPECT_FALSE(handle.is_initialized()); | 294 EXPECT_FALSE(handle.is_initialized()); |
(...skipping 356 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
647 ASSERT_LE(kNumRequests, kMaxSockets); // Otherwise the test will hang. | 651 ASSERT_LE(kNumRequests, kMaxSockets); // Otherwise the test will hang. |
648 | 652 |
649 // Queue up all the requests | 653 // Queue up all the requests |
650 for (int i = 0; i < kNumRequests; i++) | 654 for (int i = 0; i < kNumRequests; i++) |
651 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); | 655 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); |
652 | 656 |
653 for (int i = 0; i < kNumRequests; i++) | 657 for (int i = 0; i < kNumRequests; i++) |
654 EXPECT_EQ(ERR_CONNECTION_FAILED, requests_[i]->WaitForResult()); | 658 EXPECT_EQ(ERR_CONNECTION_FAILED, requests_[i]->WaitForResult()); |
655 } | 659 } |
656 | 660 |
| 661 TEST_F(TCPClientSocketPoolTest, ResetIdleSocketsOnIPAddressChange) { |
| 662 TestCompletionCallback callback; |
| 663 ClientSocketHandle handle; |
| 664 TCPSocketParams dest("www.google.com", 80, LOW, GURL(), false); |
| 665 int rv = handle.Init("a", dest, LOW, &callback, pool_, BoundNetLog()); |
| 666 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 667 EXPECT_FALSE(handle.is_initialized()); |
| 668 EXPECT_FALSE(handle.socket()); |
| 669 |
| 670 EXPECT_EQ(OK, callback.WaitForResult()); |
| 671 EXPECT_TRUE(handle.is_initialized()); |
| 672 EXPECT_TRUE(handle.socket()); |
| 673 |
| 674 handle.Reset(); |
| 675 |
| 676 // Need to run all pending to release the socket back to the pool. |
| 677 MessageLoop::current()->RunAllPending(); |
| 678 |
| 679 // Now we should have 1 idle socket. |
| 680 EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 681 |
| 682 // After an IP address change, we should have 0 idle sockets. |
| 683 notifier_.NotifyIPAddressChange(); |
| 684 EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 685 } |
| 686 |
657 TEST_F(TCPClientSocketPoolTest, BackupSocketConnect) { | 687 TEST_F(TCPClientSocketPoolTest, BackupSocketConnect) { |
658 // Case 1 tests the first socket stalling, and the backup connecting. | 688 // Case 1 tests the first socket stalling, and the backup connecting. |
659 MockClientSocketFactory::ClientSocketType case1_types[] = { | 689 MockClientSocketFactory::ClientSocketType case1_types[] = { |
660 // The first socket will not connect. | 690 // The first socket will not connect. |
661 MockClientSocketFactory::MOCK_STALLED_CLIENT_SOCKET, | 691 MockClientSocketFactory::MOCK_STALLED_CLIENT_SOCKET, |
662 // The second socket will connect more quickly. | 692 // The second socket will connect more quickly. |
663 MockClientSocketFactory::MOCK_CLIENT_SOCKET | 693 MockClientSocketFactory::MOCK_CLIENT_SOCKET |
664 }; | 694 }; |
665 | 695 |
666 // Case 2 tests the first socket being slow, so that we start the | 696 // Case 2 tests the first socket being slow, so that we start the |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
705 EXPECT_TRUE(handle.socket()); | 735 EXPECT_TRUE(handle.socket()); |
706 | 736 |
707 // One socket is stalled, the other is active. | 737 // One socket is stalled, the other is active. |
708 EXPECT_EQ(0, pool_->IdleSocketCount()); | 738 EXPECT_EQ(0, pool_->IdleSocketCount()); |
709 handle.Reset(); | 739 handle.Reset(); |
710 | 740 |
711 pool_ = new TCPClientSocketPool(kMaxSockets, | 741 pool_ = new TCPClientSocketPool(kMaxSockets, |
712 kMaxSocketsPerGroup, | 742 kMaxSocketsPerGroup, |
713 "TCPUnitTest", | 743 "TCPUnitTest", |
714 host_resolver_, | 744 host_resolver_, |
715 &client_socket_factory_); | 745 &client_socket_factory_, |
| 746 NULL); |
716 } | 747 } |
717 } | 748 } |
718 | 749 |
719 // Test the case where a socket took long enough to start the creation | 750 // Test the case where a socket took long enough to start the creation |
720 // of the backup socket, but then we cancelled the request after that. | 751 // of the backup socket, but then we cancelled the request after that. |
721 TEST_F(TCPClientSocketPoolTest, BackupSocketCancel) { | 752 TEST_F(TCPClientSocketPoolTest, BackupSocketCancel) { |
722 client_socket_factory_.set_client_socket_type( | 753 client_socket_factory_.set_client_socket_type( |
723 MockClientSocketFactory::MOCK_STALLED_CLIENT_SOCKET); | 754 MockClientSocketFactory::MOCK_STALLED_CLIENT_SOCKET); |
724 | 755 |
725 enum { CANCEL_BEFORE_WAIT, CANCEL_AFTER_WAIT }; | 756 enum { CANCEL_BEFORE_WAIT, CANCEL_AFTER_WAIT }; |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
844 EXPECT_FALSE(handle.socket()); | 875 EXPECT_FALSE(handle.socket()); |
845 handle.Reset(); | 876 handle.Reset(); |
846 | 877 |
847 // Reset for the next case. | 878 // Reset for the next case. |
848 host_resolver_->set_synchronous_mode(false); | 879 host_resolver_->set_synchronous_mode(false); |
849 } | 880 } |
850 | 881 |
851 } // namespace | 882 } // namespace |
852 | 883 |
853 } // namespace net | 884 } // namespace net |
OLD | NEW |