2 * Copyright (C) 2011 Red Hat, Inc.
4 * This work is provided "as is"; redistribution and modification
5 * in whole or in part, in any medium, physical or electronic is
6 * permitted without restriction.
8 * This work is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * In no event shall the authors or contributors be liable for any
13 * direct, indirect, incidental, special, exemplary, or consequential
14 * damages (including, but not limited to, procurement of substitute
15 * goods or services; loss of use, data, or profits; or business
16 * interruption) however caused and on any theory of liability, whether
17 * in contract, strict liability, or tort (including negligence or
18 * otherwise) arising in any way out of the use of this software, even
19 * if advised of the possibility of such damage.
21 * Author: Colin Walters <walters@verbum.org>
29 static char *echo_prog_path;
33 gboolean child_exited;
36 } SpawnAsyncMultithreadedData;
39 on_child_exited (GPid pid,
43 SpawnAsyncMultithreadedData *data = datap;
45 data->child_exited = TRUE;
46 if (data->child_exited && data->stdout_done)
47 g_main_loop_quit (data->loop);
53 on_child_stdout (GIOChannel *channel,
54 GIOCondition condition,
60 SpawnAsyncMultithreadedData *data = datap;
62 if (condition & G_IO_IN)
65 status = g_io_channel_read_chars (channel, buf, sizeof (buf), &bytes_read, &error);
66 g_assert_no_error (error);
67 g_string_append_len (data->stdout_buf, buf, (gssize) bytes_read);
68 if (status == G_IO_STATUS_EOF)
69 data->stdout_done = TRUE;
71 if (condition & G_IO_HUP)
72 data->stdout_done = TRUE;
73 if (condition & G_IO_ERR)
74 g_error ("Error reading from child stdin");
76 if (data->child_exited && data->stdout_done)
77 g_main_loop_quit (data->loop);
79 return !data->stdout_done;
83 test_spawn_async (void)
90 GMainContext *context;
95 SpawnAsyncMultithreadedData data;
97 context = g_main_context_new ();
98 loop = g_main_loop_new (context, TRUE);
100 arg = g_strdup_printf ("thread %d", tnum);
102 argv = g_ptr_array_new ();
103 g_ptr_array_add (argv, echo_prog_path);
104 g_ptr_array_add (argv, arg);
105 g_ptr_array_add (argv, NULL);
107 g_spawn_async_with_pipes (NULL, (char**)argv->pdata, NULL, G_SPAWN_DO_NOT_REAP_CHILD, NULL, NULL, &pid, NULL,
108 &child_stdout_fd, NULL, &error);
109 g_assert_no_error (error);
110 g_ptr_array_free (argv, TRUE);
113 data.stdout_done = FALSE;
114 data.child_exited = FALSE;
115 data.stdout_buf = g_string_new (0);
117 source = g_child_watch_source_new (pid);
118 g_source_set_callback (source, (GSourceFunc)on_child_exited, &data, NULL);
119 g_source_attach (source, context);
120 g_source_unref (source);
122 channel = g_io_channel_unix_new (child_stdout_fd);
123 source = g_io_create_watch (channel, G_IO_IN | G_IO_HUP | G_IO_ERR);
124 g_source_set_callback (source, (GSourceFunc)on_child_stdout, &data, NULL);
125 g_source_attach (source, context);
126 g_source_unref (source);
128 g_main_loop_run (loop);
130 g_assert (data.child_exited);
131 g_assert (data.stdout_done);
132 g_assert_cmpstr (data.stdout_buf->str, ==, arg);
133 g_string_free (data.stdout_buf, TRUE);
135 g_io_channel_unref (channel);
136 g_main_context_unref (context);
137 g_main_loop_unref (loop);
143 test_spawn_sync (void)
146 GError *error = NULL;
152 arg = g_strdup_printf ("thread %d", tnum);
154 argv = g_ptr_array_new ();
155 g_ptr_array_add (argv, echo_prog_path);
156 g_ptr_array_add (argv, arg);
157 g_ptr_array_add (argv, NULL);
159 g_spawn_sync (NULL, (char**)argv->pdata, NULL, 0, NULL, NULL, &stdout_str, NULL, &estatus, &error);
160 g_assert_no_error (error);
161 g_assert_cmpstr (arg, ==, stdout_str);
164 g_ptr_array_free (argv, TRUE);
173 g_test_init (&argc, &argv, NULL);
175 dirname = g_path_get_dirname (argv[0]);
176 echo_prog_path = g_build_filename (dirname, "test-spawn-echo", NULL);
177 if (!g_file_test (echo_prog_path, G_FILE_TEST_EXISTS))
179 g_free (echo_prog_path);
180 echo_prog_path = g_build_filename (dirname, "lt-test-spawn-echo", NULL);
184 g_assert (g_file_test (echo_prog_path, G_FILE_TEST_EXISTS));
186 g_test_add_func ("/gthread/spawn-single-sync", test_spawn_sync);
187 g_test_add_func ("/gthread/spawn-single-async", test_spawn_async);