xref: /openbmc/sdbusplus/test/timer/suite.hpp (revision 2cd25e64299ecc20e2258727e836a31b7ce6fad3)
1 #pragma once
2 
3 #include <sdbusplus/timer.hpp>
4 
5 #include <gtest/gtest.h>
6 
7 using sdbusplus::Timer;
8 
9 class TimerTest : public ::testing::Test
10 {
11   public:
12     // systemd event handler
13     sd_event* events = nullptr;
14 
15     // Need this so that events can be initialized.
16     int rc;
17 
18     // Source of event
19     sd_event_source* eventSource = nullptr;
20 
21     // Add a Timer Object
22     Timer timer;
23 
24     // Gets called as part of each TEST_F construction
TimerTest()25     TimerTest() : rc(sd_event_default(&events)), timer(events)
26     {
27         // Check for successful creation of
28         // event handler and timer object.
29         EXPECT_GE(rc, 0);
30     }
31 
32     // Gets called as part of each TEST_F destruction
~TimerTest()33     ~TimerTest() override
34     {
35         events = sd_event_unref(events);
36     }
37 };
38 
39 class TimerTestCallBack : public ::testing::Test
40 {
41   public:
42     // systemd event handler
43     sd_event* events;
44 
45     // Need this so that events can be initialized.
46     int rc;
47 
48     // Source of event
49     sd_event_source* eventSource = nullptr;
50 
51     // Add a Timer Object
52     std::unique_ptr<Timer> timer = nullptr;
53 
54     // Indicates optional call back fun was called
55     bool callBackDone = false;
56 
callBack()57     void callBack()
58     {
59         callBackDone = true;
60     }
61 
62     // Gets called as part of each TEST_F construction
TimerTestCallBack()63     TimerTestCallBack() : rc(sd_event_default(&events))
64 
65     {
66         // Check for successful creation of
67         // event handler and timer object.
68         EXPECT_GE(rc, 0);
69 
70         std::function<void()> func(
71             std::bind(&TimerTestCallBack::callBack, this));
72         timer = std::make_unique<Timer>(events, func);
73     }
74 
75     // Gets called as part of each TEST_F destruction
~TimerTestCallBack()76     ~TimerTestCallBack() override
77     {
78         events = sd_event_unref(events);
79     }
80 };
81