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 333 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
344 // Fail after successful connection via signal reset. | 344 // Fail after successful connection via signal reset. |
345 TEST_F(ConnectionFactoryImplTest, FailViaSignalReset) { | 345 TEST_F(ConnectionFactoryImplTest, FailViaSignalReset) { |
346 factory()->Initialize( | 346 factory()->Initialize( |
347 ConnectionFactory::BuildLoginRequestCallback(), | 347 ConnectionFactory::BuildLoginRequestCallback(), |
348 ConnectionHandler::ProtoReceivedCallback(), | 348 ConnectionHandler::ProtoReceivedCallback(), |
349 ConnectionHandler::ProtoSentCallback()); | 349 ConnectionHandler::ProtoSentCallback()); |
350 factory()->SetConnectResult(net::OK); | 350 factory()->SetConnectResult(net::OK); |
351 factory()->Connect(); | 351 factory()->Connect(); |
352 EXPECT_TRUE(factory()->NextRetryAttempt().is_null()); | 352 EXPECT_TRUE(factory()->NextRetryAttempt().is_null()); |
353 | 353 |
354 factory()->SignalConnectionReset(); | 354 factory()->SignalConnectionReset(ConnectionFactory::SOCKET_FAILURE); |
355 EXPECT_FALSE(factory()->NextRetryAttempt().is_null()); | 355 EXPECT_FALSE(factory()->NextRetryAttempt().is_null()); |
356 EXPECT_FALSE(factory()->GetConnectionHandler()->CanSendMessage()); | 356 EXPECT_FALSE(factory()->GetConnectionHandler()->CanSendMessage()); |
357 } | 357 } |
358 | 358 |
359 TEST_F(ConnectionFactoryImplTest, IgnoreResetWhileConnecting) { | 359 TEST_F(ConnectionFactoryImplTest, IgnoreResetWhileConnecting) { |
360 factory()->Initialize( | 360 factory()->Initialize( |
361 ConnectionFactory::BuildLoginRequestCallback(), | 361 ConnectionFactory::BuildLoginRequestCallback(), |
362 ConnectionHandler::ProtoReceivedCallback(), | 362 ConnectionHandler::ProtoReceivedCallback(), |
363 ConnectionHandler::ProtoSentCallback()); | 363 ConnectionHandler::ProtoSentCallback()); |
364 factory()->SetConnectResult(net::OK); | 364 factory()->SetConnectResult(net::OK); |
365 factory()->Connect(); | 365 factory()->Connect(); |
366 EXPECT_TRUE(factory()->NextRetryAttempt().is_null()); | 366 EXPECT_TRUE(factory()->NextRetryAttempt().is_null()); |
367 | 367 |
368 factory()->SignalConnectionReset(); | 368 factory()->SignalConnectionReset(ConnectionFactory::SOCKET_FAILURE); |
369 base::TimeTicks retry_time = factory()->NextRetryAttempt(); | 369 base::TimeTicks retry_time = factory()->NextRetryAttempt(); |
370 EXPECT_FALSE(retry_time.is_null()); | 370 EXPECT_FALSE(retry_time.is_null()); |
371 | 371 |
372 const int kNumAttempts = 5; | 372 const int kNumAttempts = 5; |
373 for (int i = 0; i < kNumAttempts; ++i) | 373 for (int i = 0; i < kNumAttempts; ++i) |
374 factory()->SignalConnectionReset(); | 374 factory()->SignalConnectionReset(ConnectionFactory::SOCKET_FAILURE); |
375 EXPECT_EQ(retry_time, factory()->NextRetryAttempt()); | 375 EXPECT_EQ(retry_time, factory()->NextRetryAttempt()); |
376 } | 376 } |
377 | 377 |
378 // Go into backoff due to connection failure. On successful connection, receive | 378 // Go into backoff due to connection failure. On successful connection, receive |
379 // a signal reset. The original backoff should be restored and extended, rather | 379 // a signal reset. The original backoff should be restored and extended, rather |
380 // than a new backoff starting from scratch. | 380 // than a new backoff starting from scratch. |
381 TEST_F(ConnectionFactoryImplTest, SignalResetRestoresBackoff) { | 381 TEST_F(ConnectionFactoryImplTest, SignalResetRestoresBackoff) { |
382 factory()->Initialize( | 382 factory()->Initialize( |
383 ConnectionFactory::BuildLoginRequestCallback(), | 383 ConnectionFactory::BuildLoginRequestCallback(), |
384 ConnectionHandler::ProtoReceivedCallback(), | 384 ConnectionHandler::ProtoReceivedCallback(), |
385 ConnectionHandler::ProtoSentCallback()); | 385 ConnectionHandler::ProtoSentCallback()); |
386 factory()->SetConnectResult(net::ERR_CONNECTION_FAILED); | 386 factory()->SetConnectResult(net::ERR_CONNECTION_FAILED); |
387 base::TimeTicks connect_time = factory()->tick_clock()->NowTicks(); | 387 base::TimeTicks connect_time = factory()->tick_clock()->NowTicks(); |
388 factory()->Connect(); | 388 factory()->Connect(); |
389 WaitForConnections(); | 389 WaitForConnections(); |
390 base::TimeTicks retry_time = factory()->NextRetryAttempt(); | 390 base::TimeTicks retry_time = factory()->NextRetryAttempt(); |
391 EXPECT_FALSE(retry_time.is_null()); | 391 EXPECT_FALSE(retry_time.is_null()); |
392 | 392 |
393 factory()->SetConnectResult(net::OK); | 393 factory()->SetConnectResult(net::OK); |
394 connect_time = factory()->tick_clock()->NowTicks(); | 394 connect_time = factory()->tick_clock()->NowTicks(); |
395 WaitForConnections(); | 395 WaitForConnections(); |
396 EXPECT_TRUE(factory()->NextRetryAttempt().is_null()); | 396 EXPECT_TRUE(factory()->NextRetryAttempt().is_null()); |
397 | 397 |
398 factory()->SignalConnectionReset(); | 398 factory()->SignalConnectionReset(ConnectionFactory::SOCKET_FAILURE); |
399 EXPECT_FALSE(factory()->GetConnectionHandler()->CanSendMessage()); | 399 EXPECT_FALSE(factory()->GetConnectionHandler()->CanSendMessage()); |
400 EXPECT_NE(retry_time, factory()->NextRetryAttempt()); | 400 EXPECT_NE(retry_time, factory()->NextRetryAttempt()); |
401 retry_time = factory()->NextRetryAttempt(); | 401 retry_time = factory()->NextRetryAttempt(); |
402 EXPECT_FALSE(retry_time.is_null()); | 402 EXPECT_FALSE(retry_time.is_null()); |
403 EXPECT_GE((retry_time - connect_time).InMilliseconds(), | 403 EXPECT_GE((retry_time - connect_time).InMilliseconds(), |
404 CalculateBackoff(2)); | 404 CalculateBackoff(2)); |
405 | 405 |
406 factory()->SetConnectResult(net::OK); | 406 factory()->SetConnectResult(net::OK); |
407 connect_time = factory()->tick_clock()->NowTicks(); | 407 connect_time = factory()->tick_clock()->NowTicks(); |
408 factory()->tick_clock()->Advance( | 408 factory()->tick_clock()->Advance( |
409 factory()->NextRetryAttempt() - connect_time); | 409 factory()->NextRetryAttempt() - connect_time); |
410 WaitForConnections(); | 410 WaitForConnections(); |
411 EXPECT_TRUE(factory()->NextRetryAttempt().is_null()); | 411 EXPECT_TRUE(factory()->NextRetryAttempt().is_null()); |
412 | 412 |
413 factory()->SignalConnectionReset(); | 413 factory()->SignalConnectionReset(ConnectionFactory::SOCKET_FAILURE); |
414 EXPECT_NE(retry_time, factory()->NextRetryAttempt()); | 414 EXPECT_NE(retry_time, factory()->NextRetryAttempt()); |
415 retry_time = factory()->NextRetryAttempt(); | 415 retry_time = factory()->NextRetryAttempt(); |
416 EXPECT_FALSE(retry_time.is_null()); | 416 EXPECT_FALSE(retry_time.is_null()); |
417 EXPECT_GE((retry_time - connect_time).InMilliseconds(), | 417 EXPECT_GE((retry_time - connect_time).InMilliseconds(), |
418 CalculateBackoff(3)); | 418 CalculateBackoff(3)); |
419 } | 419 } |
420 | 420 |
421 } // namespace | 421 } // namespace |
422 } // namespace gcm | 422 } // namespace gcm |
OLD | NEW |