| 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/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" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
| 24 | 24 |
| 25 namespace net { | 25 namespace net { |
| 26 | 26 |
| 27 namespace { | 27 namespace { |
| 28 | 28 |
| 29 const int kDefaultMaxSockets = 4; | 29 const int kDefaultMaxSockets = 4; |
| 30 const int kDefaultMaxSocketsPerGroup = 2; | 30 const int kDefaultMaxSocketsPerGroup = 2; |
| 31 const net::RequestPriority kDefaultPriority = MEDIUM; | 31 const net::RequestPriority kDefaultPriority = MEDIUM; |
| 32 | 32 |
| 33 typedef const void* TestSocketParams; | 33 struct TestSocketParams {}; |
| 34 typedef ClientSocketPoolBase<TestSocketParams> TestClientSocketPoolBase; | 34 typedef ClientSocketPoolBase<TestSocketParams> TestClientSocketPoolBase; |
| 35 | 35 |
| 36 class MockClientSocket : public ClientSocket { | 36 class MockClientSocket : public ClientSocket { |
| 37 public: | 37 public: |
| 38 MockClientSocket() : connected_(false) {} | 38 MockClientSocket() : connected_(false) {} |
| 39 | 39 |
| 40 // Socket methods: | 40 // Socket methods: |
| 41 virtual int Read( | 41 virtual int Read( |
| 42 IOBuffer* /* buf */, int /* len */, CompletionCallback* /* callback */) { | 42 IOBuffer* /* buf */, int /* len */, CompletionCallback* /* callback */) { |
| 43 return ERR_UNEXPECTED; | 43 return ERR_UNEXPECTED; |
| (...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 292 unused_idle_socket_timeout, used_idle_socket_timeout, | 292 unused_idle_socket_timeout, used_idle_socket_timeout, |
| 293 connect_job_factory, NULL) {} | 293 connect_job_factory, NULL) {} |
| 294 | 294 |
| 295 virtual int RequestSocket( | 295 virtual int RequestSocket( |
| 296 const std::string& group_name, | 296 const std::string& group_name, |
| 297 const void* params, | 297 const void* params, |
| 298 net::RequestPriority priority, | 298 net::RequestPriority priority, |
| 299 ClientSocketHandle* handle, | 299 ClientSocketHandle* handle, |
| 300 CompletionCallback* callback, | 300 CompletionCallback* callback, |
| 301 const BoundNetLog& net_log) { | 301 const BoundNetLog& net_log) { |
| 302 const TestSocketParams* casted_socket_params = |
| 303 static_cast<const TestSocketParams*>(params); |
| 302 return base_.RequestSocket( | 304 return base_.RequestSocket( |
| 303 group_name, params, priority, handle, callback, net_log); | 305 group_name, *casted_socket_params, priority, handle, callback, net_log); |
| 304 } | 306 } |
| 305 | 307 |
| 306 virtual void CancelRequest( | 308 virtual void CancelRequest( |
| 307 const std::string& group_name, | 309 const std::string& group_name, |
| 308 const ClientSocketHandle* handle) { | 310 const ClientSocketHandle* handle) { |
| 309 base_.CancelRequest(group_name, handle); | 311 base_.CancelRequest(group_name, handle); |
| 310 } | 312 } |
| 311 | 313 |
| 312 virtual void ReleaseSocket( | 314 virtual void ReleaseSocket( |
| 313 const std::string& group_name, | 315 const std::string& group_name, |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 431 pool_ = new TestClientSocketPool(max_sockets, | 433 pool_ = new TestClientSocketPool(max_sockets, |
| 432 max_sockets_per_group, | 434 max_sockets_per_group, |
| 433 histograms_, | 435 histograms_, |
| 434 unused_idle_socket_timeout, | 436 unused_idle_socket_timeout, |
| 435 used_idle_socket_timeout, | 437 used_idle_socket_timeout, |
| 436 connect_job_factory_); | 438 connect_job_factory_); |
| 437 } | 439 } |
| 438 | 440 |
| 439 int StartRequest(const std::string& group_name, | 441 int StartRequest(const std::string& group_name, |
| 440 net::RequestPriority priority) { | 442 net::RequestPriority priority) { |
| 443 TestSocketParams params; |
| 441 return StartRequestUsingPool<TestClientSocketPool, TestSocketParams>( | 444 return StartRequestUsingPool<TestClientSocketPool, TestSocketParams>( |
| 442 pool_, group_name, priority, NULL); | 445 pool_, group_name, priority, params); |
| 443 } | 446 } |
| 444 | 447 |
| 445 virtual void TearDown() { | 448 virtual void TearDown() { |
| 446 // We post all of our delayed tasks with a 2ms delay. I.e. they don't | 449 // We post all of our delayed tasks with a 2ms delay. I.e. they don't |
| 447 // actually become pending until 2ms after they have been created. In order | 450 // actually become pending until 2ms after they have been created. In order |
| 448 // to flush all tasks, we need to wait so that we know there are no | 451 // to flush all tasks, we need to wait so that we know there are no |
| 449 // soon-to-be-pending tasks waiting. | 452 // soon-to-be-pending tasks waiting. |
| 450 PlatformThread::Sleep(10); | 453 PlatformThread::Sleep(10); |
| 451 MessageLoop::current()->RunAllPending(); | 454 MessageLoop::current()->RunAllPending(); |
| 452 | 455 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 467 }; | 470 }; |
| 468 | 471 |
| 469 // Helper function which explicitly specifies the template parameters, since | 472 // Helper function which explicitly specifies the template parameters, since |
| 470 // the compiler will infer (in this case, incorrectly) that NULL is of type int. | 473 // the compiler will infer (in this case, incorrectly) that NULL is of type int. |
| 471 int InitHandle(ClientSocketHandle* handle, | 474 int InitHandle(ClientSocketHandle* handle, |
| 472 const std::string& group_name, | 475 const std::string& group_name, |
| 473 net::RequestPriority priority, | 476 net::RequestPriority priority, |
| 474 CompletionCallback* callback, | 477 CompletionCallback* callback, |
| 475 const scoped_refptr<TestClientSocketPool>& pool, | 478 const scoped_refptr<TestClientSocketPool>& pool, |
| 476 const BoundNetLog& net_log) { | 479 const BoundNetLog& net_log) { |
| 480 TestSocketParams params; |
| 477 return handle->Init<TestSocketParams, TestClientSocketPool>( | 481 return handle->Init<TestSocketParams, TestClientSocketPool>( |
| 478 group_name, NULL, priority, callback, pool, net_log); | 482 group_name, params, priority, callback, pool, net_log); |
| 479 } | 483 } |
| 480 | 484 |
| 481 // Even though a timeout is specified, it doesn't time out on a synchronous | 485 // Even though a timeout is specified, it doesn't time out on a synchronous |
| 482 // completion. | 486 // completion. |
| 483 TEST_F(ClientSocketPoolBaseTest, ConnectJob_NoTimeoutOnSynchronousCompletion) { | 487 TEST_F(ClientSocketPoolBaseTest, ConnectJob_NoTimeoutOnSynchronousCompletion) { |
| 484 TestConnectJobDelegate delegate; | 488 TestConnectJobDelegate delegate; |
| 485 ClientSocketHandle ignored; | 489 ClientSocketHandle ignored; |
| 490 TestSocketParams params; |
| 486 TestClientSocketPoolBase::Request request( | 491 TestClientSocketPoolBase::Request request( |
| 487 &ignored, NULL, kDefaultPriority, NULL, BoundNetLog()); | 492 &ignored, NULL, kDefaultPriority, params, BoundNetLog()); |
| 488 scoped_ptr<TestConnectJob> job( | 493 scoped_ptr<TestConnectJob> job( |
| 489 new TestConnectJob(TestConnectJob::kMockJob, | 494 new TestConnectJob(TestConnectJob::kMockJob, |
| 490 "a", | 495 "a", |
| 491 request, | 496 request, |
| 492 base::TimeDelta::FromMicroseconds(1), | 497 base::TimeDelta::FromMicroseconds(1), |
| 493 &delegate, | 498 &delegate, |
| 494 &client_socket_factory_, | 499 &client_socket_factory_, |
| 495 NULL)); | 500 NULL)); |
| 496 EXPECT_EQ(OK, job->Connect()); | 501 EXPECT_EQ(OK, job->Connect()); |
| 497 } | 502 } |
| 498 | 503 |
| 499 TEST_F(ClientSocketPoolBaseTest, ConnectJob_TimedOut) { | 504 TEST_F(ClientSocketPoolBaseTest, ConnectJob_TimedOut) { |
| 500 TestConnectJobDelegate delegate; | 505 TestConnectJobDelegate delegate; |
| 501 ClientSocketHandle ignored; | 506 ClientSocketHandle ignored; |
| 502 CapturingNetLog log(CapturingNetLog::kUnbounded); | 507 CapturingNetLog log(CapturingNetLog::kUnbounded); |
| 503 | 508 |
| 509 TestSocketParams params; |
| 504 TestClientSocketPoolBase::Request request( | 510 TestClientSocketPoolBase::Request request( |
| 505 &ignored, NULL, kDefaultPriority, NULL, BoundNetLog()); | 511 &ignored, NULL, kDefaultPriority, params, BoundNetLog()); |
| 506 // Deleted by TestConnectJobDelegate. | 512 // Deleted by TestConnectJobDelegate. |
| 507 TestConnectJob* job = | 513 TestConnectJob* job = |
| 508 new TestConnectJob(TestConnectJob::kMockPendingJob, | 514 new TestConnectJob(TestConnectJob::kMockPendingJob, |
| 509 "a", | 515 "a", |
| 510 request, | 516 request, |
| 511 base::TimeDelta::FromMicroseconds(1), | 517 base::TimeDelta::FromMicroseconds(1), |
| 512 &delegate, | 518 &delegate, |
| 513 &client_socket_factory_, | 519 &client_socket_factory_, |
| 514 &log); | 520 &log); |
| 515 ASSERT_EQ(ERR_IO_PENDING, job->Connect()); | 521 ASSERT_EQ(ERR_IO_PENDING, job->Connect()); |
| (...skipping 1254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1770 EXPECT_EQ(ERR_IO_PENDING, | 1776 EXPECT_EQ(ERR_IO_PENDING, |
| 1771 InitHandle(&handle, "a", kDefaultPriority, | 1777 InitHandle(&handle, "a", kDefaultPriority, |
| 1772 &callback, pool_, BoundNetLog())); | 1778 &callback, pool_, BoundNetLog())); |
| 1773 EXPECT_EQ(OK, callback.WaitForResult()); | 1779 EXPECT_EQ(OK, callback.WaitForResult()); |
| 1774 EXPECT_EQ(ClientSocketHandle::UNUSED, handle.reuse_type()); | 1780 EXPECT_EQ(ClientSocketHandle::UNUSED, handle.reuse_type()); |
| 1775 } | 1781 } |
| 1776 | 1782 |
| 1777 } // namespace | 1783 } // namespace |
| 1778 | 1784 |
| 1779 } // namespace net | 1785 } // namespace net |
| OLD | NEW |