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);
256 g_assert (data == g_thread_self ());
264 call_func (gpointer data)
266 func (g_thread_self ());
268 return G_SOURCE_REMOVE;
273 static gboolean thread_ready;
276 thread_func (gpointer data)
278 GMainContext *ctx = data;
281 g_main_context_push_thread_default (ctx);
283 g_mutex_lock (&mutex);
285 g_cond_signal (&cond);
286 g_mutex_unlock (&mutex);
288 source = g_timeout_source_new (500);
289 g_source_set_callback (source, (GSourceFunc)g_thread_exit, NULL, NULL);
290 g_source_attach (source, ctx);
291 g_source_unref (source);
294 g_main_context_iteration (ctx, TRUE);
307 /* this one gets invoked directly */
308 g_main_context_invoke (NULL, func, g_thread_self ());
309 g_assert_cmpint (count, ==, 1);
311 /* invoking out of an idle works too */
312 g_idle_add (call_func, NULL);
313 g_main_context_iteration (g_main_context_default (), FALSE);
314 g_assert_cmpint (count, ==, 2);
316 /* test thread-default forcing the invocation to go
319 ctx = g_main_context_new ();
320 thread = g_thread_new ("worker", thread_func, ctx);
322 g_mutex_lock (&mutex);
323 while (!thread_ready)
324 g_cond_wait (&cond, &mutex);
325 g_mutex_unlock (&mutex);
327 g_main_context_invoke (ctx, func, thread);
329 g_thread_join (thread);
330 g_assert_cmpint (count, ==, 3);
334 quit_loop (gpointer data)
336 GMainLoop *loop = data;
338 g_main_loop_quit (loop);
340 return G_SOURCE_REMOVE;
344 run_inner_loop (gpointer user_data)
346 GMainContext *ctx = user_data;
352 inner = g_main_loop_new (ctx, FALSE);
353 timeout = g_timeout_source_new (100);
354 g_source_set_callback (timeout, quit_loop, inner, NULL);
355 g_source_attach (timeout, ctx);
357 g_main_loop_run (inner);
358 g_main_loop_unref (inner);
360 return G_SOURCE_CONTINUE;
364 test_child_sources (void)
368 GSource *parent, *child_b, *child_c, *end;
370 ctx = g_main_context_new ();
371 loop = g_main_loop_new (ctx, FALSE);
375 parent = g_timeout_source_new (2000);
376 g_source_set_callback (parent, run_inner_loop, ctx, NULL);
377 g_source_set_priority (parent, G_PRIORITY_LOW);
378 g_source_attach (parent, ctx);
380 child_b = g_timeout_source_new (250);
381 g_source_set_callback (child_b, count_calls, &b, NULL);
382 g_source_add_child_source (parent, child_b);
384 child_c = g_timeout_source_new (330);
385 g_source_set_callback (child_c, count_calls, &c, NULL);
386 g_source_set_priority (child_c, G_PRIORITY_HIGH);
387 g_source_add_child_source (parent, child_c);
389 /* Child sources always have the priority of the parent */
390 g_assert_cmpint (g_source_get_priority (parent), ==, G_PRIORITY_LOW);
391 g_assert_cmpint (g_source_get_priority (child_b), ==, G_PRIORITY_LOW);
392 g_assert_cmpint (g_source_get_priority (child_c), ==, G_PRIORITY_LOW);
393 g_source_set_priority (parent, G_PRIORITY_DEFAULT);
394 g_assert_cmpint (g_source_get_priority (parent), ==, G_PRIORITY_DEFAULT);
395 g_assert_cmpint (g_source_get_priority (child_b), ==, G_PRIORITY_DEFAULT);
396 g_assert_cmpint (g_source_get_priority (child_c), ==, G_PRIORITY_DEFAULT);
398 end = g_timeout_source_new (1050);
399 g_source_set_callback (end, quit_loop, loop, NULL);
400 g_source_attach (end, ctx);
401 g_source_unref (end);
403 g_main_loop_run (loop);
405 /* The parent source's own timeout will never trigger, so "a" will
406 * only get incremented when "b" or "c" does. And when timeouts get
407 * blocked, they still wait the full interval next time rather than
408 * "catching up". So the timing is:
410 * 250 - b++ -> a++, run_inner_loop
411 * 330 - (c is blocked)
412 * 350 - inner_loop ends
413 * 350 - c++ belatedly -> a++, run_inner_loop
414 * 450 - inner loop ends
415 * 500 - b++ -> a++, run_inner_loop
416 * 600 - inner_loop ends
417 * 680 - c++ -> a++, run_inner_loop
418 * 750 - (b is blocked)
419 * 780 - inner loop ends
420 * 780 - b++ belatedly -> a++, run_inner_loop
421 * 880 - inner loop ends
422 * 1010 - c++ -> a++, run_inner_loop
423 * 1030 - (b is blocked)
424 * 1050 - end runs, quits outer loop, which has no effect yet
425 * 1110 - inner loop ends, a returns, outer loop exits
428 g_assert_cmpint (a, ==, 6);
429 g_assert_cmpint (b, ==, 3);
430 g_assert_cmpint (c, ==, 3);
432 g_source_unref (parent);
433 g_source_unref (child_b);
434 g_source_unref (child_c);
436 g_main_loop_unref (loop);
437 g_main_context_unref (ctx);
441 test_recursive_child_sources (void)
445 GSource *parent, *child_b, *child_c, *end;
447 ctx = g_main_context_new ();
448 loop = g_main_loop_new (ctx, FALSE);
452 parent = g_timeout_source_new (500);
453 g_source_set_callback (parent, count_calls, &a, NULL);
455 child_b = g_timeout_source_new (220);
456 g_source_set_callback (child_b, count_calls, &b, NULL);
457 g_source_add_child_source (parent, child_b);
459 child_c = g_timeout_source_new (430);
460 g_source_set_callback (child_c, count_calls, &c, NULL);
461 g_source_add_child_source (child_b, child_c);
463 g_source_attach (parent, ctx);
465 end = g_timeout_source_new (2010);
466 g_source_set_callback (end, (GSourceFunc)g_main_loop_quit, loop, NULL);
467 g_source_attach (end, ctx);
468 g_source_unref (end);
470 g_main_loop_run (loop);
472 /* Sequence of events:
473 * 220 b (b = 440, a = 720)
474 * 430 c (c = 860, b = 650, a = 930)
475 * 650 b (b = 870, a = 1150)
476 * 860 c (c = 1290, b = 1080, a = 1360)
477 * 1080 b (b = 1300, a = 1580)
478 * 1290 c (c = 1720, b = 1510, a = 1790)
479 * 1510 b (b = 1730, a = 2010)
480 * 1720 c (c = 2150, b = 1940, a = 2220)
481 * 1940 b (b = 2160, a = 2440)
484 g_assert_cmpint (a, ==, 9);
485 g_assert_cmpint (b, ==, 9);
486 g_assert_cmpint (c, ==, 4);
488 g_source_unref (parent);
489 g_source_unref (child_b);
490 g_source_unref (child_c);
492 g_main_loop_unref (loop);
493 g_main_context_unref (ctx);
497 GSource *parent, *old_child, *new_child;
502 swap_sources (gpointer user_data)
504 SwappingTestData *data = user_data;
508 g_source_remove_child_source (data->parent, data->old_child);
509 g_clear_pointer (&data->old_child, g_source_unref);
512 if (!data->new_child)
514 data->new_child = g_timeout_source_new (0);
515 g_source_set_callback (data->new_child, quit_loop, data->loop, NULL);
516 g_source_add_child_source (data->parent, data->new_child);
519 return G_SOURCE_CONTINUE;
523 assert_not_reached_callback (gpointer user_data)
525 g_assert_not_reached ();
527 return G_SOURCE_REMOVE;
531 test_swapping_child_sources (void)
535 SwappingTestData data;
537 ctx = g_main_context_new ();
538 loop = g_main_loop_new (ctx, FALSE);
540 data.parent = g_timeout_source_new (50);
542 g_source_set_callback (data.parent, swap_sources, &data, NULL);
543 g_source_attach (data.parent, ctx);
545 data.old_child = g_timeout_source_new (100);
546 g_source_add_child_source (data.parent, data.old_child);
547 g_source_set_callback (data.old_child, assert_not_reached_callback, NULL, NULL);
549 data.new_child = NULL;
550 g_main_loop_run (loop);
552 g_source_destroy (data.parent);
553 g_source_unref (data.parent);
554 g_source_unref (data.new_child);
556 g_main_loop_unref (loop);
557 g_main_context_unref (ctx);
564 GSource *timeout1, *timeout2;
569 timeout1_callback (gpointer user_data)
571 TimeTestData *data = user_data;
573 gint64 mtime1, mtime2, time2;
575 source = g_main_current_source ();
576 g_assert (source == data->timeout1);
578 if (data->time1 == -1)
580 /* First iteration */
581 g_assert (!g_source_is_destroyed (data->timeout2));
583 mtime1 = g_get_monotonic_time ();
584 data->time1 = g_source_get_time (source);
586 /* g_source_get_time() does not change during a single callback */
588 mtime2 = g_get_monotonic_time ();
589 time2 = g_source_get_time (source);
591 g_assert_cmpint (mtime1, <, mtime2);
592 g_assert_cmpint (data->time1, ==, time2);
596 /* Second iteration */
597 g_assert (g_source_is_destroyed (data->timeout2));
599 /* g_source_get_time() MAY change between iterations; in this
600 * case we know for sure that it did because of the g_usleep()
603 time2 = g_source_get_time (source);
604 g_assert_cmpint (data->time1, <, time2);
606 g_main_loop_quit (data->loop);
613 timeout2_callback (gpointer user_data)
615 TimeTestData *data = user_data;
619 source = g_main_current_source ();
620 g_assert (source == data->timeout2);
622 g_assert (!g_source_is_destroyed (data->timeout1));
624 /* g_source_get_time() does not change between different sources in
625 * a single iteration of the mainloop.
627 time2 = g_source_get_time (source);
628 g_assert_cmpint (data->time1, ==, time2);
630 /* The source should still have a valid time even after being
631 * destroyed, since it's currently running.
633 g_source_destroy (source);
634 time3 = g_source_get_time (source);
635 g_assert_cmpint (time2, ==, time3);
641 test_source_time (void)
645 data.ctx = g_main_context_new ();
646 data.loop = g_main_loop_new (data.ctx, FALSE);
648 data.timeout1 = g_timeout_source_new (0);
649 g_source_set_callback (data.timeout1, timeout1_callback, &data, NULL);
650 g_source_attach (data.timeout1, data.ctx);
652 data.timeout2 = g_timeout_source_new (0);
653 g_source_set_callback (data.timeout2, timeout2_callback, &data, NULL);
654 g_source_attach (data.timeout2, data.ctx);
658 g_main_loop_run (data.loop);
660 g_assert (!g_source_is_destroyed (data.timeout1));
661 g_assert (g_source_is_destroyed (data.timeout2));
663 g_source_destroy (data.timeout1);
664 g_source_unref (data.timeout1);
665 g_source_unref (data.timeout2);
667 g_main_loop_unref (data.loop);
668 g_main_context_unref (data.ctx);
672 main (int argc, char *argv[])
674 g_test_init (&argc, &argv, NULL);
676 g_test_add_func ("/maincontext/basic", test_maincontext_basic);
677 g_test_add_func ("/mainloop/basic", test_mainloop_basic);
678 g_test_add_func ("/mainloop/timeouts", test_timeouts);
679 g_test_add_func ("/mainloop/priorities", test_priorities);
680 g_test_add_func ("/mainloop/invoke", test_invoke);
681 g_test_add_func ("/mainloop/child_sources", test_child_sources);
682 g_test_add_func ("/mainloop/recursive_child_sources", test_recursive_child_sources);
683 g_test_add_func ("/mainloop/swapping_child_sources", test_swapping_child_sources);
684 g_test_add_func ("/mainloop/source_time", test_source_time);
686 return g_test_run ();