Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(95)

Side by Side Diff: google_apis/gcm/engine/connection_factory_impl_unittest.cc

Issue 2481873002: Added ClientEvent proto and structure for storing events in the factory. (Closed)
Patch Set: Rebase and address code review comments. Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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;
104 104
105 // Helpers for verifying connection attempts are made. Connection results 105 // Helpers for verifying connection attempts are made. Connection results
106 // must be consumed. 106 // must be consumed.
107 void SetConnectResult(int connect_result); 107 void SetConnectResult(int connect_result);
108 void SetMultipleConnectResults(int connect_result, int num_expected_attempts); 108 void SetMultipleConnectResults(int connect_result, int num_expected_attempts);
109 109
110 // Force a login handshake to be delayed. 110 // Force a login handshake to be delayed.
111 void SetDelayLogin(bool delay_login); 111 void SetDelayLogin(bool delay_login);
112 112
113 // Simulate a socket error. 113 // Simulate a socket error.
114 void SetSocketError(); 114 void SetSocketError();
115 115
116 // Get the client events recorded by the event tracker.
117 const google::protobuf::RepeatedPtrField<mcs_proto::ClientEvent>
118 GetClientEventsForTest() {
Peter Beverloo 2016/12/05 16:29:35 nit: I'd drop "ForTest" since you're in a test. Up
harkness 2016/12/06 12:53:35 Done.
119 mcs_proto::LoginRequest login_request;
120 GetEventTrackerForTesting()->WriteToLoginRequest(&login_request);
121 return login_request.client_event();
122 }
123
116 base::SimpleTestTickClock* tick_clock() { return &tick_clock_; } 124 base::SimpleTestTickClock* tick_clock() { return &tick_clock_; }
117 125
118 private: 126 private:
119 // Clock for controlling delay. 127 // Clock for controlling delay.
120 base::SimpleTestTickClock tick_clock_; 128 base::SimpleTestTickClock tick_clock_;
121 // The result to return on the next connect attempt. 129 // The result to return on the next connect attempt.
122 int connect_result_; 130 int connect_result_;
123 // The number of expected connection attempts; 131 // The number of expected connection attempts;
124 int num_expected_attempts_; 132 int num_expected_attempts_;
125 // Whether all expected connection attempts have been fulfilled since an 133 // Whether all expected connection attempts have been fulfilled since an
(...skipping 30 matching lines...) Expand all
156 base::Bind(&WriteContinuation))), 164 base::Bind(&WriteContinuation))),
157 fake_handler_(scoped_handler_.get()) { 165 fake_handler_(scoped_handler_.get()) {
158 // Set a non-null time. 166 // Set a non-null time.
159 tick_clock_.Advance(base::TimeDelta::FromMilliseconds(1)); 167 tick_clock_.Advance(base::TimeDelta::FromMilliseconds(1));
160 } 168 }
161 169
162 TestConnectionFactoryImpl::~TestConnectionFactoryImpl() { 170 TestConnectionFactoryImpl::~TestConnectionFactoryImpl() {
163 EXPECT_EQ(0, num_expected_attempts_); 171 EXPECT_EQ(0, num_expected_attempts_);
164 } 172 }
165 173
166 void TestConnectionFactoryImpl::ConnectImpl() { 174 void TestConnectionFactoryImpl::StartConnection() {
167 ASSERT_GT(num_expected_attempts_, 0); 175 ASSERT_GT(num_expected_attempts_, 0);
168 ASSERT_FALSE(GetConnectionHandler()->CanSendMessage()); 176 ASSERT_FALSE(GetConnectionHandler()->CanSendMessage());
169 std::unique_ptr<mcs_proto::LoginRequest> request(BuildLoginRequest(0, 0, "")); 177 std::unique_ptr<mcs_proto::LoginRequest> request(BuildLoginRequest(0, 0, ""));
170 GetConnectionHandler()->Init(*request, NULL); 178 GetConnectionHandler()->Init(*request, NULL);
171 OnConnectDone(connect_result_); 179 OnConnectDone(connect_result_);
172 if (!NextRetryAttempt().is_null()) { 180 if (!NextRetryAttempt().is_null()) {
173 // Advance the time to the next retry time. 181 // Advance the time to the next retry time.
174 base::TimeDelta time_till_retry = 182 base::TimeDelta time_till_retry =
175 NextRetryAttempt() - tick_clock_.NowTicks(); 183 NextRetryAttempt() - tick_clock_.NowTicks();
176 tick_clock_.Advance(time_till_retry); 184 tick_clock_.Advance(time_till_retry);
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 = factory()->GetClientEventsForTest();
Peter Beverloo 2016/12/05 16:29:35 nit (and on line 395, 603 and 617): GetClientEvent
harkness 2016/12/06 12:53:35 Done.
381 ASSERT_EQ(kNumAttempts, client_events.size());
382
383 for (const auto client_event : client_events) {
Peter Beverloo 2016/12/05 16:29:35 nit (and on line 606): No need to copy the individ
harkness 2016/12/06 12:53:35 Done.
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 = factory()->GetClientEventsForTest();
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
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 = factory()->GetClientEventsForTest();
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 = factory()->GetClientEventsForTest();
618 ASSERT_EQ(0, new_client_events.size());
619 }
620
572 } // namespace gcm 621 } // namespace gcm
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698