xref: /openbmc/sdbusplus/test/bus/match.cpp (revision faa89f27152b3d9727e16825eac0e90d0f588e3a)
1 #include <gtest/gtest.h>
2 #include <sdbusplus/bus.hpp>
3 #include <sdbusplus/bus/match.hpp>
4 
5 class Match : public ::testing::Test
6 {
7     protected:
8         decltype(sdbusplus::bus::new_default()) bus =
9                 sdbusplus::bus::new_default();
10 
11         static constexpr auto busName =
12                 "xyz.openbmc_project.sdbusplus.test.Match";
13 
14         static constexpr auto matchRule =
15                 "type='signal',"
16                 "interface=org.freedesktop.DBus,"
17                 "member='NameOwnerChanged',"
18                 "arg0='xyz.openbmc_project.sdbusplus.test.Match'";
19 
20         void waitForIt(bool& triggered)
21         {
22             for (size_t i = 0; (i < 16) && !triggered; ++i)
23             {
24                 bus.wait(0);
25                 bus.process_discard();
26             }
27         }
28 };
29 
30 TEST_F(Match, FunctorIs_sd_bus_message_handler_t)
31 {
32     using namespace std::literals;
33 
34     bool triggered = false;
35     auto trigger = [](sd_bus_message *m, void* context, sd_bus_error* e)
36         {
37             *static_cast<bool*>(context) = true;
38             return 0;
39         };
40 
41     sdbusplus::bus::match_t m{bus, matchRule, trigger, &triggered};
42     auto m2 = std::move(m);  // ensure match is move-safe.
43 
44     waitForIt(triggered);
45     ASSERT_FALSE(triggered);
46 
47     bus.request_name(busName);
48 
49     waitForIt(triggered);
50     ASSERT_TRUE(triggered);
51 }
52