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"
7 #include "base/message_loop/message_loop.h"
8 #include "base/time/time.h"
9 #include "google_apis/gcm/protocol/mcs.pb.h"
10 #include "testing/gtest/include/gtest/gtest.h"
16 mcs_proto::HeartbeatConfig
BuildHeartbeatConfig(int interval_ms
) {
17 mcs_proto::HeartbeatConfig config
;
18 config
.set_interval_ms(interval_ms
);
22 class TestHeartbeatManager
: public HeartbeatManager
{
24 TestHeartbeatManager() {}
25 virtual ~TestHeartbeatManager() {}
27 // Bypass the heartbeat timer, and send the heartbeat now.
28 void TriggerHearbeat();
31 void TestHeartbeatManager::TriggerHearbeat() {
32 OnHeartbeatTriggered();
35 class HeartbeatManagerTest
: public testing::Test
{
37 HeartbeatManagerTest();
38 virtual ~HeartbeatManagerTest() {}
40 TestHeartbeatManager
* manager() const { return manager_
.get(); }
41 int heartbeats_sent() const { return heartbeats_sent_
; }
42 int reconnects_triggered() const { return reconnects_triggered_
; }
44 // Starts the heartbeat manager.
48 // Helper functions for verifying heartbeat manager effects.
49 void SendHeartbeatClosure();
50 void TriggerReconnectClosure();
52 scoped_ptr
<TestHeartbeatManager
> manager_
;
55 int reconnects_triggered_
;
57 base::MessageLoop message_loop_
;
60 HeartbeatManagerTest::HeartbeatManagerTest()
61 : manager_(new TestHeartbeatManager()),
63 reconnects_triggered_(0) {
66 void HeartbeatManagerTest::StartManager() {
67 manager_
->Start(base::Bind(&HeartbeatManagerTest::SendHeartbeatClosure
,
68 base::Unretained(this)),
69 base::Bind(&HeartbeatManagerTest::TriggerReconnectClosure
,
70 base::Unretained(this)));
73 void HeartbeatManagerTest::SendHeartbeatClosure() {
77 void HeartbeatManagerTest::TriggerReconnectClosure() {
78 reconnects_triggered_
++;
81 // Basic initialization. No heartbeat should be pending.
82 TEST_F(HeartbeatManagerTest
, Init
) {
83 EXPECT_TRUE(manager()->GetNextHeartbeatTime().is_null());
86 // Acknowledging a heartbeat before starting the manager should have no effect.
87 TEST_F(HeartbeatManagerTest
, AckBeforeStart
) {
88 manager()->OnHeartbeatAcked();
89 EXPECT_TRUE(manager()->GetNextHeartbeatTime().is_null());
92 // Starting the manager should start the heartbeat timer.
93 TEST_F(HeartbeatManagerTest
, Start
) {
95 EXPECT_GT(manager()->GetNextHeartbeatTime(), base::TimeTicks::Now());
96 EXPECT_EQ(0, heartbeats_sent());
97 EXPECT_EQ(0, reconnects_triggered());
100 // Acking the heartbeat should trigger a new heartbeat timer.
101 TEST_F(HeartbeatManagerTest
, AckedHeartbeat
) {
103 manager()->TriggerHearbeat();
104 base::TimeTicks heartbeat
= manager()->GetNextHeartbeatTime();
105 EXPECT_GT(heartbeat
, base::TimeTicks::Now());
106 EXPECT_EQ(1, heartbeats_sent());
107 EXPECT_EQ(0, reconnects_triggered());
109 manager()->OnHeartbeatAcked();
110 EXPECT_LT(heartbeat
, manager()->GetNextHeartbeatTime());
111 EXPECT_EQ(1, heartbeats_sent());
112 EXPECT_EQ(0, reconnects_triggered());
114 manager()->TriggerHearbeat();
115 EXPECT_EQ(2, heartbeats_sent());
116 EXPECT_EQ(0, reconnects_triggered());
119 // Trigger a heartbeat when one was outstanding should reset the connection.
120 TEST_F(HeartbeatManagerTest
, UnackedHeartbeat
) {
122 manager()->TriggerHearbeat();
123 EXPECT_EQ(1, heartbeats_sent());
124 EXPECT_EQ(0, reconnects_triggered());
126 manager()->TriggerHearbeat();
127 EXPECT_EQ(1, heartbeats_sent());
128 EXPECT_EQ(1, reconnects_triggered());
131 // Updating the heartbeat interval before starting should result in the new
132 // interval being used at Start time.
133 TEST_F(HeartbeatManagerTest
, UpdateIntervalThenStart
) {
134 const int kIntervalMs
= 60 * 1000; // 60 seconds.
135 manager()->UpdateHeartbeatConfig(BuildHeartbeatConfig(kIntervalMs
));
136 EXPECT_TRUE(manager()->GetNextHeartbeatTime().is_null());
138 EXPECT_LE(manager()->GetNextHeartbeatTime() - base::TimeTicks::Now(),
139 base::TimeDelta::FromMilliseconds(kIntervalMs
));
142 // Updating the heartbeat interval after starting should only use the new
143 // interval on the next heartbeat.
144 TEST_F(HeartbeatManagerTest
, StartThenUpdateInterval
) {
145 const int kIntervalMs
= 60 * 1000; // 60 seconds.
147 base::TimeTicks heartbeat
= manager()->GetNextHeartbeatTime();
148 EXPECT_GT(heartbeat
- base::TimeTicks::Now(),
149 base::TimeDelta::FromMilliseconds(kIntervalMs
));
151 // Updating the interval should not affect an outstanding heartbeat.
152 manager()->UpdateHeartbeatConfig(BuildHeartbeatConfig(kIntervalMs
));
153 EXPECT_EQ(heartbeat
, manager()->GetNextHeartbeatTime());
155 // Triggering and acking the heartbeat should result in a heartbeat being
156 // posted with the new interval.
157 manager()->TriggerHearbeat();
158 manager()->OnHeartbeatAcked();
160 EXPECT_LE(manager()->GetNextHeartbeatTime() - base::TimeTicks::Now(),
161 base::TimeDelta::FromMilliseconds(kIntervalMs
));
162 EXPECT_NE(heartbeat
, manager()->GetNextHeartbeatTime());
165 // Stopping the manager should reset the heartbeat timer.
166 TEST_F(HeartbeatManagerTest
, Stop
) {
168 EXPECT_GT(manager()->GetNextHeartbeatTime(), base::TimeTicks::Now());
171 EXPECT_TRUE(manager()->GetNextHeartbeatTime().is_null());