OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/heartbeat_manager.h" | 5 #include "google_apis/gcm/engine/heartbeat_manager.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/memory/scoped_ptr.h" |
9 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
10 #include "base/time/time.h" | 11 #include "base/time/time.h" |
11 #include "base/timer/timer.h" | 12 #include "base/timer/timer.h" |
12 #include "google_apis/gcm/protocol/mcs.pb.h" | 13 #include "google_apis/gcm/protocol/mcs.pb.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
14 | 15 |
15 namespace gcm { | 16 namespace gcm { |
16 | 17 |
17 namespace { | 18 namespace { |
18 | 19 |
19 mcs_proto::HeartbeatConfig BuildHeartbeatConfig(int interval_ms) { | 20 mcs_proto::HeartbeatConfig BuildHeartbeatConfig(int interval_ms) { |
20 mcs_proto::HeartbeatConfig config; | 21 mcs_proto::HeartbeatConfig config; |
21 config.set_interval_ms(interval_ms); | 22 config.set_interval_ms(interval_ms); |
22 return config; | 23 return config; |
23 } | 24 } |
24 | 25 |
25 class TestHeartbeatManager : public HeartbeatManager { | 26 class TestHeartbeatManager : public HeartbeatManager { |
26 public: | 27 public: |
27 TestHeartbeatManager() | 28 TestHeartbeatManager() {} |
28 : HeartbeatManager(make_scoped_ptr( | |
29 new base::Timer(true, /* retain user task */ | |
30 false /* non repeating */))) {} | |
31 virtual ~TestHeartbeatManager() {} | 29 virtual ~TestHeartbeatManager() {} |
32 | 30 |
33 // Bypass the heartbeat timer, and send the heartbeat now. | 31 // Bypass the heartbeat timer, and send the heartbeat now. |
34 void TriggerHearbeat(); | 32 void TriggerHearbeat(); |
35 | 33 |
36 // Check for a missed heartbeat now. | 34 // Check for a missed heartbeat now. |
37 void TriggerMissedHeartbeatCheck(); | 35 void TriggerMissedHeartbeatCheck(); |
38 }; | 36 }; |
39 | 37 |
40 void TestHeartbeatManager::TriggerHearbeat() { | 38 void TestHeartbeatManager::TriggerHearbeat() { |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
168 // Triggering and acking the heartbeat should result in a heartbeat being | 166 // Triggering and acking the heartbeat should result in a heartbeat being |
169 // posted with the new interval. | 167 // posted with the new interval. |
170 manager()->TriggerHearbeat(); | 168 manager()->TriggerHearbeat(); |
171 manager()->OnHeartbeatAcked(); | 169 manager()->OnHeartbeatAcked(); |
172 | 170 |
173 EXPECT_LE(manager()->GetNextHeartbeatTime() - base::TimeTicks::Now(), | 171 EXPECT_LE(manager()->GetNextHeartbeatTime() - base::TimeTicks::Now(), |
174 base::TimeDelta::FromMilliseconds(kIntervalMs)); | 172 base::TimeDelta::FromMilliseconds(kIntervalMs)); |
175 EXPECT_NE(heartbeat, manager()->GetNextHeartbeatTime()); | 173 EXPECT_NE(heartbeat, manager()->GetNextHeartbeatTime()); |
176 } | 174 } |
177 | 175 |
| 176 // Updating the timer used for heartbeats before starting should not start the |
| 177 // timer. |
| 178 TEST_F(HeartbeatManagerTest, UpdateTimerBeforeStart) { |
| 179 manager()->UpdateHeartbeatTimer( |
| 180 make_scoped_ptr(new base::Timer(true, false))); |
| 181 EXPECT_TRUE(manager()->GetNextHeartbeatTime().is_null()); |
| 182 } |
| 183 |
| 184 // Updating the timer used for heartbeats after starting should restart the |
| 185 // timer but not increase the heartbeat time by more than a millisecond. |
| 186 TEST_F(HeartbeatManagerTest, UpdateTimerAfterStart) { |
| 187 StartManager(); |
| 188 base::TimeTicks heartbeat = manager()->GetNextHeartbeatTime(); |
| 189 |
| 190 manager()->UpdateHeartbeatTimer( |
| 191 make_scoped_ptr(new base::Timer(true, false))); |
| 192 EXPECT_LT(manager()->GetNextHeartbeatTime() - heartbeat, |
| 193 base::TimeDelta::FromMilliseconds(1)); |
| 194 } |
| 195 |
178 // Stopping the manager should reset the heartbeat timer. | 196 // Stopping the manager should reset the heartbeat timer. |
179 TEST_F(HeartbeatManagerTest, Stop) { | 197 TEST_F(HeartbeatManagerTest, Stop) { |
180 StartManager(); | 198 StartManager(); |
181 EXPECT_GT(manager()->GetNextHeartbeatTime(), base::TimeTicks::Now()); | 199 EXPECT_GT(manager()->GetNextHeartbeatTime(), base::TimeTicks::Now()); |
182 | 200 |
183 manager()->Stop(); | 201 manager()->Stop(); |
184 EXPECT_TRUE(manager()->GetNextHeartbeatTime().is_null()); | 202 EXPECT_TRUE(manager()->GetNextHeartbeatTime().is_null()); |
185 } | 203 } |
186 | 204 |
187 // Simulate missing a heartbeat by manually invoking the check method. The | 205 // Simulate missing a heartbeat by manually invoking the check method. The |
188 // heartbeat should only be triggered once, and only if the heartbeat timer | 206 // heartbeat should only be triggered once, and only if the heartbeat timer |
189 // is running. Because the period is several minutes, none should fire. | 207 // is running. Because the period is several minutes, none should fire. |
190 TEST_F(HeartbeatManagerTest, MissedHeartbeat) { | 208 TEST_F(HeartbeatManagerTest, MissedHeartbeat) { |
191 // Do nothing while stopped. | 209 // Do nothing while stopped. |
192 manager()->TriggerMissedHeartbeatCheck(); | 210 manager()->TriggerMissedHeartbeatCheck(); |
193 StartManager(); | 211 StartManager(); |
194 EXPECT_EQ(0, heartbeats_sent()); | 212 EXPECT_EQ(0, heartbeats_sent()); |
195 | 213 |
196 // Do nothing before the period is reached. | 214 // Do nothing before the period is reached. |
197 manager()->TriggerMissedHeartbeatCheck(); | 215 manager()->TriggerMissedHeartbeatCheck(); |
198 EXPECT_EQ(0, heartbeats_sent()); | 216 EXPECT_EQ(0, heartbeats_sent()); |
199 } | 217 } |
200 | 218 |
201 } // namespace | 219 } // namespace |
202 | 220 |
203 } // namespace gcm | 221 } // namespace gcm |
OLD | NEW |