Chromium Code Reviews| 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" |
| 11 #include "base/threading/platform_thread.h" | |
| 10 #include "base/time/time.h" | 12 #include "base/time/time.h" |
| 11 #include "base/timer/timer.h" | 13 #include "base/timer/timer.h" |
| 12 #include "google_apis/gcm/protocol/mcs.pb.h" | 14 #include "google_apis/gcm/protocol/mcs.pb.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 16 |
| 15 namespace gcm { | 17 namespace gcm { |
| 16 | 18 |
| 17 namespace { | 19 namespace { |
| 18 | 20 |
| 19 mcs_proto::HeartbeatConfig BuildHeartbeatConfig(int interval_ms) { | 21 mcs_proto::HeartbeatConfig BuildHeartbeatConfig(int interval_ms) { |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 168 // Triggering and acking the heartbeat should result in a heartbeat being | 170 // Triggering and acking the heartbeat should result in a heartbeat being |
| 169 // posted with the new interval. | 171 // posted with the new interval. |
| 170 manager()->TriggerHearbeat(); | 172 manager()->TriggerHearbeat(); |
| 171 manager()->OnHeartbeatAcked(); | 173 manager()->OnHeartbeatAcked(); |
| 172 | 174 |
| 173 EXPECT_LE(manager()->GetNextHeartbeatTime() - base::TimeTicks::Now(), | 175 EXPECT_LE(manager()->GetNextHeartbeatTime() - base::TimeTicks::Now(), |
| 174 base::TimeDelta::FromMilliseconds(kIntervalMs)); | 176 base::TimeDelta::FromMilliseconds(kIntervalMs)); |
| 175 EXPECT_NE(heartbeat, manager()->GetNextHeartbeatTime()); | 177 EXPECT_NE(heartbeat, manager()->GetNextHeartbeatTime()); |
| 176 } | 178 } |
| 177 | 179 |
| 180 // Updating the timer used for heartbeats before starting should not start the | |
| 181 // timer. | |
| 182 TEST_F(HeartbeatManagerTest, UpdateTimerBeforeStart) { | |
| 183 manager()->UpdateHeartbeatTimer( | |
| 184 make_scoped_ptr(new base::Timer(true, false))); | |
| 185 EXPECT_TRUE(manager()->GetNextHeartbeatTime().is_null()); | |
| 186 } | |
| 187 | |
| 188 // Updating the timer used for heartbeats after starting should restart the | |
| 189 // timer and update the heartbeat time. | |
| 190 TEST_F(HeartbeatManagerTest, UpdateTimerAfterStart) { | |
| 191 StartManager(); | |
| 192 base::TimeTicks heartbeat = manager()->GetNextHeartbeatTime(); | |
| 193 | |
| 194 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(2)); | |
|
Nicolas Zea
2014/12/05 00:53:05
why is this necessary?
Chirantan Ekbote
2014/12/05 21:49:05
Because otherwise the test would be too fast to re
| |
| 195 | |
| 196 manager()->UpdateHeartbeatTimer( | |
| 197 make_scoped_ptr(new base::Timer(true, false))); | |
| 198 EXPECT_NE(heartbeat, manager()->GetNextHeartbeatTime()); | |
| 199 } | |
| 200 | |
| 178 // Stopping the manager should reset the heartbeat timer. | 201 // Stopping the manager should reset the heartbeat timer. |
| 179 TEST_F(HeartbeatManagerTest, Stop) { | 202 TEST_F(HeartbeatManagerTest, Stop) { |
| 180 StartManager(); | 203 StartManager(); |
| 181 EXPECT_GT(manager()->GetNextHeartbeatTime(), base::TimeTicks::Now()); | 204 EXPECT_GT(manager()->GetNextHeartbeatTime(), base::TimeTicks::Now()); |
| 182 | 205 |
| 183 manager()->Stop(); | 206 manager()->Stop(); |
| 184 EXPECT_TRUE(manager()->GetNextHeartbeatTime().is_null()); | 207 EXPECT_TRUE(manager()->GetNextHeartbeatTime().is_null()); |
| 185 } | 208 } |
| 186 | 209 |
| 187 // Simulate missing a heartbeat by manually invoking the check method. The | 210 // Simulate missing a heartbeat by manually invoking the check method. The |
| 188 // heartbeat should only be triggered once, and only if the heartbeat timer | 211 // heartbeat should only be triggered once, and only if the heartbeat timer |
| 189 // is running. Because the period is several minutes, none should fire. | 212 // is running. Because the period is several minutes, none should fire. |
| 190 TEST_F(HeartbeatManagerTest, MissedHeartbeat) { | 213 TEST_F(HeartbeatManagerTest, MissedHeartbeat) { |
| 191 // Do nothing while stopped. | 214 // Do nothing while stopped. |
| 192 manager()->TriggerMissedHeartbeatCheck(); | 215 manager()->TriggerMissedHeartbeatCheck(); |
| 193 StartManager(); | 216 StartManager(); |
| 194 EXPECT_EQ(0, heartbeats_sent()); | 217 EXPECT_EQ(0, heartbeats_sent()); |
| 195 | 218 |
| 196 // Do nothing before the period is reached. | 219 // Do nothing before the period is reached. |
| 197 manager()->TriggerMissedHeartbeatCheck(); | 220 manager()->TriggerMissedHeartbeatCheck(); |
| 198 EXPECT_EQ(0, heartbeats_sent()); | 221 EXPECT_EQ(0, heartbeats_sent()); |
| 199 } | 222 } |
| 200 | 223 |
| 201 } // namespace | 224 } // namespace |
| 202 | 225 |
| 203 } // namespace gcm | 226 } // namespace gcm |
| OLD | NEW |