Chromium Code Reviews| Index: google_apis/gcm/engine/mcs_client_unittest.cc |
| diff --git a/google_apis/gcm/engine/mcs_client_unittest.cc b/google_apis/gcm/engine/mcs_client_unittest.cc |
| index 1b6d2ef61fa9e787a1a78ee91a880bfda745b34d..1c6e2c4eb1ea9ec030d235d5451b94ac3b07676b 100644 |
| --- a/google_apis/gcm/engine/mcs_client_unittest.cc |
| +++ b/google_apis/gcm/engine/mcs_client_unittest.cc |
| @@ -113,8 +113,11 @@ class MCSClientTest : public testing::Test { |
| void InitializeClient(); |
| void StoreCredentials(); |
| void LoginClient(const std::vector<std::string>& acknowledged_ids); |
| + void LoginClientWithHeartbeat( |
| + const std::vector<std::string>& acknowledged_ids, |
| + int heartbeat_interval_ms); |
| void AddExpectedLoginRequest(const std::vector<std::string>& acknowledged_ids, |
| - int custom_heartbeat_interval); |
| + int heartbeat_interval_ms); |
| base::SimpleTestClock* clock() { return &clock_; } |
| TestMCSClient* mcs_client() const { return mcs_client_.get(); } |
| @@ -209,7 +212,13 @@ void MCSClientTest::InitializeClient() { |
| void MCSClientTest::LoginClient( |
| const std::vector<std::string>& acknowledged_ids) { |
| - AddExpectedLoginRequest(acknowledged_ids, 0); |
| + LoginClientWithHeartbeat(acknowledged_ids, 0); |
| +} |
| + |
| +void MCSClientTest::LoginClientWithHeartbeat( |
| + const std::vector<std::string>& acknowledged_ids, |
| + int heartbeat_interval_ms) { |
| + AddExpectedLoginRequest(acknowledged_ids, heartbeat_interval_ms); |
| mcs_client_->Login(kAndroidId, kSecurityToken); |
| run_loop_->Run(); |
| run_loop_.reset(new base::RunLoop()); |
| @@ -217,15 +226,15 @@ void MCSClientTest::LoginClient( |
| void MCSClientTest::AddExpectedLoginRequest( |
| const std::vector<std::string>& acknowledged_ids, |
| - int custom_heartbeat_interval) { |
| + int heartbeat_interval_ms) { |
| scoped_ptr<mcs_proto::LoginRequest> login_request = |
| BuildLoginRequest(kAndroidId, kSecurityToken, ""); |
| for (size_t i = 0; i < acknowledged_ids.size(); ++i) |
| login_request->add_received_persistent_id(acknowledged_ids[i]); |
| - if (custom_heartbeat_interval) { |
| + if (heartbeat_interval_ms) { |
| mcs_proto::Setting* setting = login_request->add_setting(); |
| setting->set_name("hbping"); |
| - setting->set_value(base::IntToString(custom_heartbeat_interval)); |
| + setting->set_value(base::IntToString(heartbeat_interval_ms)); |
| } |
| GetFakeHandler()->ExpectOutgoingMessage( |
| MCSMessage(kLoginRequestTag, login_request.Pass())); |
| @@ -907,20 +916,20 @@ TEST_F(MCSClientTest, CustomHeartbeatInterval) { |
| TestConnectionListener test_connection_listener; |
| connection_factory()->SetConnectionListener(&test_connection_listener); |
| - HeartbeatManager& hb_manager = mcs_client()->GetHeartbeatManagerForTesting(); |
| + HeartbeatManager* hb_manager = mcs_client()->GetHeartbeatManagerForTesting(); |
| // By default custom client interval is not set. |
| - EXPECT_FALSE(hb_manager.HasClientHeartbeatInterval()); |
| + EXPECT_FALSE(hb_manager->HasClientHeartbeatInterval()); |
| const std::string component_1 = "component1"; |
| int interval_ms = 30 * 1000; // 30 seconds, too low. |
| mcs_client()->AddHeartbeatInterval(component_1, interval_ms); |
| // Setting was too low so it was ignored. |
| - EXPECT_FALSE(hb_manager.HasClientHeartbeatInterval()); |
| + EXPECT_FALSE(hb_manager->HasClientHeartbeatInterval()); |
| interval_ms = 60 * 60 * 1000; // 1 hour, too high. |
| mcs_client()->AddHeartbeatInterval(component_1, interval_ms); |
| // Setting was too high, again it was ignored. |
| - EXPECT_FALSE(hb_manager.HasClientHeartbeatInterval()); |
| + EXPECT_FALSE(hb_manager->HasClientHeartbeatInterval()); |
| int expected_disconnect_counter = 0; |
| EXPECT_EQ(expected_disconnect_counter, |
| @@ -930,8 +939,8 @@ TEST_F(MCSClientTest, CustomHeartbeatInterval) { |
| AddExpectedLoginRequest(std::vector<std::string>(), interval_ms); |
| mcs_client()->AddHeartbeatInterval(component_1, interval_ms); |
| // Setting was OK. HearbeatManager should get that setting now. |
| - EXPECT_TRUE(hb_manager.HasClientHeartbeatInterval()); |
| - EXPECT_EQ(interval_ms, hb_manager.GetClientHeartbeatIntervalMs()); |
| + EXPECT_TRUE(hb_manager->HasClientHeartbeatInterval()); |
| + EXPECT_EQ(interval_ms, hb_manager->GetClientHeartbeatIntervalMs()); |
| ++expected_disconnect_counter; |
| EXPECT_EQ(expected_disconnect_counter, |
| @@ -942,8 +951,8 @@ TEST_F(MCSClientTest, CustomHeartbeatInterval) { |
| mcs_client()->AddHeartbeatInterval(component_2, other_interval_ms); |
| // Setting was OK, but higher than the previous setting and HearbeatManager |
| // will not be updated. |
| - EXPECT_TRUE(hb_manager.HasClientHeartbeatInterval()); |
| - EXPECT_EQ(interval_ms, hb_manager.GetClientHeartbeatIntervalMs()); |
| + EXPECT_TRUE(hb_manager->HasClientHeartbeatInterval()); |
| + EXPECT_EQ(interval_ms, hb_manager->GetClientHeartbeatIntervalMs()); |
| // No connection reset expected. |
| EXPECT_EQ(expected_disconnect_counter, |
| test_connection_listener.get_disconnect_counter()); |
| @@ -953,44 +962,148 @@ TEST_F(MCSClientTest, CustomHeartbeatInterval) { |
| mcs_client()->AddHeartbeatInterval(component_2, other_interval_ms); |
| // Setting was OK and lower then present setting. HearbeatManager should get |
| // that setting now. |
| - EXPECT_TRUE(hb_manager.HasClientHeartbeatInterval()); |
| - EXPECT_EQ(other_interval_ms, hb_manager.GetClientHeartbeatIntervalMs()); |
| + EXPECT_TRUE(hb_manager->HasClientHeartbeatInterval()); |
| + EXPECT_EQ(other_interval_ms, hb_manager->GetClientHeartbeatIntervalMs()); |
| ++expected_disconnect_counter; |
| EXPECT_EQ(expected_disconnect_counter, |
| test_connection_listener.get_disconnect_counter()); |
| mcs_client()->RemoveHeartbeatInterval(component_2); |
| // Removing the lowest setting reverts to second lowest. |
| - EXPECT_TRUE(hb_manager.HasClientHeartbeatInterval()); |
| - EXPECT_EQ(interval_ms, hb_manager.GetClientHeartbeatIntervalMs()); |
| + EXPECT_TRUE(hb_manager->HasClientHeartbeatInterval()); |
| + EXPECT_EQ(interval_ms, hb_manager->GetClientHeartbeatIntervalMs()); |
| // No connection reset expected. |
| EXPECT_EQ(expected_disconnect_counter, |
| test_connection_listener.get_disconnect_counter()); |
| mcs_client()->RemoveHeartbeatInterval(component_1); |
| // Removing all of the intervals, removes it from the HeartbeatManager. |
| - EXPECT_FALSE(hb_manager.HasClientHeartbeatInterval()); |
| + EXPECT_FALSE(hb_manager->HasClientHeartbeatInterval()); |
| // No connection reset expected. |
| EXPECT_EQ(expected_disconnect_counter, |
| test_connection_listener.get_disconnect_counter()); |
| mcs_client()->AddHeartbeatInterval(component_2, other_interval_ms); |
| mcs_client()->AddHeartbeatInterval(component_1, interval_ms); |
| - EXPECT_TRUE(hb_manager.HasClientHeartbeatInterval()); |
| - EXPECT_EQ(other_interval_ms, hb_manager.GetClientHeartbeatIntervalMs()); |
| + EXPECT_TRUE(hb_manager->HasClientHeartbeatInterval()); |
| + EXPECT_EQ(other_interval_ms, hb_manager->GetClientHeartbeatIntervalMs()); |
| // No connection reset expected. |
| EXPECT_EQ(expected_disconnect_counter, |
| test_connection_listener.get_disconnect_counter()); |
| // Removing interval other than lowest does not change anything. |
| mcs_client()->RemoveHeartbeatInterval(component_1); |
| - EXPECT_TRUE(hb_manager.HasClientHeartbeatInterval()); |
| - EXPECT_EQ(other_interval_ms, hb_manager.GetClientHeartbeatIntervalMs()); |
| + EXPECT_TRUE(hb_manager->HasClientHeartbeatInterval()); |
| + EXPECT_EQ(other_interval_ms, hb_manager->GetClientHeartbeatIntervalMs()); |
| // No connection reset expected. |
| EXPECT_EQ(expected_disconnect_counter, |
| test_connection_listener.get_disconnect_counter()); |
| } |
| +// Tests adding and removing custom heartbeat interval. |
| +TEST_F(MCSClientTest, StoringCustomHeartbeatInterval) { |
|
Nicolas Zea
2015/05/07 18:13:52
Can we break this up into multiple test cases?
Al
fgorski
2015/05/10 06:44:34
Done.
|
| + BuildMCSClient(); |
| + InitializeClient(); |
| + LoginClient(std::vector<std::string>()); |
| + PumpLoop(); |
| + StoreCredentials(); |
| + |
| + const std::string component_1 = "component1"; |
| + int interval_ms = 30 * 1000; // 30 seconds, too low. |
| + mcs_client()->AddHeartbeatInterval(component_1, interval_ms); |
| + // Setting was too low so it was ignored. |
| + |
| + BuildMCSClient(); |
| + InitializeClient(); |
| + LoginClientWithHeartbeat(std::vector<std::string>(), 0); |
| + PumpLoop(); |
| + |
| + HeartbeatManager* hb_manager = mcs_client()->GetHeartbeatManagerForTesting(); |
| + EXPECT_FALSE(hb_manager->HasClientHeartbeatInterval()); |
| + |
| + interval_ms = 60 * 60 * 1000; // 1 hour, too high. |
| + mcs_client()->AddHeartbeatInterval(component_1, interval_ms); |
| + |
| + BuildMCSClient(); |
| + InitializeClient(); |
| + LoginClientWithHeartbeat(std::vector<std::string>(), 0); |
| + PumpLoop(); |
| + |
| + // Setting was too high, again it was ignored. |
| + hb_manager = mcs_client()->GetHeartbeatManagerForTesting(); |
| + EXPECT_FALSE(hb_manager->HasClientHeartbeatInterval()); |
| + |
| + interval_ms = 5 * 60 * 1000; // 5 minutes. A valid setting. |
| + AddExpectedLoginRequest(std::vector<std::string>(), interval_ms); |
| + mcs_client()->AddHeartbeatInterval(component_1, interval_ms); |
| + PumpLoop(); |
| + |
| + BuildMCSClient(); |
| + InitializeClient(); |
| + LoginClientWithHeartbeat(std::vector<std::string>(), interval_ms); |
| + PumpLoop(); |
| + |
| + // Setting was OK. HearbeatManager should get that setting now. |
| + hb_manager = mcs_client()->GetHeartbeatManagerForTesting(); |
| + EXPECT_TRUE(hb_manager->HasClientHeartbeatInterval()); |
| + EXPECT_EQ(interval_ms, hb_manager->GetClientHeartbeatIntervalMs()); |
| + |
| + const std::string component_2 = "component2"; |
| + int other_interval_ms = 10 * 60 * 1000; // 10 minutes. A valid setting. |
| + mcs_client()->AddHeartbeatInterval(component_2, other_interval_ms); |
| + |
| + BuildMCSClient(); |
| + InitializeClient(); |
| + LoginClientWithHeartbeat(std::vector<std::string>(), interval_ms); |
| + PumpLoop(); |
| + |
| + // Setting was OK, but higher than the previous setting and HearbeatManager |
| + // will not be updated. |
| + hb_manager = mcs_client()->GetHeartbeatManagerForTesting(); |
| + EXPECT_TRUE(hb_manager->HasClientHeartbeatInterval()); |
| + EXPECT_EQ(interval_ms, hb_manager->GetClientHeartbeatIntervalMs()); |
| + |
| + other_interval_ms = 3 * 60 * 1000; // 3 minutes. A valid setting. |
| + AddExpectedLoginRequest(std::vector<std::string>(), other_interval_ms); |
| + mcs_client()->AddHeartbeatInterval(component_2, other_interval_ms); |
| + PumpLoop(); |
| + |
| + BuildMCSClient(); |
| + InitializeClient(); |
| + LoginClientWithHeartbeat(std::vector<std::string>(), other_interval_ms); |
| + PumpLoop(); |
| + |
| + // Setting was OK and lower then present setting. HearbeatManager should get |
| + // that setting now. |
| + hb_manager = mcs_client()->GetHeartbeatManagerForTesting(); |
| + EXPECT_TRUE(hb_manager->HasClientHeartbeatInterval()); |
| + EXPECT_EQ(other_interval_ms, hb_manager->GetClientHeartbeatIntervalMs()); |
| + |
| + mcs_client()->RemoveHeartbeatInterval(component_2); |
| + |
| + BuildMCSClient(); |
| + InitializeClient(); |
| + LoginClientWithHeartbeat(std::vector<std::string>(), interval_ms); |
| + PumpLoop(); |
| + |
| + // Heartbeat interval from component 1 is in effect at restart. |
| + hb_manager = mcs_client()->GetHeartbeatManagerForTesting(); |
| + EXPECT_TRUE(hb_manager->HasClientHeartbeatInterval()); |
| + EXPECT_EQ(interval_ms, hb_manager->GetClientHeartbeatIntervalMs()); |
| + |
| + mcs_client()->RemoveHeartbeatInterval(component_1); |
| + |
| + BuildMCSClient(); |
| + InitializeClient(); |
| + LoginClientWithHeartbeat(std::vector<std::string>(), 0); |
| + PumpLoop(); |
| + |
| + // All of the intervals were removed, so nothing should be set on hb_manager |
| + // after restart. |
| + hb_manager = mcs_client()->GetHeartbeatManagerForTesting(); |
| + EXPECT_FALSE(hb_manager->HasClientHeartbeatInterval()); |
| +} |
| + |
| } // namespace |
| } // namespace gcm |