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 2014 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2025 // Cancel the pending request. | 2025 // Cancel the pending request. |
2026 handle.Reset(); | 2026 handle.Reset(); |
2027 | 2027 |
2028 // Wait for the backup timer to fire (add some slop to ensure it fires) | 2028 // Wait for the backup timer to fire (add some slop to ensure it fires) |
2029 PlatformThread::Sleep(ClientSocketPool::kMaxConnectRetryIntervalMs / 2 * 3); | 2029 PlatformThread::Sleep(ClientSocketPool::kMaxConnectRetryIntervalMs / 2 * 3); |
2030 | 2030 |
2031 MessageLoop::current()->RunAllPending(); | 2031 MessageLoop::current()->RunAllPending(); |
2032 EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); | 2032 EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
2033 } | 2033 } |
2034 | 2034 |
2035 TEST_F(ClientSocketPoolBaseTest, CancelBackupSocketWhenThereAreNoRequests) { | 2035 TEST_F(ClientSocketPoolBaseTest, CancelBackupSocketAfterCancelingAllRequests) { |
2036 CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); | 2036 CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
2037 pool_->EnableConnectBackupJobs(); | 2037 pool_->EnableConnectBackupJobs(); |
2038 | 2038 |
2039 // Create the first socket and set to ERR_IO_PENDING. This starts the backup | 2039 // Create the first socket and set to ERR_IO_PENDING. This starts the backup |
2040 // timer. | 2040 // timer. |
2041 connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); | 2041 connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); |
2042 ClientSocketHandle handle; | 2042 ClientSocketHandle handle; |
2043 TestCompletionCallback callback; | 2043 TestCompletionCallback callback; |
2044 EXPECT_EQ(ERR_IO_PENDING, handle.Init("bar", params_, kDefaultPriority, | 2044 EXPECT_EQ(ERR_IO_PENDING, handle.Init("bar", params_, kDefaultPriority, |
2045 &callback, pool_, BoundNetLog())); | 2045 &callback, pool_, BoundNetLog())); |
2046 ASSERT_TRUE(pool_->HasGroup("bar")); | 2046 ASSERT_TRUE(pool_->HasGroup("bar")); |
2047 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("bar")); | 2047 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("bar")); |
2048 | 2048 |
2049 // Cancel the socket request. This should cancel the backup timer. Wait for | 2049 // Cancel the socket request. This should cancel the backup timer. Wait for |
2050 // the backup time to see if it indeed got canceled. | 2050 // the backup time to see if it indeed got canceled. |
2051 handle.Reset(); | 2051 handle.Reset(); |
2052 // Wait for the backup timer to fire (add some slop to ensure it fires) | 2052 // Wait for the backup timer to fire (add some slop to ensure it fires) |
2053 PlatformThread::Sleep(ClientSocketPool::kMaxConnectRetryIntervalMs / 2 * 3); | 2053 PlatformThread::Sleep(ClientSocketPool::kMaxConnectRetryIntervalMs / 2 * 3); |
2054 MessageLoop::current()->RunAllPending(); | 2054 MessageLoop::current()->RunAllPending(); |
2055 ASSERT_TRUE(pool_->HasGroup("bar")); | 2055 ASSERT_TRUE(pool_->HasGroup("bar")); |
2056 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("bar")); | 2056 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("bar")); |
2057 } | 2057 } |
2058 | 2058 |
2059 TEST_F(ClientSocketPoolBaseTest, CancelBackupSocketAfterFinishingAllRequests) { | |
2060 CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); | |
2061 pool_->EnableConnectBackupJobs(); | |
2062 | |
2063 // Create the first socket and set to ERR_IO_PENDING. This starts the backup | |
2064 // timer. | |
2065 connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob); | |
2066 ClientSocketHandle handle; | |
2067 TestCompletionCallback callback; | |
2068 EXPECT_EQ(ERR_IO_PENDING, handle.Init("bar", params_, kDefaultPriority, | |
2069 &callback, pool_, BoundNetLog())); | |
2070 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); | |
Mike Belshe
2010/09/27 19:00:49
nit: line 2070 is unnecessary due to line 2065.
willchan no longer on Chromium
2010/09/27 19:05:04
It's necessary. Waiting != Pending. We need a jo
Mike Belshe
2010/09/28 15:14:00
Doh - I misread. Somehow I read them as the same
| |
2071 ClientSocketHandle handle2; | |
2072 TestCompletionCallback callback2; | |
2073 EXPECT_EQ(ERR_IO_PENDING, handle2.Init("bar", params_, kDefaultPriority, | |
2074 &callback2, pool_, BoundNetLog())); | |
2075 ASSERT_TRUE(pool_->HasGroup("bar")); | |
2076 EXPECT_EQ(2, pool_->NumConnectJobsInGroup("bar")); | |
2077 | |
2078 // Cancel request 1 and then complete request 2. With the requests finished, | |
2079 // the backup timer should be cancelled. | |
2080 handle.Reset(); | |
2081 EXPECT_EQ(OK, callback2.WaitForResult()); | |
2082 // Wait for the backup timer to fire (add some slop to ensure it fires) | |
2083 PlatformThread::Sleep(ClientSocketPool::kMaxConnectRetryIntervalMs / 2 * 3); | |
Paweł Hajdan Jr.
2010/09/28 08:08:34
Can we use some smarter method to wait than Sleep?
| |
2084 MessageLoop::current()->RunAllPending(); | |
Mike Belshe
2010/09/27 19:00:49
nit: We're not actually verifying that the backup
willchan no longer on Chromium
2010/09/27 19:05:04
Well, previously we would hit a LOG(DFATAL) saying
| |
2085 } | |
2086 | |
2059 // Test delayed socket binding for the case where we have two connects, | 2087 // Test delayed socket binding for the case where we have two connects, |
2060 // and while one is waiting on a connect, the other frees up. | 2088 // and while one is waiting on a connect, the other frees up. |
2061 // The socket waiting on a connect should switch immediately to the freed | 2089 // The socket waiting on a connect should switch immediately to the freed |
2062 // up socket. | 2090 // up socket. |
2063 TEST_F(ClientSocketPoolBaseTest, DelayedSocketBindingWaitingForConnect) { | 2091 TEST_F(ClientSocketPoolBaseTest, DelayedSocketBindingWaitingForConnect) { |
2064 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); | 2092 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
2065 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); | 2093 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
2066 | 2094 |
2067 ClientSocketHandle handle1; | 2095 ClientSocketHandle handle1; |
2068 TestCompletionCallback callback; | 2096 TestCompletionCallback callback; |
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2234 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a")); | 2262 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a")); |
2235 | 2263 |
2236 EXPECT_EQ(ERR_CONNECTION_FAILED, callback1.WaitForResult()); | 2264 EXPECT_EQ(ERR_CONNECTION_FAILED, callback1.WaitForResult()); |
2237 EXPECT_EQ(ERR_CONNECTION_FAILED, callback2.WaitForResult()); | 2265 EXPECT_EQ(ERR_CONNECTION_FAILED, callback2.WaitForResult()); |
2238 EXPECT_FALSE(pool_->HasGroup("a")); | 2266 EXPECT_FALSE(pool_->HasGroup("a")); |
2239 } | 2267 } |
2240 | 2268 |
2241 } // namespace | 2269 } // namespace |
2242 | 2270 |
2243 } // namespace net | 2271 } // namespace net |
OLD | NEW |