File: signals_test.cpp

package info (click to toggle)
properties-cpp 0.0.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 336 kB
  • sloc: cpp: 673; makefile: 21
file content (192 lines) | stat: -rw-r--r-- 5,116 bytes parent folder | download | duplicates (5)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
 * Copyright © 2013 Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authored by: Thomas Voß <thomas.voss@canonical.com>
 */

#include <core/signal.h>

#include <gtest/gtest.h>

#include <condition_variable>
#include <functional>
#include <mutex>
#include <thread>

namespace
{
template<typename T>
struct Expectation
{
    Expectation(const T& expected_value) : expected_value(expected_value)
    {
    }

    bool satisfied() const
    {
        return triggered && current_value == expected_value;
    }

    bool triggered = false;
    T expected_value;
    T current_value;
};
}

TEST(Signal, emission_works)
{
    Expectation<int> expectation{42};

    core::Signal<int> s;
    s.connect([&expectation](int value) { expectation.triggered = true; expectation.current_value = value; });

    s(42);

    EXPECT_TRUE(expectation.satisfied());
}

TEST(Signal, disconnect_results_in_slots_not_invoked_anymore)
{
    Expectation<int> expectation{42};

    core::Signal<int> s;
    auto connection = s.connect(
                [&expectation](int value)
                {
                    expectation.triggered = true;
                    expectation.current_value = value;
                });
    connection.disconnect();
    s(42);

    EXPECT_FALSE(expectation.satisfied());
}

TEST(Signal, disconnect_via_scoped_connection_results_in_slots_not_invoked_anymore)
{
    Expectation<int> expectation{42};

    core::Signal<int> s;
    auto connection = s.connect(
                [&expectation](int value)
                {
                    expectation.triggered = true;
                    expectation.current_value = value;
                });
    {
        core::ScopedConnection sc{connection};
    }
    s(42);

    EXPECT_FALSE(expectation.satisfied());
}

TEST(Signal, a_signal_going_out_of_scope_disconnects_from_slots)
{
    auto signal = std::make_shared<core::Signal<int>>();

    auto connection = signal->connect([](int value) { std::cout << value << std::endl; });

    signal.reset();

    core::Connection::Dispatcher dispatcher{};

    EXPECT_NO_THROW(connection.disconnect());
    EXPECT_NO_THROW(connection.dispatch_via(dispatcher));
}

#include <queue>

namespace
{
struct EventLoop
{
    typedef std::function<void()> Handler;

    void stop()
    {
        stop_requested = true;
    }

    void run()
    {
        while (!stop_requested)
        {
            std::unique_lock<std::mutex> ul(guard);
            wait_condition.wait_for(
                        ul,
                        std::chrono::milliseconds{500},
                        [this]() { return handlers.size() > 0; });

            while (handlers.size() > 0)
            {
                handlers.front()();
                handlers.pop();
            }
        }
    }

    void dispatch(const Handler& h)
    {
        std::lock_guard<std::mutex> lg(guard);
        handlers.push(h);
    }

    bool stop_requested = false;
    std::queue<Handler> handlers;
    std::mutex guard;
    std::condition_variable wait_condition;
};
}

TEST(Signal, installing_a_custom_dispatcher_ensures_invocation_on_correct_thread)
{
    // We instantiate an event loop and run it on a different thread than the main one.
    EventLoop dispatcher;
    std::thread dispatcher_thread{[&dispatcher]() { dispatcher.run(); }};
    std::thread::id dispatcher_thread_id = dispatcher_thread.get_id();

    // The signal that we want to dispatch via the event loop.
    core::Signal<int, double> s;

    static const int expected_invocation_count = 10000;

    // Setup the connection. For each invocation we check that the id of the
    // thread the handler is being called upon equals the thread that the
    // event loop is running upon.
    auto connection = s.connect(
                [&dispatcher, dispatcher_thread_id](int value, double)
                {
                    EXPECT_EQ(dispatcher_thread_id,
                              std::this_thread::get_id());                    

                    if (value == expected_invocation_count)
                        dispatcher.stop();
                });

    // Route the connection via the dispatcher
    connection.dispatch_via(
                std::bind(
                    &EventLoop::dispatch,
                    std::ref(dispatcher),
                    std::placeholders::_1));

    // Invoke the signal from the main thread.
    for (unsigned int i = 1; i <= expected_invocation_count; i++)
        s(i, 42.);

    if (dispatcher_thread.joinable())
        dispatcher_thread.join();
}