1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
|
#include <glib-object.h>
#ifdef G_OS_UNIX
#include <glib-unix.h>
#include <fcntl.h>
#include <signal.h>
#include <unistd.h>
#endif
static void
test_source (GSource *one, GCallback quit_callback)
{
GClosure *closure;
GMainLoop *loop;
/* Callback with GMainLoop user_data */
loop = g_main_loop_new (NULL, FALSE);
closure = g_cclosure_new (quit_callback, loop, NULL);
g_source_set_closure (one, closure);
g_source_attach (one, NULL);
g_main_loop_run (loop);
g_source_destroy (one);
g_main_loop_unref (loop);
}
static gboolean
simple_quit_callback (gpointer user_data)
{
GMainLoop *loop = user_data;
g_main_loop_quit (loop);
return TRUE;
}
static void
test_closure_idle (void)
{
GSource *source;
source = g_idle_source_new ();
test_source (source, G_CALLBACK (simple_quit_callback));
g_source_unref (source);
}
static void
test_closure_timeout (void)
{
GSource *source;
source = g_timeout_source_new (10);
test_source (source, G_CALLBACK (simple_quit_callback));
g_source_unref (source);
}
static gboolean
iochannel_quit_callback (GIOChannel *channel,
GIOCondition cond,
gpointer user_data)
{
GMainLoop *loop = user_data;
g_main_loop_quit (loop);
return TRUE;
}
static void
test_closure_iochannel (void)
{
GIOChannel *chan;
GSource *source;
char *path;
GError *error = NULL;
if (g_path_is_absolute (g_get_prgname ()))
path = g_strdup (g_get_prgname ());
else
{
path = g_test_build_filename (G_TEST_BUILT,
g_get_prgname (),
NULL);
}
chan = g_io_channel_new_file (path, "r", &error);
g_assert_no_error (error);
g_free (path);
source = g_io_create_watch (chan, G_IO_IN);
test_source (source, G_CALLBACK (iochannel_quit_callback));
g_source_unref (source);
g_io_channel_unref (chan);
}
static void
test_closure_child (void)
{
GSource *source;
GPid pid;
GError *error = NULL;
gchar *argv[3];
g_assert (g_getenv ("DO_NOT_ACCIDENTALLY_RECURSE") == NULL);
g_setenv ("DO_NOT_ACCIDENTALLY_RECURSE", "1", TRUE);
if (g_path_is_absolute (g_get_prgname ()))
argv[0] = g_strdup (g_get_prgname ());
else
{
argv[0] = g_test_build_filename (G_TEST_BUILT,
g_get_prgname (),
NULL);
}
argv[1] = "-l";
argv[2] = NULL;
g_spawn_async (NULL, argv, NULL,
G_SPAWN_STDOUT_TO_DEV_NULL |
G_SPAWN_STDERR_TO_DEV_NULL |
G_SPAWN_DO_NOT_REAP_CHILD,
NULL, NULL,
&pid, &error);
g_assert_no_error (error);
g_free (argv[0]);
source = g_child_watch_source_new (pid);
test_source (source, G_CALLBACK (iochannel_quit_callback));
g_source_unref (source);
}
#ifdef G_OS_UNIX
static gboolean
fd_quit_callback (gint fd,
GIOCondition condition,
gpointer user_data)
{
GMainLoop *loop = user_data;
g_main_loop_quit (loop);
return TRUE;
}
static void
test_closure_fd (void)
{
gint fd;
GSource *source;
fd = open ("/dev/null", O_RDONLY);
g_assert (fd != -1);
source = g_unix_fd_source_new (fd, G_IO_IN);
test_source (source, G_CALLBACK (fd_quit_callback));
g_source_unref (source);
close (fd);
}
static gboolean
send_usr1 (gpointer user_data)
{
kill (getpid (), SIGUSR1);
return FALSE;
}
static gboolean
closure_quit_callback (gpointer user_data)
{
GMainLoop *loop = user_data;
g_main_loop_quit (loop);
return TRUE;
}
static void
test_closure_signal (void)
{
GSource *source;
g_idle_add_full (G_PRIORITY_LOW, send_usr1, NULL, NULL);
source = g_unix_signal_source_new (SIGUSR1);
test_source (source, G_CALLBACK (closure_quit_callback));
g_source_unref (source);
}
#endif
int
main (int argc,
char *argv[])
{
#ifndef G_OS_WIN32
sigset_t sig_mask, old_mask;
sigemptyset (&sig_mask);
sigaddset (&sig_mask, SIGUSR1);
if (sigprocmask (SIG_UNBLOCK, &sig_mask, &old_mask) == 0)
{
if (sigismember (&old_mask, SIGUSR1))
g_message ("SIGUSR1 was blocked, unblocking it");
}
#endif
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/closure/idle", test_closure_idle);
g_test_add_func ("/closure/timeout", test_closure_timeout);
g_test_add_func ("/closure/iochannel", test_closure_iochannel);
g_test_add_func ("/closure/child", test_closure_child);
#ifdef G_OS_UNIX
g_test_add_func ("/closure/fd", test_closure_fd);
g_test_add_func ("/closure/signal", test_closure_signal);
#endif
return g_test_run ();
}
|