blob: 72347aa2e56dc27d92902ce85c1b38a27a03200a [file] [log] [blame]
William A. Kennington III99c69de2018-03-01 10:59:22 -08001#include <chrono>
2
3#include "watchdog_test.hpp"
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +05304
5using namespace phosphor::watchdog;
6
William A. Kennington III99c69de2018-03-01 10:59:22 -08007seconds WdogTest::waitForWatchdog(seconds timeLimit)
8{
9 auto previousTimeRemaining = wdog->timeRemaining();
10 auto ret = 0s;
11 while (ret < timeLimit &&
12 previousTimeRemaining >= wdog->timeRemaining() &&
13 wdog->timerEnabled())
14 {
15 previousTimeRemaining = wdog->timeRemaining();
16
17 // Returns -0- on timeout and positive number on dispatch
18 auto sleepTime = 1s;
19 if(!sd_event_run(eventP.get(), microseconds(sleepTime).count()))
20 {
21 ret += sleepTime;
22 }
23 }
24
25 return ret;
26}
27
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +053028/** @brief Make sure that watchdog is started and not enabled */
29TEST_F(WdogTest, createWdogAndDontEnable)
30{
William A. Kennington IIIf0fe2d62018-02-28 15:20:16 -080031 EXPECT_FALSE(wdog->enabled());
32 EXPECT_EQ(0, wdog->timeRemaining());
33 EXPECT_FALSE(wdog->timerExpired());
William A. Kennington III0650a3f2018-03-01 10:53:25 -080034 EXPECT_FALSE(wdog->timerEnabled());
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +053035}
36
37/** @brief Make sure that watchdog is started and enabled */
38TEST_F(WdogTest, createWdogAndEnable)
39{
40 // Enable and then verify
William A. Kennington IIIf0fe2d62018-02-28 15:20:16 -080041 EXPECT_TRUE(wdog->enabled(true));
42 EXPECT_FALSE(wdog->timerExpired());
William A. Kennington III0650a3f2018-03-01 10:53:25 -080043 EXPECT_TRUE(wdog->timerEnabled());
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +053044
45 // Get the configured interval
William A. Kennington IIIf0fe2d62018-02-28 15:20:16 -080046 auto remaining = milliseconds(wdog->timeRemaining());
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +053047
48 // Its possible that we are off by few msecs depending on
49 // how we get scheduled. So checking a range here.
50 EXPECT_TRUE((remaining >= defaultInterval - defaultDrift) &&
51 (remaining <= defaultInterval));
52
William A. Kennington IIIf0fe2d62018-02-28 15:20:16 -080053 EXPECT_FALSE(wdog->timerExpired());
William A. Kennington III0650a3f2018-03-01 10:53:25 -080054 EXPECT_TRUE(wdog->timerEnabled());
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +053055}
56
57/** @brief Make sure that watchdog is started and enabled.
58 * Later, disable watchdog
59 */
60TEST_F(WdogTest, createWdogAndEnableThenDisable)
61{
62 // Enable and then verify
William A. Kennington IIIf0fe2d62018-02-28 15:20:16 -080063 EXPECT_TRUE(wdog->enabled(true));
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +053064
65 // Disable and then verify
William A. Kennington IIIf0fe2d62018-02-28 15:20:16 -080066 EXPECT_FALSE(wdog->enabled(false));
67 EXPECT_FALSE(wdog->enabled());
68 EXPECT_EQ(0, wdog->timeRemaining());
William A. Kennington III0650a3f2018-03-01 10:53:25 -080069 EXPECT_FALSE(wdog->timerExpired());
70 EXPECT_FALSE(wdog->timerEnabled());
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +053071}
72
73/** @brief Make sure that watchdog is started and enabled.
74 * Wait for 5 seconds and make sure that the remaining
75 * time shows 25 seconds.
76 */
77TEST_F(WdogTest, enableWdogAndWait5Seconds)
78{
79 // Enable and then verify
William A. Kennington IIIf0fe2d62018-02-28 15:20:16 -080080 EXPECT_TRUE(wdog->enabled(true));
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +053081
82 // Sleep for 5 seconds
83 auto sleepTime = seconds(5s);
84 std::this_thread::sleep_for(sleepTime);
85
86 // Get the remaining time again and expectation is that we get 25s
William A. Kennington IIIf0fe2d62018-02-28 15:20:16 -080087 auto remaining = milliseconds(wdog->timeRemaining());
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +053088 auto expected = defaultInterval -
89 duration_cast<milliseconds>(sleepTime);
90
91 // Its possible that we are off by few msecs depending on
92 // how we get scheduled. So checking a range here.
93 EXPECT_TRUE((remaining >= expected - defaultDrift) &&
94 (remaining <= expected));
William A. Kennington IIIf0fe2d62018-02-28 15:20:16 -080095 EXPECT_FALSE(wdog->timerExpired());
William A. Kennington III0650a3f2018-03-01 10:53:25 -080096 EXPECT_TRUE(wdog->timerEnabled());
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +053097}
98
99/** @brief Make sure that watchdog is started and enabled.
100 * Wait 1 second and then reset the timer to 5 seconds
101 * and then expect the watchdog to expire in 5 seconds
102 */
103TEST_F(WdogTest, enableWdogAndResetTo5Seconds)
104{
105 // Enable and then verify
William A. Kennington IIIf0fe2d62018-02-28 15:20:16 -0800106 EXPECT_TRUE(wdog->enabled(true));
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +0530107
108 // Sleep for 1 second
109 std::this_thread::sleep_for(1s);
110
111 // Next timer will expire in 5 seconds from now.
112 auto expireTime = seconds(5s);
113 auto newTime = duration_cast<milliseconds>(expireTime);
William A. Kennington IIIf0fe2d62018-02-28 15:20:16 -0800114 wdog->timeRemaining(newTime.count());
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +0530115
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +0530116 // Waiting for expiration
William A. Kennington III99c69de2018-03-01 10:59:22 -0800117 EXPECT_EQ(expireTime - 1s, waitForWatchdog(expireTime));
William A. Kennington IIIf0fe2d62018-02-28 15:20:16 -0800118 EXPECT_TRUE(wdog->timerExpired());
William A. Kennington III0650a3f2018-03-01 10:53:25 -0800119 EXPECT_FALSE(wdog->timerEnabled());
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +0530120
121 // Make sure secondary callback was not called.
William A. Kennington IIId5d14832018-02-28 09:54:08 -0800122 EXPECT_FALSE(expired);
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +0530123}
124
Gunnar Millsbfe5cb82017-10-25 20:48:50 -0500125/** @brief Make sure the Interval can be updated directly.
Patrick Venture96816342017-08-17 12:32:22 -0700126 */
127TEST_F(WdogTest, verifyIntervalUpdateReceived)
128{
129 auto expireTime = seconds(5s);
130 auto newTime = duration_cast<milliseconds>(expireTime);
William A. Kennington IIIf0fe2d62018-02-28 15:20:16 -0800131 wdog->interval(newTime.count());
Patrick Venture96816342017-08-17 12:32:22 -0700132
133 // Expect an update in the Interval
William A. Kennington IIIf0fe2d62018-02-28 15:20:16 -0800134 EXPECT_EQ(newTime.count(), wdog->interval());
Patrick Venture96816342017-08-17 12:32:22 -0700135}
136
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +0530137/** @brief Make sure that watchdog is started and enabled.
138 * Wait default interval seconds and make sure that wdog has died
139 */
140TEST_F(WdogTest, enableWdogAndWaitTillEnd)
141{
142 // Enable and then verify
William A. Kennington IIIf0fe2d62018-02-28 15:20:16 -0800143 EXPECT_TRUE(wdog->enabled(true));
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +0530144 auto expireTime = duration_cast<seconds>(
145 milliseconds(defaultInterval));
146
147 // Waiting default expiration
William A. Kennington III99c69de2018-03-01 10:59:22 -0800148 EXPECT_EQ(expireTime - 1s, waitForWatchdog(expireTime));
149
William A. Kennington IIIf0fe2d62018-02-28 15:20:16 -0800150 EXPECT_TRUE(wdog->enabled());
151 EXPECT_EQ(0, wdog->timeRemaining());
152 EXPECT_TRUE(wdog->timerExpired());
William A. Kennington III0650a3f2018-03-01 10:53:25 -0800153 EXPECT_FALSE(wdog->timerEnabled());
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +0530154}