net cleanup: Remove unnecessary namespace prefixes.
[chromium-blink-merge.git] / net / socket / client_socket_pool_base_unittest.cc
blob2c5026600d3d077822a5e46fd43678e84cb56c29
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, NetLog::SOURCE_SOCKET)),
120 was_used_to_convey_data_(false) {}
122 // Sets whether the socket has unread data. If true, the next call to Read()
123 // will return 1 byte and IsConnectedAndIdle() will return false.
124 void set_has_unread_data(bool has_unread_data) {
125 has_unread_data_ = has_unread_data;
128 // Socket implementation.
129 int Read(IOBuffer* /* buf */,
130 int len,
131 const CompletionCallback& /* callback */) override {
132 if (has_unread_data_ && len > 0) {
133 has_unread_data_ = false;
134 was_used_to_convey_data_ = true;
135 return 1;
137 return ERR_UNEXPECTED;
140 int Write(IOBuffer* /* buf */,
141 int len,
142 const CompletionCallback& /* callback */) override {
143 was_used_to_convey_data_ = true;
144 return len;
146 int SetReceiveBufferSize(int32 size) override { return OK; }
147 int SetSendBufferSize(int32 size) override { return OK; }
149 // StreamSocket implementation.
150 int Connect(const CompletionCallback& callback) override {
151 connected_ = true;
152 return OK;
155 void Disconnect() override { connected_ = false; }
156 bool IsConnected() const override { return connected_; }
157 bool IsConnectedAndIdle() const override {
158 return connected_ && !has_unread_data_;
161 int GetPeerAddress(IPEndPoint* /* address */) const override {
162 return ERR_UNEXPECTED;
165 int GetLocalAddress(IPEndPoint* /* address */) const override {
166 return ERR_UNEXPECTED;
169 const BoundNetLog& NetLog() const override { return net_log_; }
171 void SetSubresourceSpeculation() override {}
172 void SetOmniboxSpeculation() override {}
173 bool WasEverUsed() const override { return was_used_to_convey_data_; }
174 bool UsingTCPFastOpen() const override { return false; }
175 bool WasNpnNegotiated() const override { return false; }
176 NextProto GetNegotiatedProtocol() const override { return kProtoUnknown; }
177 bool GetSSLInfo(SSLInfo* ssl_info) override { return false; }
179 private:
180 bool connected_;
181 bool has_unread_data_;
182 BoundNetLog net_log_;
183 bool was_used_to_convey_data_;
185 DISALLOW_COPY_AND_ASSIGN(MockClientSocket);
188 class TestConnectJob;
190 class MockClientSocketFactory : public ClientSocketFactory {
191 public:
192 MockClientSocketFactory() : allocation_count_(0) {}
194 scoped_ptr<DatagramClientSocket> CreateDatagramClientSocket(
195 DatagramSocket::BindType bind_type,
196 const RandIntCallback& rand_int_cb,
197 NetLog* net_log,
198 const NetLog::Source& source) override {
199 NOTREACHED();
200 return scoped_ptr<DatagramClientSocket>();
203 scoped_ptr<StreamSocket> CreateTransportClientSocket(
204 const AddressList& addresses,
205 NetLog* /* net_log */,
206 const NetLog::Source& /*source*/) override {
207 allocation_count_++;
208 return scoped_ptr<StreamSocket>();
211 scoped_ptr<SSLClientSocket> CreateSSLClientSocket(
212 scoped_ptr<ClientSocketHandle> transport_socket,
213 const HostPortPair& host_and_port,
214 const SSLConfig& ssl_config,
215 const SSLClientSocketContext& context) override {
216 NOTIMPLEMENTED();
217 return scoped_ptr<SSLClientSocket>();
220 void ClearSSLSessionCache() override { NOTIMPLEMENTED(); }
222 void WaitForSignal(TestConnectJob* job) { waiting_jobs_.push_back(job); }
224 void SignalJobs();
226 void SignalJob(size_t job);
228 void SetJobLoadState(size_t job, LoadState load_state);
230 int allocation_count() const { return allocation_count_; }
232 private:
233 int allocation_count_;
234 std::vector<TestConnectJob*> waiting_jobs_;
237 class TestConnectJob : public ConnectJob {
238 public:
239 enum JobType {
240 kMockJob,
241 kMockFailingJob,
242 kMockPendingJob,
243 kMockPendingFailingJob,
244 kMockWaitingJob,
245 kMockRecoverableJob,
246 kMockPendingRecoverableJob,
247 kMockAdditionalErrorStateJob,
248 kMockPendingAdditionalErrorStateJob,
249 kMockUnreadDataJob,
252 // The kMockPendingJob uses a slight delay before allowing the connect
253 // to complete.
254 static const int kPendingConnectDelay = 2;
256 TestConnectJob(JobType job_type,
257 const std::string& group_name,
258 const TestClientSocketPoolBase::Request& request,
259 base::TimeDelta timeout_duration,
260 ConnectJob::Delegate* delegate,
261 MockClientSocketFactory* client_socket_factory,
262 NetLog* net_log)
263 : ConnectJob(group_name, timeout_duration, request.priority(), delegate,
264 BoundNetLog::Make(net_log, NetLog::SOURCE_CONNECT_JOB)),
265 job_type_(job_type),
266 client_socket_factory_(client_socket_factory),
267 load_state_(LOAD_STATE_IDLE),
268 store_additional_error_state_(false),
269 weak_factory_(this) {
272 void Signal() {
273 DoConnect(waiting_success_, true /* async */, false /* recoverable */);
276 void set_load_state(LoadState load_state) { load_state_ = load_state; }
278 // From ConnectJob:
280 LoadState GetLoadState() const override { return load_state_; }
282 void GetAdditionalErrorState(ClientSocketHandle* handle) override {
283 if (store_additional_error_state_) {
284 // Set all of the additional error state fields in some way.
285 handle->set_is_ssl_error(true);
286 HttpResponseInfo info;
287 info.headers = new HttpResponseHeaders(std::string());
288 handle->set_ssl_error_response_info(info);
292 private:
293 // From ConnectJob:
295 int ConnectInternal() override {
296 AddressList ignored;
297 client_socket_factory_->CreateTransportClientSocket(ignored, NULL,
298 NetLog::Source());
299 SetSocket(
300 scoped_ptr<StreamSocket>(new MockClientSocket(net_log().net_log())));
301 switch (job_type_) {
302 case kMockJob:
303 return DoConnect(true /* successful */, false /* sync */,
304 false /* recoverable */);
305 case kMockFailingJob:
306 return DoConnect(false /* error */, false /* sync */,
307 false /* recoverable */);
308 case kMockPendingJob:
309 set_load_state(LOAD_STATE_CONNECTING);
311 // Depending on execution timings, posting a delayed task can result
312 // in the task getting executed the at the earliest possible
313 // opportunity or only after returning once from the message loop and
314 // then a second call into the message loop. In order to make behavior
315 // more deterministic, we change the default delay to 2ms. This should
316 // always require us to wait for the second call into the message loop.
318 // N.B. The correct fix for this and similar timing problems is to
319 // abstract time for the purpose of unittests. Unfortunately, we have
320 // a lot of third-party components that directly call the various
321 // time functions, so this change would be rather invasive.
322 base::MessageLoop::current()->PostDelayedTask(
323 FROM_HERE,
324 base::Bind(base::IgnoreResult(&TestConnectJob::DoConnect),
325 weak_factory_.GetWeakPtr(),
326 true /* successful */,
327 true /* async */,
328 false /* recoverable */),
329 base::TimeDelta::FromMilliseconds(kPendingConnectDelay));
330 return ERR_IO_PENDING;
331 case kMockPendingFailingJob:
332 set_load_state(LOAD_STATE_CONNECTING);
333 base::MessageLoop::current()->PostDelayedTask(
334 FROM_HERE,
335 base::Bind(base::IgnoreResult(&TestConnectJob::DoConnect),
336 weak_factory_.GetWeakPtr(),
337 false /* error */,
338 true /* async */,
339 false /* recoverable */),
340 base::TimeDelta::FromMilliseconds(2));
341 return ERR_IO_PENDING;
342 case kMockWaitingJob:
343 set_load_state(LOAD_STATE_CONNECTING);
344 client_socket_factory_->WaitForSignal(this);
345 waiting_success_ = true;
346 return ERR_IO_PENDING;
347 case kMockRecoverableJob:
348 return DoConnect(false /* error */, false /* sync */,
349 true /* recoverable */);
350 case kMockPendingRecoverableJob:
351 set_load_state(LOAD_STATE_CONNECTING);
352 base::MessageLoop::current()->PostDelayedTask(
353 FROM_HERE,
354 base::Bind(base::IgnoreResult(&TestConnectJob::DoConnect),
355 weak_factory_.GetWeakPtr(),
356 false /* error */,
357 true /* async */,
358 true /* recoverable */),
359 base::TimeDelta::FromMilliseconds(2));
360 return ERR_IO_PENDING;
361 case kMockAdditionalErrorStateJob:
362 store_additional_error_state_ = true;
363 return DoConnect(false /* error */, false /* sync */,
364 false /* recoverable */);
365 case kMockPendingAdditionalErrorStateJob:
366 set_load_state(LOAD_STATE_CONNECTING);
367 store_additional_error_state_ = true;
368 base::MessageLoop::current()->PostDelayedTask(
369 FROM_HERE,
370 base::Bind(base::IgnoreResult(&TestConnectJob::DoConnect),
371 weak_factory_.GetWeakPtr(),
372 false /* error */,
373 true /* async */,
374 false /* recoverable */),
375 base::TimeDelta::FromMilliseconds(2));
376 return ERR_IO_PENDING;
377 case kMockUnreadDataJob: {
378 int ret = DoConnect(true /* successful */, false /* sync */,
379 false /* recoverable */);
380 static_cast<MockClientSocket*>(socket())->set_has_unread_data(true);
381 return ret;
383 default:
384 NOTREACHED();
385 SetSocket(scoped_ptr<StreamSocket>());
386 return ERR_FAILED;
390 int DoConnect(bool succeed, bool was_async, bool recoverable) {
391 int result = OK;
392 if (succeed) {
393 socket()->Connect(CompletionCallback());
394 } else if (recoverable) {
395 result = ERR_PROXY_AUTH_REQUESTED;
396 } else {
397 result = ERR_CONNECTION_FAILED;
398 SetSocket(scoped_ptr<StreamSocket>());
401 if (was_async)
402 NotifyDelegateOfCompletion(result);
403 return result;
406 bool waiting_success_;
407 const JobType job_type_;
408 MockClientSocketFactory* const client_socket_factory_;
409 LoadState load_state_;
410 bool store_additional_error_state_;
412 base::WeakPtrFactory<TestConnectJob> weak_factory_;
414 DISALLOW_COPY_AND_ASSIGN(TestConnectJob);
417 class TestConnectJobFactory
418 : public TestClientSocketPoolBase::ConnectJobFactory {
419 public:
420 TestConnectJobFactory(MockClientSocketFactory* client_socket_factory,
421 NetLog* net_log)
422 : job_type_(TestConnectJob::kMockJob),
423 job_types_(NULL),
424 client_socket_factory_(client_socket_factory),
425 net_log_(net_log) {
428 ~TestConnectJobFactory() override {}
430 void set_job_type(TestConnectJob::JobType job_type) { job_type_ = job_type; }
432 void set_job_types(std::list<TestConnectJob::JobType>* job_types) {
433 job_types_ = job_types;
434 CHECK(!job_types_->empty());
437 void set_timeout_duration(base::TimeDelta timeout_duration) {
438 timeout_duration_ = timeout_duration;
441 // ConnectJobFactory implementation.
443 scoped_ptr<ConnectJob> NewConnectJob(
444 const std::string& group_name,
445 const TestClientSocketPoolBase::Request& request,
446 ConnectJob::Delegate* delegate) const override {
447 EXPECT_TRUE(!job_types_ || !job_types_->empty());
448 TestConnectJob::JobType job_type = job_type_;
449 if (job_types_ && !job_types_->empty()) {
450 job_type = job_types_->front();
451 job_types_->pop_front();
453 return scoped_ptr<ConnectJob>(new TestConnectJob(job_type,
454 group_name,
455 request,
456 timeout_duration_,
457 delegate,
458 client_socket_factory_,
459 net_log_));
462 base::TimeDelta ConnectionTimeout() const override {
463 return timeout_duration_;
466 private:
467 TestConnectJob::JobType job_type_;
468 std::list<TestConnectJob::JobType>* job_types_;
469 base::TimeDelta timeout_duration_;
470 MockClientSocketFactory* const client_socket_factory_;
471 NetLog* net_log_;
473 DISALLOW_COPY_AND_ASSIGN(TestConnectJobFactory);
476 class TestClientSocketPool : public ClientSocketPool {
477 public:
478 typedef TestSocketParams SocketParams;
480 TestClientSocketPool(
481 int max_sockets,
482 int max_sockets_per_group,
483 base::TimeDelta unused_idle_socket_timeout,
484 base::TimeDelta used_idle_socket_timeout,
485 TestClientSocketPoolBase::ConnectJobFactory* connect_job_factory)
486 : base_(NULL,
487 max_sockets,
488 max_sockets_per_group,
489 unused_idle_socket_timeout,
490 used_idle_socket_timeout,
491 connect_job_factory) {}
493 ~TestClientSocketPool() override {}
495 int RequestSocket(const std::string& group_name,
496 const void* params,
497 RequestPriority priority,
498 ClientSocketHandle* handle,
499 const CompletionCallback& callback,
500 const BoundNetLog& net_log) override {
501 const scoped_refptr<TestSocketParams>* casted_socket_params =
502 static_cast<const scoped_refptr<TestSocketParams>*>(params);
503 return base_.RequestSocket(group_name, *casted_socket_params, priority,
504 handle, callback, net_log);
507 void RequestSockets(const std::string& group_name,
508 const void* params,
509 int num_sockets,
510 const BoundNetLog& net_log) override {
511 const scoped_refptr<TestSocketParams>* casted_params =
512 static_cast<const scoped_refptr<TestSocketParams>*>(params);
514 base_.RequestSockets(group_name, *casted_params, num_sockets, net_log);
517 void CancelRequest(const std::string& group_name,
518 ClientSocketHandle* handle) override {
519 base_.CancelRequest(group_name, handle);
522 void ReleaseSocket(const std::string& group_name,
523 scoped_ptr<StreamSocket> socket,
524 int id) override {
525 base_.ReleaseSocket(group_name, socket.Pass(), id);
528 void FlushWithError(int error) override { base_.FlushWithError(error); }
530 bool IsStalled() const override { return base_.IsStalled(); }
532 void CloseIdleSockets() override { base_.CloseIdleSockets(); }
534 int IdleSocketCount() const override { return base_.idle_socket_count(); }
536 int IdleSocketCountInGroup(const std::string& group_name) const override {
537 return base_.IdleSocketCountInGroup(group_name);
540 LoadState GetLoadState(const std::string& group_name,
541 const ClientSocketHandle* handle) const override {
542 return base_.GetLoadState(group_name, handle);
545 void AddHigherLayeredPool(HigherLayeredPool* higher_pool) override {
546 base_.AddHigherLayeredPool(higher_pool);
549 void RemoveHigherLayeredPool(HigherLayeredPool* higher_pool) override {
550 base_.RemoveHigherLayeredPool(higher_pool);
553 base::DictionaryValue* GetInfoAsValue(
554 const std::string& name,
555 const std::string& type,
556 bool include_nested_pools) const override {
557 return base_.GetInfoAsValue(name, type);
560 base::TimeDelta ConnectionTimeout() const override {
561 return base_.ConnectionTimeout();
564 const TestClientSocketPoolBase* base() const { return &base_; }
566 int NumUnassignedConnectJobsInGroup(const std::string& group_name) const {
567 return base_.NumUnassignedConnectJobsInGroup(group_name);
570 int NumConnectJobsInGroup(const std::string& group_name) const {
571 return base_.NumConnectJobsInGroup(group_name);
574 int NumActiveSocketsInGroup(const std::string& group_name) const {
575 return base_.NumActiveSocketsInGroup(group_name);
578 bool HasGroup(const std::string& group_name) const {
579 return base_.HasGroup(group_name);
582 void CleanupTimedOutIdleSockets() { base_.CleanupIdleSockets(false); }
584 void EnableConnectBackupJobs() { base_.EnableConnectBackupJobs(); }
586 bool CloseOneIdleConnectionInHigherLayeredPool() {
587 return base_.CloseOneIdleConnectionInHigherLayeredPool();
590 private:
591 TestClientSocketPoolBase base_;
593 DISALLOW_COPY_AND_ASSIGN(TestClientSocketPool);
596 } // namespace
598 namespace {
600 void MockClientSocketFactory::SignalJobs() {
601 for (std::vector<TestConnectJob*>::iterator it = waiting_jobs_.begin();
602 it != waiting_jobs_.end(); ++it) {
603 (*it)->Signal();
605 waiting_jobs_.clear();
608 void MockClientSocketFactory::SignalJob(size_t job) {
609 ASSERT_LT(job, waiting_jobs_.size());
610 waiting_jobs_[job]->Signal();
611 waiting_jobs_.erase(waiting_jobs_.begin() + job);
614 void MockClientSocketFactory::SetJobLoadState(size_t job,
615 LoadState load_state) {
616 ASSERT_LT(job, waiting_jobs_.size());
617 waiting_jobs_[job]->set_load_state(load_state);
620 class TestConnectJobDelegate : public ConnectJob::Delegate {
621 public:
622 TestConnectJobDelegate()
623 : have_result_(false), waiting_for_result_(false), result_(OK) {}
624 ~TestConnectJobDelegate() override {}
626 void OnConnectJobComplete(int result, ConnectJob* job) override {
627 result_ = result;
628 scoped_ptr<ConnectJob> owned_job(job);
629 scoped_ptr<StreamSocket> socket = owned_job->PassSocket();
630 // socket.get() should be NULL iff result != OK
631 EXPECT_EQ(socket == NULL, result != OK);
632 have_result_ = true;
633 if (waiting_for_result_)
634 base::MessageLoop::current()->Quit();
637 int WaitForResult() {
638 DCHECK(!waiting_for_result_);
639 while (!have_result_) {
640 waiting_for_result_ = true;
641 base::MessageLoop::current()->Run();
642 waiting_for_result_ = false;
644 have_result_ = false; // auto-reset for next callback
645 return result_;
648 private:
649 bool have_result_;
650 bool waiting_for_result_;
651 int result_;
654 class ClientSocketPoolBaseTest : public testing::Test {
655 protected:
656 ClientSocketPoolBaseTest()
657 : params_(new TestSocketParams(false /* ignore_limits */)) {
658 connect_backup_jobs_enabled_ =
659 internal::ClientSocketPoolBaseHelper::connect_backup_jobs_enabled();
660 internal::ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(true);
661 cleanup_timer_enabled_ =
662 internal::ClientSocketPoolBaseHelper::cleanup_timer_enabled();
665 ~ClientSocketPoolBaseTest() override {
666 internal::ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(
667 connect_backup_jobs_enabled_);
668 internal::ClientSocketPoolBaseHelper::set_cleanup_timer_enabled(
669 cleanup_timer_enabled_);
672 void CreatePool(int max_sockets, int max_sockets_per_group) {
673 CreatePoolWithIdleTimeouts(
674 max_sockets,
675 max_sockets_per_group,
676 ClientSocketPool::unused_idle_socket_timeout(),
677 ClientSocketPool::used_idle_socket_timeout());
680 void CreatePoolWithIdleTimeouts(
681 int max_sockets, int max_sockets_per_group,
682 base::TimeDelta unused_idle_socket_timeout,
683 base::TimeDelta used_idle_socket_timeout) {
684 DCHECK(!pool_.get());
685 connect_job_factory_ = new TestConnectJobFactory(&client_socket_factory_,
686 &net_log_);
687 pool_.reset(new TestClientSocketPool(max_sockets,
688 max_sockets_per_group,
689 unused_idle_socket_timeout,
690 used_idle_socket_timeout,
691 connect_job_factory_));
694 int StartRequestWithParams(
695 const std::string& group_name,
696 RequestPriority priority,
697 const scoped_refptr<TestSocketParams>& params) {
698 return test_base_.StartRequestUsingPool(
699 pool_.get(), group_name, priority, params);
702 int StartRequest(const std::string& group_name, RequestPriority priority) {
703 return StartRequestWithParams(group_name, priority, params_);
706 int GetOrderOfRequest(size_t index) const {
707 return test_base_.GetOrderOfRequest(index);
710 bool ReleaseOneConnection(ClientSocketPoolTest::KeepAlive keep_alive) {
711 return test_base_.ReleaseOneConnection(keep_alive);
714 void ReleaseAllConnections(ClientSocketPoolTest::KeepAlive keep_alive) {
715 test_base_.ReleaseAllConnections(keep_alive);
718 TestSocketRequest* request(int i) { return test_base_.request(i); }
719 size_t requests_size() const { return test_base_.requests_size(); }
720 ScopedVector<TestSocketRequest>* requests() { return test_base_.requests(); }
721 size_t completion_count() const { return test_base_.completion_count(); }
723 TestNetLog net_log_;
724 bool connect_backup_jobs_enabled_;
725 bool cleanup_timer_enabled_;
726 MockClientSocketFactory client_socket_factory_;
727 TestConnectJobFactory* connect_job_factory_;
728 scoped_refptr<TestSocketParams> params_;
729 scoped_ptr<TestClientSocketPool> pool_;
730 ClientSocketPoolTest test_base_;
733 // Even though a timeout is specified, it doesn't time out on a synchronous
734 // completion.
735 TEST_F(ClientSocketPoolBaseTest, ConnectJob_NoTimeoutOnSynchronousCompletion) {
736 TestConnectJobDelegate delegate;
737 ClientSocketHandle ignored;
738 TestClientSocketPoolBase::Request request(
739 &ignored, CompletionCallback(), DEFAULT_PRIORITY,
740 internal::ClientSocketPoolBaseHelper::NORMAL,
741 false, params_, BoundNetLog());
742 scoped_ptr<TestConnectJob> job(
743 new TestConnectJob(TestConnectJob::kMockJob,
744 "a",
745 request,
746 base::TimeDelta::FromMicroseconds(1),
747 &delegate,
748 &client_socket_factory_,
749 NULL));
750 EXPECT_EQ(OK, job->Connect());
753 TEST_F(ClientSocketPoolBaseTest, ConnectJob_TimedOut) {
754 TestConnectJobDelegate delegate;
755 ClientSocketHandle ignored;
756 TestNetLog log;
758 TestClientSocketPoolBase::Request request(
759 &ignored, CompletionCallback(), DEFAULT_PRIORITY,
760 internal::ClientSocketPoolBaseHelper::NORMAL,
761 false, params_, BoundNetLog());
762 // Deleted by TestConnectJobDelegate.
763 TestConnectJob* job =
764 new TestConnectJob(TestConnectJob::kMockPendingJob,
765 "a",
766 request,
767 base::TimeDelta::FromMicroseconds(1),
768 &delegate,
769 &client_socket_factory_,
770 &log);
771 ASSERT_EQ(ERR_IO_PENDING, job->Connect());
772 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(1));
773 EXPECT_EQ(ERR_TIMED_OUT, delegate.WaitForResult());
775 TestNetLog::CapturedEntryList entries;
776 log.GetEntries(&entries);
778 EXPECT_EQ(6u, entries.size());
779 EXPECT_TRUE(LogContainsBeginEvent(
780 entries, 0, NetLog::TYPE_SOCKET_POOL_CONNECT_JOB));
781 EXPECT_TRUE(LogContainsBeginEvent(
782 entries, 1, NetLog::TYPE_SOCKET_POOL_CONNECT_JOB_CONNECT));
783 EXPECT_TRUE(LogContainsEvent(
784 entries, 2, NetLog::TYPE_CONNECT_JOB_SET_SOCKET,
785 NetLog::PHASE_NONE));
786 EXPECT_TRUE(LogContainsEvent(
787 entries, 3, NetLog::TYPE_SOCKET_POOL_CONNECT_JOB_TIMED_OUT,
788 NetLog::PHASE_NONE));
789 EXPECT_TRUE(LogContainsEndEvent(
790 entries, 4, NetLog::TYPE_SOCKET_POOL_CONNECT_JOB_CONNECT));
791 EXPECT_TRUE(LogContainsEndEvent(
792 entries, 5, NetLog::TYPE_SOCKET_POOL_CONNECT_JOB));
795 TEST_F(ClientSocketPoolBaseTest, BasicSynchronous) {
796 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
798 TestCompletionCallback callback;
799 ClientSocketHandle handle;
800 BoundTestNetLog log;
801 TestLoadTimingInfoNotConnected(handle);
803 EXPECT_EQ(OK,
804 handle.Init("a",
805 params_,
806 DEFAULT_PRIORITY,
807 callback.callback(),
808 pool_.get(),
809 log.bound()));
810 EXPECT_TRUE(handle.is_initialized());
811 EXPECT_TRUE(handle.socket());
812 TestLoadTimingInfoConnectedNotReused(handle);
814 handle.Reset();
815 TestLoadTimingInfoNotConnected(handle);
817 TestNetLog::CapturedEntryList entries;
818 log.GetEntries(&entries);
820 EXPECT_EQ(4u, entries.size());
821 EXPECT_TRUE(LogContainsBeginEvent(
822 entries, 0, NetLog::TYPE_SOCKET_POOL));
823 EXPECT_TRUE(LogContainsEvent(
824 entries, 1, NetLog::TYPE_SOCKET_POOL_BOUND_TO_CONNECT_JOB,
825 NetLog::PHASE_NONE));
826 EXPECT_TRUE(LogContainsEvent(
827 entries, 2, NetLog::TYPE_SOCKET_POOL_BOUND_TO_SOCKET,
828 NetLog::PHASE_NONE));
829 EXPECT_TRUE(LogContainsEndEvent(
830 entries, 3, NetLog::TYPE_SOCKET_POOL));
833 TEST_F(ClientSocketPoolBaseTest, InitConnectionFailure) {
834 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
836 connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob);
837 BoundTestNetLog log;
839 ClientSocketHandle handle;
840 TestCompletionCallback callback;
841 // Set the additional error state members to ensure that they get cleared.
842 handle.set_is_ssl_error(true);
843 HttpResponseInfo info;
844 info.headers = new HttpResponseHeaders(std::string());
845 handle.set_ssl_error_response_info(info);
846 EXPECT_EQ(ERR_CONNECTION_FAILED,
847 handle.Init("a",
848 params_,
849 DEFAULT_PRIORITY,
850 callback.callback(),
851 pool_.get(),
852 log.bound()));
853 EXPECT_FALSE(handle.socket());
854 EXPECT_FALSE(handle.is_ssl_error());
855 EXPECT_TRUE(handle.ssl_error_response_info().headers.get() == NULL);
856 TestLoadTimingInfoNotConnected(handle);
858 TestNetLog::CapturedEntryList entries;
859 log.GetEntries(&entries);
861 EXPECT_EQ(3u, entries.size());
862 EXPECT_TRUE(LogContainsBeginEvent(
863 entries, 0, NetLog::TYPE_SOCKET_POOL));
864 EXPECT_TRUE(LogContainsEvent(
865 entries, 1, NetLog::TYPE_SOCKET_POOL_BOUND_TO_CONNECT_JOB,
866 NetLog::PHASE_NONE));
867 EXPECT_TRUE(LogContainsEndEvent(
868 entries, 2, NetLog::TYPE_SOCKET_POOL));
871 TEST_F(ClientSocketPoolBaseTest, TotalLimit) {
872 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
874 // TODO(eroman): Check that the NetLog contains this event.
876 EXPECT_EQ(OK, StartRequest("a", DEFAULT_PRIORITY));
877 EXPECT_EQ(OK, StartRequest("b", DEFAULT_PRIORITY));
878 EXPECT_EQ(OK, StartRequest("c", DEFAULT_PRIORITY));
879 EXPECT_EQ(OK, StartRequest("d", DEFAULT_PRIORITY));
881 EXPECT_EQ(static_cast<int>(requests_size()),
882 client_socket_factory_.allocation_count());
883 EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count());
885 EXPECT_EQ(ERR_IO_PENDING, StartRequest("e", DEFAULT_PRIORITY));
886 EXPECT_EQ(ERR_IO_PENDING, StartRequest("f", DEFAULT_PRIORITY));
887 EXPECT_EQ(ERR_IO_PENDING, StartRequest("g", DEFAULT_PRIORITY));
889 ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE);
891 EXPECT_EQ(static_cast<int>(requests_size()),
892 client_socket_factory_.allocation_count());
893 EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count());
895 EXPECT_EQ(1, GetOrderOfRequest(1));
896 EXPECT_EQ(2, GetOrderOfRequest(2));
897 EXPECT_EQ(3, GetOrderOfRequest(3));
898 EXPECT_EQ(4, GetOrderOfRequest(4));
899 EXPECT_EQ(5, GetOrderOfRequest(5));
900 EXPECT_EQ(6, GetOrderOfRequest(6));
901 EXPECT_EQ(7, GetOrderOfRequest(7));
903 // Make sure we test order of all requests made.
904 EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(8));
907 TEST_F(ClientSocketPoolBaseTest, TotalLimitReachedNewGroup) {
908 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
910 // TODO(eroman): Check that the NetLog contains this event.
912 // Reach all limits: max total sockets, and max sockets per group.
913 EXPECT_EQ(OK, StartRequest("a", DEFAULT_PRIORITY));
914 EXPECT_EQ(OK, StartRequest("a", DEFAULT_PRIORITY));
915 EXPECT_EQ(OK, StartRequest("b", DEFAULT_PRIORITY));
916 EXPECT_EQ(OK, StartRequest("b", DEFAULT_PRIORITY));
918 EXPECT_EQ(static_cast<int>(requests_size()),
919 client_socket_factory_.allocation_count());
920 EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count());
922 // Now create a new group and verify that we don't starve it.
923 EXPECT_EQ(ERR_IO_PENDING, StartRequest("c", DEFAULT_PRIORITY));
925 ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE);
927 EXPECT_EQ(static_cast<int>(requests_size()),
928 client_socket_factory_.allocation_count());
929 EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count());
931 EXPECT_EQ(1, GetOrderOfRequest(1));
932 EXPECT_EQ(2, GetOrderOfRequest(2));
933 EXPECT_EQ(3, GetOrderOfRequest(3));
934 EXPECT_EQ(4, GetOrderOfRequest(4));
935 EXPECT_EQ(5, GetOrderOfRequest(5));
937 // Make sure we test order of all requests made.
938 EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(6));
941 TEST_F(ClientSocketPoolBaseTest, TotalLimitRespectsPriority) {
942 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
944 EXPECT_EQ(OK, StartRequest("b", LOWEST));
945 EXPECT_EQ(OK, StartRequest("a", MEDIUM));
946 EXPECT_EQ(OK, StartRequest("b", HIGHEST));
947 EXPECT_EQ(OK, StartRequest("a", LOWEST));
949 EXPECT_EQ(static_cast<int>(requests_size()),
950 client_socket_factory_.allocation_count());
952 EXPECT_EQ(ERR_IO_PENDING, StartRequest("c", LOWEST));
953 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", MEDIUM));
954 EXPECT_EQ(ERR_IO_PENDING, StartRequest("b", HIGHEST));
956 ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE);
958 EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count());
960 // First 4 requests don't have to wait, and finish in order.
961 EXPECT_EQ(1, GetOrderOfRequest(1));
962 EXPECT_EQ(2, GetOrderOfRequest(2));
963 EXPECT_EQ(3, GetOrderOfRequest(3));
964 EXPECT_EQ(4, GetOrderOfRequest(4));
966 // Request ("b", HIGHEST) has the highest priority, then ("a", MEDIUM),
967 // and then ("c", LOWEST).
968 EXPECT_EQ(7, GetOrderOfRequest(5));
969 EXPECT_EQ(6, GetOrderOfRequest(6));
970 EXPECT_EQ(5, GetOrderOfRequest(7));
972 // Make sure we test order of all requests made.
973 EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(9));
976 TEST_F(ClientSocketPoolBaseTest, TotalLimitRespectsGroupLimit) {
977 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
979 EXPECT_EQ(OK, StartRequest("a", LOWEST));
980 EXPECT_EQ(OK, StartRequest("a", LOW));
981 EXPECT_EQ(OK, StartRequest("b", HIGHEST));
982 EXPECT_EQ(OK, StartRequest("b", MEDIUM));
984 EXPECT_EQ(static_cast<int>(requests_size()),
985 client_socket_factory_.allocation_count());
987 EXPECT_EQ(ERR_IO_PENDING, StartRequest("c", MEDIUM));
988 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOW));
989 EXPECT_EQ(ERR_IO_PENDING, StartRequest("b", HIGHEST));
991 ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE);
993 EXPECT_EQ(static_cast<int>(requests_size()),
994 client_socket_factory_.allocation_count());
995 EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count());
997 // First 4 requests don't have to wait, and finish in order.
998 EXPECT_EQ(1, GetOrderOfRequest(1));
999 EXPECT_EQ(2, GetOrderOfRequest(2));
1000 EXPECT_EQ(3, GetOrderOfRequest(3));
1001 EXPECT_EQ(4, GetOrderOfRequest(4));
1003 // Request ("b", 7) has the highest priority, but we can't make new socket for
1004 // group "b", because it has reached the per-group limit. Then we make
1005 // socket for ("c", 6), because it has higher priority than ("a", 4),
1006 // and we still can't make a socket for group "b".
1007 EXPECT_EQ(5, GetOrderOfRequest(5));
1008 EXPECT_EQ(6, GetOrderOfRequest(6));
1009 EXPECT_EQ(7, GetOrderOfRequest(7));
1011 // Make sure we test order of all requests made.
1012 EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(8));
1015 // Make sure that we count connecting sockets against the total limit.
1016 TEST_F(ClientSocketPoolBaseTest, TotalLimitCountsConnectingSockets) {
1017 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
1019 EXPECT_EQ(OK, StartRequest("a", DEFAULT_PRIORITY));
1020 EXPECT_EQ(OK, StartRequest("b", DEFAULT_PRIORITY));
1021 EXPECT_EQ(OK, StartRequest("c", DEFAULT_PRIORITY));
1023 // Create one asynchronous request.
1024 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
1025 EXPECT_EQ(ERR_IO_PENDING, StartRequest("d", DEFAULT_PRIORITY));
1027 // We post all of our delayed tasks with a 2ms delay. I.e. they don't
1028 // actually become pending until 2ms after they have been created. In order
1029 // to flush all tasks, we need to wait so that we know there are no
1030 // soon-to-be-pending tasks waiting.
1031 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10));
1032 base::MessageLoop::current()->RunUntilIdle();
1034 // The next synchronous request should wait for its turn.
1035 connect_job_factory_->set_job_type(TestConnectJob::kMockJob);
1036 EXPECT_EQ(ERR_IO_PENDING, StartRequest("e", DEFAULT_PRIORITY));
1038 ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE);
1040 EXPECT_EQ(static_cast<int>(requests_size()),
1041 client_socket_factory_.allocation_count());
1043 EXPECT_EQ(1, GetOrderOfRequest(1));
1044 EXPECT_EQ(2, GetOrderOfRequest(2));
1045 EXPECT_EQ(3, GetOrderOfRequest(3));
1046 EXPECT_EQ(4, GetOrderOfRequest(4));
1047 EXPECT_EQ(5, GetOrderOfRequest(5));
1049 // Make sure we test order of all requests made.
1050 EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(6));
1053 TEST_F(ClientSocketPoolBaseTest, CorrectlyCountStalledGroups) {
1054 CreatePool(kDefaultMaxSockets, kDefaultMaxSockets);
1055 connect_job_factory_->set_job_type(TestConnectJob::kMockJob);
1057 EXPECT_EQ(OK, StartRequest("a", DEFAULT_PRIORITY));
1058 EXPECT_EQ(OK, StartRequest("a", DEFAULT_PRIORITY));
1059 EXPECT_EQ(OK, StartRequest("a", DEFAULT_PRIORITY));
1060 EXPECT_EQ(OK, StartRequest("a", DEFAULT_PRIORITY));
1062 connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob);
1064 EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count());
1066 EXPECT_EQ(ERR_IO_PENDING, StartRequest("b", DEFAULT_PRIORITY));
1067 EXPECT_EQ(ERR_IO_PENDING, StartRequest("c", DEFAULT_PRIORITY));
1069 EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count());
1071 EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE));
1072 EXPECT_EQ(kDefaultMaxSockets + 1, client_socket_factory_.allocation_count());
1073 EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE));
1074 EXPECT_EQ(kDefaultMaxSockets + 2, client_socket_factory_.allocation_count());
1075 EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE));
1076 EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE));
1077 EXPECT_EQ(kDefaultMaxSockets + 2, client_socket_factory_.allocation_count());
1080 TEST_F(ClientSocketPoolBaseTest, StallAndThenCancelAndTriggerAvailableSocket) {
1081 CreatePool(kDefaultMaxSockets, kDefaultMaxSockets);
1082 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
1084 ClientSocketHandle handle;
1085 TestCompletionCallback callback;
1086 EXPECT_EQ(ERR_IO_PENDING,
1087 handle.Init("a",
1088 params_,
1089 DEFAULT_PRIORITY,
1090 callback.callback(),
1091 pool_.get(),
1092 BoundNetLog()));
1094 ClientSocketHandle handles[4];
1095 for (size_t i = 0; i < arraysize(handles); ++i) {
1096 TestCompletionCallback callback;
1097 EXPECT_EQ(ERR_IO_PENDING,
1098 handles[i].Init("b",
1099 params_,
1100 DEFAULT_PRIORITY,
1101 callback.callback(),
1102 pool_.get(),
1103 BoundNetLog()));
1106 // One will be stalled, cancel all the handles now.
1107 // This should hit the OnAvailableSocketSlot() code where we previously had
1108 // stalled groups, but no longer have any.
1109 for (size_t i = 0; i < arraysize(handles); ++i)
1110 handles[i].Reset();
1113 TEST_F(ClientSocketPoolBaseTest, CancelStalledSocketAtSocketLimit) {
1114 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
1115 connect_job_factory_->set_job_type(TestConnectJob::kMockJob);
1118 ClientSocketHandle handles[kDefaultMaxSockets];
1119 TestCompletionCallback callbacks[kDefaultMaxSockets];
1120 for (int i = 0; i < kDefaultMaxSockets; ++i) {
1121 EXPECT_EQ(OK, handles[i].Init(base::IntToString(i),
1122 params_,
1123 DEFAULT_PRIORITY,
1124 callbacks[i].callback(),
1125 pool_.get(),
1126 BoundNetLog()));
1129 // Force a stalled group.
1130 ClientSocketHandle stalled_handle;
1131 TestCompletionCallback callback;
1132 EXPECT_EQ(ERR_IO_PENDING, stalled_handle.Init("foo",
1133 params_,
1134 DEFAULT_PRIORITY,
1135 callback.callback(),
1136 pool_.get(),
1137 BoundNetLog()));
1139 // Cancel the stalled request.
1140 stalled_handle.Reset();
1142 EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count());
1143 EXPECT_EQ(0, pool_->IdleSocketCount());
1145 // Dropping out of scope will close all handles and return them to idle.
1148 EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count());
1149 EXPECT_EQ(kDefaultMaxSockets, pool_->IdleSocketCount());
1152 TEST_F(ClientSocketPoolBaseTest, CancelPendingSocketAtSocketLimit) {
1153 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
1154 connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob);
1157 ClientSocketHandle handles[kDefaultMaxSockets];
1158 for (int i = 0; i < kDefaultMaxSockets; ++i) {
1159 TestCompletionCallback callback;
1160 EXPECT_EQ(ERR_IO_PENDING, handles[i].Init(base::IntToString(i),
1161 params_,
1162 DEFAULT_PRIORITY,
1163 callback.callback(),
1164 pool_.get(),
1165 BoundNetLog()));
1168 // Force a stalled group.
1169 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
1170 ClientSocketHandle stalled_handle;
1171 TestCompletionCallback callback;
1172 EXPECT_EQ(ERR_IO_PENDING, stalled_handle.Init("foo",
1173 params_,
1174 DEFAULT_PRIORITY,
1175 callback.callback(),
1176 pool_.get(),
1177 BoundNetLog()));
1179 // Since it is stalled, it should have no connect jobs.
1180 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("foo"));
1181 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("foo"));
1183 // Cancel the stalled request.
1184 handles[0].Reset();
1186 // Now we should have a connect job.
1187 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("foo"));
1188 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("foo"));
1190 // The stalled socket should connect.
1191 EXPECT_EQ(OK, callback.WaitForResult());
1193 EXPECT_EQ(kDefaultMaxSockets + 1,
1194 client_socket_factory_.allocation_count());
1195 EXPECT_EQ(0, pool_->IdleSocketCount());
1196 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("foo"));
1197 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("foo"));
1199 // Dropping out of scope will close all handles and return them to idle.
1202 EXPECT_EQ(1, pool_->IdleSocketCount());
1205 TEST_F(ClientSocketPoolBaseTest, WaitForStalledSocketAtSocketLimit) {
1206 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
1207 connect_job_factory_->set_job_type(TestConnectJob::kMockJob);
1209 ClientSocketHandle stalled_handle;
1210 TestCompletionCallback callback;
1212 EXPECT_FALSE(pool_->IsStalled());
1213 ClientSocketHandle handles[kDefaultMaxSockets];
1214 for (int i = 0; i < kDefaultMaxSockets; ++i) {
1215 TestCompletionCallback callback;
1216 EXPECT_EQ(OK, handles[i].Init(base::StringPrintf(
1217 "Take 2: %d", i),
1218 params_,
1219 DEFAULT_PRIORITY,
1220 callback.callback(),
1221 pool_.get(),
1222 BoundNetLog()));
1225 EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count());
1226 EXPECT_EQ(0, pool_->IdleSocketCount());
1227 EXPECT_FALSE(pool_->IsStalled());
1229 // Now we will hit the socket limit.
1230 EXPECT_EQ(ERR_IO_PENDING, stalled_handle.Init("foo",
1231 params_,
1232 DEFAULT_PRIORITY,
1233 callback.callback(),
1234 pool_.get(),
1235 BoundNetLog()));
1236 EXPECT_TRUE(pool_->IsStalled());
1238 // Dropping out of scope will close all handles and return them to idle.
1241 // But if we wait for it, the released idle sockets will be closed in
1242 // preference of the waiting request.
1243 EXPECT_EQ(OK, callback.WaitForResult());
1245 EXPECT_EQ(kDefaultMaxSockets + 1, client_socket_factory_.allocation_count());
1246 EXPECT_EQ(3, pool_->IdleSocketCount());
1249 // Regression test for http://crbug.com/40952.
1250 TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketAtSocketLimitDeleteGroup) {
1251 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
1252 pool_->EnableConnectBackupJobs();
1253 connect_job_factory_->set_job_type(TestConnectJob::kMockJob);
1255 for (int i = 0; i < kDefaultMaxSockets; ++i) {
1256 ClientSocketHandle handle;
1257 TestCompletionCallback callback;
1258 EXPECT_EQ(OK, handle.Init(base::IntToString(i),
1259 params_,
1260 DEFAULT_PRIORITY,
1261 callback.callback(),
1262 pool_.get(),
1263 BoundNetLog()));
1266 // Flush all the DoReleaseSocket tasks.
1267 base::MessageLoop::current()->RunUntilIdle();
1269 // Stall a group. Set a pending job so it'll trigger a backup job if we don't
1270 // reuse a socket.
1271 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
1272 ClientSocketHandle handle;
1273 TestCompletionCallback callback;
1275 // "0" is special here, since it should be the first entry in the sorted map,
1276 // which is the one which we would close an idle socket for. We shouldn't
1277 // close an idle socket though, since we should reuse the idle socket.
1278 EXPECT_EQ(OK, handle.Init("0",
1279 params_,
1280 DEFAULT_PRIORITY,
1281 callback.callback(),
1282 pool_.get(),
1283 BoundNetLog()));
1285 EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count());
1286 EXPECT_EQ(kDefaultMaxSockets - 1, pool_->IdleSocketCount());
1289 TEST_F(ClientSocketPoolBaseTest, PendingRequests) {
1290 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
1292 EXPECT_EQ(OK, StartRequest("a", DEFAULT_PRIORITY));
1293 EXPECT_EQ(OK, StartRequest("a", DEFAULT_PRIORITY));
1294 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", IDLE));
1295 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOWEST));
1296 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", MEDIUM));
1297 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", HIGHEST));
1298 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOW));
1299 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOWEST));
1301 ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE);
1303 EXPECT_EQ(kDefaultMaxSocketsPerGroup,
1304 client_socket_factory_.allocation_count());
1305 EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup,
1306 completion_count());
1308 EXPECT_EQ(1, GetOrderOfRequest(1));
1309 EXPECT_EQ(2, GetOrderOfRequest(2));
1310 EXPECT_EQ(8, GetOrderOfRequest(3));
1311 EXPECT_EQ(6, GetOrderOfRequest(4));
1312 EXPECT_EQ(4, GetOrderOfRequest(5));
1313 EXPECT_EQ(3, GetOrderOfRequest(6));
1314 EXPECT_EQ(5, GetOrderOfRequest(7));
1315 EXPECT_EQ(7, GetOrderOfRequest(8));
1317 // Make sure we test order of all requests made.
1318 EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(9));
1321 TEST_F(ClientSocketPoolBaseTest, PendingRequests_NoKeepAlive) {
1322 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
1324 EXPECT_EQ(OK, StartRequest("a", DEFAULT_PRIORITY));
1325 EXPECT_EQ(OK, StartRequest("a", DEFAULT_PRIORITY));
1326 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOWEST));
1327 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", MEDIUM));
1328 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", HIGHEST));
1329 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOW));
1330 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOWEST));
1332 ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE);
1334 for (size_t i = kDefaultMaxSocketsPerGroup; i < requests_size(); ++i)
1335 EXPECT_EQ(OK, request(i)->WaitForResult());
1337 EXPECT_EQ(static_cast<int>(requests_size()),
1338 client_socket_factory_.allocation_count());
1339 EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup,
1340 completion_count());
1343 // This test will start up a RequestSocket() and then immediately Cancel() it.
1344 // The pending connect job will be cancelled and should not call back into
1345 // ClientSocketPoolBase.
1346 TEST_F(ClientSocketPoolBaseTest, CancelRequestClearGroup) {
1347 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
1349 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
1350 ClientSocketHandle handle;
1351 TestCompletionCallback callback;
1352 EXPECT_EQ(ERR_IO_PENDING, handle.Init("a",
1353 params_,
1354 DEFAULT_PRIORITY,
1355 callback.callback(),
1356 pool_.get(),
1357 BoundNetLog()));
1358 handle.Reset();
1361 TEST_F(ClientSocketPoolBaseTest, ConnectCancelConnect) {
1362 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
1364 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
1365 ClientSocketHandle handle;
1366 TestCompletionCallback callback;
1368 EXPECT_EQ(ERR_IO_PENDING, handle.Init("a",
1369 params_,
1370 DEFAULT_PRIORITY,
1371 callback.callback(),
1372 pool_.get(),
1373 BoundNetLog()));
1375 handle.Reset();
1377 TestCompletionCallback callback2;
1378 EXPECT_EQ(ERR_IO_PENDING,
1379 handle.Init("a",
1380 params_,
1381 DEFAULT_PRIORITY,
1382 callback2.callback(),
1383 pool_.get(),
1384 BoundNetLog()));
1386 EXPECT_EQ(OK, callback2.WaitForResult());
1387 EXPECT_FALSE(callback.have_result());
1389 handle.Reset();
1392 TEST_F(ClientSocketPoolBaseTest, CancelRequest) {
1393 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
1395 EXPECT_EQ(OK, StartRequest("a", DEFAULT_PRIORITY));
1396 EXPECT_EQ(OK, StartRequest("a", DEFAULT_PRIORITY));
1397 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOWEST));
1398 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", MEDIUM));
1399 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", HIGHEST));
1400 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOW));
1401 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOWEST));
1403 // Cancel a request.
1404 size_t index_to_cancel = kDefaultMaxSocketsPerGroup + 2;
1405 EXPECT_FALSE((*requests())[index_to_cancel]->handle()->is_initialized());
1406 (*requests())[index_to_cancel]->handle()->Reset();
1408 ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE);
1410 EXPECT_EQ(kDefaultMaxSocketsPerGroup,
1411 client_socket_factory_.allocation_count());
1412 EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup - 1,
1413 completion_count());
1415 EXPECT_EQ(1, GetOrderOfRequest(1));
1416 EXPECT_EQ(2, GetOrderOfRequest(2));
1417 EXPECT_EQ(5, GetOrderOfRequest(3));
1418 EXPECT_EQ(3, GetOrderOfRequest(4));
1419 EXPECT_EQ(ClientSocketPoolTest::kRequestNotFound,
1420 GetOrderOfRequest(5)); // Canceled request.
1421 EXPECT_EQ(4, GetOrderOfRequest(6));
1422 EXPECT_EQ(6, GetOrderOfRequest(7));
1424 // Make sure we test order of all requests made.
1425 EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(8));
1428 class RequestSocketCallback : public TestCompletionCallbackBase {
1429 public:
1430 RequestSocketCallback(ClientSocketHandle* handle,
1431 TestClientSocketPool* pool,
1432 TestConnectJobFactory* test_connect_job_factory,
1433 TestConnectJob::JobType next_job_type)
1434 : handle_(handle),
1435 pool_(pool),
1436 within_callback_(false),
1437 test_connect_job_factory_(test_connect_job_factory),
1438 next_job_type_(next_job_type),
1439 callback_(base::Bind(&RequestSocketCallback::OnComplete,
1440 base::Unretained(this))) {
1443 ~RequestSocketCallback() override {}
1445 const CompletionCallback& callback() const { return callback_; }
1447 private:
1448 void OnComplete(int result) {
1449 SetResult(result);
1450 ASSERT_EQ(OK, result);
1452 if (!within_callback_) {
1453 test_connect_job_factory_->set_job_type(next_job_type_);
1455 // Don't allow reuse of the socket. Disconnect it and then release it and
1456 // run through the MessageLoop once to get it completely released.
1457 handle_->socket()->Disconnect();
1458 handle_->Reset();
1460 // TODO: Resolve conflicting intentions of stopping recursion with the
1461 // |!within_callback_| test (above) and the call to |RunUntilIdle()|
1462 // below. http://crbug.com/114130.
1463 base::MessageLoop::ScopedNestableTaskAllower allow(
1464 base::MessageLoop::current());
1465 base::MessageLoop::current()->RunUntilIdle();
1467 within_callback_ = true;
1468 TestCompletionCallback next_job_callback;
1469 scoped_refptr<TestSocketParams> params(
1470 new TestSocketParams(false /* ignore_limits */));
1471 int rv = handle_->Init("a",
1472 params,
1473 DEFAULT_PRIORITY,
1474 next_job_callback.callback(),
1475 pool_,
1476 BoundNetLog());
1477 switch (next_job_type_) {
1478 case TestConnectJob::kMockJob:
1479 EXPECT_EQ(OK, rv);
1480 break;
1481 case TestConnectJob::kMockPendingJob:
1482 EXPECT_EQ(ERR_IO_PENDING, rv);
1484 // For pending jobs, wait for new socket to be created. This makes
1485 // sure there are no more pending operations nor any unclosed sockets
1486 // when the test finishes.
1487 // We need to give it a little bit of time to run, so that all the
1488 // operations that happen on timers (e.g. cleanup of idle
1489 // connections) can execute.
1491 base::MessageLoop::ScopedNestableTaskAllower allow(
1492 base::MessageLoop::current());
1493 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10));
1494 EXPECT_EQ(OK, next_job_callback.WaitForResult());
1496 break;
1497 default:
1498 FAIL() << "Unexpected job type: " << next_job_type_;
1499 break;
1504 ClientSocketHandle* const handle_;
1505 TestClientSocketPool* const pool_;
1506 bool within_callback_;
1507 TestConnectJobFactory* const test_connect_job_factory_;
1508 TestConnectJob::JobType next_job_type_;
1509 CompletionCallback callback_;
1512 TEST_F(ClientSocketPoolBaseTest, RequestPendingJobTwice) {
1513 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
1515 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
1516 ClientSocketHandle handle;
1517 RequestSocketCallback callback(
1518 &handle, pool_.get(), connect_job_factory_,
1519 TestConnectJob::kMockPendingJob);
1520 int rv = handle.Init("a",
1521 params_,
1522 DEFAULT_PRIORITY,
1523 callback.callback(),
1524 pool_.get(),
1525 BoundNetLog());
1526 ASSERT_EQ(ERR_IO_PENDING, rv);
1528 EXPECT_EQ(OK, callback.WaitForResult());
1531 TEST_F(ClientSocketPoolBaseTest, RequestPendingJobThenSynchronous) {
1532 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
1534 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
1535 ClientSocketHandle handle;
1536 RequestSocketCallback callback(
1537 &handle, pool_.get(), connect_job_factory_, TestConnectJob::kMockJob);
1538 int rv = handle.Init("a",
1539 params_,
1540 DEFAULT_PRIORITY,
1541 callback.callback(),
1542 pool_.get(),
1543 BoundNetLog());
1544 ASSERT_EQ(ERR_IO_PENDING, rv);
1546 EXPECT_EQ(OK, callback.WaitForResult());
1549 // Make sure that pending requests get serviced after active requests get
1550 // cancelled.
1551 TEST_F(ClientSocketPoolBaseTest, CancelActiveRequestWithPendingRequests) {
1552 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
1554 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
1556 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", DEFAULT_PRIORITY));
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));
1564 // Now, kDefaultMaxSocketsPerGroup requests should be active.
1565 // Let's cancel them.
1566 for (int i = 0; i < kDefaultMaxSocketsPerGroup; ++i) {
1567 ASSERT_FALSE(request(i)->handle()->is_initialized());
1568 request(i)->handle()->Reset();
1571 // Let's wait for the rest to complete now.
1572 for (size_t i = kDefaultMaxSocketsPerGroup; i < requests_size(); ++i) {
1573 EXPECT_EQ(OK, request(i)->WaitForResult());
1574 request(i)->handle()->Reset();
1577 EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup,
1578 completion_count());
1581 // Make sure that pending requests get serviced after active requests fail.
1582 TEST_F(ClientSocketPoolBaseTest, FailingActiveRequestWithPendingRequests) {
1583 const size_t kMaxSockets = 5;
1584 CreatePool(kMaxSockets, kDefaultMaxSocketsPerGroup);
1586 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob);
1588 const size_t kNumberOfRequests = 2 * kDefaultMaxSocketsPerGroup + 1;
1589 ASSERT_LE(kNumberOfRequests, kMaxSockets); // Otherwise the test will hang.
1591 // Queue up all the requests
1592 for (size_t i = 0; i < kNumberOfRequests; ++i)
1593 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", DEFAULT_PRIORITY));
1595 for (size_t i = 0; i < kNumberOfRequests; ++i)
1596 EXPECT_EQ(ERR_CONNECTION_FAILED, request(i)->WaitForResult());
1599 TEST_F(ClientSocketPoolBaseTest, CancelActiveRequestThenRequestSocket) {
1600 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
1602 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
1604 ClientSocketHandle handle;
1605 TestCompletionCallback callback;
1606 int rv = handle.Init("a",
1607 params_,
1608 DEFAULT_PRIORITY,
1609 callback.callback(),
1610 pool_.get(),
1611 BoundNetLog());
1612 EXPECT_EQ(ERR_IO_PENDING, rv);
1614 // Cancel the active request.
1615 handle.Reset();
1617 rv = handle.Init("a",
1618 params_,
1619 DEFAULT_PRIORITY,
1620 callback.callback(),
1621 pool_.get(),
1622 BoundNetLog());
1623 EXPECT_EQ(ERR_IO_PENDING, rv);
1624 EXPECT_EQ(OK, callback.WaitForResult());
1626 EXPECT_FALSE(handle.is_reused());
1627 TestLoadTimingInfoConnectedNotReused(handle);
1628 EXPECT_EQ(2, client_socket_factory_.allocation_count());
1631 // Regression test for http://crbug.com/17985.
1632 TEST_F(ClientSocketPoolBaseTest, GroupWithPendingRequestsIsNotEmpty) {
1633 const int kMaxSockets = 3;
1634 const int kMaxSocketsPerGroup = 2;
1635 CreatePool(kMaxSockets, kMaxSocketsPerGroup);
1637 const RequestPriority kHighPriority = HIGHEST;
1639 EXPECT_EQ(OK, StartRequest("a", DEFAULT_PRIORITY));
1640 EXPECT_EQ(OK, StartRequest("a", DEFAULT_PRIORITY));
1642 // This is going to be a pending request in an otherwise empty group.
1643 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", DEFAULT_PRIORITY));
1645 // Reach the maximum socket limit.
1646 EXPECT_EQ(OK, StartRequest("b", DEFAULT_PRIORITY));
1648 // Create a stalled group with high priorities.
1649 EXPECT_EQ(ERR_IO_PENDING, StartRequest("c", kHighPriority));
1650 EXPECT_EQ(ERR_IO_PENDING, StartRequest("c", kHighPriority));
1652 // Release the first two sockets from "a". Because this is a keepalive,
1653 // the first release will unblock the pending request for "a". The
1654 // second release will unblock a request for "c", becaue it is the next
1655 // high priority socket.
1656 EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE));
1657 EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE));
1659 // Closing idle sockets should not get us into trouble, but in the bug
1660 // we were hitting a CHECK here.
1661 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
1662 pool_->CloseIdleSockets();
1664 // Run the released socket wakeups.
1665 base::MessageLoop::current()->RunUntilIdle();
1668 TEST_F(ClientSocketPoolBaseTest, BasicAsynchronous) {
1669 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
1671 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
1672 ClientSocketHandle handle;
1673 TestCompletionCallback callback;
1674 BoundTestNetLog log;
1675 int rv = handle.Init("a",
1676 params_,
1677 LOWEST,
1678 callback.callback(),
1679 pool_.get(),
1680 log.bound());
1681 EXPECT_EQ(ERR_IO_PENDING, rv);
1682 EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", &handle));
1683 TestLoadTimingInfoNotConnected(handle);
1685 EXPECT_EQ(OK, callback.WaitForResult());
1686 EXPECT_TRUE(handle.is_initialized());
1687 EXPECT_TRUE(handle.socket());
1688 TestLoadTimingInfoConnectedNotReused(handle);
1690 handle.Reset();
1691 TestLoadTimingInfoNotConnected(handle);
1693 TestNetLog::CapturedEntryList entries;
1694 log.GetEntries(&entries);
1696 EXPECT_EQ(4u, entries.size());
1697 EXPECT_TRUE(LogContainsBeginEvent(
1698 entries, 0, NetLog::TYPE_SOCKET_POOL));
1699 EXPECT_TRUE(LogContainsEvent(
1700 entries, 1, NetLog::TYPE_SOCKET_POOL_BOUND_TO_CONNECT_JOB,
1701 NetLog::PHASE_NONE));
1702 EXPECT_TRUE(LogContainsEvent(
1703 entries, 2, NetLog::TYPE_SOCKET_POOL_BOUND_TO_SOCKET,
1704 NetLog::PHASE_NONE));
1705 EXPECT_TRUE(LogContainsEndEvent(
1706 entries, 3, NetLog::TYPE_SOCKET_POOL));
1709 TEST_F(ClientSocketPoolBaseTest,
1710 InitConnectionAsynchronousFailure) {
1711 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
1713 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob);
1714 ClientSocketHandle handle;
1715 TestCompletionCallback callback;
1716 BoundTestNetLog log;
1717 // Set the additional error state members to ensure that they get cleared.
1718 handle.set_is_ssl_error(true);
1719 HttpResponseInfo info;
1720 info.headers = new HttpResponseHeaders(std::string());
1721 handle.set_ssl_error_response_info(info);
1722 EXPECT_EQ(ERR_IO_PENDING, handle.Init("a",
1723 params_,
1724 DEFAULT_PRIORITY,
1725 callback.callback(),
1726 pool_.get(),
1727 log.bound()));
1728 EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", &handle));
1729 EXPECT_EQ(ERR_CONNECTION_FAILED, callback.WaitForResult());
1730 EXPECT_FALSE(handle.is_ssl_error());
1731 EXPECT_TRUE(handle.ssl_error_response_info().headers.get() == NULL);
1733 TestNetLog::CapturedEntryList entries;
1734 log.GetEntries(&entries);
1736 EXPECT_EQ(3u, entries.size());
1737 EXPECT_TRUE(LogContainsBeginEvent(
1738 entries, 0, NetLog::TYPE_SOCKET_POOL));
1739 EXPECT_TRUE(LogContainsEvent(
1740 entries, 1, NetLog::TYPE_SOCKET_POOL_BOUND_TO_CONNECT_JOB,
1741 NetLog::PHASE_NONE));
1742 EXPECT_TRUE(LogContainsEndEvent(
1743 entries, 2, NetLog::TYPE_SOCKET_POOL));
1746 // Check that an async ConnectJob failure does not result in creation of a new
1747 // ConnectJob when there's another pending request also waiting on its own
1748 // ConnectJob. See http://crbug.com/463960.
1749 TEST_F(ClientSocketPoolBaseTest, AsyncFailureWithPendingRequestWithJob) {
1750 CreatePool(2, 2);
1751 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob);
1753 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", DEFAULT_PRIORITY));
1754 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", DEFAULT_PRIORITY));
1756 EXPECT_EQ(ERR_CONNECTION_FAILED, request(0)->WaitForResult());
1757 EXPECT_EQ(ERR_CONNECTION_FAILED, request(1)->WaitForResult());
1759 EXPECT_EQ(2, client_socket_factory_.allocation_count());
1762 TEST_F(ClientSocketPoolBaseTest, TwoRequestsCancelOne) {
1763 // TODO(eroman): Add back the log expectations! Removed them because the
1764 // ordering is difficult, and some may fire during destructor.
1765 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
1767 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
1768 ClientSocketHandle handle;
1769 TestCompletionCallback callback;
1770 ClientSocketHandle handle2;
1771 TestCompletionCallback callback2;
1773 EXPECT_EQ(ERR_IO_PENDING,
1774 handle.Init("a",
1775 params_,
1776 DEFAULT_PRIORITY,
1777 callback.callback(),
1778 pool_.get(),
1779 BoundNetLog()));
1780 BoundTestNetLog log2;
1781 EXPECT_EQ(ERR_IO_PENDING,
1782 handle2.Init("a",
1783 params_,
1784 DEFAULT_PRIORITY,
1785 callback2.callback(),
1786 pool_.get(),
1787 BoundNetLog()));
1789 handle.Reset();
1792 // At this point, request 2 is just waiting for the connect job to finish.
1794 EXPECT_EQ(OK, callback2.WaitForResult());
1795 handle2.Reset();
1797 // Now request 2 has actually finished.
1798 // TODO(eroman): Add back log expectations.
1801 TEST_F(ClientSocketPoolBaseTest, CancelRequestLimitsJobs) {
1802 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
1804 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
1806 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOWEST));
1807 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOW));
1808 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", MEDIUM));
1809 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", HIGHEST));
1811 EXPECT_EQ(kDefaultMaxSocketsPerGroup, pool_->NumConnectJobsInGroup("a"));
1812 (*requests())[2]->handle()->Reset();
1813 (*requests())[3]->handle()->Reset();
1814 EXPECT_EQ(kDefaultMaxSocketsPerGroup, pool_->NumConnectJobsInGroup("a"));
1816 (*requests())[1]->handle()->Reset();
1817 EXPECT_EQ(kDefaultMaxSocketsPerGroup, pool_->NumConnectJobsInGroup("a"));
1819 (*requests())[0]->handle()->Reset();
1820 EXPECT_EQ(kDefaultMaxSocketsPerGroup, pool_->NumConnectJobsInGroup("a"));
1823 // When requests and ConnectJobs are not coupled, the request will get serviced
1824 // by whatever comes first.
1825 TEST_F(ClientSocketPoolBaseTest, ReleaseSockets) {
1826 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
1828 // Start job 1 (async OK)
1829 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
1831 std::vector<TestSocketRequest*> request_order;
1832 size_t completion_count; // unused
1833 TestSocketRequest req1(&request_order, &completion_count);
1834 int rv = req1.handle()->Init("a",
1835 params_,
1836 DEFAULT_PRIORITY,
1837 req1.callback(), pool_.get(),
1838 BoundNetLog());
1839 EXPECT_EQ(ERR_IO_PENDING, rv);
1840 EXPECT_EQ(OK, req1.WaitForResult());
1842 // Job 1 finished OK. Start job 2 (also async OK). Request 3 is pending
1843 // without a job.
1844 connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob);
1846 TestSocketRequest req2(&request_order, &completion_count);
1847 rv = req2.handle()->Init("a",
1848 params_,
1849 DEFAULT_PRIORITY,
1850 req2.callback(),
1851 pool_.get(),
1852 BoundNetLog());
1853 EXPECT_EQ(ERR_IO_PENDING, rv);
1854 TestSocketRequest req3(&request_order, &completion_count);
1855 rv = req3.handle()->Init("a",
1856 params_,
1857 DEFAULT_PRIORITY,
1858 req3.callback(),
1859 pool_.get(),
1860 BoundNetLog());
1861 EXPECT_EQ(ERR_IO_PENDING, rv);
1863 // Both Requests 2 and 3 are pending. We release socket 1 which should
1864 // service request 2. Request 3 should still be waiting.
1865 req1.handle()->Reset();
1866 // Run the released socket wakeups.
1867 base::MessageLoop::current()->RunUntilIdle();
1868 ASSERT_TRUE(req2.handle()->socket());
1869 EXPECT_EQ(OK, req2.WaitForResult());
1870 EXPECT_FALSE(req3.handle()->socket());
1872 // Signal job 2, which should service request 3.
1874 client_socket_factory_.SignalJobs();
1875 EXPECT_EQ(OK, req3.WaitForResult());
1877 ASSERT_EQ(3U, request_order.size());
1878 EXPECT_EQ(&req1, request_order[0]);
1879 EXPECT_EQ(&req2, request_order[1]);
1880 EXPECT_EQ(&req3, request_order[2]);
1881 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
1884 // The requests are not coupled to the jobs. So, the requests should finish in
1885 // their priority / insertion order.
1886 TEST_F(ClientSocketPoolBaseTest, PendingJobCompletionOrder) {
1887 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
1888 // First two jobs are async.
1889 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob);
1891 std::vector<TestSocketRequest*> request_order;
1892 size_t completion_count; // unused
1893 TestSocketRequest req1(&request_order, &completion_count);
1894 int rv = req1.handle()->Init("a",
1895 params_,
1896 DEFAULT_PRIORITY,
1897 req1.callback(),
1898 pool_.get(),
1899 BoundNetLog());
1900 EXPECT_EQ(ERR_IO_PENDING, rv);
1902 TestSocketRequest req2(&request_order, &completion_count);
1903 rv = req2.handle()->Init("a",
1904 params_,
1905 DEFAULT_PRIORITY,
1906 req2.callback(),
1907 pool_.get(),
1908 BoundNetLog());
1909 EXPECT_EQ(ERR_IO_PENDING, rv);
1911 // The pending job is sync.
1912 connect_job_factory_->set_job_type(TestConnectJob::kMockJob);
1914 TestSocketRequest req3(&request_order, &completion_count);
1915 rv = req3.handle()->Init("a",
1916 params_,
1917 DEFAULT_PRIORITY,
1918 req3.callback(),
1919 pool_.get(),
1920 BoundNetLog());
1921 EXPECT_EQ(ERR_IO_PENDING, rv);
1923 EXPECT_EQ(ERR_CONNECTION_FAILED, req1.WaitForResult());
1924 EXPECT_EQ(OK, req2.WaitForResult());
1925 EXPECT_EQ(ERR_CONNECTION_FAILED, req3.WaitForResult());
1927 ASSERT_EQ(3U, request_order.size());
1928 EXPECT_EQ(&req1, request_order[0]);
1929 EXPECT_EQ(&req2, request_order[1]);
1930 EXPECT_EQ(&req3, request_order[2]);
1933 // Test GetLoadState in the case there's only one socket request.
1934 TEST_F(ClientSocketPoolBaseTest, LoadStateOneRequest) {
1935 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
1936 connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob);
1938 ClientSocketHandle handle;
1939 TestCompletionCallback callback;
1940 int rv = handle.Init("a",
1941 params_,
1942 DEFAULT_PRIORITY,
1943 callback.callback(),
1944 pool_.get(),
1945 BoundNetLog());
1946 EXPECT_EQ(ERR_IO_PENDING, rv);
1947 EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState());
1949 client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE);
1950 EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle.GetLoadState());
1952 // No point in completing the connection, since ClientSocketHandles only
1953 // expect the LoadState to be checked while connecting.
1956 // Test GetLoadState in the case there are two socket requests.
1957 // Only the first connection in the pool should affect the pool's load status.
1958 TEST_F(ClientSocketPoolBaseTest, LoadStateTwoRequests) {
1959 CreatePool(2, 2);
1960 connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob);
1962 ClientSocketHandle handle;
1963 TestCompletionCallback callback;
1964 int rv = handle.Init("a", params_, DEFAULT_PRIORITY, callback.callback(),
1965 pool_.get(), BoundNetLog());
1966 EXPECT_EQ(ERR_IO_PENDING, rv);
1967 client_socket_factory_.SetJobLoadState(0, LOAD_STATE_RESOLVING_HOST);
1969 ClientSocketHandle handle2;
1970 TestCompletionCallback callback2;
1971 rv = handle2.Init("a", params_, DEFAULT_PRIORITY, callback2.callback(),
1972 pool_.get(), BoundNetLog());
1973 EXPECT_EQ(ERR_IO_PENDING, rv);
1974 client_socket_factory_.SetJobLoadState(1, LOAD_STATE_RESOLVING_HOST);
1976 // Check that both handles report the state of the first job.
1977 EXPECT_EQ(LOAD_STATE_RESOLVING_HOST, handle.GetLoadState());
1978 EXPECT_EQ(LOAD_STATE_RESOLVING_HOST, handle2.GetLoadState());
1980 client_socket_factory_.SetJobLoadState(0, LOAD_STATE_CONNECTING);
1982 // Check that both handles change to LOAD_STATE_CONNECTING.
1983 EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState());
1984 EXPECT_EQ(LOAD_STATE_CONNECTING, handle2.GetLoadState());
1987 // Test that the second connection request does not affect the pool's load
1988 // status.
1989 TEST_F(ClientSocketPoolBaseTest, LoadStateTwoRequestsChangeSecondRequestState) {
1990 CreatePool(2, 2);
1991 connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob);
1993 ClientSocketHandle handle;
1994 TestCompletionCallback callback;
1995 int rv = handle.Init("a", params_, DEFAULT_PRIORITY, callback.callback(),
1996 pool_.get(), BoundNetLog());
1997 EXPECT_EQ(ERR_IO_PENDING, rv);
1999 ClientSocketHandle handle2;
2000 TestCompletionCallback callback2;
2001 rv = handle2.Init("a", params_, DEFAULT_PRIORITY, callback2.callback(),
2002 pool_.get(), BoundNetLog());
2003 EXPECT_EQ(ERR_IO_PENDING, rv);
2004 client_socket_factory_.SetJobLoadState(1, LOAD_STATE_RESOLVING_HOST);
2006 EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState());
2007 EXPECT_EQ(LOAD_STATE_CONNECTING, handle2.GetLoadState());
2009 // First job connects and the first request gets the socket. The
2010 // second handle switches to the state of the remaining ConnectJob.
2011 client_socket_factory_.SignalJob(0);
2012 EXPECT_EQ(OK, callback.WaitForResult());
2013 EXPECT_EQ(LOAD_STATE_RESOLVING_HOST, handle2.GetLoadState());
2016 // Test GetLoadState in the case the per-group limit is reached.
2017 TEST_F(ClientSocketPoolBaseTest, LoadStateGroupLimit) {
2018 CreatePool(2, 1);
2019 connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob);
2021 ClientSocketHandle handle;
2022 TestCompletionCallback callback;
2023 int rv = handle.Init("a",
2024 params_,
2025 MEDIUM,
2026 callback.callback(),
2027 pool_.get(),
2028 BoundNetLog());
2029 EXPECT_EQ(ERR_IO_PENDING, rv);
2030 EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState());
2032 // Request another socket from the same pool, buth with a higher priority.
2033 // The first request should now be stalled at the socket group limit.
2034 ClientSocketHandle handle2;
2035 TestCompletionCallback callback2;
2036 rv = handle2.Init("a",
2037 params_,
2038 HIGHEST,
2039 callback2.callback(),
2040 pool_.get(),
2041 BoundNetLog());
2042 EXPECT_EQ(ERR_IO_PENDING, rv);
2043 EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET, handle.GetLoadState());
2044 EXPECT_EQ(LOAD_STATE_CONNECTING, handle2.GetLoadState());
2046 // The first handle should remain stalled as the other socket goes through
2047 // the connect process.
2049 client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE);
2050 EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET, handle.GetLoadState());
2051 EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle2.GetLoadState());
2053 client_socket_factory_.SignalJob(0);
2054 EXPECT_EQ(OK, callback2.WaitForResult());
2055 EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET, handle.GetLoadState());
2057 // Closing the second socket should cause the stalled handle to finally get a
2058 // ConnectJob.
2059 handle2.socket()->Disconnect();
2060 handle2.Reset();
2061 EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState());
2064 // Test GetLoadState in the case the per-pool limit is reached.
2065 TEST_F(ClientSocketPoolBaseTest, LoadStatePoolLimit) {
2066 CreatePool(2, 2);
2067 connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob);
2069 ClientSocketHandle handle;
2070 TestCompletionCallback callback;
2071 int rv = handle.Init("a",
2072 params_,
2073 DEFAULT_PRIORITY,
2074 callback.callback(),
2075 pool_.get(),
2076 BoundNetLog());
2077 EXPECT_EQ(ERR_IO_PENDING, rv);
2079 // Request for socket from another pool.
2080 ClientSocketHandle handle2;
2081 TestCompletionCallback callback2;
2082 rv = handle2.Init("b",
2083 params_,
2084 DEFAULT_PRIORITY,
2085 callback2.callback(),
2086 pool_.get(),
2087 BoundNetLog());
2088 EXPECT_EQ(ERR_IO_PENDING, rv);
2090 // Request another socket from the first pool. Request should stall at the
2091 // socket pool limit.
2092 ClientSocketHandle handle3;
2093 TestCompletionCallback callback3;
2094 rv = handle3.Init("a",
2095 params_,
2096 DEFAULT_PRIORITY,
2097 callback2.callback(),
2098 pool_.get(),
2099 BoundNetLog());
2100 EXPECT_EQ(ERR_IO_PENDING, rv);
2102 // The third handle should remain stalled as the other sockets in its group
2103 // goes through the connect process.
2105 EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState());
2106 EXPECT_EQ(LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL, handle3.GetLoadState());
2108 client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE);
2109 EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle.GetLoadState());
2110 EXPECT_EQ(LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL, handle3.GetLoadState());
2112 client_socket_factory_.SignalJob(0);
2113 EXPECT_EQ(OK, callback.WaitForResult());
2114 EXPECT_EQ(LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL, handle3.GetLoadState());
2116 // Closing a socket should allow the stalled handle to finally get a new
2117 // ConnectJob.
2118 handle.socket()->Disconnect();
2119 handle.Reset();
2120 EXPECT_EQ(LOAD_STATE_CONNECTING, handle3.GetLoadState());
2123 TEST_F(ClientSocketPoolBaseTest, Recoverable) {
2124 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
2125 connect_job_factory_->set_job_type(TestConnectJob::kMockRecoverableJob);
2127 ClientSocketHandle handle;
2128 TestCompletionCallback callback;
2129 EXPECT_EQ(ERR_PROXY_AUTH_REQUESTED,
2130 handle.Init("a", params_, DEFAULT_PRIORITY, callback.callback(),
2131 pool_.get(), BoundNetLog()));
2132 EXPECT_TRUE(handle.is_initialized());
2133 EXPECT_TRUE(handle.socket());
2136 TEST_F(ClientSocketPoolBaseTest, AsyncRecoverable) {
2137 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
2139 connect_job_factory_->set_job_type(
2140 TestConnectJob::kMockPendingRecoverableJob);
2141 ClientSocketHandle handle;
2142 TestCompletionCallback callback;
2143 EXPECT_EQ(ERR_IO_PENDING,
2144 handle.Init("a",
2145 params_,
2146 DEFAULT_PRIORITY,
2147 callback.callback(),
2148 pool_.get(),
2149 BoundNetLog()));
2150 EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", &handle));
2151 EXPECT_EQ(ERR_PROXY_AUTH_REQUESTED, callback.WaitForResult());
2152 EXPECT_TRUE(handle.is_initialized());
2153 EXPECT_TRUE(handle.socket());
2156 TEST_F(ClientSocketPoolBaseTest, AdditionalErrorStateSynchronous) {
2157 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
2158 connect_job_factory_->set_job_type(
2159 TestConnectJob::kMockAdditionalErrorStateJob);
2161 ClientSocketHandle handle;
2162 TestCompletionCallback callback;
2163 EXPECT_EQ(ERR_CONNECTION_FAILED,
2164 handle.Init("a",
2165 params_,
2166 DEFAULT_PRIORITY,
2167 callback.callback(),
2168 pool_.get(),
2169 BoundNetLog()));
2170 EXPECT_FALSE(handle.is_initialized());
2171 EXPECT_FALSE(handle.socket());
2172 EXPECT_TRUE(handle.is_ssl_error());
2173 EXPECT_FALSE(handle.ssl_error_response_info().headers.get() == NULL);
2176 TEST_F(ClientSocketPoolBaseTest, AdditionalErrorStateAsynchronous) {
2177 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
2179 connect_job_factory_->set_job_type(
2180 TestConnectJob::kMockPendingAdditionalErrorStateJob);
2181 ClientSocketHandle handle;
2182 TestCompletionCallback callback;
2183 EXPECT_EQ(ERR_IO_PENDING,
2184 handle.Init("a",
2185 params_,
2186 DEFAULT_PRIORITY,
2187 callback.callback(),
2188 pool_.get(),
2189 BoundNetLog()));
2190 EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", &handle));
2191 EXPECT_EQ(ERR_CONNECTION_FAILED, callback.WaitForResult());
2192 EXPECT_FALSE(handle.is_initialized());
2193 EXPECT_FALSE(handle.socket());
2194 EXPECT_TRUE(handle.is_ssl_error());
2195 EXPECT_FALSE(handle.ssl_error_response_info().headers.get() == NULL);
2198 // Make sure we can reuse sockets when the cleanup timer is disabled.
2199 TEST_F(ClientSocketPoolBaseTest, DisableCleanupTimerReuse) {
2200 // Disable cleanup timer.
2201 internal::ClientSocketPoolBaseHelper::set_cleanup_timer_enabled(false);
2203 CreatePoolWithIdleTimeouts(
2204 kDefaultMaxSockets, kDefaultMaxSocketsPerGroup,
2205 base::TimeDelta(), // Time out unused sockets immediately.
2206 base::TimeDelta::FromDays(1)); // Don't time out used sockets.
2208 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
2210 ClientSocketHandle handle;
2211 TestCompletionCallback callback;
2212 int rv = handle.Init("a",
2213 params_,
2214 LOWEST,
2215 callback.callback(),
2216 pool_.get(),
2217 BoundNetLog());
2218 ASSERT_EQ(ERR_IO_PENDING, rv);
2219 EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", &handle));
2220 ASSERT_EQ(OK, callback.WaitForResult());
2222 // Use and release the socket.
2223 EXPECT_EQ(1, handle.socket()->Write(NULL, 1, CompletionCallback()));
2224 TestLoadTimingInfoConnectedNotReused(handle);
2225 handle.Reset();
2227 // Should now have one idle socket.
2228 ASSERT_EQ(1, pool_->IdleSocketCount());
2230 // Request a new socket. This should reuse the old socket and complete
2231 // synchronously.
2232 BoundTestNetLog log;
2233 rv = handle.Init("a",
2234 params_,
2235 LOWEST,
2236 CompletionCallback(),
2237 pool_.get(),
2238 log.bound());
2239 ASSERT_EQ(OK, rv);
2240 EXPECT_TRUE(handle.is_reused());
2241 TestLoadTimingInfoConnectedReused(handle);
2243 ASSERT_TRUE(pool_->HasGroup("a"));
2244 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
2245 EXPECT_EQ(1, pool_->NumActiveSocketsInGroup("a"));
2247 TestNetLog::CapturedEntryList entries;
2248 log.GetEntries(&entries);
2249 EXPECT_TRUE(LogContainsEntryWithType(
2250 entries, 1, NetLog::TYPE_SOCKET_POOL_REUSED_AN_EXISTING_SOCKET));
2253 // Make sure we cleanup old unused sockets when the cleanup timer is disabled.
2254 TEST_F(ClientSocketPoolBaseTest, DisableCleanupTimerNoReuse) {
2255 // Disable cleanup timer.
2256 internal::ClientSocketPoolBaseHelper::set_cleanup_timer_enabled(false);
2258 CreatePoolWithIdleTimeouts(
2259 kDefaultMaxSockets, kDefaultMaxSocketsPerGroup,
2260 base::TimeDelta(), // Time out unused sockets immediately
2261 base::TimeDelta()); // Time out used sockets immediately
2263 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
2265 // Startup two mock pending connect jobs, which will sit in the MessageLoop.
2267 ClientSocketHandle handle;
2268 TestCompletionCallback callback;
2269 int rv = handle.Init("a",
2270 params_,
2271 LOWEST,
2272 callback.callback(),
2273 pool_.get(),
2274 BoundNetLog());
2275 ASSERT_EQ(ERR_IO_PENDING, rv);
2276 EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", &handle));
2278 ClientSocketHandle handle2;
2279 TestCompletionCallback callback2;
2280 rv = handle2.Init("a",
2281 params_,
2282 LOWEST,
2283 callback2.callback(),
2284 pool_.get(),
2285 BoundNetLog());
2286 ASSERT_EQ(ERR_IO_PENDING, rv);
2287 EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", &handle2));
2289 // Cancel one of the requests. Wait for the other, which will get the first
2290 // job. Release the socket. Run the loop again to make sure the second
2291 // socket is sitting idle and the first one is released (since ReleaseSocket()
2292 // just posts a DoReleaseSocket() task).
2294 handle.Reset();
2295 ASSERT_EQ(OK, callback2.WaitForResult());
2296 // Use the socket.
2297 EXPECT_EQ(1, handle2.socket()->Write(NULL, 1, CompletionCallback()));
2298 handle2.Reset();
2300 // We post all of our delayed tasks with a 2ms delay. I.e. they don't
2301 // actually become pending until 2ms after they have been created. In order
2302 // to flush all tasks, we need to wait so that we know there are no
2303 // soon-to-be-pending tasks waiting.
2304 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10));
2305 base::MessageLoop::current()->RunUntilIdle();
2307 // Both sockets should now be idle.
2308 ASSERT_EQ(2, pool_->IdleSocketCount());
2310 // Request a new socket. This should cleanup the unused and timed out ones.
2311 // A new socket will be created rather than reusing the idle one.
2312 BoundTestNetLog log;
2313 TestCompletionCallback callback3;
2314 rv = handle.Init("a",
2315 params_,
2316 LOWEST,
2317 callback3.callback(),
2318 pool_.get(),
2319 log.bound());
2320 ASSERT_EQ(ERR_IO_PENDING, rv);
2321 ASSERT_EQ(OK, callback3.WaitForResult());
2322 EXPECT_FALSE(handle.is_reused());
2324 // Make sure the idle socket is closed.
2325 ASSERT_TRUE(pool_->HasGroup("a"));
2326 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
2327 EXPECT_EQ(1, pool_->NumActiveSocketsInGroup("a"));
2329 TestNetLog::CapturedEntryList entries;
2330 log.GetEntries(&entries);
2331 EXPECT_FALSE(LogContainsEntryWithType(
2332 entries, 1, NetLog::TYPE_SOCKET_POOL_REUSED_AN_EXISTING_SOCKET));
2335 TEST_F(ClientSocketPoolBaseTest, CleanupTimedOutIdleSockets) {
2336 CreatePoolWithIdleTimeouts(
2337 kDefaultMaxSockets, kDefaultMaxSocketsPerGroup,
2338 base::TimeDelta(), // Time out unused sockets immediately.
2339 base::TimeDelta::FromDays(1)); // Don't time out used sockets.
2341 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
2343 // Startup two mock pending connect jobs, which will sit in the MessageLoop.
2345 ClientSocketHandle handle;
2346 TestCompletionCallback callback;
2347 int rv = handle.Init("a",
2348 params_,
2349 LOWEST,
2350 callback.callback(),
2351 pool_.get(),
2352 BoundNetLog());
2353 EXPECT_EQ(ERR_IO_PENDING, rv);
2354 EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", &handle));
2356 ClientSocketHandle handle2;
2357 TestCompletionCallback callback2;
2358 rv = handle2.Init("a",
2359 params_,
2360 LOWEST,
2361 callback2.callback(),
2362 pool_.get(),
2363 BoundNetLog());
2364 EXPECT_EQ(ERR_IO_PENDING, rv);
2365 EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", &handle2));
2367 // Cancel one of the requests. Wait for the other, which will get the first
2368 // job. Release the socket. Run the loop again to make sure the second
2369 // socket is sitting idle and the first one is released (since ReleaseSocket()
2370 // just posts a DoReleaseSocket() task).
2372 handle.Reset();
2373 EXPECT_EQ(OK, callback2.WaitForResult());
2374 // Use the socket.
2375 EXPECT_EQ(1, handle2.socket()->Write(NULL, 1, CompletionCallback()));
2376 handle2.Reset();
2378 // We post all of our delayed tasks with a 2ms delay. I.e. they don't
2379 // actually become pending until 2ms after they have been created. In order
2380 // to flush all tasks, we need to wait so that we know there are no
2381 // soon-to-be-pending tasks waiting.
2382 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10));
2383 base::MessageLoop::current()->RunUntilIdle();
2385 ASSERT_EQ(2, pool_->IdleSocketCount());
2387 // Invoke the idle socket cleanup check. Only one socket should be left, the
2388 // used socket. Request it to make sure that it's used.
2390 pool_->CleanupTimedOutIdleSockets();
2391 BoundTestNetLog log;
2392 rv = handle.Init("a",
2393 params_,
2394 LOWEST,
2395 callback.callback(),
2396 pool_.get(),
2397 log.bound());
2398 EXPECT_EQ(OK, rv);
2399 EXPECT_TRUE(handle.is_reused());
2401 TestNetLog::CapturedEntryList entries;
2402 log.GetEntries(&entries);
2403 EXPECT_TRUE(LogContainsEntryWithType(
2404 entries, 1, NetLog::TYPE_SOCKET_POOL_REUSED_AN_EXISTING_SOCKET));
2407 // Make sure that we process all pending requests even when we're stalling
2408 // because of multiple releasing disconnected sockets.
2409 TEST_F(ClientSocketPoolBaseTest, MultipleReleasingDisconnectedSockets) {
2410 CreatePoolWithIdleTimeouts(
2411 kDefaultMaxSockets, kDefaultMaxSocketsPerGroup,
2412 base::TimeDelta(), // Time out unused sockets immediately.
2413 base::TimeDelta::FromDays(1)); // Don't time out used sockets.
2415 connect_job_factory_->set_job_type(TestConnectJob::kMockJob);
2417 // Startup 4 connect jobs. Two of them will be pending.
2419 ClientSocketHandle handle;
2420 TestCompletionCallback callback;
2421 int rv = handle.Init("a",
2422 params_,
2423 LOWEST,
2424 callback.callback(),
2425 pool_.get(),
2426 BoundNetLog());
2427 EXPECT_EQ(OK, rv);
2429 ClientSocketHandle handle2;
2430 TestCompletionCallback callback2;
2431 rv = handle2.Init("a",
2432 params_,
2433 LOWEST,
2434 callback2.callback(),
2435 pool_.get(),
2436 BoundNetLog());
2437 EXPECT_EQ(OK, rv);
2439 ClientSocketHandle handle3;
2440 TestCompletionCallback callback3;
2441 rv = handle3.Init("a",
2442 params_,
2443 LOWEST,
2444 callback3.callback(),
2445 pool_.get(),
2446 BoundNetLog());
2447 EXPECT_EQ(ERR_IO_PENDING, rv);
2449 ClientSocketHandle handle4;
2450 TestCompletionCallback callback4;
2451 rv = handle4.Init("a",
2452 params_,
2453 LOWEST,
2454 callback4.callback(),
2455 pool_.get(),
2456 BoundNetLog());
2457 EXPECT_EQ(ERR_IO_PENDING, rv);
2459 // Release two disconnected sockets.
2461 handle.socket()->Disconnect();
2462 handle.Reset();
2463 handle2.socket()->Disconnect();
2464 handle2.Reset();
2466 EXPECT_EQ(OK, callback3.WaitForResult());
2467 EXPECT_FALSE(handle3.is_reused());
2468 EXPECT_EQ(OK, callback4.WaitForResult());
2469 EXPECT_FALSE(handle4.is_reused());
2472 // Regression test for http://crbug.com/42267.
2473 // When DoReleaseSocket() is processed for one socket, it is blocked because the
2474 // other stalled groups all have releasing sockets, so no progress can be made.
2475 TEST_F(ClientSocketPoolBaseTest, SocketLimitReleasingSockets) {
2476 CreatePoolWithIdleTimeouts(
2477 4 /* socket limit */, 4 /* socket limit per group */,
2478 base::TimeDelta(), // Time out unused sockets immediately.
2479 base::TimeDelta::FromDays(1)); // Don't time out used sockets.
2481 connect_job_factory_->set_job_type(TestConnectJob::kMockJob);
2483 // Max out the socket limit with 2 per group.
2485 ClientSocketHandle handle_a[4];
2486 TestCompletionCallback callback_a[4];
2487 ClientSocketHandle handle_b[4];
2488 TestCompletionCallback callback_b[4];
2490 for (int i = 0; i < 2; ++i) {
2491 EXPECT_EQ(OK, handle_a[i].Init("a",
2492 params_,
2493 LOWEST,
2494 callback_a[i].callback(),
2495 pool_.get(),
2496 BoundNetLog()));
2497 EXPECT_EQ(OK, handle_b[i].Init("b",
2498 params_,
2499 LOWEST,
2500 callback_b[i].callback(),
2501 pool_.get(),
2502 BoundNetLog()));
2505 // Make 4 pending requests, 2 per group.
2507 for (int i = 2; i < 4; ++i) {
2508 EXPECT_EQ(ERR_IO_PENDING,
2509 handle_a[i].Init("a",
2510 params_,
2511 LOWEST,
2512 callback_a[i].callback(),
2513 pool_.get(),
2514 BoundNetLog()));
2515 EXPECT_EQ(ERR_IO_PENDING,
2516 handle_b[i].Init("b",
2517 params_,
2518 LOWEST,
2519 callback_b[i].callback(),
2520 pool_.get(),
2521 BoundNetLog()));
2524 // Release b's socket first. The order is important, because in
2525 // DoReleaseSocket(), we'll process b's released socket, and since both b and
2526 // a are stalled, but 'a' is lower lexicographically, we'll process group 'a'
2527 // first, which has a releasing socket, so it refuses to start up another
2528 // ConnectJob. So, we used to infinite loop on this.
2529 handle_b[0].socket()->Disconnect();
2530 handle_b[0].Reset();
2531 handle_a[0].socket()->Disconnect();
2532 handle_a[0].Reset();
2534 // Used to get stuck here.
2535 base::MessageLoop::current()->RunUntilIdle();
2537 handle_b[1].socket()->Disconnect();
2538 handle_b[1].Reset();
2539 handle_a[1].socket()->Disconnect();
2540 handle_a[1].Reset();
2542 for (int i = 2; i < 4; ++i) {
2543 EXPECT_EQ(OK, callback_b[i].WaitForResult());
2544 EXPECT_EQ(OK, callback_a[i].WaitForResult());
2548 TEST_F(ClientSocketPoolBaseTest,
2549 ReleasingDisconnectedSocketsMaintainsPriorityOrder) {
2550 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
2552 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
2554 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", DEFAULT_PRIORITY));
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));
2559 EXPECT_EQ(OK, (*requests())[0]->WaitForResult());
2560 EXPECT_EQ(OK, (*requests())[1]->WaitForResult());
2561 EXPECT_EQ(2u, completion_count());
2563 // Releases one connection.
2564 EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::NO_KEEP_ALIVE));
2565 EXPECT_EQ(OK, (*requests())[2]->WaitForResult());
2567 EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::NO_KEEP_ALIVE));
2568 EXPECT_EQ(OK, (*requests())[3]->WaitForResult());
2569 EXPECT_EQ(4u, completion_count());
2571 EXPECT_EQ(1, GetOrderOfRequest(1));
2572 EXPECT_EQ(2, GetOrderOfRequest(2));
2573 EXPECT_EQ(3, GetOrderOfRequest(3));
2574 EXPECT_EQ(4, GetOrderOfRequest(4));
2576 // Make sure we test order of all requests made.
2577 EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(5));
2580 class TestReleasingSocketRequest : public TestCompletionCallbackBase {
2581 public:
2582 TestReleasingSocketRequest(TestClientSocketPool* pool,
2583 int expected_result,
2584 bool reset_releasing_handle)
2585 : pool_(pool),
2586 expected_result_(expected_result),
2587 reset_releasing_handle_(reset_releasing_handle),
2588 callback_(base::Bind(&TestReleasingSocketRequest::OnComplete,
2589 base::Unretained(this))) {
2592 ~TestReleasingSocketRequest() override {}
2594 ClientSocketHandle* handle() { return &handle_; }
2596 const CompletionCallback& callback() const { return callback_; }
2598 private:
2599 void OnComplete(int result) {
2600 SetResult(result);
2601 if (reset_releasing_handle_)
2602 handle_.Reset();
2604 scoped_refptr<TestSocketParams> con_params(
2605 new TestSocketParams(false /* ignore_limits */));
2606 EXPECT_EQ(expected_result_,
2607 handle2_.Init("a", con_params, DEFAULT_PRIORITY,
2608 callback2_.callback(), pool_, BoundNetLog()));
2611 TestClientSocketPool* const pool_;
2612 int expected_result_;
2613 bool reset_releasing_handle_;
2614 ClientSocketHandle handle_;
2615 ClientSocketHandle handle2_;
2616 CompletionCallback callback_;
2617 TestCompletionCallback callback2_;
2621 TEST_F(ClientSocketPoolBaseTest, AdditionalErrorSocketsDontUseSlot) {
2622 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
2624 EXPECT_EQ(OK, StartRequest("b", DEFAULT_PRIORITY));
2625 EXPECT_EQ(OK, StartRequest("a", DEFAULT_PRIORITY));
2626 EXPECT_EQ(OK, StartRequest("b", DEFAULT_PRIORITY));
2628 EXPECT_EQ(static_cast<int>(requests_size()),
2629 client_socket_factory_.allocation_count());
2631 connect_job_factory_->set_job_type(
2632 TestConnectJob::kMockPendingAdditionalErrorStateJob);
2633 TestReleasingSocketRequest req(pool_.get(), OK, false);
2634 EXPECT_EQ(ERR_IO_PENDING,
2635 req.handle()->Init("a", params_, DEFAULT_PRIORITY, req.callback(),
2636 pool_.get(), BoundNetLog()));
2637 // The next job should complete synchronously
2638 connect_job_factory_->set_job_type(TestConnectJob::kMockJob);
2640 EXPECT_EQ(ERR_CONNECTION_FAILED, req.WaitForResult());
2641 EXPECT_FALSE(req.handle()->is_initialized());
2642 EXPECT_FALSE(req.handle()->socket());
2643 EXPECT_TRUE(req.handle()->is_ssl_error());
2644 EXPECT_FALSE(req.handle()->ssl_error_response_info().headers.get() == NULL);
2647 // http://crbug.com/44724 regression test.
2648 // We start releasing the pool when we flush on network change. When that
2649 // happens, the only active references are in the ClientSocketHandles. When a
2650 // ConnectJob completes and calls back into the last ClientSocketHandle, that
2651 // callback can release the last reference and delete the pool. After the
2652 // callback finishes, we go back to the stack frame within the now-deleted pool.
2653 // Executing any code that refers to members of the now-deleted pool can cause
2654 // crashes.
2655 TEST_F(ClientSocketPoolBaseTest, CallbackThatReleasesPool) {
2656 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
2657 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob);
2659 ClientSocketHandle handle;
2660 TestCompletionCallback callback;
2661 EXPECT_EQ(ERR_IO_PENDING, handle.Init("a",
2662 params_,
2663 DEFAULT_PRIORITY,
2664 callback.callback(),
2665 pool_.get(),
2666 BoundNetLog()));
2668 pool_->FlushWithError(ERR_NETWORK_CHANGED);
2670 // We'll call back into this now.
2671 callback.WaitForResult();
2674 TEST_F(ClientSocketPoolBaseTest, DoNotReuseSocketAfterFlush) {
2675 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
2676 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
2678 ClientSocketHandle handle;
2679 TestCompletionCallback callback;
2680 EXPECT_EQ(ERR_IO_PENDING, handle.Init("a",
2681 params_,
2682 DEFAULT_PRIORITY,
2683 callback.callback(),
2684 pool_.get(),
2685 BoundNetLog()));
2686 EXPECT_EQ(OK, callback.WaitForResult());
2687 EXPECT_EQ(ClientSocketHandle::UNUSED, handle.reuse_type());
2689 pool_->FlushWithError(ERR_NETWORK_CHANGED);
2691 handle.Reset();
2692 base::MessageLoop::current()->RunUntilIdle();
2694 EXPECT_EQ(ERR_IO_PENDING, handle.Init("a",
2695 params_,
2696 DEFAULT_PRIORITY,
2697 callback.callback(),
2698 pool_.get(),
2699 BoundNetLog()));
2700 EXPECT_EQ(OK, callback.WaitForResult());
2701 EXPECT_EQ(ClientSocketHandle::UNUSED, handle.reuse_type());
2704 class ConnectWithinCallback : public TestCompletionCallbackBase {
2705 public:
2706 ConnectWithinCallback(
2707 const std::string& group_name,
2708 const scoped_refptr<TestSocketParams>& params,
2709 TestClientSocketPool* pool)
2710 : group_name_(group_name),
2711 params_(params),
2712 pool_(pool),
2713 callback_(base::Bind(&ConnectWithinCallback::OnComplete,
2714 base::Unretained(this))) {
2717 ~ConnectWithinCallback() override {}
2719 int WaitForNestedResult() {
2720 return nested_callback_.WaitForResult();
2723 const CompletionCallback& callback() const { return callback_; }
2725 private:
2726 void OnComplete(int result) {
2727 SetResult(result);
2728 EXPECT_EQ(ERR_IO_PENDING,
2729 handle_.Init(group_name_,
2730 params_,
2731 DEFAULT_PRIORITY,
2732 nested_callback_.callback(),
2733 pool_,
2734 BoundNetLog()));
2737 const std::string group_name_;
2738 const scoped_refptr<TestSocketParams> params_;
2739 TestClientSocketPool* const pool_;
2740 ClientSocketHandle handle_;
2741 CompletionCallback callback_;
2742 TestCompletionCallback nested_callback_;
2744 DISALLOW_COPY_AND_ASSIGN(ConnectWithinCallback);
2747 TEST_F(ClientSocketPoolBaseTest, AbortAllRequestsOnFlush) {
2748 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
2750 // First job will be waiting until it gets aborted.
2751 connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob);
2753 ClientSocketHandle handle;
2754 ConnectWithinCallback callback("a", params_, pool_.get());
2755 EXPECT_EQ(ERR_IO_PENDING, handle.Init("a",
2756 params_,
2757 DEFAULT_PRIORITY,
2758 callback.callback(),
2759 pool_.get(),
2760 BoundNetLog()));
2762 // Second job will be started during the first callback, and will
2763 // asynchronously complete with OK.
2764 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
2765 pool_->FlushWithError(ERR_NETWORK_CHANGED);
2766 EXPECT_EQ(ERR_NETWORK_CHANGED, callback.WaitForResult());
2767 EXPECT_EQ(OK, callback.WaitForNestedResult());
2770 // Cancel a pending socket request while we're at max sockets,
2771 // and verify that the backup socket firing doesn't cause a crash.
2772 TEST_F(ClientSocketPoolBaseTest, BackupSocketCancelAtMaxSockets) {
2773 // Max 4 sockets globally, max 4 sockets per group.
2774 CreatePool(kDefaultMaxSockets, kDefaultMaxSockets);
2775 pool_->EnableConnectBackupJobs();
2777 // Create the first socket and set to ERR_IO_PENDING. This starts the backup
2778 // timer.
2779 connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob);
2780 ClientSocketHandle handle;
2781 TestCompletionCallback callback;
2782 EXPECT_EQ(ERR_IO_PENDING, handle.Init("bar",
2783 params_,
2784 DEFAULT_PRIORITY,
2785 callback.callback(),
2786 pool_.get(),
2787 BoundNetLog()));
2789 // Start (MaxSockets - 1) connected sockets to reach max sockets.
2790 connect_job_factory_->set_job_type(TestConnectJob::kMockJob);
2791 ClientSocketHandle handles[kDefaultMaxSockets];
2792 for (int i = 1; i < kDefaultMaxSockets; ++i) {
2793 TestCompletionCallback callback;
2794 EXPECT_EQ(OK, handles[i].Init("bar",
2795 params_,
2796 DEFAULT_PRIORITY,
2797 callback.callback(),
2798 pool_.get(),
2799 BoundNetLog()));
2802 base::MessageLoop::current()->RunUntilIdle();
2804 // Cancel the pending request.
2805 handle.Reset();
2807 // Wait for the backup timer to fire (add some slop to ensure it fires)
2808 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(
2809 ClientSocketPool::kMaxConnectRetryIntervalMs / 2 * 3));
2811 base::MessageLoop::current()->RunUntilIdle();
2812 EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count());
2815 TEST_F(ClientSocketPoolBaseTest, CancelBackupSocketAfterCancelingAllRequests) {
2816 CreatePool(kDefaultMaxSockets, kDefaultMaxSockets);
2817 pool_->EnableConnectBackupJobs();
2819 // Create the first socket and set to ERR_IO_PENDING. This starts the backup
2820 // timer.
2821 connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob);
2822 ClientSocketHandle handle;
2823 TestCompletionCallback callback;
2824 EXPECT_EQ(ERR_IO_PENDING, handle.Init("bar",
2825 params_,
2826 DEFAULT_PRIORITY,
2827 callback.callback(),
2828 pool_.get(),
2829 BoundNetLog()));
2830 ASSERT_TRUE(pool_->HasGroup("bar"));
2831 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("bar"));
2832 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("bar"));
2834 // Cancel the socket request. This should cancel the backup timer. Wait for
2835 // the backup time to see if it indeed got canceled.
2836 handle.Reset();
2837 // Wait for the backup timer to fire (add some slop to ensure it fires)
2838 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(
2839 ClientSocketPool::kMaxConnectRetryIntervalMs / 2 * 3));
2840 base::MessageLoop::current()->RunUntilIdle();
2841 ASSERT_TRUE(pool_->HasGroup("bar"));
2842 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("bar"));
2845 TEST_F(ClientSocketPoolBaseTest, CancelBackupSocketAfterFinishingAllRequests) {
2846 CreatePool(kDefaultMaxSockets, kDefaultMaxSockets);
2847 pool_->EnableConnectBackupJobs();
2849 // Create the first socket and set to ERR_IO_PENDING. This starts the backup
2850 // timer.
2851 connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob);
2852 ClientSocketHandle handle;
2853 TestCompletionCallback callback;
2854 EXPECT_EQ(ERR_IO_PENDING, handle.Init("bar",
2855 params_,
2856 DEFAULT_PRIORITY,
2857 callback.callback(),
2858 pool_.get(),
2859 BoundNetLog()));
2860 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
2861 ClientSocketHandle handle2;
2862 TestCompletionCallback callback2;
2863 EXPECT_EQ(ERR_IO_PENDING, handle2.Init("bar",
2864 params_,
2865 DEFAULT_PRIORITY,
2866 callback2.callback(),
2867 pool_.get(),
2868 BoundNetLog()));
2869 ASSERT_TRUE(pool_->HasGroup("bar"));
2870 EXPECT_EQ(2, pool_->NumConnectJobsInGroup("bar"));
2872 // Cancel request 1 and then complete request 2. With the requests finished,
2873 // the backup timer should be cancelled.
2874 handle.Reset();
2875 EXPECT_EQ(OK, callback2.WaitForResult());
2876 // Wait for the backup timer to fire (add some slop to ensure it fires)
2877 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(
2878 ClientSocketPool::kMaxConnectRetryIntervalMs / 2 * 3));
2879 base::MessageLoop::current()->RunUntilIdle();
2882 // Test delayed socket binding for the case where we have two connects,
2883 // and while one is waiting on a connect, the other frees up.
2884 // The socket waiting on a connect should switch immediately to the freed
2885 // up socket.
2886 TEST_F(ClientSocketPoolBaseTest, DelayedSocketBindingWaitingForConnect) {
2887 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
2888 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
2890 ClientSocketHandle handle1;
2891 TestCompletionCallback callback;
2892 EXPECT_EQ(ERR_IO_PENDING,
2893 handle1.Init("a",
2894 params_,
2895 DEFAULT_PRIORITY,
2896 callback.callback(),
2897 pool_.get(),
2898 BoundNetLog()));
2899 EXPECT_EQ(OK, callback.WaitForResult());
2901 // No idle sockets, no pending jobs.
2902 EXPECT_EQ(0, pool_->IdleSocketCount());
2903 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a"));
2905 // Create a second socket to the same host, but this one will wait.
2906 connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob);
2907 ClientSocketHandle handle2;
2908 EXPECT_EQ(ERR_IO_PENDING,
2909 handle2.Init("a",
2910 params_,
2911 DEFAULT_PRIORITY,
2912 callback.callback(),
2913 pool_.get(),
2914 BoundNetLog()));
2915 // No idle sockets, and one connecting job.
2916 EXPECT_EQ(0, pool_->IdleSocketCount());
2917 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a"));
2919 // Return the first handle to the pool. This will initiate the delayed
2920 // binding.
2921 handle1.Reset();
2923 base::MessageLoop::current()->RunUntilIdle();
2925 // Still no idle sockets, still one pending connect job.
2926 EXPECT_EQ(0, pool_->IdleSocketCount());
2927 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a"));
2929 // The second socket connected, even though it was a Waiting Job.
2930 EXPECT_EQ(OK, callback.WaitForResult());
2932 // And we can see there is still one job waiting.
2933 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a"));
2935 // Finally, signal the waiting Connect.
2936 client_socket_factory_.SignalJobs();
2937 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a"));
2939 base::MessageLoop::current()->RunUntilIdle();
2942 // Test delayed socket binding when a group is at capacity and one
2943 // of the group's sockets frees up.
2944 TEST_F(ClientSocketPoolBaseTest, DelayedSocketBindingAtGroupCapacity) {
2945 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
2946 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
2948 ClientSocketHandle handle1;
2949 TestCompletionCallback callback;
2950 EXPECT_EQ(ERR_IO_PENDING,
2951 handle1.Init("a",
2952 params_,
2953 DEFAULT_PRIORITY,
2954 callback.callback(),
2955 pool_.get(),
2956 BoundNetLog()));
2957 EXPECT_EQ(OK, callback.WaitForResult());
2959 // No idle sockets, no pending jobs.
2960 EXPECT_EQ(0, pool_->IdleSocketCount());
2961 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a"));
2963 // Create a second socket to the same host, but this one will wait.
2964 connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob);
2965 ClientSocketHandle handle2;
2966 EXPECT_EQ(ERR_IO_PENDING,
2967 handle2.Init("a",
2968 params_,
2969 DEFAULT_PRIORITY,
2970 callback.callback(),
2971 pool_.get(),
2972 BoundNetLog()));
2973 // No idle sockets, and one connecting job.
2974 EXPECT_EQ(0, pool_->IdleSocketCount());
2975 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a"));
2977 // Return the first handle to the pool. This will initiate the delayed
2978 // binding.
2979 handle1.Reset();
2981 base::MessageLoop::current()->RunUntilIdle();
2983 // Still no idle sockets, still one pending connect job.
2984 EXPECT_EQ(0, pool_->IdleSocketCount());
2985 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a"));
2987 // The second socket connected, even though it was a Waiting Job.
2988 EXPECT_EQ(OK, callback.WaitForResult());
2990 // And we can see there is still one job waiting.
2991 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a"));
2993 // Finally, signal the waiting Connect.
2994 client_socket_factory_.SignalJobs();
2995 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a"));
2997 base::MessageLoop::current()->RunUntilIdle();
3000 // Test out the case where we have one socket connected, one
3001 // connecting, when the first socket finishes and goes idle.
3002 // Although the second connection is pending, the second request
3003 // should complete, by taking the first socket's idle socket.
3004 TEST_F(ClientSocketPoolBaseTest, DelayedSocketBindingAtStall) {
3005 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
3006 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
3008 ClientSocketHandle handle1;
3009 TestCompletionCallback callback;
3010 EXPECT_EQ(ERR_IO_PENDING,
3011 handle1.Init("a",
3012 params_,
3013 DEFAULT_PRIORITY,
3014 callback.callback(),
3015 pool_.get(),
3016 BoundNetLog()));
3017 EXPECT_EQ(OK, callback.WaitForResult());
3019 // No idle sockets, no pending jobs.
3020 EXPECT_EQ(0, pool_->IdleSocketCount());
3021 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a"));
3023 // Create a second socket to the same host, but this one will wait.
3024 connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob);
3025 ClientSocketHandle handle2;
3026 EXPECT_EQ(ERR_IO_PENDING,
3027 handle2.Init("a",
3028 params_,
3029 DEFAULT_PRIORITY,
3030 callback.callback(),
3031 pool_.get(),
3032 BoundNetLog()));
3033 // No idle sockets, and one connecting job.
3034 EXPECT_EQ(0, pool_->IdleSocketCount());
3035 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a"));
3037 // Return the first handle to the pool. This will initiate the delayed
3038 // binding.
3039 handle1.Reset();
3041 base::MessageLoop::current()->RunUntilIdle();
3043 // Still no idle sockets, still one pending connect job.
3044 EXPECT_EQ(0, pool_->IdleSocketCount());
3045 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a"));
3047 // The second socket connected, even though it was a Waiting Job.
3048 EXPECT_EQ(OK, callback.WaitForResult());
3050 // And we can see there is still one job waiting.
3051 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a"));
3053 // Finally, signal the waiting Connect.
3054 client_socket_factory_.SignalJobs();
3055 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a"));
3057 base::MessageLoop::current()->RunUntilIdle();
3060 // Cover the case where on an available socket slot, we have one pending
3061 // request that completes synchronously, thereby making the Group empty.
3062 TEST_F(ClientSocketPoolBaseTest, SynchronouslyProcessOnePendingRequest) {
3063 const int kUnlimitedSockets = 100;
3064 const int kOneSocketPerGroup = 1;
3065 CreatePool(kUnlimitedSockets, kOneSocketPerGroup);
3067 // Make the first request asynchronous fail.
3068 // This will free up a socket slot later.
3069 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob);
3071 ClientSocketHandle handle1;
3072 TestCompletionCallback callback1;
3073 EXPECT_EQ(ERR_IO_PENDING,
3074 handle1.Init("a",
3075 params_,
3076 DEFAULT_PRIORITY,
3077 callback1.callback(),
3078 pool_.get(),
3079 BoundNetLog()));
3080 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a"));
3082 // Make the second request synchronously fail. This should make the Group
3083 // empty.
3084 connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob);
3085 ClientSocketHandle handle2;
3086 TestCompletionCallback callback2;
3087 // It'll be ERR_IO_PENDING now, but the TestConnectJob will synchronously fail
3088 // when created.
3089 EXPECT_EQ(ERR_IO_PENDING,
3090 handle2.Init("a",
3091 params_,
3092 DEFAULT_PRIORITY,
3093 callback2.callback(),
3094 pool_.get(),
3095 BoundNetLog()));
3097 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a"));
3099 EXPECT_EQ(ERR_CONNECTION_FAILED, callback1.WaitForResult());
3100 EXPECT_EQ(ERR_CONNECTION_FAILED, callback2.WaitForResult());
3101 EXPECT_FALSE(pool_->HasGroup("a"));
3104 TEST_F(ClientSocketPoolBaseTest, PreferUsedSocketToUnusedSocket) {
3105 CreatePool(kDefaultMaxSockets, kDefaultMaxSockets);
3107 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
3109 ClientSocketHandle handle1;
3110 TestCompletionCallback callback1;
3111 EXPECT_EQ(ERR_IO_PENDING, handle1.Init("a",
3112 params_,
3113 DEFAULT_PRIORITY,
3114 callback1.callback(),
3115 pool_.get(),
3116 BoundNetLog()));
3118 ClientSocketHandle handle2;
3119 TestCompletionCallback callback2;
3120 EXPECT_EQ(ERR_IO_PENDING, handle2.Init("a",
3121 params_,
3122 DEFAULT_PRIORITY,
3123 callback2.callback(),
3124 pool_.get(),
3125 BoundNetLog()));
3126 ClientSocketHandle handle3;
3127 TestCompletionCallback callback3;
3128 EXPECT_EQ(ERR_IO_PENDING, handle3.Init("a",
3129 params_,
3130 DEFAULT_PRIORITY,
3131 callback3.callback(),
3132 pool_.get(),
3133 BoundNetLog()));
3135 EXPECT_EQ(OK, callback1.WaitForResult());
3136 EXPECT_EQ(OK, callback2.WaitForResult());
3137 EXPECT_EQ(OK, callback3.WaitForResult());
3139 // Use the socket.
3140 EXPECT_EQ(1, handle1.socket()->Write(NULL, 1, CompletionCallback()));
3141 EXPECT_EQ(1, handle3.socket()->Write(NULL, 1, CompletionCallback()));
3143 handle1.Reset();
3144 handle2.Reset();
3145 handle3.Reset();
3147 EXPECT_EQ(OK, handle1.Init("a",
3148 params_,
3149 DEFAULT_PRIORITY,
3150 callback1.callback(),
3151 pool_.get(),
3152 BoundNetLog()));
3153 EXPECT_EQ(OK, handle2.Init("a",
3154 params_,
3155 DEFAULT_PRIORITY,
3156 callback2.callback(),
3157 pool_.get(),
3158 BoundNetLog()));
3159 EXPECT_EQ(OK, handle3.Init("a",
3160 params_,
3161 DEFAULT_PRIORITY,
3162 callback3.callback(),
3163 pool_.get(),
3164 BoundNetLog()));
3166 EXPECT_TRUE(handle1.socket()->WasEverUsed());
3167 EXPECT_TRUE(handle2.socket()->WasEverUsed());
3168 EXPECT_FALSE(handle3.socket()->WasEverUsed());
3171 TEST_F(ClientSocketPoolBaseTest, RequestSockets) {
3172 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
3173 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
3175 pool_->RequestSockets("a", &params_, 2, BoundNetLog());
3177 ASSERT_TRUE(pool_->HasGroup("a"));
3178 EXPECT_EQ(2, pool_->NumConnectJobsInGroup("a"));
3179 EXPECT_EQ(2, pool_->NumUnassignedConnectJobsInGroup("a"));
3180 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
3182 ClientSocketHandle handle1;
3183 TestCompletionCallback callback1;
3184 EXPECT_EQ(ERR_IO_PENDING, handle1.Init("a",
3185 params_,
3186 DEFAULT_PRIORITY,
3187 callback1.callback(),
3188 pool_.get(),
3189 BoundNetLog()));
3191 ClientSocketHandle handle2;
3192 TestCompletionCallback callback2;
3193 EXPECT_EQ(ERR_IO_PENDING, handle2.Init("a",
3194 params_,
3195 DEFAULT_PRIORITY,
3196 callback2.callback(),
3197 pool_.get(),
3198 BoundNetLog()));
3200 EXPECT_EQ(2, pool_->NumConnectJobsInGroup("a"));
3201 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a"));
3202 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
3204 EXPECT_EQ(OK, callback1.WaitForResult());
3205 EXPECT_EQ(OK, callback2.WaitForResult());
3206 handle1.Reset();
3207 handle2.Reset();
3209 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a"));
3210 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a"));
3211 EXPECT_EQ(2, pool_->IdleSocketCountInGroup("a"));
3214 TEST_F(ClientSocketPoolBaseTest, RequestSocketsWhenAlreadyHaveAConnectJob) {
3215 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
3216 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
3218 ClientSocketHandle handle1;
3219 TestCompletionCallback callback1;
3220 EXPECT_EQ(ERR_IO_PENDING, handle1.Init("a",
3221 params_,
3222 DEFAULT_PRIORITY,
3223 callback1.callback(),
3224 pool_.get(),
3225 BoundNetLog()));
3227 ASSERT_TRUE(pool_->HasGroup("a"));
3228 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a"));
3229 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a"));
3230 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
3232 pool_->RequestSockets("a", &params_, 2, BoundNetLog());
3234 EXPECT_EQ(2, pool_->NumConnectJobsInGroup("a"));
3235 EXPECT_EQ(1, pool_->NumUnassignedConnectJobsInGroup("a"));
3236 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
3238 ClientSocketHandle handle2;
3239 TestCompletionCallback callback2;
3240 EXPECT_EQ(ERR_IO_PENDING, handle2.Init("a",
3241 params_,
3242 DEFAULT_PRIORITY,
3243 callback2.callback(),
3244 pool_.get(),
3245 BoundNetLog()));
3247 EXPECT_EQ(2, pool_->NumConnectJobsInGroup("a"));
3248 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a"));
3249 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
3251 EXPECT_EQ(OK, callback1.WaitForResult());
3252 EXPECT_EQ(OK, callback2.WaitForResult());
3253 handle1.Reset();
3254 handle2.Reset();
3256 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a"));
3257 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a"));
3258 EXPECT_EQ(2, pool_->IdleSocketCountInGroup("a"));
3261 TEST_F(ClientSocketPoolBaseTest,
3262 RequestSocketsWhenAlreadyHaveMultipleConnectJob) {
3263 CreatePool(4, 4);
3264 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
3266 ClientSocketHandle handle1;
3267 TestCompletionCallback callback1;
3268 EXPECT_EQ(ERR_IO_PENDING, handle1.Init("a",
3269 params_,
3270 DEFAULT_PRIORITY,
3271 callback1.callback(),
3272 pool_.get(),
3273 BoundNetLog()));
3275 ClientSocketHandle handle2;
3276 TestCompletionCallback callback2;
3277 EXPECT_EQ(ERR_IO_PENDING, handle2.Init("a",
3278 params_,
3279 DEFAULT_PRIORITY,
3280 callback2.callback(),
3281 pool_.get(),
3282 BoundNetLog()));
3284 ClientSocketHandle handle3;
3285 TestCompletionCallback callback3;
3286 EXPECT_EQ(ERR_IO_PENDING, handle3.Init("a",
3287 params_,
3288 DEFAULT_PRIORITY,
3289 callback3.callback(),
3290 pool_.get(),
3291 BoundNetLog()));
3293 ASSERT_TRUE(pool_->HasGroup("a"));
3294 EXPECT_EQ(3, pool_->NumConnectJobsInGroup("a"));
3295 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a"));
3296 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
3298 pool_->RequestSockets("a", &params_, 2, BoundNetLog());
3300 EXPECT_EQ(3, pool_->NumConnectJobsInGroup("a"));
3301 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a"));
3302 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
3304 EXPECT_EQ(OK, callback1.WaitForResult());
3305 EXPECT_EQ(OK, callback2.WaitForResult());
3306 EXPECT_EQ(OK, callback3.WaitForResult());
3307 handle1.Reset();
3308 handle2.Reset();
3309 handle3.Reset();
3311 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a"));
3312 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a"));
3313 EXPECT_EQ(3, pool_->IdleSocketCountInGroup("a"));
3316 TEST_F(ClientSocketPoolBaseTest, RequestSocketsAtMaxSocketLimit) {
3317 CreatePool(kDefaultMaxSockets, kDefaultMaxSockets);
3318 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
3320 ASSERT_FALSE(pool_->HasGroup("a"));
3322 pool_->RequestSockets("a", &params_, kDefaultMaxSockets,
3323 BoundNetLog());
3325 ASSERT_TRUE(pool_->HasGroup("a"));
3326 EXPECT_EQ(kDefaultMaxSockets, pool_->NumConnectJobsInGroup("a"));
3327 EXPECT_EQ(kDefaultMaxSockets, pool_->NumUnassignedConnectJobsInGroup("a"));
3329 ASSERT_FALSE(pool_->HasGroup("b"));
3331 pool_->RequestSockets("b", &params_, kDefaultMaxSockets,
3332 BoundNetLog());
3334 ASSERT_FALSE(pool_->HasGroup("b"));
3337 TEST_F(ClientSocketPoolBaseTest, RequestSocketsHitMaxSocketLimit) {
3338 CreatePool(kDefaultMaxSockets, kDefaultMaxSockets);
3339 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
3341 ASSERT_FALSE(pool_->HasGroup("a"));
3343 pool_->RequestSockets("a", &params_, kDefaultMaxSockets - 1,
3344 BoundNetLog());
3346 ASSERT_TRUE(pool_->HasGroup("a"));
3347 EXPECT_EQ(kDefaultMaxSockets - 1, pool_->NumConnectJobsInGroup("a"));
3348 EXPECT_EQ(kDefaultMaxSockets - 1,
3349 pool_->NumUnassignedConnectJobsInGroup("a"));
3350 EXPECT_FALSE(pool_->IsStalled());
3352 ASSERT_FALSE(pool_->HasGroup("b"));
3354 pool_->RequestSockets("b", &params_, kDefaultMaxSockets,
3355 BoundNetLog());
3357 ASSERT_TRUE(pool_->HasGroup("b"));
3358 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("b"));
3359 EXPECT_FALSE(pool_->IsStalled());
3362 TEST_F(ClientSocketPoolBaseTest, RequestSocketsCountIdleSockets) {
3363 CreatePool(4, 4);
3364 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
3366 ClientSocketHandle handle1;
3367 TestCompletionCallback callback1;
3368 EXPECT_EQ(ERR_IO_PENDING, handle1.Init("a",
3369 params_,
3370 DEFAULT_PRIORITY,
3371 callback1.callback(),
3372 pool_.get(),
3373 BoundNetLog()));
3374 ASSERT_EQ(OK, callback1.WaitForResult());
3375 handle1.Reset();
3377 ASSERT_TRUE(pool_->HasGroup("a"));
3378 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a"));
3379 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a"));
3380 EXPECT_EQ(1, pool_->IdleSocketCountInGroup("a"));
3382 pool_->RequestSockets("a", &params_, 2, BoundNetLog());
3384 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a"));
3385 EXPECT_EQ(1, pool_->NumUnassignedConnectJobsInGroup("a"));
3386 EXPECT_EQ(1, pool_->IdleSocketCountInGroup("a"));
3389 TEST_F(ClientSocketPoolBaseTest, RequestSocketsCountActiveSockets) {
3390 CreatePool(4, 4);
3391 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
3393 ClientSocketHandle handle1;
3394 TestCompletionCallback callback1;
3395 EXPECT_EQ(ERR_IO_PENDING, handle1.Init("a",
3396 params_,
3397 DEFAULT_PRIORITY,
3398 callback1.callback(),
3399 pool_.get(),
3400 BoundNetLog()));
3401 ASSERT_EQ(OK, callback1.WaitForResult());
3403 ASSERT_TRUE(pool_->HasGroup("a"));
3404 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a"));
3405 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a"));
3406 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
3407 EXPECT_EQ(1, pool_->NumActiveSocketsInGroup("a"));
3409 pool_->RequestSockets("a", &params_, 2, BoundNetLog());
3411 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a"));
3412 EXPECT_EQ(1, pool_->NumUnassignedConnectJobsInGroup("a"));
3413 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
3414 EXPECT_EQ(1, pool_->NumActiveSocketsInGroup("a"));
3417 TEST_F(ClientSocketPoolBaseTest, RequestSocketsSynchronous) {
3418 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
3419 connect_job_factory_->set_job_type(TestConnectJob::kMockJob);
3421 pool_->RequestSockets("a", &params_, kDefaultMaxSocketsPerGroup,
3422 BoundNetLog());
3424 ASSERT_TRUE(pool_->HasGroup("a"));
3425 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a"));
3426 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a"));
3427 EXPECT_EQ(kDefaultMaxSocketsPerGroup, pool_->IdleSocketCountInGroup("a"));
3429 pool_->RequestSockets("b", &params_, kDefaultMaxSocketsPerGroup,
3430 BoundNetLog());
3432 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("b"));
3433 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("b"));
3434 EXPECT_EQ(kDefaultMaxSocketsPerGroup, pool_->IdleSocketCountInGroup("b"));
3437 TEST_F(ClientSocketPoolBaseTest, RequestSocketsSynchronousError) {
3438 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
3439 connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob);
3441 pool_->RequestSockets("a", &params_, kDefaultMaxSocketsPerGroup,
3442 BoundNetLog());
3444 ASSERT_FALSE(pool_->HasGroup("a"));
3446 connect_job_factory_->set_job_type(
3447 TestConnectJob::kMockAdditionalErrorStateJob);
3448 pool_->RequestSockets("a", &params_, kDefaultMaxSocketsPerGroup,
3449 BoundNetLog());
3451 ASSERT_FALSE(pool_->HasGroup("a"));
3454 TEST_F(ClientSocketPoolBaseTest, RequestSocketsMultipleTimesDoesNothing) {
3455 CreatePool(4, 4);
3456 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
3458 pool_->RequestSockets("a", &params_, 2, BoundNetLog());
3460 ASSERT_TRUE(pool_->HasGroup("a"));
3461 EXPECT_EQ(2, pool_->NumConnectJobsInGroup("a"));
3462 EXPECT_EQ(2, pool_->NumUnassignedConnectJobsInGroup("a"));
3463 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
3465 pool_->RequestSockets("a", &params_, 2, BoundNetLog());
3466 EXPECT_EQ(2, pool_->NumConnectJobsInGroup("a"));
3467 EXPECT_EQ(2, pool_->NumUnassignedConnectJobsInGroup("a"));
3468 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
3470 ClientSocketHandle handle1;
3471 TestCompletionCallback callback1;
3472 EXPECT_EQ(ERR_IO_PENDING, handle1.Init("a",
3473 params_,
3474 DEFAULT_PRIORITY,
3475 callback1.callback(),
3476 pool_.get(),
3477 BoundNetLog()));
3478 ASSERT_EQ(OK, callback1.WaitForResult());
3480 ClientSocketHandle handle2;
3481 TestCompletionCallback callback2;
3482 int rv = handle2.Init("a",
3483 params_,
3484 DEFAULT_PRIORITY,
3485 callback2.callback(),
3486 pool_.get(),
3487 BoundNetLog());
3488 if (rv != OK) {
3489 EXPECT_EQ(ERR_IO_PENDING, rv);
3490 EXPECT_EQ(OK, callback2.WaitForResult());
3493 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a"));
3494 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a"));
3495 EXPECT_EQ(2, pool_->NumActiveSocketsInGroup("a"));
3496 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
3498 handle1.Reset();
3499 handle2.Reset();
3501 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a"));
3502 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a"));
3503 EXPECT_EQ(2, pool_->IdleSocketCountInGroup("a"));
3505 pool_->RequestSockets("a", &params_, 2, BoundNetLog());
3506 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a"));
3507 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a"));
3508 EXPECT_EQ(2, pool_->IdleSocketCountInGroup("a"));
3511 TEST_F(ClientSocketPoolBaseTest, RequestSocketsDifferentNumSockets) {
3512 CreatePool(4, 4);
3513 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
3515 pool_->RequestSockets("a", &params_, 1, BoundNetLog());
3517 ASSERT_TRUE(pool_->HasGroup("a"));
3518 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a"));
3519 EXPECT_EQ(1, pool_->NumUnassignedConnectJobsInGroup("a"));
3520 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
3522 pool_->RequestSockets("a", &params_, 2, BoundNetLog());
3523 EXPECT_EQ(2, pool_->NumConnectJobsInGroup("a"));
3524 EXPECT_EQ(2, pool_->NumUnassignedConnectJobsInGroup("a"));
3525 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
3527 pool_->RequestSockets("a", &params_, 3, BoundNetLog());
3528 EXPECT_EQ(3, pool_->NumConnectJobsInGroup("a"));
3529 EXPECT_EQ(3, pool_->NumUnassignedConnectJobsInGroup("a"));
3530 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
3532 pool_->RequestSockets("a", &params_, 1, BoundNetLog());
3533 EXPECT_EQ(3, pool_->NumConnectJobsInGroup("a"));
3534 EXPECT_EQ(3, pool_->NumUnassignedConnectJobsInGroup("a"));
3535 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
3538 TEST_F(ClientSocketPoolBaseTest, PreconnectJobsTakenByNormalRequests) {
3539 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
3540 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
3542 pool_->RequestSockets("a", &params_, 1, BoundNetLog());
3544 ASSERT_TRUE(pool_->HasGroup("a"));
3545 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a"));
3546 EXPECT_EQ(1, pool_->NumUnassignedConnectJobsInGroup("a"));
3547 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
3549 ClientSocketHandle handle1;
3550 TestCompletionCallback callback1;
3551 EXPECT_EQ(ERR_IO_PENDING, handle1.Init("a",
3552 params_,
3553 DEFAULT_PRIORITY,
3554 callback1.callback(),
3555 pool_.get(),
3556 BoundNetLog()));
3558 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a"));
3559 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a"));
3560 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
3562 ASSERT_EQ(OK, callback1.WaitForResult());
3564 // Make sure if a preconnected socket is not fully connected when a request
3565 // starts, it has a connect start time.
3566 TestLoadTimingInfoConnectedNotReused(handle1);
3567 handle1.Reset();
3569 EXPECT_EQ(1, pool_->IdleSocketCountInGroup("a"));
3572 // Checks that fully connected preconnect jobs have no connect times, and are
3573 // marked as reused.
3574 TEST_F(ClientSocketPoolBaseTest, ConnectedPreconnectJobsHaveNoConnectTimes) {
3575 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
3576 connect_job_factory_->set_job_type(TestConnectJob::kMockJob);
3577 pool_->RequestSockets("a", &params_, 1, BoundNetLog());
3579 ASSERT_TRUE(pool_->HasGroup("a"));
3580 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a"));
3581 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a"));
3582 EXPECT_EQ(1, pool_->IdleSocketCountInGroup("a"));
3584 ClientSocketHandle handle;
3585 TestCompletionCallback callback;
3586 EXPECT_EQ(OK, handle.Init("a",
3587 params_,
3588 DEFAULT_PRIORITY,
3589 callback.callback(),
3590 pool_.get(),
3591 BoundNetLog()));
3593 // Make sure the idle socket was used.
3594 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
3596 TestLoadTimingInfoConnectedReused(handle);
3597 handle.Reset();
3598 TestLoadTimingInfoNotConnected(handle);
3601 // http://crbug.com/64940 regression test.
3602 TEST_F(ClientSocketPoolBaseTest, PreconnectClosesIdleSocketRemovesGroup) {
3603 const int kMaxTotalSockets = 3;
3604 const int kMaxSocketsPerGroup = 2;
3605 CreatePool(kMaxTotalSockets, kMaxSocketsPerGroup);
3606 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
3608 // Note that group name ordering matters here. "a" comes before "b", so
3609 // CloseOneIdleSocket() will try to close "a"'s idle socket.
3611 // Set up one idle socket in "a".
3612 ClientSocketHandle handle1;
3613 TestCompletionCallback callback1;
3614 EXPECT_EQ(ERR_IO_PENDING, handle1.Init("a",
3615 params_,
3616 DEFAULT_PRIORITY,
3617 callback1.callback(),
3618 pool_.get(),
3619 BoundNetLog()));
3621 ASSERT_EQ(OK, callback1.WaitForResult());
3622 handle1.Reset();
3623 EXPECT_EQ(1, pool_->IdleSocketCountInGroup("a"));
3625 // Set up two active sockets in "b".
3626 ClientSocketHandle handle2;
3627 TestCompletionCallback callback2;
3628 EXPECT_EQ(ERR_IO_PENDING, handle1.Init("b",
3629 params_,
3630 DEFAULT_PRIORITY,
3631 callback1.callback(),
3632 pool_.get(),
3633 BoundNetLog()));
3634 EXPECT_EQ(ERR_IO_PENDING, handle2.Init("b",
3635 params_,
3636 DEFAULT_PRIORITY,
3637 callback2.callback(),
3638 pool_.get(),
3639 BoundNetLog()));
3641 ASSERT_EQ(OK, callback1.WaitForResult());
3642 ASSERT_EQ(OK, callback2.WaitForResult());
3643 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("b"));
3644 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("b"));
3645 EXPECT_EQ(2, pool_->NumActiveSocketsInGroup("b"));
3647 // Now we have 1 idle socket in "a" and 2 active sockets in "b". This means
3648 // we've maxed out on sockets, since we set |kMaxTotalSockets| to 3.
3649 // Requesting 2 preconnected sockets for "a" should fail to allocate any more
3650 // sockets for "a", and "b" should still have 2 active sockets.
3652 pool_->RequestSockets("a", &params_, 2, BoundNetLog());
3653 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a"));
3654 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a"));
3655 EXPECT_EQ(1, pool_->IdleSocketCountInGroup("a"));
3656 EXPECT_EQ(0, pool_->NumActiveSocketsInGroup("a"));
3657 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("b"));
3658 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("b"));
3659 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("b"));
3660 EXPECT_EQ(2, pool_->NumActiveSocketsInGroup("b"));
3662 // Now release the 2 active sockets for "b". This will give us 1 idle socket
3663 // in "a" and 2 idle sockets in "b". Requesting 2 preconnected sockets for
3664 // "a" should result in closing 1 for "b".
3665 handle1.Reset();
3666 handle2.Reset();
3667 EXPECT_EQ(2, pool_->IdleSocketCountInGroup("b"));
3668 EXPECT_EQ(0, pool_->NumActiveSocketsInGroup("b"));
3670 pool_->RequestSockets("a", &params_, 2, BoundNetLog());
3671 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a"));
3672 EXPECT_EQ(1, pool_->NumUnassignedConnectJobsInGroup("a"));
3673 EXPECT_EQ(1, pool_->IdleSocketCountInGroup("a"));
3674 EXPECT_EQ(0, pool_->NumActiveSocketsInGroup("a"));
3675 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("b"));
3676 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("b"));
3677 EXPECT_EQ(1, pool_->IdleSocketCountInGroup("b"));
3678 EXPECT_EQ(0, pool_->NumActiveSocketsInGroup("b"));
3681 TEST_F(ClientSocketPoolBaseTest, PreconnectWithoutBackupJob) {
3682 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
3683 pool_->EnableConnectBackupJobs();
3685 // Make the ConnectJob hang until it times out, shorten the timeout.
3686 connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob);
3687 connect_job_factory_->set_timeout_duration(
3688 base::TimeDelta::FromMilliseconds(500));
3689 pool_->RequestSockets("a", &params_, 1, BoundNetLog());
3690 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a"));
3691 EXPECT_EQ(1, pool_->NumUnassignedConnectJobsInGroup("a"));
3692 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
3694 // Verify the backup timer doesn't create a backup job, by making
3695 // the backup job a pending job instead of a waiting job, so it
3696 // *would* complete if it were created.
3697 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
3698 base::MessageLoop::current()->PostDelayedTask(
3699 FROM_HERE,
3700 base::MessageLoop::QuitClosure(),
3701 base::TimeDelta::FromSeconds(1));
3702 base::MessageLoop::current()->Run();
3703 EXPECT_FALSE(pool_->HasGroup("a"));
3706 TEST_F(ClientSocketPoolBaseTest, PreconnectWithBackupJob) {
3707 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
3708 pool_->EnableConnectBackupJobs();
3710 // Make the ConnectJob hang forever.
3711 connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob);
3712 pool_->RequestSockets("a", &params_, 1, BoundNetLog());
3713 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a"));
3714 EXPECT_EQ(1, pool_->NumUnassignedConnectJobsInGroup("a"));
3715 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
3716 base::MessageLoop::current()->RunUntilIdle();
3718 // Make the backup job be a pending job, so it completes normally.
3719 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
3720 ClientSocketHandle handle;
3721 TestCompletionCallback callback;
3722 EXPECT_EQ(ERR_IO_PENDING, handle.Init("a",
3723 params_,
3724 DEFAULT_PRIORITY,
3725 callback.callback(),
3726 pool_.get(),
3727 BoundNetLog()));
3728 // Timer has started, but the backup connect job shouldn't be created yet.
3729 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a"));
3730 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a"));
3731 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
3732 EXPECT_EQ(0, pool_->NumActiveSocketsInGroup("a"));
3733 ASSERT_EQ(OK, callback.WaitForResult());
3735 // The hung connect job should still be there, but everything else should be
3736 // complete.
3737 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a"));
3738 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a"));
3739 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
3740 EXPECT_EQ(1, pool_->NumActiveSocketsInGroup("a"));
3743 // Tests that a preconnect that starts out with unread data can still be used.
3744 // http://crbug.com/334467
3745 TEST_F(ClientSocketPoolBaseTest, PreconnectWithUnreadData) {
3746 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
3747 connect_job_factory_->set_job_type(TestConnectJob::kMockUnreadDataJob);
3749 pool_->RequestSockets("a", &params_, 1, BoundNetLog());
3751 ASSERT_TRUE(pool_->HasGroup("a"));
3752 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a"));
3753 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a"));
3754 EXPECT_EQ(1, pool_->IdleSocketCountInGroup("a"));
3756 // Fail future jobs to be sure that handle receives the preconnected socket
3757 // rather than closing it and making a new one.
3758 connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob);
3759 ClientSocketHandle handle;
3760 TestCompletionCallback callback;
3761 EXPECT_EQ(OK, handle.Init("a",
3762 params_,
3763 DEFAULT_PRIORITY,
3764 callback.callback(),
3765 pool_.get(),
3766 BoundNetLog()));
3768 ASSERT_TRUE(pool_->HasGroup("a"));
3769 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a"));
3770 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a"));
3771 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
3773 // Drain the pending read.
3774 EXPECT_EQ(1, handle.socket()->Read(NULL, 1, CompletionCallback()));
3776 TestLoadTimingInfoConnectedReused(handle);
3777 handle.Reset();
3779 // The socket should be usable now that it's idle again.
3780 EXPECT_EQ(1, pool_->IdleSocketCountInGroup("a"));
3783 class MockLayeredPool : public HigherLayeredPool {
3784 public:
3785 MockLayeredPool(TestClientSocketPool* pool,
3786 const std::string& group_name)
3787 : pool_(pool),
3788 group_name_(group_name),
3789 can_release_connection_(true) {
3790 pool_->AddHigherLayeredPool(this);
3793 ~MockLayeredPool() {
3794 pool_->RemoveHigherLayeredPool(this);
3797 int RequestSocket(TestClientSocketPool* pool) {
3798 scoped_refptr<TestSocketParams> params(
3799 new TestSocketParams(false /* ignore_limits */));
3800 return handle_.Init(group_name_, params, DEFAULT_PRIORITY,
3801 callback_.callback(), pool, BoundNetLog());
3804 int RequestSocketWithoutLimits(TestClientSocketPool* pool) {
3805 scoped_refptr<TestSocketParams> params(
3806 new TestSocketParams(true /* ignore_limits */));
3807 return handle_.Init(group_name_, params, MAXIMUM_PRIORITY,
3808 callback_.callback(), pool, BoundNetLog());
3811 bool ReleaseOneConnection() {
3812 if (!handle_.is_initialized() || !can_release_connection_) {
3813 return false;
3815 handle_.socket()->Disconnect();
3816 handle_.Reset();
3817 return true;
3820 void set_can_release_connection(bool can_release_connection) {
3821 can_release_connection_ = can_release_connection;
3824 MOCK_METHOD0(CloseOneIdleConnection, bool());
3826 private:
3827 TestClientSocketPool* const pool_;
3828 ClientSocketHandle handle_;
3829 TestCompletionCallback callback_;
3830 const std::string group_name_;
3831 bool can_release_connection_;
3834 TEST_F(ClientSocketPoolBaseTest, FailToCloseIdleSocketsNotHeldByLayeredPool) {
3835 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
3836 connect_job_factory_->set_job_type(TestConnectJob::kMockJob);
3838 MockLayeredPool mock_layered_pool(pool_.get(), "foo");
3839 EXPECT_EQ(OK, mock_layered_pool.RequestSocket(pool_.get()));
3840 EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection())
3841 .WillOnce(Return(false));
3842 EXPECT_FALSE(pool_->CloseOneIdleConnectionInHigherLayeredPool());
3845 TEST_F(ClientSocketPoolBaseTest, ForciblyCloseIdleSocketsHeldByLayeredPool) {
3846 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
3847 connect_job_factory_->set_job_type(TestConnectJob::kMockJob);
3849 MockLayeredPool mock_layered_pool(pool_.get(), "foo");
3850 EXPECT_EQ(OK, mock_layered_pool.RequestSocket(pool_.get()));
3851 EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection())
3852 .WillOnce(Invoke(&mock_layered_pool,
3853 &MockLayeredPool::ReleaseOneConnection));
3854 EXPECT_TRUE(pool_->CloseOneIdleConnectionInHigherLayeredPool());
3857 // Tests the basic case of closing an idle socket in a higher layered pool when
3858 // a new request is issued and the lower layer pool is stalled.
3859 TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketsHeldByLayeredPoolWhenNeeded) {
3860 CreatePool(1, 1);
3861 connect_job_factory_->set_job_type(TestConnectJob::kMockJob);
3863 MockLayeredPool mock_layered_pool(pool_.get(), "foo");
3864 EXPECT_EQ(OK, mock_layered_pool.RequestSocket(pool_.get()));
3865 EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection())
3866 .WillOnce(Invoke(&mock_layered_pool,
3867 &MockLayeredPool::ReleaseOneConnection));
3868 ClientSocketHandle handle;
3869 TestCompletionCallback callback;
3870 EXPECT_EQ(ERR_IO_PENDING, handle.Init("a",
3871 params_,
3872 DEFAULT_PRIORITY,
3873 callback.callback(),
3874 pool_.get(),
3875 BoundNetLog()));
3876 EXPECT_EQ(OK, callback.WaitForResult());
3879 // Same as above, but the idle socket is in the same group as the stalled
3880 // socket, and closes the only other request in its group when closing requests
3881 // in higher layered pools. This generally shouldn't happen, but it may be
3882 // possible if a higher level pool issues a request and the request is
3883 // subsequently cancelled. Even if it's not possible, best not to crash.
3884 TEST_F(ClientSocketPoolBaseTest,
3885 CloseIdleSocketsHeldByLayeredPoolWhenNeededSameGroup) {
3886 CreatePool(2, 2);
3887 connect_job_factory_->set_job_type(TestConnectJob::kMockJob);
3889 // Need a socket in another group for the pool to be stalled (If a group
3890 // has the maximum number of connections already, it's not stalled).
3891 ClientSocketHandle handle1;
3892 TestCompletionCallback callback1;
3893 EXPECT_EQ(OK, handle1.Init("group1",
3894 params_,
3895 DEFAULT_PRIORITY,
3896 callback1.callback(),
3897 pool_.get(),
3898 BoundNetLog()));
3900 MockLayeredPool mock_layered_pool(pool_.get(), "group2");
3901 EXPECT_EQ(OK, mock_layered_pool.RequestSocket(pool_.get()));
3902 EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection())
3903 .WillOnce(Invoke(&mock_layered_pool,
3904 &MockLayeredPool::ReleaseOneConnection));
3905 ClientSocketHandle handle;
3906 TestCompletionCallback callback2;
3907 EXPECT_EQ(ERR_IO_PENDING, handle.Init("group2",
3908 params_,
3909 DEFAULT_PRIORITY,
3910 callback2.callback(),
3911 pool_.get(),
3912 BoundNetLog()));
3913 EXPECT_EQ(OK, callback2.WaitForResult());
3916 // Tests the case when an idle socket can be closed when a new request is
3917 // issued, and the new request belongs to a group that was previously stalled.
3918 TEST_F(ClientSocketPoolBaseTest,
3919 CloseIdleSocketsHeldByLayeredPoolInSameGroupWhenNeeded) {
3920 CreatePool(2, 2);
3921 std::list<TestConnectJob::JobType> job_types;
3922 job_types.push_back(TestConnectJob::kMockJob);
3923 job_types.push_back(TestConnectJob::kMockJob);
3924 job_types.push_back(TestConnectJob::kMockJob);
3925 job_types.push_back(TestConnectJob::kMockJob);
3926 connect_job_factory_->set_job_types(&job_types);
3928 ClientSocketHandle handle1;
3929 TestCompletionCallback callback1;
3930 EXPECT_EQ(OK, handle1.Init("group1",
3931 params_,
3932 DEFAULT_PRIORITY,
3933 callback1.callback(),
3934 pool_.get(),
3935 BoundNetLog()));
3937 MockLayeredPool mock_layered_pool(pool_.get(), "group2");
3938 EXPECT_EQ(OK, mock_layered_pool.RequestSocket(pool_.get()));
3939 EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection())
3940 .WillRepeatedly(Invoke(&mock_layered_pool,
3941 &MockLayeredPool::ReleaseOneConnection));
3942 mock_layered_pool.set_can_release_connection(false);
3944 // The third request is made when the socket pool is in a stalled state.
3945 ClientSocketHandle handle3;
3946 TestCompletionCallback callback3;
3947 EXPECT_EQ(ERR_IO_PENDING, handle3.Init("group3",
3948 params_,
3949 DEFAULT_PRIORITY,
3950 callback3.callback(),
3951 pool_.get(),
3952 BoundNetLog()));
3954 base::RunLoop().RunUntilIdle();
3955 EXPECT_FALSE(callback3.have_result());
3957 // The fourth request is made when the pool is no longer stalled. The third
3958 // request should be serviced first, since it was issued first and has the
3959 // same priority.
3960 mock_layered_pool.set_can_release_connection(true);
3961 ClientSocketHandle handle4;
3962 TestCompletionCallback callback4;
3963 EXPECT_EQ(ERR_IO_PENDING, handle4.Init("group3",
3964 params_,
3965 DEFAULT_PRIORITY,
3966 callback4.callback(),
3967 pool_.get(),
3968 BoundNetLog()));
3969 EXPECT_EQ(OK, callback3.WaitForResult());
3970 EXPECT_FALSE(callback4.have_result());
3972 // Closing a handle should free up another socket slot.
3973 handle1.Reset();
3974 EXPECT_EQ(OK, callback4.WaitForResult());
3977 // Tests the case when an idle socket can be closed when a new request is
3978 // issued, and the new request belongs to a group that was previously stalled.
3980 // The two differences from the above test are that the stalled requests are not
3981 // in the same group as the layered pool's request, and the the fourth request
3982 // has a higher priority than the third one, so gets a socket first.
3983 TEST_F(ClientSocketPoolBaseTest,
3984 CloseIdleSocketsHeldByLayeredPoolInSameGroupWhenNeeded2) {
3985 CreatePool(2, 2);
3986 std::list<TestConnectJob::JobType> job_types;
3987 job_types.push_back(TestConnectJob::kMockJob);
3988 job_types.push_back(TestConnectJob::kMockJob);
3989 job_types.push_back(TestConnectJob::kMockJob);
3990 job_types.push_back(TestConnectJob::kMockJob);
3991 connect_job_factory_->set_job_types(&job_types);
3993 ClientSocketHandle handle1;
3994 TestCompletionCallback callback1;
3995 EXPECT_EQ(OK, handle1.Init("group1",
3996 params_,
3997 DEFAULT_PRIORITY,
3998 callback1.callback(),
3999 pool_.get(),
4000 BoundNetLog()));
4002 MockLayeredPool mock_layered_pool(pool_.get(), "group2");
4003 EXPECT_EQ(OK, mock_layered_pool.RequestSocket(pool_.get()));
4004 EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection())
4005 .WillRepeatedly(Invoke(&mock_layered_pool,
4006 &MockLayeredPool::ReleaseOneConnection));
4007 mock_layered_pool.set_can_release_connection(false);
4009 // The third request is made when the socket pool is in a stalled state.
4010 ClientSocketHandle handle3;
4011 TestCompletionCallback callback3;
4012 EXPECT_EQ(ERR_IO_PENDING, handle3.Init("group3",
4013 params_,
4014 MEDIUM,
4015 callback3.callback(),
4016 pool_.get(),
4017 BoundNetLog()));
4019 base::RunLoop().RunUntilIdle();
4020 EXPECT_FALSE(callback3.have_result());
4022 // The fourth request is made when the pool is no longer stalled. This
4023 // request has a higher priority than the third request, so is serviced first.
4024 mock_layered_pool.set_can_release_connection(true);
4025 ClientSocketHandle handle4;
4026 TestCompletionCallback callback4;
4027 EXPECT_EQ(ERR_IO_PENDING, handle4.Init("group3",
4028 params_,
4029 HIGHEST,
4030 callback4.callback(),
4031 pool_.get(),
4032 BoundNetLog()));
4033 EXPECT_EQ(OK, callback4.WaitForResult());
4034 EXPECT_FALSE(callback3.have_result());
4036 // Closing a handle should free up another socket slot.
4037 handle1.Reset();
4038 EXPECT_EQ(OK, callback3.WaitForResult());
4041 TEST_F(ClientSocketPoolBaseTest,
4042 CloseMultipleIdleSocketsHeldByLayeredPoolWhenNeeded) {
4043 CreatePool(1, 1);
4044 connect_job_factory_->set_job_type(TestConnectJob::kMockJob);
4046 MockLayeredPool mock_layered_pool1(pool_.get(), "foo");
4047 EXPECT_EQ(OK, mock_layered_pool1.RequestSocket(pool_.get()));
4048 EXPECT_CALL(mock_layered_pool1, CloseOneIdleConnection())
4049 .WillRepeatedly(Invoke(&mock_layered_pool1,
4050 &MockLayeredPool::ReleaseOneConnection));
4051 MockLayeredPool mock_layered_pool2(pool_.get(), "bar");
4052 EXPECT_EQ(OK, mock_layered_pool2.RequestSocketWithoutLimits(pool_.get()));
4053 EXPECT_CALL(mock_layered_pool2, CloseOneIdleConnection())
4054 .WillRepeatedly(Invoke(&mock_layered_pool2,
4055 &MockLayeredPool::ReleaseOneConnection));
4056 ClientSocketHandle handle;
4057 TestCompletionCallback callback;
4058 EXPECT_EQ(ERR_IO_PENDING, handle.Init("a",
4059 params_,
4060 DEFAULT_PRIORITY,
4061 callback.callback(),
4062 pool_.get(),
4063 BoundNetLog()));
4064 EXPECT_EQ(OK, callback.WaitForResult());
4067 // Test that when a socket pool and group are at their limits, a request
4068 // with |ignore_limits| triggers creation of a new socket, and gets the socket
4069 // instead of a request with the same priority that was issued earlier, but
4070 // that does not have |ignore_limits| set.
4071 TEST_F(ClientSocketPoolBaseTest, IgnoreLimits) {
4072 scoped_refptr<TestSocketParams> params_ignore_limits(
4073 new TestSocketParams(true /* ignore_limits */));
4074 CreatePool(1, 1);
4076 // Issue a request to reach the socket pool limit.
4077 EXPECT_EQ(OK, StartRequestWithParams("a", MAXIMUM_PRIORITY, params_));
4078 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a"));
4080 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
4082 EXPECT_EQ(ERR_IO_PENDING, StartRequestWithParams("a", MAXIMUM_PRIORITY,
4083 params_));
4084 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a"));
4086 EXPECT_EQ(ERR_IO_PENDING, StartRequestWithParams("a", MAXIMUM_PRIORITY,
4087 params_ignore_limits));
4088 ASSERT_EQ(1, pool_->NumConnectJobsInGroup("a"));
4090 EXPECT_EQ(OK, request(2)->WaitForResult());
4091 EXPECT_FALSE(request(1)->have_result());
4094 // Test that when a socket pool and group are at their limits, a ConnectJob
4095 // issued for a request with |ignore_limits| set is not cancelled when a request
4096 // without |ignore_limits| issued to the same group is cancelled.
4097 TEST_F(ClientSocketPoolBaseTest, IgnoreLimitsCancelOtherJob) {
4098 scoped_refptr<TestSocketParams> params_ignore_limits(
4099 new TestSocketParams(true /* ignore_limits */));
4100 CreatePool(1, 1);
4102 // Issue a request to reach the socket pool limit.
4103 EXPECT_EQ(OK, StartRequestWithParams("a", MAXIMUM_PRIORITY, params_));
4104 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a"));
4106 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
4108 EXPECT_EQ(ERR_IO_PENDING, StartRequestWithParams("a", MAXIMUM_PRIORITY,
4109 params_));
4110 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a"));
4112 EXPECT_EQ(ERR_IO_PENDING, StartRequestWithParams("a", MAXIMUM_PRIORITY,
4113 params_ignore_limits));
4114 ASSERT_EQ(1, pool_->NumConnectJobsInGroup("a"));
4116 // Cancel the pending request without ignore_limits set. The ConnectJob
4117 // should not be cancelled.
4118 request(1)->handle()->Reset();
4119 ASSERT_EQ(1, pool_->NumConnectJobsInGroup("a"));
4121 EXPECT_EQ(OK, request(2)->WaitForResult());
4122 EXPECT_FALSE(request(1)->have_result());
4125 } // namespace
4127 } // namespace net