Show Pages in chrome://md-settings
[chromium-blink-merge.git] / net / socket / client_socket_pool_base_unittest.cc
blobc7cbda1562f00cc2e417cb25b4322e92dfdcf193
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/net_log.h"
25 #include "net/base/net_log_unittest.h"
26 #include "net/base/request_priority.h"
27 #include "net/base/test_completion_callback.h"
28 #include "net/http/http_response_headers.h"
29 #include "net/socket/client_socket_factory.h"
30 #include "net/socket/client_socket_handle.h"
31 #include "net/socket/client_socket_pool_histograms.h"
32 #include "net/socket/socket_test_util.h"
33 #include "net/socket/ssl_client_socket.h"
34 #include "net/socket/stream_socket.h"
35 #include "net/udp/datagram_client_socket.h"
36 #include "testing/gmock/include/gmock/gmock.h"
37 #include "testing/gtest/include/gtest/gtest.h"
39 using ::testing::Invoke;
40 using ::testing::Return;
42 namespace net {
44 namespace {
46 const int kDefaultMaxSockets = 4;
47 const int kDefaultMaxSocketsPerGroup = 2;
49 // Make sure |handle| sets load times correctly when it has been assigned a
50 // reused socket.
51 void TestLoadTimingInfoConnectedReused(const ClientSocketHandle& handle) {
52 LoadTimingInfo load_timing_info;
53 // Only pass true in as |is_reused|, as in general, HttpStream types should
54 // have stricter concepts of reuse than socket pools.
55 EXPECT_TRUE(handle.GetLoadTimingInfo(true, &load_timing_info));
57 EXPECT_EQ(true, load_timing_info.socket_reused);
58 EXPECT_NE(NetLog::Source::kInvalidId, load_timing_info.socket_log_id);
60 ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing);
61 ExpectLoadTimingHasOnlyConnectionTimes(load_timing_info);
64 // Make sure |handle| sets load times correctly when it has been assigned a
65 // fresh socket. Also runs TestLoadTimingInfoConnectedReused, since the owner
66 // of a connection where |is_reused| is false may consider the connection
67 // reused.
68 void TestLoadTimingInfoConnectedNotReused(const ClientSocketHandle& handle) {
69 EXPECT_FALSE(handle.is_reused());
71 LoadTimingInfo load_timing_info;
72 EXPECT_TRUE(handle.GetLoadTimingInfo(false, &load_timing_info));
74 EXPECT_FALSE(load_timing_info.socket_reused);
75 EXPECT_NE(NetLog::Source::kInvalidId, load_timing_info.socket_log_id);
77 ExpectConnectTimingHasTimes(load_timing_info.connect_timing,
78 CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY);
79 ExpectLoadTimingHasOnlyConnectionTimes(load_timing_info);
81 TestLoadTimingInfoConnectedReused(handle);
84 // Make sure |handle| sets load times correctly, in the case that it does not
85 // currently have a socket.
86 void TestLoadTimingInfoNotConnected(const ClientSocketHandle& handle) {
87 // Should only be set to true once a socket is assigned, if at all.
88 EXPECT_FALSE(handle.is_reused());
90 LoadTimingInfo load_timing_info;
91 EXPECT_FALSE(handle.GetLoadTimingInfo(false, &load_timing_info));
93 EXPECT_FALSE(load_timing_info.socket_reused);
94 EXPECT_EQ(NetLog::Source::kInvalidId, load_timing_info.socket_log_id);
96 ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing);
97 ExpectLoadTimingHasOnlyConnectionTimes(load_timing_info);
100 class TestSocketParams : public base::RefCounted<TestSocketParams> {
101 public:
102 explicit TestSocketParams(bool ignore_limits)
103 : ignore_limits_(ignore_limits) {}
105 bool ignore_limits() { return ignore_limits_; }
107 private:
108 friend class base::RefCounted<TestSocketParams>;
109 ~TestSocketParams() {}
111 const bool ignore_limits_;
113 typedef ClientSocketPoolBase<TestSocketParams> TestClientSocketPoolBase;
115 class MockClientSocket : public StreamSocket {
116 public:
117 explicit MockClientSocket(net::NetLog* net_log)
118 : connected_(false),
119 has_unread_data_(false),
120 net_log_(BoundNetLog::Make(net_log, net::NetLog::SOURCE_SOCKET)),
121 was_used_to_convey_data_(false) {
124 // Sets whether the socket has unread data. If true, the next call to Read()
125 // will return 1 byte and IsConnectedAndIdle() will return false.
126 void set_has_unread_data(bool has_unread_data) {
127 has_unread_data_ = has_unread_data;
130 // Socket implementation.
131 int Read(IOBuffer* /* buf */,
132 int len,
133 const CompletionCallback& /* callback */) override {
134 if (has_unread_data_ && len > 0) {
135 has_unread_data_ = false;
136 was_used_to_convey_data_ = true;
137 return 1;
139 return ERR_UNEXPECTED;
142 int Write(IOBuffer* /* buf */,
143 int len,
144 const CompletionCallback& /* callback */) override {
145 was_used_to_convey_data_ = true;
146 return len;
148 int SetReceiveBufferSize(int32 size) override { return OK; }
149 int SetSendBufferSize(int32 size) override { return OK; }
151 // StreamSocket implementation.
152 int Connect(const CompletionCallback& callback) override {
153 connected_ = true;
154 return OK;
157 void Disconnect() override { connected_ = false; }
158 bool IsConnected() const override { return connected_; }
159 bool IsConnectedAndIdle() const override {
160 return connected_ && !has_unread_data_;
163 int GetPeerAddress(IPEndPoint* /* address */) const override {
164 return ERR_UNEXPECTED;
167 int GetLocalAddress(IPEndPoint* /* address */) const override {
168 return ERR_UNEXPECTED;
171 const BoundNetLog& NetLog() const override { return net_log_; }
173 void SetSubresourceSpeculation() override {}
174 void SetOmniboxSpeculation() override {}
175 bool WasEverUsed() const override { return was_used_to_convey_data_; }
176 bool UsingTCPFastOpen() const override { return false; }
177 bool WasNpnNegotiated() const override { return false; }
178 NextProto GetNegotiatedProtocol() const override { return kProtoUnknown; }
179 bool GetSSLInfo(SSLInfo* ssl_info) override { return false; }
181 private:
182 bool connected_;
183 bool has_unread_data_;
184 BoundNetLog net_log_;
185 bool was_used_to_convey_data_;
187 DISALLOW_COPY_AND_ASSIGN(MockClientSocket);
190 class TestConnectJob;
192 class MockClientSocketFactory : public ClientSocketFactory {
193 public:
194 MockClientSocketFactory() : allocation_count_(0) {}
196 scoped_ptr<DatagramClientSocket> CreateDatagramClientSocket(
197 DatagramSocket::BindType bind_type,
198 const RandIntCallback& rand_int_cb,
199 NetLog* net_log,
200 const NetLog::Source& source) override {
201 NOTREACHED();
202 return scoped_ptr<DatagramClientSocket>();
205 scoped_ptr<StreamSocket> CreateTransportClientSocket(
206 const AddressList& addresses,
207 NetLog* /* net_log */,
208 const NetLog::Source& /*source*/) override {
209 allocation_count_++;
210 return scoped_ptr<StreamSocket>();
213 scoped_ptr<SSLClientSocket> CreateSSLClientSocket(
214 scoped_ptr<ClientSocketHandle> transport_socket,
215 const HostPortPair& host_and_port,
216 const SSLConfig& ssl_config,
217 const SSLClientSocketContext& context) override {
218 NOTIMPLEMENTED();
219 return scoped_ptr<SSLClientSocket>();
222 void ClearSSLSessionCache() override { NOTIMPLEMENTED(); }
224 void WaitForSignal(TestConnectJob* job) { waiting_jobs_.push_back(job); }
226 void SignalJobs();
228 void SignalJob(size_t job);
230 void SetJobLoadState(size_t job, LoadState load_state);
232 int allocation_count() const { return allocation_count_; }
234 private:
235 int allocation_count_;
236 std::vector<TestConnectJob*> waiting_jobs_;
239 class TestConnectJob : public ConnectJob {
240 public:
241 enum JobType {
242 kMockJob,
243 kMockFailingJob,
244 kMockPendingJob,
245 kMockPendingFailingJob,
246 kMockWaitingJob,
247 kMockRecoverableJob,
248 kMockPendingRecoverableJob,
249 kMockAdditionalErrorStateJob,
250 kMockPendingAdditionalErrorStateJob,
251 kMockUnreadDataJob,
254 // The kMockPendingJob uses a slight delay before allowing the connect
255 // to complete.
256 static const int kPendingConnectDelay = 2;
258 TestConnectJob(JobType job_type,
259 const std::string& group_name,
260 const TestClientSocketPoolBase::Request& request,
261 base::TimeDelta timeout_duration,
262 ConnectJob::Delegate* delegate,
263 MockClientSocketFactory* client_socket_factory,
264 NetLog* net_log)
265 : ConnectJob(group_name, timeout_duration, request.priority(), delegate,
266 BoundNetLog::Make(net_log, NetLog::SOURCE_CONNECT_JOB)),
267 job_type_(job_type),
268 client_socket_factory_(client_socket_factory),
269 load_state_(LOAD_STATE_IDLE),
270 store_additional_error_state_(false),
271 weak_factory_(this) {
274 void Signal() {
275 DoConnect(waiting_success_, true /* async */, false /* recoverable */);
278 void set_load_state(LoadState load_state) { load_state_ = load_state; }
280 // From ConnectJob:
282 LoadState GetLoadState() const override { return load_state_; }
284 void GetAdditionalErrorState(ClientSocketHandle* handle) override {
285 if (store_additional_error_state_) {
286 // Set all of the additional error state fields in some way.
287 handle->set_is_ssl_error(true);
288 HttpResponseInfo info;
289 info.headers = new HttpResponseHeaders(std::string());
290 handle->set_ssl_error_response_info(info);
294 private:
295 // From ConnectJob:
297 int ConnectInternal() override {
298 AddressList ignored;
299 client_socket_factory_->CreateTransportClientSocket(
300 ignored, NULL, net::NetLog::Source());
301 SetSocket(
302 scoped_ptr<StreamSocket>(new MockClientSocket(net_log().net_log())));
303 switch (job_type_) {
304 case kMockJob:
305 return DoConnect(true /* successful */, false /* sync */,
306 false /* recoverable */);
307 case kMockFailingJob:
308 return DoConnect(false /* error */, false /* sync */,
309 false /* recoverable */);
310 case kMockPendingJob:
311 set_load_state(LOAD_STATE_CONNECTING);
313 // Depending on execution timings, posting a delayed task can result
314 // in the task getting executed the at the earliest possible
315 // opportunity or only after returning once from the message loop and
316 // then a second call into the message loop. In order to make behavior
317 // more deterministic, we change the default delay to 2ms. This should
318 // always require us to wait for the second call into the message loop.
320 // N.B. The correct fix for this and similar timing problems is to
321 // abstract time for the purpose of unittests. Unfortunately, we have
322 // a lot of third-party components that directly call the various
323 // time functions, so this change would be rather invasive.
324 base::MessageLoop::current()->PostDelayedTask(
325 FROM_HERE,
326 base::Bind(base::IgnoreResult(&TestConnectJob::DoConnect),
327 weak_factory_.GetWeakPtr(),
328 true /* successful */,
329 true /* async */,
330 false /* recoverable */),
331 base::TimeDelta::FromMilliseconds(kPendingConnectDelay));
332 return ERR_IO_PENDING;
333 case kMockPendingFailingJob:
334 set_load_state(LOAD_STATE_CONNECTING);
335 base::MessageLoop::current()->PostDelayedTask(
336 FROM_HERE,
337 base::Bind(base::IgnoreResult(&TestConnectJob::DoConnect),
338 weak_factory_.GetWeakPtr(),
339 false /* error */,
340 true /* async */,
341 false /* recoverable */),
342 base::TimeDelta::FromMilliseconds(2));
343 return ERR_IO_PENDING;
344 case kMockWaitingJob:
345 set_load_state(LOAD_STATE_CONNECTING);
346 client_socket_factory_->WaitForSignal(this);
347 waiting_success_ = true;
348 return ERR_IO_PENDING;
349 case kMockRecoverableJob:
350 return DoConnect(false /* error */, false /* sync */,
351 true /* recoverable */);
352 case kMockPendingRecoverableJob:
353 set_load_state(LOAD_STATE_CONNECTING);
354 base::MessageLoop::current()->PostDelayedTask(
355 FROM_HERE,
356 base::Bind(base::IgnoreResult(&TestConnectJob::DoConnect),
357 weak_factory_.GetWeakPtr(),
358 false /* error */,
359 true /* async */,
360 true /* recoverable */),
361 base::TimeDelta::FromMilliseconds(2));
362 return ERR_IO_PENDING;
363 case kMockAdditionalErrorStateJob:
364 store_additional_error_state_ = true;
365 return DoConnect(false /* error */, false /* sync */,
366 false /* recoverable */);
367 case kMockPendingAdditionalErrorStateJob:
368 set_load_state(LOAD_STATE_CONNECTING);
369 store_additional_error_state_ = true;
370 base::MessageLoop::current()->PostDelayedTask(
371 FROM_HERE,
372 base::Bind(base::IgnoreResult(&TestConnectJob::DoConnect),
373 weak_factory_.GetWeakPtr(),
374 false /* error */,
375 true /* async */,
376 false /* recoverable */),
377 base::TimeDelta::FromMilliseconds(2));
378 return ERR_IO_PENDING;
379 case kMockUnreadDataJob: {
380 int ret = DoConnect(true /* successful */, false /* sync */,
381 false /* recoverable */);
382 static_cast<MockClientSocket*>(socket())->set_has_unread_data(true);
383 return ret;
385 default:
386 NOTREACHED();
387 SetSocket(scoped_ptr<StreamSocket>());
388 return ERR_FAILED;
392 int DoConnect(bool succeed, bool was_async, bool recoverable) {
393 int result = OK;
394 if (succeed) {
395 socket()->Connect(CompletionCallback());
396 } else if (recoverable) {
397 result = ERR_PROXY_AUTH_REQUESTED;
398 } else {
399 result = ERR_CONNECTION_FAILED;
400 SetSocket(scoped_ptr<StreamSocket>());
403 if (was_async)
404 NotifyDelegateOfCompletion(result);
405 return result;
408 bool waiting_success_;
409 const JobType job_type_;
410 MockClientSocketFactory* const client_socket_factory_;
411 LoadState load_state_;
412 bool store_additional_error_state_;
414 base::WeakPtrFactory<TestConnectJob> weak_factory_;
416 DISALLOW_COPY_AND_ASSIGN(TestConnectJob);
419 class TestConnectJobFactory
420 : public TestClientSocketPoolBase::ConnectJobFactory {
421 public:
422 TestConnectJobFactory(MockClientSocketFactory* client_socket_factory,
423 NetLog* net_log)
424 : job_type_(TestConnectJob::kMockJob),
425 job_types_(NULL),
426 client_socket_factory_(client_socket_factory),
427 net_log_(net_log) {
430 ~TestConnectJobFactory() override {}
432 void set_job_type(TestConnectJob::JobType job_type) { job_type_ = job_type; }
434 void set_job_types(std::list<TestConnectJob::JobType>* job_types) {
435 job_types_ = job_types;
436 CHECK(!job_types_->empty());
439 void set_timeout_duration(base::TimeDelta timeout_duration) {
440 timeout_duration_ = timeout_duration;
443 // ConnectJobFactory implementation.
445 scoped_ptr<ConnectJob> NewConnectJob(
446 const std::string& group_name,
447 const TestClientSocketPoolBase::Request& request,
448 ConnectJob::Delegate* delegate) const override {
449 EXPECT_TRUE(!job_types_ || !job_types_->empty());
450 TestConnectJob::JobType job_type = job_type_;
451 if (job_types_ && !job_types_->empty()) {
452 job_type = job_types_->front();
453 job_types_->pop_front();
455 return scoped_ptr<ConnectJob>(new TestConnectJob(job_type,
456 group_name,
457 request,
458 timeout_duration_,
459 delegate,
460 client_socket_factory_,
461 net_log_));
464 base::TimeDelta ConnectionTimeout() const override {
465 return timeout_duration_;
468 private:
469 TestConnectJob::JobType job_type_;
470 std::list<TestConnectJob::JobType>* job_types_;
471 base::TimeDelta timeout_duration_;
472 MockClientSocketFactory* const client_socket_factory_;
473 NetLog* net_log_;
475 DISALLOW_COPY_AND_ASSIGN(TestConnectJobFactory);
478 class TestClientSocketPool : public ClientSocketPool {
479 public:
480 typedef TestSocketParams SocketParams;
482 TestClientSocketPool(
483 int max_sockets,
484 int max_sockets_per_group,
485 ClientSocketPoolHistograms* histograms,
486 base::TimeDelta unused_idle_socket_timeout,
487 base::TimeDelta used_idle_socket_timeout,
488 TestClientSocketPoolBase::ConnectJobFactory* connect_job_factory)
489 : base_(NULL, max_sockets, max_sockets_per_group, histograms,
490 unused_idle_socket_timeout, used_idle_socket_timeout,
491 connect_job_factory) {}
493 ~TestClientSocketPool() override {}
495 int RequestSocket(const std::string& group_name,
496 const void* params,
497 net::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 ClientSocketPoolHistograms* histograms() const override {
565 return base_.histograms();
568 const TestClientSocketPoolBase* base() const { return &base_; }
570 int NumUnassignedConnectJobsInGroup(const std::string& group_name) const {
571 return base_.NumUnassignedConnectJobsInGroup(group_name);
574 int NumConnectJobsInGroup(const std::string& group_name) const {
575 return base_.NumConnectJobsInGroup(group_name);
578 int NumActiveSocketsInGroup(const std::string& group_name) const {
579 return base_.NumActiveSocketsInGroup(group_name);
582 bool HasGroup(const std::string& group_name) const {
583 return base_.HasGroup(group_name);
586 void CleanupTimedOutIdleSockets() { base_.CleanupIdleSockets(false); }
588 void EnableConnectBackupJobs() { base_.EnableConnectBackupJobs(); }
590 bool CloseOneIdleConnectionInHigherLayeredPool() {
591 return base_.CloseOneIdleConnectionInHigherLayeredPool();
594 private:
595 TestClientSocketPoolBase base_;
597 DISALLOW_COPY_AND_ASSIGN(TestClientSocketPool);
600 } // namespace
602 namespace {
604 void MockClientSocketFactory::SignalJobs() {
605 for (std::vector<TestConnectJob*>::iterator it = waiting_jobs_.begin();
606 it != waiting_jobs_.end(); ++it) {
607 (*it)->Signal();
609 waiting_jobs_.clear();
612 void MockClientSocketFactory::SignalJob(size_t job) {
613 ASSERT_LT(job, waiting_jobs_.size());
614 waiting_jobs_[job]->Signal();
615 waiting_jobs_.erase(waiting_jobs_.begin() + job);
618 void MockClientSocketFactory::SetJobLoadState(size_t job,
619 LoadState load_state) {
620 ASSERT_LT(job, waiting_jobs_.size());
621 waiting_jobs_[job]->set_load_state(load_state);
624 class TestConnectJobDelegate : public ConnectJob::Delegate {
625 public:
626 TestConnectJobDelegate()
627 : have_result_(false), waiting_for_result_(false), result_(OK) {}
628 ~TestConnectJobDelegate() override {}
630 void OnConnectJobComplete(int result, ConnectJob* job) override {
631 result_ = result;
632 scoped_ptr<ConnectJob> owned_job(job);
633 scoped_ptr<StreamSocket> socket = owned_job->PassSocket();
634 // socket.get() should be NULL iff result != OK
635 EXPECT_EQ(socket == NULL, result != OK);
636 have_result_ = true;
637 if (waiting_for_result_)
638 base::MessageLoop::current()->Quit();
641 int WaitForResult() {
642 DCHECK(!waiting_for_result_);
643 while (!have_result_) {
644 waiting_for_result_ = true;
645 base::MessageLoop::current()->Run();
646 waiting_for_result_ = false;
648 have_result_ = false; // auto-reset for next callback
649 return result_;
652 private:
653 bool have_result_;
654 bool waiting_for_result_;
655 int result_;
658 class ClientSocketPoolBaseTest : public testing::Test {
659 protected:
660 ClientSocketPoolBaseTest()
661 : params_(new TestSocketParams(false /* ignore_limits */)),
662 histograms_("ClientSocketPoolTest") {
663 connect_backup_jobs_enabled_ =
664 internal::ClientSocketPoolBaseHelper::connect_backup_jobs_enabled();
665 internal::ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(true);
666 cleanup_timer_enabled_ =
667 internal::ClientSocketPoolBaseHelper::cleanup_timer_enabled();
670 ~ClientSocketPoolBaseTest() override {
671 internal::ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(
672 connect_backup_jobs_enabled_);
673 internal::ClientSocketPoolBaseHelper::set_cleanup_timer_enabled(
674 cleanup_timer_enabled_);
677 void CreatePool(int max_sockets, int max_sockets_per_group) {
678 CreatePoolWithIdleTimeouts(
679 max_sockets,
680 max_sockets_per_group,
681 ClientSocketPool::unused_idle_socket_timeout(),
682 ClientSocketPool::used_idle_socket_timeout());
685 void CreatePoolWithIdleTimeouts(
686 int max_sockets, int max_sockets_per_group,
687 base::TimeDelta unused_idle_socket_timeout,
688 base::TimeDelta used_idle_socket_timeout) {
689 DCHECK(!pool_.get());
690 connect_job_factory_ = new TestConnectJobFactory(&client_socket_factory_,
691 &net_log_);
692 pool_.reset(new TestClientSocketPool(max_sockets,
693 max_sockets_per_group,
694 &histograms_,
695 unused_idle_socket_timeout,
696 used_idle_socket_timeout,
697 connect_job_factory_));
700 int StartRequestWithParams(
701 const std::string& group_name,
702 RequestPriority priority,
703 const scoped_refptr<TestSocketParams>& params) {
704 return test_base_.StartRequestUsingPool(
705 pool_.get(), group_name, priority, params);
708 int StartRequest(const std::string& group_name, RequestPriority priority) {
709 return StartRequestWithParams(group_name, priority, params_);
712 int GetOrderOfRequest(size_t index) const {
713 return test_base_.GetOrderOfRequest(index);
716 bool ReleaseOneConnection(ClientSocketPoolTest::KeepAlive keep_alive) {
717 return test_base_.ReleaseOneConnection(keep_alive);
720 void ReleaseAllConnections(ClientSocketPoolTest::KeepAlive keep_alive) {
721 test_base_.ReleaseAllConnections(keep_alive);
724 TestSocketRequest* request(int i) { return test_base_.request(i); }
725 size_t requests_size() const { return test_base_.requests_size(); }
726 ScopedVector<TestSocketRequest>* requests() { return test_base_.requests(); }
727 size_t completion_count() const { return test_base_.completion_count(); }
729 CapturingNetLog net_log_;
730 bool connect_backup_jobs_enabled_;
731 bool cleanup_timer_enabled_;
732 MockClientSocketFactory client_socket_factory_;
733 TestConnectJobFactory* connect_job_factory_;
734 scoped_refptr<TestSocketParams> params_;
735 ClientSocketPoolHistograms histograms_;
736 scoped_ptr<TestClientSocketPool> pool_;
737 ClientSocketPoolTest test_base_;
740 // Even though a timeout is specified, it doesn't time out on a synchronous
741 // completion.
742 TEST_F(ClientSocketPoolBaseTest, ConnectJob_NoTimeoutOnSynchronousCompletion) {
743 TestConnectJobDelegate delegate;
744 ClientSocketHandle ignored;
745 TestClientSocketPoolBase::Request request(
746 &ignored, CompletionCallback(), DEFAULT_PRIORITY,
747 internal::ClientSocketPoolBaseHelper::NORMAL,
748 false, params_, BoundNetLog());
749 scoped_ptr<TestConnectJob> job(
750 new TestConnectJob(TestConnectJob::kMockJob,
751 "a",
752 request,
753 base::TimeDelta::FromMicroseconds(1),
754 &delegate,
755 &client_socket_factory_,
756 NULL));
757 EXPECT_EQ(OK, job->Connect());
760 TEST_F(ClientSocketPoolBaseTest, ConnectJob_TimedOut) {
761 TestConnectJobDelegate delegate;
762 ClientSocketHandle ignored;
763 CapturingNetLog log;
765 TestClientSocketPoolBase::Request request(
766 &ignored, CompletionCallback(), DEFAULT_PRIORITY,
767 internal::ClientSocketPoolBaseHelper::NORMAL,
768 false, params_, BoundNetLog());
769 // Deleted by TestConnectJobDelegate.
770 TestConnectJob* job =
771 new TestConnectJob(TestConnectJob::kMockPendingJob,
772 "a",
773 request,
774 base::TimeDelta::FromMicroseconds(1),
775 &delegate,
776 &client_socket_factory_,
777 &log);
778 ASSERT_EQ(ERR_IO_PENDING, job->Connect());
779 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(1));
780 EXPECT_EQ(ERR_TIMED_OUT, delegate.WaitForResult());
782 CapturingNetLog::CapturedEntryList entries;
783 log.GetEntries(&entries);
785 EXPECT_EQ(6u, entries.size());
786 EXPECT_TRUE(LogContainsBeginEvent(
787 entries, 0, NetLog::TYPE_SOCKET_POOL_CONNECT_JOB));
788 EXPECT_TRUE(LogContainsBeginEvent(
789 entries, 1, NetLog::TYPE_SOCKET_POOL_CONNECT_JOB_CONNECT));
790 EXPECT_TRUE(LogContainsEvent(
791 entries, 2, NetLog::TYPE_CONNECT_JOB_SET_SOCKET,
792 NetLog::PHASE_NONE));
793 EXPECT_TRUE(LogContainsEvent(
794 entries, 3, NetLog::TYPE_SOCKET_POOL_CONNECT_JOB_TIMED_OUT,
795 NetLog::PHASE_NONE));
796 EXPECT_TRUE(LogContainsEndEvent(
797 entries, 4, NetLog::TYPE_SOCKET_POOL_CONNECT_JOB_CONNECT));
798 EXPECT_TRUE(LogContainsEndEvent(
799 entries, 5, NetLog::TYPE_SOCKET_POOL_CONNECT_JOB));
802 TEST_F(ClientSocketPoolBaseTest, BasicSynchronous) {
803 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
805 TestCompletionCallback callback;
806 ClientSocketHandle handle;
807 CapturingBoundNetLog log;
808 TestLoadTimingInfoNotConnected(handle);
810 EXPECT_EQ(OK,
811 handle.Init("a",
812 params_,
813 DEFAULT_PRIORITY,
814 callback.callback(),
815 pool_.get(),
816 log.bound()));
817 EXPECT_TRUE(handle.is_initialized());
818 EXPECT_TRUE(handle.socket());
819 TestLoadTimingInfoConnectedNotReused(handle);
821 handle.Reset();
822 TestLoadTimingInfoNotConnected(handle);
824 CapturingNetLog::CapturedEntryList entries;
825 log.GetEntries(&entries);
827 EXPECT_EQ(4u, entries.size());
828 EXPECT_TRUE(LogContainsBeginEvent(
829 entries, 0, NetLog::TYPE_SOCKET_POOL));
830 EXPECT_TRUE(LogContainsEvent(
831 entries, 1, NetLog::TYPE_SOCKET_POOL_BOUND_TO_CONNECT_JOB,
832 NetLog::PHASE_NONE));
833 EXPECT_TRUE(LogContainsEvent(
834 entries, 2, NetLog::TYPE_SOCKET_POOL_BOUND_TO_SOCKET,
835 NetLog::PHASE_NONE));
836 EXPECT_TRUE(LogContainsEndEvent(
837 entries, 3, NetLog::TYPE_SOCKET_POOL));
840 TEST_F(ClientSocketPoolBaseTest, InitConnectionFailure) {
841 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
843 connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob);
844 CapturingBoundNetLog log;
846 ClientSocketHandle handle;
847 TestCompletionCallback callback;
848 // Set the additional error state members to ensure that they get cleared.
849 handle.set_is_ssl_error(true);
850 HttpResponseInfo info;
851 info.headers = new HttpResponseHeaders(std::string());
852 handle.set_ssl_error_response_info(info);
853 EXPECT_EQ(ERR_CONNECTION_FAILED,
854 handle.Init("a",
855 params_,
856 DEFAULT_PRIORITY,
857 callback.callback(),
858 pool_.get(),
859 log.bound()));
860 EXPECT_FALSE(handle.socket());
861 EXPECT_FALSE(handle.is_ssl_error());
862 EXPECT_TRUE(handle.ssl_error_response_info().headers.get() == NULL);
863 TestLoadTimingInfoNotConnected(handle);
865 CapturingNetLog::CapturedEntryList entries;
866 log.GetEntries(&entries);
868 EXPECT_EQ(3u, entries.size());
869 EXPECT_TRUE(LogContainsBeginEvent(
870 entries, 0, NetLog::TYPE_SOCKET_POOL));
871 EXPECT_TRUE(LogContainsEvent(
872 entries, 1, NetLog::TYPE_SOCKET_POOL_BOUND_TO_CONNECT_JOB,
873 NetLog::PHASE_NONE));
874 EXPECT_TRUE(LogContainsEndEvent(
875 entries, 2, NetLog::TYPE_SOCKET_POOL));
878 TEST_F(ClientSocketPoolBaseTest, TotalLimit) {
879 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
881 // TODO(eroman): Check that the NetLog contains this event.
883 EXPECT_EQ(OK, StartRequest("a", DEFAULT_PRIORITY));
884 EXPECT_EQ(OK, StartRequest("b", DEFAULT_PRIORITY));
885 EXPECT_EQ(OK, StartRequest("c", DEFAULT_PRIORITY));
886 EXPECT_EQ(OK, StartRequest("d", DEFAULT_PRIORITY));
888 EXPECT_EQ(static_cast<int>(requests_size()),
889 client_socket_factory_.allocation_count());
890 EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count());
892 EXPECT_EQ(ERR_IO_PENDING, StartRequest("e", DEFAULT_PRIORITY));
893 EXPECT_EQ(ERR_IO_PENDING, StartRequest("f", DEFAULT_PRIORITY));
894 EXPECT_EQ(ERR_IO_PENDING, StartRequest("g", DEFAULT_PRIORITY));
896 ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE);
898 EXPECT_EQ(static_cast<int>(requests_size()),
899 client_socket_factory_.allocation_count());
900 EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count());
902 EXPECT_EQ(1, GetOrderOfRequest(1));
903 EXPECT_EQ(2, GetOrderOfRequest(2));
904 EXPECT_EQ(3, GetOrderOfRequest(3));
905 EXPECT_EQ(4, GetOrderOfRequest(4));
906 EXPECT_EQ(5, GetOrderOfRequest(5));
907 EXPECT_EQ(6, GetOrderOfRequest(6));
908 EXPECT_EQ(7, GetOrderOfRequest(7));
910 // Make sure we test order of all requests made.
911 EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(8));
914 TEST_F(ClientSocketPoolBaseTest, TotalLimitReachedNewGroup) {
915 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
917 // TODO(eroman): Check that the NetLog contains this event.
919 // Reach all limits: max total sockets, and max sockets per group.
920 EXPECT_EQ(OK, StartRequest("a", DEFAULT_PRIORITY));
921 EXPECT_EQ(OK, StartRequest("a", DEFAULT_PRIORITY));
922 EXPECT_EQ(OK, StartRequest("b", DEFAULT_PRIORITY));
923 EXPECT_EQ(OK, StartRequest("b", DEFAULT_PRIORITY));
925 EXPECT_EQ(static_cast<int>(requests_size()),
926 client_socket_factory_.allocation_count());
927 EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count());
929 // Now create a new group and verify that we don't starve it.
930 EXPECT_EQ(ERR_IO_PENDING, StartRequest("c", DEFAULT_PRIORITY));
932 ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE);
934 EXPECT_EQ(static_cast<int>(requests_size()),
935 client_socket_factory_.allocation_count());
936 EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count());
938 EXPECT_EQ(1, GetOrderOfRequest(1));
939 EXPECT_EQ(2, GetOrderOfRequest(2));
940 EXPECT_EQ(3, GetOrderOfRequest(3));
941 EXPECT_EQ(4, GetOrderOfRequest(4));
942 EXPECT_EQ(5, GetOrderOfRequest(5));
944 // Make sure we test order of all requests made.
945 EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(6));
948 TEST_F(ClientSocketPoolBaseTest, TotalLimitRespectsPriority) {
949 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
951 EXPECT_EQ(OK, StartRequest("b", LOWEST));
952 EXPECT_EQ(OK, StartRequest("a", MEDIUM));
953 EXPECT_EQ(OK, StartRequest("b", HIGHEST));
954 EXPECT_EQ(OK, StartRequest("a", LOWEST));
956 EXPECT_EQ(static_cast<int>(requests_size()),
957 client_socket_factory_.allocation_count());
959 EXPECT_EQ(ERR_IO_PENDING, StartRequest("c", LOWEST));
960 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", MEDIUM));
961 EXPECT_EQ(ERR_IO_PENDING, StartRequest("b", HIGHEST));
963 ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE);
965 EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count());
967 // First 4 requests don't have to wait, and finish in order.
968 EXPECT_EQ(1, GetOrderOfRequest(1));
969 EXPECT_EQ(2, GetOrderOfRequest(2));
970 EXPECT_EQ(3, GetOrderOfRequest(3));
971 EXPECT_EQ(4, GetOrderOfRequest(4));
973 // Request ("b", HIGHEST) has the highest priority, then ("a", MEDIUM),
974 // and then ("c", LOWEST).
975 EXPECT_EQ(7, GetOrderOfRequest(5));
976 EXPECT_EQ(6, GetOrderOfRequest(6));
977 EXPECT_EQ(5, GetOrderOfRequest(7));
979 // Make sure we test order of all requests made.
980 EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(9));
983 TEST_F(ClientSocketPoolBaseTest, TotalLimitRespectsGroupLimit) {
984 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
986 EXPECT_EQ(OK, StartRequest("a", LOWEST));
987 EXPECT_EQ(OK, StartRequest("a", LOW));
988 EXPECT_EQ(OK, StartRequest("b", HIGHEST));
989 EXPECT_EQ(OK, StartRequest("b", MEDIUM));
991 EXPECT_EQ(static_cast<int>(requests_size()),
992 client_socket_factory_.allocation_count());
994 EXPECT_EQ(ERR_IO_PENDING, StartRequest("c", MEDIUM));
995 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOW));
996 EXPECT_EQ(ERR_IO_PENDING, StartRequest("b", HIGHEST));
998 ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE);
1000 EXPECT_EQ(static_cast<int>(requests_size()),
1001 client_socket_factory_.allocation_count());
1002 EXPECT_EQ(requests_size() - kDefaultMaxSockets, completion_count());
1004 // First 4 requests don't have to wait, and finish in order.
1005 EXPECT_EQ(1, GetOrderOfRequest(1));
1006 EXPECT_EQ(2, GetOrderOfRequest(2));
1007 EXPECT_EQ(3, GetOrderOfRequest(3));
1008 EXPECT_EQ(4, GetOrderOfRequest(4));
1010 // Request ("b", 7) has the highest priority, but we can't make new socket for
1011 // group "b", because it has reached the per-group limit. Then we make
1012 // socket for ("c", 6), because it has higher priority than ("a", 4),
1013 // and we still can't make a socket for group "b".
1014 EXPECT_EQ(5, GetOrderOfRequest(5));
1015 EXPECT_EQ(6, GetOrderOfRequest(6));
1016 EXPECT_EQ(7, GetOrderOfRequest(7));
1018 // Make sure we test order of all requests made.
1019 EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(8));
1022 // Make sure that we count connecting sockets against the total limit.
1023 TEST_F(ClientSocketPoolBaseTest, TotalLimitCountsConnectingSockets) {
1024 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
1026 EXPECT_EQ(OK, StartRequest("a", DEFAULT_PRIORITY));
1027 EXPECT_EQ(OK, StartRequest("b", DEFAULT_PRIORITY));
1028 EXPECT_EQ(OK, StartRequest("c", DEFAULT_PRIORITY));
1030 // Create one asynchronous request.
1031 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
1032 EXPECT_EQ(ERR_IO_PENDING, StartRequest("d", DEFAULT_PRIORITY));
1034 // We post all of our delayed tasks with a 2ms delay. I.e. they don't
1035 // actually become pending until 2ms after they have been created. In order
1036 // to flush all tasks, we need to wait so that we know there are no
1037 // soon-to-be-pending tasks waiting.
1038 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10));
1039 base::MessageLoop::current()->RunUntilIdle();
1041 // The next synchronous request should wait for its turn.
1042 connect_job_factory_->set_job_type(TestConnectJob::kMockJob);
1043 EXPECT_EQ(ERR_IO_PENDING, StartRequest("e", DEFAULT_PRIORITY));
1045 ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE);
1047 EXPECT_EQ(static_cast<int>(requests_size()),
1048 client_socket_factory_.allocation_count());
1050 EXPECT_EQ(1, GetOrderOfRequest(1));
1051 EXPECT_EQ(2, GetOrderOfRequest(2));
1052 EXPECT_EQ(3, GetOrderOfRequest(3));
1053 EXPECT_EQ(4, GetOrderOfRequest(4));
1054 EXPECT_EQ(5, GetOrderOfRequest(5));
1056 // Make sure we test order of all requests made.
1057 EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(6));
1060 TEST_F(ClientSocketPoolBaseTest, CorrectlyCountStalledGroups) {
1061 CreatePool(kDefaultMaxSockets, kDefaultMaxSockets);
1062 connect_job_factory_->set_job_type(TestConnectJob::kMockJob);
1064 EXPECT_EQ(OK, StartRequest("a", DEFAULT_PRIORITY));
1065 EXPECT_EQ(OK, StartRequest("a", DEFAULT_PRIORITY));
1066 EXPECT_EQ(OK, StartRequest("a", DEFAULT_PRIORITY));
1067 EXPECT_EQ(OK, StartRequest("a", DEFAULT_PRIORITY));
1069 connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob);
1071 EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count());
1073 EXPECT_EQ(ERR_IO_PENDING, StartRequest("b", DEFAULT_PRIORITY));
1074 EXPECT_EQ(ERR_IO_PENDING, StartRequest("c", DEFAULT_PRIORITY));
1076 EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count());
1078 EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE));
1079 EXPECT_EQ(kDefaultMaxSockets + 1, client_socket_factory_.allocation_count());
1080 EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE));
1081 EXPECT_EQ(kDefaultMaxSockets + 2, client_socket_factory_.allocation_count());
1082 EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE));
1083 EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE));
1084 EXPECT_EQ(kDefaultMaxSockets + 2, client_socket_factory_.allocation_count());
1087 TEST_F(ClientSocketPoolBaseTest, StallAndThenCancelAndTriggerAvailableSocket) {
1088 CreatePool(kDefaultMaxSockets, kDefaultMaxSockets);
1089 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
1091 ClientSocketHandle handle;
1092 TestCompletionCallback callback;
1093 EXPECT_EQ(ERR_IO_PENDING,
1094 handle.Init("a",
1095 params_,
1096 DEFAULT_PRIORITY,
1097 callback.callback(),
1098 pool_.get(),
1099 BoundNetLog()));
1101 ClientSocketHandle handles[4];
1102 for (size_t i = 0; i < arraysize(handles); ++i) {
1103 TestCompletionCallback callback;
1104 EXPECT_EQ(ERR_IO_PENDING,
1105 handles[i].Init("b",
1106 params_,
1107 DEFAULT_PRIORITY,
1108 callback.callback(),
1109 pool_.get(),
1110 BoundNetLog()));
1113 // One will be stalled, cancel all the handles now.
1114 // This should hit the OnAvailableSocketSlot() code where we previously had
1115 // stalled groups, but no longer have any.
1116 for (size_t i = 0; i < arraysize(handles); ++i)
1117 handles[i].Reset();
1120 TEST_F(ClientSocketPoolBaseTest, CancelStalledSocketAtSocketLimit) {
1121 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
1122 connect_job_factory_->set_job_type(TestConnectJob::kMockJob);
1125 ClientSocketHandle handles[kDefaultMaxSockets];
1126 TestCompletionCallback callbacks[kDefaultMaxSockets];
1127 for (int i = 0; i < kDefaultMaxSockets; ++i) {
1128 EXPECT_EQ(OK, handles[i].Init(base::IntToString(i),
1129 params_,
1130 DEFAULT_PRIORITY,
1131 callbacks[i].callback(),
1132 pool_.get(),
1133 BoundNetLog()));
1136 // Force a stalled group.
1137 ClientSocketHandle stalled_handle;
1138 TestCompletionCallback callback;
1139 EXPECT_EQ(ERR_IO_PENDING, stalled_handle.Init("foo",
1140 params_,
1141 DEFAULT_PRIORITY,
1142 callback.callback(),
1143 pool_.get(),
1144 BoundNetLog()));
1146 // Cancel the stalled request.
1147 stalled_handle.Reset();
1149 EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count());
1150 EXPECT_EQ(0, pool_->IdleSocketCount());
1152 // Dropping out of scope will close all handles and return them to idle.
1155 EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count());
1156 EXPECT_EQ(kDefaultMaxSockets, pool_->IdleSocketCount());
1159 TEST_F(ClientSocketPoolBaseTest, CancelPendingSocketAtSocketLimit) {
1160 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
1161 connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob);
1164 ClientSocketHandle handles[kDefaultMaxSockets];
1165 for (int i = 0; i < kDefaultMaxSockets; ++i) {
1166 TestCompletionCallback callback;
1167 EXPECT_EQ(ERR_IO_PENDING, handles[i].Init(base::IntToString(i),
1168 params_,
1169 DEFAULT_PRIORITY,
1170 callback.callback(),
1171 pool_.get(),
1172 BoundNetLog()));
1175 // Force a stalled group.
1176 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
1177 ClientSocketHandle stalled_handle;
1178 TestCompletionCallback callback;
1179 EXPECT_EQ(ERR_IO_PENDING, stalled_handle.Init("foo",
1180 params_,
1181 DEFAULT_PRIORITY,
1182 callback.callback(),
1183 pool_.get(),
1184 BoundNetLog()));
1186 // Since it is stalled, it should have no connect jobs.
1187 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("foo"));
1188 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("foo"));
1190 // Cancel the stalled request.
1191 handles[0].Reset();
1193 // Now we should have a connect job.
1194 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("foo"));
1195 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("foo"));
1197 // The stalled socket should connect.
1198 EXPECT_EQ(OK, callback.WaitForResult());
1200 EXPECT_EQ(kDefaultMaxSockets + 1,
1201 client_socket_factory_.allocation_count());
1202 EXPECT_EQ(0, pool_->IdleSocketCount());
1203 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("foo"));
1204 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("foo"));
1206 // Dropping out of scope will close all handles and return them to idle.
1209 EXPECT_EQ(1, pool_->IdleSocketCount());
1212 TEST_F(ClientSocketPoolBaseTest, WaitForStalledSocketAtSocketLimit) {
1213 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
1214 connect_job_factory_->set_job_type(TestConnectJob::kMockJob);
1216 ClientSocketHandle stalled_handle;
1217 TestCompletionCallback callback;
1219 EXPECT_FALSE(pool_->IsStalled());
1220 ClientSocketHandle handles[kDefaultMaxSockets];
1221 for (int i = 0; i < kDefaultMaxSockets; ++i) {
1222 TestCompletionCallback callback;
1223 EXPECT_EQ(OK, handles[i].Init(base::StringPrintf(
1224 "Take 2: %d", i),
1225 params_,
1226 DEFAULT_PRIORITY,
1227 callback.callback(),
1228 pool_.get(),
1229 BoundNetLog()));
1232 EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count());
1233 EXPECT_EQ(0, pool_->IdleSocketCount());
1234 EXPECT_FALSE(pool_->IsStalled());
1236 // Now we will hit the socket limit.
1237 EXPECT_EQ(ERR_IO_PENDING, stalled_handle.Init("foo",
1238 params_,
1239 DEFAULT_PRIORITY,
1240 callback.callback(),
1241 pool_.get(),
1242 BoundNetLog()));
1243 EXPECT_TRUE(pool_->IsStalled());
1245 // Dropping out of scope will close all handles and return them to idle.
1248 // But if we wait for it, the released idle sockets will be closed in
1249 // preference of the waiting request.
1250 EXPECT_EQ(OK, callback.WaitForResult());
1252 EXPECT_EQ(kDefaultMaxSockets + 1, client_socket_factory_.allocation_count());
1253 EXPECT_EQ(3, pool_->IdleSocketCount());
1256 // Regression test for http://crbug.com/40952.
1257 TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketAtSocketLimitDeleteGroup) {
1258 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
1259 pool_->EnableConnectBackupJobs();
1260 connect_job_factory_->set_job_type(TestConnectJob::kMockJob);
1262 for (int i = 0; i < kDefaultMaxSockets; ++i) {
1263 ClientSocketHandle handle;
1264 TestCompletionCallback callback;
1265 EXPECT_EQ(OK, handle.Init(base::IntToString(i),
1266 params_,
1267 DEFAULT_PRIORITY,
1268 callback.callback(),
1269 pool_.get(),
1270 BoundNetLog()));
1273 // Flush all the DoReleaseSocket tasks.
1274 base::MessageLoop::current()->RunUntilIdle();
1276 // Stall a group. Set a pending job so it'll trigger a backup job if we don't
1277 // reuse a socket.
1278 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
1279 ClientSocketHandle handle;
1280 TestCompletionCallback callback;
1282 // "0" is special here, since it should be the first entry in the sorted map,
1283 // which is the one which we would close an idle socket for. We shouldn't
1284 // close an idle socket though, since we should reuse the idle socket.
1285 EXPECT_EQ(OK, handle.Init("0",
1286 params_,
1287 DEFAULT_PRIORITY,
1288 callback.callback(),
1289 pool_.get(),
1290 BoundNetLog()));
1292 EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count());
1293 EXPECT_EQ(kDefaultMaxSockets - 1, pool_->IdleSocketCount());
1296 TEST_F(ClientSocketPoolBaseTest, PendingRequests) {
1297 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
1299 EXPECT_EQ(OK, StartRequest("a", DEFAULT_PRIORITY));
1300 EXPECT_EQ(OK, StartRequest("a", DEFAULT_PRIORITY));
1301 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", IDLE));
1302 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOWEST));
1303 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", MEDIUM));
1304 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", HIGHEST));
1305 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOW));
1306 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOWEST));
1308 ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE);
1310 EXPECT_EQ(kDefaultMaxSocketsPerGroup,
1311 client_socket_factory_.allocation_count());
1312 EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup,
1313 completion_count());
1315 EXPECT_EQ(1, GetOrderOfRequest(1));
1316 EXPECT_EQ(2, GetOrderOfRequest(2));
1317 EXPECT_EQ(8, GetOrderOfRequest(3));
1318 EXPECT_EQ(6, GetOrderOfRequest(4));
1319 EXPECT_EQ(4, GetOrderOfRequest(5));
1320 EXPECT_EQ(3, GetOrderOfRequest(6));
1321 EXPECT_EQ(5, GetOrderOfRequest(7));
1322 EXPECT_EQ(7, GetOrderOfRequest(8));
1324 // Make sure we test order of all requests made.
1325 EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(9));
1328 TEST_F(ClientSocketPoolBaseTest, PendingRequests_NoKeepAlive) {
1329 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
1331 EXPECT_EQ(OK, StartRequest("a", DEFAULT_PRIORITY));
1332 EXPECT_EQ(OK, StartRequest("a", DEFAULT_PRIORITY));
1333 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOWEST));
1334 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", MEDIUM));
1335 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", HIGHEST));
1336 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOW));
1337 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOWEST));
1339 ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE);
1341 for (size_t i = kDefaultMaxSocketsPerGroup; i < requests_size(); ++i)
1342 EXPECT_EQ(OK, request(i)->WaitForResult());
1344 EXPECT_EQ(static_cast<int>(requests_size()),
1345 client_socket_factory_.allocation_count());
1346 EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup,
1347 completion_count());
1350 // This test will start up a RequestSocket() and then immediately Cancel() it.
1351 // The pending connect job will be cancelled and should not call back into
1352 // ClientSocketPoolBase.
1353 TEST_F(ClientSocketPoolBaseTest, CancelRequestClearGroup) {
1354 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
1356 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
1357 ClientSocketHandle handle;
1358 TestCompletionCallback callback;
1359 EXPECT_EQ(ERR_IO_PENDING, handle.Init("a",
1360 params_,
1361 DEFAULT_PRIORITY,
1362 callback.callback(),
1363 pool_.get(),
1364 BoundNetLog()));
1365 handle.Reset();
1368 TEST_F(ClientSocketPoolBaseTest, ConnectCancelConnect) {
1369 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
1371 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
1372 ClientSocketHandle handle;
1373 TestCompletionCallback callback;
1375 EXPECT_EQ(ERR_IO_PENDING, handle.Init("a",
1376 params_,
1377 DEFAULT_PRIORITY,
1378 callback.callback(),
1379 pool_.get(),
1380 BoundNetLog()));
1382 handle.Reset();
1384 TestCompletionCallback callback2;
1385 EXPECT_EQ(ERR_IO_PENDING,
1386 handle.Init("a",
1387 params_,
1388 DEFAULT_PRIORITY,
1389 callback2.callback(),
1390 pool_.get(),
1391 BoundNetLog()));
1393 EXPECT_EQ(OK, callback2.WaitForResult());
1394 EXPECT_FALSE(callback.have_result());
1396 handle.Reset();
1399 TEST_F(ClientSocketPoolBaseTest, CancelRequest) {
1400 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
1402 EXPECT_EQ(OK, StartRequest("a", DEFAULT_PRIORITY));
1403 EXPECT_EQ(OK, StartRequest("a", DEFAULT_PRIORITY));
1404 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOWEST));
1405 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", MEDIUM));
1406 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", HIGHEST));
1407 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOW));
1408 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOWEST));
1410 // Cancel a request.
1411 size_t index_to_cancel = kDefaultMaxSocketsPerGroup + 2;
1412 EXPECT_FALSE((*requests())[index_to_cancel]->handle()->is_initialized());
1413 (*requests())[index_to_cancel]->handle()->Reset();
1415 ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE);
1417 EXPECT_EQ(kDefaultMaxSocketsPerGroup,
1418 client_socket_factory_.allocation_count());
1419 EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup - 1,
1420 completion_count());
1422 EXPECT_EQ(1, GetOrderOfRequest(1));
1423 EXPECT_EQ(2, GetOrderOfRequest(2));
1424 EXPECT_EQ(5, GetOrderOfRequest(3));
1425 EXPECT_EQ(3, GetOrderOfRequest(4));
1426 EXPECT_EQ(ClientSocketPoolTest::kRequestNotFound,
1427 GetOrderOfRequest(5)); // Canceled request.
1428 EXPECT_EQ(4, GetOrderOfRequest(6));
1429 EXPECT_EQ(6, GetOrderOfRequest(7));
1431 // Make sure we test order of all requests made.
1432 EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(8));
1435 class RequestSocketCallback : public TestCompletionCallbackBase {
1436 public:
1437 RequestSocketCallback(ClientSocketHandle* handle,
1438 TestClientSocketPool* pool,
1439 TestConnectJobFactory* test_connect_job_factory,
1440 TestConnectJob::JobType next_job_type)
1441 : handle_(handle),
1442 pool_(pool),
1443 within_callback_(false),
1444 test_connect_job_factory_(test_connect_job_factory),
1445 next_job_type_(next_job_type),
1446 callback_(base::Bind(&RequestSocketCallback::OnComplete,
1447 base::Unretained(this))) {
1450 ~RequestSocketCallback() override {}
1452 const CompletionCallback& callback() const { return callback_; }
1454 private:
1455 void OnComplete(int result) {
1456 SetResult(result);
1457 ASSERT_EQ(OK, result);
1459 if (!within_callback_) {
1460 test_connect_job_factory_->set_job_type(next_job_type_);
1462 // Don't allow reuse of the socket. Disconnect it and then release it and
1463 // run through the MessageLoop once to get it completely released.
1464 handle_->socket()->Disconnect();
1465 handle_->Reset();
1467 // TODO: Resolve conflicting intentions of stopping recursion with the
1468 // |!within_callback_| test (above) and the call to |RunUntilIdle()|
1469 // below. http://crbug.com/114130.
1470 base::MessageLoop::ScopedNestableTaskAllower allow(
1471 base::MessageLoop::current());
1472 base::MessageLoop::current()->RunUntilIdle();
1474 within_callback_ = true;
1475 TestCompletionCallback next_job_callback;
1476 scoped_refptr<TestSocketParams> params(
1477 new TestSocketParams(false /* ignore_limits */));
1478 int rv = handle_->Init("a",
1479 params,
1480 DEFAULT_PRIORITY,
1481 next_job_callback.callback(),
1482 pool_,
1483 BoundNetLog());
1484 switch (next_job_type_) {
1485 case TestConnectJob::kMockJob:
1486 EXPECT_EQ(OK, rv);
1487 break;
1488 case TestConnectJob::kMockPendingJob:
1489 EXPECT_EQ(ERR_IO_PENDING, rv);
1491 // For pending jobs, wait for new socket to be created. This makes
1492 // sure there are no more pending operations nor any unclosed sockets
1493 // when the test finishes.
1494 // We need to give it a little bit of time to run, so that all the
1495 // operations that happen on timers (e.g. cleanup of idle
1496 // connections) can execute.
1498 base::MessageLoop::ScopedNestableTaskAllower allow(
1499 base::MessageLoop::current());
1500 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10));
1501 EXPECT_EQ(OK, next_job_callback.WaitForResult());
1503 break;
1504 default:
1505 FAIL() << "Unexpected job type: " << next_job_type_;
1506 break;
1511 ClientSocketHandle* const handle_;
1512 TestClientSocketPool* const pool_;
1513 bool within_callback_;
1514 TestConnectJobFactory* const test_connect_job_factory_;
1515 TestConnectJob::JobType next_job_type_;
1516 CompletionCallback callback_;
1519 TEST_F(ClientSocketPoolBaseTest, RequestPendingJobTwice) {
1520 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
1522 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
1523 ClientSocketHandle handle;
1524 RequestSocketCallback callback(
1525 &handle, pool_.get(), connect_job_factory_,
1526 TestConnectJob::kMockPendingJob);
1527 int rv = handle.Init("a",
1528 params_,
1529 DEFAULT_PRIORITY,
1530 callback.callback(),
1531 pool_.get(),
1532 BoundNetLog());
1533 ASSERT_EQ(ERR_IO_PENDING, rv);
1535 EXPECT_EQ(OK, callback.WaitForResult());
1538 TEST_F(ClientSocketPoolBaseTest, RequestPendingJobThenSynchronous) {
1539 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
1541 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
1542 ClientSocketHandle handle;
1543 RequestSocketCallback callback(
1544 &handle, pool_.get(), connect_job_factory_, TestConnectJob::kMockJob);
1545 int rv = handle.Init("a",
1546 params_,
1547 DEFAULT_PRIORITY,
1548 callback.callback(),
1549 pool_.get(),
1550 BoundNetLog());
1551 ASSERT_EQ(ERR_IO_PENDING, rv);
1553 EXPECT_EQ(OK, callback.WaitForResult());
1556 // Make sure that pending requests get serviced after active requests get
1557 // cancelled.
1558 TEST_F(ClientSocketPoolBaseTest, CancelActiveRequestWithPendingRequests) {
1559 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
1561 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
1563 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", DEFAULT_PRIORITY));
1564 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", DEFAULT_PRIORITY));
1565 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", DEFAULT_PRIORITY));
1566 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", DEFAULT_PRIORITY));
1567 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", DEFAULT_PRIORITY));
1568 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", DEFAULT_PRIORITY));
1569 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", DEFAULT_PRIORITY));
1571 // Now, kDefaultMaxSocketsPerGroup requests should be active.
1572 // Let's cancel them.
1573 for (int i = 0; i < kDefaultMaxSocketsPerGroup; ++i) {
1574 ASSERT_FALSE(request(i)->handle()->is_initialized());
1575 request(i)->handle()->Reset();
1578 // Let's wait for the rest to complete now.
1579 for (size_t i = kDefaultMaxSocketsPerGroup; i < requests_size(); ++i) {
1580 EXPECT_EQ(OK, request(i)->WaitForResult());
1581 request(i)->handle()->Reset();
1584 EXPECT_EQ(requests_size() - kDefaultMaxSocketsPerGroup,
1585 completion_count());
1588 // Make sure that pending requests get serviced after active requests fail.
1589 TEST_F(ClientSocketPoolBaseTest, FailingActiveRequestWithPendingRequests) {
1590 const size_t kMaxSockets = 5;
1591 CreatePool(kMaxSockets, kDefaultMaxSocketsPerGroup);
1593 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob);
1595 const size_t kNumberOfRequests = 2 * kDefaultMaxSocketsPerGroup + 1;
1596 ASSERT_LE(kNumberOfRequests, kMaxSockets); // Otherwise the test will hang.
1598 // Queue up all the requests
1599 for (size_t i = 0; i < kNumberOfRequests; ++i)
1600 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", DEFAULT_PRIORITY));
1602 for (size_t i = 0; i < kNumberOfRequests; ++i)
1603 EXPECT_EQ(ERR_CONNECTION_FAILED, request(i)->WaitForResult());
1606 TEST_F(ClientSocketPoolBaseTest, CancelActiveRequestThenRequestSocket) {
1607 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
1609 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
1611 ClientSocketHandle handle;
1612 TestCompletionCallback callback;
1613 int rv = handle.Init("a",
1614 params_,
1615 DEFAULT_PRIORITY,
1616 callback.callback(),
1617 pool_.get(),
1618 BoundNetLog());
1619 EXPECT_EQ(ERR_IO_PENDING, rv);
1621 // Cancel the active request.
1622 handle.Reset();
1624 rv = handle.Init("a",
1625 params_,
1626 DEFAULT_PRIORITY,
1627 callback.callback(),
1628 pool_.get(),
1629 BoundNetLog());
1630 EXPECT_EQ(ERR_IO_PENDING, rv);
1631 EXPECT_EQ(OK, callback.WaitForResult());
1633 EXPECT_FALSE(handle.is_reused());
1634 TestLoadTimingInfoConnectedNotReused(handle);
1635 EXPECT_EQ(2, client_socket_factory_.allocation_count());
1638 // Regression test for http://crbug.com/17985.
1639 TEST_F(ClientSocketPoolBaseTest, GroupWithPendingRequestsIsNotEmpty) {
1640 const int kMaxSockets = 3;
1641 const int kMaxSocketsPerGroup = 2;
1642 CreatePool(kMaxSockets, kMaxSocketsPerGroup);
1644 const RequestPriority kHighPriority = HIGHEST;
1646 EXPECT_EQ(OK, StartRequest("a", DEFAULT_PRIORITY));
1647 EXPECT_EQ(OK, StartRequest("a", DEFAULT_PRIORITY));
1649 // This is going to be a pending request in an otherwise empty group.
1650 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", DEFAULT_PRIORITY));
1652 // Reach the maximum socket limit.
1653 EXPECT_EQ(OK, StartRequest("b", DEFAULT_PRIORITY));
1655 // Create a stalled group with high priorities.
1656 EXPECT_EQ(ERR_IO_PENDING, StartRequest("c", kHighPriority));
1657 EXPECT_EQ(ERR_IO_PENDING, StartRequest("c", kHighPriority));
1659 // Release the first two sockets from "a". Because this is a keepalive,
1660 // the first release will unblock the pending request for "a". The
1661 // second release will unblock a request for "c", becaue it is the next
1662 // high priority socket.
1663 EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE));
1664 EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::KEEP_ALIVE));
1666 // Closing idle sockets should not get us into trouble, but in the bug
1667 // we were hitting a CHECK here.
1668 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
1669 pool_->CloseIdleSockets();
1671 // Run the released socket wakeups.
1672 base::MessageLoop::current()->RunUntilIdle();
1675 TEST_F(ClientSocketPoolBaseTest, BasicAsynchronous) {
1676 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
1678 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
1679 ClientSocketHandle handle;
1680 TestCompletionCallback callback;
1681 CapturingBoundNetLog log;
1682 int rv = handle.Init("a",
1683 params_,
1684 LOWEST,
1685 callback.callback(),
1686 pool_.get(),
1687 log.bound());
1688 EXPECT_EQ(ERR_IO_PENDING, rv);
1689 EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", &handle));
1690 TestLoadTimingInfoNotConnected(handle);
1692 EXPECT_EQ(OK, callback.WaitForResult());
1693 EXPECT_TRUE(handle.is_initialized());
1694 EXPECT_TRUE(handle.socket());
1695 TestLoadTimingInfoConnectedNotReused(handle);
1697 handle.Reset();
1698 TestLoadTimingInfoNotConnected(handle);
1700 CapturingNetLog::CapturedEntryList entries;
1701 log.GetEntries(&entries);
1703 EXPECT_EQ(4u, entries.size());
1704 EXPECT_TRUE(LogContainsBeginEvent(
1705 entries, 0, NetLog::TYPE_SOCKET_POOL));
1706 EXPECT_TRUE(LogContainsEvent(
1707 entries, 1, NetLog::TYPE_SOCKET_POOL_BOUND_TO_CONNECT_JOB,
1708 NetLog::PHASE_NONE));
1709 EXPECT_TRUE(LogContainsEvent(
1710 entries, 2, NetLog::TYPE_SOCKET_POOL_BOUND_TO_SOCKET,
1711 NetLog::PHASE_NONE));
1712 EXPECT_TRUE(LogContainsEndEvent(
1713 entries, 3, NetLog::TYPE_SOCKET_POOL));
1716 TEST_F(ClientSocketPoolBaseTest,
1717 InitConnectionAsynchronousFailure) {
1718 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
1720 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob);
1721 ClientSocketHandle handle;
1722 TestCompletionCallback callback;
1723 CapturingBoundNetLog log;
1724 // Set the additional error state members to ensure that they get cleared.
1725 handle.set_is_ssl_error(true);
1726 HttpResponseInfo info;
1727 info.headers = new HttpResponseHeaders(std::string());
1728 handle.set_ssl_error_response_info(info);
1729 EXPECT_EQ(ERR_IO_PENDING, handle.Init("a",
1730 params_,
1731 DEFAULT_PRIORITY,
1732 callback.callback(),
1733 pool_.get(),
1734 log.bound()));
1735 EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", &handle));
1736 EXPECT_EQ(ERR_CONNECTION_FAILED, callback.WaitForResult());
1737 EXPECT_FALSE(handle.is_ssl_error());
1738 EXPECT_TRUE(handle.ssl_error_response_info().headers.get() == NULL);
1740 CapturingNetLog::CapturedEntryList entries;
1741 log.GetEntries(&entries);
1743 EXPECT_EQ(3u, entries.size());
1744 EXPECT_TRUE(LogContainsBeginEvent(
1745 entries, 0, NetLog::TYPE_SOCKET_POOL));
1746 EXPECT_TRUE(LogContainsEvent(
1747 entries, 1, NetLog::TYPE_SOCKET_POOL_BOUND_TO_CONNECT_JOB,
1748 NetLog::PHASE_NONE));
1749 EXPECT_TRUE(LogContainsEndEvent(
1750 entries, 2, NetLog::TYPE_SOCKET_POOL));
1753 // Check that an async ConnectJob failure does not result in creation of a new
1754 // ConnectJob when there's another pending request also waiting on its own
1755 // ConnectJob. See http://crbug.com/463960.
1756 TEST_F(ClientSocketPoolBaseTest, AsyncFailureWithPendingRequestWithJob) {
1757 CreatePool(2, 2);
1758 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob);
1760 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", DEFAULT_PRIORITY));
1761 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", DEFAULT_PRIORITY));
1763 EXPECT_EQ(ERR_CONNECTION_FAILED, request(0)->WaitForResult());
1764 EXPECT_EQ(ERR_CONNECTION_FAILED, request(1)->WaitForResult());
1766 EXPECT_EQ(2, client_socket_factory_.allocation_count());
1769 TEST_F(ClientSocketPoolBaseTest, TwoRequestsCancelOne) {
1770 // TODO(eroman): Add back the log expectations! Removed them because the
1771 // ordering is difficult, and some may fire during destructor.
1772 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
1774 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
1775 ClientSocketHandle handle;
1776 TestCompletionCallback callback;
1777 ClientSocketHandle handle2;
1778 TestCompletionCallback callback2;
1780 EXPECT_EQ(ERR_IO_PENDING,
1781 handle.Init("a",
1782 params_,
1783 DEFAULT_PRIORITY,
1784 callback.callback(),
1785 pool_.get(),
1786 BoundNetLog()));
1787 CapturingBoundNetLog log2;
1788 EXPECT_EQ(ERR_IO_PENDING,
1789 handle2.Init("a",
1790 params_,
1791 DEFAULT_PRIORITY,
1792 callback2.callback(),
1793 pool_.get(),
1794 BoundNetLog()));
1796 handle.Reset();
1799 // At this point, request 2 is just waiting for the connect job to finish.
1801 EXPECT_EQ(OK, callback2.WaitForResult());
1802 handle2.Reset();
1804 // Now request 2 has actually finished.
1805 // TODO(eroman): Add back log expectations.
1808 TEST_F(ClientSocketPoolBaseTest, CancelRequestLimitsJobs) {
1809 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
1811 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
1813 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOWEST));
1814 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOW));
1815 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", MEDIUM));
1816 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", HIGHEST));
1818 EXPECT_EQ(kDefaultMaxSocketsPerGroup, pool_->NumConnectJobsInGroup("a"));
1819 (*requests())[2]->handle()->Reset();
1820 (*requests())[3]->handle()->Reset();
1821 EXPECT_EQ(kDefaultMaxSocketsPerGroup, pool_->NumConnectJobsInGroup("a"));
1823 (*requests())[1]->handle()->Reset();
1824 EXPECT_EQ(kDefaultMaxSocketsPerGroup, pool_->NumConnectJobsInGroup("a"));
1826 (*requests())[0]->handle()->Reset();
1827 EXPECT_EQ(kDefaultMaxSocketsPerGroup, pool_->NumConnectJobsInGroup("a"));
1830 // When requests and ConnectJobs are not coupled, the request will get serviced
1831 // by whatever comes first.
1832 TEST_F(ClientSocketPoolBaseTest, ReleaseSockets) {
1833 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
1835 // Start job 1 (async OK)
1836 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
1838 std::vector<TestSocketRequest*> request_order;
1839 size_t completion_count; // unused
1840 TestSocketRequest req1(&request_order, &completion_count);
1841 int rv = req1.handle()->Init("a",
1842 params_,
1843 DEFAULT_PRIORITY,
1844 req1.callback(), pool_.get(),
1845 BoundNetLog());
1846 EXPECT_EQ(ERR_IO_PENDING, rv);
1847 EXPECT_EQ(OK, req1.WaitForResult());
1849 // Job 1 finished OK. Start job 2 (also async OK). Request 3 is pending
1850 // without a job.
1851 connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob);
1853 TestSocketRequest req2(&request_order, &completion_count);
1854 rv = req2.handle()->Init("a",
1855 params_,
1856 DEFAULT_PRIORITY,
1857 req2.callback(),
1858 pool_.get(),
1859 BoundNetLog());
1860 EXPECT_EQ(ERR_IO_PENDING, rv);
1861 TestSocketRequest req3(&request_order, &completion_count);
1862 rv = req3.handle()->Init("a",
1863 params_,
1864 DEFAULT_PRIORITY,
1865 req3.callback(),
1866 pool_.get(),
1867 BoundNetLog());
1868 EXPECT_EQ(ERR_IO_PENDING, rv);
1870 // Both Requests 2 and 3 are pending. We release socket 1 which should
1871 // service request 2. Request 3 should still be waiting.
1872 req1.handle()->Reset();
1873 // Run the released socket wakeups.
1874 base::MessageLoop::current()->RunUntilIdle();
1875 ASSERT_TRUE(req2.handle()->socket());
1876 EXPECT_EQ(OK, req2.WaitForResult());
1877 EXPECT_FALSE(req3.handle()->socket());
1879 // Signal job 2, which should service request 3.
1881 client_socket_factory_.SignalJobs();
1882 EXPECT_EQ(OK, req3.WaitForResult());
1884 ASSERT_EQ(3U, request_order.size());
1885 EXPECT_EQ(&req1, request_order[0]);
1886 EXPECT_EQ(&req2, request_order[1]);
1887 EXPECT_EQ(&req3, request_order[2]);
1888 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
1891 // The requests are not coupled to the jobs. So, the requests should finish in
1892 // their priority / insertion order.
1893 TEST_F(ClientSocketPoolBaseTest, PendingJobCompletionOrder) {
1894 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
1895 // First two jobs are async.
1896 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob);
1898 std::vector<TestSocketRequest*> request_order;
1899 size_t completion_count; // unused
1900 TestSocketRequest req1(&request_order, &completion_count);
1901 int rv = req1.handle()->Init("a",
1902 params_,
1903 DEFAULT_PRIORITY,
1904 req1.callback(),
1905 pool_.get(),
1906 BoundNetLog());
1907 EXPECT_EQ(ERR_IO_PENDING, rv);
1909 TestSocketRequest req2(&request_order, &completion_count);
1910 rv = req2.handle()->Init("a",
1911 params_,
1912 DEFAULT_PRIORITY,
1913 req2.callback(),
1914 pool_.get(),
1915 BoundNetLog());
1916 EXPECT_EQ(ERR_IO_PENDING, rv);
1918 // The pending job is sync.
1919 connect_job_factory_->set_job_type(TestConnectJob::kMockJob);
1921 TestSocketRequest req3(&request_order, &completion_count);
1922 rv = req3.handle()->Init("a",
1923 params_,
1924 DEFAULT_PRIORITY,
1925 req3.callback(),
1926 pool_.get(),
1927 BoundNetLog());
1928 EXPECT_EQ(ERR_IO_PENDING, rv);
1930 EXPECT_EQ(ERR_CONNECTION_FAILED, req1.WaitForResult());
1931 EXPECT_EQ(OK, req2.WaitForResult());
1932 EXPECT_EQ(ERR_CONNECTION_FAILED, req3.WaitForResult());
1934 ASSERT_EQ(3U, request_order.size());
1935 EXPECT_EQ(&req1, request_order[0]);
1936 EXPECT_EQ(&req2, request_order[1]);
1937 EXPECT_EQ(&req3, request_order[2]);
1940 // Test GetLoadState in the case there's only one socket request.
1941 TEST_F(ClientSocketPoolBaseTest, LoadStateOneRequest) {
1942 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
1943 connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob);
1945 ClientSocketHandle handle;
1946 TestCompletionCallback callback;
1947 int rv = handle.Init("a",
1948 params_,
1949 DEFAULT_PRIORITY,
1950 callback.callback(),
1951 pool_.get(),
1952 BoundNetLog());
1953 EXPECT_EQ(ERR_IO_PENDING, rv);
1954 EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState());
1956 client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE);
1957 EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle.GetLoadState());
1959 // No point in completing the connection, since ClientSocketHandles only
1960 // expect the LoadState to be checked while connecting.
1963 // Test GetLoadState in the case there are two socket requests.
1964 TEST_F(ClientSocketPoolBaseTest, LoadStateTwoRequests) {
1965 CreatePool(2, 2);
1966 connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob);
1968 ClientSocketHandle handle;
1969 TestCompletionCallback callback;
1970 int rv = handle.Init("a",
1971 params_,
1972 DEFAULT_PRIORITY,
1973 callback.callback(),
1974 pool_.get(),
1975 BoundNetLog());
1976 EXPECT_EQ(ERR_IO_PENDING, rv);
1978 ClientSocketHandle handle2;
1979 TestCompletionCallback callback2;
1980 rv = handle2.Init("a",
1981 params_,
1982 DEFAULT_PRIORITY,
1983 callback2.callback(),
1984 pool_.get(),
1985 BoundNetLog());
1986 EXPECT_EQ(ERR_IO_PENDING, rv);
1988 // If the first Job is in an earlier state than the second, the state of
1989 // the second job should be used for both handles.
1990 client_socket_factory_.SetJobLoadState(0, LOAD_STATE_RESOLVING_HOST);
1991 EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState());
1992 EXPECT_EQ(LOAD_STATE_CONNECTING, handle2.GetLoadState());
1994 // If the second Job is in an earlier state than the second, the state of
1995 // the first job should be used for both handles.
1996 client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE);
1997 // One request is farther
1998 EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle.GetLoadState());
1999 EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle2.GetLoadState());
2001 // Farthest along job connects and the first request gets the socket. The
2002 // second handle switches to the state of the remaining ConnectJob.
2003 client_socket_factory_.SignalJob(0);
2004 EXPECT_EQ(OK, callback.WaitForResult());
2005 EXPECT_EQ(LOAD_STATE_CONNECTING, handle2.GetLoadState());
2008 // Test GetLoadState in the case the per-group limit is reached.
2009 TEST_F(ClientSocketPoolBaseTest, LoadStateGroupLimit) {
2010 CreatePool(2, 1);
2011 connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob);
2013 ClientSocketHandle handle;
2014 TestCompletionCallback callback;
2015 int rv = handle.Init("a",
2016 params_,
2017 MEDIUM,
2018 callback.callback(),
2019 pool_.get(),
2020 BoundNetLog());
2021 EXPECT_EQ(ERR_IO_PENDING, rv);
2022 EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState());
2024 // Request another socket from the same pool, buth with a higher priority.
2025 // The first request should now be stalled at the socket group limit.
2026 ClientSocketHandle handle2;
2027 TestCompletionCallback callback2;
2028 rv = handle2.Init("a",
2029 params_,
2030 HIGHEST,
2031 callback2.callback(),
2032 pool_.get(),
2033 BoundNetLog());
2034 EXPECT_EQ(ERR_IO_PENDING, rv);
2035 EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET, handle.GetLoadState());
2036 EXPECT_EQ(LOAD_STATE_CONNECTING, handle2.GetLoadState());
2038 // The first handle should remain stalled as the other socket goes through
2039 // the connect process.
2041 client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE);
2042 EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET, handle.GetLoadState());
2043 EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle2.GetLoadState());
2045 client_socket_factory_.SignalJob(0);
2046 EXPECT_EQ(OK, callback2.WaitForResult());
2047 EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET, handle.GetLoadState());
2049 // Closing the second socket should cause the stalled handle to finally get a
2050 // ConnectJob.
2051 handle2.socket()->Disconnect();
2052 handle2.Reset();
2053 EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState());
2056 // Test GetLoadState in the case the per-pool limit is reached.
2057 TEST_F(ClientSocketPoolBaseTest, LoadStatePoolLimit) {
2058 CreatePool(2, 2);
2059 connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob);
2061 ClientSocketHandle handle;
2062 TestCompletionCallback callback;
2063 int rv = handle.Init("a",
2064 params_,
2065 DEFAULT_PRIORITY,
2066 callback.callback(),
2067 pool_.get(),
2068 BoundNetLog());
2069 EXPECT_EQ(ERR_IO_PENDING, rv);
2071 // Request for socket from another pool.
2072 ClientSocketHandle handle2;
2073 TestCompletionCallback callback2;
2074 rv = handle2.Init("b",
2075 params_,
2076 DEFAULT_PRIORITY,
2077 callback2.callback(),
2078 pool_.get(),
2079 BoundNetLog());
2080 EXPECT_EQ(ERR_IO_PENDING, rv);
2082 // Request another socket from the first pool. Request should stall at the
2083 // socket pool limit.
2084 ClientSocketHandle handle3;
2085 TestCompletionCallback callback3;
2086 rv = handle3.Init("a",
2087 params_,
2088 DEFAULT_PRIORITY,
2089 callback2.callback(),
2090 pool_.get(),
2091 BoundNetLog());
2092 EXPECT_EQ(ERR_IO_PENDING, rv);
2094 // The third handle should remain stalled as the other sockets in its group
2095 // goes through the connect process.
2097 EXPECT_EQ(LOAD_STATE_CONNECTING, handle.GetLoadState());
2098 EXPECT_EQ(LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL, handle3.GetLoadState());
2100 client_socket_factory_.SetJobLoadState(0, LOAD_STATE_SSL_HANDSHAKE);
2101 EXPECT_EQ(LOAD_STATE_SSL_HANDSHAKE, handle.GetLoadState());
2102 EXPECT_EQ(LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL, handle3.GetLoadState());
2104 client_socket_factory_.SignalJob(0);
2105 EXPECT_EQ(OK, callback.WaitForResult());
2106 EXPECT_EQ(LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL, handle3.GetLoadState());
2108 // Closing a socket should allow the stalled handle to finally get a new
2109 // ConnectJob.
2110 handle.socket()->Disconnect();
2111 handle.Reset();
2112 EXPECT_EQ(LOAD_STATE_CONNECTING, handle3.GetLoadState());
2115 TEST_F(ClientSocketPoolBaseTest, Recoverable) {
2116 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
2117 connect_job_factory_->set_job_type(TestConnectJob::kMockRecoverableJob);
2119 ClientSocketHandle handle;
2120 TestCompletionCallback callback;
2121 EXPECT_EQ(ERR_PROXY_AUTH_REQUESTED,
2122 handle.Init("a", params_, DEFAULT_PRIORITY, callback.callback(),
2123 pool_.get(), BoundNetLog()));
2124 EXPECT_TRUE(handle.is_initialized());
2125 EXPECT_TRUE(handle.socket());
2128 TEST_F(ClientSocketPoolBaseTest, AsyncRecoverable) {
2129 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
2131 connect_job_factory_->set_job_type(
2132 TestConnectJob::kMockPendingRecoverableJob);
2133 ClientSocketHandle handle;
2134 TestCompletionCallback callback;
2135 EXPECT_EQ(ERR_IO_PENDING,
2136 handle.Init("a",
2137 params_,
2138 DEFAULT_PRIORITY,
2139 callback.callback(),
2140 pool_.get(),
2141 BoundNetLog()));
2142 EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", &handle));
2143 EXPECT_EQ(ERR_PROXY_AUTH_REQUESTED, callback.WaitForResult());
2144 EXPECT_TRUE(handle.is_initialized());
2145 EXPECT_TRUE(handle.socket());
2148 TEST_F(ClientSocketPoolBaseTest, AdditionalErrorStateSynchronous) {
2149 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
2150 connect_job_factory_->set_job_type(
2151 TestConnectJob::kMockAdditionalErrorStateJob);
2153 ClientSocketHandle handle;
2154 TestCompletionCallback callback;
2155 EXPECT_EQ(ERR_CONNECTION_FAILED,
2156 handle.Init("a",
2157 params_,
2158 DEFAULT_PRIORITY,
2159 callback.callback(),
2160 pool_.get(),
2161 BoundNetLog()));
2162 EXPECT_FALSE(handle.is_initialized());
2163 EXPECT_FALSE(handle.socket());
2164 EXPECT_TRUE(handle.is_ssl_error());
2165 EXPECT_FALSE(handle.ssl_error_response_info().headers.get() == NULL);
2168 TEST_F(ClientSocketPoolBaseTest, AdditionalErrorStateAsynchronous) {
2169 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
2171 connect_job_factory_->set_job_type(
2172 TestConnectJob::kMockPendingAdditionalErrorStateJob);
2173 ClientSocketHandle handle;
2174 TestCompletionCallback callback;
2175 EXPECT_EQ(ERR_IO_PENDING,
2176 handle.Init("a",
2177 params_,
2178 DEFAULT_PRIORITY,
2179 callback.callback(),
2180 pool_.get(),
2181 BoundNetLog()));
2182 EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", &handle));
2183 EXPECT_EQ(ERR_CONNECTION_FAILED, callback.WaitForResult());
2184 EXPECT_FALSE(handle.is_initialized());
2185 EXPECT_FALSE(handle.socket());
2186 EXPECT_TRUE(handle.is_ssl_error());
2187 EXPECT_FALSE(handle.ssl_error_response_info().headers.get() == NULL);
2190 // Make sure we can reuse sockets when the cleanup timer is disabled.
2191 TEST_F(ClientSocketPoolBaseTest, DisableCleanupTimerReuse) {
2192 // Disable cleanup timer.
2193 internal::ClientSocketPoolBaseHelper::set_cleanup_timer_enabled(false);
2195 CreatePoolWithIdleTimeouts(
2196 kDefaultMaxSockets, kDefaultMaxSocketsPerGroup,
2197 base::TimeDelta(), // Time out unused sockets immediately.
2198 base::TimeDelta::FromDays(1)); // Don't time out used sockets.
2200 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
2202 ClientSocketHandle handle;
2203 TestCompletionCallback callback;
2204 int rv = handle.Init("a",
2205 params_,
2206 LOWEST,
2207 callback.callback(),
2208 pool_.get(),
2209 BoundNetLog());
2210 ASSERT_EQ(ERR_IO_PENDING, rv);
2211 EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", &handle));
2212 ASSERT_EQ(OK, callback.WaitForResult());
2214 // Use and release the socket.
2215 EXPECT_EQ(1, handle.socket()->Write(NULL, 1, CompletionCallback()));
2216 TestLoadTimingInfoConnectedNotReused(handle);
2217 handle.Reset();
2219 // Should now have one idle socket.
2220 ASSERT_EQ(1, pool_->IdleSocketCount());
2222 // Request a new socket. This should reuse the old socket and complete
2223 // synchronously.
2224 CapturingBoundNetLog log;
2225 rv = handle.Init("a",
2226 params_,
2227 LOWEST,
2228 CompletionCallback(),
2229 pool_.get(),
2230 log.bound());
2231 ASSERT_EQ(OK, rv);
2232 EXPECT_TRUE(handle.is_reused());
2233 TestLoadTimingInfoConnectedReused(handle);
2235 ASSERT_TRUE(pool_->HasGroup("a"));
2236 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
2237 EXPECT_EQ(1, pool_->NumActiveSocketsInGroup("a"));
2239 CapturingNetLog::CapturedEntryList entries;
2240 log.GetEntries(&entries);
2241 EXPECT_TRUE(LogContainsEntryWithType(
2242 entries, 1, NetLog::TYPE_SOCKET_POOL_REUSED_AN_EXISTING_SOCKET));
2245 // Make sure we cleanup old unused sockets when the cleanup timer is disabled.
2246 TEST_F(ClientSocketPoolBaseTest, DisableCleanupTimerNoReuse) {
2247 // Disable cleanup timer.
2248 internal::ClientSocketPoolBaseHelper::set_cleanup_timer_enabled(false);
2250 CreatePoolWithIdleTimeouts(
2251 kDefaultMaxSockets, kDefaultMaxSocketsPerGroup,
2252 base::TimeDelta(), // Time out unused sockets immediately
2253 base::TimeDelta()); // Time out used sockets immediately
2255 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
2257 // Startup two mock pending connect jobs, which will sit in the MessageLoop.
2259 ClientSocketHandle handle;
2260 TestCompletionCallback callback;
2261 int rv = handle.Init("a",
2262 params_,
2263 LOWEST,
2264 callback.callback(),
2265 pool_.get(),
2266 BoundNetLog());
2267 ASSERT_EQ(ERR_IO_PENDING, rv);
2268 EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", &handle));
2270 ClientSocketHandle handle2;
2271 TestCompletionCallback callback2;
2272 rv = handle2.Init("a",
2273 params_,
2274 LOWEST,
2275 callback2.callback(),
2276 pool_.get(),
2277 BoundNetLog());
2278 ASSERT_EQ(ERR_IO_PENDING, rv);
2279 EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", &handle2));
2281 // Cancel one of the requests. Wait for the other, which will get the first
2282 // job. Release the socket. Run the loop again to make sure the second
2283 // socket is sitting idle and the first one is released (since ReleaseSocket()
2284 // just posts a DoReleaseSocket() task).
2286 handle.Reset();
2287 ASSERT_EQ(OK, callback2.WaitForResult());
2288 // Use the socket.
2289 EXPECT_EQ(1, handle2.socket()->Write(NULL, 1, CompletionCallback()));
2290 handle2.Reset();
2292 // We post all of our delayed tasks with a 2ms delay. I.e. they don't
2293 // actually become pending until 2ms after they have been created. In order
2294 // to flush all tasks, we need to wait so that we know there are no
2295 // soon-to-be-pending tasks waiting.
2296 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10));
2297 base::MessageLoop::current()->RunUntilIdle();
2299 // Both sockets should now be idle.
2300 ASSERT_EQ(2, pool_->IdleSocketCount());
2302 // Request a new socket. This should cleanup the unused and timed out ones.
2303 // A new socket will be created rather than reusing the idle one.
2304 CapturingBoundNetLog log;
2305 TestCompletionCallback callback3;
2306 rv = handle.Init("a",
2307 params_,
2308 LOWEST,
2309 callback3.callback(),
2310 pool_.get(),
2311 log.bound());
2312 ASSERT_EQ(ERR_IO_PENDING, rv);
2313 ASSERT_EQ(OK, callback3.WaitForResult());
2314 EXPECT_FALSE(handle.is_reused());
2316 // Make sure the idle socket is closed.
2317 ASSERT_TRUE(pool_->HasGroup("a"));
2318 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
2319 EXPECT_EQ(1, pool_->NumActiveSocketsInGroup("a"));
2321 CapturingNetLog::CapturedEntryList entries;
2322 log.GetEntries(&entries);
2323 EXPECT_FALSE(LogContainsEntryWithType(
2324 entries, 1, NetLog::TYPE_SOCKET_POOL_REUSED_AN_EXISTING_SOCKET));
2327 TEST_F(ClientSocketPoolBaseTest, CleanupTimedOutIdleSockets) {
2328 CreatePoolWithIdleTimeouts(
2329 kDefaultMaxSockets, kDefaultMaxSocketsPerGroup,
2330 base::TimeDelta(), // Time out unused sockets immediately.
2331 base::TimeDelta::FromDays(1)); // Don't time out used sockets.
2333 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
2335 // Startup two mock pending connect jobs, which will sit in the MessageLoop.
2337 ClientSocketHandle handle;
2338 TestCompletionCallback callback;
2339 int rv = handle.Init("a",
2340 params_,
2341 LOWEST,
2342 callback.callback(),
2343 pool_.get(),
2344 BoundNetLog());
2345 EXPECT_EQ(ERR_IO_PENDING, rv);
2346 EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", &handle));
2348 ClientSocketHandle handle2;
2349 TestCompletionCallback callback2;
2350 rv = handle2.Init("a",
2351 params_,
2352 LOWEST,
2353 callback2.callback(),
2354 pool_.get(),
2355 BoundNetLog());
2356 EXPECT_EQ(ERR_IO_PENDING, rv);
2357 EXPECT_EQ(LOAD_STATE_CONNECTING, pool_->GetLoadState("a", &handle2));
2359 // Cancel one of the requests. Wait for the other, which will get the first
2360 // job. Release the socket. Run the loop again to make sure the second
2361 // socket is sitting idle and the first one is released (since ReleaseSocket()
2362 // just posts a DoReleaseSocket() task).
2364 handle.Reset();
2365 EXPECT_EQ(OK, callback2.WaitForResult());
2366 // Use the socket.
2367 EXPECT_EQ(1, handle2.socket()->Write(NULL, 1, CompletionCallback()));
2368 handle2.Reset();
2370 // We post all of our delayed tasks with a 2ms delay. I.e. they don't
2371 // actually become pending until 2ms after they have been created. In order
2372 // to flush all tasks, we need to wait so that we know there are no
2373 // soon-to-be-pending tasks waiting.
2374 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10));
2375 base::MessageLoop::current()->RunUntilIdle();
2377 ASSERT_EQ(2, pool_->IdleSocketCount());
2379 // Invoke the idle socket cleanup check. Only one socket should be left, the
2380 // used socket. Request it to make sure that it's used.
2382 pool_->CleanupTimedOutIdleSockets();
2383 CapturingBoundNetLog log;
2384 rv = handle.Init("a",
2385 params_,
2386 LOWEST,
2387 callback.callback(),
2388 pool_.get(),
2389 log.bound());
2390 EXPECT_EQ(OK, rv);
2391 EXPECT_TRUE(handle.is_reused());
2393 CapturingNetLog::CapturedEntryList entries;
2394 log.GetEntries(&entries);
2395 EXPECT_TRUE(LogContainsEntryWithType(
2396 entries, 1, NetLog::TYPE_SOCKET_POOL_REUSED_AN_EXISTING_SOCKET));
2399 // Make sure that we process all pending requests even when we're stalling
2400 // because of multiple releasing disconnected sockets.
2401 TEST_F(ClientSocketPoolBaseTest, MultipleReleasingDisconnectedSockets) {
2402 CreatePoolWithIdleTimeouts(
2403 kDefaultMaxSockets, kDefaultMaxSocketsPerGroup,
2404 base::TimeDelta(), // Time out unused sockets immediately.
2405 base::TimeDelta::FromDays(1)); // Don't time out used sockets.
2407 connect_job_factory_->set_job_type(TestConnectJob::kMockJob);
2409 // Startup 4 connect jobs. Two of them will be pending.
2411 ClientSocketHandle handle;
2412 TestCompletionCallback callback;
2413 int rv = handle.Init("a",
2414 params_,
2415 LOWEST,
2416 callback.callback(),
2417 pool_.get(),
2418 BoundNetLog());
2419 EXPECT_EQ(OK, rv);
2421 ClientSocketHandle handle2;
2422 TestCompletionCallback callback2;
2423 rv = handle2.Init("a",
2424 params_,
2425 LOWEST,
2426 callback2.callback(),
2427 pool_.get(),
2428 BoundNetLog());
2429 EXPECT_EQ(OK, rv);
2431 ClientSocketHandle handle3;
2432 TestCompletionCallback callback3;
2433 rv = handle3.Init("a",
2434 params_,
2435 LOWEST,
2436 callback3.callback(),
2437 pool_.get(),
2438 BoundNetLog());
2439 EXPECT_EQ(ERR_IO_PENDING, rv);
2441 ClientSocketHandle handle4;
2442 TestCompletionCallback callback4;
2443 rv = handle4.Init("a",
2444 params_,
2445 LOWEST,
2446 callback4.callback(),
2447 pool_.get(),
2448 BoundNetLog());
2449 EXPECT_EQ(ERR_IO_PENDING, rv);
2451 // Release two disconnected sockets.
2453 handle.socket()->Disconnect();
2454 handle.Reset();
2455 handle2.socket()->Disconnect();
2456 handle2.Reset();
2458 EXPECT_EQ(OK, callback3.WaitForResult());
2459 EXPECT_FALSE(handle3.is_reused());
2460 EXPECT_EQ(OK, callback4.WaitForResult());
2461 EXPECT_FALSE(handle4.is_reused());
2464 // Regression test for http://crbug.com/42267.
2465 // When DoReleaseSocket() is processed for one socket, it is blocked because the
2466 // other stalled groups all have releasing sockets, so no progress can be made.
2467 TEST_F(ClientSocketPoolBaseTest, SocketLimitReleasingSockets) {
2468 CreatePoolWithIdleTimeouts(
2469 4 /* socket limit */, 4 /* socket limit per group */,
2470 base::TimeDelta(), // Time out unused sockets immediately.
2471 base::TimeDelta::FromDays(1)); // Don't time out used sockets.
2473 connect_job_factory_->set_job_type(TestConnectJob::kMockJob);
2475 // Max out the socket limit with 2 per group.
2477 ClientSocketHandle handle_a[4];
2478 TestCompletionCallback callback_a[4];
2479 ClientSocketHandle handle_b[4];
2480 TestCompletionCallback callback_b[4];
2482 for (int i = 0; i < 2; ++i) {
2483 EXPECT_EQ(OK, handle_a[i].Init("a",
2484 params_,
2485 LOWEST,
2486 callback_a[i].callback(),
2487 pool_.get(),
2488 BoundNetLog()));
2489 EXPECT_EQ(OK, handle_b[i].Init("b",
2490 params_,
2491 LOWEST,
2492 callback_b[i].callback(),
2493 pool_.get(),
2494 BoundNetLog()));
2497 // Make 4 pending requests, 2 per group.
2499 for (int i = 2; i < 4; ++i) {
2500 EXPECT_EQ(ERR_IO_PENDING,
2501 handle_a[i].Init("a",
2502 params_,
2503 LOWEST,
2504 callback_a[i].callback(),
2505 pool_.get(),
2506 BoundNetLog()));
2507 EXPECT_EQ(ERR_IO_PENDING,
2508 handle_b[i].Init("b",
2509 params_,
2510 LOWEST,
2511 callback_b[i].callback(),
2512 pool_.get(),
2513 BoundNetLog()));
2516 // Release b's socket first. The order is important, because in
2517 // DoReleaseSocket(), we'll process b's released socket, and since both b and
2518 // a are stalled, but 'a' is lower lexicographically, we'll process group 'a'
2519 // first, which has a releasing socket, so it refuses to start up another
2520 // ConnectJob. So, we used to infinite loop on this.
2521 handle_b[0].socket()->Disconnect();
2522 handle_b[0].Reset();
2523 handle_a[0].socket()->Disconnect();
2524 handle_a[0].Reset();
2526 // Used to get stuck here.
2527 base::MessageLoop::current()->RunUntilIdle();
2529 handle_b[1].socket()->Disconnect();
2530 handle_b[1].Reset();
2531 handle_a[1].socket()->Disconnect();
2532 handle_a[1].Reset();
2534 for (int i = 2; i < 4; ++i) {
2535 EXPECT_EQ(OK, callback_b[i].WaitForResult());
2536 EXPECT_EQ(OK, callback_a[i].WaitForResult());
2540 TEST_F(ClientSocketPoolBaseTest,
2541 ReleasingDisconnectedSocketsMaintainsPriorityOrder) {
2542 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
2544 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
2546 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", DEFAULT_PRIORITY));
2547 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", DEFAULT_PRIORITY));
2548 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", DEFAULT_PRIORITY));
2549 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", DEFAULT_PRIORITY));
2551 EXPECT_EQ(OK, (*requests())[0]->WaitForResult());
2552 EXPECT_EQ(OK, (*requests())[1]->WaitForResult());
2553 EXPECT_EQ(2u, completion_count());
2555 // Releases one connection.
2556 EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::NO_KEEP_ALIVE));
2557 EXPECT_EQ(OK, (*requests())[2]->WaitForResult());
2559 EXPECT_TRUE(ReleaseOneConnection(ClientSocketPoolTest::NO_KEEP_ALIVE));
2560 EXPECT_EQ(OK, (*requests())[3]->WaitForResult());
2561 EXPECT_EQ(4u, completion_count());
2563 EXPECT_EQ(1, GetOrderOfRequest(1));
2564 EXPECT_EQ(2, GetOrderOfRequest(2));
2565 EXPECT_EQ(3, GetOrderOfRequest(3));
2566 EXPECT_EQ(4, GetOrderOfRequest(4));
2568 // Make sure we test order of all requests made.
2569 EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(5));
2572 class TestReleasingSocketRequest : public TestCompletionCallbackBase {
2573 public:
2574 TestReleasingSocketRequest(TestClientSocketPool* pool,
2575 int expected_result,
2576 bool reset_releasing_handle)
2577 : pool_(pool),
2578 expected_result_(expected_result),
2579 reset_releasing_handle_(reset_releasing_handle),
2580 callback_(base::Bind(&TestReleasingSocketRequest::OnComplete,
2581 base::Unretained(this))) {
2584 ~TestReleasingSocketRequest() override {}
2586 ClientSocketHandle* handle() { return &handle_; }
2588 const CompletionCallback& callback() const { return callback_; }
2590 private:
2591 void OnComplete(int result) {
2592 SetResult(result);
2593 if (reset_releasing_handle_)
2594 handle_.Reset();
2596 scoped_refptr<TestSocketParams> con_params(
2597 new TestSocketParams(false /* ignore_limits */));
2598 EXPECT_EQ(expected_result_,
2599 handle2_.Init("a", con_params, DEFAULT_PRIORITY,
2600 callback2_.callback(), pool_, BoundNetLog()));
2603 TestClientSocketPool* const pool_;
2604 int expected_result_;
2605 bool reset_releasing_handle_;
2606 ClientSocketHandle handle_;
2607 ClientSocketHandle handle2_;
2608 CompletionCallback callback_;
2609 TestCompletionCallback callback2_;
2613 TEST_F(ClientSocketPoolBaseTest, AdditionalErrorSocketsDontUseSlot) {
2614 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
2616 EXPECT_EQ(OK, StartRequest("b", DEFAULT_PRIORITY));
2617 EXPECT_EQ(OK, StartRequest("a", DEFAULT_PRIORITY));
2618 EXPECT_EQ(OK, StartRequest("b", DEFAULT_PRIORITY));
2620 EXPECT_EQ(static_cast<int>(requests_size()),
2621 client_socket_factory_.allocation_count());
2623 connect_job_factory_->set_job_type(
2624 TestConnectJob::kMockPendingAdditionalErrorStateJob);
2625 TestReleasingSocketRequest req(pool_.get(), OK, false);
2626 EXPECT_EQ(ERR_IO_PENDING,
2627 req.handle()->Init("a", params_, DEFAULT_PRIORITY, req.callback(),
2628 pool_.get(), BoundNetLog()));
2629 // The next job should complete synchronously
2630 connect_job_factory_->set_job_type(TestConnectJob::kMockJob);
2632 EXPECT_EQ(ERR_CONNECTION_FAILED, req.WaitForResult());
2633 EXPECT_FALSE(req.handle()->is_initialized());
2634 EXPECT_FALSE(req.handle()->socket());
2635 EXPECT_TRUE(req.handle()->is_ssl_error());
2636 EXPECT_FALSE(req.handle()->ssl_error_response_info().headers.get() == NULL);
2639 // http://crbug.com/44724 regression test.
2640 // We start releasing the pool when we flush on network change. When that
2641 // happens, the only active references are in the ClientSocketHandles. When a
2642 // ConnectJob completes and calls back into the last ClientSocketHandle, that
2643 // callback can release the last reference and delete the pool. After the
2644 // callback finishes, we go back to the stack frame within the now-deleted pool.
2645 // Executing any code that refers to members of the now-deleted pool can cause
2646 // crashes.
2647 TEST_F(ClientSocketPoolBaseTest, CallbackThatReleasesPool) {
2648 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
2649 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob);
2651 ClientSocketHandle handle;
2652 TestCompletionCallback callback;
2653 EXPECT_EQ(ERR_IO_PENDING, handle.Init("a",
2654 params_,
2655 DEFAULT_PRIORITY,
2656 callback.callback(),
2657 pool_.get(),
2658 BoundNetLog()));
2660 pool_->FlushWithError(ERR_NETWORK_CHANGED);
2662 // We'll call back into this now.
2663 callback.WaitForResult();
2666 TEST_F(ClientSocketPoolBaseTest, DoNotReuseSocketAfterFlush) {
2667 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
2668 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
2670 ClientSocketHandle handle;
2671 TestCompletionCallback callback;
2672 EXPECT_EQ(ERR_IO_PENDING, handle.Init("a",
2673 params_,
2674 DEFAULT_PRIORITY,
2675 callback.callback(),
2676 pool_.get(),
2677 BoundNetLog()));
2678 EXPECT_EQ(OK, callback.WaitForResult());
2679 EXPECT_EQ(ClientSocketHandle::UNUSED, handle.reuse_type());
2681 pool_->FlushWithError(ERR_NETWORK_CHANGED);
2683 handle.Reset();
2684 base::MessageLoop::current()->RunUntilIdle();
2686 EXPECT_EQ(ERR_IO_PENDING, handle.Init("a",
2687 params_,
2688 DEFAULT_PRIORITY,
2689 callback.callback(),
2690 pool_.get(),
2691 BoundNetLog()));
2692 EXPECT_EQ(OK, callback.WaitForResult());
2693 EXPECT_EQ(ClientSocketHandle::UNUSED, handle.reuse_type());
2696 class ConnectWithinCallback : public TestCompletionCallbackBase {
2697 public:
2698 ConnectWithinCallback(
2699 const std::string& group_name,
2700 const scoped_refptr<TestSocketParams>& params,
2701 TestClientSocketPool* pool)
2702 : group_name_(group_name),
2703 params_(params),
2704 pool_(pool),
2705 callback_(base::Bind(&ConnectWithinCallback::OnComplete,
2706 base::Unretained(this))) {
2709 ~ConnectWithinCallback() override {}
2711 int WaitForNestedResult() {
2712 return nested_callback_.WaitForResult();
2715 const CompletionCallback& callback() const { return callback_; }
2717 private:
2718 void OnComplete(int result) {
2719 SetResult(result);
2720 EXPECT_EQ(ERR_IO_PENDING,
2721 handle_.Init(group_name_,
2722 params_,
2723 DEFAULT_PRIORITY,
2724 nested_callback_.callback(),
2725 pool_,
2726 BoundNetLog()));
2729 const std::string group_name_;
2730 const scoped_refptr<TestSocketParams> params_;
2731 TestClientSocketPool* const pool_;
2732 ClientSocketHandle handle_;
2733 CompletionCallback callback_;
2734 TestCompletionCallback nested_callback_;
2736 DISALLOW_COPY_AND_ASSIGN(ConnectWithinCallback);
2739 TEST_F(ClientSocketPoolBaseTest, AbortAllRequestsOnFlush) {
2740 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
2742 // First job will be waiting until it gets aborted.
2743 connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob);
2745 ClientSocketHandle handle;
2746 ConnectWithinCallback callback("a", params_, pool_.get());
2747 EXPECT_EQ(ERR_IO_PENDING, handle.Init("a",
2748 params_,
2749 DEFAULT_PRIORITY,
2750 callback.callback(),
2751 pool_.get(),
2752 BoundNetLog()));
2754 // Second job will be started during the first callback, and will
2755 // asynchronously complete with OK.
2756 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
2757 pool_->FlushWithError(ERR_NETWORK_CHANGED);
2758 EXPECT_EQ(ERR_NETWORK_CHANGED, callback.WaitForResult());
2759 EXPECT_EQ(OK, callback.WaitForNestedResult());
2762 // Cancel a pending socket request while we're at max sockets,
2763 // and verify that the backup socket firing doesn't cause a crash.
2764 TEST_F(ClientSocketPoolBaseTest, BackupSocketCancelAtMaxSockets) {
2765 // Max 4 sockets globally, max 4 sockets per group.
2766 CreatePool(kDefaultMaxSockets, kDefaultMaxSockets);
2767 pool_->EnableConnectBackupJobs();
2769 // Create the first socket and set to ERR_IO_PENDING. This starts the backup
2770 // timer.
2771 connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob);
2772 ClientSocketHandle handle;
2773 TestCompletionCallback callback;
2774 EXPECT_EQ(ERR_IO_PENDING, handle.Init("bar",
2775 params_,
2776 DEFAULT_PRIORITY,
2777 callback.callback(),
2778 pool_.get(),
2779 BoundNetLog()));
2781 // Start (MaxSockets - 1) connected sockets to reach max sockets.
2782 connect_job_factory_->set_job_type(TestConnectJob::kMockJob);
2783 ClientSocketHandle handles[kDefaultMaxSockets];
2784 for (int i = 1; i < kDefaultMaxSockets; ++i) {
2785 TestCompletionCallback callback;
2786 EXPECT_EQ(OK, handles[i].Init("bar",
2787 params_,
2788 DEFAULT_PRIORITY,
2789 callback.callback(),
2790 pool_.get(),
2791 BoundNetLog()));
2794 base::MessageLoop::current()->RunUntilIdle();
2796 // Cancel the pending request.
2797 handle.Reset();
2799 // Wait for the backup timer to fire (add some slop to ensure it fires)
2800 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(
2801 ClientSocketPool::kMaxConnectRetryIntervalMs / 2 * 3));
2803 base::MessageLoop::current()->RunUntilIdle();
2804 EXPECT_EQ(kDefaultMaxSockets, client_socket_factory_.allocation_count());
2807 TEST_F(ClientSocketPoolBaseTest, CancelBackupSocketAfterCancelingAllRequests) {
2808 CreatePool(kDefaultMaxSockets, kDefaultMaxSockets);
2809 pool_->EnableConnectBackupJobs();
2811 // Create the first socket and set to ERR_IO_PENDING. This starts the backup
2812 // timer.
2813 connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob);
2814 ClientSocketHandle handle;
2815 TestCompletionCallback callback;
2816 EXPECT_EQ(ERR_IO_PENDING, handle.Init("bar",
2817 params_,
2818 DEFAULT_PRIORITY,
2819 callback.callback(),
2820 pool_.get(),
2821 BoundNetLog()));
2822 ASSERT_TRUE(pool_->HasGroup("bar"));
2823 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("bar"));
2824 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("bar"));
2826 // Cancel the socket request. This should cancel the backup timer. Wait for
2827 // the backup time to see if it indeed got canceled.
2828 handle.Reset();
2829 // Wait for the backup timer to fire (add some slop to ensure it fires)
2830 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(
2831 ClientSocketPool::kMaxConnectRetryIntervalMs / 2 * 3));
2832 base::MessageLoop::current()->RunUntilIdle();
2833 ASSERT_TRUE(pool_->HasGroup("bar"));
2834 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("bar"));
2837 TEST_F(ClientSocketPoolBaseTest, CancelBackupSocketAfterFinishingAllRequests) {
2838 CreatePool(kDefaultMaxSockets, kDefaultMaxSockets);
2839 pool_->EnableConnectBackupJobs();
2841 // Create the first socket and set to ERR_IO_PENDING. This starts the backup
2842 // timer.
2843 connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob);
2844 ClientSocketHandle handle;
2845 TestCompletionCallback callback;
2846 EXPECT_EQ(ERR_IO_PENDING, handle.Init("bar",
2847 params_,
2848 DEFAULT_PRIORITY,
2849 callback.callback(),
2850 pool_.get(),
2851 BoundNetLog()));
2852 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
2853 ClientSocketHandle handle2;
2854 TestCompletionCallback callback2;
2855 EXPECT_EQ(ERR_IO_PENDING, handle2.Init("bar",
2856 params_,
2857 DEFAULT_PRIORITY,
2858 callback2.callback(),
2859 pool_.get(),
2860 BoundNetLog()));
2861 ASSERT_TRUE(pool_->HasGroup("bar"));
2862 EXPECT_EQ(2, pool_->NumConnectJobsInGroup("bar"));
2864 // Cancel request 1 and then complete request 2. With the requests finished,
2865 // the backup timer should be cancelled.
2866 handle.Reset();
2867 EXPECT_EQ(OK, callback2.WaitForResult());
2868 // Wait for the backup timer to fire (add some slop to ensure it fires)
2869 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(
2870 ClientSocketPool::kMaxConnectRetryIntervalMs / 2 * 3));
2871 base::MessageLoop::current()->RunUntilIdle();
2874 // Test delayed socket binding for the case where we have two connects,
2875 // and while one is waiting on a connect, the other frees up.
2876 // The socket waiting on a connect should switch immediately to the freed
2877 // up socket.
2878 TEST_F(ClientSocketPoolBaseTest, DelayedSocketBindingWaitingForConnect) {
2879 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
2880 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
2882 ClientSocketHandle handle1;
2883 TestCompletionCallback callback;
2884 EXPECT_EQ(ERR_IO_PENDING,
2885 handle1.Init("a",
2886 params_,
2887 DEFAULT_PRIORITY,
2888 callback.callback(),
2889 pool_.get(),
2890 BoundNetLog()));
2891 EXPECT_EQ(OK, callback.WaitForResult());
2893 // No idle sockets, no pending jobs.
2894 EXPECT_EQ(0, pool_->IdleSocketCount());
2895 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a"));
2897 // Create a second socket to the same host, but this one will wait.
2898 connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob);
2899 ClientSocketHandle handle2;
2900 EXPECT_EQ(ERR_IO_PENDING,
2901 handle2.Init("a",
2902 params_,
2903 DEFAULT_PRIORITY,
2904 callback.callback(),
2905 pool_.get(),
2906 BoundNetLog()));
2907 // No idle sockets, and one connecting job.
2908 EXPECT_EQ(0, pool_->IdleSocketCount());
2909 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a"));
2911 // Return the first handle to the pool. This will initiate the delayed
2912 // binding.
2913 handle1.Reset();
2915 base::MessageLoop::current()->RunUntilIdle();
2917 // Still no idle sockets, still one pending connect job.
2918 EXPECT_EQ(0, pool_->IdleSocketCount());
2919 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a"));
2921 // The second socket connected, even though it was a Waiting Job.
2922 EXPECT_EQ(OK, callback.WaitForResult());
2924 // And we can see there is still one job waiting.
2925 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a"));
2927 // Finally, signal the waiting Connect.
2928 client_socket_factory_.SignalJobs();
2929 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a"));
2931 base::MessageLoop::current()->RunUntilIdle();
2934 // Test delayed socket binding when a group is at capacity and one
2935 // of the group's sockets frees up.
2936 TEST_F(ClientSocketPoolBaseTest, DelayedSocketBindingAtGroupCapacity) {
2937 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
2938 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
2940 ClientSocketHandle handle1;
2941 TestCompletionCallback callback;
2942 EXPECT_EQ(ERR_IO_PENDING,
2943 handle1.Init("a",
2944 params_,
2945 DEFAULT_PRIORITY,
2946 callback.callback(),
2947 pool_.get(),
2948 BoundNetLog()));
2949 EXPECT_EQ(OK, callback.WaitForResult());
2951 // No idle sockets, no pending jobs.
2952 EXPECT_EQ(0, pool_->IdleSocketCount());
2953 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a"));
2955 // Create a second socket to the same host, but this one will wait.
2956 connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob);
2957 ClientSocketHandle handle2;
2958 EXPECT_EQ(ERR_IO_PENDING,
2959 handle2.Init("a",
2960 params_,
2961 DEFAULT_PRIORITY,
2962 callback.callback(),
2963 pool_.get(),
2964 BoundNetLog()));
2965 // No idle sockets, and one connecting job.
2966 EXPECT_EQ(0, pool_->IdleSocketCount());
2967 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a"));
2969 // Return the first handle to the pool. This will initiate the delayed
2970 // binding.
2971 handle1.Reset();
2973 base::MessageLoop::current()->RunUntilIdle();
2975 // Still no idle sockets, still one pending connect job.
2976 EXPECT_EQ(0, pool_->IdleSocketCount());
2977 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a"));
2979 // The second socket connected, even though it was a Waiting Job.
2980 EXPECT_EQ(OK, callback.WaitForResult());
2982 // And we can see there is still one job waiting.
2983 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a"));
2985 // Finally, signal the waiting Connect.
2986 client_socket_factory_.SignalJobs();
2987 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a"));
2989 base::MessageLoop::current()->RunUntilIdle();
2992 // Test out the case where we have one socket connected, one
2993 // connecting, when the first socket finishes and goes idle.
2994 // Although the second connection is pending, the second request
2995 // should complete, by taking the first socket's idle socket.
2996 TEST_F(ClientSocketPoolBaseTest, DelayedSocketBindingAtStall) {
2997 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
2998 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
3000 ClientSocketHandle handle1;
3001 TestCompletionCallback callback;
3002 EXPECT_EQ(ERR_IO_PENDING,
3003 handle1.Init("a",
3004 params_,
3005 DEFAULT_PRIORITY,
3006 callback.callback(),
3007 pool_.get(),
3008 BoundNetLog()));
3009 EXPECT_EQ(OK, callback.WaitForResult());
3011 // No idle sockets, no pending jobs.
3012 EXPECT_EQ(0, pool_->IdleSocketCount());
3013 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a"));
3015 // Create a second socket to the same host, but this one will wait.
3016 connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob);
3017 ClientSocketHandle handle2;
3018 EXPECT_EQ(ERR_IO_PENDING,
3019 handle2.Init("a",
3020 params_,
3021 DEFAULT_PRIORITY,
3022 callback.callback(),
3023 pool_.get(),
3024 BoundNetLog()));
3025 // No idle sockets, and one connecting job.
3026 EXPECT_EQ(0, pool_->IdleSocketCount());
3027 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a"));
3029 // Return the first handle to the pool. This will initiate the delayed
3030 // binding.
3031 handle1.Reset();
3033 base::MessageLoop::current()->RunUntilIdle();
3035 // Still no idle sockets, still one pending connect job.
3036 EXPECT_EQ(0, pool_->IdleSocketCount());
3037 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a"));
3039 // The second socket connected, even though it was a Waiting Job.
3040 EXPECT_EQ(OK, callback.WaitForResult());
3042 // And we can see there is still one job waiting.
3043 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a"));
3045 // Finally, signal the waiting Connect.
3046 client_socket_factory_.SignalJobs();
3047 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a"));
3049 base::MessageLoop::current()->RunUntilIdle();
3052 // Cover the case where on an available socket slot, we have one pending
3053 // request that completes synchronously, thereby making the Group empty.
3054 TEST_F(ClientSocketPoolBaseTest, SynchronouslyProcessOnePendingRequest) {
3055 const int kUnlimitedSockets = 100;
3056 const int kOneSocketPerGroup = 1;
3057 CreatePool(kUnlimitedSockets, kOneSocketPerGroup);
3059 // Make the first request asynchronous fail.
3060 // This will free up a socket slot later.
3061 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingFailingJob);
3063 ClientSocketHandle handle1;
3064 TestCompletionCallback callback1;
3065 EXPECT_EQ(ERR_IO_PENDING,
3066 handle1.Init("a",
3067 params_,
3068 DEFAULT_PRIORITY,
3069 callback1.callback(),
3070 pool_.get(),
3071 BoundNetLog()));
3072 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a"));
3074 // Make the second request synchronously fail. This should make the Group
3075 // empty.
3076 connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob);
3077 ClientSocketHandle handle2;
3078 TestCompletionCallback callback2;
3079 // It'll be ERR_IO_PENDING now, but the TestConnectJob will synchronously fail
3080 // when created.
3081 EXPECT_EQ(ERR_IO_PENDING,
3082 handle2.Init("a",
3083 params_,
3084 DEFAULT_PRIORITY,
3085 callback2.callback(),
3086 pool_.get(),
3087 BoundNetLog()));
3089 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a"));
3091 EXPECT_EQ(ERR_CONNECTION_FAILED, callback1.WaitForResult());
3092 EXPECT_EQ(ERR_CONNECTION_FAILED, callback2.WaitForResult());
3093 EXPECT_FALSE(pool_->HasGroup("a"));
3096 TEST_F(ClientSocketPoolBaseTest, PreferUsedSocketToUnusedSocket) {
3097 CreatePool(kDefaultMaxSockets, kDefaultMaxSockets);
3099 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
3101 ClientSocketHandle handle1;
3102 TestCompletionCallback callback1;
3103 EXPECT_EQ(ERR_IO_PENDING, handle1.Init("a",
3104 params_,
3105 DEFAULT_PRIORITY,
3106 callback1.callback(),
3107 pool_.get(),
3108 BoundNetLog()));
3110 ClientSocketHandle handle2;
3111 TestCompletionCallback callback2;
3112 EXPECT_EQ(ERR_IO_PENDING, handle2.Init("a",
3113 params_,
3114 DEFAULT_PRIORITY,
3115 callback2.callback(),
3116 pool_.get(),
3117 BoundNetLog()));
3118 ClientSocketHandle handle3;
3119 TestCompletionCallback callback3;
3120 EXPECT_EQ(ERR_IO_PENDING, handle3.Init("a",
3121 params_,
3122 DEFAULT_PRIORITY,
3123 callback3.callback(),
3124 pool_.get(),
3125 BoundNetLog()));
3127 EXPECT_EQ(OK, callback1.WaitForResult());
3128 EXPECT_EQ(OK, callback2.WaitForResult());
3129 EXPECT_EQ(OK, callback3.WaitForResult());
3131 // Use the socket.
3132 EXPECT_EQ(1, handle1.socket()->Write(NULL, 1, CompletionCallback()));
3133 EXPECT_EQ(1, handle3.socket()->Write(NULL, 1, CompletionCallback()));
3135 handle1.Reset();
3136 handle2.Reset();
3137 handle3.Reset();
3139 EXPECT_EQ(OK, handle1.Init("a",
3140 params_,
3141 DEFAULT_PRIORITY,
3142 callback1.callback(),
3143 pool_.get(),
3144 BoundNetLog()));
3145 EXPECT_EQ(OK, handle2.Init("a",
3146 params_,
3147 DEFAULT_PRIORITY,
3148 callback2.callback(),
3149 pool_.get(),
3150 BoundNetLog()));
3151 EXPECT_EQ(OK, handle3.Init("a",
3152 params_,
3153 DEFAULT_PRIORITY,
3154 callback3.callback(),
3155 pool_.get(),
3156 BoundNetLog()));
3158 EXPECT_TRUE(handle1.socket()->WasEverUsed());
3159 EXPECT_TRUE(handle2.socket()->WasEverUsed());
3160 EXPECT_FALSE(handle3.socket()->WasEverUsed());
3163 TEST_F(ClientSocketPoolBaseTest, RequestSockets) {
3164 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
3165 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
3167 pool_->RequestSockets("a", &params_, 2, BoundNetLog());
3169 ASSERT_TRUE(pool_->HasGroup("a"));
3170 EXPECT_EQ(2, pool_->NumConnectJobsInGroup("a"));
3171 EXPECT_EQ(2, pool_->NumUnassignedConnectJobsInGroup("a"));
3172 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
3174 ClientSocketHandle handle1;
3175 TestCompletionCallback callback1;
3176 EXPECT_EQ(ERR_IO_PENDING, handle1.Init("a",
3177 params_,
3178 DEFAULT_PRIORITY,
3179 callback1.callback(),
3180 pool_.get(),
3181 BoundNetLog()));
3183 ClientSocketHandle handle2;
3184 TestCompletionCallback callback2;
3185 EXPECT_EQ(ERR_IO_PENDING, handle2.Init("a",
3186 params_,
3187 DEFAULT_PRIORITY,
3188 callback2.callback(),
3189 pool_.get(),
3190 BoundNetLog()));
3192 EXPECT_EQ(2, pool_->NumConnectJobsInGroup("a"));
3193 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a"));
3194 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
3196 EXPECT_EQ(OK, callback1.WaitForResult());
3197 EXPECT_EQ(OK, callback2.WaitForResult());
3198 handle1.Reset();
3199 handle2.Reset();
3201 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a"));
3202 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a"));
3203 EXPECT_EQ(2, pool_->IdleSocketCountInGroup("a"));
3206 TEST_F(ClientSocketPoolBaseTest, RequestSocketsWhenAlreadyHaveAConnectJob) {
3207 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
3208 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
3210 ClientSocketHandle handle1;
3211 TestCompletionCallback callback1;
3212 EXPECT_EQ(ERR_IO_PENDING, handle1.Init("a",
3213 params_,
3214 DEFAULT_PRIORITY,
3215 callback1.callback(),
3216 pool_.get(),
3217 BoundNetLog()));
3219 ASSERT_TRUE(pool_->HasGroup("a"));
3220 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a"));
3221 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a"));
3222 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
3224 pool_->RequestSockets("a", &params_, 2, BoundNetLog());
3226 EXPECT_EQ(2, pool_->NumConnectJobsInGroup("a"));
3227 EXPECT_EQ(1, pool_->NumUnassignedConnectJobsInGroup("a"));
3228 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
3230 ClientSocketHandle handle2;
3231 TestCompletionCallback callback2;
3232 EXPECT_EQ(ERR_IO_PENDING, handle2.Init("a",
3233 params_,
3234 DEFAULT_PRIORITY,
3235 callback2.callback(),
3236 pool_.get(),
3237 BoundNetLog()));
3239 EXPECT_EQ(2, pool_->NumConnectJobsInGroup("a"));
3240 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a"));
3241 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
3243 EXPECT_EQ(OK, callback1.WaitForResult());
3244 EXPECT_EQ(OK, callback2.WaitForResult());
3245 handle1.Reset();
3246 handle2.Reset();
3248 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a"));
3249 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a"));
3250 EXPECT_EQ(2, pool_->IdleSocketCountInGroup("a"));
3253 TEST_F(ClientSocketPoolBaseTest,
3254 RequestSocketsWhenAlreadyHaveMultipleConnectJob) {
3255 CreatePool(4, 4);
3256 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
3258 ClientSocketHandle handle1;
3259 TestCompletionCallback callback1;
3260 EXPECT_EQ(ERR_IO_PENDING, handle1.Init("a",
3261 params_,
3262 DEFAULT_PRIORITY,
3263 callback1.callback(),
3264 pool_.get(),
3265 BoundNetLog()));
3267 ClientSocketHandle handle2;
3268 TestCompletionCallback callback2;
3269 EXPECT_EQ(ERR_IO_PENDING, handle2.Init("a",
3270 params_,
3271 DEFAULT_PRIORITY,
3272 callback2.callback(),
3273 pool_.get(),
3274 BoundNetLog()));
3276 ClientSocketHandle handle3;
3277 TestCompletionCallback callback3;
3278 EXPECT_EQ(ERR_IO_PENDING, handle3.Init("a",
3279 params_,
3280 DEFAULT_PRIORITY,
3281 callback3.callback(),
3282 pool_.get(),
3283 BoundNetLog()));
3285 ASSERT_TRUE(pool_->HasGroup("a"));
3286 EXPECT_EQ(3, pool_->NumConnectJobsInGroup("a"));
3287 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a"));
3288 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
3290 pool_->RequestSockets("a", &params_, 2, BoundNetLog());
3292 EXPECT_EQ(3, pool_->NumConnectJobsInGroup("a"));
3293 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a"));
3294 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
3296 EXPECT_EQ(OK, callback1.WaitForResult());
3297 EXPECT_EQ(OK, callback2.WaitForResult());
3298 EXPECT_EQ(OK, callback3.WaitForResult());
3299 handle1.Reset();
3300 handle2.Reset();
3301 handle3.Reset();
3303 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a"));
3304 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a"));
3305 EXPECT_EQ(3, pool_->IdleSocketCountInGroup("a"));
3308 TEST_F(ClientSocketPoolBaseTest, RequestSocketsAtMaxSocketLimit) {
3309 CreatePool(kDefaultMaxSockets, kDefaultMaxSockets);
3310 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
3312 ASSERT_FALSE(pool_->HasGroup("a"));
3314 pool_->RequestSockets("a", &params_, kDefaultMaxSockets,
3315 BoundNetLog());
3317 ASSERT_TRUE(pool_->HasGroup("a"));
3318 EXPECT_EQ(kDefaultMaxSockets, pool_->NumConnectJobsInGroup("a"));
3319 EXPECT_EQ(kDefaultMaxSockets, pool_->NumUnassignedConnectJobsInGroup("a"));
3321 ASSERT_FALSE(pool_->HasGroup("b"));
3323 pool_->RequestSockets("b", &params_, kDefaultMaxSockets,
3324 BoundNetLog());
3326 ASSERT_FALSE(pool_->HasGroup("b"));
3329 TEST_F(ClientSocketPoolBaseTest, RequestSocketsHitMaxSocketLimit) {
3330 CreatePool(kDefaultMaxSockets, kDefaultMaxSockets);
3331 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
3333 ASSERT_FALSE(pool_->HasGroup("a"));
3335 pool_->RequestSockets("a", &params_, kDefaultMaxSockets - 1,
3336 BoundNetLog());
3338 ASSERT_TRUE(pool_->HasGroup("a"));
3339 EXPECT_EQ(kDefaultMaxSockets - 1, pool_->NumConnectJobsInGroup("a"));
3340 EXPECT_EQ(kDefaultMaxSockets - 1,
3341 pool_->NumUnassignedConnectJobsInGroup("a"));
3342 EXPECT_FALSE(pool_->IsStalled());
3344 ASSERT_FALSE(pool_->HasGroup("b"));
3346 pool_->RequestSockets("b", &params_, kDefaultMaxSockets,
3347 BoundNetLog());
3349 ASSERT_TRUE(pool_->HasGroup("b"));
3350 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("b"));
3351 EXPECT_FALSE(pool_->IsStalled());
3354 TEST_F(ClientSocketPoolBaseTest, RequestSocketsCountIdleSockets) {
3355 CreatePool(4, 4);
3356 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
3358 ClientSocketHandle handle1;
3359 TestCompletionCallback callback1;
3360 EXPECT_EQ(ERR_IO_PENDING, handle1.Init("a",
3361 params_,
3362 DEFAULT_PRIORITY,
3363 callback1.callback(),
3364 pool_.get(),
3365 BoundNetLog()));
3366 ASSERT_EQ(OK, callback1.WaitForResult());
3367 handle1.Reset();
3369 ASSERT_TRUE(pool_->HasGroup("a"));
3370 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a"));
3371 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a"));
3372 EXPECT_EQ(1, pool_->IdleSocketCountInGroup("a"));
3374 pool_->RequestSockets("a", &params_, 2, BoundNetLog());
3376 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a"));
3377 EXPECT_EQ(1, pool_->NumUnassignedConnectJobsInGroup("a"));
3378 EXPECT_EQ(1, pool_->IdleSocketCountInGroup("a"));
3381 TEST_F(ClientSocketPoolBaseTest, RequestSocketsCountActiveSockets) {
3382 CreatePool(4, 4);
3383 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
3385 ClientSocketHandle handle1;
3386 TestCompletionCallback callback1;
3387 EXPECT_EQ(ERR_IO_PENDING, handle1.Init("a",
3388 params_,
3389 DEFAULT_PRIORITY,
3390 callback1.callback(),
3391 pool_.get(),
3392 BoundNetLog()));
3393 ASSERT_EQ(OK, callback1.WaitForResult());
3395 ASSERT_TRUE(pool_->HasGroup("a"));
3396 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a"));
3397 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a"));
3398 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
3399 EXPECT_EQ(1, pool_->NumActiveSocketsInGroup("a"));
3401 pool_->RequestSockets("a", &params_, 2, BoundNetLog());
3403 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a"));
3404 EXPECT_EQ(1, pool_->NumUnassignedConnectJobsInGroup("a"));
3405 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
3406 EXPECT_EQ(1, pool_->NumActiveSocketsInGroup("a"));
3409 TEST_F(ClientSocketPoolBaseTest, RequestSocketsSynchronous) {
3410 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
3411 connect_job_factory_->set_job_type(TestConnectJob::kMockJob);
3413 pool_->RequestSockets("a", &params_, kDefaultMaxSocketsPerGroup,
3414 BoundNetLog());
3416 ASSERT_TRUE(pool_->HasGroup("a"));
3417 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a"));
3418 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a"));
3419 EXPECT_EQ(kDefaultMaxSocketsPerGroup, pool_->IdleSocketCountInGroup("a"));
3421 pool_->RequestSockets("b", &params_, kDefaultMaxSocketsPerGroup,
3422 BoundNetLog());
3424 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("b"));
3425 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("b"));
3426 EXPECT_EQ(kDefaultMaxSocketsPerGroup, pool_->IdleSocketCountInGroup("b"));
3429 TEST_F(ClientSocketPoolBaseTest, RequestSocketsSynchronousError) {
3430 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
3431 connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob);
3433 pool_->RequestSockets("a", &params_, kDefaultMaxSocketsPerGroup,
3434 BoundNetLog());
3436 ASSERT_FALSE(pool_->HasGroup("a"));
3438 connect_job_factory_->set_job_type(
3439 TestConnectJob::kMockAdditionalErrorStateJob);
3440 pool_->RequestSockets("a", &params_, kDefaultMaxSocketsPerGroup,
3441 BoundNetLog());
3443 ASSERT_FALSE(pool_->HasGroup("a"));
3446 TEST_F(ClientSocketPoolBaseTest, RequestSocketsMultipleTimesDoesNothing) {
3447 CreatePool(4, 4);
3448 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
3450 pool_->RequestSockets("a", &params_, 2, BoundNetLog());
3452 ASSERT_TRUE(pool_->HasGroup("a"));
3453 EXPECT_EQ(2, pool_->NumConnectJobsInGroup("a"));
3454 EXPECT_EQ(2, pool_->NumUnassignedConnectJobsInGroup("a"));
3455 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
3457 pool_->RequestSockets("a", &params_, 2, BoundNetLog());
3458 EXPECT_EQ(2, pool_->NumConnectJobsInGroup("a"));
3459 EXPECT_EQ(2, pool_->NumUnassignedConnectJobsInGroup("a"));
3460 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
3462 ClientSocketHandle handle1;
3463 TestCompletionCallback callback1;
3464 EXPECT_EQ(ERR_IO_PENDING, handle1.Init("a",
3465 params_,
3466 DEFAULT_PRIORITY,
3467 callback1.callback(),
3468 pool_.get(),
3469 BoundNetLog()));
3470 ASSERT_EQ(OK, callback1.WaitForResult());
3472 ClientSocketHandle handle2;
3473 TestCompletionCallback callback2;
3474 int rv = handle2.Init("a",
3475 params_,
3476 DEFAULT_PRIORITY,
3477 callback2.callback(),
3478 pool_.get(),
3479 BoundNetLog());
3480 if (rv != OK) {
3481 EXPECT_EQ(ERR_IO_PENDING, rv);
3482 EXPECT_EQ(OK, callback2.WaitForResult());
3485 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a"));
3486 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a"));
3487 EXPECT_EQ(2, pool_->NumActiveSocketsInGroup("a"));
3488 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
3490 handle1.Reset();
3491 handle2.Reset();
3493 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a"));
3494 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a"));
3495 EXPECT_EQ(2, pool_->IdleSocketCountInGroup("a"));
3497 pool_->RequestSockets("a", &params_, 2, BoundNetLog());
3498 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a"));
3499 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a"));
3500 EXPECT_EQ(2, pool_->IdleSocketCountInGroup("a"));
3503 TEST_F(ClientSocketPoolBaseTest, RequestSocketsDifferentNumSockets) {
3504 CreatePool(4, 4);
3505 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
3507 pool_->RequestSockets("a", &params_, 1, BoundNetLog());
3509 ASSERT_TRUE(pool_->HasGroup("a"));
3510 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a"));
3511 EXPECT_EQ(1, pool_->NumUnassignedConnectJobsInGroup("a"));
3512 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
3514 pool_->RequestSockets("a", &params_, 2, BoundNetLog());
3515 EXPECT_EQ(2, pool_->NumConnectJobsInGroup("a"));
3516 EXPECT_EQ(2, pool_->NumUnassignedConnectJobsInGroup("a"));
3517 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
3519 pool_->RequestSockets("a", &params_, 3, BoundNetLog());
3520 EXPECT_EQ(3, pool_->NumConnectJobsInGroup("a"));
3521 EXPECT_EQ(3, pool_->NumUnassignedConnectJobsInGroup("a"));
3522 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
3524 pool_->RequestSockets("a", &params_, 1, BoundNetLog());
3525 EXPECT_EQ(3, pool_->NumConnectJobsInGroup("a"));
3526 EXPECT_EQ(3, pool_->NumUnassignedConnectJobsInGroup("a"));
3527 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
3530 TEST_F(ClientSocketPoolBaseTest, PreconnectJobsTakenByNormalRequests) {
3531 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
3532 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
3534 pool_->RequestSockets("a", &params_, 1, BoundNetLog());
3536 ASSERT_TRUE(pool_->HasGroup("a"));
3537 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a"));
3538 EXPECT_EQ(1, pool_->NumUnassignedConnectJobsInGroup("a"));
3539 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
3541 ClientSocketHandle handle1;
3542 TestCompletionCallback callback1;
3543 EXPECT_EQ(ERR_IO_PENDING, handle1.Init("a",
3544 params_,
3545 DEFAULT_PRIORITY,
3546 callback1.callback(),
3547 pool_.get(),
3548 BoundNetLog()));
3550 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a"));
3551 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a"));
3552 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
3554 ASSERT_EQ(OK, callback1.WaitForResult());
3556 // Make sure if a preconnected socket is not fully connected when a request
3557 // starts, it has a connect start time.
3558 TestLoadTimingInfoConnectedNotReused(handle1);
3559 handle1.Reset();
3561 EXPECT_EQ(1, pool_->IdleSocketCountInGroup("a"));
3564 // Checks that fully connected preconnect jobs have no connect times, and are
3565 // marked as reused.
3566 TEST_F(ClientSocketPoolBaseTest, ConnectedPreconnectJobsHaveNoConnectTimes) {
3567 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
3568 connect_job_factory_->set_job_type(TestConnectJob::kMockJob);
3569 pool_->RequestSockets("a", &params_, 1, BoundNetLog());
3571 ASSERT_TRUE(pool_->HasGroup("a"));
3572 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a"));
3573 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a"));
3574 EXPECT_EQ(1, pool_->IdleSocketCountInGroup("a"));
3576 ClientSocketHandle handle;
3577 TestCompletionCallback callback;
3578 EXPECT_EQ(OK, handle.Init("a",
3579 params_,
3580 DEFAULT_PRIORITY,
3581 callback.callback(),
3582 pool_.get(),
3583 BoundNetLog()));
3585 // Make sure the idle socket was used.
3586 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
3588 TestLoadTimingInfoConnectedReused(handle);
3589 handle.Reset();
3590 TestLoadTimingInfoNotConnected(handle);
3593 // http://crbug.com/64940 regression test.
3594 TEST_F(ClientSocketPoolBaseTest, PreconnectClosesIdleSocketRemovesGroup) {
3595 const int kMaxTotalSockets = 3;
3596 const int kMaxSocketsPerGroup = 2;
3597 CreatePool(kMaxTotalSockets, kMaxSocketsPerGroup);
3598 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
3600 // Note that group name ordering matters here. "a" comes before "b", so
3601 // CloseOneIdleSocket() will try to close "a"'s idle socket.
3603 // Set up one idle socket in "a".
3604 ClientSocketHandle handle1;
3605 TestCompletionCallback callback1;
3606 EXPECT_EQ(ERR_IO_PENDING, handle1.Init("a",
3607 params_,
3608 DEFAULT_PRIORITY,
3609 callback1.callback(),
3610 pool_.get(),
3611 BoundNetLog()));
3613 ASSERT_EQ(OK, callback1.WaitForResult());
3614 handle1.Reset();
3615 EXPECT_EQ(1, pool_->IdleSocketCountInGroup("a"));
3617 // Set up two active sockets in "b".
3618 ClientSocketHandle handle2;
3619 TestCompletionCallback callback2;
3620 EXPECT_EQ(ERR_IO_PENDING, handle1.Init("b",
3621 params_,
3622 DEFAULT_PRIORITY,
3623 callback1.callback(),
3624 pool_.get(),
3625 BoundNetLog()));
3626 EXPECT_EQ(ERR_IO_PENDING, handle2.Init("b",
3627 params_,
3628 DEFAULT_PRIORITY,
3629 callback2.callback(),
3630 pool_.get(),
3631 BoundNetLog()));
3633 ASSERT_EQ(OK, callback1.WaitForResult());
3634 ASSERT_EQ(OK, callback2.WaitForResult());
3635 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("b"));
3636 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("b"));
3637 EXPECT_EQ(2, pool_->NumActiveSocketsInGroup("b"));
3639 // Now we have 1 idle socket in "a" and 2 active sockets in "b". This means
3640 // we've maxed out on sockets, since we set |kMaxTotalSockets| to 3.
3641 // Requesting 2 preconnected sockets for "a" should fail to allocate any more
3642 // sockets for "a", and "b" should still have 2 active sockets.
3644 pool_->RequestSockets("a", &params_, 2, BoundNetLog());
3645 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a"));
3646 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a"));
3647 EXPECT_EQ(1, pool_->IdleSocketCountInGroup("a"));
3648 EXPECT_EQ(0, pool_->NumActiveSocketsInGroup("a"));
3649 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("b"));
3650 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("b"));
3651 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("b"));
3652 EXPECT_EQ(2, pool_->NumActiveSocketsInGroup("b"));
3654 // Now release the 2 active sockets for "b". This will give us 1 idle socket
3655 // in "a" and 2 idle sockets in "b". Requesting 2 preconnected sockets for
3656 // "a" should result in closing 1 for "b".
3657 handle1.Reset();
3658 handle2.Reset();
3659 EXPECT_EQ(2, pool_->IdleSocketCountInGroup("b"));
3660 EXPECT_EQ(0, pool_->NumActiveSocketsInGroup("b"));
3662 pool_->RequestSockets("a", &params_, 2, BoundNetLog());
3663 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a"));
3664 EXPECT_EQ(1, pool_->NumUnassignedConnectJobsInGroup("a"));
3665 EXPECT_EQ(1, pool_->IdleSocketCountInGroup("a"));
3666 EXPECT_EQ(0, pool_->NumActiveSocketsInGroup("a"));
3667 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("b"));
3668 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("b"));
3669 EXPECT_EQ(1, pool_->IdleSocketCountInGroup("b"));
3670 EXPECT_EQ(0, pool_->NumActiveSocketsInGroup("b"));
3673 TEST_F(ClientSocketPoolBaseTest, PreconnectWithoutBackupJob) {
3674 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
3675 pool_->EnableConnectBackupJobs();
3677 // Make the ConnectJob hang until it times out, shorten the timeout.
3678 connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob);
3679 connect_job_factory_->set_timeout_duration(
3680 base::TimeDelta::FromMilliseconds(500));
3681 pool_->RequestSockets("a", &params_, 1, BoundNetLog());
3682 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a"));
3683 EXPECT_EQ(1, pool_->NumUnassignedConnectJobsInGroup("a"));
3684 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
3686 // Verify the backup timer doesn't create a backup job, by making
3687 // the backup job a pending job instead of a waiting job, so it
3688 // *would* complete if it were created.
3689 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
3690 base::MessageLoop::current()->PostDelayedTask(
3691 FROM_HERE,
3692 base::MessageLoop::QuitClosure(),
3693 base::TimeDelta::FromSeconds(1));
3694 base::MessageLoop::current()->Run();
3695 EXPECT_FALSE(pool_->HasGroup("a"));
3698 TEST_F(ClientSocketPoolBaseTest, PreconnectWithBackupJob) {
3699 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
3700 pool_->EnableConnectBackupJobs();
3702 // Make the ConnectJob hang forever.
3703 connect_job_factory_->set_job_type(TestConnectJob::kMockWaitingJob);
3704 pool_->RequestSockets("a", &params_, 1, BoundNetLog());
3705 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a"));
3706 EXPECT_EQ(1, pool_->NumUnassignedConnectJobsInGroup("a"));
3707 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
3708 base::MessageLoop::current()->RunUntilIdle();
3710 // Make the backup job be a pending job, so it completes normally.
3711 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
3712 ClientSocketHandle handle;
3713 TestCompletionCallback callback;
3714 EXPECT_EQ(ERR_IO_PENDING, handle.Init("a",
3715 params_,
3716 DEFAULT_PRIORITY,
3717 callback.callback(),
3718 pool_.get(),
3719 BoundNetLog()));
3720 // Timer has started, but the backup connect job shouldn't be created yet.
3721 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a"));
3722 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a"));
3723 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
3724 EXPECT_EQ(0, pool_->NumActiveSocketsInGroup("a"));
3725 ASSERT_EQ(OK, callback.WaitForResult());
3727 // The hung connect job should still be there, but everything else should be
3728 // complete.
3729 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a"));
3730 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a"));
3731 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
3732 EXPECT_EQ(1, pool_->NumActiveSocketsInGroup("a"));
3735 // Tests that a preconnect that starts out with unread data can still be used.
3736 // http://crbug.com/334467
3737 TEST_F(ClientSocketPoolBaseTest, PreconnectWithUnreadData) {
3738 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
3739 connect_job_factory_->set_job_type(TestConnectJob::kMockUnreadDataJob);
3741 pool_->RequestSockets("a", &params_, 1, BoundNetLog());
3743 ASSERT_TRUE(pool_->HasGroup("a"));
3744 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a"));
3745 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a"));
3746 EXPECT_EQ(1, pool_->IdleSocketCountInGroup("a"));
3748 // Fail future jobs to be sure that handle receives the preconnected socket
3749 // rather than closing it and making a new one.
3750 connect_job_factory_->set_job_type(TestConnectJob::kMockFailingJob);
3751 ClientSocketHandle handle;
3752 TestCompletionCallback callback;
3753 EXPECT_EQ(OK, handle.Init("a",
3754 params_,
3755 DEFAULT_PRIORITY,
3756 callback.callback(),
3757 pool_.get(),
3758 BoundNetLog()));
3760 ASSERT_TRUE(pool_->HasGroup("a"));
3761 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a"));
3762 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a"));
3763 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a"));
3765 // Drain the pending read.
3766 EXPECT_EQ(1, handle.socket()->Read(NULL, 1, CompletionCallback()));
3768 TestLoadTimingInfoConnectedReused(handle);
3769 handle.Reset();
3771 // The socket should be usable now that it's idle again.
3772 EXPECT_EQ(1, pool_->IdleSocketCountInGroup("a"));
3775 class MockLayeredPool : public HigherLayeredPool {
3776 public:
3777 MockLayeredPool(TestClientSocketPool* pool,
3778 const std::string& group_name)
3779 : pool_(pool),
3780 group_name_(group_name),
3781 can_release_connection_(true) {
3782 pool_->AddHigherLayeredPool(this);
3785 ~MockLayeredPool() {
3786 pool_->RemoveHigherLayeredPool(this);
3789 int RequestSocket(TestClientSocketPool* pool) {
3790 scoped_refptr<TestSocketParams> params(
3791 new TestSocketParams(false /* ignore_limits */));
3792 return handle_.Init(group_name_, params, DEFAULT_PRIORITY,
3793 callback_.callback(), pool, BoundNetLog());
3796 int RequestSocketWithoutLimits(TestClientSocketPool* pool) {
3797 scoped_refptr<TestSocketParams> params(
3798 new TestSocketParams(true /* ignore_limits */));
3799 return handle_.Init(group_name_, params, MAXIMUM_PRIORITY,
3800 callback_.callback(), pool, BoundNetLog());
3803 bool ReleaseOneConnection() {
3804 if (!handle_.is_initialized() || !can_release_connection_) {
3805 return false;
3807 handle_.socket()->Disconnect();
3808 handle_.Reset();
3809 return true;
3812 void set_can_release_connection(bool can_release_connection) {
3813 can_release_connection_ = can_release_connection;
3816 MOCK_METHOD0(CloseOneIdleConnection, bool());
3818 private:
3819 TestClientSocketPool* const pool_;
3820 ClientSocketHandle handle_;
3821 TestCompletionCallback callback_;
3822 const std::string group_name_;
3823 bool can_release_connection_;
3826 TEST_F(ClientSocketPoolBaseTest, FailToCloseIdleSocketsNotHeldByLayeredPool) {
3827 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
3828 connect_job_factory_->set_job_type(TestConnectJob::kMockJob);
3830 MockLayeredPool mock_layered_pool(pool_.get(), "foo");
3831 EXPECT_EQ(OK, mock_layered_pool.RequestSocket(pool_.get()));
3832 EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection())
3833 .WillOnce(Return(false));
3834 EXPECT_FALSE(pool_->CloseOneIdleConnectionInHigherLayeredPool());
3837 TEST_F(ClientSocketPoolBaseTest, ForciblyCloseIdleSocketsHeldByLayeredPool) {
3838 CreatePool(kDefaultMaxSockets, kDefaultMaxSocketsPerGroup);
3839 connect_job_factory_->set_job_type(TestConnectJob::kMockJob);
3841 MockLayeredPool mock_layered_pool(pool_.get(), "foo");
3842 EXPECT_EQ(OK, mock_layered_pool.RequestSocket(pool_.get()));
3843 EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection())
3844 .WillOnce(Invoke(&mock_layered_pool,
3845 &MockLayeredPool::ReleaseOneConnection));
3846 EXPECT_TRUE(pool_->CloseOneIdleConnectionInHigherLayeredPool());
3849 // Tests the basic case of closing an idle socket in a higher layered pool when
3850 // a new request is issued and the lower layer pool is stalled.
3851 TEST_F(ClientSocketPoolBaseTest, CloseIdleSocketsHeldByLayeredPoolWhenNeeded) {
3852 CreatePool(1, 1);
3853 connect_job_factory_->set_job_type(TestConnectJob::kMockJob);
3855 MockLayeredPool mock_layered_pool(pool_.get(), "foo");
3856 EXPECT_EQ(OK, mock_layered_pool.RequestSocket(pool_.get()));
3857 EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection())
3858 .WillOnce(Invoke(&mock_layered_pool,
3859 &MockLayeredPool::ReleaseOneConnection));
3860 ClientSocketHandle handle;
3861 TestCompletionCallback callback;
3862 EXPECT_EQ(ERR_IO_PENDING, handle.Init("a",
3863 params_,
3864 DEFAULT_PRIORITY,
3865 callback.callback(),
3866 pool_.get(),
3867 BoundNetLog()));
3868 EXPECT_EQ(OK, callback.WaitForResult());
3871 // Same as above, but the idle socket is in the same group as the stalled
3872 // socket, and closes the only other request in its group when closing requests
3873 // in higher layered pools. This generally shouldn't happen, but it may be
3874 // possible if a higher level pool issues a request and the request is
3875 // subsequently cancelled. Even if it's not possible, best not to crash.
3876 TEST_F(ClientSocketPoolBaseTest,
3877 CloseIdleSocketsHeldByLayeredPoolWhenNeededSameGroup) {
3878 CreatePool(2, 2);
3879 connect_job_factory_->set_job_type(TestConnectJob::kMockJob);
3881 // Need a socket in another group for the pool to be stalled (If a group
3882 // has the maximum number of connections already, it's not stalled).
3883 ClientSocketHandle handle1;
3884 TestCompletionCallback callback1;
3885 EXPECT_EQ(OK, handle1.Init("group1",
3886 params_,
3887 DEFAULT_PRIORITY,
3888 callback1.callback(),
3889 pool_.get(),
3890 BoundNetLog()));
3892 MockLayeredPool mock_layered_pool(pool_.get(), "group2");
3893 EXPECT_EQ(OK, mock_layered_pool.RequestSocket(pool_.get()));
3894 EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection())
3895 .WillOnce(Invoke(&mock_layered_pool,
3896 &MockLayeredPool::ReleaseOneConnection));
3897 ClientSocketHandle handle;
3898 TestCompletionCallback callback2;
3899 EXPECT_EQ(ERR_IO_PENDING, handle.Init("group2",
3900 params_,
3901 DEFAULT_PRIORITY,
3902 callback2.callback(),
3903 pool_.get(),
3904 BoundNetLog()));
3905 EXPECT_EQ(OK, callback2.WaitForResult());
3908 // Tests the case when an idle socket can be closed when a new request is
3909 // issued, and the new request belongs to a group that was previously stalled.
3910 TEST_F(ClientSocketPoolBaseTest,
3911 CloseIdleSocketsHeldByLayeredPoolInSameGroupWhenNeeded) {
3912 CreatePool(2, 2);
3913 std::list<TestConnectJob::JobType> job_types;
3914 job_types.push_back(TestConnectJob::kMockJob);
3915 job_types.push_back(TestConnectJob::kMockJob);
3916 job_types.push_back(TestConnectJob::kMockJob);
3917 job_types.push_back(TestConnectJob::kMockJob);
3918 connect_job_factory_->set_job_types(&job_types);
3920 ClientSocketHandle handle1;
3921 TestCompletionCallback callback1;
3922 EXPECT_EQ(OK, handle1.Init("group1",
3923 params_,
3924 DEFAULT_PRIORITY,
3925 callback1.callback(),
3926 pool_.get(),
3927 BoundNetLog()));
3929 MockLayeredPool mock_layered_pool(pool_.get(), "group2");
3930 EXPECT_EQ(OK, mock_layered_pool.RequestSocket(pool_.get()));
3931 EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection())
3932 .WillRepeatedly(Invoke(&mock_layered_pool,
3933 &MockLayeredPool::ReleaseOneConnection));
3934 mock_layered_pool.set_can_release_connection(false);
3936 // The third request is made when the socket pool is in a stalled state.
3937 ClientSocketHandle handle3;
3938 TestCompletionCallback callback3;
3939 EXPECT_EQ(ERR_IO_PENDING, handle3.Init("group3",
3940 params_,
3941 DEFAULT_PRIORITY,
3942 callback3.callback(),
3943 pool_.get(),
3944 BoundNetLog()));
3946 base::RunLoop().RunUntilIdle();
3947 EXPECT_FALSE(callback3.have_result());
3949 // The fourth request is made when the pool is no longer stalled. The third
3950 // request should be serviced first, since it was issued first and has the
3951 // same priority.
3952 mock_layered_pool.set_can_release_connection(true);
3953 ClientSocketHandle handle4;
3954 TestCompletionCallback callback4;
3955 EXPECT_EQ(ERR_IO_PENDING, handle4.Init("group3",
3956 params_,
3957 DEFAULT_PRIORITY,
3958 callback4.callback(),
3959 pool_.get(),
3960 BoundNetLog()));
3961 EXPECT_EQ(OK, callback3.WaitForResult());
3962 EXPECT_FALSE(callback4.have_result());
3964 // Closing a handle should free up another socket slot.
3965 handle1.Reset();
3966 EXPECT_EQ(OK, callback4.WaitForResult());
3969 // Tests the case when an idle socket can be closed when a new request is
3970 // issued, and the new request belongs to a group that was previously stalled.
3972 // The two differences from the above test are that the stalled requests are not
3973 // in the same group as the layered pool's request, and the the fourth request
3974 // has a higher priority than the third one, so gets a socket first.
3975 TEST_F(ClientSocketPoolBaseTest,
3976 CloseIdleSocketsHeldByLayeredPoolInSameGroupWhenNeeded2) {
3977 CreatePool(2, 2);
3978 std::list<TestConnectJob::JobType> job_types;
3979 job_types.push_back(TestConnectJob::kMockJob);
3980 job_types.push_back(TestConnectJob::kMockJob);
3981 job_types.push_back(TestConnectJob::kMockJob);
3982 job_types.push_back(TestConnectJob::kMockJob);
3983 connect_job_factory_->set_job_types(&job_types);
3985 ClientSocketHandle handle1;
3986 TestCompletionCallback callback1;
3987 EXPECT_EQ(OK, handle1.Init("group1",
3988 params_,
3989 DEFAULT_PRIORITY,
3990 callback1.callback(),
3991 pool_.get(),
3992 BoundNetLog()));
3994 MockLayeredPool mock_layered_pool(pool_.get(), "group2");
3995 EXPECT_EQ(OK, mock_layered_pool.RequestSocket(pool_.get()));
3996 EXPECT_CALL(mock_layered_pool, CloseOneIdleConnection())
3997 .WillRepeatedly(Invoke(&mock_layered_pool,
3998 &MockLayeredPool::ReleaseOneConnection));
3999 mock_layered_pool.set_can_release_connection(false);
4001 // The third request is made when the socket pool is in a stalled state.
4002 ClientSocketHandle handle3;
4003 TestCompletionCallback callback3;
4004 EXPECT_EQ(ERR_IO_PENDING, handle3.Init("group3",
4005 params_,
4006 MEDIUM,
4007 callback3.callback(),
4008 pool_.get(),
4009 BoundNetLog()));
4011 base::RunLoop().RunUntilIdle();
4012 EXPECT_FALSE(callback3.have_result());
4014 // The fourth request is made when the pool is no longer stalled. This
4015 // request has a higher priority than the third request, so is serviced first.
4016 mock_layered_pool.set_can_release_connection(true);
4017 ClientSocketHandle handle4;
4018 TestCompletionCallback callback4;
4019 EXPECT_EQ(ERR_IO_PENDING, handle4.Init("group3",
4020 params_,
4021 HIGHEST,
4022 callback4.callback(),
4023 pool_.get(),
4024 BoundNetLog()));
4025 EXPECT_EQ(OK, callback4.WaitForResult());
4026 EXPECT_FALSE(callback3.have_result());
4028 // Closing a handle should free up another socket slot.
4029 handle1.Reset();
4030 EXPECT_EQ(OK, callback3.WaitForResult());
4033 TEST_F(ClientSocketPoolBaseTest,
4034 CloseMultipleIdleSocketsHeldByLayeredPoolWhenNeeded) {
4035 CreatePool(1, 1);
4036 connect_job_factory_->set_job_type(TestConnectJob::kMockJob);
4038 MockLayeredPool mock_layered_pool1(pool_.get(), "foo");
4039 EXPECT_EQ(OK, mock_layered_pool1.RequestSocket(pool_.get()));
4040 EXPECT_CALL(mock_layered_pool1, CloseOneIdleConnection())
4041 .WillRepeatedly(Invoke(&mock_layered_pool1,
4042 &MockLayeredPool::ReleaseOneConnection));
4043 MockLayeredPool mock_layered_pool2(pool_.get(), "bar");
4044 EXPECT_EQ(OK, mock_layered_pool2.RequestSocketWithoutLimits(pool_.get()));
4045 EXPECT_CALL(mock_layered_pool2, CloseOneIdleConnection())
4046 .WillRepeatedly(Invoke(&mock_layered_pool2,
4047 &MockLayeredPool::ReleaseOneConnection));
4048 ClientSocketHandle handle;
4049 TestCompletionCallback callback;
4050 EXPECT_EQ(ERR_IO_PENDING, handle.Init("a",
4051 params_,
4052 DEFAULT_PRIORITY,
4053 callback.callback(),
4054 pool_.get(),
4055 BoundNetLog()));
4056 EXPECT_EQ(OK, callback.WaitForResult());
4059 // Test that when a socket pool and group are at their limits, a request
4060 // with |ignore_limits| triggers creation of a new socket, and gets the socket
4061 // instead of a request with the same priority that was issued earlier, but
4062 // that does not have |ignore_limits| set.
4063 TEST_F(ClientSocketPoolBaseTest, IgnoreLimits) {
4064 scoped_refptr<TestSocketParams> params_ignore_limits(
4065 new TestSocketParams(true /* ignore_limits */));
4066 CreatePool(1, 1);
4068 // Issue a request to reach the socket pool limit.
4069 EXPECT_EQ(OK, StartRequestWithParams("a", MAXIMUM_PRIORITY, params_));
4070 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a"));
4072 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
4074 EXPECT_EQ(ERR_IO_PENDING, StartRequestWithParams("a", MAXIMUM_PRIORITY,
4075 params_));
4076 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a"));
4078 EXPECT_EQ(ERR_IO_PENDING, StartRequestWithParams("a", MAXIMUM_PRIORITY,
4079 params_ignore_limits));
4080 ASSERT_EQ(1, pool_->NumConnectJobsInGroup("a"));
4082 EXPECT_EQ(OK, request(2)->WaitForResult());
4083 EXPECT_FALSE(request(1)->have_result());
4086 // Test that when a socket pool and group are at their limits, a ConnectJob
4087 // issued for a request with |ignore_limits| set is not cancelled when a request
4088 // without |ignore_limits| issued to the same group is cancelled.
4089 TEST_F(ClientSocketPoolBaseTest, IgnoreLimitsCancelOtherJob) {
4090 scoped_refptr<TestSocketParams> params_ignore_limits(
4091 new TestSocketParams(true /* ignore_limits */));
4092 CreatePool(1, 1);
4094 // Issue a request to reach the socket pool limit.
4095 EXPECT_EQ(OK, StartRequestWithParams("a", MAXIMUM_PRIORITY, params_));
4096 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a"));
4098 connect_job_factory_->set_job_type(TestConnectJob::kMockPendingJob);
4100 EXPECT_EQ(ERR_IO_PENDING, StartRequestWithParams("a", MAXIMUM_PRIORITY,
4101 params_));
4102 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("a"));
4104 EXPECT_EQ(ERR_IO_PENDING, StartRequestWithParams("a", MAXIMUM_PRIORITY,
4105 params_ignore_limits));
4106 ASSERT_EQ(1, pool_->NumConnectJobsInGroup("a"));
4108 // Cancel the pending request without ignore_limits set. The ConnectJob
4109 // should not be cancelled.
4110 request(1)->handle()->Reset();
4111 ASSERT_EQ(1, pool_->NumConnectJobsInGroup("a"));
4113 EXPECT_EQ(OK, request(2)->WaitForResult());
4114 EXPECT_FALSE(request(1)->have_result());
4117 } // namespace
4119 } // namespace net