tests/mainloop: Add a test for recursive child sources
[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_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);
83
84   g_source_set_priority (source, G_PRIORITY_HIGH);
85   g_assert_cmpint (g_source_get_priority (source), ==, G_PRIORITY_HIGH);
86
87   g_source_destroy (source);
88   g_main_context_unref (ctx);
89
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));
100
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));
107
108   g_idle_add (cb, data);
109   g_assert (g_idle_remove_by_data (data));
110 }
111
112 static void
113 test_mainloop_basic (void)
114 {
115   GMainLoop *loop;
116   GMainContext *ctx;
117
118   loop = g_main_loop_new (NULL, FALSE);
119
120   g_assert (!g_main_loop_is_running (loop));
121
122   g_main_loop_ref (loop);
123
124   ctx = g_main_loop_get_context (loop);
125   g_assert (ctx == g_main_context_default ());
126
127   g_main_loop_unref (loop);
128
129   g_assert_cmpint (g_main_depth (), ==, 0);
130
131   g_main_loop_unref (loop);
132 }
133
134 static gint a;
135 static gint b;
136 static gint c;
137
138 static gboolean
139 count_calls (gpointer data)
140 {
141   gint *i = data;
142
143   (*i)++;
144
145   return TRUE;
146 }
147
148 static void
149 test_timeouts (void)
150 {
151   GMainContext *ctx;
152   GMainLoop *loop;
153   GSource *source;
154
155   a = b = c = 0;
156
157   ctx = g_main_context_new ();
158   loop = g_main_loop_new (ctx, FALSE);
159
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);
164
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);
169
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);
174
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);
179
180   g_main_loop_run (loop);
181
182   g_assert_cmpint (a, ==, 10);
183   g_assert_cmpint (b, ==, 4);
184   g_assert_cmpint (c, ==, 3);
185
186   g_main_loop_unref (loop);
187   g_main_context_unref (ctx);
188 }
189
190 static void
191 test_priorities (void)
192 {
193   GMainContext *ctx;
194   GSource *sourcea;
195   GSource *sourceb;
196
197   a = b = c = 0;
198
199   ctx = g_main_context_new ();
200
201   sourcea = g_idle_source_new ();
202   g_source_set_callback (sourcea, count_calls, &a, NULL);
203   g_source_set_priority (sourcea, 1);
204   g_source_attach (sourcea, ctx);
205   g_source_unref (sourcea);
206
207   sourceb = g_idle_source_new ();
208   g_source_set_callback (sourceb, count_calls, &b, NULL);
209   g_source_set_priority (sourceb, 0);
210   g_source_attach (sourceb, ctx);
211   g_source_unref (sourceb);
212
213   g_assert (g_main_context_pending (ctx));
214   g_assert (g_main_context_iteration (ctx, FALSE));
215   g_assert_cmpint (a, ==, 0);
216   g_assert_cmpint (b, ==, 1);
217
218   g_assert (g_main_context_iteration (ctx, FALSE));
219   g_assert_cmpint (a, ==, 0);
220   g_assert_cmpint (b, ==, 2);
221
222   g_source_destroy (sourceb);
223
224   g_assert (g_main_context_iteration (ctx, FALSE));
225   g_assert_cmpint (a, ==, 1);
226   g_assert_cmpint (b, ==, 2);
227
228   g_assert (g_main_context_pending (ctx));
229   g_source_destroy (sourcea);
230   g_assert (!g_main_context_pending (ctx));
231
232   g_main_context_unref (ctx);
233 }
234
235 static gint count;
236
237 static gboolean
238 func (gpointer data)
239 {
240   if (data != NULL)
241     g_assert (data == g_thread_self ());
242
243   count++;
244
245   return FALSE;
246 }
247
248 static gboolean
249 call_func (gpointer data)
250 {
251   func (g_thread_self ());
252
253   return G_SOURCE_REMOVE;
254 }
255
256 static GMutex mutex;
257 static GCond cond;
258
259 static gpointer
260 thread_func (gpointer data)
261 {
262   GMainContext *ctx = data;
263   GSource *source;
264
265   g_main_context_push_thread_default (ctx);
266
267   g_mutex_lock (&mutex);
268   g_cond_signal (&cond);
269   g_mutex_unlock (&mutex);
270
271   source = g_timeout_source_new (500);
272   g_source_set_callback (source, (GSourceFunc)g_thread_exit, NULL, NULL);
273   g_source_attach (source, ctx);
274   g_source_unref (source);
275
276   while (TRUE)
277     g_main_context_iteration (ctx, TRUE);
278
279   return NULL;
280 }
281
282 static void
283 test_invoke (void)
284 {
285   GMainContext *ctx;
286   GThread *thread;
287
288   count = 0;
289
290   /* this one gets invoked directly */
291   g_main_context_invoke (NULL, func, g_thread_self ());
292   g_assert_cmpint (count, ==, 1);
293
294   /* invoking out of an idle works too */
295   g_idle_add (call_func, NULL);
296   g_main_context_iteration (g_main_context_default (), FALSE);
297   g_assert_cmpint (count, ==, 2);
298
299   /* test thread-default forcing the invocation to go
300    * to another thread
301    */
302   ctx = g_main_context_new ();
303   g_mutex_lock (&mutex);
304   thread = g_thread_new ("worker", thread_func, ctx);
305
306   g_cond_wait (&cond, &mutex);
307
308   g_main_context_invoke (ctx, func, thread);
309   g_assert_cmpint (count, ==, 2);
310
311   g_thread_join (thread);
312   g_assert_cmpint (count, ==, 3);
313 }
314
315 static gboolean
316 run_inner_loop (gpointer user_data)
317 {
318   GMainContext *ctx = user_data;
319   GMainLoop *inner;
320   GSource *timeout;
321
322   a++;
323
324   inner = g_main_loop_new (ctx, FALSE);
325   timeout = g_timeout_source_new (100);
326   g_source_set_callback (timeout, (GSourceFunc)g_main_loop_quit, inner, NULL);
327   g_source_attach (timeout, ctx);
328
329   g_main_loop_run (inner);
330   g_main_loop_unref (inner);
331
332   return TRUE;
333 }
334
335 static void
336 test_child_sources (void)
337 {
338   GMainContext *ctx;
339   GMainLoop *loop;
340   GSource *parent, *child_b, *child_c, *end;
341
342   ctx = g_main_context_new ();
343   loop = g_main_loop_new (ctx, FALSE);
344
345   a = b = c = 0;
346
347   parent = g_timeout_source_new (2000);
348   g_source_set_callback (parent, run_inner_loop, ctx, NULL);
349   g_source_set_priority (parent, G_PRIORITY_LOW);
350   g_source_attach (parent, ctx);
351
352   child_b = g_timeout_source_new (250);
353   g_source_set_callback (child_b, count_calls, &b, NULL);
354   g_source_add_child_source (parent, child_b);
355
356   child_c = g_timeout_source_new (330);
357   g_source_set_callback (child_c, count_calls, &c, NULL);
358   g_source_set_priority (child_c, G_PRIORITY_HIGH);
359   g_source_add_child_source (parent, child_c);
360
361   /* Child sources always have the priority of the parent */
362   g_assert_cmpint (g_source_get_priority (parent), ==, G_PRIORITY_LOW);
363   g_assert_cmpint (g_source_get_priority (child_b), ==, G_PRIORITY_LOW);
364   g_assert_cmpint (g_source_get_priority (child_c), ==, G_PRIORITY_LOW);
365   g_source_set_priority (parent, G_PRIORITY_DEFAULT);
366   g_assert_cmpint (g_source_get_priority (parent), ==, G_PRIORITY_DEFAULT);
367   g_assert_cmpint (g_source_get_priority (child_b), ==, G_PRIORITY_DEFAULT);
368   g_assert_cmpint (g_source_get_priority (child_c), ==, G_PRIORITY_DEFAULT);
369
370   end = g_timeout_source_new (1050);
371   g_source_set_callback (end, (GSourceFunc)g_main_loop_quit, loop, NULL);
372   g_source_attach (end, ctx);
373   g_source_unref (end);
374
375   g_main_loop_run (loop);
376
377   /* The parent source's own timeout will never trigger, so "a" will
378    * only get incremented when "b" or "c" does. And when timeouts get
379    * blocked, they still wait the full interval next time rather than
380    * "catching up". So the timing is:
381    *
382    *  250 - b++ -> a++, run_inner_loop
383    *  330 - (c is blocked)
384    *  350 - inner_loop ends
385    *  350 - c++ belatedly -> a++, run_inner_loop
386    *  450 - inner loop ends
387    *  500 - b++ -> a++, run_inner_loop
388    *  600 - inner_loop ends
389    *  680 - c++ -> a++, run_inner_loop
390    *  750 - (b is blocked)
391    *  780 - inner loop ends
392    *  780 - b++ belatedly -> a++, run_inner_loop
393    *  880 - inner loop ends
394    * 1010 - c++ -> a++, run_inner_loop
395    * 1030 - (b is blocked)
396    * 1050 - end runs, quits outer loop, which has no effect yet
397    * 1110 - inner loop ends, a returns, outer loop exits
398    */
399
400   g_assert_cmpint (a, ==, 6);
401   g_assert_cmpint (b, ==, 3);
402   g_assert_cmpint (c, ==, 3);
403
404   g_source_unref (parent);
405   g_source_unref (child_b);
406   g_source_unref (child_c);
407
408   g_main_loop_unref (loop);
409   g_main_context_unref (ctx);
410 }
411
412 static void
413 test_recursive_child_sources (void)
414 {
415   GMainContext *ctx;
416   GMainLoop *loop;
417   GSource *parent, *child_b, *child_c, *end;
418
419   ctx = g_main_context_new ();
420   loop = g_main_loop_new (ctx, FALSE);
421
422   a = b = c = 0;
423
424   parent = g_timeout_source_new (500);
425   g_source_set_callback (parent, count_calls, &a, NULL);
426
427   child_b = g_timeout_source_new (220);
428   g_source_set_callback (child_b, count_calls, &b, NULL);
429   g_source_add_child_source (parent, child_b);
430
431   child_c = g_timeout_source_new (430);
432   g_source_set_callback (child_c, count_calls, &c, NULL);
433   g_source_add_child_source (child_b, child_c);
434
435   g_source_attach (parent, ctx);
436
437   end = g_timeout_source_new (2010);
438   g_source_set_callback (end, (GSourceFunc)g_main_loop_quit, loop, NULL);
439   g_source_attach (end, ctx);
440   g_source_unref (end);
441
442   g_main_loop_run (loop);
443
444   /* Sequence of events:
445    * 220 b (b = 440, a = 720)
446    * 430 c (c = 860, b = 650, a = 930)
447    * 650 b (b = 870, a = 1150)
448    * 860 c (c = 1290, b = 1080, a = 1360)
449    * 1080 b (b = 1300, a = 1580)
450    * 1290 c (c = 1720, b = 1510, a = 1790)
451    * 1510 b (b = 1730, a = 2010)
452    * 1720 c (c = 2150, b = 1940, a = 2220)
453    * 1940 b (b = 2160, a = 2440)
454    */
455
456   g_assert_cmpint (a, ==, 9);
457   g_assert_cmpint (b, ==, 9);
458   g_assert_cmpint (c, ==, 4);
459
460   g_source_unref (parent);
461   g_source_unref (child_b);
462   g_source_unref (child_c);
463
464   g_main_loop_unref (loop);
465   g_main_context_unref (ctx);
466 }
467
468 int
469 main (int argc, char *argv[])
470 {
471   g_test_init (&argc, &argv, NULL);
472
473   g_test_add_func ("/maincontext/basic", test_maincontext_basic);
474   g_test_add_func ("/mainloop/basic", test_mainloop_basic);
475   g_test_add_func ("/mainloop/timeouts", test_timeouts);
476   g_test_add_func ("/mainloop/priorities", test_priorities);
477   g_test_add_func ("/mainloop/invoke", test_invoke);
478   g_test_add_func ("/mainloop/child_sources", test_child_sources);
479   g_test_add_func ("/mainloop/recursive_child_sources", test_recursive_child_sources);
480
481   return g_test_run ();
482 }