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