blob: 3fa164702766070a4d1ba3892300a2a45ef2f530 [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:
William A. Kennington III062205d2018-12-19 17:34:13 -08009 sdbusplus::bus::bus 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 Williams78b78032020-05-20 10:32:05 -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 Williams78b78032020-05-20 10:32:05 -050053 auto trigger = [&triggered](sdbusplus::message::message& /*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{
71
72 class BoolHolder
73 {
Andrew Geissler072da3e2018-01-18 07:21:42 -080074 public:
75 bool triggered = false;
Patrick Williams2f7c8872017-05-01 16:27:57 -050076
Patrick Williams78b78032020-05-20 10:32:05 -050077 void callback(sdbusplus::message::message& /*m*/)
Andrew Geissler072da3e2018-01-18 07:21:42 -080078 {
79 triggered = true;
80 }
Patrick Williams2f7c8872017-05-01 16:27:57 -050081 };
82 BoolHolder b;
83
Patrick Williamsda25f342017-05-01 18:08:16 -050084 sdbusplus::bus::match_t m{bus, matchRule(),
Andrew Geissler072da3e2018-01-18 07:21:42 -080085 std::bind(std::mem_fn(&BoolHolder::callback), &b,
86 std::placeholders::_1)};
87 auto m2 = std::move(m); // ensure match is move-safe.
Patrick Williams2f7c8872017-05-01 16:27:57 -050088
89 waitForIt(b.triggered);
90 ASSERT_FALSE(b.triggered);
91
92 bus.request_name(busName);
93
94 waitForIt(b.triggered);
95 ASSERT_TRUE(b.triggered);
96}