diff options
author | willchan@chromium.org <willchan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-08-18 16:01:06 +0000 |
---|---|---|
committer | willchan@chromium.org <willchan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-08-18 16:01:06 +0000 |
commit | a512f598dca5030e9c02c497b36839cbdcd6e0cb (patch) | |
tree | 0d1e24b54244557c52fe1bc1eac534316f378e8c /net/socket | |
parent | e4b553615a137a007b7c073d2ac2189fad00b6eb (diff) | |
download | chromium_src-a512f598dca5030e9c02c497b36839cbdcd6e0cb.zip chromium_src-a512f598dca5030e9c02c497b36839cbdcd6e0cb.tar.gz chromium_src-a512f598dca5030e9c02c497b36839cbdcd6e0cb.tar.bz2 |
Change ClientSocketHandle to take the pool as a parameter to Init() rather than the constructor. The purpose of this change is to allow ClientSocketHandle to be able to accept ClientSocketPools of different types, since we will want to pass a TCPClientSocketPool or an SSLClientSocketPool or whatever to it.
BUG=http://crbug.com/13289
Review URL: http://codereview.chromium.org/171003
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@23629 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/socket')
-rw-r--r-- | net/socket/client_socket_handle.cc | 6 | ||||
-rw-r--r-- | net/socket/client_socket_handle.h | 11 | ||||
-rw-r--r-- | net/socket/client_socket_pool_base_unittest.cc | 197 | ||||
-rw-r--r-- | net/socket/socket_test_util.cc | 15 | ||||
-rw-r--r-- | net/socket/socket_test_util.h | 20 | ||||
-rw-r--r-- | net/socket/tcp_client_socket_pool_unittest.cc | 58 |
6 files changed, 165 insertions, 142 deletions
diff --git a/net/socket/client_socket_handle.cc b/net/socket/client_socket_handle.cc index 6e6310d..d283dd6 100644 --- a/net/socket/client_socket_handle.cc +++ b/net/socket/client_socket_handle.cc @@ -11,9 +11,8 @@ namespace net { -ClientSocketHandle::ClientSocketHandle(ClientSocketPool* pool) - : pool_(pool), - socket_(NULL), +ClientSocketHandle::ClientSocketHandle() + : socket_(NULL), is_reused_(false), ALLOW_THIS_IN_INITIALIZER_LIST( callback_(this, &ClientSocketHandle::OnIOComplete)) {} @@ -41,6 +40,7 @@ void ClientSocketHandle::ResetInternal(bool cancel) { group_name_.clear(); is_reused_ = false; user_callback_ = NULL; + pool_ = NULL; idle_time_ = base::TimeDelta(); init_time_ = base::TimeTicks(); } diff --git a/net/socket/client_socket_handle.h b/net/socket/client_socket_handle.h index 9d63a41..c58e832 100644 --- a/net/socket/client_socket_handle.h +++ b/net/socket/client_socket_handle.h @@ -34,7 +34,7 @@ class ClientSocketHandle { NUM_TYPES, } SocketReuseType; - explicit ClientSocketHandle(ClientSocketPool* pool); + ClientSocketHandle(); ~ClientSocketHandle(); // Initializes a ClientSocketHandle object, which involves talking to the @@ -56,11 +56,12 @@ class ClientSocketHandle { // // Profiling information for the request is saved to |load_log| if non-NULL. // - template <typename SocketParams> + template <typename SocketParams, typename PoolType> int Init(const std::string& group_name, const SocketParams& socket_params, int priority, CompletionCallback* callback, + PoolType* pool, LoadLog* load_log); // An initialized handle can be reset, which causes it to return to the @@ -129,14 +130,18 @@ class ClientSocketHandle { }; // Template function implementation: -template <typename SocketParams> +// TODO(willchan): Register valid (SocketParams,PoolType) pairs to provide +// type safety. +template <typename SocketParams, typename PoolType> int ClientSocketHandle::Init(const std::string& group_name, const SocketParams& socket_params, int priority, CompletionCallback* callback, + PoolType* pool, LoadLog* load_log) { CHECK(!group_name.empty()); ResetInternal(true); + pool_ = pool; group_name_ = group_name; init_time_ = base::TimeTicks::Now(); int rv = pool_->RequestSocket( diff --git a/net/socket/client_socket_pool_base_unittest.cc b/net/socket/client_socket_pool_base_unittest.cc index 1f20c76..840ca36 100644 --- a/net/socket/client_socket_pool_base_unittest.cc +++ b/net/socket/client_socket_pool_base_unittest.cc @@ -380,7 +380,7 @@ class ClientSocketPoolBaseTest : public ClientSocketPoolTest { // completion. TEST_F(ClientSocketPoolBaseTest, ConnectJob_NoTimeoutOnSynchronousCompletion) { TestConnectJobDelegate delegate; - ClientSocketHandle ignored(NULL); + ClientSocketHandle ignored; TestClientSocketPoolBase::Request request( &ignored, NULL, kDefaultPriority, NULL, NULL); scoped_ptr<TestConnectJob> job( @@ -395,7 +395,7 @@ TEST_F(ClientSocketPoolBaseTest, ConnectJob_NoTimeoutOnSynchronousCompletion) { TEST_F(ClientSocketPoolBaseTest, ConnectJob_TimedOut) { TestConnectJobDelegate delegate; - ClientSocketHandle ignored(NULL); + ClientSocketHandle ignored; TestClientSocketPoolBase::Request request( &ignored, NULL, kDefaultPriority, NULL, NULL); // Deleted by TestConnectJobDelegate. @@ -415,9 +415,9 @@ TEST_F(ClientSocketPoolBaseTest, BasicSynchronous) { CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); TestCompletionCallback callback; - ClientSocketHandle handle(pool_.get()); - EXPECT_EQ(OK, handle.Init("a", ignored_request_info_, - kDefaultPriority, &callback, NULL)); + ClientSocketHandle handle; + EXPECT_EQ(OK, handle.Init("a", ignored_request_info_, kDefaultPriority, + &callback, pool_.get(), NULL)); EXPECT_TRUE(handle.is_initialized()); EXPECT_TRUE(handle.socket()); handle.Reset(); @@ -427,8 +427,9 @@ TEST_F(ClientSocketPoolBaseTest, BasicAsynchronous) { CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); - TestSocketRequest req(pool_.get(), &request_order_, &completion_count_); - int rv = req.handle()->Init("a", ignored_request_info_, 0, &req, NULL); + TestSocketRequest req(&request_order_, &completion_count_); + int rv = req.handle()->Init( + "a", ignored_request_info_, 0, &req, pool_.get(), NULL); EXPECT_EQ(ERR_IO_PENDING, rv); EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", req.handle())); EXPECT_EQ(OK, req.WaitForResult()); @@ -441,20 +442,20 @@ TEST_F(ClientSocketPoolBaseTest, InitConnectionFailure) { CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); - TestSocketRequest req(pool_.get(), &request_order_, &completion_count_); + TestSocketRequest req(&request_order_, &completion_count_); EXPECT_EQ(ERR_CONNECTION_FAILED, req.handle()->Init("a", ignored_request_info_, - kDefaultPriority, &req, NULL)); + kDefaultPriority, &req, pool_.get(), NULL)); } TEST_F(ClientSocketPoolBaseTest, InitConnectionAsynchronousFailure) { CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); - TestSocketRequest req(pool_.get(), &request_order_, &completion_count_); + TestSocketRequest req(&request_order_, &completion_count_); EXPECT_EQ(ERR_IO_PENDING, - req.handle()->Init("a", ignored_request_info_, - kDefaultPriority, &req, NULL)); + req.handle()->Init("a", ignored_request_info_, kDefaultPriority, + &req, pool_.get(), NULL)); EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", req.handle())); EXPECT_EQ(ERR_CONNECTION_FAILED, req.WaitForResult()); } @@ -743,10 +744,10 @@ TEST_F(ClientSocketPoolBaseTest, CancelRequestClearGroup) { CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); - TestSocketRequest req(pool_.get(), &request_order_, &completion_count_); + TestSocketRequest req(&request_order_, &completion_count_); EXPECT_EQ(ERR_IO_PENDING, req.handle()->Init("a", ignored_request_info_, - kDefaultPriority, &req, NULL)); + kDefaultPriority, &req, pool_.get(), NULL)); req.handle()->Reset(); } @@ -754,15 +755,15 @@ TEST_F(ClientSocketPoolBaseTest, TwoRequestsCancelOne) { CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); - TestSocketRequest req(pool_.get(), &request_order_, &completion_count_); - TestSocketRequest req2(pool_.get(), &request_order_, &completion_count_); + TestSocketRequest req(&request_order_, &completion_count_); + TestSocketRequest req2(&request_order_, &completion_count_); EXPECT_EQ(ERR_IO_PENDING, req.handle()->Init("a", ignored_request_info_, - kDefaultPriority, &req, NULL)); + kDefaultPriority, &req, pool_.get(), NULL)); EXPECT_EQ(ERR_IO_PENDING, req2.handle()->Init("a", ignored_request_info_, - kDefaultPriority, &req2, NULL)); + kDefaultPriority, &req2, pool_.get(), NULL)); req.handle()->Reset(); @@ -774,20 +775,20 @@ TEST_F(ClientSocketPoolBaseTest, ConnectCancelConnect) { CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); - ClientSocketHandle handle(pool_.get()); + ClientSocketHandle handle; TestCompletionCallback callback; - TestSocketRequest req(pool_.get(), &request_order_, &completion_count_); + TestSocketRequest req(&request_order_, &completion_count_); EXPECT_EQ(ERR_IO_PENDING, handle.Init("a", ignored_request_info_, - kDefaultPriority, &callback, NULL)); + kDefaultPriority, &callback, pool_.get(), NULL)); handle.Reset(); TestCompletionCallback callback2; EXPECT_EQ(ERR_IO_PENDING, handle.Init("a", ignored_request_info_, - kDefaultPriority, &callback2, NULL)); + kDefaultPriority, &callback2, pool_.get(), NULL)); EXPECT_EQ(OK, callback2.WaitForResult()); EXPECT_FALSE(callback.have_result()); @@ -833,9 +834,11 @@ TEST_F(ClientSocketPoolBaseTest, CancelRequest) { class RequestSocketCallback : public CallbackRunner< Tuple1<int> > { public: RequestSocketCallback(ClientSocketHandle* handle, + ClientSocketPool* pool, TestConnectJobFactory* test_connect_job_factory, TestConnectJob::JobType next_job_type) : handle_(handle), + pool_(pool), within_callback_(false), test_connect_job_factory_(test_connect_job_factory), next_job_type_(next_job_type) {} @@ -850,7 +853,7 @@ class RequestSocketCallback : public CallbackRunner< Tuple1<int> > { within_callback_ = true; int rv = handle_->Init( "a", HostResolver::RequestInfo("www.google.com", 80), - kDefaultPriority, this, NULL); + kDefaultPriority, this, pool_.get(), NULL); switch (next_job_type_) { case TestConnectJob::kMockJob: EXPECT_EQ(OK, rv); @@ -871,6 +874,7 @@ class RequestSocketCallback : public CallbackRunner< Tuple1<int> > { private: ClientSocketHandle* const handle_; + const scoped_refptr<ClientSocketPool> pool_; bool within_callback_; TestConnectJobFactory* const test_connect_job_factory_; TestConnectJob::JobType next_job_type_; @@ -881,11 +885,13 @@ TEST_F(ClientSocketPoolBaseTest, RequestPendingJobTwice) { CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); - ClientSocketHandle handle(pool_.get()); + ClientSocketHandle handle; RequestSocketCallback callback( - &handle, connect_job_factory_, TestConnectJob::kMockPendingJob); + &handle, pool_.get(), connect_job_factory_, + TestConnectJob::kMockPendingJob); int rv = handle.Init( - "a", ignored_request_info_, kDefaultPriority, &callback, NULL); + "a", ignored_request_info_, kDefaultPriority, &callback, + pool_.get(), NULL); ASSERT_EQ(ERR_IO_PENDING, rv); EXPECT_EQ(OK, callback.WaitForResult()); @@ -896,11 +902,12 @@ TEST_F(ClientSocketPoolBaseTest, RequestPendingJobThenSynchronous) { CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); - ClientSocketHandle handle(pool_.get()); + ClientSocketHandle handle; RequestSocketCallback callback( - &handle, connect_job_factory_, TestConnectJob::kMockJob); + &handle, pool_.get(), connect_job_factory_, TestConnectJob::kMockJob); int rv = handle.Init( - "a", ignored_request_info_, kDefaultPriority, &callback, NULL); + "a", ignored_request_info_, kDefaultPriority, &callback, + pool_.get(), NULL); ASSERT_EQ(ERR_IO_PENDING, rv); EXPECT_EQ(OK, callback.WaitForResult()); @@ -961,16 +968,16 @@ TEST_F(ClientSocketPoolBaseTest, CancelActiveRequestThenRequestSocket) { connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); - TestSocketRequest req(pool_.get(), &request_order_, &completion_count_); + TestSocketRequest req(&request_order_, &completion_count_); int rv = req.handle()->Init( - "a", ignored_request_info_, kDefaultPriority, &req, NULL); + "a", ignored_request_info_, kDefaultPriority, &req, pool_.get(), NULL); EXPECT_EQ(ERR_IO_PENDING, rv); // Cancel the active request. req.handle()->Reset(); rv = req.handle()->Init( - "a", ignored_request_info_, kDefaultPriority, &req, NULL); + "a", ignored_request_info_, kDefaultPriority, &req, pool_.get(), NULL); EXPECT_EQ(ERR_IO_PENDING, rv); EXPECT_EQ(OK, req.WaitForResult()); @@ -989,24 +996,24 @@ TEST_F(ClientSocketPoolBaseTest, PendingJobCompletionOrder) { connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); // Start job 1 (async error). - TestSocketRequest req1(pool_.get(), &request_order_, &completion_count_); + TestSocketRequest req1(&request_order_, &completion_count_); int rv = req1.handle()->Init("a", ignored_request_info_, - kDefaultPriority, &req1, NULL); + kDefaultPriority, &req1, pool_.get(), NULL); EXPECT_EQ(ERR_IO_PENDING, rv); // Start job 2 (async error). - TestSocketRequest req2(pool_.get(), &request_order_, &completion_count_); + TestSocketRequest req2(&request_order_, &completion_count_); rv = req2.handle()->Init( - "a", ignored_request_info_, kDefaultPriority, &req2, NULL); + "a", ignored_request_info_, kDefaultPriority, &req2, pool_.get(), NULL); EXPECT_EQ(ERR_IO_PENDING, rv); // The pending job is sync. connect_job_factory_->set_job_type(TestConnectJob::kMockJob); // Request 3 does not have a ConnectJob yet. It's just pending. - TestSocketRequest req3(pool_.get(), &request_order_, &completion_count_); + TestSocketRequest req3(&request_order_, &completion_count_); rv = req3.handle()->Init( - "a", ignored_request_info_, kDefaultPriority, &req3, NULL); + "a", ignored_request_info_, kDefaultPriority, &req3, pool_.get(), NULL); EXPECT_EQ(ERR_IO_PENDING, rv); EXPECT_EQ(ERR_CONNECTION_FAILED, req1.WaitForResult()); @@ -1032,18 +1039,18 @@ TEST_F(ClientSocketPoolBaseTest, ReleaseSockets) { // Start job 1 (async OK) connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); - TestSocketRequest req1(pool_.get(), &request_order_, &completion_count_); - int rv = req1.handle()->Init("a", ignored_request_info_, - kDefaultPriority, &req1, NULL); + TestSocketRequest req1(&request_order_, &completion_count_); + int rv = req1.handle()->Init("a", ignored_request_info_, kDefaultPriority, + &req1, pool_.get(), NULL); EXPECT_EQ(ERR_IO_PENDING, rv); EXPECT_EQ(OK, req1.WaitForResult()); // Job 1 finished OK. Start job 2 (also async OK). Release socket 1. connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); - TestSocketRequest req2(pool_.get(), &request_order_, &completion_count_); + TestSocketRequest req2(&request_order_, &completion_count_); rv = req2.handle()->Init( - "a", ignored_request_info_, kDefaultPriority, &req2, NULL); + "a", ignored_request_info_, kDefaultPriority, &req2, pool_.get(), NULL); EXPECT_EQ(ERR_IO_PENDING, rv); req1.handle()->Reset(); MessageLoop::current()->RunAllPending(); // Run the DoReleaseSocket() @@ -1051,9 +1058,9 @@ TEST_F(ClientSocketPoolBaseTest, ReleaseSockets) { // Job 2 is pending. Start request 3 (which has no associated job since it // will use the idle socket). - TestSocketRequest req3(pool_.get(), &request_order_, &completion_count_); + TestSocketRequest req3(&request_order_, &completion_count_); rv = req3.handle()->Init( - "a", ignored_request_info_, kDefaultPriority, &req3, NULL); + "a", ignored_request_info_, kDefaultPriority, &req3, pool_.get(), NULL); EXPECT_EQ(OK, rv); EXPECT_FALSE(req2.handle()->socket()); @@ -1114,7 +1121,7 @@ class ClientSocketPoolBaseTest_LateBinding : public ClientSocketPoolBaseTest { TEST_F(ClientSocketPoolBaseTest_LateBinding, ConnectJob_NoTimeoutOnSynchronousCompletion) { TestConnectJobDelegate delegate; - ClientSocketHandle ignored(pool_.get()); + ClientSocketHandle ignored; TestClientSocketPoolBase::Request request(&ignored, NULL, 0, NULL, NULL); scoped_ptr<TestConnectJob> job( new TestConnectJob(TestConnectJob::kMockJob, @@ -1128,7 +1135,7 @@ TEST_F(ClientSocketPoolBaseTest_LateBinding, TEST_F(ClientSocketPoolBaseTest_LateBinding, ConnectJob_TimedOut) { TestConnectJobDelegate delegate; - ClientSocketHandle ignored(pool_.get()); + ClientSocketHandle ignored; TestClientSocketPoolBase::Request request(&ignored, NULL, 0, NULL, NULL); // Deleted by TestConnectJobDelegate. TestConnectJob* job = @@ -1147,9 +1154,9 @@ TEST_F(ClientSocketPoolBaseTest_LateBinding, BasicSynchronous) { CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); TestCompletionCallback callback; - ClientSocketHandle handle(pool_.get()); + ClientSocketHandle handle; EXPECT_EQ(OK, handle.Init("a", ignored_request_info_, kDefaultPriority, - &callback, NULL)); + &callback, pool_.get(), NULL)); EXPECT_TRUE(handle.is_initialized()); EXPECT_TRUE(handle.socket()); handle.Reset(); @@ -1159,8 +1166,9 @@ TEST_F(ClientSocketPoolBaseTest_LateBinding, BasicAsynchronous) { CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); - TestSocketRequest req(pool_.get(), &request_order_, &completion_count_); - int rv = req.handle()->Init("a", ignored_request_info_, 0, &req, NULL); + TestSocketRequest req(&request_order_, &completion_count_); + int rv = req.handle()->Init("a", ignored_request_info_, 0, &req, + pool_.get(), NULL); EXPECT_EQ(ERR_IO_PENDING, rv); EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", req.handle())); EXPECT_EQ(OK, req.WaitForResult()); @@ -1173,10 +1181,10 @@ TEST_F(ClientSocketPoolBaseTest_LateBinding, InitConnectionFailure) { CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob); - TestSocketRequest req(pool_.get(), &request_order_, &completion_count_); + TestSocketRequest req(&request_order_, &completion_count_); EXPECT_EQ(ERR_CONNECTION_FAILED, req.handle()->Init("a", ignored_request_info_, - kDefaultPriority, &req, NULL)); + kDefaultPriority, &req, pool_.get(), NULL)); } TEST_F(ClientSocketPoolBaseTest_LateBinding, @@ -1184,10 +1192,10 @@ TEST_F(ClientSocketPoolBaseTest_LateBinding, CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); - TestSocketRequest req(pool_.get(), &request_order_, &completion_count_); + TestSocketRequest req(&request_order_, &completion_count_); EXPECT_EQ(ERR_IO_PENDING, - req.handle()->Init("a", ignored_request_info_, - kDefaultPriority, &req, NULL)); + req.handle()->Init("a", ignored_request_info_, kDefaultPriority, + &req, pool_.get(), NULL)); EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", req.handle())); EXPECT_EQ(ERR_CONNECTION_FAILED, req.WaitForResult()); } @@ -1249,10 +1257,10 @@ TEST_F(ClientSocketPoolBaseTest_LateBinding, CancelRequestClearGroup) { CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); - TestSocketRequest req(pool_.get(), &request_order_, &completion_count_); + TestSocketRequest req(&request_order_, &completion_count_); EXPECT_EQ(ERR_IO_PENDING, req.handle()->Init("a", ignored_request_info_, - kDefaultPriority, &req, NULL)); + kDefaultPriority, &req, pool_.get(), NULL)); req.handle()->Reset(); } @@ -1260,15 +1268,15 @@ TEST_F(ClientSocketPoolBaseTest_LateBinding, TwoRequestsCancelOne) { CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); - TestSocketRequest req(pool_.get(), &request_order_, &completion_count_); - TestSocketRequest req2(pool_.get(), &request_order_, &completion_count_); + TestSocketRequest req(&request_order_, &completion_count_); + TestSocketRequest req2(&request_order_, &completion_count_); EXPECT_EQ(ERR_IO_PENDING, req.handle()->Init("a", ignored_request_info_, - kDefaultPriority, &req, NULL)); + kDefaultPriority, &req, pool_.get(), NULL)); EXPECT_EQ(ERR_IO_PENDING, req2.handle()->Init("a", ignored_request_info_, - kDefaultPriority, &req2, NULL)); + kDefaultPriority, &req2, pool_.get(), NULL)); req.handle()->Reset(); @@ -1280,20 +1288,20 @@ TEST_F(ClientSocketPoolBaseTest_LateBinding, ConnectCancelConnect) { CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); - ClientSocketHandle handle(pool_.get()); + ClientSocketHandle handle; TestCompletionCallback callback; - TestSocketRequest req(pool_.get(), &request_order_, &completion_count_); + TestSocketRequest req(&request_order_, &completion_count_); EXPECT_EQ(ERR_IO_PENDING, handle.Init("a", ignored_request_info_, - kDefaultPriority, &callback, NULL)); + kDefaultPriority, &callback, pool_.get(), NULL)); handle.Reset(); TestCompletionCallback callback2; EXPECT_EQ(ERR_IO_PENDING, handle.Init("a", ignored_request_info_, - kDefaultPriority, &callback2, NULL)); + kDefaultPriority, &callback2, pool_.get(), NULL)); EXPECT_EQ(OK, callback2.WaitForResult()); EXPECT_FALSE(callback.have_result()); @@ -1362,11 +1370,13 @@ TEST_F(ClientSocketPoolBaseTest_LateBinding, RequestPendingJobTwice) { CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); - ClientSocketHandle handle(pool_.get()); + ClientSocketHandle handle; RequestSocketCallback callback( - &handle, connect_job_factory_, TestConnectJob::kMockPendingJob); + &handle, pool_.get(), connect_job_factory_, + TestConnectJob::kMockPendingJob); int rv = handle.Init( - "a", ignored_request_info_, kDefaultPriority, &callback, NULL); + "a", ignored_request_info_, kDefaultPriority, &callback, + pool_.get(), NULL); ASSERT_EQ(ERR_IO_PENDING, rv); EXPECT_EQ(OK, callback.WaitForResult()); @@ -1377,11 +1387,12 @@ TEST_F(ClientSocketPoolBaseTest_LateBinding, RequestPendingJobThenSynchronous) { CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); - ClientSocketHandle handle(pool_.get()); + ClientSocketHandle handle; RequestSocketCallback callback( - &handle, connect_job_factory_, TestConnectJob::kMockJob); + &handle, pool_.get(), connect_job_factory_, TestConnectJob::kMockJob); int rv = handle.Init( - "a", ignored_request_info_, kDefaultPriority, &callback, NULL); + "a", ignored_request_info_, kDefaultPriority, &callback, + pool_.get(), NULL); ASSERT_EQ(ERR_IO_PENDING, rv); EXPECT_EQ(OK, callback.WaitForResult()); @@ -1445,16 +1456,16 @@ TEST_F(ClientSocketPoolBaseTest_LateBinding, connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); - TestSocketRequest req(pool_.get(), &request_order_, &completion_count_); + TestSocketRequest req(&request_order_, &completion_count_); int rv = req.handle()->Init( - "a", ignored_request_info_, kDefaultPriority, &req, NULL); + "a", ignored_request_info_, kDefaultPriority, &req, pool_.get(), NULL); EXPECT_EQ(ERR_IO_PENDING, rv); // Cancel the active request. req.handle()->Reset(); rv = req.handle()->Init( - "a", ignored_request_info_, kDefaultPriority, &req, NULL); + "a", ignored_request_info_, kDefaultPriority, &req, pool_.get(), NULL); EXPECT_EQ(ERR_IO_PENDING, rv); EXPECT_EQ(OK, req.WaitForResult()); @@ -1471,9 +1482,9 @@ TEST_F(ClientSocketPoolBaseTest_LateBinding, ReleaseSockets) { // Start job 1 (async OK) connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); - TestSocketRequest req1(pool_.get(), &request_order_, &completion_count_); - int rv = req1.handle()->Init("a", ignored_request_info_, - kDefaultPriority, &req1, NULL); + TestSocketRequest req1(&request_order_, &completion_count_); + int rv = req1.handle()->Init("a", ignored_request_info_, kDefaultPriority, + &req1, pool_.get(), NULL); EXPECT_EQ(ERR_IO_PENDING, rv); EXPECT_EQ(OK, req1.WaitForResult()); @@ -1481,13 +1492,13 @@ TEST_F(ClientSocketPoolBaseTest_LateBinding, ReleaseSockets) { // without a job. connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); - TestSocketRequest req2(pool_.get(), &request_order_, &completion_count_); + TestSocketRequest req2(&request_order_, &completion_count_); rv = req2.handle()->Init( - "a", ignored_request_info_, kDefaultPriority, &req2, NULL); + "a", ignored_request_info_, kDefaultPriority, &req2, pool_.get(), NULL); EXPECT_EQ(ERR_IO_PENDING, rv); - TestSocketRequest req3(pool_.get(), &request_order_, &completion_count_); + TestSocketRequest req3(&request_order_, &completion_count_); rv = req3.handle()->Init( - "a", ignored_request_info_, kDefaultPriority, &req3, NULL); + "a", ignored_request_info_, kDefaultPriority, &req3, pool_.get(), NULL); EXPECT_EQ(ERR_IO_PENDING, rv); // Both Requests 2 and 3 are pending. We release socket 1 which should @@ -1517,22 +1528,22 @@ TEST_F(ClientSocketPoolBaseTest_LateBinding, PendingJobCompletionOrder) { // First two jobs are async. connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob); - TestSocketRequest req1(pool_.get(), &request_order_, &completion_count_); + TestSocketRequest req1(&request_order_, &completion_count_); int rv = req1.handle()->Init( - "a", ignored_request_info_, kDefaultPriority, &req1, NULL); + "a", ignored_request_info_, kDefaultPriority, &req1, pool_.get(), NULL); EXPECT_EQ(ERR_IO_PENDING, rv); - TestSocketRequest req2(pool_.get(), &request_order_, &completion_count_); + TestSocketRequest req2(&request_order_, &completion_count_); rv = req2.handle()->Init( - "a", ignored_request_info_, kDefaultPriority, &req2, NULL); + "a", ignored_request_info_, kDefaultPriority, &req2, pool_.get(), NULL); EXPECT_EQ(ERR_IO_PENDING, rv); // The pending job is sync. connect_job_factory_->set_job_type(TestConnectJob::kMockJob); - TestSocketRequest req3(pool_.get(), &request_order_, &completion_count_); + TestSocketRequest req3(&request_order_, &completion_count_); rv = req3.handle()->Init( - "a", ignored_request_info_, kDefaultPriority, &req3, NULL); + "a", ignored_request_info_, kDefaultPriority, &req3, pool_.get(), NULL); EXPECT_EQ(ERR_IO_PENDING, rv); EXPECT_EQ(ERR_CONNECTION_FAILED, req1.WaitForResult()); @@ -1550,17 +1561,17 @@ TEST_F(ClientSocketPoolBaseTest_LateBinding, DISABLED_LoadState) { connect_job_factory_->set_job_type( TestConnectJob::kMockAdvancingLoadStateJob); - TestSocketRequest req1(pool_.get(), &request_order_, &completion_count_); - int rv = req1.handle()->Init( - "a", ignored_request_info_, kDefaultPriority, &req1, NULL); + TestSocketRequest req1(&request_order_, &completion_count_); + int rv = req1.handle()->Init("a", ignored_request_info_, kDefaultPriority, + &req1, pool_.get(), NULL); EXPECT_EQ(ERR_IO_PENDING, rv); EXPECT_EQ(LOAD_STATE_IDLE, req1.handle()->GetLoadState()); MessageLoop::current()->RunAllPending(); - TestSocketRequest req2(pool_.get(), &request_order_, &completion_count_); + TestSocketRequest req2(&request_order_, &completion_count_); rv = req2.handle()->Init( - "a", ignored_request_info_, kDefaultPriority, &req2, NULL); + "a", ignored_request_info_, kDefaultPriority, &req2, pool_.get(), NULL); EXPECT_EQ(ERR_IO_PENDING, rv); EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, req1.handle()->GetLoadState()); EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, req2.handle()->GetLoadState()); diff --git a/net/socket/socket_test_util.cc b/net/socket/socket_test_util.cc index 30927ad..b1af59f 100644 --- a/net/socket/socket_test_util.cc +++ b/net/socket/socket_test_util.cc @@ -329,21 +329,6 @@ void ClientSocketPoolTest::TearDown() { MessageLoop::current()->RunAllPending(); } -int ClientSocketPoolTest::StartRequestUsingPool(ClientSocketPool* socket_pool, - const std::string& group_name, - int priority) { - DCHECK(socket_pool); - TestSocketRequest* request = new TestSocketRequest(socket_pool, - &request_order_, - &completion_count_); - requests_.push_back(request); - int rv = request->handle()->Init(group_name, ignored_request_info_, - priority, request, NULL); - if (rv != ERR_IO_PENDING) - request_order_.push_back(request); - return rv; -} - int ClientSocketPoolTest::GetOrderOfRequest(size_t index) { index--; if (index < 0 || index >= requests_.size()) diff --git a/net/socket/socket_test_util.h b/net/socket/socket_test_util.h index a4973d8..8307d44 100644 --- a/net/socket/socket_test_util.h +++ b/net/socket/socket_test_util.h @@ -297,11 +297,9 @@ class MockSSLClientSocket : public MockClientSocket { class TestSocketRequest : public CallbackRunner< Tuple1<int> > { public: TestSocketRequest( - ClientSocketPool* pool, std::vector<TestSocketRequest*>* request_order, size_t* completion_count) - : handle_(pool), - request_order_(request_order), + : request_order_(request_order), completion_count_(completion_count) { DCHECK(request_order); DCHECK(completion_count); @@ -337,9 +335,21 @@ class ClientSocketPoolTest : public testing::Test { virtual void SetUp(); virtual void TearDown(); - int StartRequestUsingPool(ClientSocketPool* socket_pool, + template <typename PoolType> + int StartRequestUsingPool(PoolType* socket_pool, const std::string& group_name, - int priority); + int priority) { + DCHECK(socket_pool); + TestSocketRequest* request = new TestSocketRequest(&request_order_, + &completion_count_); + requests_.push_back(request); + int rv = request->handle()->Init( + group_name, ignored_request_info_, priority, request, + socket_pool, NULL); + if (rv != ERR_IO_PENDING) + request_order_.push_back(request); + return rv; + } // Provided there were n requests started, takes |index| in range 1..n // and returns order in which that request completed, in range 1..n, diff --git a/net/socket/tcp_client_socket_pool_unittest.cc b/net/socket/tcp_client_socket_pool_unittest.cc index 9dd3974..4d679f9 100644 --- a/net/socket/tcp_client_socket_pool_unittest.cc +++ b/net/socket/tcp_client_socket_pool_unittest.cc @@ -202,14 +202,14 @@ class TCPClientSocketPoolTest : public ClientSocketPoolTest { scoped_refptr<MockHostResolver> host_resolver_; MockClientSocketFactory client_socket_factory_; - scoped_refptr<ClientSocketPool> pool_; + scoped_refptr<TCPClientSocketPool> pool_; }; TEST_F(TCPClientSocketPoolTest, Basic) { TestCompletionCallback callback; - ClientSocketHandle handle(pool_.get()); + ClientSocketHandle handle; HostResolver::RequestInfo info("www.google.com", 80); - int rv = handle.Init("a", info, 0, &callback, NULL); + int rv = handle.Init("a", info, 0, &callback, pool_.get(), NULL); EXPECT_EQ(ERR_IO_PENDING, rv); EXPECT_FALSE(handle.is_initialized()); EXPECT_FALSE(handle.socket()); @@ -223,26 +223,29 @@ TEST_F(TCPClientSocketPoolTest, Basic) { TEST_F(TCPClientSocketPoolTest, InitHostResolutionFailure) { host_resolver_->rules()->AddSimulatedFailure("unresolvable.host.name"); - TestSocketRequest req(pool_.get(), &request_order_, &completion_count_); + TestSocketRequest req(&request_order_, &completion_count_); HostResolver::RequestInfo info("unresolvable.host.name", 80); EXPECT_EQ(ERR_IO_PENDING, - req.handle()->Init("a", info, kDefaultPriority, &req, NULL)); + req.handle()->Init( + "a", info, kDefaultPriority, &req, pool_.get(), NULL)); EXPECT_EQ(ERR_NAME_NOT_RESOLVED, req.WaitForResult()); } TEST_F(TCPClientSocketPoolTest, InitConnectionFailure) { client_socket_factory_.set_client_socket_type( MockClientSocketFactory::MOCK_FAILING_CLIENT_SOCKET); - TestSocketRequest req(pool_.get(), &request_order_, &completion_count_); + TestSocketRequest req(&request_order_, &completion_count_); HostResolver::RequestInfo info("a", 80); EXPECT_EQ(ERR_IO_PENDING, - req.handle()->Init("a", info, kDefaultPriority, &req, NULL)); + req.handle()->Init( + "a", info, kDefaultPriority, &req, pool_.get(), NULL)); EXPECT_EQ(ERR_CONNECTION_FAILED, req.WaitForResult()); // Make the host resolutions complete synchronously this time. host_resolver_->set_synchronous_mode(true); EXPECT_EQ(ERR_CONNECTION_FAILED, - req.handle()->Init("a", info, kDefaultPriority, &req, NULL)); + req.handle()->Init( + "a", info, kDefaultPriority, &req, pool_.get(), NULL)); } TEST_F(TCPClientSocketPoolTest, PendingRequests) { @@ -345,10 +348,11 @@ TEST_F(TCPClientSocketPoolTest, PendingRequests_NoKeepAlive) { // The pending host resolution will eventually complete, and destroy the // ClientSocketPool which will crash if the group was not cleared properly. TEST_F(TCPClientSocketPoolTest, CancelRequestClearGroup) { - TestSocketRequest req(pool_.get(), &request_order_, &completion_count_); + TestSocketRequest req(&request_order_, &completion_count_); HostResolver::RequestInfo info("www.google.com", 80); EXPECT_EQ(ERR_IO_PENDING, - req.handle()->Init("a", info, kDefaultPriority, &req, NULL)); + req.handle()->Init( + "a", info, kDefaultPriority, &req, pool_.get(), NULL)); req.handle()->Reset(); PlatformThread::Sleep(100); @@ -362,14 +366,16 @@ TEST_F(TCPClientSocketPoolTest, CancelRequestClearGroup) { } TEST_F(TCPClientSocketPoolTest, TwoRequestsCancelOne) { - TestSocketRequest req(pool_.get(), &request_order_, &completion_count_); - TestSocketRequest req2(pool_.get(), &request_order_, &completion_count_); + TestSocketRequest req(&request_order_, &completion_count_); + TestSocketRequest req2(&request_order_, &completion_count_); HostResolver::RequestInfo info("www.google.com", 80); EXPECT_EQ(ERR_IO_PENDING, - req.handle()->Init("a", info, kDefaultPriority, &req, NULL)); + req.handle()->Init( + "a", info, kDefaultPriority, &req, pool_.get(), NULL)); EXPECT_EQ(ERR_IO_PENDING, - req2.handle()->Init("a", info, kDefaultPriority, &req2, NULL)); + req2.handle()->Init( + "a", info, kDefaultPriority, &req2, pool_.get(), NULL)); req.handle()->Reset(); @@ -380,19 +386,21 @@ TEST_F(TCPClientSocketPoolTest, TwoRequestsCancelOne) { TEST_F(TCPClientSocketPoolTest, ConnectCancelConnect) { client_socket_factory_.set_client_socket_type( MockClientSocketFactory::MOCK_PENDING_CLIENT_SOCKET); - ClientSocketHandle handle(pool_.get()); + ClientSocketHandle handle; TestCompletionCallback callback; - TestSocketRequest req(pool_.get(), &request_order_, &completion_count_); + TestSocketRequest req(&request_order_, &completion_count_); HostResolver::RequestInfo info("www.google.com", 80); EXPECT_EQ(ERR_IO_PENDING, - handle.Init("a", info, kDefaultPriority, &callback, NULL)); + handle.Init( + "a", info, kDefaultPriority, &callback, pool_.get(), NULL)); handle.Reset(); TestCompletionCallback callback2; EXPECT_EQ(ERR_IO_PENDING, - handle.Init("a", info, kDefaultPriority, &callback2, NULL)); + handle.Init( + "a", info, kDefaultPriority, &callback2, pool_.get(), NULL)); host_resolver_->set_synchronous_mode(true); // At this point, handle has two ConnectingSockets out for it. Due to the @@ -470,8 +478,9 @@ TEST_F(TCPClientSocketPoolTest, CancelRequest) { class RequestSocketCallback : public CallbackRunner< Tuple1<int> > { public: - explicit RequestSocketCallback(ClientSocketHandle* handle) + RequestSocketCallback(ClientSocketHandle* handle, TCPClientSocketPool* pool) : handle_(handle), + pool_(pool), within_callback_(false) {} virtual void RunWithParams(const Tuple1<int>& params) { @@ -482,7 +491,8 @@ class RequestSocketCallback : public CallbackRunner< Tuple1<int> > { handle_->Reset(); within_callback_ = true; int rv = handle_->Init( - "a", HostResolver::RequestInfo("www.google.com", 80), 0, this, NULL); + "a", HostResolver::RequestInfo("www.google.com", 80), 0, + this, pool_.get(), NULL); EXPECT_EQ(OK, rv); } } @@ -493,15 +503,17 @@ class RequestSocketCallback : public CallbackRunner< Tuple1<int> > { private: ClientSocketHandle* const handle_; + const scoped_refptr<TCPClientSocketPool> pool_; bool within_callback_; TestCompletionCallback callback_; }; TEST_F(TCPClientSocketPoolTest, RequestTwice) { - ClientSocketHandle handle(pool_.get()); - RequestSocketCallback callback(&handle); + ClientSocketHandle handle; + RequestSocketCallback callback(&handle, pool_.get()); int rv = handle.Init( - "a", HostResolver::RequestInfo("www.google.com", 80), 0, &callback, NULL); + "a", HostResolver::RequestInfo("www.google.com", 80), 0, + &callback, pool_.get(), NULL); ASSERT_EQ(ERR_IO_PENDING, rv); // The callback is going to request "www.google.com". We want it to complete |