Rename InputLatency::ScrollUpdate to Latency::ScrollUpdate
[chromium-blink-merge.git] / net / socket / client_socket_pool_base_unittest.cc
blob2de67bdbd67413e0d091c5ef800d0ecd0cd77e73
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"
7 #include <vector>
9 #include "base/bind.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;
41 namespace net {
43 namespace {
45 const int kDefaultMaxSockets = 4;
46 const int kDefaultMaxSocketsPerGroup = 2;
48 // Make sure |handle| sets load times correctly when it has been assigned a
49 // reused socket.
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
66 // reused.
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> {
100 public:
101 explicit TestSocketParams(bool ignore_limits)
102 : ignore_limits_(ignore_limits) {}
104 bool ignore_limits() { return ignore_limits_; }
106 private:
107 friend class base::RefCounted<TestSocketParams>;
108 ~TestSocketParams() {}
110 const bool ignore_limits_;
112 typedef ClientSocketPoolBase<TestSocketParams> TestClientSocketPoolBase;
114 class MockClientSocket : public StreamSocket {
115 public:
116 explicit MockClientSocket(net::NetLog* net_log)
117 : connected_(false),
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 */,
131 int len,
132 const CompletionCallback& /* callback */) override {
133 if (has_unread_data_ && len > 0) {
134 has_unread_data_ = false;
135 was_used_to_convey_data_ = true;
136 return 1;
138 return ERR_UNEXPECTED;
141 int Write(IOBuffer* /* buf */,
142 int len,
143 const CompletionCallback& /* callback */) override {
144 was_used_to_convey_data_ = true;
145 return len;
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 {
152 connected_ = true;
153 return OK;
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; }
180 private:
181 bool connected_;
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 {
192 public:
193 MockClientSocketFactory() : allocation_count_(0) {}
195 scoped_ptr<DatagramClientSocket> CreateDatagramClientSocket(
196 DatagramSocket::BindType bind_type,
197 const RandIntCallback& rand_int_cb,
198 NetLog* net_log,
199 const NetLog::Source& source) override {
200 NOTREACHED();
201 return scoped_ptr<DatagramClientSocket>();
204 scoped_ptr<StreamSocket> CreateTransportClientSocket(
205 const AddressList& addresses,
206 NetLog* /* net_log */,
207 const NetLog::Source& /*source*/) override {
208 allocation_count_++;
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 {
217 NOTIMPLEMENTED();
218 return scoped_ptr<SSLClientSocket>();
221 void ClearSSLSessionCache() override { NOTIMPLEMENTED(); }
223 void WaitForSignal(TestConnectJob* job) { waiting_jobs_.push_back(job); }
225 void SignalJobs();
227 void SignalJob(size_t job);
229 void SetJobLoadState(size_t job, LoadState load_state);
231 int allocation_count() const { return allocation_count_; }
233 private:
234 int allocation_count_;
235 std::vector<TestConnectJob*> waiting_jobs_;
238 class TestConnectJob : public ConnectJob {
239 public:
240 enum JobType {
241 kMockJob,
242 kMockFailingJob,
243 kMockPendingJob,
244 kMockPendingFailingJob,
245 kMockWaitingJob,
246 kMockRecoverableJob,
247 kMockPendingRecoverableJob,
248 kMockAdditionalErrorStateJob,
249 kMockPendingAdditionalErrorStateJob,
250 kMockUnreadDataJob,
253 // The kMockPendingJob uses a slight delay before allowing the connect
254 // to complete.
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,
263 NetLog* net_log)
264 : ConnectJob(group_name, timeout_duration, request.priority(), delegate,
265 BoundNetLog::Make(net_log, NetLog::SOURCE_CONNECT_JOB)),
266 job_type_(job_type),
267 client_socket_factory_(client_socket_factory),
268 load_state_(LOAD_STATE_IDLE),
269 store_additional_error_state_(false),
270 weak_factory_(this) {
273 void Signal() {
274 DoConnect(waiting_success_, true /* async */, false /* recoverable */);
277 void set_load_state(LoadState load_state) { load_state_ = load_state; }
279 // From ConnectJob:
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);
293 private:
294 // From ConnectJob:
296 int ConnectInternal() override {
297 AddressList ignored;
298 client_socket_factory_->CreateTransportClientSocket(
299 ignored, NULL, net::NetLog::Source());
300 SetSocket(
301 scoped_ptr<StreamSocket>(new MockClientSocket(net_log().net_log())));
302 switch (job_type_) {
303 case kMockJob:
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(
324 FROM_HERE,
325 base::Bind(base::IgnoreResult(&TestConnectJob::DoConnect),
326 weak_factory_.GetWeakPtr(),
327 true /* successful */,
328 true /* async */,
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(
335 FROM_HERE,
336 base::Bind(base::IgnoreResult(&TestConnectJob::DoConnect),
337 weak_factory_.GetWeakPtr(),
338 false /* error */,
339 true /* async */,
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(
354 FROM_HERE,
355 base::Bind(base::IgnoreResult(&TestConnectJob::DoConnect),
356 weak_factory_.GetWeakPtr(),
357 false /* error */,
358 true /* async */,
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(
370 FROM_HERE,
371 base::Bind(base::IgnoreResult(&TestConnectJob::DoConnect),
372 weak_factory_.GetWeakPtr(),
373 false /* error */,
374 true /* async */,
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);
382 return ret;
384 default:
385 NOTREACHED();
386 SetSocket(scoped_ptr<StreamSocket>());
387 return ERR_FAILED;
391 int DoConnect(bool succeed, bool was_async, bool recoverable) {
392 int result = OK;
393 if (succeed) {
394 socket()->Connect(CompletionCallback());
395 } else if (recoverable) {
396 result = ERR_PROXY_AUTH_REQUESTED;
397 } else {
398 result = ERR_CONNECTION_FAILED;
399 SetSocket(scoped_ptr<StreamSocket>());
402 if (was_async)
403 NotifyDelegateOfCompletion(result);
404 return 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 {
420 public:
421 TestConnectJobFactory(MockClientSocketFactory* client_socket_factory,
422 NetLog* net_log)
423 : job_type_(TestConnectJob::kMockJob),
424 job_types_(NULL),
425 client_socket_factory_(client_socket_factory),
426 net_log_(net_log) {
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,
455 group_name,
456 request,
457 timeout_duration_,
458 delegate,
459 client_socket_factory_,
460 net_log_));
463 base::TimeDelta ConnectionTimeout() const override {
464 return timeout_duration_;
467 private:
468 TestConnectJob::JobType job_type_;
469 std::list<TestConnectJob::JobType>* job_types_;
470 base::TimeDelta timeout_duration_;
471 MockClientSocketFactory* const client_socket_factory_;
472 NetLog* net_log_;
474 DISALLOW_COPY_AND_ASSIGN(TestConnectJobFactory);
477 class TestClientSocketPool : public ClientSocketPool {
478 public:
479 typedef TestSocketParams SocketParams;
481 TestClientSocketPool(
482 int max_sockets,
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)
487 : base_(NULL,
488 max_sockets,
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,
497 const void* params,
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,
509 const void* params,
510 int num_sockets,
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,
525 int id) override {
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();
591 private:
592 TestClientSocketPoolBase base_;
594 DISALLOW_COPY_AND_ASSIGN(TestClientSocketPool);
597 } // namespace
599 namespace {
601 void MockClientSocketFactory::SignalJobs() {
602 for (std::vector<TestConnectJob*>::iterator it = waiting_jobs_.begin();
603 it != waiting_jobs_.end(); ++it) {
604 (*it)->Signal();
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 {
622 public:
623 TestConnectJobDelegate()
624 : have_result_(false), waiting_for_result_(false), result_(OK) {}
625 ~TestConnectJobDelegate() override {}
627 void OnConnectJobComplete(int result, ConnectJob* job) override {
628 result_ = result;
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);
633 have_result_ = true;
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
646 return result_;
649 private:
650 bool have_result_;
651 bool waiting_for_result_;
652 int result_;
655 class ClientSocketPoolBaseTest : public testing::Test {
656 protected:
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(
675 max_sockets,
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_,
687 &net_log_);
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 TestNetLog 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
735 // completion.
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,
745 "a",
746 request,
747 base::TimeDelta::FromMicroseconds(1),
748 &delegate,
749 &client_socket_factory_,
750 NULL));
751 EXPECT_EQ(OK, job->Connect());
754 TEST_F(ClientSocketPoolBaseTest, ConnectJob_TimedOut) {
755 TestConnectJobDelegate delegate;
756 ClientSocketHandle ignored;
757 TestNetLog log;
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,
766 "a",
767 request,
768 base::TimeDelta::FromMicroseconds(1),
769 &delegate,
770 &client_socket_factory_,
771 &log);
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 TestNetLog::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 BoundTestNetLog log;
802 TestLoadTimingInfoNotConnected(handle);
804 EXPECT_EQ(OK,
805 handle.Init("a",
806 params_,
807 DEFAULT_PRIORITY,
808 callback.callback(),
809 pool_.get(),
810 log.bound()));
811 EXPECT_TRUE(handle.is_initialized());
812 EXPECT_TRUE(handle.socket());
813 TestLoadTimingInfoConnectedNotReused(handle);
815 handle.Reset();
816 TestLoadTimingInfoNotConnected(handle);
818 TestNetLog::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 BoundTestNetLog 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,
848 handle.Init("a",
849 params_,
850 DEFAULT_PRIORITY,
851 callback.callback(),
852 pool_.get(),
853 log.bound()));
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 TestNetLog::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,
1088 handle.Init("a",
1089 params_,
1090 DEFAULT_PRIORITY,
1091 callback.callback(),
1092 pool_.get(),
1093 BoundNetLog()));
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",
1100 params_,
1101 DEFAULT_PRIORITY,
1102 callback.callback(),
1103 pool_.get(),
1104 BoundNetLog()));
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)
1111 handles[i].Reset();
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),
1123 params_,
1124 DEFAULT_PRIORITY,
1125 callbacks[i].callback(),
1126 pool_.get(),
1127 BoundNetLog()));
1130 // Force a stalled group.
1131 ClientSocketHandle stalled_handle;
1132 TestCompletionCallback callback;
1133 EXPECT_EQ(ERR_IO_PENDING, stalled_handle.Init("foo",
1134 params_,
1135 DEFAULT_PRIORITY,
1136 callback.callback(),
1137 pool_.get(),
1138 BoundNetLog()));
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),
1162 params_,
1163 DEFAULT_PRIORITY,
1164 callback.callback(),
1165 pool_.get(),
1166 BoundNetLog()));
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",
1174 params_,
1175 DEFAULT_PRIORITY,
1176 callback.callback(),
1177 pool_.get(),
1178 BoundNetLog()));
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.
1185 handles[0].Reset();
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(
1218 "Take 2: %d", i),
1219 params_,
1220 DEFAULT_PRIORITY,
1221 callback.callback(),
1222 pool_.get(),
1223 BoundNetLog()));
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",
1232 params_,
1233 DEFAULT_PRIORITY,
1234 callback.callback(),
1235 pool_.get(),
1236 BoundNetLog()));
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),
1260 params_,
1261 DEFAULT_PRIORITY,
1262 callback.callback(),
1263 pool_.get(),
1264 BoundNetLog()));
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
1271 // reuse a socket.
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",
1280 params_,
1281 DEFAULT_PRIORITY,
1282 callback.callback(),
1283 pool_.get(),
1284 BoundNetLog()));
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",
1354 params_,
1355 DEFAULT_PRIORITY,
1356 callback.callback(),
1357 pool_.get(),
1358 BoundNetLog()));
1359 handle.Reset();
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",
1370 params_,
1371 DEFAULT_PRIORITY,
1372 callback.callback(),
1373 pool_.get(),
1374 BoundNetLog()));
1376 handle.Reset();
1378 TestCompletionCallback callback2;
1379 EXPECT_EQ(ERR_IO_PENDING,
1380 handle.Init("a",
1381 params_,
1382 DEFAULT_PRIORITY,
1383 callback2.callback(),
1384 pool_.get(),
1385 BoundNetLog()));
1387 EXPECT_EQ(OK, callback2.WaitForResult());
1388 EXPECT_FALSE(callback.have_result());
1390 handle.Reset();
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 {
1430 public:
1431 RequestSocketCallback(ClientSocketHandle* handle,
1432 TestClientSocketPool* pool,
1433 TestConnectJobFactory* test_connect_job_factory,
1434 TestConnectJob::JobType next_job_type)
1435 : handle_(handle),
1436 pool_(pool),
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_; }
1448 private:
1449 void OnComplete(int result) {
1450 SetResult(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();
1459 handle_->Reset();
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",
1473 params,
1474 DEFAULT_PRIORITY,
1475 next_job_callback.callback(),
1476 pool_,
1477 BoundNetLog());
1478 switch (next_job_type_) {
1479 case TestConnectJob::kMockJob:
1480 EXPECT_EQ(OK, rv);
1481 break;
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());
1497 break;
1498 default:
1499 FAIL() << "Unexpected job type: " << next_job_type_;
1500 break;
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",
1522 params_,
1523 DEFAULT_PRIORITY,
1524 callback.callback(),
1525 pool_.get(),
1526 BoundNetLog());
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",
1540 params_,
1541 DEFAULT_PRIORITY,
1542 callback.callback(),
1543 pool_.get(),
1544 BoundNetLog());
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
1551 // cancelled.
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",
1608 params_,
1609 DEFAULT_PRIORITY,
1610 callback.callback(),
1611 pool_.get(),
1612 BoundNetLog());
1613 EXPECT_EQ(ERR_IO_PENDING, rv);
1615 // Cancel the active request.
1616 handle.Reset();
1618 rv = handle.Init("a",
1619 params_,
1620 DEFAULT_PRIORITY,
1621 callback.callback(),
1622 pool_.get(),
1623 BoundNetLog());
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 BoundTestNetLog log;
1676 int rv = handle.Init("a",
1677 params_,
1678 LOWEST,
1679 callback.callback(),
1680 pool_.get(),
1681 log.bound());
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);
1691 handle.Reset();
1692 TestLoadTimingInfoNotConnected(handle);
1694 TestNetLog::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 BoundTestNetLog 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",
1724 params_,
1725 DEFAULT_PRIORITY,
1726 callback.callback(),
1727 pool_.get(),
1728 log.bound()));
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 TestNetLog::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) {
1751 CreatePool(2, 2);
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,
1775 handle.Init("a",
1776 params_,
1777 DEFAULT_PRIORITY,
1778 callback.callback(),
1779 pool_.get(),
1780 BoundNetLog()));
1781 BoundTestNetLog log2;
1782 EXPECT_EQ(ERR_IO_PENDING,
1783 handle2.Init("a",
1784 params_,
1785 DEFAULT_PRIORITY,
1786 callback2.callback(),
1787 pool_.get(),
1788 BoundNetLog()));
1790 handle.Reset();
1793 // At this point, request 2 is just waiting for the connect job to finish.
1795 EXPECT_EQ(OK, callback2.WaitForResult());
1796 handle2.Reset();
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",
1836 params_,
1837 DEFAULT_PRIORITY,
1838 req1.callback(), pool_.get(),
1839 BoundNetLog());
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
1844 // without a job.
1845 connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob);
1847 TestSocketRequest req2(&request_order, &completion_count);
1848 rv = req2.handle()->Init("a",
1849 params_,
1850 DEFAULT_PRIORITY,
1851 req2.callback(),
1852 pool_.get(),
1853 BoundNetLog());
1854 EXPECT_EQ(ERR_IO_PENDING, rv);
1855 TestSocketRequest req3(&request_order, &completion_count);
1856 rv = req3.handle()->Init("a",
1857 params_,
1858 DEFAULT_PRIORITY,
1859 req3.callback(),
1860 pool_.get(),
1861 BoundNetLog());
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",
1896 params_,
1897 DEFAULT_PRIORITY,
1898 req1.callback(),
1899 pool_.get(),
1900 BoundNetLog());
1901 EXPECT_EQ(ERR_IO_PENDING, rv);
1903 TestSocketRequest req2(&request_order, &completion_count);
1904 rv = req2.handle()->Init("a",
1905 params_,
1906 DEFAULT_PRIORITY,
1907 req2.callback(),
1908 pool_.get(),
1909 BoundNetLog());
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",
1917 params_,
1918 DEFAULT_PRIORITY,
1919 req3.callback(),
1920 pool_.get(),
1921 BoundNetLog());
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",
1942 params_,
1943 DEFAULT_PRIORITY,
1944 callback.callback(),
1945 pool_.get(),
1946 BoundNetLog());
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 // Only the first connection in the pool should affect the pool's load status.
1959 TEST_F(ClientSocketPoolBaseTest, LoadStateTwoRequests) {
1960 CreatePool(2, 2);
1961 connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob);
1963 ClientSocketHandle handle;
1964 TestCompletionCallback callback;
1965 int rv = handle.Init("a", params_, DEFAULT_PRIORITY, callback.callback(),
1966 pool_.get(), BoundNetLog());
1967 EXPECT_EQ(ERR_IO_PENDING, rv);
1968 client_socket_factory_.SetJobLoadState(0, LOAD_STATE_RESOLVING_HOST);
1970 ClientSocketHandle handle2;
1971 TestCompletionCallback callback2;
1972 rv = handle2.Init("a", params_, DEFAULT_PRIORITY, callback2.callback(),
1973 pool_.get(), BoundNetLog());
1974 EXPECT_EQ(ERR_IO_PENDING, rv);
1975 client_socket_factory_.SetJobLoadState(1, LOAD_STATE_RESOLVING_HOST);
1977 // Check that both handles report the state of the first job.
1978 EXPECT_EQ(LOAD_STATE_RESOLVING_HOST, handle.GetLoadState());
1979 EXPECT_EQ(LOAD_STATE_RESOLVING_HOST, handle2.GetLoadState());
1981 client_socket_factory_.SetJobLoadState(0, LOAD_STATE_CONNECTING);
1983 // Check that both handles change to LOAD_STATE_CONNECTING.
1984 EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState());
1985 EXPECT_EQ(LOAD_STATE_CONNECTING, handle2.GetLoadState());
1988 // Test that the second connection request does not affect the pool's load
1989 // status.
1990 TEST_F(ClientSocketPoolBaseTest, LoadStateTwoRequestsChangeSecondRequestState) {
1991 CreatePool(2, 2);
1992 connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob);
1994 ClientSocketHandle handle;
1995 TestCompletionCallback callback;
1996 int rv = handle.Init("a", params_, DEFAULT_PRIORITY, callback.callback(),
1997 pool_.get(), BoundNetLog());
1998 EXPECT_EQ(ERR_IO_PENDING, rv);
2000 ClientSocketHandle handle2;
2001 TestCompletionCallback callback2;
2002 rv = handle2.Init("a", params_, DEFAULT_PRIORITY, callback2.callback(),
2003 pool_.get(), BoundNetLog());
2004 EXPECT_EQ(ERR_IO_PENDING, rv);
2005 client_socket_factory_.SetJobLoadState(1, LOAD_STATE_RESOLVING_HOST);
2007 EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState());
2008 EXPECT_EQ(LOAD_STATE_CONNECTING, handle2.GetLoadState());
2010 // First job connects and the first request gets the socket. The
2011 // second handle switches to the state of the remaining ConnectJob.
2012 client_socket_factory_.SignalJob(0);
2013 EXPECT_EQ(OK, callback.WaitForResult());
2014 EXPECT_EQ(LOAD_STATE_RESOLVING_HOST, handle2.GetLoadState());
2017 // Test GetLoadState in the case the per-group limit is reached.
2018 TEST_F(ClientSocketPoolBaseTest, LoadStateGroupLimit) {
2019 CreatePool(2, 1);
2020 connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob);
2022 ClientSocketHandle handle;
2023 TestCompletionCallback callback;
2024 int rv = handle.Init("a",
2025 params_,
2026 MEDIUM,
2027 callback.callback(),
2028 pool_.get(),
2029 BoundNetLog());
2030 EXPECT_EQ(ERR_IO_PENDING, rv);
2031 EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState());
2033 // Request another socket from the same pool, buth with a higher priority.
2034 // The first request should now be stalled at the socket group limit.
2035 ClientSocketHandle handle2;
2036 TestCompletionCallback callback2;
2037 rv = handle2.Init("a",
2038 params_,
2039 HIGHEST,
2040 callback2.callback(),
2041 pool_.get(),
2042 BoundNetLog());
2043 EXPECT_EQ(ERR_IO_PENDING, rv);
2044 EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET, handle.GetLoadState());
2045 EXPECT_EQ(LOAD_STATE_CONNECTING, handle2.GetLoadState());
2047 // The first handle should remain stalled as the other socket goes through
2048 // the connect process.
2050 client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE);
2051 EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET, handle.GetLoadState());
2052 EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle2.GetLoadState());
2054 client_socket_factory_.SignalJob(0);
2055 EXPECT_EQ(OK, callback2.WaitForResult());
2056 EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET, handle.GetLoadState());
2058 // Closing the second socket should cause the stalled handle to finally get a
2059 // ConnectJob.
2060 handle2.socket()->Disconnect();
2061 handle2.Reset();
2062 EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState());
2065 // Test GetLoadState in the case the per-pool limit is reached.
2066 TEST_F(ClientSocketPoolBaseTest, LoadStatePoolLimit) {
2067 CreatePool(2, 2);
2068 connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob);
2070 ClientSocketHandle handle;
2071 TestCompletionCallback callback;
2072 int rv = handle.Init("a",
2073 params_,
2074 DEFAULT_PRIORITY,
2075 callback.callback(),
2076 pool_.get(),
2077 BoundNetLog());
2078 EXPECT_EQ(ERR_IO_PENDING, rv);
2080 // Request for socket from another pool.
2081 ClientSocketHandle handle2;
2082 TestCompletionCallback callback2;
2083 rv = handle2.Init("b",
2084 params_,
2085 DEFAULT_PRIORITY,
2086 callback2.callback(),
2087 pool_.get(),
2088 BoundNetLog());
2089 EXPECT_EQ(ERR_IO_PENDING, rv);
2091 // Request another socket from the first pool. Request should stall at the
2092 // socket pool limit.
2093 ClientSocketHandle handle3;
2094 TestCompletionCallback callback3;
2095 rv = handle3.Init("a",
2096 params_,
2097 DEFAULT_PRIORITY,
2098 callback2.callback(),
2099 pool_.get(),
2100 BoundNetLog());
2101 EXPECT_EQ(ERR_IO_PENDING, rv);
2103 // The third handle should remain stalled as the other sockets in its group
2104 // goes through the connect process.
2106 EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState());
2107 EXPECT_EQ(LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL, handle3.GetLoadState());
2109 client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE);
2110 EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle.GetLoadState());
2111 EXPECT_EQ(LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL, handle3.GetLoadState());
2113 client_socket_factory_.SignalJob(0);
2114 EXPECT_EQ(OK, callback.WaitForResult());
2115 EXPECT_EQ(LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL, handle3.GetLoadState());
2117 // Closing a socket should allow the stalled handle to finally get a new
2118 // ConnectJob.
2119 handle.socket()->Disconnect();
2120 handle.Reset();
2121 EXPECT_EQ(LOAD_STATE_CONNECTING, handle3.GetLoadState());
2124 TEST_F(ClientSocketPoolBaseTest, Recoverable) {
2125 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
2126 connect_job_factory_->set_job_type(TestConnectJob::kMockRecoverableJob);
2128 ClientSocketHandle handle;
2129 TestCompletionCallback callback;
2130 EXPECT_EQ(ERR_PROXY_AUTH_REQUESTED,
2131 handle.Init("a", params_, DEFAULT_PRIORITY, callback.callback(),
2132 pool_.get(), BoundNetLog()));
2133 EXPECT_TRUE(handle.is_initialized());
2134 EXPECT_TRUE(handle.socket());
2137 TEST_F(ClientSocketPoolBaseTest, AsyncRecoverable) {
2138 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
2140 connect_job_factory_->set_job_type(
2141 TestConnectJob::kMockPendingRecoverableJob);
2142 ClientSocketHandle handle;
2143 TestCompletionCallback callback;
2144 EXPECT_EQ(ERR_IO_PENDING,
2145 handle.Init("a",
2146 params_,
2147 DEFAULT_PRIORITY,
2148 callback.callback(),
2149 pool_.get(),
2150 BoundNetLog()));
2151 EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", &handle));
2152 EXPECT_EQ(ERR_PROXY_AUTH_REQUESTED, callback.WaitForResult());
2153 EXPECT_TRUE(handle.is_initialized());
2154 EXPECT_TRUE(handle.socket());
2157 TEST_F(ClientSocketPoolBaseTest, AdditionalErrorStateSynchronous) {
2158 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
2159 connect_job_factory_->set_job_type(
2160 TestConnectJob::kMockAdditionalErrorStateJob);
2162 ClientSocketHandle handle;
2163 TestCompletionCallback callback;
2164 EXPECT_EQ(ERR_CONNECTION_FAILED,
2165 handle.Init("a",
2166 params_,
2167 DEFAULT_PRIORITY,
2168 callback.callback(),
2169 pool_.get(),
2170 BoundNetLog()));
2171 EXPECT_FALSE(handle.is_initialized());
2172 EXPECT_FALSE(handle.socket());
2173 EXPECT_TRUE(handle.is_ssl_error());
2174 EXPECT_FALSE(handle.ssl_error_response_info().headers.get() == NULL);
2177 TEST_F(ClientSocketPoolBaseTest, AdditionalErrorStateAsynchronous) {
2178 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
2180 connect_job_factory_->set_job_type(
2181 TestConnectJob::kMockPendingAdditionalErrorStateJob);
2182 ClientSocketHandle handle;
2183 TestCompletionCallback callback;
2184 EXPECT_EQ(ERR_IO_PENDING,
2185 handle.Init("a",
2186 params_,
2187 DEFAULT_PRIORITY,
2188 callback.callback(),
2189 pool_.get(),
2190 BoundNetLog()));
2191 EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", &handle));
2192 EXPECT_EQ(ERR_CONNECTION_FAILED, callback.WaitForResult());
2193 EXPECT_FALSE(handle.is_initialized());
2194 EXPECT_FALSE(handle.socket());
2195 EXPECT_TRUE(handle.is_ssl_error());
2196 EXPECT_FALSE(handle.ssl_error_response_info().headers.get() == NULL);
2199 // Make sure we can reuse sockets when the cleanup timer is disabled.
2200 TEST_F(ClientSocketPoolBaseTest, DisableCleanupTimerReuse) {
2201 // Disable cleanup timer.
2202 internal::ClientSocketPoolBaseHelper::set_cleanup_timer_enabled(false);
2204 CreatePoolWithIdleTimeouts(
2205 kDefaultMaxSockets, kDefaultMaxSocketsPerGroup,
2206 base::TimeDelta(), // Time out unused sockets immediately.
2207 base::TimeDelta::FromDays(1)); // Don't time out used sockets.
2209 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
2211 ClientSocketHandle handle;
2212 TestCompletionCallback callback;
2213 int rv = handle.Init("a",
2214 params_,
2215 LOWEST,
2216 callback.callback(),
2217 pool_.get(),
2218 BoundNetLog());
2219 ASSERT_EQ(ERR_IO_PENDING, rv);
2220 EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", &handle));
2221 ASSERT_EQ(OK, callback.WaitForResult());
2223 // Use and release the socket.
2224 EXPECT_EQ(1, handle.socket()->Write(NULL, 1, CompletionCallback()));
2225 TestLoadTimingInfoConnectedNotReused(handle);
2226 handle.Reset();
2228 // Should now have one idle socket.
2229 ASSERT_EQ(1, pool_->IdleSocketCount());
2231 // Request a new socket. This should reuse the old socket and complete
2232 // synchronously.
2233 BoundTestNetLog log;
2234 rv = handle.Init("a",
2235 params_,
2236 LOWEST,
2237 CompletionCallback(),
2238 pool_.get(),
2239 log.bound());
2240 ASSERT_EQ(OK, rv);
2241 EXPECT_TRUE(handle.is_reused());
2242 TestLoadTimingInfoConnectedReused(handle);
2244 ASSERT_TRUE(pool_->HasGroup("a"));
2245 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
2246 EXPECT_EQ(1, pool_->NumActiveSocketsInGroup("a"));
2248 TestNetLog::CapturedEntryList entries;
2249 log.GetEntries(&entries);
2250 EXPECT_TRUE(LogContainsEntryWithType(
2251 entries, 1, NetLog::TYPE_SOCKET_POOL_REUSED_AN_EXISTING_SOCKET));
2254 // Make sure we cleanup old unused sockets when the cleanup timer is disabled.
2255 TEST_F(ClientSocketPoolBaseTest, DisableCleanupTimerNoReuse) {
2256 // Disable cleanup timer.
2257 internal::ClientSocketPoolBaseHelper::set_cleanup_timer_enabled(false);
2259 CreatePoolWithIdleTimeouts(
2260 kDefaultMaxSockets, kDefaultMaxSocketsPerGroup,
2261 base::TimeDelta(), // Time out unused sockets immediately
2262 base::TimeDelta()); // Time out used sockets immediately
2264 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
2266 // Startup two mock pending connect jobs, which will sit in the MessageLoop.
2268 ClientSocketHandle handle;
2269 TestCompletionCallback callback;
2270 int rv = handle.Init("a",
2271 params_,
2272 LOWEST,
2273 callback.callback(),
2274 pool_.get(),
2275 BoundNetLog());
2276 ASSERT_EQ(ERR_IO_PENDING, rv);
2277 EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", &handle));
2279 ClientSocketHandle handle2;
2280 TestCompletionCallback callback2;
2281 rv = handle2.Init("a",
2282 params_,
2283 LOWEST,
2284 callback2.callback(),
2285 pool_.get(),
2286 BoundNetLog());
2287 ASSERT_EQ(ERR_IO_PENDING, rv);
2288 EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", &handle2));
2290 // Cancel one of the requests. Wait for the other, which will get the first
2291 // job. Release the socket. Run the loop again to make sure the second
2292 // socket is sitting idle and the first one is released (since ReleaseSocket()
2293 // just posts a DoReleaseSocket() task).
2295 handle.Reset();
2296 ASSERT_EQ(OK, callback2.WaitForResult());
2297 // Use the socket.
2298 EXPECT_EQ(1, handle2.socket()->Write(NULL, 1, CompletionCallback()));
2299 handle2.Reset();
2301 // We post all of our delayed tasks with a 2ms delay. I.e. they don't
2302 // actually become pending until 2ms after they have been created. In order
2303 // to flush all tasks, we need to wait so that we know there are no
2304 // soon-to-be-pending tasks waiting.
2305 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10));
2306 base::MessageLoop::current()->RunUntilIdle();
2308 // Both sockets should now be idle.
2309 ASSERT_EQ(2, pool_->IdleSocketCount());
2311 // Request a new socket. This should cleanup the unused and timed out ones.
2312 // A new socket will be created rather than reusing the idle one.
2313 BoundTestNetLog log;
2314 TestCompletionCallback callback3;
2315 rv = handle.Init("a",
2316 params_,
2317 LOWEST,
2318 callback3.callback(),
2319 pool_.get(),
2320 log.bound());
2321 ASSERT_EQ(ERR_IO_PENDING, rv);
2322 ASSERT_EQ(OK, callback3.WaitForResult());
2323 EXPECT_FALSE(handle.is_reused());
2325 // Make sure the idle socket is closed.
2326 ASSERT_TRUE(pool_->HasGroup("a"));
2327 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
2328 EXPECT_EQ(1, pool_->NumActiveSocketsInGroup("a"));
2330 TestNetLog::CapturedEntryList entries;
2331 log.GetEntries(&entries);
2332 EXPECT_FALSE(LogContainsEntryWithType(
2333 entries, 1, NetLog::TYPE_SOCKET_POOL_REUSED_AN_EXISTING_SOCKET));
2336 TEST_F(ClientSocketPoolBaseTest, CleanupTimedOutIdleSockets) {
2337 CreatePoolWithIdleTimeouts(
2338 kDefaultMaxSockets, kDefaultMaxSocketsPerGroup,
2339 base::TimeDelta(), // Time out unused sockets immediately.
2340 base::TimeDelta::FromDays(1)); // Don't time out used sockets.
2342 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
2344 // Startup two mock pending connect jobs, which will sit in the MessageLoop.
2346 ClientSocketHandle handle;
2347 TestCompletionCallback callback;
2348 int rv = handle.Init("a",
2349 params_,
2350 LOWEST,
2351 callback.callback(),
2352 pool_.get(),
2353 BoundNetLog());
2354 EXPECT_EQ(ERR_IO_PENDING, rv);
2355 EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", &handle));
2357 ClientSocketHandle handle2;
2358 TestCompletionCallback callback2;
2359 rv = handle2.Init("a",
2360 params_,
2361 LOWEST,
2362 callback2.callback(),
2363 pool_.get(),
2364 BoundNetLog());
2365 EXPECT_EQ(ERR_IO_PENDING, rv);
2366 EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", &handle2));
2368 // Cancel one of the requests. Wait for the other, which will get the first
2369 // job. Release the socket. Run the loop again to make sure the second
2370 // socket is sitting idle and the first one is released (since ReleaseSocket()
2371 // just posts a DoReleaseSocket() task).
2373 handle.Reset();
2374 EXPECT_EQ(OK, callback2.WaitForResult());
2375 // Use the socket.
2376 EXPECT_EQ(1, handle2.socket()->Write(NULL, 1, CompletionCallback()));
2377 handle2.Reset();
2379 // We post all of our delayed tasks with a 2ms delay. I.e. they don't
2380 // actually become pending until 2ms after they have been created. In order
2381 // to flush all tasks, we need to wait so that we know there are no
2382 // soon-to-be-pending tasks waiting.
2383 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10));
2384 base::MessageLoop::current()->RunUntilIdle();
2386 ASSERT_EQ(2, pool_->IdleSocketCount());
2388 // Invoke the idle socket cleanup check. Only one socket should be left, the
2389 // used socket. Request it to make sure that it's used.
2391 pool_->CleanupTimedOutIdleSockets();
2392 BoundTestNetLog log;
2393 rv = handle.Init("a",
2394 params_,
2395 LOWEST,
2396 callback.callback(),
2397 pool_.get(),
2398 log.bound());
2399 EXPECT_EQ(OK, rv);
2400 EXPECT_TRUE(handle.is_reused());
2402 TestNetLog::CapturedEntryList entries;
2403 log.GetEntries(&entries);
2404 EXPECT_TRUE(LogContainsEntryWithType(
2405 entries, 1, NetLog::TYPE_SOCKET_POOL_REUSED_AN_EXISTING_SOCKET));
2408 // Make sure that we process all pending requests even when we're stalling
2409 // because of multiple releasing disconnected sockets.
2410 TEST_F(ClientSocketPoolBaseTest, MultipleReleasingDisconnectedSockets) {
2411 CreatePoolWithIdleTimeouts(
2412 kDefaultMaxSockets, kDefaultMaxSocketsPerGroup,
2413 base::TimeDelta(), // Time out unused sockets immediately.
2414 base::TimeDelta::FromDays(1)); // Don't time out used sockets.
2416 connect_job_factory_->set_job_type(TestConnectJob::kMockJob);
2418 // Startup 4 connect jobs. Two of them will be pending.
2420 ClientSocketHandle handle;
2421 TestCompletionCallback callback;
2422 int rv = handle.Init("a",
2423 params_,
2424 LOWEST,
2425 callback.callback(),
2426 pool_.get(),
2427 BoundNetLog());
2428 EXPECT_EQ(OK, rv);
2430 ClientSocketHandle handle2;
2431 TestCompletionCallback callback2;
2432 rv = handle2.Init("a",
2433 params_,
2434 LOWEST,
2435 callback2.callback(),
2436 pool_.get(),
2437 BoundNetLog());
2438 EXPECT_EQ(OK, rv);
2440 ClientSocketHandle handle3;
2441 TestCompletionCallback callback3;
2442 rv = handle3.Init("a",
2443 params_,
2444 LOWEST,
2445 callback3.callback(),
2446 pool_.get(),
2447 BoundNetLog());
2448 EXPECT_EQ(ERR_IO_PENDING, rv);
2450 ClientSocketHandle handle4;
2451 TestCompletionCallback callback4;
2452 rv = handle4.Init("a",
2453 params_,
2454 LOWEST,
2455 callback4.callback(),
2456 pool_.get(),
2457 BoundNetLog());
2458 EXPECT_EQ(ERR_IO_PENDING, rv);
2460 // Release two disconnected sockets.
2462 handle.socket()->Disconnect();
2463 handle.Reset();
2464 handle2.socket()->Disconnect();
2465 handle2.Reset();
2467 EXPECT_EQ(OK, callback3.WaitForResult());
2468 EXPECT_FALSE(handle3.is_reused());
2469 EXPECT_EQ(OK, callback4.WaitForResult());
2470 EXPECT_FALSE(handle4.is_reused());
2473 // Regression test for http://crbug.com/42267.
2474 // When DoReleaseSocket() is processed for one socket, it is blocked because the
2475 // other stalled groups all have releasing sockets, so no progress can be made.
2476 TEST_F(ClientSocketPoolBaseTest, SocketLimitReleasingSockets) {
2477 CreatePoolWithIdleTimeouts(
2478 4 /* socket limit */, 4 /* socket limit per group */,
2479 base::TimeDelta(), // Time out unused sockets immediately.
2480 base::TimeDelta::FromDays(1)); // Don't time out used sockets.
2482 connect_job_factory_->set_job_type(TestConnectJob::kMockJob);
2484 // Max out the socket limit with 2 per group.
2486 ClientSocketHandle handle_a[4];
2487 TestCompletionCallback callback_a[4];
2488 ClientSocketHandle handle_b[4];
2489 TestCompletionCallback callback_b[4];
2491 for (int i = 0; i < 2; ++i) {
2492 EXPECT_EQ(OK, handle_a[i].Init("a",
2493 params_,
2494 LOWEST,
2495 callback_a[i].callback(),
2496 pool_.get(),
2497 BoundNetLog()));
2498 EXPECT_EQ(OK, handle_b[i].Init("b",
2499 params_,
2500 LOWEST,
2501 callback_b[i].callback(),
2502 pool_.get(),
2503 BoundNetLog()));
2506 // Make 4 pending requests, 2 per group.
2508 for (int i = 2; i < 4; ++i) {
2509 EXPECT_EQ(ERR_IO_PENDING,
2510 handle_a[i].Init("a",
2511 params_,
2512 LOWEST,
2513 callback_a[i].callback(),
2514 pool_.get(),
2515 BoundNetLog()));
2516 EXPECT_EQ(ERR_IO_PENDING,
2517 handle_b[i].Init("b",
2518 params_,
2519 LOWEST,
2520 callback_b[i].callback(),
2521 pool_.get(),
2522 BoundNetLog()));
2525 // Release b's socket first. The order is important, because in
2526 // DoReleaseSocket(), we'll process b's released socket, and since both b and
2527 // a are stalled, but 'a' is lower lexicographically, we'll process group 'a'
2528 // first, which has a releasing socket, so it refuses to start up another
2529 // ConnectJob. So, we used to infinite loop on this.
2530 handle_b[0].socket()->Disconnect();
2531 handle_b[0].Reset();
2532 handle_a[0].socket()->Disconnect();
2533 handle_a[0].Reset();
2535 // Used to get stuck here.
2536 base::MessageLoop::current()->RunUntilIdle();
2538 handle_b[1].socket()->Disconnect();
2539 handle_b[1].Reset();
2540 handle_a[1].socket()->Disconnect();
2541 handle_a[1].Reset();
2543 for (int i = 2; i < 4; ++i) {
2544 EXPECT_EQ(OK, callback_b[i].WaitForResult());
2545 EXPECT_EQ(OK, callback_a[i].WaitForResult());
2549 TEST_F(ClientSocketPoolBaseTest,
2550 ReleasingDisconnectedSocketsMaintainsPriorityOrder) {
2551 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
2553 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
2555 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", DEFAULT_PRIORITY));
2556 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", DEFAULT_PRIORITY));
2557 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", DEFAULT_PRIORITY));
2558 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", DEFAULT_PRIORITY));
2560 EXPECT_EQ(OK, (*requests())[0]->WaitForResult());
2561 EXPECT_EQ(OK, (*requests())[1]->WaitForResult());
2562 EXPECT_EQ(2u, completion_count());
2564 // Releases one connection.
2565 EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::NO_KEEP_ALIVE));
2566 EXPECT_EQ(OK, (*requests())[2]->WaitForResult());
2568 EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::NO_KEEP_ALIVE));
2569 EXPECT_EQ(OK, (*requests())[3]->WaitForResult());
2570 EXPECT_EQ(4u, completion_count());
2572 EXPECT_EQ(1, GetOrderOfRequest(1));
2573 EXPECT_EQ(2, GetOrderOfRequest(2));
2574 EXPECT_EQ(3, GetOrderOfRequest(3));
2575 EXPECT_EQ(4, GetOrderOfRequest(4));
2577 // Make sure we test order of all requests made.
2578 EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(5));
2581 class TestReleasingSocketRequest : public TestCompletionCallbackBase {
2582 public:
2583 TestReleasingSocketRequest(TestClientSocketPool* pool,
2584 int expected_result,
2585 bool reset_releasing_handle)
2586 : pool_(pool),
2587 expected_result_(expected_result),
2588 reset_releasing_handle_(reset_releasing_handle),
2589 callback_(base::Bind(&TestReleasingSocketRequest::OnComplete,
2590 base::Unretained(this))) {
2593 ~TestReleasingSocketRequest() override {}
2595 ClientSocketHandle* handle() { return &handle_; }
2597 const CompletionCallback& callback() const { return callback_; }
2599 private:
2600 void OnComplete(int result) {
2601 SetResult(result);
2602 if (reset_releasing_handle_)
2603 handle_.Reset();
2605 scoped_refptr<TestSocketParams> con_params(
2606 new TestSocketParams(false /* ignore_limits */));
2607 EXPECT_EQ(expected_result_,
2608 handle2_.Init("a", con_params, DEFAULT_PRIORITY,
2609 callback2_.callback(), pool_, BoundNetLog()));
2612 TestClientSocketPool* const pool_;
2613 int expected_result_;
2614 bool reset_releasing_handle_;
2615 ClientSocketHandle handle_;
2616 ClientSocketHandle handle2_;
2617 CompletionCallback callback_;
2618 TestCompletionCallback callback2_;
2622 TEST_F(ClientSocketPoolBaseTest, AdditionalErrorSocketsDontUseSlot) {
2623 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
2625 EXPECT_EQ(OK, StartRequest("b", DEFAULT_PRIORITY));
2626 EXPECT_EQ(OK, StartRequest("a", DEFAULT_PRIORITY));
2627 EXPECT_EQ(OK, StartRequest("b", DEFAULT_PRIORITY));
2629 EXPECT_EQ(static_cast<int>(requests_size()),
2630 client_socket_factory_.allocation_count());
2632 connect_job_factory_->set_job_type(
2633 TestConnectJob::kMockPendingAdditionalErrorStateJob);
2634 TestReleasingSocketRequest req(pool_.get(), OK, false);
2635 EXPECT_EQ(ERR_IO_PENDING,
2636 req.handle()->Init("a", params_, DEFAULT_PRIORITY, req.callback(),
2637 pool_.get(), BoundNetLog()));
2638 // The next job should complete synchronously
2639 connect_job_factory_->set_job_type(TestConnectJob::kMockJob);
2641 EXPECT_EQ(ERR_CONNECTION_FAILED, req.WaitForResult());
2642 EXPECT_FALSE(req.handle()->is_initialized());
2643 EXPECT_FALSE(req.handle()->socket());
2644 EXPECT_TRUE(req.handle()->is_ssl_error());
2645 EXPECT_FALSE(req.handle()->ssl_error_response_info().headers.get() == NULL);
2648 // http://crbug.com/44724 regression test.
2649 // We start releasing the pool when we flush on network change. When that
2650 // happens, the only active references are in the ClientSocketHandles. When a
2651 // ConnectJob completes and calls back into the last ClientSocketHandle, that
2652 // callback can release the last reference and delete the pool. After the
2653 // callback finishes, we go back to the stack frame within the now-deleted pool.
2654 // Executing any code that refers to members of the now-deleted pool can cause
2655 // crashes.
2656 TEST_F(ClientSocketPoolBaseTest, CallbackThatReleasesPool) {
2657 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
2658 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob);
2660 ClientSocketHandle handle;
2661 TestCompletionCallback callback;
2662 EXPECT_EQ(ERR_IO_PENDING, handle.Init("a",
2663 params_,
2664 DEFAULT_PRIORITY,
2665 callback.callback(),
2666 pool_.get(),
2667 BoundNetLog()));
2669 pool_->FlushWithError(ERR_NETWORK_CHANGED);
2671 // We'll call back into this now.
2672 callback.WaitForResult();
2675 TEST_F(ClientSocketPoolBaseTest, DoNotReuseSocketAfterFlush) {
2676 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
2677 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
2679 ClientSocketHandle handle;
2680 TestCompletionCallback callback;
2681 EXPECT_EQ(ERR_IO_PENDING, handle.Init("a",
2682 params_,
2683 DEFAULT_PRIORITY,
2684 callback.callback(),
2685 pool_.get(),
2686 BoundNetLog()));
2687 EXPECT_EQ(OK, callback.WaitForResult());
2688 EXPECT_EQ(ClientSocketHandle::UNUSED, handle.reuse_type());
2690 pool_->FlushWithError(ERR_NETWORK_CHANGED);
2692 handle.Reset();
2693 base::MessageLoop::current()->RunUntilIdle();
2695 EXPECT_EQ(ERR_IO_PENDING, handle.Init("a",
2696 params_,
2697 DEFAULT_PRIORITY,
2698 callback.callback(),
2699 pool_.get(),
2700 BoundNetLog()));
2701 EXPECT_EQ(OK, callback.WaitForResult());
2702 EXPECT_EQ(ClientSocketHandle::UNUSED, handle.reuse_type());
2705 class ConnectWithinCallback : public TestCompletionCallbackBase {
2706 public:
2707 ConnectWithinCallback(
2708 const std::string& group_name,
2709 const scoped_refptr<TestSocketParams>& params,
2710 TestClientSocketPool* pool)
2711 : group_name_(group_name),
2712 params_(params),
2713 pool_(pool),
2714 callback_(base::Bind(&ConnectWithinCallback::OnComplete,
2715 base::Unretained(this))) {
2718 ~ConnectWithinCallback() override {}
2720 int WaitForNestedResult() {
2721 return nested_callback_.WaitForResult();
2724 const CompletionCallback& callback() const { return callback_; }
2726 private:
2727 void OnComplete(int result) {
2728 SetResult(result);
2729 EXPECT_EQ(ERR_IO_PENDING,
2730 handle_.Init(group_name_,
2731 params_,
2732 DEFAULT_PRIORITY,
2733 nested_callback_.callback(),
2734 pool_,
2735 BoundNetLog()));
2738 const std::string group_name_;
2739 const scoped_refptr<TestSocketParams> params_;
2740 TestClientSocketPool* const pool_;
2741 ClientSocketHandle handle_;
2742 CompletionCallback callback_;
2743 TestCompletionCallback nested_callback_;
2745 DISALLOW_COPY_AND_ASSIGN(ConnectWithinCallback);
2748 TEST_F(ClientSocketPoolBaseTest, AbortAllRequestsOnFlush) {
2749 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
2751 // First job will be waiting until it gets aborted.
2752 connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob);
2754 ClientSocketHandle handle;
2755 ConnectWithinCallback callback("a", params_, pool_.get());
2756 EXPECT_EQ(ERR_IO_PENDING, handle.Init("a",
2757 params_,
2758 DEFAULT_PRIORITY,
2759 callback.callback(),
2760 pool_.get(),
2761 BoundNetLog()));
2763 // Second job will be started during the first callback, and will
2764 // asynchronously complete with OK.
2765 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
2766 pool_->FlushWithError(ERR_NETWORK_CHANGED);
2767 EXPECT_EQ(ERR_NETWORK_CHANGED, callback.WaitForResult());
2768 EXPECT_EQ(OK, callback.WaitForNestedResult());
2771 // Cancel a pending socket request while we're at max sockets,
2772 // and verify that the backup socket firing doesn't cause a crash.
2773 TEST_F(ClientSocketPoolBaseTest, BackupSocketCancelAtMaxSockets) {
2774 // Max 4 sockets globally, max 4 sockets per group.
2775 CreatePool(kDefaultMaxSockets, kDefaultMaxSockets);
2776 pool_->EnableConnectBackupJobs();
2778 // Create the first socket and set to ERR_IO_PENDING. This starts the backup
2779 // timer.
2780 connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob);
2781 ClientSocketHandle handle;
2782 TestCompletionCallback callback;
2783 EXPECT_EQ(ERR_IO_PENDING, handle.Init("bar",
2784 params_,
2785 DEFAULT_PRIORITY,
2786 callback.callback(),
2787 pool_.get(),
2788 BoundNetLog()));
2790 // Start (MaxSockets - 1) connected sockets to reach max sockets.
2791 connect_job_factory_->set_job_type(TestConnectJob::kMockJob);
2792 ClientSocketHandle handles[kDefaultMaxSockets];
2793 for (int i = 1; i < kDefaultMaxSockets; ++i) {
2794 TestCompletionCallback callback;
2795 EXPECT_EQ(OK, handles[i].Init("bar",
2796 params_,
2797 DEFAULT_PRIORITY,
2798 callback.callback(),
2799 pool_.get(),
2800 BoundNetLog()));
2803 base::MessageLoop::current()->RunUntilIdle();
2805 // Cancel the pending request.
2806 handle.Reset();
2808 // Wait for the backup timer to fire (add some slop to ensure it fires)
2809 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(
2810 ClientSocketPool::kMaxConnectRetryIntervalMs / 2 * 3));
2812 base::MessageLoop::current()->RunUntilIdle();
2813 EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count());
2816 TEST_F(ClientSocketPoolBaseTest, CancelBackupSocketAfterCancelingAllRequests) {
2817 CreatePool(kDefaultMaxSockets, kDefaultMaxSockets);
2818 pool_->EnableConnectBackupJobs();
2820 // Create the first socket and set to ERR_IO_PENDING. This starts the backup
2821 // timer.
2822 connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob);
2823 ClientSocketHandle handle;
2824 TestCompletionCallback callback;
2825 EXPECT_EQ(ERR_IO_PENDING, handle.Init("bar",
2826 params_,
2827 DEFAULT_PRIORITY,
2828 callback.callback(),
2829 pool_.get(),
2830 BoundNetLog()));
2831 ASSERT_TRUE(pool_->HasGroup("bar"));
2832 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("bar"));
2833 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("bar"));
2835 // Cancel the socket request. This should cancel the backup timer. Wait for
2836 // the backup time to see if it indeed got canceled.
2837 handle.Reset();
2838 // Wait for the backup timer to fire (add some slop to ensure it fires)
2839 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(
2840 ClientSocketPool::kMaxConnectRetryIntervalMs / 2 * 3));
2841 base::MessageLoop::current()->RunUntilIdle();
2842 ASSERT_TRUE(pool_->HasGroup("bar"));
2843 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("bar"));
2846 TEST_F(ClientSocketPoolBaseTest, CancelBackupSocketAfterFinishingAllRequests) {
2847 CreatePool(kDefaultMaxSockets, kDefaultMaxSockets);
2848 pool_->EnableConnectBackupJobs();
2850 // Create the first socket and set to ERR_IO_PENDING. This starts the backup
2851 // timer.
2852 connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob);
2853 ClientSocketHandle handle;
2854 TestCompletionCallback callback;
2855 EXPECT_EQ(ERR_IO_PENDING, handle.Init("bar",
2856 params_,
2857 DEFAULT_PRIORITY,
2858 callback.callback(),
2859 pool_.get(),
2860 BoundNetLog()));
2861 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
2862 ClientSocketHandle handle2;
2863 TestCompletionCallback callback2;
2864 EXPECT_EQ(ERR_IO_PENDING, handle2.Init("bar",
2865 params_,
2866 DEFAULT_PRIORITY,
2867 callback2.callback(),
2868 pool_.get(),
2869 BoundNetLog()));
2870 ASSERT_TRUE(pool_->HasGroup("bar"));
2871 EXPECT_EQ(2, pool_->NumConnectJobsInGroup("bar"));
2873 // Cancel request 1 and then complete request 2. With the requests finished,
2874 // the backup timer should be cancelled.
2875 handle.Reset();
2876 EXPECT_EQ(OK, callback2.WaitForResult());
2877 // Wait for the backup timer to fire (add some slop to ensure it fires)
2878 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(
2879 ClientSocketPool::kMaxConnectRetryIntervalMs / 2 * 3));
2880 base::MessageLoop::current()->RunUntilIdle();
2883 // Test delayed socket binding for the case where we have two connects,
2884 // and while one is waiting on a connect, the other frees up.
2885 // The socket waiting on a connect should switch immediately to the freed
2886 // up socket.
2887 TEST_F(ClientSocketPoolBaseTest, DelayedSocketBindingWaitingForConnect) {
2888 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
2889 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
2891 ClientSocketHandle handle1;
2892 TestCompletionCallback callback;
2893 EXPECT_EQ(ERR_IO_PENDING,
2894 handle1.Init("a",
2895 params_,
2896 DEFAULT_PRIORITY,
2897 callback.callback(),
2898 pool_.get(),
2899 BoundNetLog()));
2900 EXPECT_EQ(OK, callback.WaitForResult());
2902 // No idle sockets, no pending jobs.
2903 EXPECT_EQ(0, pool_->IdleSocketCount());
2904 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a"));
2906 // Create a second socket to the same host, but this one will wait.
2907 connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob);
2908 ClientSocketHandle handle2;
2909 EXPECT_EQ(ERR_IO_PENDING,
2910 handle2.Init("a",
2911 params_,
2912 DEFAULT_PRIORITY,
2913 callback.callback(),
2914 pool_.get(),
2915 BoundNetLog()));
2916 // No idle sockets, and one connecting job.
2917 EXPECT_EQ(0, pool_->IdleSocketCount());
2918 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a"));
2920 // Return the first handle to the pool. This will initiate the delayed
2921 // binding.
2922 handle1.Reset();
2924 base::MessageLoop::current()->RunUntilIdle();
2926 // Still no idle sockets, still one pending connect job.
2927 EXPECT_EQ(0, pool_->IdleSocketCount());
2928 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a"));
2930 // The second socket connected, even though it was a Waiting Job.
2931 EXPECT_EQ(OK, callback.WaitForResult());
2933 // And we can see there is still one job waiting.
2934 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a"));
2936 // Finally, signal the waiting Connect.
2937 client_socket_factory_.SignalJobs();
2938 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a"));
2940 base::MessageLoop::current()->RunUntilIdle();
2943 // Test delayed socket binding when a group is at capacity and one
2944 // of the group's sockets frees up.
2945 TEST_F(ClientSocketPoolBaseTest, DelayedSocketBindingAtGroupCapacity) {
2946 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
2947 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
2949 ClientSocketHandle handle1;
2950 TestCompletionCallback callback;
2951 EXPECT_EQ(ERR_IO_PENDING,
2952 handle1.Init("a",
2953 params_,
2954 DEFAULT_PRIORITY,
2955 callback.callback(),
2956 pool_.get(),
2957 BoundNetLog()));
2958 EXPECT_EQ(OK, callback.WaitForResult());
2960 // No idle sockets, no pending jobs.
2961 EXPECT_EQ(0, pool_->IdleSocketCount());
2962 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a"));
2964 // Create a second socket to the same host, but this one will wait.
2965 connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob);
2966 ClientSocketHandle handle2;
2967 EXPECT_EQ(ERR_IO_PENDING,
2968 handle2.Init("a",
2969 params_,
2970 DEFAULT_PRIORITY,
2971 callback.callback(),
2972 pool_.get(),
2973 BoundNetLog()));
2974 // No idle sockets, and one connecting job.
2975 EXPECT_EQ(0, pool_->IdleSocketCount());
2976 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a"));
2978 // Return the first handle to the pool. This will initiate the delayed
2979 // binding.
2980 handle1.Reset();
2982 base::MessageLoop::current()->RunUntilIdle();
2984 // Still no idle sockets, still one pending connect job.
2985 EXPECT_EQ(0, pool_->IdleSocketCount());
2986 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a"));
2988 // The second socket connected, even though it was a Waiting Job.
2989 EXPECT_EQ(OK, callback.WaitForResult());
2991 // And we can see there is still one job waiting.
2992 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a"));
2994 // Finally, signal the waiting Connect.
2995 client_socket_factory_.SignalJobs();
2996 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a"));
2998 base::MessageLoop::current()->RunUntilIdle();
3001 // Test out the case where we have one socket connected, one
3002 // connecting, when the first socket finishes and goes idle.
3003 // Although the second connection is pending, the second request
3004 // should complete, by taking the first socket's idle socket.
3005 TEST_F(ClientSocketPoolBaseTest, DelayedSocketBindingAtStall) {
3006 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
3007 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
3009 ClientSocketHandle handle1;
3010 TestCompletionCallback callback;
3011 EXPECT_EQ(ERR_IO_PENDING,
3012 handle1.Init("a",
3013 params_,
3014 DEFAULT_PRIORITY,
3015 callback.callback(),
3016 pool_.get(),
3017 BoundNetLog()));
3018 EXPECT_EQ(OK, callback.WaitForResult());
3020 // No idle sockets, no pending jobs.
3021 EXPECT_EQ(0, pool_->IdleSocketCount());
3022 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a"));
3024 // Create a second socket to the same host, but this one will wait.
3025 connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob);
3026 ClientSocketHandle handle2;
3027 EXPECT_EQ(ERR_IO_PENDING,
3028 handle2.Init("a",
3029 params_,
3030 DEFAULT_PRIORITY,
3031 callback.callback(),
3032 pool_.get(),
3033 BoundNetLog()));
3034 // No idle sockets, and one connecting job.
3035 EXPECT_EQ(0, pool_->IdleSocketCount());
3036 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a"));
3038 // Return the first handle to the pool. This will initiate the delayed
3039 // binding.
3040 handle1.Reset();
3042 base::MessageLoop::current()->RunUntilIdle();
3044 // Still no idle sockets, still one pending connect job.
3045 EXPECT_EQ(0, pool_->IdleSocketCount());
3046 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a"));
3048 // The second socket connected, even though it was a Waiting Job.
3049 EXPECT_EQ(OK, callback.WaitForResult());
3051 // And we can see there is still one job waiting.
3052 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a"));
3054 // Finally, signal the waiting Connect.
3055 client_socket_factory_.SignalJobs();
3056 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a"));
3058 base::MessageLoop::current()->RunUntilIdle();
3061 // Cover the case where on an available socket slot, we have one pending
3062 // request that completes synchronously, thereby making the Group empty.
3063 TEST_F(ClientSocketPoolBaseTest, SynchronouslyProcessOnePendingRequest) {
3064 const int kUnlimitedSockets = 100;
3065 const int kOneSocketPerGroup = 1;
3066 CreatePool(kUnlimitedSockets, kOneSocketPerGroup);
3068 // Make the first request asynchronous fail.
3069 // This will free up a socket slot later.
3070 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob);
3072 ClientSocketHandle handle1;
3073 TestCompletionCallback callback1;
3074 EXPECT_EQ(ERR_IO_PENDING,
3075 handle1.Init("a",
3076 params_,
3077 DEFAULT_PRIORITY,
3078 callback1.callback(),
3079 pool_.get(),
3080 BoundNetLog()));
3081 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a"));
3083 // Make the second request synchronously fail. This should make the Group
3084 // empty.
3085 connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob);
3086 ClientSocketHandle handle2;
3087 TestCompletionCallback callback2;
3088 // It'll be ERR_IO_PENDING now, but the TestConnectJob will synchronously fail
3089 // when created.
3090 EXPECT_EQ(ERR_IO_PENDING,
3091 handle2.Init("a",
3092 params_,
3093 DEFAULT_PRIORITY,
3094 callback2.callback(),
3095 pool_.get(),
3096 BoundNetLog()));
3098 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a"));
3100 EXPECT_EQ(ERR_CONNECTION_FAILED, callback1.WaitForResult());
3101 EXPECT_EQ(ERR_CONNECTION_FAILED, callback2.WaitForResult());
3102 EXPECT_FALSE(pool_->HasGroup("a"));
3105 TEST_F(ClientSocketPoolBaseTest, PreferUsedSocketToUnusedSocket) {
3106 CreatePool(kDefaultMaxSockets, kDefaultMaxSockets);
3108 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
3110 ClientSocketHandle handle1;
3111 TestCompletionCallback callback1;
3112 EXPECT_EQ(ERR_IO_PENDING, handle1.Init("a",
3113 params_,
3114 DEFAULT_PRIORITY,
3115 callback1.callback(),
3116 pool_.get(),
3117 BoundNetLog()));
3119 ClientSocketHandle handle2;
3120 TestCompletionCallback callback2;
3121 EXPECT_EQ(ERR_IO_PENDING, handle2.Init("a",
3122 params_,
3123 DEFAULT_PRIORITY,
3124 callback2.callback(),
3125 pool_.get(),
3126 BoundNetLog()));
3127 ClientSocketHandle handle3;
3128 TestCompletionCallback callback3;
3129 EXPECT_EQ(ERR_IO_PENDING, handle3.Init("a",
3130 params_,
3131 DEFAULT_PRIORITY,
3132 callback3.callback(),
3133 pool_.get(),
3134 BoundNetLog()));
3136 EXPECT_EQ(OK, callback1.WaitForResult());
3137 EXPECT_EQ(OK, callback2.WaitForResult());
3138 EXPECT_EQ(OK, callback3.WaitForResult());
3140 // Use the socket.
3141 EXPECT_EQ(1, handle1.socket()->Write(NULL, 1, CompletionCallback()));
3142 EXPECT_EQ(1, handle3.socket()->Write(NULL, 1, CompletionCallback()));
3144 handle1.Reset();
3145 handle2.Reset();
3146 handle3.Reset();
3148 EXPECT_EQ(OK, handle1.Init("a",
3149 params_,
3150 DEFAULT_PRIORITY,
3151 callback1.callback(),
3152 pool_.get(),
3153 BoundNetLog()));
3154 EXPECT_EQ(OK, handle2.Init("a",
3155 params_,
3156 DEFAULT_PRIORITY,
3157 callback2.callback(),
3158 pool_.get(),
3159 BoundNetLog()));
3160 EXPECT_EQ(OK, handle3.Init("a",
3161 params_,
3162 DEFAULT_PRIORITY,
3163 callback3.callback(),
3164 pool_.get(),
3165 BoundNetLog()));
3167 EXPECT_TRUE(handle1.socket()->WasEverUsed());
3168 EXPECT_TRUE(handle2.socket()->WasEverUsed());
3169 EXPECT_FALSE(handle3.socket()->WasEverUsed());
3172 TEST_F(ClientSocketPoolBaseTest, RequestSockets) {
3173 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
3174 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
3176 pool_->RequestSockets("a", &params_, 2, BoundNetLog());
3178 ASSERT_TRUE(pool_->HasGroup("a"));
3179 EXPECT_EQ(2, pool_->NumConnectJobsInGroup("a"));
3180 EXPECT_EQ(2, pool_->NumUnassignedConnectJobsInGroup("a"));
3181 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
3183 ClientSocketHandle handle1;
3184 TestCompletionCallback callback1;
3185 EXPECT_EQ(ERR_IO_PENDING, handle1.Init("a",
3186 params_,
3187 DEFAULT_PRIORITY,
3188 callback1.callback(),
3189 pool_.get(),
3190 BoundNetLog()));
3192 ClientSocketHandle handle2;
3193 TestCompletionCallback callback2;
3194 EXPECT_EQ(ERR_IO_PENDING, handle2.Init("a",
3195 params_,
3196 DEFAULT_PRIORITY,
3197 callback2.callback(),
3198 pool_.get(),
3199 BoundNetLog()));
3201 EXPECT_EQ(2, pool_->NumConnectJobsInGroup("a"));
3202 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a"));
3203 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
3205 EXPECT_EQ(OK, callback1.WaitForResult());
3206 EXPECT_EQ(OK, callback2.WaitForResult());
3207 handle1.Reset();
3208 handle2.Reset();
3210 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a"));
3211 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a"));
3212 EXPECT_EQ(2, pool_->IdleSocketCountInGroup("a"));
3215 TEST_F(ClientSocketPoolBaseTest, RequestSocketsWhenAlreadyHaveAConnectJob) {
3216 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
3217 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
3219 ClientSocketHandle handle1;
3220 TestCompletionCallback callback1;
3221 EXPECT_EQ(ERR_IO_PENDING, handle1.Init("a",
3222 params_,
3223 DEFAULT_PRIORITY,
3224 callback1.callback(),
3225 pool_.get(),
3226 BoundNetLog()));
3228 ASSERT_TRUE(pool_->HasGroup("a"));
3229 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a"));
3230 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a"));
3231 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
3233 pool_->RequestSockets("a", &params_, 2, BoundNetLog());
3235 EXPECT_EQ(2, pool_->NumConnectJobsInGroup("a"));
3236 EXPECT_EQ(1, pool_->NumUnassignedConnectJobsInGroup("a"));
3237 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
3239 ClientSocketHandle handle2;
3240 TestCompletionCallback callback2;
3241 EXPECT_EQ(ERR_IO_PENDING, handle2.Init("a",
3242 params_,
3243 DEFAULT_PRIORITY,
3244 callback2.callback(),
3245 pool_.get(),
3246 BoundNetLog()));
3248 EXPECT_EQ(2, pool_->NumConnectJobsInGroup("a"));
3249 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a"));
3250 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
3252 EXPECT_EQ(OK, callback1.WaitForResult());
3253 EXPECT_EQ(OK, callback2.WaitForResult());
3254 handle1.Reset();
3255 handle2.Reset();
3257 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a"));
3258 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a"));
3259 EXPECT_EQ(2, pool_->IdleSocketCountInGroup("a"));
3262 TEST_F(ClientSocketPoolBaseTest,
3263 RequestSocketsWhenAlreadyHaveMultipleConnectJob) {
3264 CreatePool(4, 4);
3265 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
3267 ClientSocketHandle handle1;
3268 TestCompletionCallback callback1;
3269 EXPECT_EQ(ERR_IO_PENDING, handle1.Init("a",
3270 params_,
3271 DEFAULT_PRIORITY,
3272 callback1.callback(),
3273 pool_.get(),
3274 BoundNetLog()));
3276 ClientSocketHandle handle2;
3277 TestCompletionCallback callback2;
3278 EXPECT_EQ(ERR_IO_PENDING, handle2.Init("a",
3279 params_,
3280 DEFAULT_PRIORITY,
3281 callback2.callback(),
3282 pool_.get(),
3283 BoundNetLog()));
3285 ClientSocketHandle handle3;
3286 TestCompletionCallback callback3;
3287 EXPECT_EQ(ERR_IO_PENDING, handle3.Init("a",
3288 params_,
3289 DEFAULT_PRIORITY,
3290 callback3.callback(),
3291 pool_.get(),
3292 BoundNetLog()));
3294 ASSERT_TRUE(pool_->HasGroup("a"));
3295 EXPECT_EQ(3, pool_->NumConnectJobsInGroup("a"));
3296 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a"));
3297 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
3299 pool_->RequestSockets("a", &params_, 2, BoundNetLog());
3301 EXPECT_EQ(3, pool_->NumConnectJobsInGroup("a"));
3302 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a"));
3303 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
3305 EXPECT_EQ(OK, callback1.WaitForResult());
3306 EXPECT_EQ(OK, callback2.WaitForResult());
3307 EXPECT_EQ(OK, callback3.WaitForResult());
3308 handle1.Reset();
3309 handle2.Reset();
3310 handle3.Reset();
3312 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a"));
3313 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a"));
3314 EXPECT_EQ(3, pool_->IdleSocketCountInGroup("a"));
3317 TEST_F(ClientSocketPoolBaseTest, RequestSocketsAtMaxSocketLimit) {
3318 CreatePool(kDefaultMaxSockets, kDefaultMaxSockets);
3319 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
3321 ASSERT_FALSE(pool_->HasGroup("a"));
3323 pool_->RequestSockets("a", &params_, kDefaultMaxSockets,
3324 BoundNetLog());
3326 ASSERT_TRUE(pool_->HasGroup("a"));
3327 EXPECT_EQ(kDefaultMaxSockets, pool_->NumConnectJobsInGroup("a"));
3328 EXPECT_EQ(kDefaultMaxSockets, pool_->NumUnassignedConnectJobsInGroup("a"));
3330 ASSERT_FALSE(pool_->HasGroup("b"));
3332 pool_->RequestSockets("b", &params_, kDefaultMaxSockets,
3333 BoundNetLog());
3335 ASSERT_FALSE(pool_->HasGroup("b"));
3338 TEST_F(ClientSocketPoolBaseTest, RequestSocketsHitMaxSocketLimit) {
3339 CreatePool(kDefaultMaxSockets, kDefaultMaxSockets);
3340 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
3342 ASSERT_FALSE(pool_->HasGroup("a"));
3344 pool_->RequestSockets("a", &params_, kDefaultMaxSockets - 1,
3345 BoundNetLog());
3347 ASSERT_TRUE(pool_->HasGroup("a"));
3348 EXPECT_EQ(kDefaultMaxSockets - 1, pool_->NumConnectJobsInGroup("a"));
3349 EXPECT_EQ(kDefaultMaxSockets - 1,
3350 pool_->NumUnassignedConnectJobsInGroup("a"));
3351 EXPECT_FALSE(pool_->IsStalled());
3353 ASSERT_FALSE(pool_->HasGroup("b"));
3355 pool_->RequestSockets("b", &params_, kDefaultMaxSockets,
3356 BoundNetLog());
3358 ASSERT_TRUE(pool_->HasGroup("b"));
3359 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("b"));
3360 EXPECT_FALSE(pool_->IsStalled());
3363 TEST_F(ClientSocketPoolBaseTest, RequestSocketsCountIdleSockets) {
3364 CreatePool(4, 4);
3365 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
3367 ClientSocketHandle handle1;
3368 TestCompletionCallback callback1;
3369 EXPECT_EQ(ERR_IO_PENDING, handle1.Init("a",
3370 params_,
3371 DEFAULT_PRIORITY,
3372 callback1.callback(),
3373 pool_.get(),
3374 BoundNetLog()));
3375 ASSERT_EQ(OK, callback1.WaitForResult());
3376 handle1.Reset();
3378 ASSERT_TRUE(pool_->HasGroup("a"));
3379 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a"));
3380 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a"));
3381 EXPECT_EQ(1, pool_->IdleSocketCountInGroup("a"));
3383 pool_->RequestSockets("a", &params_, 2, BoundNetLog());
3385 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a"));
3386 EXPECT_EQ(1, pool_->NumUnassignedConnectJobsInGroup("a"));
3387 EXPECT_EQ(1, pool_->IdleSocketCountInGroup("a"));
3390 TEST_F(ClientSocketPoolBaseTest, RequestSocketsCountActiveSockets) {
3391 CreatePool(4, 4);
3392 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
3394 ClientSocketHandle handle1;
3395 TestCompletionCallback callback1;
3396 EXPECT_EQ(ERR_IO_PENDING, handle1.Init("a",
3397 params_,
3398 DEFAULT_PRIORITY,
3399 callback1.callback(),
3400 pool_.get(),
3401 BoundNetLog()));
3402 ASSERT_EQ(OK, callback1.WaitForResult());
3404 ASSERT_TRUE(pool_->HasGroup("a"));
3405 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a"));
3406 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a"));
3407 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
3408 EXPECT_EQ(1, pool_->NumActiveSocketsInGroup("a"));
3410 pool_->RequestSockets("a", &params_, 2, BoundNetLog());
3412 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a"));
3413 EXPECT_EQ(1, pool_->NumUnassignedConnectJobsInGroup("a"));
3414 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
3415 EXPECT_EQ(1, pool_->NumActiveSocketsInGroup("a"));
3418 TEST_F(ClientSocketPoolBaseTest, RequestSocketsSynchronous) {
3419 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
3420 connect_job_factory_->set_job_type(TestConnectJob::kMockJob);
3422 pool_->RequestSockets("a", &params_, kDefaultMaxSocketsPerGroup,
3423 BoundNetLog());
3425 ASSERT_TRUE(pool_->HasGroup("a"));
3426 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a"));
3427 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a"));
3428 EXPECT_EQ(kDefaultMaxSocketsPerGroup, pool_->IdleSocketCountInGroup("a"));
3430 pool_->RequestSockets("b", &params_, kDefaultMaxSocketsPerGroup,
3431 BoundNetLog());
3433 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("b"));
3434 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("b"));
3435 EXPECT_EQ(kDefaultMaxSocketsPerGroup, pool_->IdleSocketCountInGroup("b"));
3438 TEST_F(ClientSocketPoolBaseTest, RequestSocketsSynchronousError) {
3439 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
3440 connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob);
3442 pool_->RequestSockets("a", &params_, kDefaultMaxSocketsPerGroup,
3443 BoundNetLog());
3445 ASSERT_FALSE(pool_->HasGroup("a"));
3447 connect_job_factory_->set_job_type(
3448 TestConnectJob::kMockAdditionalErrorStateJob);
3449 pool_->RequestSockets("a", &params_, kDefaultMaxSocketsPerGroup,
3450 BoundNetLog());
3452 ASSERT_FALSE(pool_->HasGroup("a"));
3455 TEST_F(ClientSocketPoolBaseTest, RequestSocketsMultipleTimesDoesNothing) {
3456 CreatePool(4, 4);
3457 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
3459 pool_->RequestSockets("a", &params_, 2, BoundNetLog());
3461 ASSERT_TRUE(pool_->HasGroup("a"));
3462 EXPECT_EQ(2, pool_->NumConnectJobsInGroup("a"));
3463 EXPECT_EQ(2, pool_->NumUnassignedConnectJobsInGroup("a"));
3464 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
3466 pool_->RequestSockets("a", &params_, 2, BoundNetLog());
3467 EXPECT_EQ(2, pool_->NumConnectJobsInGroup("a"));
3468 EXPECT_EQ(2, pool_->NumUnassignedConnectJobsInGroup("a"));
3469 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
3471 ClientSocketHandle handle1;
3472 TestCompletionCallback callback1;
3473 EXPECT_EQ(ERR_IO_PENDING, handle1.Init("a",
3474 params_,
3475 DEFAULT_PRIORITY,
3476 callback1.callback(),
3477 pool_.get(),
3478 BoundNetLog()));
3479 ASSERT_EQ(OK, callback1.WaitForResult());
3481 ClientSocketHandle handle2;
3482 TestCompletionCallback callback2;
3483 int rv = handle2.Init("a",
3484 params_,
3485 DEFAULT_PRIORITY,
3486 callback2.callback(),
3487 pool_.get(),
3488 BoundNetLog());
3489 if (rv != OK) {
3490 EXPECT_EQ(ERR_IO_PENDING, rv);
3491 EXPECT_EQ(OK, callback2.WaitForResult());
3494 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a"));
3495 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a"));
3496 EXPECT_EQ(2, pool_->NumActiveSocketsInGroup("a"));
3497 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
3499 handle1.Reset();
3500 handle2.Reset();
3502 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a"));
3503 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a"));
3504 EXPECT_EQ(2, pool_->IdleSocketCountInGroup("a"));
3506 pool_->RequestSockets("a", &params_, 2, BoundNetLog());
3507 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a"));
3508 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a"));
3509 EXPECT_EQ(2, pool_->IdleSocketCountInGroup("a"));
3512 TEST_F(ClientSocketPoolBaseTest, RequestSocketsDifferentNumSockets) {
3513 CreatePool(4, 4);
3514 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
3516 pool_->RequestSockets("a", &params_, 1, BoundNetLog());
3518 ASSERT_TRUE(pool_->HasGroup("a"));
3519 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a"));
3520 EXPECT_EQ(1, pool_->NumUnassignedConnectJobsInGroup("a"));
3521 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
3523 pool_->RequestSockets("a", &params_, 2, BoundNetLog());
3524 EXPECT_EQ(2, pool_->NumConnectJobsInGroup("a"));
3525 EXPECT_EQ(2, pool_->NumUnassignedConnectJobsInGroup("a"));
3526 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
3528 pool_->RequestSockets("a", &params_, 3, BoundNetLog());
3529 EXPECT_EQ(3, pool_->NumConnectJobsInGroup("a"));
3530 EXPECT_EQ(3, pool_->NumUnassignedConnectJobsInGroup("a"));
3531 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
3533 pool_->RequestSockets("a", &params_, 1, BoundNetLog());
3534 EXPECT_EQ(3, pool_->NumConnectJobsInGroup("a"));
3535 EXPECT_EQ(3, pool_->NumUnassignedConnectJobsInGroup("a"));
3536 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
3539 TEST_F(ClientSocketPoolBaseTest, PreconnectJobsTakenByNormalRequests) {
3540 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
3541 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
3543 pool_->RequestSockets("a", &params_, 1, BoundNetLog());
3545 ASSERT_TRUE(pool_->HasGroup("a"));
3546 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a"));
3547 EXPECT_EQ(1, pool_->NumUnassignedConnectJobsInGroup("a"));
3548 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
3550 ClientSocketHandle handle1;
3551 TestCompletionCallback callback1;
3552 EXPECT_EQ(ERR_IO_PENDING, handle1.Init("a",
3553 params_,
3554 DEFAULT_PRIORITY,
3555 callback1.callback(),
3556 pool_.get(),
3557 BoundNetLog()));
3559 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a"));
3560 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a"));
3561 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
3563 ASSERT_EQ(OK, callback1.WaitForResult());
3565 // Make sure if a preconnected socket is not fully connected when a request
3566 // starts, it has a connect start time.
3567 TestLoadTimingInfoConnectedNotReused(handle1);
3568 handle1.Reset();
3570 EXPECT_EQ(1, pool_->IdleSocketCountInGroup("a"));
3573 // Checks that fully connected preconnect jobs have no connect times, and are
3574 // marked as reused.
3575 TEST_F(ClientSocketPoolBaseTest, ConnectedPreconnectJobsHaveNoConnectTimes) {
3576 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
3577 connect_job_factory_->set_job_type(TestConnectJob::kMockJob);
3578 pool_->RequestSockets("a", &params_, 1, BoundNetLog());
3580 ASSERT_TRUE(pool_->HasGroup("a"));
3581 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a"));
3582 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a"));
3583 EXPECT_EQ(1, pool_->IdleSocketCountInGroup("a"));
3585 ClientSocketHandle handle;
3586 TestCompletionCallback callback;
3587 EXPECT_EQ(OK, handle.Init("a",
3588 params_,
3589 DEFAULT_PRIORITY,
3590 callback.callback(),
3591 pool_.get(),
3592 BoundNetLog()));
3594 // Make sure the idle socket was used.
3595 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
3597 TestLoadTimingInfoConnectedReused(handle);
3598 handle.Reset();
3599 TestLoadTimingInfoNotConnected(handle);
3602 // http://crbug.com/64940 regression test.
3603 TEST_F(ClientSocketPoolBaseTest, PreconnectClosesIdleSocketRemovesGroup) {
3604 const int kMaxTotalSockets = 3;
3605 const int kMaxSocketsPerGroup = 2;
3606 CreatePool(kMaxTotalSockets, kMaxSocketsPerGroup);
3607 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
3609 // Note that group name ordering matters here. "a" comes before "b", so
3610 // CloseOneIdleSocket() will try to close "a"'s idle socket.
3612 // Set up one idle socket in "a".
3613 ClientSocketHandle handle1;
3614 TestCompletionCallback callback1;
3615 EXPECT_EQ(ERR_IO_PENDING, handle1.Init("a",
3616 params_,
3617 DEFAULT_PRIORITY,
3618 callback1.callback(),
3619 pool_.get(),
3620 BoundNetLog()));
3622 ASSERT_EQ(OK, callback1.WaitForResult());
3623 handle1.Reset();
3624 EXPECT_EQ(1, pool_->IdleSocketCountInGroup("a"));
3626 // Set up two active sockets in "b".
3627 ClientSocketHandle handle2;
3628 TestCompletionCallback callback2;
3629 EXPECT_EQ(ERR_IO_PENDING, handle1.Init("b",
3630 params_,
3631 DEFAULT_PRIORITY,
3632 callback1.callback(),
3633 pool_.get(),
3634 BoundNetLog()));
3635 EXPECT_EQ(ERR_IO_PENDING, handle2.Init("b",
3636 params_,
3637 DEFAULT_PRIORITY,
3638 callback2.callback(),
3639 pool_.get(),
3640 BoundNetLog()));
3642 ASSERT_EQ(OK, callback1.WaitForResult());
3643 ASSERT_EQ(OK, callback2.WaitForResult());
3644 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("b"));
3645 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("b"));
3646 EXPECT_EQ(2, pool_->NumActiveSocketsInGroup("b"));
3648 // Now we have 1 idle socket in "a" and 2 active sockets in "b". This means
3649 // we've maxed out on sockets, since we set |kMaxTotalSockets| to 3.
3650 // Requesting 2 preconnected sockets for "a" should fail to allocate any more
3651 // sockets for "a", and "b" should still have 2 active sockets.
3653 pool_->RequestSockets("a", &params_, 2, BoundNetLog());
3654 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a"));
3655 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a"));
3656 EXPECT_EQ(1, pool_->IdleSocketCountInGroup("a"));
3657 EXPECT_EQ(0, pool_->NumActiveSocketsInGroup("a"));
3658 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("b"));
3659 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("b"));
3660 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("b"));
3661 EXPECT_EQ(2, pool_->NumActiveSocketsInGroup("b"));
3663 // Now release the 2 active sockets for "b". This will give us 1 idle socket
3664 // in "a" and 2 idle sockets in "b". Requesting 2 preconnected sockets for
3665 // "a" should result in closing 1 for "b".
3666 handle1.Reset();
3667 handle2.Reset();
3668 EXPECT_EQ(2, pool_->IdleSocketCountInGroup("b"));
3669 EXPECT_EQ(0, pool_->NumActiveSocketsInGroup("b"));
3671 pool_->RequestSockets("a", &params_, 2, BoundNetLog());
3672 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a"));
3673 EXPECT_EQ(1, pool_->NumUnassignedConnectJobsInGroup("a"));
3674 EXPECT_EQ(1, pool_->IdleSocketCountInGroup("a"));
3675 EXPECT_EQ(0, pool_->NumActiveSocketsInGroup("a"));
3676 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("b"));
3677 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("b"));
3678 EXPECT_EQ(1, pool_->IdleSocketCountInGroup("b"));
3679 EXPECT_EQ(0, pool_->NumActiveSocketsInGroup("b"));
3682 TEST_F(ClientSocketPoolBaseTest, PreconnectWithoutBackupJob) {
3683 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
3684 pool_->EnableConnectBackupJobs();
3686 // Make the ConnectJob hang until it times out, shorten the timeout.
3687 connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob);
3688 connect_job_factory_->set_timeout_duration(
3689 base::TimeDelta::FromMilliseconds(500));
3690 pool_->RequestSockets("a", &params_, 1, BoundNetLog());
3691 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a"));
3692 EXPECT_EQ(1, pool_->NumUnassignedConnectJobsInGroup("a"));
3693 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
3695 // Verify the backup timer doesn't create a backup job, by making
3696 // the backup job a pending job instead of a waiting job, so it
3697 // *would* complete if it were created.
3698 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
3699 base::MessageLoop::current()->PostDelayedTask(
3700 FROM_HERE,
3701 base::MessageLoop::QuitClosure(),
3702 base::TimeDelta::FromSeconds(1));
3703 base::MessageLoop::current()->Run();
3704 EXPECT_FALSE(pool_->HasGroup("a"));
3707 TEST_F(ClientSocketPoolBaseTest, PreconnectWithBackupJob) {
3708 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
3709 pool_->EnableConnectBackupJobs();
3711 // Make the ConnectJob hang forever.
3712 connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob);
3713 pool_->RequestSockets("a", &params_, 1, BoundNetLog());
3714 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a"));
3715 EXPECT_EQ(1, pool_->NumUnassignedConnectJobsInGroup("a"));
3716 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
3717 base::MessageLoop::current()->RunUntilIdle();
3719 // Make the backup job be a pending job, so it completes normally.
3720 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
3721 ClientSocketHandle handle;
3722 TestCompletionCallback callback;
3723 EXPECT_EQ(ERR_IO_PENDING, handle.Init("a",
3724 params_,
3725 DEFAULT_PRIORITY,
3726 callback.callback(),
3727 pool_.get(),
3728 BoundNetLog()));
3729 // Timer has started, but the backup connect job shouldn't be created yet.
3730 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a"));
3731 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a"));
3732 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
3733 EXPECT_EQ(0, pool_->NumActiveSocketsInGroup("a"));
3734 ASSERT_EQ(OK, callback.WaitForResult());
3736 // The hung connect job should still be there, but everything else should be
3737 // complete.
3738 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a"));
3739 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a"));
3740 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
3741 EXPECT_EQ(1, pool_->NumActiveSocketsInGroup("a"));
3744 // Tests that a preconnect that starts out with unread data can still be used.
3745 // http://crbug.com/334467
3746 TEST_F(ClientSocketPoolBaseTest, PreconnectWithUnreadData) {
3747 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
3748 connect_job_factory_->set_job_type(TestConnectJob::kMockUnreadDataJob);
3750 pool_->RequestSockets("a", &params_, 1, BoundNetLog());
3752 ASSERT_TRUE(pool_->HasGroup("a"));
3753 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a"));
3754 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a"));
3755 EXPECT_EQ(1, pool_->IdleSocketCountInGroup("a"));
3757 // Fail future jobs to be sure that handle receives the preconnected socket
3758 // rather than closing it and making a new one.
3759 connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob);
3760 ClientSocketHandle handle;
3761 TestCompletionCallback callback;
3762 EXPECT_EQ(OK, handle.Init("a",
3763 params_,
3764 DEFAULT_PRIORITY,
3765 callback.callback(),
3766 pool_.get(),
3767 BoundNetLog()));
3769 ASSERT_TRUE(pool_->HasGroup("a"));
3770 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a"));
3771 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a"));
3772 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
3774 // Drain the pending read.
3775 EXPECT_EQ(1, handle.socket()->Read(NULL, 1, CompletionCallback()));
3777 TestLoadTimingInfoConnectedReused(handle);
3778 handle.Reset();
3780 // The socket should be usable now that it's idle again.
3781 EXPECT_EQ(1, pool_->IdleSocketCountInGroup("a"));
3784 class MockLayeredPool : public HigherLayeredPool {
3785 public:
3786 MockLayeredPool(TestClientSocketPool* pool,
3787 const std::string& group_name)
3788 : pool_(pool),
3789 group_name_(group_name),
3790 can_release_connection_(true) {
3791 pool_->AddHigherLayeredPool(this);
3794 ~MockLayeredPool() {
3795 pool_->RemoveHigherLayeredPool(this);
3798 int RequestSocket(TestClientSocketPool* pool) {
3799 scoped_refptr<TestSocketParams> params(
3800 new TestSocketParams(false /* ignore_limits */));
3801 return handle_.Init(group_name_, params, DEFAULT_PRIORITY,
3802 callback_.callback(), pool, BoundNetLog());
3805 int RequestSocketWithoutLimits(TestClientSocketPool* pool) {
3806 scoped_refptr<TestSocketParams> params(
3807 new TestSocketParams(true /* ignore_limits */));
3808 return handle_.Init(group_name_, params, MAXIMUM_PRIORITY,
3809 callback_.callback(), pool, BoundNetLog());
3812 bool ReleaseOneConnection() {
3813 if (!handle_.is_initialized() || !can_release_connection_) {
3814 return false;
3816 handle_.socket()->Disconnect();
3817 handle_.Reset();
3818 return true;
3821 void set_can_release_connection(bool can_release_connection) {
3822 can_release_connection_ = can_release_connection;
3825 MOCK_METHOD0(CloseOneIdleConnection, bool());
3827 private:
3828 TestClientSocketPool* const pool_;
3829 ClientSocketHandle handle_;
3830 TestCompletionCallback callback_;
3831 const std::string group_name_;
3832 bool can_release_connection_;
3835 TEST_F(ClientSocketPoolBaseTest, FailToCloseIdleSocketsNotHeldByLayeredPool) {
3836 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
3837 connect_job_factory_->set_job_type(TestConnectJob::kMockJob);
3839 MockLayeredPool mock_layered_pool(pool_.get(), "foo");
3840 EXPECT_EQ(OK, mock_layered_pool.RequestSocket(pool_.get()));
3841 EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection())
3842 .WillOnce(Return(false));
3843 EXPECT_FALSE(pool_->CloseOneIdleConnectionInHigherLayeredPool());
3846 TEST_F(ClientSocketPoolBaseTest, ForciblyCloseIdleSocketsHeldByLayeredPool) {
3847 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
3848 connect_job_factory_->set_job_type(TestConnectJob::kMockJob);
3850 MockLayeredPool mock_layered_pool(pool_.get(), "foo");
3851 EXPECT_EQ(OK, mock_layered_pool.RequestSocket(pool_.get()));
3852 EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection())
3853 .WillOnce(Invoke(&mock_layered_pool,
3854 &MockLayeredPool::ReleaseOneConnection));
3855 EXPECT_TRUE(pool_->CloseOneIdleConnectionInHigherLayeredPool());
3858 // Tests the basic case of closing an idle socket in a higher layered pool when
3859 // a new request is issued and the lower layer pool is stalled.
3860 TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketsHeldByLayeredPoolWhenNeeded) {
3861 CreatePool(1, 1);
3862 connect_job_factory_->set_job_type(TestConnectJob::kMockJob);
3864 MockLayeredPool mock_layered_pool(pool_.get(), "foo");
3865 EXPECT_EQ(OK, mock_layered_pool.RequestSocket(pool_.get()));
3866 EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection())
3867 .WillOnce(Invoke(&mock_layered_pool,
3868 &MockLayeredPool::ReleaseOneConnection));
3869 ClientSocketHandle handle;
3870 TestCompletionCallback callback;
3871 EXPECT_EQ(ERR_IO_PENDING, handle.Init("a",
3872 params_,
3873 DEFAULT_PRIORITY,
3874 callback.callback(),
3875 pool_.get(),
3876 BoundNetLog()));
3877 EXPECT_EQ(OK, callback.WaitForResult());
3880 // Same as above, but the idle socket is in the same group as the stalled
3881 // socket, and closes the only other request in its group when closing requests
3882 // in higher layered pools. This generally shouldn't happen, but it may be
3883 // possible if a higher level pool issues a request and the request is
3884 // subsequently cancelled. Even if it's not possible, best not to crash.
3885 TEST_F(ClientSocketPoolBaseTest,
3886 CloseIdleSocketsHeldByLayeredPoolWhenNeededSameGroup) {
3887 CreatePool(2, 2);
3888 connect_job_factory_->set_job_type(TestConnectJob::kMockJob);
3890 // Need a socket in another group for the pool to be stalled (If a group
3891 // has the maximum number of connections already, it's not stalled).
3892 ClientSocketHandle handle1;
3893 TestCompletionCallback callback1;
3894 EXPECT_EQ(OK, handle1.Init("group1",
3895 params_,
3896 DEFAULT_PRIORITY,
3897 callback1.callback(),
3898 pool_.get(),
3899 BoundNetLog()));
3901 MockLayeredPool mock_layered_pool(pool_.get(), "group2");
3902 EXPECT_EQ(OK, mock_layered_pool.RequestSocket(pool_.get()));
3903 EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection())
3904 .WillOnce(Invoke(&mock_layered_pool,
3905 &MockLayeredPool::ReleaseOneConnection));
3906 ClientSocketHandle handle;
3907 TestCompletionCallback callback2;
3908 EXPECT_EQ(ERR_IO_PENDING, handle.Init("group2",
3909 params_,
3910 DEFAULT_PRIORITY,
3911 callback2.callback(),
3912 pool_.get(),
3913 BoundNetLog()));
3914 EXPECT_EQ(OK, callback2.WaitForResult());
3917 // Tests the case when an idle socket can be closed when a new request is
3918 // issued, and the new request belongs to a group that was previously stalled.
3919 TEST_F(ClientSocketPoolBaseTest,
3920 CloseIdleSocketsHeldByLayeredPoolInSameGroupWhenNeeded) {
3921 CreatePool(2, 2);
3922 std::list<TestConnectJob::JobType> job_types;
3923 job_types.push_back(TestConnectJob::kMockJob);
3924 job_types.push_back(TestConnectJob::kMockJob);
3925 job_types.push_back(TestConnectJob::kMockJob);
3926 job_types.push_back(TestConnectJob::kMockJob);
3927 connect_job_factory_->set_job_types(&job_types);
3929 ClientSocketHandle handle1;
3930 TestCompletionCallback callback1;
3931 EXPECT_EQ(OK, handle1.Init("group1",
3932 params_,
3933 DEFAULT_PRIORITY,
3934 callback1.callback(),
3935 pool_.get(),
3936 BoundNetLog()));
3938 MockLayeredPool mock_layered_pool(pool_.get(), "group2");
3939 EXPECT_EQ(OK, mock_layered_pool.RequestSocket(pool_.get()));
3940 EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection())
3941 .WillRepeatedly(Invoke(&mock_layered_pool,
3942 &MockLayeredPool::ReleaseOneConnection));
3943 mock_layered_pool.set_can_release_connection(false);
3945 // The third request is made when the socket pool is in a stalled state.
3946 ClientSocketHandle handle3;
3947 TestCompletionCallback callback3;
3948 EXPECT_EQ(ERR_IO_PENDING, handle3.Init("group3",
3949 params_,
3950 DEFAULT_PRIORITY,
3951 callback3.callback(),
3952 pool_.get(),
3953 BoundNetLog()));
3955 base::RunLoop().RunUntilIdle();
3956 EXPECT_FALSE(callback3.have_result());
3958 // The fourth request is made when the pool is no longer stalled. The third
3959 // request should be serviced first, since it was issued first and has the
3960 // same priority.
3961 mock_layered_pool.set_can_release_connection(true);
3962 ClientSocketHandle handle4;
3963 TestCompletionCallback callback4;
3964 EXPECT_EQ(ERR_IO_PENDING, handle4.Init("group3",
3965 params_,
3966 DEFAULT_PRIORITY,
3967 callback4.callback(),
3968 pool_.get(),
3969 BoundNetLog()));
3970 EXPECT_EQ(OK, callback3.WaitForResult());
3971 EXPECT_FALSE(callback4.have_result());
3973 // Closing a handle should free up another socket slot.
3974 handle1.Reset();
3975 EXPECT_EQ(OK, callback4.WaitForResult());
3978 // Tests the case when an idle socket can be closed when a new request is
3979 // issued, and the new request belongs to a group that was previously stalled.
3981 // The two differences from the above test are that the stalled requests are not
3982 // in the same group as the layered pool's request, and the the fourth request
3983 // has a higher priority than the third one, so gets a socket first.
3984 TEST_F(ClientSocketPoolBaseTest,
3985 CloseIdleSocketsHeldByLayeredPoolInSameGroupWhenNeeded2) {
3986 CreatePool(2, 2);
3987 std::list<TestConnectJob::JobType> job_types;
3988 job_types.push_back(TestConnectJob::kMockJob);
3989 job_types.push_back(TestConnectJob::kMockJob);
3990 job_types.push_back(TestConnectJob::kMockJob);
3991 job_types.push_back(TestConnectJob::kMockJob);
3992 connect_job_factory_->set_job_types(&job_types);
3994 ClientSocketHandle handle1;
3995 TestCompletionCallback callback1;
3996 EXPECT_EQ(OK, handle1.Init("group1",
3997 params_,
3998 DEFAULT_PRIORITY,
3999 callback1.callback(),
4000 pool_.get(),
4001 BoundNetLog()));
4003 MockLayeredPool mock_layered_pool(pool_.get(), "group2");
4004 EXPECT_EQ(OK, mock_layered_pool.RequestSocket(pool_.get()));
4005 EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection())
4006 .WillRepeatedly(Invoke(&mock_layered_pool,
4007 &MockLayeredPool::ReleaseOneConnection));
4008 mock_layered_pool.set_can_release_connection(false);
4010 // The third request is made when the socket pool is in a stalled state.
4011 ClientSocketHandle handle3;
4012 TestCompletionCallback callback3;
4013 EXPECT_EQ(ERR_IO_PENDING, handle3.Init("group3",
4014 params_,
4015 MEDIUM,
4016 callback3.callback(),
4017 pool_.get(),
4018 BoundNetLog()));
4020 base::RunLoop().RunUntilIdle();
4021 EXPECT_FALSE(callback3.have_result());
4023 // The fourth request is made when the pool is no longer stalled. This
4024 // request has a higher priority than the third request, so is serviced first.
4025 mock_layered_pool.set_can_release_connection(true);
4026 ClientSocketHandle handle4;
4027 TestCompletionCallback callback4;
4028 EXPECT_EQ(ERR_IO_PENDING, handle4.Init("group3",
4029 params_,
4030 HIGHEST,
4031 callback4.callback(),
4032 pool_.get(),
4033 BoundNetLog()));
4034 EXPECT_EQ(OK, callback4.WaitForResult());
4035 EXPECT_FALSE(callback3.have_result());
4037 // Closing a handle should free up another socket slot.
4038 handle1.Reset();
4039 EXPECT_EQ(OK, callback3.WaitForResult());
4042 TEST_F(ClientSocketPoolBaseTest,
4043 CloseMultipleIdleSocketsHeldByLayeredPoolWhenNeeded) {
4044 CreatePool(1, 1);
4045 connect_job_factory_->set_job_type(TestConnectJob::kMockJob);
4047 MockLayeredPool mock_layered_pool1(pool_.get(), "foo");
4048 EXPECT_EQ(OK, mock_layered_pool1.RequestSocket(pool_.get()));
4049 EXPECT_CALL(mock_layered_pool1, CloseOneIdleConnection())
4050 .WillRepeatedly(Invoke(&mock_layered_pool1,
4051 &MockLayeredPool::ReleaseOneConnection));
4052 MockLayeredPool mock_layered_pool2(pool_.get(), "bar");
4053 EXPECT_EQ(OK, mock_layered_pool2.RequestSocketWithoutLimits(pool_.get()));
4054 EXPECT_CALL(mock_layered_pool2, CloseOneIdleConnection())
4055 .WillRepeatedly(Invoke(&mock_layered_pool2,
4056 &MockLayeredPool::ReleaseOneConnection));
4057 ClientSocketHandle handle;
4058 TestCompletionCallback callback;
4059 EXPECT_EQ(ERR_IO_PENDING, handle.Init("a",
4060 params_,
4061 DEFAULT_PRIORITY,
4062 callback.callback(),
4063 pool_.get(),
4064 BoundNetLog()));
4065 EXPECT_EQ(OK, callback.WaitForResult());
4068 // Test that when a socket pool and group are at their limits, a request
4069 // with |ignore_limits| triggers creation of a new socket, and gets the socket
4070 // instead of a request with the same priority that was issued earlier, but
4071 // that does not have |ignore_limits| set.
4072 TEST_F(ClientSocketPoolBaseTest, IgnoreLimits) {
4073 scoped_refptr<TestSocketParams> params_ignore_limits(
4074 new TestSocketParams(true /* ignore_limits */));
4075 CreatePool(1, 1);
4077 // Issue a request to reach the socket pool limit.
4078 EXPECT_EQ(OK, StartRequestWithParams("a", MAXIMUM_PRIORITY, params_));
4079 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a"));
4081 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
4083 EXPECT_EQ(ERR_IO_PENDING, StartRequestWithParams("a", MAXIMUM_PRIORITY,
4084 params_));
4085 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a"));
4087 EXPECT_EQ(ERR_IO_PENDING, StartRequestWithParams("a", MAXIMUM_PRIORITY,
4088 params_ignore_limits));
4089 ASSERT_EQ(1, pool_->NumConnectJobsInGroup("a"));
4091 EXPECT_EQ(OK, request(2)->WaitForResult());
4092 EXPECT_FALSE(request(1)->have_result());
4095 // Test that when a socket pool and group are at their limits, a ConnectJob
4096 // issued for a request with |ignore_limits| set is not cancelled when a request
4097 // without |ignore_limits| issued to the same group is cancelled.
4098 TEST_F(ClientSocketPoolBaseTest, IgnoreLimitsCancelOtherJob) {
4099 scoped_refptr<TestSocketParams> params_ignore_limits(
4100 new TestSocketParams(true /* ignore_limits */));
4101 CreatePool(1, 1);
4103 // Issue a request to reach the socket pool limit.
4104 EXPECT_EQ(OK, StartRequestWithParams("a", MAXIMUM_PRIORITY, params_));
4105 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a"));
4107 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
4109 EXPECT_EQ(ERR_IO_PENDING, StartRequestWithParams("a", MAXIMUM_PRIORITY,
4110 params_));
4111 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a"));
4113 EXPECT_EQ(ERR_IO_PENDING, StartRequestWithParams("a", MAXIMUM_PRIORITY,
4114 params_ignore_limits));
4115 ASSERT_EQ(1, pool_->NumConnectJobsInGroup("a"));
4117 // Cancel the pending request without ignore_limits set. The ConnectJob
4118 // should not be cancelled.
4119 request(1)->handle()->Reset();
4120 ASSERT_EQ(1, pool_->NumConnectJobsInGroup("a"));
4122 EXPECT_EQ(OK, request(2)->WaitForResult());
4123 EXPECT_FALSE(request(1)->have_result());
4126 } // namespace
4128 } // namespace net