glib/tests: fix leaks
[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 gboolean
251 quit_loop (gpointer data)
252 {
253   GMainLoop *loop = data;
254
255   g_main_loop_quit (loop);
256
257   return G_SOURCE_REMOVE;
258 }
259
260 static gint count;
261
262 static gboolean
263 func (gpointer data)
264 {
265   if (data != NULL)
266     g_assert (data == g_thread_self ());
267
268   count++;
269
270   return FALSE;
271 }
272
273 static gboolean
274 call_func (gpointer data)
275 {
276   func (g_thread_self ());
277
278   return G_SOURCE_REMOVE;
279 }
280
281 static GMutex mutex;
282 static GCond cond;
283 static gboolean thread_ready;
284
285 static gpointer
286 thread_func (gpointer data)
287 {
288   GMainContext *ctx = data;
289   GMainLoop *loop;
290   GSource *source;
291
292   g_main_context_push_thread_default (ctx);
293   loop = g_main_loop_new (ctx, FALSE);
294
295   g_mutex_lock (&mutex);
296   thread_ready = TRUE;
297   g_cond_signal (&cond);
298   g_mutex_unlock (&mutex);
299
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);
304
305   g_main_loop_run (loop);
306
307   g_main_context_pop_thread_default (ctx);
308   g_main_loop_unref (loop);
309
310   return NULL;
311 }
312
313 static void
314 test_invoke (void)
315 {
316   GMainContext *ctx;
317   GThread *thread;
318
319   count = 0;
320
321   /* this one gets invoked directly */
322   g_main_context_invoke (NULL, func, g_thread_self ());
323   g_assert_cmpint (count, ==, 1);
324
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);
329
330   /* test thread-default forcing the invocation to go
331    * to another thread
332    */
333   ctx = g_main_context_new ();
334   thread = g_thread_new ("worker", thread_func, ctx);
335
336   g_mutex_lock (&mutex);
337   while (!thread_ready)
338     g_cond_wait (&cond, &mutex);
339   g_mutex_unlock (&mutex);
340
341   g_main_context_invoke (ctx, func, thread);
342
343   g_thread_join (thread);
344   g_assert_cmpint (count, ==, 3);
345
346   g_main_context_unref (ctx);
347 }
348
349 static gboolean
350 run_inner_loop (gpointer user_data)
351 {
352   GMainContext *ctx = user_data;
353   GMainLoop *inner;
354   GSource *timeout;
355
356   a++;
357
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);
363
364   g_main_loop_run (inner);
365   g_main_loop_unref (inner);
366
367   return G_SOURCE_CONTINUE;
368 }
369
370 static void
371 test_child_sources (void)
372 {
373   GMainContext *ctx;
374   GMainLoop *loop;
375   GSource *parent, *child_b, *child_c, *end;
376
377   ctx = g_main_context_new ();
378   loop = g_main_loop_new (ctx, FALSE);
379
380   a = b = c = 0;
381
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);
386
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);
390
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);
395
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);
404
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);
409
410   g_main_loop_run (loop);
411
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:
416    *
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
433    */
434
435   g_assert_cmpint (a, ==, 6);
436   g_assert_cmpint (b, ==, 3);
437   g_assert_cmpint (c, ==, 3);
438
439   g_source_destroy (parent);
440   g_source_unref (parent);
441   g_source_unref (child_b);
442   g_source_unref (child_c);
443
444   g_main_loop_unref (loop);
445   g_main_context_unref (ctx);
446 }
447
448 static void
449 test_recursive_child_sources (void)
450 {
451   GMainContext *ctx;
452   GMainLoop *loop;
453   GSource *parent, *child_b, *child_c, *end;
454
455   ctx = g_main_context_new ();
456   loop = g_main_loop_new (ctx, FALSE);
457
458   a = b = c = 0;
459
460   parent = g_timeout_source_new (500);
461   g_source_set_callback (parent, count_calls, &a, NULL);
462
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);
466
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);
470
471   g_source_attach (parent, ctx);
472
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);
477
478   g_main_loop_run (loop);
479
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)
490    */
491
492   g_assert_cmpint (a, ==, 9);
493   g_assert_cmpint (b, ==, 9);
494   g_assert_cmpint (c, ==, 4);
495
496   g_source_destroy (parent);
497   g_source_unref (parent);
498   g_source_unref (child_b);
499   g_source_unref (child_c);
500
501   g_main_loop_unref (loop);
502   g_main_context_unref (ctx);
503 }
504
505 typedef struct {
506   GSource *parent, *old_child, *new_child;
507   GMainLoop *loop;
508 } SwappingTestData;
509
510 static gboolean
511 swap_sources (gpointer user_data)
512 {
513   SwappingTestData *data = user_data;
514
515   if (data->old_child)
516     {
517       g_source_remove_child_source (data->parent, data->old_child);
518       g_clear_pointer (&data->old_child, g_source_unref);
519     }
520
521   if (!data->new_child)
522     {
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);
526     }
527
528   return G_SOURCE_CONTINUE;
529 }
530
531 static gboolean
532 assert_not_reached_callback (gpointer user_data)
533 {
534   g_assert_not_reached ();
535
536   return G_SOURCE_REMOVE;
537 }
538
539 static void
540 test_swapping_child_sources (void)
541 {
542   GMainContext *ctx;
543   GMainLoop *loop;
544   SwappingTestData data;
545
546   ctx = g_main_context_new ();
547   loop = g_main_loop_new (ctx, FALSE);
548
549   data.parent = g_timeout_source_new (50);
550   data.loop = loop;
551   g_source_set_callback (data.parent, swap_sources, &data, NULL);
552   g_source_attach (data.parent, ctx);
553
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);
557
558   data.new_child = NULL;
559   g_main_loop_run (loop);
560
561   g_source_destroy (data.parent);
562   g_source_unref (data.parent);
563   g_source_unref (data.new_child);
564
565   g_main_loop_unref (loop);
566   g_main_context_unref (ctx);
567 }
568
569 typedef struct {
570   GMainContext *ctx;
571   GMainLoop *loop;
572
573   GSource *timeout1, *timeout2;
574   gint64 time1;
575 } TimeTestData;
576
577 static gboolean
578 timeout1_callback (gpointer user_data)
579 {
580   TimeTestData *data = user_data;
581   GSource *source;
582   gint64 mtime1, mtime2, time2;
583
584   source = g_main_current_source ();
585   g_assert (source == data->timeout1);
586
587   if (data->time1 == -1)
588     {
589       /* First iteration */
590       g_assert (!g_source_is_destroyed (data->timeout2));
591
592       mtime1 = g_get_monotonic_time ();
593       data->time1 = g_source_get_time (source);
594
595       /* g_source_get_time() does not change during a single callback */
596       g_usleep (1000000);
597       mtime2 = g_get_monotonic_time ();
598       time2 = g_source_get_time (source);
599
600       g_assert_cmpint (mtime1, <, mtime2);
601       g_assert_cmpint (data->time1, ==, time2);
602     }
603   else
604     {
605       /* Second iteration */
606       g_assert (g_source_is_destroyed (data->timeout2));
607
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()
610        * last time.
611        */
612       time2 = g_source_get_time (source);
613       g_assert_cmpint (data->time1, <, time2);
614
615       g_main_loop_quit (data->loop);
616     }
617
618   return TRUE;
619 }
620
621 static gboolean
622 timeout2_callback (gpointer user_data)
623 {
624   TimeTestData *data = user_data;
625   GSource *source;
626   gint64 time2, time3;
627
628   source = g_main_current_source ();
629   g_assert (source == data->timeout2);
630
631   g_assert (!g_source_is_destroyed (data->timeout1));
632
633   /* g_source_get_time() does not change between different sources in
634    * a single iteration of the mainloop.
635    */
636   time2 = g_source_get_time (source);
637   g_assert_cmpint (data->time1, ==, time2);
638
639   /* The source should still have a valid time even after being
640    * destroyed, since it's currently running.
641    */
642   g_source_destroy (source);
643   time3 = g_source_get_time (source);
644   g_assert_cmpint (time2, ==, time3);
645
646   return FALSE;
647 }
648
649 static void
650 test_source_time (void)
651 {
652   TimeTestData data;
653
654   data.ctx = g_main_context_new ();
655   data.loop = g_main_loop_new (data.ctx, FALSE);
656
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);
660
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);
664
665   data.time1 = -1;
666
667   g_main_loop_run (data.loop);
668
669   g_assert (!g_source_is_destroyed (data.timeout1));
670   g_assert (g_source_is_destroyed (data.timeout2));
671
672   g_source_destroy (data.timeout1);
673   g_source_unref (data.timeout1);
674   g_source_unref (data.timeout2);
675
676   g_main_loop_unref (data.loop);
677   g_main_context_unref (data.ctx);
678 }
679
680 int
681 main (int argc, char *argv[])
682 {
683   g_test_init (&argc, &argv, NULL);
684
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);
694
695   return g_test_run ();
696 }