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