blob: 2074cde4c2982c73e652103696326768bab4d30b [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;
Andrew Geissler072da3e2018-01-18 07:21:42 -080032 auto trigger = [](sd_bus_message* m, void* context, sd_bus_error* e) {
33 *static_cast<bool*>(context) = true;
34 return 0;
35 };
Patrick Williamsfaa89f22017-04-29 15:50:13 -050036
Patrick Williamsda25f342017-05-01 18:08:16 -050037 sdbusplus::bus::match_t m{bus, matchRule(), trigger, &triggered};
Andrew Geissler072da3e2018-01-18 07:21:42 -080038 auto m2 = std::move(m); // ensure match is move-safe.
Patrick Williamsfaa89f22017-04-29 15:50:13 -050039
40 waitForIt(triggered);
41 ASSERT_FALSE(triggered);
42
43 bus.request_name(busName);
44
45 waitForIt(triggered);
46 ASSERT_TRUE(triggered);
47}
Patrick Williams2f7c8872017-05-01 16:27:57 -050048
49TEST_F(Match, FunctorIs_LambdaTakingMessage)
50{
51 bool triggered = false;
Andrew Geissler072da3e2018-01-18 07:21:42 -080052 auto trigger = [&triggered](sdbusplus::message::message& m) {
53 triggered = true;
54 };
Patrick Williams2f7c8872017-05-01 16:27:57 -050055
Patrick Williamsda25f342017-05-01 18:08:16 -050056 sdbusplus::bus::match_t m{bus, matchRule(), trigger};
Andrew Geissler072da3e2018-01-18 07:21:42 -080057 auto m2 = std::move(m); // ensure match is move-safe.
Patrick Williams2f7c8872017-05-01 16:27:57 -050058
59 waitForIt(triggered);
60 ASSERT_FALSE(triggered);
61
62 bus.request_name(busName);
63
64 waitForIt(triggered);
65 ASSERT_TRUE(triggered);
66}
67
68TEST_F(Match, FunctorIs_MemberFunctionTakingMessage)
69{
70
71 class BoolHolder
72 {
Andrew Geissler072da3e2018-01-18 07:21:42 -080073 public:
74 bool triggered = false;
Patrick Williams2f7c8872017-05-01 16:27:57 -050075
Andrew Geissler072da3e2018-01-18 07:21:42 -080076 void callback(sdbusplus::message::message& m)
77 {
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}