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

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

Issue 1124783002: [GCM] Wiring heartbeat interval calls to GCMDriver (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@store-heartbeat-gcm
Patch Set: Adding MCS client tests for storing heartbeat interval Created 5 years, 7 months 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 2013 The Chromium Authors. All rights reserved. 1 // Copyright 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/mcs_client.h" 5 #include "google_apis/gcm/engine/mcs_client.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
9 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/files/scoped_temp_dir.h" 10 #include "base/files/scoped_temp_dir.h"
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
106 public: 106 public:
107 MCSClientTest(); 107 MCSClientTest();
108 ~MCSClientTest() override; 108 ~MCSClientTest() override;
109 109
110 void SetUp() override; 110 void SetUp() override;
111 111
112 void BuildMCSClient(); 112 void BuildMCSClient();
113 void InitializeClient(); 113 void InitializeClient();
114 void StoreCredentials(); 114 void StoreCredentials();
115 void LoginClient(const std::vector<std::string>& acknowledged_ids); 115 void LoginClient(const std::vector<std::string>& acknowledged_ids);
116 void LoginClientWithHeartbeat(
117 const std::vector<std::string>& acknowledged_ids,
118 int heartbeat_interval_ms);
116 void AddExpectedLoginRequest(const std::vector<std::string>& acknowledged_ids, 119 void AddExpectedLoginRequest(const std::vector<std::string>& acknowledged_ids,
117 int custom_heartbeat_interval); 120 int heartbeat_interval_ms);
118 121
119 base::SimpleTestClock* clock() { return &clock_; } 122 base::SimpleTestClock* clock() { return &clock_; }
120 TestMCSClient* mcs_client() const { return mcs_client_.get(); } 123 TestMCSClient* mcs_client() const { return mcs_client_.get(); }
121 FakeConnectionFactory* connection_factory() { 124 FakeConnectionFactory* connection_factory() {
122 return &connection_factory_; 125 return &connection_factory_;
123 } 126 }
124 bool init_success() const { return init_success_; } 127 bool init_success() const { return init_success_; }
125 uint64 restored_android_id() const { return restored_android_id_; } 128 uint64 restored_android_id() const { return restored_android_id_; }
126 uint64 restored_security_token() const { return restored_security_token_; } 129 uint64 restored_security_token() const { return restored_security_token_; }
127 MCSMessage* received_message() const { return received_message_.get(); } 130 MCSMessage* received_message() const { return received_message_.get(); }
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
202 base::Unretained(this)), 205 base::Unretained(this)),
203 base::Bind(&MCSClientTest::MessageReceivedCallback, 206 base::Bind(&MCSClientTest::MessageReceivedCallback,
204 base::Unretained(this)), 207 base::Unretained(this)),
205 base::Bind(&MCSClientTest::MessageSentCallback, base::Unretained(this)))); 208 base::Bind(&MCSClientTest::MessageSentCallback, base::Unretained(this))));
206 run_loop_->RunUntilIdle(); 209 run_loop_->RunUntilIdle();
207 run_loop_.reset(new base::RunLoop()); 210 run_loop_.reset(new base::RunLoop());
208 } 211 }
209 212
210 void MCSClientTest::LoginClient( 213 void MCSClientTest::LoginClient(
211 const std::vector<std::string>& acknowledged_ids) { 214 const std::vector<std::string>& acknowledged_ids) {
212 AddExpectedLoginRequest(acknowledged_ids, 0); 215 LoginClientWithHeartbeat(acknowledged_ids, 0);
216 }
217
218 void MCSClientTest::LoginClientWithHeartbeat(
219 const std::vector<std::string>& acknowledged_ids,
220 int heartbeat_interval_ms) {
221 AddExpectedLoginRequest(acknowledged_ids, heartbeat_interval_ms);
213 mcs_client_->Login(kAndroidId, kSecurityToken); 222 mcs_client_->Login(kAndroidId, kSecurityToken);
214 run_loop_->Run(); 223 run_loop_->Run();
215 run_loop_.reset(new base::RunLoop()); 224 run_loop_.reset(new base::RunLoop());
216 } 225 }
217 226
218 void MCSClientTest::AddExpectedLoginRequest( 227 void MCSClientTest::AddExpectedLoginRequest(
219 const std::vector<std::string>& acknowledged_ids, 228 const std::vector<std::string>& acknowledged_ids,
220 int custom_heartbeat_interval) { 229 int heartbeat_interval_ms) {
221 scoped_ptr<mcs_proto::LoginRequest> login_request = 230 scoped_ptr<mcs_proto::LoginRequest> login_request =
222 BuildLoginRequest(kAndroidId, kSecurityToken, ""); 231 BuildLoginRequest(kAndroidId, kSecurityToken, "");
223 for (size_t i = 0; i < acknowledged_ids.size(); ++i) 232 for (size_t i = 0; i < acknowledged_ids.size(); ++i)
224 login_request->add_received_persistent_id(acknowledged_ids[i]); 233 login_request->add_received_persistent_id(acknowledged_ids[i]);
225 if (custom_heartbeat_interval) { 234 if (heartbeat_interval_ms) {
226 mcs_proto::Setting* setting = login_request->add_setting(); 235 mcs_proto::Setting* setting = login_request->add_setting();
227 setting->set_name("hbping"); 236 setting->set_name("hbping");
228 setting->set_value(base::IntToString(custom_heartbeat_interval)); 237 setting->set_value(base::IntToString(heartbeat_interval_ms));
229 } 238 }
230 GetFakeHandler()->ExpectOutgoingMessage( 239 GetFakeHandler()->ExpectOutgoingMessage(
231 MCSMessage(kLoginRequestTag, login_request.Pass())); 240 MCSMessage(kLoginRequestTag, login_request.Pass()));
232 } 241 }
233 242
234 void MCSClientTest::StoreCredentials() { 243 void MCSClientTest::StoreCredentials() {
235 gcm_store_->SetDeviceCredentials( 244 gcm_store_->SetDeviceCredentials(
236 kAndroidId, kSecurityToken, 245 kAndroidId, kSecurityToken,
237 base::Bind(&MCSClientTest::SetDeviceCredentialsCallback, 246 base::Bind(&MCSClientTest::SetDeviceCredentialsCallback,
238 base::Unretained(this))); 247 base::Unretained(this)));
(...skipping 661 matching lines...) Expand 10 before | Expand all | Expand 10 after
900 909
901 // Tests adding and removing custom heartbeat interval. 910 // Tests adding and removing custom heartbeat interval.
902 TEST_F(MCSClientTest, CustomHeartbeatInterval) { 911 TEST_F(MCSClientTest, CustomHeartbeatInterval) {
903 BuildMCSClient(); 912 BuildMCSClient();
904 InitializeClient(); 913 InitializeClient();
905 LoginClient(std::vector<std::string>()); 914 LoginClient(std::vector<std::string>());
906 915
907 TestConnectionListener test_connection_listener; 916 TestConnectionListener test_connection_listener;
908 connection_factory()->SetConnectionListener(&test_connection_listener); 917 connection_factory()->SetConnectionListener(&test_connection_listener);
909 918
910 HeartbeatManager& hb_manager = mcs_client()->GetHeartbeatManagerForTesting(); 919 HeartbeatManager* hb_manager = mcs_client()->GetHeartbeatManagerForTesting();
911 // By default custom client interval is not set. 920 // By default custom client interval is not set.
912 EXPECT_FALSE(hb_manager.HasClientHeartbeatInterval()); 921 EXPECT_FALSE(hb_manager->HasClientHeartbeatInterval());
913 922
914 const std::string component_1 = "component1"; 923 const std::string component_1 = "component1";
915 int interval_ms = 30 * 1000; // 30 seconds, too low. 924 int interval_ms = 30 * 1000; // 30 seconds, too low.
916 mcs_client()->AddHeartbeatInterval(component_1, interval_ms); 925 mcs_client()->AddHeartbeatInterval(component_1, interval_ms);
917 // Setting was too low so it was ignored. 926 // Setting was too low so it was ignored.
918 EXPECT_FALSE(hb_manager.HasClientHeartbeatInterval()); 927 EXPECT_FALSE(hb_manager->HasClientHeartbeatInterval());
919 928
920 interval_ms = 60 * 60 * 1000; // 1 hour, too high. 929 interval_ms = 60 * 60 * 1000; // 1 hour, too high.
921 mcs_client()->AddHeartbeatInterval(component_1, interval_ms); 930 mcs_client()->AddHeartbeatInterval(component_1, interval_ms);
922 // Setting was too high, again it was ignored. 931 // Setting was too high, again it was ignored.
923 EXPECT_FALSE(hb_manager.HasClientHeartbeatInterval()); 932 EXPECT_FALSE(hb_manager->HasClientHeartbeatInterval());
924 933
925 int expected_disconnect_counter = 0; 934 int expected_disconnect_counter = 0;
926 EXPECT_EQ(expected_disconnect_counter, 935 EXPECT_EQ(expected_disconnect_counter,
927 test_connection_listener.get_disconnect_counter()); 936 test_connection_listener.get_disconnect_counter());
928 937
929 interval_ms = 5 * 60 * 1000; // 5 minutes. A valid setting. 938 interval_ms = 5 * 60 * 1000; // 5 minutes. A valid setting.
930 AddExpectedLoginRequest(std::vector<std::string>(), interval_ms); 939 AddExpectedLoginRequest(std::vector<std::string>(), interval_ms);
931 mcs_client()->AddHeartbeatInterval(component_1, interval_ms); 940 mcs_client()->AddHeartbeatInterval(component_1, interval_ms);
932 // Setting was OK. HearbeatManager should get that setting now. 941 // Setting was OK. HearbeatManager should get that setting now.
933 EXPECT_TRUE(hb_manager.HasClientHeartbeatInterval()); 942 EXPECT_TRUE(hb_manager->HasClientHeartbeatInterval());
934 EXPECT_EQ(interval_ms, hb_manager.GetClientHeartbeatIntervalMs()); 943 EXPECT_EQ(interval_ms, hb_manager->GetClientHeartbeatIntervalMs());
935 944
936 ++expected_disconnect_counter; 945 ++expected_disconnect_counter;
937 EXPECT_EQ(expected_disconnect_counter, 946 EXPECT_EQ(expected_disconnect_counter,
938 test_connection_listener.get_disconnect_counter()); 947 test_connection_listener.get_disconnect_counter());
939 948
940 const std::string component_2 = "component2"; 949 const std::string component_2 = "component2";
941 int other_interval_ms = 10 * 60 * 1000; // 10 minutes. A valid setting. 950 int other_interval_ms = 10 * 60 * 1000; // 10 minutes. A valid setting.
942 mcs_client()->AddHeartbeatInterval(component_2, other_interval_ms); 951 mcs_client()->AddHeartbeatInterval(component_2, other_interval_ms);
943 // Setting was OK, but higher than the previous setting and HearbeatManager 952 // Setting was OK, but higher than the previous setting and HearbeatManager
944 // will not be updated. 953 // will not be updated.
945 EXPECT_TRUE(hb_manager.HasClientHeartbeatInterval()); 954 EXPECT_TRUE(hb_manager->HasClientHeartbeatInterval());
946 EXPECT_EQ(interval_ms, hb_manager.GetClientHeartbeatIntervalMs()); 955 EXPECT_EQ(interval_ms, hb_manager->GetClientHeartbeatIntervalMs());
947 // No connection reset expected. 956 // No connection reset expected.
948 EXPECT_EQ(expected_disconnect_counter, 957 EXPECT_EQ(expected_disconnect_counter,
949 test_connection_listener.get_disconnect_counter()); 958 test_connection_listener.get_disconnect_counter());
950 959
951 other_interval_ms = 3 * 60 * 1000; // 3 minutes. A valid setting. 960 other_interval_ms = 3 * 60 * 1000; // 3 minutes. A valid setting.
952 AddExpectedLoginRequest(std::vector<std::string>(), other_interval_ms); 961 AddExpectedLoginRequest(std::vector<std::string>(), other_interval_ms);
953 mcs_client()->AddHeartbeatInterval(component_2, other_interval_ms); 962 mcs_client()->AddHeartbeatInterval(component_2, other_interval_ms);
954 // Setting was OK and lower then present setting. HearbeatManager should get 963 // Setting was OK and lower then present setting. HearbeatManager should get
955 // that setting now. 964 // that setting now.
956 EXPECT_TRUE(hb_manager.HasClientHeartbeatInterval()); 965 EXPECT_TRUE(hb_manager->HasClientHeartbeatInterval());
957 EXPECT_EQ(other_interval_ms, hb_manager.GetClientHeartbeatIntervalMs()); 966 EXPECT_EQ(other_interval_ms, hb_manager->GetClientHeartbeatIntervalMs());
958 ++expected_disconnect_counter; 967 ++expected_disconnect_counter;
959 EXPECT_EQ(expected_disconnect_counter, 968 EXPECT_EQ(expected_disconnect_counter,
960 test_connection_listener.get_disconnect_counter()); 969 test_connection_listener.get_disconnect_counter());
961 970
962 mcs_client()->RemoveHeartbeatInterval(component_2); 971 mcs_client()->RemoveHeartbeatInterval(component_2);
963 // Removing the lowest setting reverts to second lowest. 972 // Removing the lowest setting reverts to second lowest.
964 EXPECT_TRUE(hb_manager.HasClientHeartbeatInterval()); 973 EXPECT_TRUE(hb_manager->HasClientHeartbeatInterval());
965 EXPECT_EQ(interval_ms, hb_manager.GetClientHeartbeatIntervalMs()); 974 EXPECT_EQ(interval_ms, hb_manager->GetClientHeartbeatIntervalMs());
966 // No connection reset expected. 975 // No connection reset expected.
967 EXPECT_EQ(expected_disconnect_counter, 976 EXPECT_EQ(expected_disconnect_counter,
968 test_connection_listener.get_disconnect_counter()); 977 test_connection_listener.get_disconnect_counter());
969 978
970 mcs_client()->RemoveHeartbeatInterval(component_1); 979 mcs_client()->RemoveHeartbeatInterval(component_1);
971 // Removing all of the intervals, removes it from the HeartbeatManager. 980 // Removing all of the intervals, removes it from the HeartbeatManager.
972 EXPECT_FALSE(hb_manager.HasClientHeartbeatInterval()); 981 EXPECT_FALSE(hb_manager->HasClientHeartbeatInterval());
973 // No connection reset expected. 982 // No connection reset expected.
974 EXPECT_EQ(expected_disconnect_counter, 983 EXPECT_EQ(expected_disconnect_counter,
975 test_connection_listener.get_disconnect_counter()); 984 test_connection_listener.get_disconnect_counter());
976 985
977 mcs_client()->AddHeartbeatInterval(component_2, other_interval_ms); 986 mcs_client()->AddHeartbeatInterval(component_2, other_interval_ms);
978 mcs_client()->AddHeartbeatInterval(component_1, interval_ms); 987 mcs_client()->AddHeartbeatInterval(component_1, interval_ms);
979 EXPECT_TRUE(hb_manager.HasClientHeartbeatInterval()); 988 EXPECT_TRUE(hb_manager->HasClientHeartbeatInterval());
980 EXPECT_EQ(other_interval_ms, hb_manager.GetClientHeartbeatIntervalMs()); 989 EXPECT_EQ(other_interval_ms, hb_manager->GetClientHeartbeatIntervalMs());
981 // No connection reset expected. 990 // No connection reset expected.
982 EXPECT_EQ(expected_disconnect_counter, 991 EXPECT_EQ(expected_disconnect_counter,
983 test_connection_listener.get_disconnect_counter()); 992 test_connection_listener.get_disconnect_counter());
984 993
985 // Removing interval other than lowest does not change anything. 994 // Removing interval other than lowest does not change anything.
986 mcs_client()->RemoveHeartbeatInterval(component_1); 995 mcs_client()->RemoveHeartbeatInterval(component_1);
987 EXPECT_TRUE(hb_manager.HasClientHeartbeatInterval()); 996 EXPECT_TRUE(hb_manager->HasClientHeartbeatInterval());
988 EXPECT_EQ(other_interval_ms, hb_manager.GetClientHeartbeatIntervalMs()); 997 EXPECT_EQ(other_interval_ms, hb_manager->GetClientHeartbeatIntervalMs());
989 // No connection reset expected. 998 // No connection reset expected.
990 EXPECT_EQ(expected_disconnect_counter, 999 EXPECT_EQ(expected_disconnect_counter,
991 test_connection_listener.get_disconnect_counter()); 1000 test_connection_listener.get_disconnect_counter());
992 } 1001 }
993 1002
1003 // Tests adding and removing custom heartbeat interval.
1004 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.
1005 BuildMCSClient();
1006 InitializeClient();
1007 LoginClient(std::vector<std::string>());
1008 PumpLoop();
1009 StoreCredentials();
1010
1011 const std::string component_1 = "component1";
1012 int interval_ms = 30 * 1000; // 30 seconds, too low.
1013 mcs_client()->AddHeartbeatInterval(component_1, interval_ms);
1014 // Setting was too low so it was ignored.
1015
1016 BuildMCSClient();
1017 InitializeClient();
1018 LoginClientWithHeartbeat(std::vector<std::string>(), 0);
1019 PumpLoop();
1020
1021 HeartbeatManager* hb_manager = mcs_client()->GetHeartbeatManagerForTesting();
1022 EXPECT_FALSE(hb_manager->HasClientHeartbeatInterval());
1023
1024 interval_ms = 60 * 60 * 1000; // 1 hour, too high.
1025 mcs_client()->AddHeartbeatInterval(component_1, interval_ms);
1026
1027 BuildMCSClient();
1028 InitializeClient();
1029 LoginClientWithHeartbeat(std::vector<std::string>(), 0);
1030 PumpLoop();
1031
1032 // Setting was too high, again it was ignored.
1033 hb_manager = mcs_client()->GetHeartbeatManagerForTesting();
1034 EXPECT_FALSE(hb_manager->HasClientHeartbeatInterval());
1035
1036 interval_ms = 5 * 60 * 1000; // 5 minutes. A valid setting.
1037 AddExpectedLoginRequest(std::vector<std::string>(), interval_ms);
1038 mcs_client()->AddHeartbeatInterval(component_1, interval_ms);
1039 PumpLoop();
1040
1041 BuildMCSClient();
1042 InitializeClient();
1043 LoginClientWithHeartbeat(std::vector<std::string>(), interval_ms);
1044 PumpLoop();
1045
1046 // Setting was OK. HearbeatManager should get that setting now.
1047 hb_manager = mcs_client()->GetHeartbeatManagerForTesting();
1048 EXPECT_TRUE(hb_manager->HasClientHeartbeatInterval());
1049 EXPECT_EQ(interval_ms, hb_manager->GetClientHeartbeatIntervalMs());
1050
1051 const std::string component_2 = "component2";
1052 int other_interval_ms = 10 * 60 * 1000; // 10 minutes. A valid setting.
1053 mcs_client()->AddHeartbeatInterval(component_2, other_interval_ms);
1054
1055 BuildMCSClient();
1056 InitializeClient();
1057 LoginClientWithHeartbeat(std::vector<std::string>(), interval_ms);
1058 PumpLoop();
1059
1060 // Setting was OK, but higher than the previous setting and HearbeatManager
1061 // will not be updated.
1062 hb_manager = mcs_client()->GetHeartbeatManagerForTesting();
1063 EXPECT_TRUE(hb_manager->HasClientHeartbeatInterval());
1064 EXPECT_EQ(interval_ms, hb_manager->GetClientHeartbeatIntervalMs());
1065
1066 other_interval_ms = 3 * 60 * 1000; // 3 minutes. A valid setting.
1067 AddExpectedLoginRequest(std::vector<std::string>(), other_interval_ms);
1068 mcs_client()->AddHeartbeatInterval(component_2, other_interval_ms);
1069 PumpLoop();
1070
1071 BuildMCSClient();
1072 InitializeClient();
1073 LoginClientWithHeartbeat(std::vector<std::string>(), other_interval_ms);
1074 PumpLoop();
1075
1076 // Setting was OK and lower then present setting. HearbeatManager should get
1077 // that setting now.
1078 hb_manager = mcs_client()->GetHeartbeatManagerForTesting();
1079 EXPECT_TRUE(hb_manager->HasClientHeartbeatInterval());
1080 EXPECT_EQ(other_interval_ms, hb_manager->GetClientHeartbeatIntervalMs());
1081
1082 mcs_client()->RemoveHeartbeatInterval(component_2);
1083
1084 BuildMCSClient();
1085 InitializeClient();
1086 LoginClientWithHeartbeat(std::vector<std::string>(), interval_ms);
1087 PumpLoop();
1088
1089 // Heartbeat interval from component 1 is in effect at restart.
1090 hb_manager = mcs_client()->GetHeartbeatManagerForTesting();
1091 EXPECT_TRUE(hb_manager->HasClientHeartbeatInterval());
1092 EXPECT_EQ(interval_ms, hb_manager->GetClientHeartbeatIntervalMs());
1093
1094 mcs_client()->RemoveHeartbeatInterval(component_1);
1095
1096 BuildMCSClient();
1097 InitializeClient();
1098 LoginClientWithHeartbeat(std::vector<std::string>(), 0);
1099 PumpLoop();
1100
1101 // All of the intervals were removed, so nothing should be set on hb_manager
1102 // after restart.
1103 hb_manager = mcs_client()->GetHeartbeatManagerForTesting();
1104 EXPECT_FALSE(hb_manager->HasClientHeartbeatInterval());
1105 }
1106
994 } // namespace 1107 } // namespace
995 1108
996 } // namespace gcm 1109 } // namespace gcm
OLDNEW
« google_apis/gcm/engine/mcs_client.cc ('K') | « google_apis/gcm/engine/mcs_client.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698