| 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 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 // A connection factory that stubs out network requests and overrides the | 83 // A connection factory that stubs out network requests and overrides the |
| 84 // backoff policy. | 84 // backoff policy. |
| 85 class TestConnectionFactoryImpl : public ConnectionFactoryImpl { | 85 class TestConnectionFactoryImpl : public ConnectionFactoryImpl { |
| 86 public: | 86 public: |
| 87 TestConnectionFactoryImpl(const base::Closure& finished_callback); | 87 TestConnectionFactoryImpl(const base::Closure& finished_callback); |
| 88 ~TestConnectionFactoryImpl() override; | 88 ~TestConnectionFactoryImpl() override; |
| 89 | 89 |
| 90 void InitializeFactory(); | 90 void InitializeFactory(); |
| 91 | 91 |
| 92 // Overridden stubs. | 92 // Overridden stubs. |
| 93 void ConnectImpl() override; | 93 void StartConnection() override; |
| 94 void InitHandler() override; | 94 void InitHandler() override; |
| 95 std::unique_ptr<net::BackoffEntry> CreateBackoffEntry( | 95 std::unique_ptr<net::BackoffEntry> CreateBackoffEntry( |
| 96 const net::BackoffEntry::Policy* const policy) override; | 96 const net::BackoffEntry::Policy* const policy) override; |
| 97 std::unique_ptr<ConnectionHandler> CreateConnectionHandler( | 97 std::unique_ptr<ConnectionHandler> CreateConnectionHandler( |
| 98 base::TimeDelta read_timeout, | 98 base::TimeDelta read_timeout, |
| 99 const ConnectionHandler::ProtoReceivedCallback& read_callback, | 99 const ConnectionHandler::ProtoReceivedCallback& read_callback, |
| 100 const ConnectionHandler::ProtoSentCallback& write_callback, | 100 const ConnectionHandler::ProtoSentCallback& write_callback, |
| 101 const ConnectionHandler::ConnectionChangedCallback& connection_callback) | 101 const ConnectionHandler::ConnectionChangedCallback& connection_callback) |
| 102 override; | 102 override; |
| 103 base::TimeTicks NowTicks() override; | 103 base::TimeTicks NowTicks() override; |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 base::Bind(&WriteContinuation))), | 156 base::Bind(&WriteContinuation))), |
| 157 fake_handler_(scoped_handler_.get()) { | 157 fake_handler_(scoped_handler_.get()) { |
| 158 // Set a non-null time. | 158 // Set a non-null time. |
| 159 tick_clock_.Advance(base::TimeDelta::FromMilliseconds(1)); | 159 tick_clock_.Advance(base::TimeDelta::FromMilliseconds(1)); |
| 160 } | 160 } |
| 161 | 161 |
| 162 TestConnectionFactoryImpl::~TestConnectionFactoryImpl() { | 162 TestConnectionFactoryImpl::~TestConnectionFactoryImpl() { |
| 163 EXPECT_EQ(0, num_expected_attempts_); | 163 EXPECT_EQ(0, num_expected_attempts_); |
| 164 } | 164 } |
| 165 | 165 |
| 166 void TestConnectionFactoryImpl::ConnectImpl() { | 166 void TestConnectionFactoryImpl::StartConnection() { |
| 167 ASSERT_GT(num_expected_attempts_, 0); | 167 ASSERT_GT(num_expected_attempts_, 0); |
| 168 ASSERT_FALSE(GetConnectionHandler()->CanSendMessage()); | 168 ASSERT_FALSE(GetConnectionHandler()->CanSendMessage()); |
| 169 std::unique_ptr<mcs_proto::LoginRequest> request(BuildLoginRequest(0, 0, "")); | 169 std::unique_ptr<mcs_proto::LoginRequest> request(BuildLoginRequest(0, 0, "")); |
| 170 GetConnectionHandler()->Init(*request, NULL); | 170 GetConnectionHandler()->Init(*request, NULL); |
| 171 OnConnectDone(connect_result_); | 171 OnConnectDone(connect_result_); |
| 172 if (!NextRetryAttempt().is_null()) { | 172 if (!NextRetryAttempt().is_null()) { |
| 173 // Advance the time to the next retry time. | 173 // Advance the time to the next retry time. |
| 174 base::TimeDelta time_till_retry = | 174 base::TimeDelta time_till_retry = |
| 175 NextRetryAttempt() - tick_clock_.NowTicks(); | 175 NextRetryAttempt() - tick_clock_.NowTicks(); |
| 176 tick_clock_.Advance(time_till_retry); | 176 tick_clock_.Advance(time_till_retry); |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 254 TestConnectionFactoryImpl* factory() { return &factory_; } | 254 TestConnectionFactoryImpl* factory() { return &factory_; } |
| 255 GURL& connected_server() { return connected_server_; } | 255 GURL& connected_server() { return connected_server_; } |
| 256 | 256 |
| 257 void WaitForConnections(); | 257 void WaitForConnections(); |
| 258 | 258 |
| 259 // ConnectionFactory::ConnectionListener | 259 // ConnectionFactory::ConnectionListener |
| 260 void OnConnected(const GURL& current_server, | 260 void OnConnected(const GURL& current_server, |
| 261 const net::IPEndPoint& ip_endpoint) override; | 261 const net::IPEndPoint& ip_endpoint) override; |
| 262 void OnDisconnected() override; | 262 void OnDisconnected() override; |
| 263 | 263 |
| 264 // Get the client events recorded by the event tracker. |
| 265 const google::protobuf::RepeatedPtrField<mcs_proto::ClientEvent> |
| 266 GetClientEvents() { |
| 267 mcs_proto::LoginRequest login_request; |
| 268 factory()->event_tracker_.WriteToLoginRequest(&login_request); |
| 269 return login_request.client_event(); |
| 270 } |
| 271 |
| 264 private: | 272 private: |
| 265 void ConnectionsComplete(); | 273 void ConnectionsComplete(); |
| 266 | 274 |
| 267 TestConnectionFactoryImpl factory_; | 275 TestConnectionFactoryImpl factory_; |
| 268 base::MessageLoop message_loop_; | 276 base::MessageLoop message_loop_; |
| 269 std::unique_ptr<base::RunLoop> run_loop_; | 277 std::unique_ptr<base::RunLoop> run_loop_; |
| 270 | 278 |
| 271 GURL connected_server_; | 279 GURL connected_server_; |
| 272 }; | 280 }; |
| 273 | 281 |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 361 base::TimeTicks connect_time = factory()->tick_clock()->NowTicks(); | 369 base::TimeTicks connect_time = factory()->tick_clock()->NowTicks(); |
| 362 factory()->Connect(); | 370 factory()->Connect(); |
| 363 WaitForConnections(); | 371 WaitForConnections(); |
| 364 EXPECT_FALSE(factory()->IsEndpointReachable()); | 372 EXPECT_FALSE(factory()->IsEndpointReachable()); |
| 365 EXPECT_FALSE(connected_server().is_valid()); | 373 EXPECT_FALSE(connected_server().is_valid()); |
| 366 base::TimeTicks retry_time = factory()->NextRetryAttempt(); | 374 base::TimeTicks retry_time = factory()->NextRetryAttempt(); |
| 367 EXPECT_FALSE(retry_time.is_null()); | 375 EXPECT_FALSE(retry_time.is_null()); |
| 368 EXPECT_GE((retry_time - connect_time).InMilliseconds(), | 376 EXPECT_GE((retry_time - connect_time).InMilliseconds(), |
| 369 CalculateBackoff(kNumAttempts)); | 377 CalculateBackoff(kNumAttempts)); |
| 370 | 378 |
| 379 // There should be one failed client event for each failed connection. |
| 380 const auto client_events = GetClientEvents(); |
| 381 ASSERT_EQ(kNumAttempts, client_events.size()); |
| 382 |
| 383 for (const auto& client_event : client_events) { |
| 384 EXPECT_EQ(mcs_proto::ClientEvent::FAILED_CONNECTION, client_event.type()); |
| 385 EXPECT_EQ(net::ERR_CONNECTION_FAILED, client_event.error_code()); |
| 386 } |
| 387 |
| 371 factory()->SetConnectResult(net::OK); | 388 factory()->SetConnectResult(net::OK); |
| 372 WaitForConnections(); | 389 WaitForConnections(); |
| 373 EXPECT_TRUE(factory()->NextRetryAttempt().is_null()); | 390 EXPECT_TRUE(factory()->NextRetryAttempt().is_null()); |
| 374 EXPECT_TRUE(factory()->IsEndpointReachable()); | 391 EXPECT_TRUE(factory()->IsEndpointReachable()); |
| 375 EXPECT_TRUE(connected_server().is_valid()); | 392 EXPECT_TRUE(connected_server().is_valid()); |
| 393 |
| 394 // Old client events should have been reset after the successful connection. |
| 395 const auto new_client_events = GetClientEvents(); |
| 396 ASSERT_EQ(0, new_client_events.size()); |
| 376 } | 397 } |
| 377 | 398 |
| 378 // Network change events should trigger canary connections. | 399 // Network change events should trigger canary connections. |
| 379 TEST_F(ConnectionFactoryImplTest, FailThenNetworkChangeEvent) { | 400 TEST_F(ConnectionFactoryImplTest, FailThenNetworkChangeEvent) { |
| 380 factory()->SetConnectResult(net::ERR_CONNECTION_FAILED); | 401 factory()->SetConnectResult(net::ERR_CONNECTION_FAILED); |
| 381 factory()->Connect(); | 402 factory()->Connect(); |
| 382 WaitForConnections(); | 403 WaitForConnections(); |
| 383 base::TimeTicks initial_backoff = factory()->NextRetryAttempt(); | 404 base::TimeTicks initial_backoff = factory()->NextRetryAttempt(); |
| 384 EXPECT_FALSE(initial_backoff.is_null()); | 405 EXPECT_FALSE(initial_backoff.is_null()); |
| 385 | 406 |
| (...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 562 | 583 |
| 563 // Now trigger force a re-connection. | 584 // Now trigger force a re-connection. |
| 564 factory()->SetConnectResult(net::OK); | 585 factory()->SetConnectResult(net::OK); |
| 565 factory()->Connect(); | 586 factory()->Connect(); |
| 566 WaitForConnections(); | 587 WaitForConnections(); |
| 567 | 588 |
| 568 // Re-connection should succeed. | 589 // Re-connection should succeed. |
| 569 EXPECT_TRUE(factory()->IsEndpointReachable()); | 590 EXPECT_TRUE(factory()->IsEndpointReachable()); |
| 570 } | 591 } |
| 571 | 592 |
| 593 TEST_F(ConnectionFactoryImplTest, MultipleFailuresWrapClientEvents) { |
| 594 const int kNumAttempts = 50; |
| 595 factory()->SetMultipleConnectResults(net::ERR_CONNECTION_FAILED, |
| 596 kNumAttempts); |
| 597 |
| 598 factory()->Connect(); |
| 599 WaitForConnections(); |
| 600 |
| 601 // There should be one failed client event for each failed connection, but |
| 602 // there is a maximum cap of kMaxClientEvents, which is 30. |
| 603 const auto client_events = GetClientEvents(); |
| 604 ASSERT_EQ(30, client_events.size()); |
| 605 |
| 606 for (const auto& client_event : client_events) { |
| 607 EXPECT_EQ(mcs_proto::ClientEvent::FAILED_CONNECTION, client_event.type()); |
| 608 EXPECT_EQ(net::ERR_CONNECTION_FAILED, client_event.error_code()); |
| 609 } |
| 610 |
| 611 factory()->SetConnectResult(net::OK); |
| 612 WaitForConnections(); |
| 613 EXPECT_TRUE(factory()->IsEndpointReachable()); |
| 614 EXPECT_TRUE(connected_server().is_valid()); |
| 615 |
| 616 // Old client events should have been reset after the successful connection. |
| 617 const auto new_client_events = GetClientEvents(); |
| 618 ASSERT_EQ(0, new_client_events.size()); |
| 619 } |
| 620 |
| 572 } // namespace gcm | 621 } // namespace gcm |
| OLD | NEW |