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