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