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)
31 test_maincontext_basic (void)
37 gpointer data = &funcs;
39 ctx = g_main_context_new ();
41 g_assert (!g_main_context_pending (ctx));
42 g_assert (!g_main_context_iteration (ctx, FALSE));
44 source = g_source_new (&funcs, sizeof (GSource));
45 g_assert (g_source_get_priority (source) == G_PRIORITY_DEFAULT);
46 g_assert (!g_source_is_destroyed (source));
48 g_assert (!g_source_get_can_recurse (source));
49 g_assert (g_source_get_name (source) == NULL);
51 g_source_set_can_recurse (source, TRUE);
52 g_source_set_name (source, "d");
54 g_assert (g_source_get_can_recurse (source));
55 g_assert_cmpstr (g_source_get_name (source), ==, "d");
57 g_assert (g_main_context_find_source_by_user_data (ctx, NULL) == NULL);
58 g_assert (g_main_context_find_source_by_funcs_user_data (ctx, &funcs, NULL) == NULL);
60 id = g_source_attach (source, ctx);
61 g_assert_cmpint (g_source_get_id (source), ==, id);
62 g_assert (g_main_context_find_source_by_id (ctx, id) == source);
64 g_source_set_priority (source, G_PRIORITY_HIGH);
65 g_assert (g_source_get_priority (source) == G_PRIORITY_HIGH);
67 g_main_context_unref (ctx);
69 ctx = g_main_context_default ();
70 source = g_source_new (&funcs, sizeof (GSource));
71 g_source_set_funcs (source, &funcs);
72 g_source_set_callback (source, cb, data, NULL);
73 id = g_source_attach (source, ctx);
74 g_source_set_name_by_id (id, "e");
75 g_assert_cmpstr (g_source_get_name (source), ==, "e");
76 g_assert (g_source_get_context (source) == ctx);
77 g_assert (g_source_remove_by_funcs_user_data (&funcs, data));
81 test_mainloop_basic (void)
86 loop = g_main_loop_new (NULL, FALSE);
88 g_assert (!g_main_loop_is_running (loop));
90 g_main_loop_ref (loop);
92 ctx = g_main_loop_get_context (loop);
93 g_assert (ctx == g_main_context_default ());
95 g_main_loop_unref (loop);
97 g_assert (g_main_depth () == 0);
105 count_calls (gpointer data)
123 ctx = g_main_context_new ();
124 loop = g_main_loop_new (ctx, FALSE);
126 source = g_timeout_source_new (100);
127 g_source_set_callback (source, count_calls, &a, NULL);
128 g_source_attach (source, ctx);
129 g_source_unref (source);
131 source = g_timeout_source_new (250);
132 g_source_set_callback (source, count_calls, &b, NULL);
133 g_source_attach (source, ctx);
134 g_source_unref (source);
136 source = g_timeout_source_new (330);
137 g_source_set_callback (source, count_calls, &c, NULL);
138 g_source_attach (source, ctx);
139 g_source_unref (source);
141 source = g_timeout_source_new (1050);
142 g_source_set_callback (source, (GSourceFunc)g_main_loop_quit, loop, NULL);
143 g_source_attach (source, ctx);
144 g_source_unref (source);
146 g_main_loop_run (loop);
152 g_main_loop_unref (loop);
153 g_main_context_unref (ctx);
157 test_priorities (void)
165 ctx = g_main_context_new ();
167 sourcea = g_idle_source_new ();
168 g_source_set_callback (sourcea, count_calls, &a, NULL);
169 g_source_set_priority (sourcea, 1);
170 g_source_attach (sourcea, ctx);
171 g_source_unref (sourcea);
173 sourceb = g_idle_source_new ();
174 g_source_set_callback (sourceb, count_calls, &b, NULL);
175 g_source_set_priority (sourceb, 0);
176 g_source_attach (sourceb, ctx);
177 g_source_unref (sourceb);
179 g_assert (g_main_context_pending (ctx));
180 g_assert (g_main_context_iteration (ctx, FALSE));
184 g_assert (g_main_context_iteration (ctx, FALSE));
188 g_source_destroy (sourceb);
190 g_assert (g_main_context_iteration (ctx, FALSE));
194 g_assert (g_main_context_pending (ctx));
195 g_source_destroy (sourcea);
196 g_assert (!g_main_context_pending (ctx));
198 g_main_context_unref (ctx);
207 g_assert (data == g_thread_self ());
215 call_func (gpointer data)
217 func (g_thread_self ());
223 thread_func (gpointer data)
225 GMainContext *ctx = data;
228 g_main_context_push_thread_default (ctx);
230 source = g_timeout_source_new (500);
231 g_source_set_callback (source, (GSourceFunc)g_thread_exit, NULL, NULL);
232 g_source_attach (source, ctx);
233 g_source_unref (source);
236 g_main_context_iteration (ctx, TRUE);
249 /* this one gets invoked directly */
250 g_main_context_invoke (NULL, func, g_thread_self ());
251 g_assert (count == 1);
253 /* invoking out of an idle works too */
254 g_idle_add (call_func, NULL);
255 g_main_context_iteration (g_main_context_default (), FALSE);
256 g_assert (count == 2);
258 /* test thread-default forcing the invocation to go
261 ctx = g_main_context_new ();
262 thread = g_thread_new ("worker", thread_func, ctx);
264 g_usleep (1000); /* give some time to push the thread-default */
266 g_main_context_invoke (ctx, func, thread);
267 g_assert (count == 2);
269 g_thread_join (thread);
270 g_assert (count == 3);
274 main (int argc, char *argv[])
276 g_test_init (&argc, &argv, NULL);
278 g_test_add_func ("/maincontext/basic", test_maincontext_basic);
279 g_test_add_func ("/mainloop/basic", test_mainloop_basic);
280 g_test_add_func ("/mainloop/timeouts", test_timeouts);
281 g_test_add_func ("/mainloop/priorities", test_priorities);
282 g_test_add_func ("/mainloop/invoke", test_invoke);
284 return g_test_run ();