blob: b557e6231609a8ef6a2799cf4cbaa8daa5c6d7e5 [file] [log] [blame]
William A. Kennington IIIaf60e632019-01-16 15:00:18 -08001#include "watchdog.hpp"
Patrick Venture8f6c5152018-09-11 17:45:33 -07002
William A. Kennington IIId1331082018-02-27 18:47:05 -08003#include <memory>
William A. Kennington IIIf505fc02018-09-12 18:30:09 -07004#include <thread>
William A. Kennington IIId1331082018-02-27 18:47:05 -08005#include <utility>
William A. Kennington III99c69de2018-03-01 10:59:22 -08006
Ofer Yehiellic35135d2019-06-14 11:30:25 -07007namespace phosphor
8{
9namespace watchdog
10{
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +053011
William A. Kennington III3964f002019-01-16 15:43:43 -080012WdogTest::Quantum WdogTest::waitForWatchdog(Quantum timeLimit)
William A. Kennington III99c69de2018-03-01 10:59:22 -080013{
14 auto previousTimeRemaining = wdog->timeRemaining();
William A. Kennington III3964f002019-01-16 15:43:43 -080015 auto ret = Quantum(0);
Patrick Venture8f6c5152018-09-11 17:45:33 -070016 while (ret < timeLimit && previousTimeRemaining >= wdog->timeRemaining() &&
William A. Kennington III99c69de2018-03-01 10:59:22 -080017 wdog->timerEnabled())
18 {
19 previousTimeRemaining = wdog->timeRemaining();
20
William A. Kennington III3964f002019-01-16 15:43:43 -080021 constexpr auto sleepTime = Quantum(1);
William A. Kennington IIIf505fc02018-09-12 18:30:09 -070022 if (event.run(sleepTime) == 0)
William A. Kennington III99c69de2018-03-01 10:59:22 -080023 {
24 ret += sleepTime;
25 }
26 }
27
28 return ret;
29}
30
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +053031/** @brief Make sure that watchdog is started and not enabled */
32TEST_F(WdogTest, createWdogAndDontEnable)
33{
William A. Kennington IIIf0fe2d62018-02-28 15:20:16 -080034 EXPECT_FALSE(wdog->enabled());
35 EXPECT_EQ(0, wdog->timeRemaining());
36 EXPECT_FALSE(wdog->timerExpired());
William A. Kennington III0650a3f2018-03-01 10:53:25 -080037 EXPECT_FALSE(wdog->timerEnabled());
William A. Kennington III25c1b202018-03-01 11:00:19 -080038
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 Subbanna00bd3772017-05-31 14:53:42 +053058}
59
60/** @brief Make sure that watchdog is started and enabled */
61TEST_F(WdogTest, createWdogAndEnable)
62{
63 // Enable and then verify
William A. Kennington IIIf0fe2d62018-02-28 15:20:16 -080064 EXPECT_TRUE(wdog->enabled(true));
65 EXPECT_FALSE(wdog->timerExpired());
William A. Kennington III0650a3f2018-03-01 10:53:25 -080066 EXPECT_TRUE(wdog->timerEnabled());
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +053067
68 // Get the configured interval
William A. Kennington IIIf0fe2d62018-02-28 15:20:16 -080069 auto remaining = milliseconds(wdog->timeRemaining());
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +053070
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 III3964f002019-01-16 15:43:43 -080073 EXPECT_TRUE((remaining >= defaultInterval - Quantum(1)) &&
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +053074 (remaining <= defaultInterval));
75
William A. Kennington IIIf0fe2d62018-02-28 15:20:16 -080076 EXPECT_FALSE(wdog->timerExpired());
William A. Kennington III0650a3f2018-03-01 10:53:25 -080077 EXPECT_TRUE(wdog->timerEnabled());
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +053078}
79
80/** @brief Make sure that watchdog is started and enabled.
81 * Later, disable watchdog
82 */
83TEST_F(WdogTest, createWdogAndEnableThenDisable)
84{
85 // Enable and then verify
William A. Kennington IIIf0fe2d62018-02-28 15:20:16 -080086 EXPECT_TRUE(wdog->enabled(true));
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +053087
88 // Disable and then verify
William A. Kennington IIIf0fe2d62018-02-28 15:20:16 -080089 EXPECT_FALSE(wdog->enabled(false));
90 EXPECT_FALSE(wdog->enabled());
91 EXPECT_EQ(0, wdog->timeRemaining());
William A. Kennington III0650a3f2018-03-01 10:53:25 -080092 EXPECT_FALSE(wdog->timerExpired());
93 EXPECT_FALSE(wdog->timerEnabled());
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +053094}
95
96/** @brief Make sure that watchdog is started and enabled.
William A. Kennington III3964f002019-01-16 15:43:43 -080097 * Wait for 5 quantums and make sure that the remaining
98 * time shows 5 fewer quantums.
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +053099 */
William A. Kennington III3964f002019-01-16 15:43:43 -0800100TEST_F(WdogTest, enableWdogAndWait5Quantums)
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +0530101{
102 // Enable and then verify
William A. Kennington IIIf0fe2d62018-02-28 15:20:16 -0800103 EXPECT_TRUE(wdog->enabled(true));
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +0530104
William A. Kennington III3964f002019-01-16 15:43:43 -0800105 // Sleep for 5 quantums
106 auto sleepTime = Quantum(2);
107 ASSERT_LT(sleepTime, defaultInterval);
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +0530108 std::this_thread::sleep_for(sleepTime);
109
William A. Kennington III3964f002019-01-16 15:43:43 -0800110 // Get the remaining time again and expectation is that we get fewer
William A. Kennington IIIf0fe2d62018-02-28 15:20:16 -0800111 auto remaining = milliseconds(wdog->timeRemaining());
William A. Kennington III8cd38392018-03-01 11:07:36 -0800112 auto expected = defaultInterval - sleepTime;
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +0530113
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 III3964f002019-01-16 15:43:43 -0800116 EXPECT_TRUE((remaining >= expected - Quantum(1)) &&
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +0530117 (remaining <= expected));
William A. Kennington IIIf0fe2d62018-02-28 15:20:16 -0800118 EXPECT_FALSE(wdog->timerExpired());
William A. Kennington III0650a3f2018-03-01 10:53:25 -0800119 EXPECT_TRUE(wdog->timerEnabled());
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +0530120}
121
122/** @brief Make sure that watchdog is started and enabled.
William A. Kennington III3964f002019-01-16 15:43:43 -0800123 * Wait 1 quantum and then reset the timer to 5 quantums
124 * and then expect the watchdog to expire in 5 quantums
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +0530125 */
William A. Kennington III3964f002019-01-16 15:43:43 -0800126TEST_F(WdogTest, enableWdogAndResetTo5Quantums)
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +0530127{
128 // Enable and then verify
William A. Kennington IIIf0fe2d62018-02-28 15:20:16 -0800129 EXPECT_TRUE(wdog->enabled(true));
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +0530130
131 // Sleep for 1 second
William A. Kennington III3964f002019-01-16 15:43:43 -0800132 std::this_thread::sleep_for(Quantum(1));
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +0530133
William A. Kennington IIIf505fc02018-09-12 18:30:09 -0700134 // Timer should still be running unexpired
135 EXPECT_FALSE(wdog->timerExpired());
136 EXPECT_TRUE(wdog->timerEnabled());
137
William A. Kennington III3964f002019-01-16 15:43:43 -0800138 // Next timer will expire in 5 quantums from now.
139 auto expireTime = Quantum(5);
William A. Kennington III8cd38392018-03-01 11:07:36 -0800140 auto expireTimeMs = milliseconds(expireTime).count();
141 EXPECT_EQ(expireTimeMs, wdog->timeRemaining(expireTimeMs));
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +0530142
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +0530143 // Waiting for expiration
William A. Kennington III3964f002019-01-16 15:43:43 -0800144 EXPECT_EQ(expireTime - Quantum(1), waitForWatchdog(expireTime));
William A. Kennington IIIf0fe2d62018-02-28 15:20:16 -0800145 EXPECT_TRUE(wdog->timerExpired());
William A. Kennington III0650a3f2018-03-01 10:53:25 -0800146 EXPECT_FALSE(wdog->timerEnabled());
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +0530147}
148
Gunnar Millsbfe5cb82017-10-25 20:48:50 -0500149/** @brief Make sure the Interval can be updated directly.
Patrick Venture96816342017-08-17 12:32:22 -0700150 */
151TEST_F(WdogTest, verifyIntervalUpdateReceived)
152{
William A. Kennington III3964f002019-01-16 15:43:43 -0800153 auto expireTime = Quantum(5);
William A. Kennington III8cd38392018-03-01 11:07:36 -0800154 auto expireTimeMs = milliseconds(expireTime).count();
155 EXPECT_EQ(expireTimeMs, wdog->interval(expireTimeMs));
Patrick Venture96816342017-08-17 12:32:22 -0700156
157 // Expect an update in the Interval
William A. Kennington III8cd38392018-03-01 11:07:36 -0800158 EXPECT_EQ(expireTimeMs, wdog->interval());
Patrick Venture96816342017-08-17 12:32:22 -0700159}
160
William A. Kennington IIId4cbc5a2018-09-24 14:23:05 -0700161/** @brief Make sure the Interval can be updated while the timer is running.
162 */
163TEST_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 III3964f002019-01-16 15:43:43 -0800171 EXPECT_LE(oldInterval - Quantum(1), remaining);
William A. Kennington IIId4cbc5a2018-09-24 14:23:05 -0700172 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 III3964f002019-01-16 15:43:43 -0800178 EXPECT_LE(oldInterval - Quantum(1), remaining);
William A. Kennington IIId4cbc5a2018-09-24 14:23:05 -0700179 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 III3964f002019-01-16 15:43:43 -0800185 EXPECT_LE(newInterval - Quantum(1), remaining);
William A. Kennington IIId4cbc5a2018-09-24 14:23:05 -0700186}
187
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +0530188/** @brief Make sure that watchdog is started and enabled.
William A. Kennington III3964f002019-01-16 15:43:43 -0800189 * Wait default interval quantums and make sure that wdog has died
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +0530190 */
191TEST_F(WdogTest, enableWdogAndWaitTillEnd)
192{
193 // Enable and then verify
William A. Kennington IIIf0fe2d62018-02-28 15:20:16 -0800194 EXPECT_TRUE(wdog->enabled(true));
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +0530195
196 // Waiting default expiration
William A. Kennington III3964f002019-01-16 15:43:43 -0800197 EXPECT_EQ(defaultInterval - Quantum(1), waitForWatchdog(defaultInterval));
William A. Kennington III99c69de2018-03-01 10:59:22 -0800198
William A. Kennington III825f4982018-02-27 19:10:56 -0800199 EXPECT_FALSE(wdog->enabled());
William A. Kennington IIIf0fe2d62018-02-28 15:20:16 -0800200 EXPECT_EQ(0, wdog->timeRemaining());
201 EXPECT_TRUE(wdog->timerExpired());
William A. Kennington III0650a3f2018-03-01 10:53:25 -0800202 EXPECT_FALSE(wdog->timerEnabled());
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +0530203}
William A. Kennington IIId1331082018-02-27 18:47:05 -0800204
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 */
210TEST_F(WdogTest, enableWdogWithFallbackTillEnd)
211{
William A. Kennington III3964f002019-01-16 15:43:43 -0800212 auto primaryInterval = Quantum(5);
William A. Kennington IIId1331082018-02-27 18:47:05 -0800213 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 III8cf5f642019-01-16 16:58:14 -0800220 Watchdog::Fallback fallback;
221 fallback.action = Watchdog::Action::PowerOff;
222 fallback.interval = static_cast<uint64_t>(fallbackIntervalMs);
223 fallback.always = false;
William A. Kennington IIIf505fc02018-09-12 18:30:09 -0700224 wdog = std::make_unique<Watchdog>(bus, TEST_PATH, event,
William A. Kennington III3bb2f402018-09-13 00:35:47 -0700225 Watchdog::ActionTargetMap(),
Patrick Venture8f6c5152018-09-11 17:45:33 -0700226 std::move(fallback));
William A. Kennington IIId1331082018-02-27 18:47:05 -0800227 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 III3964f002019-01-16 15:43:43 -0800235 EXPECT_EQ(primaryInterval - Quantum(1), waitForWatchdog(primaryInterval));
William A. Kennington IIId1331082018-02-27 18:47:05 -0800236
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 III3964f002019-01-16 15:43:43 -0800246 auto newInterval = primaryInterval - Quantum(1);
William A. Kennington IIId1331082018-02-27 18:47:05 -0800247 auto newIntervalMs = milliseconds(newInterval).count();
248 EXPECT_EQ(newInterval, milliseconds(wdog->interval(newIntervalMs)));
249 EXPECT_EQ(Watchdog::Action::None,
Patrick Venture8f6c5152018-09-11 17:45:33 -0700250 wdog->expireAction(Watchdog::Action::None));
William A. Kennington IIId1331082018-02-27 18:47:05 -0800251
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 III3964f002019-01-16 15:43:43 -0800265 EXPECT_LE(fallbackInterval - Quantum(1), remaining);
William A. Kennington IIId1331082018-02-27 18:47:05 -0800266 EXPECT_FALSE(wdog->timerExpired());
267 EXPECT_TRUE(wdog->timerEnabled());
268
269 // Waiting fallback expiration
William A. Kennington III3964f002019-01-16 15:43:43 -0800270 EXPECT_EQ(fallbackInterval - Quantum(1), waitForWatchdog(fallbackInterval));
William A. Kennington IIId1331082018-02-27 18:47:05 -0800271
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 */
292TEST_F(WdogTest, enableWdogWithFallbackReEnable)
293{
William A. Kennington III3964f002019-01-16 15:43:43 -0800294 auto primaryInterval = Quantum(5);
William A. Kennington IIId1331082018-02-27 18:47:05 -0800295 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 III8cf5f642019-01-16 16:58:14 -0800302 Watchdog::Fallback fallback;
303 fallback.action = Watchdog::Action::PowerOff;
304 fallback.interval = static_cast<uint64_t>(fallbackIntervalMs);
305 fallback.always = false;
William A. Kennington IIIf505fc02018-09-12 18:30:09 -0700306 wdog = std::make_unique<Watchdog>(bus, TEST_PATH, event,
William A. Kennington III3bb2f402018-09-13 00:35:47 -0700307 Watchdog::ActionTargetMap(),
Patrick Venture8f6c5152018-09-11 17:45:33 -0700308 std::move(fallback));
William A. Kennington IIId1331082018-02-27 18:47:05 -0800309 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 III3964f002019-01-16 15:43:43 -0800319 EXPECT_EQ(primaryInterval - Quantum(1), waitForWatchdog(primaryInterval));
William A. Kennington IIId1331082018-02-27 18:47:05 -0800320
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 III22352192018-02-27 18:51:44 -0800337
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 */
345TEST_F(WdogTest, enableWdogWithFallbackAlways)
346{
William A. Kennington III3964f002019-01-16 15:43:43 -0800347 auto primaryInterval = Quantum(5);
William A. Kennington III22352192018-02-27 18:51:44 -0800348 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 III8cf5f642019-01-16 16:58:14 -0800355 Watchdog::Fallback fallback;
356 fallback.action = Watchdog::Action::PowerOff;
357 fallback.interval = static_cast<uint64_t>(fallbackIntervalMs);
358 fallback.always = true;
William A. Kennington IIIf505fc02018-09-12 18:30:09 -0700359 wdog = std::make_unique<Watchdog>(bus, TEST_PATH, event,
Ofer Yehiellic35135d2019-06-14 11:30:25 -0700360 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 III22352192018-02-27 18:51:44 -0800367 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 III3964f002019-01-16 15:43:43 -0800380 EXPECT_EQ(primaryInterval - Quantum(1), waitForWatchdog(primaryInterval));
William A. Kennington III22352192018-02-27 18:51:44 -0800381
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 III3964f002019-01-16 15:43:43 -0800391 EXPECT_EQ(fallbackInterval - Quantum(1), waitForWatchdog(fallbackInterval));
William A. Kennington III22352192018-02-27 18:51:44 -0800392
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 Yehiellic35135d2019-06-14 11:30:25 -0700401
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 */
406TEST_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 */
427TEST_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