Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(98)

Side by Side Diff: net/socket/client_socket_pool_base_unittest.cc

Issue 176021: Control the amount of time to leave an unused socket idle before closing it. (Closed)
Patch Set: Address wtc's comments. Created 11 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/socket/client_socket_pool_base.cc ('k') | net/socket/tcp_client_socket_pool.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/client_socket_pool_base.h" 5 #include "net/socket/client_socket_pool_base.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 "base/platform_thread.h" 9 #include "base/platform_thread.h"
10 #include "base/scoped_vector.h" 10 #include "base/scoped_vector.h"
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after
243 MockClientSocketFactory* const client_socket_factory_; 243 MockClientSocketFactory* const client_socket_factory_;
244 244
245 DISALLOW_COPY_AND_ASSIGN(TestConnectJobFactory); 245 DISALLOW_COPY_AND_ASSIGN(TestConnectJobFactory);
246 }; 246 };
247 247
248 class TestClientSocketPool : public ClientSocketPool { 248 class TestClientSocketPool : public ClientSocketPool {
249 public: 249 public:
250 TestClientSocketPool( 250 TestClientSocketPool(
251 int max_sockets, 251 int max_sockets,
252 int max_sockets_per_group, 252 int max_sockets_per_group,
253 base::TimeDelta unused_idle_socket_timeout,
254 base::TimeDelta used_idle_socket_timeout,
253 TestClientSocketPoolBase::ConnectJobFactory* connect_job_factory) 255 TestClientSocketPoolBase::ConnectJobFactory* connect_job_factory)
254 : base_(max_sockets, max_sockets_per_group, connect_job_factory) {} 256 : base_(max_sockets, max_sockets_per_group,
257 unused_idle_socket_timeout, used_idle_socket_timeout,
258 connect_job_factory) {}
255 259
256 virtual int RequestSocket( 260 virtual int RequestSocket(
257 const std::string& group_name, 261 const std::string& group_name,
258 const void* params, 262 const void* params,
259 int priority, 263 int priority,
260 ClientSocketHandle* handle, 264 ClientSocketHandle* handle,
261 CompletionCallback* callback, 265 CompletionCallback* callback,
262 LoadLog* load_log) { 266 LoadLog* load_log) {
263 return base_.RequestSocket( 267 return base_.RequestSocket(
264 group_name, params, priority, handle, callback, load_log); 268 group_name, params, priority, handle, callback, load_log);
(...skipping 25 matching lines...) Expand all
290 const ClientSocketHandle* handle) const { 294 const ClientSocketHandle* handle) const {
291 return base_.GetLoadState(group_name, handle); 295 return base_.GetLoadState(group_name, handle);
292 } 296 }
293 297
294 const TestClientSocketPoolBase* base() const { return &base_; } 298 const TestClientSocketPoolBase* base() const { return &base_; }
295 299
296 int NumConnectJobsInGroup(const std::string& group_name) const { 300 int NumConnectJobsInGroup(const std::string& group_name) const {
297 return base_.NumConnectJobsInGroup(group_name); 301 return base_.NumConnectJobsInGroup(group_name);
298 } 302 }
299 303
304 void CleanupTimedOutIdleSockets() { base_.CleanupIdleSockets(false); }
305
300 private: 306 private:
301 TestClientSocketPoolBase base_; 307 TestClientSocketPoolBase base_;
302 308
303 DISALLOW_COPY_AND_ASSIGN(TestClientSocketPool); 309 DISALLOW_COPY_AND_ASSIGN(TestClientSocketPool);
304 }; 310 };
305 311
306 } // namespace 312 } // namespace
307 313
308 REGISTER_SOCKET_PARAMS_FOR_POOL(TestClientSocketPool, const void*); 314 REGISTER_SOCKET_PARAMS_FOR_POOL(TestClientSocketPool, const void*);
309 315
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
352 bool have_result_; 358 bool have_result_;
353 bool waiting_for_result_; 359 bool waiting_for_result_;
354 int result_; 360 int result_;
355 }; 361 };
356 362
357 class ClientSocketPoolBaseTest : public ClientSocketPoolTest { 363 class ClientSocketPoolBaseTest : public ClientSocketPoolTest {
358 protected: 364 protected:
359 ClientSocketPoolBaseTest() {} 365 ClientSocketPoolBaseTest() {}
360 366
361 void CreatePool(int max_sockets, int max_sockets_per_group) { 367 void CreatePool(int max_sockets, int max_sockets_per_group) {
368 CreatePoolWithIdleTimeouts(
369 max_sockets,
370 max_sockets_per_group,
371 base::TimeDelta::FromSeconds(kUnusedIdleSocketTimeout),
372 base::TimeDelta::FromSeconds(kUsedIdleSocketTimeout));
373 }
374
375 void CreatePoolWithIdleTimeouts(
376 int max_sockets, int max_sockets_per_group,
377 base::TimeDelta unused_idle_socket_timeout,
378 base::TimeDelta used_idle_socket_timeout) {
362 DCHECK(!pool_.get()); 379 DCHECK(!pool_.get());
363 connect_job_factory_ = new TestConnectJobFactory(&client_socket_factory_); 380 connect_job_factory_ = new TestConnectJobFactory(&client_socket_factory_);
364 pool_ = new TestClientSocketPool(max_sockets, 381 pool_ = new TestClientSocketPool(max_sockets,
365 max_sockets_per_group, 382 max_sockets_per_group,
383 unused_idle_socket_timeout,
384 used_idle_socket_timeout,
366 connect_job_factory_); 385 connect_job_factory_);
367 } 386 }
368 387
369 int StartRequest(const std::string& group_name, int priority) { 388 int StartRequest(const std::string& group_name, int priority) {
370 return StartRequestUsingPool<TestClientSocketPool, const void*>( 389 return StartRequestUsingPool<TestClientSocketPool, const void*>(
371 pool_.get(), group_name, priority, NULL); 390 pool_.get(), group_name, priority, NULL);
372 } 391 }
373 392
374 virtual void TearDown() { 393 virtual void TearDown() {
375 // Need to delete |pool_| before we turn late binding back off. We also need 394 // Need to delete |pool_| before we turn late binding back off. We also need
(...skipping 1377 matching lines...) Expand 10 before | Expand all | Expand 10 after
1753 EXPECT_TRUE(ReleaseOneConnection(KEEP_ALIVE)); 1772 EXPECT_TRUE(ReleaseOneConnection(KEEP_ALIVE));
1754 EXPECT_TRUE(ReleaseOneConnection(KEEP_ALIVE)); 1773 EXPECT_TRUE(ReleaseOneConnection(KEEP_ALIVE));
1755 1774
1756 // Closing idle sockets should not get us into trouble, but in the bug 1775 // Closing idle sockets should not get us into trouble, but in the bug
1757 // we were hitting a CHECK here. 1776 // we were hitting a CHECK here.
1758 EXPECT_EQ(2, pool_->IdleSocketCountInGroup("a")); 1777 EXPECT_EQ(2, pool_->IdleSocketCountInGroup("a"));
1759 pool_->CloseIdleSockets(); 1778 pool_->CloseIdleSockets();
1760 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a")); 1779 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
1761 } 1780 }
1762 1781
1782 TEST_F(ClientSocketPoolBaseTest_LateBinding, CleanupTimedOutIdleSockets) {
1783 CreatePoolWithIdleTimeouts(
1784 kDefaultMaxSockets, kDefaultMaxSocketsPerGroup,
1785 base::TimeDelta(), // Time out unused sockets immediately.
1786 base::TimeDelta::FromDays(1)); // Don't time out used sockets.
1787
1788 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
1789
1790 // Startup two mock pending connect jobs, which will sit in the MessageLoop.
1791
1792 TestSocketRequest req(&request_order_, &completion_count_);
1793 int rv = InitHandle(req.handle(), "a", 0, &req, pool_.get(), NULL);
1794 EXPECT_EQ(ERR_IO_PENDING, rv);
1795 EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", req.handle()));
1796
1797 TestSocketRequest req2(&request_order_, &completion_count_);
1798 rv = InitHandle(req2.handle(), "a", 0, &req2, pool_.get(), NULL);
1799 EXPECT_EQ(ERR_IO_PENDING, rv);
1800 EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", req2.handle()));
1801
1802 // Cancel one of the requests. Wait for the other, which will get the first
1803 // job. Release the socket. Run the loop again to make sure the second
1804 // socket is sitting idle and the first one is released (since ReleaseSocket()
1805 // just posts a DoReleaseSocket() task).
1806
1807 req.handle()->Reset();
1808 EXPECT_EQ(OK, req2.WaitForResult());
1809 req2.handle()->Reset();
1810 MessageLoop::current()->RunAllPending();
1811
1812 ASSERT_EQ(2, pool_->IdleSocketCount());
1813
1814 // Invoke the idle socket cleanup check. Only one socket should be left, the
1815 // used socket. Request it to make sure that it's used.
1816
1817 pool_->CleanupTimedOutIdleSockets();
1818 rv = InitHandle(req.handle(), "a", 0, &req, pool_.get(), NULL);
1819 EXPECT_EQ(OK, rv);
1820 EXPECT_TRUE(req.handle()->is_reused());
1821 }
1822
1763 } // namespace 1823 } // namespace
1764 1824
1765 } // namespace net 1825 } // namespace net
OLDNEW
« no previous file with comments | « net/socket/client_socket_pool_base.cc ('k') | net/socket/tcp_client_socket_pool.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698