blob: f9a992c74d90030cbf3b0b785c4ef38e4a754b45 [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 Subbannaf2309dd2017-06-06 23:15:19 +053087 // Expect an update in the Interval
88 EXPECT_EQ(newTime.count(), wdog.interval());
89
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +053090 // Waiting for expiration
91 int count = 0;
92 while(count < expireTime.count() && !wdog.timerExpired())
93 {
94 // Returns -0- on timeout and positive number on dispatch
95 auto sleepTime = duration_cast<microseconds>(seconds(1s));
96 if(!sd_event_run(eventP.get(), sleepTime.count()))
97 {
98 count++;
99 }
100 }
101 EXPECT_EQ(true, wdog.timerExpired());
102 EXPECT_EQ(expireTime.count() - 1 , count);
103
104 // Make sure secondary callback was not called.
105 EXPECT_EQ(false, expired);
106}
107
108/** @brief Make sure that watchdog is started and enabled.
109 * Wait default interval seconds and make sure that wdog has died
110 */
111TEST_F(WdogTest, enableWdogAndWaitTillEnd)
112{
113 // Enable and then verify
114 EXPECT_EQ(true, wdog.enabled(true));
115 auto expireTime = duration_cast<seconds>(
116 milliseconds(defaultInterval));
117
118 // Waiting default expiration
119 int count = 0;
120 while(count < expireTime.count() && !wdog.timerExpired())
121 {
122 // Returns -0- on timeout and positive number on dispatch
123 auto sleepTime = duration_cast<microseconds>(seconds(1s));
124 if(!sd_event_run(eventP.get(), sleepTime.count()))
125 {
126 count++;
127 }
128 }
129 EXPECT_EQ(true, wdog.enabled());
130 EXPECT_EQ(0, wdog.timeRemaining());
131 EXPECT_EQ(true, wdog.timerExpired());
132 EXPECT_EQ(expireTime.count() - 1, count);
133}