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 | |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 7 | using namespace phosphor::watchdog; |
| 8 | |
William A. Kennington III | 99c69de | 2018-03-01 10:59:22 -0800 | [diff] [blame] | 9 | seconds WdogTest::waitForWatchdog(seconds timeLimit) |
| 10 | { |
| 11 | auto previousTimeRemaining = wdog->timeRemaining(); |
| 12 | auto ret = 0s; |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 13 | while (ret < timeLimit && previousTimeRemaining >= wdog->timeRemaining() && |
William A. Kennington III | 99c69de | 2018-03-01 10:59:22 -0800 | [diff] [blame] | 14 | wdog->timerEnabled()) |
| 15 | { |
| 16 | previousTimeRemaining = wdog->timeRemaining(); |
| 17 | |
William A. Kennington III | f505fc0 | 2018-09-12 18:30:09 -0700 | [diff] [blame] | 18 | constexpr auto sleepTime = 1s; |
| 19 | if (event.run(sleepTime) == 0) |
William A. Kennington III | 99c69de | 2018-03-01 10:59:22 -0800 | [diff] [blame] | 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 | |
William A. Kennington III | f505fc0 | 2018-09-12 18:30:09 -0700 | [diff] [blame] | 130 | // Timer should still be running unexpired |
| 131 | EXPECT_FALSE(wdog->timerExpired()); |
| 132 | EXPECT_TRUE(wdog->timerEnabled()); |
| 133 | |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 134 | // Next timer will expire in 5 seconds from now. |
William A. Kennington III | 8cd3839 | 2018-03-01 11:07:36 -0800 | [diff] [blame] | 135 | auto expireTime = 5s; |
| 136 | auto expireTimeMs = milliseconds(expireTime).count(); |
| 137 | EXPECT_EQ(expireTimeMs, wdog->timeRemaining(expireTimeMs)); |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 138 | |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 139 | // Waiting for expiration |
William A. Kennington III | 99c69de | 2018-03-01 10:59:22 -0800 | [diff] [blame] | 140 | EXPECT_EQ(expireTime - 1s, waitForWatchdog(expireTime)); |
William A. Kennington III | f0fe2d6 | 2018-02-28 15:20:16 -0800 | [diff] [blame] | 141 | EXPECT_TRUE(wdog->timerExpired()); |
William A. Kennington III | 0650a3f | 2018-03-01 10:53:25 -0800 | [diff] [blame] | 142 | EXPECT_FALSE(wdog->timerEnabled()); |
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 | { |
William A. Kennington III | 8cd3839 | 2018-03-01 11:07:36 -0800 | [diff] [blame] | 149 | auto expireTime = 5s; |
| 150 | auto expireTimeMs = milliseconds(expireTime).count(); |
| 151 | EXPECT_EQ(expireTimeMs, wdog->interval(expireTimeMs)); |
Patrick Venture | 9681634 | 2017-08-17 12:32:22 -0700 | [diff] [blame] | 152 | |
| 153 | // Expect an update in the Interval |
William A. Kennington III | 8cd3839 | 2018-03-01 11:07:36 -0800 | [diff] [blame] | 154 | EXPECT_EQ(expireTimeMs, wdog->interval()); |
Patrick Venture | 9681634 | 2017-08-17 12:32:22 -0700 | [diff] [blame] | 155 | } |
| 156 | |
William A. Kennington III | d4cbc5a | 2018-09-24 14:23:05 -0700 | [diff] [blame] | 157 | /** @brief Make sure the Interval can be updated while the timer is running. |
| 158 | */ |
| 159 | TEST_F(WdogTest, verifyIntervalUpdateRunning) |
| 160 | { |
| 161 | const auto oldInterval = milliseconds(wdog->interval()); |
| 162 | const auto newInterval = 5s; |
| 163 | |
| 164 | EXPECT_TRUE(wdog->enabled(true)); |
| 165 | auto remaining = milliseconds(wdog->timeRemaining()); |
| 166 | EXPECT_GE(oldInterval, remaining); |
| 167 | EXPECT_LE(oldInterval - defaultDrift, remaining); |
| 168 | EXPECT_EQ(newInterval, |
| 169 | milliseconds(wdog->interval(milliseconds(newInterval).count()))); |
| 170 | |
| 171 | // Expect only the interval to update |
| 172 | remaining = milliseconds(wdog->timeRemaining()); |
| 173 | EXPECT_GE(oldInterval, remaining); |
| 174 | EXPECT_LE(oldInterval - defaultDrift, remaining); |
| 175 | EXPECT_EQ(newInterval, milliseconds(wdog->interval())); |
| 176 | |
| 177 | // Expect reset to use the new interval |
| 178 | wdog->resetTimeRemaining(false); |
| 179 | remaining = milliseconds(wdog->timeRemaining()); |
| 180 | EXPECT_GE(newInterval, remaining); |
| 181 | EXPECT_LE(newInterval - defaultDrift, remaining); |
| 182 | } |
| 183 | |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 184 | /** @brief Make sure that watchdog is started and enabled. |
| 185 | * Wait default interval seconds and make sure that wdog has died |
| 186 | */ |
| 187 | TEST_F(WdogTest, enableWdogAndWaitTillEnd) |
| 188 | { |
| 189 | // Enable and then verify |
William A. Kennington III | f0fe2d6 | 2018-02-28 15:20:16 -0800 | [diff] [blame] | 190 | EXPECT_TRUE(wdog->enabled(true)); |
William A. Kennington III | 8cd3839 | 2018-03-01 11:07:36 -0800 | [diff] [blame] | 191 | auto expireTime = duration_cast<seconds>(defaultInterval); |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 192 | |
| 193 | // Waiting default expiration |
William A. Kennington III | 99c69de | 2018-03-01 10:59:22 -0800 | [diff] [blame] | 194 | EXPECT_EQ(expireTime - 1s, waitForWatchdog(expireTime)); |
| 195 | |
William A. Kennington III | 825f498 | 2018-02-27 19:10:56 -0800 | [diff] [blame] | 196 | EXPECT_FALSE(wdog->enabled()); |
William A. Kennington III | f0fe2d6 | 2018-02-28 15:20:16 -0800 | [diff] [blame] | 197 | EXPECT_EQ(0, wdog->timeRemaining()); |
| 198 | EXPECT_TRUE(wdog->timerExpired()); |
William A. Kennington III | 0650a3f | 2018-03-01 10:53:25 -0800 | [diff] [blame] | 199 | EXPECT_FALSE(wdog->timerEnabled()); |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 200 | } |
William A. Kennington III | d133108 | 2018-02-27 18:47:05 -0800 | [diff] [blame] | 201 | |
| 202 | /** @brief Make sure the watchdog is started and enabled with a fallback |
| 203 | * Wait through the initial trip and ensure the fallback is observed |
| 204 | * Make sure that fallback runs to completion and ensure the watchdog |
| 205 | * is disabled |
| 206 | */ |
| 207 | TEST_F(WdogTest, enableWdogWithFallbackTillEnd) |
| 208 | { |
| 209 | auto primaryInterval = 5s; |
| 210 | auto primaryIntervalMs = milliseconds(primaryInterval).count(); |
| 211 | auto fallbackInterval = primaryInterval * 2; |
| 212 | auto fallbackIntervalMs = milliseconds(fallbackInterval).count(); |
| 213 | |
| 214 | // We need to make a wdog with the right fallback options |
| 215 | // The interval is set to be noticeably different from the default |
| 216 | // so we can always tell the difference |
William A. Kennington III | 8cf5f64 | 2019-01-16 16:58:14 -0800 | [diff] [blame^] | 217 | Watchdog::Fallback fallback; |
| 218 | fallback.action = Watchdog::Action::PowerOff; |
| 219 | fallback.interval = static_cast<uint64_t>(fallbackIntervalMs); |
| 220 | fallback.always = false; |
William A. Kennington III | f505fc0 | 2018-09-12 18:30:09 -0700 | [diff] [blame] | 221 | wdog = std::make_unique<Watchdog>(bus, TEST_PATH, event, |
William A. Kennington III | 3bb2f40 | 2018-09-13 00:35:47 -0700 | [diff] [blame] | 222 | Watchdog::ActionTargetMap(), |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 223 | std::move(fallback)); |
William A. Kennington III | d133108 | 2018-02-27 18:47:05 -0800 | [diff] [blame] | 224 | EXPECT_EQ(primaryInterval, milliseconds(wdog->interval(primaryIntervalMs))); |
| 225 | EXPECT_FALSE(wdog->enabled()); |
| 226 | EXPECT_EQ(0, wdog->timeRemaining()); |
| 227 | |
| 228 | // Enable and then verify |
| 229 | EXPECT_TRUE(wdog->enabled(true)); |
| 230 | |
| 231 | // Waiting default expiration |
| 232 | EXPECT_EQ(primaryInterval - 1s, waitForWatchdog(primaryInterval)); |
| 233 | |
| 234 | // We should now have entered the fallback once the primary expires |
| 235 | EXPECT_FALSE(wdog->enabled()); |
| 236 | auto remaining = milliseconds(wdog->timeRemaining()); |
| 237 | EXPECT_GE(fallbackInterval, remaining); |
| 238 | EXPECT_LT(primaryInterval, remaining); |
| 239 | EXPECT_FALSE(wdog->timerExpired()); |
| 240 | EXPECT_TRUE(wdog->timerEnabled()); |
| 241 | |
| 242 | // We should still be ticking in fallback when setting action or interval |
| 243 | auto newInterval = primaryInterval - 1s; |
| 244 | auto newIntervalMs = milliseconds(newInterval).count(); |
| 245 | EXPECT_EQ(newInterval, milliseconds(wdog->interval(newIntervalMs))); |
| 246 | EXPECT_EQ(Watchdog::Action::None, |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 247 | wdog->expireAction(Watchdog::Action::None)); |
William A. Kennington III | d133108 | 2018-02-27 18:47:05 -0800 | [diff] [blame] | 248 | |
| 249 | EXPECT_FALSE(wdog->enabled()); |
| 250 | EXPECT_GE(remaining, milliseconds(wdog->timeRemaining())); |
| 251 | EXPECT_LT(primaryInterval, milliseconds(wdog->timeRemaining())); |
| 252 | EXPECT_FALSE(wdog->timerExpired()); |
| 253 | EXPECT_TRUE(wdog->timerEnabled()); |
| 254 | |
| 255 | // Test that setting the timeRemaining always resets the timer to the |
| 256 | // fallback interval |
| 257 | EXPECT_EQ(fallback.interval, wdog->timeRemaining(primaryInterval.count())); |
| 258 | EXPECT_FALSE(wdog->enabled()); |
| 259 | |
| 260 | remaining = milliseconds(wdog->timeRemaining()); |
| 261 | EXPECT_GE(fallbackInterval, remaining); |
| 262 | EXPECT_LE(fallbackInterval - defaultDrift, remaining); |
| 263 | EXPECT_FALSE(wdog->timerExpired()); |
| 264 | EXPECT_TRUE(wdog->timerEnabled()); |
| 265 | |
| 266 | // Waiting fallback expiration |
| 267 | EXPECT_EQ(fallbackInterval - 1s, waitForWatchdog(fallbackInterval)); |
| 268 | |
| 269 | // We should now have disabled the watchdog after the fallback expires |
| 270 | EXPECT_FALSE(wdog->enabled()); |
| 271 | EXPECT_EQ(0, wdog->timeRemaining()); |
| 272 | EXPECT_TRUE(wdog->timerExpired()); |
| 273 | EXPECT_FALSE(wdog->timerEnabled()); |
| 274 | |
| 275 | // Make sure enabling the watchdog again works |
| 276 | EXPECT_TRUE(wdog->enabled(true)); |
| 277 | |
| 278 | // We should have re-entered the primary |
| 279 | EXPECT_TRUE(wdog->enabled()); |
| 280 | EXPECT_GE(primaryInterval, milliseconds(wdog->timeRemaining())); |
| 281 | EXPECT_FALSE(wdog->timerExpired()); |
| 282 | EXPECT_TRUE(wdog->timerEnabled()); |
| 283 | } |
| 284 | |
| 285 | /** @brief Make sure the watchdog is started and enabled with a fallback |
| 286 | * Wait through the initial trip and ensure the fallback is observed |
| 287 | * Make sure that we can re-enable the watchdog during fallback |
| 288 | */ |
| 289 | TEST_F(WdogTest, enableWdogWithFallbackReEnable) |
| 290 | { |
| 291 | auto primaryInterval = 5s; |
| 292 | auto primaryIntervalMs = milliseconds(primaryInterval).count(); |
| 293 | auto fallbackInterval = primaryInterval * 2; |
| 294 | auto fallbackIntervalMs = milliseconds(fallbackInterval).count(); |
| 295 | |
| 296 | // We need to make a wdog with the right fallback options |
| 297 | // The interval is set to be noticeably different from the default |
| 298 | // so we can always tell the difference |
William A. Kennington III | 8cf5f64 | 2019-01-16 16:58:14 -0800 | [diff] [blame^] | 299 | Watchdog::Fallback fallback; |
| 300 | fallback.action = Watchdog::Action::PowerOff; |
| 301 | fallback.interval = static_cast<uint64_t>(fallbackIntervalMs); |
| 302 | fallback.always = false; |
William A. Kennington III | f505fc0 | 2018-09-12 18:30:09 -0700 | [diff] [blame] | 303 | wdog = std::make_unique<Watchdog>(bus, TEST_PATH, event, |
William A. Kennington III | 3bb2f40 | 2018-09-13 00:35:47 -0700 | [diff] [blame] | 304 | Watchdog::ActionTargetMap(), |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 305 | std::move(fallback)); |
William A. Kennington III | d133108 | 2018-02-27 18:47:05 -0800 | [diff] [blame] | 306 | EXPECT_EQ(primaryInterval, milliseconds(wdog->interval(primaryIntervalMs))); |
| 307 | EXPECT_FALSE(wdog->enabled()); |
| 308 | EXPECT_EQ(0, wdog->timeRemaining()); |
| 309 | EXPECT_FALSE(wdog->timerExpired()); |
| 310 | EXPECT_FALSE(wdog->timerEnabled()); |
| 311 | |
| 312 | // Enable and then verify |
| 313 | EXPECT_TRUE(wdog->enabled(true)); |
| 314 | |
| 315 | // Waiting default expiration |
| 316 | EXPECT_EQ(primaryInterval - 1s, waitForWatchdog(primaryInterval)); |
| 317 | |
| 318 | // We should now have entered the fallback once the primary expires |
| 319 | EXPECT_FALSE(wdog->enabled()); |
| 320 | auto remaining = milliseconds(wdog->timeRemaining()); |
| 321 | EXPECT_GE(fallbackInterval, remaining); |
| 322 | EXPECT_LT(primaryInterval, remaining); |
| 323 | EXPECT_FALSE(wdog->timerExpired()); |
| 324 | EXPECT_TRUE(wdog->timerEnabled()); |
| 325 | |
| 326 | EXPECT_TRUE(wdog->enabled(true)); |
| 327 | |
| 328 | // We should have re-entered the primary |
| 329 | EXPECT_TRUE(wdog->enabled()); |
| 330 | EXPECT_GE(primaryInterval, milliseconds(wdog->timeRemaining())); |
| 331 | EXPECT_FALSE(wdog->timerExpired()); |
| 332 | EXPECT_TRUE(wdog->timerEnabled()); |
| 333 | } |
William A. Kennington III | 2235219 | 2018-02-27 18:51:44 -0800 | [diff] [blame] | 334 | |
| 335 | /** @brief Make sure the watchdog is started and with a fallback without |
| 336 | * sending an enable |
| 337 | * Then enable the watchdog |
| 338 | * Wait through the initial trip and ensure the fallback is observed |
| 339 | * Make sure that fallback runs to completion and ensure the watchdog |
| 340 | * is in the fallback state again |
| 341 | */ |
| 342 | TEST_F(WdogTest, enableWdogWithFallbackAlways) |
| 343 | { |
| 344 | auto primaryInterval = 5s; |
| 345 | auto primaryIntervalMs = milliseconds(primaryInterval).count(); |
| 346 | auto fallbackInterval = primaryInterval * 2; |
| 347 | auto fallbackIntervalMs = milliseconds(fallbackInterval).count(); |
| 348 | |
| 349 | // We need to make a wdog with the right fallback options |
| 350 | // The interval is set to be noticeably different from the default |
| 351 | // so we can always tell the difference |
William A. Kennington III | 8cf5f64 | 2019-01-16 16:58:14 -0800 | [diff] [blame^] | 352 | Watchdog::Fallback fallback; |
| 353 | fallback.action = Watchdog::Action::PowerOff; |
| 354 | fallback.interval = static_cast<uint64_t>(fallbackIntervalMs); |
| 355 | fallback.always = true; |
William A. Kennington III | f505fc0 | 2018-09-12 18:30:09 -0700 | [diff] [blame] | 356 | wdog = std::make_unique<Watchdog>(bus, TEST_PATH, event, |
William A. Kennington III | 3bb2f40 | 2018-09-13 00:35:47 -0700 | [diff] [blame] | 357 | Watchdog::ActionTargetMap(), |
Patrick Venture | 8f6c515 | 2018-09-11 17:45:33 -0700 | [diff] [blame] | 358 | std::move(fallback)); |
William A. Kennington III | 2235219 | 2018-02-27 18:51:44 -0800 | [diff] [blame] | 359 | EXPECT_EQ(primaryInterval, milliseconds(wdog->interval(primaryIntervalMs))); |
| 360 | EXPECT_FALSE(wdog->enabled()); |
| 361 | auto remaining = milliseconds(wdog->timeRemaining()); |
| 362 | EXPECT_GE(fallbackInterval, remaining); |
| 363 | EXPECT_LT(primaryInterval, remaining); |
| 364 | EXPECT_FALSE(wdog->timerExpired()); |
| 365 | EXPECT_TRUE(wdog->timerEnabled()); |
| 366 | |
| 367 | // Enable and then verify |
| 368 | EXPECT_TRUE(wdog->enabled(true)); |
| 369 | EXPECT_GE(primaryInterval, milliseconds(wdog->timeRemaining())); |
| 370 | |
| 371 | // Waiting default expiration |
| 372 | EXPECT_EQ(primaryInterval - 1s, waitForWatchdog(primaryInterval)); |
| 373 | |
| 374 | // We should now have entered the fallback once the primary expires |
| 375 | EXPECT_FALSE(wdog->enabled()); |
| 376 | remaining = milliseconds(wdog->timeRemaining()); |
| 377 | EXPECT_GE(fallbackInterval, remaining); |
| 378 | EXPECT_LT(primaryInterval, remaining); |
| 379 | EXPECT_FALSE(wdog->timerExpired()); |
| 380 | EXPECT_TRUE(wdog->timerEnabled()); |
| 381 | |
| 382 | // Waiting fallback expiration |
| 383 | EXPECT_EQ(fallbackInterval - 1s, waitForWatchdog(fallbackInterval)); |
| 384 | |
| 385 | // We should now enter the fallback again |
| 386 | EXPECT_FALSE(wdog->enabled()); |
| 387 | remaining = milliseconds(wdog->timeRemaining()); |
| 388 | EXPECT_GE(fallbackInterval, remaining); |
| 389 | EXPECT_LT(primaryInterval, remaining); |
| 390 | EXPECT_FALSE(wdog->timerExpired()); |
| 391 | EXPECT_TRUE(wdog->timerEnabled()); |
| 392 | } |