xref: /openbmc/sdbusplus/test/timer/timer_callback_not_done.cpp (revision 2cd25e64299ecc20e2258727e836a31b7ce6fad3)
1 #include "suite.hpp"
2 
3 #include <sdbusplus/timer.hpp>
4 
5 #include <chrono>
6 
7 #include <gtest/gtest.h>
8 
9 /** @brief Makes sure that timer is not expired
10  */
TEST_F(TimerTestCallBack,timerNotExpiredAfter2SecondsNoOptionalCallBack)11 TEST_F(TimerTestCallBack, timerNotExpiredAfter2SecondsNoOptionalCallBack)
12 {
13     using namespace std::chrono;
14 
15     auto time = duration_cast<microseconds>(seconds(2));
16     EXPECT_GE(timer->start(time), 0);
17 
18     // Now turn off the timer post a 1 second sleep
19     sleep(1);
20     EXPECT_GE(timer->stop(), 0);
21 
22     // Wait 2 seconds and see that timer is not expired
23     int count = 0;
24     while (count < 2)
25     {
26         // Returns -0- on timeout
27         auto sleepTime = duration_cast<microseconds>(seconds(1));
28         if (!sd_event_run(events, sleepTime.count()))
29         {
30             count++;
31         }
32     }
33     EXPECT_EQ(false, timer->isExpired());
34     EXPECT_EQ(false, callBackDone);
35 
36     // 2 because of one more count that happens prior to exiting
37     EXPECT_EQ(2, count);
38 }
39