summaryrefslogtreecommitdiff
path: root/gthread
diff options
context:
space:
mode:
authorColin Walters <walters@verbum.org>2011-06-02 11:53:25 -0400
committerColin Walters <walters@verbum.org>2011-06-03 11:43:26 -0400
commitd51198baad17f88e046300a9375619b4bffcd881 (patch)
tree6f49d0c0ea101da4dd846ea88f8d027f501c4152 /gthread
parentc34a6a66e7b918983b561ee8ff1cf7abd3f4b347 (diff)
spawn-multithreaded: New test case
We didn't have any coverage of threads spawning processes, which we should definitely support. https://bugzilla.gnome.org/show_bug.cgi?id=651725
Diffstat (limited to 'gthread')
-rw-r--r--gthread/tests/Makefile.am9
-rw-r--r--gthread/tests/spawn-multithreaded.c231
-rw-r--r--gthread/tests/test-spawn-echo.c39
3 files changed, 278 insertions, 1 deletions
diff --git a/gthread/tests/Makefile.am b/gthread/tests/Makefile.am
index 5d0748dcd..d3b98a557 100644
--- a/gthread/tests/Makefile.am
+++ b/gthread/tests/Makefile.am
@@ -2,10 +2,13 @@ include $(top_srcdir)/Makefile.decl
INCLUDES = -g $(gthread_INCLUDES) $(GLIB_DEBUG_FLAGS)
-noinst_PROGRAMS = $(TEST_PROGS)
+noinst_PROGRAMS = $(TEST_PROGS) test-spawn-echo
progs_ldadd = $(top_builddir)/glib/libglib-2.0.la \
$(top_builddir)/gthread/libgthread-2.0.la
+test_spawn_echo_SOURCES = test-spawn-echo.c
+test_spawn_echo_LDADD = $(progs_ldadd)
+
TEST_PROGS += 1bit-mutex
1bit_mutex_LDADD = $(progs_ldadd) $(top_builddir)/gthread/libgthread-2.0.la
@@ -27,3 +30,7 @@ endif
TEST_PROGS += atomic
atomic_SOURCES = atomic.c
atomic_LDADD = $(progs_ldadd) $(top_builddir)/gthread/libgthread-2.0.la
+
+TEST_PROGS += spawn-multithreaded
+spawn_multithreaded_SOURCES = spawn-multithreaded.c
+spawn_multithreaded_LDADD = $(progs_ldadd) $(top_builddir)/gthread/libgthread-2.0.la
diff --git a/gthread/tests/spawn-multithreaded.c b/gthread/tests/spawn-multithreaded.c
new file mode 100644
index 000000000..29b83e9b6
--- /dev/null
+++ b/gthread/tests/spawn-multithreaded.c
@@ -0,0 +1,231 @@
+/*
+ * Copyright (C) 2011 Red Hat, Inc.
+ *
+ * This work is provided "as is"; redistribution and modification
+ * in whole or in part, in any medium, physical or electronic is
+ * permitted without restriction.
+ *
+ * This work is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * In no event shall the authors or contributors be liable for any
+ * direct, indirect, incidental, special, exemplary, or consequential
+ * damages (including, but not limited to, procurement of substitute
+ * goods or services; loss of use, data, or profits; or business
+ * interruption) however caused and on any theory of liability, whether
+ * in contract, strict liability, or tort (including negligence or
+ * otherwise) arising in any way out of the use of this software, even
+ * if advised of the possibility of such damage.
+ *
+ * Author: Colin Walters <walters@verbum.org>
+ */
+
+#include "config.h"
+
+#include <glib.h>
+#include <string.h>
+
+#define N_THREADS (100)
+
+static char *echo_prog_path;
+
+static void
+multithreaded_test_run (GThreadFunc function)
+{
+ int i;
+ GError *error = NULL;
+ GPtrArray *threads = g_ptr_array_new ();
+
+ for (i = 0; i < N_THREADS; i++)
+ {
+ GThread *thread;
+
+ thread = g_thread_create (function,
+ GINT_TO_POINTER (i), TRUE, &error);
+ g_assert_no_error (error);
+ g_ptr_array_add (threads, thread);
+ }
+
+ for (i = 0; i < N_THREADS; i++)
+ {
+ gpointer ret;
+ ret = g_thread_join (g_ptr_array_index (threads, i));
+ g_assert_cmpint (GPOINTER_TO_INT (ret), ==, i);
+ }
+ g_ptr_array_free (threads, TRUE);
+}
+
+static gpointer
+test_spawn_sync_multithreaded_instance (gpointer data)
+{
+ int tnum = GPOINTER_TO_INT (data);
+ GError *error = NULL;
+ GPtrArray *argv;
+ char *arg;
+ char *stdout_str;
+ int estatus;
+
+ arg = g_strdup_printf ("thread %d", tnum);
+
+ argv = g_ptr_array_new ();
+ g_ptr_array_add (argv, echo_prog_path);
+ g_ptr_array_add (argv, arg);
+ g_ptr_array_add (argv, NULL);
+
+ g_spawn_sync (NULL, (char**)argv->pdata, NULL, 0, NULL, NULL, &stdout_str, NULL, &estatus, &error);
+ g_assert_no_error (error);
+ g_assert_cmpstr (arg, ==, stdout_str);
+ g_free (arg);
+ g_free (stdout_str);
+ g_ptr_array_free (argv, TRUE);
+
+ return GINT_TO_POINTER (tnum);
+}
+
+static void
+test_spawn_sync_multithreaded (void)
+{
+ multithreaded_test_run (test_spawn_sync_multithreaded_instance);
+}
+
+typedef struct {
+ GMainLoop *loop;
+ gboolean child_exited;
+ gboolean stdout_done;
+ GString *stdout_buf;
+} SpawnAsyncMultithreadedData;
+
+static gboolean
+on_child_exited (GPid pid,
+ gint status,
+ gpointer datap)
+{
+ SpawnAsyncMultithreadedData *data = datap;
+
+ data->child_exited = TRUE;
+ if (data->child_exited && data->stdout_done)
+ g_main_loop_quit (data->loop);
+
+ return FALSE;
+}
+
+static gboolean
+on_child_stdout (GIOChannel *channel,
+ GIOCondition condition,
+ gpointer datap)
+{
+ char buf[1024];
+ GError *error = NULL;
+ gsize bytes_read;
+ SpawnAsyncMultithreadedData *data = datap;
+
+ if (condition & G_IO_IN)
+ {
+ GIOStatus status;
+ status = g_io_channel_read_chars (channel, buf, sizeof (buf), &bytes_read, &error);
+ g_assert_no_error (error);
+ g_string_append_len (data->stdout_buf, buf, (gssize) bytes_read);
+ if (status == G_IO_STATUS_EOF)
+ data->stdout_done = TRUE;
+ }
+ if (condition & G_IO_HUP)
+ data->stdout_done = TRUE;
+ if (condition & G_IO_ERR)
+ g_error ("Error reading from child stdin");
+
+ if (data->child_exited && data->stdout_done)
+ g_main_loop_quit (data->loop);
+
+ return !data->stdout_done;
+}
+
+static gpointer
+test_spawn_async_multithreaded_instance (gpointer thread_data)
+{
+ int tnum = GPOINTER_TO_INT (thread_data);
+ GError *error = NULL;
+ GPtrArray *argv;
+ char *arg;
+ GPid pid;
+ GMainContext *context;
+ GMainLoop *loop;
+ GIOChannel *channel;
+ GSource *source;
+ int child_stdout_fd;
+ SpawnAsyncMultithreadedData data;
+
+ context = g_main_context_new ();
+ loop = g_main_loop_new (context, TRUE);
+
+ arg = g_strdup_printf ("thread %d", tnum);
+
+ argv = g_ptr_array_new ();
+ g_ptr_array_add (argv, echo_prog_path);
+ g_ptr_array_add (argv, arg);
+ g_ptr_array_add (argv, NULL);
+
+ g_spawn_async_with_pipes (NULL, (char**)argv->pdata, NULL, G_SPAWN_DO_NOT_REAP_CHILD, NULL, NULL, &pid, NULL,
+ &child_stdout_fd, NULL, &error);
+ g_assert_no_error (error);
+ g_ptr_array_free (argv, TRUE);
+
+ data.loop = loop;
+ data.stdout_done = FALSE;
+ data.child_exited = FALSE;
+ data.stdout_buf = g_string_new (0);
+
+ source = g_child_watch_source_new (pid);
+ g_source_set_callback (source, (GSourceFunc)on_child_exited, &data, NULL);
+ g_source_attach (source, context);
+ g_source_unref (source);
+
+ channel = g_io_channel_unix_new (child_stdout_fd);
+ source = g_io_create_watch (channel, G_IO_IN | G_IO_HUP | G_IO_ERR);
+ g_source_set_callback (source, (GSourceFunc)on_child_stdout, &data, NULL);
+ g_source_attach (source, context);
+ g_source_unref (source);
+
+ g_main_loop_run (loop);
+
+ g_assert (data.child_exited);
+ g_assert (data.stdout_done);
+ g_assert_cmpstr (data.stdout_buf->str, ==, arg);
+ g_string_free (data.stdout_buf, TRUE);
+
+ g_io_channel_unref (channel);
+ g_main_context_unref (context);
+ g_main_loop_unref (loop);
+
+ g_free (arg);
+
+ return GINT_TO_POINTER (tnum);
+}
+
+static void
+test_spawn_async_multithreaded (void)
+{
+ multithreaded_test_run (test_spawn_async_multithreaded_instance);
+}
+
+int
+main (int argc,
+ char *argv[])
+{
+ char *dirname;
+
+ g_test_init (&argc, &argv, NULL);
+
+ g_thread_init (NULL);
+
+ dirname = g_path_get_dirname (argv[0]);
+ echo_prog_path = g_build_filename (dirname, "test-spawn-echo", NULL);
+ g_free (dirname);
+
+ g_assert (g_file_test (echo_prog_path, G_FILE_TEST_EXISTS));
+
+ g_test_add_func ("/gthread/spawn-sync", test_spawn_sync_multithreaded);
+ g_test_add_func ("/gthread/spawn-async", test_spawn_async_multithreaded);
+
+ return g_test_run();
+}
diff --git a/gthread/tests/test-spawn-echo.c b/gthread/tests/test-spawn-echo.c
new file mode 100644
index 000000000..cb387ba84
--- /dev/null
+++ b/gthread/tests/test-spawn-echo.c
@@ -0,0 +1,39 @@
+/*
+ * Copyright (C) 2011 Red Hat, Inc.
+ *
+ * This work is provided "as is"; redistribution and modification
+ * in whole or in part, in any medium, physical or electronic is
+ * permitted without restriction.
+ *
+ * This work is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * In no event shall the authors or contributors be liable for any
+ * direct, indirect, incidental, special, exemplary, or consequential
+ * damages (including, but not limited to, procurement of substitute
+ * goods or services; loss of use, data, or profits; or business
+ * interruption) however caused and on any theory of liability, whether
+ * in contract, strict liability, or tort (including negligence or
+ * otherwise) arising in any way out of the use of this software, even
+ * if advised of the possibility of such damage.
+ *
+ * Author: Colin Walters <walters@verbum.org>
+ */
+
+#include "config.h"
+
+#include "glib.h"
+
+int
+main (int argc,
+ char *argv[])
+{
+ int i;
+ for (i = 1; i < argc; i++)
+ {
+ g_print ("%s", argv[i]);
+ }
+
+ return 0;
+}