blob: cb22a830009eee55493d509f883790a9bdca6ea5 [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{
William A. Kennington IIIf0fe2d62018-02-28 15:20:16 -08008 EXPECT_FALSE(wdog->enabled());
9 EXPECT_EQ(0, wdog->timeRemaining());
10 EXPECT_FALSE(wdog->timerExpired());
William A. Kennington III0650a3f2018-03-01 10:53:25 -080011 EXPECT_FALSE(wdog->timerEnabled());
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +053012}
13
14/** @brief Make sure that watchdog is started and enabled */
15TEST_F(WdogTest, createWdogAndEnable)
16{
17 // Enable and then verify
William A. Kennington IIIf0fe2d62018-02-28 15:20:16 -080018 EXPECT_TRUE(wdog->enabled(true));
19 EXPECT_FALSE(wdog->timerExpired());
William A. Kennington III0650a3f2018-03-01 10:53:25 -080020 EXPECT_TRUE(wdog->timerEnabled());
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +053021
22 // Get the configured interval
William A. Kennington IIIf0fe2d62018-02-28 15:20:16 -080023 auto remaining = milliseconds(wdog->timeRemaining());
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +053024
25 // Its possible that we are off by few msecs depending on
26 // how we get scheduled. So checking a range here.
27 EXPECT_TRUE((remaining >= defaultInterval - defaultDrift) &&
28 (remaining <= defaultInterval));
29
William A. Kennington IIIf0fe2d62018-02-28 15:20:16 -080030 EXPECT_FALSE(wdog->timerExpired());
William A. Kennington III0650a3f2018-03-01 10:53:25 -080031 EXPECT_TRUE(wdog->timerEnabled());
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +053032}
33
34/** @brief Make sure that watchdog is started and enabled.
35 * Later, disable watchdog
36 */
37TEST_F(WdogTest, createWdogAndEnableThenDisable)
38{
39 // Enable and then verify
William A. Kennington IIIf0fe2d62018-02-28 15:20:16 -080040 EXPECT_TRUE(wdog->enabled(true));
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +053041
42 // Disable and then verify
William A. Kennington IIIf0fe2d62018-02-28 15:20:16 -080043 EXPECT_FALSE(wdog->enabled(false));
44 EXPECT_FALSE(wdog->enabled());
45 EXPECT_EQ(0, wdog->timeRemaining());
William A. Kennington III0650a3f2018-03-01 10:53:25 -080046 EXPECT_FALSE(wdog->timerExpired());
47 EXPECT_FALSE(wdog->timerEnabled());
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +053048}
49
50/** @brief Make sure that watchdog is started and enabled.
51 * Wait for 5 seconds and make sure that the remaining
52 * time shows 25 seconds.
53 */
54TEST_F(WdogTest, enableWdogAndWait5Seconds)
55{
56 // Enable and then verify
William A. Kennington IIIf0fe2d62018-02-28 15:20:16 -080057 EXPECT_TRUE(wdog->enabled(true));
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +053058
59 // Sleep for 5 seconds
60 auto sleepTime = seconds(5s);
61 std::this_thread::sleep_for(sleepTime);
62
63 // Get the remaining time again and expectation is that we get 25s
William A. Kennington IIIf0fe2d62018-02-28 15:20:16 -080064 auto remaining = milliseconds(wdog->timeRemaining());
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +053065 auto expected = defaultInterval -
66 duration_cast<milliseconds>(sleepTime);
67
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 >= expected - defaultDrift) &&
71 (remaining <= expected));
William A. Kennington IIIf0fe2d62018-02-28 15:20:16 -080072 EXPECT_FALSE(wdog->timerExpired());
William A. Kennington III0650a3f2018-03-01 10:53:25 -080073 EXPECT_TRUE(wdog->timerEnabled());
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +053074}
75
76/** @brief Make sure that watchdog is started and enabled.
77 * Wait 1 second and then reset the timer to 5 seconds
78 * and then expect the watchdog to expire in 5 seconds
79 */
80TEST_F(WdogTest, enableWdogAndResetTo5Seconds)
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 // Sleep for 1 second
86 std::this_thread::sleep_for(1s);
87
88 // Next timer will expire in 5 seconds from now.
89 auto expireTime = seconds(5s);
90 auto newTime = duration_cast<milliseconds>(expireTime);
William A. Kennington IIIf0fe2d62018-02-28 15:20:16 -080091 wdog->timeRemaining(newTime.count());
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +053092
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +053093 // Waiting for expiration
94 int count = 0;
William A. Kennington IIIf0fe2d62018-02-28 15:20:16 -080095 while(count < expireTime.count() && !wdog->timerExpired())
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +053096 {
97 // Returns -0- on timeout and positive number on dispatch
98 auto sleepTime = duration_cast<microseconds>(seconds(1s));
99 if(!sd_event_run(eventP.get(), sleepTime.count()))
100 {
101 count++;
102 }
103 }
William A. Kennington IIIf0fe2d62018-02-28 15:20:16 -0800104 EXPECT_TRUE(wdog->timerExpired());
William A. Kennington III0650a3f2018-03-01 10:53:25 -0800105 EXPECT_FALSE(wdog->timerEnabled());
106 EXPECT_EQ(expireTime.count() - 1, count);
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +0530107
108 // Make sure secondary callback was not called.
William A. Kennington IIId5d14832018-02-28 09:54:08 -0800109 EXPECT_FALSE(expired);
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +0530110}
111
Gunnar Millsbfe5cb82017-10-25 20:48:50 -0500112/** @brief Make sure the Interval can be updated directly.
Patrick Venture96816342017-08-17 12:32:22 -0700113 */
114TEST_F(WdogTest, verifyIntervalUpdateReceived)
115{
116 auto expireTime = seconds(5s);
117 auto newTime = duration_cast<milliseconds>(expireTime);
William A. Kennington IIIf0fe2d62018-02-28 15:20:16 -0800118 wdog->interval(newTime.count());
Patrick Venture96816342017-08-17 12:32:22 -0700119
120 // Expect an update in the Interval
William A. Kennington IIIf0fe2d62018-02-28 15:20:16 -0800121 EXPECT_EQ(newTime.count(), wdog->interval());
Patrick Venture96816342017-08-17 12:32:22 -0700122}
123
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +0530124/** @brief Make sure that watchdog is started and enabled.
125 * Wait default interval seconds and make sure that wdog has died
126 */
127TEST_F(WdogTest, enableWdogAndWaitTillEnd)
128{
129 // Enable and then verify
William A. Kennington IIIf0fe2d62018-02-28 15:20:16 -0800130 EXPECT_TRUE(wdog->enabled(true));
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +0530131 auto expireTime = duration_cast<seconds>(
132 milliseconds(defaultInterval));
133
134 // Waiting default expiration
135 int count = 0;
William A. Kennington IIIf0fe2d62018-02-28 15:20:16 -0800136 while(count < expireTime.count() && !wdog->timerExpired())
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +0530137 {
138 // Returns -0- on timeout and positive number on dispatch
139 auto sleepTime = duration_cast<microseconds>(seconds(1s));
140 if(!sd_event_run(eventP.get(), sleepTime.count()))
141 {
142 count++;
143 }
144 }
William A. Kennington IIIf0fe2d62018-02-28 15:20:16 -0800145 EXPECT_TRUE(wdog->enabled());
146 EXPECT_EQ(0, wdog->timeRemaining());
147 EXPECT_TRUE(wdog->timerExpired());
William A. Kennington III0650a3f2018-03-01 10:53:25 -0800148 EXPECT_FALSE(wdog->timerEnabled());
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +0530149 EXPECT_EQ(expireTime.count() - 1, count);
150}