OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/socket/client_socket_pool_base.h" | 5 #include "net/socket/client_socket_pool_base.h" |
6 | 6 |
7 #include "base/callback.h" | 7 #include "base/callback.h" |
8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
10 #include "base/memory/scoped_vector.h" | 10 #include "base/memory/scoped_vector.h" |
(...skipping 27 matching lines...) Expand all Loading... |
38 public: | 38 public: |
39 bool ignore_limits() { return false; } | 39 bool ignore_limits() { return false; } |
40 private: | 40 private: |
41 friend class base::RefCounted<TestSocketParams>; | 41 friend class base::RefCounted<TestSocketParams>; |
42 ~TestSocketParams() {} | 42 ~TestSocketParams() {} |
43 }; | 43 }; |
44 typedef ClientSocketPoolBase<TestSocketParams> TestClientSocketPoolBase; | 44 typedef ClientSocketPoolBase<TestSocketParams> TestClientSocketPoolBase; |
45 | 45 |
46 class MockClientSocket : public StreamSocket { | 46 class MockClientSocket : public StreamSocket { |
47 public: | 47 public: |
48 MockClientSocket() : connected_(false), was_used_to_convey_data_(false), | 48 MockClientSocket() : connected_(false), was_used_to_convey_data_(false) {} |
49 num_bytes_read_(0) {} | |
50 | 49 |
51 // Socket methods: | 50 // Socket methods: |
52 virtual int Read( | 51 virtual int Read( |
53 IOBuffer* /* buf */, int len, CompletionCallback* /* callback */) { | 52 IOBuffer* /* buf */, int /* len */, CompletionCallback* /* callback */) { |
54 num_bytes_read_ += len; | 53 return ERR_UNEXPECTED; |
55 return len; | |
56 } | 54 } |
57 | 55 |
58 virtual int Write( | 56 virtual int Write( |
59 IOBuffer* /* buf */, int len, CompletionCallback* /* callback */) { | 57 IOBuffer* /* buf */, int len, CompletionCallback* /* callback */) { |
60 was_used_to_convey_data_ = true; | 58 was_used_to_convey_data_ = true; |
61 return len; | 59 return len; |
62 } | 60 } |
63 virtual bool SetReceiveBufferSize(int32 size) { return true; } | 61 virtual bool SetReceiveBufferSize(int32 size) { return true; } |
64 virtual bool SetSendBufferSize(int32 size) { return true; } | 62 virtual bool SetSendBufferSize(int32 size) { return true; } |
65 | 63 |
(...skipping 15 matching lines...) Expand all Loading... |
81 virtual int GetLocalAddress(IPEndPoint* /* address */) const { | 79 virtual int GetLocalAddress(IPEndPoint* /* address */) const { |
82 return ERR_UNEXPECTED; | 80 return ERR_UNEXPECTED; |
83 } | 81 } |
84 | 82 |
85 virtual const BoundNetLog& NetLog() const { | 83 virtual const BoundNetLog& NetLog() const { |
86 return net_log_; | 84 return net_log_; |
87 } | 85 } |
88 | 86 |
89 virtual void SetSubresourceSpeculation() {} | 87 virtual void SetSubresourceSpeculation() {} |
90 virtual void SetOmniboxSpeculation() {} | 88 virtual void SetOmniboxSpeculation() {} |
91 virtual bool WasEverUsed() const { | 89 virtual bool WasEverUsed() const { return was_used_to_convey_data_; } |
92 return was_used_to_convey_data_ || num_bytes_read_ > 0; | |
93 } | |
94 virtual bool UsingTCPFastOpen() const { return false; } | 90 virtual bool UsingTCPFastOpen() const { return false; } |
95 virtual int64 NumBytesRead() const { return num_bytes_read_; } | |
96 virtual base::TimeDelta GetConnectTimeMicros() const { | |
97 static const base::TimeDelta kDummyConnectTimeMicros = | |
98 base::TimeDelta::FromMicroseconds(10); | |
99 return kDummyConnectTimeMicros; // Dummy value. | |
100 } | |
101 | 91 |
102 private: | 92 private: |
103 bool connected_; | 93 bool connected_; |
104 BoundNetLog net_log_; | 94 BoundNetLog net_log_; |
105 bool was_used_to_convey_data_; | 95 bool was_used_to_convey_data_; |
106 int num_bytes_read_; | |
107 | 96 |
108 DISALLOW_COPY_AND_ASSIGN(MockClientSocket); | 97 DISALLOW_COPY_AND_ASSIGN(MockClientSocket); |
109 }; | 98 }; |
110 | 99 |
111 class TestConnectJob; | 100 class TestConnectJob; |
112 | 101 |
113 class MockClientSocketFactory : public ClientSocketFactory { | 102 class MockClientSocketFactory : public ClientSocketFactory { |
114 public: | 103 public: |
115 MockClientSocketFactory() : allocation_count_(0) {} | 104 MockClientSocketFactory() : allocation_count_(0) {} |
116 | 105 |
(...skipping 491 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
608 | 597 |
609 bool connect_backup_jobs_enabled_; | 598 bool connect_backup_jobs_enabled_; |
610 MockClientSocketFactory client_socket_factory_; | 599 MockClientSocketFactory client_socket_factory_; |
611 TestConnectJobFactory* connect_job_factory_; | 600 TestConnectJobFactory* connect_job_factory_; |
612 scoped_refptr<TestSocketParams> params_; | 601 scoped_refptr<TestSocketParams> params_; |
613 ClientSocketPoolHistograms histograms_; | 602 ClientSocketPoolHistograms histograms_; |
614 scoped_ptr<TestClientSocketPool> pool_; | 603 scoped_ptr<TestClientSocketPool> pool_; |
615 ClientSocketPoolTest test_base_; | 604 ClientSocketPoolTest test_base_; |
616 }; | 605 }; |
617 | 606 |
618 TEST_F(ClientSocketPoolBaseTest, AssignIdleSocketToGroup_WarmestSocket) { | |
619 CreatePool(4, 4); | |
620 net::SetSocketReusePolicy(0); | |
621 | |
622 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); | |
623 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); | |
624 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); | |
625 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); | |
626 | |
627 std::map<int, StreamSocket*> sockets_; | |
628 for (size_t i = 0; i < test_base_.requests_size(); i++) { | |
629 TestSocketRequest* req = test_base_.request(i); | |
630 StreamSocket* s = req->handle()->socket(); | |
631 MockClientSocket* sock = static_cast<MockClientSocket*>(s); | |
632 CHECK(sock); | |
633 sockets_[i] = sock; | |
634 sock->Read(NULL, 1024 - i, NULL); | |
635 } | |
636 | |
637 ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE); | |
638 | |
639 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); | |
640 TestSocketRequest* req = test_base_.request(test_base_.requests_size() - 1); | |
641 | |
642 // First socket is warmest. | |
643 EXPECT_EQ(sockets_[0], req->handle()->socket()); | |
644 | |
645 // Test that NumBytes are as expected. | |
646 EXPECT_EQ(1024, sockets_[0]->NumBytesRead()); | |
647 EXPECT_EQ(1023, sockets_[1]->NumBytesRead()); | |
648 EXPECT_EQ(1022, sockets_[2]->NumBytesRead()); | |
649 EXPECT_EQ(1021, sockets_[3]->NumBytesRead()); | |
650 | |
651 ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); | |
652 } | |
653 | |
654 TEST_F(ClientSocketPoolBaseTest, AssignIdleSocketToGroup_LastAccessedSocket) { | |
655 CreatePool(4, 4); | |
656 net::SetSocketReusePolicy(2); | |
657 | |
658 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); | |
659 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); | |
660 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); | |
661 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); | |
662 | |
663 std::map<int, StreamSocket*> sockets_; | |
664 for (size_t i = 0; i < test_base_.requests_size(); i++) { | |
665 TestSocketRequest* req = test_base_.request(i); | |
666 StreamSocket* s = req->handle()->socket(); | |
667 MockClientSocket* sock = static_cast<MockClientSocket*>(s); | |
668 CHECK(sock); | |
669 sockets_[i] = sock; | |
670 sock->Read(NULL, 1024 - i, NULL); | |
671 } | |
672 | |
673 ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE); | |
674 | |
675 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); | |
676 TestSocketRequest* req = test_base_.request(test_base_.requests_size() - 1); | |
677 | |
678 // Last socket is most recently accessed. | |
679 EXPECT_EQ(sockets_[3], req->handle()->socket()); | |
680 ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); | |
681 } | |
682 | |
683 // Even though a timeout is specified, it doesn't time out on a synchronous | 607 // Even though a timeout is specified, it doesn't time out on a synchronous |
684 // completion. | 608 // completion. |
685 TEST_F(ClientSocketPoolBaseTest, ConnectJob_NoTimeoutOnSynchronousCompletion) { | 609 TEST_F(ClientSocketPoolBaseTest, ConnectJob_NoTimeoutOnSynchronousCompletion) { |
686 TestConnectJobDelegate delegate; | 610 TestConnectJobDelegate delegate; |
687 ClientSocketHandle ignored; | 611 ClientSocketHandle ignored; |
688 TestClientSocketPoolBase::Request request( | 612 TestClientSocketPoolBase::Request request( |
689 &ignored, NULL, kDefaultPriority, | 613 &ignored, NULL, kDefaultPriority, |
690 internal::ClientSocketPoolBaseHelper::NORMAL, | 614 internal::ClientSocketPoolBaseHelper::NORMAL, |
691 false, params_, BoundNetLog()); | 615 false, params_, BoundNetLog()); |
692 scoped_ptr<TestConnectJob> job( | 616 scoped_ptr<TestConnectJob> job( |
(...skipping 2571 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3264 // The hung connect job should still be there, but everything else should be | 3188 // The hung connect job should still be there, but everything else should be |
3265 // complete. | 3189 // complete. |
3266 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a")); | 3190 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a")); |
3267 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a")); | 3191 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a")); |
3268 EXPECT_EQ(1, pool_->NumActiveSocketsInGroup("a")); | 3192 EXPECT_EQ(1, pool_->NumActiveSocketsInGroup("a")); |
3269 } | 3193 } |
3270 | 3194 |
3271 } // namespace | 3195 } // namespace |
3272 | 3196 |
3273 } // namespace net | 3197 } // namespace net |
OLD | NEW |