xref: /openbmc/sdbusplus/test/timer/timer_expire.cpp (revision 7c0fb15f6bca1fb107d4c55006a5eed9bd82b325)
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  */
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