glib/tests: use g_test_expect_message()
[platform/upstream/glib.git] / glib / tests / mainloop.c
1 /* Unit tests for GMainLoop
2  * Copyright (C) 2011 Red Hat, Inc
3  * Author: Matthias Clasen
4  *
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.
8  *
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.
12  *
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.
21  */
22
23 #include <glib.h>
24
25 static gboolean cb (gpointer data)
26 {
27   return FALSE;
28 }
29
30 static gboolean prepare (GSource *source, gint *time)
31 {
32   return FALSE;
33 }
34 static gboolean check (GSource *source)
35 {
36   return FALSE;
37 }
38 static gboolean dispatch (GSource *source, GSourceFunc cb, gpointer date)
39 {
40   return FALSE;
41 }
42
43 GSourceFuncs funcs = {
44   prepare,
45   check,
46   dispatch,
47   NULL
48 };
49
50 static void
51 test_maincontext_basic (void)
52 {
53   GMainContext *ctx;
54   GSource *source;
55   guint id;
56   gpointer data = &funcs;
57
58   ctx = g_main_context_new ();
59
60   g_assert (!g_main_context_pending (ctx));
61   g_assert (!g_main_context_iteration (ctx, FALSE));
62
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));
66
67   g_assert (!g_source_get_can_recurse (source));
68   g_assert (g_source_get_name (source) == NULL);
69
70   g_source_set_can_recurse (source, TRUE);
71   g_source_set_name (source, "d");
72
73   g_assert (g_source_get_can_recurse (source));
74   g_assert_cmpstr (g_source_get_name (source), ==, "d");
75
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);
78
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);
82
83   g_source_set_priority (source, G_PRIORITY_HIGH);
84   g_assert_cmpint (g_source_get_priority (source), ==, G_PRIORITY_HIGH);
85
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);
89
90   g_main_context_unref (ctx);
91
92   if (g_test_undefined ())
93     {
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 ();
98     }
99
100   g_source_unref (source);
101
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));
112
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));
119
120   g_idle_add (cb, data);
121   g_assert (g_idle_remove_by_data (data));
122 }
123
124 static void
125 test_mainloop_basic (void)
126 {
127   GMainLoop *loop;
128   GMainContext *ctx;
129
130   loop = g_main_loop_new (NULL, FALSE);
131
132   g_assert (!g_main_loop_is_running (loop));
133
134   g_main_loop_ref (loop);
135
136   ctx = g_main_loop_get_context (loop);
137   g_assert (ctx == g_main_context_default ());
138
139   g_main_loop_unref (loop);
140
141   g_assert_cmpint (g_main_depth (), ==, 0);
142
143   g_main_loop_unref (loop);
144 }
145
146 static gint a;
147 static gint b;
148 static gint c;
149
150 static gboolean
151 count_calls (gpointer data)
152 {
153   gint *i = data;
154
155   (*i)++;
156
157   return TRUE;
158 }
159
160 static void
161 test_timeouts (void)
162 {
163   GMainContext *ctx;
164   GMainLoop *loop;
165   GSource *source;
166
167   a = b = c = 0;
168
169   ctx = g_main_context_new ();
170   loop = g_main_loop_new (ctx, FALSE);
171
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);
176
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);
181
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);
186
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);
191
192   g_main_loop_run (loop);
193
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);
200
201   g_main_loop_unref (loop);
202   g_main_context_unref (ctx);
203 }
204
205 static void
206 test_priorities (void)
207 {
208   GMainContext *ctx;
209   GSource *sourcea;
210   GSource *sourceb;
211
212   a = b = c = 0;
213
214   ctx = g_main_context_new ();
215
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);
221
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);
227
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);
232
233   g_assert (g_main_context_iteration (ctx, FALSE));
234   g_assert_cmpint (a, ==, 0);
235   g_assert_cmpint (b, ==, 2);
236
237   g_source_destroy (sourceb);
238
239   g_assert (g_main_context_iteration (ctx, FALSE));
240   g_assert_cmpint (a, ==, 1);
241   g_assert_cmpint (b, ==, 2);
242
243   g_assert (g_main_context_pending (ctx));
244   g_source_destroy (sourcea);
245   g_assert (!g_main_context_pending (ctx));
246
247   g_main_context_unref (ctx);
248 }
249
250 static gint count;
251
252 static gboolean
253 func (gpointer data)
254 {
255   if (data != NULL)
256     g_assert (data == g_thread_self ());
257
258   count++;
259
260   return FALSE;
261 }
262
263 static gboolean
264 call_func (gpointer data)
265 {
266   func (g_thread_self ());
267
268   return G_SOURCE_REMOVE;
269 }
270
271 static GMutex mutex;
272 static GCond cond;
273 static gboolean thread_ready;
274
275 static gpointer
276 thread_func (gpointer data)
277 {
278   GMainContext *ctx = data;
279   GSource *source;
280
281   g_main_context_push_thread_default (ctx);
282
283   g_mutex_lock (&mutex);
284   thread_ready = TRUE;
285   g_cond_signal (&cond);
286   g_mutex_unlock (&mutex);
287
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);
292
293   while (TRUE)
294     g_main_context_iteration (ctx, TRUE);
295
296   return NULL;
297 }
298
299 static void
300 test_invoke (void)
301 {
302   GMainContext *ctx;
303   GThread *thread;
304
305   count = 0;
306
307   /* this one gets invoked directly */
308   g_main_context_invoke (NULL, func, g_thread_self ());
309   g_assert_cmpint (count, ==, 1);
310
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);
315
316   /* test thread-default forcing the invocation to go
317    * to another thread
318    */
319   ctx = g_main_context_new ();
320   thread = g_thread_new ("worker", thread_func, ctx);
321
322   g_mutex_lock (&mutex);
323   while (!thread_ready)
324     g_cond_wait (&cond, &mutex);
325   g_mutex_unlock (&mutex);
326
327   g_main_context_invoke (ctx, func, thread);
328
329   g_thread_join (thread);
330   g_assert_cmpint (count, ==, 3);
331 }
332
333 static gboolean
334 quit_loop (gpointer data)
335 {
336   GMainLoop *loop = data;
337
338   g_main_loop_quit (loop);
339
340   return G_SOURCE_REMOVE;
341 }
342
343 static gboolean
344 run_inner_loop (gpointer user_data)
345 {
346   GMainContext *ctx = user_data;
347   GMainLoop *inner;
348   GSource *timeout;
349
350   a++;
351
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);
356
357   g_main_loop_run (inner);
358   g_main_loop_unref (inner);
359
360   return G_SOURCE_CONTINUE;
361 }
362
363 static void
364 test_child_sources (void)
365 {
366   GMainContext *ctx;
367   GMainLoop *loop;
368   GSource *parent, *child_b, *child_c, *end;
369
370   ctx = g_main_context_new ();
371   loop = g_main_loop_new (ctx, FALSE);
372
373   a = b = c = 0;
374
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);
379
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);
383
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);
388
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);
397
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);
402
403   g_main_loop_run (loop);
404
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:
409    *
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
426    */
427
428   g_assert_cmpint (a, ==, 6);
429   g_assert_cmpint (b, ==, 3);
430   g_assert_cmpint (c, ==, 3);
431
432   g_source_unref (parent);
433   g_source_unref (child_b);
434   g_source_unref (child_c);
435
436   g_main_loop_unref (loop);
437   g_main_context_unref (ctx);
438 }
439
440 static void
441 test_recursive_child_sources (void)
442 {
443   GMainContext *ctx;
444   GMainLoop *loop;
445   GSource *parent, *child_b, *child_c, *end;
446
447   ctx = g_main_context_new ();
448   loop = g_main_loop_new (ctx, FALSE);
449
450   a = b = c = 0;
451
452   parent = g_timeout_source_new (500);
453   g_source_set_callback (parent, count_calls, &a, NULL);
454
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);
458
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);
462
463   g_source_attach (parent, ctx);
464
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);
469
470   g_main_loop_run (loop);
471
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)
482    */
483
484   g_assert_cmpint (a, ==, 9);
485   g_assert_cmpint (b, ==, 9);
486   g_assert_cmpint (c, ==, 4);
487
488   g_source_unref (parent);
489   g_source_unref (child_b);
490   g_source_unref (child_c);
491
492   g_main_loop_unref (loop);
493   g_main_context_unref (ctx);
494 }
495
496 typedef struct {
497   GSource *parent, *old_child, *new_child;
498   GMainLoop *loop;
499 } SwappingTestData;
500
501 static gboolean
502 swap_sources (gpointer user_data)
503 {
504   SwappingTestData *data = user_data;
505
506   if (data->old_child)
507     {
508       g_source_remove_child_source (data->parent, data->old_child);
509       g_clear_pointer (&data->old_child, g_source_unref);
510     }
511
512   if (!data->new_child)
513     {
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);
517     }
518
519   return G_SOURCE_CONTINUE;
520 }
521
522 static gboolean
523 assert_not_reached_callback (gpointer user_data)
524 {
525   g_assert_not_reached ();
526
527   return G_SOURCE_REMOVE;
528 }
529
530 static void
531 test_swapping_child_sources (void)
532 {
533   GMainContext *ctx;
534   GMainLoop *loop;
535   SwappingTestData data;
536
537   ctx = g_main_context_new ();
538   loop = g_main_loop_new (ctx, FALSE);
539
540   data.parent = g_timeout_source_new (50);
541   data.loop = loop;
542   g_source_set_callback (data.parent, swap_sources, &data, NULL);
543   g_source_attach (data.parent, ctx);
544
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);
548
549   data.new_child = NULL;
550   g_main_loop_run (loop);
551
552   g_source_destroy (data.parent);
553   g_source_unref (data.parent);
554   g_source_unref (data.new_child);
555
556   g_main_loop_unref (loop);
557   g_main_context_unref (ctx);
558 }
559
560 typedef struct {
561   GMainContext *ctx;
562   GMainLoop *loop;
563
564   GSource *timeout1, *timeout2;
565   gint64 time1;
566 } TimeTestData;
567
568 static gboolean
569 timeout1_callback (gpointer user_data)
570 {
571   TimeTestData *data = user_data;
572   GSource *source;
573   gint64 mtime1, mtime2, time2;
574
575   source = g_main_current_source ();
576   g_assert (source == data->timeout1);
577
578   if (data->time1 == -1)
579     {
580       /* First iteration */
581       g_assert (!g_source_is_destroyed (data->timeout2));
582
583       mtime1 = g_get_monotonic_time ();
584       data->time1 = g_source_get_time (source);
585
586       /* g_source_get_time() does not change during a single callback */
587       g_usleep (1000000);
588       mtime2 = g_get_monotonic_time ();
589       time2 = g_source_get_time (source);
590
591       g_assert_cmpint (mtime1, <, mtime2);
592       g_assert_cmpint (data->time1, ==, time2);
593     }
594   else
595     {
596       /* Second iteration */
597       g_assert (g_source_is_destroyed (data->timeout2));
598
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()
601        * last time.
602        */
603       time2 = g_source_get_time (source);
604       g_assert_cmpint (data->time1, <, time2);
605
606       g_main_loop_quit (data->loop);
607     }
608
609   return TRUE;
610 }
611
612 static gboolean
613 timeout2_callback (gpointer user_data)
614 {
615   TimeTestData *data = user_data;
616   GSource *source;
617   gint64 time2, time3;
618
619   source = g_main_current_source ();
620   g_assert (source == data->timeout2);
621
622   g_assert (!g_source_is_destroyed (data->timeout1));
623
624   /* g_source_get_time() does not change between different sources in
625    * a single iteration of the mainloop.
626    */
627   time2 = g_source_get_time (source);
628   g_assert_cmpint (data->time1, ==, time2);
629
630   /* The source should still have a valid time even after being
631    * destroyed, since it's currently running.
632    */
633   g_source_destroy (source);
634   time3 = g_source_get_time (source);
635   g_assert_cmpint (time2, ==, time3);
636
637   return FALSE;
638 }
639
640 static void
641 test_source_time (void)
642 {
643   TimeTestData data;
644
645   data.ctx = g_main_context_new ();
646   data.loop = g_main_loop_new (data.ctx, FALSE);
647
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);
651
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);
655
656   data.time1 = -1;
657
658   g_main_loop_run (data.loop);
659
660   g_assert (!g_source_is_destroyed (data.timeout1));
661   g_assert (g_source_is_destroyed (data.timeout2));
662
663   g_source_destroy (data.timeout1);
664   g_source_unref (data.timeout1);
665   g_source_unref (data.timeout2);
666
667   g_main_loop_unref (data.loop);
668   g_main_context_unref (data.ctx);
669 }
670
671 int
672 main (int argc, char *argv[])
673 {
674   g_test_init (&argc, &argv, NULL);
675
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);
685
686   return g_test_run ();
687 }