Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(575)

Side by Side Diff: net/socket/transport_client_socket_pool_unittest.cc

Issue 8824006: Migrate net/socket/socket.h, net/socket/stream_socket.h to base::Bind(). (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebased Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/transport_client_socket_pool.h" 5 #include "net/socket/transport_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/logging.h" 9 #include "base/logging.h"
10 #include "base/message_loop.h" 10 #include "base/message_loop.h"
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
45 *address = IPEndPoint(number, 80); 45 *address = IPEndPoint(number, 80);
46 } 46 }
47 47
48 class MockClientSocket : public StreamSocket { 48 class MockClientSocket : public StreamSocket {
49 public: 49 public:
50 MockClientSocket(const AddressList& addrlist) 50 MockClientSocket(const AddressList& addrlist)
51 : connected_(false), 51 : connected_(false),
52 addrlist_(addrlist) {} 52 addrlist_(addrlist) {}
53 53
54 // StreamSocket implementation. 54 // StreamSocket implementation.
55 virtual int Connect(OldCompletionCallback* callback) {
56 connected_ = true;
57 return OK;
58 }
59 virtual int Connect(const CompletionCallback& callback) { 55 virtual int Connect(const CompletionCallback& callback) {
60 connected_ = true; 56 connected_ = true;
61 return OK; 57 return OK;
62 } 58 }
63 virtual void Disconnect() { 59 virtual void Disconnect() {
64 connected_ = false; 60 connected_ = false;
65 } 61 }
66 virtual bool IsConnected() const { 62 virtual bool IsConnected() const {
67 return connected_; 63 return connected_;
68 } 64 }
(...skipping 20 matching lines...) Expand all
89 virtual void SetOmniboxSpeculation() {} 85 virtual void SetOmniboxSpeculation() {}
90 virtual bool WasEverUsed() const { return false; } 86 virtual bool WasEverUsed() const { return false; }
91 virtual bool UsingTCPFastOpen() const { return false; } 87 virtual bool UsingTCPFastOpen() const { return false; }
92 virtual int64 NumBytesRead() const { return -1; } 88 virtual int64 NumBytesRead() const { return -1; }
93 virtual base::TimeDelta GetConnectTimeMicros() const { 89 virtual base::TimeDelta GetConnectTimeMicros() const {
94 return base::TimeDelta::FromMicroseconds(-1); 90 return base::TimeDelta::FromMicroseconds(-1);
95 } 91 }
96 92
97 // Socket implementation. 93 // Socket implementation.
98 virtual int Read(IOBuffer* buf, int buf_len, 94 virtual int Read(IOBuffer* buf, int buf_len,
99 OldCompletionCallback* callback) {
100 return ERR_FAILED;
101 }
102 virtual int Read(IOBuffer* buf, int buf_len,
103 const CompletionCallback& callback) { 95 const CompletionCallback& callback) {
104 return ERR_FAILED; 96 return ERR_FAILED;
105 } 97 }
106 virtual int Write(IOBuffer* buf, int buf_len, 98 virtual int Write(IOBuffer* buf, int buf_len,
107 OldCompletionCallback* callback) { 99 const CompletionCallback& callback) {
108 return ERR_FAILED; 100 return ERR_FAILED;
109 } 101 }
110 virtual bool SetReceiveBufferSize(int32 size) { return true; } 102 virtual bool SetReceiveBufferSize(int32 size) { return true; }
111 virtual bool SetSendBufferSize(int32 size) { return true; } 103 virtual bool SetSendBufferSize(int32 size) { return true; }
112 104
113 private: 105 private:
114 bool connected_; 106 bool connected_;
115 const AddressList addrlist_; 107 const AddressList addrlist_;
116 BoundNetLog net_log_; 108 BoundNetLog net_log_;
117 }; 109 };
118 110
119 class MockFailingClientSocket : public StreamSocket { 111 class MockFailingClientSocket : public StreamSocket {
120 public: 112 public:
121 MockFailingClientSocket(const AddressList& addrlist) : addrlist_(addrlist) {} 113 MockFailingClientSocket(const AddressList& addrlist) : addrlist_(addrlist) {}
122 114
123 // StreamSocket implementation. 115 // StreamSocket implementation.
124 virtual int Connect(OldCompletionCallback* callback) { 116 virtual int Connect(const CompletionCallback& callback) {
125 return ERR_CONNECTION_FAILED;
126 }
127 virtual int Connect(const net::CompletionCallback& callback) {
128 return ERR_CONNECTION_FAILED; 117 return ERR_CONNECTION_FAILED;
129 } 118 }
130 119
131 virtual void Disconnect() {} 120 virtual void Disconnect() {}
132 121
133 virtual bool IsConnected() const { 122 virtual bool IsConnected() const {
134 return false; 123 return false;
135 } 124 }
136 virtual bool IsConnectedAndIdle() const { 125 virtual bool IsConnectedAndIdle() const {
137 return false; 126 return false;
(...skipping 12 matching lines...) Expand all
150 virtual void SetOmniboxSpeculation() {} 139 virtual void SetOmniboxSpeculation() {}
151 virtual bool WasEverUsed() const { return false; } 140 virtual bool WasEverUsed() const { return false; }
152 virtual bool UsingTCPFastOpen() const { return false; } 141 virtual bool UsingTCPFastOpen() const { return false; }
153 virtual int64 NumBytesRead() const { return -1; } 142 virtual int64 NumBytesRead() const { return -1; }
154 virtual base::TimeDelta GetConnectTimeMicros() const { 143 virtual base::TimeDelta GetConnectTimeMicros() const {
155 return base::TimeDelta::FromMicroseconds(-1); 144 return base::TimeDelta::FromMicroseconds(-1);
156 } 145 }
157 146
158 // Socket implementation. 147 // Socket implementation.
159 virtual int Read(IOBuffer* buf, int buf_len, 148 virtual int Read(IOBuffer* buf, int buf_len,
160 OldCompletionCallback* callback) {
161 return ERR_FAILED;
162 }
163 virtual int Read(IOBuffer* buf, int buf_len,
164 const CompletionCallback& callback) { 149 const CompletionCallback& callback) {
165 return ERR_FAILED; 150 return ERR_FAILED;
166 } 151 }
167 152
168 virtual int Write(IOBuffer* buf, int buf_len, 153 virtual int Write(IOBuffer* buf, int buf_len,
169 OldCompletionCallback* callback) { 154 const CompletionCallback& callback) {
170 return ERR_FAILED; 155 return ERR_FAILED;
171 } 156 }
172 virtual bool SetReceiveBufferSize(int32 size) { return true; } 157 virtual bool SetReceiveBufferSize(int32 size) { return true; }
173 virtual bool SetSendBufferSize(int32 size) { return true; } 158 virtual bool SetSendBufferSize(int32 size) { return true; }
174 159
175 private: 160 private:
176 const AddressList addrlist_; 161 const AddressList addrlist_;
177 BoundNetLog net_log_; 162 BoundNetLog net_log_;
178 }; 163 };
179 164
180 class MockPendingClientSocket : public StreamSocket { 165 class MockPendingClientSocket : public StreamSocket {
181 public: 166 public:
182 // |should_connect| indicates whether the socket should successfully complete 167 // |should_connect| indicates whether the socket should successfully complete
183 // or fail. 168 // or fail.
184 // |should_stall| indicates that this socket should never connect. 169 // |should_stall| indicates that this socket should never connect.
185 // |delay_ms| is the delay, in milliseconds, before simulating a connect. 170 // |delay_ms| is the delay, in milliseconds, before simulating a connect.
186 MockPendingClientSocket( 171 MockPendingClientSocket(
187 const AddressList& addrlist, 172 const AddressList& addrlist,
188 bool should_connect, 173 bool should_connect,
189 bool should_stall, 174 bool should_stall,
190 int delay_ms) 175 int delay_ms)
191 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), 176 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)),
192 should_connect_(should_connect), 177 should_connect_(should_connect),
193 should_stall_(should_stall), 178 should_stall_(should_stall),
194 delay_ms_(delay_ms), 179 delay_ms_(delay_ms),
195 is_connected_(false), 180 is_connected_(false),
196 addrlist_(addrlist) {} 181 addrlist_(addrlist) {}
197 182
198 // StreamSocket implementation. 183 // StreamSocket implementation.
199 virtual int Connect(OldCompletionCallback* callback) {
200 MessageLoop::current()->PostDelayedTask(
201 FROM_HERE,
202 base::Bind(&MockPendingClientSocket::DoOldCallback,
203 weak_factory_.GetWeakPtr(), callback),
204 delay_ms_);
205 return ERR_IO_PENDING;
206 }
207 virtual int Connect(const CompletionCallback& callback) { 184 virtual int Connect(const CompletionCallback& callback) {
208 MessageLoop::current()->PostDelayedTask( 185 MessageLoop::current()->PostDelayedTask(
209 FROM_HERE, 186 FROM_HERE,
210 base::Bind(&MockPendingClientSocket::DoCallback, 187 base::Bind(&MockPendingClientSocket::DoCallback,
211 weak_factory_.GetWeakPtr(), callback), 188 weak_factory_.GetWeakPtr(), callback),
212 delay_ms_); 189 delay_ms_);
213 return ERR_IO_PENDING; 190 return ERR_IO_PENDING;
214 } 191 }
215 192
216 virtual void Disconnect() {} 193 virtual void Disconnect() {}
(...skipping 24 matching lines...) Expand all
241 virtual void SetOmniboxSpeculation() {} 218 virtual void SetOmniboxSpeculation() {}
242 virtual bool WasEverUsed() const { return false; } 219 virtual bool WasEverUsed() const { return false; }
243 virtual bool UsingTCPFastOpen() const { return false; } 220 virtual bool UsingTCPFastOpen() const { return false; }
244 virtual int64 NumBytesRead() const { return -1; } 221 virtual int64 NumBytesRead() const { return -1; }
245 virtual base::TimeDelta GetConnectTimeMicros() const { 222 virtual base::TimeDelta GetConnectTimeMicros() const {
246 return base::TimeDelta::FromMicroseconds(-1); 223 return base::TimeDelta::FromMicroseconds(-1);
247 } 224 }
248 225
249 // Socket implementation. 226 // Socket implementation.
250 virtual int Read(IOBuffer* buf, int buf_len, 227 virtual int Read(IOBuffer* buf, int buf_len,
251 OldCompletionCallback* callback) {
252 return ERR_FAILED;
253 }
254 virtual int Read(IOBuffer* buf, int buf_len,
255 const CompletionCallback& callback) { 228 const CompletionCallback& callback) {
256 return ERR_FAILED; 229 return ERR_FAILED;
257 } 230 }
258 231
259 virtual int Write(IOBuffer* buf, int buf_len, 232 virtual int Write(IOBuffer* buf, int buf_len,
260 OldCompletionCallback* callback) { 233 const CompletionCallback& callback) {
261 return ERR_FAILED; 234 return ERR_FAILED;
262 } 235 }
263 virtual bool SetReceiveBufferSize(int32 size) { return true; } 236 virtual bool SetReceiveBufferSize(int32 size) { return true; }
264 virtual bool SetSendBufferSize(int32 size) { return true; } 237 virtual bool SetSendBufferSize(int32 size) { return true; }
265 238
266 private: 239 private:
267 void DoOldCallback(OldCompletionCallback* callback) {
268 if (should_stall_)
269 return;
270
271 if (should_connect_) {
272 is_connected_ = true;
273 callback->Run(OK);
274 } else {
275 is_connected_ = false;
276 callback->Run(ERR_CONNECTION_FAILED);
277 }
278 }
279 void DoCallback(const CompletionCallback& callback) { 240 void DoCallback(const CompletionCallback& callback) {
280 if (should_stall_) 241 if (should_stall_)
281 return; 242 return;
282 243
283 if (should_connect_) { 244 if (should_connect_) {
284 is_connected_ = true; 245 is_connected_ = true;
285 callback.Run(OK); 246 callback.Run(OK);
286 } else { 247 } else {
287 is_connected_ = false; 248 is_connected_ = false;
288 callback.Run(ERR_CONNECTION_FAILED); 249 callback.Run(ERR_CONNECTION_FAILED);
(...skipping 978 matching lines...) Expand 10 before | Expand all | Expand 10 after
1267 EXPECT_TRUE(handle.socket()); 1228 EXPECT_TRUE(handle.socket());
1268 IPEndPoint endpoint; 1229 IPEndPoint endpoint;
1269 handle.socket()->GetLocalAddress(&endpoint); 1230 handle.socket()->GetLocalAddress(&endpoint);
1270 EXPECT_EQ(kIPv4AddressSize, endpoint.address().size()); 1231 EXPECT_EQ(kIPv4AddressSize, endpoint.address().size());
1271 EXPECT_EQ(1, client_socket_factory_.allocation_count()); 1232 EXPECT_EQ(1, client_socket_factory_.allocation_count());
1272 } 1233 }
1273 1234
1274 } // namespace 1235 } // namespace
1275 1236
1276 } // namespace net 1237 } // namespace net
OLDNEW
« no previous file with comments | « net/socket/transport_client_socket_pool.cc ('k') | net/socket/transport_client_socket_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698