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" |
11 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
12 #include "base/string_number_conversions.h" | 12 #include "base/string_number_conversions.h" |
13 #include "base/string_util.h" | 13 #include "base/string_util.h" |
14 #include "base/threading/platform_thread.h" | 14 #include "base/threading/platform_thread.h" |
15 #include "net/base/net_errors.h" | 15 #include "net/base/net_errors.h" |
16 #include "net/base/net_log.h" | 16 #include "net/base/net_log.h" |
17 #include "net/base/net_log_unittest.h" | 17 #include "net/base/net_log_unittest.h" |
18 #include "net/base/request_priority.h" | 18 #include "net/base/request_priority.h" |
19 #include "net/base/test_completion_callback.h" | 19 #include "net/base/test_completion_callback.h" |
20 #include "net/http/http_response_headers.h" | 20 #include "net/http/http_response_headers.h" |
21 #include "net/socket/client_socket.h" | |
22 #include "net/socket/client_socket_factory.h" | 21 #include "net/socket/client_socket_factory.h" |
23 #include "net/socket/client_socket_handle.h" | 22 #include "net/socket/client_socket_handle.h" |
24 #include "net/socket/client_socket_pool_histograms.h" | 23 #include "net/socket/client_socket_pool_histograms.h" |
25 #include "net/socket/socket_test_util.h" | 24 #include "net/socket/socket_test_util.h" |
26 #include "net/socket/ssl_host_info.h" | 25 #include "net/socket/ssl_host_info.h" |
| 26 #include "net/socket/stream_socket.h" |
27 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
28 | 28 |
29 namespace net { | 29 namespace net { |
30 | 30 |
31 namespace { | 31 namespace { |
32 | 32 |
33 const int kDefaultMaxSockets = 4; | 33 const int kDefaultMaxSockets = 4; |
34 const int kDefaultMaxSocketsPerGroup = 2; | 34 const int kDefaultMaxSocketsPerGroup = 2; |
35 const net::RequestPriority kDefaultPriority = MEDIUM; | 35 const net::RequestPriority kDefaultPriority = MEDIUM; |
36 | 36 |
37 class TestSocketParams : public base::RefCounted<TestSocketParams> { | 37 class TestSocketParams : public base::RefCounted<TestSocketParams> { |
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 ClientSocket { | 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 | 49 |
50 // Socket methods: | 50 // Socket methods: |
51 virtual int Read( | 51 virtual int Read( |
52 IOBuffer* /* buf */, int /* len */, CompletionCallback* /* callback */) { | 52 IOBuffer* /* buf */, int /* len */, CompletionCallback* /* callback */) { |
53 return ERR_UNEXPECTED; | 53 return ERR_UNEXPECTED; |
54 } | 54 } |
55 | 55 |
56 virtual int Write( | 56 virtual int Write( |
57 IOBuffer* /* buf */, int len, CompletionCallback* /* callback */) { | 57 IOBuffer* /* buf */, int len, CompletionCallback* /* callback */) { |
58 was_used_to_convey_data_ = true; | 58 was_used_to_convey_data_ = true; |
59 return len; | 59 return len; |
60 } | 60 } |
61 virtual bool SetReceiveBufferSize(int32 size) { return true; } | 61 virtual bool SetReceiveBufferSize(int32 size) { return true; } |
62 virtual bool SetSendBufferSize(int32 size) { return true; } | 62 virtual bool SetSendBufferSize(int32 size) { return true; } |
63 | 63 |
64 // ClientSocket methods: | 64 // StreamSocket methods: |
65 | 65 |
66 virtual int Connect(CompletionCallback* callback) { | 66 virtual int Connect(CompletionCallback* callback) { |
67 connected_ = true; | 67 connected_ = true; |
68 return OK; | 68 return OK; |
69 } | 69 } |
70 | 70 |
71 virtual void Disconnect() { connected_ = false; } | 71 virtual void Disconnect() { connected_ = false; } |
72 virtual bool IsConnected() const { return connected_; } | 72 virtual bool IsConnected() const { return connected_; } |
73 virtual bool IsConnectedAndIdle() const { return connected_; } | 73 virtual bool IsConnectedAndIdle() const { return connected_; } |
74 | 74 |
(...skipping 21 matching lines...) Expand all Loading... |
96 | 96 |
97 DISALLOW_COPY_AND_ASSIGN(MockClientSocket); | 97 DISALLOW_COPY_AND_ASSIGN(MockClientSocket); |
98 }; | 98 }; |
99 | 99 |
100 class TestConnectJob; | 100 class TestConnectJob; |
101 | 101 |
102 class MockClientSocketFactory : public ClientSocketFactory { | 102 class MockClientSocketFactory : public ClientSocketFactory { |
103 public: | 103 public: |
104 MockClientSocketFactory() : allocation_count_(0) {} | 104 MockClientSocketFactory() : allocation_count_(0) {} |
105 | 105 |
106 virtual ClientSocket* CreateTransportClientSocket( | 106 virtual StreamSocket* CreateTransportClientSocket( |
107 const AddressList& addresses, | 107 const AddressList& addresses, |
108 NetLog* /* net_log */, | 108 NetLog* /* net_log */, |
109 const NetLog::Source& /*source*/) { | 109 const NetLog::Source& /*source*/) { |
110 allocation_count_++; | 110 allocation_count_++; |
111 return NULL; | 111 return NULL; |
112 } | 112 } |
113 | 113 |
114 virtual SSLClientSocket* CreateSSLClientSocket( | 114 virtual SSLClientSocket* CreateSSLClientSocket( |
115 ClientSocketHandle* transport_socket, | 115 ClientSocketHandle* transport_socket, |
116 const HostPortPair& host_and_port, | 116 const HostPortPair& host_and_port, |
(...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
409 } | 409 } |
410 | 410 |
411 virtual void CancelRequest( | 411 virtual void CancelRequest( |
412 const std::string& group_name, | 412 const std::string& group_name, |
413 ClientSocketHandle* handle) { | 413 ClientSocketHandle* handle) { |
414 base_.CancelRequest(group_name, handle); | 414 base_.CancelRequest(group_name, handle); |
415 } | 415 } |
416 | 416 |
417 virtual void ReleaseSocket( | 417 virtual void ReleaseSocket( |
418 const std::string& group_name, | 418 const std::string& group_name, |
419 ClientSocket* socket, | 419 StreamSocket* socket, |
420 int id) { | 420 int id) { |
421 base_.ReleaseSocket(group_name, socket, id); | 421 base_.ReleaseSocket(group_name, socket, id); |
422 } | 422 } |
423 | 423 |
424 virtual void Flush() { | 424 virtual void Flush() { |
425 base_.Flush(); | 425 base_.Flush(); |
426 } | 426 } |
427 | 427 |
428 virtual void CloseIdleSockets() { | 428 virtual void CloseIdleSockets() { |
429 base_.CloseIdleSockets(); | 429 base_.CloseIdleSockets(); |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
493 } | 493 } |
494 | 494 |
495 class TestConnectJobDelegate : public ConnectJob::Delegate { | 495 class TestConnectJobDelegate : public ConnectJob::Delegate { |
496 public: | 496 public: |
497 TestConnectJobDelegate() | 497 TestConnectJobDelegate() |
498 : have_result_(false), waiting_for_result_(false), result_(OK) {} | 498 : have_result_(false), waiting_for_result_(false), result_(OK) {} |
499 virtual ~TestConnectJobDelegate() {} | 499 virtual ~TestConnectJobDelegate() {} |
500 | 500 |
501 virtual void OnConnectJobComplete(int result, ConnectJob* job) { | 501 virtual void OnConnectJobComplete(int result, ConnectJob* job) { |
502 result_ = result; | 502 result_ = result; |
503 scoped_ptr<ClientSocket> socket(job->ReleaseSocket()); | 503 scoped_ptr<StreamSocket> socket(job->ReleaseSocket()); |
504 // socket.get() should be NULL iff result != OK | 504 // socket.get() should be NULL iff result != OK |
505 EXPECT_EQ(socket.get() == NULL, result != OK); | 505 EXPECT_EQ(socket.get() == NULL, result != OK); |
506 delete job; | 506 delete job; |
507 have_result_ = true; | 507 have_result_ = true; |
508 if (waiting_for_result_) | 508 if (waiting_for_result_) |
509 MessageLoop::current()->Quit(); | 509 MessageLoop::current()->Quit(); |
510 } | 510 } |
511 | 511 |
512 int WaitForResult() { | 512 int WaitForResult() { |
513 DCHECK(!waiting_for_result_); | 513 DCHECK(!waiting_for_result_); |
(...skipping 2611 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3125 EXPECT_EQ(1, pool_->IdleSocketCountInGroup("a")); | 3125 EXPECT_EQ(1, pool_->IdleSocketCountInGroup("a")); |
3126 EXPECT_EQ(0, pool_->NumActiveSocketsInGroup("a")); | 3126 EXPECT_EQ(0, pool_->NumActiveSocketsInGroup("a")); |
3127 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("b")); | 3127 EXPECT_EQ(0, pool_->NumConnectJobsInGroup("b")); |
3128 EXPECT_EQ(1, pool_->IdleSocketCountInGroup("b")); | 3128 EXPECT_EQ(1, pool_->IdleSocketCountInGroup("b")); |
3129 EXPECT_EQ(0, pool_->NumActiveSocketsInGroup("b")); | 3129 EXPECT_EQ(0, pool_->NumActiveSocketsInGroup("b")); |
3130 } | 3130 } |
3131 | 3131 |
3132 } // namespace | 3132 } // namespace |
3133 | 3133 |
3134 } // namespace net | 3134 } // namespace net |
OLD | NEW |