OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "net/socket/sctp_client_socket_pool.h" |
| 6 |
| 7 #include "base/callback.h" |
| 8 #include "base/compiler_specific.h" |
| 9 #include "base/message_loop.h" |
| 10 #include "base/threading/platform_thread.h" |
| 11 #include "net/base/mock_host_resolver.h" |
| 12 #include "net/base/net_errors.h" |
| 13 #include "net/base/test_completion_callback.h" |
| 14 #include "net/socket/client_socket.h" |
| 15 #include "net/socket/client_socket_factory.h" |
| 16 #include "net/socket/client_socket_handle.h" |
| 17 #include "net/socket/client_socket_pool_histograms.h" |
| 18 #include "net/socket/socket_test_util.h" |
| 19 #include "net/socket/ssl_host_info.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" |
| 21 |
| 22 namespace net { |
| 23 |
| 24 namespace { |
| 25 |
| 26 const int kMaxSockets = 32; |
| 27 const int kMaxSocketsPerGroup = 6; |
| 28 const net::RequestPriority kDefaultPriority = LOW; |
| 29 |
| 30 class MockClientSocket : public ClientSocket { |
| 31 public: |
| 32 MockClientSocket() : connected_(false) {} |
| 33 |
| 34 // ClientSocket methods: |
| 35 virtual int Connect(CompletionCallback* callback) { |
| 36 connected_ = true; |
| 37 return OK; |
| 38 } |
| 39 virtual void Disconnect() { |
| 40 connected_ = false; |
| 41 } |
| 42 virtual bool IsConnected() const { |
| 43 return connected_; |
| 44 } |
| 45 virtual bool IsConnectedAndIdle() const { |
| 46 return connected_; |
| 47 } |
| 48 virtual int GetPeerAddress(AddressList* address) const { |
| 49 return ERR_UNEXPECTED; |
| 50 } |
| 51 virtual const BoundNetLog& NetLog() const { |
| 52 return net_log_; |
| 53 } |
| 54 |
| 55 virtual void SetSubresourceSpeculation() {} |
| 56 virtual void SetOmniboxSpeculation() {} |
| 57 virtual bool WasEverUsed() const { return false; } |
| 58 virtual bool UsingTCPFastOpen() const { return false; } |
| 59 |
| 60 // Socket methods: |
| 61 virtual int Read(IOBuffer* buf, int buf_len, |
| 62 CompletionCallback* callback) { |
| 63 return ERR_FAILED; |
| 64 } |
| 65 virtual int Write(IOBuffer* buf, int buf_len, |
| 66 CompletionCallback* callback) { |
| 67 return ERR_FAILED; |
| 68 } |
| 69 virtual bool SetReceiveBufferSize(int32 size) { return true; } |
| 70 virtual bool SetSendBufferSize(int32 size) { return true; } |
| 71 |
| 72 private: |
| 73 bool connected_; |
| 74 BoundNetLog net_log_; |
| 75 }; |
| 76 |
| 77 class MockFailingClientSocket : public ClientSocket { |
| 78 public: |
| 79 MockFailingClientSocket() {} |
| 80 |
| 81 // ClientSocket methods: |
| 82 virtual int Connect(CompletionCallback* callback) { |
| 83 return ERR_CONNECTION_FAILED; |
| 84 } |
| 85 |
| 86 virtual void Disconnect() {} |
| 87 |
| 88 virtual bool IsConnected() const { |
| 89 return false; |
| 90 } |
| 91 virtual bool IsConnectedAndIdle() const { |
| 92 return false; |
| 93 } |
| 94 virtual int GetPeerAddress(AddressList* address) const { |
| 95 return ERR_UNEXPECTED; |
| 96 } |
| 97 virtual const BoundNetLog& NetLog() const { |
| 98 return net_log_; |
| 99 } |
| 100 |
| 101 virtual void SetSubresourceSpeculation() {} |
| 102 virtual void SetOmniboxSpeculation() {} |
| 103 virtual bool WasEverUsed() const { return false; } |
| 104 virtual bool UsingTCPFastOpen() const { return false; } |
| 105 |
| 106 // Socket methods: |
| 107 virtual int Read(IOBuffer* buf, int buf_len, |
| 108 CompletionCallback* callback) { |
| 109 return ERR_FAILED; |
| 110 } |
| 111 |
| 112 virtual int Write(IOBuffer* buf, int buf_len, |
| 113 CompletionCallback* callback) { |
| 114 return ERR_FAILED; |
| 115 } |
| 116 virtual bool SetReceiveBufferSize(int32 size) { return true; } |
| 117 virtual bool SetSendBufferSize(int32 size) { return true; } |
| 118 |
| 119 private: |
| 120 BoundNetLog net_log_; |
| 121 }; |
| 122 |
| 123 class MockPendingClientSocket : public ClientSocket { |
| 124 public: |
| 125 // |should_connect| indicates whether the socket should successfully complete |
| 126 // or fail. |
| 127 // |should_stall| indicates that this socket should never connect. |
| 128 // |delay_ms| is the delay, in milliseconds, before simulating a connect. |
| 129 MockPendingClientSocket(bool should_connect, bool should_stall, int delay_ms) |
| 130 : method_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), |
| 131 should_connect_(should_connect), |
| 132 should_stall_(should_stall), |
| 133 delay_ms_(delay_ms), |
| 134 is_connected_(false) {} |
| 135 |
| 136 // ClientSocket methods: |
| 137 virtual int Connect(CompletionCallback* callback) { |
| 138 MessageLoop::current()->PostDelayedTask( |
| 139 FROM_HERE, |
| 140 method_factory_.NewRunnableMethod( |
| 141 &MockPendingClientSocket::DoCallback, callback), delay_ms_); |
| 142 return ERR_IO_PENDING; |
| 143 } |
| 144 |
| 145 virtual void Disconnect() {} |
| 146 |
| 147 virtual bool IsConnected() const { |
| 148 return is_connected_; |
| 149 } |
| 150 virtual bool IsConnectedAndIdle() const { |
| 151 return is_connected_; |
| 152 } |
| 153 virtual int GetPeerAddress(AddressList* address) const { |
| 154 return ERR_UNEXPECTED; |
| 155 } |
| 156 virtual const BoundNetLog& NetLog() const { |
| 157 return net_log_; |
| 158 } |
| 159 |
| 160 virtual void SetSubresourceSpeculation() {} |
| 161 virtual void SetOmniboxSpeculation() {} |
| 162 virtual bool WasEverUsed() const { return false; } |
| 163 virtual bool UsingTCPFastOpen() const { return false; } |
| 164 |
| 165 // Socket methods: |
| 166 virtual int Read(IOBuffer* buf, int buf_len, |
| 167 CompletionCallback* callback) { |
| 168 return ERR_FAILED; |
| 169 } |
| 170 |
| 171 virtual int Write(IOBuffer* buf, int buf_len, |
| 172 CompletionCallback* callback) { |
| 173 return ERR_FAILED; |
| 174 } |
| 175 virtual bool SetReceiveBufferSize(int32 size) { return true; } |
| 176 virtual bool SetSendBufferSize(int32 size) { return true; } |
| 177 |
| 178 private: |
| 179 void DoCallback(CompletionCallback* callback) { |
| 180 if (should_stall_) |
| 181 return; |
| 182 |
| 183 if (should_connect_) { |
| 184 is_connected_ = true; |
| 185 callback->Run(OK); |
| 186 } else { |
| 187 is_connected_ = false; |
| 188 callback->Run(ERR_CONNECTION_FAILED); |
| 189 } |
| 190 } |
| 191 |
| 192 ScopedRunnableMethodFactory<MockPendingClientSocket> method_factory_; |
| 193 bool should_connect_; |
| 194 bool should_stall_; |
| 195 int delay_ms_; |
| 196 bool is_connected_; |
| 197 BoundNetLog net_log_; |
| 198 }; |
| 199 |
| 200 class MockClientSocketFactory : public ClientSocketFactory { |
| 201 public: |
| 202 enum ClientSocketType { |
| 203 MOCK_CLIENT_SOCKET, |
| 204 MOCK_FAILING_CLIENT_SOCKET, |
| 205 MOCK_PENDING_CLIENT_SOCKET, |
| 206 MOCK_PENDING_FAILING_CLIENT_SOCKET, |
| 207 // A delayed socket will pause before connecting through the message loop. |
| 208 MOCK_DELAYED_CLIENT_SOCKET, |
| 209 // A stalled socket that never connects at all. |
| 210 MOCK_STALLED_CLIENT_SOCKET, |
| 211 }; |
| 212 |
| 213 MockClientSocketFactory() |
| 214 : allocation_count_(0), client_socket_type_(MOCK_CLIENT_SOCKET), |
| 215 client_socket_types_(NULL), client_socket_index_(0), |
| 216 client_socket_index_max_(0) {} |
| 217 |
| 218 virtual ClientSocket* CreateSCTPClientSocket( |
| 219 const AddressList& addresses, |
| 220 NetLog* /* net_log */, |
| 221 const NetLog::Source& /* source */) { |
| 222 allocation_count_++; |
| 223 |
| 224 ClientSocketType type = client_socket_type_; |
| 225 if (client_socket_types_ && |
| 226 client_socket_index_ < client_socket_index_max_) { |
| 227 type = client_socket_types_[client_socket_index_++]; |
| 228 } |
| 229 |
| 230 switch (type) { |
| 231 case MOCK_CLIENT_SOCKET: |
| 232 return new MockClientSocket(); |
| 233 case MOCK_FAILING_CLIENT_SOCKET: |
| 234 return new MockFailingClientSocket(); |
| 235 case MOCK_PENDING_CLIENT_SOCKET: |
| 236 return new MockPendingClientSocket(true, false, 0); |
| 237 case MOCK_PENDING_FAILING_CLIENT_SOCKET: |
| 238 return new MockPendingClientSocket(false, false, 0); |
| 239 case MOCK_DELAYED_CLIENT_SOCKET: |
| 240 return new MockPendingClientSocket(true, false, |
| 241 ClientSocketPool::kMaxConnectRetryIntervalMs); |
| 242 case MOCK_STALLED_CLIENT_SOCKET: |
| 243 return new MockPendingClientSocket(true, true, 0); |
| 244 default: |
| 245 NOTREACHED(); |
| 246 return new MockClientSocket(); |
| 247 } |
| 248 } |
| 249 |
| 250 virtual ClientSocket* CreateTCPClientSocket( |
| 251 const AddressList& addresses, |
| 252 NetLog* /* net_log */, |
| 253 const NetLog::Source& /* source */) { |
| 254 NOTIMPLEMENTED(); |
| 255 return NULL; |
| 256 } |
| 257 |
| 258 virtual SSLClientSocket* CreateSSLClientSocket( |
| 259 ClientSocketHandle* transport_socket, |
| 260 const HostPortPair& host_and_port, |
| 261 const SSLConfig& ssl_config, |
| 262 SSLHostInfo* ssl_host_info, |
| 263 CertVerifier* cert_verifier, |
| 264 DnsCertProvenanceChecker* dns_cert_checker) { |
| 265 NOTIMPLEMENTED(); |
| 266 delete ssl_host_info; |
| 267 return NULL; |
| 268 } |
| 269 |
| 270 virtual void ClearSSLSessionCache() { |
| 271 NOTIMPLEMENTED(); |
| 272 } |
| 273 |
| 274 int allocation_count() const { return allocation_count_; } |
| 275 |
| 276 // Set the default ClientSocketType. |
| 277 void set_client_socket_type(ClientSocketType type) { |
| 278 client_socket_type_ = type; |
| 279 } |
| 280 |
| 281 // Set a list of ClientSocketTypes to be used. |
| 282 void set_client_socket_types(ClientSocketType* type_list, int num_types) { |
| 283 DCHECK_GT(num_types, 0); |
| 284 client_socket_types_ = type_list; |
| 285 client_socket_index_ = 0; |
| 286 client_socket_index_max_ = num_types; |
| 287 } |
| 288 |
| 289 private: |
| 290 int allocation_count_; |
| 291 ClientSocketType client_socket_type_; |
| 292 ClientSocketType* client_socket_types_; |
| 293 int client_socket_index_; |
| 294 int client_socket_index_max_; |
| 295 }; |
| 296 |
| 297 class SCTPClientSocketPoolTest : public testing::Test { |
| 298 protected: |
| 299 SCTPClientSocketPoolTest() |
| 300 : params_(new SCTPSocketParams(HostPortPair("www.google.com", 80), |
| 301 kDefaultPriority, GURL(), false, false)), |
| 302 low_params_(new SCTPSocketParams(HostPortPair("www.google.com", 80), |
| 303 LOW, GURL(), false, false)), |
| 304 histograms_(new ClientSocketPoolHistograms("SCTPUnitTest")), |
| 305 host_resolver_(new MockHostResolver), |
| 306 pool_(kMaxSockets, |
| 307 kMaxSocketsPerGroup, |
| 308 histograms_.get(), |
| 309 host_resolver_.get(), |
| 310 &client_socket_factory_, |
| 311 NULL) { |
| 312 } |
| 313 |
| 314 int StartRequest(const std::string& group_name, RequestPriority priority) { |
| 315 scoped_refptr<SCTPSocketParams> params(new SCTPSocketParams( |
| 316 HostPortPair("www.google.com", 80), MEDIUM, GURL(), false, false)); |
| 317 return test_base_.StartRequestUsingPool( |
| 318 &pool_, group_name, priority, params); |
| 319 } |
| 320 |
| 321 int GetOrderOfRequest(size_t index) { |
| 322 return test_base_.GetOrderOfRequest(index); |
| 323 } |
| 324 |
| 325 bool ReleaseOneConnection(ClientSocketPoolTest::KeepAlive keep_alive) { |
| 326 return test_base_.ReleaseOneConnection(keep_alive); |
| 327 } |
| 328 |
| 329 void ReleaseAllConnections(ClientSocketPoolTest::KeepAlive keep_alive) { |
| 330 test_base_.ReleaseAllConnections(keep_alive); |
| 331 } |
| 332 |
| 333 ScopedVector<TestSocketRequest>* requests() { return test_base_.requests(); } |
| 334 size_t completion_count() const { return test_base_.completion_count(); } |
| 335 |
| 336 scoped_refptr<SCTPSocketParams> params_; |
| 337 scoped_refptr<SCTPSocketParams> low_params_; |
| 338 scoped_ptr<ClientSocketPoolHistograms> histograms_; |
| 339 scoped_ptr<MockHostResolver> host_resolver_; |
| 340 MockClientSocketFactory client_socket_factory_; |
| 341 SCTPClientSocketPool pool_; |
| 342 ClientSocketPoolTest test_base_; |
| 343 }; |
| 344 |
| 345 TEST_F(SCTPClientSocketPoolTest, Basic) { |
| 346 TestCompletionCallback callback; |
| 347 ClientSocketHandle handle; |
| 348 int rv = handle.Init("a", low_params_, LOW, &callback, &pool_, BoundNetLog()); |
| 349 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 350 EXPECT_FALSE(handle.is_initialized()); |
| 351 EXPECT_FALSE(handle.socket()); |
| 352 |
| 353 EXPECT_EQ(OK, callback.WaitForResult()); |
| 354 EXPECT_TRUE(handle.is_initialized()); |
| 355 EXPECT_TRUE(handle.socket()); |
| 356 |
| 357 handle.Reset(); |
| 358 } |
| 359 |
| 360 TEST_F(SCTPClientSocketPoolTest, InitHostResolutionFailure) { |
| 361 host_resolver_->rules()->AddSimulatedFailure("unresolvable.host.name"); |
| 362 TestCompletionCallback callback; |
| 363 ClientSocketHandle handle; |
| 364 HostPortPair host_port_pair("unresolvable.host.name", 80); |
| 365 scoped_refptr<SCTPSocketParams> dest(new SCTPSocketParams( |
| 366 host_port_pair, kDefaultPriority, GURL(), false, false)); |
| 367 EXPECT_EQ(ERR_IO_PENDING, |
| 368 handle.Init("a", dest, kDefaultPriority, &callback, &pool_, |
| 369 BoundNetLog())); |
| 370 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, callback.WaitForResult()); |
| 371 } |
| 372 |
| 373 TEST_F(SCTPClientSocketPoolTest, InitConnectionFailure) { |
| 374 client_socket_factory_.set_client_socket_type( |
| 375 MockClientSocketFactory::MOCK_FAILING_CLIENT_SOCKET); |
| 376 TestCompletionCallback callback; |
| 377 ClientSocketHandle handle; |
| 378 EXPECT_EQ(ERR_IO_PENDING, handle.Init("a", params_, kDefaultPriority, |
| 379 &callback, &pool_, BoundNetLog())); |
| 380 EXPECT_EQ(ERR_CONNECTION_FAILED, callback.WaitForResult()); |
| 381 |
| 382 // Make the host resolutions complete synchronously this time. |
| 383 host_resolver_->set_synchronous_mode(true); |
| 384 EXPECT_EQ(ERR_CONNECTION_FAILED, handle.Init("a", params_, |
| 385 kDefaultPriority, &callback, |
| 386 &pool_, BoundNetLog())); |
| 387 } |
| 388 |
| 389 TEST_F(SCTPClientSocketPoolTest, PendingRequests) { |
| 390 // First request finishes asynchronously. |
| 391 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); |
| 392 EXPECT_EQ(OK, (*requests())[0]->WaitForResult()); |
| 393 |
| 394 // Make all subsequent host resolutions complete synchronously. |
| 395 host_resolver_->set_synchronous_mode(true); |
| 396 |
| 397 // Rest of them finish synchronously, until we reach the per-group limit. |
| 398 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); |
| 399 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); |
| 400 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); |
| 401 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); |
| 402 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); |
| 403 |
| 404 // The rest are pending since we've used all active sockets. |
| 405 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", HIGHEST)); |
| 406 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOWEST)); |
| 407 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOWEST)); |
| 408 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", MEDIUM)); |
| 409 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOW)); |
| 410 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", HIGHEST)); |
| 411 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOWEST)); |
| 412 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", MEDIUM)); |
| 413 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", MEDIUM)); |
| 414 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", HIGHEST)); |
| 415 |
| 416 ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE); |
| 417 |
| 418 EXPECT_EQ(kMaxSocketsPerGroup, client_socket_factory_.allocation_count()); |
| 419 |
| 420 // One initial asynchronous request and then 10 pending requests. |
| 421 EXPECT_EQ(11U, completion_count()); |
| 422 |
| 423 // First part of requests, all with the same priority, finishes in FIFO order. |
| 424 EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 425 EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 426 EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 427 EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 428 EXPECT_EQ(5, GetOrderOfRequest(5)); |
| 429 EXPECT_EQ(6, GetOrderOfRequest(6)); |
| 430 |
| 431 // Make sure that rest of the requests complete in the order of priority. |
| 432 EXPECT_EQ(7, GetOrderOfRequest(7)); |
| 433 EXPECT_EQ(14, GetOrderOfRequest(8)); |
| 434 EXPECT_EQ(15, GetOrderOfRequest(9)); |
| 435 EXPECT_EQ(10, GetOrderOfRequest(10)); |
| 436 EXPECT_EQ(13, GetOrderOfRequest(11)); |
| 437 EXPECT_EQ(8, GetOrderOfRequest(12)); |
| 438 EXPECT_EQ(16, GetOrderOfRequest(13)); |
| 439 EXPECT_EQ(11, GetOrderOfRequest(14)); |
| 440 EXPECT_EQ(12, GetOrderOfRequest(15)); |
| 441 EXPECT_EQ(9, GetOrderOfRequest(16)); |
| 442 |
| 443 // Make sure we test order of all requests made. |
| 444 EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(17)); |
| 445 } |
| 446 |
| 447 TEST_F(SCTPClientSocketPoolTest, PendingRequests_NoKeepAlive) { |
| 448 // First request finishes asynchronously. |
| 449 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); |
| 450 EXPECT_EQ(OK, (*requests())[0]->WaitForResult()); |
| 451 |
| 452 // Make all subsequent host resolutions complete synchronously. |
| 453 host_resolver_->set_synchronous_mode(true); |
| 454 |
| 455 // Rest of them finish synchronously, until we reach the per-group limit. |
| 456 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); |
| 457 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); |
| 458 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); |
| 459 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); |
| 460 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); |
| 461 |
| 462 // The rest are pending since we've used all active sockets. |
| 463 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); |
| 464 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); |
| 465 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); |
| 466 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); |
| 467 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); |
| 468 |
| 469 ReleaseAllConnections(ClientSocketPoolTest::NO_KEEP_ALIVE); |
| 470 |
| 471 // The pending requests should finish successfully. |
| 472 EXPECT_EQ(OK, (*requests())[6]->WaitForResult()); |
| 473 EXPECT_EQ(OK, (*requests())[7]->WaitForResult()); |
| 474 EXPECT_EQ(OK, (*requests())[8]->WaitForResult()); |
| 475 EXPECT_EQ(OK, (*requests())[9]->WaitForResult()); |
| 476 EXPECT_EQ(OK, (*requests())[10]->WaitForResult()); |
| 477 |
| 478 EXPECT_EQ(static_cast<int>(requests()->size()), |
| 479 client_socket_factory_.allocation_count()); |
| 480 |
| 481 // First asynchronous request, and then last 5 pending requests. |
| 482 EXPECT_EQ(6U, completion_count()); |
| 483 } |
| 484 |
| 485 // This test will start up a RequestSocket() and then immediately Cancel() it. |
| 486 // The pending host resolution will eventually complete, and destroy the |
| 487 // ClientSocketPool which will crash if the group was not cleared properly. |
| 488 TEST_F(SCTPClientSocketPoolTest, CancelRequestClearGroup) { |
| 489 TestCompletionCallback callback; |
| 490 ClientSocketHandle handle; |
| 491 EXPECT_EQ(ERR_IO_PENDING, handle.Init("a", params_, kDefaultPriority, |
| 492 &callback, &pool_, BoundNetLog())); |
| 493 handle.Reset(); |
| 494 } |
| 495 |
| 496 TEST_F(SCTPClientSocketPoolTest, TwoRequestsCancelOne) { |
| 497 ClientSocketHandle handle; |
| 498 TestCompletionCallback callback; |
| 499 ClientSocketHandle handle2; |
| 500 TestCompletionCallback callback2; |
| 501 |
| 502 EXPECT_EQ(ERR_IO_PENDING, handle.Init("a", params_, kDefaultPriority, |
| 503 &callback, &pool_, BoundNetLog())); |
| 504 EXPECT_EQ(ERR_IO_PENDING, handle2.Init("a", params_, kDefaultPriority, |
| 505 &callback2, &pool_, BoundNetLog())); |
| 506 |
| 507 handle.Reset(); |
| 508 |
| 509 EXPECT_EQ(OK, callback2.WaitForResult()); |
| 510 handle2.Reset(); |
| 511 } |
| 512 |
| 513 TEST_F(SCTPClientSocketPoolTest, ConnectCancelConnect) { |
| 514 client_socket_factory_.set_client_socket_type( |
| 515 MockClientSocketFactory::MOCK_PENDING_CLIENT_SOCKET); |
| 516 ClientSocketHandle handle; |
| 517 TestCompletionCallback callback; |
| 518 EXPECT_EQ(ERR_IO_PENDING, handle.Init("a", params_, kDefaultPriority, |
| 519 &callback, &pool_, BoundNetLog())); |
| 520 |
| 521 handle.Reset(); |
| 522 |
| 523 TestCompletionCallback callback2; |
| 524 EXPECT_EQ(ERR_IO_PENDING, handle.Init("a", params_, kDefaultPriority, |
| 525 &callback2, &pool_, BoundNetLog())); |
| 526 |
| 527 host_resolver_->set_synchronous_mode(true); |
| 528 // At this point, handle has two ConnectingSockets out for it. Due to the |
| 529 // setting the mock resolver into synchronous mode, the host resolution for |
| 530 // both will return in the same loop of the MessageLoop. The client socket |
| 531 // is a pending socket, so the Connect() will asynchronously complete on the |
| 532 // next loop of the MessageLoop. That means that the first |
| 533 // ConnectingSocket will enter OnIOComplete, and then the second one will. |
| 534 // If the first one is not cancelled, it will advance the load state, and |
| 535 // then the second one will crash. |
| 536 |
| 537 EXPECT_EQ(OK, callback2.WaitForResult()); |
| 538 EXPECT_FALSE(callback.have_result()); |
| 539 |
| 540 handle.Reset(); |
| 541 } |
| 542 |
| 543 TEST_F(SCTPClientSocketPoolTest, CancelRequest) { |
| 544 // First request finishes asynchronously. |
| 545 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); |
| 546 EXPECT_EQ(OK, (*requests())[0]->WaitForResult()); |
| 547 |
| 548 // Make all subsequent host resolutions complete synchronously. |
| 549 host_resolver_->set_synchronous_mode(true); |
| 550 |
| 551 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); |
| 552 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); |
| 553 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); |
| 554 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); |
| 555 EXPECT_EQ(OK, StartRequest("a", kDefaultPriority)); |
| 556 |
| 557 // Reached per-group limit, queue up requests. |
| 558 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOWEST)); |
| 559 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", HIGHEST)); |
| 560 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", HIGHEST)); |
| 561 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", MEDIUM)); |
| 562 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", MEDIUM)); |
| 563 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOW)); |
| 564 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", HIGHEST)); |
| 565 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOW)); |
| 566 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOW)); |
| 567 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", LOWEST)); |
| 568 |
| 569 // Cancel a request. |
| 570 size_t index_to_cancel = kMaxSocketsPerGroup + 2; |
| 571 EXPECT_FALSE((*requests())[index_to_cancel]->handle()->is_initialized()); |
| 572 (*requests())[index_to_cancel]->handle()->Reset(); |
| 573 |
| 574 ReleaseAllConnections(ClientSocketPoolTest::KEEP_ALIVE); |
| 575 |
| 576 EXPECT_EQ(kMaxSocketsPerGroup, |
| 577 client_socket_factory_.allocation_count()); |
| 578 EXPECT_EQ(requests()->size() - kMaxSocketsPerGroup, completion_count()); |
| 579 |
| 580 EXPECT_EQ(1, GetOrderOfRequest(1)); |
| 581 EXPECT_EQ(2, GetOrderOfRequest(2)); |
| 582 EXPECT_EQ(3, GetOrderOfRequest(3)); |
| 583 EXPECT_EQ(4, GetOrderOfRequest(4)); |
| 584 EXPECT_EQ(5, GetOrderOfRequest(5)); |
| 585 EXPECT_EQ(6, GetOrderOfRequest(6)); |
| 586 EXPECT_EQ(14, GetOrderOfRequest(7)); |
| 587 EXPECT_EQ(7, GetOrderOfRequest(8)); |
| 588 EXPECT_EQ(ClientSocketPoolTest::kRequestNotFound, |
| 589 GetOrderOfRequest(9)); // Canceled request. |
| 590 EXPECT_EQ(9, GetOrderOfRequest(10)); |
| 591 EXPECT_EQ(10, GetOrderOfRequest(11)); |
| 592 EXPECT_EQ(11, GetOrderOfRequest(12)); |
| 593 EXPECT_EQ(8, GetOrderOfRequest(13)); |
| 594 EXPECT_EQ(12, GetOrderOfRequest(14)); |
| 595 EXPECT_EQ(13, GetOrderOfRequest(15)); |
| 596 EXPECT_EQ(15, GetOrderOfRequest(16)); |
| 597 |
| 598 // Make sure we test order of all requests made. |
| 599 EXPECT_EQ(ClientSocketPoolTest::kIndexOutOfBounds, GetOrderOfRequest(17)); |
| 600 } |
| 601 |
| 602 class RequestSocketCallback : public CallbackRunner< Tuple1<int> > { |
| 603 public: |
| 604 RequestSocketCallback(ClientSocketHandle* handle, SCTPClientSocketPool* pool) |
| 605 : handle_(handle), |
| 606 pool_(pool), |
| 607 within_callback_(false) {} |
| 608 |
| 609 virtual void RunWithParams(const Tuple1<int>& params) { |
| 610 callback_.RunWithParams(params); |
| 611 ASSERT_EQ(OK, params.a); |
| 612 |
| 613 if (!within_callback_) { |
| 614 // Don't allow reuse of the socket. Disconnect it and then release it and |
| 615 // run through the MessageLoop once to get it completely released. |
| 616 handle_->socket()->Disconnect(); |
| 617 handle_->Reset(); |
| 618 { |
| 619 MessageLoop::ScopedNestableTaskAllower nestable( |
| 620 MessageLoop::current()); |
| 621 MessageLoop::current()->RunAllPending(); |
| 622 } |
| 623 within_callback_ = true; |
| 624 scoped_refptr<SCTPSocketParams> dest(new SCTPSocketParams( |
| 625 HostPortPair("www.google.com", 80), LOWEST, GURL(), false, false)); |
| 626 int rv = handle_->Init("a", dest, LOWEST, this, pool_, BoundNetLog()); |
| 627 EXPECT_EQ(OK, rv); |
| 628 } |
| 629 } |
| 630 |
| 631 int WaitForResult() { |
| 632 return callback_.WaitForResult(); |
| 633 } |
| 634 |
| 635 private: |
| 636 ClientSocketHandle* const handle_; |
| 637 SCTPClientSocketPool* const pool_; |
| 638 bool within_callback_; |
| 639 TestCompletionCallback callback_; |
| 640 }; |
| 641 |
| 642 TEST_F(SCTPClientSocketPoolTest, RequestTwice) { |
| 643 ClientSocketHandle handle; |
| 644 RequestSocketCallback callback(&handle, &pool_); |
| 645 scoped_refptr<SCTPSocketParams> dest(new SCTPSocketParams( |
| 646 HostPortPair("www.google.com", 80), LOWEST, GURL(), false, false)); |
| 647 int rv = handle.Init("a", dest, LOWEST, &callback, &pool_, |
| 648 BoundNetLog()); |
| 649 ASSERT_EQ(ERR_IO_PENDING, rv); |
| 650 |
| 651 // The callback is going to request "www.google.com". We want it to complete |
| 652 // synchronously this time. |
| 653 host_resolver_->set_synchronous_mode(true); |
| 654 |
| 655 EXPECT_EQ(OK, callback.WaitForResult()); |
| 656 |
| 657 handle.Reset(); |
| 658 } |
| 659 |
| 660 // Make sure that pending requests get serviced after active requests get |
| 661 // cancelled. |
| 662 TEST_F(SCTPClientSocketPoolTest, CancelActiveRequestWithPendingRequests) { |
| 663 client_socket_factory_.set_client_socket_type( |
| 664 MockClientSocketFactory::MOCK_PENDING_CLIENT_SOCKET); |
| 665 |
| 666 // Queue up all the requests |
| 667 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); |
| 668 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); |
| 669 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); |
| 670 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); |
| 671 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); |
| 672 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); |
| 673 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); |
| 674 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); |
| 675 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); |
| 676 |
| 677 // Now, kMaxSocketsPerGroup requests should be active. Let's cancel them. |
| 678 ASSERT_LE(kMaxSocketsPerGroup, static_cast<int>(requests()->size())); |
| 679 for (int i = 0; i < kMaxSocketsPerGroup; i++) |
| 680 (*requests())[i]->handle()->Reset(); |
| 681 |
| 682 // Let's wait for the rest to complete now. |
| 683 for (size_t i = kMaxSocketsPerGroup; i < requests()->size(); ++i) { |
| 684 EXPECT_EQ(OK, (*requests())[i]->WaitForResult()); |
| 685 (*requests())[i]->handle()->Reset(); |
| 686 } |
| 687 |
| 688 EXPECT_EQ(requests()->size() - kMaxSocketsPerGroup, completion_count()); |
| 689 } |
| 690 |
| 691 // Make sure that pending requests get serviced after active requests fail. |
| 692 TEST_F(SCTPClientSocketPoolTest, FailingActiveRequestWithPendingRequests) { |
| 693 client_socket_factory_.set_client_socket_type( |
| 694 MockClientSocketFactory::MOCK_PENDING_FAILING_CLIENT_SOCKET); |
| 695 |
| 696 const int kNumRequests = 2 * kMaxSocketsPerGroup + 1; |
| 697 ASSERT_LE(kNumRequests, kMaxSockets); // Otherwise the test will hang. |
| 698 |
| 699 // Queue up all the requests |
| 700 for (int i = 0; i < kNumRequests; i++) |
| 701 EXPECT_EQ(ERR_IO_PENDING, StartRequest("a", kDefaultPriority)); |
| 702 |
| 703 for (int i = 0; i < kNumRequests; i++) |
| 704 EXPECT_EQ(ERR_CONNECTION_FAILED, (*requests())[i]->WaitForResult()); |
| 705 } |
| 706 |
| 707 TEST_F(SCTPClientSocketPoolTest, ResetIdleSocketsOnIPAddressChange) { |
| 708 TestCompletionCallback callback; |
| 709 ClientSocketHandle handle; |
| 710 int rv = handle.Init("a", low_params_, LOW, &callback, &pool_, BoundNetLog()); |
| 711 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 712 EXPECT_FALSE(handle.is_initialized()); |
| 713 EXPECT_FALSE(handle.socket()); |
| 714 |
| 715 EXPECT_EQ(OK, callback.WaitForResult()); |
| 716 EXPECT_TRUE(handle.is_initialized()); |
| 717 EXPECT_TRUE(handle.socket()); |
| 718 |
| 719 handle.Reset(); |
| 720 |
| 721 // Need to run all pending to release the socket back to the pool. |
| 722 MessageLoop::current()->RunAllPending(); |
| 723 |
| 724 // Now we should have 1 idle socket. |
| 725 EXPECT_EQ(1, pool_.IdleSocketCount()); |
| 726 |
| 727 // After an IP address change, we should have 0 idle sockets. |
| 728 NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests(); |
| 729 MessageLoop::current()->RunAllPending(); // Notification happens async. |
| 730 |
| 731 EXPECT_EQ(0, pool_.IdleSocketCount()); |
| 732 } |
| 733 |
| 734 TEST_F(SCTPClientSocketPoolTest, BackupSocketConnect) { |
| 735 // Case 1 tests the first socket stalling, and the backup connecting. |
| 736 MockClientSocketFactory::ClientSocketType case1_types[] = { |
| 737 // The first socket will not connect. |
| 738 MockClientSocketFactory::MOCK_STALLED_CLIENT_SOCKET, |
| 739 // The second socket will connect more quickly. |
| 740 MockClientSocketFactory::MOCK_CLIENT_SOCKET |
| 741 }; |
| 742 |
| 743 // Case 2 tests the first socket being slow, so that we start the |
| 744 // second connect, but the second connect stalls, and we still |
| 745 // complete the first. |
| 746 MockClientSocketFactory::ClientSocketType case2_types[] = { |
| 747 // The first socket will connect, although delayed. |
| 748 MockClientSocketFactory::MOCK_DELAYED_CLIENT_SOCKET, |
| 749 // The second socket will not connect. |
| 750 MockClientSocketFactory::MOCK_STALLED_CLIENT_SOCKET |
| 751 }; |
| 752 |
| 753 MockClientSocketFactory::ClientSocketType* cases[2] = { |
| 754 case1_types, |
| 755 case2_types |
| 756 }; |
| 757 |
| 758 for (size_t index = 0; index < arraysize(cases); ++index) { |
| 759 client_socket_factory_.set_client_socket_types(cases[index], 2); |
| 760 |
| 761 EXPECT_EQ(0, pool_.IdleSocketCount()); |
| 762 |
| 763 TestCompletionCallback callback; |
| 764 ClientSocketHandle handle; |
| 765 int rv = handle.Init("b", low_params_, LOW, &callback, &pool_, |
| 766 BoundNetLog()); |
| 767 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 768 EXPECT_FALSE(handle.is_initialized()); |
| 769 EXPECT_FALSE(handle.socket()); |
| 770 |
| 771 // Create the first socket, set the timer. |
| 772 MessageLoop::current()->RunAllPending(); |
| 773 |
| 774 // Wait for the backup socket timer to fire. |
| 775 base::PlatformThread::Sleep( |
| 776 ClientSocketPool::kMaxConnectRetryIntervalMs * 2); |
| 777 |
| 778 // Let the appropriate socket connect. |
| 779 MessageLoop::current()->RunAllPending(); |
| 780 |
| 781 EXPECT_EQ(OK, callback.WaitForResult()); |
| 782 EXPECT_TRUE(handle.is_initialized()); |
| 783 EXPECT_TRUE(handle.socket()); |
| 784 |
| 785 // One socket is stalled, the other is active. |
| 786 EXPECT_EQ(0, pool_.IdleSocketCount()); |
| 787 handle.Reset(); |
| 788 |
| 789 // Close all pending connect jobs and existing sockets. |
| 790 pool_.Flush(); |
| 791 } |
| 792 } |
| 793 |
| 794 // Test the case where a socket took long enough to start the creation |
| 795 // of the backup socket, but then we cancelled the request after that. |
| 796 TEST_F(SCTPClientSocketPoolTest, BackupSocketCancel) { |
| 797 client_socket_factory_.set_client_socket_type( |
| 798 MockClientSocketFactory::MOCK_STALLED_CLIENT_SOCKET); |
| 799 |
| 800 enum { CANCEL_BEFORE_WAIT, CANCEL_AFTER_WAIT }; |
| 801 |
| 802 for (int index = CANCEL_BEFORE_WAIT; index < CANCEL_AFTER_WAIT; ++index) { |
| 803 EXPECT_EQ(0, pool_.IdleSocketCount()); |
| 804 |
| 805 TestCompletionCallback callback; |
| 806 ClientSocketHandle handle; |
| 807 int rv = handle.Init("c", low_params_, LOW, &callback, &pool_, |
| 808 BoundNetLog()); |
| 809 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 810 EXPECT_FALSE(handle.is_initialized()); |
| 811 EXPECT_FALSE(handle.socket()); |
| 812 |
| 813 // Create the first socket, set the timer. |
| 814 MessageLoop::current()->RunAllPending(); |
| 815 |
| 816 if (index == CANCEL_AFTER_WAIT) { |
| 817 // Wait for the backup socket timer to fire. |
| 818 base::PlatformThread::Sleep(ClientSocketPool::kMaxConnectRetryIntervalMs); |
| 819 } |
| 820 |
| 821 // Let the appropriate socket connect. |
| 822 MessageLoop::current()->RunAllPending(); |
| 823 |
| 824 handle.Reset(); |
| 825 |
| 826 EXPECT_FALSE(callback.have_result()); |
| 827 EXPECT_FALSE(handle.is_initialized()); |
| 828 EXPECT_FALSE(handle.socket()); |
| 829 |
| 830 // One socket is stalled, the other is active. |
| 831 EXPECT_EQ(0, pool_.IdleSocketCount()); |
| 832 } |
| 833 } |
| 834 |
| 835 // Test the case where a socket took long enough to start the creation |
| 836 // of the backup socket and never completes, and then the backup |
| 837 // connection fails. |
| 838 TEST_F(SCTPClientSocketPoolTest, BackupSocketFailAfterStall) { |
| 839 MockClientSocketFactory::ClientSocketType case_types[] = { |
| 840 // The first socket will not connect. |
| 841 MockClientSocketFactory::MOCK_STALLED_CLIENT_SOCKET, |
| 842 // The second socket will fail immediately. |
| 843 MockClientSocketFactory::MOCK_FAILING_CLIENT_SOCKET |
| 844 }; |
| 845 |
| 846 client_socket_factory_.set_client_socket_types(case_types, 2); |
| 847 |
| 848 EXPECT_EQ(0, pool_.IdleSocketCount()); |
| 849 |
| 850 TestCompletionCallback callback; |
| 851 ClientSocketHandle handle; |
| 852 int rv = handle.Init("b", low_params_, LOW, &callback, &pool_, BoundNetLog()); |
| 853 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 854 EXPECT_FALSE(handle.is_initialized()); |
| 855 EXPECT_FALSE(handle.socket()); |
| 856 |
| 857 // Create the first socket, set the timer. |
| 858 MessageLoop::current()->RunAllPending(); |
| 859 |
| 860 // Wait for the backup socket timer to fire. |
| 861 base::PlatformThread::Sleep(ClientSocketPool::kMaxConnectRetryIntervalMs); |
| 862 |
| 863 // Let the second connect be synchronous. Otherwise, the emulated |
| 864 // host resolution takes an extra trip through the message loop. |
| 865 host_resolver_->set_synchronous_mode(true); |
| 866 |
| 867 // Let the appropriate socket connect. |
| 868 MessageLoop::current()->RunAllPending(); |
| 869 |
| 870 EXPECT_EQ(ERR_CONNECTION_FAILED, callback.WaitForResult()); |
| 871 EXPECT_FALSE(handle.is_initialized()); |
| 872 EXPECT_FALSE(handle.socket()); |
| 873 EXPECT_EQ(0, pool_.IdleSocketCount()); |
| 874 handle.Reset(); |
| 875 |
| 876 // Reset for the next case. |
| 877 host_resolver_->set_synchronous_mode(false); |
| 878 } |
| 879 |
| 880 // Test the case where a socket took long enough to start the creation |
| 881 // of the backup socket and eventually completes, but the backup socket |
| 882 // fails. |
| 883 TEST_F(SCTPClientSocketPoolTest, BackupSocketFailAfterDelay) { |
| 884 MockClientSocketFactory::ClientSocketType case_types[] = { |
| 885 // The first socket will connect, although delayed. |
| 886 MockClientSocketFactory::MOCK_DELAYED_CLIENT_SOCKET, |
| 887 // The second socket will not connect. |
| 888 MockClientSocketFactory::MOCK_FAILING_CLIENT_SOCKET |
| 889 }; |
| 890 |
| 891 client_socket_factory_.set_client_socket_types(case_types, 2); |
| 892 |
| 893 EXPECT_EQ(0, pool_.IdleSocketCount()); |
| 894 |
| 895 TestCompletionCallback callback; |
| 896 ClientSocketHandle handle; |
| 897 int rv = handle.Init("b", low_params_, LOW, &callback, &pool_, BoundNetLog()); |
| 898 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 899 EXPECT_FALSE(handle.is_initialized()); |
| 900 EXPECT_FALSE(handle.socket()); |
| 901 |
| 902 // Create the first socket, set the timer. |
| 903 MessageLoop::current()->RunAllPending(); |
| 904 |
| 905 // Wait for the backup socket timer to fire. |
| 906 base::PlatformThread::Sleep(ClientSocketPool::kMaxConnectRetryIntervalMs); |
| 907 |
| 908 // Let the second connect be synchronous. Otherwise, the emulated |
| 909 // host resolution takes an extra trip through the message loop. |
| 910 host_resolver_->set_synchronous_mode(true); |
| 911 |
| 912 // Let the appropriate socket connect. |
| 913 MessageLoop::current()->RunAllPending(); |
| 914 |
| 915 EXPECT_EQ(ERR_CONNECTION_FAILED, callback.WaitForResult()); |
| 916 EXPECT_FALSE(handle.is_initialized()); |
| 917 EXPECT_FALSE(handle.socket()); |
| 918 handle.Reset(); |
| 919 |
| 920 // Reset for the next case. |
| 921 host_resolver_->set_synchronous_mode(false); |
| 922 } |
| 923 |
| 924 } // namespace |
| 925 |
| 926 } // namespace net |
OLD | NEW |