| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/callback.h" | 9 #include "base/callback.h" |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
| 12 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
| 13 #include "base/threading/platform_thread.h" | 13 #include "base/threading/platform_thread.h" |
| 14 #include "net/base/ip_address.h" | 14 #include "net/base/ip_address.h" |
| 15 #include "net/base/ip_endpoint.h" | 15 #include "net/base/ip_endpoint.h" |
| 16 #include "net/base/load_timing_info.h" | 16 #include "net/base/load_timing_info.h" |
| 17 #include "net/base/load_timing_info_test_util.h" | 17 #include "net/base/load_timing_info_test_util.h" |
| 18 #include "net/base/net_errors.h" | 18 #include "net/base/net_errors.h" |
| 19 #include "net/base/test_completion_callback.h" | 19 #include "net/base/test_completion_callback.h" |
| 20 #include "net/dns/mock_host_resolver.h" | 20 #include "net/dns/mock_host_resolver.h" |
| 21 #include "net/log/test_net_log.h" | 21 #include "net/log/test_net_log.h" |
| 22 #include "net/socket/client_socket_handle.h" | 22 #include "net/socket/client_socket_handle.h" |
| 23 #include "net/socket/socket_test_util.h" | 23 #include "net/socket/socket_test_util.h" |
| 24 #include "net/socket/stream_socket.h" | 24 #include "net/socket/stream_socket.h" |
| 25 #include "net/socket/transport_client_socket_pool_test_util.h" | 25 #include "net/socket/transport_client_socket_pool_test_util.h" |
| 26 #include "net/test/gtest_util.h" |
| 27 #include "testing/gmock/include/gmock/gmock.h" |
| 26 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
| 27 | 29 |
| 30 using net::test::IsError; |
| 31 using net::test::IsOk; |
| 32 |
| 28 namespace net { | 33 namespace net { |
| 29 | 34 |
| 30 using internal::ClientSocketPoolBaseHelper; | 35 using internal::ClientSocketPoolBaseHelper; |
| 31 | 36 |
| 32 namespace { | 37 namespace { |
| 33 | 38 |
| 34 const int kMaxSockets = 32; | 39 const int kMaxSockets = 32; |
| 35 const int kMaxSocketsPerGroup = 6; | 40 const int kMaxSocketsPerGroup = 6; |
| 36 const RequestPriority kDefaultPriority = LOW; | 41 const RequestPriority kDefaultPriority = LOW; |
| 37 | 42 |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 EXPECT_EQ(ADDRESS_FAMILY_IPV6, addrlist[2].GetFamily()); | 176 EXPECT_EQ(ADDRESS_FAMILY_IPV6, addrlist[2].GetFamily()); |
| 172 EXPECT_EQ(ADDRESS_FAMILY_IPV6, addrlist[3].GetFamily()); | 177 EXPECT_EQ(ADDRESS_FAMILY_IPV6, addrlist[3].GetFamily()); |
| 173 } | 178 } |
| 174 | 179 |
| 175 TEST_F(TransportClientSocketPoolTest, Basic) { | 180 TEST_F(TransportClientSocketPoolTest, Basic) { |
| 176 TestCompletionCallback callback; | 181 TestCompletionCallback callback; |
| 177 ClientSocketHandle handle; | 182 ClientSocketHandle handle; |
| 178 int rv = | 183 int rv = |
| 179 handle.Init("a", params_, LOW, ClientSocketPool::RespectLimits::ENABLED, | 184 handle.Init("a", params_, LOW, ClientSocketPool::RespectLimits::ENABLED, |
| 180 callback.callback(), &pool_, BoundNetLog()); | 185 callback.callback(), &pool_, BoundNetLog()); |
| 181 EXPECT_EQ(ERR_IO_PENDING, rv); | 186 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 182 EXPECT_FALSE(handle.is_initialized()); | 187 EXPECT_FALSE(handle.is_initialized()); |
| 183 EXPECT_FALSE(handle.socket()); | 188 EXPECT_FALSE(handle.socket()); |
| 184 | 189 |
| 185 EXPECT_EQ(OK, callback.WaitForResult()); | 190 EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 186 EXPECT_TRUE(handle.is_initialized()); | 191 EXPECT_TRUE(handle.is_initialized()); |
| 187 EXPECT_TRUE(handle.socket()); | 192 EXPECT_TRUE(handle.socket()); |
| 188 TestLoadTimingInfoConnectedNotReused(handle); | 193 TestLoadTimingInfoConnectedNotReused(handle); |
| 189 EXPECT_EQ(0u, handle.connection_attempts().size()); | 194 EXPECT_EQ(0u, handle.connection_attempts().size()); |
| 190 } | 195 } |
| 191 | 196 |
| 192 // Make sure that TransportConnectJob passes on its priority to its | 197 // Make sure that TransportConnectJob passes on its priority to its |
| 193 // HostResolver request on Init. | 198 // HostResolver request on Init. |
| 194 TEST_F(TransportClientSocketPoolTest, SetResolvePriorityOnInit) { | 199 TEST_F(TransportClientSocketPoolTest, SetResolvePriorityOnInit) { |
| 195 for (int i = MINIMUM_PRIORITY; i <= MAXIMUM_PRIORITY; ++i) { | 200 for (int i = MINIMUM_PRIORITY; i <= MAXIMUM_PRIORITY; ++i) { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 209 TestCompletionCallback callback; | 214 TestCompletionCallback callback; |
| 210 ClientSocketHandle handle; | 215 ClientSocketHandle handle; |
| 211 HostPortPair host_port_pair("unresolvable.host.name", 80); | 216 HostPortPair host_port_pair("unresolvable.host.name", 80); |
| 212 scoped_refptr<TransportSocketParams> dest(new TransportSocketParams( | 217 scoped_refptr<TransportSocketParams> dest(new TransportSocketParams( |
| 213 host_port_pair, false, OnHostResolutionCallback(), | 218 host_port_pair, false, OnHostResolutionCallback(), |
| 214 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DEFAULT)); | 219 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DEFAULT)); |
| 215 EXPECT_EQ(ERR_IO_PENDING, | 220 EXPECT_EQ(ERR_IO_PENDING, |
| 216 handle.Init("a", dest, kDefaultPriority, | 221 handle.Init("a", dest, kDefaultPriority, |
| 217 ClientSocketPool::RespectLimits::ENABLED, | 222 ClientSocketPool::RespectLimits::ENABLED, |
| 218 callback.callback(), &pool_, BoundNetLog())); | 223 callback.callback(), &pool_, BoundNetLog())); |
| 219 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, callback.WaitForResult()); | 224 EXPECT_THAT(callback.WaitForResult(), IsError(ERR_NAME_NOT_RESOLVED)); |
| 220 ASSERT_EQ(1u, handle.connection_attempts().size()); | 225 ASSERT_EQ(1u, handle.connection_attempts().size()); |
| 221 EXPECT_TRUE(handle.connection_attempts()[0].endpoint.address().empty()); | 226 EXPECT_TRUE(handle.connection_attempts()[0].endpoint.address().empty()); |
| 222 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, handle.connection_attempts()[0].result); | 227 EXPECT_THAT(handle.connection_attempts()[0].result, |
| 228 IsError(ERR_NAME_NOT_RESOLVED)); |
| 223 } | 229 } |
| 224 | 230 |
| 225 TEST_F(TransportClientSocketPoolTest, InitConnectionFailure) { | 231 TEST_F(TransportClientSocketPoolTest, InitConnectionFailure) { |
| 226 client_socket_factory_.set_default_client_socket_type( | 232 client_socket_factory_.set_default_client_socket_type( |
| 227 MockTransportClientSocketFactory::MOCK_FAILING_CLIENT_SOCKET); | 233 MockTransportClientSocketFactory::MOCK_FAILING_CLIENT_SOCKET); |
| 228 TestCompletionCallback callback; | 234 TestCompletionCallback callback; |
| 229 ClientSocketHandle handle; | 235 ClientSocketHandle handle; |
| 230 EXPECT_EQ(ERR_IO_PENDING, | 236 EXPECT_EQ(ERR_IO_PENDING, |
| 231 handle.Init("a", params_, kDefaultPriority, | 237 handle.Init("a", params_, kDefaultPriority, |
| 232 ClientSocketPool::RespectLimits::ENABLED, | 238 ClientSocketPool::RespectLimits::ENABLED, |
| 233 callback.callback(), &pool_, BoundNetLog())); | 239 callback.callback(), &pool_, BoundNetLog())); |
| 234 EXPECT_EQ(ERR_CONNECTION_FAILED, callback.WaitForResult()); | 240 EXPECT_THAT(callback.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 235 ASSERT_EQ(1u, handle.connection_attempts().size()); | 241 ASSERT_EQ(1u, handle.connection_attempts().size()); |
| 236 EXPECT_EQ("127.0.0.1:80", | 242 EXPECT_EQ("127.0.0.1:80", |
| 237 handle.connection_attempts()[0].endpoint.ToString()); | 243 handle.connection_attempts()[0].endpoint.ToString()); |
| 238 EXPECT_EQ(ERR_CONNECTION_FAILED, handle.connection_attempts()[0].result); | 244 EXPECT_THAT(handle.connection_attempts()[0].result, |
| 245 IsError(ERR_CONNECTION_FAILED)); |
| 239 | 246 |
| 240 // Make the host resolutions complete synchronously this time. | 247 // Make the host resolutions complete synchronously this time. |
| 241 host_resolver_->set_synchronous_mode(true); | 248 host_resolver_->set_synchronous_mode(true); |
| 242 EXPECT_EQ(ERR_CONNECTION_FAILED, | 249 EXPECT_EQ(ERR_CONNECTION_FAILED, |
| 243 handle.Init("a", params_, kDefaultPriority, | 250 handle.Init("a", params_, kDefaultPriority, |
| 244 ClientSocketPool::RespectLimits::ENABLED, | 251 ClientSocketPool::RespectLimits::ENABLED, |
| 245 callback.callback(), &pool_, BoundNetLog())); | 252 callback.callback(), &pool_, BoundNetLog())); |
| 246 ASSERT_EQ(1u, handle.connection_attempts().size()); | 253 ASSERT_EQ(1u, handle.connection_attempts().size()); |
| 247 EXPECT_EQ("127.0.0.1:80", | 254 EXPECT_EQ("127.0.0.1:80", |
| 248 handle.connection_attempts()[0].endpoint.ToString()); | 255 handle.connection_attempts()[0].endpoint.ToString()); |
| 249 EXPECT_EQ(ERR_CONNECTION_FAILED, handle.connection_attempts()[0].result); | 256 EXPECT_THAT(handle.connection_attempts()[0].result, |
| 257 IsError(ERR_CONNECTION_FAILED)); |
| 250 } | 258 } |
| 251 | 259 |
| 252 TEST_F(TransportClientSocketPoolTest, PendingRequests) { | 260 TEST_F(TransportClientSocketPoolTest, PendingRequests) { |
| 253 // First request finishes asynchronously. | 261 // First request finishes asynchronously. |
| 254 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); | 262 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsError(ERR_IO_PENDING)); |
| 255 EXPECT_EQ(OK, (*requests())[0]->WaitForResult()); | 263 EXPECT_THAT((*requests())[0]->WaitForResult(), IsOk()); |
| 256 | 264 |
| 257 // Make all subsequent host resolutions complete synchronously. | 265 // Make all subsequent host resolutions complete synchronously. |
| 258 host_resolver_->set_synchronous_mode(true); | 266 host_resolver_->set_synchronous_mode(true); |
| 259 | 267 |
| 260 // Rest of them finish synchronously, until we reach the per-group limit. | 268 // Rest of them finish synchronously, until we reach the per-group limit. |
| 261 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); | 269 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsOk()); |
| 262 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); | 270 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsOk()); |
| 263 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); | 271 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsOk()); |
| 264 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); | 272 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsOk()); |
| 265 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); | 273 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsOk()); |
| 266 | 274 |
| 267 // The rest are pending since we've used all active sockets. | 275 // The rest are pending since we've used all active sockets. |
| 268 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", HIGHEST)); | 276 EXPECT_THAT(StartRequest("a", HIGHEST), IsError(ERR_IO_PENDING)); |
| 269 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOWEST)); | 277 EXPECT_THAT(StartRequest("a", LOWEST), IsError(ERR_IO_PENDING)); |
| 270 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOWEST)); | 278 EXPECT_THAT(StartRequest("a", LOWEST), IsError(ERR_IO_PENDING)); |
| 271 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", MEDIUM)); | 279 EXPECT_THAT(StartRequest("a", MEDIUM), IsError(ERR_IO_PENDING)); |
| 272 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOW)); | 280 EXPECT_THAT(StartRequest("a", LOW), IsError(ERR_IO_PENDING)); |
| 273 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", HIGHEST)); | 281 EXPECT_THAT(StartRequest("a", HIGHEST), IsError(ERR_IO_PENDING)); |
| 274 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOWEST)); | 282 EXPECT_THAT(StartRequest("a", LOWEST), IsError(ERR_IO_PENDING)); |
| 275 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", MEDIUM)); | 283 EXPECT_THAT(StartRequest("a", MEDIUM), IsError(ERR_IO_PENDING)); |
| 276 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", MEDIUM)); | 284 EXPECT_THAT(StartRequest("a", MEDIUM), IsError(ERR_IO_PENDING)); |
| 277 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", HIGHEST)); | 285 EXPECT_THAT(StartRequest("a", HIGHEST), IsError(ERR_IO_PENDING)); |
| 278 | 286 |
| 279 ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE); | 287 ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE); |
| 280 | 288 |
| 281 EXPECT_EQ(kMaxSocketsPerGroup, client_socket_factory_.allocation_count()); | 289 EXPECT_EQ(kMaxSocketsPerGroup, client_socket_factory_.allocation_count()); |
| 282 | 290 |
| 283 // One initial asynchronous request and then 10 pending requests. | 291 // One initial asynchronous request and then 10 pending requests. |
| 284 EXPECT_EQ(11U, completion_count()); | 292 EXPECT_EQ(11U, completion_count()); |
| 285 | 293 |
| 286 // First part of requests, all with the same priority, finishes in FIFO order. | 294 // First part of requests, all with the same priority, finishes in FIFO order. |
| 287 EXPECT_EQ(1, GetOrderOfRequest(1)); | 295 EXPECT_EQ(1, GetOrderOfRequest(1)); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 302 EXPECT_EQ(11, GetOrderOfRequest(14)); | 310 EXPECT_EQ(11, GetOrderOfRequest(14)); |
| 303 EXPECT_EQ(12, GetOrderOfRequest(15)); | 311 EXPECT_EQ(12, GetOrderOfRequest(15)); |
| 304 EXPECT_EQ(9, GetOrderOfRequest(16)); | 312 EXPECT_EQ(9, GetOrderOfRequest(16)); |
| 305 | 313 |
| 306 // Make sure we test order of all requests made. | 314 // Make sure we test order of all requests made. |
| 307 EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(17)); | 315 EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(17)); |
| 308 } | 316 } |
| 309 | 317 |
| 310 TEST_F(TransportClientSocketPoolTest, PendingRequests_NoKeepAlive) { | 318 TEST_F(TransportClientSocketPoolTest, PendingRequests_NoKeepAlive) { |
| 311 // First request finishes asynchronously. | 319 // First request finishes asynchronously. |
| 312 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); | 320 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsError(ERR_IO_PENDING)); |
| 313 EXPECT_EQ(OK, (*requests())[0]->WaitForResult()); | 321 EXPECT_THAT((*requests())[0]->WaitForResult(), IsOk()); |
| 314 | 322 |
| 315 // Make all subsequent host resolutions complete synchronously. | 323 // Make all subsequent host resolutions complete synchronously. |
| 316 host_resolver_->set_synchronous_mode(true); | 324 host_resolver_->set_synchronous_mode(true); |
| 317 | 325 |
| 318 // Rest of them finish synchronously, until we reach the per-group limit. | 326 // Rest of them finish synchronously, until we reach the per-group limit. |
| 319 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); | 327 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsOk()); |
| 320 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); | 328 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsOk()); |
| 321 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); | 329 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsOk()); |
| 322 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); | 330 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsOk()); |
| 323 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); | 331 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsOk()); |
| 324 | 332 |
| 325 // The rest are pending since we've used all active sockets. | 333 // The rest are pending since we've used all active sockets. |
| 326 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); | 334 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsError(ERR_IO_PENDING)); |
| 327 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); | 335 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsError(ERR_IO_PENDING)); |
| 328 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); | 336 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsError(ERR_IO_PENDING)); |
| 329 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); | 337 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsError(ERR_IO_PENDING)); |
| 330 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); | 338 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsError(ERR_IO_PENDING)); |
| 331 | 339 |
| 332 ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); | 340 ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 333 | 341 |
| 334 // The pending requests should finish successfully. | 342 // The pending requests should finish successfully. |
| 335 EXPECT_EQ(OK, (*requests())[6]->WaitForResult()); | 343 EXPECT_THAT((*requests())[6]->WaitForResult(), IsOk()); |
| 336 EXPECT_EQ(OK, (*requests())[7]->WaitForResult()); | 344 EXPECT_THAT((*requests())[7]->WaitForResult(), IsOk()); |
| 337 EXPECT_EQ(OK, (*requests())[8]->WaitForResult()); | 345 EXPECT_THAT((*requests())[8]->WaitForResult(), IsOk()); |
| 338 EXPECT_EQ(OK, (*requests())[9]->WaitForResult()); | 346 EXPECT_THAT((*requests())[9]->WaitForResult(), IsOk()); |
| 339 EXPECT_EQ(OK, (*requests())[10]->WaitForResult()); | 347 EXPECT_THAT((*requests())[10]->WaitForResult(), IsOk()); |
| 340 | 348 |
| 341 EXPECT_EQ(static_cast<int>(requests()->size()), | 349 EXPECT_EQ(static_cast<int>(requests()->size()), |
| 342 client_socket_factory_.allocation_count()); | 350 client_socket_factory_.allocation_count()); |
| 343 | 351 |
| 344 // First asynchronous request, and then last 5 pending requests. | 352 // First asynchronous request, and then last 5 pending requests. |
| 345 EXPECT_EQ(6U, completion_count()); | 353 EXPECT_EQ(6U, completion_count()); |
| 346 } | 354 } |
| 347 | 355 |
| 348 // This test will start up a RequestSocket() and then immediately Cancel() it. | 356 // This test will start up a RequestSocket() and then immediately Cancel() it. |
| 349 // The pending host resolution will eventually complete, and destroy the | 357 // The pending host resolution will eventually complete, and destroy the |
| (...skipping 18 matching lines...) Expand all Loading... |
| 368 handle.Init("a", params_, kDefaultPriority, | 376 handle.Init("a", params_, kDefaultPriority, |
| 369 ClientSocketPool::RespectLimits::ENABLED, | 377 ClientSocketPool::RespectLimits::ENABLED, |
| 370 callback.callback(), &pool_, BoundNetLog())); | 378 callback.callback(), &pool_, BoundNetLog())); |
| 371 EXPECT_EQ(ERR_IO_PENDING, | 379 EXPECT_EQ(ERR_IO_PENDING, |
| 372 handle2.Init("a", params_, kDefaultPriority, | 380 handle2.Init("a", params_, kDefaultPriority, |
| 373 ClientSocketPool::RespectLimits::ENABLED, | 381 ClientSocketPool::RespectLimits::ENABLED, |
| 374 callback2.callback(), &pool_, BoundNetLog())); | 382 callback2.callback(), &pool_, BoundNetLog())); |
| 375 | 383 |
| 376 handle.Reset(); | 384 handle.Reset(); |
| 377 | 385 |
| 378 EXPECT_EQ(OK, callback2.WaitForResult()); | 386 EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
| 379 handle2.Reset(); | 387 handle2.Reset(); |
| 380 } | 388 } |
| 381 | 389 |
| 382 TEST_F(TransportClientSocketPoolTest, ConnectCancelConnect) { | 390 TEST_F(TransportClientSocketPoolTest, ConnectCancelConnect) { |
| 383 client_socket_factory_.set_default_client_socket_type( | 391 client_socket_factory_.set_default_client_socket_type( |
| 384 MockTransportClientSocketFactory::MOCK_PENDING_CLIENT_SOCKET); | 392 MockTransportClientSocketFactory::MOCK_PENDING_CLIENT_SOCKET); |
| 385 ClientSocketHandle handle; | 393 ClientSocketHandle handle; |
| 386 TestCompletionCallback callback; | 394 TestCompletionCallback callback; |
| 387 EXPECT_EQ(ERR_IO_PENDING, | 395 EXPECT_EQ(ERR_IO_PENDING, |
| 388 handle.Init("a", params_, kDefaultPriority, | 396 handle.Init("a", params_, kDefaultPriority, |
| (...skipping 11 matching lines...) Expand all Loading... |
| 400 host_resolver_->set_synchronous_mode(true); | 408 host_resolver_->set_synchronous_mode(true); |
| 401 // At this point, handle has two ConnectingSockets out for it. Due to the | 409 // At this point, handle has two ConnectingSockets out for it. Due to the |
| 402 // setting the mock resolver into synchronous mode, the host resolution for | 410 // setting the mock resolver into synchronous mode, the host resolution for |
| 403 // both will return in the same loop of the MessageLoop. The client socket | 411 // both will return in the same loop of the MessageLoop. The client socket |
| 404 // is a pending socket, so the Connect() will asynchronously complete on the | 412 // is a pending socket, so the Connect() will asynchronously complete on the |
| 405 // next loop of the MessageLoop. That means that the first | 413 // next loop of the MessageLoop. That means that the first |
| 406 // ConnectingSocket will enter OnIOComplete, and then the second one will. | 414 // ConnectingSocket will enter OnIOComplete, and then the second one will. |
| 407 // If the first one is not cancelled, it will advance the load state, and | 415 // If the first one is not cancelled, it will advance the load state, and |
| 408 // then the second one will crash. | 416 // then the second one will crash. |
| 409 | 417 |
| 410 EXPECT_EQ(OK, callback2.WaitForResult()); | 418 EXPECT_THAT(callback2.WaitForResult(), IsOk()); |
| 411 EXPECT_FALSE(callback.have_result()); | 419 EXPECT_FALSE(callback.have_result()); |
| 412 | 420 |
| 413 handle.Reset(); | 421 handle.Reset(); |
| 414 } | 422 } |
| 415 | 423 |
| 416 TEST_F(TransportClientSocketPoolTest, CancelRequest) { | 424 TEST_F(TransportClientSocketPoolTest, CancelRequest) { |
| 417 // First request finishes asynchronously. | 425 // First request finishes asynchronously. |
| 418 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); | 426 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsError(ERR_IO_PENDING)); |
| 419 EXPECT_EQ(OK, (*requests())[0]->WaitForResult()); | 427 EXPECT_THAT((*requests())[0]->WaitForResult(), IsOk()); |
| 420 | 428 |
| 421 // Make all subsequent host resolutions complete synchronously. | 429 // Make all subsequent host resolutions complete synchronously. |
| 422 host_resolver_->set_synchronous_mode(true); | 430 host_resolver_->set_synchronous_mode(true); |
| 423 | 431 |
| 424 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); | 432 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsOk()); |
| 425 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); | 433 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsOk()); |
| 426 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); | 434 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsOk()); |
| 427 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); | 435 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsOk()); |
| 428 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); | 436 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsOk()); |
| 429 | 437 |
| 430 // Reached per-group limit, queue up requests. | 438 // Reached per-group limit, queue up requests. |
| 431 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOWEST)); | 439 EXPECT_THAT(StartRequest("a", LOWEST), IsError(ERR_IO_PENDING)); |
| 432 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", HIGHEST)); | 440 EXPECT_THAT(StartRequest("a", HIGHEST), IsError(ERR_IO_PENDING)); |
| 433 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", HIGHEST)); | 441 EXPECT_THAT(StartRequest("a", HIGHEST), IsError(ERR_IO_PENDING)); |
| 434 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", MEDIUM)); | 442 EXPECT_THAT(StartRequest("a", MEDIUM), IsError(ERR_IO_PENDING)); |
| 435 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", MEDIUM)); | 443 EXPECT_THAT(StartRequest("a", MEDIUM), IsError(ERR_IO_PENDING)); |
| 436 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOW)); | 444 EXPECT_THAT(StartRequest("a", LOW), IsError(ERR_IO_PENDING)); |
| 437 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", HIGHEST)); | 445 EXPECT_THAT(StartRequest("a", HIGHEST), IsError(ERR_IO_PENDING)); |
| 438 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOW)); | 446 EXPECT_THAT(StartRequest("a", LOW), IsError(ERR_IO_PENDING)); |
| 439 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOW)); | 447 EXPECT_THAT(StartRequest("a", LOW), IsError(ERR_IO_PENDING)); |
| 440 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOWEST)); | 448 EXPECT_THAT(StartRequest("a", LOWEST), IsError(ERR_IO_PENDING)); |
| 441 | 449 |
| 442 // Cancel a request. | 450 // Cancel a request. |
| 443 size_t index_to_cancel = kMaxSocketsPerGroup + 2; | 451 size_t index_to_cancel = kMaxSocketsPerGroup + 2; |
| 444 EXPECT_FALSE((*requests())[index_to_cancel]->handle()->is_initialized()); | 452 EXPECT_FALSE((*requests())[index_to_cancel]->handle()->is_initialized()); |
| 445 (*requests())[index_to_cancel]->handle()->Reset(); | 453 (*requests())[index_to_cancel]->handle()->Reset(); |
| 446 | 454 |
| 447 ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE); | 455 ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE); |
| 448 | 456 |
| 449 EXPECT_EQ(kMaxSocketsPerGroup, | 457 EXPECT_EQ(kMaxSocketsPerGroup, |
| 450 client_socket_factory_.allocation_count()); | 458 client_socket_factory_.allocation_count()); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 483 base::Unretained(this))) { | 491 base::Unretained(this))) { |
| 484 } | 492 } |
| 485 | 493 |
| 486 ~RequestSocketCallback() override {} | 494 ~RequestSocketCallback() override {} |
| 487 | 495 |
| 488 const CompletionCallback& callback() const { return callback_; } | 496 const CompletionCallback& callback() const { return callback_; } |
| 489 | 497 |
| 490 private: | 498 private: |
| 491 void OnComplete(int result) { | 499 void OnComplete(int result) { |
| 492 SetResult(result); | 500 SetResult(result); |
| 493 ASSERT_EQ(OK, result); | 501 ASSERT_THAT(result, IsOk()); |
| 494 | 502 |
| 495 if (!within_callback_) { | 503 if (!within_callback_) { |
| 496 // Don't allow reuse of the socket. Disconnect it and then release it and | 504 // Don't allow reuse of the socket. Disconnect it and then release it and |
| 497 // run through the MessageLoop once to get it completely released. | 505 // run through the MessageLoop once to get it completely released. |
| 498 handle_->socket()->Disconnect(); | 506 handle_->socket()->Disconnect(); |
| 499 handle_->Reset(); | 507 handle_->Reset(); |
| 500 { | 508 { |
| 501 base::MessageLoop::ScopedNestableTaskAllower allow( | 509 base::MessageLoop::ScopedNestableTaskAllower allow( |
| 502 base::MessageLoop::current()); | 510 base::MessageLoop::current()); |
| 503 base::RunLoop().RunUntilIdle(); | 511 base::RunLoop().RunUntilIdle(); |
| 504 } | 512 } |
| 505 within_callback_ = true; | 513 within_callback_ = true; |
| 506 scoped_refptr<TransportSocketParams> dest(new TransportSocketParams( | 514 scoped_refptr<TransportSocketParams> dest(new TransportSocketParams( |
| 507 HostPortPair("www.google.com", 80), false, OnHostResolutionCallback(), | 515 HostPortPair("www.google.com", 80), false, OnHostResolutionCallback(), |
| 508 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DEFAULT)); | 516 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DEFAULT)); |
| 509 int rv = handle_->Init("a", dest, LOWEST, | 517 int rv = handle_->Init("a", dest, LOWEST, |
| 510 ClientSocketPool::RespectLimits::ENABLED, | 518 ClientSocketPool::RespectLimits::ENABLED, |
| 511 callback(), pool_, BoundNetLog()); | 519 callback(), pool_, BoundNetLog()); |
| 512 EXPECT_EQ(OK, rv); | 520 EXPECT_THAT(rv, IsOk()); |
| 513 } | 521 } |
| 514 } | 522 } |
| 515 | 523 |
| 516 ClientSocketHandle* const handle_; | 524 ClientSocketHandle* const handle_; |
| 517 TransportClientSocketPool* const pool_; | 525 TransportClientSocketPool* const pool_; |
| 518 bool within_callback_; | 526 bool within_callback_; |
| 519 CompletionCallback callback_; | 527 CompletionCallback callback_; |
| 520 | 528 |
| 521 DISALLOW_COPY_AND_ASSIGN(RequestSocketCallback); | 529 DISALLOW_COPY_AND_ASSIGN(RequestSocketCallback); |
| 522 }; | 530 }; |
| 523 | 531 |
| 524 TEST_F(TransportClientSocketPoolTest, RequestTwice) { | 532 TEST_F(TransportClientSocketPoolTest, RequestTwice) { |
| 525 ClientSocketHandle handle; | 533 ClientSocketHandle handle; |
| 526 RequestSocketCallback callback(&handle, &pool_); | 534 RequestSocketCallback callback(&handle, &pool_); |
| 527 scoped_refptr<TransportSocketParams> dest(new TransportSocketParams( | 535 scoped_refptr<TransportSocketParams> dest(new TransportSocketParams( |
| 528 HostPortPair("www.google.com", 80), false, OnHostResolutionCallback(), | 536 HostPortPair("www.google.com", 80), false, OnHostResolutionCallback(), |
| 529 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DEFAULT)); | 537 TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DEFAULT)); |
| 530 int rv = | 538 int rv = |
| 531 handle.Init("a", dest, LOWEST, ClientSocketPool::RespectLimits::ENABLED, | 539 handle.Init("a", dest, LOWEST, ClientSocketPool::RespectLimits::ENABLED, |
| 532 callback.callback(), &pool_, BoundNetLog()); | 540 callback.callback(), &pool_, BoundNetLog()); |
| 533 ASSERT_EQ(ERR_IO_PENDING, rv); | 541 ASSERT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 534 | 542 |
| 535 // The callback is going to request "www.google.com". We want it to complete | 543 // The callback is going to request "www.google.com". We want it to complete |
| 536 // synchronously this time. | 544 // synchronously this time. |
| 537 host_resolver_->set_synchronous_mode(true); | 545 host_resolver_->set_synchronous_mode(true); |
| 538 | 546 |
| 539 EXPECT_EQ(OK, callback.WaitForResult()); | 547 EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 540 | 548 |
| 541 handle.Reset(); | 549 handle.Reset(); |
| 542 } | 550 } |
| 543 | 551 |
| 544 // Make sure that pending requests get serviced after active requests get | 552 // Make sure that pending requests get serviced after active requests get |
| 545 // cancelled. | 553 // cancelled. |
| 546 TEST_F(TransportClientSocketPoolTest, CancelActiveRequestWithPendingRequests) { | 554 TEST_F(TransportClientSocketPoolTest, CancelActiveRequestWithPendingRequests) { |
| 547 client_socket_factory_.set_default_client_socket_type( | 555 client_socket_factory_.set_default_client_socket_type( |
| 548 MockTransportClientSocketFactory::MOCK_PENDING_CLIENT_SOCKET); | 556 MockTransportClientSocketFactory::MOCK_PENDING_CLIENT_SOCKET); |
| 549 | 557 |
| 550 // Queue up all the requests | 558 // Queue up all the requests |
| 551 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); | 559 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsError(ERR_IO_PENDING)); |
| 552 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); | 560 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsError(ERR_IO_PENDING)); |
| 553 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); | 561 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsError(ERR_IO_PENDING)); |
| 554 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); | 562 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsError(ERR_IO_PENDING)); |
| 555 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); | 563 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsError(ERR_IO_PENDING)); |
| 556 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); | 564 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsError(ERR_IO_PENDING)); |
| 557 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); | 565 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsError(ERR_IO_PENDING)); |
| 558 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); | 566 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsError(ERR_IO_PENDING)); |
| 559 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); | 567 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsError(ERR_IO_PENDING)); |
| 560 | 568 |
| 561 // Now, kMaxSocketsPerGroup requests should be active. Let's cancel them. | 569 // Now, kMaxSocketsPerGroup requests should be active. Let's cancel them. |
| 562 ASSERT_LE(kMaxSocketsPerGroup, static_cast<int>(requests()->size())); | 570 ASSERT_LE(kMaxSocketsPerGroup, static_cast<int>(requests()->size())); |
| 563 for (int i = 0; i < kMaxSocketsPerGroup; i++) | 571 for (int i = 0; i < kMaxSocketsPerGroup; i++) |
| 564 (*requests())[i]->handle()->Reset(); | 572 (*requests())[i]->handle()->Reset(); |
| 565 | 573 |
| 566 // Let's wait for the rest to complete now. | 574 // Let's wait for the rest to complete now. |
| 567 for (size_t i = kMaxSocketsPerGroup; i < requests()->size(); ++i) { | 575 for (size_t i = kMaxSocketsPerGroup; i < requests()->size(); ++i) { |
| 568 EXPECT_EQ(OK, (*requests())[i]->WaitForResult()); | 576 EXPECT_THAT((*requests())[i]->WaitForResult(), IsOk()); |
| 569 (*requests())[i]->handle()->Reset(); | 577 (*requests())[i]->handle()->Reset(); |
| 570 } | 578 } |
| 571 | 579 |
| 572 EXPECT_EQ(requests()->size() - kMaxSocketsPerGroup, completion_count()); | 580 EXPECT_EQ(requests()->size() - kMaxSocketsPerGroup, completion_count()); |
| 573 } | 581 } |
| 574 | 582 |
| 575 // Make sure that pending requests get serviced after active requests fail. | 583 // Make sure that pending requests get serviced after active requests fail. |
| 576 TEST_F(TransportClientSocketPoolTest, FailingActiveRequestWithPendingRequests) { | 584 TEST_F(TransportClientSocketPoolTest, FailingActiveRequestWithPendingRequests) { |
| 577 client_socket_factory_.set_default_client_socket_type( | 585 client_socket_factory_.set_default_client_socket_type( |
| 578 MockTransportClientSocketFactory::MOCK_PENDING_FAILING_CLIENT_SOCKET); | 586 MockTransportClientSocketFactory::MOCK_PENDING_FAILING_CLIENT_SOCKET); |
| 579 | 587 |
| 580 const int kNumRequests = 2 * kMaxSocketsPerGroup + 1; | 588 const int kNumRequests = 2 * kMaxSocketsPerGroup + 1; |
| 581 ASSERT_LE(kNumRequests, kMaxSockets); // Otherwise the test will hang. | 589 ASSERT_LE(kNumRequests, kMaxSockets); // Otherwise the test will hang. |
| 582 | 590 |
| 583 // Queue up all the requests | 591 // Queue up all the requests |
| 584 for (int i = 0; i < kNumRequests; i++) | 592 for (int i = 0; i < kNumRequests; i++) |
| 585 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); | 593 EXPECT_THAT(StartRequest("a", kDefaultPriority), IsError(ERR_IO_PENDING)); |
| 586 | 594 |
| 587 for (int i = 0; i < kNumRequests; i++) | 595 for (int i = 0; i < kNumRequests; i++) |
| 588 EXPECT_EQ(ERR_CONNECTION_FAILED, (*requests())[i]->WaitForResult()); | 596 EXPECT_THAT((*requests())[i]->WaitForResult(), |
| 597 IsError(ERR_CONNECTION_FAILED)); |
| 589 } | 598 } |
| 590 | 599 |
| 591 TEST_F(TransportClientSocketPoolTest, IdleSocketLoadTiming) { | 600 TEST_F(TransportClientSocketPoolTest, IdleSocketLoadTiming) { |
| 592 TestCompletionCallback callback; | 601 TestCompletionCallback callback; |
| 593 ClientSocketHandle handle; | 602 ClientSocketHandle handle; |
| 594 int rv = | 603 int rv = |
| 595 handle.Init("a", params_, LOW, ClientSocketPool::RespectLimits::ENABLED, | 604 handle.Init("a", params_, LOW, ClientSocketPool::RespectLimits::ENABLED, |
| 596 callback.callback(), &pool_, BoundNetLog()); | 605 callback.callback(), &pool_, BoundNetLog()); |
| 597 EXPECT_EQ(ERR_IO_PENDING, rv); | 606 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 598 EXPECT_FALSE(handle.is_initialized()); | 607 EXPECT_FALSE(handle.is_initialized()); |
| 599 EXPECT_FALSE(handle.socket()); | 608 EXPECT_FALSE(handle.socket()); |
| 600 | 609 |
| 601 EXPECT_EQ(OK, callback.WaitForResult()); | 610 EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 602 EXPECT_TRUE(handle.is_initialized()); | 611 EXPECT_TRUE(handle.is_initialized()); |
| 603 EXPECT_TRUE(handle.socket()); | 612 EXPECT_TRUE(handle.socket()); |
| 604 TestLoadTimingInfoConnectedNotReused(handle); | 613 TestLoadTimingInfoConnectedNotReused(handle); |
| 605 | 614 |
| 606 handle.Reset(); | 615 handle.Reset(); |
| 607 // Need to run all pending to release the socket back to the pool. | 616 // Need to run all pending to release the socket back to the pool. |
| 608 base::RunLoop().RunUntilIdle(); | 617 base::RunLoop().RunUntilIdle(); |
| 609 | 618 |
| 610 // Now we should have 1 idle socket. | 619 // Now we should have 1 idle socket. |
| 611 EXPECT_EQ(1, pool_.IdleSocketCount()); | 620 EXPECT_EQ(1, pool_.IdleSocketCount()); |
| 612 | 621 |
| 613 rv = handle.Init("a", params_, LOW, ClientSocketPool::RespectLimits::ENABLED, | 622 rv = handle.Init("a", params_, LOW, ClientSocketPool::RespectLimits::ENABLED, |
| 614 callback.callback(), &pool_, BoundNetLog()); | 623 callback.callback(), &pool_, BoundNetLog()); |
| 615 EXPECT_EQ(OK, rv); | 624 EXPECT_THAT(rv, IsOk()); |
| 616 EXPECT_EQ(0, pool_.IdleSocketCount()); | 625 EXPECT_EQ(0, pool_.IdleSocketCount()); |
| 617 TestLoadTimingInfoConnectedReused(handle); | 626 TestLoadTimingInfoConnectedReused(handle); |
| 618 } | 627 } |
| 619 | 628 |
| 620 TEST_F(TransportClientSocketPoolTest, ResetIdleSocketsOnIPAddressChange) { | 629 TEST_F(TransportClientSocketPoolTest, ResetIdleSocketsOnIPAddressChange) { |
| 621 TestCompletionCallback callback; | 630 TestCompletionCallback callback; |
| 622 ClientSocketHandle handle; | 631 ClientSocketHandle handle; |
| 623 int rv = | 632 int rv = |
| 624 handle.Init("a", params_, LOW, ClientSocketPool::RespectLimits::ENABLED, | 633 handle.Init("a", params_, LOW, ClientSocketPool::RespectLimits::ENABLED, |
| 625 callback.callback(), &pool_, BoundNetLog()); | 634 callback.callback(), &pool_, BoundNetLog()); |
| 626 EXPECT_EQ(ERR_IO_PENDING, rv); | 635 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 627 EXPECT_FALSE(handle.is_initialized()); | 636 EXPECT_FALSE(handle.is_initialized()); |
| 628 EXPECT_FALSE(handle.socket()); | 637 EXPECT_FALSE(handle.socket()); |
| 629 | 638 |
| 630 EXPECT_EQ(OK, callback.WaitForResult()); | 639 EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 631 EXPECT_TRUE(handle.is_initialized()); | 640 EXPECT_TRUE(handle.is_initialized()); |
| 632 EXPECT_TRUE(handle.socket()); | 641 EXPECT_TRUE(handle.socket()); |
| 633 | 642 |
| 634 handle.Reset(); | 643 handle.Reset(); |
| 635 | 644 |
| 636 // Need to run all pending to release the socket back to the pool. | 645 // Need to run all pending to release the socket back to the pool. |
| 637 base::RunLoop().RunUntilIdle(); | 646 base::RunLoop().RunUntilIdle(); |
| 638 | 647 |
| 639 // Now we should have 1 idle socket. | 648 // Now we should have 1 idle socket. |
| 640 EXPECT_EQ(1, pool_.IdleSocketCount()); | 649 EXPECT_EQ(1, pool_.IdleSocketCount()); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 673 for (size_t index = 0; index < arraysize(cases); ++index) { | 682 for (size_t index = 0; index < arraysize(cases); ++index) { |
| 674 client_socket_factory_.set_client_socket_types(cases[index], 2); | 683 client_socket_factory_.set_client_socket_types(cases[index], 2); |
| 675 | 684 |
| 676 EXPECT_EQ(0, pool_.IdleSocketCount()); | 685 EXPECT_EQ(0, pool_.IdleSocketCount()); |
| 677 | 686 |
| 678 TestCompletionCallback callback; | 687 TestCompletionCallback callback; |
| 679 ClientSocketHandle handle; | 688 ClientSocketHandle handle; |
| 680 int rv = | 689 int rv = |
| 681 handle.Init("b", params_, LOW, ClientSocketPool::RespectLimits::ENABLED, | 690 handle.Init("b", params_, LOW, ClientSocketPool::RespectLimits::ENABLED, |
| 682 callback.callback(), &pool_, BoundNetLog()); | 691 callback.callback(), &pool_, BoundNetLog()); |
| 683 EXPECT_EQ(ERR_IO_PENDING, rv); | 692 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 684 EXPECT_FALSE(handle.is_initialized()); | 693 EXPECT_FALSE(handle.is_initialized()); |
| 685 EXPECT_FALSE(handle.socket()); | 694 EXPECT_FALSE(handle.socket()); |
| 686 | 695 |
| 687 // Create the first socket, set the timer. | 696 // Create the first socket, set the timer. |
| 688 base::RunLoop().RunUntilIdle(); | 697 base::RunLoop().RunUntilIdle(); |
| 689 | 698 |
| 690 // Wait for the backup socket timer to fire. | 699 // Wait for the backup socket timer to fire. |
| 691 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds( | 700 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds( |
| 692 ClientSocketPool::kMaxConnectRetryIntervalMs + 50)); | 701 ClientSocketPool::kMaxConnectRetryIntervalMs + 50)); |
| 693 | 702 |
| 694 // Let the appropriate socket connect. | 703 // Let the appropriate socket connect. |
| 695 base::RunLoop().RunUntilIdle(); | 704 base::RunLoop().RunUntilIdle(); |
| 696 | 705 |
| 697 EXPECT_EQ(OK, callback.WaitForResult()); | 706 EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 698 EXPECT_TRUE(handle.is_initialized()); | 707 EXPECT_TRUE(handle.is_initialized()); |
| 699 EXPECT_TRUE(handle.socket()); | 708 EXPECT_TRUE(handle.socket()); |
| 700 | 709 |
| 701 // One socket is stalled, the other is active. | 710 // One socket is stalled, the other is active. |
| 702 EXPECT_EQ(0, pool_.IdleSocketCount()); | 711 EXPECT_EQ(0, pool_.IdleSocketCount()); |
| 703 handle.Reset(); | 712 handle.Reset(); |
| 704 | 713 |
| 705 // Close all pending connect jobs and existing sockets. | 714 // Close all pending connect jobs and existing sockets. |
| 706 pool_.FlushWithError(ERR_NETWORK_CHANGED); | 715 pool_.FlushWithError(ERR_NETWORK_CHANGED); |
| 707 } | 716 } |
| 708 } | 717 } |
| 709 | 718 |
| 710 // Test the case where a socket took long enough to start the creation | 719 // Test the case where a socket took long enough to start the creation |
| 711 // of the backup socket, but then we cancelled the request after that. | 720 // of the backup socket, but then we cancelled the request after that. |
| 712 TEST_F(TransportClientSocketPoolTest, BackupSocketCancel) { | 721 TEST_F(TransportClientSocketPoolTest, BackupSocketCancel) { |
| 713 client_socket_factory_.set_default_client_socket_type( | 722 client_socket_factory_.set_default_client_socket_type( |
| 714 MockTransportClientSocketFactory::MOCK_STALLED_CLIENT_SOCKET); | 723 MockTransportClientSocketFactory::MOCK_STALLED_CLIENT_SOCKET); |
| 715 | 724 |
| 716 enum { CANCEL_BEFORE_WAIT, CANCEL_AFTER_WAIT }; | 725 enum { CANCEL_BEFORE_WAIT, CANCEL_AFTER_WAIT }; |
| 717 | 726 |
| 718 for (int index = CANCEL_BEFORE_WAIT; index < CANCEL_AFTER_WAIT; ++index) { | 727 for (int index = CANCEL_BEFORE_WAIT; index < CANCEL_AFTER_WAIT; ++index) { |
| 719 EXPECT_EQ(0, pool_.IdleSocketCount()); | 728 EXPECT_EQ(0, pool_.IdleSocketCount()); |
| 720 | 729 |
| 721 TestCompletionCallback callback; | 730 TestCompletionCallback callback; |
| 722 ClientSocketHandle handle; | 731 ClientSocketHandle handle; |
| 723 int rv = | 732 int rv = |
| 724 handle.Init("c", params_, LOW, ClientSocketPool::RespectLimits::ENABLED, | 733 handle.Init("c", params_, LOW, ClientSocketPool::RespectLimits::ENABLED, |
| 725 callback.callback(), &pool_, BoundNetLog()); | 734 callback.callback(), &pool_, BoundNetLog()); |
| 726 EXPECT_EQ(ERR_IO_PENDING, rv); | 735 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 727 EXPECT_FALSE(handle.is_initialized()); | 736 EXPECT_FALSE(handle.is_initialized()); |
| 728 EXPECT_FALSE(handle.socket()); | 737 EXPECT_FALSE(handle.socket()); |
| 729 | 738 |
| 730 // Create the first socket, set the timer. | 739 // Create the first socket, set the timer. |
| 731 base::RunLoop().RunUntilIdle(); | 740 base::RunLoop().RunUntilIdle(); |
| 732 | 741 |
| 733 if (index == CANCEL_AFTER_WAIT) { | 742 if (index == CANCEL_AFTER_WAIT) { |
| 734 // Wait for the backup socket timer to fire. | 743 // Wait for the backup socket timer to fire. |
| 735 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds( | 744 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds( |
| 736 ClientSocketPool::kMaxConnectRetryIntervalMs)); | 745 ClientSocketPool::kMaxConnectRetryIntervalMs)); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 763 | 772 |
| 764 client_socket_factory_.set_client_socket_types(case_types, 2); | 773 client_socket_factory_.set_client_socket_types(case_types, 2); |
| 765 | 774 |
| 766 EXPECT_EQ(0, pool_.IdleSocketCount()); | 775 EXPECT_EQ(0, pool_.IdleSocketCount()); |
| 767 | 776 |
| 768 TestCompletionCallback callback; | 777 TestCompletionCallback callback; |
| 769 ClientSocketHandle handle; | 778 ClientSocketHandle handle; |
| 770 int rv = | 779 int rv = |
| 771 handle.Init("b", params_, LOW, ClientSocketPool::RespectLimits::ENABLED, | 780 handle.Init("b", params_, LOW, ClientSocketPool::RespectLimits::ENABLED, |
| 772 callback.callback(), &pool_, BoundNetLog()); | 781 callback.callback(), &pool_, BoundNetLog()); |
| 773 EXPECT_EQ(ERR_IO_PENDING, rv); | 782 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 774 EXPECT_FALSE(handle.is_initialized()); | 783 EXPECT_FALSE(handle.is_initialized()); |
| 775 EXPECT_FALSE(handle.socket()); | 784 EXPECT_FALSE(handle.socket()); |
| 776 | 785 |
| 777 // Create the first socket, set the timer. | 786 // Create the first socket, set the timer. |
| 778 base::RunLoop().RunUntilIdle(); | 787 base::RunLoop().RunUntilIdle(); |
| 779 | 788 |
| 780 // Wait for the backup socket timer to fire. | 789 // Wait for the backup socket timer to fire. |
| 781 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds( | 790 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds( |
| 782 ClientSocketPool::kMaxConnectRetryIntervalMs)); | 791 ClientSocketPool::kMaxConnectRetryIntervalMs)); |
| 783 | 792 |
| 784 // Let the second connect be synchronous. Otherwise, the emulated | 793 // Let the second connect be synchronous. Otherwise, the emulated |
| 785 // host resolution takes an extra trip through the message loop. | 794 // host resolution takes an extra trip through the message loop. |
| 786 host_resolver_->set_synchronous_mode(true); | 795 host_resolver_->set_synchronous_mode(true); |
| 787 | 796 |
| 788 // Let the appropriate socket connect. | 797 // Let the appropriate socket connect. |
| 789 base::RunLoop().RunUntilIdle(); | 798 base::RunLoop().RunUntilIdle(); |
| 790 | 799 |
| 791 EXPECT_EQ(ERR_CONNECTION_FAILED, callback.WaitForResult()); | 800 EXPECT_THAT(callback.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 792 EXPECT_FALSE(handle.is_initialized()); | 801 EXPECT_FALSE(handle.is_initialized()); |
| 793 EXPECT_FALSE(handle.socket()); | 802 EXPECT_FALSE(handle.socket()); |
| 794 ASSERT_EQ(1u, handle.connection_attempts().size()); | 803 ASSERT_EQ(1u, handle.connection_attempts().size()); |
| 795 EXPECT_EQ(ERR_CONNECTION_FAILED, handle.connection_attempts()[0].result); | 804 EXPECT_THAT(handle.connection_attempts()[0].result, |
| 805 IsError(ERR_CONNECTION_FAILED)); |
| 796 EXPECT_EQ(0, pool_.IdleSocketCount()); | 806 EXPECT_EQ(0, pool_.IdleSocketCount()); |
| 797 handle.Reset(); | 807 handle.Reset(); |
| 798 | 808 |
| 799 // Reset for the next case. | 809 // Reset for the next case. |
| 800 host_resolver_->set_synchronous_mode(false); | 810 host_resolver_->set_synchronous_mode(false); |
| 801 } | 811 } |
| 802 | 812 |
| 803 // Test the case where a socket took long enough to start the creation | 813 // Test the case where a socket took long enough to start the creation |
| 804 // of the backup socket and eventually completes, but the backup socket | 814 // of the backup socket and eventually completes, but the backup socket |
| 805 // fails. | 815 // fails. |
| 806 TEST_F(TransportClientSocketPoolTest, BackupSocketFailAfterDelay) { | 816 TEST_F(TransportClientSocketPoolTest, BackupSocketFailAfterDelay) { |
| 807 MockTransportClientSocketFactory::ClientSocketType case_types[] = { | 817 MockTransportClientSocketFactory::ClientSocketType case_types[] = { |
| 808 // The first socket will connect, although delayed. | 818 // The first socket will connect, although delayed. |
| 809 MockTransportClientSocketFactory::MOCK_DELAYED_CLIENT_SOCKET, | 819 MockTransportClientSocketFactory::MOCK_DELAYED_CLIENT_SOCKET, |
| 810 // The second socket will not connect. | 820 // The second socket will not connect. |
| 811 MockTransportClientSocketFactory::MOCK_FAILING_CLIENT_SOCKET | 821 MockTransportClientSocketFactory::MOCK_FAILING_CLIENT_SOCKET |
| 812 }; | 822 }; |
| 813 | 823 |
| 814 client_socket_factory_.set_client_socket_types(case_types, 2); | 824 client_socket_factory_.set_client_socket_types(case_types, 2); |
| 815 client_socket_factory_.set_delay(base::TimeDelta::FromSeconds(5)); | 825 client_socket_factory_.set_delay(base::TimeDelta::FromSeconds(5)); |
| 816 | 826 |
| 817 EXPECT_EQ(0, pool_.IdleSocketCount()); | 827 EXPECT_EQ(0, pool_.IdleSocketCount()); |
| 818 | 828 |
| 819 TestCompletionCallback callback; | 829 TestCompletionCallback callback; |
| 820 ClientSocketHandle handle; | 830 ClientSocketHandle handle; |
| 821 int rv = | 831 int rv = |
| 822 handle.Init("b", params_, LOW, ClientSocketPool::RespectLimits::ENABLED, | 832 handle.Init("b", params_, LOW, ClientSocketPool::RespectLimits::ENABLED, |
| 823 callback.callback(), &pool_, BoundNetLog()); | 833 callback.callback(), &pool_, BoundNetLog()); |
| 824 EXPECT_EQ(ERR_IO_PENDING, rv); | 834 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 825 EXPECT_FALSE(handle.is_initialized()); | 835 EXPECT_FALSE(handle.is_initialized()); |
| 826 EXPECT_FALSE(handle.socket()); | 836 EXPECT_FALSE(handle.socket()); |
| 827 | 837 |
| 828 // Create the first socket, set the timer. | 838 // Create the first socket, set the timer. |
| 829 base::RunLoop().RunUntilIdle(); | 839 base::RunLoop().RunUntilIdle(); |
| 830 | 840 |
| 831 // Wait for the backup socket timer to fire. | 841 // Wait for the backup socket timer to fire. |
| 832 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds( | 842 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds( |
| 833 ClientSocketPool::kMaxConnectRetryIntervalMs)); | 843 ClientSocketPool::kMaxConnectRetryIntervalMs)); |
| 834 | 844 |
| 835 // Let the second connect be synchronous. Otherwise, the emulated | 845 // Let the second connect be synchronous. Otherwise, the emulated |
| 836 // host resolution takes an extra trip through the message loop. | 846 // host resolution takes an extra trip through the message loop. |
| 837 host_resolver_->set_synchronous_mode(true); | 847 host_resolver_->set_synchronous_mode(true); |
| 838 | 848 |
| 839 // Let the appropriate socket connect. | 849 // Let the appropriate socket connect. |
| 840 base::RunLoop().RunUntilIdle(); | 850 base::RunLoop().RunUntilIdle(); |
| 841 | 851 |
| 842 EXPECT_EQ(ERR_CONNECTION_FAILED, callback.WaitForResult()); | 852 EXPECT_THAT(callback.WaitForResult(), IsError(ERR_CONNECTION_FAILED)); |
| 843 EXPECT_FALSE(handle.is_initialized()); | 853 EXPECT_FALSE(handle.is_initialized()); |
| 844 EXPECT_FALSE(handle.socket()); | 854 EXPECT_FALSE(handle.socket()); |
| 845 ASSERT_EQ(1u, handle.connection_attempts().size()); | 855 ASSERT_EQ(1u, handle.connection_attempts().size()); |
| 846 EXPECT_EQ(ERR_CONNECTION_FAILED, handle.connection_attempts()[0].result); | 856 EXPECT_THAT(handle.connection_attempts()[0].result, |
| 857 IsError(ERR_CONNECTION_FAILED)); |
| 847 handle.Reset(); | 858 handle.Reset(); |
| 848 | 859 |
| 849 // Reset for the next case. | 860 // Reset for the next case. |
| 850 host_resolver_->set_synchronous_mode(false); | 861 host_resolver_->set_synchronous_mode(false); |
| 851 } | 862 } |
| 852 | 863 |
| 853 // Test the case of the IPv6 address stalling, and falling back to the IPv4 | 864 // Test the case of the IPv6 address stalling, and falling back to the IPv4 |
| 854 // socket which finishes first. | 865 // socket which finishes first. |
| 855 TEST_F(TransportClientSocketPoolTest, IPv6FallbackSocketIPv4FinishesFirst) { | 866 TEST_F(TransportClientSocketPoolTest, IPv6FallbackSocketIPv4FinishesFirst) { |
| 856 // Create a pool without backup jobs. | 867 // Create a pool without backup jobs. |
| (...skipping 13 matching lines...) Expand all Loading... |
| 870 | 881 |
| 871 // Resolve an AddressList with a IPv6 address first and then a IPv4 address. | 882 // Resolve an AddressList with a IPv6 address first and then a IPv4 address. |
| 872 host_resolver_->rules() | 883 host_resolver_->rules() |
| 873 ->AddIPLiteralRule("*", "2:abcd::3:4:ff,2.2.2.2", std::string()); | 884 ->AddIPLiteralRule("*", "2:abcd::3:4:ff,2.2.2.2", std::string()); |
| 874 | 885 |
| 875 TestCompletionCallback callback; | 886 TestCompletionCallback callback; |
| 876 ClientSocketHandle handle; | 887 ClientSocketHandle handle; |
| 877 int rv = | 888 int rv = |
| 878 handle.Init("a", params_, LOW, ClientSocketPool::RespectLimits::ENABLED, | 889 handle.Init("a", params_, LOW, ClientSocketPool::RespectLimits::ENABLED, |
| 879 callback.callback(), &pool, BoundNetLog()); | 890 callback.callback(), &pool, BoundNetLog()); |
| 880 EXPECT_EQ(ERR_IO_PENDING, rv); | 891 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 881 EXPECT_FALSE(handle.is_initialized()); | 892 EXPECT_FALSE(handle.is_initialized()); |
| 882 EXPECT_FALSE(handle.socket()); | 893 EXPECT_FALSE(handle.socket()); |
| 883 | 894 |
| 884 EXPECT_EQ(OK, callback.WaitForResult()); | 895 EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 885 EXPECT_TRUE(handle.is_initialized()); | 896 EXPECT_TRUE(handle.is_initialized()); |
| 886 EXPECT_TRUE(handle.socket()); | 897 EXPECT_TRUE(handle.socket()); |
| 887 IPEndPoint endpoint; | 898 IPEndPoint endpoint; |
| 888 handle.socket()->GetLocalAddress(&endpoint); | 899 handle.socket()->GetLocalAddress(&endpoint); |
| 889 EXPECT_TRUE(endpoint.address().IsIPv4()); | 900 EXPECT_TRUE(endpoint.address().IsIPv4()); |
| 890 | 901 |
| 891 // Check that the failed connection attempt on the main socket is collected. | 902 // Check that the failed connection attempt on the main socket is collected. |
| 892 ConnectionAttempts attempts; | 903 ConnectionAttempts attempts; |
| 893 handle.socket()->GetConnectionAttempts(&attempts); | 904 handle.socket()->GetConnectionAttempts(&attempts); |
| 894 ASSERT_EQ(1u, attempts.size()); | 905 ASSERT_EQ(1u, attempts.size()); |
| 895 EXPECT_EQ(ERR_CONNECTION_FAILED, attempts[0].result); | 906 EXPECT_THAT(attempts[0].result, IsError(ERR_CONNECTION_FAILED)); |
| 896 EXPECT_TRUE(attempts[0].endpoint.address().IsIPv6()); | 907 EXPECT_TRUE(attempts[0].endpoint.address().IsIPv6()); |
| 897 | 908 |
| 898 EXPECT_EQ(2, client_socket_factory_.allocation_count()); | 909 EXPECT_EQ(2, client_socket_factory_.allocation_count()); |
| 899 } | 910 } |
| 900 | 911 |
| 901 // Test the case of the IPv6 address being slow, thus falling back to trying to | 912 // Test the case of the IPv6 address being slow, thus falling back to trying to |
| 902 // connect to the IPv4 address, but having the connect to the IPv6 address | 913 // connect to the IPv4 address, but having the connect to the IPv6 address |
| 903 // finish first. | 914 // finish first. |
| 904 TEST_F(TransportClientSocketPoolTest, IPv6FallbackSocketIPv6FinishesFirst) { | 915 TEST_F(TransportClientSocketPoolTest, IPv6FallbackSocketIPv6FinishesFirst) { |
| 905 // Create a pool without backup jobs. | 916 // Create a pool without backup jobs. |
| (...skipping 15 matching lines...) Expand all Loading... |
| 921 | 932 |
| 922 // Resolve an AddressList with a IPv6 address first and then a IPv4 address. | 933 // Resolve an AddressList with a IPv6 address first and then a IPv4 address. |
| 923 host_resolver_->rules() | 934 host_resolver_->rules() |
| 924 ->AddIPLiteralRule("*", "2:abcd::3:4:ff,2.2.2.2", std::string()); | 935 ->AddIPLiteralRule("*", "2:abcd::3:4:ff,2.2.2.2", std::string()); |
| 925 | 936 |
| 926 TestCompletionCallback callback; | 937 TestCompletionCallback callback; |
| 927 ClientSocketHandle handle; | 938 ClientSocketHandle handle; |
| 928 int rv = | 939 int rv = |
| 929 handle.Init("a", params_, LOW, ClientSocketPool::RespectLimits::ENABLED, | 940 handle.Init("a", params_, LOW, ClientSocketPool::RespectLimits::ENABLED, |
| 930 callback.callback(), &pool, BoundNetLog()); | 941 callback.callback(), &pool, BoundNetLog()); |
| 931 EXPECT_EQ(ERR_IO_PENDING, rv); | 942 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 932 EXPECT_FALSE(handle.is_initialized()); | 943 EXPECT_FALSE(handle.is_initialized()); |
| 933 EXPECT_FALSE(handle.socket()); | 944 EXPECT_FALSE(handle.socket()); |
| 934 | 945 |
| 935 EXPECT_EQ(OK, callback.WaitForResult()); | 946 EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 936 EXPECT_TRUE(handle.is_initialized()); | 947 EXPECT_TRUE(handle.is_initialized()); |
| 937 EXPECT_TRUE(handle.socket()); | 948 EXPECT_TRUE(handle.socket()); |
| 938 IPEndPoint endpoint; | 949 IPEndPoint endpoint; |
| 939 handle.socket()->GetLocalAddress(&endpoint); | 950 handle.socket()->GetLocalAddress(&endpoint); |
| 940 EXPECT_TRUE(endpoint.address().IsIPv6()); | 951 EXPECT_TRUE(endpoint.address().IsIPv6()); |
| 941 | 952 |
| 942 // Check that the failed connection attempt on the fallback socket is | 953 // Check that the failed connection attempt on the fallback socket is |
| 943 // collected. | 954 // collected. |
| 944 ConnectionAttempts attempts; | 955 ConnectionAttempts attempts; |
| 945 handle.socket()->GetConnectionAttempts(&attempts); | 956 handle.socket()->GetConnectionAttempts(&attempts); |
| 946 ASSERT_EQ(1u, attempts.size()); | 957 ASSERT_EQ(1u, attempts.size()); |
| 947 EXPECT_EQ(ERR_CONNECTION_FAILED, attempts[0].result); | 958 EXPECT_THAT(attempts[0].result, IsError(ERR_CONNECTION_FAILED)); |
| 948 EXPECT_TRUE(attempts[0].endpoint.address().IsIPv4()); | 959 EXPECT_TRUE(attempts[0].endpoint.address().IsIPv4()); |
| 949 | 960 |
| 950 EXPECT_EQ(2, client_socket_factory_.allocation_count()); | 961 EXPECT_EQ(2, client_socket_factory_.allocation_count()); |
| 951 } | 962 } |
| 952 | 963 |
| 953 TEST_F(TransportClientSocketPoolTest, IPv6NoIPv4AddressesToFallbackTo) { | 964 TEST_F(TransportClientSocketPoolTest, IPv6NoIPv4AddressesToFallbackTo) { |
| 954 // Create a pool without backup jobs. | 965 // Create a pool without backup jobs. |
| 955 ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(false); | 966 ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(false); |
| 956 TransportClientSocketPool pool(kMaxSockets, kMaxSocketsPerGroup, | 967 TransportClientSocketPool pool(kMaxSockets, kMaxSocketsPerGroup, |
| 957 host_resolver_.get(), &client_socket_factory_, | 968 host_resolver_.get(), &client_socket_factory_, |
| 958 NULL, NULL); | 969 NULL, NULL); |
| 959 | 970 |
| 960 client_socket_factory_.set_default_client_socket_type( | 971 client_socket_factory_.set_default_client_socket_type( |
| 961 MockTransportClientSocketFactory::MOCK_DELAYED_CLIENT_SOCKET); | 972 MockTransportClientSocketFactory::MOCK_DELAYED_CLIENT_SOCKET); |
| 962 | 973 |
| 963 // Resolve an AddressList with only IPv6 addresses. | 974 // Resolve an AddressList with only IPv6 addresses. |
| 964 host_resolver_->rules() | 975 host_resolver_->rules() |
| 965 ->AddIPLiteralRule("*", "2:abcd::3:4:ff,3:abcd::3:4:ff", std::string()); | 976 ->AddIPLiteralRule("*", "2:abcd::3:4:ff,3:abcd::3:4:ff", std::string()); |
| 966 | 977 |
| 967 TestCompletionCallback callback; | 978 TestCompletionCallback callback; |
| 968 ClientSocketHandle handle; | 979 ClientSocketHandle handle; |
| 969 int rv = | 980 int rv = |
| 970 handle.Init("a", params_, LOW, ClientSocketPool::RespectLimits::ENABLED, | 981 handle.Init("a", params_, LOW, ClientSocketPool::RespectLimits::ENABLED, |
| 971 callback.callback(), &pool, BoundNetLog()); | 982 callback.callback(), &pool, BoundNetLog()); |
| 972 EXPECT_EQ(ERR_IO_PENDING, rv); | 983 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 973 EXPECT_FALSE(handle.is_initialized()); | 984 EXPECT_FALSE(handle.is_initialized()); |
| 974 EXPECT_FALSE(handle.socket()); | 985 EXPECT_FALSE(handle.socket()); |
| 975 | 986 |
| 976 EXPECT_EQ(OK, callback.WaitForResult()); | 987 EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 977 EXPECT_TRUE(handle.is_initialized()); | 988 EXPECT_TRUE(handle.is_initialized()); |
| 978 EXPECT_TRUE(handle.socket()); | 989 EXPECT_TRUE(handle.socket()); |
| 979 IPEndPoint endpoint; | 990 IPEndPoint endpoint; |
| 980 handle.socket()->GetLocalAddress(&endpoint); | 991 handle.socket()->GetLocalAddress(&endpoint); |
| 981 EXPECT_TRUE(endpoint.address().IsIPv6()); | 992 EXPECT_TRUE(endpoint.address().IsIPv6()); |
| 982 EXPECT_EQ(0u, handle.connection_attempts().size()); | 993 EXPECT_EQ(0u, handle.connection_attempts().size()); |
| 983 EXPECT_EQ(1, client_socket_factory_.allocation_count()); | 994 EXPECT_EQ(1, client_socket_factory_.allocation_count()); |
| 984 } | 995 } |
| 985 | 996 |
| 986 TEST_F(TransportClientSocketPoolTest, IPv4HasNoFallback) { | 997 TEST_F(TransportClientSocketPoolTest, IPv4HasNoFallback) { |
| 987 // Create a pool without backup jobs. | 998 // Create a pool without backup jobs. |
| 988 ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(false); | 999 ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(false); |
| 989 TransportClientSocketPool pool(kMaxSockets, kMaxSocketsPerGroup, | 1000 TransportClientSocketPool pool(kMaxSockets, kMaxSocketsPerGroup, |
| 990 host_resolver_.get(), &client_socket_factory_, | 1001 host_resolver_.get(), &client_socket_factory_, |
| 991 NULL, NULL); | 1002 NULL, NULL); |
| 992 | 1003 |
| 993 client_socket_factory_.set_default_client_socket_type( | 1004 client_socket_factory_.set_default_client_socket_type( |
| 994 MockTransportClientSocketFactory::MOCK_DELAYED_CLIENT_SOCKET); | 1005 MockTransportClientSocketFactory::MOCK_DELAYED_CLIENT_SOCKET); |
| 995 | 1006 |
| 996 // Resolve an AddressList with only IPv4 addresses. | 1007 // Resolve an AddressList with only IPv4 addresses. |
| 997 host_resolver_->rules()->AddIPLiteralRule("*", "1.1.1.1", std::string()); | 1008 host_resolver_->rules()->AddIPLiteralRule("*", "1.1.1.1", std::string()); |
| 998 | 1009 |
| 999 TestCompletionCallback callback; | 1010 TestCompletionCallback callback; |
| 1000 ClientSocketHandle handle; | 1011 ClientSocketHandle handle; |
| 1001 int rv = | 1012 int rv = |
| 1002 handle.Init("a", params_, LOW, ClientSocketPool::RespectLimits::ENABLED, | 1013 handle.Init("a", params_, LOW, ClientSocketPool::RespectLimits::ENABLED, |
| 1003 callback.callback(), &pool, BoundNetLog()); | 1014 callback.callback(), &pool, BoundNetLog()); |
| 1004 EXPECT_EQ(ERR_IO_PENDING, rv); | 1015 EXPECT_THAT(rv, IsError(ERR_IO_PENDING)); |
| 1005 EXPECT_FALSE(handle.is_initialized()); | 1016 EXPECT_FALSE(handle.is_initialized()); |
| 1006 EXPECT_FALSE(handle.socket()); | 1017 EXPECT_FALSE(handle.socket()); |
| 1007 | 1018 |
| 1008 EXPECT_EQ(OK, callback.WaitForResult()); | 1019 EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 1009 EXPECT_TRUE(handle.is_initialized()); | 1020 EXPECT_TRUE(handle.is_initialized()); |
| 1010 EXPECT_TRUE(handle.socket()); | 1021 EXPECT_TRUE(handle.socket()); |
| 1011 IPEndPoint endpoint; | 1022 IPEndPoint endpoint; |
| 1012 handle.socket()->GetLocalAddress(&endpoint); | 1023 handle.socket()->GetLocalAddress(&endpoint); |
| 1013 EXPECT_TRUE(endpoint.address().IsIPv4()); | 1024 EXPECT_TRUE(endpoint.address().IsIPv4()); |
| 1014 EXPECT_EQ(0u, handle.connection_attempts().size()); | 1025 EXPECT_EQ(0u, handle.connection_attempts().size()); |
| 1015 EXPECT_EQ(1, client_socket_factory_.allocation_count()); | 1026 EXPECT_EQ(1, client_socket_factory_.allocation_count()); |
| 1016 } | 1027 } |
| 1017 | 1028 |
| 1018 // Test that if TCP FastOpen is enabled, it is set on the socket | 1029 // Test that if TCP FastOpen is enabled, it is set on the socket |
| 1019 // when we have only an IPv4 address. | 1030 // when we have only an IPv4 address. |
| 1020 TEST_F(TransportClientSocketPoolTest, TCPFastOpenOnIPv4WithNoFallback) { | 1031 TEST_F(TransportClientSocketPoolTest, TCPFastOpenOnIPv4WithNoFallback) { |
| 1021 SequencedSocketData socket_data(nullptr, 0, nullptr, 0); | 1032 SequencedSocketData socket_data(nullptr, 0, nullptr, 0); |
| 1022 MockClientSocketFactory factory; | 1033 MockClientSocketFactory factory; |
| 1023 factory.AddSocketDataProvider(&socket_data); | 1034 factory.AddSocketDataProvider(&socket_data); |
| 1024 // Create a pool without backup jobs. | 1035 // Create a pool without backup jobs. |
| 1025 ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(false); | 1036 ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(false); |
| 1026 TransportClientSocketPool pool(kMaxSockets, kMaxSocketsPerGroup, | 1037 TransportClientSocketPool pool(kMaxSockets, kMaxSocketsPerGroup, |
| 1027 host_resolver_.get(), &factory, NULL, NULL); | 1038 host_resolver_.get(), &factory, NULL, NULL); |
| 1028 // Resolve an AddressList with only IPv4 addresses. | 1039 // Resolve an AddressList with only IPv4 addresses. |
| 1029 host_resolver_->rules()->AddIPLiteralRule("*", "1.1.1.1", std::string()); | 1040 host_resolver_->rules()->AddIPLiteralRule("*", "1.1.1.1", std::string()); |
| 1030 | 1041 |
| 1031 TestCompletionCallback callback; | 1042 TestCompletionCallback callback; |
| 1032 ClientSocketHandle handle; | 1043 ClientSocketHandle handle; |
| 1033 // Enable TCP FastOpen in TransportSocketParams. | 1044 // Enable TCP FastOpen in TransportSocketParams. |
| 1034 scoped_refptr<TransportSocketParams> params = CreateParamsForTCPFastOpen(); | 1045 scoped_refptr<TransportSocketParams> params = CreateParamsForTCPFastOpen(); |
| 1035 handle.Init("a", params, LOW, ClientSocketPool::RespectLimits::ENABLED, | 1046 handle.Init("a", params, LOW, ClientSocketPool::RespectLimits::ENABLED, |
| 1036 callback.callback(), &pool, BoundNetLog()); | 1047 callback.callback(), &pool, BoundNetLog()); |
| 1037 EXPECT_EQ(OK, callback.WaitForResult()); | 1048 EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 1038 EXPECT_TRUE(socket_data.IsUsingTCPFastOpen()); | 1049 EXPECT_TRUE(socket_data.IsUsingTCPFastOpen()); |
| 1039 } | 1050 } |
| 1040 | 1051 |
| 1041 // Test that if TCP FastOpen is enabled, it is set on the socket | 1052 // Test that if TCP FastOpen is enabled, it is set on the socket |
| 1042 // when we have only IPv6 addresses. | 1053 // when we have only IPv6 addresses. |
| 1043 TEST_F(TransportClientSocketPoolTest, TCPFastOpenOnIPv6WithNoFallback) { | 1054 TEST_F(TransportClientSocketPoolTest, TCPFastOpenOnIPv6WithNoFallback) { |
| 1044 SequencedSocketData socket_data(nullptr, 0, nullptr, 0); | 1055 SequencedSocketData socket_data(nullptr, 0, nullptr, 0); |
| 1045 MockClientSocketFactory factory; | 1056 MockClientSocketFactory factory; |
| 1046 factory.AddSocketDataProvider(&socket_data); | 1057 factory.AddSocketDataProvider(&socket_data); |
| 1047 // Create a pool without backup jobs. | 1058 // Create a pool without backup jobs. |
| 1048 ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(false); | 1059 ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(false); |
| 1049 TransportClientSocketPool pool(kMaxSockets, kMaxSocketsPerGroup, | 1060 TransportClientSocketPool pool(kMaxSockets, kMaxSocketsPerGroup, |
| 1050 host_resolver_.get(), &factory, NULL, NULL); | 1061 host_resolver_.get(), &factory, NULL, NULL); |
| 1051 client_socket_factory_.set_default_client_socket_type( | 1062 client_socket_factory_.set_default_client_socket_type( |
| 1052 MockTransportClientSocketFactory::MOCK_DELAYED_CLIENT_SOCKET); | 1063 MockTransportClientSocketFactory::MOCK_DELAYED_CLIENT_SOCKET); |
| 1053 // Resolve an AddressList with only IPv6 addresses. | 1064 // Resolve an AddressList with only IPv6 addresses. |
| 1054 host_resolver_->rules() | 1065 host_resolver_->rules() |
| 1055 ->AddIPLiteralRule("*", "2:abcd::3:4:ff,3:abcd::3:4:ff", std::string()); | 1066 ->AddIPLiteralRule("*", "2:abcd::3:4:ff,3:abcd::3:4:ff", std::string()); |
| 1056 | 1067 |
| 1057 TestCompletionCallback callback; | 1068 TestCompletionCallback callback; |
| 1058 ClientSocketHandle handle; | 1069 ClientSocketHandle handle; |
| 1059 // Enable TCP FastOpen in TransportSocketParams. | 1070 // Enable TCP FastOpen in TransportSocketParams. |
| 1060 scoped_refptr<TransportSocketParams> params = CreateParamsForTCPFastOpen(); | 1071 scoped_refptr<TransportSocketParams> params = CreateParamsForTCPFastOpen(); |
| 1061 handle.Init("a", params, LOW, ClientSocketPool::RespectLimits::ENABLED, | 1072 handle.Init("a", params, LOW, ClientSocketPool::RespectLimits::ENABLED, |
| 1062 callback.callback(), &pool, BoundNetLog()); | 1073 callback.callback(), &pool, BoundNetLog()); |
| 1063 EXPECT_EQ(OK, callback.WaitForResult()); | 1074 EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 1064 EXPECT_TRUE(socket_data.IsUsingTCPFastOpen()); | 1075 EXPECT_TRUE(socket_data.IsUsingTCPFastOpen()); |
| 1065 } | 1076 } |
| 1066 | 1077 |
| 1067 // Test that if TCP FastOpen is enabled, it does not do anything when there | 1078 // Test that if TCP FastOpen is enabled, it does not do anything when there |
| 1068 // is a IPv6 address with fallback to an IPv4 address. This test tests the case | 1079 // is a IPv6 address with fallback to an IPv4 address. This test tests the case |
| 1069 // when the IPv6 connect fails and the IPv4 one succeeds. | 1080 // when the IPv6 connect fails and the IPv4 one succeeds. |
| 1070 TEST_F(TransportClientSocketPoolTest, | 1081 TEST_F(TransportClientSocketPoolTest, |
| 1071 NoTCPFastOpenOnIPv6FailureWithIPv4Fallback) { | 1082 NoTCPFastOpenOnIPv6FailureWithIPv4Fallback) { |
| 1072 SequencedSocketData socket_data_1(nullptr, 0, nullptr, 0); | 1083 SequencedSocketData socket_data_1(nullptr, 0, nullptr, 0); |
| 1073 socket_data_1.set_connect_data(MockConnect(SYNCHRONOUS, ERR_IO_PENDING)); | 1084 socket_data_1.set_connect_data(MockConnect(SYNCHRONOUS, ERR_IO_PENDING)); |
| 1074 SequencedSocketData socket_data_2(nullptr, 0, nullptr, 0); | 1085 SequencedSocketData socket_data_2(nullptr, 0, nullptr, 0); |
| 1075 MockClientSocketFactory factory; | 1086 MockClientSocketFactory factory; |
| 1076 factory.AddSocketDataProvider(&socket_data_1); | 1087 factory.AddSocketDataProvider(&socket_data_1); |
| 1077 factory.AddSocketDataProvider(&socket_data_2); | 1088 factory.AddSocketDataProvider(&socket_data_2); |
| 1078 // Create a pool without backup jobs. | 1089 // Create a pool without backup jobs. |
| 1079 ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(false); | 1090 ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(false); |
| 1080 TransportClientSocketPool pool(kMaxSockets, kMaxSocketsPerGroup, | 1091 TransportClientSocketPool pool(kMaxSockets, kMaxSocketsPerGroup, |
| 1081 host_resolver_.get(), &factory, NULL, NULL); | 1092 host_resolver_.get(), &factory, NULL, NULL); |
| 1082 | 1093 |
| 1083 // Resolve an AddressList with a IPv6 address first and then a IPv4 address. | 1094 // Resolve an AddressList with a IPv6 address first and then a IPv4 address. |
| 1084 host_resolver_->rules() | 1095 host_resolver_->rules() |
| 1085 ->AddIPLiteralRule("*", "2:abcd::3:4:ff,2.2.2.2", std::string()); | 1096 ->AddIPLiteralRule("*", "2:abcd::3:4:ff,2.2.2.2", std::string()); |
| 1086 | 1097 |
| 1087 TestCompletionCallback callback; | 1098 TestCompletionCallback callback; |
| 1088 ClientSocketHandle handle; | 1099 ClientSocketHandle handle; |
| 1089 // Enable TCP FastOpen in TransportSocketParams. | 1100 // Enable TCP FastOpen in TransportSocketParams. |
| 1090 scoped_refptr<TransportSocketParams> params = CreateParamsForTCPFastOpen(); | 1101 scoped_refptr<TransportSocketParams> params = CreateParamsForTCPFastOpen(); |
| 1091 handle.Init("a", params, LOW, ClientSocketPool::RespectLimits::ENABLED, | 1102 handle.Init("a", params, LOW, ClientSocketPool::RespectLimits::ENABLED, |
| 1092 callback.callback(), &pool, BoundNetLog()); | 1103 callback.callback(), &pool, BoundNetLog()); |
| 1093 EXPECT_EQ(OK, callback.WaitForResult()); | 1104 EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 1094 // Verify that the socket used is connected to the fallback IPv4 address. | 1105 // Verify that the socket used is connected to the fallback IPv4 address. |
| 1095 IPEndPoint endpoint; | 1106 IPEndPoint endpoint; |
| 1096 handle.socket()->GetPeerAddress(&endpoint); | 1107 handle.socket()->GetPeerAddress(&endpoint); |
| 1097 EXPECT_TRUE(endpoint.address().IsIPv4()); | 1108 EXPECT_TRUE(endpoint.address().IsIPv4()); |
| 1098 // Verify that TCP FastOpen was not turned on for the socket. | 1109 // Verify that TCP FastOpen was not turned on for the socket. |
| 1099 EXPECT_FALSE(socket_data_1.IsUsingTCPFastOpen()); | 1110 EXPECT_FALSE(socket_data_1.IsUsingTCPFastOpen()); |
| 1100 } | 1111 } |
| 1101 | 1112 |
| 1102 // Test that if TCP FastOpen is enabled, it does not do anything when there | 1113 // Test that if TCP FastOpen is enabled, it does not do anything when there |
| 1103 // is a IPv6 address with fallback to an IPv4 address. This test tests the case | 1114 // is a IPv6 address with fallback to an IPv4 address. This test tests the case |
| (...skipping 11 matching lines...) Expand all Loading... |
| 1115 // Resolve an AddressList with a IPv6 address first and then a IPv4 address. | 1126 // Resolve an AddressList with a IPv6 address first and then a IPv4 address. |
| 1116 host_resolver_->rules() | 1127 host_resolver_->rules() |
| 1117 ->AddIPLiteralRule("*", "2:abcd::3:4:ff,2.2.2.2", std::string()); | 1128 ->AddIPLiteralRule("*", "2:abcd::3:4:ff,2.2.2.2", std::string()); |
| 1118 | 1129 |
| 1119 TestCompletionCallback callback; | 1130 TestCompletionCallback callback; |
| 1120 ClientSocketHandle handle; | 1131 ClientSocketHandle handle; |
| 1121 // Enable TCP FastOpen in TransportSocketParams. | 1132 // Enable TCP FastOpen in TransportSocketParams. |
| 1122 scoped_refptr<TransportSocketParams> params = CreateParamsForTCPFastOpen(); | 1133 scoped_refptr<TransportSocketParams> params = CreateParamsForTCPFastOpen(); |
| 1123 handle.Init("a", params, LOW, ClientSocketPool::RespectLimits::ENABLED, | 1134 handle.Init("a", params, LOW, ClientSocketPool::RespectLimits::ENABLED, |
| 1124 callback.callback(), &pool, BoundNetLog()); | 1135 callback.callback(), &pool, BoundNetLog()); |
| 1125 EXPECT_EQ(OK, callback.WaitForResult()); | 1136 EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 1126 IPEndPoint endpoint; | 1137 IPEndPoint endpoint; |
| 1127 handle.socket()->GetPeerAddress(&endpoint); | 1138 handle.socket()->GetPeerAddress(&endpoint); |
| 1128 // Verify that the socket used is connected to the IPv6 address. | 1139 // Verify that the socket used is connected to the IPv6 address. |
| 1129 EXPECT_TRUE(endpoint.address().IsIPv6()); | 1140 EXPECT_TRUE(endpoint.address().IsIPv6()); |
| 1130 // Verify that TCP FastOpen was not turned on for the socket. | 1141 // Verify that TCP FastOpen was not turned on for the socket. |
| 1131 EXPECT_FALSE(socket_data.IsUsingTCPFastOpen()); | 1142 EXPECT_FALSE(socket_data.IsUsingTCPFastOpen()); |
| 1132 } | 1143 } |
| 1133 | 1144 |
| 1134 } // namespace | 1145 } // namespace |
| 1135 | 1146 |
| 1136 } // namespace net | 1147 } // namespace net |
| OLD | NEW |