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 |
7 #include "base/bind.h" | 9 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
9 #include "base/callback.h" | 11 #include "base/callback.h" |
10 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
11 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
12 #include "base/memory/scoped_vector.h" | 14 #include "base/memory/scoped_vector.h" |
13 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
14 #include "base/message_loop.h" | 16 #include "base/message_loop.h" |
15 #include "base/stringprintf.h" | 17 #include "base/stringprintf.h" |
16 #include "base/string_number_conversions.h" | 18 #include "base/string_number_conversions.h" |
17 #include "base/threading/platform_thread.h" | 19 #include "base/threading/platform_thread.h" |
18 #include "base/values.h" | 20 #include "base/values.h" |
19 #include "net/base/net_errors.h" | 21 #include "net/base/net_errors.h" |
20 #include "net/base/net_log.h" | 22 #include "net/base/net_log.h" |
21 #include "net/base/net_log_unittest.h" | 23 #include "net/base/net_log_unittest.h" |
22 #include "net/base/request_priority.h" | 24 #include "net/base/request_priority.h" |
23 #include "net/base/test_completion_callback.h" | 25 #include "net/base/test_completion_callback.h" |
24 #include "net/http/http_response_headers.h" | 26 #include "net/http/http_response_headers.h" |
25 #include "net/socket/client_socket_factory.h" | 27 #include "net/socket/client_socket_factory.h" |
26 #include "net/socket/client_socket_handle.h" | 28 #include "net/socket/client_socket_handle.h" |
27 #include "net/socket/client_socket_pool_histograms.h" | 29 #include "net/socket/client_socket_pool_histograms.h" |
28 #include "net/socket/socket_test_util.h" | 30 #include "net/socket/socket_test_util.h" |
29 #include "net/socket/ssl_host_info.h" | 31 #include "net/socket/ssl_host_info.h" |
30 #include "net/socket/stream_socket.h" | 32 #include "net/socket/stream_socket.h" |
| 33 #include "testing/gmock/include/gmock/gmock.h" |
31 #include "testing/gtest/include/gtest/gtest.h" | 34 #include "testing/gtest/include/gtest/gtest.h" |
32 | 35 |
| 36 using ::testing::Invoke; |
| 37 using ::testing::Return; |
| 38 |
33 namespace net { | 39 namespace net { |
34 | 40 |
35 namespace { | 41 namespace { |
36 | 42 |
37 const int kDefaultMaxSockets = 4; | 43 const int kDefaultMaxSockets = 4; |
38 const int kDefaultMaxSocketsPerGroup = 2; | 44 const int kDefaultMaxSocketsPerGroup = 2; |
39 const net::RequestPriority kDefaultPriority = MEDIUM; | 45 const net::RequestPriority kDefaultPriority = MEDIUM; |
40 | 46 |
41 class TestSocketParams : public base::RefCounted<TestSocketParams> { | 47 class TestSocketParams : public base::RefCounted<TestSocketParams> { |
42 public: | 48 public: |
43 bool ignore_limits() { return false; } | 49 TestSocketParams() : ignore_limits_(false) {} |
| 50 |
| 51 void set_ignore_limits(bool ignore_limits) { |
| 52 ignore_limits_ = ignore_limits; |
| 53 } |
| 54 bool ignore_limits() { return ignore_limits_; } |
| 55 |
44 private: | 56 private: |
45 friend class base::RefCounted<TestSocketParams>; | 57 friend class base::RefCounted<TestSocketParams>; |
46 ~TestSocketParams() {} | 58 ~TestSocketParams() {} |
| 59 |
| 60 bool ignore_limits_; |
47 }; | 61 }; |
48 typedef ClientSocketPoolBase<TestSocketParams> TestClientSocketPoolBase; | 62 typedef ClientSocketPoolBase<TestSocketParams> TestClientSocketPoolBase; |
49 | 63 |
50 class MockClientSocket : public StreamSocket { | 64 class MockClientSocket : public StreamSocket { |
51 public: | 65 public: |
52 MockClientSocket() : connected_(false), was_used_to_convey_data_(false), | 66 MockClientSocket() : connected_(false), was_used_to_convey_data_(false), |
53 num_bytes_read_(0) {} | 67 num_bytes_read_(0) {} |
54 | 68 |
55 // Socket implementation. | 69 // Socket implementation. |
56 virtual int Read( | 70 virtual int Read( |
(...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
348 bool store_additional_error_state_; | 362 bool store_additional_error_state_; |
349 | 363 |
350 DISALLOW_COPY_AND_ASSIGN(TestConnectJob); | 364 DISALLOW_COPY_AND_ASSIGN(TestConnectJob); |
351 }; | 365 }; |
352 | 366 |
353 class TestConnectJobFactory | 367 class TestConnectJobFactory |
354 : public TestClientSocketPoolBase::ConnectJobFactory { | 368 : public TestClientSocketPoolBase::ConnectJobFactory { |
355 public: | 369 public: |
356 explicit TestConnectJobFactory(MockClientSocketFactory* client_socket_factory) | 370 explicit TestConnectJobFactory(MockClientSocketFactory* client_socket_factory) |
357 : job_type_(TestConnectJob::kMockJob), | 371 : job_type_(TestConnectJob::kMockJob), |
| 372 job_types_(NULL), |
358 client_socket_factory_(client_socket_factory) {} | 373 client_socket_factory_(client_socket_factory) {} |
359 | 374 |
360 virtual ~TestConnectJobFactory() {} | 375 virtual ~TestConnectJobFactory() {} |
361 | 376 |
362 void set_job_type(TestConnectJob::JobType job_type) { job_type_ = job_type; } | 377 void set_job_type(TestConnectJob::JobType job_type) { job_type_ = job_type; } |
363 | 378 |
| 379 void set_job_types(std::list<TestConnectJob::JobType>* job_types) { |
| 380 job_types_ = job_types; |
| 381 CHECK(!job_types_->empty()); |
| 382 } |
| 383 |
364 void set_timeout_duration(base::TimeDelta timeout_duration) { | 384 void set_timeout_duration(base::TimeDelta timeout_duration) { |
365 timeout_duration_ = timeout_duration; | 385 timeout_duration_ = timeout_duration; |
366 } | 386 } |
367 | 387 |
368 // ConnectJobFactory implementation. | 388 // ConnectJobFactory implementation. |
369 | 389 |
370 virtual ConnectJob* NewConnectJob( | 390 virtual ConnectJob* NewConnectJob( |
371 const std::string& group_name, | 391 const std::string& group_name, |
372 const TestClientSocketPoolBase::Request& request, | 392 const TestClientSocketPoolBase::Request& request, |
373 ConnectJob::Delegate* delegate) const { | 393 ConnectJob::Delegate* delegate) const { |
374 return new TestConnectJob(job_type_, | 394 EXPECT_TRUE(!job_types_ || !job_types_->empty()); |
| 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, |
375 group_name, | 401 group_name, |
376 request, | 402 request, |
377 timeout_duration_, | 403 timeout_duration_, |
378 delegate, | 404 delegate, |
379 client_socket_factory_, | 405 client_socket_factory_, |
380 NULL); | 406 NULL); |
381 } | 407 } |
382 | 408 |
383 virtual base::TimeDelta ConnectionTimeout() const { | 409 virtual base::TimeDelta ConnectionTimeout() const { |
384 return timeout_duration_; | 410 return timeout_duration_; |
385 } | 411 } |
386 | 412 |
387 private: | 413 private: |
388 TestConnectJob::JobType job_type_; | 414 TestConnectJob::JobType job_type_; |
| 415 std::list<TestConnectJob::JobType>* job_types_; |
389 base::TimeDelta timeout_duration_; | 416 base::TimeDelta timeout_duration_; |
390 MockClientSocketFactory* const client_socket_factory_; | 417 MockClientSocketFactory* const client_socket_factory_; |
391 | 418 |
392 DISALLOW_COPY_AND_ASSIGN(TestConnectJobFactory); | 419 DISALLOW_COPY_AND_ASSIGN(TestConnectJobFactory); |
393 }; | 420 }; |
394 | 421 |
395 class TestClientSocketPool : public ClientSocketPool { | 422 class TestClientSocketPool : public ClientSocketPool { |
396 public: | 423 public: |
397 TestClientSocketPool( | 424 TestClientSocketPool( |
398 int max_sockets, | 425 int max_sockets, |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
440 const std::string& group_name, | 467 const std::string& group_name, |
441 StreamSocket* socket, | 468 StreamSocket* socket, |
442 int id) OVERRIDE { | 469 int id) OVERRIDE { |
443 base_.ReleaseSocket(group_name, socket, id); | 470 base_.ReleaseSocket(group_name, socket, id); |
444 } | 471 } |
445 | 472 |
446 virtual void Flush() OVERRIDE { | 473 virtual void Flush() OVERRIDE { |
447 base_.Flush(); | 474 base_.Flush(); |
448 } | 475 } |
449 | 476 |
| 477 virtual bool IsStalled() const OVERRIDE { |
| 478 return base_.IsStalled(); |
| 479 } |
| 480 |
450 virtual void CloseIdleSockets() OVERRIDE { | 481 virtual void CloseIdleSockets() OVERRIDE { |
451 base_.CloseIdleSockets(); | 482 base_.CloseIdleSockets(); |
452 } | 483 } |
453 | 484 |
454 virtual int IdleSocketCount() const OVERRIDE { | 485 virtual int IdleSocketCount() const OVERRIDE { |
455 return base_.idle_socket_count(); | 486 return base_.idle_socket_count(); |
456 } | 487 } |
457 | 488 |
458 virtual int IdleSocketCountInGroup( | 489 virtual int IdleSocketCountInGroup( |
459 const std::string& group_name) const OVERRIDE { | 490 const std::string& group_name) const OVERRIDE { |
460 return base_.IdleSocketCountInGroup(group_name); | 491 return base_.IdleSocketCountInGroup(group_name); |
461 } | 492 } |
462 | 493 |
463 virtual LoadState GetLoadState( | 494 virtual LoadState GetLoadState( |
464 const std::string& group_name, | 495 const std::string& group_name, |
465 const ClientSocketHandle* handle) const OVERRIDE { | 496 const ClientSocketHandle* handle) const OVERRIDE { |
466 return base_.GetLoadState(group_name, handle); | 497 return base_.GetLoadState(group_name, handle); |
467 } | 498 } |
468 | 499 |
| 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 |
469 virtual DictionaryValue* GetInfoAsValue( | 508 virtual DictionaryValue* GetInfoAsValue( |
470 const std::string& name, | 509 const std::string& name, |
471 const std::string& type, | 510 const std::string& type, |
472 bool include_nested_pools) const OVERRIDE { | 511 bool include_nested_pools) const OVERRIDE { |
473 return base_.GetInfoAsValue(name, type); | 512 return base_.GetInfoAsValue(name, type); |
474 } | 513 } |
475 | 514 |
476 virtual base::TimeDelta ConnectionTimeout() const OVERRIDE { | 515 virtual base::TimeDelta ConnectionTimeout() const OVERRIDE { |
477 return base_.ConnectionTimeout(); | 516 return base_.ConnectionTimeout(); |
478 } | 517 } |
(...skipping 13 matching lines...) Expand all Loading... |
492 } | 531 } |
493 | 532 |
494 bool HasGroup(const std::string& group_name) const { | 533 bool HasGroup(const std::string& group_name) const { |
495 return base_.HasGroup(group_name); | 534 return base_.HasGroup(group_name); |
496 } | 535 } |
497 | 536 |
498 void CleanupTimedOutIdleSockets() { base_.CleanupIdleSockets(false); } | 537 void CleanupTimedOutIdleSockets() { base_.CleanupIdleSockets(false); } |
499 | 538 |
500 void EnableConnectBackupJobs() { base_.EnableConnectBackupJobs(); } | 539 void EnableConnectBackupJobs() { base_.EnableConnectBackupJobs(); } |
501 | 540 |
| 541 bool CloseOneIdleConnectionInLayeredPool() { |
| 542 return base_.CloseOneIdleConnectionInLayeredPool(); |
| 543 } |
| 544 |
502 private: | 545 private: |
503 TestClientSocketPoolBase base_; | 546 TestClientSocketPoolBase base_; |
504 | 547 |
505 DISALLOW_COPY_AND_ASSIGN(TestClientSocketPool); | 548 DISALLOW_COPY_AND_ASSIGN(TestClientSocketPool); |
506 }; | 549 }; |
507 | 550 |
508 } // namespace | 551 } // namespace |
509 | 552 |
510 REGISTER_SOCKET_PARAMS_FOR_POOL(TestClientSocketPool, TestSocketParams); | 553 REGISTER_SOCKET_PARAMS_FOR_POOL(TestClientSocketPool, TestSocketParams); |
511 | 554 |
(...skipping 645 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1157 EXPECT_EQ(1, pool_->IdleSocketCount()); | 1200 EXPECT_EQ(1, pool_->IdleSocketCount()); |
1158 } | 1201 } |
1159 | 1202 |
1160 TEST_F(ClientSocketPoolBaseTest, WaitForStalledSocketAtSocketLimit) { | 1203 TEST_F(ClientSocketPoolBaseTest, WaitForStalledSocketAtSocketLimit) { |
1161 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); | 1204 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup); |
1162 connect_job_factory_->set_job_type(TestConnectJob::kMockJob); | 1205 connect_job_factory_->set_job_type(TestConnectJob::kMockJob); |
1163 | 1206 |
1164 ClientSocketHandle stalled_handle; | 1207 ClientSocketHandle stalled_handle; |
1165 TestCompletionCallback callback; | 1208 TestCompletionCallback callback; |
1166 { | 1209 { |
| 1210 EXPECT_FALSE(pool_->IsStalled()); |
1167 ClientSocketHandle handles[kDefaultMaxSockets]; | 1211 ClientSocketHandle handles[kDefaultMaxSockets]; |
1168 for (int i = 0; i < kDefaultMaxSockets; ++i) { | 1212 for (int i = 0; i < kDefaultMaxSockets; ++i) { |
1169 TestCompletionCallback callback; | 1213 TestCompletionCallback callback; |
1170 EXPECT_EQ(OK, handles[i].Init(base::StringPrintf( | 1214 EXPECT_EQ(OK, handles[i].Init(base::StringPrintf( |
1171 "Take 2: %d", i), | 1215 "Take 2: %d", i), |
1172 params_, | 1216 params_, |
1173 kDefaultPriority, | 1217 kDefaultPriority, |
1174 callback.callback(), | 1218 callback.callback(), |
1175 pool_.get(), | 1219 pool_.get(), |
1176 BoundNetLog())); | 1220 BoundNetLog())); |
1177 } | 1221 } |
1178 | 1222 |
1179 EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); | 1223 EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count()); |
1180 EXPECT_EQ(0, pool_->IdleSocketCount()); | 1224 EXPECT_EQ(0, pool_->IdleSocketCount()); |
| 1225 EXPECT_FALSE(pool_->IsStalled()); |
1181 | 1226 |
1182 // Now we will hit the socket limit. | 1227 // Now we will hit the socket limit. |
1183 EXPECT_EQ(ERR_IO_PENDING, stalled_handle.Init("foo", | 1228 EXPECT_EQ(ERR_IO_PENDING, stalled_handle.Init("foo", |
1184 params_, | 1229 params_, |
1185 kDefaultPriority, | 1230 kDefaultPriority, |
1186 callback.callback(), | 1231 callback.callback(), |
1187 pool_.get(), | 1232 pool_.get(), |
1188 BoundNetLog())); | 1233 BoundNetLog())); |
| 1234 EXPECT_TRUE(pool_->IsStalled()); |
1189 | 1235 |
1190 // Dropping out of scope will close all handles and return them to idle. | 1236 // Dropping out of scope will close all handles and return them to idle. |
1191 } | 1237 } |
1192 | 1238 |
1193 // But if we wait for it, the released idle sockets will be closed in | 1239 // But if we wait for it, the released idle sockets will be closed in |
1194 // preference of the waiting request. | 1240 // preference of the waiting request. |
1195 EXPECT_EQ(OK, callback.WaitForResult()); | 1241 EXPECT_EQ(OK, callback.WaitForResult()); |
1196 | 1242 |
1197 EXPECT_EQ(kDefaultMaxSockets + 1, client_socket_factory_.allocation_count()); | 1243 EXPECT_EQ(kDefaultMaxSockets + 1, client_socket_factory_.allocation_count()); |
1198 EXPECT_EQ(3, pool_->IdleSocketCount()); | 1244 EXPECT_EQ(3, pool_->IdleSocketCount()); |
(...skipping 799 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1998 // job. Release the socket. Run the loop again to make sure the second | 2044 // job. Release the socket. Run the loop again to make sure the second |
1999 // socket is sitting idle and the first one is released (since ReleaseSocket() | 2045 // socket is sitting idle and the first one is released (since ReleaseSocket() |
2000 // just posts a DoReleaseSocket() task). | 2046 // just posts a DoReleaseSocket() task). |
2001 | 2047 |
2002 handle.Reset(); | 2048 handle.Reset(); |
2003 EXPECT_EQ(OK, callback2.WaitForResult()); | 2049 EXPECT_EQ(OK, callback2.WaitForResult()); |
2004 // Use the socket. | 2050 // Use the socket. |
2005 EXPECT_EQ(1, handle2.socket()->Write(NULL, 1, CompletionCallback())); | 2051 EXPECT_EQ(1, handle2.socket()->Write(NULL, 1, CompletionCallback())); |
2006 handle2.Reset(); | 2052 handle2.Reset(); |
2007 | 2053 |
2008 // The idle socket timeout value was set to 10 milliseconds. Wait 20 | 2054 // The idle socket timeout value was set to 10 milliseconds. Wait 100 |
2009 // milliseconds so the sockets timeout. | 2055 // milliseconds so the sockets timeout. |
2010 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(20)); | 2056 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(100)); |
2011 MessageLoop::current()->RunAllPending(); | 2057 MessageLoop::current()->RunAllPending(); |
2012 | 2058 |
2013 ASSERT_EQ(2, pool_->IdleSocketCount()); | 2059 ASSERT_EQ(2, pool_->IdleSocketCount()); |
2014 | 2060 |
2015 // Request a new socket. This should cleanup the unused and timed out ones. | 2061 // Request a new socket. This should cleanup the unused and timed out ones. |
2016 // A new socket will be created rather than reusing the idle one. | 2062 // A new socket will be created rather than reusing the idle one. |
2017 CapturingBoundNetLog log(CapturingNetLog::kUnbounded); | 2063 CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
2018 TestCompletionCallback callback3; | 2064 TestCompletionCallback callback3; |
2019 rv = handle.Init("a", | 2065 rv = handle.Init("a", |
2020 params_, | 2066 params_, |
(...skipping 1011 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3032 CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); | 3078 CreatePool(kDefaultMaxSockets, kDefaultMaxSockets); |
3033 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); | 3079 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
3034 | 3080 |
3035 ASSERT_FALSE(pool_->HasGroup("a")); | 3081 ASSERT_FALSE(pool_->HasGroup("a")); |
3036 | 3082 |
3037 pool_->RequestSockets("a", ¶ms_, kDefaultMaxSockets - 1, | 3083 pool_->RequestSockets("a", ¶ms_, kDefaultMaxSockets - 1, |
3038 BoundNetLog()); | 3084 BoundNetLog()); |
3039 | 3085 |
3040 ASSERT_TRUE(pool_->HasGroup("a")); | 3086 ASSERT_TRUE(pool_->HasGroup("a")); |
3041 EXPECT_EQ(kDefaultMaxSockets - 1, pool_->NumConnectJobsInGroup("a")); | 3087 EXPECT_EQ(kDefaultMaxSockets - 1, pool_->NumConnectJobsInGroup("a")); |
| 3088 EXPECT_FALSE(pool_->IsStalled()); |
3042 | 3089 |
3043 ASSERT_FALSE(pool_->HasGroup("b")); | 3090 ASSERT_FALSE(pool_->HasGroup("b")); |
3044 | 3091 |
3045 pool_->RequestSockets("b", ¶ms_, kDefaultMaxSockets, | 3092 pool_->RequestSockets("b", ¶ms_, kDefaultMaxSockets, |
3046 BoundNetLog()); | 3093 BoundNetLog()); |
3047 | 3094 |
3048 ASSERT_TRUE(pool_->HasGroup("b")); | 3095 ASSERT_TRUE(pool_->HasGroup("b")); |
3049 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("b")); | 3096 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("b")); |
| 3097 EXPECT_FALSE(pool_->IsStalled()); |
3050 } | 3098 } |
3051 | 3099 |
3052 TEST_F(ClientSocketPoolBaseTest, RequestSocketsCountIdleSockets) { | 3100 TEST_F(ClientSocketPoolBaseTest, RequestSocketsCountIdleSockets) { |
3053 CreatePool(4, 4); | 3101 CreatePool(4, 4); |
3054 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); | 3102 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob); |
3055 | 3103 |
3056 ClientSocketHandle handle1; | 3104 ClientSocketHandle handle1; |
3057 TestCompletionCallback callback1; | 3105 TestCompletionCallback callback1; |
3058 EXPECT_EQ(ERR_IO_PENDING, handle1.Init("a", | 3106 EXPECT_EQ(ERR_IO_PENDING, handle1.Init("a", |
3059 params_, | 3107 params_, |
(...skipping 298 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3358 EXPECT_EQ(0, pool_->NumActiveSocketsInGroup("a")); | 3406 EXPECT_EQ(0, pool_->NumActiveSocketsInGroup("a")); |
3359 ASSERT_EQ(OK, callback.WaitForResult()); | 3407 ASSERT_EQ(OK, callback.WaitForResult()); |
3360 | 3408 |
3361 // The hung connect job should still be there, but everything else should be | 3409 // The hung connect job should still be there, but everything else should be |
3362 // complete. | 3410 // complete. |
3363 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a")); | 3411 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a")); |
3364 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a")); | 3412 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a")); |
3365 EXPECT_EQ(1, pool_->NumActiveSocketsInGroup("a")); | 3413 EXPECT_EQ(1, pool_->NumActiveSocketsInGroup("a")); |
3366 } | 3414 } |
3367 | 3415 |
| 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 |
3368 } // namespace | 3583 } // namespace |
3369 | 3584 |
3370 } // namespace net | 3585 } // namespace net |
OLD | NEW |