| OLD | NEW |
| 1 // Copyright (c) 2009 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/client_socket_pool_base.h" | 5 #include "net/socket/client_socket_pool_base.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 "base/platform_thread.h" | 10 #include "base/platform_thread.h" |
| 11 #include "base/scoped_vector.h" | 11 #include "base/scoped_vector.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 22 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
| 23 | 23 |
| 24 namespace net { | 24 namespace net { |
| 25 | 25 |
| 26 namespace { | 26 namespace { |
| 27 | 27 |
| 28 const int kDefaultMaxSockets = 4; | 28 const int kDefaultMaxSockets = 4; |
| 29 const int kDefaultMaxSocketsPerGroup = 2; | 29 const int kDefaultMaxSocketsPerGroup = 2; |
| 30 const net::RequestPriority kDefaultPriority = MEDIUM; | 30 const net::RequestPriority kDefaultPriority = MEDIUM; |
| 31 | 31 |
| 32 typedef ClientSocketPoolBase<const void*> TestClientSocketPoolBase; | 32 typedef const void* TestSocketParams; |
| 33 typedef ClientSocketPoolBase<TestSocketParams> TestClientSocketPoolBase; |
| 33 | 34 |
| 34 class MockClientSocket : public ClientSocket { | 35 class MockClientSocket : public ClientSocket { |
| 35 public: | 36 public: |
| 36 MockClientSocket() : connected_(false) {} | 37 MockClientSocket() : connected_(false) {} |
| 37 | 38 |
| 38 // Socket methods: | 39 // Socket methods: |
| 39 virtual int Read( | 40 virtual int Read( |
| 40 IOBuffer* /* buf */, int /* len */, CompletionCallback* /* callback */) { | 41 IOBuffer* /* buf */, int /* len */, CompletionCallback* /* callback */) { |
| 41 return ERR_UNEXPECTED; | 42 return ERR_UNEXPECTED; |
| 42 } | 43 } |
| (...skipping 285 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 328 private: | 329 private: |
| 329 ~TestClientSocketPool() {} | 330 ~TestClientSocketPool() {} |
| 330 | 331 |
| 331 TestClientSocketPoolBase base_; | 332 TestClientSocketPoolBase base_; |
| 332 | 333 |
| 333 DISALLOW_COPY_AND_ASSIGN(TestClientSocketPool); | 334 DISALLOW_COPY_AND_ASSIGN(TestClientSocketPool); |
| 334 }; | 335 }; |
| 335 | 336 |
| 336 } // namespace | 337 } // namespace |
| 337 | 338 |
| 338 REGISTER_SOCKET_PARAMS_FOR_POOL(TestClientSocketPool, const void*); | 339 REGISTER_SOCKET_PARAMS_FOR_POOL(TestClientSocketPool, TestSocketParams); |
| 339 | 340 |
| 340 namespace { | 341 namespace { |
| 341 | 342 |
| 342 void MockClientSocketFactory::SignalJobs() { | 343 void MockClientSocketFactory::SignalJobs() { |
| 343 for (std::vector<TestConnectJob*>::iterator it = waiting_jobs_.begin(); | 344 for (std::vector<TestConnectJob*>::iterator it = waiting_jobs_.begin(); |
| 344 it != waiting_jobs_.end(); ++it) { | 345 it != waiting_jobs_.end(); ++it) { |
| 345 (*it)->Signal(); | 346 (*it)->Signal(); |
| 346 } | 347 } |
| 347 waiting_jobs_.clear(); | 348 waiting_jobs_.clear(); |
| 348 } | 349 } |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 401 connect_job_factory_ = new TestConnectJobFactory(&client_socket_factory_); | 402 connect_job_factory_ = new TestConnectJobFactory(&client_socket_factory_); |
| 402 pool_ = new TestClientSocketPool(max_sockets, | 403 pool_ = new TestClientSocketPool(max_sockets, |
| 403 max_sockets_per_group, | 404 max_sockets_per_group, |
| 404 unused_idle_socket_timeout, | 405 unused_idle_socket_timeout, |
| 405 used_idle_socket_timeout, | 406 used_idle_socket_timeout, |
| 406 connect_job_factory_); | 407 connect_job_factory_); |
| 407 } | 408 } |
| 408 | 409 |
| 409 int StartRequest(const std::string& group_name, | 410 int StartRequest(const std::string& group_name, |
| 410 net::RequestPriority priority) { | 411 net::RequestPriority priority) { |
| 411 return StartRequestUsingPool<TestClientSocketPool, const void*>( | 412 return StartRequestUsingPool<TestClientSocketPool, TestSocketParams>( |
| 412 pool_.get(), group_name, priority, NULL); | 413 pool_.get(), group_name, priority, NULL); |
| 413 } | 414 } |
| 414 | 415 |
| 415 virtual void TearDown() { | 416 virtual void TearDown() { |
| 416 // We post all of our delayed tasks with a 2ms delay. I.e. they don't | 417 // We post all of our delayed tasks with a 2ms delay. I.e. they don't |
| 417 // actually become pending until 2ms after they have been created. In order | 418 // actually become pending until 2ms after they have been created. In order |
| 418 // to flush all tasks, we need to wait so that we know there are no | 419 // to flush all tasks, we need to wait so that we know there are no |
| 419 // soon-to-be-pending tasks waiting. | 420 // soon-to-be-pending tasks waiting. |
| 420 PlatformThread::Sleep(10); | 421 PlatformThread::Sleep(10); |
| 421 MessageLoop::current()->RunAllPending(); | 422 MessageLoop::current()->RunAllPending(); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 436 }; | 437 }; |
| 437 | 438 |
| 438 // Helper function which explicitly specifies the template parameters, since | 439 // Helper function which explicitly specifies the template parameters, since |
| 439 // the compiler will infer (in this case, incorrectly) that NULL is of type int. | 440 // the compiler will infer (in this case, incorrectly) that NULL is of type int. |
| 440 int InitHandle(ClientSocketHandle* handle, | 441 int InitHandle(ClientSocketHandle* handle, |
| 441 const std::string& group_name, | 442 const std::string& group_name, |
| 442 net::RequestPriority priority, | 443 net::RequestPriority priority, |
| 443 CompletionCallback* callback, | 444 CompletionCallback* callback, |
| 444 TestClientSocketPool* pool, | 445 TestClientSocketPool* pool, |
| 445 LoadLog* load_log) { | 446 LoadLog* load_log) { |
| 446 return handle->Init<const void*, TestClientSocketPool>( | 447 return handle->Init<TestSocketParams, TestClientSocketPool>( |
| 447 group_name, NULL, priority, callback, pool, load_log); | 448 group_name, NULL, priority, callback, pool, load_log); |
| 448 } | 449 } |
| 449 | 450 |
| 450 // Even though a timeout is specified, it doesn't time out on a synchronous | 451 // Even though a timeout is specified, it doesn't time out on a synchronous |
| 451 // completion. | 452 // completion. |
| 452 TEST_F(ClientSocketPoolBaseTest, ConnectJob_NoTimeoutOnSynchronousCompletion) { | 453 TEST_F(ClientSocketPoolBaseTest, ConnectJob_NoTimeoutOnSynchronousCompletion) { |
| 453 TestConnectJobDelegate delegate; | 454 TestConnectJobDelegate delegate; |
| 454 ClientSocketHandle ignored; | 455 ClientSocketHandle ignored; |
| 455 TestClientSocketPoolBase::Request request( | 456 TestClientSocketPoolBase::Request request( |
| 456 &ignored, NULL, kDefaultPriority, NULL, NULL); | 457 &ignored, NULL, kDefaultPriority, NULL, NULL); |
| (...skipping 988 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1445 EXPECT_EQ(3, GetOrderOfRequest(3)); | 1446 EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 1446 EXPECT_EQ(4, GetOrderOfRequest(4)); | 1447 EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 1447 | 1448 |
| 1448 // Make sure we test order of all requests made. | 1449 // Make sure we test order of all requests made. |
| 1449 EXPECT_EQ(kIndexOutOfBounds, GetOrderOfRequest(5)); | 1450 EXPECT_EQ(kIndexOutOfBounds, GetOrderOfRequest(5)); |
| 1450 } | 1451 } |
| 1451 | 1452 |
| 1452 } // namespace | 1453 } // namespace |
| 1453 | 1454 |
| 1454 } // namespace net | 1455 } // namespace net |
| OLD | NEW |