William A. Kennington III | af60e63 | 2019-01-16 15:00:18 -0800 | [diff] [blame] | 1 | #include "watchdog.hpp" |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 2 | |
William A. Kennington III | d133108 | 2018-02-27 18:47:05 -0800 | [diff] [blame] | 3 | #include <memory> |
William A. Kennington III | f505fc0 | 2018-09-12 18:30:09 -0700 | [diff] [blame] | 4 | #include <thread> |
William A. Kennington III | d133108 | 2018-02-27 18:47:05 -0800 | [diff] [blame] | 5 | #include <utility> |
William A. Kennington III | 99c69de | 2018-03-01 10:59:22 -0800 | [diff] [blame] | 6 | |
Ofer Yehielli | c35135d | 2019-06-14 11:30:25 -0700 | [diff] [blame] | 7 | namespace phosphor |
| 8 | { |
| 9 | namespace watchdog |
| 10 | { |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 11 | |
William A. Kennington III | 3964f00 | 2019-01-16 15:43:43 -0800 | [diff] [blame] | 12 | WdogTest::Quantum WdogTest::waitForWatchdog(Quantum timeLimit) |
William A. Kennington III | 99c69de | 2018-03-01 10:59:22 -0800 | [diff] [blame] | 13 | { |
| 14 | auto previousTimeRemaining = wdog->timeRemaining(); |
William A. Kennington III | 3964f00 | 2019-01-16 15:43:43 -0800 | [diff] [blame] | 15 | auto ret = Quantum(0); |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 16 | while (ret < timeLimit && previousTimeRemaining >= wdog->timeRemaining() && |
William A. Kennington III | 99c69de | 2018-03-01 10:59:22 -0800 | [diff] [blame] | 17 | wdog->timerEnabled()) |
| 18 | { |
| 19 | previousTimeRemaining = wdog->timeRemaining(); |
| 20 | |
William A. Kennington III | 3964f00 | 2019-01-16 15:43:43 -0800 | [diff] [blame] | 21 | constexpr auto sleepTime = Quantum(1); |
William A. Kennington III | f505fc0 | 2018-09-12 18:30:09 -0700 | [diff] [blame] | 22 | if (event.run(sleepTime) == 0) |
William A. Kennington III | 99c69de | 2018-03-01 10:59:22 -0800 | [diff] [blame] | 23 | { |
| 24 | ret += sleepTime; |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | return ret; |
| 29 | } |
| 30 | |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 31 | /** @brief Make sure that watchdog is started and not enabled */ |
| 32 | TEST_F(WdogTest, createWdogAndDontEnable) |
| 33 | { |
William A. Kennington III | f0fe2d6 | 2018-02-28 15:20:16 -0800 | [diff] [blame] | 34 | EXPECT_FALSE(wdog->enabled()); |
| 35 | EXPECT_EQ(0, wdog->timeRemaining()); |
| 36 | EXPECT_FALSE(wdog->timerExpired()); |
William A. Kennington III | 0650a3f | 2018-03-01 10:53:25 -0800 | [diff] [blame] | 37 | EXPECT_FALSE(wdog->timerEnabled()); |
William A. Kennington III | 25c1b20 | 2018-03-01 11:00:19 -0800 | [diff] [blame] | 38 | |
| 39 | // We should be able to configure persistent properties |
| 40 | // while disabled |
| 41 | auto newAction = Watchdog::Action::PowerOff; |
| 42 | EXPECT_EQ(newAction, wdog->expireAction(newAction)); |
| 43 | auto newIntervalMs = milliseconds(defaultInterval * 2).count(); |
| 44 | EXPECT_EQ(newIntervalMs, wdog->interval(newIntervalMs)); |
| 45 | |
| 46 | EXPECT_EQ(newAction, wdog->expireAction()); |
| 47 | EXPECT_EQ(newIntervalMs, wdog->interval()); |
| 48 | |
| 49 | // We won't be able to configure timeRemaining |
| 50 | EXPECT_EQ(0, wdog->timeRemaining(1000)); |
| 51 | EXPECT_EQ(0, wdog->timeRemaining()); |
| 52 | |
| 53 | // Timer should not have become enabled |
| 54 | EXPECT_FALSE(wdog->enabled()); |
| 55 | EXPECT_EQ(0, wdog->timeRemaining()); |
| 56 | EXPECT_FALSE(wdog->timerExpired()); |
| 57 | EXPECT_FALSE(wdog->timerEnabled()); |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | /** @brief Make sure that watchdog is started and enabled */ |
| 61 | TEST_F(WdogTest, createWdogAndEnable) |
| 62 | { |
| 63 | // Enable and then verify |
William A. Kennington III | f0fe2d6 | 2018-02-28 15:20:16 -0800 | [diff] [blame] | 64 | EXPECT_TRUE(wdog->enabled(true)); |
| 65 | EXPECT_FALSE(wdog->timerExpired()); |
William A. Kennington III | 0650a3f | 2018-03-01 10:53:25 -0800 | [diff] [blame] | 66 | EXPECT_TRUE(wdog->timerEnabled()); |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 67 | |
| 68 | // Get the configured interval |
William A. Kennington III | f0fe2d6 | 2018-02-28 15:20:16 -0800 | [diff] [blame] | 69 | auto remaining = milliseconds(wdog->timeRemaining()); |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 70 | |
| 71 | // Its possible that we are off by few msecs depending on |
| 72 | // how we get scheduled. So checking a range here. |
William A. Kennington III | 3964f00 | 2019-01-16 15:43:43 -0800 | [diff] [blame] | 73 | EXPECT_TRUE((remaining >= defaultInterval - Quantum(1)) && |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 74 | (remaining <= defaultInterval)); |
| 75 | |
William A. Kennington III | f0fe2d6 | 2018-02-28 15:20:16 -0800 | [diff] [blame] | 76 | EXPECT_FALSE(wdog->timerExpired()); |
William A. Kennington III | 0650a3f | 2018-03-01 10:53:25 -0800 | [diff] [blame] | 77 | EXPECT_TRUE(wdog->timerEnabled()); |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | /** @brief Make sure that watchdog is started and enabled. |
| 81 | * Later, disable watchdog |
| 82 | */ |
| 83 | TEST_F(WdogTest, createWdogAndEnableThenDisable) |
| 84 | { |
| 85 | // Enable and then verify |
William A. Kennington III | f0fe2d6 | 2018-02-28 15:20:16 -0800 | [diff] [blame] | 86 | EXPECT_TRUE(wdog->enabled(true)); |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 87 | |
| 88 | // Disable and then verify |
William A. Kennington III | f0fe2d6 | 2018-02-28 15:20:16 -0800 | [diff] [blame] | 89 | EXPECT_FALSE(wdog->enabled(false)); |
| 90 | EXPECT_FALSE(wdog->enabled()); |
| 91 | EXPECT_EQ(0, wdog->timeRemaining()); |
William A. Kennington III | 0650a3f | 2018-03-01 10:53:25 -0800 | [diff] [blame] | 92 | EXPECT_FALSE(wdog->timerExpired()); |
| 93 | EXPECT_FALSE(wdog->timerEnabled()); |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | /** @brief Make sure that watchdog is started and enabled. |
William A. Kennington III | 3964f00 | 2019-01-16 15:43:43 -0800 | [diff] [blame] | 97 | * Wait for 5 quantums and make sure that the remaining |
| 98 | * time shows 5 fewer quantums. |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 99 | */ |
William A. Kennington III | 3964f00 | 2019-01-16 15:43:43 -0800 | [diff] [blame] | 100 | TEST_F(WdogTest, enableWdogAndWait5Quantums) |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 101 | { |
| 102 | // Enable and then verify |
William A. Kennington III | f0fe2d6 | 2018-02-28 15:20:16 -0800 | [diff] [blame] | 103 | EXPECT_TRUE(wdog->enabled(true)); |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 104 | |
William A. Kennington III | 3964f00 | 2019-01-16 15:43:43 -0800 | [diff] [blame] | 105 | // Sleep for 5 quantums |
| 106 | auto sleepTime = Quantum(2); |
| 107 | ASSERT_LT(sleepTime, defaultInterval); |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 108 | std::this_thread::sleep_for(sleepTime); |
| 109 | |
William A. Kennington III | 3964f00 | 2019-01-16 15:43:43 -0800 | [diff] [blame] | 110 | // Get the remaining time again and expectation is that we get fewer |
William A. Kennington III | f0fe2d6 | 2018-02-28 15:20:16 -0800 | [diff] [blame] | 111 | auto remaining = milliseconds(wdog->timeRemaining()); |
William A. Kennington III | 8cd3839 | 2018-03-01 11:07:36 -0800 | [diff] [blame] | 112 | auto expected = defaultInterval - sleepTime; |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 113 | |
| 114 | // Its possible that we are off by few msecs depending on |
| 115 | // how we get scheduled. So checking a range here. |
William A. Kennington III | 3964f00 | 2019-01-16 15:43:43 -0800 | [diff] [blame] | 116 | EXPECT_TRUE((remaining >= expected - Quantum(1)) && |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 117 | (remaining <= expected)); |
William A. Kennington III | f0fe2d6 | 2018-02-28 15:20:16 -0800 | [diff] [blame] | 118 | EXPECT_FALSE(wdog->timerExpired()); |
William A. Kennington III | 0650a3f | 2018-03-01 10:53:25 -0800 | [diff] [blame] | 119 | EXPECT_TRUE(wdog->timerEnabled()); |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | /** @brief Make sure that watchdog is started and enabled. |
William A. Kennington III | 3964f00 | 2019-01-16 15:43:43 -0800 | [diff] [blame] | 123 | * Wait 1 quantum and then reset the timer to 5 quantums |
| 124 | * and then expect the watchdog to expire in 5 quantums |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 125 | */ |
William A. Kennington III | 3964f00 | 2019-01-16 15:43:43 -0800 | [diff] [blame] | 126 | TEST_F(WdogTest, enableWdogAndResetTo5Quantums) |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 127 | { |
| 128 | // Enable and then verify |
William A. Kennington III | f0fe2d6 | 2018-02-28 15:20:16 -0800 | [diff] [blame] | 129 | EXPECT_TRUE(wdog->enabled(true)); |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 130 | |
| 131 | // Sleep for 1 second |
William A. Kennington III | 3964f00 | 2019-01-16 15:43:43 -0800 | [diff] [blame] | 132 | std::this_thread::sleep_for(Quantum(1)); |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 133 | |
William A. Kennington III | f505fc0 | 2018-09-12 18:30:09 -0700 | [diff] [blame] | 134 | // Timer should still be running unexpired |
| 135 | EXPECT_FALSE(wdog->timerExpired()); |
| 136 | EXPECT_TRUE(wdog->timerEnabled()); |
| 137 | |
William A. Kennington III | 3964f00 | 2019-01-16 15:43:43 -0800 | [diff] [blame] | 138 | // Next timer will expire in 5 quantums from now. |
| 139 | auto expireTime = Quantum(5); |
William A. Kennington III | 8cd3839 | 2018-03-01 11:07:36 -0800 | [diff] [blame] | 140 | auto expireTimeMs = milliseconds(expireTime).count(); |
| 141 | EXPECT_EQ(expireTimeMs, wdog->timeRemaining(expireTimeMs)); |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 142 | |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 143 | // Waiting for expiration |
William A. Kennington III | 3964f00 | 2019-01-16 15:43:43 -0800 | [diff] [blame] | 144 | EXPECT_EQ(expireTime - Quantum(1), waitForWatchdog(expireTime)); |
William A. Kennington III | f0fe2d6 | 2018-02-28 15:20:16 -0800 | [diff] [blame] | 145 | EXPECT_TRUE(wdog->timerExpired()); |
William A. Kennington III | 0650a3f | 2018-03-01 10:53:25 -0800 | [diff] [blame] | 146 | EXPECT_FALSE(wdog->timerEnabled()); |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 147 | } |
| 148 | |
Gunnar Mills | bfe5cb8 | 2017-10-25 20:48:50 -0500 | [diff] [blame] | 149 | /** @brief Make sure the Interval can be updated directly. |
Patrick Venture | 9681634 | 2017-08-17 12:32:22 -0700 | [diff] [blame] | 150 | */ |
| 151 | TEST_F(WdogTest, verifyIntervalUpdateReceived) |
| 152 | { |
William A. Kennington III | 3964f00 | 2019-01-16 15:43:43 -0800 | [diff] [blame] | 153 | auto expireTime = Quantum(5); |
William A. Kennington III | 8cd3839 | 2018-03-01 11:07:36 -0800 | [diff] [blame] | 154 | auto expireTimeMs = milliseconds(expireTime).count(); |
| 155 | EXPECT_EQ(expireTimeMs, wdog->interval(expireTimeMs)); |
Patrick Venture | 9681634 | 2017-08-17 12:32:22 -0700 | [diff] [blame] | 156 | |
| 157 | // Expect an update in the Interval |
William A. Kennington III | 8cd3839 | 2018-03-01 11:07:36 -0800 | [diff] [blame] | 158 | EXPECT_EQ(expireTimeMs, wdog->interval()); |
Patrick Venture | 9681634 | 2017-08-17 12:32:22 -0700 | [diff] [blame] | 159 | } |
| 160 | |
William A. Kennington III | d4cbc5a | 2018-09-24 14:23:05 -0700 | [diff] [blame] | 161 | /** @brief Make sure the Interval can be updated while the timer is running. |
| 162 | */ |
| 163 | TEST_F(WdogTest, verifyIntervalUpdateRunning) |
| 164 | { |
| 165 | const auto oldInterval = milliseconds(wdog->interval()); |
| 166 | const auto newInterval = 5s; |
| 167 | |
| 168 | EXPECT_TRUE(wdog->enabled(true)); |
| 169 | auto remaining = milliseconds(wdog->timeRemaining()); |
| 170 | EXPECT_GE(oldInterval, remaining); |
William A. Kennington III | 3964f00 | 2019-01-16 15:43:43 -0800 | [diff] [blame] | 171 | EXPECT_LE(oldInterval - Quantum(1), remaining); |
William A. Kennington III | d4cbc5a | 2018-09-24 14:23:05 -0700 | [diff] [blame] | 172 | EXPECT_EQ(newInterval, |
| 173 | milliseconds(wdog->interval(milliseconds(newInterval).count()))); |
| 174 | |
| 175 | // Expect only the interval to update |
| 176 | remaining = milliseconds(wdog->timeRemaining()); |
| 177 | EXPECT_GE(oldInterval, remaining); |
William A. Kennington III | 3964f00 | 2019-01-16 15:43:43 -0800 | [diff] [blame] | 178 | EXPECT_LE(oldInterval - Quantum(1), remaining); |
William A. Kennington III | d4cbc5a | 2018-09-24 14:23:05 -0700 | [diff] [blame] | 179 | EXPECT_EQ(newInterval, milliseconds(wdog->interval())); |
| 180 | |
| 181 | // Expect reset to use the new interval |
| 182 | wdog->resetTimeRemaining(false); |
| 183 | remaining = milliseconds(wdog->timeRemaining()); |
| 184 | EXPECT_GE(newInterval, remaining); |
William A. Kennington III | 3964f00 | 2019-01-16 15:43:43 -0800 | [diff] [blame] | 185 | EXPECT_LE(newInterval - Quantum(1), remaining); |
William A. Kennington III | d4cbc5a | 2018-09-24 14:23:05 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 188 | /** @brief Make sure that watchdog is started and enabled. |
William A. Kennington III | 3964f00 | 2019-01-16 15:43:43 -0800 | [diff] [blame] | 189 | * Wait default interval quantums and make sure that wdog has died |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 190 | */ |
| 191 | TEST_F(WdogTest, enableWdogAndWaitTillEnd) |
| 192 | { |
| 193 | // Enable and then verify |
William A. Kennington III | f0fe2d6 | 2018-02-28 15:20:16 -0800 | [diff] [blame] | 194 | EXPECT_TRUE(wdog->enabled(true)); |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 195 | |
| 196 | // Waiting default expiration |
William A. Kennington III | 3964f00 | 2019-01-16 15:43:43 -0800 | [diff] [blame] | 197 | EXPECT_EQ(defaultInterval - Quantum(1), waitForWatchdog(defaultInterval)); |
William A. Kennington III | 99c69de | 2018-03-01 10:59:22 -0800 | [diff] [blame] | 198 | |
William A. Kennington III | 825f498 | 2018-02-27 19:10:56 -0800 | [diff] [blame] | 199 | EXPECT_FALSE(wdog->enabled()); |
William A. Kennington III | f0fe2d6 | 2018-02-28 15:20:16 -0800 | [diff] [blame] | 200 | EXPECT_EQ(0, wdog->timeRemaining()); |
| 201 | EXPECT_TRUE(wdog->timerExpired()); |
William A. Kennington III | 0650a3f | 2018-03-01 10:53:25 -0800 | [diff] [blame] | 202 | EXPECT_FALSE(wdog->timerEnabled()); |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 203 | } |
William A. Kennington III | d133108 | 2018-02-27 18:47:05 -0800 | [diff] [blame] | 204 | |
| 205 | /** @brief Make sure the watchdog is started and enabled with a fallback |
| 206 | * Wait through the initial trip and ensure the fallback is observed |
| 207 | * Make sure that fallback runs to completion and ensure the watchdog |
| 208 | * is disabled |
| 209 | */ |
| 210 | TEST_F(WdogTest, enableWdogWithFallbackTillEnd) |
| 211 | { |
William A. Kennington III | 3964f00 | 2019-01-16 15:43:43 -0800 | [diff] [blame] | 212 | auto primaryInterval = Quantum(5); |
William A. Kennington III | d133108 | 2018-02-27 18:47:05 -0800 | [diff] [blame] | 213 | auto primaryIntervalMs = milliseconds(primaryInterval).count(); |
| 214 | auto fallbackInterval = primaryInterval * 2; |
| 215 | auto fallbackIntervalMs = milliseconds(fallbackInterval).count(); |
| 216 | |
| 217 | // We need to make a wdog with the right fallback options |
| 218 | // The interval is set to be noticeably different from the default |
| 219 | // so we can always tell the difference |
William A. Kennington III | 8cf5f64 | 2019-01-16 16:58:14 -0800 | [diff] [blame] | 220 | Watchdog::Fallback fallback; |
| 221 | fallback.action = Watchdog::Action::PowerOff; |
| 222 | fallback.interval = static_cast<uint64_t>(fallbackIntervalMs); |
| 223 | fallback.always = false; |
William A. Kennington III | f505fc0 | 2018-09-12 18:30:09 -0700 | [diff] [blame] | 224 | wdog = std::make_unique<Watchdog>(bus, TEST_PATH, event, |
William A. Kennington III | 3bb2f40 | 2018-09-13 00:35:47 -0700 | [diff] [blame] | 225 | Watchdog::ActionTargetMap(), |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 226 | std::move(fallback)); |
William A. Kennington III | d133108 | 2018-02-27 18:47:05 -0800 | [diff] [blame] | 227 | EXPECT_EQ(primaryInterval, milliseconds(wdog->interval(primaryIntervalMs))); |
| 228 | EXPECT_FALSE(wdog->enabled()); |
| 229 | EXPECT_EQ(0, wdog->timeRemaining()); |
| 230 | |
| 231 | // Enable and then verify |
| 232 | EXPECT_TRUE(wdog->enabled(true)); |
| 233 | |
| 234 | // Waiting default expiration |
William A. Kennington III | 3964f00 | 2019-01-16 15:43:43 -0800 | [diff] [blame] | 235 | EXPECT_EQ(primaryInterval - Quantum(1), waitForWatchdog(primaryInterval)); |
William A. Kennington III | d133108 | 2018-02-27 18:47:05 -0800 | [diff] [blame] | 236 | |
| 237 | // We should now have entered the fallback once the primary expires |
| 238 | EXPECT_FALSE(wdog->enabled()); |
| 239 | auto remaining = milliseconds(wdog->timeRemaining()); |
| 240 | EXPECT_GE(fallbackInterval, remaining); |
| 241 | EXPECT_LT(primaryInterval, remaining); |
| 242 | EXPECT_FALSE(wdog->timerExpired()); |
| 243 | EXPECT_TRUE(wdog->timerEnabled()); |
| 244 | |
| 245 | // We should still be ticking in fallback when setting action or interval |
William A. Kennington III | 3964f00 | 2019-01-16 15:43:43 -0800 | [diff] [blame] | 246 | auto newInterval = primaryInterval - Quantum(1); |
William A. Kennington III | d133108 | 2018-02-27 18:47:05 -0800 | [diff] [blame] | 247 | auto newIntervalMs = milliseconds(newInterval).count(); |
| 248 | EXPECT_EQ(newInterval, milliseconds(wdog->interval(newIntervalMs))); |
| 249 | EXPECT_EQ(Watchdog::Action::None, |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 250 | wdog->expireAction(Watchdog::Action::None)); |
William A. Kennington III | d133108 | 2018-02-27 18:47:05 -0800 | [diff] [blame] | 251 | |
| 252 | EXPECT_FALSE(wdog->enabled()); |
| 253 | EXPECT_GE(remaining, milliseconds(wdog->timeRemaining())); |
| 254 | EXPECT_LT(primaryInterval, milliseconds(wdog->timeRemaining())); |
| 255 | EXPECT_FALSE(wdog->timerExpired()); |
| 256 | EXPECT_TRUE(wdog->timerEnabled()); |
| 257 | |
| 258 | // Test that setting the timeRemaining always resets the timer to the |
| 259 | // fallback interval |
| 260 | EXPECT_EQ(fallback.interval, wdog->timeRemaining(primaryInterval.count())); |
| 261 | EXPECT_FALSE(wdog->enabled()); |
| 262 | |
| 263 | remaining = milliseconds(wdog->timeRemaining()); |
| 264 | EXPECT_GE(fallbackInterval, remaining); |
William A. Kennington III | 3964f00 | 2019-01-16 15:43:43 -0800 | [diff] [blame] | 265 | EXPECT_LE(fallbackInterval - Quantum(1), remaining); |
William A. Kennington III | d133108 | 2018-02-27 18:47:05 -0800 | [diff] [blame] | 266 | EXPECT_FALSE(wdog->timerExpired()); |
| 267 | EXPECT_TRUE(wdog->timerEnabled()); |
| 268 | |
| 269 | // Waiting fallback expiration |
William A. Kennington III | 3964f00 | 2019-01-16 15:43:43 -0800 | [diff] [blame] | 270 | EXPECT_EQ(fallbackInterval - Quantum(1), waitForWatchdog(fallbackInterval)); |
William A. Kennington III | d133108 | 2018-02-27 18:47:05 -0800 | [diff] [blame] | 271 | |
| 272 | // We should now have disabled the watchdog after the fallback expires |
| 273 | EXPECT_FALSE(wdog->enabled()); |
| 274 | EXPECT_EQ(0, wdog->timeRemaining()); |
| 275 | EXPECT_TRUE(wdog->timerExpired()); |
| 276 | EXPECT_FALSE(wdog->timerEnabled()); |
| 277 | |
| 278 | // Make sure enabling the watchdog again works |
| 279 | EXPECT_TRUE(wdog->enabled(true)); |
| 280 | |
| 281 | // We should have re-entered the primary |
| 282 | EXPECT_TRUE(wdog->enabled()); |
| 283 | EXPECT_GE(primaryInterval, milliseconds(wdog->timeRemaining())); |
| 284 | EXPECT_FALSE(wdog->timerExpired()); |
| 285 | EXPECT_TRUE(wdog->timerEnabled()); |
| 286 | } |
| 287 | |
| 288 | /** @brief Make sure the watchdog is started and enabled with a fallback |
| 289 | * Wait through the initial trip and ensure the fallback is observed |
| 290 | * Make sure that we can re-enable the watchdog during fallback |
| 291 | */ |
| 292 | TEST_F(WdogTest, enableWdogWithFallbackReEnable) |
| 293 | { |
William A. Kennington III | 3964f00 | 2019-01-16 15:43:43 -0800 | [diff] [blame] | 294 | auto primaryInterval = Quantum(5); |
William A. Kennington III | d133108 | 2018-02-27 18:47:05 -0800 | [diff] [blame] | 295 | auto primaryIntervalMs = milliseconds(primaryInterval).count(); |
| 296 | auto fallbackInterval = primaryInterval * 2; |
| 297 | auto fallbackIntervalMs = milliseconds(fallbackInterval).count(); |
| 298 | |
| 299 | // We need to make a wdog with the right fallback options |
| 300 | // The interval is set to be noticeably different from the default |
| 301 | // so we can always tell the difference |
William A. Kennington III | 8cf5f64 | 2019-01-16 16:58:14 -0800 | [diff] [blame] | 302 | Watchdog::Fallback fallback; |
| 303 | fallback.action = Watchdog::Action::PowerOff; |
| 304 | fallback.interval = static_cast<uint64_t>(fallbackIntervalMs); |
| 305 | fallback.always = false; |
William A. Kennington III | f505fc0 | 2018-09-12 18:30:09 -0700 | [diff] [blame] | 306 | wdog = std::make_unique<Watchdog>(bus, TEST_PATH, event, |
William A. Kennington III | 3bb2f40 | 2018-09-13 00:35:47 -0700 | [diff] [blame] | 307 | Watchdog::ActionTargetMap(), |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 308 | std::move(fallback)); |
William A. Kennington III | d133108 | 2018-02-27 18:47:05 -0800 | [diff] [blame] | 309 | EXPECT_EQ(primaryInterval, milliseconds(wdog->interval(primaryIntervalMs))); |
| 310 | EXPECT_FALSE(wdog->enabled()); |
| 311 | EXPECT_EQ(0, wdog->timeRemaining()); |
| 312 | EXPECT_FALSE(wdog->timerExpired()); |
| 313 | EXPECT_FALSE(wdog->timerEnabled()); |
| 314 | |
| 315 | // Enable and then verify |
| 316 | EXPECT_TRUE(wdog->enabled(true)); |
| 317 | |
| 318 | // Waiting default expiration |
William A. Kennington III | 3964f00 | 2019-01-16 15:43:43 -0800 | [diff] [blame] | 319 | EXPECT_EQ(primaryInterval - Quantum(1), waitForWatchdog(primaryInterval)); |
William A. Kennington III | d133108 | 2018-02-27 18:47:05 -0800 | [diff] [blame] | 320 | |
| 321 | // We should now have entered the fallback once the primary expires |
| 322 | EXPECT_FALSE(wdog->enabled()); |
| 323 | auto remaining = milliseconds(wdog->timeRemaining()); |
| 324 | EXPECT_GE(fallbackInterval, remaining); |
| 325 | EXPECT_LT(primaryInterval, remaining); |
| 326 | EXPECT_FALSE(wdog->timerExpired()); |
| 327 | EXPECT_TRUE(wdog->timerEnabled()); |
| 328 | |
| 329 | EXPECT_TRUE(wdog->enabled(true)); |
| 330 | |
| 331 | // We should have re-entered the primary |
| 332 | EXPECT_TRUE(wdog->enabled()); |
| 333 | EXPECT_GE(primaryInterval, milliseconds(wdog->timeRemaining())); |
| 334 | EXPECT_FALSE(wdog->timerExpired()); |
| 335 | EXPECT_TRUE(wdog->timerEnabled()); |
| 336 | } |
William A. Kennington III | 2235219 | 2018-02-27 18:51:44 -0800 | [diff] [blame] | 337 | |
| 338 | /** @brief Make sure the watchdog is started and with a fallback without |
| 339 | * sending an enable |
| 340 | * Then enable the watchdog |
| 341 | * Wait through the initial trip and ensure the fallback is observed |
| 342 | * Make sure that fallback runs to completion and ensure the watchdog |
| 343 | * is in the fallback state again |
| 344 | */ |
| 345 | TEST_F(WdogTest, enableWdogWithFallbackAlways) |
| 346 | { |
William A. Kennington III | 3964f00 | 2019-01-16 15:43:43 -0800 | [diff] [blame] | 347 | auto primaryInterval = Quantum(5); |
William A. Kennington III | 2235219 | 2018-02-27 18:51:44 -0800 | [diff] [blame] | 348 | auto primaryIntervalMs = milliseconds(primaryInterval).count(); |
| 349 | auto fallbackInterval = primaryInterval * 2; |
| 350 | auto fallbackIntervalMs = milliseconds(fallbackInterval).count(); |
| 351 | |
| 352 | // We need to make a wdog with the right fallback options |
| 353 | // The interval is set to be noticeably different from the default |
| 354 | // so we can always tell the difference |
William A. Kennington III | 8cf5f64 | 2019-01-16 16:58:14 -0800 | [diff] [blame] | 355 | Watchdog::Fallback fallback; |
| 356 | fallback.action = Watchdog::Action::PowerOff; |
| 357 | fallback.interval = static_cast<uint64_t>(fallbackIntervalMs); |
| 358 | fallback.always = true; |
William A. Kennington III | f505fc0 | 2018-09-12 18:30:09 -0700 | [diff] [blame] | 359 | wdog = std::make_unique<Watchdog>(bus, TEST_PATH, event, |
Ofer Yehielli | c35135d | 2019-06-14 11:30:25 -0700 | [diff] [blame] | 360 | Watchdog::ActionTargetMap(), fallback, |
| 361 | milliseconds(TEST_MIN_INTERVAL).count()); |
| 362 | |
| 363 | // Make sure defualt interval is biggger than min interval |
| 364 | EXPECT_LT(milliseconds((TEST_MIN_INTERVAL).count()), |
| 365 | milliseconds(wdog->interval())); |
| 366 | |
William A. Kennington III | 2235219 | 2018-02-27 18:51:44 -0800 | [diff] [blame] | 367 | EXPECT_EQ(primaryInterval, milliseconds(wdog->interval(primaryIntervalMs))); |
| 368 | EXPECT_FALSE(wdog->enabled()); |
| 369 | auto remaining = milliseconds(wdog->timeRemaining()); |
| 370 | EXPECT_GE(fallbackInterval, remaining); |
| 371 | EXPECT_LT(primaryInterval, remaining); |
| 372 | EXPECT_FALSE(wdog->timerExpired()); |
| 373 | EXPECT_TRUE(wdog->timerEnabled()); |
| 374 | |
| 375 | // Enable and then verify |
| 376 | EXPECT_TRUE(wdog->enabled(true)); |
| 377 | EXPECT_GE(primaryInterval, milliseconds(wdog->timeRemaining())); |
| 378 | |
| 379 | // Waiting default expiration |
William A. Kennington III | 3964f00 | 2019-01-16 15:43:43 -0800 | [diff] [blame] | 380 | EXPECT_EQ(primaryInterval - Quantum(1), waitForWatchdog(primaryInterval)); |
William A. Kennington III | 2235219 | 2018-02-27 18:51:44 -0800 | [diff] [blame] | 381 | |
| 382 | // We should now have entered the fallback once the primary expires |
| 383 | EXPECT_FALSE(wdog->enabled()); |
| 384 | remaining = milliseconds(wdog->timeRemaining()); |
| 385 | EXPECT_GE(fallbackInterval, remaining); |
| 386 | EXPECT_LT(primaryInterval, remaining); |
| 387 | EXPECT_FALSE(wdog->timerExpired()); |
| 388 | EXPECT_TRUE(wdog->timerEnabled()); |
| 389 | |
| 390 | // Waiting fallback expiration |
William A. Kennington III | 3964f00 | 2019-01-16 15:43:43 -0800 | [diff] [blame] | 391 | EXPECT_EQ(fallbackInterval - Quantum(1), waitForWatchdog(fallbackInterval)); |
William A. Kennington III | 2235219 | 2018-02-27 18:51:44 -0800 | [diff] [blame] | 392 | |
| 393 | // We should now enter the fallback again |
| 394 | EXPECT_FALSE(wdog->enabled()); |
| 395 | remaining = milliseconds(wdog->timeRemaining()); |
| 396 | EXPECT_GE(fallbackInterval, remaining); |
| 397 | EXPECT_LT(primaryInterval, remaining); |
| 398 | EXPECT_FALSE(wdog->timerExpired()); |
| 399 | EXPECT_TRUE(wdog->timerEnabled()); |
| 400 | } |
Ofer Yehielli | c35135d | 2019-06-14 11:30:25 -0700 | [diff] [blame] | 401 | |
| 402 | /** @brief Test minimal interval |
| 403 | * The minimal interval was set 2 seconds |
| 404 | * Test that when setting interval to 1s , it is still returning 2s |
| 405 | */ |
| 406 | TEST_F(WdogTest, verifyMinIntervalSetting) |
| 407 | { |
| 408 | auto newInterval = Quantum(1); |
| 409 | auto newIntervalMs = milliseconds(newInterval).count(); |
| 410 | auto minIntervalMs = milliseconds(TEST_MIN_INTERVAL).count(); |
| 411 | |
| 412 | // Check first that the current interval is greater than minInterval |
| 413 | EXPECT_LT(minIntervalMs, wdog->interval()); |
| 414 | // Check that the interval was not set to smaller value than minInterval |
| 415 | EXPECT_EQ(minIntervalMs, wdog->interval(newIntervalMs)); |
| 416 | // Check that the interval was not set to smaller value than minInterval |
| 417 | EXPECT_EQ(minIntervalMs, wdog->interval()); |
| 418 | } |
| 419 | |
| 420 | /** @brief Test minimal interval |
| 421 | * Initiate default Watchdog in order to get the default |
| 422 | * interval. |
| 423 | * Initiate watchdog with minInterval greater than default |
| 424 | * interval, and make sure the default interval was set to the |
| 425 | * minInterval. |
| 426 | */ |
| 427 | TEST_F(WdogTest, verifyConstructorMinIntervalSetting) |
| 428 | { |
| 429 | // Initiate default Watchdog and get the default interval value. |
| 430 | wdog = std::make_unique<Watchdog>(bus, TEST_PATH, event); |
| 431 | auto defaultInterval = wdog->interval(); |
| 432 | auto minInterval = defaultInterval + 100; |
| 433 | // We initiate a new Watchdog with min interval greater than the default |
| 434 | // intrval |
| 435 | wdog = std::make_unique<Watchdog>(bus, TEST_PATH, event, |
| 436 | Watchdog::ActionTargetMap(), std::nullopt, |
| 437 | minInterval); |
| 438 | // Check that the interval was set to the minInterval |
| 439 | EXPECT_EQ(minInterval, wdog->interval()); |
| 440 | } |
| 441 | |
| 442 | } // namespace watchdog |
| 443 | } // namespace phosphor |