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: |
William A. Kennington III | 062205d | 2018-12-19 17:34:13 -0800 | [diff] [blame] | 9 | sdbusplus::bus::bus 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 | 78b7803 | 2020-05-20 10:32:05 -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 | 78b7803 | 2020-05-20 10:32:05 -0500 | [diff] [blame] | 53 | auto trigger = [&triggered](sdbusplus::message::message& /*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 | { |
| 71 | |
| 72 | class BoolHolder |
| 73 | { |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 74 | public: |
| 75 | bool triggered = false; |
Patrick Williams | 2f7c887 | 2017-05-01 16:27:57 -0500 | [diff] [blame] | 76 | |
Patrick Williams | 78b7803 | 2020-05-20 10:32:05 -0500 | [diff] [blame] | 77 | void callback(sdbusplus::message::message& /*m*/) |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 78 | { |
| 79 | triggered = true; |
| 80 | } |
Patrick Williams | 2f7c887 | 2017-05-01 16:27:57 -0500 | [diff] [blame] | 81 | }; |
| 82 | BoolHolder b; |
| 83 | |
Patrick Williams | da25f34 | 2017-05-01 18:08:16 -0500 | [diff] [blame] | 84 | sdbusplus::bus::match_t m{bus, matchRule(), |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 85 | std::bind(std::mem_fn(&BoolHolder::callback), &b, |
| 86 | std::placeholders::_1)}; |
| 87 | auto m2 = std::move(m); // ensure match is move-safe. |
Patrick Williams | 2f7c887 | 2017-05-01 16:27:57 -0500 | [diff] [blame] | 88 | |
| 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 | } |