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