| 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/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
| 11 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
| 12 #include "base/test/simple_test_tick_clock.h" | 13 #include "base/test/simple_test_tick_clock.h" |
| 13 #include "google_apis/gcm/base/mcs_util.h" | 14 #include "google_apis/gcm/base/mcs_util.h" |
| 14 #include "google_apis/gcm/engine/fake_connection_handler.h" | 15 #include "google_apis/gcm/engine/fake_connection_handler.h" |
| 15 #include "google_apis/gcm/monitoring/fake_gcm_stats_recorder.h" | 16 #include "google_apis/gcm/monitoring/fake_gcm_stats_recorder.h" |
| 16 #include "net/base/backoff_entry.h" | 17 #include "net/base/backoff_entry.h" |
| 17 #include "net/http/http_network_session.h" | 18 #include "net/http/http_network_session.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 20 |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 double CalculateBackoff(int num_attempts) { | 68 double CalculateBackoff(int num_attempts) { |
| 68 double delay = kBackoffDelayMs; | 69 double delay = kBackoffDelayMs; |
| 69 for (int i = 1; i < num_attempts; ++i) { | 70 for (int i = 1; i < num_attempts; ++i) { |
| 70 delay += kBackoffDelayMs * pow(static_cast<double>(kBackoffMultiplier), | 71 delay += kBackoffDelayMs * pow(static_cast<double>(kBackoffMultiplier), |
| 71 i - 1); | 72 i - 1); |
| 72 } | 73 } |
| 73 DVLOG(1) << "Expected backoff " << delay << " milliseconds."; | 74 DVLOG(1) << "Expected backoff " << delay << " milliseconds."; |
| 74 return delay; | 75 return delay; |
| 75 } | 76 } |
| 76 | 77 |
| 77 void ReadContinuation( | 78 void ReadContinuation(std::unique_ptr<google::protobuf::MessageLite> message) {} |
| 78 scoped_ptr<google::protobuf::MessageLite> message) { | |
| 79 } | |
| 80 | 79 |
| 81 void WriteContinuation() { | 80 void WriteContinuation() { |
| 82 } | 81 } |
| 83 | 82 |
| 84 // A connection factory that stubs out network requests and overrides the | 83 // A connection factory that stubs out network requests and overrides the |
| 85 // backoff policy. | 84 // backoff policy. |
| 86 class TestConnectionFactoryImpl : public ConnectionFactoryImpl { | 85 class TestConnectionFactoryImpl : public ConnectionFactoryImpl { |
| 87 public: | 86 public: |
| 88 TestConnectionFactoryImpl(const base::Closure& finished_callback); | 87 TestConnectionFactoryImpl(const base::Closure& finished_callback); |
| 89 ~TestConnectionFactoryImpl() override; | 88 ~TestConnectionFactoryImpl() override; |
| 90 | 89 |
| 91 void InitializeFactory(); | 90 void InitializeFactory(); |
| 92 | 91 |
| 93 // Overridden stubs. | 92 // Overridden stubs. |
| 94 void ConnectImpl() override; | 93 void ConnectImpl() override; |
| 95 void InitHandler() override; | 94 void InitHandler() override; |
| 96 scoped_ptr<net::BackoffEntry> CreateBackoffEntry( | 95 std::unique_ptr<net::BackoffEntry> CreateBackoffEntry( |
| 97 const net::BackoffEntry::Policy* const policy) override; | 96 const net::BackoffEntry::Policy* const policy) override; |
| 98 scoped_ptr<ConnectionHandler> CreateConnectionHandler( | 97 std::unique_ptr<ConnectionHandler> CreateConnectionHandler( |
| 99 base::TimeDelta read_timeout, | 98 base::TimeDelta read_timeout, |
| 100 const ConnectionHandler::ProtoReceivedCallback& read_callback, | 99 const ConnectionHandler::ProtoReceivedCallback& read_callback, |
| 101 const ConnectionHandler::ProtoSentCallback& write_callback, | 100 const ConnectionHandler::ProtoSentCallback& write_callback, |
| 102 const ConnectionHandler::ConnectionChangedCallback& connection_callback) | 101 const ConnectionHandler::ConnectionChangedCallback& connection_callback) |
| 103 override; | 102 override; |
| 104 base::TimeTicks NowTicks() override; | 103 base::TimeTicks NowTicks() override; |
| 105 | 104 |
| 106 // Helpers for verifying connection attempts are made. Connection results | 105 // Helpers for verifying connection attempts are made. Connection results |
| 107 // must be consumed. | 106 // must be consumed. |
| 108 void SetConnectResult(int connect_result); | 107 void SetConnectResult(int connect_result); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 125 int num_expected_attempts_; | 124 int num_expected_attempts_; |
| 126 // Whether all expected connection attempts have been fulfilled since an | 125 // Whether all expected connection attempts have been fulfilled since an |
| 127 // expectation was last set. | 126 // expectation was last set. |
| 128 bool connections_fulfilled_; | 127 bool connections_fulfilled_; |
| 129 // Whether to delay a login handshake completion or not. | 128 // Whether to delay a login handshake completion or not. |
| 130 bool delay_login_; | 129 bool delay_login_; |
| 131 // Callback to invoke when all connection attempts have been made. | 130 // Callback to invoke when all connection attempts have been made. |
| 132 base::Closure finished_callback_; | 131 base::Closure finished_callback_; |
| 133 // A temporary scoped pointer to make sure we don't leak the handler in the | 132 // A temporary scoped pointer to make sure we don't leak the handler in the |
| 134 // cases it's never consumed by the ConnectionFactory. | 133 // cases it's never consumed by the ConnectionFactory. |
| 135 scoped_ptr<FakeConnectionHandler> scoped_handler_; | 134 std::unique_ptr<FakeConnectionHandler> scoped_handler_; |
| 136 // The current fake connection handler.. | 135 // The current fake connection handler.. |
| 137 FakeConnectionHandler* fake_handler_; | 136 FakeConnectionHandler* fake_handler_; |
| 138 // Dummy GCM Stats recorder. | 137 // Dummy GCM Stats recorder. |
| 139 FakeGCMStatsRecorder dummy_recorder_; | 138 FakeGCMStatsRecorder dummy_recorder_; |
| 140 }; | 139 }; |
| 141 | 140 |
| 142 TestConnectionFactoryImpl::TestConnectionFactoryImpl( | 141 TestConnectionFactoryImpl::TestConnectionFactoryImpl( |
| 143 const base::Closure& finished_callback) | 142 const base::Closure& finished_callback) |
| 144 : ConnectionFactoryImpl(BuildEndpoints(), | 143 : ConnectionFactoryImpl(BuildEndpoints(), |
| 145 net::BackoffEntry::Policy(), | 144 net::BackoffEntry::Policy(), |
| (...skipping 14 matching lines...) Expand all Loading... |
| 160 tick_clock_.Advance(base::TimeDelta::FromMilliseconds(1)); | 159 tick_clock_.Advance(base::TimeDelta::FromMilliseconds(1)); |
| 161 } | 160 } |
| 162 | 161 |
| 163 TestConnectionFactoryImpl::~TestConnectionFactoryImpl() { | 162 TestConnectionFactoryImpl::~TestConnectionFactoryImpl() { |
| 164 EXPECT_EQ(0, num_expected_attempts_); | 163 EXPECT_EQ(0, num_expected_attempts_); |
| 165 } | 164 } |
| 166 | 165 |
| 167 void TestConnectionFactoryImpl::ConnectImpl() { | 166 void TestConnectionFactoryImpl::ConnectImpl() { |
| 168 ASSERT_GT(num_expected_attempts_, 0); | 167 ASSERT_GT(num_expected_attempts_, 0); |
| 169 ASSERT_FALSE(GetConnectionHandler()->CanSendMessage()); | 168 ASSERT_FALSE(GetConnectionHandler()->CanSendMessage()); |
| 170 scoped_ptr<mcs_proto::LoginRequest> request(BuildLoginRequest(0, 0, "")); | 169 std::unique_ptr<mcs_proto::LoginRequest> request(BuildLoginRequest(0, 0, "")); |
| 171 GetConnectionHandler()->Init(*request, NULL); | 170 GetConnectionHandler()->Init(*request, NULL); |
| 172 OnConnectDone(connect_result_); | 171 OnConnectDone(connect_result_); |
| 173 if (!NextRetryAttempt().is_null()) { | 172 if (!NextRetryAttempt().is_null()) { |
| 174 // Advance the time to the next retry time. | 173 // Advance the time to the next retry time. |
| 175 base::TimeDelta time_till_retry = | 174 base::TimeDelta time_till_retry = |
| 176 NextRetryAttempt() - tick_clock_.NowTicks(); | 175 NextRetryAttempt() - tick_clock_.NowTicks(); |
| 177 tick_clock_.Advance(time_till_retry); | 176 tick_clock_.Advance(time_till_retry); |
| 178 } | 177 } |
| 179 --num_expected_attempts_; | 178 --num_expected_attempts_; |
| 180 if (num_expected_attempts_ == 0) { | 179 if (num_expected_attempts_ == 0) { |
| 181 connect_result_ = net::ERR_UNEXPECTED; | 180 connect_result_ = net::ERR_UNEXPECTED; |
| 182 connections_fulfilled_ = true; | 181 connections_fulfilled_ = true; |
| 183 finished_callback_.Run(); | 182 finished_callback_.Run(); |
| 184 } | 183 } |
| 185 } | 184 } |
| 186 | 185 |
| 187 void TestConnectionFactoryImpl::InitHandler() { | 186 void TestConnectionFactoryImpl::InitHandler() { |
| 188 EXPECT_NE(connect_result_, net::ERR_UNEXPECTED); | 187 EXPECT_NE(connect_result_, net::ERR_UNEXPECTED); |
| 189 if (!delay_login_) | 188 if (!delay_login_) |
| 190 ConnectionHandlerCallback(net::OK); | 189 ConnectionHandlerCallback(net::OK); |
| 191 } | 190 } |
| 192 | 191 |
| 193 scoped_ptr<net::BackoffEntry> TestConnectionFactoryImpl::CreateBackoffEntry( | 192 std::unique_ptr<net::BackoffEntry> |
| 193 TestConnectionFactoryImpl::CreateBackoffEntry( |
| 194 const net::BackoffEntry::Policy* const policy) { | 194 const net::BackoffEntry::Policy* const policy) { |
| 195 return make_scoped_ptr(new net::BackoffEntry(&kTestBackoffPolicy, | 195 return base::WrapUnique( |
| 196 &tick_clock_)); | 196 new net::BackoffEntry(&kTestBackoffPolicy, &tick_clock_)); |
| 197 } | 197 } |
| 198 | 198 |
| 199 scoped_ptr<ConnectionHandler> | 199 std::unique_ptr<ConnectionHandler> |
| 200 TestConnectionFactoryImpl::CreateConnectionHandler( | 200 TestConnectionFactoryImpl::CreateConnectionHandler( |
| 201 base::TimeDelta read_timeout, | 201 base::TimeDelta read_timeout, |
| 202 const ConnectionHandler::ProtoReceivedCallback& read_callback, | 202 const ConnectionHandler::ProtoReceivedCallback& read_callback, |
| 203 const ConnectionHandler::ProtoSentCallback& write_callback, | 203 const ConnectionHandler::ProtoSentCallback& write_callback, |
| 204 const ConnectionHandler::ConnectionChangedCallback& connection_callback) { | 204 const ConnectionHandler::ConnectionChangedCallback& connection_callback) { |
| 205 return std::move(scoped_handler_); | 205 return std::move(scoped_handler_); |
| 206 } | 206 } |
| 207 | 207 |
| 208 base::TimeTicks TestConnectionFactoryImpl::NowTicks() { | 208 base::TimeTicks TestConnectionFactoryImpl::NowTicks() { |
| 209 return tick_clock_.NowTicks(); | 209 return tick_clock_.NowTicks(); |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 260 // ConnectionFactory::ConnectionListener | 260 // ConnectionFactory::ConnectionListener |
| 261 void OnConnected(const GURL& current_server, | 261 void OnConnected(const GURL& current_server, |
| 262 const net::IPEndPoint& ip_endpoint) override; | 262 const net::IPEndPoint& ip_endpoint) override; |
| 263 void OnDisconnected() override; | 263 void OnDisconnected() override; |
| 264 | 264 |
| 265 private: | 265 private: |
| 266 void ConnectionsComplete(); | 266 void ConnectionsComplete(); |
| 267 | 267 |
| 268 TestConnectionFactoryImpl factory_; | 268 TestConnectionFactoryImpl factory_; |
| 269 base::MessageLoop message_loop_; | 269 base::MessageLoop message_loop_; |
| 270 scoped_ptr<base::RunLoop> run_loop_; | 270 std::unique_ptr<base::RunLoop> run_loop_; |
| 271 | 271 |
| 272 GURL connected_server_; | 272 GURL connected_server_; |
| 273 }; | 273 }; |
| 274 | 274 |
| 275 ConnectionFactoryImplTest::ConnectionFactoryImplTest() | 275 ConnectionFactoryImplTest::ConnectionFactoryImplTest() |
| 276 : factory_(base::Bind(&ConnectionFactoryImplTest::ConnectionsComplete, | 276 : factory_(base::Bind(&ConnectionFactoryImplTest::ConnectionsComplete, |
| 277 base::Unretained(this))), | 277 base::Unretained(this))), |
| 278 run_loop_(new base::RunLoop()) { | 278 run_loop_(new base::RunLoop()) { |
| 279 factory()->SetConnectionListener(this); | 279 factory()->SetConnectionListener(this); |
| 280 factory()->Initialize( | 280 factory()->Initialize( |
| (...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 564 // Now trigger force a re-connection. | 564 // Now trigger force a re-connection. |
| 565 factory()->SetConnectResult(net::OK); | 565 factory()->SetConnectResult(net::OK); |
| 566 factory()->Connect(); | 566 factory()->Connect(); |
| 567 WaitForConnections(); | 567 WaitForConnections(); |
| 568 | 568 |
| 569 // Re-connection should succeed. | 569 // Re-connection should succeed. |
| 570 EXPECT_TRUE(factory()->IsEndpointReachable()); | 570 EXPECT_TRUE(factory()->IsEndpointReachable()); |
| 571 } | 571 } |
| 572 | 572 |
| 573 } // namespace gcm | 573 } // namespace gcm |
| OLD | NEW |