diff options
author | William A. Kennington III <wak@google.com> | 2018-03-01 10:59:22 -0800 |
---|---|---|
committer | Brad Bishop <bradleyb@fuzziesquirrel.com> | 2018-03-07 11:22:42 +0000 |
commit | 99c69de1a6682fab61cc898d54dad438134468d4 (patch) | |
tree | f68559a03423610b9657aeea05e3e512938d2a61 | |
parent | 0650a3f09da8899a742988ff55d02eebd6e796aa (diff) | |
download | phosphor-watchdog-99c69de1a6682fab61cc898d54dad438134468d4.tar.gz phosphor-watchdog-99c69de1a6682fab61cc898d54dad438134468d4.zip |
tests/watchdog: Add a helper for counting down with the watchdog
This reduces a bunch of duplicate code that loops through an event loop
to try and guage how long it took for the watchdog to expire.
Change-Id: Ib3b33e250b157df02eff39751277c564ea40705c
Signed-off-by: William A. Kennington III <wak@google.com>
-rw-r--r-- | test/watchdog_test.cpp | 50 | ||||
-rw-r--r-- | test/watchdog_test.hpp | 4 |
2 files changed, 31 insertions, 23 deletions
diff --git a/test/watchdog_test.cpp b/test/watchdog_test.cpp index cb22a83..72347aa 100644 --- a/test/watchdog_test.cpp +++ b/test/watchdog_test.cpp @@ -1,7 +1,30 @@ -#include <watchdog_test.hpp> +#include <chrono> + +#include "watchdog_test.hpp" using namespace phosphor::watchdog; +seconds WdogTest::waitForWatchdog(seconds timeLimit) +{ + auto previousTimeRemaining = wdog->timeRemaining(); + auto ret = 0s; + while (ret < timeLimit && + previousTimeRemaining >= wdog->timeRemaining() && + wdog->timerEnabled()) + { + previousTimeRemaining = wdog->timeRemaining(); + + // Returns -0- on timeout and positive number on dispatch + auto sleepTime = 1s; + if(!sd_event_run(eventP.get(), microseconds(sleepTime).count())) + { + ret += sleepTime; + } + } + + return ret; +} + /** @brief Make sure that watchdog is started and not enabled */ TEST_F(WdogTest, createWdogAndDontEnable) { @@ -91,19 +114,9 @@ TEST_F(WdogTest, enableWdogAndResetTo5Seconds) wdog->timeRemaining(newTime.count()); // Waiting for expiration - int count = 0; - while(count < expireTime.count() && !wdog->timerExpired()) - { - // Returns -0- on timeout and positive number on dispatch - auto sleepTime = duration_cast<microseconds>(seconds(1s)); - if(!sd_event_run(eventP.get(), sleepTime.count())) - { - count++; - } - } + EXPECT_EQ(expireTime - 1s, waitForWatchdog(expireTime)); EXPECT_TRUE(wdog->timerExpired()); EXPECT_FALSE(wdog->timerEnabled()); - EXPECT_EQ(expireTime.count() - 1, count); // Make sure secondary callback was not called. EXPECT_FALSE(expired); @@ -132,19 +145,10 @@ TEST_F(WdogTest, enableWdogAndWaitTillEnd) milliseconds(defaultInterval)); // Waiting default expiration - int count = 0; - while(count < expireTime.count() && !wdog->timerExpired()) - { - // Returns -0- on timeout and positive number on dispatch - auto sleepTime = duration_cast<microseconds>(seconds(1s)); - if(!sd_event_run(eventP.get(), sleepTime.count())) - { - count++; - } - } + EXPECT_EQ(expireTime - 1s, waitForWatchdog(expireTime)); + EXPECT_TRUE(wdog->enabled()); EXPECT_EQ(0, wdog->timeRemaining()); EXPECT_TRUE(wdog->timerExpired()); EXPECT_FALSE(wdog->timerEnabled()); - EXPECT_EQ(expireTime.count() - 1, count); } diff --git a/test/watchdog_test.hpp b/test/watchdog_test.hpp index 5b5348a..87c64e9 100644 --- a/test/watchdog_test.hpp +++ b/test/watchdog_test.hpp @@ -45,4 +45,8 @@ class WdogTest : public TimerTest // This is just to satisfy the constructor. Does not have // a need to check if the objects paths have been created. static constexpr auto TEST_PATH = "/test/path"; + + // Returns how long it took for the current watchdog timer to be + // disabled or have its timeRemaining reset. + seconds waitForWatchdog(seconds timeLimit); }; |