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