| 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" | |
| 12 #include "net/base/net_errors.h" | 11 #include "net/base/net_errors.h" |
| 13 #include "net/base/test_completion_callback.h" | 12 #include "net/base/test_completion_callback.h" |
| 14 #include "net/socket/client_socket.h" | 13 #include "net/socket/client_socket.h" |
| 15 #include "net/socket/client_socket_factory.h" | 14 #include "net/socket/client_socket_factory.h" |
| 16 #include "net/socket/client_socket_handle.h" | 15 #include "net/socket/client_socket_handle.h" |
| 17 #include "net/socket/client_socket_pool_histograms.h" | 16 #include "net/socket/client_socket_pool_histograms.h" |
| 18 #include "net/socket/socket_test_util.h" | 17 #include "net/socket/socket_test_util.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 20 | 19 |
| 21 namespace net { | 20 namespace net { |
| (...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 265 TCPClientSocketPoolTest() | 264 TCPClientSocketPoolTest() |
| 266 : ignored_socket_params_( | 265 : ignored_socket_params_( |
| 267 HostPortPair("ignored", 80), MEDIUM, GURL(), false), | 266 HostPortPair("ignored", 80), MEDIUM, GURL(), false), |
| 268 histograms_(new ClientSocketPoolHistograms("TCPUnitTest")), | 267 histograms_(new ClientSocketPoolHistograms("TCPUnitTest")), |
| 269 host_resolver_(new MockHostResolver), | 268 host_resolver_(new MockHostResolver), |
| 270 pool_(new TCPClientSocketPool(kMaxSockets, | 269 pool_(new TCPClientSocketPool(kMaxSockets, |
| 271 kMaxSocketsPerGroup, | 270 kMaxSocketsPerGroup, |
| 272 histograms_, | 271 histograms_, |
| 273 host_resolver_, | 272 host_resolver_, |
| 274 &client_socket_factory_, | 273 &client_socket_factory_, |
| 275 ¬ifier_, | |
| 276 NULL)) { | 274 NULL)) { |
| 277 } | 275 } |
| 278 | 276 |
| 279 int StartRequest(const std::string& group_name, RequestPriority priority) { | 277 int StartRequest(const std::string& group_name, RequestPriority priority) { |
| 280 return StartRequestUsingPool( | 278 return StartRequestUsingPool( |
| 281 pool_, group_name, priority, ignored_socket_params_); | 279 pool_, group_name, priority, ignored_socket_params_); |
| 282 } | 280 } |
| 283 | 281 |
| 284 TCPSocketParams ignored_socket_params_; | 282 TCPSocketParams ignored_socket_params_; |
| 285 scoped_refptr<ClientSocketPoolHistograms> histograms_; | 283 scoped_refptr<ClientSocketPoolHistograms> histograms_; |
| 286 scoped_refptr<MockHostResolver> host_resolver_; | 284 scoped_refptr<MockHostResolver> host_resolver_; |
| 287 MockClientSocketFactory client_socket_factory_; | 285 MockClientSocketFactory client_socket_factory_; |
| 288 MockNetworkChangeNotifier notifier_; | |
| 289 scoped_refptr<TCPClientSocketPool> pool_; | 286 scoped_refptr<TCPClientSocketPool> pool_; |
| 290 }; | 287 }; |
| 291 | 288 |
| 292 TEST_F(TCPClientSocketPoolTest, Basic) { | 289 TEST_F(TCPClientSocketPoolTest, Basic) { |
| 293 TestCompletionCallback callback; | 290 TestCompletionCallback callback; |
| 294 ClientSocketHandle handle; | 291 ClientSocketHandle handle; |
| 295 TCPSocketParams dest(HostPortPair("www.google.com", 80), LOW, GURL(), false); | 292 TCPSocketParams dest(HostPortPair("www.google.com", 80), LOW, GURL(), false); |
| 296 int rv = handle.Init("a", dest, LOW, &callback, pool_, BoundNetLog()); | 293 int rv = handle.Init("a", dest, LOW, &callback, pool_, BoundNetLog()); |
| 297 EXPECT_EQ(ERR_IO_PENDING, rv); | 294 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 298 EXPECT_FALSE(handle.is_initialized()); | 295 EXPECT_FALSE(handle.is_initialized()); |
| (...skipping 378 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 677 | 674 |
| 678 handle.Reset(); | 675 handle.Reset(); |
| 679 | 676 |
| 680 // Need to run all pending to release the socket back to the pool. | 677 // Need to run all pending to release the socket back to the pool. |
| 681 MessageLoop::current()->RunAllPending(); | 678 MessageLoop::current()->RunAllPending(); |
| 682 | 679 |
| 683 // Now we should have 1 idle socket. | 680 // Now we should have 1 idle socket. |
| 684 EXPECT_EQ(1, pool_->IdleSocketCount()); | 681 EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 685 | 682 |
| 686 // After an IP address change, we should have 0 idle sockets. | 683 // After an IP address change, we should have 0 idle sockets. |
| 687 notifier_.NotifyIPAddressChange(); | 684 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); |
| 685 MessageLoop::current()->RunAllPending(); // Notification happens async. |
| 686 |
| 688 EXPECT_EQ(0, pool_->IdleSocketCount()); | 687 EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 689 } | 688 } |
| 690 | 689 |
| 691 TEST_F(TCPClientSocketPoolTest, BackupSocketConnect) { | 690 TEST_F(TCPClientSocketPoolTest, BackupSocketConnect) { |
| 692 // Case 1 tests the first socket stalling, and the backup connecting. | 691 // Case 1 tests the first socket stalling, and the backup connecting. |
| 693 MockClientSocketFactory::ClientSocketType case1_types[] = { | 692 MockClientSocketFactory::ClientSocketType case1_types[] = { |
| 694 // The first socket will not connect. | 693 // The first socket will not connect. |
| 695 MockClientSocketFactory::MOCK_STALLED_CLIENT_SOCKET, | 694 MockClientSocketFactory::MOCK_STALLED_CLIENT_SOCKET, |
| 696 // The second socket will connect more quickly. | 695 // The second socket will connect more quickly. |
| 697 MockClientSocketFactory::MOCK_CLIENT_SOCKET | 696 MockClientSocketFactory::MOCK_CLIENT_SOCKET |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 735 MessageLoop::current()->RunAllPending(); | 734 MessageLoop::current()->RunAllPending(); |
| 736 | 735 |
| 737 EXPECT_EQ(OK, callback.WaitForResult()); | 736 EXPECT_EQ(OK, callback.WaitForResult()); |
| 738 EXPECT_TRUE(handle.is_initialized()); | 737 EXPECT_TRUE(handle.is_initialized()); |
| 739 EXPECT_TRUE(handle.socket()); | 738 EXPECT_TRUE(handle.socket()); |
| 740 | 739 |
| 741 // One socket is stalled, the other is active. | 740 // One socket is stalled, the other is active. |
| 742 EXPECT_EQ(0, pool_->IdleSocketCount()); | 741 EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 743 handle.Reset(); | 742 handle.Reset(); |
| 744 | 743 |
| 745 pool_ = new TCPClientSocketPool(kMaxSockets, | 744 pool_ = new TCPClientSocketPool(kMaxSockets, kMaxSocketsPerGroup, |
| 746 kMaxSocketsPerGroup, | 745 histograms_, host_resolver_, &client_socket_factory_, NULL); |
| 747 histograms_, | |
| 748 host_resolver_, | |
| 749 &client_socket_factory_, | |
| 750 NULL, | |
| 751 NULL); | |
| 752 } | 746 } |
| 753 } | 747 } |
| 754 | 748 |
| 755 // Test the case where a socket took long enough to start the creation | 749 // Test the case where a socket took long enough to start the creation |
| 756 // of the backup socket, but then we cancelled the request after that. | 750 // of the backup socket, but then we cancelled the request after that. |
| 757 TEST_F(TCPClientSocketPoolTest, BackupSocketCancel) { | 751 TEST_F(TCPClientSocketPoolTest, BackupSocketCancel) { |
| 758 client_socket_factory_.set_client_socket_type( | 752 client_socket_factory_.set_client_socket_type( |
| 759 MockClientSocketFactory::MOCK_STALLED_CLIENT_SOCKET); | 753 MockClientSocketFactory::MOCK_STALLED_CLIENT_SOCKET); |
| 760 | 754 |
| 761 enum { CANCEL_BEFORE_WAIT, CANCEL_AFTER_WAIT }; | 755 enum { CANCEL_BEFORE_WAIT, CANCEL_AFTER_WAIT }; |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 880 EXPECT_FALSE(handle.socket()); | 874 EXPECT_FALSE(handle.socket()); |
| 881 handle.Reset(); | 875 handle.Reset(); |
| 882 | 876 |
| 883 // Reset for the next case. | 877 // Reset for the next case. |
| 884 host_resolver_->set_synchronous_mode(false); | 878 host_resolver_->set_synchronous_mode(false); |
| 885 } | 879 } |
| 886 | 880 |
| 887 } // namespace | 881 } // namespace |
| 888 | 882 |
| 889 } // namespace net | 883 } // namespace net |
| OLD | NEW |