summaryrefslogtreecommitdiffstats
path: root/test/bus/match.cpp
blob: 2074cde4c2982c73e652103696326768bab4d30b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <sdbusplus/bus.hpp>
#include <sdbusplus/bus/match.hpp>

#include <gtest/gtest.h>

class Match : public ::testing::Test
{
  protected:
    sdbusplus::bus::bus bus = sdbusplus::bus::new_bus();

    static constexpr auto busName = "xyz.openbmc_project.sdbusplus.test.Match";

    static auto matchRule()
    {
        using namespace sdbusplus::bus::match::rules;
        return nameOwnerChanged() + argN(0, busName);
    }

    void waitForIt(bool& triggered)
    {
        for (size_t i = 0; (i < 16) && !triggered; ++i)
        {
            bus.wait(0);
            bus.process_discard();
        }
    }
};

TEST_F(Match, FunctorIs_sd_bus_message_handler_t)
{
    bool triggered = false;
    auto trigger = [](sd_bus_message* m, void* context, sd_bus_error* e) {
        *static_cast<bool*>(context) = true;
        return 0;
    };

    sdbusplus::bus::match_t m{bus, matchRule(), trigger, &triggered};
    auto m2 = std::move(m); // ensure match is move-safe.

    waitForIt(triggered);
    ASSERT_FALSE(triggered);

    bus.request_name(busName);

    waitForIt(triggered);
    ASSERT_TRUE(triggered);
}

TEST_F(Match, FunctorIs_LambdaTakingMessage)
{
    bool triggered = false;
    auto trigger = [&triggered](sdbusplus::message::message& m) {
        triggered = true;
    };

    sdbusplus::bus::match_t m{bus, matchRule(), trigger};
    auto m2 = std::move(m); // ensure match is move-safe.

    waitForIt(triggered);
    ASSERT_FALSE(triggered);

    bus.request_name(busName);

    waitForIt(triggered);
    ASSERT_TRUE(triggered);
}

TEST_F(Match, FunctorIs_MemberFunctionTakingMessage)
{

    class BoolHolder
    {
      public:
        bool triggered = false;

        void callback(sdbusplus::message::message& m)
        {
            triggered = true;
        }
    };
    BoolHolder b;

    sdbusplus::bus::match_t m{bus, matchRule(),
                              std::bind(std::mem_fn(&BoolHolder::callback), &b,
                                        std::placeholders::_1)};
    auto m2 = std::move(m); // ensure match is move-safe.

    waitForIt(b.triggered);
    ASSERT_FALSE(b.triggered);

    bus.request_name(busName);

    waitForIt(b.triggered);
    ASSERT_TRUE(b.triggered);
}
OpenPOWER on IntegriCloud