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 |
William A. Kennington III | 8cd3839 | 2018-03-01 11:07:36 -0800 | [diff] [blame] | 103 | auto sleepTime = 5s; |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 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()); |
William A. Kennington III | 8cd3839 | 2018-03-01 11:07:36 -0800 | [diff] [blame] | 108 | auto expected = defaultInterval - sleepTime; |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 109 | |
| 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 III | f0fe2d6 | 2018-02-28 15:20:16 -0800 | [diff] [blame] | 114 | EXPECT_FALSE(wdog->timerExpired()); |
William A. Kennington III | 0650a3f | 2018-03-01 10:53:25 -0800 | [diff] [blame] | 115 | EXPECT_TRUE(wdog->timerEnabled()); |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 116 | } |
| 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 | */ |
| 122 | TEST_F(WdogTest, enableWdogAndResetTo5Seconds) |
| 123 | { |
| 124 | // Enable and then verify |
William A. Kennington III | f0fe2d6 | 2018-02-28 15:20:16 -0800 | [diff] [blame] | 125 | EXPECT_TRUE(wdog->enabled(true)); |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 126 | |
| 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 III | 8cd3839 | 2018-03-01 11:07:36 -0800 | [diff] [blame] | 131 | auto expireTime = 5s; |
| 132 | auto expireTimeMs = milliseconds(expireTime).count(); |
| 133 | EXPECT_EQ(expireTimeMs, wdog->timeRemaining(expireTimeMs)); |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 134 | |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 135 | // Waiting for expiration |
William A. Kennington III | 99c69de | 2018-03-01 10:59:22 -0800 | [diff] [blame] | 136 | EXPECT_EQ(expireTime - 1s, waitForWatchdog(expireTime)); |
William A. Kennington III | f0fe2d6 | 2018-02-28 15:20:16 -0800 | [diff] [blame] | 137 | EXPECT_TRUE(wdog->timerExpired()); |
William A. Kennington III | 0650a3f | 2018-03-01 10:53:25 -0800 | [diff] [blame] | 138 | EXPECT_FALSE(wdog->timerEnabled()); |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 139 | |
| 140 | // Make sure secondary callback was not called. |
William A. Kennington III | d5d1483 | 2018-02-28 09:54:08 -0800 | [diff] [blame] | 141 | EXPECT_FALSE(expired); |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 142 | } |
| 143 | |
Gunnar Mills | bfe5cb8 | 2017-10-25 20:48:50 -0500 | [diff] [blame] | 144 | /** @brief Make sure the Interval can be updated directly. |
Patrick Venture | 9681634 | 2017-08-17 12:32:22 -0700 | [diff] [blame] | 145 | */ |
| 146 | TEST_F(WdogTest, verifyIntervalUpdateReceived) |
| 147 | { |
William A. Kennington III | 8cd3839 | 2018-03-01 11:07:36 -0800 | [diff] [blame] | 148 | auto expireTime = 5s; |
| 149 | auto expireTimeMs = milliseconds(expireTime).count(); |
| 150 | EXPECT_EQ(expireTimeMs, wdog->interval(expireTimeMs)); |
Patrick Venture | 9681634 | 2017-08-17 12:32:22 -0700 | [diff] [blame] | 151 | |
| 152 | // Expect an update in the Interval |
William A. Kennington III | 8cd3839 | 2018-03-01 11:07:36 -0800 | [diff] [blame] | 153 | EXPECT_EQ(expireTimeMs, wdog->interval()); |
Patrick Venture | 9681634 | 2017-08-17 12:32:22 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 156 | /** @brief Make sure that watchdog is started and enabled. |
| 157 | * Wait default interval seconds and make sure that wdog has died |
| 158 | */ |
| 159 | TEST_F(WdogTest, enableWdogAndWaitTillEnd) |
| 160 | { |
| 161 | // Enable and then verify |
William A. Kennington III | f0fe2d6 | 2018-02-28 15:20:16 -0800 | [diff] [blame] | 162 | EXPECT_TRUE(wdog->enabled(true)); |
William A. Kennington III | 8cd3839 | 2018-03-01 11:07:36 -0800 | [diff] [blame] | 163 | auto expireTime = duration_cast<seconds>(defaultInterval); |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 164 | |
| 165 | // Waiting default expiration |
William A. Kennington III | 99c69de | 2018-03-01 10:59:22 -0800 | [diff] [blame] | 166 | EXPECT_EQ(expireTime - 1s, waitForWatchdog(expireTime)); |
| 167 | |
William A. Kennington III | 825f498 | 2018-02-27 19:10:56 -0800 | [diff] [blame^] | 168 | EXPECT_FALSE(wdog->enabled()); |
William A. Kennington III | f0fe2d6 | 2018-02-28 15:20:16 -0800 | [diff] [blame] | 169 | EXPECT_EQ(0, wdog->timeRemaining()); |
| 170 | EXPECT_TRUE(wdog->timerExpired()); |
William A. Kennington III | 0650a3f | 2018-03-01 10:53:25 -0800 | [diff] [blame] | 171 | EXPECT_FALSE(wdog->timerEnabled()); |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 172 | } |