xref: /openbmc/telemetry/tests/src/test_detached_timer.cpp (revision d22381949a8705da399107fcff71f8fbfe910f46)
1b5645947SKrzysztof Grobelny #include "dbus_environment.hpp"
2*d2238194SKrzysztof Grobelny #include "helpers.hpp"
3b5645947SKrzysztof Grobelny #include "utils/detached_timer.hpp"
4b5645947SKrzysztof Grobelny 
5b5645947SKrzysztof Grobelny #include <gmock/gmock.h>
6b5645947SKrzysztof Grobelny 
7b5645947SKrzysztof Grobelny namespace utils
8b5645947SKrzysztof Grobelny {
9b5645947SKrzysztof Grobelny 
10b5645947SKrzysztof Grobelny using namespace testing;
11b5645947SKrzysztof Grobelny using namespace std::chrono_literals;
12b5645947SKrzysztof Grobelny 
13b5645947SKrzysztof Grobelny class TestDetachedTimer : public Test
14b5645947SKrzysztof Grobelny {
15b5645947SKrzysztof Grobelny   public:
16b5645947SKrzysztof Grobelny     uint32_t value = 0;
17b5645947SKrzysztof Grobelny };
18b5645947SKrzysztof Grobelny 
TEST_F(TestDetachedTimer,executesLambdaAfterTimeout)19b5645947SKrzysztof Grobelny TEST_F(TestDetachedTimer, executesLambdaAfterTimeout)
20b5645947SKrzysztof Grobelny {
21b5645947SKrzysztof Grobelny     auto setPromise = DbusEnvironment::setPromise("timer");
22b5645947SKrzysztof Grobelny 
23b5645947SKrzysztof Grobelny     makeDetachedTimer(DbusEnvironment::getIoc(), 100ms, [this, &setPromise] {
24b5645947SKrzysztof Grobelny         ++value;
25b5645947SKrzysztof Grobelny         setPromise();
26b5645947SKrzysztof Grobelny     });
27b5645947SKrzysztof Grobelny 
28b5645947SKrzysztof Grobelny     auto elapsed = DbusEnvironment::measureTime(
29b5645947SKrzysztof Grobelny         [] { DbusEnvironment::waitForFuture("timer"); });
30b5645947SKrzysztof Grobelny 
31b5645947SKrzysztof Grobelny     EXPECT_THAT(elapsed, AllOf(Ge(100ms), Lt(200ms)));
32b5645947SKrzysztof Grobelny     EXPECT_THAT(value, Eq(1u));
33b5645947SKrzysztof Grobelny }
34b5645947SKrzysztof Grobelny 
35b5645947SKrzysztof Grobelny } // namespace utils
36