OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/websocket_transport_client_socket_pool.h" | 5 #include "net/socket/websocket_transport_client_socket_pool.h" |
6 | 6 |
7 #include <queue> | 7 #include <queue> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 11 matching lines...) Expand all Loading... |
22 #include "net/base/load_timing_info_test_util.h" | 22 #include "net/base/load_timing_info_test_util.h" |
23 #include "net/base/net_errors.h" | 23 #include "net/base/net_errors.h" |
24 #include "net/base/test_completion_callback.h" | 24 #include "net/base/test_completion_callback.h" |
25 #include "net/dns/mock_host_resolver.h" | 25 #include "net/dns/mock_host_resolver.h" |
26 #include "net/log/test_net_log.h" | 26 #include "net/log/test_net_log.h" |
27 #include "net/socket/client_socket_handle.h" | 27 #include "net/socket/client_socket_handle.h" |
28 #include "net/socket/socket_test_util.h" | 28 #include "net/socket/socket_test_util.h" |
29 #include "net/socket/stream_socket.h" | 29 #include "net/socket/stream_socket.h" |
30 #include "net/socket/transport_client_socket_pool_test_util.h" | 30 #include "net/socket/transport_client_socket_pool_test_util.h" |
31 #include "net/socket/websocket_endpoint_lock_manager.h" | 31 #include "net/socket/websocket_endpoint_lock_manager.h" |
| 32 #include "net/test/gtest_util.h" |
| 33 #include "testing/gmock/include/gmock/gmock.h" |
32 #include "testing/gtest/include/gtest/gtest.h" | 34 #include "testing/gtest/include/gtest/gtest.h" |
33 | 35 |
| 36 using net::test::IsError; |
| 37 using net::test::IsOk; |
| 38 |
34 namespace net { | 39 namespace net { |
35 | 40 |
36 namespace { | 41 namespace { |
37 | 42 |
38 const int kMaxSockets = 32; | 43 const int kMaxSockets = 32; |
39 const int kMaxSocketsPerGroup = 6; | 44 const int kMaxSocketsPerGroup = 6; |
40 const RequestPriority kDefaultPriority = LOW; | 45 const RequestPriority kDefaultPriority = LOW; |
41 | 46 |
42 // RunLoop doesn't support this natively but it is easy to emulate. | 47 // RunLoop doesn't support this natively but it is easy to emulate. |
43 void RunLoopForTimePeriod(base::TimeDelta period) { | 48 void RunLoopForTimePeriod(base::TimeDelta period) { |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 private: | 121 private: |
117 DISALLOW_COPY_AND_ASSIGN(WebSocketTransportClientSocketPoolTest); | 122 DISALLOW_COPY_AND_ASSIGN(WebSocketTransportClientSocketPoolTest); |
118 }; | 123 }; |
119 | 124 |
120 TEST_F(WebSocketTransportClientSocketPoolTest, Basic) { | 125 TEST_F(WebSocketTransportClientSocketPoolTest, Basic) { |
121 TestCompletionCallback callback; | 126 TestCompletionCallback callback; |
122 ClientSocketHandle handle; | 127 ClientSocketHandle handle; |
123 int rv = | 128 int rv = |
124 handle.Init("a", params_, LOW, ClientSocketPool::RespectLimits::ENABLED, | 129 handle.Init("a", params_, LOW, ClientSocketPool::RespectLimits::ENABLED, |
125 callback.callback(), &pool_, BoundNetLog()); | 130 callback.callback(), &pool_, BoundNetLog()); |
126 EXPECT_EQ(ERR_IO_PENDING, rv); | 131 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
127 EXPECT_FALSE(handle.is_initialized()); | 132 EXPECT_FALSE(handle.is_initialized()); |
128 EXPECT_FALSE(handle.socket()); | 133 EXPECT_FALSE(handle.socket()); |
129 | 134 |
130 EXPECT_EQ(OK, callback.WaitForResult()); | 135 EXPECT_THAT(callback.WaitForResult(), IsOk()); |
131 EXPECT_TRUE(handle.is_initialized()); | 136 EXPECT_TRUE(handle.is_initialized()); |
132 EXPECT_TRUE(handle.socket()); | 137 EXPECT_TRUE(handle.socket()); |
133 TestLoadTimingInfoConnectedNotReused(handle); | 138 TestLoadTimingInfoConnectedNotReused(handle); |
134 } | 139 } |
135 | 140 |
136 // Make sure that WebSocketTransportConnectJob passes on its priority to its | 141 // Make sure that WebSocketTransportConnectJob passes on its priority to its |
137 // HostResolver request on Init. | 142 // HostResolver request on Init. |
138 TEST_F(WebSocketTransportClientSocketPoolTest, SetResolvePriorityOnInit) { | 143 TEST_F(WebSocketTransportClientSocketPoolTest, SetResolvePriorityOnInit) { |
139 for (int i = MINIMUM_PRIORITY; i <= MAXIMUM_PRIORITY; ++i) { | 144 for (int i = MINIMUM_PRIORITY; i <= MAXIMUM_PRIORITY; ++i) { |
140 RequestPriority priority = static_cast<RequestPriority>(i); | 145 RequestPriority priority = static_cast<RequestPriority>(i); |
(...skipping 12 matching lines...) Expand all Loading... |
153 TestCompletionCallback callback; | 158 TestCompletionCallback callback; |
154 ClientSocketHandle handle; | 159 ClientSocketHandle handle; |
155 HostPortPair host_port_pair("unresolvable.host.name", 80); | 160 HostPortPair host_port_pair("unresolvable.host.name", 80); |
156 scoped_refptr<TransportSocketParams> dest(new TransportSocketParams( | 161 scoped_refptr<TransportSocketParams> dest(new TransportSocketParams( |
157 host_port_pair, false, OnHostResolutionCallback(), | 162 host_port_pair, false, OnHostResolutionCallback(), |
158 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DEFAULT)); | 163 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DEFAULT)); |
159 EXPECT_EQ(ERR_IO_PENDING, | 164 EXPECT_EQ(ERR_IO_PENDING, |
160 handle.Init("a", dest, kDefaultPriority, | 165 handle.Init("a", dest, kDefaultPriority, |
161 ClientSocketPool::RespectLimits::ENABLED, | 166 ClientSocketPool::RespectLimits::ENABLED, |
162 callback.callback(), &pool_, BoundNetLog())); | 167 callback.callback(), &pool_, BoundNetLog())); |
163 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, callback.WaitForResult()); | 168 EXPECT_THAT(callback.WaitForResult(), IsError(ERR_NAME_NOT_RESOLVED)); |
164 } | 169 } |
165 | 170 |
166 TEST_F(WebSocketTransportClientSocketPoolTest, InitConnectionFailure) { | 171 TEST_F(WebSocketTransportClientSocketPoolTest, InitConnectionFailure) { |
167 client_socket_factory_.set_default_client_socket_type( | 172 client_socket_factory_.set_default_client_socket_type( |
168 MockTransportClientSocketFactory::MOCK_FAILING_CLIENT_SOCKET); | 173 MockTransportClientSocketFactory::MOCK_FAILING_CLIENT_SOCKET); |
169 TestCompletionCallback callback; | 174 TestCompletionCallback callback; |
170 ClientSocketHandle handle; | 175 ClientSocketHandle handle; |
171 EXPECT_EQ(ERR_IO_PENDING, | 176 EXPECT_EQ(ERR_IO_PENDING, |
172 handle.Init("a", params_, kDefaultPriority, | 177 handle.Init("a", params_, kDefaultPriority, |
173 ClientSocketPool::RespectLimits::ENABLED, | 178 ClientSocketPool::RespectLimits::ENABLED, |
174 callback.callback(), &pool_, BoundNetLog())); | 179 callback.callback(), &pool_, BoundNetLog())); |
175 EXPECT_EQ(ERR_CONNECTION_FAILED, callback.WaitForResult()); | 180 EXPECT_THAT(callback.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
176 | 181 |
177 // Make the host resolutions complete synchronously this time. | 182 // Make the host resolutions complete synchronously this time. |
178 host_resolver_->set_synchronous_mode(true); | 183 host_resolver_->set_synchronous_mode(true); |
179 EXPECT_EQ(ERR_CONNECTION_FAILED, | 184 EXPECT_EQ(ERR_CONNECTION_FAILED, |
180 handle.Init("a", params_, kDefaultPriority, | 185 handle.Init("a", params_, kDefaultPriority, |
181 ClientSocketPool::RespectLimits::ENABLED, | 186 ClientSocketPool::RespectLimits::ENABLED, |
182 callback.callback(), &pool_, BoundNetLog())); | 187 callback.callback(), &pool_, BoundNetLog())); |
183 } | 188 } |
184 | 189 |
185 TEST_F(WebSocketTransportClientSocketPoolTest, PendingRequestsFinishFifo) { | 190 TEST_F(WebSocketTransportClientSocketPoolTest, PendingRequestsFinishFifo) { |
186 // First request finishes asynchronously. | 191 // First request finishes asynchronously. |
187 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); | 192 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsError(ERR_IO_PENDING)); |
188 EXPECT_EQ(OK, request(0)->WaitForResult()); | 193 EXPECT_THAT(request(0)->WaitForResult(), IsOk()); |
189 | 194 |
190 // Make all subsequent host resolutions complete synchronously. | 195 // Make all subsequent host resolutions complete synchronously. |
191 host_resolver_->set_synchronous_mode(true); | 196 host_resolver_->set_synchronous_mode(true); |
192 | 197 |
193 // Rest of them wait for the first socket to be released. | 198 // Rest of them wait for the first socket to be released. |
194 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); | 199 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsError(ERR_IO_PENDING)); |
195 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); | 200 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsError(ERR_IO_PENDING)); |
196 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); | 201 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsError(ERR_IO_PENDING)); |
197 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); | 202 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsError(ERR_IO_PENDING)); |
198 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); | 203 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsError(ERR_IO_PENDING)); |
199 | 204 |
200 ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE); | 205 ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE); |
201 | 206 |
202 EXPECT_EQ(6, client_socket_factory_.allocation_count()); | 207 EXPECT_EQ(6, client_socket_factory_.allocation_count()); |
203 | 208 |
204 // One initial asynchronous request and then 5 pending requests. | 209 // One initial asynchronous request and then 5 pending requests. |
205 EXPECT_EQ(6U, completion_count()); | 210 EXPECT_EQ(6U, completion_count()); |
206 | 211 |
207 // The requests finish in FIFO order. | 212 // The requests finish in FIFO order. |
208 EXPECT_EQ(1, GetOrderOfRequest(1)); | 213 EXPECT_EQ(1, GetOrderOfRequest(1)); |
209 EXPECT_EQ(2, GetOrderOfRequest(2)); | 214 EXPECT_EQ(2, GetOrderOfRequest(2)); |
210 EXPECT_EQ(3, GetOrderOfRequest(3)); | 215 EXPECT_EQ(3, GetOrderOfRequest(3)); |
211 EXPECT_EQ(4, GetOrderOfRequest(4)); | 216 EXPECT_EQ(4, GetOrderOfRequest(4)); |
212 EXPECT_EQ(5, GetOrderOfRequest(5)); | 217 EXPECT_EQ(5, GetOrderOfRequest(5)); |
213 EXPECT_EQ(6, GetOrderOfRequest(6)); | 218 EXPECT_EQ(6, GetOrderOfRequest(6)); |
214 | 219 |
215 // Make sure we test order of all requests made. | 220 // Make sure we test order of all requests made. |
216 EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(7)); | 221 EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(7)); |
217 } | 222 } |
218 | 223 |
219 TEST_F(WebSocketTransportClientSocketPoolTest, PendingRequests_NoKeepAlive) { | 224 TEST_F(WebSocketTransportClientSocketPoolTest, PendingRequests_NoKeepAlive) { |
220 // First request finishes asynchronously. | 225 // First request finishes asynchronously. |
221 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); | 226 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsError(ERR_IO_PENDING)); |
222 EXPECT_EQ(OK, request(0)->WaitForResult()); | 227 EXPECT_THAT(request(0)->WaitForResult(), IsOk()); |
223 | 228 |
224 // Make all subsequent host resolutions complete synchronously. | 229 // Make all subsequent host resolutions complete synchronously. |
225 host_resolver_->set_synchronous_mode(true); | 230 host_resolver_->set_synchronous_mode(true); |
226 | 231 |
227 // Rest of them wait for the first socket to be released. | 232 // Rest of them wait for the first socket to be released. |
228 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); | 233 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsError(ERR_IO_PENDING)); |
229 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); | 234 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsError(ERR_IO_PENDING)); |
230 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); | 235 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsError(ERR_IO_PENDING)); |
231 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); | 236 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsError(ERR_IO_PENDING)); |
232 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); | 237 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsError(ERR_IO_PENDING)); |
233 | 238 |
234 ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); | 239 ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
235 | 240 |
236 // The pending requests should finish successfully. | 241 // The pending requests should finish successfully. |
237 EXPECT_EQ(OK, request(1)->WaitForResult()); | 242 EXPECT_THAT(request(1)->WaitForResult(), IsOk()); |
238 EXPECT_EQ(OK, request(2)->WaitForResult()); | 243 EXPECT_THAT(request(2)->WaitForResult(), IsOk()); |
239 EXPECT_EQ(OK, request(3)->WaitForResult()); | 244 EXPECT_THAT(request(3)->WaitForResult(), IsOk()); |
240 EXPECT_EQ(OK, request(4)->WaitForResult()); | 245 EXPECT_THAT(request(4)->WaitForResult(), IsOk()); |
241 EXPECT_EQ(OK, request(5)->WaitForResult()); | 246 EXPECT_THAT(request(5)->WaitForResult(), IsOk()); |
242 | 247 |
243 EXPECT_EQ(static_cast<int>(requests()->size()), | 248 EXPECT_EQ(static_cast<int>(requests()->size()), |
244 client_socket_factory_.allocation_count()); | 249 client_socket_factory_.allocation_count()); |
245 | 250 |
246 // First asynchronous request, and then last 5 pending requests. | 251 // First asynchronous request, and then last 5 pending requests. |
247 EXPECT_EQ(6U, completion_count()); | 252 EXPECT_EQ(6U, completion_count()); |
248 } | 253 } |
249 | 254 |
250 // This test will start up a RequestSocket() and then immediately Cancel() it. | 255 // This test will start up a RequestSocket() and then immediately Cancel() it. |
251 // The pending host resolution will eventually complete, and destroy the | 256 // The pending host resolution will eventually complete, and destroy the |
(...skipping 18 matching lines...) Expand all Loading... |
270 handle.Init("a", params_, kDefaultPriority, | 275 handle.Init("a", params_, kDefaultPriority, |
271 ClientSocketPool::RespectLimits::ENABLED, | 276 ClientSocketPool::RespectLimits::ENABLED, |
272 callback.callback(), &pool_, BoundNetLog())); | 277 callback.callback(), &pool_, BoundNetLog())); |
273 EXPECT_EQ(ERR_IO_PENDING, | 278 EXPECT_EQ(ERR_IO_PENDING, |
274 handle2.Init("a", params_, kDefaultPriority, | 279 handle2.Init("a", params_, kDefaultPriority, |
275 ClientSocketPool::RespectLimits::ENABLED, | 280 ClientSocketPool::RespectLimits::ENABLED, |
276 callback2.callback(), &pool_, BoundNetLog())); | 281 callback2.callback(), &pool_, BoundNetLog())); |
277 | 282 |
278 handle.Reset(); | 283 handle.Reset(); |
279 | 284 |
280 EXPECT_EQ(OK, callback2.WaitForResult()); | 285 EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
281 handle2.Reset(); | 286 handle2.Reset(); |
282 } | 287 } |
283 | 288 |
284 TEST_F(WebSocketTransportClientSocketPoolTest, ConnectCancelConnect) { | 289 TEST_F(WebSocketTransportClientSocketPoolTest, ConnectCancelConnect) { |
285 client_socket_factory_.set_default_client_socket_type( | 290 client_socket_factory_.set_default_client_socket_type( |
286 MockTransportClientSocketFactory::MOCK_PENDING_CLIENT_SOCKET); | 291 MockTransportClientSocketFactory::MOCK_PENDING_CLIENT_SOCKET); |
287 ClientSocketHandle handle; | 292 ClientSocketHandle handle; |
288 TestCompletionCallback callback; | 293 TestCompletionCallback callback; |
289 EXPECT_EQ(ERR_IO_PENDING, | 294 EXPECT_EQ(ERR_IO_PENDING, |
290 handle.Init("a", params_, kDefaultPriority, | 295 handle.Init("a", params_, kDefaultPriority, |
(...skipping 11 matching lines...) Expand all Loading... |
302 host_resolver_->set_synchronous_mode(true); | 307 host_resolver_->set_synchronous_mode(true); |
303 // At this point, handle has two ConnectingSockets out for it. Due to the | 308 // At this point, handle has two ConnectingSockets out for it. Due to the |
304 // setting the mock resolver into synchronous mode, the host resolution for | 309 // setting the mock resolver into synchronous mode, the host resolution for |
305 // both will return in the same loop of the MessageLoop. The client socket | 310 // both will return in the same loop of the MessageLoop. The client socket |
306 // is a pending socket, so the Connect() will asynchronously complete on the | 311 // is a pending socket, so the Connect() will asynchronously complete on the |
307 // next loop of the MessageLoop. That means that the first | 312 // next loop of the MessageLoop. That means that the first |
308 // ConnectingSocket will enter OnIOComplete, and then the second one will. | 313 // ConnectingSocket will enter OnIOComplete, and then the second one will. |
309 // If the first one is not cancelled, it will advance the load state, and | 314 // If the first one is not cancelled, it will advance the load state, and |
310 // then the second one will crash. | 315 // then the second one will crash. |
311 | 316 |
312 EXPECT_EQ(OK, callback2.WaitForResult()); | 317 EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
313 EXPECT_FALSE(callback.have_result()); | 318 EXPECT_FALSE(callback.have_result()); |
314 | 319 |
315 handle.Reset(); | 320 handle.Reset(); |
316 } | 321 } |
317 | 322 |
318 TEST_F(WebSocketTransportClientSocketPoolTest, CancelRequest) { | 323 TEST_F(WebSocketTransportClientSocketPoolTest, CancelRequest) { |
319 // First request finishes asynchronously. | 324 // First request finishes asynchronously. |
320 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); | 325 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsError(ERR_IO_PENDING)); |
321 EXPECT_EQ(OK, request(0)->WaitForResult()); | 326 EXPECT_THAT(request(0)->WaitForResult(), IsOk()); |
322 | 327 |
323 // Make all subsequent host resolutions complete synchronously. | 328 // Make all subsequent host resolutions complete synchronously. |
324 host_resolver_->set_synchronous_mode(true); | 329 host_resolver_->set_synchronous_mode(true); |
325 | 330 |
326 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); | 331 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsError(ERR_IO_PENDING)); |
327 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); | 332 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsError(ERR_IO_PENDING)); |
328 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); | 333 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsError(ERR_IO_PENDING)); |
329 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); | 334 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsError(ERR_IO_PENDING)); |
330 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); | 335 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsError(ERR_IO_PENDING)); |
331 | 336 |
332 // Cancel a request. | 337 // Cancel a request. |
333 const size_t index_to_cancel = 2; | 338 const size_t index_to_cancel = 2; |
334 EXPECT_FALSE(request(index_to_cancel)->handle()->is_initialized()); | 339 EXPECT_FALSE(request(index_to_cancel)->handle()->is_initialized()); |
335 request(index_to_cancel)->handle()->Reset(); | 340 request(index_to_cancel)->handle()->Reset(); |
336 | 341 |
337 ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE); | 342 ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE); |
338 | 343 |
339 EXPECT_EQ(5, client_socket_factory_.allocation_count()); | 344 EXPECT_EQ(5, client_socket_factory_.allocation_count()); |
340 | 345 |
(...skipping 12 matching lines...) Expand all Loading... |
353 // Function to be used as a callback on socket request completion. It first | 358 // Function to be used as a callback on socket request completion. It first |
354 // disconnects the successfully connected socket from the first request, and | 359 // disconnects the successfully connected socket from the first request, and |
355 // then reuses the ClientSocketHandle to request another socket. The second | 360 // then reuses the ClientSocketHandle to request another socket. The second |
356 // request is expected to succeed asynchronously. | 361 // request is expected to succeed asynchronously. |
357 // | 362 // |
358 // |nested_callback| is called with the result of the second socket request. | 363 // |nested_callback| is called with the result of the second socket request. |
359 void RequestSocketOnComplete(ClientSocketHandle* handle, | 364 void RequestSocketOnComplete(ClientSocketHandle* handle, |
360 WebSocketTransportClientSocketPool* pool, | 365 WebSocketTransportClientSocketPool* pool, |
361 const CompletionCallback& nested_callback, | 366 const CompletionCallback& nested_callback, |
362 int first_request_result) { | 367 int first_request_result) { |
363 EXPECT_EQ(OK, first_request_result); | 368 EXPECT_THAT(first_request_result, IsOk()); |
364 | 369 |
365 // Don't allow reuse of the socket. Disconnect it and then release it. | 370 // Don't allow reuse of the socket. Disconnect it and then release it. |
366 handle->socket()->Disconnect(); | 371 handle->socket()->Disconnect(); |
367 handle->Reset(); | 372 handle->Reset(); |
368 | 373 |
369 scoped_refptr<TransportSocketParams> dest(new TransportSocketParams( | 374 scoped_refptr<TransportSocketParams> dest(new TransportSocketParams( |
370 HostPortPair("www.google.com", 80), false, OnHostResolutionCallback(), | 375 HostPortPair("www.google.com", 80), false, OnHostResolutionCallback(), |
371 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DEFAULT)); | 376 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DEFAULT)); |
372 int rv = | 377 int rv = |
373 handle->Init("a", dest, LOWEST, ClientSocketPool::RespectLimits::ENABLED, | 378 handle->Init("a", dest, LOWEST, ClientSocketPool::RespectLimits::ENABLED, |
374 nested_callback, pool, BoundNetLog()); | 379 nested_callback, pool, BoundNetLog()); |
375 EXPECT_EQ(ERR_IO_PENDING, rv); | 380 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
376 if (ERR_IO_PENDING != rv) | 381 if (ERR_IO_PENDING != rv) |
377 nested_callback.Run(rv); | 382 nested_callback.Run(rv); |
378 } | 383 } |
379 | 384 |
380 // Tests the case where a second socket is requested in a completion callback, | 385 // Tests the case where a second socket is requested in a completion callback, |
381 // and the second socket connects asynchronously. Reuses the same | 386 // and the second socket connects asynchronously. Reuses the same |
382 // ClientSocketHandle for the second socket, after disconnecting the first. | 387 // ClientSocketHandle for the second socket, after disconnecting the first. |
383 TEST_F(WebSocketTransportClientSocketPoolTest, RequestTwice) { | 388 TEST_F(WebSocketTransportClientSocketPoolTest, RequestTwice) { |
384 ClientSocketHandle handle; | 389 ClientSocketHandle handle; |
385 scoped_refptr<TransportSocketParams> dest( | 390 scoped_refptr<TransportSocketParams> dest( |
386 new TransportSocketParams( | 391 new TransportSocketParams( |
387 HostPortPair("www.google.com", 80), | 392 HostPortPair("www.google.com", 80), |
388 false, | 393 false, |
389 OnHostResolutionCallback(), | 394 OnHostResolutionCallback(), |
390 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DEFAULT)); | 395 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DEFAULT)); |
391 TestCompletionCallback second_result_callback; | 396 TestCompletionCallback second_result_callback; |
392 int rv = | 397 int rv = |
393 handle.Init("a", dest, LOWEST, ClientSocketPool::RespectLimits::ENABLED, | 398 handle.Init("a", dest, LOWEST, ClientSocketPool::RespectLimits::ENABLED, |
394 base::Bind(&RequestSocketOnComplete, &handle, &pool_, | 399 base::Bind(&RequestSocketOnComplete, &handle, &pool_, |
395 second_result_callback.callback()), | 400 second_result_callback.callback()), |
396 &pool_, BoundNetLog()); | 401 &pool_, BoundNetLog()); |
397 ASSERT_EQ(ERR_IO_PENDING, rv); | 402 ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
398 EXPECT_EQ(OK, second_result_callback.WaitForResult()); | 403 EXPECT_THAT(second_result_callback.WaitForResult(), IsOk()); |
399 | 404 |
400 handle.Reset(); | 405 handle.Reset(); |
401 } | 406 } |
402 | 407 |
403 // Make sure that pending requests get serviced after active requests get | 408 // Make sure that pending requests get serviced after active requests get |
404 // cancelled. | 409 // cancelled. |
405 TEST_F(WebSocketTransportClientSocketPoolTest, | 410 TEST_F(WebSocketTransportClientSocketPoolTest, |
406 CancelActiveRequestWithPendingRequests) { | 411 CancelActiveRequestWithPendingRequests) { |
407 client_socket_factory_.set_default_client_socket_type( | 412 client_socket_factory_.set_default_client_socket_type( |
408 MockTransportClientSocketFactory::MOCK_PENDING_CLIENT_SOCKET); | 413 MockTransportClientSocketFactory::MOCK_PENDING_CLIENT_SOCKET); |
409 | 414 |
410 // Queue up all the requests | 415 // Queue up all the requests |
411 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); | 416 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsError(ERR_IO_PENDING)); |
412 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); | 417 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsError(ERR_IO_PENDING)); |
413 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); | 418 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsError(ERR_IO_PENDING)); |
414 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); | 419 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsError(ERR_IO_PENDING)); |
415 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); | 420 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsError(ERR_IO_PENDING)); |
416 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); | 421 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsError(ERR_IO_PENDING)); |
417 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); | 422 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsError(ERR_IO_PENDING)); |
418 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); | 423 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsError(ERR_IO_PENDING)); |
419 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); | 424 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsError(ERR_IO_PENDING)); |
420 | 425 |
421 // Now, kMaxSocketsPerGroup requests should be active. Let's cancel them. | 426 // Now, kMaxSocketsPerGroup requests should be active. Let's cancel them. |
422 ASSERT_LE(kMaxSocketsPerGroup, static_cast<int>(requests()->size())); | 427 ASSERT_LE(kMaxSocketsPerGroup, static_cast<int>(requests()->size())); |
423 for (int i = 0; i < kMaxSocketsPerGroup; i++) | 428 for (int i = 0; i < kMaxSocketsPerGroup; i++) |
424 request(i)->handle()->Reset(); | 429 request(i)->handle()->Reset(); |
425 | 430 |
426 // Let's wait for the rest to complete now. | 431 // Let's wait for the rest to complete now. |
427 for (size_t i = kMaxSocketsPerGroup; i < requests()->size(); ++i) { | 432 for (size_t i = kMaxSocketsPerGroup; i < requests()->size(); ++i) { |
428 EXPECT_EQ(OK, request(i)->WaitForResult()); | 433 EXPECT_THAT(request(i)->WaitForResult(), IsOk()); |
429 request(i)->handle()->Reset(); | 434 request(i)->handle()->Reset(); |
430 } | 435 } |
431 | 436 |
432 EXPECT_EQ(requests()->size() - kMaxSocketsPerGroup, completion_count()); | 437 EXPECT_EQ(requests()->size() - kMaxSocketsPerGroup, completion_count()); |
433 } | 438 } |
434 | 439 |
435 // Make sure that pending requests get serviced after active requests fail. | 440 // Make sure that pending requests get serviced after active requests fail. |
436 TEST_F(WebSocketTransportClientSocketPoolTest, | 441 TEST_F(WebSocketTransportClientSocketPoolTest, |
437 FailingActiveRequestWithPendingRequests) { | 442 FailingActiveRequestWithPendingRequests) { |
438 client_socket_factory_.set_default_client_socket_type( | 443 client_socket_factory_.set_default_client_socket_type( |
439 MockTransportClientSocketFactory::MOCK_PENDING_FAILING_CLIENT_SOCKET); | 444 MockTransportClientSocketFactory::MOCK_PENDING_FAILING_CLIENT_SOCKET); |
440 | 445 |
441 const int kNumRequests = 2 * kMaxSocketsPerGroup + 1; | 446 const int kNumRequests = 2 * kMaxSocketsPerGroup + 1; |
442 ASSERT_LE(kNumRequests, kMaxSockets); // Otherwise the test will hang. | 447 ASSERT_LE(kNumRequests, kMaxSockets); // Otherwise the test will hang. |
443 | 448 |
444 // Queue up all the requests | 449 // Queue up all the requests |
445 for (int i = 0; i < kNumRequests; i++) | 450 for (int i = 0; i < kNumRequests; i++) |
446 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); | 451 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsError(ERR_IO_PENDING)); |
447 | 452 |
448 for (int i = 0; i < kNumRequests; i++) | 453 for (int i = 0; i < kNumRequests; i++) |
449 EXPECT_EQ(ERR_CONNECTION_FAILED, request(i)->WaitForResult()); | 454 EXPECT_THAT(request(i)->WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
450 } | 455 } |
451 | 456 |
452 // The lock on the endpoint is released when a ClientSocketHandle is reset. | 457 // The lock on the endpoint is released when a ClientSocketHandle is reset. |
453 TEST_F(WebSocketTransportClientSocketPoolTest, LockReleasedOnHandleReset) { | 458 TEST_F(WebSocketTransportClientSocketPoolTest, LockReleasedOnHandleReset) { |
454 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); | 459 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsError(ERR_IO_PENDING)); |
455 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); | 460 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsError(ERR_IO_PENDING)); |
456 EXPECT_EQ(OK, request(0)->WaitForResult()); | 461 EXPECT_THAT(request(0)->WaitForResult(), IsOk()); |
457 EXPECT_FALSE(request(1)->handle()->is_initialized()); | 462 EXPECT_FALSE(request(1)->handle()->is_initialized()); |
458 request(0)->handle()->Reset(); | 463 request(0)->handle()->Reset(); |
459 RunUntilIdle(); | 464 RunUntilIdle(); |
460 EXPECT_TRUE(request(1)->handle()->is_initialized()); | 465 EXPECT_TRUE(request(1)->handle()->is_initialized()); |
461 } | 466 } |
462 | 467 |
463 // The lock on the endpoint is released when a ClientSocketHandle is deleted. | 468 // The lock on the endpoint is released when a ClientSocketHandle is deleted. |
464 TEST_F(WebSocketTransportClientSocketPoolTest, LockReleasedOnHandleDelete) { | 469 TEST_F(WebSocketTransportClientSocketPoolTest, LockReleasedOnHandleDelete) { |
465 TestCompletionCallback callback; | 470 TestCompletionCallback callback; |
466 std::unique_ptr<ClientSocketHandle> handle(new ClientSocketHandle); | 471 std::unique_ptr<ClientSocketHandle> handle(new ClientSocketHandle); |
467 int rv = | 472 int rv = |
468 handle->Init("a", params_, LOW, ClientSocketPool::RespectLimits::ENABLED, | 473 handle->Init("a", params_, LOW, ClientSocketPool::RespectLimits::ENABLED, |
469 callback.callback(), &pool_, BoundNetLog()); | 474 callback.callback(), &pool_, BoundNetLog()); |
470 EXPECT_EQ(ERR_IO_PENDING, rv); | 475 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
471 | 476 |
472 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); | 477 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsError(ERR_IO_PENDING)); |
473 EXPECT_EQ(OK, callback.WaitForResult()); | 478 EXPECT_THAT(callback.WaitForResult(), IsOk()); |
474 EXPECT_FALSE(request(0)->handle()->is_initialized()); | 479 EXPECT_FALSE(request(0)->handle()->is_initialized()); |
475 handle.reset(); | 480 handle.reset(); |
476 RunUntilIdle(); | 481 RunUntilIdle(); |
477 EXPECT_TRUE(request(0)->handle()->is_initialized()); | 482 EXPECT_TRUE(request(0)->handle()->is_initialized()); |
478 } | 483 } |
479 | 484 |
480 // A new connection is performed when the lock on the previous connection is | 485 // A new connection is performed when the lock on the previous connection is |
481 // explicitly released. | 486 // explicitly released. |
482 TEST_F(WebSocketTransportClientSocketPoolTest, | 487 TEST_F(WebSocketTransportClientSocketPoolTest, |
483 ConnectionProceedsOnExplicitRelease) { | 488 ConnectionProceedsOnExplicitRelease) { |
484 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); | 489 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsError(ERR_IO_PENDING)); |
485 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); | 490 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsError(ERR_IO_PENDING)); |
486 EXPECT_EQ(OK, request(0)->WaitForResult()); | 491 EXPECT_THAT(request(0)->WaitForResult(), IsOk()); |
487 EXPECT_FALSE(request(1)->handle()->is_initialized()); | 492 EXPECT_FALSE(request(1)->handle()->is_initialized()); |
488 WebSocketTransportClientSocketPool::UnlockEndpoint(request(0)->handle()); | 493 WebSocketTransportClientSocketPool::UnlockEndpoint(request(0)->handle()); |
489 RunUntilIdle(); | 494 RunUntilIdle(); |
490 EXPECT_TRUE(request(1)->handle()->is_initialized()); | 495 EXPECT_TRUE(request(1)->handle()->is_initialized()); |
491 } | 496 } |
492 | 497 |
493 // A connection which is cancelled before completion does not block subsequent | 498 // A connection which is cancelled before completion does not block subsequent |
494 // connections. | 499 // connections. |
495 TEST_F(WebSocketTransportClientSocketPoolTest, | 500 TEST_F(WebSocketTransportClientSocketPoolTest, |
496 CancelDuringConnectionReleasesLock) { | 501 CancelDuringConnectionReleasesLock) { |
497 MockTransportClientSocketFactory::ClientSocketType case_types[] = { | 502 MockTransportClientSocketFactory::ClientSocketType case_types[] = { |
498 MockTransportClientSocketFactory::MOCK_STALLED_CLIENT_SOCKET, | 503 MockTransportClientSocketFactory::MOCK_STALLED_CLIENT_SOCKET, |
499 MockTransportClientSocketFactory::MOCK_PENDING_CLIENT_SOCKET}; | 504 MockTransportClientSocketFactory::MOCK_PENDING_CLIENT_SOCKET}; |
500 | 505 |
501 client_socket_factory_.set_client_socket_types(case_types, | 506 client_socket_factory_.set_client_socket_types(case_types, |
502 arraysize(case_types)); | 507 arraysize(case_types)); |
503 | 508 |
504 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); | 509 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsError(ERR_IO_PENDING)); |
505 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); | 510 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsError(ERR_IO_PENDING)); |
506 RunUntilIdle(); | 511 RunUntilIdle(); |
507 pool_.CancelRequest("a", request(0)->handle()); | 512 pool_.CancelRequest("a", request(0)->handle()); |
508 EXPECT_EQ(OK, request(1)->WaitForResult()); | 513 EXPECT_THAT(request(1)->WaitForResult(), IsOk()); |
509 } | 514 } |
510 | 515 |
511 // Test the case of the IPv6 address stalling, and falling back to the IPv4 | 516 // Test the case of the IPv6 address stalling, and falling back to the IPv4 |
512 // socket which finishes first. | 517 // socket which finishes first. |
513 TEST_F(WebSocketTransportClientSocketPoolTest, | 518 TEST_F(WebSocketTransportClientSocketPoolTest, |
514 IPv6FallbackSocketIPv4FinishesFirst) { | 519 IPv6FallbackSocketIPv4FinishesFirst) { |
515 WebSocketTransportClientSocketPool pool(kMaxSockets, | 520 WebSocketTransportClientSocketPool pool(kMaxSockets, |
516 kMaxSocketsPerGroup, | 521 kMaxSocketsPerGroup, |
517 host_resolver_.get(), | 522 host_resolver_.get(), |
518 &client_socket_factory_, | 523 &client_socket_factory_, |
519 NULL); | 524 NULL); |
520 | 525 |
521 MockTransportClientSocketFactory::ClientSocketType case_types[] = { | 526 MockTransportClientSocketFactory::ClientSocketType case_types[] = { |
522 // This is the IPv6 socket. | 527 // This is the IPv6 socket. |
523 MockTransportClientSocketFactory::MOCK_STALLED_CLIENT_SOCKET, | 528 MockTransportClientSocketFactory::MOCK_STALLED_CLIENT_SOCKET, |
524 // This is the IPv4 socket. | 529 // This is the IPv4 socket. |
525 MockTransportClientSocketFactory::MOCK_PENDING_CLIENT_SOCKET}; | 530 MockTransportClientSocketFactory::MOCK_PENDING_CLIENT_SOCKET}; |
526 | 531 |
527 client_socket_factory_.set_client_socket_types(case_types, 2); | 532 client_socket_factory_.set_client_socket_types(case_types, 2); |
528 | 533 |
529 // Resolve an AddressList with an IPv6 address first and then an IPv4 address. | 534 // Resolve an AddressList with an IPv6 address first and then an IPv4 address. |
530 host_resolver_->rules()->AddIPLiteralRule( | 535 host_resolver_->rules()->AddIPLiteralRule( |
531 "*", "2:abcd::3:4:ff,2.2.2.2", std::string()); | 536 "*", "2:abcd::3:4:ff,2.2.2.2", std::string()); |
532 | 537 |
533 TestCompletionCallback callback; | 538 TestCompletionCallback callback; |
534 ClientSocketHandle handle; | 539 ClientSocketHandle handle; |
535 int rv = | 540 int rv = |
536 handle.Init("a", params_, LOW, ClientSocketPool::RespectLimits::ENABLED, | 541 handle.Init("a", params_, LOW, ClientSocketPool::RespectLimits::ENABLED, |
537 callback.callback(), &pool, BoundNetLog()); | 542 callback.callback(), &pool, BoundNetLog()); |
538 EXPECT_EQ(ERR_IO_PENDING, rv); | 543 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
539 EXPECT_FALSE(handle.is_initialized()); | 544 EXPECT_FALSE(handle.is_initialized()); |
540 EXPECT_FALSE(handle.socket()); | 545 EXPECT_FALSE(handle.socket()); |
541 | 546 |
542 EXPECT_EQ(OK, callback.WaitForResult()); | 547 EXPECT_THAT(callback.WaitForResult(), IsOk()); |
543 EXPECT_TRUE(handle.is_initialized()); | 548 EXPECT_TRUE(handle.is_initialized()); |
544 EXPECT_TRUE(handle.socket()); | 549 EXPECT_TRUE(handle.socket()); |
545 IPEndPoint endpoint; | 550 IPEndPoint endpoint; |
546 handle.socket()->GetLocalAddress(&endpoint); | 551 handle.socket()->GetLocalAddress(&endpoint); |
547 EXPECT_TRUE(endpoint.address().IsIPv4()); | 552 EXPECT_TRUE(endpoint.address().IsIPv4()); |
548 EXPECT_EQ(2, client_socket_factory_.allocation_count()); | 553 EXPECT_EQ(2, client_socket_factory_.allocation_count()); |
549 } | 554 } |
550 | 555 |
551 // Test the case of the IPv6 address being slow, thus falling back to trying to | 556 // Test the case of the IPv6 address being slow, thus falling back to trying to |
552 // connect to the IPv4 address, but having the connect to the IPv6 address | 557 // connect to the IPv4 address, but having the connect to the IPv6 address |
(...skipping 18 matching lines...) Expand all Loading... |
571 | 576 |
572 // Resolve an AddressList with an IPv6 address first and then an IPv4 address. | 577 // Resolve an AddressList with an IPv6 address first and then an IPv4 address. |
573 host_resolver_->rules()->AddIPLiteralRule( | 578 host_resolver_->rules()->AddIPLiteralRule( |
574 "*", "2:abcd::3:4:ff,2.2.2.2", std::string()); | 579 "*", "2:abcd::3:4:ff,2.2.2.2", std::string()); |
575 | 580 |
576 TestCompletionCallback callback; | 581 TestCompletionCallback callback; |
577 ClientSocketHandle handle; | 582 ClientSocketHandle handle; |
578 int rv = | 583 int rv = |
579 handle.Init("a", params_, LOW, ClientSocketPool::RespectLimits::ENABLED, | 584 handle.Init("a", params_, LOW, ClientSocketPool::RespectLimits::ENABLED, |
580 callback.callback(), &pool, BoundNetLog()); | 585 callback.callback(), &pool, BoundNetLog()); |
581 EXPECT_EQ(ERR_IO_PENDING, rv); | 586 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
582 EXPECT_FALSE(handle.is_initialized()); | 587 EXPECT_FALSE(handle.is_initialized()); |
583 EXPECT_FALSE(handle.socket()); | 588 EXPECT_FALSE(handle.socket()); |
584 | 589 |
585 EXPECT_EQ(OK, callback.WaitForResult()); | 590 EXPECT_THAT(callback.WaitForResult(), IsOk()); |
586 EXPECT_TRUE(handle.is_initialized()); | 591 EXPECT_TRUE(handle.is_initialized()); |
587 EXPECT_TRUE(handle.socket()); | 592 EXPECT_TRUE(handle.socket()); |
588 IPEndPoint endpoint; | 593 IPEndPoint endpoint; |
589 handle.socket()->GetLocalAddress(&endpoint); | 594 handle.socket()->GetLocalAddress(&endpoint); |
590 EXPECT_TRUE(endpoint.address().IsIPv6()); | 595 EXPECT_TRUE(endpoint.address().IsIPv6()); |
591 EXPECT_EQ(2, client_socket_factory_.allocation_count()); | 596 EXPECT_EQ(2, client_socket_factory_.allocation_count()); |
592 } | 597 } |
593 | 598 |
594 TEST_F(WebSocketTransportClientSocketPoolTest, | 599 TEST_F(WebSocketTransportClientSocketPoolTest, |
595 IPv6NoIPv4AddressesToFallbackTo) { | 600 IPv6NoIPv4AddressesToFallbackTo) { |
596 WebSocketTransportClientSocketPool pool(kMaxSockets, | 601 WebSocketTransportClientSocketPool pool(kMaxSockets, |
597 kMaxSocketsPerGroup, | 602 kMaxSocketsPerGroup, |
598 host_resolver_.get(), | 603 host_resolver_.get(), |
599 &client_socket_factory_, | 604 &client_socket_factory_, |
600 NULL); | 605 NULL); |
601 | 606 |
602 client_socket_factory_.set_default_client_socket_type( | 607 client_socket_factory_.set_default_client_socket_type( |
603 MockTransportClientSocketFactory::MOCK_DELAYED_CLIENT_SOCKET); | 608 MockTransportClientSocketFactory::MOCK_DELAYED_CLIENT_SOCKET); |
604 | 609 |
605 // Resolve an AddressList with only IPv6 addresses. | 610 // Resolve an AddressList with only IPv6 addresses. |
606 host_resolver_->rules()->AddIPLiteralRule( | 611 host_resolver_->rules()->AddIPLiteralRule( |
607 "*", "2:abcd::3:4:ff,3:abcd::3:4:ff", std::string()); | 612 "*", "2:abcd::3:4:ff,3:abcd::3:4:ff", std::string()); |
608 | 613 |
609 TestCompletionCallback callback; | 614 TestCompletionCallback callback; |
610 ClientSocketHandle handle; | 615 ClientSocketHandle handle; |
611 int rv = | 616 int rv = |
612 handle.Init("a", params_, LOW, ClientSocketPool::RespectLimits::ENABLED, | 617 handle.Init("a", params_, LOW, ClientSocketPool::RespectLimits::ENABLED, |
613 callback.callback(), &pool, BoundNetLog()); | 618 callback.callback(), &pool, BoundNetLog()); |
614 EXPECT_EQ(ERR_IO_PENDING, rv); | 619 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
615 EXPECT_FALSE(handle.is_initialized()); | 620 EXPECT_FALSE(handle.is_initialized()); |
616 EXPECT_FALSE(handle.socket()); | 621 EXPECT_FALSE(handle.socket()); |
617 | 622 |
618 EXPECT_EQ(OK, callback.WaitForResult()); | 623 EXPECT_THAT(callback.WaitForResult(), IsOk()); |
619 EXPECT_TRUE(handle.is_initialized()); | 624 EXPECT_TRUE(handle.is_initialized()); |
620 EXPECT_TRUE(handle.socket()); | 625 EXPECT_TRUE(handle.socket()); |
621 IPEndPoint endpoint; | 626 IPEndPoint endpoint; |
622 handle.socket()->GetLocalAddress(&endpoint); | 627 handle.socket()->GetLocalAddress(&endpoint); |
623 EXPECT_TRUE(endpoint.address().IsIPv6()); | 628 EXPECT_TRUE(endpoint.address().IsIPv6()); |
624 EXPECT_EQ(1, client_socket_factory_.allocation_count()); | 629 EXPECT_EQ(1, client_socket_factory_.allocation_count()); |
625 } | 630 } |
626 | 631 |
627 TEST_F(WebSocketTransportClientSocketPoolTest, IPv4HasNoFallback) { | 632 TEST_F(WebSocketTransportClientSocketPoolTest, IPv4HasNoFallback) { |
628 WebSocketTransportClientSocketPool pool(kMaxSockets, | 633 WebSocketTransportClientSocketPool pool(kMaxSockets, |
629 kMaxSocketsPerGroup, | 634 kMaxSocketsPerGroup, |
630 host_resolver_.get(), | 635 host_resolver_.get(), |
631 &client_socket_factory_, | 636 &client_socket_factory_, |
632 NULL); | 637 NULL); |
633 | 638 |
634 client_socket_factory_.set_default_client_socket_type( | 639 client_socket_factory_.set_default_client_socket_type( |
635 MockTransportClientSocketFactory::MOCK_DELAYED_CLIENT_SOCKET); | 640 MockTransportClientSocketFactory::MOCK_DELAYED_CLIENT_SOCKET); |
636 | 641 |
637 // Resolve an AddressList with only IPv4 addresses. | 642 // Resolve an AddressList with only IPv4 addresses. |
638 host_resolver_->rules()->AddIPLiteralRule("*", "1.1.1.1", std::string()); | 643 host_resolver_->rules()->AddIPLiteralRule("*", "1.1.1.1", std::string()); |
639 | 644 |
640 TestCompletionCallback callback; | 645 TestCompletionCallback callback; |
641 ClientSocketHandle handle; | 646 ClientSocketHandle handle; |
642 int rv = | 647 int rv = |
643 handle.Init("a", params_, LOW, ClientSocketPool::RespectLimits::ENABLED, | 648 handle.Init("a", params_, LOW, ClientSocketPool::RespectLimits::ENABLED, |
644 callback.callback(), &pool, BoundNetLog()); | 649 callback.callback(), &pool, BoundNetLog()); |
645 EXPECT_EQ(ERR_IO_PENDING, rv); | 650 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
646 EXPECT_FALSE(handle.is_initialized()); | 651 EXPECT_FALSE(handle.is_initialized()); |
647 EXPECT_FALSE(handle.socket()); | 652 EXPECT_FALSE(handle.socket()); |
648 | 653 |
649 EXPECT_EQ(OK, callback.WaitForResult()); | 654 EXPECT_THAT(callback.WaitForResult(), IsOk()); |
650 EXPECT_TRUE(handle.is_initialized()); | 655 EXPECT_TRUE(handle.is_initialized()); |
651 EXPECT_TRUE(handle.socket()); | 656 EXPECT_TRUE(handle.socket()); |
652 IPEndPoint endpoint; | 657 IPEndPoint endpoint; |
653 handle.socket()->GetLocalAddress(&endpoint); | 658 handle.socket()->GetLocalAddress(&endpoint); |
654 EXPECT_TRUE(endpoint.address().IsIPv4()); | 659 EXPECT_TRUE(endpoint.address().IsIPv4()); |
655 EXPECT_EQ(1, client_socket_factory_.allocation_count()); | 660 EXPECT_EQ(1, client_socket_factory_.allocation_count()); |
656 } | 661 } |
657 | 662 |
658 // If all IPv6 addresses fail to connect synchronously, then IPv4 connections | 663 // If all IPv6 addresses fail to connect synchronously, then IPv4 connections |
659 // proceeed immediately. | 664 // proceeed immediately. |
(...skipping 17 matching lines...) Expand all Loading... |
677 | 682 |
678 // Resolve an AddressList with two IPv6 addresses and then an IPv4 address. | 683 // Resolve an AddressList with two IPv6 addresses and then an IPv4 address. |
679 host_resolver_->rules()->AddIPLiteralRule( | 684 host_resolver_->rules()->AddIPLiteralRule( |
680 "*", "2:abcd::3:4:ff,2:abcd::3:5:ff,2.2.2.2", std::string()); | 685 "*", "2:abcd::3:4:ff,2:abcd::3:5:ff,2.2.2.2", std::string()); |
681 host_resolver_->set_synchronous_mode(true); | 686 host_resolver_->set_synchronous_mode(true); |
682 TestCompletionCallback callback; | 687 TestCompletionCallback callback; |
683 ClientSocketHandle handle; | 688 ClientSocketHandle handle; |
684 int rv = | 689 int rv = |
685 handle.Init("a", params_, LOW, ClientSocketPool::RespectLimits::ENABLED, | 690 handle.Init("a", params_, LOW, ClientSocketPool::RespectLimits::ENABLED, |
686 callback.callback(), &pool, BoundNetLog()); | 691 callback.callback(), &pool, BoundNetLog()); |
687 EXPECT_EQ(OK, rv); | 692 EXPECT_THAT(rv, IsOk()); |
688 ASSERT_TRUE(handle.socket()); | 693 ASSERT_TRUE(handle.socket()); |
689 | 694 |
690 IPEndPoint endpoint; | 695 IPEndPoint endpoint; |
691 handle.socket()->GetPeerAddress(&endpoint); | 696 handle.socket()->GetPeerAddress(&endpoint); |
692 EXPECT_EQ("2.2.2.2", endpoint.ToStringWithoutPort()); | 697 EXPECT_EQ("2.2.2.2", endpoint.ToStringWithoutPort()); |
693 } | 698 } |
694 | 699 |
695 // If all IPv6 addresses fail before the IPv4 fallback timeout, then the IPv4 | 700 // If all IPv6 addresses fail before the IPv4 fallback timeout, then the IPv4 |
696 // connections proceed immediately. | 701 // connections proceed immediately. |
697 TEST_F(WebSocketTransportClientSocketPoolTest, IPv6RapidFail) { | 702 TEST_F(WebSocketTransportClientSocketPoolTest, IPv6RapidFail) { |
(...skipping 16 matching lines...) Expand all Loading... |
714 | 719 |
715 // Resolve an AddressList with two IPv6 addresses and then an IPv4 address. | 720 // Resolve an AddressList with two IPv6 addresses and then an IPv4 address. |
716 host_resolver_->rules()->AddIPLiteralRule( | 721 host_resolver_->rules()->AddIPLiteralRule( |
717 "*", "2:abcd::3:4:ff,2:abcd::3:5:ff,2.2.2.2", std::string()); | 722 "*", "2:abcd::3:4:ff,2:abcd::3:5:ff,2.2.2.2", std::string()); |
718 | 723 |
719 TestCompletionCallback callback; | 724 TestCompletionCallback callback; |
720 ClientSocketHandle handle; | 725 ClientSocketHandle handle; |
721 int rv = | 726 int rv = |
722 handle.Init("a", params_, LOW, ClientSocketPool::RespectLimits::ENABLED, | 727 handle.Init("a", params_, LOW, ClientSocketPool::RespectLimits::ENABLED, |
723 callback.callback(), &pool, BoundNetLog()); | 728 callback.callback(), &pool, BoundNetLog()); |
724 EXPECT_EQ(ERR_IO_PENDING, rv); | 729 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
725 EXPECT_FALSE(handle.socket()); | 730 EXPECT_FALSE(handle.socket()); |
726 | 731 |
727 base::TimeTicks start(base::TimeTicks::Now()); | 732 base::TimeTicks start(base::TimeTicks::Now()); |
728 EXPECT_EQ(OK, callback.WaitForResult()); | 733 EXPECT_THAT(callback.WaitForResult(), IsOk()); |
729 EXPECT_LT(base::TimeTicks::Now() - start, | 734 EXPECT_LT(base::TimeTicks::Now() - start, |
730 base::TimeDelta::FromMilliseconds( | 735 base::TimeDelta::FromMilliseconds( |
731 TransportConnectJob::kIPv6FallbackTimerInMs)); | 736 TransportConnectJob::kIPv6FallbackTimerInMs)); |
732 ASSERT_TRUE(handle.socket()); | 737 ASSERT_TRUE(handle.socket()); |
733 | 738 |
734 IPEndPoint endpoint; | 739 IPEndPoint endpoint; |
735 handle.socket()->GetPeerAddress(&endpoint); | 740 handle.socket()->GetPeerAddress(&endpoint); |
736 EXPECT_EQ("2.2.2.2", endpoint.ToStringWithoutPort()); | 741 EXPECT_EQ("2.2.2.2", endpoint.ToStringWithoutPort()); |
737 } | 742 } |
738 | 743 |
(...skipping 12 matching lines...) Expand all Loading... |
751 | 756 |
752 // Resolve an AddressList with an IPv6 addresses and an IPv4 address. | 757 // Resolve an AddressList with an IPv6 addresses and an IPv4 address. |
753 host_resolver_->rules()->AddIPLiteralRule( | 758 host_resolver_->rules()->AddIPLiteralRule( |
754 "*", "2:abcd::3:4:ff,2.2.2.2", std::string()); | 759 "*", "2:abcd::3:4:ff,2.2.2.2", std::string()); |
755 | 760 |
756 TestCompletionCallback callback; | 761 TestCompletionCallback callback; |
757 ClientSocketHandle handle; | 762 ClientSocketHandle handle; |
758 int rv = | 763 int rv = |
759 handle.Init("a", params_, LOW, ClientSocketPool::RespectLimits::ENABLED, | 764 handle.Init("a", params_, LOW, ClientSocketPool::RespectLimits::ENABLED, |
760 callback.callback(), &pool, BoundNetLog()); | 765 callback.callback(), &pool, BoundNetLog()); |
761 EXPECT_EQ(ERR_IO_PENDING, rv); | 766 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
762 ASSERT_FALSE(handle.socket()); | 767 ASSERT_FALSE(handle.socket()); |
763 | 768 |
764 base::Closure ipv6_connect_trigger = | 769 base::Closure ipv6_connect_trigger = |
765 client_socket_factory_.WaitForTriggerableSocketCreation(); | 770 client_socket_factory_.WaitForTriggerableSocketCreation(); |
766 base::Closure ipv4_connect_trigger = | 771 base::Closure ipv4_connect_trigger = |
767 client_socket_factory_.WaitForTriggerableSocketCreation(); | 772 client_socket_factory_.WaitForTriggerableSocketCreation(); |
768 | 773 |
769 ipv4_connect_trigger.Run(); | 774 ipv4_connect_trigger.Run(); |
770 ipv6_connect_trigger.Run(); | 775 ipv6_connect_trigger.Run(); |
771 | 776 |
772 EXPECT_EQ(OK, callback.WaitForResult()); | 777 EXPECT_THAT(callback.WaitForResult(), IsOk()); |
773 ASSERT_TRUE(handle.socket()); | 778 ASSERT_TRUE(handle.socket()); |
774 | 779 |
775 IPEndPoint endpoint; | 780 IPEndPoint endpoint; |
776 handle.socket()->GetPeerAddress(&endpoint); | 781 handle.socket()->GetPeerAddress(&endpoint); |
777 EXPECT_EQ("2.2.2.2", endpoint.ToStringWithoutPort()); | 782 EXPECT_EQ("2.2.2.2", endpoint.ToStringWithoutPort()); |
778 } | 783 } |
779 | 784 |
780 // We should not report failure until all connections have failed. | 785 // We should not report failure until all connections have failed. |
781 TEST_F(WebSocketTransportClientSocketPoolTest, LastFailureWins) { | 786 TEST_F(WebSocketTransportClientSocketPoolTest, LastFailureWins) { |
782 WebSocketTransportClientSocketPool pool(kMaxSockets, | 787 WebSocketTransportClientSocketPool pool(kMaxSockets, |
(...skipping 21 matching lines...) Expand all Loading... |
804 // After 300ms: Connect to 3:abcd::3:4:ff times out, IPv4 fallback starts | 809 // After 300ms: Connect to 3:abcd::3:4:ff times out, IPv4 fallback starts |
805 // After 400ms: Connect to 4:abcd::3:4:ff and 1.1.1.1 time out | 810 // After 400ms: Connect to 4:abcd::3:4:ff and 1.1.1.1 time out |
806 // After 500ms: Connect to 2.2.2.2 times out | 811 // After 500ms: Connect to 2.2.2.2 times out |
807 | 812 |
808 TestCompletionCallback callback; | 813 TestCompletionCallback callback; |
809 ClientSocketHandle handle; | 814 ClientSocketHandle handle; |
810 base::TimeTicks start(base::TimeTicks::Now()); | 815 base::TimeTicks start(base::TimeTicks::Now()); |
811 int rv = | 816 int rv = |
812 handle.Init("a", params_, LOW, ClientSocketPool::RespectLimits::ENABLED, | 817 handle.Init("a", params_, LOW, ClientSocketPool::RespectLimits::ENABLED, |
813 callback.callback(), &pool, BoundNetLog()); | 818 callback.callback(), &pool, BoundNetLog()); |
814 EXPECT_EQ(ERR_IO_PENDING, rv); | 819 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
815 | 820 |
816 EXPECT_EQ(ERR_CONNECTION_FAILED, callback.WaitForResult()); | 821 EXPECT_THAT(callback.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
817 | 822 |
818 EXPECT_GE(base::TimeTicks::Now() - start, delay * 5); | 823 EXPECT_GE(base::TimeTicks::Now() - start, delay * 5); |
819 } | 824 } |
820 | 825 |
821 // Global timeout for all connects applies. This test is disabled by default | 826 // Global timeout for all connects applies. This test is disabled by default |
822 // because it takes 4 minutes. Run with --gtest_also_run_disabled_tests if you | 827 // because it takes 4 minutes. Run with --gtest_also_run_disabled_tests if you |
823 // want to run it. | 828 // want to run it. |
824 TEST_F(WebSocketTransportClientSocketPoolTest, DISABLED_OverallTimeoutApplies) { | 829 TEST_F(WebSocketTransportClientSocketPoolTest, DISABLED_OverallTimeoutApplies) { |
825 WebSocketTransportClientSocketPool pool(kMaxSockets, | 830 WebSocketTransportClientSocketPool pool(kMaxSockets, |
826 kMaxSocketsPerGroup, | 831 kMaxSocketsPerGroup, |
(...skipping 15 matching lines...) Expand all Loading... |
842 "1.1.1.1,2.2.2.2,3.3.3.3," | 847 "1.1.1.1,2.2.2.2,3.3.3.3," |
843 "4.4.4.4,5.5.5.5,6.6.6.6", | 848 "4.4.4.4,5.5.5.5,6.6.6.6", |
844 std::string()); | 849 std::string()); |
845 | 850 |
846 TestCompletionCallback callback; | 851 TestCompletionCallback callback; |
847 ClientSocketHandle handle; | 852 ClientSocketHandle handle; |
848 | 853 |
849 int rv = | 854 int rv = |
850 handle.Init("a", params_, LOW, ClientSocketPool::RespectLimits::ENABLED, | 855 handle.Init("a", params_, LOW, ClientSocketPool::RespectLimits::ENABLED, |
851 callback.callback(), &pool, BoundNetLog()); | 856 callback.callback(), &pool, BoundNetLog()); |
852 EXPECT_EQ(ERR_IO_PENDING, rv); | 857 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
853 | 858 |
854 EXPECT_EQ(ERR_TIMED_OUT, callback.WaitForResult()); | 859 EXPECT_THAT(callback.WaitForResult(), IsError(ERR_TIMED_OUT)); |
855 } | 860 } |
856 | 861 |
857 TEST_F(WebSocketTransportClientSocketPoolTest, MaxSocketsEnforced) { | 862 TEST_F(WebSocketTransportClientSocketPoolTest, MaxSocketsEnforced) { |
858 host_resolver_->set_synchronous_mode(true); | 863 host_resolver_->set_synchronous_mode(true); |
859 for (int i = 0; i < kMaxSockets; ++i) { | 864 for (int i = 0; i < kMaxSockets; ++i) { |
860 ASSERT_EQ(OK, StartRequest("a", kDefaultPriority)); | 865 ASSERT_THAT(StartRequest("a", kDefaultPriority), IsOk()); |
861 WebSocketTransportClientSocketPool::UnlockEndpoint(request(i)->handle()); | 866 WebSocketTransportClientSocketPool::UnlockEndpoint(request(i)->handle()); |
862 RunUntilIdle(); | 867 RunUntilIdle(); |
863 } | 868 } |
864 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); | 869 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsError(ERR_IO_PENDING)); |
865 } | 870 } |
866 | 871 |
867 TEST_F(WebSocketTransportClientSocketPoolTest, MaxSocketsEnforcedWhenPending) { | 872 TEST_F(WebSocketTransportClientSocketPoolTest, MaxSocketsEnforcedWhenPending) { |
868 for (int i = 0; i < kMaxSockets + 1; ++i) { | 873 for (int i = 0; i < kMaxSockets + 1; ++i) { |
869 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); | 874 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsError(ERR_IO_PENDING)); |
870 } | 875 } |
871 // Now there are 32 sockets waiting to connect, and one stalled. | 876 // Now there are 32 sockets waiting to connect, and one stalled. |
872 for (int i = 0; i < kMaxSockets; ++i) { | 877 for (int i = 0; i < kMaxSockets; ++i) { |
873 RunUntilIdle(); | 878 RunUntilIdle(); |
874 EXPECT_TRUE(request(i)->handle()->is_initialized()); | 879 EXPECT_TRUE(request(i)->handle()->is_initialized()); |
875 EXPECT_TRUE(request(i)->handle()->socket()); | 880 EXPECT_TRUE(request(i)->handle()->socket()); |
876 WebSocketTransportClientSocketPool::UnlockEndpoint(request(i)->handle()); | 881 WebSocketTransportClientSocketPool::UnlockEndpoint(request(i)->handle()); |
877 } | 882 } |
878 // Now there are 32 sockets connected, and one stalled. | 883 // Now there are 32 sockets connected, and one stalled. |
879 RunUntilIdle(); | 884 RunUntilIdle(); |
880 EXPECT_FALSE(request(kMaxSockets)->handle()->is_initialized()); | 885 EXPECT_FALSE(request(kMaxSockets)->handle()->is_initialized()); |
881 EXPECT_FALSE(request(kMaxSockets)->handle()->socket()); | 886 EXPECT_FALSE(request(kMaxSockets)->handle()->socket()); |
882 } | 887 } |
883 | 888 |
884 TEST_F(WebSocketTransportClientSocketPoolTest, StalledSocketReleased) { | 889 TEST_F(WebSocketTransportClientSocketPoolTest, StalledSocketReleased) { |
885 host_resolver_->set_synchronous_mode(true); | 890 host_resolver_->set_synchronous_mode(true); |
886 for (int i = 0; i < kMaxSockets; ++i) { | 891 for (int i = 0; i < kMaxSockets; ++i) { |
887 ASSERT_EQ(OK, StartRequest("a", kDefaultPriority)); | 892 ASSERT_THAT(StartRequest("a", kDefaultPriority), IsOk()); |
888 WebSocketTransportClientSocketPool::UnlockEndpoint(request(i)->handle()); | 893 WebSocketTransportClientSocketPool::UnlockEndpoint(request(i)->handle()); |
889 RunUntilIdle(); | 894 RunUntilIdle(); |
890 } | 895 } |
891 | 896 |
892 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); | 897 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsError(ERR_IO_PENDING)); |
893 ReleaseOneConnection(ClientSocketPoolTest::NO_KEEP_ALIVE); | 898 ReleaseOneConnection(ClientSocketPoolTest::NO_KEEP_ALIVE); |
894 EXPECT_TRUE(request(kMaxSockets)->handle()->is_initialized()); | 899 EXPECT_TRUE(request(kMaxSockets)->handle()->is_initialized()); |
895 EXPECT_TRUE(request(kMaxSockets)->handle()->socket()); | 900 EXPECT_TRUE(request(kMaxSockets)->handle()->socket()); |
896 } | 901 } |
897 | 902 |
898 TEST_F(WebSocketTransportClientSocketPoolTest, IsStalledTrueWhenStalled) { | 903 TEST_F(WebSocketTransportClientSocketPoolTest, IsStalledTrueWhenStalled) { |
899 for (int i = 0; i < kMaxSockets + 1; ++i) { | 904 for (int i = 0; i < kMaxSockets + 1; ++i) { |
900 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); | 905 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsError(ERR_IO_PENDING)); |
901 } | 906 } |
902 EXPECT_EQ(OK, request(0)->WaitForResult()); | 907 EXPECT_THAT(request(0)->WaitForResult(), IsOk()); |
903 EXPECT_TRUE(pool_.IsStalled()); | 908 EXPECT_TRUE(pool_.IsStalled()); |
904 } | 909 } |
905 | 910 |
906 TEST_F(WebSocketTransportClientSocketPoolTest, | 911 TEST_F(WebSocketTransportClientSocketPoolTest, |
907 CancellingPendingSocketUnstallsStalledSocket) { | 912 CancellingPendingSocketUnstallsStalledSocket) { |
908 for (int i = 0; i < kMaxSockets + 1; ++i) { | 913 for (int i = 0; i < kMaxSockets + 1; ++i) { |
909 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); | 914 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsError(ERR_IO_PENDING)); |
910 } | 915 } |
911 EXPECT_EQ(OK, request(0)->WaitForResult()); | 916 EXPECT_THAT(request(0)->WaitForResult(), IsOk()); |
912 request(1)->handle()->Reset(); | 917 request(1)->handle()->Reset(); |
913 RunUntilIdle(); | 918 RunUntilIdle(); |
914 EXPECT_FALSE(pool_.IsStalled()); | 919 EXPECT_FALSE(pool_.IsStalled()); |
915 } | 920 } |
916 | 921 |
917 TEST_F(WebSocketTransportClientSocketPoolTest, | 922 TEST_F(WebSocketTransportClientSocketPoolTest, |
918 LoadStateOfStalledSocketIsWaitingForAvailableSocket) { | 923 LoadStateOfStalledSocketIsWaitingForAvailableSocket) { |
919 for (int i = 0; i < kMaxSockets + 1; ++i) { | 924 for (int i = 0; i < kMaxSockets + 1; ++i) { |
920 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); | 925 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsError(ERR_IO_PENDING)); |
921 } | 926 } |
922 EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET, | 927 EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET, |
923 pool_.GetLoadState("a", request(kMaxSockets)->handle())); | 928 pool_.GetLoadState("a", request(kMaxSockets)->handle())); |
924 } | 929 } |
925 | 930 |
926 TEST_F(WebSocketTransportClientSocketPoolTest, | 931 TEST_F(WebSocketTransportClientSocketPoolTest, |
927 CancellingStalledSocketUnstallsPool) { | 932 CancellingStalledSocketUnstallsPool) { |
928 for (int i = 0; i < kMaxSockets + 1; ++i) { | 933 for (int i = 0; i < kMaxSockets + 1; ++i) { |
929 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); | 934 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsError(ERR_IO_PENDING)); |
930 } | 935 } |
931 request(kMaxSockets)->handle()->Reset(); | 936 request(kMaxSockets)->handle()->Reset(); |
932 RunUntilIdle(); | 937 RunUntilIdle(); |
933 EXPECT_FALSE(pool_.IsStalled()); | 938 EXPECT_FALSE(pool_.IsStalled()); |
934 } | 939 } |
935 | 940 |
936 TEST_F(WebSocketTransportClientSocketPoolTest, | 941 TEST_F(WebSocketTransportClientSocketPoolTest, |
937 FlushWithErrorFlushesPendingConnections) { | 942 FlushWithErrorFlushesPendingConnections) { |
938 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); | 943 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsError(ERR_IO_PENDING)); |
939 pool_.FlushWithError(ERR_FAILED); | 944 pool_.FlushWithError(ERR_FAILED); |
940 EXPECT_EQ(ERR_FAILED, request(0)->WaitForResult()); | 945 EXPECT_THAT(request(0)->WaitForResult(), IsError(ERR_FAILED)); |
941 } | 946 } |
942 | 947 |
943 TEST_F(WebSocketTransportClientSocketPoolTest, | 948 TEST_F(WebSocketTransportClientSocketPoolTest, |
944 FlushWithErrorFlushesStalledConnections) { | 949 FlushWithErrorFlushesStalledConnections) { |
945 for (int i = 0; i < kMaxSockets + 1; ++i) { | 950 for (int i = 0; i < kMaxSockets + 1; ++i) { |
946 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); | 951 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsError(ERR_IO_PENDING)); |
947 } | 952 } |
948 pool_.FlushWithError(ERR_FAILED); | 953 pool_.FlushWithError(ERR_FAILED); |
949 EXPECT_EQ(ERR_FAILED, request(kMaxSockets)->WaitForResult()); | 954 EXPECT_THAT(request(kMaxSockets)->WaitForResult(), IsError(ERR_FAILED)); |
950 } | 955 } |
951 | 956 |
952 TEST_F(WebSocketTransportClientSocketPoolTest, | 957 TEST_F(WebSocketTransportClientSocketPoolTest, |
953 AfterFlushWithErrorCanMakeNewConnections) { | 958 AfterFlushWithErrorCanMakeNewConnections) { |
954 for (int i = 0; i < kMaxSockets + 1; ++i) { | 959 for (int i = 0; i < kMaxSockets + 1; ++i) { |
955 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); | 960 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsError(ERR_IO_PENDING)); |
956 } | 961 } |
957 pool_.FlushWithError(ERR_FAILED); | 962 pool_.FlushWithError(ERR_FAILED); |
958 host_resolver_->set_synchronous_mode(true); | 963 host_resolver_->set_synchronous_mode(true); |
959 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); | 964 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsOk()); |
960 } | 965 } |
961 | 966 |
962 // Deleting pending connections can release the lock on the endpoint, which can | 967 // Deleting pending connections can release the lock on the endpoint, which can |
963 // in principle lead to other pending connections succeeding. However, when we | 968 // in principle lead to other pending connections succeeding. However, when we |
964 // call FlushWithError(), everything should fail. | 969 // call FlushWithError(), everything should fail. |
965 TEST_F(WebSocketTransportClientSocketPoolTest, | 970 TEST_F(WebSocketTransportClientSocketPoolTest, |
966 FlushWithErrorDoesNotCauseSuccessfulConnections) { | 971 FlushWithErrorDoesNotCauseSuccessfulConnections) { |
967 host_resolver_->set_synchronous_mode(true); | 972 host_resolver_->set_synchronous_mode(true); |
968 MockTransportClientSocketFactory::ClientSocketType first_type[] = { | 973 MockTransportClientSocketFactory::ClientSocketType first_type[] = { |
969 // First socket | 974 // First socket |
970 MockTransportClientSocketFactory::MOCK_PENDING_CLIENT_SOCKET | 975 MockTransportClientSocketFactory::MOCK_PENDING_CLIENT_SOCKET |
971 }; | 976 }; |
972 client_socket_factory_.set_client_socket_types(first_type, | 977 client_socket_factory_.set_client_socket_types(first_type, |
973 arraysize(first_type)); | 978 arraysize(first_type)); |
974 // The rest of the sockets will connect synchronously. | 979 // The rest of the sockets will connect synchronously. |
975 client_socket_factory_.set_default_client_socket_type( | 980 client_socket_factory_.set_default_client_socket_type( |
976 MockTransportClientSocketFactory::MOCK_CLIENT_SOCKET); | 981 MockTransportClientSocketFactory::MOCK_CLIENT_SOCKET); |
977 for (int i = 0; i < kMaxSockets; ++i) { | 982 for (int i = 0; i < kMaxSockets; ++i) { |
978 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); | 983 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsError(ERR_IO_PENDING)); |
979 } | 984 } |
980 // Now we have one socket in STATE_TRANSPORT_CONNECT and the rest in | 985 // Now we have one socket in STATE_TRANSPORT_CONNECT and the rest in |
981 // STATE_OBTAIN_LOCK. If any of the sockets in STATE_OBTAIN_LOCK is given the | 986 // STATE_OBTAIN_LOCK. If any of the sockets in STATE_OBTAIN_LOCK is given the |
982 // lock, they will synchronously connect. | 987 // lock, they will synchronously connect. |
983 pool_.FlushWithError(ERR_FAILED); | 988 pool_.FlushWithError(ERR_FAILED); |
984 for (int i = 0; i < kMaxSockets; ++i) { | 989 for (int i = 0; i < kMaxSockets; ++i) { |
985 EXPECT_EQ(ERR_FAILED, request(i)->WaitForResult()); | 990 EXPECT_THAT(request(i)->WaitForResult(), IsError(ERR_FAILED)); |
986 } | 991 } |
987 } | 992 } |
988 | 993 |
989 // This is a regression test for the first attempted fix for | 994 // This is a regression test for the first attempted fix for |
990 // FlushWithErrorDoesNotCauseSuccessfulConnections. Because a ConnectJob can | 995 // FlushWithErrorDoesNotCauseSuccessfulConnections. Because a ConnectJob can |
991 // have both IPv4 and IPv6 subjobs, it can be both connecting and waiting for | 996 // have both IPv4 and IPv6 subjobs, it can be both connecting and waiting for |
992 // the lock at the same time. | 997 // the lock at the same time. |
993 TEST_F(WebSocketTransportClientSocketPoolTest, | 998 TEST_F(WebSocketTransportClientSocketPoolTest, |
994 FlushWithErrorDoesNotCauseSuccessfulConnectionsMultipleAddressTypes) { | 999 FlushWithErrorDoesNotCauseSuccessfulConnectionsMultipleAddressTypes) { |
995 host_resolver_->set_synchronous_mode(true); | 1000 host_resolver_->set_synchronous_mode(true); |
(...skipping 11 matching lines...) Expand all Loading... |
1007 host_resolver_->rules()->ClearRules(); | 1012 host_resolver_->rules()->ClearRules(); |
1008 // Each connect job has a different IPv6 address but the same IPv4 address. | 1013 // Each connect job has a different IPv6 address but the same IPv4 address. |
1009 // So the IPv6 connections happen in parallel but the IPv4 ones are | 1014 // So the IPv6 connections happen in parallel but the IPv4 ones are |
1010 // serialised. | 1015 // serialised. |
1011 host_resolver_->rules()->AddIPLiteralRule("*", | 1016 host_resolver_->rules()->AddIPLiteralRule("*", |
1012 base::StringPrintf( | 1017 base::StringPrintf( |
1013 "%x:abcd::3:4:ff," | 1018 "%x:abcd::3:4:ff," |
1014 "1.1.1.1", | 1019 "1.1.1.1", |
1015 i + 1), | 1020 i + 1), |
1016 std::string()); | 1021 std::string()); |
1017 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); | 1022 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsError(ERR_IO_PENDING)); |
1018 } | 1023 } |
1019 // Now we have |kMaxSockets| IPv6 sockets stalled in connect. No IPv4 sockets | 1024 // Now we have |kMaxSockets| IPv6 sockets stalled in connect. No IPv4 sockets |
1020 // are started yet. | 1025 // are started yet. |
1021 RunLoopForTimePeriod(base::TimeDelta::FromMilliseconds( | 1026 RunLoopForTimePeriod(base::TimeDelta::FromMilliseconds( |
1022 TransportConnectJob::kIPv6FallbackTimerInMs)); | 1027 TransportConnectJob::kIPv6FallbackTimerInMs)); |
1023 // Now we have |kMaxSockets| IPv6 sockets and one IPv4 socket stalled in | 1028 // Now we have |kMaxSockets| IPv6 sockets and one IPv4 socket stalled in |
1024 // connect, and |kMaxSockets - 1| IPv4 sockets waiting for the endpoint lock. | 1029 // connect, and |kMaxSockets - 1| IPv4 sockets waiting for the endpoint lock. |
1025 pool_.FlushWithError(ERR_FAILED); | 1030 pool_.FlushWithError(ERR_FAILED); |
1026 for (int i = 0; i < kMaxSockets; ++i) { | 1031 for (int i = 0; i < kMaxSockets; ++i) { |
1027 EXPECT_EQ(ERR_FAILED, request(i)->WaitForResult()); | 1032 EXPECT_THAT(request(i)->WaitForResult(), IsError(ERR_FAILED)); |
1028 } | 1033 } |
1029 } | 1034 } |
1030 | 1035 |
1031 // Sockets that have had ownership transferred to a ClientSocketHandle should | 1036 // Sockets that have had ownership transferred to a ClientSocketHandle should |
1032 // not be affected by FlushWithError. | 1037 // not be affected by FlushWithError. |
1033 TEST_F(WebSocketTransportClientSocketPoolTest, | 1038 TEST_F(WebSocketTransportClientSocketPoolTest, |
1034 FlushWithErrorDoesNotAffectHandedOutSockets) { | 1039 FlushWithErrorDoesNotAffectHandedOutSockets) { |
1035 host_resolver_->set_synchronous_mode(true); | 1040 host_resolver_->set_synchronous_mode(true); |
1036 MockTransportClientSocketFactory::ClientSocketType socket_types[] = { | 1041 MockTransportClientSocketFactory::ClientSocketType socket_types[] = { |
1037 MockTransportClientSocketFactory::MOCK_CLIENT_SOCKET, | 1042 MockTransportClientSocketFactory::MOCK_CLIENT_SOCKET, |
1038 MockTransportClientSocketFactory::MOCK_STALLED_CLIENT_SOCKET}; | 1043 MockTransportClientSocketFactory::MOCK_STALLED_CLIENT_SOCKET}; |
1039 client_socket_factory_.set_client_socket_types(socket_types, | 1044 client_socket_factory_.set_client_socket_types(socket_types, |
1040 arraysize(socket_types)); | 1045 arraysize(socket_types)); |
1041 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); | 1046 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsOk()); |
1042 // Socket has been "handed out". | 1047 // Socket has been "handed out". |
1043 EXPECT_TRUE(request(0)->handle()->socket()); | 1048 EXPECT_TRUE(request(0)->handle()->socket()); |
1044 | 1049 |
1045 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); | 1050 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsError(ERR_IO_PENDING)); |
1046 // Now we have one socket handed out, and one pending. | 1051 // Now we have one socket handed out, and one pending. |
1047 pool_.FlushWithError(ERR_FAILED); | 1052 pool_.FlushWithError(ERR_FAILED); |
1048 EXPECT_EQ(ERR_FAILED, request(1)->WaitForResult()); | 1053 EXPECT_THAT(request(1)->WaitForResult(), IsError(ERR_FAILED)); |
1049 // Socket owned by ClientSocketHandle is unaffected: | 1054 // Socket owned by ClientSocketHandle is unaffected: |
1050 EXPECT_TRUE(request(0)->handle()->socket()); | 1055 EXPECT_TRUE(request(0)->handle()->socket()); |
1051 // Return it to the pool (which deletes it). | 1056 // Return it to the pool (which deletes it). |
1052 request(0)->handle()->Reset(); | 1057 request(0)->handle()->Reset(); |
1053 } | 1058 } |
1054 | 1059 |
1055 // Sockets should not be leaked if CancelRequest() is called in between | 1060 // Sockets should not be leaked if CancelRequest() is called in between |
1056 // SetSocket() being called on the ClientSocketHandle and InvokeUserCallback(). | 1061 // SetSocket() being called on the ClientSocketHandle and InvokeUserCallback(). |
1057 TEST_F(WebSocketTransportClientSocketPoolTest, CancelRequestReclaimsSockets) { | 1062 TEST_F(WebSocketTransportClientSocketPoolTest, CancelRequestReclaimsSockets) { |
1058 host_resolver_->set_synchronous_mode(true); | 1063 host_resolver_->set_synchronous_mode(true); |
1059 MockTransportClientSocketFactory::ClientSocketType socket_types[] = { | 1064 MockTransportClientSocketFactory::ClientSocketType socket_types[] = { |
1060 MockTransportClientSocketFactory::MOCK_TRIGGERABLE_CLIENT_SOCKET, | 1065 MockTransportClientSocketFactory::MOCK_TRIGGERABLE_CLIENT_SOCKET, |
1061 MockTransportClientSocketFactory::MOCK_CLIENT_SOCKET}; | 1066 MockTransportClientSocketFactory::MOCK_CLIENT_SOCKET}; |
1062 | 1067 |
1063 client_socket_factory_.set_client_socket_types(socket_types, | 1068 client_socket_factory_.set_client_socket_types(socket_types, |
1064 arraysize(socket_types)); | 1069 arraysize(socket_types)); |
1065 | 1070 |
1066 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); | 1071 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsError(ERR_IO_PENDING)); |
1067 | 1072 |
1068 base::Closure connect_trigger = | 1073 base::Closure connect_trigger = |
1069 client_socket_factory_.WaitForTriggerableSocketCreation(); | 1074 client_socket_factory_.WaitForTriggerableSocketCreation(); |
1070 | 1075 |
1071 connect_trigger.Run(); // Calls InvokeUserCallbackLater() | 1076 connect_trigger.Run(); // Calls InvokeUserCallbackLater() |
1072 | 1077 |
1073 request(0)->handle()->Reset(); // calls CancelRequest() | 1078 request(0)->handle()->Reset(); // calls CancelRequest() |
1074 | 1079 |
1075 RunUntilIdle(); | 1080 RunUntilIdle(); |
1076 // We should now be able to create a new connection without blocking on the | 1081 // We should now be able to create a new connection without blocking on the |
1077 // endpoint lock. | 1082 // endpoint lock. |
1078 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); | 1083 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsOk()); |
1079 } | 1084 } |
1080 | 1085 |
1081 // A handshake completing and then the WebSocket closing should only release one | 1086 // A handshake completing and then the WebSocket closing should only release one |
1082 // Endpoint, not two. | 1087 // Endpoint, not two. |
1083 TEST_F(WebSocketTransportClientSocketPoolTest, EndpointLockIsOnlyReleasedOnce) { | 1088 TEST_F(WebSocketTransportClientSocketPoolTest, EndpointLockIsOnlyReleasedOnce) { |
1084 host_resolver_->set_synchronous_mode(true); | 1089 host_resolver_->set_synchronous_mode(true); |
1085 ASSERT_EQ(OK, StartRequest("a", kDefaultPriority)); | 1090 ASSERT_THAT(StartRequest("a", kDefaultPriority), IsOk()); |
1086 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); | 1091 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsError(ERR_IO_PENDING)); |
1087 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); | 1092 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsError(ERR_IO_PENDING)); |
1088 // First socket completes handshake. | 1093 // First socket completes handshake. |
1089 WebSocketTransportClientSocketPool::UnlockEndpoint(request(0)->handle()); | 1094 WebSocketTransportClientSocketPool::UnlockEndpoint(request(0)->handle()); |
1090 RunUntilIdle(); | 1095 RunUntilIdle(); |
1091 // First socket is closed. | 1096 // First socket is closed. |
1092 request(0)->handle()->Reset(); | 1097 request(0)->handle()->Reset(); |
1093 // Second socket should have been released. | 1098 // Second socket should have been released. |
1094 EXPECT_EQ(OK, request(1)->WaitForResult()); | 1099 EXPECT_THAT(request(1)->WaitForResult(), IsOk()); |
1095 // Third socket should still be waiting for endpoint. | 1100 // Third socket should still be waiting for endpoint. |
1096 ASSERT_FALSE(request(2)->handle()->is_initialized()); | 1101 ASSERT_FALSE(request(2)->handle()->is_initialized()); |
1097 EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET, | 1102 EXPECT_EQ(LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET, |
1098 request(2)->handle()->GetLoadState()); | 1103 request(2)->handle()->GetLoadState()); |
1099 } | 1104 } |
1100 | 1105 |
1101 } // namespace | 1106 } // namespace |
1102 | 1107 |
1103 } // namespace net | 1108 } // namespace net |
OLD | NEW |