xref: /openbmc/sdbusplus/test/timer/timer_update_not_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 value is changed in between
10  *  and turn off and make sure that timer does not expire
11  */
TEST_F(TimerTest,updateTimerAndNeverExpire)12 TEST_F(TimerTest, updateTimerAndNeverExpire)
13 {
14     using namespace std::chrono;
15 
16     auto time = duration_cast<microseconds>(seconds(2));
17     EXPECT_GE(timer.start(time), 0);
18 
19     // Now sleep for a second and then set the new timeout value
20     sleep(1);
21 
22     // New timeout is 2 seconds from THIS point.
23     time = duration_cast<microseconds>(seconds(2));
24     EXPECT_GE(timer.start(time), 0);
25 
26     // Now turn off the timer post a 1 second sleep
27     sleep(1);
28     EXPECT_GE(timer.stop(), 0);
29 
30     // Wait 2 seconds and see that timer is expired
31     int count = 0;
32     while (count < 2)
33     {
34         // Returns -0- on timeout
35         auto sleepTime = duration_cast<microseconds>(seconds(1));
36         if (!sd_event_run(events, sleepTime.count()))
37         {
38             count++;
39         }
40     }
41     EXPECT_EQ(false, timer.isExpired());
42 
43     // 2 because of one more count that happens prior to exiting
44     EXPECT_EQ(2, count);
45 }
46