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
3 // found in the LICENSE file.
5 #include "google_apis/gcm/engine/heartbeat_manager.h"
8 #include "base/bind_helpers.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/time/time.h"
12 #include "base/timer/timer.h"
13 #include "google_apis/gcm/protocol/mcs.pb.h"
14 #include "testing/gtest/include/gtest/gtest.h"
20 mcs_proto::HeartbeatConfig
BuildHeartbeatConfig(int interval_ms
) {
21 mcs_proto::HeartbeatConfig config
;
22 config
.set_interval_ms(interval_ms
);
26 class TestHeartbeatManager
: public HeartbeatManager
{
28 TestHeartbeatManager() {}
29 ~TestHeartbeatManager() override
{}
31 // Bypass the heartbeat timer, and send the heartbeat now.
32 void TriggerHearbeat();
34 // Check for a missed heartbeat now.
35 void TriggerMissedHeartbeatCheck();
38 void TestHeartbeatManager::TriggerHearbeat() {
39 OnHeartbeatTriggered();
42 void TestHeartbeatManager::TriggerMissedHeartbeatCheck() {
43 CheckForMissedHeartbeat();
46 class HeartbeatManagerTest
: public testing::Test
{
48 HeartbeatManagerTest();
49 ~HeartbeatManagerTest() override
{}
51 TestHeartbeatManager
* manager() const { return manager_
.get(); }
52 int heartbeats_sent() const { return heartbeats_sent_
; }
53 int reconnects_triggered() const { return reconnects_triggered_
; }
55 // Starts the heartbeat manager.
59 // Helper functions for verifying heartbeat manager effects.
60 void SendHeartbeatClosure();
61 void TriggerReconnectClosure();
63 scoped_ptr
<TestHeartbeatManager
> manager_
;
66 int reconnects_triggered_
;
68 base::MessageLoop message_loop_
;
71 HeartbeatManagerTest::HeartbeatManagerTest()
72 : manager_(new TestHeartbeatManager()),
74 reconnects_triggered_(0) {
77 void HeartbeatManagerTest::StartManager() {
78 manager_
->Start(base::Bind(&HeartbeatManagerTest::SendHeartbeatClosure
,
79 base::Unretained(this)),
80 base::Bind(&HeartbeatManagerTest::TriggerReconnectClosure
,
81 base::Unretained(this)));
84 void HeartbeatManagerTest::SendHeartbeatClosure() {
88 void HeartbeatManagerTest::TriggerReconnectClosure() {
89 reconnects_triggered_
++;
92 // Basic initialization. No heartbeat should be pending.
93 TEST_F(HeartbeatManagerTest
, Init
) {
94 EXPECT_TRUE(manager()->GetNextHeartbeatTime().is_null());
97 // Acknowledging a heartbeat before starting the manager should have no effect.
98 TEST_F(HeartbeatManagerTest
, AckBeforeStart
) {
99 manager()->OnHeartbeatAcked();
100 EXPECT_TRUE(manager()->GetNextHeartbeatTime().is_null());
103 // Starting the manager should start the heartbeat timer.
104 TEST_F(HeartbeatManagerTest
, Start
) {
106 EXPECT_GT(manager()->GetNextHeartbeatTime(), base::TimeTicks::Now());
107 EXPECT_EQ(0, heartbeats_sent());
108 EXPECT_EQ(0, reconnects_triggered());
111 // Acking the heartbeat should trigger a new heartbeat timer.
112 TEST_F(HeartbeatManagerTest
, AckedHeartbeat
) {
114 manager()->TriggerHearbeat();
115 base::TimeTicks heartbeat
= manager()->GetNextHeartbeatTime();
116 EXPECT_GT(heartbeat
, base::TimeTicks::Now());
117 EXPECT_EQ(1, heartbeats_sent());
118 EXPECT_EQ(0, reconnects_triggered());
120 manager()->OnHeartbeatAcked();
121 EXPECT_LT(heartbeat
, manager()->GetNextHeartbeatTime());
122 EXPECT_EQ(1, heartbeats_sent());
123 EXPECT_EQ(0, reconnects_triggered());
125 manager()->TriggerHearbeat();
126 EXPECT_EQ(2, heartbeats_sent());
127 EXPECT_EQ(0, reconnects_triggered());
130 // Trigger a heartbeat when one was outstanding should reset the connection.
131 TEST_F(HeartbeatManagerTest
, UnackedHeartbeat
) {
133 manager()->TriggerHearbeat();
134 EXPECT_EQ(1, heartbeats_sent());
135 EXPECT_EQ(0, reconnects_triggered());
137 manager()->TriggerHearbeat();
138 EXPECT_EQ(1, heartbeats_sent());
139 EXPECT_EQ(1, reconnects_triggered());
142 // Updating the heartbeat interval before starting should result in the new
143 // interval being used at Start time.
144 TEST_F(HeartbeatManagerTest
, UpdateIntervalThenStart
) {
145 const int kIntervalMs
= 60 * 1000; // 60 seconds.
146 manager()->UpdateHeartbeatConfig(BuildHeartbeatConfig(kIntervalMs
));
147 EXPECT_TRUE(manager()->GetNextHeartbeatTime().is_null());
149 EXPECT_LE(manager()->GetNextHeartbeatTime() - base::TimeTicks::Now(),
150 base::TimeDelta::FromMilliseconds(kIntervalMs
));
153 // Updating the heartbeat interval after starting should only use the new
154 // interval on the next heartbeat.
155 TEST_F(HeartbeatManagerTest
, StartThenUpdateInterval
) {
156 const int kIntervalMs
= 60 * 1000; // 60 seconds.
158 base::TimeTicks heartbeat
= manager()->GetNextHeartbeatTime();
159 EXPECT_GT(heartbeat
- base::TimeTicks::Now(),
160 base::TimeDelta::FromMilliseconds(kIntervalMs
));
162 // Updating the interval should not affect an outstanding heartbeat.
163 manager()->UpdateHeartbeatConfig(BuildHeartbeatConfig(kIntervalMs
));
164 EXPECT_EQ(heartbeat
, manager()->GetNextHeartbeatTime());
166 // Triggering and acking the heartbeat should result in a heartbeat being
167 // posted with the new interval.
168 manager()->TriggerHearbeat();
169 manager()->OnHeartbeatAcked();
171 EXPECT_LE(manager()->GetNextHeartbeatTime() - base::TimeTicks::Now(),
172 base::TimeDelta::FromMilliseconds(kIntervalMs
));
173 EXPECT_NE(heartbeat
, manager()->GetNextHeartbeatTime());
176 // Updating the timer used for heartbeats before starting should not start the
178 TEST_F(HeartbeatManagerTest
, UpdateTimerBeforeStart
) {
179 manager()->UpdateHeartbeatTimer(
180 make_scoped_ptr(new base::Timer(true, false)));
181 EXPECT_TRUE(manager()->GetNextHeartbeatTime().is_null());
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
) {
188 base::TimeTicks heartbeat
= manager()->GetNextHeartbeatTime();
190 manager()->UpdateHeartbeatTimer(
191 make_scoped_ptr(new base::Timer(true, false)));
192 EXPECT_LT(manager()->GetNextHeartbeatTime() - heartbeat
,
193 base::TimeDelta::FromMilliseconds(5));
196 // Stopping the manager should reset the heartbeat timer.
197 TEST_F(HeartbeatManagerTest
, Stop
) {
199 EXPECT_GT(manager()->GetNextHeartbeatTime(), base::TimeTicks::Now());
202 EXPECT_TRUE(manager()->GetNextHeartbeatTime().is_null());
205 // Simulate missing a heartbeat by manually invoking the check method. The
206 // heartbeat should only be triggered once, and only if the heartbeat timer
207 // is running. Because the period is several minutes, none should fire.
208 TEST_F(HeartbeatManagerTest
, MissedHeartbeat
) {
209 // Do nothing while stopped.
210 manager()->TriggerMissedHeartbeatCheck();
212 EXPECT_EQ(0, heartbeats_sent());
214 // Do nothing before the period is reached.
215 manager()->TriggerMissedHeartbeatCheck();
216 EXPECT_EQ(0, heartbeats_sent());