| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
| 8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
| 9 #include "net/base/mock_host_resolver.h" | 9 #include "net/base/mock_host_resolver.h" |
| 10 #include "net/base/net_errors.h" | 10 #include "net/base/net_errors.h" |
| 11 #include "net/base/test_completion_callback.h" | 11 #include "net/base/test_completion_callback.h" |
| 12 #include "net/socket/client_socket.h" | 12 #include "net/socket/client_socket.h" |
| 13 #include "net/socket/client_socket_factory.h" | 13 #include "net/socket/client_socket_factory.h" |
| 14 #include "net/socket/client_socket_handle.h" | 14 #include "net/socket/client_socket_handle.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 16 |
| 17 namespace net { | 17 namespace net { |
| 18 | 18 |
| 19 namespace { | 19 namespace { |
| 20 | 20 |
| 21 const int kMaxSockets = 32; |
| 21 const int kMaxSocketsPerGroup = 6; | 22 const int kMaxSocketsPerGroup = 6; |
| 22 | 23 |
| 23 // Note that the first and the last are the same, the first should be handled | 24 // Note that the first and the last are the same, the first should be handled |
| 24 // before the last, since it was inserted first. | 25 // before the last, since it was inserted first. |
| 25 const int kPriorities[10] = { 1, 7, 9, 5, 6, 2, 8, 3, 4, 1 }; | 26 const int kPriorities[10] = { 1, 7, 9, 5, 6, 2, 8, 3, 4, 1 }; |
| 26 | 27 |
| 27 // This is the number of extra requests beyond the first few that use up all | 28 // This is the number of extra requests beyond the first few that use up all |
| 28 // available sockets in the socket group. | 29 // available sockets in the socket group. |
| 29 const int kNumPendingRequests = arraysize(kPriorities); | 30 const int kNumPendingRequests = arraysize(kPriorities); |
| 30 | 31 |
| (...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 218 std::vector<TestSocketRequest*>* request_order_; | 219 std::vector<TestSocketRequest*>* request_order_; |
| 219 TestCompletionCallback callback_; | 220 TestCompletionCallback callback_; |
| 220 }; | 221 }; |
| 221 | 222 |
| 222 int TestSocketRequest::completion_count = 0; | 223 int TestSocketRequest::completion_count = 0; |
| 223 | 224 |
| 224 class TCPClientSocketPoolTest : public testing::Test { | 225 class TCPClientSocketPoolTest : public testing::Test { |
| 225 protected: | 226 protected: |
| 226 TCPClientSocketPoolTest() | 227 TCPClientSocketPoolTest() |
| 227 : host_resolver_(new MockHostResolver), | 228 : host_resolver_(new MockHostResolver), |
| 228 pool_(new TCPClientSocketPool(kMaxSocketsPerGroup, | 229 pool_(new TCPClientSocketPool(kMaxSockets, |
| 230 kMaxSocketsPerGroup, |
| 229 host_resolver_, | 231 host_resolver_, |
| 230 &client_socket_factory_)) { | 232 &client_socket_factory_)) { |
| 231 } | 233 } |
| 232 | 234 |
| 233 virtual void SetUp() { | 235 virtual void SetUp() { |
| 234 TestSocketRequest::completion_count = 0; | 236 TestSocketRequest::completion_count = 0; |
| 235 } | 237 } |
| 236 | 238 |
| 237 virtual void TearDown() { | 239 virtual void TearDown() { |
| 238 // The tests often call Reset() on handles at the end which may post | 240 // The tests often call Reset() on handles at the end which may post |
| (...skipping 372 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 611 | 613 |
| 612 EXPECT_EQ(kNumPendingRequests, TestSocketRequest::completion_count); | 614 EXPECT_EQ(kNumPendingRequests, TestSocketRequest::completion_count); |
| 613 } | 615 } |
| 614 | 616 |
| 615 // Make sure that pending requests get serviced after active requests fail. | 617 // Make sure that pending requests get serviced after active requests fail. |
| 616 TEST_F(TCPClientSocketPoolTest, FailingActiveRequestWithPendingRequests) { | 618 TEST_F(TCPClientSocketPoolTest, FailingActiveRequestWithPendingRequests) { |
| 617 client_socket_factory_.set_client_socket_type( | 619 client_socket_factory_.set_client_socket_type( |
| 618 MockClientSocketFactory::MOCK_PENDING_FAILING_CLIENT_SOCKET); | 620 MockClientSocketFactory::MOCK_PENDING_FAILING_CLIENT_SOCKET); |
| 619 | 621 |
| 620 scoped_ptr<TestSocketRequest> reqs[kMaxSocketsPerGroup * 2 + 1]; | 622 scoped_ptr<TestSocketRequest> reqs[kMaxSocketsPerGroup * 2 + 1]; |
| 623 ASSERT_LE(static_cast<int>(arraysize(reqs)), kMaxSockets); |
| 621 | 624 |
| 622 // Queue up all the requests | 625 // Queue up all the requests |
| 623 | 626 |
| 624 HostResolver::RequestInfo info("www.google.com", 80); | 627 HostResolver::RequestInfo info("www.google.com", 80); |
| 625 for (size_t i = 0; i < arraysize(reqs); ++i) { | 628 for (size_t i = 0; i < arraysize(reqs); ++i) { |
| 626 reqs[i].reset(new TestSocketRequest(pool_.get(), &request_order_)); | 629 reqs[i].reset(new TestSocketRequest(pool_.get(), &request_order_)); |
| 627 int rv = reqs[i]->handle.Init("a", info, 5, reqs[i].get()); | 630 int rv = reqs[i]->handle.Init("a", info, 5, reqs[i].get()); |
| 628 EXPECT_EQ(ERR_IO_PENDING, rv); | 631 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 629 } | 632 } |
| 630 | 633 |
| 631 for (size_t i = 0; i < arraysize(reqs); ++i) | 634 for (size_t i = 0; i < arraysize(reqs); ++i) |
| 632 EXPECT_EQ(ERR_CONNECTION_FAILED, reqs[i]->WaitForResult()); | 635 EXPECT_EQ(ERR_CONNECTION_FAILED, reqs[i]->WaitForResult()); |
| 633 } | 636 } |
| 634 | 637 |
| 635 } // namespace | 638 } // namespace |
| 636 | 639 |
| 637 } // namespace net | 640 } // namespace net |
| OLD | NEW |