OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
11 #include "base/callback.h" | 11 #include "base/callback.h" |
12 #include "base/compiler_specific.h" | |
13 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
14 #include "base/memory/scoped_vector.h" | 13 #include "base/memory/scoped_vector.h" |
15 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
16 #include "base/message_loop.h" | 15 #include "base/message_loop.h" |
17 #include "base/run_loop.h" | 16 #include "base/run_loop.h" |
18 #include "base/stringprintf.h" | 17 #include "base/stringprintf.h" |
19 #include "base/string_number_conversions.h" | 18 #include "base/string_number_conversions.h" |
20 #include "base/threading/platform_thread.h" | 19 #include "base/threading/platform_thread.h" |
21 #include "base/values.h" | 20 #include "base/values.h" |
22 #include "net/base/load_timing_info.h" | 21 #include "net/base/load_timing_info.h" |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 | 111 |
113 bool ignore_limits_; | 112 bool ignore_limits_; |
114 }; | 113 }; |
115 typedef ClientSocketPoolBase<TestSocketParams> TestClientSocketPoolBase; | 114 typedef ClientSocketPoolBase<TestSocketParams> TestClientSocketPoolBase; |
116 | 115 |
117 class MockClientSocket : public StreamSocket { | 116 class MockClientSocket : public StreamSocket { |
118 public: | 117 public: |
119 explicit MockClientSocket(net::NetLog* net_log) | 118 explicit MockClientSocket(net::NetLog* net_log) |
120 : connected_(false), | 119 : connected_(false), |
121 net_log_(BoundNetLog::Make(net_log, net::NetLog::SOURCE_SOCKET)), | 120 net_log_(BoundNetLog::Make(net_log, net::NetLog::SOURCE_SOCKET)), |
122 was_used_to_convey_data_(false), | 121 was_used_to_convey_data_(false) { |
123 num_bytes_read_(0) { | |
124 } | 122 } |
125 | 123 |
126 // Socket implementation. | 124 // Socket implementation. |
127 virtual int Read( | 125 virtual int Read( |
128 IOBuffer* /* buf */, int len, | 126 IOBuffer* /* buf */, int len, |
129 const CompletionCallback& /* callback */) OVERRIDE { | 127 const CompletionCallback& /* callback */) OVERRIDE { |
130 num_bytes_read_ += len; | 128 return ERR_UNEXPECTED; |
131 return len; | |
132 } | 129 } |
133 | 130 |
134 virtual int Write( | 131 virtual int Write( |
135 IOBuffer* /* buf */, int len, | 132 IOBuffer* /* buf */, int len, |
136 const CompletionCallback& /* callback */) OVERRIDE { | 133 const CompletionCallback& /* callback */) OVERRIDE { |
137 was_used_to_convey_data_ = true; | 134 was_used_to_convey_data_ = true; |
138 return len; | 135 return len; |
139 } | 136 } |
140 virtual bool SetReceiveBufferSize(int32 size) OVERRIDE { return true; } | 137 virtual bool SetReceiveBufferSize(int32 size) OVERRIDE { return true; } |
141 virtual bool SetSendBufferSize(int32 size) OVERRIDE { return true; } | 138 virtual bool SetSendBufferSize(int32 size) OVERRIDE { return true; } |
(...skipping 16 matching lines...) Expand all Loading... |
158 return ERR_UNEXPECTED; | 155 return ERR_UNEXPECTED; |
159 } | 156 } |
160 | 157 |
161 virtual const BoundNetLog& NetLog() const OVERRIDE { | 158 virtual const BoundNetLog& NetLog() const OVERRIDE { |
162 return net_log_; | 159 return net_log_; |
163 } | 160 } |
164 | 161 |
165 virtual void SetSubresourceSpeculation() OVERRIDE {} | 162 virtual void SetSubresourceSpeculation() OVERRIDE {} |
166 virtual void SetOmniboxSpeculation() OVERRIDE {} | 163 virtual void SetOmniboxSpeculation() OVERRIDE {} |
167 virtual bool WasEverUsed() const OVERRIDE { | 164 virtual bool WasEverUsed() const OVERRIDE { |
168 return was_used_to_convey_data_ || num_bytes_read_ > 0; | 165 return was_used_to_convey_data_; |
169 } | 166 } |
170 virtual bool UsingTCPFastOpen() const OVERRIDE { return false; } | 167 virtual bool UsingTCPFastOpen() const OVERRIDE { return false; } |
171 virtual int64 NumBytesRead() const OVERRIDE { return num_bytes_read_; } | |
172 virtual base::TimeDelta GetConnectTimeMicros() const OVERRIDE { | |
173 static const base::TimeDelta kDummyConnectTimeMicros = | |
174 base::TimeDelta::FromMicroseconds(10); | |
175 return kDummyConnectTimeMicros; // Dummy value. | |
176 } | |
177 virtual bool WasNpnNegotiated() const OVERRIDE { | 168 virtual bool WasNpnNegotiated() const OVERRIDE { |
178 return false; | 169 return false; |
179 } | 170 } |
180 virtual NextProto GetNegotiatedProtocol() const OVERRIDE { | 171 virtual NextProto GetNegotiatedProtocol() const OVERRIDE { |
181 return kProtoUnknown; | 172 return kProtoUnknown; |
182 } | 173 } |
183 virtual bool GetSSLInfo(SSLInfo* ssl_info) OVERRIDE { | 174 virtual bool GetSSLInfo(SSLInfo* ssl_info) OVERRIDE { |
184 return false; | 175 return false; |
185 } | 176 } |
186 | 177 |
187 private: | 178 private: |
188 bool connected_; | 179 bool connected_; |
189 BoundNetLog net_log_; | 180 BoundNetLog net_log_; |
190 bool was_used_to_convey_data_; | 181 bool was_used_to_convey_data_; |
191 int num_bytes_read_; | |
192 | 182 |
193 DISALLOW_COPY_AND_ASSIGN(MockClientSocket); | 183 DISALLOW_COPY_AND_ASSIGN(MockClientSocket); |
194 }; | 184 }; |
195 | 185 |
196 class TestConnectJob; | 186 class TestConnectJob; |
197 | 187 |
198 class MockClientSocketFactory : public ClientSocketFactory { | 188 class MockClientSocketFactory : public ClientSocketFactory { |
199 public: | 189 public: |
200 MockClientSocketFactory() : allocation_count_(0) {} | 190 MockClientSocketFactory() : allocation_count_(0) {} |
201 | 191 |
(...skipping 536 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
738 bool connect_backup_jobs_enabled_; | 728 bool connect_backup_jobs_enabled_; |
739 bool cleanup_timer_enabled_; | 729 bool cleanup_timer_enabled_; |
740 MockClientSocketFactory client_socket_factory_; | 730 MockClientSocketFactory client_socket_factory_; |
741 TestConnectJobFactory* connect_job_factory_; | 731 TestConnectJobFactory* connect_job_factory_; |
742 scoped_refptr<TestSocketParams> params_; | 732 scoped_refptr<TestSocketParams> params_; |
743 ClientSocketPoolHistograms histograms_; | 733 ClientSocketPoolHistograms histograms_; |
744 scoped_ptr<TestClientSocketPool> pool_; | 734 scoped_ptr<TestClientSocketPool> pool_; |
745 ClientSocketPoolTest test_base_; | 735 ClientSocketPoolTest test_base_; |
746 }; | 736 }; |
747 | 737 |
748 TEST_F(ClientSocketPoolBaseTest, AssignIdleSocketToGroup_WarmestSocket) { | |
749 CreatePool(4, 4); | |
750 net::SetSocketReusePolicy(0); | |
751 | |
752 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); | |
753 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); | |
754 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); | |
755 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); | |
756 | |
757 std::map<int, StreamSocket*> sockets_; | |
758 for (size_t i = 0; i < test_base_.requests_size(); i++) { | |
759 TestSocketRequest* req = test_base_.request(i); | |
760 StreamSocket* s = req->handle()->socket(); | |
761 MockClientSocket* sock = static_cast<MockClientSocket*>(s); | |
762 CHECK(sock); | |
763 sockets_[i] = sock; | |
764 sock->Read(NULL, 1024 - i, CompletionCallback()); | |
765 } | |
766 | |
767 ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE); | |
768 | |
769 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); | |
770 TestSocketRequest* req = test_base_.request(test_base_.requests_size() - 1); | |
771 | |
772 // First socket is warmest. | |
773 EXPECT_EQ(sockets_[0], req->handle()->socket()); | |
774 | |
775 // Test that NumBytes are as expected. | |
776 EXPECT_EQ(1024, sockets_[0]->NumBytesRead()); | |
777 EXPECT_EQ(1023, sockets_[1]->NumBytesRead()); | |
778 EXPECT_EQ(1022, sockets_[2]->NumBytesRead()); | |
779 EXPECT_EQ(1021, sockets_[3]->NumBytesRead()); | |
780 | |
781 ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); | |
782 } | |
783 | |
784 TEST_F(ClientSocketPoolBaseTest, AssignIdleSocketToGroup_LastAccessedSocket) { | |
785 CreatePool(4, 4); | |
786 net::SetSocketReusePolicy(2); | |
787 | |
788 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); | |
789 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); | |
790 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); | |
791 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); | |
792 | |
793 std::map<int, StreamSocket*> sockets_; | |
794 for (size_t i = 0; i < test_base_.requests_size(); i++) { | |
795 TestSocketRequest* req = test_base_.request(i); | |
796 StreamSocket* s = req->handle()->socket(); | |
797 MockClientSocket* sock = static_cast<MockClientSocket*>(s); | |
798 CHECK(sock); | |
799 sockets_[i] = sock; | |
800 sock->Read(NULL, 1024 - i, CompletionCallback()); | |
801 } | |
802 | |
803 ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE); | |
804 | |
805 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); | |
806 TestSocketRequest* req = test_base_.request(test_base_.requests_size() - 1); | |
807 | |
808 // Last socket is most recently accessed. | |
809 EXPECT_EQ(sockets_[3], req->handle()->socket()); | |
810 ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); | |
811 } | |
812 | |
813 // Even though a timeout is specified, it doesn't time out on a synchronous | 738 // Even though a timeout is specified, it doesn't time out on a synchronous |
814 // completion. | 739 // completion. |
815 TEST_F(ClientSocketPoolBaseTest, ConnectJob_NoTimeoutOnSynchronousCompletion) { | 740 TEST_F(ClientSocketPoolBaseTest, ConnectJob_NoTimeoutOnSynchronousCompletion) { |
816 TestConnectJobDelegate delegate; | 741 TestConnectJobDelegate delegate; |
817 ClientSocketHandle ignored; | 742 ClientSocketHandle ignored; |
818 TestClientSocketPoolBase::Request request( | 743 TestClientSocketPoolBase::Request request( |
819 &ignored, CompletionCallback(), kDefaultPriority, | 744 &ignored, CompletionCallback(), kDefaultPriority, |
820 internal::ClientSocketPoolBaseHelper::NORMAL, | 745 internal::ClientSocketPoolBaseHelper::NORMAL, |
821 false, params_, BoundNetLog()); | 746 false, params_, BoundNetLog()); |
822 scoped_ptr<TestConnectJob> job( | 747 scoped_ptr<TestConnectJob> job( |
(...skipping 2810 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3633 // complete. | 3558 // complete. |
3634 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a")); | 3559 EXPECT_EQ(1, pool_->NumConnectJobsInGroup("a")); |
3635 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a")); | 3560 EXPECT_EQ(0, pool_->NumUnassignedConnectJobsInGroup("a")); |
3636 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a")); | 3561 EXPECT_EQ(0, pool_->IdleSocketCountInGroup("a")); |
3637 EXPECT_EQ(1, pool_->NumActiveSocketsInGroup("a")); | 3562 EXPECT_EQ(1, pool_->NumActiveSocketsInGroup("a")); |
3638 } | 3563 } |
3639 | 3564 |
3640 } // namespace | 3565 } // namespace |
3641 | 3566 |
3642 } // namespace net | 3567 } // namespace net |
OLD | NEW |