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)
30 static gboolean prepare (GSource *source, gint *time)
34 static gboolean check (GSource *source)
38 static gboolean dispatch (GSource *source, GSourceFunc cb, gpointer date)
43 GSourceFuncs funcs = {
51 test_maincontext_basic (void)
56 gpointer data = &funcs;
58 ctx = g_main_context_new ();
60 g_assert (!g_main_context_pending (ctx));
61 g_assert (!g_main_context_iteration (ctx, FALSE));
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));
67 g_assert (!g_source_get_can_recurse (source));
68 g_assert (g_source_get_name (source) == NULL);
70 g_source_set_can_recurse (source, TRUE);
71 g_source_set_name (source, "d");
73 g_assert (g_source_get_can_recurse (source));
74 g_assert_cmpstr (g_source_get_name (source), ==, "d");
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);
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);
84 g_source_set_priority (source, G_PRIORITY_HIGH);
85 g_assert_cmpint (g_source_get_priority (source), ==, G_PRIORITY_HIGH);
87 g_source_destroy (source);
88 g_main_context_unref (ctx);
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));
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));
108 g_idle_add (cb, data);
109 g_assert (g_idle_remove_by_data (data));
113 test_mainloop_basic (void)
118 loop = g_main_loop_new (NULL, FALSE);
120 g_assert (!g_main_loop_is_running (loop));
122 g_main_loop_ref (loop);
124 ctx = g_main_loop_get_context (loop);
125 g_assert (ctx == g_main_context_default ());
127 g_main_loop_unref (loop);
129 g_assert_cmpint (g_main_depth (), ==, 0);
131 g_main_loop_unref (loop);
139 count_calls (gpointer data)
157 ctx = g_main_context_new ();
158 loop = g_main_loop_new (ctx, FALSE);
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);
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);
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);
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);
180 g_main_loop_run (loop);
182 g_assert_cmpint (a, ==, 10);
183 g_assert_cmpint (b, ==, 4);
184 g_assert_cmpint (c, ==, 3);
186 g_main_loop_unref (loop);
187 g_main_context_unref (ctx);
191 test_priorities (void)
199 ctx = g_main_context_new ();
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);
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);
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);
218 g_assert (g_main_context_iteration (ctx, FALSE));
219 g_assert_cmpint (a, ==, 0);
220 g_assert_cmpint (b, ==, 2);
222 g_source_destroy (sourceb);
224 g_assert (g_main_context_iteration (ctx, FALSE));
225 g_assert_cmpint (a, ==, 1);
226 g_assert_cmpint (b, ==, 2);
228 g_assert (g_main_context_pending (ctx));
229 g_source_destroy (sourcea);
230 g_assert (!g_main_context_pending (ctx));
232 g_main_context_unref (ctx);
241 g_assert (data == g_thread_self ());
249 call_func (gpointer data)
251 func (g_thread_self ());
253 return G_SOURCE_REMOVE;
260 thread_func (gpointer data)
262 GMainContext *ctx = data;
265 g_main_context_push_thread_default (ctx);
267 g_mutex_lock (&mutex);
268 g_cond_signal (&cond);
269 g_mutex_unlock (&mutex);
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);
277 g_main_context_iteration (ctx, TRUE);
290 /* this one gets invoked directly */
291 g_main_context_invoke (NULL, func, g_thread_self ());
292 g_assert_cmpint (count, ==, 1);
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);
299 /* test thread-default forcing the invocation to go
302 ctx = g_main_context_new ();
303 g_mutex_lock (&mutex);
304 thread = g_thread_new ("worker", thread_func, ctx);
306 g_cond_wait (&cond, &mutex);
308 g_main_context_invoke (ctx, func, thread);
309 g_assert_cmpint (count, ==, 2);
311 g_thread_join (thread);
312 g_assert_cmpint (count, ==, 3);
316 main (int argc, char *argv[])
318 g_test_init (&argc, &argv, NULL);
320 g_test_add_func ("/maincontext/basic", test_maincontext_basic);
321 g_test_add_func ("/mainloop/basic", test_mainloop_basic);
322 g_test_add_func ("/mainloop/timeouts", test_timeouts);
323 g_test_add_func ("/mainloop/priorities", test_priorities);
324 g_test_add_func ("/mainloop/invoke", test_invoke);
326 return g_test_run ();