blob: ea002287e11d7aacc505631037c61cea94ee73ad [file] [log] [blame]
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +05301#include <chrono>
2#include <timer_test.hpp>
3
4using namespace std::chrono;
5using namespace std::chrono_literals;
6
7/** @brief Starts the timer and expects it to
8 * expire in configured time and expects the
9 * deault callback handler to kick-in
10 */
11TEST_F(TimerTest, testTimerForExpirationDefaultTimeoutHandler)
12{
13 // Expect timer to expire in 2 seconds
14 auto expireTime = seconds(2s);
15
16 phosphor::watchdog::Timer timer(eventP);
17
18 // Set the expiration and enable the timer
19 timer.start(duration_cast<milliseconds>(expireTime));
20 timer.setEnabled<std::true_type>();
21
22 // Waiting 2 seconds to expect expiration
23 int count = 0;
Patrick Venture8f6c5152018-09-11 17:45:33 -070024 while (count < expireTime.count() && !timer.expired())
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +053025 {
26 // Returns -0- on timeout and positive number on dispatch
27 auto sleepTime = duration_cast<microseconds>(seconds(1));
Patrick Venture8f6c5152018-09-11 17:45:33 -070028 if (!sd_event_run(eventP.get(), sleepTime.count()))
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +053029 {
30 count++;
31 }
32 }
William A. Kennington IIId5d14832018-02-28 09:54:08 -080033 EXPECT_TRUE(timer.expired());
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +053034 EXPECT_EQ(expireTime.count() - 1, count);
35
36 // Make sure secondary callback was not called.
William A. Kennington IIId5d14832018-02-28 09:54:08 -080037 EXPECT_FALSE(expired);
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +053038}
39
40/** @brief Starts the timer and expects it to expire
41 * in configured time and expects the secondary
42 * callback to be called into along with default.
43 */
44TEST_F(TimerTest, testTimerForExpirationSecondCallBack)
45{
46 // Expect timer to expire in 2 seconds
47 auto expireTime = seconds(2s);
48
Patrick Venture8f6c5152018-09-11 17:45:33 -070049 phosphor::watchdog::Timer timer(
50 eventP, std::bind(&TimerTest::timeOutHandler, this));
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +053051
52 // Set the expiration and enable the timer
53 timer.start(duration_cast<milliseconds>(expireTime));
54 timer.setEnabled<std::true_type>();
55
56 // Waiting 2 seconds to expect expiration
57 int count = 0;
Patrick Venture8f6c5152018-09-11 17:45:33 -070058 while (count < expireTime.count() && !timer.expired())
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +053059 {
60 // Returns -0- on timeout and positive number on dispatch
61 auto sleepTime = duration_cast<microseconds>(seconds(1));
Patrick Venture8f6c5152018-09-11 17:45:33 -070062 if (!sd_event_run(eventP.get(), sleepTime.count()))
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +053063 {
64 count++;
65 }
66 }
William A. Kennington IIId5d14832018-02-28 09:54:08 -080067 EXPECT_TRUE(timer.expired());
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +053068 EXPECT_EQ(expireTime.count() - 1, count);
69
70 // This gets set as part of secondary callback
William A. Kennington IIId5d14832018-02-28 09:54:08 -080071 EXPECT_TRUE(expired);
Vishwanatha Subbanna00bd3772017-05-31 14:53:42 +053072}