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
|
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include "poller.h"
#include "test.h"
static void test_poller_init(void) {
struct poller poller;
test_assert(poller_init(&poller) == 0);
poller_finish(&poller);
}
struct test_fd {
int fd;
uint32_t events;
};
static int test_fd_event(int fd, uint32_t mask, void *data) {
struct test_fd *d = data;
d->fd = fd;
d->events = mask;
return 0;
}
static void test_poller_single_fd(void) {
struct poller poller;
test_assert(poller_init(&poller) == 0);
int fds[2];
test_assert(pipe(fds) == 0);
struct test_fd evd;
struct event_source_fd *ev =
poller_add_fd(&poller, fds[0], EVENT_READABLE, test_fd_event, &evd);
test_assert(ev != NULL);
evd.fd = 0;
evd.events = 0;
test_assert(write(fds[1], "\0", 1) == 1);
test_assert(poller_poll(&poller) == 0);
test_assert(evd.fd == fds[0]);
test_assert(evd.events == EVENT_READABLE);
evd.fd = 0;
evd.events = 0;
test_assert(write(fds[1], "\0", 1) == 1);
test_assert(poller_poll(&poller) == 0);
test_assert(evd.fd == fds[0]);
test_assert(evd.events == EVENT_READABLE);
close(fds[0]);
close(fds[1]);
poller_finish(&poller);
}
static void test_poller_multi_fd(void) {
struct poller poller;
test_assert(poller_init(&poller) == 0);
char dummy[8];
int fdsa[2], fdsb[2];
test_assert(pipe(fdsa) == 0);
test_assert(pipe(fdsb) == 0);
struct test_fd evd1, evd2;
struct event_source_fd *ev1 =
poller_add_fd(&poller, fdsa[0], EVENT_READABLE, test_fd_event, &evd1);
struct event_source_fd *ev2 =
poller_add_fd(&poller, fdsb[0], EVENT_READABLE, test_fd_event, &evd2);
test_assert(ev1 != NULL);
test_assert(ev2 != NULL);
evd1.fd = evd2.fd = 0;
evd1.events = evd2.events = 0;
test_assert(write(fdsa[1], "\0", 1) == 1);
test_assert(poller_poll(&poller) == 0);
test_assert(read(fdsa[0], &dummy, sizeof dummy) == 1);
test_assert(evd1.fd == fdsa[0]);
test_assert(evd1.events == EVENT_READABLE);
test_assert(evd2.fd == 0);
test_assert(evd2.events == 0);
evd1.fd = evd2.fd = 0;
evd1.events = evd2.events = 0;
test_assert(write(fdsb[1], "\0", 1) == 1);
test_assert(poller_poll(&poller) == 0);
test_assert(read(fdsb[0], &dummy, sizeof dummy) == 1);
test_assert(evd1.fd == 0);
test_assert(evd1.events == 0);
test_assert(evd2.fd == fdsb[0]);
test_assert(evd2.events == EVENT_READABLE);
evd1.fd = evd2.fd = 0;
evd1.events = evd2.events = 0;
test_assert(write(fdsa[1], "\0", 1) == 1);
test_assert(write(fdsb[1], "\0", 1) == 1);
test_assert(poller_poll(&poller) == 0);
test_assert(read(fdsa[0], &dummy, sizeof dummy) == 1);
test_assert(read(fdsb[0], &dummy, sizeof dummy) == 1);
test_assert(evd1.fd == fdsa[0]);
test_assert(evd1.events == EVENT_READABLE);
test_assert(evd2.fd == fdsb[0]);
test_assert(evd2.events == EVENT_READABLE);
close(fdsa[0]);
close(fdsa[1]);
close(fdsb[0]);
close(fdsb[1]);
poller_finish(&poller);
}
struct test_signal {
int signal;
};
static int test_signal_event(int signal, void *data) {
struct test_signal *d = data;
d->signal = signal;
return 0;
}
static void test_poller_single_signal(void) {
struct poller poller;
test_assert(poller_init(&poller) == 0);
struct test_signal evd;
struct event_source_signal *ev =
poller_add_signal(&poller, SIGRTMIN, test_signal_event, &evd);
test_assert(ev != NULL);
evd.signal = 0;
test_assert(kill(getpid(), SIGRTMIN) == 0);
test_assert(poller_poll(&poller) == 0);
test_assert(evd.signal == SIGRTMIN);
evd.signal = 0;
test_assert(kill(getpid(), SIGRTMIN) == 0);
test_assert(poller_poll(&poller) == 0);
test_assert(evd.signal == SIGRTMIN);
poller_finish(&poller);
}
static void test_poller_multi_signal(void) {
struct poller poller;
test_assert(poller_init(&poller) == 0);
struct test_signal evd1, evd2;
struct event_source_signal *ev1 =
poller_add_signal(&poller, SIGRTMIN, test_signal_event, &evd1);
struct event_source_signal *ev2 =
poller_add_signal(&poller, SIGRTMIN + 1, test_signal_event, &evd2);
test_assert(ev1 != NULL);
test_assert(ev2 != NULL);
evd1.signal = evd2.signal = 0;
test_assert(kill(getpid(), SIGRTMIN) == 0);
test_assert(poller_poll(&poller) == 0);
test_assert(evd1.signal == SIGRTMIN);
test_assert(evd2.signal == 0);
evd1.signal = evd2.signal = 0;
test_assert(kill(getpid(), SIGRTMIN + 1) == 0);
test_assert(poller_poll(&poller) == 0);
test_assert(evd1.signal == 0);
test_assert(evd2.signal == SIGRTMIN + 1);
evd1.signal = evd2.signal = 0;
test_assert(kill(getpid(), SIGRTMIN) == 0);
test_assert(kill(getpid(), SIGRTMIN + 1) == 0);
test_assert(poller_poll(&poller) == 0);
test_assert(evd1.signal == SIGRTMIN);
test_assert(evd2.signal == SIGRTMIN + 1);
poller_finish(&poller);
}
int main(int argc, char *argv[]) {
(void)argc;
(void)argv;
test_run(test_poller_init);
test_run(test_poller_single_fd);
test_run(test_poller_multi_fd);
test_run(test_poller_single_signal);
test_run(test_poller_multi_signal);
}
|