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