1 #include <glib-object.h>
12 test_source (GSource *one, GCallback quit_callback)
17 /* Callback with GMainLoop user_data */
18 loop = g_main_loop_new (NULL, FALSE);
20 closure = g_cclosure_new (quit_callback, loop, NULL);
21 g_source_set_closure (one, closure);
23 g_source_attach (one, NULL);
24 g_main_loop_run (loop);
26 g_source_destroy (one);
27 g_main_loop_unref (loop);
31 simple_quit_callback (gpointer user_data)
33 GMainLoop *loop = user_data;
35 g_main_loop_quit (loop);
41 test_closure_idle (void)
45 source = g_idle_source_new ();
46 test_source (source, G_CALLBACK (simple_quit_callback));
47 g_source_unref (source);
51 test_closure_timeout (void)
55 source = g_timeout_source_new (10);
56 test_source (source, G_CALLBACK (simple_quit_callback));
57 g_source_unref (source);
61 iochannel_quit_callback (GIOChannel *channel,
65 GMainLoop *loop = user_data;
67 g_main_loop_quit (loop);
73 test_closure_iochannel (void)
80 if (g_path_is_absolute (g_get_prgname ()))
81 path = g_strdup (g_get_prgname ());
84 path = g_test_build_filename (G_TEST_BUILT,
88 chan = g_io_channel_new_file (path, "r", &error);
89 g_assert_no_error (error);
92 source = g_io_create_watch (chan, G_IO_IN);
93 test_source (source, G_CALLBACK (iochannel_quit_callback));
94 g_source_unref (source);
96 g_io_channel_unref (chan);
100 test_closure_child (void)
104 GError *error = NULL;
107 g_assert (g_getenv ("DO_NOT_ACCIDENTALLY_RECURSE") == NULL);
108 g_setenv ("DO_NOT_ACCIDENTALLY_RECURSE", "1", TRUE);
110 if (g_path_is_absolute (g_get_prgname ()))
111 argv[0] = g_strdup (g_get_prgname ());
114 argv[0] = g_test_build_filename (G_TEST_BUILT,
121 g_spawn_async (NULL, argv, NULL,
122 G_SPAWN_STDOUT_TO_DEV_NULL |
123 G_SPAWN_STDERR_TO_DEV_NULL |
124 G_SPAWN_DO_NOT_REAP_CHILD,
127 g_assert_no_error (error);
131 source = g_child_watch_source_new (pid);
132 test_source (source, G_CALLBACK (iochannel_quit_callback));
133 g_source_unref (source);
138 fd_quit_callback (gint fd,
139 GIOCondition condition,
142 GMainLoop *loop = user_data;
144 g_main_loop_quit (loop);
150 test_closure_fd (void)
155 fd = open ("/dev/null", O_RDONLY);
158 source = g_unix_fd_source_new (fd, G_IO_IN);
159 test_source (source, G_CALLBACK (fd_quit_callback));
160 g_source_unref (source);
166 send_usr1 (gpointer user_data)
168 kill (getpid (), SIGUSR1);
173 closure_quit_callback (gpointer user_data)
175 GMainLoop *loop = user_data;
177 g_main_loop_quit (loop);
183 test_closure_signal (void)
187 g_idle_add_full (G_PRIORITY_LOW, send_usr1, NULL, NULL);
189 source = g_unix_signal_source_new (SIGUSR1);
190 test_source (source, G_CALLBACK (closure_quit_callback));
191 g_source_unref (source);
200 sigset_t sig_mask, old_mask;
202 sigemptyset (&sig_mask);
203 sigaddset (&sig_mask, SIGUSR1);
204 if (sigprocmask (SIG_UNBLOCK, &sig_mask, &old_mask) == 0)
206 if (sigismember (&old_mask, SIGUSR1))
207 g_message ("SIGUSR1 was blocked, unblocking it");
211 g_test_init (&argc, &argv, NULL);
213 g_test_add_func ("/closure/idle", test_closure_idle);
214 g_test_add_func ("/closure/timeout", test_closure_timeout);
215 g_test_add_func ("/closure/iochannel", test_closure_iochannel);
216 g_test_add_func ("/closure/child", test_closure_child);
218 g_test_add_func ("/closure/fd", test_closure_fd);
219 g_test_add_func ("/closure/signal", test_closure_signal);
222 return g_test_run ();