Patrick Williams | faa89f2 | 2017-04-29 15:50:13 -0500 | [diff] [blame] | 1 | #include <sdbusplus/bus.hpp> |
| 2 | #include <sdbusplus/bus/match.hpp> |
| 3 | |
Patrick Venture | 95269db | 2018-08-31 09:19:17 -0700 | [diff] [blame] | 4 | #include <gtest/gtest.h> |
| 5 | |
Patrick Williams | faa89f2 | 2017-04-29 15:50:13 -0500 | [diff] [blame] | 6 | class Match : public ::testing::Test |
| 7 | { |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 8 | protected: |
Patrick Williams | 0f282c4 | 2021-11-19 11:36:18 -0600 | [diff] [blame] | 9 | sdbusplus::bus_t bus = sdbusplus::bus::new_bus(); |
Patrick Williams | faa89f2 | 2017-04-29 15:50:13 -0500 | [diff] [blame] | 10 | |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 11 | static constexpr auto busName = "xyz.openbmc_project.sdbusplus.test.Match"; |
Patrick Williams | faa89f2 | 2017-04-29 15:50:13 -0500 | [diff] [blame] | 12 | |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 13 | static auto matchRule() |
| 14 | { |
| 15 | using namespace sdbusplus::bus::match::rules; |
| 16 | return nameOwnerChanged() + argN(0, busName); |
| 17 | } |
Patrick Williams | a85fdff | 2017-05-01 17:52:15 -0500 | [diff] [blame] | 18 | |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 19 | void waitForIt(bool& triggered) |
| 20 | { |
| 21 | for (size_t i = 0; (i < 16) && !triggered; ++i) |
Patrick Williams | a85fdff | 2017-05-01 17:52:15 -0500 | [diff] [blame] | 22 | { |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 23 | bus.wait(0); |
| 24 | bus.process_discard(); |
Patrick Williams | a85fdff | 2017-05-01 17:52:15 -0500 | [diff] [blame] | 25 | } |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 26 | } |
Patrick Williams | faa89f2 | 2017-04-29 15:50:13 -0500 | [diff] [blame] | 27 | }; |
| 28 | |
| 29 | TEST_F(Match, FunctorIs_sd_bus_message_handler_t) |
| 30 | { |
Patrick Williams | faa89f2 | 2017-04-29 15:50:13 -0500 | [diff] [blame] | 31 | bool triggered = false; |
Patrick Williams | 6db8838 | 2023-10-20 11:18:17 -0500 | [diff] [blame] | 32 | auto trigger = [](sd_bus_message* /*m*/, void* context, |
| 33 | sd_bus_error* /*e*/) { |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 34 | *static_cast<bool*>(context) = true; |
| 35 | return 0; |
| 36 | }; |
Patrick Williams | faa89f2 | 2017-04-29 15:50:13 -0500 | [diff] [blame] | 37 | |
Patrick Williams | da25f34 | 2017-05-01 18:08:16 -0500 | [diff] [blame] | 38 | sdbusplus::bus::match_t m{bus, matchRule(), trigger, &triggered}; |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 39 | auto m2 = std::move(m); // ensure match is move-safe. |
Patrick Williams | faa89f2 | 2017-04-29 15:50:13 -0500 | [diff] [blame] | 40 | |
| 41 | waitForIt(triggered); |
| 42 | ASSERT_FALSE(triggered); |
| 43 | |
| 44 | bus.request_name(busName); |
| 45 | |
| 46 | waitForIt(triggered); |
| 47 | ASSERT_TRUE(triggered); |
| 48 | } |
Patrick Williams | 2f7c887 | 2017-05-01 16:27:57 -0500 | [diff] [blame] | 49 | |
| 50 | TEST_F(Match, FunctorIs_LambdaTakingMessage) |
| 51 | { |
| 52 | bool triggered = false; |
Patrick Williams | 10d7aa1 | 2021-11-19 11:36:18 -0600 | [diff] [blame] | 53 | auto trigger = [&triggered](sdbusplus::message_t& /*m*/) { |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 54 | triggered = true; |
| 55 | }; |
Patrick Williams | 2f7c887 | 2017-05-01 16:27:57 -0500 | [diff] [blame] | 56 | |
Patrick Williams | da25f34 | 2017-05-01 18:08:16 -0500 | [diff] [blame] | 57 | sdbusplus::bus::match_t m{bus, matchRule(), trigger}; |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 58 | auto m2 = std::move(m); // ensure match is move-safe. |
Patrick Williams | 2f7c887 | 2017-05-01 16:27:57 -0500 | [diff] [blame] | 59 | |
| 60 | waitForIt(triggered); |
| 61 | ASSERT_FALSE(triggered); |
| 62 | |
| 63 | bus.request_name(busName); |
| 64 | |
| 65 | waitForIt(triggered); |
| 66 | ASSERT_TRUE(triggered); |
| 67 | } |
| 68 | |
| 69 | TEST_F(Match, FunctorIs_MemberFunctionTakingMessage) |
| 70 | { |
Patrick Williams | 2f7c887 | 2017-05-01 16:27:57 -0500 | [diff] [blame] | 71 | class BoolHolder |
| 72 | { |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 73 | public: |
| 74 | bool triggered = false; |
Patrick Williams | 2f7c887 | 2017-05-01 16:27:57 -0500 | [diff] [blame] | 75 | |
Patrick Williams | 10d7aa1 | 2021-11-19 11:36:18 -0600 | [diff] [blame] | 76 | void callback(sdbusplus::message_t& /*m*/) |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 77 | { |
| 78 | triggered = true; |
| 79 | } |
Patrick Williams | 2f7c887 | 2017-05-01 16:27:57 -0500 | [diff] [blame] | 80 | }; |
| 81 | BoolHolder b; |
| 82 | |
Patrick Williams | da25f34 | 2017-05-01 18:08:16 -0500 | [diff] [blame] | 83 | sdbusplus::bus::match_t m{bus, matchRule(), |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 84 | std::bind(std::mem_fn(&BoolHolder::callback), &b, |
| 85 | std::placeholders::_1)}; |
| 86 | auto m2 = std::move(m); // ensure match is move-safe. |
Patrick Williams | 2f7c887 | 2017-05-01 16:27:57 -0500 | [diff] [blame] | 87 | |
| 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 | } |