| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <vector> | |
| 8 | |
| 9 #include "base/bind.h" | 7 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 11 #include "base/callback.h" | 9 #include "base/callback.h" |
| 12 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| 13 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 14 #include "base/memory/scoped_vector.h" | 12 #include "base/memory/scoped_vector.h" |
| 15 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
| 16 #include "base/message_loop.h" | 14 #include "base/message_loop.h" |
| 17 #include "base/stringprintf.h" | 15 #include "base/stringprintf.h" |
| 18 #include "base/string_number_conversions.h" | 16 #include "base/string_number_conversions.h" |
| 19 #include "base/threading/platform_thread.h" | 17 #include "base/threading/platform_thread.h" |
| 20 #include "base/values.h" | 18 #include "base/values.h" |
| 21 #include "net/base/net_errors.h" | 19 #include "net/base/net_errors.h" |
| 22 #include "net/base/net_log.h" | 20 #include "net/base/net_log.h" |
| 23 #include "net/base/net_log_unittest.h" | 21 #include "net/base/net_log_unittest.h" |
| 24 #include "net/base/request_priority.h" | 22 #include "net/base/request_priority.h" |
| 25 #include "net/base/test_completion_callback.h" | 23 #include "net/base/test_completion_callback.h" |
| 26 #include "net/http/http_response_headers.h" | 24 #include "net/http/http_response_headers.h" |
| 27 #include "net/socket/client_socket_factory.h" | 25 #include "net/socket/client_socket_factory.h" |
| 28 #include "net/socket/client_socket_handle.h" | 26 #include "net/socket/client_socket_handle.h" |
| 29 #include "net/socket/client_socket_pool_histograms.h" | 27 #include "net/socket/client_socket_pool_histograms.h" |
| 30 #include "net/socket/socket_test_util.h" | 28 #include "net/socket/socket_test_util.h" |
| 31 #include "net/socket/ssl_host_info.h" | 29 #include "net/socket/ssl_host_info.h" |
| 32 #include "net/socket/stream_socket.h" | 30 #include "net/socket/stream_socket.h" |
| 33 #include "testing/gmock/include/gmock/gmock.h" | |
| 34 #include "testing/gtest/include/gtest/gtest.h" | 31 #include "testing/gtest/include/gtest/gtest.h" |
| 35 | 32 |
| 36 using ::testing::Invoke; | |
| 37 using ::testing::Return; | |
| 38 | |
| 39 namespace net { | 33 namespace net { |
| 40 | 34 |
| 41 namespace { | 35 namespace { |
| 42 | 36 |
| 43 const int kDefaultMaxSockets = 4; | 37 const int kDefaultMaxSockets = 4; |
| 44 const int kDefaultMaxSocketsPerGroup = 2; | 38 const int kDefaultMaxSocketsPerGroup = 2; |
| 45 const net::RequestPriority kDefaultPriority = MEDIUM; | 39 const net::RequestPriority kDefaultPriority = MEDIUM; |
| 46 | 40 |
| 47 class TestSocketParams : public base::RefCounted<TestSocketParams> { | 41 class TestSocketParams : public base::RefCounted<TestSocketParams> { |
| 48 public: | 42 public: |
| 49 TestSocketParams() : ignore_limits_(false) {} | 43 bool ignore_limits() { return false; } |
| 50 | |
| 51 void set_ignore_limits(bool ignore_limits) { | |
| 52 ignore_limits_ = ignore_limits; | |
| 53 } | |
| 54 bool ignore_limits() { return ignore_limits_; } | |
| 55 | |
| 56 private: | 44 private: |
| 57 friend class base::RefCounted<TestSocketParams>; | 45 friend class base::RefCounted<TestSocketParams>; |
| 58 ~TestSocketParams() {} | 46 ~TestSocketParams() {} |
| 59 | |
| 60 bool ignore_limits_; | |
| 61 }; | 47 }; |
| 62 typedef ClientSocketPoolBase<TestSocketParams> TestClientSocketPoolBase; | 48 typedef ClientSocketPoolBase<TestSocketParams> TestClientSocketPoolBase; |
| 63 | 49 |
| 64 class MockClientSocket : public StreamSocket { | 50 class MockClientSocket : public StreamSocket { |
| 65 public: | 51 public: |
| 66 MockClientSocket() : connected_(false), was_used_to_convey_data_(false), | 52 MockClientSocket() : connected_(false), was_used_to_convey_data_(false), |
| 67 num_bytes_read_(0) {} | 53 num_bytes_read_(0) {} |
| 68 | 54 |
| 69 // Socket implementation. | 55 // Socket implementation. |
| 70 virtual int Read( | 56 virtual int Read( |
| (...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 362 bool store_additional_error_state_; | 348 bool store_additional_error_state_; |
| 363 | 349 |
| 364 DISALLOW_COPY_AND_ASSIGN(TestConnectJob); | 350 DISALLOW_COPY_AND_ASSIGN(TestConnectJob); |
| 365 }; | 351 }; |
| 366 | 352 |
| 367 class TestConnectJobFactory | 353 class TestConnectJobFactory |
| 368 : public TestClientSocketPoolBase::ConnectJobFactory { | 354 : public TestClientSocketPoolBase::ConnectJobFactory { |
| 369 public: | 355 public: |
| 370 explicit TestConnectJobFactory(MockClientSocketFactory* client_socket_factory) | 356 explicit TestConnectJobFactory(MockClientSocketFactory* client_socket_factory) |
| 371 : job_type_(TestConnectJob::kMockJob), | 357 : job_type_(TestConnectJob::kMockJob), |
| 372 job_types_(NULL), | |
| 373 client_socket_factory_(client_socket_factory) {} | 358 client_socket_factory_(client_socket_factory) {} |
| 374 | 359 |
| 375 virtual ~TestConnectJobFactory() {} | 360 virtual ~TestConnectJobFactory() {} |
| 376 | 361 |
| 377 void set_job_type(TestConnectJob::JobType job_type) { job_type_ = job_type; } | 362 void set_job_type(TestConnectJob::JobType job_type) { job_type_ = job_type; } |
| 378 | 363 |
| 379 void set_job_types(std::list<TestConnectJob::JobType>* job_types) { | |
| 380 job_types_ = job_types; | |
| 381 CHECK(!job_types_->empty()); | |
| 382 } | |
| 383 | |
| 384 void set_timeout_duration(base::TimeDelta timeout_duration) { | 364 void set_timeout_duration(base::TimeDelta timeout_duration) { |
| 385 timeout_duration_ = timeout_duration; | 365 timeout_duration_ = timeout_duration; |
| 386 } | 366 } |
| 387 | 367 |
| 388 // ConnectJobFactory implementation. | 368 // ConnectJobFactory implementation. |
| 389 | 369 |
| 390 virtual ConnectJob* NewConnectJob( | 370 virtual ConnectJob* NewConnectJob( |
| 391 const std::string& group_name, | 371 const std::string& group_name, |
| 392 const TestClientSocketPoolBase::Request& request, | 372 const TestClientSocketPoolBase::Request& request, |
| 393 ConnectJob::Delegate* delegate) const { | 373 ConnectJob::Delegate* delegate) const { |
| 394 EXPECT_TRUE(!job_types_ || !job_types_->empty()); | 374 return new TestConnectJob(job_type_, |
| 395 TestConnectJob::JobType job_type = job_type_; | |
| 396 if (job_types_ && !job_types_->empty()) { | |
| 397 job_type = job_types_->front(); | |
| 398 job_types_->pop_front(); | |
| 399 } | |
| 400 return new TestConnectJob(job_type, | |
| 401 group_name, | 375 group_name, |
| 402 request, | 376 request, |
| 403 timeout_duration_, | 377 timeout_duration_, |
| 404 delegate, | 378 delegate, |
| 405 client_socket_factory_, | 379 client_socket_factory_, |
| 406 NULL); | 380 NULL); |
| 407 } | 381 } |
| 408 | 382 |
| 409 virtual base::TimeDelta ConnectionTimeout() const { | 383 virtual base::TimeDelta ConnectionTimeout() const { |
| 410 return timeout_duration_; | 384 return timeout_duration_; |
| 411 } | 385 } |
| 412 | 386 |
| 413 private: | 387 private: |
| 414 TestConnectJob::JobType job_type_; | 388 TestConnectJob::JobType job_type_; |
| 415 std::list<TestConnectJob::JobType>* job_types_; | |
| 416 base::TimeDelta timeout_duration_; | 389 base::TimeDelta timeout_duration_; |
| 417 MockClientSocketFactory* const client_socket_factory_; | 390 MockClientSocketFactory* const client_socket_factory_; |
| 418 | 391 |
| 419 DISALLOW_COPY_AND_ASSIGN(TestConnectJobFactory); | 392 DISALLOW_COPY_AND_ASSIGN(TestConnectJobFactory); |
| 420 }; | 393 }; |
| 421 | 394 |
| 422 class TestClientSocketPool : public ClientSocketPool { | 395 class TestClientSocketPool : public ClientSocketPool { |
| 423 public: | 396 public: |
| 424 TestClientSocketPool( | 397 TestClientSocketPool( |
| 425 int max_sockets, | 398 int max_sockets, |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 467 const std::string& group_name, | 440 const std::string& group_name, |
| 468 StreamSocket* socket, | 441 StreamSocket* socket, |
| 469 int id) OVERRIDE { | 442 int id) OVERRIDE { |
| 470 base_.ReleaseSocket(group_name, socket, id); | 443 base_.ReleaseSocket(group_name, socket, id); |
| 471 } | 444 } |
| 472 | 445 |
| 473 virtual void Flush() OVERRIDE { | 446 virtual void Flush() OVERRIDE { |
| 474 base_.Flush(); | 447 base_.Flush(); |
| 475 } | 448 } |
| 476 | 449 |
| 477 virtual bool IsStalled() const OVERRIDE { | |
| 478 return base_.IsStalled(); | |
| 479 } | |
| 480 | |
| 481 virtual void CloseIdleSockets() OVERRIDE { | 450 virtual void CloseIdleSockets() OVERRIDE { |
| 482 base_.CloseIdleSockets(); | 451 base_.CloseIdleSockets(); |
| 483 } | 452 } |
| 484 | 453 |
| 485 virtual int IdleSocketCount() const OVERRIDE { | 454 virtual int IdleSocketCount() const OVERRIDE { |
| 486 return base_.idle_socket_count(); | 455 return base_.idle_socket_count(); |
| 487 } | 456 } |
| 488 | 457 |
| 489 virtual int IdleSocketCountInGroup( | 458 virtual int IdleSocketCountInGroup( |
| 490 const std::string& group_name) const OVERRIDE { | 459 const std::string& group_name) const OVERRIDE { |
| 491 return base_.IdleSocketCountInGroup(group_name); | 460 return base_.IdleSocketCountInGroup(group_name); |
| 492 } | 461 } |
| 493 | 462 |
| 494 virtual LoadState GetLoadState( | 463 virtual LoadState GetLoadState( |
| 495 const std::string& group_name, | 464 const std::string& group_name, |
| 496 const ClientSocketHandle* handle) const OVERRIDE { | 465 const ClientSocketHandle* handle) const OVERRIDE { |
| 497 return base_.GetLoadState(group_name, handle); | 466 return base_.GetLoadState(group_name, handle); |
| 498 } | 467 } |
| 499 | 468 |
| 500 virtual void AddLayeredPool(LayeredPool* pool) OVERRIDE { | |
| 501 base_.AddLayeredPool(pool); | |
| 502 } | |
| 503 | |
| 504 virtual void RemoveLayeredPool(LayeredPool* pool) OVERRIDE { | |
| 505 base_.RemoveLayeredPool(pool); | |
| 506 } | |
| 507 | |
| 508 virtual DictionaryValue* GetInfoAsValue( | 469 virtual DictionaryValue* GetInfoAsValue( |
| 509 const std::string& name, | 470 const std::string& name, |
| 510 const std::string& type, | 471 const std::string& type, |
| 511 bool include_nested_pools) const OVERRIDE { | 472 bool include_nested_pools) const OVERRIDE { |
| 512 return base_.GetInfoAsValue(name, type); | 473 return base_.GetInfoAsValue(name, type); |
| 513 } | 474 } |
| 514 | 475 |
| 515 virtual base::TimeDelta ConnectionTimeout() const OVERRIDE { | 476 virtual base::TimeDelta ConnectionTimeout() const OVERRIDE { |
| 516 return base_.ConnectionTimeout(); | 477 return base_.ConnectionTimeout(); |
| 517 } | 478 } |
| (...skipping 13 matching lines...) Expand all Loading... |
| 531 } | 492 } |
| 532 | 493 |
| 533 bool HasGroup(const std::string& group_name) const { | 494 bool HasGroup(const std::string& group_name) const { |
| 534 return base_.HasGroup(group_name); | 495 return base_.HasGroup(group_name); |
| 535 } | 496 } |
| 536 | 497 |
| 537 void CleanupTimedOutIdleSockets() { base_.CleanupIdleSockets(false); } | 498 void CleanupTimedOutIdleSockets() { base_.CleanupIdleSockets(false); } |
| 538 | 499 |
| 539 void EnableConnectBackupJobs() { base_.EnableConnectBackupJobs(); } | 500 void EnableConnectBackupJobs() { base_.EnableConnectBackupJobs(); } |
| 540 | 501 |
| 541 bool CloseOneIdleConnectionInLayeredPool() { | |
| 542 return base_.CloseOneIdleConnectionInLayeredPool(); | |
| 543 } | |
| 544 | |
| 545 private: | 502 private: |
| 546 TestClientSocketPoolBase base_; | 503 TestClientSocketPoolBase base_; |
| 547 | 504 |
| 548 DISALLOW_COPY_AND_ASSIGN(TestClientSocketPool); | 505 DISALLOW_COPY_AND_ASSIGN(TestClientSocketPool); |
| 549 }; | 506 }; |
| 550 | 507 |
| 551 } // namespace | 508 } // namespace |
| 552 | 509 |
| 553 REGISTER_SOCKET_PARAMS_FOR_POOL(TestClientSocketPool, TestSocketParams); | 510 REGISTER_SOCKET_PARAMS_FOR_POOL(TestClientSocketPool, TestSocketParams); |
| 554 | 511 |
| (...skipping 645 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1200 EXPECT_EQ(1, pool_->IdleSocketCount()); | 1157 EXPECT_EQ(1, pool_->IdleSocketCount()); |
| 1201 } | 1158 } |
| 1202 | 1159 |
| 1203 TEST_F(ClientSocketPoolBaseTest, WaitForStalledSocketAtSocketLimit) { | 1160 TEST_F(ClientSocketPoolBaseTest, WaitForStalledSocketAtSocketLimit) { |
| 1204 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); | 1161 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
| 1205 connect_job_factory_->set_job_type(TestConnectJob::kMockJob); | 1162 connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
| 1206 | 1163 |
| 1207 ClientSocketHandle stalled_handle; | 1164 ClientSocketHandle stalled_handle; |
| 1208 TestCompletionCallback callback; | 1165 TestCompletionCallback callback; |
| 1209 { | 1166 { |
| 1210 EXPECT_FALSE(pool_->IsStalled()); | |
| 1211 ClientSocketHandle handles[kDefaultMaxSockets]; | 1167 ClientSocketHandle handles[kDefaultMaxSockets]; |
| 1212 for (int i = 0; i < kDefaultMaxSockets; ++i) { | 1168 for (int i = 0; i < kDefaultMaxSockets; ++i) { |
| 1213 TestCompletionCallback callback; | 1169 TestCompletionCallback callback; |
| 1214 EXPECT_EQ(OK, handles[i].Init(base::StringPrintf( | 1170 EXPECT_EQ(OK, handles[i].Init(base::StringPrintf( |
| 1215 "Take 2: %d", i), | 1171 "Take 2: %d", i), |
| 1216 params_, | 1172 params_, |
| 1217 kDefaultPriority, | 1173 kDefaultPriority, |
| 1218 callback.callback(), | 1174 callback.callback(), |
| 1219 pool_.get(), | 1175 pool_.get(), |
| 1220 BoundNetLog())); | 1176 BoundNetLog())); |
| 1221 } | 1177 } |
| 1222 | 1178 |
| 1223 EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); | 1179 EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
| 1224 EXPECT_EQ(0, pool_->IdleSocketCount()); | 1180 EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 1225 EXPECT_FALSE(pool_->IsStalled()); | |
| 1226 | 1181 |
| 1227 // Now we will hit the socket limit. | 1182 // Now we will hit the socket limit. |
| 1228 EXPECT_EQ(ERR_IO_PENDING, stalled_handle.Init("foo", | 1183 EXPECT_EQ(ERR_IO_PENDING, stalled_handle.Init("foo", |
| 1229 params_, | 1184 params_, |
| 1230 kDefaultPriority, | 1185 kDefaultPriority, |
| 1231 callback.callback(), | 1186 callback.callback(), |
| 1232 pool_.get(), | 1187 pool_.get(), |
| 1233 BoundNetLog())); | 1188 BoundNetLog())); |
| 1234 EXPECT_TRUE(pool_->IsStalled()); | |
| 1235 | 1189 |
| 1236 // Dropping out of scope will close all handles and return them to idle. | 1190 // Dropping out of scope will close all handles and return them to idle. |
| 1237 } | 1191 } |
| 1238 | 1192 |
| 1239 // But if we wait for it, the released idle sockets will be closed in | 1193 // But if we wait for it, the released idle sockets will be closed in |
| 1240 // preference of the waiting request. | 1194 // preference of the waiting request. |
| 1241 EXPECT_EQ(OK, callback.WaitForResult()); | 1195 EXPECT_EQ(OK, callback.WaitForResult()); |
| 1242 | 1196 |
| 1243 EXPECT_EQ(kDefaultMaxSockets + 1, client_socket_factory_.allocation_count()); | 1197 EXPECT_EQ(kDefaultMaxSockets + 1, client_socket_factory_.allocation_count()); |
| 1244 EXPECT_EQ(3, pool_->IdleSocketCount()); | 1198 EXPECT_EQ(3, pool_->IdleSocketCount()); |
| (...skipping 799 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2044 // job. Release the socket. Run the loop again to make sure the second | 1998 // job. Release the socket. Run the loop again to make sure the second |
| 2045 // socket is sitting idle and the first one is released (since ReleaseSocket() | 1999 // socket is sitting idle and the first one is released (since ReleaseSocket() |
| 2046 // just posts a DoReleaseSocket() task). | 2000 // just posts a DoReleaseSocket() task). |
| 2047 | 2001 |
| 2048 handle.Reset(); | 2002 handle.Reset(); |
| 2049 EXPECT_EQ(OK, callback2.WaitForResult()); | 2003 EXPECT_EQ(OK, callback2.WaitForResult()); |
| 2050 // Use the socket. | 2004 // Use the socket. |
| 2051 EXPECT_EQ(1, handle2.socket()->Write(NULL, 1, CompletionCallback())); | 2005 EXPECT_EQ(1, handle2.socket()->Write(NULL, 1, CompletionCallback())); |
| 2052 handle2.Reset(); | 2006 handle2.Reset(); |
| 2053 | 2007 |
| 2054 // The idle socket timeout value was set to 10 milliseconds. Wait 100 | 2008 // The idle socket timeout value was set to 10 milliseconds. Wait 20 |
| 2055 // milliseconds so the sockets timeout. | 2009 // milliseconds so the sockets timeout. |
| 2056 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(100)); | 2010 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(20)); |
| 2057 MessageLoop::current()->RunAllPending(); | 2011 MessageLoop::current()->RunAllPending(); |
| 2058 | 2012 |
| 2059 ASSERT_EQ(2, pool_->IdleSocketCount()); | 2013 ASSERT_EQ(2, pool_->IdleSocketCount()); |
| 2060 | 2014 |
| 2061 // Request a new socket. This should cleanup the unused and timed out ones. | 2015 // Request a new socket. This should cleanup the unused and timed out ones. |
| 2062 // A new socket will be created rather than reusing the idle one. | 2016 // A new socket will be created rather than reusing the idle one. |
| 2063 CapturingBoundNetLog log(CapturingNetLog::kUnbounded); | 2017 CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
| 2064 TestCompletionCallback callback3; | 2018 TestCompletionCallback callback3; |
| 2065 rv = handle.Init("a", | 2019 rv = handle.Init("a", |
| 2066 params_, | 2020 params_, |
| (...skipping 1011 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3078 CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); | 3032 CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
| 3079 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); | 3033 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3080 | 3034 |
| 3081 ASSERT_FALSE(pool_->HasGroup("a")); | 3035 ASSERT_FALSE(pool_->HasGroup("a")); |
| 3082 | 3036 |
| 3083 pool_->RequestSockets("a", ¶ms_, kDefaultMaxSockets - 1, | 3037 pool_->RequestSockets("a", ¶ms_, kDefaultMaxSockets - 1, |
| 3084 BoundNetLog()); | 3038 BoundNetLog()); |
| 3085 | 3039 |
| 3086 ASSERT_TRUE(pool_->HasGroup("a")); | 3040 ASSERT_TRUE(pool_->HasGroup("a")); |
| 3087 EXPECT_EQ(kDefaultMaxSockets - 1, pool_->NumConnectJobsInGroup("a")); | 3041 EXPECT_EQ(kDefaultMaxSockets - 1, pool_->NumConnectJobsInGroup("a")); |
| 3088 EXPECT_FALSE(pool_->IsStalled()); | |
| 3089 | 3042 |
| 3090 ASSERT_FALSE(pool_->HasGroup("b")); | 3043 ASSERT_FALSE(pool_->HasGroup("b")); |
| 3091 | 3044 |
| 3092 pool_->RequestSockets("b", ¶ms_, kDefaultMaxSockets, | 3045 pool_->RequestSockets("b", ¶ms_, kDefaultMaxSockets, |
| 3093 BoundNetLog()); | 3046 BoundNetLog()); |
| 3094 | 3047 |
| 3095 ASSERT_TRUE(pool_->HasGroup("b")); | 3048 ASSERT_TRUE(pool_->HasGroup("b")); |
| 3096 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("b")); | 3049 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("b")); |
| 3097 EXPECT_FALSE(pool_->IsStalled()); | |
| 3098 } | 3050 } |
| 3099 | 3051 |
| 3100 TEST_F(ClientSocketPoolBaseTest, RequestSocketsCountIdleSockets) { | 3052 TEST_F(ClientSocketPoolBaseTest, RequestSocketsCountIdleSockets) { |
| 3101 CreatePool(4, 4); | 3053 CreatePool(4, 4); |
| 3102 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); | 3054 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
| 3103 | 3055 |
| 3104 ClientSocketHandle handle1; | 3056 ClientSocketHandle handle1; |
| 3105 TestCompletionCallback callback1; | 3057 TestCompletionCallback callback1; |
| 3106 EXPECT_EQ(ERR_IO_PENDING, handle1.Init("a", | 3058 EXPECT_EQ(ERR_IO_PENDING, handle1.Init("a", |
| 3107 params_, | 3059 params_, |
| (...skipping 298 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3406 EXPECT_EQ(0, pool_->NumActiveSocketsInGroup("a")); | 3358 EXPECT_EQ(0, pool_->NumActiveSocketsInGroup("a")); |
| 3407 ASSERT_EQ(OK, callback.WaitForResult()); | 3359 ASSERT_EQ(OK, callback.WaitForResult()); |
| 3408 | 3360 |
| 3409 // The hung connect job should still be there, but everything else should be | 3361 // The hung connect job should still be there, but everything else should be |
| 3410 // complete. | 3362 // complete. |
| 3411 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a")); | 3363 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a")); |
| 3412 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a")); | 3364 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a")); |
| 3413 EXPECT_EQ(1, pool_->NumActiveSocketsInGroup("a")); | 3365 EXPECT_EQ(1, pool_->NumActiveSocketsInGroup("a")); |
| 3414 } | 3366 } |
| 3415 | 3367 |
| 3416 class MockLayeredPool : public LayeredPool { | |
| 3417 public: | |
| 3418 MockLayeredPool(TestClientSocketPool* pool, | |
| 3419 const std::string& group_name) | |
| 3420 : pool_(pool), | |
| 3421 params_(new TestSocketParams), | |
| 3422 group_name_(group_name), | |
| 3423 can_release_connection_(true) { | |
| 3424 pool_->AddLayeredPool(this); | |
| 3425 } | |
| 3426 | |
| 3427 ~MockLayeredPool() { | |
| 3428 pool_->RemoveLayeredPool(this); | |
| 3429 } | |
| 3430 | |
| 3431 int RequestSocket(TestClientSocketPool* pool) { | |
| 3432 return handle_.Init(group_name_, params_, kDefaultPriority, | |
| 3433 callback_.callback(), pool, BoundNetLog()); | |
| 3434 } | |
| 3435 | |
| 3436 int RequestSocketWithoutLimits(TestClientSocketPool* pool) { | |
| 3437 params_->set_ignore_limits(true); | |
| 3438 return handle_.Init(group_name_, params_, kDefaultPriority, | |
| 3439 callback_.callback(), pool, BoundNetLog()); | |
| 3440 } | |
| 3441 | |
| 3442 bool ReleaseOneConnection() { | |
| 3443 if (!handle_.is_initialized() || !can_release_connection_) { | |
| 3444 return false; | |
| 3445 } | |
| 3446 handle_.socket()->Disconnect(); | |
| 3447 handle_.Reset(); | |
| 3448 return true; | |
| 3449 } | |
| 3450 | |
| 3451 void set_can_release_connection(bool can_release_connection) { | |
| 3452 can_release_connection_ = can_release_connection; | |
| 3453 } | |
| 3454 | |
| 3455 MOCK_METHOD0(CloseOneIdleConnection, bool()); | |
| 3456 | |
| 3457 private: | |
| 3458 TestClientSocketPool* const pool_; | |
| 3459 scoped_refptr<TestSocketParams> params_; | |
| 3460 ClientSocketHandle handle_; | |
| 3461 TestCompletionCallback callback_; | |
| 3462 const std::string group_name_; | |
| 3463 bool can_release_connection_; | |
| 3464 }; | |
| 3465 | |
| 3466 TEST_F(ClientSocketPoolBaseTest, FailToCloseIdleSocketsNotHeldByLayeredPool) { | |
| 3467 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); | |
| 3468 connect_job_factory_->set_job_type(TestConnectJob::kMockJob); | |
| 3469 | |
| 3470 MockLayeredPool mock_layered_pool(pool_.get(), "foo"); | |
| 3471 EXPECT_EQ(OK, mock_layered_pool.RequestSocket(pool_.get())); | |
| 3472 EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) | |
| 3473 .WillOnce(Return(false)); | |
| 3474 EXPECT_FALSE(pool_->CloseOneIdleConnectionInLayeredPool()); | |
| 3475 } | |
| 3476 | |
| 3477 TEST_F(ClientSocketPoolBaseTest, ForciblyCloseIdleSocketsHeldByLayeredPool) { | |
| 3478 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); | |
| 3479 connect_job_factory_->set_job_type(TestConnectJob::kMockJob); | |
| 3480 | |
| 3481 MockLayeredPool mock_layered_pool(pool_.get(), "foo"); | |
| 3482 EXPECT_EQ(OK, mock_layered_pool.RequestSocket(pool_.get())); | |
| 3483 EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) | |
| 3484 .WillOnce(Invoke(&mock_layered_pool, | |
| 3485 &MockLayeredPool::ReleaseOneConnection)); | |
| 3486 EXPECT_TRUE(pool_->CloseOneIdleConnectionInLayeredPool()); | |
| 3487 } | |
| 3488 | |
| 3489 // This test exercises the codepath which caused http://crbug.com/109876 | |
| 3490 TEST_F(ClientSocketPoolBaseTest, | |
| 3491 CloseIdleSocketsHeldByLayeredPoolInSameGroupWhenNeeded) { | |
| 3492 CreatePool(2, 2); | |
| 3493 std::list<TestConnectJob::JobType> job_types; | |
| 3494 job_types.push_back(TestConnectJob::kMockJob); | |
| 3495 job_types.push_back(TestConnectJob::kMockJob); | |
| 3496 job_types.push_back(TestConnectJob::kMockFailingJob); | |
| 3497 job_types.push_back(TestConnectJob::kMockJob); | |
| 3498 connect_job_factory_->set_job_types(&job_types); | |
| 3499 | |
| 3500 ClientSocketHandle handle1; | |
| 3501 TestCompletionCallback callback1; | |
| 3502 EXPECT_EQ(OK, handle1.Init("group1", | |
| 3503 params_, | |
| 3504 kDefaultPriority, | |
| 3505 callback1.callback(), | |
| 3506 pool_.get(), | |
| 3507 BoundNetLog())); | |
| 3508 | |
| 3509 MockLayeredPool mock_layered_pool(pool_.get(), "group2"); | |
| 3510 EXPECT_EQ(OK, mock_layered_pool.RequestSocket(pool_.get())); | |
| 3511 EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) | |
| 3512 .WillRepeatedly(Invoke(&mock_layered_pool, | |
| 3513 &MockLayeredPool::ReleaseOneConnection)); | |
| 3514 mock_layered_pool.set_can_release_connection(false); | |
| 3515 | |
| 3516 // This connection attempt will fail when the next request causes the | |
| 3517 // MockLayeredPool to delete the socket it's holding. This request is | |
| 3518 // needed to trigger the destruction of the "group2" Group. | |
| 3519 ClientSocketHandle handle3; | |
| 3520 TestCompletionCallback callback3; | |
| 3521 EXPECT_EQ(ERR_IO_PENDING, handle3.Init("group2", | |
| 3522 params_, | |
| 3523 kDefaultPriority, | |
| 3524 callback3.callback(), | |
| 3525 pool_.get(), | |
| 3526 BoundNetLog())); | |
| 3527 | |
| 3528 mock_layered_pool.set_can_release_connection(true); | |
| 3529 ClientSocketHandle handle4; | |
| 3530 TestCompletionCallback callback4; | |
| 3531 EXPECT_EQ(OK, handle4.Init("group2", | |
| 3532 params_, | |
| 3533 kDefaultPriority, | |
| 3534 callback4.callback(), | |
| 3535 pool_.get(), | |
| 3536 BoundNetLog())); | |
| 3537 } | |
| 3538 | |
| 3539 TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketsHeldByLayeredPoolWhenNeeded) { | |
| 3540 CreatePool(1, 1); | |
| 3541 connect_job_factory_->set_job_type(TestConnectJob::kMockJob); | |
| 3542 | |
| 3543 MockLayeredPool mock_layered_pool(pool_.get(), "foo"); | |
| 3544 EXPECT_EQ(OK, mock_layered_pool.RequestSocket(pool_.get())); | |
| 3545 EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection()) | |
| 3546 .WillOnce(Invoke(&mock_layered_pool, | |
| 3547 &MockLayeredPool::ReleaseOneConnection)); | |
| 3548 ClientSocketHandle handle; | |
| 3549 TestCompletionCallback callback; | |
| 3550 EXPECT_EQ(OK, handle.Init("a", | |
| 3551 params_, | |
| 3552 kDefaultPriority, | |
| 3553 callback.callback(), | |
| 3554 pool_.get(), | |
| 3555 BoundNetLog())); | |
| 3556 } | |
| 3557 | |
| 3558 TEST_F(ClientSocketPoolBaseTest, | |
| 3559 CloseMultipleIdleSocketsHeldByLayeredPoolWhenNeeded) { | |
| 3560 CreatePool(1, 1); | |
| 3561 connect_job_factory_->set_job_type(TestConnectJob::kMockJob); | |
| 3562 | |
| 3563 MockLayeredPool mock_layered_pool1(pool_.get(), "foo"); | |
| 3564 EXPECT_EQ(OK, mock_layered_pool1.RequestSocket(pool_.get())); | |
| 3565 EXPECT_CALL(mock_layered_pool1, CloseOneIdleConnection()) | |
| 3566 .WillRepeatedly(Invoke(&mock_layered_pool1, | |
| 3567 &MockLayeredPool::ReleaseOneConnection)); | |
| 3568 MockLayeredPool mock_layered_pool2(pool_.get(), "bar"); | |
| 3569 EXPECT_EQ(OK, mock_layered_pool2.RequestSocketWithoutLimits(pool_.get())); | |
| 3570 EXPECT_CALL(mock_layered_pool2, CloseOneIdleConnection()) | |
| 3571 .WillRepeatedly(Invoke(&mock_layered_pool2, | |
| 3572 &MockLayeredPool::ReleaseOneConnection)); | |
| 3573 ClientSocketHandle handle; | |
| 3574 TestCompletionCallback callback; | |
| 3575 EXPECT_EQ(OK, handle.Init("a", | |
| 3576 params_, | |
| 3577 kDefaultPriority, | |
| 3578 callback.callback(), | |
| 3579 pool_.get(), | |
| 3580 BoundNetLog())); | |
| 3581 } | |
| 3582 | |
| 3583 } // namespace | 3368 } // namespace |
| 3584 | 3369 |
| 3585 } // namespace net | 3370 } // namespace net |
| OLD | NEW |