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_source_destroy (source);
68 g_main_context_unref (ctx);
70 ctx = g_main_context_default ();
71 source = g_source_new (&funcs, sizeof (GSource));
72 g_source_set_funcs (source, &funcs);
73 g_source_set_callback (source, cb, data, NULL);
74 id = g_source_attach (source, ctx);
75 g_source_set_name_by_id (id, "e");
76 g_assert_cmpstr (g_source_get_name (source), ==, "e");
77 g_assert (g_source_get_context (source) == ctx);
78 g_assert (g_source_remove_by_funcs_user_data (&funcs, data));
82 test_mainloop_basic (void)
87 loop = g_main_loop_new (NULL, FALSE);
89 g_assert (!g_main_loop_is_running (loop));
91 g_main_loop_ref (loop);
93 ctx = g_main_loop_get_context (loop);
94 g_assert (ctx == g_main_context_default ());
96 g_main_loop_unref (loop);
98 g_assert (g_main_depth () == 0);
100 g_main_loop_unref (loop);
108 count_calls (gpointer data)
126 ctx = g_main_context_new ();
127 loop = g_main_loop_new (ctx, FALSE);
129 source = g_timeout_source_new (100);
130 g_source_set_callback (source, count_calls, &a, NULL);
131 g_source_attach (source, ctx);
132 g_source_unref (source);
134 source = g_timeout_source_new (250);
135 g_source_set_callback (source, count_calls, &b, NULL);
136 g_source_attach (source, ctx);
137 g_source_unref (source);
139 source = g_timeout_source_new (330);
140 g_source_set_callback (source, count_calls, &c, NULL);
141 g_source_attach (source, ctx);
142 g_source_unref (source);
144 source = g_timeout_source_new (1050);
145 g_source_set_callback (source, (GSourceFunc)g_main_loop_quit, loop, NULL);
146 g_source_attach (source, ctx);
147 g_source_unref (source);
149 g_main_loop_run (loop);
155 g_main_loop_unref (loop);
156 g_main_context_unref (ctx);
160 test_priorities (void)
168 ctx = g_main_context_new ();
170 sourcea = g_idle_source_new ();
171 g_source_set_callback (sourcea, count_calls, &a, NULL);
172 g_source_set_priority (sourcea, 1);
173 g_source_attach (sourcea, ctx);
174 g_source_unref (sourcea);
176 sourceb = g_idle_source_new ();
177 g_source_set_callback (sourceb, count_calls, &b, NULL);
178 g_source_set_priority (sourceb, 0);
179 g_source_attach (sourceb, ctx);
180 g_source_unref (sourceb);
182 g_assert (g_main_context_pending (ctx));
183 g_assert (g_main_context_iteration (ctx, FALSE));
187 g_assert (g_main_context_iteration (ctx, FALSE));
191 g_source_destroy (sourceb);
193 g_assert (g_main_context_iteration (ctx, FALSE));
197 g_assert (g_main_context_pending (ctx));
198 g_source_destroy (sourcea);
199 g_assert (!g_main_context_pending (ctx));
201 g_main_context_unref (ctx);
210 g_assert (data == g_thread_self ());
218 call_func (gpointer data)
220 func (g_thread_self ());
226 thread_func (gpointer data)
228 GMainContext *ctx = data;
231 g_main_context_push_thread_default (ctx);
233 source = g_timeout_source_new (500);
234 g_source_set_callback (source, (GSourceFunc)g_thread_exit, NULL, NULL);
235 g_source_attach (source, ctx);
236 g_source_unref (source);
239 g_main_context_iteration (ctx, TRUE);
252 /* this one gets invoked directly */
253 g_main_context_invoke (NULL, func, g_thread_self ());
254 g_assert (count == 1);
256 /* invoking out of an idle works too */
257 g_idle_add (call_func, NULL);
258 g_main_context_iteration (g_main_context_default (), FALSE);
259 g_assert (count == 2);
261 /* test thread-default forcing the invocation to go
264 ctx = g_main_context_new ();
265 thread = g_thread_new ("worker", thread_func, ctx);
267 g_usleep (1000); /* give some time to push the thread-default */
269 g_main_context_invoke (ctx, func, thread);
270 g_assert (count == 2);
272 g_thread_join (thread);
273 g_assert (count == 3);
277 main (int argc, char *argv[])
279 g_test_init (&argc, &argv, NULL);
281 g_test_add_func ("/maincontext/basic", test_maincontext_basic);
282 g_test_add_func ("/mainloop/basic", test_mainloop_basic);
283 g_test_add_func ("/mainloop/timeouts", test_timeouts);
284 g_test_add_func ("/mainloop/priorities", test_priorities);
285 g_test_add_func ("/mainloop/invoke", test_invoke);
287 return g_test_run ();