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
3 // found in the LICENSE file.
5 #include "net/socket/client_socket_pool_base.h"
10 #include "base/bind_helpers.h"
11 #include "base/callback.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_vector.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/message_loop/message_loop.h"
16 #include "base/run_loop.h"
17 #include "base/strings/string_number_conversions.h"
18 #include "base/strings/stringprintf.h"
19 #include "base/threading/platform_thread.h"
20 #include "base/values.h"
21 #include "net/base/load_timing_info.h"
22 #include "net/base/load_timing_info_test_util.h"
23 #include "net/base/net_errors.h"
24 #include "net/base/net_log.h"
25 #include "net/base/net_log_unittest.h"
26 #include "net/base/request_priority.h"
27 #include "net/base/test_completion_callback.h"
28 #include "net/http/http_response_headers.h"
29 #include "net/socket/client_socket_factory.h"
30 #include "net/socket/client_socket_handle.h"
31 #include "net/socket/client_socket_pool_histograms.h"
32 #include "net/socket/socket_test_util.h"
33 #include "net/socket/ssl_client_socket.h"
34 #include "net/socket/stream_socket.h"
35 #include "net/udp/datagram_client_socket.h"
36 #include "testing/gmock/include/gmock/gmock.h"
37 #include "testing/gtest/include/gtest/gtest.h"
39 using ::testing::Invoke
;
40 using ::testing::Return
;
46 const int kDefaultMaxSockets
= 4;
47 const int kDefaultMaxSocketsPerGroup
= 2;
49 // Make sure |handle| sets load times correctly when it has been assigned a
51 void TestLoadTimingInfoConnectedReused(const ClientSocketHandle
& handle
) {
52 LoadTimingInfo load_timing_info
;
53 // Only pass true in as |is_reused|, as in general, HttpStream types should
54 // have stricter concepts of reuse than socket pools.
55 EXPECT_TRUE(handle
.GetLoadTimingInfo(true, &load_timing_info
));
57 EXPECT_EQ(true, load_timing_info
.socket_reused
);
58 EXPECT_NE(NetLog::Source::kInvalidId
, load_timing_info
.socket_log_id
);
60 ExpectConnectTimingHasNoTimes(load_timing_info
.connect_timing
);
61 ExpectLoadTimingHasOnlyConnectionTimes(load_timing_info
);
64 // Make sure |handle| sets load times correctly when it has been assigned a
65 // fresh socket. Also runs TestLoadTimingInfoConnectedReused, since the owner
66 // of a connection where |is_reused| is false may consider the connection
68 void TestLoadTimingInfoConnectedNotReused(const ClientSocketHandle
& handle
) {
69 EXPECT_FALSE(handle
.is_reused());
71 LoadTimingInfo load_timing_info
;
72 EXPECT_TRUE(handle
.GetLoadTimingInfo(false, &load_timing_info
));
74 EXPECT_FALSE(load_timing_info
.socket_reused
);
75 EXPECT_NE(NetLog::Source::kInvalidId
, load_timing_info
.socket_log_id
);
77 ExpectConnectTimingHasTimes(load_timing_info
.connect_timing
,
78 CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY
);
79 ExpectLoadTimingHasOnlyConnectionTimes(load_timing_info
);
81 TestLoadTimingInfoConnectedReused(handle
);
84 // Make sure |handle| sets load times correctly, in the case that it does not
85 // currently have a socket.
86 void TestLoadTimingInfoNotConnected(const ClientSocketHandle
& handle
) {
87 // Should only be set to true once a socket is assigned, if at all.
88 EXPECT_FALSE(handle
.is_reused());
90 LoadTimingInfo load_timing_info
;
91 EXPECT_FALSE(handle
.GetLoadTimingInfo(false, &load_timing_info
));
93 EXPECT_FALSE(load_timing_info
.socket_reused
);
94 EXPECT_EQ(NetLog::Source::kInvalidId
, load_timing_info
.socket_log_id
);
96 ExpectConnectTimingHasNoTimes(load_timing_info
.connect_timing
);
97 ExpectLoadTimingHasOnlyConnectionTimes(load_timing_info
);
100 class TestSocketParams
: public base::RefCounted
<TestSocketParams
> {
102 explicit TestSocketParams(bool ignore_limits
)
103 : ignore_limits_(ignore_limits
) {}
105 bool ignore_limits() { return ignore_limits_
; }
108 friend class base::RefCounted
<TestSocketParams
>;
109 ~TestSocketParams() {}
111 const bool ignore_limits_
;
113 typedef ClientSocketPoolBase
<TestSocketParams
> TestClientSocketPoolBase
;
115 class MockClientSocket
: public StreamSocket
{
117 explicit MockClientSocket(net::NetLog
* net_log
)
119 net_log_(BoundNetLog::Make(net_log
, net::NetLog::SOURCE_SOCKET
)),
120 was_used_to_convey_data_(false) {
123 // Socket implementation.
125 IOBuffer
* /* buf */, int len
,
126 const CompletionCallback
& /* callback */) OVERRIDE
{
127 return ERR_UNEXPECTED
;
131 IOBuffer
* /* buf */, int len
,
132 const CompletionCallback
& /* callback */) OVERRIDE
{
133 was_used_to_convey_data_
= true;
136 virtual bool SetReceiveBufferSize(int32 size
) OVERRIDE
{ return true; }
137 virtual bool SetSendBufferSize(int32 size
) OVERRIDE
{ return true; }
139 // StreamSocket implementation.
140 virtual int Connect(const CompletionCallback
& callback
) OVERRIDE
{
145 virtual void Disconnect() OVERRIDE
{ connected_
= false; }
146 virtual bool IsConnected() const OVERRIDE
{ return connected_
; }
147 virtual bool IsConnectedAndIdle() const OVERRIDE
{ return connected_
; }
149 virtual int GetPeerAddress(IPEndPoint
* /* address */) const OVERRIDE
{
150 return ERR_UNEXPECTED
;
153 virtual int GetLocalAddress(IPEndPoint
* /* address */) const OVERRIDE
{
154 return ERR_UNEXPECTED
;
157 virtual const BoundNetLog
& NetLog() const OVERRIDE
{
161 virtual void SetSubresourceSpeculation() OVERRIDE
{}
162 virtual void SetOmniboxSpeculation() OVERRIDE
{}
163 virtual bool WasEverUsed() const OVERRIDE
{
164 return was_used_to_convey_data_
;
166 virtual bool UsingTCPFastOpen() const OVERRIDE
{ return false; }
167 virtual bool WasNpnNegotiated() const OVERRIDE
{
170 virtual NextProto
GetNegotiatedProtocol() const OVERRIDE
{
171 return kProtoUnknown
;
173 virtual bool GetSSLInfo(SSLInfo
* ssl_info
) OVERRIDE
{
179 BoundNetLog net_log_
;
180 bool was_used_to_convey_data_
;
182 DISALLOW_COPY_AND_ASSIGN(MockClientSocket
);
185 class TestConnectJob
;
187 class MockClientSocketFactory
: public ClientSocketFactory
{
189 MockClientSocketFactory() : allocation_count_(0) {}
191 virtual scoped_ptr
<DatagramClientSocket
> CreateDatagramClientSocket(
192 DatagramSocket::BindType bind_type
,
193 const RandIntCallback
& rand_int_cb
,
195 const NetLog::Source
& source
) OVERRIDE
{
197 return scoped_ptr
<DatagramClientSocket
>();
200 virtual scoped_ptr
<StreamSocket
> CreateTransportClientSocket(
201 const AddressList
& addresses
,
202 NetLog
* /* net_log */,
203 const NetLog::Source
& /*source*/) OVERRIDE
{
205 return scoped_ptr
<StreamSocket
>();
208 virtual scoped_ptr
<SSLClientSocket
> CreateSSLClientSocket(
209 scoped_ptr
<ClientSocketHandle
> transport_socket
,
210 const HostPortPair
& host_and_port
,
211 const SSLConfig
& ssl_config
,
212 const SSLClientSocketContext
& context
) OVERRIDE
{
214 return scoped_ptr
<SSLClientSocket
>();
217 virtual void ClearSSLSessionCache() OVERRIDE
{
221 void WaitForSignal(TestConnectJob
* job
) { waiting_jobs_
.push_back(job
); }
225 void SignalJob(size_t job
);
227 void SetJobLoadState(size_t job
, LoadState load_state
);
229 int allocation_count() const { return allocation_count_
; }
232 int allocation_count_
;
233 std::vector
<TestConnectJob
*> waiting_jobs_
;
236 class TestConnectJob
: public ConnectJob
{
242 kMockPendingFailingJob
,
245 kMockPendingRecoverableJob
,
246 kMockAdditionalErrorStateJob
,
247 kMockPendingAdditionalErrorStateJob
,
250 // The kMockPendingJob uses a slight delay before allowing the connect
252 static const int kPendingConnectDelay
= 2;
254 TestConnectJob(JobType job_type
,
255 const std::string
& group_name
,
256 const TestClientSocketPoolBase::Request
& request
,
257 base::TimeDelta timeout_duration
,
258 ConnectJob::Delegate
* delegate
,
259 MockClientSocketFactory
* client_socket_factory
,
261 : ConnectJob(group_name
, timeout_duration
, request
.priority(), delegate
,
262 BoundNetLog::Make(net_log
, NetLog::SOURCE_CONNECT_JOB
)),
264 client_socket_factory_(client_socket_factory
),
265 load_state_(LOAD_STATE_IDLE
),
266 store_additional_error_state_(false),
267 weak_factory_(this) {
271 DoConnect(waiting_success_
, true /* async */, false /* recoverable */);
274 void set_load_state(LoadState load_state
) { load_state_
= load_state
; }
278 virtual LoadState
GetLoadState() const OVERRIDE
{ return load_state_
; }
280 virtual void GetAdditionalErrorState(ClientSocketHandle
* handle
) OVERRIDE
{
281 if (store_additional_error_state_
) {
282 // Set all of the additional error state fields in some way.
283 handle
->set_is_ssl_error(true);
284 HttpResponseInfo info
;
285 info
.headers
= new HttpResponseHeaders(std::string());
286 handle
->set_ssl_error_response_info(info
);
293 virtual int ConnectInternal() OVERRIDE
{
295 client_socket_factory_
->CreateTransportClientSocket(
296 ignored
, NULL
, net::NetLog::Source());
298 scoped_ptr
<StreamSocket
>(new MockClientSocket(net_log().net_log())));
301 return DoConnect(true /* successful */, false /* sync */,
302 false /* recoverable */);
303 case kMockFailingJob
:
304 return DoConnect(false /* error */, false /* sync */,
305 false /* recoverable */);
306 case kMockPendingJob
:
307 set_load_state(LOAD_STATE_CONNECTING
);
309 // Depending on execution timings, posting a delayed task can result
310 // in the task getting executed the at the earliest possible
311 // opportunity or only after returning once from the message loop and
312 // then a second call into the message loop. In order to make behavior
313 // more deterministic, we change the default delay to 2ms. This should
314 // always require us to wait for the second call into the message loop.
316 // N.B. The correct fix for this and similar timing problems is to
317 // abstract time for the purpose of unittests. Unfortunately, we have
318 // a lot of third-party components that directly call the various
319 // time functions, so this change would be rather invasive.
320 base::MessageLoop::current()->PostDelayedTask(
322 base::Bind(base::IgnoreResult(&TestConnectJob::DoConnect
),
323 weak_factory_
.GetWeakPtr(),
324 true /* successful */,
326 false /* recoverable */),
327 base::TimeDelta::FromMilliseconds(kPendingConnectDelay
));
328 return ERR_IO_PENDING
;
329 case kMockPendingFailingJob
:
330 set_load_state(LOAD_STATE_CONNECTING
);
331 base::MessageLoop::current()->PostDelayedTask(
333 base::Bind(base::IgnoreResult(&TestConnectJob::DoConnect
),
334 weak_factory_
.GetWeakPtr(),
337 false /* recoverable */),
338 base::TimeDelta::FromMilliseconds(2));
339 return ERR_IO_PENDING
;
340 case kMockWaitingJob
:
341 set_load_state(LOAD_STATE_CONNECTING
);
342 client_socket_factory_
->WaitForSignal(this);
343 waiting_success_
= true;
344 return ERR_IO_PENDING
;
345 case kMockRecoverableJob
:
346 return DoConnect(false /* error */, false /* sync */,
347 true /* recoverable */);
348 case kMockPendingRecoverableJob
:
349 set_load_state(LOAD_STATE_CONNECTING
);
350 base::MessageLoop::current()->PostDelayedTask(
352 base::Bind(base::IgnoreResult(&TestConnectJob::DoConnect
),
353 weak_factory_
.GetWeakPtr(),
356 true /* recoverable */),
357 base::TimeDelta::FromMilliseconds(2));
358 return ERR_IO_PENDING
;
359 case kMockAdditionalErrorStateJob
:
360 store_additional_error_state_
= true;
361 return DoConnect(false /* error */, false /* sync */,
362 false /* recoverable */);
363 case kMockPendingAdditionalErrorStateJob
:
364 set_load_state(LOAD_STATE_CONNECTING
);
365 store_additional_error_state_
= true;
366 base::MessageLoop::current()->PostDelayedTask(
368 base::Bind(base::IgnoreResult(&TestConnectJob::DoConnect
),
369 weak_factory_
.GetWeakPtr(),
372 false /* recoverable */),
373 base::TimeDelta::FromMilliseconds(2));
374 return ERR_IO_PENDING
;
377 SetSocket(scoped_ptr
<StreamSocket
>());
382 int DoConnect(bool succeed
, bool was_async
, bool recoverable
) {
385 socket()->Connect(CompletionCallback());
386 } else if (recoverable
) {
387 result
= ERR_PROXY_AUTH_REQUESTED
;
389 result
= ERR_CONNECTION_FAILED
;
390 SetSocket(scoped_ptr
<StreamSocket
>());
394 NotifyDelegateOfCompletion(result
);
398 bool waiting_success_
;
399 const JobType job_type_
;
400 MockClientSocketFactory
* const client_socket_factory_
;
401 LoadState load_state_
;
402 bool store_additional_error_state_
;
404 base::WeakPtrFactory
<TestConnectJob
> weak_factory_
;
406 DISALLOW_COPY_AND_ASSIGN(TestConnectJob
);
409 class TestConnectJobFactory
410 : public TestClientSocketPoolBase::ConnectJobFactory
{
412 TestConnectJobFactory(MockClientSocketFactory
* client_socket_factory
,
414 : job_type_(TestConnectJob::kMockJob
),
416 client_socket_factory_(client_socket_factory
),
420 virtual ~TestConnectJobFactory() {}
422 void set_job_type(TestConnectJob::JobType job_type
) { job_type_
= job_type
; }
424 void set_job_types(std::list
<TestConnectJob::JobType
>* job_types
) {
425 job_types_
= job_types
;
426 CHECK(!job_types_
->empty());
429 void set_timeout_duration(base::TimeDelta timeout_duration
) {
430 timeout_duration_
= timeout_duration
;
433 // ConnectJobFactory implementation.
435 virtual scoped_ptr
<ConnectJob
> NewConnectJob(
436 const std::string
& group_name
,
437 const TestClientSocketPoolBase::Request
& request
,
438 ConnectJob::Delegate
* delegate
) const OVERRIDE
{
439 EXPECT_TRUE(!job_types_
|| !job_types_
->empty());
440 TestConnectJob::JobType job_type
= job_type_
;
441 if (job_types_
&& !job_types_
->empty()) {
442 job_type
= job_types_
->front();
443 job_types_
->pop_front();
445 return scoped_ptr
<ConnectJob
>(new TestConnectJob(job_type
,
450 client_socket_factory_
,
454 virtual base::TimeDelta
ConnectionTimeout() const OVERRIDE
{
455 return timeout_duration_
;
459 TestConnectJob::JobType job_type_
;
460 std::list
<TestConnectJob::JobType
>* job_types_
;
461 base::TimeDelta timeout_duration_
;
462 MockClientSocketFactory
* const client_socket_factory_
;
465 DISALLOW_COPY_AND_ASSIGN(TestConnectJobFactory
);
468 class TestClientSocketPool
: public ClientSocketPool
{
470 typedef TestSocketParams SocketParams
;
472 TestClientSocketPool(
474 int max_sockets_per_group
,
475 ClientSocketPoolHistograms
* histograms
,
476 base::TimeDelta unused_idle_socket_timeout
,
477 base::TimeDelta used_idle_socket_timeout
,
478 TestClientSocketPoolBase::ConnectJobFactory
* connect_job_factory
)
479 : base_(NULL
, max_sockets
, max_sockets_per_group
, histograms
,
480 unused_idle_socket_timeout
, used_idle_socket_timeout
,
481 connect_job_factory
) {}
483 virtual ~TestClientSocketPool() {}
485 virtual int RequestSocket(
486 const std::string
& group_name
,
488 net::RequestPriority priority
,
489 ClientSocketHandle
* handle
,
490 const CompletionCallback
& callback
,
491 const BoundNetLog
& net_log
) OVERRIDE
{
492 const scoped_refptr
<TestSocketParams
>* casted_socket_params
=
493 static_cast<const scoped_refptr
<TestSocketParams
>*>(params
);
494 return base_
.RequestSocket(group_name
, *casted_socket_params
, priority
,
495 handle
, callback
, net_log
);
498 virtual void RequestSockets(const std::string
& group_name
,
501 const BoundNetLog
& net_log
) OVERRIDE
{
502 const scoped_refptr
<TestSocketParams
>* casted_params
=
503 static_cast<const scoped_refptr
<TestSocketParams
>*>(params
);
505 base_
.RequestSockets(group_name
, *casted_params
, num_sockets
, net_log
);
508 virtual void CancelRequest(
509 const std::string
& group_name
,
510 ClientSocketHandle
* handle
) OVERRIDE
{
511 base_
.CancelRequest(group_name
, handle
);
514 virtual void ReleaseSocket(
515 const std::string
& group_name
,
516 scoped_ptr
<StreamSocket
> socket
,
518 base_
.ReleaseSocket(group_name
, socket
.Pass(), id
);
521 virtual void FlushWithError(int error
) OVERRIDE
{
522 base_
.FlushWithError(error
);
525 virtual bool IsStalled() const OVERRIDE
{
526 return base_
.IsStalled();
529 virtual void CloseIdleSockets() OVERRIDE
{
530 base_
.CloseIdleSockets();
533 virtual int IdleSocketCount() const OVERRIDE
{
534 return base_
.idle_socket_count();
537 virtual int IdleSocketCountInGroup(
538 const std::string
& group_name
) const OVERRIDE
{
539 return base_
.IdleSocketCountInGroup(group_name
);
542 virtual LoadState
GetLoadState(
543 const std::string
& group_name
,
544 const ClientSocketHandle
* handle
) const OVERRIDE
{
545 return base_
.GetLoadState(group_name
, handle
);
548 virtual void AddHigherLayeredPool(HigherLayeredPool
* higher_pool
) OVERRIDE
{
549 base_
.AddHigherLayeredPool(higher_pool
);
552 virtual void RemoveHigherLayeredPool(
553 HigherLayeredPool
* higher_pool
) OVERRIDE
{
554 base_
.RemoveHigherLayeredPool(higher_pool
);
557 virtual base::DictionaryValue
* GetInfoAsValue(
558 const std::string
& name
,
559 const std::string
& type
,
560 bool include_nested_pools
) const OVERRIDE
{
561 return base_
.GetInfoAsValue(name
, type
);
564 virtual base::TimeDelta
ConnectionTimeout() const OVERRIDE
{
565 return base_
.ConnectionTimeout();
568 virtual ClientSocketPoolHistograms
* histograms() const OVERRIDE
{
569 return base_
.histograms();
572 const TestClientSocketPoolBase
* base() const { return &base_
; }
574 int NumUnassignedConnectJobsInGroup(const std::string
& group_name
) const {
575 return base_
.NumUnassignedConnectJobsInGroup(group_name
);
578 int NumConnectJobsInGroup(const std::string
& group_name
) const {
579 return base_
.NumConnectJobsInGroup(group_name
);
582 int NumActiveSocketsInGroup(const std::string
& group_name
) const {
583 return base_
.NumActiveSocketsInGroup(group_name
);
586 bool HasGroup(const std::string
& group_name
) const {
587 return base_
.HasGroup(group_name
);
590 void CleanupTimedOutIdleSockets() { base_
.CleanupIdleSockets(false); }
592 void EnableConnectBackupJobs() { base_
.EnableConnectBackupJobs(); }
594 bool CloseOneIdleConnectionInHigherLayeredPool() {
595 return base_
.CloseOneIdleConnectionInHigherLayeredPool();
599 TestClientSocketPoolBase base_
;
601 DISALLOW_COPY_AND_ASSIGN(TestClientSocketPool
);
608 void MockClientSocketFactory::SignalJobs() {
609 for (std::vector
<TestConnectJob
*>::iterator it
= waiting_jobs_
.begin();
610 it
!= waiting_jobs_
.end(); ++it
) {
613 waiting_jobs_
.clear();
616 void MockClientSocketFactory::SignalJob(size_t job
) {
617 ASSERT_LT(job
, waiting_jobs_
.size());
618 waiting_jobs_
[job
]->Signal();
619 waiting_jobs_
.erase(waiting_jobs_
.begin() + job
);
622 void MockClientSocketFactory::SetJobLoadState(size_t job
,
623 LoadState load_state
) {
624 ASSERT_LT(job
, waiting_jobs_
.size());
625 waiting_jobs_
[job
]->set_load_state(load_state
);
628 class TestConnectJobDelegate
: public ConnectJob::Delegate
{
630 TestConnectJobDelegate()
631 : have_result_(false), waiting_for_result_(false), result_(OK
) {}
632 virtual ~TestConnectJobDelegate() {}
634 virtual void OnConnectJobComplete(int result
, ConnectJob
* job
) OVERRIDE
{
636 scoped_ptr
<ConnectJob
> owned_job(job
);
637 scoped_ptr
<StreamSocket
> socket
= owned_job
->PassSocket();
638 // socket.get() should be NULL iff result != OK
639 EXPECT_EQ(socket
== NULL
, result
!= OK
);
641 if (waiting_for_result_
)
642 base::MessageLoop::current()->Quit();
645 int WaitForResult() {
646 DCHECK(!waiting_for_result_
);
647 while (!have_result_
) {
648 waiting_for_result_
= true;
649 base::MessageLoop::current()->Run();
650 waiting_for_result_
= false;
652 have_result_
= false; // auto-reset for next callback
658 bool waiting_for_result_
;
662 class ClientSocketPoolBaseTest
: public testing::Test
{
664 ClientSocketPoolBaseTest()
665 : params_(new TestSocketParams(false /* ignore_limits */)),
666 histograms_("ClientSocketPoolTest") {
667 connect_backup_jobs_enabled_
=
668 internal::ClientSocketPoolBaseHelper::connect_backup_jobs_enabled();
669 internal::ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(true);
670 cleanup_timer_enabled_
=
671 internal::ClientSocketPoolBaseHelper::cleanup_timer_enabled();
674 virtual ~ClientSocketPoolBaseTest() {
675 internal::ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(
676 connect_backup_jobs_enabled_
);
677 internal::ClientSocketPoolBaseHelper::set_cleanup_timer_enabled(
678 cleanup_timer_enabled_
);
681 void CreatePool(int max_sockets
, int max_sockets_per_group
) {
682 CreatePoolWithIdleTimeouts(
684 max_sockets_per_group
,
685 ClientSocketPool::unused_idle_socket_timeout(),
686 ClientSocketPool::used_idle_socket_timeout());
689 void CreatePoolWithIdleTimeouts(
690 int max_sockets
, int max_sockets_per_group
,
691 base::TimeDelta unused_idle_socket_timeout
,
692 base::TimeDelta used_idle_socket_timeout
) {
693 DCHECK(!pool_
.get());
694 connect_job_factory_
= new TestConnectJobFactory(&client_socket_factory_
,
696 pool_
.reset(new TestClientSocketPool(max_sockets
,
697 max_sockets_per_group
,
699 unused_idle_socket_timeout
,
700 used_idle_socket_timeout
,
701 connect_job_factory_
));
704 int StartRequestWithParams(
705 const std::string
& group_name
,
706 RequestPriority priority
,
707 const scoped_refptr
<TestSocketParams
>& params
) {
708 return test_base_
.StartRequestUsingPool(
709 pool_
.get(), group_name
, priority
, params
);
712 int StartRequest(const std::string
& group_name
, RequestPriority priority
) {
713 return StartRequestWithParams(group_name
, priority
, params_
);
716 int GetOrderOfRequest(size_t index
) const {
717 return test_base_
.GetOrderOfRequest(index
);
720 bool ReleaseOneConnection(ClientSocketPoolTest::KeepAlive keep_alive
) {
721 return test_base_
.ReleaseOneConnection(keep_alive
);
724 void ReleaseAllConnections(ClientSocketPoolTest::KeepAlive keep_alive
) {
725 test_base_
.ReleaseAllConnections(keep_alive
);
728 TestSocketRequest
* request(int i
) { return test_base_
.request(i
); }
729 size_t requests_size() const { return test_base_
.requests_size(); }
730 ScopedVector
<TestSocketRequest
>* requests() { return test_base_
.requests(); }
731 size_t completion_count() const { return test_base_
.completion_count(); }
733 CapturingNetLog net_log_
;
734 bool connect_backup_jobs_enabled_
;
735 bool cleanup_timer_enabled_
;
736 MockClientSocketFactory client_socket_factory_
;
737 TestConnectJobFactory
* connect_job_factory_
;
738 scoped_refptr
<TestSocketParams
> params_
;
739 ClientSocketPoolHistograms histograms_
;
740 scoped_ptr
<TestClientSocketPool
> pool_
;
741 ClientSocketPoolTest test_base_
;
744 // Even though a timeout is specified, it doesn't time out on a synchronous
746 TEST_F(ClientSocketPoolBaseTest
, ConnectJob_NoTimeoutOnSynchronousCompletion
) {
747 TestConnectJobDelegate delegate
;
748 ClientSocketHandle ignored
;
749 TestClientSocketPoolBase::Request
request(
750 &ignored
, CompletionCallback(), DEFAULT_PRIORITY
,
751 internal::ClientSocketPoolBaseHelper::NORMAL
,
752 false, params_
, BoundNetLog());
753 scoped_ptr
<TestConnectJob
> job(
754 new TestConnectJob(TestConnectJob::kMockJob
,
757 base::TimeDelta::FromMicroseconds(1),
759 &client_socket_factory_
,
761 EXPECT_EQ(OK
, job
->Connect());
764 TEST_F(ClientSocketPoolBaseTest
, ConnectJob_TimedOut
) {
765 TestConnectJobDelegate delegate
;
766 ClientSocketHandle ignored
;
769 TestClientSocketPoolBase::Request
request(
770 &ignored
, CompletionCallback(), DEFAULT_PRIORITY
,
771 internal::ClientSocketPoolBaseHelper::NORMAL
,
772 false, params_
, BoundNetLog());
773 // Deleted by TestConnectJobDelegate.
774 TestConnectJob
* job
=
775 new TestConnectJob(TestConnectJob::kMockPendingJob
,
778 base::TimeDelta::FromMicroseconds(1),
780 &client_socket_factory_
,
782 ASSERT_EQ(ERR_IO_PENDING
, job
->Connect());
783 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(1));
784 EXPECT_EQ(ERR_TIMED_OUT
, delegate
.WaitForResult());
786 CapturingNetLog::CapturedEntryList entries
;
787 log
.GetEntries(&entries
);
789 EXPECT_EQ(6u, entries
.size());
790 EXPECT_TRUE(LogContainsBeginEvent(
791 entries
, 0, NetLog::TYPE_SOCKET_POOL_CONNECT_JOB
));
792 EXPECT_TRUE(LogContainsBeginEvent(
793 entries
, 1, NetLog::TYPE_SOCKET_POOL_CONNECT_JOB_CONNECT
));
794 EXPECT_TRUE(LogContainsEvent(
795 entries
, 2, NetLog::TYPE_CONNECT_JOB_SET_SOCKET
,
796 NetLog::PHASE_NONE
));
797 EXPECT_TRUE(LogContainsEvent(
798 entries
, 3, NetLog::TYPE_SOCKET_POOL_CONNECT_JOB_TIMED_OUT
,
799 NetLog::PHASE_NONE
));
800 EXPECT_TRUE(LogContainsEndEvent(
801 entries
, 4, NetLog::TYPE_SOCKET_POOL_CONNECT_JOB_CONNECT
));
802 EXPECT_TRUE(LogContainsEndEvent(
803 entries
, 5, NetLog::TYPE_SOCKET_POOL_CONNECT_JOB
));
806 TEST_F(ClientSocketPoolBaseTest
, BasicSynchronous
) {
807 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
809 TestCompletionCallback callback
;
810 ClientSocketHandle handle
;
811 CapturingBoundNetLog log
;
812 TestLoadTimingInfoNotConnected(handle
);
821 EXPECT_TRUE(handle
.is_initialized());
822 EXPECT_TRUE(handle
.socket());
823 TestLoadTimingInfoConnectedNotReused(handle
);
826 TestLoadTimingInfoNotConnected(handle
);
828 CapturingNetLog::CapturedEntryList entries
;
829 log
.GetEntries(&entries
);
831 EXPECT_EQ(4u, entries
.size());
832 EXPECT_TRUE(LogContainsBeginEvent(
833 entries
, 0, NetLog::TYPE_SOCKET_POOL
));
834 EXPECT_TRUE(LogContainsEvent(
835 entries
, 1, NetLog::TYPE_SOCKET_POOL_BOUND_TO_CONNECT_JOB
,
836 NetLog::PHASE_NONE
));
837 EXPECT_TRUE(LogContainsEvent(
838 entries
, 2, NetLog::TYPE_SOCKET_POOL_BOUND_TO_SOCKET
,
839 NetLog::PHASE_NONE
));
840 EXPECT_TRUE(LogContainsEndEvent(
841 entries
, 3, NetLog::TYPE_SOCKET_POOL
));
844 TEST_F(ClientSocketPoolBaseTest
, InitConnectionFailure
) {
845 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
847 connect_job_factory_
->set_job_type(TestConnectJob::kMockFailingJob
);
848 CapturingBoundNetLog log
;
850 ClientSocketHandle handle
;
851 TestCompletionCallback callback
;
852 // Set the additional error state members to ensure that they get cleared.
853 handle
.set_is_ssl_error(true);
854 HttpResponseInfo info
;
855 info
.headers
= new HttpResponseHeaders(std::string());
856 handle
.set_ssl_error_response_info(info
);
857 EXPECT_EQ(ERR_CONNECTION_FAILED
,
864 EXPECT_FALSE(handle
.socket());
865 EXPECT_FALSE(handle
.is_ssl_error());
866 EXPECT_TRUE(handle
.ssl_error_response_info().headers
.get() == NULL
);
867 TestLoadTimingInfoNotConnected(handle
);
869 CapturingNetLog::CapturedEntryList entries
;
870 log
.GetEntries(&entries
);
872 EXPECT_EQ(3u, entries
.size());
873 EXPECT_TRUE(LogContainsBeginEvent(
874 entries
, 0, NetLog::TYPE_SOCKET_POOL
));
875 EXPECT_TRUE(LogContainsEvent(
876 entries
, 1, NetLog::TYPE_SOCKET_POOL_BOUND_TO_CONNECT_JOB
,
877 NetLog::PHASE_NONE
));
878 EXPECT_TRUE(LogContainsEndEvent(
879 entries
, 2, NetLog::TYPE_SOCKET_POOL
));
882 TEST_F(ClientSocketPoolBaseTest
, TotalLimit
) {
883 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
885 // TODO(eroman): Check that the NetLog contains this event.
887 EXPECT_EQ(OK
, StartRequest("a", DEFAULT_PRIORITY
));
888 EXPECT_EQ(OK
, StartRequest("b", DEFAULT_PRIORITY
));
889 EXPECT_EQ(OK
, StartRequest("c", DEFAULT_PRIORITY
));
890 EXPECT_EQ(OK
, StartRequest("d", DEFAULT_PRIORITY
));
892 EXPECT_EQ(static_cast<int>(requests_size()),
893 client_socket_factory_
.allocation_count());
894 EXPECT_EQ(requests_size() - kDefaultMaxSockets
, completion_count());
896 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("e", DEFAULT_PRIORITY
));
897 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("f", DEFAULT_PRIORITY
));
898 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("g", DEFAULT_PRIORITY
));
900 ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE
);
902 EXPECT_EQ(static_cast<int>(requests_size()),
903 client_socket_factory_
.allocation_count());
904 EXPECT_EQ(requests_size() - kDefaultMaxSockets
, completion_count());
906 EXPECT_EQ(1, GetOrderOfRequest(1));
907 EXPECT_EQ(2, GetOrderOfRequest(2));
908 EXPECT_EQ(3, GetOrderOfRequest(3));
909 EXPECT_EQ(4, GetOrderOfRequest(4));
910 EXPECT_EQ(5, GetOrderOfRequest(5));
911 EXPECT_EQ(6, GetOrderOfRequest(6));
912 EXPECT_EQ(7, GetOrderOfRequest(7));
914 // Make sure we test order of all requests made.
915 EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds
, GetOrderOfRequest(8));
918 TEST_F(ClientSocketPoolBaseTest
, TotalLimitReachedNewGroup
) {
919 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
921 // TODO(eroman): Check that the NetLog contains this event.
923 // Reach all limits: max total sockets, and max sockets per group.
924 EXPECT_EQ(OK
, StartRequest("a", DEFAULT_PRIORITY
));
925 EXPECT_EQ(OK
, StartRequest("a", DEFAULT_PRIORITY
));
926 EXPECT_EQ(OK
, StartRequest("b", DEFAULT_PRIORITY
));
927 EXPECT_EQ(OK
, StartRequest("b", DEFAULT_PRIORITY
));
929 EXPECT_EQ(static_cast<int>(requests_size()),
930 client_socket_factory_
.allocation_count());
931 EXPECT_EQ(requests_size() - kDefaultMaxSockets
, completion_count());
933 // Now create a new group and verify that we don't starve it.
934 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("c", DEFAULT_PRIORITY
));
936 ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE
);
938 EXPECT_EQ(static_cast<int>(requests_size()),
939 client_socket_factory_
.allocation_count());
940 EXPECT_EQ(requests_size() - kDefaultMaxSockets
, completion_count());
942 EXPECT_EQ(1, GetOrderOfRequest(1));
943 EXPECT_EQ(2, GetOrderOfRequest(2));
944 EXPECT_EQ(3, GetOrderOfRequest(3));
945 EXPECT_EQ(4, GetOrderOfRequest(4));
946 EXPECT_EQ(5, GetOrderOfRequest(5));
948 // Make sure we test order of all requests made.
949 EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds
, GetOrderOfRequest(6));
952 TEST_F(ClientSocketPoolBaseTest
, TotalLimitRespectsPriority
) {
953 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
955 EXPECT_EQ(OK
, StartRequest("b", LOWEST
));
956 EXPECT_EQ(OK
, StartRequest("a", MEDIUM
));
957 EXPECT_EQ(OK
, StartRequest("b", HIGHEST
));
958 EXPECT_EQ(OK
, StartRequest("a", LOWEST
));
960 EXPECT_EQ(static_cast<int>(requests_size()),
961 client_socket_factory_
.allocation_count());
963 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("c", LOWEST
));
964 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", MEDIUM
));
965 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("b", HIGHEST
));
967 ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE
);
969 EXPECT_EQ(requests_size() - kDefaultMaxSockets
, completion_count());
971 // First 4 requests don't have to wait, and finish in order.
972 EXPECT_EQ(1, GetOrderOfRequest(1));
973 EXPECT_EQ(2, GetOrderOfRequest(2));
974 EXPECT_EQ(3, GetOrderOfRequest(3));
975 EXPECT_EQ(4, GetOrderOfRequest(4));
977 // Request ("b", HIGHEST) has the highest priority, then ("a", MEDIUM),
978 // and then ("c", LOWEST).
979 EXPECT_EQ(7, GetOrderOfRequest(5));
980 EXPECT_EQ(6, GetOrderOfRequest(6));
981 EXPECT_EQ(5, GetOrderOfRequest(7));
983 // Make sure we test order of all requests made.
984 EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds
, GetOrderOfRequest(9));
987 TEST_F(ClientSocketPoolBaseTest
, TotalLimitRespectsGroupLimit
) {
988 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
990 EXPECT_EQ(OK
, StartRequest("a", LOWEST
));
991 EXPECT_EQ(OK
, StartRequest("a", LOW
));
992 EXPECT_EQ(OK
, StartRequest("b", HIGHEST
));
993 EXPECT_EQ(OK
, StartRequest("b", MEDIUM
));
995 EXPECT_EQ(static_cast<int>(requests_size()),
996 client_socket_factory_
.allocation_count());
998 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("c", MEDIUM
));
999 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", LOW
));
1000 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("b", HIGHEST
));
1002 ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE
);
1004 EXPECT_EQ(static_cast<int>(requests_size()),
1005 client_socket_factory_
.allocation_count());
1006 EXPECT_EQ(requests_size() - kDefaultMaxSockets
, completion_count());
1008 // First 4 requests don't have to wait, and finish in order.
1009 EXPECT_EQ(1, GetOrderOfRequest(1));
1010 EXPECT_EQ(2, GetOrderOfRequest(2));
1011 EXPECT_EQ(3, GetOrderOfRequest(3));
1012 EXPECT_EQ(4, GetOrderOfRequest(4));
1014 // Request ("b", 7) has the highest priority, but we can't make new socket for
1015 // group "b", because it has reached the per-group limit. Then we make
1016 // socket for ("c", 6), because it has higher priority than ("a", 4),
1017 // and we still can't make a socket for group "b".
1018 EXPECT_EQ(5, GetOrderOfRequest(5));
1019 EXPECT_EQ(6, GetOrderOfRequest(6));
1020 EXPECT_EQ(7, GetOrderOfRequest(7));
1022 // Make sure we test order of all requests made.
1023 EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds
, GetOrderOfRequest(8));
1026 // Make sure that we count connecting sockets against the total limit.
1027 TEST_F(ClientSocketPoolBaseTest
, TotalLimitCountsConnectingSockets
) {
1028 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
1030 EXPECT_EQ(OK
, StartRequest("a", DEFAULT_PRIORITY
));
1031 EXPECT_EQ(OK
, StartRequest("b", DEFAULT_PRIORITY
));
1032 EXPECT_EQ(OK
, StartRequest("c", DEFAULT_PRIORITY
));
1034 // Create one asynchronous request.
1035 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
1036 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("d", DEFAULT_PRIORITY
));
1038 // We post all of our delayed tasks with a 2ms delay. I.e. they don't
1039 // actually become pending until 2ms after they have been created. In order
1040 // to flush all tasks, we need to wait so that we know there are no
1041 // soon-to-be-pending tasks waiting.
1042 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10));
1043 base::MessageLoop::current()->RunUntilIdle();
1045 // The next synchronous request should wait for its turn.
1046 connect_job_factory_
->set_job_type(TestConnectJob::kMockJob
);
1047 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("e", DEFAULT_PRIORITY
));
1049 ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE
);
1051 EXPECT_EQ(static_cast<int>(requests_size()),
1052 client_socket_factory_
.allocation_count());
1054 EXPECT_EQ(1, GetOrderOfRequest(1));
1055 EXPECT_EQ(2, GetOrderOfRequest(2));
1056 EXPECT_EQ(3, GetOrderOfRequest(3));
1057 EXPECT_EQ(4, GetOrderOfRequest(4));
1058 EXPECT_EQ(5, GetOrderOfRequest(5));
1060 // Make sure we test order of all requests made.
1061 EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds
, GetOrderOfRequest(6));
1064 TEST_F(ClientSocketPoolBaseTest
, CorrectlyCountStalledGroups
) {
1065 CreatePool(kDefaultMaxSockets
, kDefaultMaxSockets
);
1066 connect_job_factory_
->set_job_type(TestConnectJob::kMockJob
);
1068 EXPECT_EQ(OK
, StartRequest("a", DEFAULT_PRIORITY
));
1069 EXPECT_EQ(OK
, StartRequest("a", DEFAULT_PRIORITY
));
1070 EXPECT_EQ(OK
, StartRequest("a", DEFAULT_PRIORITY
));
1071 EXPECT_EQ(OK
, StartRequest("a", DEFAULT_PRIORITY
));
1073 connect_job_factory_
->set_job_type(TestConnectJob::kMockWaitingJob
);
1075 EXPECT_EQ(kDefaultMaxSockets
, client_socket_factory_
.allocation_count());
1077 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("b", DEFAULT_PRIORITY
));
1078 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("c", DEFAULT_PRIORITY
));
1080 EXPECT_EQ(kDefaultMaxSockets
, client_socket_factory_
.allocation_count());
1082 EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE
));
1083 EXPECT_EQ(kDefaultMaxSockets
+ 1, client_socket_factory_
.allocation_count());
1084 EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE
));
1085 EXPECT_EQ(kDefaultMaxSockets
+ 2, client_socket_factory_
.allocation_count());
1086 EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE
));
1087 EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE
));
1088 EXPECT_EQ(kDefaultMaxSockets
+ 2, client_socket_factory_
.allocation_count());
1091 TEST_F(ClientSocketPoolBaseTest
, StallAndThenCancelAndTriggerAvailableSocket
) {
1092 CreatePool(kDefaultMaxSockets
, kDefaultMaxSockets
);
1093 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
1095 ClientSocketHandle handle
;
1096 TestCompletionCallback callback
;
1097 EXPECT_EQ(ERR_IO_PENDING
,
1101 callback
.callback(),
1105 ClientSocketHandle handles
[4];
1106 for (size_t i
= 0; i
< arraysize(handles
); ++i
) {
1107 TestCompletionCallback callback
;
1108 EXPECT_EQ(ERR_IO_PENDING
,
1109 handles
[i
].Init("b",
1112 callback
.callback(),
1117 // One will be stalled, cancel all the handles now.
1118 // This should hit the OnAvailableSocketSlot() code where we previously had
1119 // stalled groups, but no longer have any.
1120 for (size_t i
= 0; i
< arraysize(handles
); ++i
)
1124 TEST_F(ClientSocketPoolBaseTest
, CancelStalledSocketAtSocketLimit
) {
1125 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
1126 connect_job_factory_
->set_job_type(TestConnectJob::kMockJob
);
1129 ClientSocketHandle handles
[kDefaultMaxSockets
];
1130 TestCompletionCallback callbacks
[kDefaultMaxSockets
];
1131 for (int i
= 0; i
< kDefaultMaxSockets
; ++i
) {
1132 EXPECT_EQ(OK
, handles
[i
].Init(base::IntToString(i
),
1135 callbacks
[i
].callback(),
1140 // Force a stalled group.
1141 ClientSocketHandle stalled_handle
;
1142 TestCompletionCallback callback
;
1143 EXPECT_EQ(ERR_IO_PENDING
, stalled_handle
.Init("foo",
1146 callback
.callback(),
1150 // Cancel the stalled request.
1151 stalled_handle
.Reset();
1153 EXPECT_EQ(kDefaultMaxSockets
, client_socket_factory_
.allocation_count());
1154 EXPECT_EQ(0, pool_
->IdleSocketCount());
1156 // Dropping out of scope will close all handles and return them to idle.
1159 EXPECT_EQ(kDefaultMaxSockets
, client_socket_factory_
.allocation_count());
1160 EXPECT_EQ(kDefaultMaxSockets
, pool_
->IdleSocketCount());
1163 TEST_F(ClientSocketPoolBaseTest
, CancelPendingSocketAtSocketLimit
) {
1164 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
1165 connect_job_factory_
->set_job_type(TestConnectJob::kMockWaitingJob
);
1168 ClientSocketHandle handles
[kDefaultMaxSockets
];
1169 for (int i
= 0; i
< kDefaultMaxSockets
; ++i
) {
1170 TestCompletionCallback callback
;
1171 EXPECT_EQ(ERR_IO_PENDING
, handles
[i
].Init(base::IntToString(i
),
1174 callback
.callback(),
1179 // Force a stalled group.
1180 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
1181 ClientSocketHandle stalled_handle
;
1182 TestCompletionCallback callback
;
1183 EXPECT_EQ(ERR_IO_PENDING
, stalled_handle
.Init("foo",
1186 callback
.callback(),
1190 // Since it is stalled, it should have no connect jobs.
1191 EXPECT_EQ(0, pool_
->NumConnectJobsInGroup("foo"));
1192 EXPECT_EQ(0, pool_
->NumUnassignedConnectJobsInGroup("foo"));
1194 // Cancel the stalled request.
1197 // Now we should have a connect job.
1198 EXPECT_EQ(1, pool_
->NumConnectJobsInGroup("foo"));
1199 EXPECT_EQ(0, pool_
->NumUnassignedConnectJobsInGroup("foo"));
1201 // The stalled socket should connect.
1202 EXPECT_EQ(OK
, callback
.WaitForResult());
1204 EXPECT_EQ(kDefaultMaxSockets
+ 1,
1205 client_socket_factory_
.allocation_count());
1206 EXPECT_EQ(0, pool_
->IdleSocketCount());
1207 EXPECT_EQ(0, pool_
->NumConnectJobsInGroup("foo"));
1208 EXPECT_EQ(0, pool_
->NumUnassignedConnectJobsInGroup("foo"));
1210 // Dropping out of scope will close all handles and return them to idle.
1213 EXPECT_EQ(1, pool_
->IdleSocketCount());
1216 TEST_F(ClientSocketPoolBaseTest
, WaitForStalledSocketAtSocketLimit
) {
1217 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
1218 connect_job_factory_
->set_job_type(TestConnectJob::kMockJob
);
1220 ClientSocketHandle stalled_handle
;
1221 TestCompletionCallback callback
;
1223 EXPECT_FALSE(pool_
->IsStalled());
1224 ClientSocketHandle handles
[kDefaultMaxSockets
];
1225 for (int i
= 0; i
< kDefaultMaxSockets
; ++i
) {
1226 TestCompletionCallback callback
;
1227 EXPECT_EQ(OK
, handles
[i
].Init(base::StringPrintf(
1231 callback
.callback(),
1236 EXPECT_EQ(kDefaultMaxSockets
, client_socket_factory_
.allocation_count());
1237 EXPECT_EQ(0, pool_
->IdleSocketCount());
1238 EXPECT_FALSE(pool_
->IsStalled());
1240 // Now we will hit the socket limit.
1241 EXPECT_EQ(ERR_IO_PENDING
, stalled_handle
.Init("foo",
1244 callback
.callback(),
1247 EXPECT_TRUE(pool_
->IsStalled());
1249 // Dropping out of scope will close all handles and return them to idle.
1252 // But if we wait for it, the released idle sockets will be closed in
1253 // preference of the waiting request.
1254 EXPECT_EQ(OK
, callback
.WaitForResult());
1256 EXPECT_EQ(kDefaultMaxSockets
+ 1, client_socket_factory_
.allocation_count());
1257 EXPECT_EQ(3, pool_
->IdleSocketCount());
1260 // Regression test for http://crbug.com/40952.
1261 TEST_F(ClientSocketPoolBaseTest
, CloseIdleSocketAtSocketLimitDeleteGroup
) {
1262 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
1263 pool_
->EnableConnectBackupJobs();
1264 connect_job_factory_
->set_job_type(TestConnectJob::kMockJob
);
1266 for (int i
= 0; i
< kDefaultMaxSockets
; ++i
) {
1267 ClientSocketHandle handle
;
1268 TestCompletionCallback callback
;
1269 EXPECT_EQ(OK
, handle
.Init(base::IntToString(i
),
1272 callback
.callback(),
1277 // Flush all the DoReleaseSocket tasks.
1278 base::MessageLoop::current()->RunUntilIdle();
1280 // Stall a group. Set a pending job so it'll trigger a backup job if we don't
1282 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
1283 ClientSocketHandle handle
;
1284 TestCompletionCallback callback
;
1286 // "0" is special here, since it should be the first entry in the sorted map,
1287 // which is the one which we would close an idle socket for. We shouldn't
1288 // close an idle socket though, since we should reuse the idle socket.
1289 EXPECT_EQ(OK
, handle
.Init("0",
1292 callback
.callback(),
1296 EXPECT_EQ(kDefaultMaxSockets
, client_socket_factory_
.allocation_count());
1297 EXPECT_EQ(kDefaultMaxSockets
- 1, pool_
->IdleSocketCount());
1300 TEST_F(ClientSocketPoolBaseTest
, PendingRequests
) {
1301 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
1303 EXPECT_EQ(OK
, StartRequest("a", DEFAULT_PRIORITY
));
1304 EXPECT_EQ(OK
, StartRequest("a", DEFAULT_PRIORITY
));
1305 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", IDLE
));
1306 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", LOWEST
));
1307 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", MEDIUM
));
1308 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", HIGHEST
));
1309 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", LOW
));
1310 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", LOWEST
));
1312 ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE
);
1314 EXPECT_EQ(kDefaultMaxSocketsPerGroup
,
1315 client_socket_factory_
.allocation_count());
1316 EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup
,
1317 completion_count());
1319 EXPECT_EQ(1, GetOrderOfRequest(1));
1320 EXPECT_EQ(2, GetOrderOfRequest(2));
1321 EXPECT_EQ(8, GetOrderOfRequest(3));
1322 EXPECT_EQ(6, GetOrderOfRequest(4));
1323 EXPECT_EQ(4, GetOrderOfRequest(5));
1324 EXPECT_EQ(3, GetOrderOfRequest(6));
1325 EXPECT_EQ(5, GetOrderOfRequest(7));
1326 EXPECT_EQ(7, GetOrderOfRequest(8));
1328 // Make sure we test order of all requests made.
1329 EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds
, GetOrderOfRequest(9));
1332 TEST_F(ClientSocketPoolBaseTest
, PendingRequests_NoKeepAlive
) {
1333 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
1335 EXPECT_EQ(OK
, StartRequest("a", DEFAULT_PRIORITY
));
1336 EXPECT_EQ(OK
, StartRequest("a", DEFAULT_PRIORITY
));
1337 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", LOWEST
));
1338 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", MEDIUM
));
1339 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", HIGHEST
));
1340 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", LOW
));
1341 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", LOWEST
));
1343 ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE
);
1345 for (size_t i
= kDefaultMaxSocketsPerGroup
; i
< requests_size(); ++i
)
1346 EXPECT_EQ(OK
, request(i
)->WaitForResult());
1348 EXPECT_EQ(static_cast<int>(requests_size()),
1349 client_socket_factory_
.allocation_count());
1350 EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup
,
1351 completion_count());
1354 // This test will start up a RequestSocket() and then immediately Cancel() it.
1355 // The pending connect job will be cancelled and should not call back into
1356 // ClientSocketPoolBase.
1357 TEST_F(ClientSocketPoolBaseTest
, CancelRequestClearGroup
) {
1358 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
1360 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
1361 ClientSocketHandle handle
;
1362 TestCompletionCallback callback
;
1363 EXPECT_EQ(ERR_IO_PENDING
, handle
.Init("a",
1366 callback
.callback(),
1372 TEST_F(ClientSocketPoolBaseTest
, ConnectCancelConnect
) {
1373 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
1375 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
1376 ClientSocketHandle handle
;
1377 TestCompletionCallback callback
;
1379 EXPECT_EQ(ERR_IO_PENDING
, handle
.Init("a",
1382 callback
.callback(),
1388 TestCompletionCallback callback2
;
1389 EXPECT_EQ(ERR_IO_PENDING
,
1393 callback2
.callback(),
1397 EXPECT_EQ(OK
, callback2
.WaitForResult());
1398 EXPECT_FALSE(callback
.have_result());
1403 TEST_F(ClientSocketPoolBaseTest
, CancelRequest
) {
1404 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
1406 EXPECT_EQ(OK
, StartRequest("a", DEFAULT_PRIORITY
));
1407 EXPECT_EQ(OK
, StartRequest("a", DEFAULT_PRIORITY
));
1408 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", LOWEST
));
1409 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", MEDIUM
));
1410 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", HIGHEST
));
1411 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", LOW
));
1412 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", LOWEST
));
1414 // Cancel a request.
1415 size_t index_to_cancel
= kDefaultMaxSocketsPerGroup
+ 2;
1416 EXPECT_FALSE((*requests())[index_to_cancel
]->handle()->is_initialized());
1417 (*requests())[index_to_cancel
]->handle()->Reset();
1419 ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE
);
1421 EXPECT_EQ(kDefaultMaxSocketsPerGroup
,
1422 client_socket_factory_
.allocation_count());
1423 EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup
- 1,
1424 completion_count());
1426 EXPECT_EQ(1, GetOrderOfRequest(1));
1427 EXPECT_EQ(2, GetOrderOfRequest(2));
1428 EXPECT_EQ(5, GetOrderOfRequest(3));
1429 EXPECT_EQ(3, GetOrderOfRequest(4));
1430 EXPECT_EQ(ClientSocketPoolTest::kRequestNotFound
,
1431 GetOrderOfRequest(5)); // Canceled request.
1432 EXPECT_EQ(4, GetOrderOfRequest(6));
1433 EXPECT_EQ(6, GetOrderOfRequest(7));
1435 // Make sure we test order of all requests made.
1436 EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds
, GetOrderOfRequest(8));
1439 class RequestSocketCallback
: public TestCompletionCallbackBase
{
1441 RequestSocketCallback(ClientSocketHandle
* handle
,
1442 TestClientSocketPool
* pool
,
1443 TestConnectJobFactory
* test_connect_job_factory
,
1444 TestConnectJob::JobType next_job_type
)
1447 within_callback_(false),
1448 test_connect_job_factory_(test_connect_job_factory
),
1449 next_job_type_(next_job_type
),
1450 callback_(base::Bind(&RequestSocketCallback::OnComplete
,
1451 base::Unretained(this))) {
1454 virtual ~RequestSocketCallback() {}
1456 const CompletionCallback
& callback() const { return callback_
; }
1459 void OnComplete(int result
) {
1461 ASSERT_EQ(OK
, result
);
1463 if (!within_callback_
) {
1464 test_connect_job_factory_
->set_job_type(next_job_type_
);
1466 // Don't allow reuse of the socket. Disconnect it and then release it and
1467 // run through the MessageLoop once to get it completely released.
1468 handle_
->socket()->Disconnect();
1471 // TODO: Resolve conflicting intentions of stopping recursion with the
1472 // |!within_callback_| test (above) and the call to |RunUntilIdle()|
1473 // below. http://crbug.com/114130.
1474 base::MessageLoop::ScopedNestableTaskAllower
allow(
1475 base::MessageLoop::current());
1476 base::MessageLoop::current()->RunUntilIdle();
1478 within_callback_
= true;
1479 TestCompletionCallback next_job_callback
;
1480 scoped_refptr
<TestSocketParams
> params(
1481 new TestSocketParams(false /* ignore_limits */));
1482 int rv
= handle_
->Init("a",
1485 next_job_callback
.callback(),
1488 switch (next_job_type_
) {
1489 case TestConnectJob::kMockJob
:
1492 case TestConnectJob::kMockPendingJob
:
1493 EXPECT_EQ(ERR_IO_PENDING
, rv
);
1495 // For pending jobs, wait for new socket to be created. This makes
1496 // sure there are no more pending operations nor any unclosed sockets
1497 // when the test finishes.
1498 // We need to give it a little bit of time to run, so that all the
1499 // operations that happen on timers (e.g. cleanup of idle
1500 // connections) can execute.
1502 base::MessageLoop::ScopedNestableTaskAllower
allow(
1503 base::MessageLoop::current());
1504 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10));
1505 EXPECT_EQ(OK
, next_job_callback
.WaitForResult());
1509 FAIL() << "Unexpected job type: " << next_job_type_
;
1515 ClientSocketHandle
* const handle_
;
1516 TestClientSocketPool
* const pool_
;
1517 bool within_callback_
;
1518 TestConnectJobFactory
* const test_connect_job_factory_
;
1519 TestConnectJob::JobType next_job_type_
;
1520 CompletionCallback callback_
;
1523 TEST_F(ClientSocketPoolBaseTest
, RequestPendingJobTwice
) {
1524 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
1526 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
1527 ClientSocketHandle handle
;
1528 RequestSocketCallback
callback(
1529 &handle
, pool_
.get(), connect_job_factory_
,
1530 TestConnectJob::kMockPendingJob
);
1531 int rv
= handle
.Init("a",
1534 callback
.callback(),
1537 ASSERT_EQ(ERR_IO_PENDING
, rv
);
1539 EXPECT_EQ(OK
, callback
.WaitForResult());
1542 TEST_F(ClientSocketPoolBaseTest
, RequestPendingJobThenSynchronous
) {
1543 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
1545 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
1546 ClientSocketHandle handle
;
1547 RequestSocketCallback
callback(
1548 &handle
, pool_
.get(), connect_job_factory_
, TestConnectJob::kMockJob
);
1549 int rv
= handle
.Init("a",
1552 callback
.callback(),
1555 ASSERT_EQ(ERR_IO_PENDING
, rv
);
1557 EXPECT_EQ(OK
, callback
.WaitForResult());
1560 // Make sure that pending requests get serviced after active requests get
1562 TEST_F(ClientSocketPoolBaseTest
, CancelActiveRequestWithPendingRequests
) {
1563 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
1565 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
1567 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", DEFAULT_PRIORITY
));
1568 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", DEFAULT_PRIORITY
));
1569 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", DEFAULT_PRIORITY
));
1570 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", DEFAULT_PRIORITY
));
1571 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", DEFAULT_PRIORITY
));
1572 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", DEFAULT_PRIORITY
));
1573 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", DEFAULT_PRIORITY
));
1575 // Now, kDefaultMaxSocketsPerGroup requests should be active.
1576 // Let's cancel them.
1577 for (int i
= 0; i
< kDefaultMaxSocketsPerGroup
; ++i
) {
1578 ASSERT_FALSE(request(i
)->handle()->is_initialized());
1579 request(i
)->handle()->Reset();
1582 // Let's wait for the rest to complete now.
1583 for (size_t i
= kDefaultMaxSocketsPerGroup
; i
< requests_size(); ++i
) {
1584 EXPECT_EQ(OK
, request(i
)->WaitForResult());
1585 request(i
)->handle()->Reset();
1588 EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup
,
1589 completion_count());
1592 // Make sure that pending requests get serviced after active requests fail.
1593 TEST_F(ClientSocketPoolBaseTest
, FailingActiveRequestWithPendingRequests
) {
1594 const size_t kMaxSockets
= 5;
1595 CreatePool(kMaxSockets
, kDefaultMaxSocketsPerGroup
);
1597 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingFailingJob
);
1599 const size_t kNumberOfRequests
= 2 * kDefaultMaxSocketsPerGroup
+ 1;
1600 ASSERT_LE(kNumberOfRequests
, kMaxSockets
); // Otherwise the test will hang.
1602 // Queue up all the requests
1603 for (size_t i
= 0; i
< kNumberOfRequests
; ++i
)
1604 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", DEFAULT_PRIORITY
));
1606 for (size_t i
= 0; i
< kNumberOfRequests
; ++i
)
1607 EXPECT_EQ(ERR_CONNECTION_FAILED
, request(i
)->WaitForResult());
1610 TEST_F(ClientSocketPoolBaseTest
, CancelActiveRequestThenRequestSocket
) {
1611 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
1613 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
1615 ClientSocketHandle handle
;
1616 TestCompletionCallback callback
;
1617 int rv
= handle
.Init("a",
1620 callback
.callback(),
1623 EXPECT_EQ(ERR_IO_PENDING
, rv
);
1625 // Cancel the active request.
1628 rv
= handle
.Init("a",
1631 callback
.callback(),
1634 EXPECT_EQ(ERR_IO_PENDING
, rv
);
1635 EXPECT_EQ(OK
, callback
.WaitForResult());
1637 EXPECT_FALSE(handle
.is_reused());
1638 TestLoadTimingInfoConnectedNotReused(handle
);
1639 EXPECT_EQ(2, client_socket_factory_
.allocation_count());
1642 // Regression test for http://crbug.com/17985.
1643 TEST_F(ClientSocketPoolBaseTest
, GroupWithPendingRequestsIsNotEmpty
) {
1644 const int kMaxSockets
= 3;
1645 const int kMaxSocketsPerGroup
= 2;
1646 CreatePool(kMaxSockets
, kMaxSocketsPerGroup
);
1648 const RequestPriority kHighPriority
= HIGHEST
;
1650 EXPECT_EQ(OK
, StartRequest("a", DEFAULT_PRIORITY
));
1651 EXPECT_EQ(OK
, StartRequest("a", DEFAULT_PRIORITY
));
1653 // This is going to be a pending request in an otherwise empty group.
1654 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", DEFAULT_PRIORITY
));
1656 // Reach the maximum socket limit.
1657 EXPECT_EQ(OK
, StartRequest("b", DEFAULT_PRIORITY
));
1659 // Create a stalled group with high priorities.
1660 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("c", kHighPriority
));
1661 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("c", kHighPriority
));
1663 // Release the first two sockets from "a". Because this is a keepalive,
1664 // the first release will unblock the pending request for "a". The
1665 // second release will unblock a request for "c", becaue it is the next
1666 // high priority socket.
1667 EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE
));
1668 EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE
));
1670 // Closing idle sockets should not get us into trouble, but in the bug
1671 // we were hitting a CHECK here.
1672 EXPECT_EQ(0, pool_
->IdleSocketCountInGroup("a"));
1673 pool_
->CloseIdleSockets();
1675 // Run the released socket wakeups.
1676 base::MessageLoop::current()->RunUntilIdle();
1679 TEST_F(ClientSocketPoolBaseTest
, BasicAsynchronous
) {
1680 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
1682 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
1683 ClientSocketHandle handle
;
1684 TestCompletionCallback callback
;
1685 CapturingBoundNetLog log
;
1686 int rv
= handle
.Init("a",
1689 callback
.callback(),
1692 EXPECT_EQ(ERR_IO_PENDING
, rv
);
1693 EXPECT_EQ(LOAD_STATE_CONNECTING
, pool_
->GetLoadState("a", &handle
));
1694 TestLoadTimingInfoNotConnected(handle
);
1696 EXPECT_EQ(OK
, callback
.WaitForResult());
1697 EXPECT_TRUE(handle
.is_initialized());
1698 EXPECT_TRUE(handle
.socket());
1699 TestLoadTimingInfoConnectedNotReused(handle
);
1702 TestLoadTimingInfoNotConnected(handle
);
1704 CapturingNetLog::CapturedEntryList entries
;
1705 log
.GetEntries(&entries
);
1707 EXPECT_EQ(4u, entries
.size());
1708 EXPECT_TRUE(LogContainsBeginEvent(
1709 entries
, 0, NetLog::TYPE_SOCKET_POOL
));
1710 EXPECT_TRUE(LogContainsEvent(
1711 entries
, 1, NetLog::TYPE_SOCKET_POOL_BOUND_TO_CONNECT_JOB
,
1712 NetLog::PHASE_NONE
));
1713 EXPECT_TRUE(LogContainsEvent(
1714 entries
, 2, NetLog::TYPE_SOCKET_POOL_BOUND_TO_SOCKET
,
1715 NetLog::PHASE_NONE
));
1716 EXPECT_TRUE(LogContainsEndEvent(
1717 entries
, 3, NetLog::TYPE_SOCKET_POOL
));
1720 TEST_F(ClientSocketPoolBaseTest
,
1721 InitConnectionAsynchronousFailure
) {
1722 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
1724 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingFailingJob
);
1725 ClientSocketHandle handle
;
1726 TestCompletionCallback callback
;
1727 CapturingBoundNetLog log
;
1728 // Set the additional error state members to ensure that they get cleared.
1729 handle
.set_is_ssl_error(true);
1730 HttpResponseInfo info
;
1731 info
.headers
= new HttpResponseHeaders(std::string());
1732 handle
.set_ssl_error_response_info(info
);
1733 EXPECT_EQ(ERR_IO_PENDING
, handle
.Init("a",
1736 callback
.callback(),
1739 EXPECT_EQ(LOAD_STATE_CONNECTING
, pool_
->GetLoadState("a", &handle
));
1740 EXPECT_EQ(ERR_CONNECTION_FAILED
, callback
.WaitForResult());
1741 EXPECT_FALSE(handle
.is_ssl_error());
1742 EXPECT_TRUE(handle
.ssl_error_response_info().headers
.get() == NULL
);
1744 CapturingNetLog::CapturedEntryList entries
;
1745 log
.GetEntries(&entries
);
1747 EXPECT_EQ(3u, entries
.size());
1748 EXPECT_TRUE(LogContainsBeginEvent(
1749 entries
, 0, NetLog::TYPE_SOCKET_POOL
));
1750 EXPECT_TRUE(LogContainsEvent(
1751 entries
, 1, NetLog::TYPE_SOCKET_POOL_BOUND_TO_CONNECT_JOB
,
1752 NetLog::PHASE_NONE
));
1753 EXPECT_TRUE(LogContainsEndEvent(
1754 entries
, 2, NetLog::TYPE_SOCKET_POOL
));
1757 TEST_F(ClientSocketPoolBaseTest
, TwoRequestsCancelOne
) {
1758 // TODO(eroman): Add back the log expectations! Removed them because the
1759 // ordering is difficult, and some may fire during destructor.
1760 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
1762 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
1763 ClientSocketHandle handle
;
1764 TestCompletionCallback callback
;
1765 ClientSocketHandle handle2
;
1766 TestCompletionCallback callback2
;
1768 EXPECT_EQ(ERR_IO_PENDING
,
1772 callback
.callback(),
1775 CapturingBoundNetLog log2
;
1776 EXPECT_EQ(ERR_IO_PENDING
,
1780 callback2
.callback(),
1787 // At this point, request 2 is just waiting for the connect job to finish.
1789 EXPECT_EQ(OK
, callback2
.WaitForResult());
1792 // Now request 2 has actually finished.
1793 // TODO(eroman): Add back log expectations.
1796 TEST_F(ClientSocketPoolBaseTest
, CancelRequestLimitsJobs
) {
1797 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
1799 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
1801 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", LOWEST
));
1802 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", LOW
));
1803 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", MEDIUM
));
1804 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", HIGHEST
));
1806 EXPECT_EQ(kDefaultMaxSocketsPerGroup
, pool_
->NumConnectJobsInGroup("a"));
1807 (*requests())[2]->handle()->Reset();
1808 (*requests())[3]->handle()->Reset();
1809 EXPECT_EQ(kDefaultMaxSocketsPerGroup
, pool_
->NumConnectJobsInGroup("a"));
1811 (*requests())[1]->handle()->Reset();
1812 EXPECT_EQ(kDefaultMaxSocketsPerGroup
, pool_
->NumConnectJobsInGroup("a"));
1814 (*requests())[0]->handle()->Reset();
1815 EXPECT_EQ(kDefaultMaxSocketsPerGroup
, pool_
->NumConnectJobsInGroup("a"));
1818 // When requests and ConnectJobs are not coupled, the request will get serviced
1819 // by whatever comes first.
1820 TEST_F(ClientSocketPoolBaseTest
, ReleaseSockets
) {
1821 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
1823 // Start job 1 (async OK)
1824 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
1826 std::vector
<TestSocketRequest
*> request_order
;
1827 size_t completion_count
; // unused
1828 TestSocketRequest
req1(&request_order
, &completion_count
);
1829 int rv
= req1
.handle()->Init("a",
1832 req1
.callback(), pool_
.get(),
1834 EXPECT_EQ(ERR_IO_PENDING
, rv
);
1835 EXPECT_EQ(OK
, req1
.WaitForResult());
1837 // Job 1 finished OK. Start job 2 (also async OK). Request 3 is pending
1839 connect_job_factory_
->set_job_type(TestConnectJob::kMockWaitingJob
);
1841 TestSocketRequest
req2(&request_order
, &completion_count
);
1842 rv
= req2
.handle()->Init("a",
1848 EXPECT_EQ(ERR_IO_PENDING
, rv
);
1849 TestSocketRequest
req3(&request_order
, &completion_count
);
1850 rv
= req3
.handle()->Init("a",
1856 EXPECT_EQ(ERR_IO_PENDING
, rv
);
1858 // Both Requests 2 and 3 are pending. We release socket 1 which should
1859 // service request 2. Request 3 should still be waiting.
1860 req1
.handle()->Reset();
1861 // Run the released socket wakeups.
1862 base::MessageLoop::current()->RunUntilIdle();
1863 ASSERT_TRUE(req2
.handle()->socket());
1864 EXPECT_EQ(OK
, req2
.WaitForResult());
1865 EXPECT_FALSE(req3
.handle()->socket());
1867 // Signal job 2, which should service request 3.
1869 client_socket_factory_
.SignalJobs();
1870 EXPECT_EQ(OK
, req3
.WaitForResult());
1872 ASSERT_EQ(3U, request_order
.size());
1873 EXPECT_EQ(&req1
, request_order
[0]);
1874 EXPECT_EQ(&req2
, request_order
[1]);
1875 EXPECT_EQ(&req3
, request_order
[2]);
1876 EXPECT_EQ(0, pool_
->IdleSocketCountInGroup("a"));
1879 // The requests are not coupled to the jobs. So, the requests should finish in
1880 // their priority / insertion order.
1881 TEST_F(ClientSocketPoolBaseTest
, PendingJobCompletionOrder
) {
1882 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
1883 // First two jobs are async.
1884 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingFailingJob
);
1886 std::vector
<TestSocketRequest
*> request_order
;
1887 size_t completion_count
; // unused
1888 TestSocketRequest
req1(&request_order
, &completion_count
);
1889 int rv
= req1
.handle()->Init("a",
1895 EXPECT_EQ(ERR_IO_PENDING
, rv
);
1897 TestSocketRequest
req2(&request_order
, &completion_count
);
1898 rv
= req2
.handle()->Init("a",
1904 EXPECT_EQ(ERR_IO_PENDING
, rv
);
1906 // The pending job is sync.
1907 connect_job_factory_
->set_job_type(TestConnectJob::kMockJob
);
1909 TestSocketRequest
req3(&request_order
, &completion_count
);
1910 rv
= req3
.handle()->Init("a",
1916 EXPECT_EQ(ERR_IO_PENDING
, rv
);
1918 EXPECT_EQ(ERR_CONNECTION_FAILED
, req1
.WaitForResult());
1919 EXPECT_EQ(OK
, req2
.WaitForResult());
1920 EXPECT_EQ(ERR_CONNECTION_FAILED
, req3
.WaitForResult());
1922 ASSERT_EQ(3U, request_order
.size());
1923 EXPECT_EQ(&req1
, request_order
[0]);
1924 EXPECT_EQ(&req2
, request_order
[1]);
1925 EXPECT_EQ(&req3
, request_order
[2]);
1928 // Test GetLoadState in the case there's only one socket request.
1929 TEST_F(ClientSocketPoolBaseTest
, LoadStateOneRequest
) {
1930 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
1931 connect_job_factory_
->set_job_type(TestConnectJob::kMockWaitingJob
);
1933 ClientSocketHandle handle
;
1934 TestCompletionCallback callback
;
1935 int rv
= handle
.Init("a",
1938 callback
.callback(),
1941 EXPECT_EQ(ERR_IO_PENDING
, rv
);
1942 EXPECT_EQ(LOAD_STATE_CONNECTING
, handle
.GetLoadState());
1944 client_socket_factory_
.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE
);
1945 EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE
, handle
.GetLoadState());
1947 // No point in completing the connection, since ClientSocketHandles only
1948 // expect the LoadState to be checked while connecting.
1951 // Test GetLoadState in the case there are two socket requests.
1952 TEST_F(ClientSocketPoolBaseTest
, LoadStateTwoRequests
) {
1954 connect_job_factory_
->set_job_type(TestConnectJob::kMockWaitingJob
);
1956 ClientSocketHandle handle
;
1957 TestCompletionCallback callback
;
1958 int rv
= handle
.Init("a",
1961 callback
.callback(),
1964 EXPECT_EQ(ERR_IO_PENDING
, rv
);
1966 ClientSocketHandle handle2
;
1967 TestCompletionCallback callback2
;
1968 rv
= handle2
.Init("a",
1971 callback2
.callback(),
1974 EXPECT_EQ(ERR_IO_PENDING
, rv
);
1976 // If the first Job is in an earlier state than the second, the state of
1977 // the second job should be used for both handles.
1978 client_socket_factory_
.SetJobLoadState(0, LOAD_STATE_RESOLVING_HOST
);
1979 EXPECT_EQ(LOAD_STATE_CONNECTING
, handle
.GetLoadState());
1980 EXPECT_EQ(LOAD_STATE_CONNECTING
, handle2
.GetLoadState());
1982 // If the second Job is in an earlier state than the second, the state of
1983 // the first job should be used for both handles.
1984 client_socket_factory_
.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE
);
1985 // One request is farther
1986 EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE
, handle
.GetLoadState());
1987 EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE
, handle2
.GetLoadState());
1989 // Farthest along job connects and the first request gets the socket. The
1990 // second handle switches to the state of the remaining ConnectJob.
1991 client_socket_factory_
.SignalJob(0);
1992 EXPECT_EQ(OK
, callback
.WaitForResult());
1993 EXPECT_EQ(LOAD_STATE_CONNECTING
, handle2
.GetLoadState());
1996 // Test GetLoadState in the case the per-group limit is reached.
1997 TEST_F(ClientSocketPoolBaseTest
, LoadStateGroupLimit
) {
1999 connect_job_factory_
->set_job_type(TestConnectJob::kMockWaitingJob
);
2001 ClientSocketHandle handle
;
2002 TestCompletionCallback callback
;
2003 int rv
= handle
.Init("a",
2006 callback
.callback(),
2009 EXPECT_EQ(ERR_IO_PENDING
, rv
);
2010 EXPECT_EQ(LOAD_STATE_CONNECTING
, handle
.GetLoadState());
2012 // Request another socket from the same pool, buth with a higher priority.
2013 // The first request should now be stalled at the socket group limit.
2014 ClientSocketHandle handle2
;
2015 TestCompletionCallback callback2
;
2016 rv
= handle2
.Init("a",
2019 callback2
.callback(),
2022 EXPECT_EQ(ERR_IO_PENDING
, rv
);
2023 EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET
, handle
.GetLoadState());
2024 EXPECT_EQ(LOAD_STATE_CONNECTING
, handle2
.GetLoadState());
2026 // The first handle should remain stalled as the other socket goes through
2027 // the connect process.
2029 client_socket_factory_
.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE
);
2030 EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET
, handle
.GetLoadState());
2031 EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE
, handle2
.GetLoadState());
2033 client_socket_factory_
.SignalJob(0);
2034 EXPECT_EQ(OK
, callback2
.WaitForResult());
2035 EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET
, handle
.GetLoadState());
2037 // Closing the second socket should cause the stalled handle to finally get a
2039 handle2
.socket()->Disconnect();
2041 EXPECT_EQ(LOAD_STATE_CONNECTING
, handle
.GetLoadState());
2044 // Test GetLoadState in the case the per-pool limit is reached.
2045 TEST_F(ClientSocketPoolBaseTest
, LoadStatePoolLimit
) {
2047 connect_job_factory_
->set_job_type(TestConnectJob::kMockWaitingJob
);
2049 ClientSocketHandle handle
;
2050 TestCompletionCallback callback
;
2051 int rv
= handle
.Init("a",
2054 callback
.callback(),
2057 EXPECT_EQ(ERR_IO_PENDING
, rv
);
2059 // Request for socket from another pool.
2060 ClientSocketHandle handle2
;
2061 TestCompletionCallback callback2
;
2062 rv
= handle2
.Init("b",
2065 callback2
.callback(),
2068 EXPECT_EQ(ERR_IO_PENDING
, rv
);
2070 // Request another socket from the first pool. Request should stall at the
2071 // socket pool limit.
2072 ClientSocketHandle handle3
;
2073 TestCompletionCallback callback3
;
2074 rv
= handle3
.Init("a",
2077 callback2
.callback(),
2080 EXPECT_EQ(ERR_IO_PENDING
, rv
);
2082 // The third handle should remain stalled as the other sockets in its group
2083 // goes through the connect process.
2085 EXPECT_EQ(LOAD_STATE_CONNECTING
, handle
.GetLoadState());
2086 EXPECT_EQ(LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL
, handle3
.GetLoadState());
2088 client_socket_factory_
.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE
);
2089 EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE
, handle
.GetLoadState());
2090 EXPECT_EQ(LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL
, handle3
.GetLoadState());
2092 client_socket_factory_
.SignalJob(0);
2093 EXPECT_EQ(OK
, callback
.WaitForResult());
2094 EXPECT_EQ(LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL
, handle3
.GetLoadState());
2096 // Closing a socket should allow the stalled handle to finally get a new
2098 handle
.socket()->Disconnect();
2100 EXPECT_EQ(LOAD_STATE_CONNECTING
, handle3
.GetLoadState());
2103 TEST_F(ClientSocketPoolBaseTest
, Recoverable
) {
2104 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
2105 connect_job_factory_
->set_job_type(TestConnectJob::kMockRecoverableJob
);
2107 ClientSocketHandle handle
;
2108 TestCompletionCallback callback
;
2109 EXPECT_EQ(ERR_PROXY_AUTH_REQUESTED
,
2110 handle
.Init("a", params_
, DEFAULT_PRIORITY
, callback
.callback(),
2111 pool_
.get(), BoundNetLog()));
2112 EXPECT_TRUE(handle
.is_initialized());
2113 EXPECT_TRUE(handle
.socket());
2116 TEST_F(ClientSocketPoolBaseTest
, AsyncRecoverable
) {
2117 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
2119 connect_job_factory_
->set_job_type(
2120 TestConnectJob::kMockPendingRecoverableJob
);
2121 ClientSocketHandle handle
;
2122 TestCompletionCallback callback
;
2123 EXPECT_EQ(ERR_IO_PENDING
,
2127 callback
.callback(),
2130 EXPECT_EQ(LOAD_STATE_CONNECTING
, pool_
->GetLoadState("a", &handle
));
2131 EXPECT_EQ(ERR_PROXY_AUTH_REQUESTED
, callback
.WaitForResult());
2132 EXPECT_TRUE(handle
.is_initialized());
2133 EXPECT_TRUE(handle
.socket());
2136 TEST_F(ClientSocketPoolBaseTest
, AdditionalErrorStateSynchronous
) {
2137 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
2138 connect_job_factory_
->set_job_type(
2139 TestConnectJob::kMockAdditionalErrorStateJob
);
2141 ClientSocketHandle handle
;
2142 TestCompletionCallback callback
;
2143 EXPECT_EQ(ERR_CONNECTION_FAILED
,
2147 callback
.callback(),
2150 EXPECT_FALSE(handle
.is_initialized());
2151 EXPECT_FALSE(handle
.socket());
2152 EXPECT_TRUE(handle
.is_ssl_error());
2153 EXPECT_FALSE(handle
.ssl_error_response_info().headers
.get() == NULL
);
2156 TEST_F(ClientSocketPoolBaseTest
, AdditionalErrorStateAsynchronous
) {
2157 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
2159 connect_job_factory_
->set_job_type(
2160 TestConnectJob::kMockPendingAdditionalErrorStateJob
);
2161 ClientSocketHandle handle
;
2162 TestCompletionCallback callback
;
2163 EXPECT_EQ(ERR_IO_PENDING
,
2167 callback
.callback(),
2170 EXPECT_EQ(LOAD_STATE_CONNECTING
, pool_
->GetLoadState("a", &handle
));
2171 EXPECT_EQ(ERR_CONNECTION_FAILED
, callback
.WaitForResult());
2172 EXPECT_FALSE(handle
.is_initialized());
2173 EXPECT_FALSE(handle
.socket());
2174 EXPECT_TRUE(handle
.is_ssl_error());
2175 EXPECT_FALSE(handle
.ssl_error_response_info().headers
.get() == NULL
);
2178 // Make sure we can reuse sockets when the cleanup timer is disabled.
2179 TEST_F(ClientSocketPoolBaseTest
, DisableCleanupTimerReuse
) {
2180 // Disable cleanup timer.
2181 internal::ClientSocketPoolBaseHelper::set_cleanup_timer_enabled(false);
2183 CreatePoolWithIdleTimeouts(
2184 kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
,
2185 base::TimeDelta(), // Time out unused sockets immediately.
2186 base::TimeDelta::FromDays(1)); // Don't time out used sockets.
2188 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
2190 ClientSocketHandle handle
;
2191 TestCompletionCallback callback
;
2192 int rv
= handle
.Init("a",
2195 callback
.callback(),
2198 ASSERT_EQ(ERR_IO_PENDING
, rv
);
2199 EXPECT_EQ(LOAD_STATE_CONNECTING
, pool_
->GetLoadState("a", &handle
));
2200 ASSERT_EQ(OK
, callback
.WaitForResult());
2202 // Use and release the socket.
2203 EXPECT_EQ(1, handle
.socket()->Write(NULL
, 1, CompletionCallback()));
2204 TestLoadTimingInfoConnectedNotReused(handle
);
2207 // Should now have one idle socket.
2208 ASSERT_EQ(1, pool_
->IdleSocketCount());
2210 // Request a new socket. This should reuse the old socket and complete
2212 CapturingBoundNetLog log
;
2213 rv
= handle
.Init("a",
2216 CompletionCallback(),
2220 EXPECT_TRUE(handle
.is_reused());
2221 TestLoadTimingInfoConnectedReused(handle
);
2223 ASSERT_TRUE(pool_
->HasGroup("a"));
2224 EXPECT_EQ(0, pool_
->IdleSocketCountInGroup("a"));
2225 EXPECT_EQ(1, pool_
->NumActiveSocketsInGroup("a"));
2227 CapturingNetLog::CapturedEntryList entries
;
2228 log
.GetEntries(&entries
);
2229 EXPECT_TRUE(LogContainsEntryWithType(
2230 entries
, 1, NetLog::TYPE_SOCKET_POOL_REUSED_AN_EXISTING_SOCKET
));
2233 // Make sure we cleanup old unused sockets when the cleanup timer is disabled.
2234 TEST_F(ClientSocketPoolBaseTest
, DisableCleanupTimerNoReuse
) {
2235 // Disable cleanup timer.
2236 internal::ClientSocketPoolBaseHelper::set_cleanup_timer_enabled(false);
2238 CreatePoolWithIdleTimeouts(
2239 kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
,
2240 base::TimeDelta(), // Time out unused sockets immediately
2241 base::TimeDelta()); // Time out used sockets immediately
2243 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
2245 // Startup two mock pending connect jobs, which will sit in the MessageLoop.
2247 ClientSocketHandle handle
;
2248 TestCompletionCallback callback
;
2249 int rv
= handle
.Init("a",
2252 callback
.callback(),
2255 ASSERT_EQ(ERR_IO_PENDING
, rv
);
2256 EXPECT_EQ(LOAD_STATE_CONNECTING
, pool_
->GetLoadState("a", &handle
));
2258 ClientSocketHandle handle2
;
2259 TestCompletionCallback callback2
;
2260 rv
= handle2
.Init("a",
2263 callback2
.callback(),
2266 ASSERT_EQ(ERR_IO_PENDING
, rv
);
2267 EXPECT_EQ(LOAD_STATE_CONNECTING
, pool_
->GetLoadState("a", &handle2
));
2269 // Cancel one of the requests. Wait for the other, which will get the first
2270 // job. Release the socket. Run the loop again to make sure the second
2271 // socket is sitting idle and the first one is released (since ReleaseSocket()
2272 // just posts a DoReleaseSocket() task).
2275 ASSERT_EQ(OK
, callback2
.WaitForResult());
2277 EXPECT_EQ(1, handle2
.socket()->Write(NULL
, 1, CompletionCallback()));
2280 // We post all of our delayed tasks with a 2ms delay. I.e. they don't
2281 // actually become pending until 2ms after they have been created. In order
2282 // to flush all tasks, we need to wait so that we know there are no
2283 // soon-to-be-pending tasks waiting.
2284 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10));
2285 base::MessageLoop::current()->RunUntilIdle();
2287 // Both sockets should now be idle.
2288 ASSERT_EQ(2, pool_
->IdleSocketCount());
2290 // Request a new socket. This should cleanup the unused and timed out ones.
2291 // A new socket will be created rather than reusing the idle one.
2292 CapturingBoundNetLog log
;
2293 TestCompletionCallback callback3
;
2294 rv
= handle
.Init("a",
2297 callback3
.callback(),
2300 ASSERT_EQ(ERR_IO_PENDING
, rv
);
2301 ASSERT_EQ(OK
, callback3
.WaitForResult());
2302 EXPECT_FALSE(handle
.is_reused());
2304 // Make sure the idle socket is closed.
2305 ASSERT_TRUE(pool_
->HasGroup("a"));
2306 EXPECT_EQ(0, pool_
->IdleSocketCountInGroup("a"));
2307 EXPECT_EQ(1, pool_
->NumActiveSocketsInGroup("a"));
2309 CapturingNetLog::CapturedEntryList entries
;
2310 log
.GetEntries(&entries
);
2311 EXPECT_FALSE(LogContainsEntryWithType(
2312 entries
, 1, NetLog::TYPE_SOCKET_POOL_REUSED_AN_EXISTING_SOCKET
));
2315 TEST_F(ClientSocketPoolBaseTest
, CleanupTimedOutIdleSockets
) {
2316 CreatePoolWithIdleTimeouts(
2317 kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
,
2318 base::TimeDelta(), // Time out unused sockets immediately.
2319 base::TimeDelta::FromDays(1)); // Don't time out used sockets.
2321 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
2323 // Startup two mock pending connect jobs, which will sit in the MessageLoop.
2325 ClientSocketHandle handle
;
2326 TestCompletionCallback callback
;
2327 int rv
= handle
.Init("a",
2330 callback
.callback(),
2333 EXPECT_EQ(ERR_IO_PENDING
, rv
);
2334 EXPECT_EQ(LOAD_STATE_CONNECTING
, pool_
->GetLoadState("a", &handle
));
2336 ClientSocketHandle handle2
;
2337 TestCompletionCallback callback2
;
2338 rv
= handle2
.Init("a",
2341 callback2
.callback(),
2344 EXPECT_EQ(ERR_IO_PENDING
, rv
);
2345 EXPECT_EQ(LOAD_STATE_CONNECTING
, pool_
->GetLoadState("a", &handle2
));
2347 // Cancel one of the requests. Wait for the other, which will get the first
2348 // job. Release the socket. Run the loop again to make sure the second
2349 // socket is sitting idle and the first one is released (since ReleaseSocket()
2350 // just posts a DoReleaseSocket() task).
2353 EXPECT_EQ(OK
, callback2
.WaitForResult());
2355 EXPECT_EQ(1, handle2
.socket()->Write(NULL
, 1, CompletionCallback()));
2358 // We post all of our delayed tasks with a 2ms delay. I.e. they don't
2359 // actually become pending until 2ms after they have been created. In order
2360 // to flush all tasks, we need to wait so that we know there are no
2361 // soon-to-be-pending tasks waiting.
2362 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10));
2363 base::MessageLoop::current()->RunUntilIdle();
2365 ASSERT_EQ(2, pool_
->IdleSocketCount());
2367 // Invoke the idle socket cleanup check. Only one socket should be left, the
2368 // used socket. Request it to make sure that it's used.
2370 pool_
->CleanupTimedOutIdleSockets();
2371 CapturingBoundNetLog log
;
2372 rv
= handle
.Init("a",
2375 callback
.callback(),
2379 EXPECT_TRUE(handle
.is_reused());
2381 CapturingNetLog::CapturedEntryList entries
;
2382 log
.GetEntries(&entries
);
2383 EXPECT_TRUE(LogContainsEntryWithType(
2384 entries
, 1, NetLog::TYPE_SOCKET_POOL_REUSED_AN_EXISTING_SOCKET
));
2387 // Make sure that we process all pending requests even when we're stalling
2388 // because of multiple releasing disconnected sockets.
2389 TEST_F(ClientSocketPoolBaseTest
, MultipleReleasingDisconnectedSockets
) {
2390 CreatePoolWithIdleTimeouts(
2391 kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
,
2392 base::TimeDelta(), // Time out unused sockets immediately.
2393 base::TimeDelta::FromDays(1)); // Don't time out used sockets.
2395 connect_job_factory_
->set_job_type(TestConnectJob::kMockJob
);
2397 // Startup 4 connect jobs. Two of them will be pending.
2399 ClientSocketHandle handle
;
2400 TestCompletionCallback callback
;
2401 int rv
= handle
.Init("a",
2404 callback
.callback(),
2409 ClientSocketHandle handle2
;
2410 TestCompletionCallback callback2
;
2411 rv
= handle2
.Init("a",
2414 callback2
.callback(),
2419 ClientSocketHandle handle3
;
2420 TestCompletionCallback callback3
;
2421 rv
= handle3
.Init("a",
2424 callback3
.callback(),
2427 EXPECT_EQ(ERR_IO_PENDING
, rv
);
2429 ClientSocketHandle handle4
;
2430 TestCompletionCallback callback4
;
2431 rv
= handle4
.Init("a",
2434 callback4
.callback(),
2437 EXPECT_EQ(ERR_IO_PENDING
, rv
);
2439 // Release two disconnected sockets.
2441 handle
.socket()->Disconnect();
2443 handle2
.socket()->Disconnect();
2446 EXPECT_EQ(OK
, callback3
.WaitForResult());
2447 EXPECT_FALSE(handle3
.is_reused());
2448 EXPECT_EQ(OK
, callback4
.WaitForResult());
2449 EXPECT_FALSE(handle4
.is_reused());
2452 // Regression test for http://crbug.com/42267.
2453 // When DoReleaseSocket() is processed for one socket, it is blocked because the
2454 // other stalled groups all have releasing sockets, so no progress can be made.
2455 TEST_F(ClientSocketPoolBaseTest
, SocketLimitReleasingSockets
) {
2456 CreatePoolWithIdleTimeouts(
2457 4 /* socket limit */, 4 /* socket limit per group */,
2458 base::TimeDelta(), // Time out unused sockets immediately.
2459 base::TimeDelta::FromDays(1)); // Don't time out used sockets.
2461 connect_job_factory_
->set_job_type(TestConnectJob::kMockJob
);
2463 // Max out the socket limit with 2 per group.
2465 ClientSocketHandle handle_a
[4];
2466 TestCompletionCallback callback_a
[4];
2467 ClientSocketHandle handle_b
[4];
2468 TestCompletionCallback callback_b
[4];
2470 for (int i
= 0; i
< 2; ++i
) {
2471 EXPECT_EQ(OK
, handle_a
[i
].Init("a",
2474 callback_a
[i
].callback(),
2477 EXPECT_EQ(OK
, handle_b
[i
].Init("b",
2480 callback_b
[i
].callback(),
2485 // Make 4 pending requests, 2 per group.
2487 for (int i
= 2; i
< 4; ++i
) {
2488 EXPECT_EQ(ERR_IO_PENDING
,
2489 handle_a
[i
].Init("a",
2492 callback_a
[i
].callback(),
2495 EXPECT_EQ(ERR_IO_PENDING
,
2496 handle_b
[i
].Init("b",
2499 callback_b
[i
].callback(),
2504 // Release b's socket first. The order is important, because in
2505 // DoReleaseSocket(), we'll process b's released socket, and since both b and
2506 // a are stalled, but 'a' is lower lexicographically, we'll process group 'a'
2507 // first, which has a releasing socket, so it refuses to start up another
2508 // ConnectJob. So, we used to infinite loop on this.
2509 handle_b
[0].socket()->Disconnect();
2510 handle_b
[0].Reset();
2511 handle_a
[0].socket()->Disconnect();
2512 handle_a
[0].Reset();
2514 // Used to get stuck here.
2515 base::MessageLoop::current()->RunUntilIdle();
2517 handle_b
[1].socket()->Disconnect();
2518 handle_b
[1].Reset();
2519 handle_a
[1].socket()->Disconnect();
2520 handle_a
[1].Reset();
2522 for (int i
= 2; i
< 4; ++i
) {
2523 EXPECT_EQ(OK
, callback_b
[i
].WaitForResult());
2524 EXPECT_EQ(OK
, callback_a
[i
].WaitForResult());
2528 TEST_F(ClientSocketPoolBaseTest
,
2529 ReleasingDisconnectedSocketsMaintainsPriorityOrder
) {
2530 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
2532 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
2534 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", DEFAULT_PRIORITY
));
2535 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", DEFAULT_PRIORITY
));
2536 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", DEFAULT_PRIORITY
));
2537 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", DEFAULT_PRIORITY
));
2539 EXPECT_EQ(OK
, (*requests())[0]->WaitForResult());
2540 EXPECT_EQ(OK
, (*requests())[1]->WaitForResult());
2541 EXPECT_EQ(2u, completion_count());
2543 // Releases one connection.
2544 EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::NO_KEEP_ALIVE
));
2545 EXPECT_EQ(OK
, (*requests())[2]->WaitForResult());
2547 EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::NO_KEEP_ALIVE
));
2548 EXPECT_EQ(OK
, (*requests())[3]->WaitForResult());
2549 EXPECT_EQ(4u, completion_count());
2551 EXPECT_EQ(1, GetOrderOfRequest(1));
2552 EXPECT_EQ(2, GetOrderOfRequest(2));
2553 EXPECT_EQ(3, GetOrderOfRequest(3));
2554 EXPECT_EQ(4, GetOrderOfRequest(4));
2556 // Make sure we test order of all requests made.
2557 EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds
, GetOrderOfRequest(5));
2560 class TestReleasingSocketRequest
: public TestCompletionCallbackBase
{
2562 TestReleasingSocketRequest(TestClientSocketPool
* pool
,
2563 int expected_result
,
2564 bool reset_releasing_handle
)
2566 expected_result_(expected_result
),
2567 reset_releasing_handle_(reset_releasing_handle
),
2568 callback_(base::Bind(&TestReleasingSocketRequest::OnComplete
,
2569 base::Unretained(this))) {
2572 virtual ~TestReleasingSocketRequest() {}
2574 ClientSocketHandle
* handle() { return &handle_
; }
2576 const CompletionCallback
& callback() const { return callback_
; }
2579 void OnComplete(int result
) {
2581 if (reset_releasing_handle_
)
2584 scoped_refptr
<TestSocketParams
> con_params(
2585 new TestSocketParams(false /* ignore_limits */));
2586 EXPECT_EQ(expected_result_
,
2587 handle2_
.Init("a", con_params
, DEFAULT_PRIORITY
,
2588 callback2_
.callback(), pool_
, BoundNetLog()));
2591 TestClientSocketPool
* const pool_
;
2592 int expected_result_
;
2593 bool reset_releasing_handle_
;
2594 ClientSocketHandle handle_
;
2595 ClientSocketHandle handle2_
;
2596 CompletionCallback callback_
;
2597 TestCompletionCallback callback2_
;
2601 TEST_F(ClientSocketPoolBaseTest
, AdditionalErrorSocketsDontUseSlot
) {
2602 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
2604 EXPECT_EQ(OK
, StartRequest("b", DEFAULT_PRIORITY
));
2605 EXPECT_EQ(OK
, StartRequest("a", DEFAULT_PRIORITY
));
2606 EXPECT_EQ(OK
, StartRequest("b", DEFAULT_PRIORITY
));
2608 EXPECT_EQ(static_cast<int>(requests_size()),
2609 client_socket_factory_
.allocation_count());
2611 connect_job_factory_
->set_job_type(
2612 TestConnectJob::kMockPendingAdditionalErrorStateJob
);
2613 TestReleasingSocketRequest
req(pool_
.get(), OK
, false);
2614 EXPECT_EQ(ERR_IO_PENDING
,
2615 req
.handle()->Init("a", params_
, DEFAULT_PRIORITY
, req
.callback(),
2616 pool_
.get(), BoundNetLog()));
2617 // The next job should complete synchronously
2618 connect_job_factory_
->set_job_type(TestConnectJob::kMockJob
);
2620 EXPECT_EQ(ERR_CONNECTION_FAILED
, req
.WaitForResult());
2621 EXPECT_FALSE(req
.handle()->is_initialized());
2622 EXPECT_FALSE(req
.handle()->socket());
2623 EXPECT_TRUE(req
.handle()->is_ssl_error());
2624 EXPECT_FALSE(req
.handle()->ssl_error_response_info().headers
.get() == NULL
);
2627 // http://crbug.com/44724 regression test.
2628 // We start releasing the pool when we flush on network change. When that
2629 // happens, the only active references are in the ClientSocketHandles. When a
2630 // ConnectJob completes and calls back into the last ClientSocketHandle, that
2631 // callback can release the last reference and delete the pool. After the
2632 // callback finishes, we go back to the stack frame within the now-deleted pool.
2633 // Executing any code that refers to members of the now-deleted pool can cause
2635 TEST_F(ClientSocketPoolBaseTest
, CallbackThatReleasesPool
) {
2636 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
2637 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingFailingJob
);
2639 ClientSocketHandle handle
;
2640 TestCompletionCallback callback
;
2641 EXPECT_EQ(ERR_IO_PENDING
, handle
.Init("a",
2644 callback
.callback(),
2648 pool_
->FlushWithError(ERR_NETWORK_CHANGED
);
2650 // We'll call back into this now.
2651 callback
.WaitForResult();
2654 TEST_F(ClientSocketPoolBaseTest
, DoNotReuseSocketAfterFlush
) {
2655 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
2656 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
2658 ClientSocketHandle handle
;
2659 TestCompletionCallback callback
;
2660 EXPECT_EQ(ERR_IO_PENDING
, handle
.Init("a",
2663 callback
.callback(),
2666 EXPECT_EQ(OK
, callback
.WaitForResult());
2667 EXPECT_EQ(ClientSocketHandle::UNUSED
, handle
.reuse_type());
2669 pool_
->FlushWithError(ERR_NETWORK_CHANGED
);
2672 base::MessageLoop::current()->RunUntilIdle();
2674 EXPECT_EQ(ERR_IO_PENDING
, handle
.Init("a",
2677 callback
.callback(),
2680 EXPECT_EQ(OK
, callback
.WaitForResult());
2681 EXPECT_EQ(ClientSocketHandle::UNUSED
, handle
.reuse_type());
2684 class ConnectWithinCallback
: public TestCompletionCallbackBase
{
2686 ConnectWithinCallback(
2687 const std::string
& group_name
,
2688 const scoped_refptr
<TestSocketParams
>& params
,
2689 TestClientSocketPool
* pool
)
2690 : group_name_(group_name
),
2693 callback_(base::Bind(&ConnectWithinCallback::OnComplete
,
2694 base::Unretained(this))) {
2697 virtual ~ConnectWithinCallback() {}
2699 int WaitForNestedResult() {
2700 return nested_callback_
.WaitForResult();
2703 const CompletionCallback
& callback() const { return callback_
; }
2706 void OnComplete(int result
) {
2708 EXPECT_EQ(ERR_IO_PENDING
,
2709 handle_
.Init(group_name_
,
2712 nested_callback_
.callback(),
2717 const std::string group_name_
;
2718 const scoped_refptr
<TestSocketParams
> params_
;
2719 TestClientSocketPool
* const pool_
;
2720 ClientSocketHandle handle_
;
2721 CompletionCallback callback_
;
2722 TestCompletionCallback nested_callback_
;
2724 DISALLOW_COPY_AND_ASSIGN(ConnectWithinCallback
);
2727 TEST_F(ClientSocketPoolBaseTest
, AbortAllRequestsOnFlush
) {
2728 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
2730 // First job will be waiting until it gets aborted.
2731 connect_job_factory_
->set_job_type(TestConnectJob::kMockWaitingJob
);
2733 ClientSocketHandle handle
;
2734 ConnectWithinCallback
callback("a", params_
, pool_
.get());
2735 EXPECT_EQ(ERR_IO_PENDING
, handle
.Init("a",
2738 callback
.callback(),
2742 // Second job will be started during the first callback, and will
2743 // asynchronously complete with OK.
2744 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
2745 pool_
->FlushWithError(ERR_NETWORK_CHANGED
);
2746 EXPECT_EQ(ERR_NETWORK_CHANGED
, callback
.WaitForResult());
2747 EXPECT_EQ(OK
, callback
.WaitForNestedResult());
2750 // Cancel a pending socket request while we're at max sockets,
2751 // and verify that the backup socket firing doesn't cause a crash.
2752 TEST_F(ClientSocketPoolBaseTest
, BackupSocketCancelAtMaxSockets
) {
2753 // Max 4 sockets globally, max 4 sockets per group.
2754 CreatePool(kDefaultMaxSockets
, kDefaultMaxSockets
);
2755 pool_
->EnableConnectBackupJobs();
2757 // Create the first socket and set to ERR_IO_PENDING. This starts the backup
2759 connect_job_factory_
->set_job_type(TestConnectJob::kMockWaitingJob
);
2760 ClientSocketHandle handle
;
2761 TestCompletionCallback callback
;
2762 EXPECT_EQ(ERR_IO_PENDING
, handle
.Init("bar",
2765 callback
.callback(),
2769 // Start (MaxSockets - 1) connected sockets to reach max sockets.
2770 connect_job_factory_
->set_job_type(TestConnectJob::kMockJob
);
2771 ClientSocketHandle handles
[kDefaultMaxSockets
];
2772 for (int i
= 1; i
< kDefaultMaxSockets
; ++i
) {
2773 TestCompletionCallback callback
;
2774 EXPECT_EQ(OK
, handles
[i
].Init("bar",
2777 callback
.callback(),
2782 base::MessageLoop::current()->RunUntilIdle();
2784 // Cancel the pending request.
2787 // Wait for the backup timer to fire (add some slop to ensure it fires)
2788 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(
2789 ClientSocketPool::kMaxConnectRetryIntervalMs
/ 2 * 3));
2791 base::MessageLoop::current()->RunUntilIdle();
2792 EXPECT_EQ(kDefaultMaxSockets
, client_socket_factory_
.allocation_count());
2795 TEST_F(ClientSocketPoolBaseTest
, CancelBackupSocketAfterCancelingAllRequests
) {
2796 CreatePool(kDefaultMaxSockets
, kDefaultMaxSockets
);
2797 pool_
->EnableConnectBackupJobs();
2799 // Create the first socket and set to ERR_IO_PENDING. This starts the backup
2801 connect_job_factory_
->set_job_type(TestConnectJob::kMockWaitingJob
);
2802 ClientSocketHandle handle
;
2803 TestCompletionCallback callback
;
2804 EXPECT_EQ(ERR_IO_PENDING
, handle
.Init("bar",
2807 callback
.callback(),
2810 ASSERT_TRUE(pool_
->HasGroup("bar"));
2811 EXPECT_EQ(1, pool_
->NumConnectJobsInGroup("bar"));
2812 EXPECT_EQ(0, pool_
->NumUnassignedConnectJobsInGroup("bar"));
2814 // Cancel the socket request. This should cancel the backup timer. Wait for
2815 // the backup time to see if it indeed got canceled.
2817 // Wait for the backup timer to fire (add some slop to ensure it fires)
2818 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(
2819 ClientSocketPool::kMaxConnectRetryIntervalMs
/ 2 * 3));
2820 base::MessageLoop::current()->RunUntilIdle();
2821 ASSERT_TRUE(pool_
->HasGroup("bar"));
2822 EXPECT_EQ(1, pool_
->NumConnectJobsInGroup("bar"));
2825 TEST_F(ClientSocketPoolBaseTest
, CancelBackupSocketAfterFinishingAllRequests
) {
2826 CreatePool(kDefaultMaxSockets
, kDefaultMaxSockets
);
2827 pool_
->EnableConnectBackupJobs();
2829 // Create the first socket and set to ERR_IO_PENDING. This starts the backup
2831 connect_job_factory_
->set_job_type(TestConnectJob::kMockWaitingJob
);
2832 ClientSocketHandle handle
;
2833 TestCompletionCallback callback
;
2834 EXPECT_EQ(ERR_IO_PENDING
, handle
.Init("bar",
2837 callback
.callback(),
2840 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
2841 ClientSocketHandle handle2
;
2842 TestCompletionCallback callback2
;
2843 EXPECT_EQ(ERR_IO_PENDING
, handle2
.Init("bar",
2846 callback2
.callback(),
2849 ASSERT_TRUE(pool_
->HasGroup("bar"));
2850 EXPECT_EQ(2, pool_
->NumConnectJobsInGroup("bar"));
2852 // Cancel request 1 and then complete request 2. With the requests finished,
2853 // the backup timer should be cancelled.
2855 EXPECT_EQ(OK
, callback2
.WaitForResult());
2856 // Wait for the backup timer to fire (add some slop to ensure it fires)
2857 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(
2858 ClientSocketPool::kMaxConnectRetryIntervalMs
/ 2 * 3));
2859 base::MessageLoop::current()->RunUntilIdle();
2862 // Test delayed socket binding for the case where we have two connects,
2863 // and while one is waiting on a connect, the other frees up.
2864 // The socket waiting on a connect should switch immediately to the freed
2866 TEST_F(ClientSocketPoolBaseTest
, DelayedSocketBindingWaitingForConnect
) {
2867 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
2868 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
2870 ClientSocketHandle handle1
;
2871 TestCompletionCallback callback
;
2872 EXPECT_EQ(ERR_IO_PENDING
,
2876 callback
.callback(),
2879 EXPECT_EQ(OK
, callback
.WaitForResult());
2881 // No idle sockets, no pending jobs.
2882 EXPECT_EQ(0, pool_
->IdleSocketCount());
2883 EXPECT_EQ(0, pool_
->NumConnectJobsInGroup("a"));
2885 // Create a second socket to the same host, but this one will wait.
2886 connect_job_factory_
->set_job_type(TestConnectJob::kMockWaitingJob
);
2887 ClientSocketHandle handle2
;
2888 EXPECT_EQ(ERR_IO_PENDING
,
2892 callback
.callback(),
2895 // No idle sockets, and one connecting job.
2896 EXPECT_EQ(0, pool_
->IdleSocketCount());
2897 EXPECT_EQ(1, pool_
->NumConnectJobsInGroup("a"));
2899 // Return the first handle to the pool. This will initiate the delayed
2903 base::MessageLoop::current()->RunUntilIdle();
2905 // Still no idle sockets, still one pending connect job.
2906 EXPECT_EQ(0, pool_
->IdleSocketCount());
2907 EXPECT_EQ(1, pool_
->NumConnectJobsInGroup("a"));
2909 // The second socket connected, even though it was a Waiting Job.
2910 EXPECT_EQ(OK
, callback
.WaitForResult());
2912 // And we can see there is still one job waiting.
2913 EXPECT_EQ(1, pool_
->NumConnectJobsInGroup("a"));
2915 // Finally, signal the waiting Connect.
2916 client_socket_factory_
.SignalJobs();
2917 EXPECT_EQ(0, pool_
->NumConnectJobsInGroup("a"));
2919 base::MessageLoop::current()->RunUntilIdle();
2922 // Test delayed socket binding when a group is at capacity and one
2923 // of the group's sockets frees up.
2924 TEST_F(ClientSocketPoolBaseTest
, DelayedSocketBindingAtGroupCapacity
) {
2925 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
2926 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
2928 ClientSocketHandle handle1
;
2929 TestCompletionCallback callback
;
2930 EXPECT_EQ(ERR_IO_PENDING
,
2934 callback
.callback(),
2937 EXPECT_EQ(OK
, callback
.WaitForResult());
2939 // No idle sockets, no pending jobs.
2940 EXPECT_EQ(0, pool_
->IdleSocketCount());
2941 EXPECT_EQ(0, pool_
->NumConnectJobsInGroup("a"));
2943 // Create a second socket to the same host, but this one will wait.
2944 connect_job_factory_
->set_job_type(TestConnectJob::kMockWaitingJob
);
2945 ClientSocketHandle handle2
;
2946 EXPECT_EQ(ERR_IO_PENDING
,
2950 callback
.callback(),
2953 // No idle sockets, and one connecting job.
2954 EXPECT_EQ(0, pool_
->IdleSocketCount());
2955 EXPECT_EQ(1, pool_
->NumConnectJobsInGroup("a"));
2957 // Return the first handle to the pool. This will initiate the delayed
2961 base::MessageLoop::current()->RunUntilIdle();
2963 // Still no idle sockets, still one pending connect job.
2964 EXPECT_EQ(0, pool_
->IdleSocketCount());
2965 EXPECT_EQ(1, pool_
->NumConnectJobsInGroup("a"));
2967 // The second socket connected, even though it was a Waiting Job.
2968 EXPECT_EQ(OK
, callback
.WaitForResult());
2970 // And we can see there is still one job waiting.
2971 EXPECT_EQ(1, pool_
->NumConnectJobsInGroup("a"));
2973 // Finally, signal the waiting Connect.
2974 client_socket_factory_
.SignalJobs();
2975 EXPECT_EQ(0, pool_
->NumConnectJobsInGroup("a"));
2977 base::MessageLoop::current()->RunUntilIdle();
2980 // Test out the case where we have one socket connected, one
2981 // connecting, when the first socket finishes and goes idle.
2982 // Although the second connection is pending, the second request
2983 // should complete, by taking the first socket's idle socket.
2984 TEST_F(ClientSocketPoolBaseTest
, DelayedSocketBindingAtStall
) {
2985 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
2986 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
2988 ClientSocketHandle handle1
;
2989 TestCompletionCallback callback
;
2990 EXPECT_EQ(ERR_IO_PENDING
,
2994 callback
.callback(),
2997 EXPECT_EQ(OK
, callback
.WaitForResult());
2999 // No idle sockets, no pending jobs.
3000 EXPECT_EQ(0, pool_
->IdleSocketCount());
3001 EXPECT_EQ(0, pool_
->NumConnectJobsInGroup("a"));
3003 // Create a second socket to the same host, but this one will wait.
3004 connect_job_factory_
->set_job_type(TestConnectJob::kMockWaitingJob
);
3005 ClientSocketHandle handle2
;
3006 EXPECT_EQ(ERR_IO_PENDING
,
3010 callback
.callback(),
3013 // No idle sockets, and one connecting job.
3014 EXPECT_EQ(0, pool_
->IdleSocketCount());
3015 EXPECT_EQ(1, pool_
->NumConnectJobsInGroup("a"));
3017 // Return the first handle to the pool. This will initiate the delayed
3021 base::MessageLoop::current()->RunUntilIdle();
3023 // Still no idle sockets, still one pending connect job.
3024 EXPECT_EQ(0, pool_
->IdleSocketCount());
3025 EXPECT_EQ(1, pool_
->NumConnectJobsInGroup("a"));
3027 // The second socket connected, even though it was a Waiting Job.
3028 EXPECT_EQ(OK
, callback
.WaitForResult());
3030 // And we can see there is still one job waiting.
3031 EXPECT_EQ(1, pool_
->NumConnectJobsInGroup("a"));
3033 // Finally, signal the waiting Connect.
3034 client_socket_factory_
.SignalJobs();
3035 EXPECT_EQ(0, pool_
->NumConnectJobsInGroup("a"));
3037 base::MessageLoop::current()->RunUntilIdle();
3040 // Cover the case where on an available socket slot, we have one pending
3041 // request that completes synchronously, thereby making the Group empty.
3042 TEST_F(ClientSocketPoolBaseTest
, SynchronouslyProcessOnePendingRequest
) {
3043 const int kUnlimitedSockets
= 100;
3044 const int kOneSocketPerGroup
= 1;
3045 CreatePool(kUnlimitedSockets
, kOneSocketPerGroup
);
3047 // Make the first request asynchronous fail.
3048 // This will free up a socket slot later.
3049 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingFailingJob
);
3051 ClientSocketHandle handle1
;
3052 TestCompletionCallback callback1
;
3053 EXPECT_EQ(ERR_IO_PENDING
,
3057 callback1
.callback(),
3060 EXPECT_EQ(1, pool_
->NumConnectJobsInGroup("a"));
3062 // Make the second request synchronously fail. This should make the Group
3064 connect_job_factory_
->set_job_type(TestConnectJob::kMockFailingJob
);
3065 ClientSocketHandle handle2
;
3066 TestCompletionCallback callback2
;
3067 // It'll be ERR_IO_PENDING now, but the TestConnectJob will synchronously fail
3069 EXPECT_EQ(ERR_IO_PENDING
,
3073 callback2
.callback(),
3077 EXPECT_EQ(1, pool_
->NumConnectJobsInGroup("a"));
3079 EXPECT_EQ(ERR_CONNECTION_FAILED
, callback1
.WaitForResult());
3080 EXPECT_EQ(ERR_CONNECTION_FAILED
, callback2
.WaitForResult());
3081 EXPECT_FALSE(pool_
->HasGroup("a"));
3084 TEST_F(ClientSocketPoolBaseTest
, PreferUsedSocketToUnusedSocket
) {
3085 CreatePool(kDefaultMaxSockets
, kDefaultMaxSockets
);
3087 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
3089 ClientSocketHandle handle1
;
3090 TestCompletionCallback callback1
;
3091 EXPECT_EQ(ERR_IO_PENDING
, handle1
.Init("a",
3094 callback1
.callback(),
3098 ClientSocketHandle handle2
;
3099 TestCompletionCallback callback2
;
3100 EXPECT_EQ(ERR_IO_PENDING
, handle2
.Init("a",
3103 callback2
.callback(),
3106 ClientSocketHandle handle3
;
3107 TestCompletionCallback callback3
;
3108 EXPECT_EQ(ERR_IO_PENDING
, handle3
.Init("a",
3111 callback3
.callback(),
3115 EXPECT_EQ(OK
, callback1
.WaitForResult());
3116 EXPECT_EQ(OK
, callback2
.WaitForResult());
3117 EXPECT_EQ(OK
, callback3
.WaitForResult());
3120 EXPECT_EQ(1, handle1
.socket()->Write(NULL
, 1, CompletionCallback()));
3121 EXPECT_EQ(1, handle3
.socket()->Write(NULL
, 1, CompletionCallback()));
3127 EXPECT_EQ(OK
, handle1
.Init("a",
3130 callback1
.callback(),
3133 EXPECT_EQ(OK
, handle2
.Init("a",
3136 callback2
.callback(),
3139 EXPECT_EQ(OK
, handle3
.Init("a",
3142 callback3
.callback(),
3146 EXPECT_TRUE(handle1
.socket()->WasEverUsed());
3147 EXPECT_TRUE(handle2
.socket()->WasEverUsed());
3148 EXPECT_FALSE(handle3
.socket()->WasEverUsed());
3151 TEST_F(ClientSocketPoolBaseTest
, RequestSockets
) {
3152 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
3153 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
3155 pool_
->RequestSockets("a", ¶ms_
, 2, BoundNetLog());
3157 ASSERT_TRUE(pool_
->HasGroup("a"));
3158 EXPECT_EQ(2, pool_
->NumConnectJobsInGroup("a"));
3159 EXPECT_EQ(2, pool_
->NumUnassignedConnectJobsInGroup("a"));
3160 EXPECT_EQ(0, pool_
->IdleSocketCountInGroup("a"));
3162 ClientSocketHandle handle1
;
3163 TestCompletionCallback callback1
;
3164 EXPECT_EQ(ERR_IO_PENDING
, handle1
.Init("a",
3167 callback1
.callback(),
3171 ClientSocketHandle handle2
;
3172 TestCompletionCallback callback2
;
3173 EXPECT_EQ(ERR_IO_PENDING
, handle2
.Init("a",
3176 callback2
.callback(),
3180 EXPECT_EQ(2, pool_
->NumConnectJobsInGroup("a"));
3181 EXPECT_EQ(0, pool_
->NumUnassignedConnectJobsInGroup("a"));
3182 EXPECT_EQ(0, pool_
->IdleSocketCountInGroup("a"));
3184 EXPECT_EQ(OK
, callback1
.WaitForResult());
3185 EXPECT_EQ(OK
, callback2
.WaitForResult());
3189 EXPECT_EQ(0, pool_
->NumConnectJobsInGroup("a"));
3190 EXPECT_EQ(0, pool_
->NumUnassignedConnectJobsInGroup("a"));
3191 EXPECT_EQ(2, pool_
->IdleSocketCountInGroup("a"));
3194 TEST_F(ClientSocketPoolBaseTest
, RequestSocketsWhenAlreadyHaveAConnectJob
) {
3195 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
3196 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
3198 ClientSocketHandle handle1
;
3199 TestCompletionCallback callback1
;
3200 EXPECT_EQ(ERR_IO_PENDING
, handle1
.Init("a",
3203 callback1
.callback(),
3207 ASSERT_TRUE(pool_
->HasGroup("a"));
3208 EXPECT_EQ(1, pool_
->NumConnectJobsInGroup("a"));
3209 EXPECT_EQ(0, pool_
->NumUnassignedConnectJobsInGroup("a"));
3210 EXPECT_EQ(0, pool_
->IdleSocketCountInGroup("a"));
3212 pool_
->RequestSockets("a", ¶ms_
, 2, BoundNetLog());
3214 EXPECT_EQ(2, pool_
->NumConnectJobsInGroup("a"));
3215 EXPECT_EQ(1, pool_
->NumUnassignedConnectJobsInGroup("a"));
3216 EXPECT_EQ(0, pool_
->IdleSocketCountInGroup("a"));
3218 ClientSocketHandle handle2
;
3219 TestCompletionCallback callback2
;
3220 EXPECT_EQ(ERR_IO_PENDING
, handle2
.Init("a",
3223 callback2
.callback(),
3227 EXPECT_EQ(2, pool_
->NumConnectJobsInGroup("a"));
3228 EXPECT_EQ(0, pool_
->NumUnassignedConnectJobsInGroup("a"));
3229 EXPECT_EQ(0, pool_
->IdleSocketCountInGroup("a"));
3231 EXPECT_EQ(OK
, callback1
.WaitForResult());
3232 EXPECT_EQ(OK
, callback2
.WaitForResult());
3236 EXPECT_EQ(0, pool_
->NumConnectJobsInGroup("a"));
3237 EXPECT_EQ(0, pool_
->NumUnassignedConnectJobsInGroup("a"));
3238 EXPECT_EQ(2, pool_
->IdleSocketCountInGroup("a"));
3241 TEST_F(ClientSocketPoolBaseTest
,
3242 RequestSocketsWhenAlreadyHaveMultipleConnectJob
) {
3244 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
3246 ClientSocketHandle handle1
;
3247 TestCompletionCallback callback1
;
3248 EXPECT_EQ(ERR_IO_PENDING
, handle1
.Init("a",
3251 callback1
.callback(),
3255 ClientSocketHandle handle2
;
3256 TestCompletionCallback callback2
;
3257 EXPECT_EQ(ERR_IO_PENDING
, handle2
.Init("a",
3260 callback2
.callback(),
3264 ClientSocketHandle handle3
;
3265 TestCompletionCallback callback3
;
3266 EXPECT_EQ(ERR_IO_PENDING
, handle3
.Init("a",
3269 callback3
.callback(),
3273 ASSERT_TRUE(pool_
->HasGroup("a"));
3274 EXPECT_EQ(3, pool_
->NumConnectJobsInGroup("a"));
3275 EXPECT_EQ(0, pool_
->NumUnassignedConnectJobsInGroup("a"));
3276 EXPECT_EQ(0, pool_
->IdleSocketCountInGroup("a"));
3278 pool_
->RequestSockets("a", ¶ms_
, 2, BoundNetLog());
3280 EXPECT_EQ(3, pool_
->NumConnectJobsInGroup("a"));
3281 EXPECT_EQ(0, pool_
->NumUnassignedConnectJobsInGroup("a"));
3282 EXPECT_EQ(0, pool_
->IdleSocketCountInGroup("a"));
3284 EXPECT_EQ(OK
, callback1
.WaitForResult());
3285 EXPECT_EQ(OK
, callback2
.WaitForResult());
3286 EXPECT_EQ(OK
, callback3
.WaitForResult());
3291 EXPECT_EQ(0, pool_
->NumConnectJobsInGroup("a"));
3292 EXPECT_EQ(0, pool_
->NumUnassignedConnectJobsInGroup("a"));
3293 EXPECT_EQ(3, pool_
->IdleSocketCountInGroup("a"));
3296 TEST_F(ClientSocketPoolBaseTest
, RequestSocketsAtMaxSocketLimit
) {
3297 CreatePool(kDefaultMaxSockets
, kDefaultMaxSockets
);
3298 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
3300 ASSERT_FALSE(pool_
->HasGroup("a"));
3302 pool_
->RequestSockets("a", ¶ms_
, kDefaultMaxSockets
,
3305 ASSERT_TRUE(pool_
->HasGroup("a"));
3306 EXPECT_EQ(kDefaultMaxSockets
, pool_
->NumConnectJobsInGroup("a"));
3307 EXPECT_EQ(kDefaultMaxSockets
, pool_
->NumUnassignedConnectJobsInGroup("a"));
3309 ASSERT_FALSE(pool_
->HasGroup("b"));
3311 pool_
->RequestSockets("b", ¶ms_
, kDefaultMaxSockets
,
3314 ASSERT_FALSE(pool_
->HasGroup("b"));
3317 TEST_F(ClientSocketPoolBaseTest
, RequestSocketsHitMaxSocketLimit
) {
3318 CreatePool(kDefaultMaxSockets
, kDefaultMaxSockets
);
3319 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
3321 ASSERT_FALSE(pool_
->HasGroup("a"));
3323 pool_
->RequestSockets("a", ¶ms_
, kDefaultMaxSockets
- 1,
3326 ASSERT_TRUE(pool_
->HasGroup("a"));
3327 EXPECT_EQ(kDefaultMaxSockets
- 1, pool_
->NumConnectJobsInGroup("a"));
3328 EXPECT_EQ(kDefaultMaxSockets
- 1,
3329 pool_
->NumUnassignedConnectJobsInGroup("a"));
3330 EXPECT_FALSE(pool_
->IsStalled());
3332 ASSERT_FALSE(pool_
->HasGroup("b"));
3334 pool_
->RequestSockets("b", ¶ms_
, kDefaultMaxSockets
,
3337 ASSERT_TRUE(pool_
->HasGroup("b"));
3338 EXPECT_EQ(1, pool_
->NumConnectJobsInGroup("b"));
3339 EXPECT_FALSE(pool_
->IsStalled());
3342 TEST_F(ClientSocketPoolBaseTest
, RequestSocketsCountIdleSockets
) {
3344 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
3346 ClientSocketHandle handle1
;
3347 TestCompletionCallback callback1
;
3348 EXPECT_EQ(ERR_IO_PENDING
, handle1
.Init("a",
3351 callback1
.callback(),
3354 ASSERT_EQ(OK
, callback1
.WaitForResult());
3357 ASSERT_TRUE(pool_
->HasGroup("a"));
3358 EXPECT_EQ(0, pool_
->NumConnectJobsInGroup("a"));
3359 EXPECT_EQ(0, pool_
->NumUnassignedConnectJobsInGroup("a"));
3360 EXPECT_EQ(1, pool_
->IdleSocketCountInGroup("a"));
3362 pool_
->RequestSockets("a", ¶ms_
, 2, BoundNetLog());
3364 EXPECT_EQ(1, pool_
->NumConnectJobsInGroup("a"));
3365 EXPECT_EQ(1, pool_
->NumUnassignedConnectJobsInGroup("a"));
3366 EXPECT_EQ(1, pool_
->IdleSocketCountInGroup("a"));
3369 TEST_F(ClientSocketPoolBaseTest
, RequestSocketsCountActiveSockets
) {
3371 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
3373 ClientSocketHandle handle1
;
3374 TestCompletionCallback callback1
;
3375 EXPECT_EQ(ERR_IO_PENDING
, handle1
.Init("a",
3378 callback1
.callback(),
3381 ASSERT_EQ(OK
, callback1
.WaitForResult());
3383 ASSERT_TRUE(pool_
->HasGroup("a"));
3384 EXPECT_EQ(0, pool_
->NumConnectJobsInGroup("a"));
3385 EXPECT_EQ(0, pool_
->NumUnassignedConnectJobsInGroup("a"));
3386 EXPECT_EQ(0, pool_
->IdleSocketCountInGroup("a"));
3387 EXPECT_EQ(1, pool_
->NumActiveSocketsInGroup("a"));
3389 pool_
->RequestSockets("a", ¶ms_
, 2, BoundNetLog());
3391 EXPECT_EQ(1, pool_
->NumConnectJobsInGroup("a"));
3392 EXPECT_EQ(1, pool_
->NumUnassignedConnectJobsInGroup("a"));
3393 EXPECT_EQ(0, pool_
->IdleSocketCountInGroup("a"));
3394 EXPECT_EQ(1, pool_
->NumActiveSocketsInGroup("a"));
3397 TEST_F(ClientSocketPoolBaseTest
, RequestSocketsSynchronous
) {
3398 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
3399 connect_job_factory_
->set_job_type(TestConnectJob::kMockJob
);
3401 pool_
->RequestSockets("a", ¶ms_
, kDefaultMaxSocketsPerGroup
,
3404 ASSERT_TRUE(pool_
->HasGroup("a"));
3405 EXPECT_EQ(0, pool_
->NumConnectJobsInGroup("a"));
3406 EXPECT_EQ(0, pool_
->NumUnassignedConnectJobsInGroup("a"));
3407 EXPECT_EQ(kDefaultMaxSocketsPerGroup
, pool_
->IdleSocketCountInGroup("a"));
3409 pool_
->RequestSockets("b", ¶ms_
, kDefaultMaxSocketsPerGroup
,
3412 EXPECT_EQ(0, pool_
->NumConnectJobsInGroup("b"));
3413 EXPECT_EQ(0, pool_
->NumUnassignedConnectJobsInGroup("b"));
3414 EXPECT_EQ(kDefaultMaxSocketsPerGroup
, pool_
->IdleSocketCountInGroup("b"));
3417 TEST_F(ClientSocketPoolBaseTest
, RequestSocketsSynchronousError
) {
3418 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
3419 connect_job_factory_
->set_job_type(TestConnectJob::kMockFailingJob
);
3421 pool_
->RequestSockets("a", ¶ms_
, kDefaultMaxSocketsPerGroup
,
3424 ASSERT_FALSE(pool_
->HasGroup("a"));
3426 connect_job_factory_
->set_job_type(
3427 TestConnectJob::kMockAdditionalErrorStateJob
);
3428 pool_
->RequestSockets("a", ¶ms_
, kDefaultMaxSocketsPerGroup
,
3431 ASSERT_FALSE(pool_
->HasGroup("a"));
3434 TEST_F(ClientSocketPoolBaseTest
, RequestSocketsMultipleTimesDoesNothing
) {
3436 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
3438 pool_
->RequestSockets("a", ¶ms_
, 2, BoundNetLog());
3440 ASSERT_TRUE(pool_
->HasGroup("a"));
3441 EXPECT_EQ(2, pool_
->NumConnectJobsInGroup("a"));
3442 EXPECT_EQ(2, pool_
->NumUnassignedConnectJobsInGroup("a"));
3443 EXPECT_EQ(0, pool_
->IdleSocketCountInGroup("a"));
3445 pool_
->RequestSockets("a", ¶ms_
, 2, BoundNetLog());
3446 EXPECT_EQ(2, pool_
->NumConnectJobsInGroup("a"));
3447 EXPECT_EQ(2, pool_
->NumUnassignedConnectJobsInGroup("a"));
3448 EXPECT_EQ(0, pool_
->IdleSocketCountInGroup("a"));
3450 ClientSocketHandle handle1
;
3451 TestCompletionCallback callback1
;
3452 EXPECT_EQ(ERR_IO_PENDING
, handle1
.Init("a",
3455 callback1
.callback(),
3458 ASSERT_EQ(OK
, callback1
.WaitForResult());
3460 ClientSocketHandle handle2
;
3461 TestCompletionCallback callback2
;
3462 int rv
= handle2
.Init("a",
3465 callback2
.callback(),
3469 EXPECT_EQ(ERR_IO_PENDING
, rv
);
3470 EXPECT_EQ(OK
, callback2
.WaitForResult());
3473 EXPECT_EQ(0, pool_
->NumConnectJobsInGroup("a"));
3474 EXPECT_EQ(0, pool_
->NumUnassignedConnectJobsInGroup("a"));
3475 EXPECT_EQ(2, pool_
->NumActiveSocketsInGroup("a"));
3476 EXPECT_EQ(0, pool_
->IdleSocketCountInGroup("a"));
3481 EXPECT_EQ(0, pool_
->NumConnectJobsInGroup("a"));
3482 EXPECT_EQ(0, pool_
->NumUnassignedConnectJobsInGroup("a"));
3483 EXPECT_EQ(2, pool_
->IdleSocketCountInGroup("a"));
3485 pool_
->RequestSockets("a", ¶ms_
, 2, BoundNetLog());
3486 EXPECT_EQ(0, pool_
->NumConnectJobsInGroup("a"));
3487 EXPECT_EQ(0, pool_
->NumUnassignedConnectJobsInGroup("a"));
3488 EXPECT_EQ(2, pool_
->IdleSocketCountInGroup("a"));
3491 TEST_F(ClientSocketPoolBaseTest
, RequestSocketsDifferentNumSockets
) {
3493 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
3495 pool_
->RequestSockets("a", ¶ms_
, 1, BoundNetLog());
3497 ASSERT_TRUE(pool_
->HasGroup("a"));
3498 EXPECT_EQ(1, pool_
->NumConnectJobsInGroup("a"));
3499 EXPECT_EQ(1, pool_
->NumUnassignedConnectJobsInGroup("a"));
3500 EXPECT_EQ(0, pool_
->IdleSocketCountInGroup("a"));
3502 pool_
->RequestSockets("a", ¶ms_
, 2, BoundNetLog());
3503 EXPECT_EQ(2, pool_
->NumConnectJobsInGroup("a"));
3504 EXPECT_EQ(2, pool_
->NumUnassignedConnectJobsInGroup("a"));
3505 EXPECT_EQ(0, pool_
->IdleSocketCountInGroup("a"));
3507 pool_
->RequestSockets("a", ¶ms_
, 3, BoundNetLog());
3508 EXPECT_EQ(3, pool_
->NumConnectJobsInGroup("a"));
3509 EXPECT_EQ(3, pool_
->NumUnassignedConnectJobsInGroup("a"));
3510 EXPECT_EQ(0, pool_
->IdleSocketCountInGroup("a"));
3512 pool_
->RequestSockets("a", ¶ms_
, 1, BoundNetLog());
3513 EXPECT_EQ(3, pool_
->NumConnectJobsInGroup("a"));
3514 EXPECT_EQ(3, pool_
->NumUnassignedConnectJobsInGroup("a"));
3515 EXPECT_EQ(0, pool_
->IdleSocketCountInGroup("a"));
3518 TEST_F(ClientSocketPoolBaseTest
, PreconnectJobsTakenByNormalRequests
) {
3519 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
3520 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
3522 pool_
->RequestSockets("a", ¶ms_
, 1, BoundNetLog());
3524 ASSERT_TRUE(pool_
->HasGroup("a"));
3525 EXPECT_EQ(1, pool_
->NumConnectJobsInGroup("a"));
3526 EXPECT_EQ(1, pool_
->NumUnassignedConnectJobsInGroup("a"));
3527 EXPECT_EQ(0, pool_
->IdleSocketCountInGroup("a"));
3529 ClientSocketHandle handle1
;
3530 TestCompletionCallback callback1
;
3531 EXPECT_EQ(ERR_IO_PENDING
, handle1
.Init("a",
3534 callback1
.callback(),
3538 EXPECT_EQ(1, pool_
->NumConnectJobsInGroup("a"));
3539 EXPECT_EQ(0, pool_
->NumUnassignedConnectJobsInGroup("a"));
3540 EXPECT_EQ(0, pool_
->IdleSocketCountInGroup("a"));
3542 ASSERT_EQ(OK
, callback1
.WaitForResult());
3544 // Make sure if a preconneced socket is not fully connected when a request
3545 // starts, it has a connect start time.
3546 TestLoadTimingInfoConnectedNotReused(handle1
);
3549 EXPECT_EQ(1, pool_
->IdleSocketCountInGroup("a"));
3552 // Checks that fully connected preconnect jobs have no connect times, and are
3553 // marked as reused.
3554 TEST_F(ClientSocketPoolBaseTest
, ConnectedPreconnectJobsHaveNoConnectTimes
) {
3555 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
3556 connect_job_factory_
->set_job_type(TestConnectJob::kMockJob
);
3557 pool_
->RequestSockets("a", ¶ms_
, 1, BoundNetLog());
3559 ASSERT_TRUE(pool_
->HasGroup("a"));
3560 EXPECT_EQ(0, pool_
->NumConnectJobsInGroup("a"));
3561 EXPECT_EQ(0, pool_
->NumUnassignedConnectJobsInGroup("a"));
3562 EXPECT_EQ(1, pool_
->IdleSocketCountInGroup("a"));
3564 ClientSocketHandle handle
;
3565 TestCompletionCallback callback
;
3566 EXPECT_EQ(OK
, handle
.Init("a",
3569 callback
.callback(),
3573 // Make sure the idle socket was used.
3574 EXPECT_EQ(0, pool_
->IdleSocketCountInGroup("a"));
3576 TestLoadTimingInfoConnectedReused(handle
);
3578 TestLoadTimingInfoNotConnected(handle
);
3581 // http://crbug.com/64940 regression test.
3582 TEST_F(ClientSocketPoolBaseTest
, PreconnectClosesIdleSocketRemovesGroup
) {
3583 const int kMaxTotalSockets
= 3;
3584 const int kMaxSocketsPerGroup
= 2;
3585 CreatePool(kMaxTotalSockets
, kMaxSocketsPerGroup
);
3586 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
3588 // Note that group name ordering matters here. "a" comes before "b", so
3589 // CloseOneIdleSocket() will try to close "a"'s idle socket.
3591 // Set up one idle socket in "a".
3592 ClientSocketHandle handle1
;
3593 TestCompletionCallback callback1
;
3594 EXPECT_EQ(ERR_IO_PENDING
, handle1
.Init("a",
3597 callback1
.callback(),
3601 ASSERT_EQ(OK
, callback1
.WaitForResult());
3603 EXPECT_EQ(1, pool_
->IdleSocketCountInGroup("a"));
3605 // Set up two active sockets in "b".
3606 ClientSocketHandle handle2
;
3607 TestCompletionCallback callback2
;
3608 EXPECT_EQ(ERR_IO_PENDING
, handle1
.Init("b",
3611 callback1
.callback(),
3614 EXPECT_EQ(ERR_IO_PENDING
, handle2
.Init("b",
3617 callback2
.callback(),
3621 ASSERT_EQ(OK
, callback1
.WaitForResult());
3622 ASSERT_EQ(OK
, callback2
.WaitForResult());
3623 EXPECT_EQ(0, pool_
->IdleSocketCountInGroup("b"));
3624 EXPECT_EQ(0, pool_
->NumUnassignedConnectJobsInGroup("b"));
3625 EXPECT_EQ(2, pool_
->NumActiveSocketsInGroup("b"));
3627 // Now we have 1 idle socket in "a" and 2 active sockets in "b". This means
3628 // we've maxed out on sockets, since we set |kMaxTotalSockets| to 3.
3629 // Requesting 2 preconnected sockets for "a" should fail to allocate any more
3630 // sockets for "a", and "b" should still have 2 active sockets.
3632 pool_
->RequestSockets("a", ¶ms_
, 2, BoundNetLog());
3633 EXPECT_EQ(0, pool_
->NumConnectJobsInGroup("a"));
3634 EXPECT_EQ(0, pool_
->NumUnassignedConnectJobsInGroup("a"));
3635 EXPECT_EQ(1, pool_
->IdleSocketCountInGroup("a"));
3636 EXPECT_EQ(0, pool_
->NumActiveSocketsInGroup("a"));
3637 EXPECT_EQ(0, pool_
->NumConnectJobsInGroup("b"));
3638 EXPECT_EQ(0, pool_
->NumUnassignedConnectJobsInGroup("b"));
3639 EXPECT_EQ(0, pool_
->IdleSocketCountInGroup("b"));
3640 EXPECT_EQ(2, pool_
->NumActiveSocketsInGroup("b"));
3642 // Now release the 2 active sockets for "b". This will give us 1 idle socket
3643 // in "a" and 2 idle sockets in "b". Requesting 2 preconnected sockets for
3644 // "a" should result in closing 1 for "b".
3647 EXPECT_EQ(2, pool_
->IdleSocketCountInGroup("b"));
3648 EXPECT_EQ(0, pool_
->NumActiveSocketsInGroup("b"));
3650 pool_
->RequestSockets("a", ¶ms_
, 2, BoundNetLog());
3651 EXPECT_EQ(1, pool_
->NumConnectJobsInGroup("a"));
3652 EXPECT_EQ(1, pool_
->NumUnassignedConnectJobsInGroup("a"));
3653 EXPECT_EQ(1, pool_
->IdleSocketCountInGroup("a"));
3654 EXPECT_EQ(0, pool_
->NumActiveSocketsInGroup("a"));
3655 EXPECT_EQ(0, pool_
->NumConnectJobsInGroup("b"));
3656 EXPECT_EQ(0, pool_
->NumUnassignedConnectJobsInGroup("b"));
3657 EXPECT_EQ(1, pool_
->IdleSocketCountInGroup("b"));
3658 EXPECT_EQ(0, pool_
->NumActiveSocketsInGroup("b"));
3661 TEST_F(ClientSocketPoolBaseTest
, PreconnectWithoutBackupJob
) {
3662 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
3663 pool_
->EnableConnectBackupJobs();
3665 // Make the ConnectJob hang until it times out, shorten the timeout.
3666 connect_job_factory_
->set_job_type(TestConnectJob::kMockWaitingJob
);
3667 connect_job_factory_
->set_timeout_duration(
3668 base::TimeDelta::FromMilliseconds(500));
3669 pool_
->RequestSockets("a", ¶ms_
, 1, BoundNetLog());
3670 EXPECT_EQ(1, pool_
->NumConnectJobsInGroup("a"));
3671 EXPECT_EQ(1, pool_
->NumUnassignedConnectJobsInGroup("a"));
3672 EXPECT_EQ(0, pool_
->IdleSocketCountInGroup("a"));
3674 // Verify the backup timer doesn't create a backup job, by making
3675 // the backup job a pending job instead of a waiting job, so it
3676 // *would* complete if it were created.
3677 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
3678 base::MessageLoop::current()->PostDelayedTask(
3680 base::MessageLoop::QuitClosure(),
3681 base::TimeDelta::FromSeconds(1));
3682 base::MessageLoop::current()->Run();
3683 EXPECT_FALSE(pool_
->HasGroup("a"));
3686 TEST_F(ClientSocketPoolBaseTest
, PreconnectWithBackupJob
) {
3687 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
3688 pool_
->EnableConnectBackupJobs();
3690 // Make the ConnectJob hang forever.
3691 connect_job_factory_
->set_job_type(TestConnectJob::kMockWaitingJob
);
3692 pool_
->RequestSockets("a", ¶ms_
, 1, BoundNetLog());
3693 EXPECT_EQ(1, pool_
->NumConnectJobsInGroup("a"));
3694 EXPECT_EQ(1, pool_
->NumUnassignedConnectJobsInGroup("a"));
3695 EXPECT_EQ(0, pool_
->IdleSocketCountInGroup("a"));
3696 base::MessageLoop::current()->RunUntilIdle();
3698 // Make the backup job be a pending job, so it completes normally.
3699 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
3700 ClientSocketHandle handle
;
3701 TestCompletionCallback callback
;
3702 EXPECT_EQ(ERR_IO_PENDING
, handle
.Init("a",
3705 callback
.callback(),
3708 // Timer has started, but the backup connect job shouldn't be created yet.
3709 EXPECT_EQ(1, pool_
->NumConnectJobsInGroup("a"));
3710 EXPECT_EQ(0, pool_
->NumUnassignedConnectJobsInGroup("a"));
3711 EXPECT_EQ(0, pool_
->IdleSocketCountInGroup("a"));
3712 EXPECT_EQ(0, pool_
->NumActiveSocketsInGroup("a"));
3713 ASSERT_EQ(OK
, callback
.WaitForResult());
3715 // The hung connect job should still be there, but everything else should be
3717 EXPECT_EQ(1, pool_
->NumConnectJobsInGroup("a"));
3718 EXPECT_EQ(0, pool_
->NumUnassignedConnectJobsInGroup("a"));
3719 EXPECT_EQ(0, pool_
->IdleSocketCountInGroup("a"));
3720 EXPECT_EQ(1, pool_
->NumActiveSocketsInGroup("a"));
3723 class MockLayeredPool
: public HigherLayeredPool
{
3725 MockLayeredPool(TestClientSocketPool
* pool
,
3726 const std::string
& group_name
)
3728 group_name_(group_name
),
3729 can_release_connection_(true) {
3730 pool_
->AddHigherLayeredPool(this);
3733 ~MockLayeredPool() {
3734 pool_
->RemoveHigherLayeredPool(this);
3737 int RequestSocket(TestClientSocketPool
* pool
) {
3738 scoped_refptr
<TestSocketParams
> params(
3739 new TestSocketParams(false /* ignore_limits */));
3740 return handle_
.Init(group_name_
, params
, DEFAULT_PRIORITY
,
3741 callback_
.callback(), pool
, BoundNetLog());
3744 int RequestSocketWithoutLimits(TestClientSocketPool
* pool
) {
3745 scoped_refptr
<TestSocketParams
> params(
3746 new TestSocketParams(true /* ignore_limits */));
3747 return handle_
.Init(group_name_
, params
, MAXIMUM_PRIORITY
,
3748 callback_
.callback(), pool
, BoundNetLog());
3751 bool ReleaseOneConnection() {
3752 if (!handle_
.is_initialized() || !can_release_connection_
) {
3755 handle_
.socket()->Disconnect();
3760 void set_can_release_connection(bool can_release_connection
) {
3761 can_release_connection_
= can_release_connection
;
3764 MOCK_METHOD0(CloseOneIdleConnection
, bool());
3767 TestClientSocketPool
* const pool_
;
3768 ClientSocketHandle handle_
;
3769 TestCompletionCallback callback_
;
3770 const std::string group_name_
;
3771 bool can_release_connection_
;
3774 TEST_F(ClientSocketPoolBaseTest
, FailToCloseIdleSocketsNotHeldByLayeredPool
) {
3775 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
3776 connect_job_factory_
->set_job_type(TestConnectJob::kMockJob
);
3778 MockLayeredPool
mock_layered_pool(pool_
.get(), "foo");
3779 EXPECT_EQ(OK
, mock_layered_pool
.RequestSocket(pool_
.get()));
3780 EXPECT_CALL(mock_layered_pool
, CloseOneIdleConnection())
3781 .WillOnce(Return(false));
3782 EXPECT_FALSE(pool_
->CloseOneIdleConnectionInHigherLayeredPool());
3785 TEST_F(ClientSocketPoolBaseTest
, ForciblyCloseIdleSocketsHeldByLayeredPool
) {
3786 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
3787 connect_job_factory_
->set_job_type(TestConnectJob::kMockJob
);
3789 MockLayeredPool
mock_layered_pool(pool_
.get(), "foo");
3790 EXPECT_EQ(OK
, mock_layered_pool
.RequestSocket(pool_
.get()));
3791 EXPECT_CALL(mock_layered_pool
, CloseOneIdleConnection())
3792 .WillOnce(Invoke(&mock_layered_pool
,
3793 &MockLayeredPool::ReleaseOneConnection
));
3794 EXPECT_TRUE(pool_
->CloseOneIdleConnectionInHigherLayeredPool());
3797 // Tests the basic case of closing an idle socket in a higher layered pool when
3798 // a new request is issued and the lower layer pool is stalled.
3799 TEST_F(ClientSocketPoolBaseTest
, CloseIdleSocketsHeldByLayeredPoolWhenNeeded
) {
3801 connect_job_factory_
->set_job_type(TestConnectJob::kMockJob
);
3803 MockLayeredPool
mock_layered_pool(pool_
.get(), "foo");
3804 EXPECT_EQ(OK
, mock_layered_pool
.RequestSocket(pool_
.get()));
3805 EXPECT_CALL(mock_layered_pool
, CloseOneIdleConnection())
3806 .WillOnce(Invoke(&mock_layered_pool
,
3807 &MockLayeredPool::ReleaseOneConnection
));
3808 ClientSocketHandle handle
;
3809 TestCompletionCallback callback
;
3810 EXPECT_EQ(ERR_IO_PENDING
, handle
.Init("a",
3813 callback
.callback(),
3816 EXPECT_EQ(OK
, callback
.WaitForResult());
3819 // Same as above, but the idle socket is in the same group as the stalled
3820 // socket, and closes the only other request in its group when closing requests
3821 // in higher layered pools. This generally shouldn't happen, but it may be
3822 // possible if a higher level pool issues a request and the request is
3823 // subsequently cancelled. Even if it's not possible, best not to crash.
3824 TEST_F(ClientSocketPoolBaseTest
,
3825 CloseIdleSocketsHeldByLayeredPoolWhenNeededSameGroup
) {
3827 connect_job_factory_
->set_job_type(TestConnectJob::kMockJob
);
3829 // Need a socket in another group for the pool to be stalled (If a group
3830 // has the maximum number of connections already, it's not stalled).
3831 ClientSocketHandle handle1
;
3832 TestCompletionCallback callback1
;
3833 EXPECT_EQ(OK
, handle1
.Init("group1",
3836 callback1
.callback(),
3840 MockLayeredPool
mock_layered_pool(pool_
.get(), "group2");
3841 EXPECT_EQ(OK
, mock_layered_pool
.RequestSocket(pool_
.get()));
3842 EXPECT_CALL(mock_layered_pool
, CloseOneIdleConnection())
3843 .WillOnce(Invoke(&mock_layered_pool
,
3844 &MockLayeredPool::ReleaseOneConnection
));
3845 ClientSocketHandle handle
;
3846 TestCompletionCallback callback2
;
3847 EXPECT_EQ(ERR_IO_PENDING
, handle
.Init("group2",
3850 callback2
.callback(),
3853 EXPECT_EQ(OK
, callback2
.WaitForResult());
3856 // Tests the case when an idle socket can be closed when a new request is
3857 // issued, and the new request belongs to a group that was previously stalled.
3858 TEST_F(ClientSocketPoolBaseTest
,
3859 CloseIdleSocketsHeldByLayeredPoolInSameGroupWhenNeeded
) {
3861 std::list
<TestConnectJob::JobType
> job_types
;
3862 job_types
.push_back(TestConnectJob::kMockJob
);
3863 job_types
.push_back(TestConnectJob::kMockJob
);
3864 job_types
.push_back(TestConnectJob::kMockJob
);
3865 job_types
.push_back(TestConnectJob::kMockJob
);
3866 connect_job_factory_
->set_job_types(&job_types
);
3868 ClientSocketHandle handle1
;
3869 TestCompletionCallback callback1
;
3870 EXPECT_EQ(OK
, handle1
.Init("group1",
3873 callback1
.callback(),
3877 MockLayeredPool
mock_layered_pool(pool_
.get(), "group2");
3878 EXPECT_EQ(OK
, mock_layered_pool
.RequestSocket(pool_
.get()));
3879 EXPECT_CALL(mock_layered_pool
, CloseOneIdleConnection())
3880 .WillRepeatedly(Invoke(&mock_layered_pool
,
3881 &MockLayeredPool::ReleaseOneConnection
));
3882 mock_layered_pool
.set_can_release_connection(false);
3884 // The third request is made when the socket pool is in a stalled state.
3885 ClientSocketHandle handle3
;
3886 TestCompletionCallback callback3
;
3887 EXPECT_EQ(ERR_IO_PENDING
, handle3
.Init("group3",
3890 callback3
.callback(),
3894 base::RunLoop().RunUntilIdle();
3895 EXPECT_FALSE(callback3
.have_result());
3897 // The fourth request is made when the pool is no longer stalled. The third
3898 // request should be serviced first, since it was issued first and has the
3900 mock_layered_pool
.set_can_release_connection(true);
3901 ClientSocketHandle handle4
;
3902 TestCompletionCallback callback4
;
3903 EXPECT_EQ(ERR_IO_PENDING
, handle4
.Init("group3",
3906 callback4
.callback(),
3909 EXPECT_EQ(OK
, callback3
.WaitForResult());
3910 EXPECT_FALSE(callback4
.have_result());
3912 // Closing a handle should free up another socket slot.
3914 EXPECT_EQ(OK
, callback4
.WaitForResult());
3917 // Tests the case when an idle socket can be closed when a new request is
3918 // issued, and the new request belongs to a group that was previously stalled.
3920 // The two differences from the above test are that the stalled requests are not
3921 // in the same group as the layered pool's request, and the the fourth request
3922 // has a higher priority than the third one, so gets a socket first.
3923 TEST_F(ClientSocketPoolBaseTest
,
3924 CloseIdleSocketsHeldByLayeredPoolInSameGroupWhenNeeded2
) {
3926 std::list
<TestConnectJob::JobType
> job_types
;
3927 job_types
.push_back(TestConnectJob::kMockJob
);
3928 job_types
.push_back(TestConnectJob::kMockJob
);
3929 job_types
.push_back(TestConnectJob::kMockJob
);
3930 job_types
.push_back(TestConnectJob::kMockJob
);
3931 connect_job_factory_
->set_job_types(&job_types
);
3933 ClientSocketHandle handle1
;
3934 TestCompletionCallback callback1
;
3935 EXPECT_EQ(OK
, handle1
.Init("group1",
3938 callback1
.callback(),
3942 MockLayeredPool
mock_layered_pool(pool_
.get(), "group2");
3943 EXPECT_EQ(OK
, mock_layered_pool
.RequestSocket(pool_
.get()));
3944 EXPECT_CALL(mock_layered_pool
, CloseOneIdleConnection())
3945 .WillRepeatedly(Invoke(&mock_layered_pool
,
3946 &MockLayeredPool::ReleaseOneConnection
));
3947 mock_layered_pool
.set_can_release_connection(false);
3949 // The third request is made when the socket pool is in a stalled state.
3950 ClientSocketHandle handle3
;
3951 TestCompletionCallback callback3
;
3952 EXPECT_EQ(ERR_IO_PENDING
, handle3
.Init("group3",
3955 callback3
.callback(),
3959 base::RunLoop().RunUntilIdle();
3960 EXPECT_FALSE(callback3
.have_result());
3962 // The fourth request is made when the pool is no longer stalled. This
3963 // request has a higher priority than the third request, so is serviced first.
3964 mock_layered_pool
.set_can_release_connection(true);
3965 ClientSocketHandle handle4
;
3966 TestCompletionCallback callback4
;
3967 EXPECT_EQ(ERR_IO_PENDING
, handle4
.Init("group3",
3970 callback4
.callback(),
3973 EXPECT_EQ(OK
, callback4
.WaitForResult());
3974 EXPECT_FALSE(callback3
.have_result());
3976 // Closing a handle should free up another socket slot.
3978 EXPECT_EQ(OK
, callback3
.WaitForResult());
3981 TEST_F(ClientSocketPoolBaseTest
,
3982 CloseMultipleIdleSocketsHeldByLayeredPoolWhenNeeded
) {
3984 connect_job_factory_
->set_job_type(TestConnectJob::kMockJob
);
3986 MockLayeredPool
mock_layered_pool1(pool_
.get(), "foo");
3987 EXPECT_EQ(OK
, mock_layered_pool1
.RequestSocket(pool_
.get()));
3988 EXPECT_CALL(mock_layered_pool1
, CloseOneIdleConnection())
3989 .WillRepeatedly(Invoke(&mock_layered_pool1
,
3990 &MockLayeredPool::ReleaseOneConnection
));
3991 MockLayeredPool
mock_layered_pool2(pool_
.get(), "bar");
3992 EXPECT_EQ(OK
, mock_layered_pool2
.RequestSocketWithoutLimits(pool_
.get()));
3993 EXPECT_CALL(mock_layered_pool2
, CloseOneIdleConnection())
3994 .WillRepeatedly(Invoke(&mock_layered_pool2
,
3995 &MockLayeredPool::ReleaseOneConnection
));
3996 ClientSocketHandle handle
;
3997 TestCompletionCallback callback
;
3998 EXPECT_EQ(ERR_IO_PENDING
, handle
.Init("a",
4001 callback
.callback(),
4004 EXPECT_EQ(OK
, callback
.WaitForResult());
4007 // Test that when a socket pool and group are at their limits, a request
4008 // with |ignore_limits| triggers creation of a new socket, and gets the socket
4009 // instead of a request with the same priority that was issued earlier, but
4010 // that does not have |ignore_limits| set.
4011 TEST_F(ClientSocketPoolBaseTest
, IgnoreLimits
) {
4012 scoped_refptr
<TestSocketParams
> params_ignore_limits(
4013 new TestSocketParams(true /* ignore_limits */));
4016 // Issue a request to reach the socket pool limit.
4017 EXPECT_EQ(OK
, StartRequestWithParams("a", MAXIMUM_PRIORITY
, params_
));
4018 EXPECT_EQ(0, pool_
->NumConnectJobsInGroup("a"));
4020 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
4022 EXPECT_EQ(ERR_IO_PENDING
, StartRequestWithParams("a", MAXIMUM_PRIORITY
,
4024 EXPECT_EQ(0, pool_
->NumConnectJobsInGroup("a"));
4026 EXPECT_EQ(ERR_IO_PENDING
, StartRequestWithParams("a", MAXIMUM_PRIORITY
,
4027 params_ignore_limits
));
4028 ASSERT_EQ(1, pool_
->NumConnectJobsInGroup("a"));
4030 EXPECT_EQ(OK
, request(2)->WaitForResult());
4031 EXPECT_FALSE(request(1)->have_result());
4034 // Test that when a socket pool and group are at their limits, a ConnectJob
4035 // issued for a request with |ignore_limits| set is not cancelled when a request
4036 // without |ignore_limits| issued to the same group is cancelled.
4037 TEST_F(ClientSocketPoolBaseTest
, IgnoreLimitsCancelOtherJob
) {
4038 scoped_refptr
<TestSocketParams
> params_ignore_limits(
4039 new TestSocketParams(true /* ignore_limits */));
4042 // Issue a request to reach the socket pool limit.
4043 EXPECT_EQ(OK
, StartRequestWithParams("a", MAXIMUM_PRIORITY
, params_
));
4044 EXPECT_EQ(0, pool_
->NumConnectJobsInGroup("a"));
4046 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
4048 EXPECT_EQ(ERR_IO_PENDING
, StartRequestWithParams("a", MAXIMUM_PRIORITY
,
4050 EXPECT_EQ(0, pool_
->NumConnectJobsInGroup("a"));
4052 EXPECT_EQ(ERR_IO_PENDING
, StartRequestWithParams("a", MAXIMUM_PRIORITY
,
4053 params_ignore_limits
));
4054 ASSERT_EQ(1, pool_
->NumConnectJobsInGroup("a"));
4056 // Cancel the pending request without ignore_limits set. The ConnectJob
4057 // should not be cancelled.
4058 request(1)->handle()->Reset();
4059 ASSERT_EQ(1, pool_
->NumConnectJobsInGroup("a"));
4061 EXPECT_EQ(OK
, request(2)->WaitForResult());
4062 EXPECT_FALSE(request(1)->have_result());