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 = sdbusplus::bus::new_default(); 9 10 static constexpr auto busName = "xyz.openbmc_project.sdbusplus.test.Match"; 11 12 static auto matchRule() 13 { 14 using namespace sdbusplus::bus::match::rules; 15 return nameOwnerChanged() + argN(0, busName); 16 } 17 18 void waitForIt(bool& triggered) 19 { 20 for (size_t i = 0; (i < 16) && !triggered; ++i) 21 { 22 bus.wait(0); 23 bus.process_discard(); 24 } 25 } 26 }; 27 28 TEST_F(Match, FunctorIs_sd_bus_message_handler_t) 29 { 30 bool triggered = false; 31 auto trigger = [](sd_bus_message* m, void* context, sd_bus_error* e) { 32 *static_cast<bool*>(context) = true; 33 return 0; 34 }; 35 36 sdbusplus::bus::match_t m{bus, matchRule(), trigger, &triggered}; 37 auto m2 = std::move(m); // ensure match is move-safe. 38 39 waitForIt(triggered); 40 ASSERT_FALSE(triggered); 41 42 bus.request_name(busName); 43 44 waitForIt(triggered); 45 ASSERT_TRUE(triggered); 46 } 47 48 TEST_F(Match, FunctorIs_LambdaTakingMessage) 49 { 50 bool triggered = false; 51 auto trigger = [&triggered](sdbusplus::message::message& m) { 52 triggered = true; 53 }; 54 55 sdbusplus::bus::match_t m{bus, matchRule(), trigger}; 56 auto m2 = std::move(m); // ensure match is move-safe. 57 58 waitForIt(triggered); 59 ASSERT_FALSE(triggered); 60 61 bus.request_name(busName); 62 63 waitForIt(triggered); 64 ASSERT_TRUE(triggered); 65 } 66 67 TEST_F(Match, FunctorIs_MemberFunctionTakingMessage) 68 { 69 70 class BoolHolder 71 { 72 public: 73 bool triggered = false; 74 75 void callback(sdbusplus::message::message& m) 76 { 77 triggered = true; 78 } 79 }; 80 BoolHolder b; 81 82 sdbusplus::bus::match_t m{bus, matchRule(), 83 std::bind(std::mem_fn(&BoolHolder::callback), &b, 84 std::placeholders::_1)}; 85 auto m2 = std::move(m); // ensure match is move-safe. 86 87 waitForIt(b.triggered); 88 ASSERT_FALSE(b.triggered); 89 90 bus.request_name(busName); 91 92 waitForIt(b.triggered); 93 ASSERT_TRUE(b.triggered); 94 } 95