William A. Kennington III | 99c69de | 2018-03-01 10:59:22 -0800 | [diff] [blame] | 1 | #include <chrono> |
| 2 | |
| 3 | #include "watchdog_test.hpp" |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 4 | |
| 5 | using namespace phosphor::watchdog; |
| 6 | |
William A. Kennington III | 99c69de | 2018-03-01 10:59:22 -0800 | [diff] [blame] | 7 | seconds 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 Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 28 | /** @brief Make sure that watchdog is started and not enabled */ |
| 29 | TEST_F(WdogTest, createWdogAndDontEnable) |
| 30 | { |
William A. Kennington III | f0fe2d6 | 2018-02-28 15:20:16 -0800 | [diff] [blame] | 31 | EXPECT_FALSE(wdog->enabled()); |
| 32 | EXPECT_EQ(0, wdog->timeRemaining()); |
| 33 | EXPECT_FALSE(wdog->timerExpired()); |
William A. Kennington III | 0650a3f | 2018-03-01 10:53:25 -0800 | [diff] [blame] | 34 | EXPECT_FALSE(wdog->timerEnabled()); |
William A. Kennington III | 25c1b20 | 2018-03-01 11:00:19 -0800 | [diff] [blame^] | 35 | |
| 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 Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | /** @brief Make sure that watchdog is started and enabled */ |
| 58 | TEST_F(WdogTest, createWdogAndEnable) |
| 59 | { |
| 60 | // Enable and then verify |
William A. Kennington III | f0fe2d6 | 2018-02-28 15:20:16 -0800 | [diff] [blame] | 61 | EXPECT_TRUE(wdog->enabled(true)); |
| 62 | EXPECT_FALSE(wdog->timerExpired()); |
William A. Kennington III | 0650a3f | 2018-03-01 10:53:25 -0800 | [diff] [blame] | 63 | EXPECT_TRUE(wdog->timerEnabled()); |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 64 | |
| 65 | // Get the configured interval |
William A. Kennington III | f0fe2d6 | 2018-02-28 15:20:16 -0800 | [diff] [blame] | 66 | auto remaining = milliseconds(wdog->timeRemaining()); |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 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 >= defaultInterval - defaultDrift) && |
| 71 | (remaining <= defaultInterval)); |
| 72 | |
William A. Kennington III | f0fe2d6 | 2018-02-28 15:20:16 -0800 | [diff] [blame] | 73 | EXPECT_FALSE(wdog->timerExpired()); |
William A. Kennington III | 0650a3f | 2018-03-01 10:53:25 -0800 | [diff] [blame] | 74 | EXPECT_TRUE(wdog->timerEnabled()); |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | /** @brief Make sure that watchdog is started and enabled. |
| 78 | * Later, disable watchdog |
| 79 | */ |
| 80 | TEST_F(WdogTest, createWdogAndEnableThenDisable) |
| 81 | { |
| 82 | // Enable and then verify |
William A. Kennington III | f0fe2d6 | 2018-02-28 15:20:16 -0800 | [diff] [blame] | 83 | EXPECT_TRUE(wdog->enabled(true)); |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 84 | |
| 85 | // Disable and then verify |
William A. Kennington III | f0fe2d6 | 2018-02-28 15:20:16 -0800 | [diff] [blame] | 86 | EXPECT_FALSE(wdog->enabled(false)); |
| 87 | EXPECT_FALSE(wdog->enabled()); |
| 88 | EXPECT_EQ(0, wdog->timeRemaining()); |
William A. Kennington III | 0650a3f | 2018-03-01 10:53:25 -0800 | [diff] [blame] | 89 | EXPECT_FALSE(wdog->timerExpired()); |
| 90 | EXPECT_FALSE(wdog->timerEnabled()); |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 91 | } |
| 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 | */ |
| 97 | TEST_F(WdogTest, enableWdogAndWait5Seconds) |
| 98 | { |
| 99 | // Enable and then verify |
William A. Kennington III | f0fe2d6 | 2018-02-28 15:20:16 -0800 | [diff] [blame] | 100 | EXPECT_TRUE(wdog->enabled(true)); |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 101 | |
| 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 III | f0fe2d6 | 2018-02-28 15:20:16 -0800 | [diff] [blame] | 107 | auto remaining = milliseconds(wdog->timeRemaining()); |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 108 | 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 III | f0fe2d6 | 2018-02-28 15:20:16 -0800 | [diff] [blame] | 115 | EXPECT_FALSE(wdog->timerExpired()); |
William A. Kennington III | 0650a3f | 2018-03-01 10:53:25 -0800 | [diff] [blame] | 116 | EXPECT_TRUE(wdog->timerEnabled()); |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 117 | } |
| 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 | */ |
| 123 | TEST_F(WdogTest, enableWdogAndResetTo5Seconds) |
| 124 | { |
| 125 | // Enable and then verify |
William A. Kennington III | f0fe2d6 | 2018-02-28 15:20:16 -0800 | [diff] [blame] | 126 | EXPECT_TRUE(wdog->enabled(true)); |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 127 | |
| 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 III | f0fe2d6 | 2018-02-28 15:20:16 -0800 | [diff] [blame] | 134 | wdog->timeRemaining(newTime.count()); |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 135 | |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 136 | // Waiting for expiration |
William A. Kennington III | 99c69de | 2018-03-01 10:59:22 -0800 | [diff] [blame] | 137 | EXPECT_EQ(expireTime - 1s, waitForWatchdog(expireTime)); |
William A. Kennington III | f0fe2d6 | 2018-02-28 15:20:16 -0800 | [diff] [blame] | 138 | EXPECT_TRUE(wdog->timerExpired()); |
William A. Kennington III | 0650a3f | 2018-03-01 10:53:25 -0800 | [diff] [blame] | 139 | EXPECT_FALSE(wdog->timerEnabled()); |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 140 | |
| 141 | // Make sure secondary callback was not called. |
William A. Kennington III | d5d1483 | 2018-02-28 09:54:08 -0800 | [diff] [blame] | 142 | EXPECT_FALSE(expired); |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 143 | } |
| 144 | |
Gunnar Mills | bfe5cb8 | 2017-10-25 20:48:50 -0500 | [diff] [blame] | 145 | /** @brief Make sure the Interval can be updated directly. |
Patrick Venture | 9681634 | 2017-08-17 12:32:22 -0700 | [diff] [blame] | 146 | */ |
| 147 | TEST_F(WdogTest, verifyIntervalUpdateReceived) |
| 148 | { |
| 149 | auto expireTime = seconds(5s); |
| 150 | auto newTime = duration_cast<milliseconds>(expireTime); |
William A. Kennington III | f0fe2d6 | 2018-02-28 15:20:16 -0800 | [diff] [blame] | 151 | wdog->interval(newTime.count()); |
Patrick Venture | 9681634 | 2017-08-17 12:32:22 -0700 | [diff] [blame] | 152 | |
| 153 | // Expect an update in the Interval |
William A. Kennington III | f0fe2d6 | 2018-02-28 15:20:16 -0800 | [diff] [blame] | 154 | EXPECT_EQ(newTime.count(), wdog->interval()); |
Patrick Venture | 9681634 | 2017-08-17 12:32:22 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 157 | /** @brief Make sure that watchdog is started and enabled. |
| 158 | * Wait default interval seconds and make sure that wdog has died |
| 159 | */ |
| 160 | TEST_F(WdogTest, enableWdogAndWaitTillEnd) |
| 161 | { |
| 162 | // Enable and then verify |
William A. Kennington III | f0fe2d6 | 2018-02-28 15:20:16 -0800 | [diff] [blame] | 163 | EXPECT_TRUE(wdog->enabled(true)); |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 164 | auto expireTime = duration_cast<seconds>( |
| 165 | milliseconds(defaultInterval)); |
| 166 | |
| 167 | // Waiting default expiration |
William A. Kennington III | 99c69de | 2018-03-01 10:59:22 -0800 | [diff] [blame] | 168 | EXPECT_EQ(expireTime - 1s, waitForWatchdog(expireTime)); |
| 169 | |
William A. Kennington III | f0fe2d6 | 2018-02-28 15:20:16 -0800 | [diff] [blame] | 170 | EXPECT_TRUE(wdog->enabled()); |
| 171 | EXPECT_EQ(0, wdog->timeRemaining()); |
| 172 | EXPECT_TRUE(wdog->timerExpired()); |
William A. Kennington III | 0650a3f | 2018-03-01 10:53:25 -0800 | [diff] [blame] | 173 | EXPECT_FALSE(wdog->timerEnabled()); |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 174 | } |