blob: a7494351dcb57c61cc9b43ab04386068efd8d961 [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
William A. Kennington III8cd38392018-03-01 11:07:36 -0800103 auto sleepTime = 5s;
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +0530104 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());
William A. Kennington III8cd38392018-03-01 11:07:36 -0800108 auto expected = defaultInterval - sleepTime;
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +0530109
110 // Its possible that we are off by few msecs depending on
111 // how we get scheduled. So checking a range here.
112 EXPECT_TRUE((remaining >= expected - defaultDrift) &&
113 (remaining <= expected));
William A. Kennington IIIf0fe2d62018-02-28 15:20:16 -0800114 EXPECT_FALSE(wdog->timerExpired());
William A. Kennington III0650a3f2018-03-01 10:53:25 -0800115 EXPECT_TRUE(wdog->timerEnabled());
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +0530116}
117
118/** @brief Make sure that watchdog is started and enabled.
119 * Wait 1 second and then reset the timer to 5 seconds
120 * and then expect the watchdog to expire in 5 seconds
121 */
122TEST_F(WdogTest, enableWdogAndResetTo5Seconds)
123{
124 // Enable and then verify
William A. Kennington IIIf0fe2d62018-02-28 15:20:16 -0800125 EXPECT_TRUE(wdog->enabled(true));
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +0530126
127 // Sleep for 1 second
128 std::this_thread::sleep_for(1s);
129
130 // Next timer will expire in 5 seconds from now.
William A. Kennington III8cd38392018-03-01 11:07:36 -0800131 auto expireTime = 5s;
132 auto expireTimeMs = milliseconds(expireTime).count();
133 EXPECT_EQ(expireTimeMs, wdog->timeRemaining(expireTimeMs));
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +0530134
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +0530135 // Waiting for expiration
William A. Kennington III99c69de2018-03-01 10:59:22 -0800136 EXPECT_EQ(expireTime - 1s, waitForWatchdog(expireTime));
William A. Kennington IIIf0fe2d62018-02-28 15:20:16 -0800137 EXPECT_TRUE(wdog->timerExpired());
William A. Kennington III0650a3f2018-03-01 10:53:25 -0800138 EXPECT_FALSE(wdog->timerEnabled());
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +0530139
140 // Make sure secondary callback was not called.
William A. Kennington IIId5d14832018-02-28 09:54:08 -0800141 EXPECT_FALSE(expired);
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +0530142}
143
Gunnar Millsbfe5cb82017-10-25 20:48:50 -0500144/** @brief Make sure the Interval can be updated directly.
Patrick Venture96816342017-08-17 12:32:22 -0700145 */
146TEST_F(WdogTest, verifyIntervalUpdateReceived)
147{
William A. Kennington III8cd38392018-03-01 11:07:36 -0800148 auto expireTime = 5s;
149 auto expireTimeMs = milliseconds(expireTime).count();
150 EXPECT_EQ(expireTimeMs, wdog->interval(expireTimeMs));
Patrick Venture96816342017-08-17 12:32:22 -0700151
152 // Expect an update in the Interval
William A. Kennington III8cd38392018-03-01 11:07:36 -0800153 EXPECT_EQ(expireTimeMs, wdog->interval());
Patrick Venture96816342017-08-17 12:32:22 -0700154}
155
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +0530156/** @brief Make sure that watchdog is started and enabled.
157 * Wait default interval seconds and make sure that wdog has died
158 */
159TEST_F(WdogTest, enableWdogAndWaitTillEnd)
160{
161 // Enable and then verify
William A. Kennington IIIf0fe2d62018-02-28 15:20:16 -0800162 EXPECT_TRUE(wdog->enabled(true));
William A. Kennington III8cd38392018-03-01 11:07:36 -0800163 auto expireTime = duration_cast<seconds>(defaultInterval);
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +0530164
165 // Waiting default expiration
William A. Kennington III99c69de2018-03-01 10:59:22 -0800166 EXPECT_EQ(expireTime - 1s, waitForWatchdog(expireTime));
167
William A. Kennington III825f4982018-02-27 19:10:56 -0800168 EXPECT_FALSE(wdog->enabled());
William A. Kennington IIIf0fe2d62018-02-28 15:20:16 -0800169 EXPECT_EQ(0, wdog->timeRemaining());
170 EXPECT_TRUE(wdog->timerExpired());
William A. Kennington III0650a3f2018-03-01 10:53:25 -0800171 EXPECT_FALSE(wdog->timerEnabled());
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +0530172}