1 /*
2 * Copyright © 2012 Intel Corporation
3 * Copyright © 2012 Jason Ekstrand
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
25 */
26
27 #define _GNU_SOURCE
28 #include <stdlib.h>
29 #include <stdint.h>
30 #include <assert.h>
31 #include <unistd.h>
32 #include <signal.h>
33 #include <string.h>
34 #include <sys/time.h>
35
36 #include "wayland-private.h"
37 #include "wayland-server.h"
38 #include "test-runner.h"
39
40 static int
fd_dispatch(int fd,uint32_t mask,void * data)41 fd_dispatch(int fd, uint32_t mask, void *data)
42 {
43 int *p = data;
44
45 assert(mask == 0);
46 ++(*p);
47
48 return 0;
49 }
50
TEST(event_loop_post_dispatch_check)51 TEST(event_loop_post_dispatch_check)
52 {
53 struct wl_event_loop *loop = wl_event_loop_create();
54 struct wl_event_source *source;
55 int dispatch_ran = 0;
56 int p[2];
57
58 assert(loop);
59 assert(pipe(p) == 0);
60
61 source = wl_event_loop_add_fd(loop, p[0], WL_EVENT_READABLE,
62 fd_dispatch, &dispatch_ran);
63 assert(source);
64 wl_event_source_check(source);
65
66 wl_event_loop_dispatch(loop, 0);
67 assert(dispatch_ran == 1);
68
69 assert(close(p[0]) == 0);
70 assert(close(p[1]) == 0);
71 wl_event_source_remove(source);
72 wl_event_loop_destroy(loop);
73 }
74
75 struct free_source_context {
76 struct wl_event_source *source1, *source2;
77 int p1[2], p2[2];
78 int count;
79 };
80
81 static int
free_source_callback(int fd,uint32_t mask,void * data)82 free_source_callback(int fd, uint32_t mask, void *data)
83 {
84 struct free_source_context *context = data;
85
86 context->count++;
87
88 /* Remove other source */
89 if (fd == context->p1[0]) {
90 wl_event_source_remove(context->source2);
91 context->source2 = NULL;
92 } else if (fd == context->p2[0]) {
93 wl_event_source_remove(context->source1);
94 context->source1 = NULL;
95 } else {
96 assert(0);
97 }
98
99 return 1;
100 }
101
TEST(event_loop_free_source_with_data)102 TEST(event_loop_free_source_with_data)
103 {
104 struct wl_event_loop *loop = wl_event_loop_create();
105 struct free_source_context context;
106 int data;
107
108 /* This test is a little tricky to get right, since we don't
109 * have any guarantee from the event loop (ie epoll) on the
110 * order of which it reports events. We want to have one
111 * source free the other, but we don't know which one is going
112 * to run first. So we add two fd sources with a callback
113 * that frees the other source and check that only one of them
114 * run (and that we don't crash, of course).
115 */
116
117 assert(loop);
118
119 context.count = 0;
120 assert(pipe(context.p1) == 0);
121 assert(pipe(context.p2) == 0);
122 context.source1 =
123 wl_event_loop_add_fd(loop, context.p1[0], WL_EVENT_READABLE,
124 free_source_callback, &context);
125 assert(context.source1);
126 context.source2 =
127 wl_event_loop_add_fd(loop, context.p2[0], WL_EVENT_READABLE,
128 free_source_callback, &context);
129 assert(context.source2);
130
131 data = 5;
132 assert(write(context.p1[1], &data, sizeof data) == sizeof data);
133 assert(write(context.p2[1], &data, sizeof data) == sizeof data);
134
135 wl_event_loop_dispatch(loop, 0);
136
137 assert(context.count == 1);
138
139 if (context.source1)
140 wl_event_source_remove(context.source1);
141 if (context.source2)
142 wl_event_source_remove(context.source2);
143 wl_event_loop_destroy(loop);
144
145 assert(close(context.p1[0]) == 0);
146 assert(close(context.p1[1]) == 0);
147 assert(close(context.p2[0]) == 0);
148 assert(close(context.p2[1]) == 0);
149 }
150
151 static int
signal_callback(int signal_number,void * data)152 signal_callback(int signal_number, void *data)
153 {
154 int *got_it = data;
155
156 assert(signal_number == SIGUSR1);
157 ++(*got_it);
158
159 return 1;
160 }
161
TEST(event_loop_signal)162 TEST(event_loop_signal)
163 {
164 struct wl_event_loop *loop = wl_event_loop_create();
165 struct wl_event_source *source;
166 int got_it = 0;
167
168 source = wl_event_loop_add_signal(loop, SIGUSR1,
169 signal_callback, &got_it);
170 assert(source);
171
172 assert(wl_event_loop_dispatch(loop, 0) == 0);
173 assert(!got_it);
174 assert(kill(getpid(), SIGUSR1) == 0);
175 /*
176 * On Linux the signal will be immediately visible in the epoll_wait()
177 * call. However, on FreeBSD we may need a small delay between kill()
178 * call and the signal being visible to the kevent() call. This
179 * sometimes happens when the signal processing and kevent processing
180 * runs on different CPUs, so becomes more likely when the system is
181 * under load (e.g. running all tests in parallel).
182 * See https://github.com/jiixyj/epoll-shim/pull/32
183 * Passing 1ms as the timeout appears to avoid this race condition in
184 * all cases tested so far, but to be safe we use 1000ms which should
185 * be enough time even on a really slow (or emulated) system.
186 */
187 assert(wl_event_loop_dispatch(loop, 1000) == 0);
188 assert(got_it == 1);
189
190 wl_event_source_remove(source);
191 wl_event_loop_destroy(loop);
192 }
193
TEST(event_loop_multiple_same_signals)194 TEST(event_loop_multiple_same_signals)
195 {
196 struct wl_event_loop *loop = wl_event_loop_create();
197 struct wl_event_source *s1, *s2;
198 int calls_no = 0;
199 int i;
200
201 s1 = wl_event_loop_add_signal(loop, SIGUSR1,
202 signal_callback, &calls_no);
203 assert(s1);
204
205 s2 = wl_event_loop_add_signal(loop, SIGUSR1,
206 signal_callback, &calls_no);
207 assert(s2);
208
209 assert(wl_event_loop_dispatch(loop, 0) == 0);
210 assert(!calls_no);
211
212 /* Try it more times */
213 for (i = 0; i < 5; ++i) {
214 calls_no = 0;
215 assert(kill(getpid(), SIGUSR1) == 0);
216 /*
217 * We need a non-zero timeout here to allow the test to pass
218 * on non-Linux systems (see comment in event_loop_signal).
219 */
220 assert(wl_event_loop_dispatch(loop, 1000) == 0);
221 assert(calls_no == 2);
222 }
223
224 wl_event_source_remove(s1);
225
226 /* Try it again with one source */
227 calls_no = 0;
228 assert(kill(getpid(), SIGUSR1) == 0);
229 /*
230 * We need a non-zero timeout here to allow the test to pass
231 * on non-Linux systems (see comment in event_loop_signal).
232 */
233 assert(wl_event_loop_dispatch(loop, 1000) == 0);
234 assert(calls_no == 1);
235
236 wl_event_source_remove(s2);
237
238 wl_event_loop_destroy(loop);
239 }
240
241 static int
timer_callback(void * data)242 timer_callback(void *data)
243 {
244 int *got_it = data;
245
246 ++(*got_it);
247
248 return 1;
249 }
250
TEST(event_loop_timer)251 TEST(event_loop_timer)
252 {
253 struct wl_event_loop *loop = wl_event_loop_create();
254 struct wl_event_source *source1, *source2;
255 int got_it = 0;
256
257 source1 = wl_event_loop_add_timer(loop, timer_callback, &got_it);
258 assert(source1);
259 wl_event_source_timer_update(source1, 20);
260
261 source2 = wl_event_loop_add_timer(loop, timer_callback, &got_it);
262 assert(source2);
263 wl_event_source_timer_update(source2, 100);
264
265 /* Check that the timer marked for 20 msec from now fires within 30
266 * msec, and that the timer marked for 100 msec is expected to fire
267 * within an additional 90 msec. (Some extra wait time is provided to
268 * account for reasonable code execution / thread preemption delays.) */
269
270 wl_event_loop_dispatch(loop, 0);
271 assert(got_it == 0);
272 wl_event_loop_dispatch(loop, 30);
273 assert(got_it == 1);
274 wl_event_loop_dispatch(loop, 0);
275 assert(got_it == 1);
276 wl_event_loop_dispatch(loop, 90);
277 assert(got_it == 2);
278
279 wl_event_source_remove(source1);
280 wl_event_source_remove(source2);
281 wl_event_loop_destroy(loop);
282 }
283
284 #define MSEC_TO_USEC(msec) ((msec) * 1000)
285
286 struct timer_update_context {
287 struct wl_event_source *source1, *source2;
288 int count;
289 };
290
291 static int
timer_update_callback_1(void * data)292 timer_update_callback_1(void *data)
293 {
294 struct timer_update_context *context = data;
295
296 context->count++;
297 wl_event_source_timer_update(context->source2, 1000);
298 return 1;
299 }
300
301 static int
timer_update_callback_2(void * data)302 timer_update_callback_2(void *data)
303 {
304 struct timer_update_context *context = data;
305
306 context->count++;
307 wl_event_source_timer_update(context->source1, 1000);
308 return 1;
309 }
310
TEST(event_loop_timer_updates)311 TEST(event_loop_timer_updates)
312 {
313 struct wl_event_loop *loop = wl_event_loop_create();
314 struct timer_update_context context;
315 struct timeval start_time, end_time, interval;
316
317 /* Create two timers that should expire at the same time (after 10ms).
318 * The first timer to receive its expiry callback updates the other timer
319 * with a much larger timeout (1s). This highlights a bug where
320 * wl_event_source_timer_dispatch would block for this larger timeout
321 * when reading from the timer fd, before calling the second timer's
322 * callback.
323 */
324
325 context.source1 = wl_event_loop_add_timer(loop, timer_update_callback_1,
326 &context);
327 assert(context.source1);
328 assert(wl_event_source_timer_update(context.source1, 10) == 0);
329
330 context.source2 = wl_event_loop_add_timer(loop, timer_update_callback_2,
331 &context);
332 assert(context.source2);
333 assert(wl_event_source_timer_update(context.source2, 10) == 0);
334
335 context.count = 0;
336
337 /* Since calling the functions between source2's update and
338 * wl_event_loop_dispatch() takes some time, it may happen
339 * that only one timer expires until we call epoll_wait.
340 * This naturally means that only one source is dispatched
341 * and the test fails. To fix that, sleep 15 ms before
342 * calling wl_event_loop_dispatch(). That should be enough
343 * for the second timer to expire.
344 *
345 * https://bugs.freedesktop.org/show_bug.cgi?id=80594
346 */
347 usleep(MSEC_TO_USEC(15));
348
349 gettimeofday(&start_time, NULL);
350 wl_event_loop_dispatch(loop, 20);
351 gettimeofday(&end_time, NULL);
352
353 assert(context.count == 2);
354
355 /* Dispatching the events should not have taken much more than 20ms,
356 * since this is the timeout passed to wl_event_loop_dispatch. If it
357 * blocked, then it will have taken over 1s.
358 * Of course, it could take over 1s anyway on a very slow or heavily
359 * loaded system, so this test isn't 100% perfect.
360 */
361
362 timersub(&end_time, &start_time, &interval);
363 assert(interval.tv_sec < 1);
364
365 wl_event_source_remove(context.source1);
366 wl_event_source_remove(context.source2);
367 wl_event_loop_destroy(loop);
368 }
369
370 struct timer_order_data {
371 struct wl_event_source *source;
372 int *last_number;
373 int number;
374 };
375
376 static int
timer_order_callback(void * data)377 timer_order_callback(void *data)
378 {
379 struct timer_order_data *tod = data;
380
381 /* Check that the timers have the correct sequence */
382 assert(tod->number == *tod->last_number + 2);
383 *tod->last_number = tod->number;
384 return 0;
385 }
386
TEST(event_loop_timer_order)387 TEST(event_loop_timer_order)
388 {
389 struct wl_event_loop *loop = wl_event_loop_create();
390 struct timer_order_data order[20];
391 int i, j;
392 int last = -1;
393
394 /* Configure a set of timers so that only timers 1, 3, 5, ..., 19
395 * (in that order) will be dispatched when the event loop is run */
396
397 for (i = 0; i < 20; i++) {
398 order[i].number = i;
399 order[i].last_number = &last;
400 order[i].source =
401 wl_event_loop_add_timer(loop, timer_order_callback,
402 &order[i]);
403 assert(order[i].source);
404 assert(wl_event_source_timer_update(order[i].source, 10) == 0);
405 }
406
407 for (i = 0; i < 20; i++) {
408 /* Permute the order in which timers are updated, so as to
409 * more exhaustively test the underlying priority queue code */
410 j = ((i + 3) * 17) % 20;
411 assert(wl_event_source_timer_update(order[j].source, j) == 0);
412 }
413 for (i = 0; i < 20; i += 2) {
414 assert(wl_event_source_timer_update(order[i].source, 0) == 0);
415 }
416
417 /* Wait until all timers are due */
418 usleep(MSEC_TO_USEC(21));
419 wl_event_loop_dispatch(loop, 0);
420 assert(last == 19);
421
422 for (i = 0; i < 20; i++) {
423 wl_event_source_remove(order[i].source);
424 }
425 wl_event_loop_destroy(loop);
426 }
427
428 struct timer_cancel_context {
429 struct wl_event_source *timers[4];
430 struct timer_cancel_context *back_refs[4];
431 int order[4];
432 int called, first;
433 };
434
435 static int
timer_cancel_callback(void * data)436 timer_cancel_callback(void *data) {
437 struct timer_cancel_context **context_ref = data;
438 struct timer_cancel_context *context = *context_ref;
439 int i = (int)(context_ref - context->back_refs);
440
441 context->called++;
442 context->order[i] = context->called;
443
444 if (context->called == 1) {
445 context->first = i;
446 /* Removing a timer always prevents its callback from
447 * being called ... */
448 wl_event_source_remove(context->timers[(i + 1) % 4]);
449 /* ... but disarming or rescheduling a timer does not,
450 * (in the case where the modified timers had already expired
451 * as of when `wl_event_loop_dispatch` was called.) */
452 assert(wl_event_source_timer_update(context->timers[(i + 2) % 4],
453 0) == 0);
454 assert(wl_event_source_timer_update(context->timers[(i + 3) % 4],
455 2000000000) == 0);
456 }
457
458 return 0;
459 }
460
TEST(event_loop_timer_cancellation)461 TEST(event_loop_timer_cancellation)
462 {
463 struct wl_event_loop *loop = wl_event_loop_create();
464 struct timer_cancel_context context;
465 int i;
466
467 memset(&context, 0, sizeof(context));
468
469 /* Test that when multiple timers are dispatched in a single call
470 * of `wl_event_loop_dispatch`, that having some timers run code
471 * to modify the other timers only actually prevents the other timers
472 * from running their callbacks when the those timers are removed, not
473 * when they are disarmed or rescheduled. */
474
475 for (i = 0; i < 4; i++) {
476 context.back_refs[i] = &context;
477 context.timers[i] =
478 wl_event_loop_add_timer(loop, timer_cancel_callback,
479 &context.back_refs[i]);
480 assert(context.timers[i]);
481
482 assert(wl_event_source_timer_update(context.timers[i], 1) == 0);
483 }
484
485 usleep(MSEC_TO_USEC(2));
486 assert(wl_event_loop_dispatch(loop, 0) == 0);
487
488 /* Tracking which timer was first makes this test independent of the
489 * actual timer dispatch order, which is not guaranteed by the docs */
490 assert(context.order[context.first] == 1);
491 assert(context.order[(context.first + 1) % 4] == 0);
492 assert(context.order[(context.first + 2) % 4] > 1);
493 assert(context.order[(context.first + 3) % 4] > 1);
494
495 wl_event_source_remove(context.timers[context.first]);
496 wl_event_source_remove(context.timers[(context.first + 2) % 4]);
497 wl_event_source_remove(context.timers[(context.first + 3) % 4]);
498
499 wl_event_loop_destroy(loop);
500 }
501
502 struct event_loop_destroy_listener {
503 struct wl_listener listener;
504 int done;
505 };
506
507 static void
event_loop_destroy_notify(struct wl_listener * l,void * data)508 event_loop_destroy_notify(struct wl_listener *l, void *data)
509 {
510 struct event_loop_destroy_listener *listener =
511 wl_container_of(l, listener, listener);
512
513 listener->done = 1;
514 }
515
TEST(event_loop_destroy)516 TEST(event_loop_destroy)
517 {
518 struct wl_event_loop *loop;
519 struct wl_display * display;
520 struct event_loop_destroy_listener a, b;
521
522 loop = wl_event_loop_create();
523 assert(loop);
524
525 a.listener.notify = &event_loop_destroy_notify;
526 a.done = 0;
527 wl_event_loop_add_destroy_listener(loop, &a.listener);
528
529 assert(wl_event_loop_get_destroy_listener(loop,
530 event_loop_destroy_notify) == &a.listener);
531
532 b.listener.notify = &event_loop_destroy_notify;
533 b.done = 0;
534 wl_event_loop_add_destroy_listener(loop, &b.listener);
535
536 wl_list_remove(&a.listener.link);
537 wl_event_loop_destroy(loop);
538
539 assert(!a.done);
540 assert(b.done);
541
542 /* Test to make sure it gets fired on display destruction */
543 display = wl_display_create();
544 assert(display);
545 loop = wl_display_get_event_loop(display);
546 assert(loop);
547
548 a.done = 0;
549 wl_event_loop_add_destroy_listener(loop, &a.listener);
550
551 wl_display_destroy(display);
552
553 assert(a.done);
554 }
555
556