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_source_unref (source);
81 g_assert_cmpint (g_source_get_id (source), ==, id);
82 g_assert (g_main_context_find_source_by_id (ctx, id) == source);
84 g_source_set_priority (source, G_PRIORITY_HIGH);
85 g_assert_cmpint (g_source_get_priority (source), ==, G_PRIORITY_HIGH);
87 g_source_destroy (source);
88 g_main_context_unref (ctx);
90 ctx = g_main_context_default ();
91 source = g_source_new (&funcs, sizeof (GSource));
92 g_source_set_funcs (source, &funcs);
93 g_source_set_callback (source, cb, data, NULL);
94 id = g_source_attach (source, ctx);
95 g_source_unref (source);
96 g_source_set_name_by_id (id, "e");
97 g_assert_cmpstr (g_source_get_name (source), ==, "e");
98 g_assert (g_source_get_context (source) == ctx);
99 g_assert (g_source_remove_by_funcs_user_data (&funcs, data));
101 source = g_source_new (&funcs, sizeof (GSource));
102 g_source_set_funcs (source, &funcs);
103 g_source_set_callback (source, cb, data, NULL);
104 id = g_source_attach (source, ctx);
105 g_source_unref (source);
106 g_assert (g_source_remove_by_user_data (data));
108 g_idle_add (cb, data);
109 g_assert (g_idle_remove_by_data (data));
113 test_mainloop_basic (void)
118 loop = g_main_loop_new (NULL, FALSE);
120 g_assert (!g_main_loop_is_running (loop));
122 g_main_loop_ref (loop);
124 ctx = g_main_loop_get_context (loop);
125 g_assert (ctx == g_main_context_default ());
127 g_main_loop_unref (loop);
129 g_assert_cmpint (g_main_depth (), ==, 0);
131 g_main_loop_unref (loop);
139 count_calls (gpointer data)
157 ctx = g_main_context_new ();
158 loop = g_main_loop_new (ctx, FALSE);
160 source = g_timeout_source_new (100);
161 g_source_set_callback (source, count_calls, &a, NULL);
162 g_source_attach (source, ctx);
163 g_source_unref (source);
165 source = g_timeout_source_new (250);
166 g_source_set_callback (source, count_calls, &b, NULL);
167 g_source_attach (source, ctx);
168 g_source_unref (source);
170 source = g_timeout_source_new (330);
171 g_source_set_callback (source, count_calls, &c, NULL);
172 g_source_attach (source, ctx);
173 g_source_unref (source);
175 source = g_timeout_source_new (1050);
176 g_source_set_callback (source, (GSourceFunc)g_main_loop_quit, loop, NULL);
177 g_source_attach (source, ctx);
178 g_source_unref (source);
180 g_main_loop_run (loop);
182 /* this is a race condition; under some circumstances we might not get 10
183 * 100ms runs in 1050 ms, so consider 9 as "close enough" */
184 g_assert_cmpint (a, >=, 9);
185 g_assert_cmpint (a, <=, 10);
186 g_assert_cmpint (b, ==, 4);
187 g_assert_cmpint (c, ==, 3);
189 g_main_loop_unref (loop);
190 g_main_context_unref (ctx);
194 test_priorities (void)
202 ctx = g_main_context_new ();
204 sourcea = g_idle_source_new ();
205 g_source_set_callback (sourcea, count_calls, &a, NULL);
206 g_source_set_priority (sourcea, 1);
207 g_source_attach (sourcea, ctx);
208 g_source_unref (sourcea);
210 sourceb = g_idle_source_new ();
211 g_source_set_callback (sourceb, count_calls, &b, NULL);
212 g_source_set_priority (sourceb, 0);
213 g_source_attach (sourceb, ctx);
214 g_source_unref (sourceb);
216 g_assert (g_main_context_pending (ctx));
217 g_assert (g_main_context_iteration (ctx, FALSE));
218 g_assert_cmpint (a, ==, 0);
219 g_assert_cmpint (b, ==, 1);
221 g_assert (g_main_context_iteration (ctx, FALSE));
222 g_assert_cmpint (a, ==, 0);
223 g_assert_cmpint (b, ==, 2);
225 g_source_destroy (sourceb);
227 g_assert (g_main_context_iteration (ctx, FALSE));
228 g_assert_cmpint (a, ==, 1);
229 g_assert_cmpint (b, ==, 2);
231 g_assert (g_main_context_pending (ctx));
232 g_source_destroy (sourcea);
233 g_assert (!g_main_context_pending (ctx));
235 g_main_context_unref (ctx);
244 g_assert (data == g_thread_self ());
252 call_func (gpointer data)
254 func (g_thread_self ());
256 return G_SOURCE_REMOVE;
261 static gboolean thread_ready;
264 thread_func (gpointer data)
266 GMainContext *ctx = data;
269 g_main_context_push_thread_default (ctx);
271 g_mutex_lock (&mutex);
273 g_cond_signal (&cond);
274 g_mutex_unlock (&mutex);
276 source = g_timeout_source_new (500);
277 g_source_set_callback (source, (GSourceFunc)g_thread_exit, NULL, NULL);
278 g_source_attach (source, ctx);
279 g_source_unref (source);
282 g_main_context_iteration (ctx, TRUE);
295 /* this one gets invoked directly */
296 g_main_context_invoke (NULL, func, g_thread_self ());
297 g_assert_cmpint (count, ==, 1);
299 /* invoking out of an idle works too */
300 g_idle_add (call_func, NULL);
301 g_main_context_iteration (g_main_context_default (), FALSE);
302 g_assert_cmpint (count, ==, 2);
304 /* test thread-default forcing the invocation to go
307 ctx = g_main_context_new ();
308 thread = g_thread_new ("worker", thread_func, ctx);
310 g_mutex_lock (&mutex);
311 while (!thread_ready)
312 g_cond_wait (&cond, &mutex);
313 g_mutex_unlock (&mutex);
315 g_main_context_invoke (ctx, func, thread);
317 g_thread_join (thread);
318 g_assert_cmpint (count, ==, 3);
322 quit_loop (gpointer data)
324 GMainLoop *loop = data;
326 g_main_loop_quit (loop);
328 return G_SOURCE_REMOVE;
332 run_inner_loop (gpointer user_data)
334 GMainContext *ctx = user_data;
340 inner = g_main_loop_new (ctx, FALSE);
341 timeout = g_timeout_source_new (100);
342 g_source_set_callback (timeout, quit_loop, inner, NULL);
343 g_source_attach (timeout, ctx);
345 g_main_loop_run (inner);
346 g_main_loop_unref (inner);
348 return G_SOURCE_CONTINUE;
352 test_child_sources (void)
356 GSource *parent, *child_b, *child_c, *end;
358 ctx = g_main_context_new ();
359 loop = g_main_loop_new (ctx, FALSE);
363 parent = g_timeout_source_new (2000);
364 g_source_set_callback (parent, run_inner_loop, ctx, NULL);
365 g_source_set_priority (parent, G_PRIORITY_LOW);
366 g_source_attach (parent, ctx);
368 child_b = g_timeout_source_new (250);
369 g_source_set_callback (child_b, count_calls, &b, NULL);
370 g_source_add_child_source (parent, child_b);
372 child_c = g_timeout_source_new (330);
373 g_source_set_callback (child_c, count_calls, &c, NULL);
374 g_source_set_priority (child_c, G_PRIORITY_HIGH);
375 g_source_add_child_source (parent, child_c);
377 /* Child sources always have the priority of the parent */
378 g_assert_cmpint (g_source_get_priority (parent), ==, G_PRIORITY_LOW);
379 g_assert_cmpint (g_source_get_priority (child_b), ==, G_PRIORITY_LOW);
380 g_assert_cmpint (g_source_get_priority (child_c), ==, G_PRIORITY_LOW);
381 g_source_set_priority (parent, G_PRIORITY_DEFAULT);
382 g_assert_cmpint (g_source_get_priority (parent), ==, G_PRIORITY_DEFAULT);
383 g_assert_cmpint (g_source_get_priority (child_b), ==, G_PRIORITY_DEFAULT);
384 g_assert_cmpint (g_source_get_priority (child_c), ==, G_PRIORITY_DEFAULT);
386 end = g_timeout_source_new (1050);
387 g_source_set_callback (end, quit_loop, loop, NULL);
388 g_source_attach (end, ctx);
389 g_source_unref (end);
391 g_main_loop_run (loop);
393 /* The parent source's own timeout will never trigger, so "a" will
394 * only get incremented when "b" or "c" does. And when timeouts get
395 * blocked, they still wait the full interval next time rather than
396 * "catching up". So the timing is:
398 * 250 - b++ -> a++, run_inner_loop
399 * 330 - (c is blocked)
400 * 350 - inner_loop ends
401 * 350 - c++ belatedly -> a++, run_inner_loop
402 * 450 - inner loop ends
403 * 500 - b++ -> a++, run_inner_loop
404 * 600 - inner_loop ends
405 * 680 - c++ -> a++, run_inner_loop
406 * 750 - (b is blocked)
407 * 780 - inner loop ends
408 * 780 - b++ belatedly -> a++, run_inner_loop
409 * 880 - inner loop ends
410 * 1010 - c++ -> a++, run_inner_loop
411 * 1030 - (b is blocked)
412 * 1050 - end runs, quits outer loop, which has no effect yet
413 * 1110 - inner loop ends, a returns, outer loop exits
416 g_assert_cmpint (a, ==, 6);
417 g_assert_cmpint (b, ==, 3);
418 g_assert_cmpint (c, ==, 3);
420 g_source_unref (parent);
421 g_source_unref (child_b);
422 g_source_unref (child_c);
424 g_main_loop_unref (loop);
425 g_main_context_unref (ctx);
429 test_recursive_child_sources (void)
433 GSource *parent, *child_b, *child_c, *end;
435 ctx = g_main_context_new ();
436 loop = g_main_loop_new (ctx, FALSE);
440 parent = g_timeout_source_new (500);
441 g_source_set_callback (parent, count_calls, &a, NULL);
443 child_b = g_timeout_source_new (220);
444 g_source_set_callback (child_b, count_calls, &b, NULL);
445 g_source_add_child_source (parent, child_b);
447 child_c = g_timeout_source_new (430);
448 g_source_set_callback (child_c, count_calls, &c, NULL);
449 g_source_add_child_source (child_b, child_c);
451 g_source_attach (parent, ctx);
453 end = g_timeout_source_new (2010);
454 g_source_set_callback (end, (GSourceFunc)g_main_loop_quit, loop, NULL);
455 g_source_attach (end, ctx);
456 g_source_unref (end);
458 g_main_loop_run (loop);
460 /* Sequence of events:
461 * 220 b (b = 440, a = 720)
462 * 430 c (c = 860, b = 650, a = 930)
463 * 650 b (b = 870, a = 1150)
464 * 860 c (c = 1290, b = 1080, a = 1360)
465 * 1080 b (b = 1300, a = 1580)
466 * 1290 c (c = 1720, b = 1510, a = 1790)
467 * 1510 b (b = 1730, a = 2010)
468 * 1720 c (c = 2150, b = 1940, a = 2220)
469 * 1940 b (b = 2160, a = 2440)
472 g_assert_cmpint (a, ==, 9);
473 g_assert_cmpint (b, ==, 9);
474 g_assert_cmpint (c, ==, 4);
476 g_source_unref (parent);
477 g_source_unref (child_b);
478 g_source_unref (child_c);
480 g_main_loop_unref (loop);
481 g_main_context_unref (ctx);
485 GSource *parent, *old_child, *new_child;
490 swap_sources (gpointer user_data)
492 SwappingTestData *data = user_data;
496 g_source_remove_child_source (data->parent, data->old_child);
497 g_clear_pointer (&data->old_child, g_source_unref);
500 if (!data->new_child)
502 data->new_child = g_timeout_source_new (0);
503 g_source_set_callback (data->new_child, quit_loop, data->loop, NULL);
504 g_source_add_child_source (data->parent, data->new_child);
507 return G_SOURCE_CONTINUE;
511 assert_not_reached_callback (gpointer user_data)
513 g_assert_not_reached ();
515 return G_SOURCE_REMOVE;
519 test_swapping_child_sources (void)
523 SwappingTestData data;
525 ctx = g_main_context_new ();
526 loop = g_main_loop_new (ctx, FALSE);
528 data.parent = g_timeout_source_new (50);
530 g_source_set_callback (data.parent, swap_sources, &data, NULL);
531 g_source_attach (data.parent, ctx);
533 data.old_child = g_timeout_source_new (100);
534 g_source_add_child_source (data.parent, data.old_child);
535 g_source_set_callback (data.old_child, assert_not_reached_callback, NULL, NULL);
537 data.new_child = NULL;
538 g_main_loop_run (loop);
540 g_source_destroy (data.parent);
541 g_source_unref (data.parent);
542 g_source_unref (data.new_child);
544 g_main_loop_unref (loop);
545 g_main_context_unref (ctx);
552 GSource *timeout1, *timeout2;
557 timeout1_callback (gpointer user_data)
559 TimeTestData *data = user_data;
561 gint64 mtime1, mtime2, time2;
563 source = g_main_current_source ();
564 g_assert (source == data->timeout1);
566 if (data->time1 == -1)
568 /* First iteration */
569 g_assert (!g_source_is_destroyed (data->timeout2));
571 mtime1 = g_get_monotonic_time ();
572 data->time1 = g_source_get_time (source);
574 /* g_source_get_time() does not change during a single callback */
576 mtime2 = g_get_monotonic_time ();
577 time2 = g_source_get_time (source);
579 g_assert_cmpint (mtime1, <, mtime2);
580 g_assert_cmpint (data->time1, ==, time2);
584 /* Second iteration */
585 g_assert (g_source_is_destroyed (data->timeout2));
587 /* g_source_get_time() MAY change between iterations; in this
588 * case we know for sure that it did because of the g_usleep()
591 time2 = g_source_get_time (source);
592 g_assert_cmpint (data->time1, <, time2);
594 g_main_loop_quit (data->loop);
601 timeout2_callback (gpointer user_data)
603 TimeTestData *data = user_data;
607 source = g_main_current_source ();
608 g_assert (source == data->timeout2);
610 g_assert (!g_source_is_destroyed (data->timeout1));
612 /* g_source_get_time() does not change between different sources in
613 * a single iteration of the mainloop.
615 time2 = g_source_get_time (source);
616 g_assert_cmpint (data->time1, ==, time2);
622 test_source_time (void)
626 data.ctx = g_main_context_new ();
627 data.loop = g_main_loop_new (data.ctx, FALSE);
629 data.timeout1 = g_timeout_source_new (0);
630 g_source_set_callback (data.timeout1, timeout1_callback, &data, NULL);
631 g_source_attach (data.timeout1, data.ctx);
633 data.timeout2 = g_timeout_source_new (0);
634 g_source_set_callback (data.timeout2, timeout2_callback, &data, NULL);
635 g_source_attach (data.timeout2, data.ctx);
639 g_main_loop_run (data.loop);
641 g_assert (!g_source_is_destroyed (data.timeout1));
642 g_assert (g_source_is_destroyed (data.timeout2));
644 g_source_destroy (data.timeout1);
645 g_source_unref (data.timeout1);
646 g_source_unref (data.timeout2);
648 g_main_loop_unref (data.loop);
649 g_main_context_unref (data.ctx);
653 main (int argc, char *argv[])
655 g_test_init (&argc, &argv, NULL);
657 g_test_add_func ("/maincontext/basic", test_maincontext_basic);
658 g_test_add_func ("/mainloop/basic", test_mainloop_basic);
659 g_test_add_func ("/mainloop/timeouts", test_timeouts);
660 g_test_add_func ("/mainloop/priorities", test_priorities);
661 g_test_add_func ("/mainloop/invoke", test_invoke);
662 g_test_add_func ("/mainloop/child_sources", test_child_sources);
663 g_test_add_func ("/mainloop/recursive_child_sources", test_recursive_child_sources);
664 g_test_add_func ("/mainloop/swapping_child_sources", test_swapping_child_sources);
665 g_test_add_func ("/mainloop/source_time", test_source_time);
667 return g_test_run ();