blob: 57f32e92b5cc975649b18734dbcdb8b8caa80a5f [file] [log] [blame]
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +05301#include <watchdog_test.hpp>
2
3using namespace phosphor::watchdog;
4
5/** @brief Make sure that watchdog is started and not enabled */
6TEST_F(WdogTest, createWdogAndDontEnable)
7{
8 EXPECT_EQ(false, wdog.enabled());
9 EXPECT_EQ(0, wdog.timeRemaining());
10 EXPECT_EQ(false, wdog.timerExpired());
11}
12
13/** @brief Make sure that watchdog is started and enabled */
14TEST_F(WdogTest, createWdogAndEnable)
15{
16 // Enable and then verify
17 EXPECT_EQ(true, wdog.enabled(true));
18 EXPECT_EQ(false, wdog.timerExpired());
19
20 // Get the configured interval
21 auto remaining = milliseconds(wdog.timeRemaining());
22
23 // Its possible that we are off by few msecs depending on
24 // how we get scheduled. So checking a range here.
25 EXPECT_TRUE((remaining >= defaultInterval - defaultDrift) &&
26 (remaining <= defaultInterval));
27
28 EXPECT_EQ(false, wdog.timerExpired());
29}
30
31/** @brief Make sure that watchdog is started and enabled.
32 * Later, disable watchdog
33 */
34TEST_F(WdogTest, createWdogAndEnableThenDisable)
35{
36 // Enable and then verify
37 EXPECT_EQ(true, wdog.enabled(true));
38
39 // Disable and then verify
40 EXPECT_EQ(false, wdog.enabled(false));
41 EXPECT_EQ(false, wdog.enabled());
42 EXPECT_EQ(0, wdog.timeRemaining());
43}
44
45/** @brief Make sure that watchdog is started and enabled.
46 * Wait for 5 seconds and make sure that the remaining
47 * time shows 25 seconds.
48 */
49TEST_F(WdogTest, enableWdogAndWait5Seconds)
50{
51 // Enable and then verify
52 EXPECT_EQ(true, wdog.enabled(true));
53
54 // Sleep for 5 seconds
55 auto sleepTime = seconds(5s);
56 std::this_thread::sleep_for(sleepTime);
57
58 // Get the remaining time again and expectation is that we get 25s
59 auto remaining = milliseconds(wdog.timeRemaining());
60 auto expected = defaultInterval -
61 duration_cast<milliseconds>(sleepTime);
62
63 // Its possible that we are off by few msecs depending on
64 // how we get scheduled. So checking a range here.
65 EXPECT_TRUE((remaining >= expected - defaultDrift) &&
66 (remaining <= expected));
67 EXPECT_EQ(false, wdog.timerExpired());
68}
69
70/** @brief Make sure that watchdog is started and enabled.
71 * Wait 1 second and then reset the timer to 5 seconds
72 * and then expect the watchdog to expire in 5 seconds
73 */
74TEST_F(WdogTest, enableWdogAndResetTo5Seconds)
75{
76 // Enable and then verify
77 EXPECT_EQ(true, wdog.enabled(true));
78
79 // Sleep for 1 second
80 std::this_thread::sleep_for(1s);
81
82 // Next timer will expire in 5 seconds from now.
83 auto expireTime = seconds(5s);
84 auto newTime = duration_cast<milliseconds>(expireTime);
85 wdog.timeRemaining(newTime.count());
86
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +053087 // Waiting for expiration
88 int count = 0;
89 while(count < expireTime.count() && !wdog.timerExpired())
90 {
91 // Returns -0- on timeout and positive number on dispatch
92 auto sleepTime = duration_cast<microseconds>(seconds(1s));
93 if(!sd_event_run(eventP.get(), sleepTime.count()))
94 {
95 count++;
96 }
97 }
98 EXPECT_EQ(true, wdog.timerExpired());
99 EXPECT_EQ(expireTime.count() - 1 , count);
100
101 // Make sure secondary callback was not called.
102 EXPECT_EQ(false, expired);
103}
104
Gunnar Millsbfe5cb82017-10-25 20:48:50 -0500105/** @brief Make sure the Interval can be updated directly.
Patrick Venture96816342017-08-17 12:32:22 -0700106 */
107TEST_F(WdogTest, verifyIntervalUpdateReceived)
108{
109 auto expireTime = seconds(5s);
110 auto newTime = duration_cast<milliseconds>(expireTime);
111 wdog.interval(newTime.count());
112
113 // Expect an update in the Interval
114 EXPECT_EQ(newTime.count(), wdog.interval());
115}
116
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +0530117/** @brief Make sure that watchdog is started and enabled.
118 * Wait default interval seconds and make sure that wdog has died
119 */
120TEST_F(WdogTest, enableWdogAndWaitTillEnd)
121{
122 // Enable and then verify
123 EXPECT_EQ(true, wdog.enabled(true));
124 auto expireTime = duration_cast<seconds>(
125 milliseconds(defaultInterval));
126
127 // Waiting default expiration
128 int count = 0;
129 while(count < expireTime.count() && !wdog.timerExpired())
130 {
131 // Returns -0- on timeout and positive number on dispatch
132 auto sleepTime = duration_cast<microseconds>(seconds(1s));
133 if(!sd_event_run(eventP.get(), sleepTime.count()))
134 {
135 count++;
136 }
137 }
138 EXPECT_EQ(true, wdog.enabled());
139 EXPECT_EQ(0, wdog.timeRemaining());
140 EXPECT_EQ(true, wdog.timerExpired());
141 EXPECT_EQ(expireTime.count() - 1, count);
142}