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

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

Issue 8801004: base::Bind: Convert StreamSocket::Connect. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Review fixes 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
« no previous file with comments | « net/socket/tcp_client_socket_win.cc ('k') | net/spdy/spdy_proxy_client_socket.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
44 CHECK(ParseIPLiteralToNumber("1:abcd::3:4:ff", &number)); 44 CHECK(ParseIPLiteralToNumber("1:abcd::3:4:ff", &number));
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 methods: 54 // StreamSocket implementation.
55 virtual int Connect(OldCompletionCallback* callback) { 55 virtual int Connect(OldCompletionCallback* callback) {
56 connected_ = true; 56 connected_ = true;
57 return OK; 57 return OK;
58 } 58 }
59 virtual int Connect(const CompletionCallback& callback) {
60 connected_ = true;
61 return OK;
62 }
59 virtual void Disconnect() { 63 virtual void Disconnect() {
60 connected_ = false; 64 connected_ = false;
61 } 65 }
62 virtual bool IsConnected() const { 66 virtual bool IsConnected() const {
63 return connected_; 67 return connected_;
64 } 68 }
65 virtual bool IsConnectedAndIdle() const { 69 virtual bool IsConnectedAndIdle() const {
66 return connected_; 70 return connected_;
67 } 71 }
68 virtual int GetPeerAddress(AddressList* address) const { 72 virtual int GetPeerAddress(AddressList* address) const {
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
105 private: 109 private:
106 bool connected_; 110 bool connected_;
107 const AddressList addrlist_; 111 const AddressList addrlist_;
108 BoundNetLog net_log_; 112 BoundNetLog net_log_;
109 }; 113 };
110 114
111 class MockFailingClientSocket : public StreamSocket { 115 class MockFailingClientSocket : public StreamSocket {
112 public: 116 public:
113 MockFailingClientSocket(const AddressList& addrlist) : addrlist_(addrlist) {} 117 MockFailingClientSocket(const AddressList& addrlist) : addrlist_(addrlist) {}
114 118
115 // StreamSocket methods: 119 // StreamSocket implementation.
116 virtual int Connect(OldCompletionCallback* callback) { 120 virtual int Connect(OldCompletionCallback* callback) {
117 return ERR_CONNECTION_FAILED; 121 return ERR_CONNECTION_FAILED;
118 } 122 }
123 virtual int Connect(const net::CompletionCallback& callback) {
124 return ERR_CONNECTION_FAILED;
125 }
119 126
120 virtual void Disconnect() {} 127 virtual void Disconnect() {}
121 128
122 virtual bool IsConnected() const { 129 virtual bool IsConnected() const {
123 return false; 130 return false;
124 } 131 }
125 virtual bool IsConnectedAndIdle() const { 132 virtual bool IsConnectedAndIdle() const {
126 return false; 133 return false;
127 } 134 }
128 virtual int GetPeerAddress(AddressList* address) const { 135 virtual int GetPeerAddress(AddressList* address) const {
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
166 public: 173 public:
167 // |should_connect| indicates whether the socket should successfully complete 174 // |should_connect| indicates whether the socket should successfully complete
168 // or fail. 175 // or fail.
169 // |should_stall| indicates that this socket should never connect. 176 // |should_stall| indicates that this socket should never connect.
170 // |delay_ms| is the delay, in milliseconds, before simulating a connect. 177 // |delay_ms| is the delay, in milliseconds, before simulating a connect.
171 MockPendingClientSocket( 178 MockPendingClientSocket(
172 const AddressList& addrlist, 179 const AddressList& addrlist,
173 bool should_connect, 180 bool should_connect,
174 bool should_stall, 181 bool should_stall,
175 int delay_ms) 182 int delay_ms)
176 : method_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), 183 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)),
177 should_connect_(should_connect), 184 should_connect_(should_connect),
178 should_stall_(should_stall), 185 should_stall_(should_stall),
179 delay_ms_(delay_ms), 186 delay_ms_(delay_ms),
180 is_connected_(false), 187 is_connected_(false),
181 addrlist_(addrlist) {} 188 addrlist_(addrlist) {}
182 189
183 // StreamSocket methods: 190 // StreamSocket implementation.
184 virtual int Connect(OldCompletionCallback* callback) { 191 virtual int Connect(OldCompletionCallback* callback) {
185 MessageLoop::current()->PostDelayedTask( 192 MessageLoop::current()->PostDelayedTask(
186 FROM_HERE, 193 FROM_HERE,
187 method_factory_.NewRunnableMethod( 194 base::Bind(&MockPendingClientSocket::DoOldCallback,
188 &MockPendingClientSocket::DoCallback, callback), delay_ms_); 195 weak_factory_.GetWeakPtr(), callback),
196 delay_ms_);
197 return ERR_IO_PENDING;
198 }
199 virtual int Connect(const CompletionCallback& callback) {
200 MessageLoop::current()->PostDelayedTask(
201 FROM_HERE,
202 base::Bind(&MockPendingClientSocket::DoCallback,
203 weak_factory_.GetWeakPtr(), callback),
204 delay_ms_);
189 return ERR_IO_PENDING; 205 return ERR_IO_PENDING;
190 } 206 }
191 207
192 virtual void Disconnect() {} 208 virtual void Disconnect() {}
193 209
194 virtual bool IsConnected() const { 210 virtual bool IsConnected() const {
195 return is_connected_; 211 return is_connected_;
196 } 212 }
197 virtual bool IsConnectedAndIdle() const { 213 virtual bool IsConnectedAndIdle() const {
198 return is_connected_; 214 return is_connected_;
(...skipping 30 matching lines...) Expand all
229 } 245 }
230 246
231 virtual int Write(IOBuffer* buf, int buf_len, 247 virtual int Write(IOBuffer* buf, int buf_len,
232 OldCompletionCallback* callback) { 248 OldCompletionCallback* callback) {
233 return ERR_FAILED; 249 return ERR_FAILED;
234 } 250 }
235 virtual bool SetReceiveBufferSize(int32 size) { return true; } 251 virtual bool SetReceiveBufferSize(int32 size) { return true; }
236 virtual bool SetSendBufferSize(int32 size) { return true; } 252 virtual bool SetSendBufferSize(int32 size) { return true; }
237 253
238 private: 254 private:
239 void DoCallback(OldCompletionCallback* callback) { 255 void DoOldCallback(OldCompletionCallback* callback) {
240 if (should_stall_) 256 if (should_stall_)
241 return; 257 return;
242 258
243 if (should_connect_) { 259 if (should_connect_) {
244 is_connected_ = true; 260 is_connected_ = true;
245 callback->Run(OK); 261 callback->Run(OK);
246 } else { 262 } else {
247 is_connected_ = false; 263 is_connected_ = false;
248 callback->Run(ERR_CONNECTION_FAILED); 264 callback->Run(ERR_CONNECTION_FAILED);
249 } 265 }
250 } 266 }
267 void DoCallback(const CompletionCallback& callback) {
268 if (should_stall_)
269 return;
251 270
252 ScopedRunnableMethodFactory<MockPendingClientSocket> method_factory_; 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
280 base::WeakPtrFactory<MockPendingClientSocket> weak_factory_;
253 bool should_connect_; 281 bool should_connect_;
254 bool should_stall_; 282 bool should_stall_;
255 int delay_ms_; 283 int delay_ms_;
256 bool is_connected_; 284 bool is_connected_;
257 const AddressList addrlist_; 285 const AddressList addrlist_;
258 BoundNetLog net_log_; 286 BoundNetLog net_log_;
259 }; 287 };
260 288
261 class MockClientSocketFactory : public ClientSocketFactory { 289 class MockClientSocketFactory : public ClientSocketFactory {
262 public: 290 public:
(...skipping 964 matching lines...) Expand 10 before | Expand all | Expand 10 after
1227 EXPECT_TRUE(handle.socket()); 1255 EXPECT_TRUE(handle.socket());
1228 IPEndPoint endpoint; 1256 IPEndPoint endpoint;
1229 handle.socket()->GetLocalAddress(&endpoint); 1257 handle.socket()->GetLocalAddress(&endpoint);
1230 EXPECT_EQ(kIPv4AddressSize, endpoint.address().size()); 1258 EXPECT_EQ(kIPv4AddressSize, endpoint.address().size());
1231 EXPECT_EQ(1, client_socket_factory_.allocation_count()); 1259 EXPECT_EQ(1, client_socket_factory_.allocation_count());
1232 } 1260 }
1233 1261
1234 } // namespace 1262 } // namespace
1235 1263
1236 } // namespace net 1264 } // namespace net
OLDNEW
« no previous file with comments | « net/socket/tcp_client_socket_win.cc ('k') | net/spdy/spdy_proxy_client_socket.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698