~afrantzis/mir/vt-switching-1

« back to all changes in this revision

Viewing changes to tests/unit-tests/test_asio_main_loop.cpp

server: Use a main loop to handle events in the display server.

Approved by PS Jenkins bot, Alan Griffiths.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright © 2013 Canonical Ltd.
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License version 3 as
 
6
 * published by the Free Software Foundation.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 * GNU General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 *
 
16
 * Authored by: Alexandros Frantzis <alexandros.frantzis@canonical.com>
 
17
 */
 
18
 
 
19
#include "mir/asio_main_loop.h"
 
20
 
 
21
#include <gtest/gtest.h>
 
22
 
 
23
#include <thread>
 
24
#include <atomic>
 
25
 
 
26
#include <sys/types.h>
 
27
#include <unistd.h>
 
28
 
 
29
TEST(AsioMainLoopTest, signal_handled)
 
30
{
 
31
    int const signum{SIGUSR1};
 
32
    int handled_signum{0};
 
33
 
 
34
    mir::AsioMainLoop ml;
 
35
 
 
36
    ml.register_signal_handler(
 
37
        {signum},
 
38
        [&handled_signum, &ml](int sig)
 
39
        {
 
40
           handled_signum = sig;
 
41
           ml.stop();
 
42
        });
 
43
 
 
44
    kill(getpid(), signum);
 
45
 
 
46
    ml.run();
 
47
 
 
48
    ASSERT_EQ(signum, handled_signum);
 
49
}
 
50
 
 
51
 
 
52
TEST(AsioMainLoopTest, multiple_signals_handled)
 
53
{
 
54
    std::vector<int> const signals{SIGUSR1, SIGUSR2};
 
55
    size_t const num_signals_to_send{10};
 
56
    std::vector<int> handled_signals;
 
57
    std::atomic<unsigned int> num_handled_signals{0};
 
58
 
 
59
    mir::AsioMainLoop ml;
 
60
 
 
61
    ml.register_signal_handler(
 
62
        {signals[0], signals[1]},
 
63
        [&handled_signals, &num_handled_signals](int sig)
 
64
        {
 
65
           handled_signals.push_back(sig);
 
66
           ++num_handled_signals;
 
67
        });
 
68
                    
 
69
 
 
70
    std::thread signal_sending_thread(
 
71
        [&ml, num_signals_to_send, &signals, &num_handled_signals]
 
72
        {
 
73
            for (size_t i = 0; i < num_signals_to_send; i++)
 
74
            {
 
75
                kill(getpid(), signals[i % signals.size()]);
 
76
                while (num_handled_signals <= i) std::this_thread::yield();
 
77
            }
 
78
            ml.stop();
 
79
        });
 
80
 
 
81
    ml.run();
 
82
 
 
83
    signal_sending_thread.join();
 
84
 
 
85
    ASSERT_EQ(num_signals_to_send, handled_signals.size());
 
86
 
 
87
    for (size_t i = 0; i < num_signals_to_send; i++)
 
88
        ASSERT_EQ(signals[i % signals.size()], handled_signals[i]) << " index " << i;
 
89
}
 
90
 
 
91
TEST(AsioMainLoopTest, all_registered_handlers_are_called)
 
92
{
 
93
    int const signum{SIGUSR1};
 
94
    std::vector<int> handled_signum{0,0,0};
 
95
 
 
96
    mir::AsioMainLoop ml;
 
97
 
 
98
    ml.register_signal_handler(
 
99
        {signum},
 
100
        [&handled_signum, &ml](int sig)
 
101
        {
 
102
            handled_signum[0] = sig;
 
103
            if (handled_signum[0] != 0 &&
 
104
                handled_signum[1] != 0 &&
 
105
                handled_signum[2] != 0)
 
106
            {
 
107
                ml.stop();
 
108
            }
 
109
        });
 
110
 
 
111
    ml.register_signal_handler(
 
112
        {signum},
 
113
        [&handled_signum, &ml](int sig)
 
114
        {
 
115
            handled_signum[1] = sig;
 
116
            if (handled_signum[0] != 0 &&
 
117
                handled_signum[1] != 0 &&
 
118
                handled_signum[2] != 0)
 
119
            {
 
120
                ml.stop();
 
121
            }
 
122
        });
 
123
 
 
124
    ml.register_signal_handler(
 
125
        {signum},
 
126
        [&handled_signum, &ml](int sig)
 
127
        {
 
128
            handled_signum[2] = sig;
 
129
            if (handled_signum[0] != 0 &&
 
130
                handled_signum[1] != 0 &&
 
131
                handled_signum[2] != 0)
 
132
            {
 
133
                ml.stop();
 
134
            }
 
135
        });
 
136
 
 
137
    kill(getpid(), signum);
 
138
 
 
139
    ml.run();
 
140
 
 
141
    ASSERT_EQ(signum, handled_signum[0]);
 
142
    ASSERT_EQ(signum, handled_signum[1]);
 
143
    ASSERT_EQ(signum, handled_signum[2]);
 
144
}