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;
32 multithreaded_test_run (GThreadFunc function)
35 GPtrArray *threads = g_ptr_array_new ();
38 /* Limit to 64, otherwise we may hit file descriptor limits and such */
39 n_threads = MIN (g_get_num_processors () * 2, 64);
41 for (i = 0; i < n_threads; i++)
45 thread = g_thread_new ("test", function, GINT_TO_POINTER (i));
46 g_ptr_array_add (threads, thread);
49 for (i = 0; i < n_threads; i++)
52 ret = g_thread_join (g_ptr_array_index (threads, i));
53 g_assert_cmpint (GPOINTER_TO_INT (ret), ==, i);
55 g_ptr_array_free (threads, TRUE);
59 test_spawn_sync_multithreaded_instance (gpointer data)
61 int tnum = GPOINTER_TO_INT (data);
68 arg = g_strdup_printf ("thread %d", tnum);
70 argv = g_ptr_array_new ();
71 g_ptr_array_add (argv, echo_prog_path);
72 g_ptr_array_add (argv, arg);
73 g_ptr_array_add (argv, NULL);
75 g_spawn_sync (NULL, (char**)argv->pdata, NULL, G_SPAWN_DEFAULT, NULL, NULL, &stdout_str, NULL, &estatus, &error);
76 g_assert_no_error (error);
77 g_assert_cmpstr (arg, ==, stdout_str);
80 g_ptr_array_free (argv, TRUE);
82 return GINT_TO_POINTER (tnum);
86 test_spawn_sync_multithreaded (void)
88 multithreaded_test_run (test_spawn_sync_multithreaded_instance);
93 gboolean child_exited;
96 } SpawnAsyncMultithreadedData;
99 on_child_exited (GPid pid,
103 SpawnAsyncMultithreadedData *data = datap;
105 data->child_exited = TRUE;
106 if (data->child_exited && data->stdout_done)
107 g_main_loop_quit (data->loop);
109 return G_SOURCE_REMOVE;
113 on_child_stdout (GIOChannel *channel,
114 GIOCondition condition,
118 GError *error = NULL;
121 SpawnAsyncMultithreadedData *data = datap;
124 status = g_io_channel_read_chars (channel, buf, sizeof (buf), &bytes_read, &error);
125 if (status == G_IO_STATUS_NORMAL)
127 g_string_append_len (data->stdout_buf, buf, (gssize) bytes_read);
128 if (bytes_read == sizeof (buf))
131 else if (status == G_IO_STATUS_EOF)
133 g_string_append_len (data->stdout_buf, buf, (gssize) bytes_read);
134 data->stdout_done = TRUE;
136 else if (status == G_IO_STATUS_ERROR)
138 g_error ("Error reading from child stdin: %s", error->message);
141 if (data->child_exited && data->stdout_done)
142 g_main_loop_quit (data->loop);
144 return !data->stdout_done;
148 test_spawn_async_multithreaded_instance (gpointer thread_data)
150 int tnum = GPOINTER_TO_INT (thread_data);
151 GError *error = NULL;
155 GMainContext *context;
160 SpawnAsyncMultithreadedData data;
162 context = g_main_context_new ();
163 loop = g_main_loop_new (context, TRUE);
165 arg = g_strdup_printf ("thread %d", tnum);
167 argv = g_ptr_array_new ();
168 g_ptr_array_add (argv, echo_prog_path);
169 g_ptr_array_add (argv, arg);
170 g_ptr_array_add (argv, NULL);
172 g_spawn_async_with_pipes (NULL, (char**)argv->pdata, NULL, G_SPAWN_DO_NOT_REAP_CHILD, NULL, NULL, &pid, NULL,
173 &child_stdout_fd, NULL, &error);
174 g_assert_no_error (error);
175 g_ptr_array_free (argv, TRUE);
178 data.stdout_done = FALSE;
179 data.child_exited = FALSE;
180 data.stdout_buf = g_string_new (0);
182 source = g_child_watch_source_new (pid);
183 g_source_set_callback (source, (GSourceFunc)on_child_exited, &data, NULL);
184 g_source_attach (source, context);
185 g_source_unref (source);
187 channel = g_io_channel_unix_new (child_stdout_fd);
188 source = g_io_create_watch (channel, G_IO_IN | G_IO_HUP);
189 g_source_set_callback (source, (GSourceFunc)on_child_stdout, &data, NULL);
190 g_source_attach (source, context);
191 g_source_unref (source);
193 g_main_loop_run (loop);
195 g_assert (data.child_exited);
196 g_assert (data.stdout_done);
197 g_assert_cmpstr (data.stdout_buf->str, ==, arg);
198 g_string_free (data.stdout_buf, TRUE);
200 g_io_channel_unref (channel);
201 g_main_context_unref (context);
202 g_main_loop_unref (loop);
206 return GINT_TO_POINTER (tnum);
210 test_spawn_async_multithreaded (void)
212 multithreaded_test_run (test_spawn_async_multithreaded_instance);
222 g_test_init (&argc, &argv, NULL);
224 dirname = g_path_get_dirname (argv[0]);
225 echo_prog_path = g_build_filename (dirname, "test-spawn-echo" EXEEXT, NULL);
226 if (!g_file_test (echo_prog_path, G_FILE_TEST_EXISTS))
228 g_free (echo_prog_path);
229 echo_prog_path = g_build_filename (dirname, "lt-test-spawn-echo" EXEEXT, NULL);
233 g_assert (g_file_test (echo_prog_path, G_FILE_TEST_EXISTS));
235 g_test_add_func ("/gthread/spawn-sync", test_spawn_sync_multithreaded);
236 g_test_add_func ("/gthread/spawn-async", test_spawn_async_multithreaded);
240 g_free (echo_prog_path);