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()); |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | /** @brief Make sure that watchdog is started and enabled */ |
| 38 | TEST_F(WdogTest, createWdogAndEnable) |
| 39 | { |
| 40 | // Enable and then verify |
William A. Kennington III | f0fe2d6 | 2018-02-28 15:20:16 -0800 | [diff] [blame] | 41 | EXPECT_TRUE(wdog->enabled(true)); |
| 42 | EXPECT_FALSE(wdog->timerExpired()); |
William A. Kennington III | 0650a3f | 2018-03-01 10:53:25 -0800 | [diff] [blame] | 43 | EXPECT_TRUE(wdog->timerEnabled()); |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 44 | |
| 45 | // Get the configured interval |
William A. Kennington III | f0fe2d6 | 2018-02-28 15:20:16 -0800 | [diff] [blame] | 46 | auto remaining = milliseconds(wdog->timeRemaining()); |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 47 | |
| 48 | // Its possible that we are off by few msecs depending on |
| 49 | // how we get scheduled. So checking a range here. |
| 50 | EXPECT_TRUE((remaining >= defaultInterval - defaultDrift) && |
| 51 | (remaining <= defaultInterval)); |
| 52 | |
William A. Kennington III | f0fe2d6 | 2018-02-28 15:20:16 -0800 | [diff] [blame] | 53 | EXPECT_FALSE(wdog->timerExpired()); |
William A. Kennington III | 0650a3f | 2018-03-01 10:53:25 -0800 | [diff] [blame] | 54 | EXPECT_TRUE(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 | * Later, disable watchdog |
| 59 | */ |
| 60 | TEST_F(WdogTest, createWdogAndEnableThenDisable) |
| 61 | { |
| 62 | // Enable and then verify |
William A. Kennington III | f0fe2d6 | 2018-02-28 15:20:16 -0800 | [diff] [blame] | 63 | EXPECT_TRUE(wdog->enabled(true)); |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 64 | |
| 65 | // Disable and then verify |
William A. Kennington III | f0fe2d6 | 2018-02-28 15:20:16 -0800 | [diff] [blame] | 66 | EXPECT_FALSE(wdog->enabled(false)); |
| 67 | EXPECT_FALSE(wdog->enabled()); |
| 68 | EXPECT_EQ(0, wdog->timeRemaining()); |
William A. Kennington III | 0650a3f | 2018-03-01 10:53:25 -0800 | [diff] [blame] | 69 | EXPECT_FALSE(wdog->timerExpired()); |
| 70 | EXPECT_FALSE(wdog->timerEnabled()); |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | /** @brief Make sure that watchdog is started and enabled. |
| 74 | * Wait for 5 seconds and make sure that the remaining |
| 75 | * time shows 25 seconds. |
| 76 | */ |
| 77 | TEST_F(WdogTest, enableWdogAndWait5Seconds) |
| 78 | { |
| 79 | // Enable and then verify |
William A. Kennington III | f0fe2d6 | 2018-02-28 15:20:16 -0800 | [diff] [blame] | 80 | EXPECT_TRUE(wdog->enabled(true)); |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 81 | |
| 82 | // Sleep for 5 seconds |
| 83 | auto sleepTime = seconds(5s); |
| 84 | std::this_thread::sleep_for(sleepTime); |
| 85 | |
| 86 | // 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] | 87 | auto remaining = milliseconds(wdog->timeRemaining()); |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 88 | auto expected = defaultInterval - |
| 89 | duration_cast<milliseconds>(sleepTime); |
| 90 | |
| 91 | // Its possible that we are off by few msecs depending on |
| 92 | // how we get scheduled. So checking a range here. |
| 93 | EXPECT_TRUE((remaining >= expected - defaultDrift) && |
| 94 | (remaining <= expected)); |
William A. Kennington III | f0fe2d6 | 2018-02-28 15:20:16 -0800 | [diff] [blame] | 95 | EXPECT_FALSE(wdog->timerExpired()); |
William A. Kennington III | 0650a3f | 2018-03-01 10:53:25 -0800 | [diff] [blame] | 96 | EXPECT_TRUE(wdog->timerEnabled()); |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | /** @brief Make sure that watchdog is started and enabled. |
| 100 | * Wait 1 second and then reset the timer to 5 seconds |
| 101 | * and then expect the watchdog to expire in 5 seconds |
| 102 | */ |
| 103 | TEST_F(WdogTest, enableWdogAndResetTo5Seconds) |
| 104 | { |
| 105 | // Enable and then verify |
William A. Kennington III | f0fe2d6 | 2018-02-28 15:20:16 -0800 | [diff] [blame] | 106 | EXPECT_TRUE(wdog->enabled(true)); |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 107 | |
| 108 | // Sleep for 1 second |
| 109 | std::this_thread::sleep_for(1s); |
| 110 | |
| 111 | // Next timer will expire in 5 seconds from now. |
| 112 | auto expireTime = seconds(5s); |
| 113 | auto newTime = duration_cast<milliseconds>(expireTime); |
William A. Kennington III | f0fe2d6 | 2018-02-28 15:20:16 -0800 | [diff] [blame] | 114 | wdog->timeRemaining(newTime.count()); |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 115 | |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 116 | // Waiting for expiration |
William A. Kennington III | 99c69de | 2018-03-01 10:59:22 -0800 | [diff] [blame^] | 117 | EXPECT_EQ(expireTime - 1s, waitForWatchdog(expireTime)); |
William A. Kennington III | f0fe2d6 | 2018-02-28 15:20:16 -0800 | [diff] [blame] | 118 | EXPECT_TRUE(wdog->timerExpired()); |
William A. Kennington III | 0650a3f | 2018-03-01 10:53:25 -0800 | [diff] [blame] | 119 | EXPECT_FALSE(wdog->timerEnabled()); |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 120 | |
| 121 | // Make sure secondary callback was not called. |
William A. Kennington III | d5d1483 | 2018-02-28 09:54:08 -0800 | [diff] [blame] | 122 | EXPECT_FALSE(expired); |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 123 | } |
| 124 | |
Gunnar Mills | bfe5cb8 | 2017-10-25 20:48:50 -0500 | [diff] [blame] | 125 | /** @brief Make sure the Interval can be updated directly. |
Patrick Venture | 9681634 | 2017-08-17 12:32:22 -0700 | [diff] [blame] | 126 | */ |
| 127 | TEST_F(WdogTest, verifyIntervalUpdateReceived) |
| 128 | { |
| 129 | auto expireTime = seconds(5s); |
| 130 | auto newTime = duration_cast<milliseconds>(expireTime); |
William A. Kennington III | f0fe2d6 | 2018-02-28 15:20:16 -0800 | [diff] [blame] | 131 | wdog->interval(newTime.count()); |
Patrick Venture | 9681634 | 2017-08-17 12:32:22 -0700 | [diff] [blame] | 132 | |
| 133 | // Expect an update in the Interval |
William A. Kennington III | f0fe2d6 | 2018-02-28 15:20:16 -0800 | [diff] [blame] | 134 | EXPECT_EQ(newTime.count(), wdog->interval()); |
Patrick Venture | 9681634 | 2017-08-17 12:32:22 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 137 | /** @brief Make sure that watchdog is started and enabled. |
| 138 | * Wait default interval seconds and make sure that wdog has died |
| 139 | */ |
| 140 | TEST_F(WdogTest, enableWdogAndWaitTillEnd) |
| 141 | { |
| 142 | // Enable and then verify |
William A. Kennington III | f0fe2d6 | 2018-02-28 15:20:16 -0800 | [diff] [blame] | 143 | EXPECT_TRUE(wdog->enabled(true)); |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 144 | auto expireTime = duration_cast<seconds>( |
| 145 | milliseconds(defaultInterval)); |
| 146 | |
| 147 | // Waiting default expiration |
William A. Kennington III | 99c69de | 2018-03-01 10:59:22 -0800 | [diff] [blame^] | 148 | EXPECT_EQ(expireTime - 1s, waitForWatchdog(expireTime)); |
| 149 | |
William A. Kennington III | f0fe2d6 | 2018-02-28 15:20:16 -0800 | [diff] [blame] | 150 | EXPECT_TRUE(wdog->enabled()); |
| 151 | EXPECT_EQ(0, wdog->timeRemaining()); |
| 152 | EXPECT_TRUE(wdog->timerExpired()); |
William A. Kennington III | 0650a3f | 2018-03-01 10:53:25 -0800 | [diff] [blame] | 153 | EXPECT_FALSE(wdog->timerEnabled()); |
Vishwanatha Subbanna | 00bd377 | 2017-05-31 14:53:42 +0530 | [diff] [blame] | 154 | } |