xref: /openbmc/sdbusplus/test/timer/timer_expire.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 expired and the
10  *  callback handler gets invoked post 2 seconds
11  */
TEST_F(TimerTest,timerExpiresAfter2seconds)12 TEST_F(TimerTest, timerExpiresAfter2seconds)
13 {
14     using namespace std::chrono;
15 
16     auto time = duration_cast<microseconds>(seconds(2));
17     EXPECT_GE(timer.start(time), 0);
18 
19     // Waiting 2 seconds is enough here since we have
20     // already spent some usec now
21     int count = 0;
22     while (count < 2 && !timer.isExpired())
23     {
24         // Returns -0- on timeout and positive number on dispatch
25         auto sleepTime = duration_cast<microseconds>(seconds(1));
26         if (!sd_event_run(events, sleepTime.count()))
27         {
28             count++;
29         }
30     }
31     EXPECT_EQ(true, timer.isExpired());
32     EXPECT_EQ(1, count);
33 }
34