OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
10 #include "base/memory/scoped_vector.h" | 10 #include "base/memory/scoped_vector.h" |
11 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
12 #include "base/stringprintf.h" | 12 #include "base/stringprintf.h" |
13 #include "base/string_number_conversions.h" | 13 #include "base/string_number_conversions.h" |
14 #include "base/threading/platform_thread.h" | 14 #include "base/threading/platform_thread.h" |
15 #include "base/values.h" | 15 #include "base/values.h" |
16 #include "net/base/net_errors.h" | 16 #include "net/base/net_errors.h" |
17 #include "net/base/net_log.h" | 17 #include "net/base/net_log.h" |
18 #include "net/base/net_log_unittest.h" | 18 #include "net/base/net_log_unittest.h" |
19 #include "net/base/request_priority.h" | 19 #include "net/base/request_priority.h" |
20 #include "net/base/test_completion_callback.h" | 20 #include "net/base/test_completion_callback.h" |
21 #include "net/http/http_response_headers.h" | 21 #include "net/http/http_response_headers.h" |
22 #include "net/socket/client_socket_factory.h" | 22 #include "net/socket/client_socket_factory.h" |
23 #include "net/socket/client_socket_handle.h" | 23 #include "net/socket/client_socket_handle.h" |
24 #include "net/socket/client_socket_pool_histograms.h" | 24 #include "net/socket/client_socket_pool_histograms.h" |
25 #include "net/socket/socket_test_util.h" | 25 #include "net/socket/socket_test_util.h" |
26 #include "net/socket/ssl_host_info.h" | 26 #include "net/socket/ssl_host_info.h" |
27 #include "net/socket/stream_socket.h" | 27 #include "net/socket/stream_socket.h" |
28 #include "testing/gmock/include/gmock/gmock.h" | |
29 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
30 | 29 |
31 using ::testing::Invoke; | |
32 using ::testing::Return; | |
33 | |
34 namespace net { | 30 namespace net { |
35 | 31 |
36 namespace { | 32 namespace { |
37 | 33 |
38 const int kDefaultMaxSockets = 4; | 34 const int kDefaultMaxSockets = 4; |
39 const int kDefaultMaxSocketsPerGroup = 2; | 35 const int kDefaultMaxSocketsPerGroup = 2; |
40 const net::RequestPriority kDefaultPriority = MEDIUM; | 36 const net::RequestPriority kDefaultPriority = MEDIUM; |
41 | 37 |
42 class TestSocketParams : public base::RefCounted<TestSocketParams> { | 38 class TestSocketParams : public base::RefCounted<TestSocketParams> { |
43 public: | 39 public: |
44 TestSocketParams() : ignore_limits_(false) {} | 40 bool ignore_limits() { return false; } |
45 | |
46 void set_ignore_limits(bool ignore_limits) { | |
47 ignore_limits_ = ignore_limits; | |
48 } | |
49 bool ignore_limits() { return ignore_limits_; } | |
50 | |
51 private: | 41 private: |
52 friend class base::RefCounted<TestSocketParams>; | 42 friend class base::RefCounted<TestSocketParams>; |
53 ~TestSocketParams() {} | 43 ~TestSocketParams() {} |
54 | |
55 bool ignore_limits_; | |
56 }; | 44 }; |
57 typedef ClientSocketPoolBase<TestSocketParams> TestClientSocketPoolBase; | 45 typedef ClientSocketPoolBase<TestSocketParams> TestClientSocketPoolBase; |
58 | 46 |
59 class MockClientSocket : public StreamSocket { | 47 class MockClientSocket : public StreamSocket { |
60 public: | 48 public: |
61 MockClientSocket() : connected_(false), was_used_to_convey_data_(false), | 49 MockClientSocket() : connected_(false), was_used_to_convey_data_(false), |
62 num_bytes_read_(0) {} | 50 num_bytes_read_(0) {} |
63 | 51 |
64 // Socket methods: | 52 // Socket methods: |
65 virtual int Read( | 53 virtual int Read( |
(...skipping 353 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
419 connect_job_factory) {} | 407 connect_job_factory) {} |
420 | 408 |
421 virtual ~TestClientSocketPool() {} | 409 virtual ~TestClientSocketPool() {} |
422 | 410 |
423 virtual int RequestSocket( | 411 virtual int RequestSocket( |
424 const std::string& group_name, | 412 const std::string& group_name, |
425 const void* params, | 413 const void* params, |
426 net::RequestPriority priority, | 414 net::RequestPriority priority, |
427 ClientSocketHandle* handle, | 415 ClientSocketHandle* handle, |
428 OldCompletionCallback* callback, | 416 OldCompletionCallback* callback, |
429 const BoundNetLog& net_log) OVERRIDE { | 417 const BoundNetLog& net_log) { |
430 const scoped_refptr<TestSocketParams>* casted_socket_params = | 418 const scoped_refptr<TestSocketParams>* casted_socket_params = |
431 static_cast<const scoped_refptr<TestSocketParams>*>(params); | 419 static_cast<const scoped_refptr<TestSocketParams>*>(params); |
432 return base_.RequestSocket(group_name, *casted_socket_params, priority, | 420 return base_.RequestSocket(group_name, *casted_socket_params, priority, |
433 handle, callback, net_log); | 421 handle, callback, net_log); |
434 } | 422 } |
435 | 423 |
436 virtual void RequestSockets(const std::string& group_name, | 424 virtual void RequestSockets(const std::string& group_name, |
437 const void* params, | 425 const void* params, |
438 int num_sockets, | 426 int num_sockets, |
439 const BoundNetLog& net_log) OVERRIDE { | 427 const BoundNetLog& net_log) { |
440 const scoped_refptr<TestSocketParams>* casted_params = | 428 const scoped_refptr<TestSocketParams>* casted_params = |
441 static_cast<const scoped_refptr<TestSocketParams>*>(params); | 429 static_cast<const scoped_refptr<TestSocketParams>*>(params); |
442 | 430 |
443 base_.RequestSockets(group_name, *casted_params, num_sockets, net_log); | 431 base_.RequestSockets(group_name, *casted_params, num_sockets, net_log); |
444 } | 432 } |
445 | 433 |
446 virtual void CancelRequest( | 434 virtual void CancelRequest( |
447 const std::string& group_name, | 435 const std::string& group_name, |
448 ClientSocketHandle* handle) OVERRIDE { | 436 ClientSocketHandle* handle) { |
449 base_.CancelRequest(group_name, handle); | 437 base_.CancelRequest(group_name, handle); |
450 } | 438 } |
451 | 439 |
452 virtual void ReleaseSocket( | 440 virtual void ReleaseSocket( |
453 const std::string& group_name, | 441 const std::string& group_name, |
454 StreamSocket* socket, | 442 StreamSocket* socket, |
455 int id) OVERRIDE { | 443 int id) { |
456 base_.ReleaseSocket(group_name, socket, id); | 444 base_.ReleaseSocket(group_name, socket, id); |
457 } | 445 } |
458 | 446 |
459 virtual void Flush() OVERRIDE { | 447 virtual void Flush() { |
460 base_.Flush(); | 448 base_.Flush(); |
461 } | 449 } |
462 | 450 |
463 virtual bool IsStalled() const OVERRIDE { | 451 virtual void CloseIdleSockets() { |
464 return base_.IsStalled(); | |
465 } | |
466 | |
467 virtual void CloseIdleSockets() OVERRIDE { | |
468 base_.CloseIdleSockets(); | 452 base_.CloseIdleSockets(); |
469 } | 453 } |
470 | 454 |
471 virtual int IdleSocketCount() const OVERRIDE { | 455 virtual int IdleSocketCount() const { return base_.idle_socket_count(); } |
472 return base_.idle_socket_count(); | |
473 } | |
474 | 456 |
475 virtual int IdleSocketCountInGroup( | 457 virtual int IdleSocketCountInGroup(const std::string& group_name) const { |
476 const std::string& group_name) const OVERRIDE { | |
477 return base_.IdleSocketCountInGroup(group_name); | 458 return base_.IdleSocketCountInGroup(group_name); |
478 } | 459 } |
479 | 460 |
480 virtual LoadState GetLoadState( | 461 virtual LoadState GetLoadState(const std::string& group_name, |
481 const std::string& group_name, | 462 const ClientSocketHandle* handle) const { |
482 const ClientSocketHandle* handle) const OVERRIDE { | |
483 return base_.GetLoadState(group_name, handle); | 463 return base_.GetLoadState(group_name, handle); |
484 } | 464 } |
485 | 465 |
486 virtual void AddLayeredPool(LayeredPool* pool) OVERRIDE { | 466 virtual DictionaryValue* GetInfoAsValue(const std::string& name, |
487 base_.AddLayeredPool(pool); | 467 const std::string& type, |
488 } | 468 bool include_nested_pools) const { |
489 | |
490 virtual void RemoveLayeredPool(LayeredPool* pool) OVERRIDE { | |
491 base_.RemoveLayeredPool(pool); | |
492 } | |
493 | |
494 virtual DictionaryValue* GetInfoAsValue( | |
495 const std::string& name, | |
496 const std::string& type, | |
497 bool include_nested_pools) const OVERRIDE { | |
498 return base_.GetInfoAsValue(name, type); | 469 return base_.GetInfoAsValue(name, type); |
499 } | 470 } |
500 | 471 |
501 virtual base::TimeDelta ConnectionTimeout() const OVERRIDE { | 472 virtual base::TimeDelta ConnectionTimeout() const { |
502 return base_.ConnectionTimeout(); | 473 return base_.ConnectionTimeout(); |
503 } | 474 } |
504 | 475 |
505 virtual ClientSocketPoolHistograms* histograms() const OVERRIDE { | 476 virtual ClientSocketPoolHistograms* histograms() const { |
506 return base_.histograms(); | 477 return base_.histograms(); |
507 } | 478 } |
508 | 479 |
509 const TestClientSocketPoolBase* base() const { return &base_; } | 480 const TestClientSocketPoolBase* base() const { return &base_; } |
510 | 481 |
511 int NumConnectJobsInGroup(const std::string& group_name) const { | 482 int NumConnectJobsInGroup(const std::string& group_name) const { |
512 return base_.NumConnectJobsInGroup(group_name); | 483 return base_.NumConnectJobsInGroup(group_name); |
513 } | 484 } |
514 | 485 |
515 int NumActiveSocketsInGroup(const std::string& group_name) const { | 486 int NumActiveSocketsInGroup(const std::string& group_name) const { |
516 return base_.NumActiveSocketsInGroup(group_name); | 487 return base_.NumActiveSocketsInGroup(group_name); |
517 } | 488 } |
518 | 489 |
519 bool HasGroup(const std::string& group_name) const { | 490 bool HasGroup(const std::string& group_name) const { |
520 return base_.HasGroup(group_name); | 491 return base_.HasGroup(group_name); |
521 } | 492 } |
522 | 493 |
523 void CleanupTimedOutIdleSockets() { base_.CleanupIdleSockets(false); } | 494 void CleanupTimedOutIdleSockets() { base_.CleanupIdleSockets(false); } |
524 | 495 |
525 void EnableConnectBackupJobs() { base_.EnableConnectBackupJobs(); } | 496 void EnableConnectBackupJobs() { base_.EnableConnectBackupJobs(); } |
526 | 497 |
527 bool CloseOneIdleConnectionInLayeredPool() { | |
528 return base_.CloseOneIdleConnectionInLayeredPool(); | |
529 } | |
530 | |
531 private: | 498 private: |
532 TestClientSocketPoolBase base_; | 499 TestClientSocketPoolBase base_; |
533 | 500 |
534 DISALLOW_COPY_AND_ASSIGN(TestClientSocketPool); | 501 DISALLOW_COPY_AND_ASSIGN(TestClientSocketPool); |
535 }; | 502 }; |
536 | 503 |
537 } // namespace | 504 } // namespace |
538 | 505 |
539 REGISTER_SOCKET_PARAMS_FOR_POOL(TestClientSocketPool, TestSocketParams); | 506 REGISTER_SOCKET_PARAMS_FOR_POOL(TestClientSocketPool, TestSocketParams); |
540 | 507 |
(...skipping 645 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1186 EXPECT_EQ(1, pool_->IdleSocketCount()); | 1153 EXPECT_EQ(1, pool_->IdleSocketCount()); |
1187 } | 1154 } |
1188 | 1155 |
1189 TEST_F(ClientSocketPoolBaseTest, WaitForStalledSocketAtSocketLimit) { | 1156 TEST_F(ClientSocketPoolBaseTest, WaitForStalledSocketAtSocketLimit) { |
1190 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); | 1157 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
1191 connect_job_factory_->set_job_type(TestConnectJob::kMockJob); | 1158 connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
1192 | 1159 |
1193 ClientSocketHandle stalled_handle; | 1160 ClientSocketHandle stalled_handle; |
1194 TestOldCompletionCallback callback; | 1161 TestOldCompletionCallback callback; |
1195 { | 1162 { |
1196 EXPECT_FALSE(pool_->IsStalled()); | |
1197 ClientSocketHandle handles[kDefaultMaxSockets]; | 1163 ClientSocketHandle handles[kDefaultMaxSockets]; |
1198 for (int i = 0; i < kDefaultMaxSockets; ++i) { | 1164 for (int i = 0; i < kDefaultMaxSockets; ++i) { |
1199 TestOldCompletionCallback callback; | 1165 TestOldCompletionCallback callback; |
1200 EXPECT_EQ(OK, handles[i].Init(base::StringPrintf( | 1166 EXPECT_EQ(OK, handles[i].Init(base::StringPrintf( |
1201 "Take 2: %d", i), | 1167 "Take 2: %d", i), |
1202 params_, | 1168 params_, |
1203 kDefaultPriority, | 1169 kDefaultPriority, |
1204 &callback, | 1170 &callback, |
1205 pool_.get(), | 1171 pool_.get(), |
1206 BoundNetLog())); | 1172 BoundNetLog())); |
1207 } | 1173 } |
1208 | 1174 |
1209 EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); | 1175 EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
1210 EXPECT_EQ(0, pool_->IdleSocketCount()); | 1176 EXPECT_EQ(0, pool_->IdleSocketCount()); |
1211 EXPECT_FALSE(pool_->IsStalled()); | |
1212 | 1177 |
1213 // Now we will hit the socket limit. | 1178 // Now we will hit the socket limit. |
1214 EXPECT_EQ(ERR_IO_PENDING, stalled_handle.Init("foo", | 1179 EXPECT_EQ(ERR_IO_PENDING, stalled_handle.Init("foo", |
1215 params_, | 1180 params_, |
1216 kDefaultPriority, | 1181 kDefaultPriority, |
1217 &callback, | 1182 &callback, |
1218 pool_.get(), | 1183 pool_.get(), |
1219 BoundNetLog())); | 1184 BoundNetLog())); |
1220 EXPECT_TRUE(pool_->IsStalled()); | |
1221 | 1185 |
1222 // Dropping out of scope will close all handles and return them to idle. | 1186 // Dropping out of scope will close all handles and return them to idle. |
1223 } | 1187 } |
1224 | 1188 |
1225 // But if we wait for it, the released idle sockets will be closed in | 1189 // But if we wait for it, the released idle sockets will be closed in |
1226 // preference of the waiting request. | 1190 // preference of the waiting request. |
1227 EXPECT_EQ(OK, callback.WaitForResult()); | 1191 EXPECT_EQ(OK, callback.WaitForResult()); |
1228 | 1192 |
1229 EXPECT_EQ(kDefaultMaxSockets + 1, client_socket_factory_.allocation_count()); | 1193 EXPECT_EQ(kDefaultMaxSockets + 1, client_socket_factory_.allocation_count()); |
1230 EXPECT_EQ(3, pool_->IdleSocketCount()); | 1194 EXPECT_EQ(3, pool_->IdleSocketCount()); |
(...skipping 798 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2029 // job. Release the socket. Run the loop again to make sure the second | 1993 // job. Release the socket. Run the loop again to make sure the second |
2030 // socket is sitting idle and the first one is released (since ReleaseSocket() | 1994 // socket is sitting idle and the first one is released (since ReleaseSocket() |
2031 // just posts a DoReleaseSocket() task). | 1995 // just posts a DoReleaseSocket() task). |
2032 | 1996 |
2033 handle.Reset(); | 1997 handle.Reset(); |
2034 EXPECT_EQ(OK, callback2.WaitForResult()); | 1998 EXPECT_EQ(OK, callback2.WaitForResult()); |
2035 // Use the socket. | 1999 // Use the socket. |
2036 EXPECT_EQ(1, handle2.socket()->Write(NULL, 1, NULL)); | 2000 EXPECT_EQ(1, handle2.socket()->Write(NULL, 1, NULL)); |
2037 handle2.Reset(); | 2001 handle2.Reset(); |
2038 | 2002 |
2039 // The idle socket timeout value was set to 10 milliseconds. Wait 100 | 2003 // The idle socket timeout value was set to 10 milliseconds. Wait 20 |
2040 // milliseconds so the sockets timeout. | 2004 // milliseconds so the sockets timeout. |
2041 base::PlatformThread::Sleep(100); | 2005 base::PlatformThread::Sleep(20); |
2042 MessageLoop::current()->RunAllPending(); | 2006 MessageLoop::current()->RunAllPending(); |
2043 | 2007 |
2044 ASSERT_EQ(2, pool_->IdleSocketCount()); | 2008 ASSERT_EQ(2, pool_->IdleSocketCount()); |
2045 | 2009 |
2046 // Request a new socket. This should cleanup the unused and timed out ones. | 2010 // Request a new socket. This should cleanup the unused and timed out ones. |
2047 // A new socket will be created rather than reusing the idle one. | 2011 // A new socket will be created rather than reusing the idle one. |
2048 CapturingBoundNetLog log(CapturingNetLog::kUnbounded); | 2012 CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
2049 rv = handle.Init("a", | 2013 rv = handle.Init("a", |
2050 params_, | 2014 params_, |
2051 LOWEST, | 2015 LOWEST, |
(...skipping 1006 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3058 CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); | 3022 CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
3059 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); | 3023 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
3060 | 3024 |
3061 ASSERT_FALSE(pool_->HasGroup("a")); | 3025 ASSERT_FALSE(pool_->HasGroup("a")); |
3062 | 3026 |
3063 pool_->RequestSockets("a", ¶ms_, kDefaultMaxSockets - 1, | 3027 pool_->RequestSockets("a", ¶ms_, kDefaultMaxSockets - 1, |
3064 BoundNetLog()); | 3028 BoundNetLog()); |
3065 | 3029 |
3066 ASSERT_TRUE(pool_->HasGroup("a")); | 3030 ASSERT_TRUE(pool_->HasGroup("a")); |
3067 EXPECT_EQ(kDefaultMaxSockets - 1, pool_->NumConnectJobsInGroup("a")); | 3031 EXPECT_EQ(kDefaultMaxSockets - 1, pool_->NumConnectJobsInGroup("a")); |
3068 EXPECT_FALSE(pool_->IsStalled()); | |
3069 | 3032 |
3070 ASSERT_FALSE(pool_->HasGroup("b")); | 3033 ASSERT_FALSE(pool_->HasGroup("b")); |
3071 | 3034 |
3072 pool_->RequestSockets("b", ¶ms_, kDefaultMaxSockets, | 3035 pool_->RequestSockets("b", ¶ms_, kDefaultMaxSockets, |
3073 BoundNetLog()); | 3036 BoundNetLog()); |
3074 | 3037 |
3075 ASSERT_TRUE(pool_->HasGroup("b")); | 3038 ASSERT_TRUE(pool_->HasGroup("b")); |
3076 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("b")); | 3039 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("b")); |
3077 EXPECT_FALSE(pool_->IsStalled()); | |
3078 } | 3040 } |
3079 | 3041 |
3080 TEST_F(ClientSocketPoolBaseTest, RequestSocketsCountIdleSockets) { | 3042 TEST_F(ClientSocketPoolBaseTest, RequestSocketsCountIdleSockets) { |
3081 CreatePool(4, 4); | 3043 CreatePool(4, 4); |
3082 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); | 3044 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
3083 | 3045 |
3084 ClientSocketHandle handle1; | 3046 ClientSocketHandle handle1; |
3085 TestOldCompletionCallback callback1; | 3047 TestOldCompletionCallback callback1; |
3086 EXPECT_EQ(ERR_IO_PENDING, handle1.Init("a", | 3048 EXPECT_EQ(ERR_IO_PENDING, handle1.Init("a", |
3087 params_, | 3049 params_, |
(...skipping 298 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3386 EXPECT_EQ(0, pool_->NumActiveSocketsInGroup("a")); | 3348 EXPECT_EQ(0, pool_->NumActiveSocketsInGroup("a")); |
3387 ASSERT_EQ(OK, callback.WaitForResult()); | 3349 ASSERT_EQ(OK, callback.WaitForResult()); |
3388 | 3350 |
3389 // The hung connect job should still be there, but everything else should be | 3351 // The hung connect job should still be there, but everything else should be |
3390 // complete. | 3352 // complete. |
3391 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a")); | 3353 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a")); |
3392 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a")); | 3354 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a")); |
3393 EXPECT_EQ(1, pool_->NumActiveSocketsInGroup("a")); | 3355 EXPECT_EQ(1, pool_->NumActiveSocketsInGroup("a")); |
3394 } | 3356 } |
3395 | 3357 |
3396 class MockLayeredPool : public LayeredPool { | |
3397 public: | |
3398 MockLayeredPool(TestClientSocketPool* pool, | |
3399 const std::string& group_name) | |
3400 : pool_(pool), | |
3401 params_(new TestSocketParams), | |
3402 group_name_(group_name) { | |
3403 pool_->AddLayeredPool(this); | |
3404 } | |
3405 | |
3406 ~MockLayeredPool() { | |
3407 pool_->RemoveLayeredPool(this); | |
3408 } | |
3409 | |
3410 int RequestSocket(TestClientSocketPool* pool) { | |
3411 return handle_.Init(group_name_, params_, kDefaultPriority, &callback_, | |
3412 pool, BoundNetLog()); | |
3413 } | |
3414 | |
3415 int RequestSocketWithoutLimits(TestClientSocketPool* pool) { | |
3416 params_->set_ignore_limits(true); | |
3417 return handle_.Init(group_name_, params_, kDefaultPriority, &callback_, | |
3418 pool, BoundNetLog()); | |
3419 } | |
3420 | |
3421 bool ReleaseOneConnection() { | |
3422 if (!handle_.is_initialized()) { | |
3423 return false; | |
3424 } | |
3425 handle_.socket()->Disconnect(); | |
3426 handle_.Reset(); | |
3427 return true; | |
3428 } | |
3429 | |
3430 MOCK_METHOD0(CloseOneIdleConnection, bool()); | |
3431 | |
3432 private: | |
3433 TestClientSocketPool* const pool_; | |
3434 scoped_refptr<TestSocketParams> params_; | |
3435 ClientSocketHandle handle_; | |
3436 TestOldCompletionCallback callback_; | |
3437 const std::string group_name_; | |
3438 }; | |
3439 | |
3440 TEST_F(ClientSocketPoolBaseTest, FailToCloseIdleSocketsNotHeldByLayeredPool) { | |
3441 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); | |
3442 connect_job_factory_->set_job_type(TestConnectJob::kMockJob); | |
3443 | |
3444 MockLayeredPool mock_layered_pool(pool_.get(), "foo"); | |
3445 EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) | |
3446 .WillOnce(Return(false)); | |
3447 EXPECT_EQ(OK, mock_layered_pool.RequestSocket(pool_.get())); | |
3448 EXPECT_FALSE(pool_->CloseOneIdleConnectionInLayeredPool()); | |
3449 } | |
3450 | |
3451 TEST_F(ClientSocketPoolBaseTest, ForciblyCloseIdleSocketsHeldByLayeredPool) { | |
3452 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); | |
3453 connect_job_factory_->set_job_type(TestConnectJob::kMockJob); | |
3454 | |
3455 MockLayeredPool mock_layered_pool(pool_.get(), "foo"); | |
3456 EXPECT_EQ(OK, mock_layered_pool.RequestSocket(pool_.get())); | |
3457 EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) | |
3458 .WillOnce(Invoke(&mock_layered_pool, | |
3459 &MockLayeredPool::ReleaseOneConnection)); | |
3460 EXPECT_TRUE(pool_->CloseOneIdleConnectionInLayeredPool()); | |
3461 } | |
3462 | |
3463 TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketsHeldByLayeredPoolWhenNeeded) { | |
3464 CreatePool(1, 1); | |
3465 connect_job_factory_->set_job_type(TestConnectJob::kMockJob); | |
3466 | |
3467 MockLayeredPool mock_layered_pool(pool_.get(), "foo"); | |
3468 EXPECT_EQ(OK, mock_layered_pool.RequestSocket(pool_.get())); | |
3469 EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) | |
3470 .WillOnce(Invoke(&mock_layered_pool, | |
3471 &MockLayeredPool::ReleaseOneConnection)); | |
3472 ClientSocketHandle handle; | |
3473 TestOldCompletionCallback callback; | |
3474 EXPECT_EQ(OK, handle.Init("a", | |
3475 params_, | |
3476 kDefaultPriority, | |
3477 &callback, | |
3478 pool_.get(), | |
3479 BoundNetLog())); | |
3480 } | |
3481 | |
3482 TEST_F(ClientSocketPoolBaseTest, | |
3483 CloseMultipleIdleSocketsHeldByLayeredPoolWhenNeeded) { | |
3484 CreatePool(1, 1); | |
3485 connect_job_factory_->set_job_type(TestConnectJob::kMockJob); | |
3486 | |
3487 MockLayeredPool mock_layered_pool1(pool_.get(), "foo"); | |
3488 EXPECT_EQ(OK, mock_layered_pool1.RequestSocket(pool_.get())); | |
3489 EXPECT_CALL(mock_layered_pool1, CloseOneIdleConnection()) | |
3490 .WillRepeatedly(Invoke(&mock_layered_pool1, | |
3491 &MockLayeredPool::ReleaseOneConnection)); | |
3492 MockLayeredPool mock_layered_pool2(pool_.get(), "bar"); | |
3493 EXPECT_EQ(OK, mock_layered_pool2.RequestSocketWithoutLimits(pool_.get())); | |
3494 EXPECT_CALL(mock_layered_pool2, CloseOneIdleConnection()) | |
3495 .WillRepeatedly(Invoke(&mock_layered_pool2, | |
3496 &MockLayeredPool::ReleaseOneConnection)); | |
3497 ClientSocketHandle handle; | |
3498 TestOldCompletionCallback callback; | |
3499 EXPECT_EQ(OK, handle.Init("a", | |
3500 params_, | |
3501 kDefaultPriority, | |
3502 &callback, | |
3503 pool_.get(), | |
3504 BoundNetLog())); | |
3505 } | |
3506 | |
3507 } // namespace | 3358 } // namespace |
3508 | 3359 |
3509 } // namespace net | 3360 } // namespace net |
OLD | NEW |