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 #define N_THREADS (100)
31 static char *echo_prog_path;
34 multithreaded_test_run (GThreadFunc function)
37 GPtrArray *threads = g_ptr_array_new ();
39 for (i = 0; i < N_THREADS; i++)
43 thread = g_thread_new ("test", function, GINT_TO_POINTER (i));
44 g_ptr_array_add (threads, thread);
47 for (i = 0; i < N_THREADS; i++)
50 ret = g_thread_join (g_ptr_array_index (threads, i));
51 g_assert_cmpint (GPOINTER_TO_INT (ret), ==, i);
53 g_ptr_array_free (threads, TRUE);
57 test_spawn_sync_multithreaded_instance (gpointer data)
59 int tnum = GPOINTER_TO_INT (data);
66 arg = g_strdup_printf ("thread %d", tnum);
68 argv = g_ptr_array_new ();
69 g_ptr_array_add (argv, echo_prog_path);
70 g_ptr_array_add (argv, arg);
71 g_ptr_array_add (argv, NULL);
73 g_spawn_sync (NULL, (char**)argv->pdata, NULL, 0, NULL, NULL, &stdout_str, NULL, &estatus, &error);
74 g_assert_no_error (error);
75 g_assert_cmpstr (arg, ==, stdout_str);
78 g_ptr_array_free (argv, TRUE);
80 return GINT_TO_POINTER (tnum);
84 test_spawn_sync_multithreaded (void)
86 multithreaded_test_run (test_spawn_sync_multithreaded_instance);
91 gboolean child_exited;
94 } SpawnAsyncMultithreadedData;
97 on_child_exited (GPid pid,
101 SpawnAsyncMultithreadedData *data = datap;
103 data->child_exited = TRUE;
104 if (data->child_exited && data->stdout_done)
105 g_main_loop_quit (data->loop);
107 return G_SOURCE_REMOVE;
111 on_child_stdout (GIOChannel *channel,
112 GIOCondition condition,
116 GError *error = NULL;
119 SpawnAsyncMultithreadedData *data = datap;
122 status = g_io_channel_read_chars (channel, buf, sizeof (buf), &bytes_read, &error);
123 if (status == G_IO_STATUS_NORMAL)
125 g_string_append_len (data->stdout_buf, buf, (gssize) bytes_read);
126 if (bytes_read == sizeof (buf))
129 else if (status == G_IO_STATUS_EOF)
131 g_string_append_len (data->stdout_buf, buf, (gssize) bytes_read);
132 data->stdout_done = TRUE;
134 else if (status == G_IO_STATUS_ERROR)
136 g_error ("Error reading from child stdin: %s", error->message);
139 if (data->child_exited && data->stdout_done)
140 g_main_loop_quit (data->loop);
142 return !data->stdout_done;
146 test_spawn_async_multithreaded_instance (gpointer thread_data)
148 int tnum = GPOINTER_TO_INT (thread_data);
149 GError *error = NULL;
153 GMainContext *context;
158 SpawnAsyncMultithreadedData data;
160 context = g_main_context_new ();
161 loop = g_main_loop_new (context, TRUE);
163 arg = g_strdup_printf ("thread %d", tnum);
165 argv = g_ptr_array_new ();
166 g_ptr_array_add (argv, echo_prog_path);
167 g_ptr_array_add (argv, arg);
168 g_ptr_array_add (argv, NULL);
170 g_spawn_async_with_pipes (NULL, (char**)argv->pdata, NULL, G_SPAWN_DO_NOT_REAP_CHILD, NULL, NULL, &pid, NULL,
171 &child_stdout_fd, NULL, &error);
172 g_assert_no_error (error);
173 g_ptr_array_free (argv, TRUE);
176 data.stdout_done = FALSE;
177 data.child_exited = FALSE;
178 data.stdout_buf = g_string_new (0);
180 source = g_child_watch_source_new (pid);
181 g_source_set_callback (source, (GSourceFunc)on_child_exited, &data, NULL);
182 g_source_attach (source, context);
183 g_source_unref (source);
185 channel = g_io_channel_unix_new (child_stdout_fd);
186 source = g_io_create_watch (channel, G_IO_IN | G_IO_HUP);
187 g_source_set_callback (source, (GSourceFunc)on_child_stdout, &data, NULL);
188 g_source_attach (source, context);
189 g_source_unref (source);
191 g_main_loop_run (loop);
193 g_assert (data.child_exited);
194 g_assert (data.stdout_done);
195 g_assert_cmpstr (data.stdout_buf->str, ==, arg);
196 g_string_free (data.stdout_buf, TRUE);
198 g_io_channel_unref (channel);
199 g_main_context_unref (context);
200 g_main_loop_unref (loop);
204 return GINT_TO_POINTER (tnum);
208 test_spawn_async_multithreaded (void)
210 multithreaded_test_run (test_spawn_async_multithreaded_instance);
219 g_test_init (&argc, &argv, NULL);
221 dirname = g_path_get_dirname (argv[0]);
222 echo_prog_path = g_build_filename (dirname, "test-spawn-echo", NULL);
223 if (!g_file_test (echo_prog_path, G_FILE_TEST_EXISTS))
225 g_free (echo_prog_path);
226 echo_prog_path = g_build_filename (dirname, "lt-test-spawn-echo", NULL);
230 g_assert (g_file_test (echo_prog_path, G_FILE_TEST_EXISTS));
232 g_test_add_func ("/gthread/spawn-sync", test_spawn_sync_multithreaded);
233 g_test_add_func ("/gthread/spawn-async", test_spawn_async_multithreaded);