| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/tcp_client_socket_pool.h" | 5 #include "net/socket/tcp_client_socket_pool.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/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "net/base/mock_host_resolver.h" | 10 #include "net/base/mock_host_resolver.h" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 | 23 |
| 24 const int kMaxSockets = 32; | 24 const int kMaxSockets = 32; |
| 25 const int kMaxSocketsPerGroup = 6; | 25 const int kMaxSocketsPerGroup = 6; |
| 26 const net::RequestPriority kDefaultPriority = LOW; | 26 const net::RequestPriority kDefaultPriority = LOW; |
| 27 | 27 |
| 28 class MockClientSocket : public ClientSocket { | 28 class MockClientSocket : public ClientSocket { |
| 29 public: | 29 public: |
| 30 MockClientSocket() : connected_(false) {} | 30 MockClientSocket() : connected_(false) {} |
| 31 | 31 |
| 32 // ClientSocket methods: | 32 // ClientSocket methods: |
| 33 virtual int Connect(CompletionCallback* callback, const BoundNetLog& /* net_lo
g */) { | 33 virtual int Connect(CompletionCallback* callback) { |
| 34 connected_ = true; | 34 connected_ = true; |
| 35 return OK; | 35 return OK; |
| 36 } | 36 } |
| 37 virtual void Disconnect() { | 37 virtual void Disconnect() { |
| 38 connected_ = false; | 38 connected_ = false; |
| 39 } | 39 } |
| 40 virtual bool IsConnected() const { | 40 virtual bool IsConnected() const { |
| 41 return connected_; | 41 return connected_; |
| 42 } | 42 } |
| 43 virtual bool IsConnectedAndIdle() const { | 43 virtual bool IsConnectedAndIdle() const { |
| 44 return connected_; | 44 return connected_; |
| 45 } | 45 } |
| 46 virtual int GetPeerAddress(AddressList* address) const { | 46 virtual int GetPeerAddress(AddressList* address) const { |
| 47 return ERR_UNEXPECTED; | 47 return ERR_UNEXPECTED; |
| 48 } | 48 } |
| 49 virtual const BoundNetLog& NetLog() const { |
| 50 return net_log_; |
| 51 } |
| 49 | 52 |
| 50 // Socket methods: | 53 // Socket methods: |
| 51 virtual int Read(IOBuffer* buf, int buf_len, | 54 virtual int Read(IOBuffer* buf, int buf_len, |
| 52 CompletionCallback* callback) { | 55 CompletionCallback* callback) { |
| 53 return ERR_FAILED; | 56 return ERR_FAILED; |
| 54 } | 57 } |
| 55 virtual int Write(IOBuffer* buf, int buf_len, | 58 virtual int Write(IOBuffer* buf, int buf_len, |
| 56 CompletionCallback* callback) { | 59 CompletionCallback* callback) { |
| 57 return ERR_FAILED; | 60 return ERR_FAILED; |
| 58 } | 61 } |
| 59 virtual bool SetReceiveBufferSize(int32 size) { return true; } | 62 virtual bool SetReceiveBufferSize(int32 size) { return true; } |
| 60 virtual bool SetSendBufferSize(int32 size) { return true; } | 63 virtual bool SetSendBufferSize(int32 size) { return true; } |
| 61 | 64 |
| 62 private: | 65 private: |
| 63 bool connected_; | 66 bool connected_; |
| 67 BoundNetLog net_log_; |
| 64 }; | 68 }; |
| 65 | 69 |
| 66 class MockFailingClientSocket : public ClientSocket { | 70 class MockFailingClientSocket : public ClientSocket { |
| 67 public: | 71 public: |
| 68 MockFailingClientSocket() {} | 72 MockFailingClientSocket() {} |
| 69 | 73 |
| 70 // ClientSocket methods: | 74 // ClientSocket methods: |
| 71 virtual int Connect(CompletionCallback* callback, const BoundNetLog& /* net_lo
g */) { | 75 virtual int Connect(CompletionCallback* callback) { |
| 72 return ERR_CONNECTION_FAILED; | 76 return ERR_CONNECTION_FAILED; |
| 73 } | 77 } |
| 74 | 78 |
| 75 virtual void Disconnect() {} | 79 virtual void Disconnect() {} |
| 76 | 80 |
| 77 virtual bool IsConnected() const { | 81 virtual bool IsConnected() const { |
| 78 return false; | 82 return false; |
| 79 } | 83 } |
| 80 virtual bool IsConnectedAndIdle() const { | 84 virtual bool IsConnectedAndIdle() const { |
| 81 return false; | 85 return false; |
| 82 } | 86 } |
| 83 virtual int GetPeerAddress(AddressList* address) const { | 87 virtual int GetPeerAddress(AddressList* address) const { |
| 84 return ERR_UNEXPECTED; | 88 return ERR_UNEXPECTED; |
| 85 } | 89 } |
| 90 virtual const BoundNetLog& NetLog() const { |
| 91 return net_log_; |
| 92 } |
| 86 | 93 |
| 87 // Socket methods: | 94 // Socket methods: |
| 88 virtual int Read(IOBuffer* buf, int buf_len, | 95 virtual int Read(IOBuffer* buf, int buf_len, |
| 89 CompletionCallback* callback) { | 96 CompletionCallback* callback) { |
| 90 return ERR_FAILED; | 97 return ERR_FAILED; |
| 91 } | 98 } |
| 92 | 99 |
| 93 virtual int Write(IOBuffer* buf, int buf_len, | 100 virtual int Write(IOBuffer* buf, int buf_len, |
| 94 CompletionCallback* callback) { | 101 CompletionCallback* callback) { |
| 95 return ERR_FAILED; | 102 return ERR_FAILED; |
| 96 } | 103 } |
| 97 virtual bool SetReceiveBufferSize(int32 size) { return true; } | 104 virtual bool SetReceiveBufferSize(int32 size) { return true; } |
| 98 virtual bool SetSendBufferSize(int32 size) { return true; } | 105 virtual bool SetSendBufferSize(int32 size) { return true; } |
| 106 |
| 107 private: |
| 108 BoundNetLog net_log_; |
| 99 }; | 109 }; |
| 100 | 110 |
| 101 class MockPendingClientSocket : public ClientSocket { | 111 class MockPendingClientSocket : public ClientSocket { |
| 102 public: | 112 public: |
| 103 // |should_connect| indicates whether the socket should successfully complete | 113 // |should_connect| indicates whether the socket should successfully complete |
| 104 // or fail. | 114 // or fail. |
| 105 // |should_stall| indicates that this socket should never connect. | 115 // |should_stall| indicates that this socket should never connect. |
| 106 // |delay_ms| is the delay, in milliseconds, before simulating a connect. | 116 // |delay_ms| is the delay, in milliseconds, before simulating a connect. |
| 107 MockPendingClientSocket(bool should_connect, bool should_stall, int delay_ms) | 117 MockPendingClientSocket(bool should_connect, bool should_stall, int delay_ms) |
| 108 : method_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), | 118 : method_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), |
| 109 should_connect_(should_connect), | 119 should_connect_(should_connect), |
| 110 should_stall_(should_stall), | 120 should_stall_(should_stall), |
| 111 delay_ms_(delay_ms), | 121 delay_ms_(delay_ms), |
| 112 is_connected_(false) {} | 122 is_connected_(false) {} |
| 113 | 123 |
| 114 // ClientSocket methods: | 124 // ClientSocket methods: |
| 115 virtual int Connect(CompletionCallback* callback, const BoundNetLog& /* net_lo
g */) { | 125 virtual int Connect(CompletionCallback* callback) { |
| 116 MessageLoop::current()->PostDelayedTask( | 126 MessageLoop::current()->PostDelayedTask( |
| 117 FROM_HERE, | 127 FROM_HERE, |
| 118 method_factory_.NewRunnableMethod( | 128 method_factory_.NewRunnableMethod( |
| 119 &MockPendingClientSocket::DoCallback, callback), delay_ms_); | 129 &MockPendingClientSocket::DoCallback, callback), delay_ms_); |
| 120 return ERR_IO_PENDING; | 130 return ERR_IO_PENDING; |
| 121 } | 131 } |
| 122 | 132 |
| 123 virtual void Disconnect() {} | 133 virtual void Disconnect() {} |
| 124 | 134 |
| 125 virtual bool IsConnected() const { | 135 virtual bool IsConnected() const { |
| 126 return is_connected_; | 136 return is_connected_; |
| 127 } | 137 } |
| 128 virtual bool IsConnectedAndIdle() const { | 138 virtual bool IsConnectedAndIdle() const { |
| 129 return is_connected_; | 139 return is_connected_; |
| 130 } | 140 } |
| 131 virtual int GetPeerAddress(AddressList* address) const{ | 141 virtual int GetPeerAddress(AddressList* address) const{ |
| 132 return ERR_UNEXPECTED; | 142 return ERR_UNEXPECTED; |
| 133 } | 143 } |
| 144 virtual const BoundNetLog& NetLog() const { |
| 145 return net_log_; |
| 146 } |
| 134 | 147 |
| 135 // Socket methods: | 148 // Socket methods: |
| 136 virtual int Read(IOBuffer* buf, int buf_len, | 149 virtual int Read(IOBuffer* buf, int buf_len, |
| 137 CompletionCallback* callback) { | 150 CompletionCallback* callback) { |
| 138 return ERR_FAILED; | 151 return ERR_FAILED; |
| 139 } | 152 } |
| 140 | 153 |
| 141 virtual int Write(IOBuffer* buf, int buf_len, | 154 virtual int Write(IOBuffer* buf, int buf_len, |
| 142 CompletionCallback* callback) { | 155 CompletionCallback* callback) { |
| 143 return ERR_FAILED; | 156 return ERR_FAILED; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 157 is_connected_ = false; | 170 is_connected_ = false; |
| 158 callback->Run(ERR_CONNECTION_FAILED); | 171 callback->Run(ERR_CONNECTION_FAILED); |
| 159 } | 172 } |
| 160 } | 173 } |
| 161 | 174 |
| 162 ScopedRunnableMethodFactory<MockPendingClientSocket> method_factory_; | 175 ScopedRunnableMethodFactory<MockPendingClientSocket> method_factory_; |
| 163 bool should_connect_; | 176 bool should_connect_; |
| 164 bool should_stall_; | 177 bool should_stall_; |
| 165 int delay_ms_; | 178 int delay_ms_; |
| 166 bool is_connected_; | 179 bool is_connected_; |
| 180 BoundNetLog net_log_; |
| 167 }; | 181 }; |
| 168 | 182 |
| 169 class MockClientSocketFactory : public ClientSocketFactory { | 183 class MockClientSocketFactory : public ClientSocketFactory { |
| 170 public: | 184 public: |
| 171 enum ClientSocketType { | 185 enum ClientSocketType { |
| 172 MOCK_CLIENT_SOCKET, | 186 MOCK_CLIENT_SOCKET, |
| 173 MOCK_FAILING_CLIENT_SOCKET, | 187 MOCK_FAILING_CLIENT_SOCKET, |
| 174 MOCK_PENDING_CLIENT_SOCKET, | 188 MOCK_PENDING_CLIENT_SOCKET, |
| 175 MOCK_PENDING_FAILING_CLIENT_SOCKET, | 189 MOCK_PENDING_FAILING_CLIENT_SOCKET, |
| 176 // A delayed socket will pause before connecting through the message loop. | 190 // A delayed socket will pause before connecting through the message loop. |
| 177 MOCK_DELAYED_CLIENT_SOCKET, | 191 MOCK_DELAYED_CLIENT_SOCKET, |
| 178 // A stalled socket that never connects at all. | 192 // A stalled socket that never connects at all. |
| 179 MOCK_STALLED_CLIENT_SOCKET, | 193 MOCK_STALLED_CLIENT_SOCKET, |
| 180 }; | 194 }; |
| 181 | 195 |
| 182 MockClientSocketFactory() | 196 MockClientSocketFactory() |
| 183 : allocation_count_(0), client_socket_type_(MOCK_CLIENT_SOCKET), | 197 : allocation_count_(0), client_socket_type_(MOCK_CLIENT_SOCKET), |
| 184 client_socket_types_(NULL), client_socket_index_(0) {} | 198 client_socket_types_(NULL), client_socket_index_(0) {} |
| 185 | 199 |
| 186 virtual ClientSocket* CreateTCPClientSocket(const AddressList& addresses) { | 200 virtual ClientSocket* CreateTCPClientSocket(const AddressList& addresses, |
| 201 NetLog* /* net_log */) { |
| 187 allocation_count_++; | 202 allocation_count_++; |
| 188 | 203 |
| 189 ClientSocketType type = client_socket_type_; | 204 ClientSocketType type = client_socket_type_; |
| 190 if (client_socket_types_) | 205 if (client_socket_types_) |
| 191 type = client_socket_types_[client_socket_index_++]; | 206 type = client_socket_types_[client_socket_index_++]; |
| 192 | 207 |
| 193 switch (type) { | 208 switch (type) { |
| 194 case MOCK_CLIENT_SOCKET: | 209 case MOCK_CLIENT_SOCKET: |
| 195 return new MockClientSocket(); | 210 return new MockClientSocket(); |
| 196 case MOCK_FAILING_CLIENT_SOCKET: | 211 case MOCK_FAILING_CLIENT_SOCKET: |
| (...skipping 641 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 838 EXPECT_FALSE(handle.socket()); | 853 EXPECT_FALSE(handle.socket()); |
| 839 handle.Reset(); | 854 handle.Reset(); |
| 840 | 855 |
| 841 // Reset for the next case. | 856 // Reset for the next case. |
| 842 host_resolver_->set_synchronous_mode(false); | 857 host_resolver_->set_synchronous_mode(false); |
| 843 } | 858 } |
| 844 | 859 |
| 845 } // namespace | 860 } // namespace |
| 846 | 861 |
| 847 } // namespace net | 862 } // namespace net |
| OLD | NEW |