| 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/socket_test_util.h" | 16 #include "net/socket/socket_test_util.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 18 |
| 20 namespace net { | 19 namespace net { |
| 21 | 20 |
| (...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 240 | 239 |
| 241 class TCPClientSocketPoolTest : public ClientSocketPoolTest { | 240 class TCPClientSocketPoolTest : public ClientSocketPoolTest { |
| 242 protected: | 241 protected: |
| 243 TCPClientSocketPoolTest() | 242 TCPClientSocketPoolTest() |
| 244 : ignored_socket_params_("ignored", 80, MEDIUM, GURL(), false), | 243 : ignored_socket_params_("ignored", 80, MEDIUM, GURL(), false), |
| 245 host_resolver_(new MockHostResolver), | 244 host_resolver_(new MockHostResolver), |
| 246 pool_(new TCPClientSocketPool(kMaxSockets, | 245 pool_(new TCPClientSocketPool(kMaxSockets, |
| 247 kMaxSocketsPerGroup, | 246 kMaxSocketsPerGroup, |
| 248 "TCPUnitTest", | 247 "TCPUnitTest", |
| 249 host_resolver_, | 248 host_resolver_, |
| 250 &client_socket_factory_, | 249 &client_socket_factory_)) { |
| 251 ¬ifier_)) { | |
| 252 } | 250 } |
| 253 | 251 |
| 254 int StartRequest(const std::string& group_name, RequestPriority priority) { | 252 int StartRequest(const std::string& group_name, RequestPriority priority) { |
| 255 return StartRequestUsingPool( | 253 return StartRequestUsingPool( |
| 256 pool_, group_name, priority, ignored_socket_params_); | 254 pool_, group_name, priority, ignored_socket_params_); |
| 257 } | 255 } |
| 258 | 256 |
| 259 TCPSocketParams ignored_socket_params_; | 257 TCPSocketParams ignored_socket_params_; |
| 260 scoped_refptr<MockHostResolver> host_resolver_; | 258 scoped_refptr<MockHostResolver> host_resolver_; |
| 261 MockClientSocketFactory client_socket_factory_; | 259 MockClientSocketFactory client_socket_factory_; |
| 262 MockNetworkChangeNotifier notifier_; | |
| 263 scoped_refptr<TCPClientSocketPool> pool_; | 260 scoped_refptr<TCPClientSocketPool> pool_; |
| 264 }; | 261 }; |
| 265 | 262 |
| 266 TEST_F(TCPClientSocketPoolTest, Basic) { | 263 TEST_F(TCPClientSocketPoolTest, Basic) { |
| 267 TestCompletionCallback callback; | 264 TestCompletionCallback callback; |
| 268 ClientSocketHandle handle; | 265 ClientSocketHandle handle; |
| 269 TCPSocketParams dest("www.google.com", 80, LOW, GURL(), false); | 266 TCPSocketParams dest("www.google.com", 80, LOW, GURL(), false); |
| 270 int rv = handle.Init("a", dest, LOW, &callback, pool_, NULL); | 267 int rv = handle.Init("a", dest, LOW, &callback, pool_, NULL); |
| 271 EXPECT_EQ(ERR_IO_PENDING, rv); | 268 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 272 EXPECT_FALSE(handle.is_initialized()); | 269 EXPECT_FALSE(handle.is_initialized()); |
| (...skipping 348 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 621 ASSERT_LE(kNumRequests, kMaxSockets); // Otherwise the test will hang. | 618 ASSERT_LE(kNumRequests, kMaxSockets); // Otherwise the test will hang. |
| 622 | 619 |
| 623 // Queue up all the requests | 620 // Queue up all the requests |
| 624 for (int i = 0; i < kNumRequests; i++) | 621 for (int i = 0; i < kNumRequests; i++) |
| 625 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); | 622 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); |
| 626 | 623 |
| 627 for (int i = 0; i < kNumRequests; i++) | 624 for (int i = 0; i < kNumRequests; i++) |
| 628 EXPECT_EQ(ERR_CONNECTION_FAILED, requests_[i]->WaitForResult()); | 625 EXPECT_EQ(ERR_CONNECTION_FAILED, requests_[i]->WaitForResult()); |
| 629 } | 626 } |
| 630 | 627 |
| 631 TEST_F(TCPClientSocketPoolTest, ResetIdleSocketsOnIPAddressChange) { | |
| 632 TestCompletionCallback callback; | |
| 633 ClientSocketHandle handle; | |
| 634 TCPSocketParams dest("www.google.com", 80, LOW, GURL(), false); | |
| 635 int rv = handle.Init("a", dest, LOW, &callback, pool_, NULL); | |
| 636 EXPECT_EQ(ERR_IO_PENDING, rv); | |
| 637 EXPECT_FALSE(handle.is_initialized()); | |
| 638 EXPECT_FALSE(handle.socket()); | |
| 639 | |
| 640 EXPECT_EQ(OK, callback.WaitForResult()); | |
| 641 EXPECT_TRUE(handle.is_initialized()); | |
| 642 EXPECT_TRUE(handle.socket()); | |
| 643 | |
| 644 handle.Reset(); | |
| 645 | |
| 646 // Need to run all pending to release the socket back to the pool. | |
| 647 MessageLoop::current()->RunAllPending(); | |
| 648 | |
| 649 // Now we should have 1 idle socket. | |
| 650 EXPECT_EQ(1, pool_->IdleSocketCount()); | |
| 651 | |
| 652 // After an IP address change, we should have 0 idle sockets. | |
| 653 notifier_.NotifyIPAddressChange(); | |
| 654 EXPECT_EQ(0, pool_->IdleSocketCount()); | |
| 655 } | |
| 656 | |
| 657 TEST_F(TCPClientSocketPoolTest, BackupSocketConnect) { | 628 TEST_F(TCPClientSocketPoolTest, BackupSocketConnect) { |
| 658 // Case 1 tests the first socket stalling, and the backup connecting. | 629 // Case 1 tests the first socket stalling, and the backup connecting. |
| 659 MockClientSocketFactory::ClientSocketType case1_types[] = { | 630 MockClientSocketFactory::ClientSocketType case1_types[] = { |
| 660 // The first socket will not connect. | 631 // The first socket will not connect. |
| 661 MockClientSocketFactory::MOCK_STALLED_CLIENT_SOCKET, | 632 MockClientSocketFactory::MOCK_STALLED_CLIENT_SOCKET, |
| 662 // The second socket will connect more quickly. | 633 // The second socket will connect more quickly. |
| 663 MockClientSocketFactory::MOCK_CLIENT_SOCKET | 634 MockClientSocketFactory::MOCK_CLIENT_SOCKET |
| 664 }; | 635 }; |
| 665 | 636 |
| 666 // Case 2 tests the first socket being slow, so that we start the | 637 // Case 2 tests the first socket being slow, so that we start the |
| (...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 838 EXPECT_FALSE(handle.socket()); | 809 EXPECT_FALSE(handle.socket()); |
| 839 handle.Reset(); | 810 handle.Reset(); |
| 840 | 811 |
| 841 // Reset for the next case. | 812 // Reset for the next case. |
| 842 host_resolver_->set_synchronous_mode(false); | 813 host_resolver_->set_synchronous_mode(false); |
| 843 } | 814 } |
| 844 | 815 |
| 845 } // namespace | 816 } // namespace |
| 846 | 817 |
| 847 } // namespace net | 818 } // namespace net |
| OLD | NEW |