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 bool triggered = false; 33 auto trigger = [](sd_bus_message *m, void* context, sd_bus_error* e) 34 { 35 *static_cast<bool*>(context) = true; 36 return 0; 37 }; 38 39 sdbusplus::bus::match_t m{bus, matchRule, trigger, &triggered}; 40 auto m2 = std::move(m); // ensure match is move-safe. 41 42 waitForIt(triggered); 43 ASSERT_FALSE(triggered); 44 45 bus.request_name(busName); 46 47 waitForIt(triggered); 48 ASSERT_TRUE(triggered); 49 } 50 51 TEST_F(Match, FunctorIs_LambdaTakingMessage) 52 { 53 bool triggered = false; 54 auto trigger = [&triggered](sdbusplus::message::message& m) 55 { 56 triggered = true; 57 }; 58 59 sdbusplus::bus::match_t m{bus, matchRule, trigger}; 60 auto m2 = std::move(m); // ensure match is move-safe. 61 62 waitForIt(triggered); 63 ASSERT_FALSE(triggered); 64 65 bus.request_name(busName); 66 67 waitForIt(triggered); 68 ASSERT_TRUE(triggered); 69 } 70 71 TEST_F(Match, FunctorIs_MemberFunctionTakingMessage) 72 { 73 74 class BoolHolder 75 { 76 public: 77 bool triggered = false; 78 79 void callback(sdbusplus::message::message& m) 80 { 81 triggered = true; 82 } 83 }; 84 BoolHolder b; 85 86 sdbusplus::bus::match_t m{bus, matchRule, 87 std::bind(std::mem_fn(&BoolHolder::callback), 88 &b, std::placeholders::_1)}; 89 auto m2 = std::move(m); // ensure match is move-safe. 90 91 waitForIt(b.triggered); 92 ASSERT_FALSE(b.triggered); 93 94 bus.request_name(busName); 95 96 waitForIt(b.triggered); 97 ASSERT_TRUE(b.triggered); 98 } 99