1 /* Unit tests for GMainLoop
2 * Copyright (C) 2011 Red Hat, Inc
3 * Author: Matthias Clasen
5 * This work is provided "as is"; redistribution and modification
6 * in whole or in part, in any medium, physical or electronic is
7 * permitted without restriction.
9 * This work is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * In no event shall the authors or contributors be liable for any
14 * direct, indirect, incidental, special, exemplary, or consequential
15 * damages (including, but not limited to, procurement of substitute
16 * goods or services; loss of use, data, or profits; or business
17 * interruption) however caused and on any theory of liability, whether
18 * in contract, strict liability, or tort (including negligence or
19 * otherwise) arising in any way out of the use of this software, even
20 * if advised of the possibility of such damage.
25 static gboolean cb (gpointer data)
30 static gboolean prepare (GSource *source, gint *time)
34 static gboolean check (GSource *source)
38 static gboolean dispatch (GSource *source, GSourceFunc cb, gpointer date)
43 GSourceFuncs funcs = {
51 test_maincontext_basic (void)
56 gpointer data = &funcs;
58 ctx = g_main_context_new ();
60 g_assert (!g_main_context_pending (ctx));
61 g_assert (!g_main_context_iteration (ctx, FALSE));
63 source = g_source_new (&funcs, sizeof (GSource));
64 g_assert_cmpint (g_source_get_priority (source), ==, G_PRIORITY_DEFAULT);
65 g_assert (!g_source_is_destroyed (source));
67 g_assert (!g_source_get_can_recurse (source));
68 g_assert (g_source_get_name (source) == NULL);
70 g_source_set_can_recurse (source, TRUE);
71 g_source_set_name (source, "d");
73 g_assert (g_source_get_can_recurse (source));
74 g_assert_cmpstr (g_source_get_name (source), ==, "d");
76 g_assert (g_main_context_find_source_by_user_data (ctx, NULL) == NULL);
77 g_assert (g_main_context_find_source_by_funcs_user_data (ctx, &funcs, NULL) == NULL);
79 id = g_source_attach (source, ctx);
80 g_assert_cmpint (g_source_get_id (source), ==, id);
81 g_assert (g_main_context_find_source_by_id (ctx, id) == source);
83 g_source_set_priority (source, G_PRIORITY_HIGH);
84 g_assert_cmpint (g_source_get_priority (source), ==, G_PRIORITY_HIGH);
86 g_source_destroy (source);
87 g_assert (g_source_get_context (source) == ctx);
88 g_assert (g_main_context_find_source_by_id (ctx, id) == NULL);
90 g_main_context_unref (ctx);
92 if (g_test_undefined ())
94 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
95 "*assertion*source->context != NULL*failed*");
96 g_assert (g_source_get_context (source) == NULL);
97 g_test_assert_expected_messages ();
100 g_source_unref (source);
102 ctx = g_main_context_default ();
103 source = g_source_new (&funcs, sizeof (GSource));
104 g_source_set_funcs (source, &funcs);
105 g_source_set_callback (source, cb, data, NULL);
106 id = g_source_attach (source, ctx);
107 g_source_unref (source);
108 g_source_set_name_by_id (id, "e");
109 g_assert_cmpstr (g_source_get_name (source), ==, "e");
110 g_assert (g_source_get_context (source) == ctx);
111 g_assert (g_source_remove_by_funcs_user_data (&funcs, data));
113 source = g_source_new (&funcs, sizeof (GSource));
114 g_source_set_funcs (source, &funcs);
115 g_source_set_callback (source, cb, data, NULL);
116 id = g_source_attach (source, ctx);
117 g_source_unref (source);
118 g_assert (g_source_remove_by_user_data (data));
120 g_idle_add (cb, data);
121 g_assert (g_idle_remove_by_data (data));
125 test_mainloop_basic (void)
130 loop = g_main_loop_new (NULL, FALSE);
132 g_assert (!g_main_loop_is_running (loop));
134 g_main_loop_ref (loop);
136 ctx = g_main_loop_get_context (loop);
137 g_assert (ctx == g_main_context_default ());
139 g_main_loop_unref (loop);
141 g_assert_cmpint (g_main_depth (), ==, 0);
143 g_main_loop_unref (loop);
151 count_calls (gpointer data)
169 ctx = g_main_context_new ();
170 loop = g_main_loop_new (ctx, FALSE);
172 source = g_timeout_source_new (100);
173 g_source_set_callback (source, count_calls, &a, NULL);
174 g_source_attach (source, ctx);
175 g_source_unref (source);
177 source = g_timeout_source_new (250);
178 g_source_set_callback (source, count_calls, &b, NULL);
179 g_source_attach (source, ctx);
180 g_source_unref (source);
182 source = g_timeout_source_new (330);
183 g_source_set_callback (source, count_calls, &c, NULL);
184 g_source_attach (source, ctx);
185 g_source_unref (source);
187 source = g_timeout_source_new (1050);
188 g_source_set_callback (source, (GSourceFunc)g_main_loop_quit, loop, NULL);
189 g_source_attach (source, ctx);
190 g_source_unref (source);
192 g_main_loop_run (loop);
194 /* this is a race condition; under some circumstances we might not get 10
195 * 100ms runs in 1050 ms, so consider 9 as "close enough" */
196 g_assert_cmpint (a, >=, 9);
197 g_assert_cmpint (a, <=, 10);
198 g_assert_cmpint (b, ==, 4);
199 g_assert_cmpint (c, ==, 3);
201 g_main_loop_unref (loop);
202 g_main_context_unref (ctx);
206 test_priorities (void)
214 ctx = g_main_context_new ();
216 sourcea = g_idle_source_new ();
217 g_source_set_callback (sourcea, count_calls, &a, NULL);
218 g_source_set_priority (sourcea, 1);
219 g_source_attach (sourcea, ctx);
220 g_source_unref (sourcea);
222 sourceb = g_idle_source_new ();
223 g_source_set_callback (sourceb, count_calls, &b, NULL);
224 g_source_set_priority (sourceb, 0);
225 g_source_attach (sourceb, ctx);
226 g_source_unref (sourceb);
228 g_assert (g_main_context_pending (ctx));
229 g_assert (g_main_context_iteration (ctx, FALSE));
230 g_assert_cmpint (a, ==, 0);
231 g_assert_cmpint (b, ==, 1);
233 g_assert (g_main_context_iteration (ctx, FALSE));
234 g_assert_cmpint (a, ==, 0);
235 g_assert_cmpint (b, ==, 2);
237 g_source_destroy (sourceb);
239 g_assert (g_main_context_iteration (ctx, FALSE));
240 g_assert_cmpint (a, ==, 1);
241 g_assert_cmpint (b, ==, 2);
243 g_assert (g_main_context_pending (ctx));
244 g_source_destroy (sourcea);
245 g_assert (!g_main_context_pending (ctx));
247 g_main_context_unref (ctx);
251 quit_loop (gpointer data)
253 GMainLoop *loop = data;
255 g_main_loop_quit (loop);
257 return G_SOURCE_REMOVE;
266 g_assert (data == g_thread_self ());
274 call_func (gpointer data)
276 func (g_thread_self ());
278 return G_SOURCE_REMOVE;
283 static gboolean thread_ready;
286 thread_func (gpointer data)
288 GMainContext *ctx = data;
292 g_main_context_push_thread_default (ctx);
293 loop = g_main_loop_new (ctx, FALSE);
295 g_mutex_lock (&mutex);
297 g_cond_signal (&cond);
298 g_mutex_unlock (&mutex);
300 source = g_timeout_source_new (500);
301 g_source_set_callback (source, quit_loop, loop, NULL);
302 g_source_attach (source, ctx);
303 g_source_unref (source);
305 g_main_loop_run (loop);
307 g_main_context_pop_thread_default (ctx);
308 g_main_loop_unref (loop);
321 /* this one gets invoked directly */
322 g_main_context_invoke (NULL, func, g_thread_self ());
323 g_assert_cmpint (count, ==, 1);
325 /* invoking out of an idle works too */
326 g_idle_add (call_func, NULL);
327 g_main_context_iteration (g_main_context_default (), FALSE);
328 g_assert_cmpint (count, ==, 2);
330 /* test thread-default forcing the invocation to go
333 ctx = g_main_context_new ();
334 thread = g_thread_new ("worker", thread_func, ctx);
336 g_mutex_lock (&mutex);
337 while (!thread_ready)
338 g_cond_wait (&cond, &mutex);
339 g_mutex_unlock (&mutex);
341 g_main_context_invoke (ctx, func, thread);
343 g_thread_join (thread);
344 g_assert_cmpint (count, ==, 3);
346 g_main_context_unref (ctx);
350 run_inner_loop (gpointer user_data)
352 GMainContext *ctx = user_data;
358 inner = g_main_loop_new (ctx, FALSE);
359 timeout = g_timeout_source_new (100);
360 g_source_set_callback (timeout, quit_loop, inner, NULL);
361 g_source_attach (timeout, ctx);
362 g_source_unref (timeout);
364 g_main_loop_run (inner);
365 g_main_loop_unref (inner);
367 return G_SOURCE_CONTINUE;
371 test_child_sources (void)
375 GSource *parent, *child_b, *child_c, *end;
377 ctx = g_main_context_new ();
378 loop = g_main_loop_new (ctx, FALSE);
382 parent = g_timeout_source_new (2000);
383 g_source_set_callback (parent, run_inner_loop, ctx, NULL);
384 g_source_set_priority (parent, G_PRIORITY_LOW);
385 g_source_attach (parent, ctx);
387 child_b = g_timeout_source_new (250);
388 g_source_set_callback (child_b, count_calls, &b, NULL);
389 g_source_add_child_source (parent, child_b);
391 child_c = g_timeout_source_new (330);
392 g_source_set_callback (child_c, count_calls, &c, NULL);
393 g_source_set_priority (child_c, G_PRIORITY_HIGH);
394 g_source_add_child_source (parent, child_c);
396 /* Child sources always have the priority of the parent */
397 g_assert_cmpint (g_source_get_priority (parent), ==, G_PRIORITY_LOW);
398 g_assert_cmpint (g_source_get_priority (child_b), ==, G_PRIORITY_LOW);
399 g_assert_cmpint (g_source_get_priority (child_c), ==, G_PRIORITY_LOW);
400 g_source_set_priority (parent, G_PRIORITY_DEFAULT);
401 g_assert_cmpint (g_source_get_priority (parent), ==, G_PRIORITY_DEFAULT);
402 g_assert_cmpint (g_source_get_priority (child_b), ==, G_PRIORITY_DEFAULT);
403 g_assert_cmpint (g_source_get_priority (child_c), ==, G_PRIORITY_DEFAULT);
405 end = g_timeout_source_new (1050);
406 g_source_set_callback (end, quit_loop, loop, NULL);
407 g_source_attach (end, ctx);
408 g_source_unref (end);
410 g_main_loop_run (loop);
412 /* The parent source's own timeout will never trigger, so "a" will
413 * only get incremented when "b" or "c" does. And when timeouts get
414 * blocked, they still wait the full interval next time rather than
415 * "catching up". So the timing is:
417 * 250 - b++ -> a++, run_inner_loop
418 * 330 - (c is blocked)
419 * 350 - inner_loop ends
420 * 350 - c++ belatedly -> a++, run_inner_loop
421 * 450 - inner loop ends
422 * 500 - b++ -> a++, run_inner_loop
423 * 600 - inner_loop ends
424 * 680 - c++ -> a++, run_inner_loop
425 * 750 - (b is blocked)
426 * 780 - inner loop ends
427 * 780 - b++ belatedly -> a++, run_inner_loop
428 * 880 - inner loop ends
429 * 1010 - c++ -> a++, run_inner_loop
430 * 1030 - (b is blocked)
431 * 1050 - end runs, quits outer loop, which has no effect yet
432 * 1110 - inner loop ends, a returns, outer loop exits
435 g_assert_cmpint (a, ==, 6);
436 g_assert_cmpint (b, ==, 3);
437 g_assert_cmpint (c, ==, 3);
439 g_source_destroy (parent);
440 g_source_unref (parent);
441 g_source_unref (child_b);
442 g_source_unref (child_c);
444 g_main_loop_unref (loop);
445 g_main_context_unref (ctx);
449 test_recursive_child_sources (void)
453 GSource *parent, *child_b, *child_c, *end;
455 ctx = g_main_context_new ();
456 loop = g_main_loop_new (ctx, FALSE);
460 parent = g_timeout_source_new (500);
461 g_source_set_callback (parent, count_calls, &a, NULL);
463 child_b = g_timeout_source_new (220);
464 g_source_set_callback (child_b, count_calls, &b, NULL);
465 g_source_add_child_source (parent, child_b);
467 child_c = g_timeout_source_new (430);
468 g_source_set_callback (child_c, count_calls, &c, NULL);
469 g_source_add_child_source (child_b, child_c);
471 g_source_attach (parent, ctx);
473 end = g_timeout_source_new (2010);
474 g_source_set_callback (end, (GSourceFunc)g_main_loop_quit, loop, NULL);
475 g_source_attach (end, ctx);
476 g_source_unref (end);
478 g_main_loop_run (loop);
480 /* Sequence of events:
481 * 220 b (b = 440, a = 720)
482 * 430 c (c = 860, b = 650, a = 930)
483 * 650 b (b = 870, a = 1150)
484 * 860 c (c = 1290, b = 1080, a = 1360)
485 * 1080 b (b = 1300, a = 1580)
486 * 1290 c (c = 1720, b = 1510, a = 1790)
487 * 1510 b (b = 1730, a = 2010)
488 * 1720 c (c = 2150, b = 1940, a = 2220)
489 * 1940 b (b = 2160, a = 2440)
492 g_assert_cmpint (a, ==, 9);
493 g_assert_cmpint (b, ==, 9);
494 g_assert_cmpint (c, ==, 4);
496 g_source_destroy (parent);
497 g_source_unref (parent);
498 g_source_unref (child_b);
499 g_source_unref (child_c);
501 g_main_loop_unref (loop);
502 g_main_context_unref (ctx);
506 GSource *parent, *old_child, *new_child;
511 swap_sources (gpointer user_data)
513 SwappingTestData *data = user_data;
517 g_source_remove_child_source (data->parent, data->old_child);
518 g_clear_pointer (&data->old_child, g_source_unref);
521 if (!data->new_child)
523 data->new_child = g_timeout_source_new (0);
524 g_source_set_callback (data->new_child, quit_loop, data->loop, NULL);
525 g_source_add_child_source (data->parent, data->new_child);
528 return G_SOURCE_CONTINUE;
532 assert_not_reached_callback (gpointer user_data)
534 g_assert_not_reached ();
536 return G_SOURCE_REMOVE;
540 test_swapping_child_sources (void)
544 SwappingTestData data;
546 ctx = g_main_context_new ();
547 loop = g_main_loop_new (ctx, FALSE);
549 data.parent = g_timeout_source_new (50);
551 g_source_set_callback (data.parent, swap_sources, &data, NULL);
552 g_source_attach (data.parent, ctx);
554 data.old_child = g_timeout_source_new (100);
555 g_source_add_child_source (data.parent, data.old_child);
556 g_source_set_callback (data.old_child, assert_not_reached_callback, NULL, NULL);
558 data.new_child = NULL;
559 g_main_loop_run (loop);
561 g_source_destroy (data.parent);
562 g_source_unref (data.parent);
563 g_source_unref (data.new_child);
565 g_main_loop_unref (loop);
566 g_main_context_unref (ctx);
573 GSource *timeout1, *timeout2;
578 timeout1_callback (gpointer user_data)
580 TimeTestData *data = user_data;
582 gint64 mtime1, mtime2, time2;
584 source = g_main_current_source ();
585 g_assert (source == data->timeout1);
587 if (data->time1 == -1)
589 /* First iteration */
590 g_assert (!g_source_is_destroyed (data->timeout2));
592 mtime1 = g_get_monotonic_time ();
593 data->time1 = g_source_get_time (source);
595 /* g_source_get_time() does not change during a single callback */
597 mtime2 = g_get_monotonic_time ();
598 time2 = g_source_get_time (source);
600 g_assert_cmpint (mtime1, <, mtime2);
601 g_assert_cmpint (data->time1, ==, time2);
605 /* Second iteration */
606 g_assert (g_source_is_destroyed (data->timeout2));
608 /* g_source_get_time() MAY change between iterations; in this
609 * case we know for sure that it did because of the g_usleep()
612 time2 = g_source_get_time (source);
613 g_assert_cmpint (data->time1, <, time2);
615 g_main_loop_quit (data->loop);
622 timeout2_callback (gpointer user_data)
624 TimeTestData *data = user_data;
628 source = g_main_current_source ();
629 g_assert (source == data->timeout2);
631 g_assert (!g_source_is_destroyed (data->timeout1));
633 /* g_source_get_time() does not change between different sources in
634 * a single iteration of the mainloop.
636 time2 = g_source_get_time (source);
637 g_assert_cmpint (data->time1, ==, time2);
639 /* The source should still have a valid time even after being
640 * destroyed, since it's currently running.
642 g_source_destroy (source);
643 time3 = g_source_get_time (source);
644 g_assert_cmpint (time2, ==, time3);
650 test_source_time (void)
654 data.ctx = g_main_context_new ();
655 data.loop = g_main_loop_new (data.ctx, FALSE);
657 data.timeout1 = g_timeout_source_new (0);
658 g_source_set_callback (data.timeout1, timeout1_callback, &data, NULL);
659 g_source_attach (data.timeout1, data.ctx);
661 data.timeout2 = g_timeout_source_new (0);
662 g_source_set_callback (data.timeout2, timeout2_callback, &data, NULL);
663 g_source_attach (data.timeout2, data.ctx);
667 g_main_loop_run (data.loop);
669 g_assert (!g_source_is_destroyed (data.timeout1));
670 g_assert (g_source_is_destroyed (data.timeout2));
672 g_source_destroy (data.timeout1);
673 g_source_unref (data.timeout1);
674 g_source_unref (data.timeout2);
676 g_main_loop_unref (data.loop);
677 g_main_context_unref (data.ctx);
681 main (int argc, char *argv[])
683 g_test_init (&argc, &argv, NULL);
685 g_test_add_func ("/maincontext/basic", test_maincontext_basic);
686 g_test_add_func ("/mainloop/basic", test_mainloop_basic);
687 g_test_add_func ("/mainloop/timeouts", test_timeouts);
688 g_test_add_func ("/mainloop/priorities", test_priorities);
689 g_test_add_func ("/mainloop/invoke", test_invoke);
690 g_test_add_func ("/mainloop/child_sources", test_child_sources);
691 g_test_add_func ("/mainloop/recursive_child_sources", test_recursive_child_sources);
692 g_test_add_func ("/mainloop/swapping_child_sources", test_swapping_child_sources);
693 g_test_add_func ("/mainloop/source_time", test_source_time);
695 return g_test_run ();