OLD | NEW |
---|---|
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 "google_apis/gcm/engine/connection_factory_impl.h" | 5 #include "google_apis/gcm/engine/connection_factory_impl.h" |
6 | 6 |
7 #include <cmath> | 7 #include <cmath> |
8 | 8 |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
(...skipping 369 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
380 EXPECT_GE((retry_time - connect_time).InMilliseconds(), | 380 EXPECT_GE((retry_time - connect_time).InMilliseconds(), |
381 CalculateBackoff(kNumAttempts)); | 381 CalculateBackoff(kNumAttempts)); |
382 | 382 |
383 factory()->SetConnectResult(net::OK); | 383 factory()->SetConnectResult(net::OK); |
384 WaitForConnections(); | 384 WaitForConnections(); |
385 EXPECT_TRUE(factory()->NextRetryAttempt().is_null()); | 385 EXPECT_TRUE(factory()->NextRetryAttempt().is_null()); |
386 EXPECT_TRUE(factory()->IsEndpointReachable()); | 386 EXPECT_TRUE(factory()->IsEndpointReachable()); |
387 EXPECT_TRUE(connected_server().is_valid()); | 387 EXPECT_TRUE(connected_server().is_valid()); |
388 } | 388 } |
389 | 389 |
390 // IP events should trigger canary connections. | 390 // Network change events should trigger canary connections. |
391 TEST_F(ConnectionFactoryImplTest, FailThenIPEvent) { | 391 TEST_F(ConnectionFactoryImplTest, FailThenNetworkChangeEvent) { |
392 factory()->SetConnectResult(net::ERR_CONNECTION_FAILED); | 392 factory()->SetConnectResult(net::ERR_CONNECTION_FAILED); |
393 factory()->Connect(); | 393 factory()->Connect(); |
394 WaitForConnections(); | 394 WaitForConnections(); |
395 base::TimeTicks initial_backoff = factory()->NextRetryAttempt(); | 395 base::TimeTicks initial_backoff = factory()->NextRetryAttempt(); |
396 EXPECT_FALSE(initial_backoff.is_null()); | 396 EXPECT_FALSE(initial_backoff.is_null()); |
397 | 397 |
398 factory()->SetConnectResult(net::ERR_FAILED); | 398 factory()->SetConnectResult(net::ERR_FAILED); |
399 factory()->OnIPAddressChanged(); | 399 factory()->OnNetworkChanged(net::NetworkChangeNotifier::CONNECTION_WIFI); |
400 WaitForConnections(); | 400 WaitForConnections(); |
401 | 401 |
402 // Backoff should increase. | 402 // Backoff should increase. |
403 base::TimeTicks next_backoff = factory()->NextRetryAttempt(); | |
404 EXPECT_GT(next_backoff, initial_backoff); | |
405 EXPECT_FALSE(factory()->IsEndpointReachable()); | |
406 } | |
407 | |
408 // Connection type events should trigger canary connections. | |
409 TEST_F(ConnectionFactoryImplTest, FailThenConnectionTypeEvent) { | |
410 factory()->SetConnectResult(net::ERR_CONNECTION_FAILED); | |
411 factory()->Connect(); | |
412 WaitForConnections(); | |
413 base::TimeTicks initial_backoff = factory()->NextRetryAttempt(); | |
414 EXPECT_FALSE(initial_backoff.is_null()); | |
415 | |
416 factory()->SetConnectResult(net::ERR_FAILED); | |
417 factory()->OnConnectionTypeChanged( | |
418 net::NetworkChangeNotifier::CONNECTION_WIFI); | |
419 WaitForConnections(); | |
420 | |
421 // Backoff should increase. | |
422 base::TimeTicks next_backoff = factory()->NextRetryAttempt(); | 403 base::TimeTicks next_backoff = factory()->NextRetryAttempt(); |
423 EXPECT_GT(next_backoff, initial_backoff); | 404 EXPECT_GT(next_backoff, initial_backoff); |
424 EXPECT_FALSE(factory()->IsEndpointReachable()); | 405 EXPECT_FALSE(factory()->IsEndpointReachable()); |
425 } | 406 } |
426 | 407 |
427 // Verify that we reconnect even if a canary succeeded then disconnected while | 408 // Verify that we reconnect even if a canary succeeded then disconnected while |
428 // a backoff was pending. | 409 // a backoff was pending. |
429 TEST_F(ConnectionFactoryImplTest, CanarySucceedsThenDisconnects) { | 410 TEST_F(ConnectionFactoryImplTest, CanarySucceedsThenDisconnects) { |
430 factory()->SetConnectResult(net::ERR_CONNECTION_FAILED); | 411 factory()->SetConnectResult(net::ERR_CONNECTION_FAILED); |
431 factory()->Connect(); | 412 factory()->Connect(); |
432 WaitForConnections(); | 413 WaitForConnections(); |
433 base::TimeTicks initial_backoff = factory()->NextRetryAttempt(); | 414 base::TimeTicks initial_backoff = factory()->NextRetryAttempt(); |
434 EXPECT_FALSE(initial_backoff.is_null()); | 415 EXPECT_FALSE(initial_backoff.is_null()); |
435 | 416 |
436 factory()->SetConnectResult(net::OK); | 417 factory()->SetConnectResult(net::OK); |
437 factory()->OnConnectionTypeChanged( | 418 factory()->OnNetworkChanged(net::NetworkChangeNotifier::CONNECTION_WIFI); |
fgorski
2014/06/19 20:01:49
nit: can we try other stuff than wifi here? CONNEC
Nicolas Zea
2014/06/19 21:50:04
Well, the code doesn't care what type of connectio
| |
438 net::NetworkChangeNotifier::CONNECTION_WIFI); | |
439 WaitForConnections(); | 419 WaitForConnections(); |
440 EXPECT_TRUE(factory()->IsEndpointReachable()); | 420 EXPECT_TRUE(factory()->IsEndpointReachable()); |
441 EXPECT_TRUE(connected_server().is_valid()); | 421 EXPECT_TRUE(connected_server().is_valid()); |
442 | 422 |
443 factory()->SetConnectResult(net::OK); | 423 factory()->SetConnectResult(net::OK); |
444 factory()->SignalConnectionReset(ConnectionFactory::SOCKET_FAILURE); | 424 factory()->SignalConnectionReset(ConnectionFactory::SOCKET_FAILURE); |
445 EXPECT_FALSE(factory()->IsEndpointReachable()); | 425 EXPECT_FALSE(factory()->IsEndpointReachable()); |
446 EXPECT_FALSE(connected_server().is_valid()); | 426 EXPECT_FALSE(connected_server().is_valid()); |
447 WaitForConnections(); | 427 WaitForConnections(); |
448 EXPECT_TRUE(factory()->IsEndpointReachable()); | 428 EXPECT_TRUE(factory()->IsEndpointReachable()); |
449 EXPECT_TRUE(connected_server().is_valid()); | 429 EXPECT_TRUE(connected_server().is_valid()); |
450 } | 430 } |
451 | 431 |
452 // Verify that if a canary connects, but hasn't finished the handshake, a | 432 // Verify that if a canary connects, but hasn't finished the handshake, a |
453 // pending backoff attempt doesn't interrupt the connection. | 433 // pending backoff attempt doesn't interrupt the connection. |
454 TEST_F(ConnectionFactoryImplTest, CanarySucceedsRetryDuringLogin) { | 434 TEST_F(ConnectionFactoryImplTest, CanarySucceedsRetryDuringLogin) { |
455 factory()->SetConnectResult(net::ERR_CONNECTION_FAILED); | 435 factory()->SetConnectResult(net::ERR_CONNECTION_FAILED); |
456 factory()->Connect(); | 436 factory()->Connect(); |
457 WaitForConnections(); | 437 WaitForConnections(); |
458 base::TimeTicks initial_backoff = factory()->NextRetryAttempt(); | 438 base::TimeTicks initial_backoff = factory()->NextRetryAttempt(); |
459 EXPECT_FALSE(initial_backoff.is_null()); | 439 EXPECT_FALSE(initial_backoff.is_null()); |
460 | 440 |
461 factory()->SetDelayLogin(true); | 441 factory()->SetDelayLogin(true); |
462 factory()->SetConnectResult(net::OK); | 442 factory()->SetConnectResult(net::OK); |
463 factory()->OnConnectionTypeChanged( | 443 factory()->OnNetworkChanged(net::NetworkChangeNotifier::CONNECTION_WIFI); |
464 net::NetworkChangeNotifier::CONNECTION_WIFI); | |
465 WaitForConnections(); | 444 WaitForConnections(); |
466 EXPECT_FALSE(factory()->IsEndpointReachable()); | 445 EXPECT_FALSE(factory()->IsEndpointReachable()); |
467 | 446 |
468 // Pump the loop, to ensure the pending backoff retry has no effect. | 447 // Pump the loop, to ensure the pending backoff retry has no effect. |
469 base::MessageLoop::current()->PostDelayedTask( | 448 base::MessageLoop::current()->PostDelayedTask( |
470 FROM_HERE, | 449 FROM_HERE, |
471 base::MessageLoop::QuitClosure(), | 450 base::MessageLoop::QuitClosure(), |
472 base::TimeDelta::FromMilliseconds(1)); | 451 base::TimeDelta::FromMilliseconds(1)); |
473 WaitForConnections(); | 452 WaitForConnections(); |
474 } | 453 } |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
538 factory()->SignalConnectionReset(ConnectionFactory::SOCKET_FAILURE); | 517 factory()->SignalConnectionReset(ConnectionFactory::SOCKET_FAILURE); |
539 EXPECT_NE(retry_time, factory()->NextRetryAttempt()); | 518 EXPECT_NE(retry_time, factory()->NextRetryAttempt()); |
540 retry_time = factory()->NextRetryAttempt(); | 519 retry_time = factory()->NextRetryAttempt(); |
541 EXPECT_FALSE(retry_time.is_null()); | 520 EXPECT_FALSE(retry_time.is_null()); |
542 EXPECT_GE((retry_time - connect_time).InMilliseconds(), | 521 EXPECT_GE((retry_time - connect_time).InMilliseconds(), |
543 CalculateBackoff(3)); | 522 CalculateBackoff(3)); |
544 EXPECT_FALSE(factory()->IsEndpointReachable()); | 523 EXPECT_FALSE(factory()->IsEndpointReachable()); |
545 EXPECT_FALSE(connected_server().is_valid()); | 524 EXPECT_FALSE(connected_server().is_valid()); |
546 } | 525 } |
547 | 526 |
527 // When the network is disconnected, close the socket and suppress further | |
528 // connection attempts until the network returns. | |
529 TEST_F(ConnectionFactoryImplTest, SuppressConnectWhenNoNetwork) { | |
530 factory()->SetConnectResult(net::OK); | |
531 factory()->Connect(); | |
532 EXPECT_TRUE(factory()->NextRetryAttempt().is_null()); | |
533 EXPECT_TRUE(factory()->IsEndpointReachable()); | |
534 | |
535 // Advance clock so the login window reset isn't encountered. | |
536 factory()->tick_clock()->Advance(base::TimeDelta::FromSeconds(11)); | |
537 | |
538 // Will trigger reset, but will not attempt a new connection. | |
539 factory()->OnNetworkChanged(net::NetworkChangeNotifier::CONNECTION_NONE); | |
540 EXPECT_FALSE(factory()->IsEndpointReachable()); | |
541 EXPECT_TRUE(factory()->NextRetryAttempt().is_null()); | |
542 | |
543 // When the network returns, attempt to connect. | |
544 factory()->SetConnectResult(net::OK); | |
545 factory()->OnNetworkChanged(net::NetworkChangeNotifier::CONNECTION_WIFI); | |
546 WaitForConnections(); | |
547 | |
548 EXPECT_TRUE(factory()->IsEndpointReachable()); | |
549 EXPECT_TRUE(factory()->NextRetryAttempt().is_null()); | |
550 } | |
551 | |
548 } // namespace gcm | 552 } // namespace gcm |
OLD | NEW |