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/host_resolver_unittest.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 { |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
217 private: | 217 private: |
218 std::vector<TestSocketRequest*>* request_order_; | 218 std::vector<TestSocketRequest*>* request_order_; |
219 TestCompletionCallback callback_; | 219 TestCompletionCallback callback_; |
220 }; | 220 }; |
221 | 221 |
222 int TestSocketRequest::completion_count = 0; | 222 int TestSocketRequest::completion_count = 0; |
223 | 223 |
224 class TCPClientSocketPoolTest : public testing::Test { | 224 class TCPClientSocketPoolTest : public testing::Test { |
225 protected: | 225 protected: |
226 TCPClientSocketPoolTest() | 226 TCPClientSocketPoolTest() |
227 : pool_(new TCPClientSocketPool(kMaxSocketsPerGroup, | 227 : host_resolver_(new MockHostResolver), |
228 new HostResolver, | 228 pool_(new TCPClientSocketPool(kMaxSocketsPerGroup, |
229 &client_socket_factory_)) {} | 229 host_resolver_, |
| 230 &client_socket_factory_)) { |
| 231 // We enable caching on the mock host-resolver (it is off by default), |
| 232 // because some of the tests in this file expect it. |
| 233 host_resolver_->Reset(NULL, 100, 60000); |
| 234 } |
230 | 235 |
231 virtual void SetUp() { | 236 virtual void SetUp() { |
232 RuleBasedHostMapper *host_mapper = new RuleBasedHostMapper(); | |
233 host_mapper->AddRule("*", "127.0.0.1"); | |
234 scoped_host_mapper_.Init(host_mapper); | |
235 TestSocketRequest::completion_count = 0; | 237 TestSocketRequest::completion_count = 0; |
236 } | 238 } |
237 | 239 |
238 virtual void TearDown() { | 240 virtual void TearDown() { |
239 // The tests often call Reset() on handles at the end which may post | 241 // The tests often call Reset() on handles at the end which may post |
240 // DoReleaseSocket() tasks. | 242 // DoReleaseSocket() tasks. |
241 MessageLoop::current()->RunAllPending(); | 243 MessageLoop::current()->RunAllPending(); |
242 } | 244 } |
243 | 245 |
244 ScopedHostMapper scoped_host_mapper_; | 246 scoped_refptr<MockHostResolver> host_resolver_; |
245 MockClientSocketFactory client_socket_factory_; | 247 MockClientSocketFactory client_socket_factory_; |
246 scoped_refptr<ClientSocketPool> pool_; | 248 scoped_refptr<ClientSocketPool> pool_; |
247 std::vector<TestSocketRequest*> request_order_; | 249 std::vector<TestSocketRequest*> request_order_; |
248 }; | 250 }; |
249 | 251 |
250 TEST_F(TCPClientSocketPoolTest, Basic) { | 252 TEST_F(TCPClientSocketPoolTest, Basic) { |
251 TestCompletionCallback callback; | 253 TestCompletionCallback callback; |
252 ClientSocketHandle handle(pool_.get()); | 254 ClientSocketHandle handle(pool_.get()); |
253 HostResolver::RequestInfo info("www.google.com", 80); | 255 HostResolver::RequestInfo info("www.google.com", 80); |
254 int rv = handle.Init("a", info, 0, &callback); | 256 int rv = handle.Init("a", info, 0, &callback); |
255 EXPECT_EQ(ERR_IO_PENDING, rv); | 257 EXPECT_EQ(ERR_IO_PENDING, rv); |
256 EXPECT_FALSE(handle.is_initialized()); | 258 EXPECT_FALSE(handle.is_initialized()); |
257 EXPECT_FALSE(handle.socket()); | 259 EXPECT_FALSE(handle.socket()); |
258 | 260 |
259 EXPECT_EQ(OK, callback.WaitForResult()); | 261 EXPECT_EQ(OK, callback.WaitForResult()); |
260 EXPECT_TRUE(handle.is_initialized()); | 262 EXPECT_TRUE(handle.is_initialized()); |
261 EXPECT_TRUE(handle.socket()); | 263 EXPECT_TRUE(handle.socket()); |
262 | 264 |
263 handle.Reset(); | 265 handle.Reset(); |
264 } | 266 } |
265 | 267 |
266 TEST_F(TCPClientSocketPoolTest, InitHostResolutionFailure) { | 268 TEST_F(TCPClientSocketPoolTest, InitHostResolutionFailure) { |
267 RuleBasedHostMapper* host_mapper = new RuleBasedHostMapper; | 269 host_resolver_->rules()->AddSimulatedFailure("unresolvable.host.name"); |
268 host_mapper->AddSimulatedFailure("unresolvable.host.name"); | |
269 ScopedHostMapper scoped_host_mapper(host_mapper); | |
270 TestSocketRequest req(pool_.get(), &request_order_); | 270 TestSocketRequest req(pool_.get(), &request_order_); |
271 HostResolver::RequestInfo info("unresolvable.host.name", 80); | 271 HostResolver::RequestInfo info("unresolvable.host.name", 80); |
272 EXPECT_EQ(ERR_IO_PENDING, req.handle.Init("a", info, 5, &req)); | 272 EXPECT_EQ(ERR_IO_PENDING, req.handle.Init("a", info, 5, &req)); |
273 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, req.WaitForResult()); | 273 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, req.WaitForResult()); |
274 } | 274 } |
275 | 275 |
276 TEST_F(TCPClientSocketPoolTest, InitConnectionFailure) { | 276 TEST_F(TCPClientSocketPoolTest, InitConnectionFailure) { |
277 client_socket_factory_.set_client_socket_type( | 277 client_socket_factory_.set_client_socket_type( |
278 MockClientSocketFactory::MOCK_FAILING_CLIENT_SOCKET); | 278 MockClientSocketFactory::MOCK_FAILING_CLIENT_SOCKET); |
279 TestSocketRequest req(pool_.get(), &request_order_); | 279 TestSocketRequest req(pool_.get(), &request_order_); |
(...skipping 335 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
615 EXPECT_EQ(ERR_IO_PENDING, rv); | 615 EXPECT_EQ(ERR_IO_PENDING, rv); |
616 } | 616 } |
617 | 617 |
618 for (size_t i = 0; i < arraysize(reqs); ++i) | 618 for (size_t i = 0; i < arraysize(reqs); ++i) |
619 EXPECT_EQ(ERR_CONNECTION_FAILED, reqs[i]->WaitForResult()); | 619 EXPECT_EQ(ERR_CONNECTION_FAILED, reqs[i]->WaitForResult()); |
620 } | 620 } |
621 | 621 |
622 } // namespace | 622 } // namespace |
623 | 623 |
624 } // namespace net | 624 } // namespace net |
OLD | NEW |