| 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 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 239 | 240 |
| 240 class TCPClientSocketPoolTest : public ClientSocketPoolTest { | 241 class TCPClientSocketPoolTest : public ClientSocketPoolTest { |
| 241 protected: | 242 protected: |
| 242 TCPClientSocketPoolTest() | 243 TCPClientSocketPoolTest() |
| 243 : ignored_socket_params_("ignored", 80, MEDIUM, GURL(), false), | 244 : ignored_socket_params_("ignored", 80, MEDIUM, GURL(), false), |
| 244 host_resolver_(new MockHostResolver), | 245 host_resolver_(new MockHostResolver), |
| 245 pool_(new TCPClientSocketPool(kMaxSockets, | 246 pool_(new TCPClientSocketPool(kMaxSockets, |
| 246 kMaxSocketsPerGroup, | 247 kMaxSocketsPerGroup, |
| 247 "TCPUnitTest", | 248 "TCPUnitTest", |
| 248 host_resolver_, | 249 host_resolver_, |
| 249 &client_socket_factory_)) { | 250 &client_socket_factory_, |
| 251 ¬ifier_)) { |
| 250 } | 252 } |
| 251 | 253 |
| 252 int StartRequest(const std::string& group_name, RequestPriority priority) { | 254 int StartRequest(const std::string& group_name, RequestPriority priority) { |
| 253 return StartRequestUsingPool( | 255 return StartRequestUsingPool( |
| 254 pool_, group_name, priority, ignored_socket_params_); | 256 pool_, group_name, priority, ignored_socket_params_); |
| 255 } | 257 } |
| 256 | 258 |
| 257 TCPSocketParams ignored_socket_params_; | 259 TCPSocketParams ignored_socket_params_; |
| 258 scoped_refptr<MockHostResolver> host_resolver_; | 260 scoped_refptr<MockHostResolver> host_resolver_; |
| 259 MockClientSocketFactory client_socket_factory_; | 261 MockClientSocketFactory client_socket_factory_; |
| 262 MockNetworkChangeNotifier notifier_; |
| 260 scoped_refptr<TCPClientSocketPool> pool_; | 263 scoped_refptr<TCPClientSocketPool> pool_; |
| 261 }; | 264 }; |
| 262 | 265 |
| 263 TEST_F(TCPClientSocketPoolTest, Basic) { | 266 TEST_F(TCPClientSocketPoolTest, Basic) { |
| 264 TestCompletionCallback callback; | 267 TestCompletionCallback callback; |
| 265 ClientSocketHandle handle; | 268 ClientSocketHandle handle; |
| 266 TCPSocketParams dest("www.google.com", 80, LOW, GURL(), false); | 269 TCPSocketParams dest("www.google.com", 80, LOW, GURL(), false); |
| 267 int rv = handle.Init("a", dest, LOW, &callback, pool_, NULL); | 270 int rv = handle.Init("a", dest, LOW, &callback, pool_, NULL); |
| 268 EXPECT_EQ(ERR_IO_PENDING, rv); | 271 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 269 EXPECT_FALSE(handle.is_initialized()); | 272 EXPECT_FALSE(handle.is_initialized()); |
| (...skipping 348 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 618 ASSERT_LE(kNumRequests, kMaxSockets); // Otherwise the test will hang. | 621 ASSERT_LE(kNumRequests, kMaxSockets); // Otherwise the test will hang. |
| 619 | 622 |
| 620 // Queue up all the requests | 623 // Queue up all the requests |
| 621 for (int i = 0; i < kNumRequests; i++) | 624 for (int i = 0; i < kNumRequests; i++) |
| 622 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); | 625 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); |
| 623 | 626 |
| 624 for (int i = 0; i < kNumRequests; i++) | 627 for (int i = 0; i < kNumRequests; i++) |
| 625 EXPECT_EQ(ERR_CONNECTION_FAILED, requests_[i]->WaitForResult()); | 628 EXPECT_EQ(ERR_CONNECTION_FAILED, requests_[i]->WaitForResult()); |
| 626 } | 629 } |
| 627 | 630 |
| 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 |
| 628 TEST_F(TCPClientSocketPoolTest, BackupSocketConnect) { | 657 TEST_F(TCPClientSocketPoolTest, BackupSocketConnect) { |
| 629 // Case 1 tests the first socket stalling, and the backup connecting. | 658 // Case 1 tests the first socket stalling, and the backup connecting. |
| 630 MockClientSocketFactory::ClientSocketType case1_types[] = { | 659 MockClientSocketFactory::ClientSocketType case1_types[] = { |
| 631 // The first socket will not connect. | 660 // The first socket will not connect. |
| 632 MockClientSocketFactory::MOCK_STALLED_CLIENT_SOCKET, | 661 MockClientSocketFactory::MOCK_STALLED_CLIENT_SOCKET, |
| 633 // The second socket will connect more quickly. | 662 // The second socket will connect more quickly. |
| 634 MockClientSocketFactory::MOCK_CLIENT_SOCKET | 663 MockClientSocketFactory::MOCK_CLIENT_SOCKET |
| 635 }; | 664 }; |
| 636 | 665 |
| 637 // Case 2 tests the first socket being slow, so that we start the | 666 // 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... |
| 809 EXPECT_FALSE(handle.socket()); | 838 EXPECT_FALSE(handle.socket()); |
| 810 handle.Reset(); | 839 handle.Reset(); |
| 811 | 840 |
| 812 // Reset for the next case. | 841 // Reset for the next case. |
| 813 host_resolver_->set_synchronous_mode(false); | 842 host_resolver_->set_synchronous_mode(false); |
| 814 } | 843 } |
| 815 | 844 |
| 816 } // namespace | 845 } // namespace |
| 817 | 846 |
| 818 } // namespace net | 847 } // namespace net |
| OLD | NEW |