blob: 049e033c13a79bb67d12fed0f9cb2296347caf50 [file] [log] [blame]
Patrick Williamsfaa89f22017-04-29 15:50:13 -05001#include <sdbusplus/bus.hpp>
2#include <sdbusplus/bus/match.hpp>
3
Patrick Venture95269db2018-08-31 09:19:17 -07004#include <gtest/gtest.h>
5
Patrick Williamsfaa89f22017-04-29 15:50:13 -05006class Match : public ::testing::Test
7{
Andrew Geissler072da3e2018-01-18 07:21:42 -08008 protected:
Patrick Williams0f282c42021-11-19 11:36:18 -06009 sdbusplus::bus_t bus = sdbusplus::bus::new_bus();
Patrick Williamsfaa89f22017-04-29 15:50:13 -050010
Andrew Geissler072da3e2018-01-18 07:21:42 -080011 static constexpr auto busName = "xyz.openbmc_project.sdbusplus.test.Match";
Patrick Williamsfaa89f22017-04-29 15:50:13 -050012
Andrew Geissler072da3e2018-01-18 07:21:42 -080013 static auto matchRule()
14 {
15 using namespace sdbusplus::bus::match::rules;
16 return nameOwnerChanged() + argN(0, busName);
17 }
Patrick Williamsa85fdff2017-05-01 17:52:15 -050018
Andrew Geissler072da3e2018-01-18 07:21:42 -080019 void waitForIt(bool& triggered)
20 {
21 for (size_t i = 0; (i < 16) && !triggered; ++i)
Patrick Williamsa85fdff2017-05-01 17:52:15 -050022 {
Andrew Geissler072da3e2018-01-18 07:21:42 -080023 bus.wait(0);
24 bus.process_discard();
Patrick Williamsa85fdff2017-05-01 17:52:15 -050025 }
Andrew Geissler072da3e2018-01-18 07:21:42 -080026 }
Patrick Williamsfaa89f22017-04-29 15:50:13 -050027};
28
29TEST_F(Match, FunctorIs_sd_bus_message_handler_t)
30{
Patrick Williamsfaa89f22017-04-29 15:50:13 -050031 bool triggered = false;
Patrick Williams6db88382023-10-20 11:18:17 -050032 auto trigger = [](sd_bus_message* /*m*/, void* context,
33 sd_bus_error* /*e*/) {
Andrew Geissler072da3e2018-01-18 07:21:42 -080034 *static_cast<bool*>(context) = true;
35 return 0;
36 };
Patrick Williamsfaa89f22017-04-29 15:50:13 -050037
Patrick Williamsda25f342017-05-01 18:08:16 -050038 sdbusplus::bus::match_t m{bus, matchRule(), trigger, &triggered};
Andrew Geissler072da3e2018-01-18 07:21:42 -080039 auto m2 = std::move(m); // ensure match is move-safe.
Patrick Williamsfaa89f22017-04-29 15:50:13 -050040
41 waitForIt(triggered);
42 ASSERT_FALSE(triggered);
43
44 bus.request_name(busName);
45
46 waitForIt(triggered);
47 ASSERT_TRUE(triggered);
48}
Patrick Williams2f7c8872017-05-01 16:27:57 -050049
50TEST_F(Match, FunctorIs_LambdaTakingMessage)
51{
52 bool triggered = false;
Patrick Williams10d7aa12021-11-19 11:36:18 -060053 auto trigger = [&triggered](sdbusplus::message_t& /*m*/) {
Andrew Geissler072da3e2018-01-18 07:21:42 -080054 triggered = true;
55 };
Patrick Williams2f7c8872017-05-01 16:27:57 -050056
Patrick Williamsda25f342017-05-01 18:08:16 -050057 sdbusplus::bus::match_t m{bus, matchRule(), trigger};
Andrew Geissler072da3e2018-01-18 07:21:42 -080058 auto m2 = std::move(m); // ensure match is move-safe.
Patrick Williams2f7c8872017-05-01 16:27:57 -050059
60 waitForIt(triggered);
61 ASSERT_FALSE(triggered);
62
63 bus.request_name(busName);
64
65 waitForIt(triggered);
66 ASSERT_TRUE(triggered);
67}
68
69TEST_F(Match, FunctorIs_MemberFunctionTakingMessage)
70{
Patrick Williams2f7c8872017-05-01 16:27:57 -050071 class BoolHolder
72 {
Andrew Geissler072da3e2018-01-18 07:21:42 -080073 public:
74 bool triggered = false;
Patrick Williams2f7c8872017-05-01 16:27:57 -050075
Patrick Williams10d7aa12021-11-19 11:36:18 -060076 void callback(sdbusplus::message_t& /*m*/)
Andrew Geissler072da3e2018-01-18 07:21:42 -080077 {
78 triggered = true;
79 }
Patrick Williams2f7c8872017-05-01 16:27:57 -050080 };
81 BoolHolder b;
82
Patrick Williamsda25f342017-05-01 18:08:16 -050083 sdbusplus::bus::match_t m{bus, matchRule(),
Andrew Geissler072da3e2018-01-18 07:21:42 -080084 std::bind(std::mem_fn(&BoolHolder::callback), &b,
85 std::placeholders::_1)};
86 auto m2 = std::move(m); // ensure match is move-safe.
Patrick Williams2f7c8872017-05-01 16:27:57 -050087
88 waitForIt(b.triggered);
89 ASSERT_FALSE(b.triggered);
90
91 bus.request_name(busName);
92
93 waitForIt(b.triggered);
94 ASSERT_TRUE(b.triggered);
95}