blob: e9f323bd97fbba626297a06b4760aee7b0985cf9 [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;
24 while(count < expireTime.count() && !timer.expired())
25 {
26 // Returns -0- on timeout and positive number on dispatch
27 auto sleepTime = duration_cast<microseconds>(seconds(1));
28 if(!sd_event_run(eventP.get(), sleepTime.count()))
29 {
30 count++;
31 }
32 }
33 EXPECT_EQ(true, timer.expired());
34 EXPECT_EQ(expireTime.count() - 1, count);
35
36 // Make sure secondary callback was not called.
37 EXPECT_EQ(false, expired);
38}
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
49 phosphor::watchdog::Timer timer(eventP,
50 std::bind(&TimerTest::timeOutHandler, this));
51
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;
58 while(count < expireTime.count() && !timer.expired())
59 {
60 // Returns -0- on timeout and positive number on dispatch
61 auto sleepTime = duration_cast<microseconds>(seconds(1));
62 if(!sd_event_run(eventP.get(), sleepTime.count()))
63 {
64 count++;
65 }
66 }
67 EXPECT_EQ(true, timer.expired());
68 EXPECT_EQ(expireTime.count() - 1, count);
69
70 // This gets set as part of secondary callback
71 EXPECT_EQ(true, expired);
72}