event-loop-test: Confirm distant timers do not fire
authorManuel Stoeckl <code@mstoeckl.com>
Thu, 16 Jan 2020 13:07:41 +0000 (08:07 -0500)
committerSimon Ser <contact@emersion.fr>
Tue, 21 Jan 2020 11:31:35 +0000 (11:31 +0000)
This change expands the `event_loop_timer` test to use two different
timers with different timeouts; it now implicitly checks that e.g.
both timers do not expire at the same time, and that the first timer
expiring does not prevent the second from doing so. (While such failure
modes are unlikely with timer event sources based on individual
timerfds, they are possible when multiple timers share a common timerfd.)

Signed-off-by: Manuel Stoeckl <code@mstoeckl.com>
tests/event-loop-test.c

index 3002e6d..e9e636c 100644 (file)
@@ -230,18 +230,33 @@ timer_callback(void *data)
 TEST(event_loop_timer)
 {
        struct wl_event_loop *loop = wl_event_loop_create();
-       struct wl_event_source *source;
+       struct wl_event_source *source1, *source2;
        int got_it = 0;
 
-       source = wl_event_loop_add_timer(loop, timer_callback, &got_it);
-       assert(source);
-       wl_event_source_timer_update(source, 10);
+       source1 = wl_event_loop_add_timer(loop, timer_callback, &got_it);
+       assert(source1);
+       wl_event_source_timer_update(source1, 20);
+
+       source2 = wl_event_loop_add_timer(loop, timer_callback, &got_it);
+       assert(source2);
+       wl_event_source_timer_update(source2, 100);
+
+       /* Check that the timer marked for 20 msec from now fires within 30
+        * msec, and that the timer marked for 100 msec is expected to fire
+        * within an additional 90 msec. (Some extra wait time is provided to
+        * account for reasonable code execution / thread preemption delays.) */
+
        wl_event_loop_dispatch(loop, 0);
-       assert(!got_it);
-       wl_event_loop_dispatch(loop, 20);
+       assert(got_it == 0);
+       wl_event_loop_dispatch(loop, 30);
        assert(got_it == 1);
+       wl_event_loop_dispatch(loop, 0);
+       assert(got_it == 1);
+       wl_event_loop_dispatch(loop, 90);
+       assert(got_it == 2);
 
-       wl_event_source_remove(source);
+       wl_event_source_remove(source1);
+       wl_event_source_remove(source2);
        wl_event_loop_destroy(loop);
 }