blob: 8a62cf2afde56c8ac4447ba3ee74db8b56e1f3c2 [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());
William A. Kennington III25c1b202018-03-01 11:00:19 -080035
36 // We should be able to configure persistent properties
37 // while disabled
38 auto newAction = Watchdog::Action::PowerOff;
39 EXPECT_EQ(newAction, wdog->expireAction(newAction));
40 auto newIntervalMs = milliseconds(defaultInterval * 2).count();
41 EXPECT_EQ(newIntervalMs, wdog->interval(newIntervalMs));
42
43 EXPECT_EQ(newAction, wdog->expireAction());
44 EXPECT_EQ(newIntervalMs, wdog->interval());
45
46 // We won't be able to configure timeRemaining
47 EXPECT_EQ(0, wdog->timeRemaining(1000));
48 EXPECT_EQ(0, wdog->timeRemaining());
49
50 // Timer should not have become enabled
51 EXPECT_FALSE(wdog->enabled());
52 EXPECT_EQ(0, wdog->timeRemaining());
53 EXPECT_FALSE(wdog->timerExpired());
54 EXPECT_FALSE(wdog->timerEnabled());
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +053055}
56
57/** @brief Make sure that watchdog is started and enabled */
58TEST_F(WdogTest, createWdogAndEnable)
59{
60 // Enable and then verify
William A. Kennington IIIf0fe2d62018-02-28 15:20:16 -080061 EXPECT_TRUE(wdog->enabled(true));
62 EXPECT_FALSE(wdog->timerExpired());
William A. Kennington III0650a3f2018-03-01 10:53:25 -080063 EXPECT_TRUE(wdog->timerEnabled());
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +053064
65 // Get the configured interval
William A. Kennington IIIf0fe2d62018-02-28 15:20:16 -080066 auto remaining = milliseconds(wdog->timeRemaining());
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +053067
68 // Its possible that we are off by few msecs depending on
69 // how we get scheduled. So checking a range here.
70 EXPECT_TRUE((remaining >= defaultInterval - defaultDrift) &&
71 (remaining <= defaultInterval));
72
William A. Kennington IIIf0fe2d62018-02-28 15:20:16 -080073 EXPECT_FALSE(wdog->timerExpired());
William A. Kennington III0650a3f2018-03-01 10:53:25 -080074 EXPECT_TRUE(wdog->timerEnabled());
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +053075}
76
77/** @brief Make sure that watchdog is started and enabled.
78 * Later, disable watchdog
79 */
80TEST_F(WdogTest, createWdogAndEnableThenDisable)
81{
82 // Enable and then verify
William A. Kennington IIIf0fe2d62018-02-28 15:20:16 -080083 EXPECT_TRUE(wdog->enabled(true));
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +053084
85 // Disable and then verify
William A. Kennington IIIf0fe2d62018-02-28 15:20:16 -080086 EXPECT_FALSE(wdog->enabled(false));
87 EXPECT_FALSE(wdog->enabled());
88 EXPECT_EQ(0, wdog->timeRemaining());
William A. Kennington III0650a3f2018-03-01 10:53:25 -080089 EXPECT_FALSE(wdog->timerExpired());
90 EXPECT_FALSE(wdog->timerEnabled());
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +053091}
92
93/** @brief Make sure that watchdog is started and enabled.
94 * Wait for 5 seconds and make sure that the remaining
95 * time shows 25 seconds.
96 */
97TEST_F(WdogTest, enableWdogAndWait5Seconds)
98{
99 // Enable and then verify
William A. Kennington IIIf0fe2d62018-02-28 15:20:16 -0800100 EXPECT_TRUE(wdog->enabled(true));
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +0530101
102 // Sleep for 5 seconds
103 auto sleepTime = seconds(5s);
104 std::this_thread::sleep_for(sleepTime);
105
106 // Get the remaining time again and expectation is that we get 25s
William A. Kennington IIIf0fe2d62018-02-28 15:20:16 -0800107 auto remaining = milliseconds(wdog->timeRemaining());
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +0530108 auto expected = defaultInterval -
109 duration_cast<milliseconds>(sleepTime);
110
111 // Its possible that we are off by few msecs depending on
112 // how we get scheduled. So checking a range here.
113 EXPECT_TRUE((remaining >= expected - defaultDrift) &&
114 (remaining <= expected));
William A. Kennington IIIf0fe2d62018-02-28 15:20:16 -0800115 EXPECT_FALSE(wdog->timerExpired());
William A. Kennington III0650a3f2018-03-01 10:53:25 -0800116 EXPECT_TRUE(wdog->timerEnabled());
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +0530117}
118
119/** @brief Make sure that watchdog is started and enabled.
120 * Wait 1 second and then reset the timer to 5 seconds
121 * and then expect the watchdog to expire in 5 seconds
122 */
123TEST_F(WdogTest, enableWdogAndResetTo5Seconds)
124{
125 // Enable and then verify
William A. Kennington IIIf0fe2d62018-02-28 15:20:16 -0800126 EXPECT_TRUE(wdog->enabled(true));
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +0530127
128 // Sleep for 1 second
129 std::this_thread::sleep_for(1s);
130
131 // Next timer will expire in 5 seconds from now.
132 auto expireTime = seconds(5s);
133 auto newTime = duration_cast<milliseconds>(expireTime);
William A. Kennington IIIf0fe2d62018-02-28 15:20:16 -0800134 wdog->timeRemaining(newTime.count());
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +0530135
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +0530136 // Waiting for expiration
William A. Kennington III99c69de2018-03-01 10:59:22 -0800137 EXPECT_EQ(expireTime - 1s, waitForWatchdog(expireTime));
William A. Kennington IIIf0fe2d62018-02-28 15:20:16 -0800138 EXPECT_TRUE(wdog->timerExpired());
William A. Kennington III0650a3f2018-03-01 10:53:25 -0800139 EXPECT_FALSE(wdog->timerEnabled());
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +0530140
141 // Make sure secondary callback was not called.
William A. Kennington IIId5d14832018-02-28 09:54:08 -0800142 EXPECT_FALSE(expired);
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +0530143}
144
Gunnar Millsbfe5cb82017-10-25 20:48:50 -0500145/** @brief Make sure the Interval can be updated directly.
Patrick Venture96816342017-08-17 12:32:22 -0700146 */
147TEST_F(WdogTest, verifyIntervalUpdateReceived)
148{
149 auto expireTime = seconds(5s);
150 auto newTime = duration_cast<milliseconds>(expireTime);
William A. Kennington IIIf0fe2d62018-02-28 15:20:16 -0800151 wdog->interval(newTime.count());
Patrick Venture96816342017-08-17 12:32:22 -0700152
153 // Expect an update in the Interval
William A. Kennington IIIf0fe2d62018-02-28 15:20:16 -0800154 EXPECT_EQ(newTime.count(), wdog->interval());
Patrick Venture96816342017-08-17 12:32:22 -0700155}
156
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +0530157/** @brief Make sure that watchdog is started and enabled.
158 * Wait default interval seconds and make sure that wdog has died
159 */
160TEST_F(WdogTest, enableWdogAndWaitTillEnd)
161{
162 // Enable and then verify
William A. Kennington IIIf0fe2d62018-02-28 15:20:16 -0800163 EXPECT_TRUE(wdog->enabled(true));
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +0530164 auto expireTime = duration_cast<seconds>(
165 milliseconds(defaultInterval));
166
167 // Waiting default expiration
William A. Kennington III99c69de2018-03-01 10:59:22 -0800168 EXPECT_EQ(expireTime - 1s, waitForWatchdog(expireTime));
169
William A. Kennington IIIf0fe2d62018-02-28 15:20:16 -0800170 EXPECT_TRUE(wdog->enabled());
171 EXPECT_EQ(0, wdog->timeRemaining());
172 EXPECT_TRUE(wdog->timerExpired());
William A. Kennington III0650a3f2018-03-01 10:53:25 -0800173 EXPECT_FALSE(wdog->timerEnabled());
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +0530174}