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; |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 32 | auto trigger = [](sd_bus_message* m, void* context, sd_bus_error* e) { |
| 33 | *static_cast<bool*>(context) = true; |
| 34 | return 0; |
| 35 | }; |
Patrick Williams | faa89f2 | 2017-04-29 15:50:13 -0500 | [diff] [blame] | 36 | |
Patrick Williams | da25f34 | 2017-05-01 18:08:16 -0500 | [diff] [blame] | 37 | sdbusplus::bus::match_t m{bus, matchRule(), trigger, &triggered}; |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 38 | auto m2 = std::move(m); // ensure match is move-safe. |
Patrick Williams | faa89f2 | 2017-04-29 15:50:13 -0500 | [diff] [blame] | 39 | |
| 40 | waitForIt(triggered); |
| 41 | ASSERT_FALSE(triggered); |
| 42 | |
| 43 | bus.request_name(busName); |
| 44 | |
| 45 | waitForIt(triggered); |
| 46 | ASSERT_TRUE(triggered); |
| 47 | } |
Patrick Williams | 2f7c887 | 2017-05-01 16:27:57 -0500 | [diff] [blame] | 48 | |
| 49 | TEST_F(Match, FunctorIs_LambdaTakingMessage) |
| 50 | { |
| 51 | bool triggered = false; |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 52 | auto trigger = [&triggered](sdbusplus::message::message& m) { |
| 53 | triggered = true; |
| 54 | }; |
Patrick Williams | 2f7c887 | 2017-05-01 16:27:57 -0500 | [diff] [blame] | 55 | |
Patrick Williams | da25f34 | 2017-05-01 18:08:16 -0500 | [diff] [blame] | 56 | sdbusplus::bus::match_t m{bus, matchRule(), trigger}; |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 57 | auto m2 = std::move(m); // ensure match is move-safe. |
Patrick Williams | 2f7c887 | 2017-05-01 16:27:57 -0500 | [diff] [blame] | 58 | |
| 59 | waitForIt(triggered); |
| 60 | ASSERT_FALSE(triggered); |
| 61 | |
| 62 | bus.request_name(busName); |
| 63 | |
| 64 | waitForIt(triggered); |
| 65 | ASSERT_TRUE(triggered); |
| 66 | } |
| 67 | |
| 68 | TEST_F(Match, FunctorIs_MemberFunctionTakingMessage) |
| 69 | { |
| 70 | |
| 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 | |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 76 | void callback(sdbusplus::message::message& m) |
| 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 | } |