blob: 80bc25924d7e79bf8def901ad1be61304aed1ec7 [file] [log] [blame]
Patrick Williamsfaa89f22017-04-29 15:50:13 -05001#include <gtest/gtest.h>
2#include <sdbusplus/bus.hpp>
3#include <sdbusplus/bus/match.hpp>
4
5class Match : public ::testing::Test
6{
Andrew Geissler072da3e2018-01-18 07:21:42 -08007 protected:
8 decltype(sdbusplus::bus::new_default()) bus = sdbusplus::bus::new_default();
Patrick Williamsfaa89f22017-04-29 15:50:13 -05009
Andrew Geissler072da3e2018-01-18 07:21:42 -080010 static constexpr auto busName = "xyz.openbmc_project.sdbusplus.test.Match";
Patrick Williamsfaa89f22017-04-29 15:50:13 -050011
Andrew Geissler072da3e2018-01-18 07:21:42 -080012 static auto matchRule()
13 {
14 using namespace sdbusplus::bus::match::rules;
15 return nameOwnerChanged() + argN(0, busName);
16 }
Patrick Williamsa85fdff2017-05-01 17:52:15 -050017
Andrew Geissler072da3e2018-01-18 07:21:42 -080018 void waitForIt(bool& triggered)
19 {
20 for (size_t i = 0; (i < 16) && !triggered; ++i)
Patrick Williamsa85fdff2017-05-01 17:52:15 -050021 {
Andrew Geissler072da3e2018-01-18 07:21:42 -080022 bus.wait(0);
23 bus.process_discard();
Patrick Williamsa85fdff2017-05-01 17:52:15 -050024 }
Andrew Geissler072da3e2018-01-18 07:21:42 -080025 }
Patrick Williamsfaa89f22017-04-29 15:50:13 -050026};
27
28TEST_F(Match, FunctorIs_sd_bus_message_handler_t)
29{
Patrick Williamsfaa89f22017-04-29 15:50:13 -050030 bool triggered = false;
Andrew Geissler072da3e2018-01-18 07:21:42 -080031 auto trigger = [](sd_bus_message* m, void* context, sd_bus_error* e) {
32 *static_cast<bool*>(context) = true;
33 return 0;
34 };
Patrick Williamsfaa89f22017-04-29 15:50:13 -050035
Patrick Williamsda25f342017-05-01 18:08:16 -050036 sdbusplus::bus::match_t m{bus, matchRule(), trigger, &triggered};
Andrew Geissler072da3e2018-01-18 07:21:42 -080037 auto m2 = std::move(m); // ensure match is move-safe.
Patrick Williamsfaa89f22017-04-29 15:50:13 -050038
39 waitForIt(triggered);
40 ASSERT_FALSE(triggered);
41
42 bus.request_name(busName);
43
44 waitForIt(triggered);
45 ASSERT_TRUE(triggered);
46}
Patrick Williams2f7c8872017-05-01 16:27:57 -050047
48TEST_F(Match, FunctorIs_LambdaTakingMessage)
49{
50 bool triggered = false;
Andrew Geissler072da3e2018-01-18 07:21:42 -080051 auto trigger = [&triggered](sdbusplus::message::message& m) {
52 triggered = true;
53 };
Patrick Williams2f7c8872017-05-01 16:27:57 -050054
Patrick Williamsda25f342017-05-01 18:08:16 -050055 sdbusplus::bus::match_t m{bus, matchRule(), trigger};
Andrew Geissler072da3e2018-01-18 07:21:42 -080056 auto m2 = std::move(m); // ensure match is move-safe.
Patrick Williams2f7c8872017-05-01 16:27:57 -050057
58 waitForIt(triggered);
59 ASSERT_FALSE(triggered);
60
61 bus.request_name(busName);
62
63 waitForIt(triggered);
64 ASSERT_TRUE(triggered);
65}
66
67TEST_F(Match, FunctorIs_MemberFunctionTakingMessage)
68{
69
70 class BoolHolder
71 {
Andrew Geissler072da3e2018-01-18 07:21:42 -080072 public:
73 bool triggered = false;
Patrick Williams2f7c8872017-05-01 16:27:57 -050074
Andrew Geissler072da3e2018-01-18 07:21:42 -080075 void callback(sdbusplus::message::message& m)
76 {
77 triggered = true;
78 }
Patrick Williams2f7c8872017-05-01 16:27:57 -050079 };
80 BoolHolder b;
81
Patrick Williamsda25f342017-05-01 18:08:16 -050082 sdbusplus::bus::match_t m{bus, matchRule(),
Andrew Geissler072da3e2018-01-18 07:21:42 -080083 std::bind(std::mem_fn(&BoolHolder::callback), &b,
84 std::placeholders::_1)};
85 auto m2 = std::move(m); // ensure match is move-safe.
Patrick Williams2f7c8872017-05-01 16:27:57 -050086
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}