| 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" |
| 11 #include "base/test/simple_test_tick_clock.h" | 11 #include "base/test/simple_test_tick_clock.h" |
| 12 #include "net/base/backoff_entry.h" | 12 #include "net/base/backoff_entry.h" |
| 13 #include "net/http/http_network_session.h" | 13 #include "net/http/http_network_session.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 15 |
| 16 class Policy; | 16 class Policy; |
| 17 | 17 |
| 18 namespace gcm { | 18 namespace gcm { |
| 19 namespace { | 19 namespace { |
| 20 | 20 |
| 21 const char kMCSEndpoint[] = "http://my.server"; | 21 const char kMCSEndpoint[] = "http://my.server"; |
| 22 const char kMCSEndpoint2[] = "http://my.alt.server"; |
| 22 | 23 |
| 23 const int kBackoffDelayMs = 1; | 24 const int kBackoffDelayMs = 1; |
| 24 const int kBackoffMultiplier = 2; | 25 const int kBackoffMultiplier = 2; |
| 25 | 26 |
| 26 // A backoff policy with small enough delays that tests aren't burdened. | 27 // A backoff policy with small enough delays that tests aren't burdened. |
| 27 const net::BackoffEntry::Policy kTestBackoffPolicy = { | 28 const net::BackoffEntry::Policy kTestBackoffPolicy = { |
| 28 // Number of initial errors (in sequence) to ignore before applying | 29 // Number of initial errors (in sequence) to ignore before applying |
| 29 // exponential back-off rules. | 30 // exponential back-off rules. |
| 30 0, | 31 0, |
| 31 | 32 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 43 10, | 44 10, |
| 44 | 45 |
| 45 // Time to keep an entry from being discarded even when it | 46 // Time to keep an entry from being discarded even when it |
| 46 // has no significant state, -1 to never discard. | 47 // has no significant state, -1 to never discard. |
| 47 -1, | 48 -1, |
| 48 | 49 |
| 49 // Don't use initial delay unless the last request was an error. | 50 // Don't use initial delay unless the last request was an error. |
| 50 false, | 51 false, |
| 51 }; | 52 }; |
| 52 | 53 |
| 54 std::vector<GURL> BuildEndpoints() { |
| 55 std::vector<GURL> endpoints; |
| 56 endpoints.push_back(GURL(kMCSEndpoint)); |
| 57 endpoints.push_back(GURL(kMCSEndpoint2)); |
| 58 return endpoints; |
| 59 } |
| 60 |
| 53 // Helper for calculating total expected exponential backoff delay given an | 61 // Helper for calculating total expected exponential backoff delay given an |
| 54 // arbitrary number of failed attempts. See BackoffEntry::CalculateReleaseTime. | 62 // arbitrary number of failed attempts. See BackoffEntry::CalculateReleaseTime. |
| 55 double CalculateBackoff(int num_attempts) { | 63 double CalculateBackoff(int num_attempts) { |
| 56 double delay = kBackoffDelayMs; | 64 double delay = kBackoffDelayMs; |
| 57 for (int i = 1; i < num_attempts; ++i) { | 65 for (int i = 1; i < num_attempts; ++i) { |
| 58 delay += kBackoffDelayMs * pow(static_cast<double>(kBackoffMultiplier), | 66 delay += kBackoffDelayMs * pow(static_cast<double>(kBackoffMultiplier), |
| 59 i - 1); | 67 i - 1); |
| 60 } | 68 } |
| 61 DVLOG(1) << "Expected backoff " << delay << " milliseconds."; | 69 DVLOG(1) << "Expected backoff " << delay << " milliseconds."; |
| 62 return delay; | 70 return delay; |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 int num_expected_attempts_; | 133 int num_expected_attempts_; |
| 126 // Whether all expected connection attempts have been fulfilled since an | 134 // Whether all expected connection attempts have been fulfilled since an |
| 127 // expectation was last set. | 135 // expectation was last set. |
| 128 bool connections_fulfilled_; | 136 bool connections_fulfilled_; |
| 129 // Callback to invoke when all connection attempts have been made. | 137 // Callback to invoke when all connection attempts have been made. |
| 130 base::Closure finished_callback_; | 138 base::Closure finished_callback_; |
| 131 }; | 139 }; |
| 132 | 140 |
| 133 TestConnectionFactoryImpl::TestConnectionFactoryImpl( | 141 TestConnectionFactoryImpl::TestConnectionFactoryImpl( |
| 134 const base::Closure& finished_callback) | 142 const base::Closure& finished_callback) |
| 135 : ConnectionFactoryImpl(GURL(kMCSEndpoint), | 143 : ConnectionFactoryImpl(BuildEndpoints(), |
| 136 net::BackoffEntry::Policy(), | 144 net::BackoffEntry::Policy(), |
| 137 NULL, | 145 NULL, |
| 138 NULL), | 146 NULL), |
| 139 connect_result_(net::ERR_UNEXPECTED), | 147 connect_result_(net::ERR_UNEXPECTED), |
| 140 num_expected_attempts_(0), | 148 num_expected_attempts_(0), |
| 141 connections_fulfilled_(true), | 149 connections_fulfilled_(true), |
| 142 finished_callback_(finished_callback) { | 150 finished_callback_(finished_callback) { |
| 143 // Set a non-null time. | 151 // Set a non-null time. |
| 144 tick_clock_.Advance(base::TimeDelta::FromMilliseconds(1)); | 152 tick_clock_.Advance(base::TimeDelta::FromMilliseconds(1)); |
| 145 } | 153 } |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 247 | 255 |
| 248 // An initial successful connection should not result in backoff. | 256 // An initial successful connection should not result in backoff. |
| 249 TEST_F(ConnectionFactoryImplTest, ConnectSuccess) { | 257 TEST_F(ConnectionFactoryImplTest, ConnectSuccess) { |
| 250 factory()->Initialize( | 258 factory()->Initialize( |
| 251 ConnectionFactory::BuildLoginRequestCallback(), | 259 ConnectionFactory::BuildLoginRequestCallback(), |
| 252 ConnectionHandler::ProtoReceivedCallback(), | 260 ConnectionHandler::ProtoReceivedCallback(), |
| 253 ConnectionHandler::ProtoSentCallback()); | 261 ConnectionHandler::ProtoSentCallback()); |
| 254 factory()->SetConnectResult(net::OK); | 262 factory()->SetConnectResult(net::OK); |
| 255 factory()->Connect(); | 263 factory()->Connect(); |
| 256 EXPECT_TRUE(factory()->NextRetryAttempt().is_null()); | 264 EXPECT_TRUE(factory()->NextRetryAttempt().is_null()); |
| 265 EXPECT_EQ(factory()->GetCurrentEndpoint(), BuildEndpoints()[0]); |
| 257 } | 266 } |
| 258 | 267 |
| 259 // A connection failure should result in backoff. | 268 // A connection failure should result in backoff, and attempting the fallback |
| 269 // endpoint next. |
| 260 TEST_F(ConnectionFactoryImplTest, ConnectFail) { | 270 TEST_F(ConnectionFactoryImplTest, ConnectFail) { |
| 261 factory()->Initialize( | 271 factory()->Initialize( |
| 262 ConnectionFactory::BuildLoginRequestCallback(), | 272 ConnectionFactory::BuildLoginRequestCallback(), |
| 263 ConnectionHandler::ProtoReceivedCallback(), | 273 ConnectionHandler::ProtoReceivedCallback(), |
| 264 ConnectionHandler::ProtoSentCallback()); | 274 ConnectionHandler::ProtoSentCallback()); |
| 265 factory()->SetConnectResult(net::ERR_CONNECTION_FAILED); | 275 factory()->SetConnectResult(net::ERR_CONNECTION_FAILED); |
| 266 factory()->Connect(); | 276 factory()->Connect(); |
| 267 EXPECT_FALSE(factory()->NextRetryAttempt().is_null()); | 277 EXPECT_FALSE(factory()->NextRetryAttempt().is_null()); |
| 278 EXPECT_EQ(factory()->GetCurrentEndpoint(), BuildEndpoints()[1]); |
| 268 } | 279 } |
| 269 | 280 |
| 270 // A connection success after a failure should reset backoff. | 281 // A connection success after a failure should reset backoff. |
| 271 TEST_F(ConnectionFactoryImplTest, FailThenSucceed) { | 282 TEST_F(ConnectionFactoryImplTest, FailThenSucceed) { |
| 272 factory()->Initialize( | 283 factory()->Initialize( |
| 273 ConnectionFactory::BuildLoginRequestCallback(), | 284 ConnectionFactory::BuildLoginRequestCallback(), |
| 274 ConnectionHandler::ProtoReceivedCallback(), | 285 ConnectionHandler::ProtoReceivedCallback(), |
| 275 ConnectionHandler::ProtoSentCallback()); | 286 ConnectionHandler::ProtoSentCallback()); |
| 276 factory()->SetConnectResult(net::ERR_CONNECTION_FAILED); | 287 factory()->SetConnectResult(net::ERR_CONNECTION_FAILED); |
| 277 base::TimeTicks connect_time = factory()->tick_clock()->NowTicks(); | 288 base::TimeTicks connect_time = factory()->tick_clock()->NowTicks(); |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 413 factory()->SignalConnectionReset(ConnectionFactory::SOCKET_FAILURE); | 424 factory()->SignalConnectionReset(ConnectionFactory::SOCKET_FAILURE); |
| 414 EXPECT_NE(retry_time, factory()->NextRetryAttempt()); | 425 EXPECT_NE(retry_time, factory()->NextRetryAttempt()); |
| 415 retry_time = factory()->NextRetryAttempt(); | 426 retry_time = factory()->NextRetryAttempt(); |
| 416 EXPECT_FALSE(retry_time.is_null()); | 427 EXPECT_FALSE(retry_time.is_null()); |
| 417 EXPECT_GE((retry_time - connect_time).InMilliseconds(), | 428 EXPECT_GE((retry_time - connect_time).InMilliseconds(), |
| 418 CalculateBackoff(3)); | 429 CalculateBackoff(3)); |
| 419 } | 430 } |
| 420 | 431 |
| 421 } // namespace | 432 } // namespace |
| 422 } // namespace gcm | 433 } // namespace gcm |
| OLD | NEW |