blob: a9dae9c8d4061333a098cbbb47de15a8206a34d1 [file] [log] [blame]
Vishwanatha Subbanna6dc96682017-02-06 21:47:18 +05301#include <iostream>
2#include <chrono>
3#include <gtest/gtest.h>
4#include "timer.hpp"
5
6using namespace phosphor::ipmi;
7
8class TimerTest : public ::testing::Test
9{
10 public:
11 // systemd event handler
12 sd_event* events;
13
14 // Need this so that events can be initialized.
15 int rc;
16
17 // Source of event
18 sd_event_source* eventSource;
19
20 // Add a Timer Object
21 Timer timer;
22
23 // Gets called as part of each TEST_F construction
24 TimerTest()
25 : rc(sd_event_default(&events)),
26 timer(events)
27 {
28 // Check for successful creation of
29 // event handler and timer object.
30 EXPECT_GE(rc, 0);
31 }
32
33 // Gets called as part of each TEST_F destruction
34 ~TimerTest()
35 {
36 events = sd_event_unref(events);
37 }
38};
39
40/** @brief Makes sure that timer is expired and the
41 * callback handler gets invoked post 2 seconds
42 */
43TEST_F(TimerTest, timerExpiresAfter2seconds)
44{
45 using namespace std::chrono;
46
47 auto time = duration_cast<microseconds>(seconds(2));
48 EXPECT_GE(timer.startTimer(time), 0);
49
50 // Waiting 2 seconds is enough here since we have
51 // already spent some usec now
52 int count = 0;
53 while(count < 2 && !timer.isExpired())
54 {
55 // Returns -0- on timeout and positive number on dispatch
56 auto sleepTime = duration_cast<microseconds>(seconds(1));
57 if(!sd_event_run(events, sleepTime.count()))
58 {
59 count++;
60 }
61 }
62 EXPECT_EQ(true, timer.isExpired());
63 EXPECT_EQ(1, count);
64}
65
66/** @brief Makes sure that timer is not expired
67 */
68TEST_F(TimerTest, timerNotExpiredAfter2Seconds)
69{
70 using namespace std::chrono;
71
72 auto time = duration_cast<microseconds>(seconds(2));
73 EXPECT_GE(timer.startTimer(time), 0);
74
75 // Now turn off the timer post a 1 second sleep
76 sleep(1);
77 EXPECT_GE(timer.setTimer(SD_EVENT_OFF), 0);
78
79 // Wait 2 seconds and see that timer is not expired
80 int count = 0;
81 while(count < 2)
82 {
83 // Returns -0- on timeout
84 auto sleepTime = duration_cast<microseconds>(seconds(1));
85 if(!sd_event_run(events, sleepTime.count()))
86 {
87 count++;
88 }
89 }
90 EXPECT_EQ(false, timer.isExpired());
91
92 // 2 because of one more count that happens prior to exiting
93 EXPECT_EQ(2, count);
94}
95
96/** @brief Makes sure that timer value is changed in between
97 * and that the new timer expires
98 */
99TEST_F(TimerTest, updateTimerAndExpectExpire)
100{
101 using namespace std::chrono;
102
103 auto time = duration_cast<microseconds>(seconds(2));
104 EXPECT_GE(timer.startTimer(time), 0);
105
106 // Now sleep for a second and then set the new timeout value
107 sleep(1);
108
109 // New timeout is 3 seconds from THIS point.
110 time = duration_cast<microseconds>(seconds(3));
111 EXPECT_GE(timer.startTimer(time), 0);
112
113 // Wait 3 seconds and see that timer is expired
114 int count = 0;
115 while(count < 3 && !timer.isExpired())
116 {
117 // Returns -0- on timeout
118 auto sleepTime = duration_cast<microseconds>(seconds(1));
119 if(!sd_event_run(events, sleepTime.count()))
120 {
121 count++;
122 }
123 }
124 EXPECT_EQ(true, timer.isExpired());
125 EXPECT_EQ(2, count);
126}
127
128/** @brief Makes sure that timer value is changed in between
129 * and turn off and make sure that timer does not expire
130 */
131TEST_F(TimerTest, updateTimerAndNeverExpire)
132{
133 using namespace std::chrono;
134
135 auto time = duration_cast<microseconds>(seconds(2));
136 EXPECT_GE(timer.startTimer(time), 0);
137
138 // Now sleep for a second and then set the new timeout value
139 sleep(1);
140
141 // New timeout is 2 seconds from THIS point.
142 time = duration_cast<microseconds>(seconds(2));
143 EXPECT_GE(timer.startTimer(time), 0);
144
145 // Now turn off the timer post a 1 second sleep
146 sleep(1);
147 EXPECT_GE(timer.setTimer(SD_EVENT_OFF), 0);
148
149 // Wait 2 seconds and see that timer is expired
150 int count = 0;
151 while(count < 2)
152 {
153 // Returns -0- on timeout
154 auto sleepTime = duration_cast<microseconds>(seconds(1));
155 if(!sd_event_run(events, sleepTime.count()))
156 {
157 count++;
158 }
159 }
160 EXPECT_EQ(false, timer.isExpired());
161
162 // 2 becase of one more count that happens prior to exiting
163 EXPECT_EQ(2, count);
164}