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/request_priority.h"
25 #include "net/base/test_completion_callback.h"
26 #include "net/http/http_response_headers.h"
27 #include "net/log/net_log.h"
28 #include "net/log/net_log_unittest.h"
29 #include "net/socket/client_socket_factory.h"
30 #include "net/socket/client_socket_handle.h"
31 #include "net/socket/socket_test_util.h"
32 #include "net/socket/ssl_client_socket.h"
33 #include "net/socket/stream_socket.h"
34 #include "net/udp/datagram_client_socket.h"
35 #include "testing/gmock/include/gmock/gmock.h"
36 #include "testing/gtest/include/gtest/gtest.h"
38 using ::testing::Invoke
;
39 using ::testing::Return
;
45 const int kDefaultMaxSockets
= 4;
46 const int kDefaultMaxSocketsPerGroup
= 2;
48 // Make sure |handle| sets load times correctly when it has been assigned a
50 void TestLoadTimingInfoConnectedReused(const ClientSocketHandle
& handle
) {
51 LoadTimingInfo load_timing_info
;
52 // Only pass true in as |is_reused|, as in general, HttpStream types should
53 // have stricter concepts of reuse than socket pools.
54 EXPECT_TRUE(handle
.GetLoadTimingInfo(true, &load_timing_info
));
56 EXPECT_EQ(true, load_timing_info
.socket_reused
);
57 EXPECT_NE(NetLog::Source::kInvalidId
, load_timing_info
.socket_log_id
);
59 ExpectConnectTimingHasNoTimes(load_timing_info
.connect_timing
);
60 ExpectLoadTimingHasOnlyConnectionTimes(load_timing_info
);
63 // Make sure |handle| sets load times correctly when it has been assigned a
64 // fresh socket. Also runs TestLoadTimingInfoConnectedReused, since the owner
65 // of a connection where |is_reused| is false may consider the connection
67 void TestLoadTimingInfoConnectedNotReused(const ClientSocketHandle
& handle
) {
68 EXPECT_FALSE(handle
.is_reused());
70 LoadTimingInfo load_timing_info
;
71 EXPECT_TRUE(handle
.GetLoadTimingInfo(false, &load_timing_info
));
73 EXPECT_FALSE(load_timing_info
.socket_reused
);
74 EXPECT_NE(NetLog::Source::kInvalidId
, load_timing_info
.socket_log_id
);
76 ExpectConnectTimingHasTimes(load_timing_info
.connect_timing
,
77 CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY
);
78 ExpectLoadTimingHasOnlyConnectionTimes(load_timing_info
);
80 TestLoadTimingInfoConnectedReused(handle
);
83 // Make sure |handle| sets load times correctly, in the case that it does not
84 // currently have a socket.
85 void TestLoadTimingInfoNotConnected(const ClientSocketHandle
& handle
) {
86 // Should only be set to true once a socket is assigned, if at all.
87 EXPECT_FALSE(handle
.is_reused());
89 LoadTimingInfo load_timing_info
;
90 EXPECT_FALSE(handle
.GetLoadTimingInfo(false, &load_timing_info
));
92 EXPECT_FALSE(load_timing_info
.socket_reused
);
93 EXPECT_EQ(NetLog::Source::kInvalidId
, load_timing_info
.socket_log_id
);
95 ExpectConnectTimingHasNoTimes(load_timing_info
.connect_timing
);
96 ExpectLoadTimingHasOnlyConnectionTimes(load_timing_info
);
99 class TestSocketParams
: public base::RefCounted
<TestSocketParams
> {
101 explicit TestSocketParams(bool ignore_limits
)
102 : ignore_limits_(ignore_limits
) {}
104 bool ignore_limits() { return ignore_limits_
; }
107 friend class base::RefCounted
<TestSocketParams
>;
108 ~TestSocketParams() {}
110 const bool ignore_limits_
;
112 typedef ClientSocketPoolBase
<TestSocketParams
> TestClientSocketPoolBase
;
114 class MockClientSocket
: public StreamSocket
{
116 explicit MockClientSocket(net::NetLog
* net_log
)
118 has_unread_data_(false),
119 net_log_(BoundNetLog::Make(net_log
, net::NetLog::SOURCE_SOCKET
)),
120 was_used_to_convey_data_(false) {
123 // Sets whether the socket has unread data. If true, the next call to Read()
124 // will return 1 byte and IsConnectedAndIdle() will return false.
125 void set_has_unread_data(bool has_unread_data
) {
126 has_unread_data_
= has_unread_data
;
129 // Socket implementation.
130 int Read(IOBuffer
* /* buf */,
132 const CompletionCallback
& /* callback */) override
{
133 if (has_unread_data_
&& len
> 0) {
134 has_unread_data_
= false;
135 was_used_to_convey_data_
= true;
138 return ERR_UNEXPECTED
;
141 int Write(IOBuffer
* /* buf */,
143 const CompletionCallback
& /* callback */) override
{
144 was_used_to_convey_data_
= true;
147 int SetReceiveBufferSize(int32 size
) override
{ return OK
; }
148 int SetSendBufferSize(int32 size
) override
{ return OK
; }
150 // StreamSocket implementation.
151 int Connect(const CompletionCallback
& callback
) override
{
156 void Disconnect() override
{ connected_
= false; }
157 bool IsConnected() const override
{ return connected_
; }
158 bool IsConnectedAndIdle() const override
{
159 return connected_
&& !has_unread_data_
;
162 int GetPeerAddress(IPEndPoint
* /* address */) const override
{
163 return ERR_UNEXPECTED
;
166 int GetLocalAddress(IPEndPoint
* /* address */) const override
{
167 return ERR_UNEXPECTED
;
170 const BoundNetLog
& NetLog() const override
{ return net_log_
; }
172 void SetSubresourceSpeculation() override
{}
173 void SetOmniboxSpeculation() override
{}
174 bool WasEverUsed() const override
{ return was_used_to_convey_data_
; }
175 bool UsingTCPFastOpen() const override
{ return false; }
176 bool WasNpnNegotiated() const override
{ return false; }
177 NextProto
GetNegotiatedProtocol() const override
{ return kProtoUnknown
; }
178 bool GetSSLInfo(SSLInfo
* ssl_info
) override
{ return false; }
182 bool has_unread_data_
;
183 BoundNetLog net_log_
;
184 bool was_used_to_convey_data_
;
186 DISALLOW_COPY_AND_ASSIGN(MockClientSocket
);
189 class TestConnectJob
;
191 class MockClientSocketFactory
: public ClientSocketFactory
{
193 MockClientSocketFactory() : allocation_count_(0) {}
195 scoped_ptr
<DatagramClientSocket
> CreateDatagramClientSocket(
196 DatagramSocket::BindType bind_type
,
197 const RandIntCallback
& rand_int_cb
,
199 const NetLog::Source
& source
) override
{
201 return scoped_ptr
<DatagramClientSocket
>();
204 scoped_ptr
<StreamSocket
> CreateTransportClientSocket(
205 const AddressList
& addresses
,
206 NetLog
* /* net_log */,
207 const NetLog::Source
& /*source*/) override
{
209 return scoped_ptr
<StreamSocket
>();
212 scoped_ptr
<SSLClientSocket
> CreateSSLClientSocket(
213 scoped_ptr
<ClientSocketHandle
> transport_socket
,
214 const HostPortPair
& host_and_port
,
215 const SSLConfig
& ssl_config
,
216 const SSLClientSocketContext
& context
) override
{
218 return scoped_ptr
<SSLClientSocket
>();
221 void ClearSSLSessionCache() override
{ NOTIMPLEMENTED(); }
223 void WaitForSignal(TestConnectJob
* job
) { waiting_jobs_
.push_back(job
); }
227 void SignalJob(size_t job
);
229 void SetJobLoadState(size_t job
, LoadState load_state
);
231 int allocation_count() const { return allocation_count_
; }
234 int allocation_count_
;
235 std::vector
<TestConnectJob
*> waiting_jobs_
;
238 class TestConnectJob
: public ConnectJob
{
244 kMockPendingFailingJob
,
247 kMockPendingRecoverableJob
,
248 kMockAdditionalErrorStateJob
,
249 kMockPendingAdditionalErrorStateJob
,
253 // The kMockPendingJob uses a slight delay before allowing the connect
255 static const int kPendingConnectDelay
= 2;
257 TestConnectJob(JobType job_type
,
258 const std::string
& group_name
,
259 const TestClientSocketPoolBase::Request
& request
,
260 base::TimeDelta timeout_duration
,
261 ConnectJob::Delegate
* delegate
,
262 MockClientSocketFactory
* client_socket_factory
,
264 : ConnectJob(group_name
, timeout_duration
, request
.priority(), delegate
,
265 BoundNetLog::Make(net_log
, NetLog::SOURCE_CONNECT_JOB
)),
267 client_socket_factory_(client_socket_factory
),
268 load_state_(LOAD_STATE_IDLE
),
269 store_additional_error_state_(false),
270 weak_factory_(this) {
274 DoConnect(waiting_success_
, true /* async */, false /* recoverable */);
277 void set_load_state(LoadState load_state
) { load_state_
= load_state
; }
281 LoadState
GetLoadState() const override
{ return load_state_
; }
283 void GetAdditionalErrorState(ClientSocketHandle
* handle
) override
{
284 if (store_additional_error_state_
) {
285 // Set all of the additional error state fields in some way.
286 handle
->set_is_ssl_error(true);
287 HttpResponseInfo info
;
288 info
.headers
= new HttpResponseHeaders(std::string());
289 handle
->set_ssl_error_response_info(info
);
296 int ConnectInternal() override
{
298 client_socket_factory_
->CreateTransportClientSocket(
299 ignored
, NULL
, net::NetLog::Source());
301 scoped_ptr
<StreamSocket
>(new MockClientSocket(net_log().net_log())));
304 return DoConnect(true /* successful */, false /* sync */,
305 false /* recoverable */);
306 case kMockFailingJob
:
307 return DoConnect(false /* error */, false /* sync */,
308 false /* recoverable */);
309 case kMockPendingJob
:
310 set_load_state(LOAD_STATE_CONNECTING
);
312 // Depending on execution timings, posting a delayed task can result
313 // in the task getting executed the at the earliest possible
314 // opportunity or only after returning once from the message loop and
315 // then a second call into the message loop. In order to make behavior
316 // more deterministic, we change the default delay to 2ms. This should
317 // always require us to wait for the second call into the message loop.
319 // N.B. The correct fix for this and similar timing problems is to
320 // abstract time for the purpose of unittests. Unfortunately, we have
321 // a lot of third-party components that directly call the various
322 // time functions, so this change would be rather invasive.
323 base::MessageLoop::current()->PostDelayedTask(
325 base::Bind(base::IgnoreResult(&TestConnectJob::DoConnect
),
326 weak_factory_
.GetWeakPtr(),
327 true /* successful */,
329 false /* recoverable */),
330 base::TimeDelta::FromMilliseconds(kPendingConnectDelay
));
331 return ERR_IO_PENDING
;
332 case kMockPendingFailingJob
:
333 set_load_state(LOAD_STATE_CONNECTING
);
334 base::MessageLoop::current()->PostDelayedTask(
336 base::Bind(base::IgnoreResult(&TestConnectJob::DoConnect
),
337 weak_factory_
.GetWeakPtr(),
340 false /* recoverable */),
341 base::TimeDelta::FromMilliseconds(2));
342 return ERR_IO_PENDING
;
343 case kMockWaitingJob
:
344 set_load_state(LOAD_STATE_CONNECTING
);
345 client_socket_factory_
->WaitForSignal(this);
346 waiting_success_
= true;
347 return ERR_IO_PENDING
;
348 case kMockRecoverableJob
:
349 return DoConnect(false /* error */, false /* sync */,
350 true /* recoverable */);
351 case kMockPendingRecoverableJob
:
352 set_load_state(LOAD_STATE_CONNECTING
);
353 base::MessageLoop::current()->PostDelayedTask(
355 base::Bind(base::IgnoreResult(&TestConnectJob::DoConnect
),
356 weak_factory_
.GetWeakPtr(),
359 true /* recoverable */),
360 base::TimeDelta::FromMilliseconds(2));
361 return ERR_IO_PENDING
;
362 case kMockAdditionalErrorStateJob
:
363 store_additional_error_state_
= true;
364 return DoConnect(false /* error */, false /* sync */,
365 false /* recoverable */);
366 case kMockPendingAdditionalErrorStateJob
:
367 set_load_state(LOAD_STATE_CONNECTING
);
368 store_additional_error_state_
= true;
369 base::MessageLoop::current()->PostDelayedTask(
371 base::Bind(base::IgnoreResult(&TestConnectJob::DoConnect
),
372 weak_factory_
.GetWeakPtr(),
375 false /* recoverable */),
376 base::TimeDelta::FromMilliseconds(2));
377 return ERR_IO_PENDING
;
378 case kMockUnreadDataJob
: {
379 int ret
= DoConnect(true /* successful */, false /* sync */,
380 false /* recoverable */);
381 static_cast<MockClientSocket
*>(socket())->set_has_unread_data(true);
386 SetSocket(scoped_ptr
<StreamSocket
>());
391 int DoConnect(bool succeed
, bool was_async
, bool recoverable
) {
394 socket()->Connect(CompletionCallback());
395 } else if (recoverable
) {
396 result
= ERR_PROXY_AUTH_REQUESTED
;
398 result
= ERR_CONNECTION_FAILED
;
399 SetSocket(scoped_ptr
<StreamSocket
>());
403 NotifyDelegateOfCompletion(result
);
407 bool waiting_success_
;
408 const JobType job_type_
;
409 MockClientSocketFactory
* const client_socket_factory_
;
410 LoadState load_state_
;
411 bool store_additional_error_state_
;
413 base::WeakPtrFactory
<TestConnectJob
> weak_factory_
;
415 DISALLOW_COPY_AND_ASSIGN(TestConnectJob
);
418 class TestConnectJobFactory
419 : public TestClientSocketPoolBase::ConnectJobFactory
{
421 TestConnectJobFactory(MockClientSocketFactory
* client_socket_factory
,
423 : job_type_(TestConnectJob::kMockJob
),
425 client_socket_factory_(client_socket_factory
),
429 ~TestConnectJobFactory() override
{}
431 void set_job_type(TestConnectJob::JobType job_type
) { job_type_
= job_type
; }
433 void set_job_types(std::list
<TestConnectJob::JobType
>* job_types
) {
434 job_types_
= job_types
;
435 CHECK(!job_types_
->empty());
438 void set_timeout_duration(base::TimeDelta timeout_duration
) {
439 timeout_duration_
= timeout_duration
;
442 // ConnectJobFactory implementation.
444 scoped_ptr
<ConnectJob
> NewConnectJob(
445 const std::string
& group_name
,
446 const TestClientSocketPoolBase::Request
& request
,
447 ConnectJob::Delegate
* delegate
) const override
{
448 EXPECT_TRUE(!job_types_
|| !job_types_
->empty());
449 TestConnectJob::JobType job_type
= job_type_
;
450 if (job_types_
&& !job_types_
->empty()) {
451 job_type
= job_types_
->front();
452 job_types_
->pop_front();
454 return scoped_ptr
<ConnectJob
>(new TestConnectJob(job_type
,
459 client_socket_factory_
,
463 base::TimeDelta
ConnectionTimeout() const override
{
464 return timeout_duration_
;
468 TestConnectJob::JobType job_type_
;
469 std::list
<TestConnectJob::JobType
>* job_types_
;
470 base::TimeDelta timeout_duration_
;
471 MockClientSocketFactory
* const client_socket_factory_
;
474 DISALLOW_COPY_AND_ASSIGN(TestConnectJobFactory
);
477 class TestClientSocketPool
: public ClientSocketPool
{
479 typedef TestSocketParams SocketParams
;
481 TestClientSocketPool(
483 int max_sockets_per_group
,
484 base::TimeDelta unused_idle_socket_timeout
,
485 base::TimeDelta used_idle_socket_timeout
,
486 TestClientSocketPoolBase::ConnectJobFactory
* connect_job_factory
)
489 max_sockets_per_group
,
490 unused_idle_socket_timeout
,
491 used_idle_socket_timeout
,
492 connect_job_factory
) {}
494 ~TestClientSocketPool() override
{}
496 int RequestSocket(const std::string
& group_name
,
498 net::RequestPriority priority
,
499 ClientSocketHandle
* handle
,
500 const CompletionCallback
& callback
,
501 const BoundNetLog
& net_log
) override
{
502 const scoped_refptr
<TestSocketParams
>* casted_socket_params
=
503 static_cast<const scoped_refptr
<TestSocketParams
>*>(params
);
504 return base_
.RequestSocket(group_name
, *casted_socket_params
, priority
,
505 handle
, callback
, net_log
);
508 void RequestSockets(const std::string
& group_name
,
511 const BoundNetLog
& net_log
) override
{
512 const scoped_refptr
<TestSocketParams
>* casted_params
=
513 static_cast<const scoped_refptr
<TestSocketParams
>*>(params
);
515 base_
.RequestSockets(group_name
, *casted_params
, num_sockets
, net_log
);
518 void CancelRequest(const std::string
& group_name
,
519 ClientSocketHandle
* handle
) override
{
520 base_
.CancelRequest(group_name
, handle
);
523 void ReleaseSocket(const std::string
& group_name
,
524 scoped_ptr
<StreamSocket
> socket
,
526 base_
.ReleaseSocket(group_name
, socket
.Pass(), id
);
529 void FlushWithError(int error
) override
{ base_
.FlushWithError(error
); }
531 bool IsStalled() const override
{ return base_
.IsStalled(); }
533 void CloseIdleSockets() override
{ base_
.CloseIdleSockets(); }
535 int IdleSocketCount() const override
{ return base_
.idle_socket_count(); }
537 int IdleSocketCountInGroup(const std::string
& group_name
) const override
{
538 return base_
.IdleSocketCountInGroup(group_name
);
541 LoadState
GetLoadState(const std::string
& group_name
,
542 const ClientSocketHandle
* handle
) const override
{
543 return base_
.GetLoadState(group_name
, handle
);
546 void AddHigherLayeredPool(HigherLayeredPool
* higher_pool
) override
{
547 base_
.AddHigherLayeredPool(higher_pool
);
550 void RemoveHigherLayeredPool(HigherLayeredPool
* higher_pool
) override
{
551 base_
.RemoveHigherLayeredPool(higher_pool
);
554 base::DictionaryValue
* GetInfoAsValue(
555 const std::string
& name
,
556 const std::string
& type
,
557 bool include_nested_pools
) const override
{
558 return base_
.GetInfoAsValue(name
, type
);
561 base::TimeDelta
ConnectionTimeout() const override
{
562 return base_
.ConnectionTimeout();
565 const TestClientSocketPoolBase
* base() const { return &base_
; }
567 int NumUnassignedConnectJobsInGroup(const std::string
& group_name
) const {
568 return base_
.NumUnassignedConnectJobsInGroup(group_name
);
571 int NumConnectJobsInGroup(const std::string
& group_name
) const {
572 return base_
.NumConnectJobsInGroup(group_name
);
575 int NumActiveSocketsInGroup(const std::string
& group_name
) const {
576 return base_
.NumActiveSocketsInGroup(group_name
);
579 bool HasGroup(const std::string
& group_name
) const {
580 return base_
.HasGroup(group_name
);
583 void CleanupTimedOutIdleSockets() { base_
.CleanupIdleSockets(false); }
585 void EnableConnectBackupJobs() { base_
.EnableConnectBackupJobs(); }
587 bool CloseOneIdleConnectionInHigherLayeredPool() {
588 return base_
.CloseOneIdleConnectionInHigherLayeredPool();
592 TestClientSocketPoolBase base_
;
594 DISALLOW_COPY_AND_ASSIGN(TestClientSocketPool
);
601 void MockClientSocketFactory::SignalJobs() {
602 for (std::vector
<TestConnectJob
*>::iterator it
= waiting_jobs_
.begin();
603 it
!= waiting_jobs_
.end(); ++it
) {
606 waiting_jobs_
.clear();
609 void MockClientSocketFactory::SignalJob(size_t job
) {
610 ASSERT_LT(job
, waiting_jobs_
.size());
611 waiting_jobs_
[job
]->Signal();
612 waiting_jobs_
.erase(waiting_jobs_
.begin() + job
);
615 void MockClientSocketFactory::SetJobLoadState(size_t job
,
616 LoadState load_state
) {
617 ASSERT_LT(job
, waiting_jobs_
.size());
618 waiting_jobs_
[job
]->set_load_state(load_state
);
621 class TestConnectJobDelegate
: public ConnectJob::Delegate
{
623 TestConnectJobDelegate()
624 : have_result_(false), waiting_for_result_(false), result_(OK
) {}
625 ~TestConnectJobDelegate() override
{}
627 void OnConnectJobComplete(int result
, ConnectJob
* job
) override
{
629 scoped_ptr
<ConnectJob
> owned_job(job
);
630 scoped_ptr
<StreamSocket
> socket
= owned_job
->PassSocket();
631 // socket.get() should be NULL iff result != OK
632 EXPECT_EQ(socket
== NULL
, result
!= OK
);
634 if (waiting_for_result_
)
635 base::MessageLoop::current()->Quit();
638 int WaitForResult() {
639 DCHECK(!waiting_for_result_
);
640 while (!have_result_
) {
641 waiting_for_result_
= true;
642 base::MessageLoop::current()->Run();
643 waiting_for_result_
= false;
645 have_result_
= false; // auto-reset for next callback
651 bool waiting_for_result_
;
655 class ClientSocketPoolBaseTest
: public testing::Test
{
657 ClientSocketPoolBaseTest()
658 : params_(new TestSocketParams(false /* ignore_limits */)) {
659 connect_backup_jobs_enabled_
=
660 internal::ClientSocketPoolBaseHelper::connect_backup_jobs_enabled();
661 internal::ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(true);
662 cleanup_timer_enabled_
=
663 internal::ClientSocketPoolBaseHelper::cleanup_timer_enabled();
666 ~ClientSocketPoolBaseTest() override
{
667 internal::ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(
668 connect_backup_jobs_enabled_
);
669 internal::ClientSocketPoolBaseHelper::set_cleanup_timer_enabled(
670 cleanup_timer_enabled_
);
673 void CreatePool(int max_sockets
, int max_sockets_per_group
) {
674 CreatePoolWithIdleTimeouts(
676 max_sockets_per_group
,
677 ClientSocketPool::unused_idle_socket_timeout(),
678 ClientSocketPool::used_idle_socket_timeout());
681 void CreatePoolWithIdleTimeouts(
682 int max_sockets
, int max_sockets_per_group
,
683 base::TimeDelta unused_idle_socket_timeout
,
684 base::TimeDelta used_idle_socket_timeout
) {
685 DCHECK(!pool_
.get());
686 connect_job_factory_
= new TestConnectJobFactory(&client_socket_factory_
,
688 pool_
.reset(new TestClientSocketPool(max_sockets
,
689 max_sockets_per_group
,
690 unused_idle_socket_timeout
,
691 used_idle_socket_timeout
,
692 connect_job_factory_
));
695 int StartRequestWithParams(
696 const std::string
& group_name
,
697 RequestPriority priority
,
698 const scoped_refptr
<TestSocketParams
>& params
) {
699 return test_base_
.StartRequestUsingPool(
700 pool_
.get(), group_name
, priority
, params
);
703 int StartRequest(const std::string
& group_name
, RequestPriority priority
) {
704 return StartRequestWithParams(group_name
, priority
, params_
);
707 int GetOrderOfRequest(size_t index
) const {
708 return test_base_
.GetOrderOfRequest(index
);
711 bool ReleaseOneConnection(ClientSocketPoolTest::KeepAlive keep_alive
) {
712 return test_base_
.ReleaseOneConnection(keep_alive
);
715 void ReleaseAllConnections(ClientSocketPoolTest::KeepAlive keep_alive
) {
716 test_base_
.ReleaseAllConnections(keep_alive
);
719 TestSocketRequest
* request(int i
) { return test_base_
.request(i
); }
720 size_t requests_size() const { return test_base_
.requests_size(); }
721 ScopedVector
<TestSocketRequest
>* requests() { return test_base_
.requests(); }
722 size_t completion_count() const { return test_base_
.completion_count(); }
724 CapturingNetLog net_log_
;
725 bool connect_backup_jobs_enabled_
;
726 bool cleanup_timer_enabled_
;
727 MockClientSocketFactory client_socket_factory_
;
728 TestConnectJobFactory
* connect_job_factory_
;
729 scoped_refptr
<TestSocketParams
> params_
;
730 scoped_ptr
<TestClientSocketPool
> pool_
;
731 ClientSocketPoolTest test_base_
;
734 // Even though a timeout is specified, it doesn't time out on a synchronous
736 TEST_F(ClientSocketPoolBaseTest
, ConnectJob_NoTimeoutOnSynchronousCompletion
) {
737 TestConnectJobDelegate delegate
;
738 ClientSocketHandle ignored
;
739 TestClientSocketPoolBase::Request
request(
740 &ignored
, CompletionCallback(), DEFAULT_PRIORITY
,
741 internal::ClientSocketPoolBaseHelper::NORMAL
,
742 false, params_
, BoundNetLog());
743 scoped_ptr
<TestConnectJob
> job(
744 new TestConnectJob(TestConnectJob::kMockJob
,
747 base::TimeDelta::FromMicroseconds(1),
749 &client_socket_factory_
,
751 EXPECT_EQ(OK
, job
->Connect());
754 TEST_F(ClientSocketPoolBaseTest
, ConnectJob_TimedOut
) {
755 TestConnectJobDelegate delegate
;
756 ClientSocketHandle ignored
;
759 TestClientSocketPoolBase::Request
request(
760 &ignored
, CompletionCallback(), DEFAULT_PRIORITY
,
761 internal::ClientSocketPoolBaseHelper::NORMAL
,
762 false, params_
, BoundNetLog());
763 // Deleted by TestConnectJobDelegate.
764 TestConnectJob
* job
=
765 new TestConnectJob(TestConnectJob::kMockPendingJob
,
768 base::TimeDelta::FromMicroseconds(1),
770 &client_socket_factory_
,
772 ASSERT_EQ(ERR_IO_PENDING
, job
->Connect());
773 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(1));
774 EXPECT_EQ(ERR_TIMED_OUT
, delegate
.WaitForResult());
776 CapturingNetLog::CapturedEntryList entries
;
777 log
.GetEntries(&entries
);
779 EXPECT_EQ(6u, entries
.size());
780 EXPECT_TRUE(LogContainsBeginEvent(
781 entries
, 0, NetLog::TYPE_SOCKET_POOL_CONNECT_JOB
));
782 EXPECT_TRUE(LogContainsBeginEvent(
783 entries
, 1, NetLog::TYPE_SOCKET_POOL_CONNECT_JOB_CONNECT
));
784 EXPECT_TRUE(LogContainsEvent(
785 entries
, 2, NetLog::TYPE_CONNECT_JOB_SET_SOCKET
,
786 NetLog::PHASE_NONE
));
787 EXPECT_TRUE(LogContainsEvent(
788 entries
, 3, NetLog::TYPE_SOCKET_POOL_CONNECT_JOB_TIMED_OUT
,
789 NetLog::PHASE_NONE
));
790 EXPECT_TRUE(LogContainsEndEvent(
791 entries
, 4, NetLog::TYPE_SOCKET_POOL_CONNECT_JOB_CONNECT
));
792 EXPECT_TRUE(LogContainsEndEvent(
793 entries
, 5, NetLog::TYPE_SOCKET_POOL_CONNECT_JOB
));
796 TEST_F(ClientSocketPoolBaseTest
, BasicSynchronous
) {
797 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
799 TestCompletionCallback callback
;
800 ClientSocketHandle handle
;
801 CapturingBoundNetLog log
;
802 TestLoadTimingInfoNotConnected(handle
);
811 EXPECT_TRUE(handle
.is_initialized());
812 EXPECT_TRUE(handle
.socket());
813 TestLoadTimingInfoConnectedNotReused(handle
);
816 TestLoadTimingInfoNotConnected(handle
);
818 CapturingNetLog::CapturedEntryList entries
;
819 log
.GetEntries(&entries
);
821 EXPECT_EQ(4u, entries
.size());
822 EXPECT_TRUE(LogContainsBeginEvent(
823 entries
, 0, NetLog::TYPE_SOCKET_POOL
));
824 EXPECT_TRUE(LogContainsEvent(
825 entries
, 1, NetLog::TYPE_SOCKET_POOL_BOUND_TO_CONNECT_JOB
,
826 NetLog::PHASE_NONE
));
827 EXPECT_TRUE(LogContainsEvent(
828 entries
, 2, NetLog::TYPE_SOCKET_POOL_BOUND_TO_SOCKET
,
829 NetLog::PHASE_NONE
));
830 EXPECT_TRUE(LogContainsEndEvent(
831 entries
, 3, NetLog::TYPE_SOCKET_POOL
));
834 TEST_F(ClientSocketPoolBaseTest
, InitConnectionFailure
) {
835 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
837 connect_job_factory_
->set_job_type(TestConnectJob::kMockFailingJob
);
838 CapturingBoundNetLog log
;
840 ClientSocketHandle handle
;
841 TestCompletionCallback callback
;
842 // Set the additional error state members to ensure that they get cleared.
843 handle
.set_is_ssl_error(true);
844 HttpResponseInfo info
;
845 info
.headers
= new HttpResponseHeaders(std::string());
846 handle
.set_ssl_error_response_info(info
);
847 EXPECT_EQ(ERR_CONNECTION_FAILED
,
854 EXPECT_FALSE(handle
.socket());
855 EXPECT_FALSE(handle
.is_ssl_error());
856 EXPECT_TRUE(handle
.ssl_error_response_info().headers
.get() == NULL
);
857 TestLoadTimingInfoNotConnected(handle
);
859 CapturingNetLog::CapturedEntryList entries
;
860 log
.GetEntries(&entries
);
862 EXPECT_EQ(3u, entries
.size());
863 EXPECT_TRUE(LogContainsBeginEvent(
864 entries
, 0, NetLog::TYPE_SOCKET_POOL
));
865 EXPECT_TRUE(LogContainsEvent(
866 entries
, 1, NetLog::TYPE_SOCKET_POOL_BOUND_TO_CONNECT_JOB
,
867 NetLog::PHASE_NONE
));
868 EXPECT_TRUE(LogContainsEndEvent(
869 entries
, 2, NetLog::TYPE_SOCKET_POOL
));
872 TEST_F(ClientSocketPoolBaseTest
, TotalLimit
) {
873 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
875 // TODO(eroman): Check that the NetLog contains this event.
877 EXPECT_EQ(OK
, StartRequest("a", DEFAULT_PRIORITY
));
878 EXPECT_EQ(OK
, StartRequest("b", DEFAULT_PRIORITY
));
879 EXPECT_EQ(OK
, StartRequest("c", DEFAULT_PRIORITY
));
880 EXPECT_EQ(OK
, StartRequest("d", DEFAULT_PRIORITY
));
882 EXPECT_EQ(static_cast<int>(requests_size()),
883 client_socket_factory_
.allocation_count());
884 EXPECT_EQ(requests_size() - kDefaultMaxSockets
, completion_count());
886 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("e", DEFAULT_PRIORITY
));
887 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("f", DEFAULT_PRIORITY
));
888 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("g", DEFAULT_PRIORITY
));
890 ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE
);
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(1, GetOrderOfRequest(1));
897 EXPECT_EQ(2, GetOrderOfRequest(2));
898 EXPECT_EQ(3, GetOrderOfRequest(3));
899 EXPECT_EQ(4, GetOrderOfRequest(4));
900 EXPECT_EQ(5, GetOrderOfRequest(5));
901 EXPECT_EQ(6, GetOrderOfRequest(6));
902 EXPECT_EQ(7, GetOrderOfRequest(7));
904 // Make sure we test order of all requests made.
905 EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds
, GetOrderOfRequest(8));
908 TEST_F(ClientSocketPoolBaseTest
, TotalLimitReachedNewGroup
) {
909 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
911 // TODO(eroman): Check that the NetLog contains this event.
913 // Reach all limits: max total sockets, and max sockets per group.
914 EXPECT_EQ(OK
, StartRequest("a", DEFAULT_PRIORITY
));
915 EXPECT_EQ(OK
, StartRequest("a", DEFAULT_PRIORITY
));
916 EXPECT_EQ(OK
, StartRequest("b", DEFAULT_PRIORITY
));
917 EXPECT_EQ(OK
, StartRequest("b", DEFAULT_PRIORITY
));
919 EXPECT_EQ(static_cast<int>(requests_size()),
920 client_socket_factory_
.allocation_count());
921 EXPECT_EQ(requests_size() - kDefaultMaxSockets
, completion_count());
923 // Now create a new group and verify that we don't starve it.
924 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("c", DEFAULT_PRIORITY
));
926 ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE
);
928 EXPECT_EQ(static_cast<int>(requests_size()),
929 client_socket_factory_
.allocation_count());
930 EXPECT_EQ(requests_size() - kDefaultMaxSockets
, completion_count());
932 EXPECT_EQ(1, GetOrderOfRequest(1));
933 EXPECT_EQ(2, GetOrderOfRequest(2));
934 EXPECT_EQ(3, GetOrderOfRequest(3));
935 EXPECT_EQ(4, GetOrderOfRequest(4));
936 EXPECT_EQ(5, GetOrderOfRequest(5));
938 // Make sure we test order of all requests made.
939 EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds
, GetOrderOfRequest(6));
942 TEST_F(ClientSocketPoolBaseTest
, TotalLimitRespectsPriority
) {
943 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
945 EXPECT_EQ(OK
, StartRequest("b", LOWEST
));
946 EXPECT_EQ(OK
, StartRequest("a", MEDIUM
));
947 EXPECT_EQ(OK
, StartRequest("b", HIGHEST
));
948 EXPECT_EQ(OK
, StartRequest("a", LOWEST
));
950 EXPECT_EQ(static_cast<int>(requests_size()),
951 client_socket_factory_
.allocation_count());
953 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("c", LOWEST
));
954 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", MEDIUM
));
955 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("b", HIGHEST
));
957 ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE
);
959 EXPECT_EQ(requests_size() - kDefaultMaxSockets
, completion_count());
961 // First 4 requests don't have to wait, and finish in order.
962 EXPECT_EQ(1, GetOrderOfRequest(1));
963 EXPECT_EQ(2, GetOrderOfRequest(2));
964 EXPECT_EQ(3, GetOrderOfRequest(3));
965 EXPECT_EQ(4, GetOrderOfRequest(4));
967 // Request ("b", HIGHEST) has the highest priority, then ("a", MEDIUM),
968 // and then ("c", LOWEST).
969 EXPECT_EQ(7, GetOrderOfRequest(5));
970 EXPECT_EQ(6, GetOrderOfRequest(6));
971 EXPECT_EQ(5, GetOrderOfRequest(7));
973 // Make sure we test order of all requests made.
974 EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds
, GetOrderOfRequest(9));
977 TEST_F(ClientSocketPoolBaseTest
, TotalLimitRespectsGroupLimit
) {
978 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
980 EXPECT_EQ(OK
, StartRequest("a", LOWEST
));
981 EXPECT_EQ(OK
, StartRequest("a", LOW
));
982 EXPECT_EQ(OK
, StartRequest("b", HIGHEST
));
983 EXPECT_EQ(OK
, StartRequest("b", MEDIUM
));
985 EXPECT_EQ(static_cast<int>(requests_size()),
986 client_socket_factory_
.allocation_count());
988 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("c", MEDIUM
));
989 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", LOW
));
990 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("b", HIGHEST
));
992 ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE
);
994 EXPECT_EQ(static_cast<int>(requests_size()),
995 client_socket_factory_
.allocation_count());
996 EXPECT_EQ(requests_size() - kDefaultMaxSockets
, completion_count());
998 // First 4 requests don't have to wait, and finish in order.
999 EXPECT_EQ(1, GetOrderOfRequest(1));
1000 EXPECT_EQ(2, GetOrderOfRequest(2));
1001 EXPECT_EQ(3, GetOrderOfRequest(3));
1002 EXPECT_EQ(4, GetOrderOfRequest(4));
1004 // Request ("b", 7) has the highest priority, but we can't make new socket for
1005 // group "b", because it has reached the per-group limit. Then we make
1006 // socket for ("c", 6), because it has higher priority than ("a", 4),
1007 // and we still can't make a socket for group "b".
1008 EXPECT_EQ(5, GetOrderOfRequest(5));
1009 EXPECT_EQ(6, GetOrderOfRequest(6));
1010 EXPECT_EQ(7, GetOrderOfRequest(7));
1012 // Make sure we test order of all requests made.
1013 EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds
, GetOrderOfRequest(8));
1016 // Make sure that we count connecting sockets against the total limit.
1017 TEST_F(ClientSocketPoolBaseTest
, TotalLimitCountsConnectingSockets
) {
1018 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
1020 EXPECT_EQ(OK
, StartRequest("a", DEFAULT_PRIORITY
));
1021 EXPECT_EQ(OK
, StartRequest("b", DEFAULT_PRIORITY
));
1022 EXPECT_EQ(OK
, StartRequest("c", DEFAULT_PRIORITY
));
1024 // Create one asynchronous request.
1025 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
1026 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("d", DEFAULT_PRIORITY
));
1028 // We post all of our delayed tasks with a 2ms delay. I.e. they don't
1029 // actually become pending until 2ms after they have been created. In order
1030 // to flush all tasks, we need to wait so that we know there are no
1031 // soon-to-be-pending tasks waiting.
1032 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10));
1033 base::MessageLoop::current()->RunUntilIdle();
1035 // The next synchronous request should wait for its turn.
1036 connect_job_factory_
->set_job_type(TestConnectJob::kMockJob
);
1037 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("e", DEFAULT_PRIORITY
));
1039 ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE
);
1041 EXPECT_EQ(static_cast<int>(requests_size()),
1042 client_socket_factory_
.allocation_count());
1044 EXPECT_EQ(1, GetOrderOfRequest(1));
1045 EXPECT_EQ(2, GetOrderOfRequest(2));
1046 EXPECT_EQ(3, GetOrderOfRequest(3));
1047 EXPECT_EQ(4, GetOrderOfRequest(4));
1048 EXPECT_EQ(5, GetOrderOfRequest(5));
1050 // Make sure we test order of all requests made.
1051 EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds
, GetOrderOfRequest(6));
1054 TEST_F(ClientSocketPoolBaseTest
, CorrectlyCountStalledGroups
) {
1055 CreatePool(kDefaultMaxSockets
, kDefaultMaxSockets
);
1056 connect_job_factory_
->set_job_type(TestConnectJob::kMockJob
);
1058 EXPECT_EQ(OK
, StartRequest("a", DEFAULT_PRIORITY
));
1059 EXPECT_EQ(OK
, StartRequest("a", DEFAULT_PRIORITY
));
1060 EXPECT_EQ(OK
, StartRequest("a", DEFAULT_PRIORITY
));
1061 EXPECT_EQ(OK
, StartRequest("a", DEFAULT_PRIORITY
));
1063 connect_job_factory_
->set_job_type(TestConnectJob::kMockWaitingJob
);
1065 EXPECT_EQ(kDefaultMaxSockets
, client_socket_factory_
.allocation_count());
1067 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("b", DEFAULT_PRIORITY
));
1068 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("c", DEFAULT_PRIORITY
));
1070 EXPECT_EQ(kDefaultMaxSockets
, client_socket_factory_
.allocation_count());
1072 EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE
));
1073 EXPECT_EQ(kDefaultMaxSockets
+ 1, client_socket_factory_
.allocation_count());
1074 EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE
));
1075 EXPECT_EQ(kDefaultMaxSockets
+ 2, client_socket_factory_
.allocation_count());
1076 EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE
));
1077 EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE
));
1078 EXPECT_EQ(kDefaultMaxSockets
+ 2, client_socket_factory_
.allocation_count());
1081 TEST_F(ClientSocketPoolBaseTest
, StallAndThenCancelAndTriggerAvailableSocket
) {
1082 CreatePool(kDefaultMaxSockets
, kDefaultMaxSockets
);
1083 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
1085 ClientSocketHandle handle
;
1086 TestCompletionCallback callback
;
1087 EXPECT_EQ(ERR_IO_PENDING
,
1091 callback
.callback(),
1095 ClientSocketHandle handles
[4];
1096 for (size_t i
= 0; i
< arraysize(handles
); ++i
) {
1097 TestCompletionCallback callback
;
1098 EXPECT_EQ(ERR_IO_PENDING
,
1099 handles
[i
].Init("b",
1102 callback
.callback(),
1107 // One will be stalled, cancel all the handles now.
1108 // This should hit the OnAvailableSocketSlot() code where we previously had
1109 // stalled groups, but no longer have any.
1110 for (size_t i
= 0; i
< arraysize(handles
); ++i
)
1114 TEST_F(ClientSocketPoolBaseTest
, CancelStalledSocketAtSocketLimit
) {
1115 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
1116 connect_job_factory_
->set_job_type(TestConnectJob::kMockJob
);
1119 ClientSocketHandle handles
[kDefaultMaxSockets
];
1120 TestCompletionCallback callbacks
[kDefaultMaxSockets
];
1121 for (int i
= 0; i
< kDefaultMaxSockets
; ++i
) {
1122 EXPECT_EQ(OK
, handles
[i
].Init(base::IntToString(i
),
1125 callbacks
[i
].callback(),
1130 // Force a stalled group.
1131 ClientSocketHandle stalled_handle
;
1132 TestCompletionCallback callback
;
1133 EXPECT_EQ(ERR_IO_PENDING
, stalled_handle
.Init("foo",
1136 callback
.callback(),
1140 // Cancel the stalled request.
1141 stalled_handle
.Reset();
1143 EXPECT_EQ(kDefaultMaxSockets
, client_socket_factory_
.allocation_count());
1144 EXPECT_EQ(0, pool_
->IdleSocketCount());
1146 // Dropping out of scope will close all handles and return them to idle.
1149 EXPECT_EQ(kDefaultMaxSockets
, client_socket_factory_
.allocation_count());
1150 EXPECT_EQ(kDefaultMaxSockets
, pool_
->IdleSocketCount());
1153 TEST_F(ClientSocketPoolBaseTest
, CancelPendingSocketAtSocketLimit
) {
1154 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
1155 connect_job_factory_
->set_job_type(TestConnectJob::kMockWaitingJob
);
1158 ClientSocketHandle handles
[kDefaultMaxSockets
];
1159 for (int i
= 0; i
< kDefaultMaxSockets
; ++i
) {
1160 TestCompletionCallback callback
;
1161 EXPECT_EQ(ERR_IO_PENDING
, handles
[i
].Init(base::IntToString(i
),
1164 callback
.callback(),
1169 // Force a stalled group.
1170 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
1171 ClientSocketHandle stalled_handle
;
1172 TestCompletionCallback callback
;
1173 EXPECT_EQ(ERR_IO_PENDING
, stalled_handle
.Init("foo",
1176 callback
.callback(),
1180 // Since it is stalled, it should have no connect jobs.
1181 EXPECT_EQ(0, pool_
->NumConnectJobsInGroup("foo"));
1182 EXPECT_EQ(0, pool_
->NumUnassignedConnectJobsInGroup("foo"));
1184 // Cancel the stalled request.
1187 // Now we should have a connect job.
1188 EXPECT_EQ(1, pool_
->NumConnectJobsInGroup("foo"));
1189 EXPECT_EQ(0, pool_
->NumUnassignedConnectJobsInGroup("foo"));
1191 // The stalled socket should connect.
1192 EXPECT_EQ(OK
, callback
.WaitForResult());
1194 EXPECT_EQ(kDefaultMaxSockets
+ 1,
1195 client_socket_factory_
.allocation_count());
1196 EXPECT_EQ(0, pool_
->IdleSocketCount());
1197 EXPECT_EQ(0, pool_
->NumConnectJobsInGroup("foo"));
1198 EXPECT_EQ(0, pool_
->NumUnassignedConnectJobsInGroup("foo"));
1200 // Dropping out of scope will close all handles and return them to idle.
1203 EXPECT_EQ(1, pool_
->IdleSocketCount());
1206 TEST_F(ClientSocketPoolBaseTest
, WaitForStalledSocketAtSocketLimit
) {
1207 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
1208 connect_job_factory_
->set_job_type(TestConnectJob::kMockJob
);
1210 ClientSocketHandle stalled_handle
;
1211 TestCompletionCallback callback
;
1213 EXPECT_FALSE(pool_
->IsStalled());
1214 ClientSocketHandle handles
[kDefaultMaxSockets
];
1215 for (int i
= 0; i
< kDefaultMaxSockets
; ++i
) {
1216 TestCompletionCallback callback
;
1217 EXPECT_EQ(OK
, handles
[i
].Init(base::StringPrintf(
1221 callback
.callback(),
1226 EXPECT_EQ(kDefaultMaxSockets
, client_socket_factory_
.allocation_count());
1227 EXPECT_EQ(0, pool_
->IdleSocketCount());
1228 EXPECT_FALSE(pool_
->IsStalled());
1230 // Now we will hit the socket limit.
1231 EXPECT_EQ(ERR_IO_PENDING
, stalled_handle
.Init("foo",
1234 callback
.callback(),
1237 EXPECT_TRUE(pool_
->IsStalled());
1239 // Dropping out of scope will close all handles and return them to idle.
1242 // But if we wait for it, the released idle sockets will be closed in
1243 // preference of the waiting request.
1244 EXPECT_EQ(OK
, callback
.WaitForResult());
1246 EXPECT_EQ(kDefaultMaxSockets
+ 1, client_socket_factory_
.allocation_count());
1247 EXPECT_EQ(3, pool_
->IdleSocketCount());
1250 // Regression test for http://crbug.com/40952.
1251 TEST_F(ClientSocketPoolBaseTest
, CloseIdleSocketAtSocketLimitDeleteGroup
) {
1252 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
1253 pool_
->EnableConnectBackupJobs();
1254 connect_job_factory_
->set_job_type(TestConnectJob::kMockJob
);
1256 for (int i
= 0; i
< kDefaultMaxSockets
; ++i
) {
1257 ClientSocketHandle handle
;
1258 TestCompletionCallback callback
;
1259 EXPECT_EQ(OK
, handle
.Init(base::IntToString(i
),
1262 callback
.callback(),
1267 // Flush all the DoReleaseSocket tasks.
1268 base::MessageLoop::current()->RunUntilIdle();
1270 // Stall a group. Set a pending job so it'll trigger a backup job if we don't
1272 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
1273 ClientSocketHandle handle
;
1274 TestCompletionCallback callback
;
1276 // "0" is special here, since it should be the first entry in the sorted map,
1277 // which is the one which we would close an idle socket for. We shouldn't
1278 // close an idle socket though, since we should reuse the idle socket.
1279 EXPECT_EQ(OK
, handle
.Init("0",
1282 callback
.callback(),
1286 EXPECT_EQ(kDefaultMaxSockets
, client_socket_factory_
.allocation_count());
1287 EXPECT_EQ(kDefaultMaxSockets
- 1, pool_
->IdleSocketCount());
1290 TEST_F(ClientSocketPoolBaseTest
, PendingRequests
) {
1291 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
1293 EXPECT_EQ(OK
, StartRequest("a", DEFAULT_PRIORITY
));
1294 EXPECT_EQ(OK
, StartRequest("a", DEFAULT_PRIORITY
));
1295 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", IDLE
));
1296 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", LOWEST
));
1297 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", MEDIUM
));
1298 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", HIGHEST
));
1299 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", LOW
));
1300 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", LOWEST
));
1302 ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE
);
1304 EXPECT_EQ(kDefaultMaxSocketsPerGroup
,
1305 client_socket_factory_
.allocation_count());
1306 EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup
,
1307 completion_count());
1309 EXPECT_EQ(1, GetOrderOfRequest(1));
1310 EXPECT_EQ(2, GetOrderOfRequest(2));
1311 EXPECT_EQ(8, GetOrderOfRequest(3));
1312 EXPECT_EQ(6, GetOrderOfRequest(4));
1313 EXPECT_EQ(4, GetOrderOfRequest(5));
1314 EXPECT_EQ(3, GetOrderOfRequest(6));
1315 EXPECT_EQ(5, GetOrderOfRequest(7));
1316 EXPECT_EQ(7, GetOrderOfRequest(8));
1318 // Make sure we test order of all requests made.
1319 EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds
, GetOrderOfRequest(9));
1322 TEST_F(ClientSocketPoolBaseTest
, PendingRequests_NoKeepAlive
) {
1323 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
1325 EXPECT_EQ(OK
, StartRequest("a", DEFAULT_PRIORITY
));
1326 EXPECT_EQ(OK
, StartRequest("a", DEFAULT_PRIORITY
));
1327 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", LOWEST
));
1328 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", MEDIUM
));
1329 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", HIGHEST
));
1330 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", LOW
));
1331 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", LOWEST
));
1333 ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE
);
1335 for (size_t i
= kDefaultMaxSocketsPerGroup
; i
< requests_size(); ++i
)
1336 EXPECT_EQ(OK
, request(i
)->WaitForResult());
1338 EXPECT_EQ(static_cast<int>(requests_size()),
1339 client_socket_factory_
.allocation_count());
1340 EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup
,
1341 completion_count());
1344 // This test will start up a RequestSocket() and then immediately Cancel() it.
1345 // The pending connect job will be cancelled and should not call back into
1346 // ClientSocketPoolBase.
1347 TEST_F(ClientSocketPoolBaseTest
, CancelRequestClearGroup
) {
1348 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
1350 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
1351 ClientSocketHandle handle
;
1352 TestCompletionCallback callback
;
1353 EXPECT_EQ(ERR_IO_PENDING
, handle
.Init("a",
1356 callback
.callback(),
1362 TEST_F(ClientSocketPoolBaseTest
, ConnectCancelConnect
) {
1363 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
1365 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
1366 ClientSocketHandle handle
;
1367 TestCompletionCallback callback
;
1369 EXPECT_EQ(ERR_IO_PENDING
, handle
.Init("a",
1372 callback
.callback(),
1378 TestCompletionCallback callback2
;
1379 EXPECT_EQ(ERR_IO_PENDING
,
1383 callback2
.callback(),
1387 EXPECT_EQ(OK
, callback2
.WaitForResult());
1388 EXPECT_FALSE(callback
.have_result());
1393 TEST_F(ClientSocketPoolBaseTest
, CancelRequest
) {
1394 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
1396 EXPECT_EQ(OK
, StartRequest("a", DEFAULT_PRIORITY
));
1397 EXPECT_EQ(OK
, StartRequest("a", DEFAULT_PRIORITY
));
1398 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", LOWEST
));
1399 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", MEDIUM
));
1400 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", HIGHEST
));
1401 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", LOW
));
1402 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", LOWEST
));
1404 // Cancel a request.
1405 size_t index_to_cancel
= kDefaultMaxSocketsPerGroup
+ 2;
1406 EXPECT_FALSE((*requests())[index_to_cancel
]->handle()->is_initialized());
1407 (*requests())[index_to_cancel
]->handle()->Reset();
1409 ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE
);
1411 EXPECT_EQ(kDefaultMaxSocketsPerGroup
,
1412 client_socket_factory_
.allocation_count());
1413 EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup
- 1,
1414 completion_count());
1416 EXPECT_EQ(1, GetOrderOfRequest(1));
1417 EXPECT_EQ(2, GetOrderOfRequest(2));
1418 EXPECT_EQ(5, GetOrderOfRequest(3));
1419 EXPECT_EQ(3, GetOrderOfRequest(4));
1420 EXPECT_EQ(ClientSocketPoolTest::kRequestNotFound
,
1421 GetOrderOfRequest(5)); // Canceled request.
1422 EXPECT_EQ(4, GetOrderOfRequest(6));
1423 EXPECT_EQ(6, GetOrderOfRequest(7));
1425 // Make sure we test order of all requests made.
1426 EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds
, GetOrderOfRequest(8));
1429 class RequestSocketCallback
: public TestCompletionCallbackBase
{
1431 RequestSocketCallback(ClientSocketHandle
* handle
,
1432 TestClientSocketPool
* pool
,
1433 TestConnectJobFactory
* test_connect_job_factory
,
1434 TestConnectJob::JobType next_job_type
)
1437 within_callback_(false),
1438 test_connect_job_factory_(test_connect_job_factory
),
1439 next_job_type_(next_job_type
),
1440 callback_(base::Bind(&RequestSocketCallback::OnComplete
,
1441 base::Unretained(this))) {
1444 ~RequestSocketCallback() override
{}
1446 const CompletionCallback
& callback() const { return callback_
; }
1449 void OnComplete(int result
) {
1451 ASSERT_EQ(OK
, result
);
1453 if (!within_callback_
) {
1454 test_connect_job_factory_
->set_job_type(next_job_type_
);
1456 // Don't allow reuse of the socket. Disconnect it and then release it and
1457 // run through the MessageLoop once to get it completely released.
1458 handle_
->socket()->Disconnect();
1461 // TODO: Resolve conflicting intentions of stopping recursion with the
1462 // |!within_callback_| test (above) and the call to |RunUntilIdle()|
1463 // below. http://crbug.com/114130.
1464 base::MessageLoop::ScopedNestableTaskAllower
allow(
1465 base::MessageLoop::current());
1466 base::MessageLoop::current()->RunUntilIdle();
1468 within_callback_
= true;
1469 TestCompletionCallback next_job_callback
;
1470 scoped_refptr
<TestSocketParams
> params(
1471 new TestSocketParams(false /* ignore_limits */));
1472 int rv
= handle_
->Init("a",
1475 next_job_callback
.callback(),
1478 switch (next_job_type_
) {
1479 case TestConnectJob::kMockJob
:
1482 case TestConnectJob::kMockPendingJob
:
1483 EXPECT_EQ(ERR_IO_PENDING
, rv
);
1485 // For pending jobs, wait for new socket to be created. This makes
1486 // sure there are no more pending operations nor any unclosed sockets
1487 // when the test finishes.
1488 // We need to give it a little bit of time to run, so that all the
1489 // operations that happen on timers (e.g. cleanup of idle
1490 // connections) can execute.
1492 base::MessageLoop::ScopedNestableTaskAllower
allow(
1493 base::MessageLoop::current());
1494 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10));
1495 EXPECT_EQ(OK
, next_job_callback
.WaitForResult());
1499 FAIL() << "Unexpected job type: " << next_job_type_
;
1505 ClientSocketHandle
* const handle_
;
1506 TestClientSocketPool
* const pool_
;
1507 bool within_callback_
;
1508 TestConnectJobFactory
* const test_connect_job_factory_
;
1509 TestConnectJob::JobType next_job_type_
;
1510 CompletionCallback callback_
;
1513 TEST_F(ClientSocketPoolBaseTest
, RequestPendingJobTwice
) {
1514 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
1516 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
1517 ClientSocketHandle handle
;
1518 RequestSocketCallback
callback(
1519 &handle
, pool_
.get(), connect_job_factory_
,
1520 TestConnectJob::kMockPendingJob
);
1521 int rv
= handle
.Init("a",
1524 callback
.callback(),
1527 ASSERT_EQ(ERR_IO_PENDING
, rv
);
1529 EXPECT_EQ(OK
, callback
.WaitForResult());
1532 TEST_F(ClientSocketPoolBaseTest
, RequestPendingJobThenSynchronous
) {
1533 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
1535 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
1536 ClientSocketHandle handle
;
1537 RequestSocketCallback
callback(
1538 &handle
, pool_
.get(), connect_job_factory_
, TestConnectJob::kMockJob
);
1539 int rv
= handle
.Init("a",
1542 callback
.callback(),
1545 ASSERT_EQ(ERR_IO_PENDING
, rv
);
1547 EXPECT_EQ(OK
, callback
.WaitForResult());
1550 // Make sure that pending requests get serviced after active requests get
1552 TEST_F(ClientSocketPoolBaseTest
, CancelActiveRequestWithPendingRequests
) {
1553 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
1555 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
1557 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", DEFAULT_PRIORITY
));
1558 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", DEFAULT_PRIORITY
));
1559 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", DEFAULT_PRIORITY
));
1560 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", DEFAULT_PRIORITY
));
1561 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", DEFAULT_PRIORITY
));
1562 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", DEFAULT_PRIORITY
));
1563 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", DEFAULT_PRIORITY
));
1565 // Now, kDefaultMaxSocketsPerGroup requests should be active.
1566 // Let's cancel them.
1567 for (int i
= 0; i
< kDefaultMaxSocketsPerGroup
; ++i
) {
1568 ASSERT_FALSE(request(i
)->handle()->is_initialized());
1569 request(i
)->handle()->Reset();
1572 // Let's wait for the rest to complete now.
1573 for (size_t i
= kDefaultMaxSocketsPerGroup
; i
< requests_size(); ++i
) {
1574 EXPECT_EQ(OK
, request(i
)->WaitForResult());
1575 request(i
)->handle()->Reset();
1578 EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup
,
1579 completion_count());
1582 // Make sure that pending requests get serviced after active requests fail.
1583 TEST_F(ClientSocketPoolBaseTest
, FailingActiveRequestWithPendingRequests
) {
1584 const size_t kMaxSockets
= 5;
1585 CreatePool(kMaxSockets
, kDefaultMaxSocketsPerGroup
);
1587 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingFailingJob
);
1589 const size_t kNumberOfRequests
= 2 * kDefaultMaxSocketsPerGroup
+ 1;
1590 ASSERT_LE(kNumberOfRequests
, kMaxSockets
); // Otherwise the test will hang.
1592 // Queue up all the requests
1593 for (size_t i
= 0; i
< kNumberOfRequests
; ++i
)
1594 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", DEFAULT_PRIORITY
));
1596 for (size_t i
= 0; i
< kNumberOfRequests
; ++i
)
1597 EXPECT_EQ(ERR_CONNECTION_FAILED
, request(i
)->WaitForResult());
1600 TEST_F(ClientSocketPoolBaseTest
, CancelActiveRequestThenRequestSocket
) {
1601 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
1603 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
1605 ClientSocketHandle handle
;
1606 TestCompletionCallback callback
;
1607 int rv
= handle
.Init("a",
1610 callback
.callback(),
1613 EXPECT_EQ(ERR_IO_PENDING
, rv
);
1615 // Cancel the active request.
1618 rv
= handle
.Init("a",
1621 callback
.callback(),
1624 EXPECT_EQ(ERR_IO_PENDING
, rv
);
1625 EXPECT_EQ(OK
, callback
.WaitForResult());
1627 EXPECT_FALSE(handle
.is_reused());
1628 TestLoadTimingInfoConnectedNotReused(handle
);
1629 EXPECT_EQ(2, client_socket_factory_
.allocation_count());
1632 // Regression test for http://crbug.com/17985.
1633 TEST_F(ClientSocketPoolBaseTest
, GroupWithPendingRequestsIsNotEmpty
) {
1634 const int kMaxSockets
= 3;
1635 const int kMaxSocketsPerGroup
= 2;
1636 CreatePool(kMaxSockets
, kMaxSocketsPerGroup
);
1638 const RequestPriority kHighPriority
= HIGHEST
;
1640 EXPECT_EQ(OK
, StartRequest("a", DEFAULT_PRIORITY
));
1641 EXPECT_EQ(OK
, StartRequest("a", DEFAULT_PRIORITY
));
1643 // This is going to be a pending request in an otherwise empty group.
1644 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", DEFAULT_PRIORITY
));
1646 // Reach the maximum socket limit.
1647 EXPECT_EQ(OK
, StartRequest("b", DEFAULT_PRIORITY
));
1649 // Create a stalled group with high priorities.
1650 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("c", kHighPriority
));
1651 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("c", kHighPriority
));
1653 // Release the first two sockets from "a". Because this is a keepalive,
1654 // the first release will unblock the pending request for "a". The
1655 // second release will unblock a request for "c", becaue it is the next
1656 // high priority socket.
1657 EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE
));
1658 EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE
));
1660 // Closing idle sockets should not get us into trouble, but in the bug
1661 // we were hitting a CHECK here.
1662 EXPECT_EQ(0, pool_
->IdleSocketCountInGroup("a"));
1663 pool_
->CloseIdleSockets();
1665 // Run the released socket wakeups.
1666 base::MessageLoop::current()->RunUntilIdle();
1669 TEST_F(ClientSocketPoolBaseTest
, BasicAsynchronous
) {
1670 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
1672 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
1673 ClientSocketHandle handle
;
1674 TestCompletionCallback callback
;
1675 CapturingBoundNetLog log
;
1676 int rv
= handle
.Init("a",
1679 callback
.callback(),
1682 EXPECT_EQ(ERR_IO_PENDING
, rv
);
1683 EXPECT_EQ(LOAD_STATE_CONNECTING
, pool_
->GetLoadState("a", &handle
));
1684 TestLoadTimingInfoNotConnected(handle
);
1686 EXPECT_EQ(OK
, callback
.WaitForResult());
1687 EXPECT_TRUE(handle
.is_initialized());
1688 EXPECT_TRUE(handle
.socket());
1689 TestLoadTimingInfoConnectedNotReused(handle
);
1692 TestLoadTimingInfoNotConnected(handle
);
1694 CapturingNetLog::CapturedEntryList entries
;
1695 log
.GetEntries(&entries
);
1697 EXPECT_EQ(4u, entries
.size());
1698 EXPECT_TRUE(LogContainsBeginEvent(
1699 entries
, 0, NetLog::TYPE_SOCKET_POOL
));
1700 EXPECT_TRUE(LogContainsEvent(
1701 entries
, 1, NetLog::TYPE_SOCKET_POOL_BOUND_TO_CONNECT_JOB
,
1702 NetLog::PHASE_NONE
));
1703 EXPECT_TRUE(LogContainsEvent(
1704 entries
, 2, NetLog::TYPE_SOCKET_POOL_BOUND_TO_SOCKET
,
1705 NetLog::PHASE_NONE
));
1706 EXPECT_TRUE(LogContainsEndEvent(
1707 entries
, 3, NetLog::TYPE_SOCKET_POOL
));
1710 TEST_F(ClientSocketPoolBaseTest
,
1711 InitConnectionAsynchronousFailure
) {
1712 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
1714 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingFailingJob
);
1715 ClientSocketHandle handle
;
1716 TestCompletionCallback callback
;
1717 CapturingBoundNetLog log
;
1718 // Set the additional error state members to ensure that they get cleared.
1719 handle
.set_is_ssl_error(true);
1720 HttpResponseInfo info
;
1721 info
.headers
= new HttpResponseHeaders(std::string());
1722 handle
.set_ssl_error_response_info(info
);
1723 EXPECT_EQ(ERR_IO_PENDING
, handle
.Init("a",
1726 callback
.callback(),
1729 EXPECT_EQ(LOAD_STATE_CONNECTING
, pool_
->GetLoadState("a", &handle
));
1730 EXPECT_EQ(ERR_CONNECTION_FAILED
, callback
.WaitForResult());
1731 EXPECT_FALSE(handle
.is_ssl_error());
1732 EXPECT_TRUE(handle
.ssl_error_response_info().headers
.get() == NULL
);
1734 CapturingNetLog::CapturedEntryList entries
;
1735 log
.GetEntries(&entries
);
1737 EXPECT_EQ(3u, entries
.size());
1738 EXPECT_TRUE(LogContainsBeginEvent(
1739 entries
, 0, NetLog::TYPE_SOCKET_POOL
));
1740 EXPECT_TRUE(LogContainsEvent(
1741 entries
, 1, NetLog::TYPE_SOCKET_POOL_BOUND_TO_CONNECT_JOB
,
1742 NetLog::PHASE_NONE
));
1743 EXPECT_TRUE(LogContainsEndEvent(
1744 entries
, 2, NetLog::TYPE_SOCKET_POOL
));
1747 // Check that an async ConnectJob failure does not result in creation of a new
1748 // ConnectJob when there's another pending request also waiting on its own
1749 // ConnectJob. See http://crbug.com/463960.
1750 TEST_F(ClientSocketPoolBaseTest
, AsyncFailureWithPendingRequestWithJob
) {
1752 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingFailingJob
);
1754 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", DEFAULT_PRIORITY
));
1755 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", DEFAULT_PRIORITY
));
1757 EXPECT_EQ(ERR_CONNECTION_FAILED
, request(0)->WaitForResult());
1758 EXPECT_EQ(ERR_CONNECTION_FAILED
, request(1)->WaitForResult());
1760 EXPECT_EQ(2, client_socket_factory_
.allocation_count());
1763 TEST_F(ClientSocketPoolBaseTest
, TwoRequestsCancelOne
) {
1764 // TODO(eroman): Add back the log expectations! Removed them because the
1765 // ordering is difficult, and some may fire during destructor.
1766 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
1768 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
1769 ClientSocketHandle handle
;
1770 TestCompletionCallback callback
;
1771 ClientSocketHandle handle2
;
1772 TestCompletionCallback callback2
;
1774 EXPECT_EQ(ERR_IO_PENDING
,
1778 callback
.callback(),
1781 CapturingBoundNetLog log2
;
1782 EXPECT_EQ(ERR_IO_PENDING
,
1786 callback2
.callback(),
1793 // At this point, request 2 is just waiting for the connect job to finish.
1795 EXPECT_EQ(OK
, callback2
.WaitForResult());
1798 // Now request 2 has actually finished.
1799 // TODO(eroman): Add back log expectations.
1802 TEST_F(ClientSocketPoolBaseTest
, CancelRequestLimitsJobs
) {
1803 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
1805 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
1807 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", LOWEST
));
1808 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", LOW
));
1809 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", MEDIUM
));
1810 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", HIGHEST
));
1812 EXPECT_EQ(kDefaultMaxSocketsPerGroup
, pool_
->NumConnectJobsInGroup("a"));
1813 (*requests())[2]->handle()->Reset();
1814 (*requests())[3]->handle()->Reset();
1815 EXPECT_EQ(kDefaultMaxSocketsPerGroup
, pool_
->NumConnectJobsInGroup("a"));
1817 (*requests())[1]->handle()->Reset();
1818 EXPECT_EQ(kDefaultMaxSocketsPerGroup
, pool_
->NumConnectJobsInGroup("a"));
1820 (*requests())[0]->handle()->Reset();
1821 EXPECT_EQ(kDefaultMaxSocketsPerGroup
, pool_
->NumConnectJobsInGroup("a"));
1824 // When requests and ConnectJobs are not coupled, the request will get serviced
1825 // by whatever comes first.
1826 TEST_F(ClientSocketPoolBaseTest
, ReleaseSockets
) {
1827 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
1829 // Start job 1 (async OK)
1830 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
1832 std::vector
<TestSocketRequest
*> request_order
;
1833 size_t completion_count
; // unused
1834 TestSocketRequest
req1(&request_order
, &completion_count
);
1835 int rv
= req1
.handle()->Init("a",
1838 req1
.callback(), pool_
.get(),
1840 EXPECT_EQ(ERR_IO_PENDING
, rv
);
1841 EXPECT_EQ(OK
, req1
.WaitForResult());
1843 // Job 1 finished OK. Start job 2 (also async OK). Request 3 is pending
1845 connect_job_factory_
->set_job_type(TestConnectJob::kMockWaitingJob
);
1847 TestSocketRequest
req2(&request_order
, &completion_count
);
1848 rv
= req2
.handle()->Init("a",
1854 EXPECT_EQ(ERR_IO_PENDING
, rv
);
1855 TestSocketRequest
req3(&request_order
, &completion_count
);
1856 rv
= req3
.handle()->Init("a",
1862 EXPECT_EQ(ERR_IO_PENDING
, rv
);
1864 // Both Requests 2 and 3 are pending. We release socket 1 which should
1865 // service request 2. Request 3 should still be waiting.
1866 req1
.handle()->Reset();
1867 // Run the released socket wakeups.
1868 base::MessageLoop::current()->RunUntilIdle();
1869 ASSERT_TRUE(req2
.handle()->socket());
1870 EXPECT_EQ(OK
, req2
.WaitForResult());
1871 EXPECT_FALSE(req3
.handle()->socket());
1873 // Signal job 2, which should service request 3.
1875 client_socket_factory_
.SignalJobs();
1876 EXPECT_EQ(OK
, req3
.WaitForResult());
1878 ASSERT_EQ(3U, request_order
.size());
1879 EXPECT_EQ(&req1
, request_order
[0]);
1880 EXPECT_EQ(&req2
, request_order
[1]);
1881 EXPECT_EQ(&req3
, request_order
[2]);
1882 EXPECT_EQ(0, pool_
->IdleSocketCountInGroup("a"));
1885 // The requests are not coupled to the jobs. So, the requests should finish in
1886 // their priority / insertion order.
1887 TEST_F(ClientSocketPoolBaseTest
, PendingJobCompletionOrder
) {
1888 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
1889 // First two jobs are async.
1890 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingFailingJob
);
1892 std::vector
<TestSocketRequest
*> request_order
;
1893 size_t completion_count
; // unused
1894 TestSocketRequest
req1(&request_order
, &completion_count
);
1895 int rv
= req1
.handle()->Init("a",
1901 EXPECT_EQ(ERR_IO_PENDING
, rv
);
1903 TestSocketRequest
req2(&request_order
, &completion_count
);
1904 rv
= req2
.handle()->Init("a",
1910 EXPECT_EQ(ERR_IO_PENDING
, rv
);
1912 // The pending job is sync.
1913 connect_job_factory_
->set_job_type(TestConnectJob::kMockJob
);
1915 TestSocketRequest
req3(&request_order
, &completion_count
);
1916 rv
= req3
.handle()->Init("a",
1922 EXPECT_EQ(ERR_IO_PENDING
, rv
);
1924 EXPECT_EQ(ERR_CONNECTION_FAILED
, req1
.WaitForResult());
1925 EXPECT_EQ(OK
, req2
.WaitForResult());
1926 EXPECT_EQ(ERR_CONNECTION_FAILED
, req3
.WaitForResult());
1928 ASSERT_EQ(3U, request_order
.size());
1929 EXPECT_EQ(&req1
, request_order
[0]);
1930 EXPECT_EQ(&req2
, request_order
[1]);
1931 EXPECT_EQ(&req3
, request_order
[2]);
1934 // Test GetLoadState in the case there's only one socket request.
1935 TEST_F(ClientSocketPoolBaseTest
, LoadStateOneRequest
) {
1936 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
1937 connect_job_factory_
->set_job_type(TestConnectJob::kMockWaitingJob
);
1939 ClientSocketHandle handle
;
1940 TestCompletionCallback callback
;
1941 int rv
= handle
.Init("a",
1944 callback
.callback(),
1947 EXPECT_EQ(ERR_IO_PENDING
, rv
);
1948 EXPECT_EQ(LOAD_STATE_CONNECTING
, handle
.GetLoadState());
1950 client_socket_factory_
.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE
);
1951 EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE
, handle
.GetLoadState());
1953 // No point in completing the connection, since ClientSocketHandles only
1954 // expect the LoadState to be checked while connecting.
1957 // Test GetLoadState in the case there are two socket requests.
1958 TEST_F(ClientSocketPoolBaseTest
, LoadStateTwoRequests
) {
1960 connect_job_factory_
->set_job_type(TestConnectJob::kMockWaitingJob
);
1962 ClientSocketHandle handle
;
1963 TestCompletionCallback callback
;
1964 int rv
= handle
.Init("a",
1967 callback
.callback(),
1970 EXPECT_EQ(ERR_IO_PENDING
, rv
);
1972 ClientSocketHandle handle2
;
1973 TestCompletionCallback callback2
;
1974 rv
= handle2
.Init("a",
1977 callback2
.callback(),
1980 EXPECT_EQ(ERR_IO_PENDING
, rv
);
1982 // If the first Job is in an earlier state than the second, the state of
1983 // the second job should be used for both handles.
1984 client_socket_factory_
.SetJobLoadState(0, LOAD_STATE_RESOLVING_HOST
);
1985 EXPECT_EQ(LOAD_STATE_CONNECTING
, handle
.GetLoadState());
1986 EXPECT_EQ(LOAD_STATE_CONNECTING
, handle2
.GetLoadState());
1988 // If the second Job is in an earlier state than the second, the state of
1989 // the first job should be used for both handles.
1990 client_socket_factory_
.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE
);
1991 // One request is farther
1992 EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE
, handle
.GetLoadState());
1993 EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE
, handle2
.GetLoadState());
1995 // Farthest along job connects and the first request gets the socket. The
1996 // second handle switches to the state of the remaining ConnectJob.
1997 client_socket_factory_
.SignalJob(0);
1998 EXPECT_EQ(OK
, callback
.WaitForResult());
1999 EXPECT_EQ(LOAD_STATE_CONNECTING
, handle2
.GetLoadState());
2002 // Test GetLoadState in the case the per-group limit is reached.
2003 TEST_F(ClientSocketPoolBaseTest
, LoadStateGroupLimit
) {
2005 connect_job_factory_
->set_job_type(TestConnectJob::kMockWaitingJob
);
2007 ClientSocketHandle handle
;
2008 TestCompletionCallback callback
;
2009 int rv
= handle
.Init("a",
2012 callback
.callback(),
2015 EXPECT_EQ(ERR_IO_PENDING
, rv
);
2016 EXPECT_EQ(LOAD_STATE_CONNECTING
, handle
.GetLoadState());
2018 // Request another socket from the same pool, buth with a higher priority.
2019 // The first request should now be stalled at the socket group limit.
2020 ClientSocketHandle handle2
;
2021 TestCompletionCallback callback2
;
2022 rv
= handle2
.Init("a",
2025 callback2
.callback(),
2028 EXPECT_EQ(ERR_IO_PENDING
, rv
);
2029 EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET
, handle
.GetLoadState());
2030 EXPECT_EQ(LOAD_STATE_CONNECTING
, handle2
.GetLoadState());
2032 // The first handle should remain stalled as the other socket goes through
2033 // the connect process.
2035 client_socket_factory_
.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE
);
2036 EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET
, handle
.GetLoadState());
2037 EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE
, handle2
.GetLoadState());
2039 client_socket_factory_
.SignalJob(0);
2040 EXPECT_EQ(OK
, callback2
.WaitForResult());
2041 EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET
, handle
.GetLoadState());
2043 // Closing the second socket should cause the stalled handle to finally get a
2045 handle2
.socket()->Disconnect();
2047 EXPECT_EQ(LOAD_STATE_CONNECTING
, handle
.GetLoadState());
2050 // Test GetLoadState in the case the per-pool limit is reached.
2051 TEST_F(ClientSocketPoolBaseTest
, LoadStatePoolLimit
) {
2053 connect_job_factory_
->set_job_type(TestConnectJob::kMockWaitingJob
);
2055 ClientSocketHandle handle
;
2056 TestCompletionCallback callback
;
2057 int rv
= handle
.Init("a",
2060 callback
.callback(),
2063 EXPECT_EQ(ERR_IO_PENDING
, rv
);
2065 // Request for socket from another pool.
2066 ClientSocketHandle handle2
;
2067 TestCompletionCallback callback2
;
2068 rv
= handle2
.Init("b",
2071 callback2
.callback(),
2074 EXPECT_EQ(ERR_IO_PENDING
, rv
);
2076 // Request another socket from the first pool. Request should stall at the
2077 // socket pool limit.
2078 ClientSocketHandle handle3
;
2079 TestCompletionCallback callback3
;
2080 rv
= handle3
.Init("a",
2083 callback2
.callback(),
2086 EXPECT_EQ(ERR_IO_PENDING
, rv
);
2088 // The third handle should remain stalled as the other sockets in its group
2089 // goes through the connect process.
2091 EXPECT_EQ(LOAD_STATE_CONNECTING
, handle
.GetLoadState());
2092 EXPECT_EQ(LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL
, handle3
.GetLoadState());
2094 client_socket_factory_
.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE
);
2095 EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE
, handle
.GetLoadState());
2096 EXPECT_EQ(LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL
, handle3
.GetLoadState());
2098 client_socket_factory_
.SignalJob(0);
2099 EXPECT_EQ(OK
, callback
.WaitForResult());
2100 EXPECT_EQ(LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL
, handle3
.GetLoadState());
2102 // Closing a socket should allow the stalled handle to finally get a new
2104 handle
.socket()->Disconnect();
2106 EXPECT_EQ(LOAD_STATE_CONNECTING
, handle3
.GetLoadState());
2109 TEST_F(ClientSocketPoolBaseTest
, Recoverable
) {
2110 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
2111 connect_job_factory_
->set_job_type(TestConnectJob::kMockRecoverableJob
);
2113 ClientSocketHandle handle
;
2114 TestCompletionCallback callback
;
2115 EXPECT_EQ(ERR_PROXY_AUTH_REQUESTED
,
2116 handle
.Init("a", params_
, DEFAULT_PRIORITY
, callback
.callback(),
2117 pool_
.get(), BoundNetLog()));
2118 EXPECT_TRUE(handle
.is_initialized());
2119 EXPECT_TRUE(handle
.socket());
2122 TEST_F(ClientSocketPoolBaseTest
, AsyncRecoverable
) {
2123 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
2125 connect_job_factory_
->set_job_type(
2126 TestConnectJob::kMockPendingRecoverableJob
);
2127 ClientSocketHandle handle
;
2128 TestCompletionCallback callback
;
2129 EXPECT_EQ(ERR_IO_PENDING
,
2133 callback
.callback(),
2136 EXPECT_EQ(LOAD_STATE_CONNECTING
, pool_
->GetLoadState("a", &handle
));
2137 EXPECT_EQ(ERR_PROXY_AUTH_REQUESTED
, callback
.WaitForResult());
2138 EXPECT_TRUE(handle
.is_initialized());
2139 EXPECT_TRUE(handle
.socket());
2142 TEST_F(ClientSocketPoolBaseTest
, AdditionalErrorStateSynchronous
) {
2143 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
2144 connect_job_factory_
->set_job_type(
2145 TestConnectJob::kMockAdditionalErrorStateJob
);
2147 ClientSocketHandle handle
;
2148 TestCompletionCallback callback
;
2149 EXPECT_EQ(ERR_CONNECTION_FAILED
,
2153 callback
.callback(),
2156 EXPECT_FALSE(handle
.is_initialized());
2157 EXPECT_FALSE(handle
.socket());
2158 EXPECT_TRUE(handle
.is_ssl_error());
2159 EXPECT_FALSE(handle
.ssl_error_response_info().headers
.get() == NULL
);
2162 TEST_F(ClientSocketPoolBaseTest
, AdditionalErrorStateAsynchronous
) {
2163 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
2165 connect_job_factory_
->set_job_type(
2166 TestConnectJob::kMockPendingAdditionalErrorStateJob
);
2167 ClientSocketHandle handle
;
2168 TestCompletionCallback callback
;
2169 EXPECT_EQ(ERR_IO_PENDING
,
2173 callback
.callback(),
2176 EXPECT_EQ(LOAD_STATE_CONNECTING
, pool_
->GetLoadState("a", &handle
));
2177 EXPECT_EQ(ERR_CONNECTION_FAILED
, callback
.WaitForResult());
2178 EXPECT_FALSE(handle
.is_initialized());
2179 EXPECT_FALSE(handle
.socket());
2180 EXPECT_TRUE(handle
.is_ssl_error());
2181 EXPECT_FALSE(handle
.ssl_error_response_info().headers
.get() == NULL
);
2184 // Make sure we can reuse sockets when the cleanup timer is disabled.
2185 TEST_F(ClientSocketPoolBaseTest
, DisableCleanupTimerReuse
) {
2186 // Disable cleanup timer.
2187 internal::ClientSocketPoolBaseHelper::set_cleanup_timer_enabled(false);
2189 CreatePoolWithIdleTimeouts(
2190 kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
,
2191 base::TimeDelta(), // Time out unused sockets immediately.
2192 base::TimeDelta::FromDays(1)); // Don't time out used sockets.
2194 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
2196 ClientSocketHandle handle
;
2197 TestCompletionCallback callback
;
2198 int rv
= handle
.Init("a",
2201 callback
.callback(),
2204 ASSERT_EQ(ERR_IO_PENDING
, rv
);
2205 EXPECT_EQ(LOAD_STATE_CONNECTING
, pool_
->GetLoadState("a", &handle
));
2206 ASSERT_EQ(OK
, callback
.WaitForResult());
2208 // Use and release the socket.
2209 EXPECT_EQ(1, handle
.socket()->Write(NULL
, 1, CompletionCallback()));
2210 TestLoadTimingInfoConnectedNotReused(handle
);
2213 // Should now have one idle socket.
2214 ASSERT_EQ(1, pool_
->IdleSocketCount());
2216 // Request a new socket. This should reuse the old socket and complete
2218 CapturingBoundNetLog log
;
2219 rv
= handle
.Init("a",
2222 CompletionCallback(),
2226 EXPECT_TRUE(handle
.is_reused());
2227 TestLoadTimingInfoConnectedReused(handle
);
2229 ASSERT_TRUE(pool_
->HasGroup("a"));
2230 EXPECT_EQ(0, pool_
->IdleSocketCountInGroup("a"));
2231 EXPECT_EQ(1, pool_
->NumActiveSocketsInGroup("a"));
2233 CapturingNetLog::CapturedEntryList entries
;
2234 log
.GetEntries(&entries
);
2235 EXPECT_TRUE(LogContainsEntryWithType(
2236 entries
, 1, NetLog::TYPE_SOCKET_POOL_REUSED_AN_EXISTING_SOCKET
));
2239 // Make sure we cleanup old unused sockets when the cleanup timer is disabled.
2240 TEST_F(ClientSocketPoolBaseTest
, DisableCleanupTimerNoReuse
) {
2241 // Disable cleanup timer.
2242 internal::ClientSocketPoolBaseHelper::set_cleanup_timer_enabled(false);
2244 CreatePoolWithIdleTimeouts(
2245 kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
,
2246 base::TimeDelta(), // Time out unused sockets immediately
2247 base::TimeDelta()); // Time out used sockets immediately
2249 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
2251 // Startup two mock pending connect jobs, which will sit in the MessageLoop.
2253 ClientSocketHandle handle
;
2254 TestCompletionCallback callback
;
2255 int rv
= handle
.Init("a",
2258 callback
.callback(),
2261 ASSERT_EQ(ERR_IO_PENDING
, rv
);
2262 EXPECT_EQ(LOAD_STATE_CONNECTING
, pool_
->GetLoadState("a", &handle
));
2264 ClientSocketHandle handle2
;
2265 TestCompletionCallback callback2
;
2266 rv
= handle2
.Init("a",
2269 callback2
.callback(),
2272 ASSERT_EQ(ERR_IO_PENDING
, rv
);
2273 EXPECT_EQ(LOAD_STATE_CONNECTING
, pool_
->GetLoadState("a", &handle2
));
2275 // Cancel one of the requests. Wait for the other, which will get the first
2276 // job. Release the socket. Run the loop again to make sure the second
2277 // socket is sitting idle and the first one is released (since ReleaseSocket()
2278 // just posts a DoReleaseSocket() task).
2281 ASSERT_EQ(OK
, callback2
.WaitForResult());
2283 EXPECT_EQ(1, handle2
.socket()->Write(NULL
, 1, CompletionCallback()));
2286 // We post all of our delayed tasks with a 2ms delay. I.e. they don't
2287 // actually become pending until 2ms after they have been created. In order
2288 // to flush all tasks, we need to wait so that we know there are no
2289 // soon-to-be-pending tasks waiting.
2290 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10));
2291 base::MessageLoop::current()->RunUntilIdle();
2293 // Both sockets should now be idle.
2294 ASSERT_EQ(2, pool_
->IdleSocketCount());
2296 // Request a new socket. This should cleanup the unused and timed out ones.
2297 // A new socket will be created rather than reusing the idle one.
2298 CapturingBoundNetLog log
;
2299 TestCompletionCallback callback3
;
2300 rv
= handle
.Init("a",
2303 callback3
.callback(),
2306 ASSERT_EQ(ERR_IO_PENDING
, rv
);
2307 ASSERT_EQ(OK
, callback3
.WaitForResult());
2308 EXPECT_FALSE(handle
.is_reused());
2310 // Make sure the idle socket is closed.
2311 ASSERT_TRUE(pool_
->HasGroup("a"));
2312 EXPECT_EQ(0, pool_
->IdleSocketCountInGroup("a"));
2313 EXPECT_EQ(1, pool_
->NumActiveSocketsInGroup("a"));
2315 CapturingNetLog::CapturedEntryList entries
;
2316 log
.GetEntries(&entries
);
2317 EXPECT_FALSE(LogContainsEntryWithType(
2318 entries
, 1, NetLog::TYPE_SOCKET_POOL_REUSED_AN_EXISTING_SOCKET
));
2321 TEST_F(ClientSocketPoolBaseTest
, CleanupTimedOutIdleSockets
) {
2322 CreatePoolWithIdleTimeouts(
2323 kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
,
2324 base::TimeDelta(), // Time out unused sockets immediately.
2325 base::TimeDelta::FromDays(1)); // Don't time out used sockets.
2327 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
2329 // Startup two mock pending connect jobs, which will sit in the MessageLoop.
2331 ClientSocketHandle handle
;
2332 TestCompletionCallback callback
;
2333 int rv
= handle
.Init("a",
2336 callback
.callback(),
2339 EXPECT_EQ(ERR_IO_PENDING
, rv
);
2340 EXPECT_EQ(LOAD_STATE_CONNECTING
, pool_
->GetLoadState("a", &handle
));
2342 ClientSocketHandle handle2
;
2343 TestCompletionCallback callback2
;
2344 rv
= handle2
.Init("a",
2347 callback2
.callback(),
2350 EXPECT_EQ(ERR_IO_PENDING
, rv
);
2351 EXPECT_EQ(LOAD_STATE_CONNECTING
, pool_
->GetLoadState("a", &handle2
));
2353 // Cancel one of the requests. Wait for the other, which will get the first
2354 // job. Release the socket. Run the loop again to make sure the second
2355 // socket is sitting idle and the first one is released (since ReleaseSocket()
2356 // just posts a DoReleaseSocket() task).
2359 EXPECT_EQ(OK
, callback2
.WaitForResult());
2361 EXPECT_EQ(1, handle2
.socket()->Write(NULL
, 1, CompletionCallback()));
2364 // We post all of our delayed tasks with a 2ms delay. I.e. they don't
2365 // actually become pending until 2ms after they have been created. In order
2366 // to flush all tasks, we need to wait so that we know there are no
2367 // soon-to-be-pending tasks waiting.
2368 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10));
2369 base::MessageLoop::current()->RunUntilIdle();
2371 ASSERT_EQ(2, pool_
->IdleSocketCount());
2373 // Invoke the idle socket cleanup check. Only one socket should be left, the
2374 // used socket. Request it to make sure that it's used.
2376 pool_
->CleanupTimedOutIdleSockets();
2377 CapturingBoundNetLog log
;
2378 rv
= handle
.Init("a",
2381 callback
.callback(),
2385 EXPECT_TRUE(handle
.is_reused());
2387 CapturingNetLog::CapturedEntryList entries
;
2388 log
.GetEntries(&entries
);
2389 EXPECT_TRUE(LogContainsEntryWithType(
2390 entries
, 1, NetLog::TYPE_SOCKET_POOL_REUSED_AN_EXISTING_SOCKET
));
2393 // Make sure that we process all pending requests even when we're stalling
2394 // because of multiple releasing disconnected sockets.
2395 TEST_F(ClientSocketPoolBaseTest
, MultipleReleasingDisconnectedSockets
) {
2396 CreatePoolWithIdleTimeouts(
2397 kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
,
2398 base::TimeDelta(), // Time out unused sockets immediately.
2399 base::TimeDelta::FromDays(1)); // Don't time out used sockets.
2401 connect_job_factory_
->set_job_type(TestConnectJob::kMockJob
);
2403 // Startup 4 connect jobs. Two of them will be pending.
2405 ClientSocketHandle handle
;
2406 TestCompletionCallback callback
;
2407 int rv
= handle
.Init("a",
2410 callback
.callback(),
2415 ClientSocketHandle handle2
;
2416 TestCompletionCallback callback2
;
2417 rv
= handle2
.Init("a",
2420 callback2
.callback(),
2425 ClientSocketHandle handle3
;
2426 TestCompletionCallback callback3
;
2427 rv
= handle3
.Init("a",
2430 callback3
.callback(),
2433 EXPECT_EQ(ERR_IO_PENDING
, rv
);
2435 ClientSocketHandle handle4
;
2436 TestCompletionCallback callback4
;
2437 rv
= handle4
.Init("a",
2440 callback4
.callback(),
2443 EXPECT_EQ(ERR_IO_PENDING
, rv
);
2445 // Release two disconnected sockets.
2447 handle
.socket()->Disconnect();
2449 handle2
.socket()->Disconnect();
2452 EXPECT_EQ(OK
, callback3
.WaitForResult());
2453 EXPECT_FALSE(handle3
.is_reused());
2454 EXPECT_EQ(OK
, callback4
.WaitForResult());
2455 EXPECT_FALSE(handle4
.is_reused());
2458 // Regression test for http://crbug.com/42267.
2459 // When DoReleaseSocket() is processed for one socket, it is blocked because the
2460 // other stalled groups all have releasing sockets, so no progress can be made.
2461 TEST_F(ClientSocketPoolBaseTest
, SocketLimitReleasingSockets
) {
2462 CreatePoolWithIdleTimeouts(
2463 4 /* socket limit */, 4 /* socket limit per group */,
2464 base::TimeDelta(), // Time out unused sockets immediately.
2465 base::TimeDelta::FromDays(1)); // Don't time out used sockets.
2467 connect_job_factory_
->set_job_type(TestConnectJob::kMockJob
);
2469 // Max out the socket limit with 2 per group.
2471 ClientSocketHandle handle_a
[4];
2472 TestCompletionCallback callback_a
[4];
2473 ClientSocketHandle handle_b
[4];
2474 TestCompletionCallback callback_b
[4];
2476 for (int i
= 0; i
< 2; ++i
) {
2477 EXPECT_EQ(OK
, handle_a
[i
].Init("a",
2480 callback_a
[i
].callback(),
2483 EXPECT_EQ(OK
, handle_b
[i
].Init("b",
2486 callback_b
[i
].callback(),
2491 // Make 4 pending requests, 2 per group.
2493 for (int i
= 2; i
< 4; ++i
) {
2494 EXPECT_EQ(ERR_IO_PENDING
,
2495 handle_a
[i
].Init("a",
2498 callback_a
[i
].callback(),
2501 EXPECT_EQ(ERR_IO_PENDING
,
2502 handle_b
[i
].Init("b",
2505 callback_b
[i
].callback(),
2510 // Release b's socket first. The order is important, because in
2511 // DoReleaseSocket(), we'll process b's released socket, and since both b and
2512 // a are stalled, but 'a' is lower lexicographically, we'll process group 'a'
2513 // first, which has a releasing socket, so it refuses to start up another
2514 // ConnectJob. So, we used to infinite loop on this.
2515 handle_b
[0].socket()->Disconnect();
2516 handle_b
[0].Reset();
2517 handle_a
[0].socket()->Disconnect();
2518 handle_a
[0].Reset();
2520 // Used to get stuck here.
2521 base::MessageLoop::current()->RunUntilIdle();
2523 handle_b
[1].socket()->Disconnect();
2524 handle_b
[1].Reset();
2525 handle_a
[1].socket()->Disconnect();
2526 handle_a
[1].Reset();
2528 for (int i
= 2; i
< 4; ++i
) {
2529 EXPECT_EQ(OK
, callback_b
[i
].WaitForResult());
2530 EXPECT_EQ(OK
, callback_a
[i
].WaitForResult());
2534 TEST_F(ClientSocketPoolBaseTest
,
2535 ReleasingDisconnectedSocketsMaintainsPriorityOrder
) {
2536 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
2538 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
2540 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", DEFAULT_PRIORITY
));
2541 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", DEFAULT_PRIORITY
));
2542 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", DEFAULT_PRIORITY
));
2543 EXPECT_EQ(ERR_IO_PENDING
, StartRequest("a", DEFAULT_PRIORITY
));
2545 EXPECT_EQ(OK
, (*requests())[0]->WaitForResult());
2546 EXPECT_EQ(OK
, (*requests())[1]->WaitForResult());
2547 EXPECT_EQ(2u, completion_count());
2549 // Releases one connection.
2550 EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::NO_KEEP_ALIVE
));
2551 EXPECT_EQ(OK
, (*requests())[2]->WaitForResult());
2553 EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::NO_KEEP_ALIVE
));
2554 EXPECT_EQ(OK
, (*requests())[3]->WaitForResult());
2555 EXPECT_EQ(4u, completion_count());
2557 EXPECT_EQ(1, GetOrderOfRequest(1));
2558 EXPECT_EQ(2, GetOrderOfRequest(2));
2559 EXPECT_EQ(3, GetOrderOfRequest(3));
2560 EXPECT_EQ(4, GetOrderOfRequest(4));
2562 // Make sure we test order of all requests made.
2563 EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds
, GetOrderOfRequest(5));
2566 class TestReleasingSocketRequest
: public TestCompletionCallbackBase
{
2568 TestReleasingSocketRequest(TestClientSocketPool
* pool
,
2569 int expected_result
,
2570 bool reset_releasing_handle
)
2572 expected_result_(expected_result
),
2573 reset_releasing_handle_(reset_releasing_handle
),
2574 callback_(base::Bind(&TestReleasingSocketRequest::OnComplete
,
2575 base::Unretained(this))) {
2578 ~TestReleasingSocketRequest() override
{}
2580 ClientSocketHandle
* handle() { return &handle_
; }
2582 const CompletionCallback
& callback() const { return callback_
; }
2585 void OnComplete(int result
) {
2587 if (reset_releasing_handle_
)
2590 scoped_refptr
<TestSocketParams
> con_params(
2591 new TestSocketParams(false /* ignore_limits */));
2592 EXPECT_EQ(expected_result_
,
2593 handle2_
.Init("a", con_params
, DEFAULT_PRIORITY
,
2594 callback2_
.callback(), pool_
, BoundNetLog()));
2597 TestClientSocketPool
* const pool_
;
2598 int expected_result_
;
2599 bool reset_releasing_handle_
;
2600 ClientSocketHandle handle_
;
2601 ClientSocketHandle handle2_
;
2602 CompletionCallback callback_
;
2603 TestCompletionCallback callback2_
;
2607 TEST_F(ClientSocketPoolBaseTest
, AdditionalErrorSocketsDontUseSlot
) {
2608 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
2610 EXPECT_EQ(OK
, StartRequest("b", DEFAULT_PRIORITY
));
2611 EXPECT_EQ(OK
, StartRequest("a", DEFAULT_PRIORITY
));
2612 EXPECT_EQ(OK
, StartRequest("b", DEFAULT_PRIORITY
));
2614 EXPECT_EQ(static_cast<int>(requests_size()),
2615 client_socket_factory_
.allocation_count());
2617 connect_job_factory_
->set_job_type(
2618 TestConnectJob::kMockPendingAdditionalErrorStateJob
);
2619 TestReleasingSocketRequest
req(pool_
.get(), OK
, false);
2620 EXPECT_EQ(ERR_IO_PENDING
,
2621 req
.handle()->Init("a", params_
, DEFAULT_PRIORITY
, req
.callback(),
2622 pool_
.get(), BoundNetLog()));
2623 // The next job should complete synchronously
2624 connect_job_factory_
->set_job_type(TestConnectJob::kMockJob
);
2626 EXPECT_EQ(ERR_CONNECTION_FAILED
, req
.WaitForResult());
2627 EXPECT_FALSE(req
.handle()->is_initialized());
2628 EXPECT_FALSE(req
.handle()->socket());
2629 EXPECT_TRUE(req
.handle()->is_ssl_error());
2630 EXPECT_FALSE(req
.handle()->ssl_error_response_info().headers
.get() == NULL
);
2633 // http://crbug.com/44724 regression test.
2634 // We start releasing the pool when we flush on network change. When that
2635 // happens, the only active references are in the ClientSocketHandles. When a
2636 // ConnectJob completes and calls back into the last ClientSocketHandle, that
2637 // callback can release the last reference and delete the pool. After the
2638 // callback finishes, we go back to the stack frame within the now-deleted pool.
2639 // Executing any code that refers to members of the now-deleted pool can cause
2641 TEST_F(ClientSocketPoolBaseTest
, CallbackThatReleasesPool
) {
2642 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
2643 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingFailingJob
);
2645 ClientSocketHandle handle
;
2646 TestCompletionCallback callback
;
2647 EXPECT_EQ(ERR_IO_PENDING
, handle
.Init("a",
2650 callback
.callback(),
2654 pool_
->FlushWithError(ERR_NETWORK_CHANGED
);
2656 // We'll call back into this now.
2657 callback
.WaitForResult();
2660 TEST_F(ClientSocketPoolBaseTest
, DoNotReuseSocketAfterFlush
) {
2661 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
2662 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
2664 ClientSocketHandle handle
;
2665 TestCompletionCallback callback
;
2666 EXPECT_EQ(ERR_IO_PENDING
, handle
.Init("a",
2669 callback
.callback(),
2672 EXPECT_EQ(OK
, callback
.WaitForResult());
2673 EXPECT_EQ(ClientSocketHandle::UNUSED
, handle
.reuse_type());
2675 pool_
->FlushWithError(ERR_NETWORK_CHANGED
);
2678 base::MessageLoop::current()->RunUntilIdle();
2680 EXPECT_EQ(ERR_IO_PENDING
, handle
.Init("a",
2683 callback
.callback(),
2686 EXPECT_EQ(OK
, callback
.WaitForResult());
2687 EXPECT_EQ(ClientSocketHandle::UNUSED
, handle
.reuse_type());
2690 class ConnectWithinCallback
: public TestCompletionCallbackBase
{
2692 ConnectWithinCallback(
2693 const std::string
& group_name
,
2694 const scoped_refptr
<TestSocketParams
>& params
,
2695 TestClientSocketPool
* pool
)
2696 : group_name_(group_name
),
2699 callback_(base::Bind(&ConnectWithinCallback::OnComplete
,
2700 base::Unretained(this))) {
2703 ~ConnectWithinCallback() override
{}
2705 int WaitForNestedResult() {
2706 return nested_callback_
.WaitForResult();
2709 const CompletionCallback
& callback() const { return callback_
; }
2712 void OnComplete(int result
) {
2714 EXPECT_EQ(ERR_IO_PENDING
,
2715 handle_
.Init(group_name_
,
2718 nested_callback_
.callback(),
2723 const std::string group_name_
;
2724 const scoped_refptr
<TestSocketParams
> params_
;
2725 TestClientSocketPool
* const pool_
;
2726 ClientSocketHandle handle_
;
2727 CompletionCallback callback_
;
2728 TestCompletionCallback nested_callback_
;
2730 DISALLOW_COPY_AND_ASSIGN(ConnectWithinCallback
);
2733 TEST_F(ClientSocketPoolBaseTest
, AbortAllRequestsOnFlush
) {
2734 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
2736 // First job will be waiting until it gets aborted.
2737 connect_job_factory_
->set_job_type(TestConnectJob::kMockWaitingJob
);
2739 ClientSocketHandle handle
;
2740 ConnectWithinCallback
callback("a", params_
, pool_
.get());
2741 EXPECT_EQ(ERR_IO_PENDING
, handle
.Init("a",
2744 callback
.callback(),
2748 // Second job will be started during the first callback, and will
2749 // asynchronously complete with OK.
2750 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
2751 pool_
->FlushWithError(ERR_NETWORK_CHANGED
);
2752 EXPECT_EQ(ERR_NETWORK_CHANGED
, callback
.WaitForResult());
2753 EXPECT_EQ(OK
, callback
.WaitForNestedResult());
2756 // Cancel a pending socket request while we're at max sockets,
2757 // and verify that the backup socket firing doesn't cause a crash.
2758 TEST_F(ClientSocketPoolBaseTest
, BackupSocketCancelAtMaxSockets
) {
2759 // Max 4 sockets globally, max 4 sockets per group.
2760 CreatePool(kDefaultMaxSockets
, kDefaultMaxSockets
);
2761 pool_
->EnableConnectBackupJobs();
2763 // Create the first socket and set to ERR_IO_PENDING. This starts the backup
2765 connect_job_factory_
->set_job_type(TestConnectJob::kMockWaitingJob
);
2766 ClientSocketHandle handle
;
2767 TestCompletionCallback callback
;
2768 EXPECT_EQ(ERR_IO_PENDING
, handle
.Init("bar",
2771 callback
.callback(),
2775 // Start (MaxSockets - 1) connected sockets to reach max sockets.
2776 connect_job_factory_
->set_job_type(TestConnectJob::kMockJob
);
2777 ClientSocketHandle handles
[kDefaultMaxSockets
];
2778 for (int i
= 1; i
< kDefaultMaxSockets
; ++i
) {
2779 TestCompletionCallback callback
;
2780 EXPECT_EQ(OK
, handles
[i
].Init("bar",
2783 callback
.callback(),
2788 base::MessageLoop::current()->RunUntilIdle();
2790 // Cancel the pending request.
2793 // Wait for the backup timer to fire (add some slop to ensure it fires)
2794 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(
2795 ClientSocketPool::kMaxConnectRetryIntervalMs
/ 2 * 3));
2797 base::MessageLoop::current()->RunUntilIdle();
2798 EXPECT_EQ(kDefaultMaxSockets
, client_socket_factory_
.allocation_count());
2801 TEST_F(ClientSocketPoolBaseTest
, CancelBackupSocketAfterCancelingAllRequests
) {
2802 CreatePool(kDefaultMaxSockets
, kDefaultMaxSockets
);
2803 pool_
->EnableConnectBackupJobs();
2805 // Create the first socket and set to ERR_IO_PENDING. This starts the backup
2807 connect_job_factory_
->set_job_type(TestConnectJob::kMockWaitingJob
);
2808 ClientSocketHandle handle
;
2809 TestCompletionCallback callback
;
2810 EXPECT_EQ(ERR_IO_PENDING
, handle
.Init("bar",
2813 callback
.callback(),
2816 ASSERT_TRUE(pool_
->HasGroup("bar"));
2817 EXPECT_EQ(1, pool_
->NumConnectJobsInGroup("bar"));
2818 EXPECT_EQ(0, pool_
->NumUnassignedConnectJobsInGroup("bar"));
2820 // Cancel the socket request. This should cancel the backup timer. Wait for
2821 // the backup time to see if it indeed got canceled.
2823 // Wait for the backup timer to fire (add some slop to ensure it fires)
2824 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(
2825 ClientSocketPool::kMaxConnectRetryIntervalMs
/ 2 * 3));
2826 base::MessageLoop::current()->RunUntilIdle();
2827 ASSERT_TRUE(pool_
->HasGroup("bar"));
2828 EXPECT_EQ(1, pool_
->NumConnectJobsInGroup("bar"));
2831 TEST_F(ClientSocketPoolBaseTest
, CancelBackupSocketAfterFinishingAllRequests
) {
2832 CreatePool(kDefaultMaxSockets
, kDefaultMaxSockets
);
2833 pool_
->EnableConnectBackupJobs();
2835 // Create the first socket and set to ERR_IO_PENDING. This starts the backup
2837 connect_job_factory_
->set_job_type(TestConnectJob::kMockWaitingJob
);
2838 ClientSocketHandle handle
;
2839 TestCompletionCallback callback
;
2840 EXPECT_EQ(ERR_IO_PENDING
, handle
.Init("bar",
2843 callback
.callback(),
2846 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
2847 ClientSocketHandle handle2
;
2848 TestCompletionCallback callback2
;
2849 EXPECT_EQ(ERR_IO_PENDING
, handle2
.Init("bar",
2852 callback2
.callback(),
2855 ASSERT_TRUE(pool_
->HasGroup("bar"));
2856 EXPECT_EQ(2, pool_
->NumConnectJobsInGroup("bar"));
2858 // Cancel request 1 and then complete request 2. With the requests finished,
2859 // the backup timer should be cancelled.
2861 EXPECT_EQ(OK
, callback2
.WaitForResult());
2862 // Wait for the backup timer to fire (add some slop to ensure it fires)
2863 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(
2864 ClientSocketPool::kMaxConnectRetryIntervalMs
/ 2 * 3));
2865 base::MessageLoop::current()->RunUntilIdle();
2868 // Test delayed socket binding for the case where we have two connects,
2869 // and while one is waiting on a connect, the other frees up.
2870 // The socket waiting on a connect should switch immediately to the freed
2872 TEST_F(ClientSocketPoolBaseTest
, DelayedSocketBindingWaitingForConnect
) {
2873 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
2874 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
2876 ClientSocketHandle handle1
;
2877 TestCompletionCallback callback
;
2878 EXPECT_EQ(ERR_IO_PENDING
,
2882 callback
.callback(),
2885 EXPECT_EQ(OK
, callback
.WaitForResult());
2887 // No idle sockets, no pending jobs.
2888 EXPECT_EQ(0, pool_
->IdleSocketCount());
2889 EXPECT_EQ(0, pool_
->NumConnectJobsInGroup("a"));
2891 // Create a second socket to the same host, but this one will wait.
2892 connect_job_factory_
->set_job_type(TestConnectJob::kMockWaitingJob
);
2893 ClientSocketHandle handle2
;
2894 EXPECT_EQ(ERR_IO_PENDING
,
2898 callback
.callback(),
2901 // No idle sockets, and one connecting job.
2902 EXPECT_EQ(0, pool_
->IdleSocketCount());
2903 EXPECT_EQ(1, pool_
->NumConnectJobsInGroup("a"));
2905 // Return the first handle to the pool. This will initiate the delayed
2909 base::MessageLoop::current()->RunUntilIdle();
2911 // Still no idle sockets, still one pending connect job.
2912 EXPECT_EQ(0, pool_
->IdleSocketCount());
2913 EXPECT_EQ(1, pool_
->NumConnectJobsInGroup("a"));
2915 // The second socket connected, even though it was a Waiting Job.
2916 EXPECT_EQ(OK
, callback
.WaitForResult());
2918 // And we can see there is still one job waiting.
2919 EXPECT_EQ(1, pool_
->NumConnectJobsInGroup("a"));
2921 // Finally, signal the waiting Connect.
2922 client_socket_factory_
.SignalJobs();
2923 EXPECT_EQ(0, pool_
->NumConnectJobsInGroup("a"));
2925 base::MessageLoop::current()->RunUntilIdle();
2928 // Test delayed socket binding when a group is at capacity and one
2929 // of the group's sockets frees up.
2930 TEST_F(ClientSocketPoolBaseTest
, DelayedSocketBindingAtGroupCapacity
) {
2931 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
2932 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
2934 ClientSocketHandle handle1
;
2935 TestCompletionCallback callback
;
2936 EXPECT_EQ(ERR_IO_PENDING
,
2940 callback
.callback(),
2943 EXPECT_EQ(OK
, callback
.WaitForResult());
2945 // No idle sockets, no pending jobs.
2946 EXPECT_EQ(0, pool_
->IdleSocketCount());
2947 EXPECT_EQ(0, pool_
->NumConnectJobsInGroup("a"));
2949 // Create a second socket to the same host, but this one will wait.
2950 connect_job_factory_
->set_job_type(TestConnectJob::kMockWaitingJob
);
2951 ClientSocketHandle handle2
;
2952 EXPECT_EQ(ERR_IO_PENDING
,
2956 callback
.callback(),
2959 // No idle sockets, and one connecting job.
2960 EXPECT_EQ(0, pool_
->IdleSocketCount());
2961 EXPECT_EQ(1, pool_
->NumConnectJobsInGroup("a"));
2963 // Return the first handle to the pool. This will initiate the delayed
2967 base::MessageLoop::current()->RunUntilIdle();
2969 // Still no idle sockets, still one pending connect job.
2970 EXPECT_EQ(0, pool_
->IdleSocketCount());
2971 EXPECT_EQ(1, pool_
->NumConnectJobsInGroup("a"));
2973 // The second socket connected, even though it was a Waiting Job.
2974 EXPECT_EQ(OK
, callback
.WaitForResult());
2976 // And we can see there is still one job waiting.
2977 EXPECT_EQ(1, pool_
->NumConnectJobsInGroup("a"));
2979 // Finally, signal the waiting Connect.
2980 client_socket_factory_
.SignalJobs();
2981 EXPECT_EQ(0, pool_
->NumConnectJobsInGroup("a"));
2983 base::MessageLoop::current()->RunUntilIdle();
2986 // Test out the case where we have one socket connected, one
2987 // connecting, when the first socket finishes and goes idle.
2988 // Although the second connection is pending, the second request
2989 // should complete, by taking the first socket's idle socket.
2990 TEST_F(ClientSocketPoolBaseTest
, DelayedSocketBindingAtStall
) {
2991 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
2992 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
2994 ClientSocketHandle handle1
;
2995 TestCompletionCallback callback
;
2996 EXPECT_EQ(ERR_IO_PENDING
,
3000 callback
.callback(),
3003 EXPECT_EQ(OK
, callback
.WaitForResult());
3005 // No idle sockets, no pending jobs.
3006 EXPECT_EQ(0, pool_
->IdleSocketCount());
3007 EXPECT_EQ(0, pool_
->NumConnectJobsInGroup("a"));
3009 // Create a second socket to the same host, but this one will wait.
3010 connect_job_factory_
->set_job_type(TestConnectJob::kMockWaitingJob
);
3011 ClientSocketHandle handle2
;
3012 EXPECT_EQ(ERR_IO_PENDING
,
3016 callback
.callback(),
3019 // No idle sockets, and one connecting job.
3020 EXPECT_EQ(0, pool_
->IdleSocketCount());
3021 EXPECT_EQ(1, pool_
->NumConnectJobsInGroup("a"));
3023 // Return the first handle to the pool. This will initiate the delayed
3027 base::MessageLoop::current()->RunUntilIdle();
3029 // Still no idle sockets, still one pending connect job.
3030 EXPECT_EQ(0, pool_
->IdleSocketCount());
3031 EXPECT_EQ(1, pool_
->NumConnectJobsInGroup("a"));
3033 // The second socket connected, even though it was a Waiting Job.
3034 EXPECT_EQ(OK
, callback
.WaitForResult());
3036 // And we can see there is still one job waiting.
3037 EXPECT_EQ(1, pool_
->NumConnectJobsInGroup("a"));
3039 // Finally, signal the waiting Connect.
3040 client_socket_factory_
.SignalJobs();
3041 EXPECT_EQ(0, pool_
->NumConnectJobsInGroup("a"));
3043 base::MessageLoop::current()->RunUntilIdle();
3046 // Cover the case where on an available socket slot, we have one pending
3047 // request that completes synchronously, thereby making the Group empty.
3048 TEST_F(ClientSocketPoolBaseTest
, SynchronouslyProcessOnePendingRequest
) {
3049 const int kUnlimitedSockets
= 100;
3050 const int kOneSocketPerGroup
= 1;
3051 CreatePool(kUnlimitedSockets
, kOneSocketPerGroup
);
3053 // Make the first request asynchronous fail.
3054 // This will free up a socket slot later.
3055 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingFailingJob
);
3057 ClientSocketHandle handle1
;
3058 TestCompletionCallback callback1
;
3059 EXPECT_EQ(ERR_IO_PENDING
,
3063 callback1
.callback(),
3066 EXPECT_EQ(1, pool_
->NumConnectJobsInGroup("a"));
3068 // Make the second request synchronously fail. This should make the Group
3070 connect_job_factory_
->set_job_type(TestConnectJob::kMockFailingJob
);
3071 ClientSocketHandle handle2
;
3072 TestCompletionCallback callback2
;
3073 // It'll be ERR_IO_PENDING now, but the TestConnectJob will synchronously fail
3075 EXPECT_EQ(ERR_IO_PENDING
,
3079 callback2
.callback(),
3083 EXPECT_EQ(1, pool_
->NumConnectJobsInGroup("a"));
3085 EXPECT_EQ(ERR_CONNECTION_FAILED
, callback1
.WaitForResult());
3086 EXPECT_EQ(ERR_CONNECTION_FAILED
, callback2
.WaitForResult());
3087 EXPECT_FALSE(pool_
->HasGroup("a"));
3090 TEST_F(ClientSocketPoolBaseTest
, PreferUsedSocketToUnusedSocket
) {
3091 CreatePool(kDefaultMaxSockets
, kDefaultMaxSockets
);
3093 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
3095 ClientSocketHandle handle1
;
3096 TestCompletionCallback callback1
;
3097 EXPECT_EQ(ERR_IO_PENDING
, handle1
.Init("a",
3100 callback1
.callback(),
3104 ClientSocketHandle handle2
;
3105 TestCompletionCallback callback2
;
3106 EXPECT_EQ(ERR_IO_PENDING
, handle2
.Init("a",
3109 callback2
.callback(),
3112 ClientSocketHandle handle3
;
3113 TestCompletionCallback callback3
;
3114 EXPECT_EQ(ERR_IO_PENDING
, handle3
.Init("a",
3117 callback3
.callback(),
3121 EXPECT_EQ(OK
, callback1
.WaitForResult());
3122 EXPECT_EQ(OK
, callback2
.WaitForResult());
3123 EXPECT_EQ(OK
, callback3
.WaitForResult());
3126 EXPECT_EQ(1, handle1
.socket()->Write(NULL
, 1, CompletionCallback()));
3127 EXPECT_EQ(1, handle3
.socket()->Write(NULL
, 1, CompletionCallback()));
3133 EXPECT_EQ(OK
, handle1
.Init("a",
3136 callback1
.callback(),
3139 EXPECT_EQ(OK
, handle2
.Init("a",
3142 callback2
.callback(),
3145 EXPECT_EQ(OK
, handle3
.Init("a",
3148 callback3
.callback(),
3152 EXPECT_TRUE(handle1
.socket()->WasEverUsed());
3153 EXPECT_TRUE(handle2
.socket()->WasEverUsed());
3154 EXPECT_FALSE(handle3
.socket()->WasEverUsed());
3157 TEST_F(ClientSocketPoolBaseTest
, RequestSockets
) {
3158 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
3159 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
3161 pool_
->RequestSockets("a", ¶ms_
, 2, BoundNetLog());
3163 ASSERT_TRUE(pool_
->HasGroup("a"));
3164 EXPECT_EQ(2, pool_
->NumConnectJobsInGroup("a"));
3165 EXPECT_EQ(2, pool_
->NumUnassignedConnectJobsInGroup("a"));
3166 EXPECT_EQ(0, pool_
->IdleSocketCountInGroup("a"));
3168 ClientSocketHandle handle1
;
3169 TestCompletionCallback callback1
;
3170 EXPECT_EQ(ERR_IO_PENDING
, handle1
.Init("a",
3173 callback1
.callback(),
3177 ClientSocketHandle handle2
;
3178 TestCompletionCallback callback2
;
3179 EXPECT_EQ(ERR_IO_PENDING
, handle2
.Init("a",
3182 callback2
.callback(),
3186 EXPECT_EQ(2, pool_
->NumConnectJobsInGroup("a"));
3187 EXPECT_EQ(0, pool_
->NumUnassignedConnectJobsInGroup("a"));
3188 EXPECT_EQ(0, pool_
->IdleSocketCountInGroup("a"));
3190 EXPECT_EQ(OK
, callback1
.WaitForResult());
3191 EXPECT_EQ(OK
, callback2
.WaitForResult());
3195 EXPECT_EQ(0, pool_
->NumConnectJobsInGroup("a"));
3196 EXPECT_EQ(0, pool_
->NumUnassignedConnectJobsInGroup("a"));
3197 EXPECT_EQ(2, pool_
->IdleSocketCountInGroup("a"));
3200 TEST_F(ClientSocketPoolBaseTest
, RequestSocketsWhenAlreadyHaveAConnectJob
) {
3201 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
3202 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
3204 ClientSocketHandle handle1
;
3205 TestCompletionCallback callback1
;
3206 EXPECT_EQ(ERR_IO_PENDING
, handle1
.Init("a",
3209 callback1
.callback(),
3213 ASSERT_TRUE(pool_
->HasGroup("a"));
3214 EXPECT_EQ(1, pool_
->NumConnectJobsInGroup("a"));
3215 EXPECT_EQ(0, pool_
->NumUnassignedConnectJobsInGroup("a"));
3216 EXPECT_EQ(0, pool_
->IdleSocketCountInGroup("a"));
3218 pool_
->RequestSockets("a", ¶ms_
, 2, BoundNetLog());
3220 EXPECT_EQ(2, pool_
->NumConnectJobsInGroup("a"));
3221 EXPECT_EQ(1, pool_
->NumUnassignedConnectJobsInGroup("a"));
3222 EXPECT_EQ(0, pool_
->IdleSocketCountInGroup("a"));
3224 ClientSocketHandle handle2
;
3225 TestCompletionCallback callback2
;
3226 EXPECT_EQ(ERR_IO_PENDING
, handle2
.Init("a",
3229 callback2
.callback(),
3233 EXPECT_EQ(2, pool_
->NumConnectJobsInGroup("a"));
3234 EXPECT_EQ(0, pool_
->NumUnassignedConnectJobsInGroup("a"));
3235 EXPECT_EQ(0, pool_
->IdleSocketCountInGroup("a"));
3237 EXPECT_EQ(OK
, callback1
.WaitForResult());
3238 EXPECT_EQ(OK
, callback2
.WaitForResult());
3242 EXPECT_EQ(0, pool_
->NumConnectJobsInGroup("a"));
3243 EXPECT_EQ(0, pool_
->NumUnassignedConnectJobsInGroup("a"));
3244 EXPECT_EQ(2, pool_
->IdleSocketCountInGroup("a"));
3247 TEST_F(ClientSocketPoolBaseTest
,
3248 RequestSocketsWhenAlreadyHaveMultipleConnectJob
) {
3250 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
3252 ClientSocketHandle handle1
;
3253 TestCompletionCallback callback1
;
3254 EXPECT_EQ(ERR_IO_PENDING
, handle1
.Init("a",
3257 callback1
.callback(),
3261 ClientSocketHandle handle2
;
3262 TestCompletionCallback callback2
;
3263 EXPECT_EQ(ERR_IO_PENDING
, handle2
.Init("a",
3266 callback2
.callback(),
3270 ClientSocketHandle handle3
;
3271 TestCompletionCallback callback3
;
3272 EXPECT_EQ(ERR_IO_PENDING
, handle3
.Init("a",
3275 callback3
.callback(),
3279 ASSERT_TRUE(pool_
->HasGroup("a"));
3280 EXPECT_EQ(3, pool_
->NumConnectJobsInGroup("a"));
3281 EXPECT_EQ(0, pool_
->NumUnassignedConnectJobsInGroup("a"));
3282 EXPECT_EQ(0, pool_
->IdleSocketCountInGroup("a"));
3284 pool_
->RequestSockets("a", ¶ms_
, 2, BoundNetLog());
3286 EXPECT_EQ(3, pool_
->NumConnectJobsInGroup("a"));
3287 EXPECT_EQ(0, pool_
->NumUnassignedConnectJobsInGroup("a"));
3288 EXPECT_EQ(0, pool_
->IdleSocketCountInGroup("a"));
3290 EXPECT_EQ(OK
, callback1
.WaitForResult());
3291 EXPECT_EQ(OK
, callback2
.WaitForResult());
3292 EXPECT_EQ(OK
, callback3
.WaitForResult());
3297 EXPECT_EQ(0, pool_
->NumConnectJobsInGroup("a"));
3298 EXPECT_EQ(0, pool_
->NumUnassignedConnectJobsInGroup("a"));
3299 EXPECT_EQ(3, pool_
->IdleSocketCountInGroup("a"));
3302 TEST_F(ClientSocketPoolBaseTest
, RequestSocketsAtMaxSocketLimit
) {
3303 CreatePool(kDefaultMaxSockets
, kDefaultMaxSockets
);
3304 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
3306 ASSERT_FALSE(pool_
->HasGroup("a"));
3308 pool_
->RequestSockets("a", ¶ms_
, kDefaultMaxSockets
,
3311 ASSERT_TRUE(pool_
->HasGroup("a"));
3312 EXPECT_EQ(kDefaultMaxSockets
, pool_
->NumConnectJobsInGroup("a"));
3313 EXPECT_EQ(kDefaultMaxSockets
, pool_
->NumUnassignedConnectJobsInGroup("a"));
3315 ASSERT_FALSE(pool_
->HasGroup("b"));
3317 pool_
->RequestSockets("b", ¶ms_
, kDefaultMaxSockets
,
3320 ASSERT_FALSE(pool_
->HasGroup("b"));
3323 TEST_F(ClientSocketPoolBaseTest
, RequestSocketsHitMaxSocketLimit
) {
3324 CreatePool(kDefaultMaxSockets
, kDefaultMaxSockets
);
3325 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
3327 ASSERT_FALSE(pool_
->HasGroup("a"));
3329 pool_
->RequestSockets("a", ¶ms_
, kDefaultMaxSockets
- 1,
3332 ASSERT_TRUE(pool_
->HasGroup("a"));
3333 EXPECT_EQ(kDefaultMaxSockets
- 1, pool_
->NumConnectJobsInGroup("a"));
3334 EXPECT_EQ(kDefaultMaxSockets
- 1,
3335 pool_
->NumUnassignedConnectJobsInGroup("a"));
3336 EXPECT_FALSE(pool_
->IsStalled());
3338 ASSERT_FALSE(pool_
->HasGroup("b"));
3340 pool_
->RequestSockets("b", ¶ms_
, kDefaultMaxSockets
,
3343 ASSERT_TRUE(pool_
->HasGroup("b"));
3344 EXPECT_EQ(1, pool_
->NumConnectJobsInGroup("b"));
3345 EXPECT_FALSE(pool_
->IsStalled());
3348 TEST_F(ClientSocketPoolBaseTest
, RequestSocketsCountIdleSockets
) {
3350 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
3352 ClientSocketHandle handle1
;
3353 TestCompletionCallback callback1
;
3354 EXPECT_EQ(ERR_IO_PENDING
, handle1
.Init("a",
3357 callback1
.callback(),
3360 ASSERT_EQ(OK
, callback1
.WaitForResult());
3363 ASSERT_TRUE(pool_
->HasGroup("a"));
3364 EXPECT_EQ(0, pool_
->NumConnectJobsInGroup("a"));
3365 EXPECT_EQ(0, pool_
->NumUnassignedConnectJobsInGroup("a"));
3366 EXPECT_EQ(1, pool_
->IdleSocketCountInGroup("a"));
3368 pool_
->RequestSockets("a", ¶ms_
, 2, BoundNetLog());
3370 EXPECT_EQ(1, pool_
->NumConnectJobsInGroup("a"));
3371 EXPECT_EQ(1, pool_
->NumUnassignedConnectJobsInGroup("a"));
3372 EXPECT_EQ(1, pool_
->IdleSocketCountInGroup("a"));
3375 TEST_F(ClientSocketPoolBaseTest
, RequestSocketsCountActiveSockets
) {
3377 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
3379 ClientSocketHandle handle1
;
3380 TestCompletionCallback callback1
;
3381 EXPECT_EQ(ERR_IO_PENDING
, handle1
.Init("a",
3384 callback1
.callback(),
3387 ASSERT_EQ(OK
, callback1
.WaitForResult());
3389 ASSERT_TRUE(pool_
->HasGroup("a"));
3390 EXPECT_EQ(0, pool_
->NumConnectJobsInGroup("a"));
3391 EXPECT_EQ(0, pool_
->NumUnassignedConnectJobsInGroup("a"));
3392 EXPECT_EQ(0, pool_
->IdleSocketCountInGroup("a"));
3393 EXPECT_EQ(1, pool_
->NumActiveSocketsInGroup("a"));
3395 pool_
->RequestSockets("a", ¶ms_
, 2, BoundNetLog());
3397 EXPECT_EQ(1, pool_
->NumConnectJobsInGroup("a"));
3398 EXPECT_EQ(1, pool_
->NumUnassignedConnectJobsInGroup("a"));
3399 EXPECT_EQ(0, pool_
->IdleSocketCountInGroup("a"));
3400 EXPECT_EQ(1, pool_
->NumActiveSocketsInGroup("a"));
3403 TEST_F(ClientSocketPoolBaseTest
, RequestSocketsSynchronous
) {
3404 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
3405 connect_job_factory_
->set_job_type(TestConnectJob::kMockJob
);
3407 pool_
->RequestSockets("a", ¶ms_
, kDefaultMaxSocketsPerGroup
,
3410 ASSERT_TRUE(pool_
->HasGroup("a"));
3411 EXPECT_EQ(0, pool_
->NumConnectJobsInGroup("a"));
3412 EXPECT_EQ(0, pool_
->NumUnassignedConnectJobsInGroup("a"));
3413 EXPECT_EQ(kDefaultMaxSocketsPerGroup
, pool_
->IdleSocketCountInGroup("a"));
3415 pool_
->RequestSockets("b", ¶ms_
, kDefaultMaxSocketsPerGroup
,
3418 EXPECT_EQ(0, pool_
->NumConnectJobsInGroup("b"));
3419 EXPECT_EQ(0, pool_
->NumUnassignedConnectJobsInGroup("b"));
3420 EXPECT_EQ(kDefaultMaxSocketsPerGroup
, pool_
->IdleSocketCountInGroup("b"));
3423 TEST_F(ClientSocketPoolBaseTest
, RequestSocketsSynchronousError
) {
3424 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
3425 connect_job_factory_
->set_job_type(TestConnectJob::kMockFailingJob
);
3427 pool_
->RequestSockets("a", ¶ms_
, kDefaultMaxSocketsPerGroup
,
3430 ASSERT_FALSE(pool_
->HasGroup("a"));
3432 connect_job_factory_
->set_job_type(
3433 TestConnectJob::kMockAdditionalErrorStateJob
);
3434 pool_
->RequestSockets("a", ¶ms_
, kDefaultMaxSocketsPerGroup
,
3437 ASSERT_FALSE(pool_
->HasGroup("a"));
3440 TEST_F(ClientSocketPoolBaseTest
, RequestSocketsMultipleTimesDoesNothing
) {
3442 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
3444 pool_
->RequestSockets("a", ¶ms_
, 2, BoundNetLog());
3446 ASSERT_TRUE(pool_
->HasGroup("a"));
3447 EXPECT_EQ(2, pool_
->NumConnectJobsInGroup("a"));
3448 EXPECT_EQ(2, pool_
->NumUnassignedConnectJobsInGroup("a"));
3449 EXPECT_EQ(0, pool_
->IdleSocketCountInGroup("a"));
3451 pool_
->RequestSockets("a", ¶ms_
, 2, BoundNetLog());
3452 EXPECT_EQ(2, pool_
->NumConnectJobsInGroup("a"));
3453 EXPECT_EQ(2, pool_
->NumUnassignedConnectJobsInGroup("a"));
3454 EXPECT_EQ(0, pool_
->IdleSocketCountInGroup("a"));
3456 ClientSocketHandle handle1
;
3457 TestCompletionCallback callback1
;
3458 EXPECT_EQ(ERR_IO_PENDING
, handle1
.Init("a",
3461 callback1
.callback(),
3464 ASSERT_EQ(OK
, callback1
.WaitForResult());
3466 ClientSocketHandle handle2
;
3467 TestCompletionCallback callback2
;
3468 int rv
= handle2
.Init("a",
3471 callback2
.callback(),
3475 EXPECT_EQ(ERR_IO_PENDING
, rv
);
3476 EXPECT_EQ(OK
, callback2
.WaitForResult());
3479 EXPECT_EQ(0, pool_
->NumConnectJobsInGroup("a"));
3480 EXPECT_EQ(0, pool_
->NumUnassignedConnectJobsInGroup("a"));
3481 EXPECT_EQ(2, pool_
->NumActiveSocketsInGroup("a"));
3482 EXPECT_EQ(0, pool_
->IdleSocketCountInGroup("a"));
3487 EXPECT_EQ(0, pool_
->NumConnectJobsInGroup("a"));
3488 EXPECT_EQ(0, pool_
->NumUnassignedConnectJobsInGroup("a"));
3489 EXPECT_EQ(2, pool_
->IdleSocketCountInGroup("a"));
3491 pool_
->RequestSockets("a", ¶ms_
, 2, BoundNetLog());
3492 EXPECT_EQ(0, pool_
->NumConnectJobsInGroup("a"));
3493 EXPECT_EQ(0, pool_
->NumUnassignedConnectJobsInGroup("a"));
3494 EXPECT_EQ(2, pool_
->IdleSocketCountInGroup("a"));
3497 TEST_F(ClientSocketPoolBaseTest
, RequestSocketsDifferentNumSockets
) {
3499 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
3501 pool_
->RequestSockets("a", ¶ms_
, 1, BoundNetLog());
3503 ASSERT_TRUE(pool_
->HasGroup("a"));
3504 EXPECT_EQ(1, pool_
->NumConnectJobsInGroup("a"));
3505 EXPECT_EQ(1, pool_
->NumUnassignedConnectJobsInGroup("a"));
3506 EXPECT_EQ(0, pool_
->IdleSocketCountInGroup("a"));
3508 pool_
->RequestSockets("a", ¶ms_
, 2, BoundNetLog());
3509 EXPECT_EQ(2, pool_
->NumConnectJobsInGroup("a"));
3510 EXPECT_EQ(2, pool_
->NumUnassignedConnectJobsInGroup("a"));
3511 EXPECT_EQ(0, pool_
->IdleSocketCountInGroup("a"));
3513 pool_
->RequestSockets("a", ¶ms_
, 3, BoundNetLog());
3514 EXPECT_EQ(3, pool_
->NumConnectJobsInGroup("a"));
3515 EXPECT_EQ(3, pool_
->NumUnassignedConnectJobsInGroup("a"));
3516 EXPECT_EQ(0, pool_
->IdleSocketCountInGroup("a"));
3518 pool_
->RequestSockets("a", ¶ms_
, 1, BoundNetLog());
3519 EXPECT_EQ(3, pool_
->NumConnectJobsInGroup("a"));
3520 EXPECT_EQ(3, pool_
->NumUnassignedConnectJobsInGroup("a"));
3521 EXPECT_EQ(0, pool_
->IdleSocketCountInGroup("a"));
3524 TEST_F(ClientSocketPoolBaseTest
, PreconnectJobsTakenByNormalRequests
) {
3525 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
3526 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
3528 pool_
->RequestSockets("a", ¶ms_
, 1, BoundNetLog());
3530 ASSERT_TRUE(pool_
->HasGroup("a"));
3531 EXPECT_EQ(1, pool_
->NumConnectJobsInGroup("a"));
3532 EXPECT_EQ(1, pool_
->NumUnassignedConnectJobsInGroup("a"));
3533 EXPECT_EQ(0, pool_
->IdleSocketCountInGroup("a"));
3535 ClientSocketHandle handle1
;
3536 TestCompletionCallback callback1
;
3537 EXPECT_EQ(ERR_IO_PENDING
, handle1
.Init("a",
3540 callback1
.callback(),
3544 EXPECT_EQ(1, pool_
->NumConnectJobsInGroup("a"));
3545 EXPECT_EQ(0, pool_
->NumUnassignedConnectJobsInGroup("a"));
3546 EXPECT_EQ(0, pool_
->IdleSocketCountInGroup("a"));
3548 ASSERT_EQ(OK
, callback1
.WaitForResult());
3550 // Make sure if a preconnected socket is not fully connected when a request
3551 // starts, it has a connect start time.
3552 TestLoadTimingInfoConnectedNotReused(handle1
);
3555 EXPECT_EQ(1, pool_
->IdleSocketCountInGroup("a"));
3558 // Checks that fully connected preconnect jobs have no connect times, and are
3559 // marked as reused.
3560 TEST_F(ClientSocketPoolBaseTest
, ConnectedPreconnectJobsHaveNoConnectTimes
) {
3561 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
3562 connect_job_factory_
->set_job_type(TestConnectJob::kMockJob
);
3563 pool_
->RequestSockets("a", ¶ms_
, 1, BoundNetLog());
3565 ASSERT_TRUE(pool_
->HasGroup("a"));
3566 EXPECT_EQ(0, pool_
->NumConnectJobsInGroup("a"));
3567 EXPECT_EQ(0, pool_
->NumUnassignedConnectJobsInGroup("a"));
3568 EXPECT_EQ(1, pool_
->IdleSocketCountInGroup("a"));
3570 ClientSocketHandle handle
;
3571 TestCompletionCallback callback
;
3572 EXPECT_EQ(OK
, handle
.Init("a",
3575 callback
.callback(),
3579 // Make sure the idle socket was used.
3580 EXPECT_EQ(0, pool_
->IdleSocketCountInGroup("a"));
3582 TestLoadTimingInfoConnectedReused(handle
);
3584 TestLoadTimingInfoNotConnected(handle
);
3587 // http://crbug.com/64940 regression test.
3588 TEST_F(ClientSocketPoolBaseTest
, PreconnectClosesIdleSocketRemovesGroup
) {
3589 const int kMaxTotalSockets
= 3;
3590 const int kMaxSocketsPerGroup
= 2;
3591 CreatePool(kMaxTotalSockets
, kMaxSocketsPerGroup
);
3592 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
3594 // Note that group name ordering matters here. "a" comes before "b", so
3595 // CloseOneIdleSocket() will try to close "a"'s idle socket.
3597 // Set up one idle socket in "a".
3598 ClientSocketHandle handle1
;
3599 TestCompletionCallback callback1
;
3600 EXPECT_EQ(ERR_IO_PENDING
, handle1
.Init("a",
3603 callback1
.callback(),
3607 ASSERT_EQ(OK
, callback1
.WaitForResult());
3609 EXPECT_EQ(1, pool_
->IdleSocketCountInGroup("a"));
3611 // Set up two active sockets in "b".
3612 ClientSocketHandle handle2
;
3613 TestCompletionCallback callback2
;
3614 EXPECT_EQ(ERR_IO_PENDING
, handle1
.Init("b",
3617 callback1
.callback(),
3620 EXPECT_EQ(ERR_IO_PENDING
, handle2
.Init("b",
3623 callback2
.callback(),
3627 ASSERT_EQ(OK
, callback1
.WaitForResult());
3628 ASSERT_EQ(OK
, callback2
.WaitForResult());
3629 EXPECT_EQ(0, pool_
->IdleSocketCountInGroup("b"));
3630 EXPECT_EQ(0, pool_
->NumUnassignedConnectJobsInGroup("b"));
3631 EXPECT_EQ(2, pool_
->NumActiveSocketsInGroup("b"));
3633 // Now we have 1 idle socket in "a" and 2 active sockets in "b". This means
3634 // we've maxed out on sockets, since we set |kMaxTotalSockets| to 3.
3635 // Requesting 2 preconnected sockets for "a" should fail to allocate any more
3636 // sockets for "a", and "b" should still have 2 active sockets.
3638 pool_
->RequestSockets("a", ¶ms_
, 2, BoundNetLog());
3639 EXPECT_EQ(0, pool_
->NumConnectJobsInGroup("a"));
3640 EXPECT_EQ(0, pool_
->NumUnassignedConnectJobsInGroup("a"));
3641 EXPECT_EQ(1, pool_
->IdleSocketCountInGroup("a"));
3642 EXPECT_EQ(0, pool_
->NumActiveSocketsInGroup("a"));
3643 EXPECT_EQ(0, pool_
->NumConnectJobsInGroup("b"));
3644 EXPECT_EQ(0, pool_
->NumUnassignedConnectJobsInGroup("b"));
3645 EXPECT_EQ(0, pool_
->IdleSocketCountInGroup("b"));
3646 EXPECT_EQ(2, pool_
->NumActiveSocketsInGroup("b"));
3648 // Now release the 2 active sockets for "b". This will give us 1 idle socket
3649 // in "a" and 2 idle sockets in "b". Requesting 2 preconnected sockets for
3650 // "a" should result in closing 1 for "b".
3653 EXPECT_EQ(2, pool_
->IdleSocketCountInGroup("b"));
3654 EXPECT_EQ(0, pool_
->NumActiveSocketsInGroup("b"));
3656 pool_
->RequestSockets("a", ¶ms_
, 2, BoundNetLog());
3657 EXPECT_EQ(1, pool_
->NumConnectJobsInGroup("a"));
3658 EXPECT_EQ(1, pool_
->NumUnassignedConnectJobsInGroup("a"));
3659 EXPECT_EQ(1, pool_
->IdleSocketCountInGroup("a"));
3660 EXPECT_EQ(0, pool_
->NumActiveSocketsInGroup("a"));
3661 EXPECT_EQ(0, pool_
->NumConnectJobsInGroup("b"));
3662 EXPECT_EQ(0, pool_
->NumUnassignedConnectJobsInGroup("b"));
3663 EXPECT_EQ(1, pool_
->IdleSocketCountInGroup("b"));
3664 EXPECT_EQ(0, pool_
->NumActiveSocketsInGroup("b"));
3667 TEST_F(ClientSocketPoolBaseTest
, PreconnectWithoutBackupJob
) {
3668 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
3669 pool_
->EnableConnectBackupJobs();
3671 // Make the ConnectJob hang until it times out, shorten the timeout.
3672 connect_job_factory_
->set_job_type(TestConnectJob::kMockWaitingJob
);
3673 connect_job_factory_
->set_timeout_duration(
3674 base::TimeDelta::FromMilliseconds(500));
3675 pool_
->RequestSockets("a", ¶ms_
, 1, BoundNetLog());
3676 EXPECT_EQ(1, pool_
->NumConnectJobsInGroup("a"));
3677 EXPECT_EQ(1, pool_
->NumUnassignedConnectJobsInGroup("a"));
3678 EXPECT_EQ(0, pool_
->IdleSocketCountInGroup("a"));
3680 // Verify the backup timer doesn't create a backup job, by making
3681 // the backup job a pending job instead of a waiting job, so it
3682 // *would* complete if it were created.
3683 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
3684 base::MessageLoop::current()->PostDelayedTask(
3686 base::MessageLoop::QuitClosure(),
3687 base::TimeDelta::FromSeconds(1));
3688 base::MessageLoop::current()->Run();
3689 EXPECT_FALSE(pool_
->HasGroup("a"));
3692 TEST_F(ClientSocketPoolBaseTest
, PreconnectWithBackupJob
) {
3693 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
3694 pool_
->EnableConnectBackupJobs();
3696 // Make the ConnectJob hang forever.
3697 connect_job_factory_
->set_job_type(TestConnectJob::kMockWaitingJob
);
3698 pool_
->RequestSockets("a", ¶ms_
, 1, BoundNetLog());
3699 EXPECT_EQ(1, pool_
->NumConnectJobsInGroup("a"));
3700 EXPECT_EQ(1, pool_
->NumUnassignedConnectJobsInGroup("a"));
3701 EXPECT_EQ(0, pool_
->IdleSocketCountInGroup("a"));
3702 base::MessageLoop::current()->RunUntilIdle();
3704 // Make the backup job be a pending job, so it completes normally.
3705 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
3706 ClientSocketHandle handle
;
3707 TestCompletionCallback callback
;
3708 EXPECT_EQ(ERR_IO_PENDING
, handle
.Init("a",
3711 callback
.callback(),
3714 // Timer has started, but the backup connect job shouldn't be created yet.
3715 EXPECT_EQ(1, pool_
->NumConnectJobsInGroup("a"));
3716 EXPECT_EQ(0, pool_
->NumUnassignedConnectJobsInGroup("a"));
3717 EXPECT_EQ(0, pool_
->IdleSocketCountInGroup("a"));
3718 EXPECT_EQ(0, pool_
->NumActiveSocketsInGroup("a"));
3719 ASSERT_EQ(OK
, callback
.WaitForResult());
3721 // The hung connect job should still be there, but everything else should be
3723 EXPECT_EQ(1, pool_
->NumConnectJobsInGroup("a"));
3724 EXPECT_EQ(0, pool_
->NumUnassignedConnectJobsInGroup("a"));
3725 EXPECT_EQ(0, pool_
->IdleSocketCountInGroup("a"));
3726 EXPECT_EQ(1, pool_
->NumActiveSocketsInGroup("a"));
3729 // Tests that a preconnect that starts out with unread data can still be used.
3730 // http://crbug.com/334467
3731 TEST_F(ClientSocketPoolBaseTest
, PreconnectWithUnreadData
) {
3732 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
3733 connect_job_factory_
->set_job_type(TestConnectJob::kMockUnreadDataJob
);
3735 pool_
->RequestSockets("a", ¶ms_
, 1, BoundNetLog());
3737 ASSERT_TRUE(pool_
->HasGroup("a"));
3738 EXPECT_EQ(0, pool_
->NumConnectJobsInGroup("a"));
3739 EXPECT_EQ(0, pool_
->NumUnassignedConnectJobsInGroup("a"));
3740 EXPECT_EQ(1, pool_
->IdleSocketCountInGroup("a"));
3742 // Fail future jobs to be sure that handle receives the preconnected socket
3743 // rather than closing it and making a new one.
3744 connect_job_factory_
->set_job_type(TestConnectJob::kMockFailingJob
);
3745 ClientSocketHandle handle
;
3746 TestCompletionCallback callback
;
3747 EXPECT_EQ(OK
, handle
.Init("a",
3750 callback
.callback(),
3754 ASSERT_TRUE(pool_
->HasGroup("a"));
3755 EXPECT_EQ(0, pool_
->NumConnectJobsInGroup("a"));
3756 EXPECT_EQ(0, pool_
->NumUnassignedConnectJobsInGroup("a"));
3757 EXPECT_EQ(0, pool_
->IdleSocketCountInGroup("a"));
3759 // Drain the pending read.
3760 EXPECT_EQ(1, handle
.socket()->Read(NULL
, 1, CompletionCallback()));
3762 TestLoadTimingInfoConnectedReused(handle
);
3765 // The socket should be usable now that it's idle again.
3766 EXPECT_EQ(1, pool_
->IdleSocketCountInGroup("a"));
3769 class MockLayeredPool
: public HigherLayeredPool
{
3771 MockLayeredPool(TestClientSocketPool
* pool
,
3772 const std::string
& group_name
)
3774 group_name_(group_name
),
3775 can_release_connection_(true) {
3776 pool_
->AddHigherLayeredPool(this);
3779 ~MockLayeredPool() {
3780 pool_
->RemoveHigherLayeredPool(this);
3783 int RequestSocket(TestClientSocketPool
* pool
) {
3784 scoped_refptr
<TestSocketParams
> params(
3785 new TestSocketParams(false /* ignore_limits */));
3786 return handle_
.Init(group_name_
, params
, DEFAULT_PRIORITY
,
3787 callback_
.callback(), pool
, BoundNetLog());
3790 int RequestSocketWithoutLimits(TestClientSocketPool
* pool
) {
3791 scoped_refptr
<TestSocketParams
> params(
3792 new TestSocketParams(true /* ignore_limits */));
3793 return handle_
.Init(group_name_
, params
, MAXIMUM_PRIORITY
,
3794 callback_
.callback(), pool
, BoundNetLog());
3797 bool ReleaseOneConnection() {
3798 if (!handle_
.is_initialized() || !can_release_connection_
) {
3801 handle_
.socket()->Disconnect();
3806 void set_can_release_connection(bool can_release_connection
) {
3807 can_release_connection_
= can_release_connection
;
3810 MOCK_METHOD0(CloseOneIdleConnection
, bool());
3813 TestClientSocketPool
* const pool_
;
3814 ClientSocketHandle handle_
;
3815 TestCompletionCallback callback_
;
3816 const std::string group_name_
;
3817 bool can_release_connection_
;
3820 TEST_F(ClientSocketPoolBaseTest
, FailToCloseIdleSocketsNotHeldByLayeredPool
) {
3821 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
3822 connect_job_factory_
->set_job_type(TestConnectJob::kMockJob
);
3824 MockLayeredPool
mock_layered_pool(pool_
.get(), "foo");
3825 EXPECT_EQ(OK
, mock_layered_pool
.RequestSocket(pool_
.get()));
3826 EXPECT_CALL(mock_layered_pool
, CloseOneIdleConnection())
3827 .WillOnce(Return(false));
3828 EXPECT_FALSE(pool_
->CloseOneIdleConnectionInHigherLayeredPool());
3831 TEST_F(ClientSocketPoolBaseTest
, ForciblyCloseIdleSocketsHeldByLayeredPool
) {
3832 CreatePool(kDefaultMaxSockets
, kDefaultMaxSocketsPerGroup
);
3833 connect_job_factory_
->set_job_type(TestConnectJob::kMockJob
);
3835 MockLayeredPool
mock_layered_pool(pool_
.get(), "foo");
3836 EXPECT_EQ(OK
, mock_layered_pool
.RequestSocket(pool_
.get()));
3837 EXPECT_CALL(mock_layered_pool
, CloseOneIdleConnection())
3838 .WillOnce(Invoke(&mock_layered_pool
,
3839 &MockLayeredPool::ReleaseOneConnection
));
3840 EXPECT_TRUE(pool_
->CloseOneIdleConnectionInHigherLayeredPool());
3843 // Tests the basic case of closing an idle socket in a higher layered pool when
3844 // a new request is issued and the lower layer pool is stalled.
3845 TEST_F(ClientSocketPoolBaseTest
, CloseIdleSocketsHeldByLayeredPoolWhenNeeded
) {
3847 connect_job_factory_
->set_job_type(TestConnectJob::kMockJob
);
3849 MockLayeredPool
mock_layered_pool(pool_
.get(), "foo");
3850 EXPECT_EQ(OK
, mock_layered_pool
.RequestSocket(pool_
.get()));
3851 EXPECT_CALL(mock_layered_pool
, CloseOneIdleConnection())
3852 .WillOnce(Invoke(&mock_layered_pool
,
3853 &MockLayeredPool::ReleaseOneConnection
));
3854 ClientSocketHandle handle
;
3855 TestCompletionCallback callback
;
3856 EXPECT_EQ(ERR_IO_PENDING
, handle
.Init("a",
3859 callback
.callback(),
3862 EXPECT_EQ(OK
, callback
.WaitForResult());
3865 // Same as above, but the idle socket is in the same group as the stalled
3866 // socket, and closes the only other request in its group when closing requests
3867 // in higher layered pools. This generally shouldn't happen, but it may be
3868 // possible if a higher level pool issues a request and the request is
3869 // subsequently cancelled. Even if it's not possible, best not to crash.
3870 TEST_F(ClientSocketPoolBaseTest
,
3871 CloseIdleSocketsHeldByLayeredPoolWhenNeededSameGroup
) {
3873 connect_job_factory_
->set_job_type(TestConnectJob::kMockJob
);
3875 // Need a socket in another group for the pool to be stalled (If a group
3876 // has the maximum number of connections already, it's not stalled).
3877 ClientSocketHandle handle1
;
3878 TestCompletionCallback callback1
;
3879 EXPECT_EQ(OK
, handle1
.Init("group1",
3882 callback1
.callback(),
3886 MockLayeredPool
mock_layered_pool(pool_
.get(), "group2");
3887 EXPECT_EQ(OK
, mock_layered_pool
.RequestSocket(pool_
.get()));
3888 EXPECT_CALL(mock_layered_pool
, CloseOneIdleConnection())
3889 .WillOnce(Invoke(&mock_layered_pool
,
3890 &MockLayeredPool::ReleaseOneConnection
));
3891 ClientSocketHandle handle
;
3892 TestCompletionCallback callback2
;
3893 EXPECT_EQ(ERR_IO_PENDING
, handle
.Init("group2",
3896 callback2
.callback(),
3899 EXPECT_EQ(OK
, callback2
.WaitForResult());
3902 // Tests the case when an idle socket can be closed when a new request is
3903 // issued, and the new request belongs to a group that was previously stalled.
3904 TEST_F(ClientSocketPoolBaseTest
,
3905 CloseIdleSocketsHeldByLayeredPoolInSameGroupWhenNeeded
) {
3907 std::list
<TestConnectJob::JobType
> job_types
;
3908 job_types
.push_back(TestConnectJob::kMockJob
);
3909 job_types
.push_back(TestConnectJob::kMockJob
);
3910 job_types
.push_back(TestConnectJob::kMockJob
);
3911 job_types
.push_back(TestConnectJob::kMockJob
);
3912 connect_job_factory_
->set_job_types(&job_types
);
3914 ClientSocketHandle handle1
;
3915 TestCompletionCallback callback1
;
3916 EXPECT_EQ(OK
, handle1
.Init("group1",
3919 callback1
.callback(),
3923 MockLayeredPool
mock_layered_pool(pool_
.get(), "group2");
3924 EXPECT_EQ(OK
, mock_layered_pool
.RequestSocket(pool_
.get()));
3925 EXPECT_CALL(mock_layered_pool
, CloseOneIdleConnection())
3926 .WillRepeatedly(Invoke(&mock_layered_pool
,
3927 &MockLayeredPool::ReleaseOneConnection
));
3928 mock_layered_pool
.set_can_release_connection(false);
3930 // The third request is made when the socket pool is in a stalled state.
3931 ClientSocketHandle handle3
;
3932 TestCompletionCallback callback3
;
3933 EXPECT_EQ(ERR_IO_PENDING
, handle3
.Init("group3",
3936 callback3
.callback(),
3940 base::RunLoop().RunUntilIdle();
3941 EXPECT_FALSE(callback3
.have_result());
3943 // The fourth request is made when the pool is no longer stalled. The third
3944 // request should be serviced first, since it was issued first and has the
3946 mock_layered_pool
.set_can_release_connection(true);
3947 ClientSocketHandle handle4
;
3948 TestCompletionCallback callback4
;
3949 EXPECT_EQ(ERR_IO_PENDING
, handle4
.Init("group3",
3952 callback4
.callback(),
3955 EXPECT_EQ(OK
, callback3
.WaitForResult());
3956 EXPECT_FALSE(callback4
.have_result());
3958 // Closing a handle should free up another socket slot.
3960 EXPECT_EQ(OK
, callback4
.WaitForResult());
3963 // Tests the case when an idle socket can be closed when a new request is
3964 // issued, and the new request belongs to a group that was previously stalled.
3966 // The two differences from the above test are that the stalled requests are not
3967 // in the same group as the layered pool's request, and the the fourth request
3968 // has a higher priority than the third one, so gets a socket first.
3969 TEST_F(ClientSocketPoolBaseTest
,
3970 CloseIdleSocketsHeldByLayeredPoolInSameGroupWhenNeeded2
) {
3972 std::list
<TestConnectJob::JobType
> job_types
;
3973 job_types
.push_back(TestConnectJob::kMockJob
);
3974 job_types
.push_back(TestConnectJob::kMockJob
);
3975 job_types
.push_back(TestConnectJob::kMockJob
);
3976 job_types
.push_back(TestConnectJob::kMockJob
);
3977 connect_job_factory_
->set_job_types(&job_types
);
3979 ClientSocketHandle handle1
;
3980 TestCompletionCallback callback1
;
3981 EXPECT_EQ(OK
, handle1
.Init("group1",
3984 callback1
.callback(),
3988 MockLayeredPool
mock_layered_pool(pool_
.get(), "group2");
3989 EXPECT_EQ(OK
, mock_layered_pool
.RequestSocket(pool_
.get()));
3990 EXPECT_CALL(mock_layered_pool
, CloseOneIdleConnection())
3991 .WillRepeatedly(Invoke(&mock_layered_pool
,
3992 &MockLayeredPool::ReleaseOneConnection
));
3993 mock_layered_pool
.set_can_release_connection(false);
3995 // The third request is made when the socket pool is in a stalled state.
3996 ClientSocketHandle handle3
;
3997 TestCompletionCallback callback3
;
3998 EXPECT_EQ(ERR_IO_PENDING
, handle3
.Init("group3",
4001 callback3
.callback(),
4005 base::RunLoop().RunUntilIdle();
4006 EXPECT_FALSE(callback3
.have_result());
4008 // The fourth request is made when the pool is no longer stalled. This
4009 // request has a higher priority than the third request, so is serviced first.
4010 mock_layered_pool
.set_can_release_connection(true);
4011 ClientSocketHandle handle4
;
4012 TestCompletionCallback callback4
;
4013 EXPECT_EQ(ERR_IO_PENDING
, handle4
.Init("group3",
4016 callback4
.callback(),
4019 EXPECT_EQ(OK
, callback4
.WaitForResult());
4020 EXPECT_FALSE(callback3
.have_result());
4022 // Closing a handle should free up another socket slot.
4024 EXPECT_EQ(OK
, callback3
.WaitForResult());
4027 TEST_F(ClientSocketPoolBaseTest
,
4028 CloseMultipleIdleSocketsHeldByLayeredPoolWhenNeeded
) {
4030 connect_job_factory_
->set_job_type(TestConnectJob::kMockJob
);
4032 MockLayeredPool
mock_layered_pool1(pool_
.get(), "foo");
4033 EXPECT_EQ(OK
, mock_layered_pool1
.RequestSocket(pool_
.get()));
4034 EXPECT_CALL(mock_layered_pool1
, CloseOneIdleConnection())
4035 .WillRepeatedly(Invoke(&mock_layered_pool1
,
4036 &MockLayeredPool::ReleaseOneConnection
));
4037 MockLayeredPool
mock_layered_pool2(pool_
.get(), "bar");
4038 EXPECT_EQ(OK
, mock_layered_pool2
.RequestSocketWithoutLimits(pool_
.get()));
4039 EXPECT_CALL(mock_layered_pool2
, CloseOneIdleConnection())
4040 .WillRepeatedly(Invoke(&mock_layered_pool2
,
4041 &MockLayeredPool::ReleaseOneConnection
));
4042 ClientSocketHandle handle
;
4043 TestCompletionCallback callback
;
4044 EXPECT_EQ(ERR_IO_PENDING
, handle
.Init("a",
4047 callback
.callback(),
4050 EXPECT_EQ(OK
, callback
.WaitForResult());
4053 // Test that when a socket pool and group are at their limits, a request
4054 // with |ignore_limits| triggers creation of a new socket, and gets the socket
4055 // instead of a request with the same priority that was issued earlier, but
4056 // that does not have |ignore_limits| set.
4057 TEST_F(ClientSocketPoolBaseTest
, IgnoreLimits
) {
4058 scoped_refptr
<TestSocketParams
> params_ignore_limits(
4059 new TestSocketParams(true /* ignore_limits */));
4062 // Issue a request to reach the socket pool limit.
4063 EXPECT_EQ(OK
, StartRequestWithParams("a", MAXIMUM_PRIORITY
, params_
));
4064 EXPECT_EQ(0, pool_
->NumConnectJobsInGroup("a"));
4066 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
4068 EXPECT_EQ(ERR_IO_PENDING
, StartRequestWithParams("a", MAXIMUM_PRIORITY
,
4070 EXPECT_EQ(0, pool_
->NumConnectJobsInGroup("a"));
4072 EXPECT_EQ(ERR_IO_PENDING
, StartRequestWithParams("a", MAXIMUM_PRIORITY
,
4073 params_ignore_limits
));
4074 ASSERT_EQ(1, pool_
->NumConnectJobsInGroup("a"));
4076 EXPECT_EQ(OK
, request(2)->WaitForResult());
4077 EXPECT_FALSE(request(1)->have_result());
4080 // Test that when a socket pool and group are at their limits, a ConnectJob
4081 // issued for a request with |ignore_limits| set is not cancelled when a request
4082 // without |ignore_limits| issued to the same group is cancelled.
4083 TEST_F(ClientSocketPoolBaseTest
, IgnoreLimitsCancelOtherJob
) {
4084 scoped_refptr
<TestSocketParams
> params_ignore_limits(
4085 new TestSocketParams(true /* ignore_limits */));
4088 // Issue a request to reach the socket pool limit.
4089 EXPECT_EQ(OK
, StartRequestWithParams("a", MAXIMUM_PRIORITY
, params_
));
4090 EXPECT_EQ(0, pool_
->NumConnectJobsInGroup("a"));
4092 connect_job_factory_
->set_job_type(TestConnectJob::kMockPendingJob
);
4094 EXPECT_EQ(ERR_IO_PENDING
, StartRequestWithParams("a", MAXIMUM_PRIORITY
,
4096 EXPECT_EQ(0, pool_
->NumConnectJobsInGroup("a"));
4098 EXPECT_EQ(ERR_IO_PENDING
, StartRequestWithParams("a", MAXIMUM_PRIORITY
,
4099 params_ignore_limits
));
4100 ASSERT_EQ(1, pool_
->NumConnectJobsInGroup("a"));
4102 // Cancel the pending request without ignore_limits set. The ConnectJob
4103 // should not be cancelled.
4104 request(1)->handle()->Reset();
4105 ASSERT_EQ(1, pool_
->NumConnectJobsInGroup("a"));
4107 EXPECT_EQ(OK
, request(2)->WaitForResult());
4108 EXPECT_FALSE(request(1)->have_result());