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 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 int connect_result_; | 141 int connect_result_; |
142 // The number of expected connection attempts; | 142 // The number of expected connection attempts; |
143 int num_expected_attempts_; | 143 int num_expected_attempts_; |
144 // Whether all expected connection attempts have been fulfilled since an | 144 // Whether all expected connection attempts have been fulfilled since an |
145 // expectation was last set. | 145 // expectation was last set. |
146 bool connections_fulfilled_; | 146 bool connections_fulfilled_; |
147 // Whether to delay a login handshake completion or not. | 147 // Whether to delay a login handshake completion or not. |
148 bool delay_login_; | 148 bool delay_login_; |
149 // Callback to invoke when all connection attempts have been made. | 149 // Callback to invoke when all connection attempts have been made. |
150 base::Closure finished_callback_; | 150 base::Closure finished_callback_; |
| 151 // A temporary scoped pointer to make sure we don't leak the handler in the |
| 152 // cases it's never consumed by the ConnectionFactory. |
| 153 scoped_ptr<FakeConnectionHandler> scoped_handler_; |
151 // The current fake connection handler.. | 154 // The current fake connection handler.. |
152 FakeConnectionHandler* fake_handler_; | 155 FakeConnectionHandler* fake_handler_; |
| 156 // Dummy GCM Stats recorder. |
153 FakeGCMStatsRecorder dummy_recorder_; | 157 FakeGCMStatsRecorder dummy_recorder_; |
154 }; | 158 }; |
155 | 159 |
156 TestConnectionFactoryImpl::TestConnectionFactoryImpl( | 160 TestConnectionFactoryImpl::TestConnectionFactoryImpl( |
157 const base::Closure& finished_callback) | 161 const base::Closure& finished_callback) |
158 : ConnectionFactoryImpl(BuildEndpoints(), | 162 : ConnectionFactoryImpl(BuildEndpoints(), |
159 net::BackoffEntry::Policy(), | 163 net::BackoffEntry::Policy(), |
160 NULL, | 164 NULL, |
161 NULL, | 165 NULL, |
162 NULL, | 166 NULL, |
163 &dummy_recorder_), | 167 &dummy_recorder_), |
164 connect_result_(net::ERR_UNEXPECTED), | 168 connect_result_(net::ERR_UNEXPECTED), |
165 num_expected_attempts_(0), | 169 num_expected_attempts_(0), |
166 connections_fulfilled_(true), | 170 connections_fulfilled_(true), |
167 delay_login_(false), | 171 delay_login_(false), |
168 finished_callback_(finished_callback), | 172 finished_callback_(finished_callback), |
169 fake_handler_(NULL) { | 173 scoped_handler_( |
| 174 new FakeConnectionHandler(base::Bind(&ReadContinuation), |
| 175 base::Bind(&WriteContinuation))), |
| 176 fake_handler_(scoped_handler_.get()) { |
170 // Set a non-null time. | 177 // Set a non-null time. |
171 tick_clock_.Advance(base::TimeDelta::FromMilliseconds(1)); | 178 tick_clock_.Advance(base::TimeDelta::FromMilliseconds(1)); |
172 } | 179 } |
173 | 180 |
174 TestConnectionFactoryImpl::~TestConnectionFactoryImpl() { | 181 TestConnectionFactoryImpl::~TestConnectionFactoryImpl() { |
175 EXPECT_EQ(0, num_expected_attempts_); | 182 EXPECT_EQ(0, num_expected_attempts_); |
176 } | 183 } |
177 | 184 |
178 void TestConnectionFactoryImpl::ConnectImpl() { | 185 void TestConnectionFactoryImpl::ConnectImpl() { |
179 ASSERT_GT(num_expected_attempts_, 0); | 186 ASSERT_GT(num_expected_attempts_, 0); |
(...skipping 24 matching lines...) Expand all Loading... |
204 const net::BackoffEntry::Policy* const policy) { | 211 const net::BackoffEntry::Policy* const policy) { |
205 return scoped_ptr<net::BackoffEntry>(new TestBackoffEntry(&tick_clock_)); | 212 return scoped_ptr<net::BackoffEntry>(new TestBackoffEntry(&tick_clock_)); |
206 } | 213 } |
207 | 214 |
208 scoped_ptr<ConnectionHandler> | 215 scoped_ptr<ConnectionHandler> |
209 TestConnectionFactoryImpl::CreateConnectionHandler( | 216 TestConnectionFactoryImpl::CreateConnectionHandler( |
210 base::TimeDelta read_timeout, | 217 base::TimeDelta read_timeout, |
211 const ConnectionHandler::ProtoReceivedCallback& read_callback, | 218 const ConnectionHandler::ProtoReceivedCallback& read_callback, |
212 const ConnectionHandler::ProtoSentCallback& write_callback, | 219 const ConnectionHandler::ProtoSentCallback& write_callback, |
213 const ConnectionHandler::ConnectionChangedCallback& connection_callback) { | 220 const ConnectionHandler::ConnectionChangedCallback& connection_callback) { |
214 fake_handler_ = new FakeConnectionHandler( | 221 return scoped_handler_.Pass(); |
215 base::Bind(&ReadContinuation), | |
216 base::Bind(&WriteContinuation)); | |
217 return make_scoped_ptr<ConnectionHandler>(fake_handler_); | |
218 } | 222 } |
219 | 223 |
220 base::TimeTicks TestConnectionFactoryImpl::NowTicks() { | 224 base::TimeTicks TestConnectionFactoryImpl::NowTicks() { |
221 return tick_clock_.NowTicks(); | 225 return tick_clock_.NowTicks(); |
222 } | 226 } |
223 | 227 |
224 void TestConnectionFactoryImpl::SetConnectResult(int connect_result) { | 228 void TestConnectionFactoryImpl::SetConnectResult(int connect_result) { |
225 DCHECK_NE(connect_result, net::ERR_UNEXPECTED); | 229 DCHECK_NE(connect_result, net::ERR_UNEXPECTED); |
226 ASSERT_EQ(0, num_expected_attempts_); | 230 ASSERT_EQ(0, num_expected_attempts_); |
227 connections_fulfilled_ = false; | 231 connections_fulfilled_ = false; |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
308 const net::IPEndPoint& ip_endpoint) { | 312 const net::IPEndPoint& ip_endpoint) { |
309 connected_server_ = current_server; | 313 connected_server_ = current_server; |
310 } | 314 } |
311 | 315 |
312 void ConnectionFactoryImplTest::OnDisconnected() { | 316 void ConnectionFactoryImplTest::OnDisconnected() { |
313 connected_server_ = GURL(); | 317 connected_server_ = GURL(); |
314 } | 318 } |
315 | 319 |
316 // Verify building a connection handler works. | 320 // Verify building a connection handler works. |
317 TEST_F(ConnectionFactoryImplTest, Initialize) { | 321 TEST_F(ConnectionFactoryImplTest, Initialize) { |
318 ConnectionHandler* handler = factory()->GetConnectionHandler(); | 322 ASSERT_FALSE(factory()->GetConnectionHandler()); |
319 ASSERT_TRUE(handler); | |
320 EXPECT_FALSE(factory()->IsEndpointReachable()); | 323 EXPECT_FALSE(factory()->IsEndpointReachable()); |
321 EXPECT_FALSE(connected_server().is_valid()); | 324 EXPECT_FALSE(connected_server().is_valid()); |
322 } | 325 } |
323 | 326 |
324 // An initial successful connection should not result in backoff. | 327 // An initial successful connection should not result in backoff. |
325 TEST_F(ConnectionFactoryImplTest, ConnectSuccess) { | 328 TEST_F(ConnectionFactoryImplTest, ConnectSuccess) { |
326 factory()->SetConnectResult(net::OK); | 329 factory()->SetConnectResult(net::OK); |
327 factory()->Connect(); | 330 factory()->Connect(); |
| 331 ASSERT_TRUE(factory()->GetConnectionHandler()); |
328 EXPECT_TRUE(factory()->NextRetryAttempt().is_null()); | 332 EXPECT_TRUE(factory()->NextRetryAttempt().is_null()); |
329 EXPECT_EQ(factory()->GetCurrentEndpoint(), BuildEndpoints()[0]); | 333 EXPECT_EQ(factory()->GetCurrentEndpoint(), BuildEndpoints()[0]); |
330 EXPECT_TRUE(factory()->IsEndpointReachable()); | 334 EXPECT_TRUE(factory()->IsEndpointReachable()); |
331 EXPECT_TRUE(connected_server().is_valid()); | 335 EXPECT_TRUE(connected_server().is_valid()); |
332 } | 336 } |
333 | 337 |
334 // A connection failure should result in backoff, and attempting the fallback | 338 // A connection failure should result in backoff, and attempting the fallback |
335 // endpoint next. | 339 // endpoint next. |
336 TEST_F(ConnectionFactoryImplTest, ConnectFail) { | 340 TEST_F(ConnectionFactoryImplTest, ConnectFail) { |
337 factory()->SetConnectResult(net::ERR_CONNECTION_FAILED); | 341 factory()->SetConnectResult(net::ERR_CONNECTION_FAILED); |
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
540 | 544 |
541 // When the network returns, attempt to connect. | 545 // When the network returns, attempt to connect. |
542 factory()->SetConnectResult(net::OK); | 546 factory()->SetConnectResult(net::OK); |
543 factory()->OnNetworkChanged(net::NetworkChangeNotifier::CONNECTION_4G); | 547 factory()->OnNetworkChanged(net::NetworkChangeNotifier::CONNECTION_4G); |
544 WaitForConnections(); | 548 WaitForConnections(); |
545 | 549 |
546 EXPECT_TRUE(factory()->IsEndpointReachable()); | 550 EXPECT_TRUE(factory()->IsEndpointReachable()); |
547 EXPECT_TRUE(factory()->NextRetryAttempt().is_null()); | 551 EXPECT_TRUE(factory()->NextRetryAttempt().is_null()); |
548 } | 552 } |
549 | 553 |
| 554 // Receiving a network change event before the initial connection should have |
| 555 // no effect. |
| 556 TEST_F(ConnectionFactoryImplTest, NetworkChangeBeforeFirstConnection) { |
| 557 factory()->OnNetworkChanged(net::NetworkChangeNotifier::CONNECTION_4G); |
| 558 factory()->SetConnectResult(net::OK); |
| 559 factory()->Connect(); |
| 560 EXPECT_TRUE(factory()->NextRetryAttempt().is_null()); |
| 561 EXPECT_TRUE(factory()->IsEndpointReachable()); |
| 562 } |
| 563 |
550 } // namespace gcm | 564 } // namespace gcm |
OLD | NEW |