Blame glib/tests/spawn-singlethread.c

Packit ae235b
/* 
Packit ae235b
 * Copyright (C) 2011 Red Hat, Inc.
Packit ae235b
 *
Packit ae235b
 * This work is provided "as is"; redistribution and modification
Packit ae235b
 * in whole or in part, in any medium, physical or electronic is
Packit ae235b
 * permitted without restriction.
Packit ae235b
 *
Packit ae235b
 * This work is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Packit ae235b
 *
Packit ae235b
 * In no event shall the authors or contributors be liable for any
Packit ae235b
 * direct, indirect, incidental, special, exemplary, or consequential
Packit ae235b
 * damages (including, but not limited to, procurement of substitute
Packit ae235b
 * goods or services; loss of use, data, or profits; or business
Packit ae235b
 * interruption) however caused and on any theory of liability, whether
Packit ae235b
 * in contract, strict liability, or tort (including negligence or
Packit ae235b
 * otherwise) arising in any way out of the use of this software, even
Packit ae235b
 * if advised of the possibility of such damage.
Packit ae235b
 *
Packit ae235b
 * Author: Colin Walters <walters@verbum.org> 
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include <glib.h>
Packit ae235b
#include <string.h>
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
#define LINEEND "\r\n"
Packit ae235b
#else
Packit ae235b
#define LINEEND "\n"
Packit ae235b
#endif
Packit ae235b
Packit ae235b
/* MinGW builds are likely done using a BASH-style shell, so run the
Packit ae235b
 * normal script there, as on non-Windows builds, as it is more likely
Packit ae235b
 * that one will run 'make check' in such shells to test the code
Packit ae235b
 */
Packit ae235b
#if defined (G_OS_WIN32) && defined (_MSC_VER)
Packit ae235b
#define SCRIPT_EXT ".bat"
Packit ae235b
#else
Packit ae235b
#define SCRIPT_EXT
Packit ae235b
#endif
Packit ae235b
Packit ae235b
static char *echo_prog_path;
Packit ae235b
static char *echo_script_path;
Packit ae235b
Packit ae235b
typedef struct {
Packit ae235b
  GMainLoop *loop;
Packit ae235b
  gboolean child_exited;
Packit ae235b
  gboolean stdout_done;
Packit ae235b
  GString *stdout_buf;
Packit ae235b
} SpawnAsyncMultithreadedData;
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
on_child_exited (GPid     pid,
Packit ae235b
		 gint     status,
Packit ae235b
		 gpointer datap)
Packit ae235b
{
Packit ae235b
  SpawnAsyncMultithreadedData *data = datap;
Packit ae235b
Packit ae235b
  data->child_exited = TRUE;
Packit ae235b
  if (data->child_exited && data->stdout_done)
Packit ae235b
    g_main_loop_quit (data->loop);
Packit ae235b
  
Packit ae235b
  return G_SOURCE_REMOVE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
on_child_stdout (GIOChannel   *channel,
Packit ae235b
		 GIOCondition  condition,
Packit ae235b
		 gpointer      datap)
Packit ae235b
{
Packit ae235b
  char buf[1024];
Packit ae235b
  GError *error = NULL;
Packit ae235b
  gsize bytes_read;
Packit ae235b
  SpawnAsyncMultithreadedData *data = datap;
Packit ae235b
Packit ae235b
  if (condition & G_IO_IN)
Packit ae235b
    {
Packit ae235b
      GIOStatus status;
Packit ae235b
      status = g_io_channel_read_chars (channel, buf, sizeof (buf), &bytes_read, &error);
Packit ae235b
      g_assert_no_error (error);
Packit ae235b
      g_string_append_len (data->stdout_buf, buf, (gssize) bytes_read);
Packit ae235b
      if (status == G_IO_STATUS_EOF)
Packit ae235b
	data->stdout_done = TRUE;
Packit ae235b
    }
Packit ae235b
  if (condition & G_IO_HUP)
Packit ae235b
    data->stdout_done = TRUE;
Packit ae235b
  if (condition & G_IO_ERR)
Packit ae235b
    g_error ("Error reading from child stdin");
Packit ae235b
Packit ae235b
  if (data->child_exited && data->stdout_done)
Packit ae235b
    g_main_loop_quit (data->loop);
Packit ae235b
Packit ae235b
  return !data->stdout_done;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_spawn_async (void)
Packit ae235b
{
Packit ae235b
  int tnum = 1;
Packit ae235b
  GError *error = NULL;
Packit ae235b
  GPtrArray *argv;
Packit ae235b
  char *arg;
Packit ae235b
  GPid pid;
Packit ae235b
  GMainContext *context;
Packit ae235b
  GMainLoop *loop;
Packit ae235b
  GIOChannel *channel;
Packit ae235b
  GSource *source;
Packit ae235b
  int child_stdout_fd;
Packit ae235b
  SpawnAsyncMultithreadedData data;
Packit ae235b
Packit ae235b
  context = g_main_context_new ();
Packit ae235b
  loop = g_main_loop_new (context, TRUE);
Packit ae235b
Packit ae235b
  arg = g_strdup_printf ("thread %d", tnum);
Packit ae235b
Packit ae235b
  argv = g_ptr_array_new ();
Packit ae235b
  g_ptr_array_add (argv, echo_prog_path);
Packit ae235b
  g_ptr_array_add (argv, arg);
Packit ae235b
  g_ptr_array_add (argv, NULL);
Packit ae235b
Packit ae235b
  g_spawn_async_with_pipes (NULL, (char**)argv->pdata, NULL, G_SPAWN_DO_NOT_REAP_CHILD, NULL, NULL, &pid, NULL,
Packit ae235b
			    &child_stdout_fd, NULL, &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_ptr_array_free (argv, TRUE);
Packit ae235b
Packit ae235b
  data.loop = loop;
Packit ae235b
  data.stdout_done = FALSE;
Packit ae235b
  data.child_exited = FALSE;
Packit ae235b
  data.stdout_buf = g_string_new (0);
Packit ae235b
Packit ae235b
  source = g_child_watch_source_new (pid);
Packit ae235b
  g_source_set_callback (source, (GSourceFunc)on_child_exited, &data, NULL);
Packit ae235b
  g_source_attach (source, context);
Packit ae235b
  g_source_unref (source);
Packit ae235b
Packit ae235b
  channel = g_io_channel_unix_new (child_stdout_fd);
Packit ae235b
  source = g_io_create_watch (channel, G_IO_IN | G_IO_HUP | G_IO_ERR);
Packit ae235b
  g_source_set_callback (source, (GSourceFunc)on_child_stdout, &data, NULL);
Packit ae235b
  g_source_attach (source, context);
Packit ae235b
  g_source_unref (source);
Packit ae235b
Packit ae235b
  g_main_loop_run (loop);
Packit ae235b
Packit ae235b
  g_assert (data.child_exited);
Packit ae235b
  g_assert (data.stdout_done);
Packit ae235b
  g_assert_cmpstr (data.stdout_buf->str, ==, arg);
Packit ae235b
  g_string_free (data.stdout_buf, TRUE);
Packit ae235b
Packit ae235b
  g_io_channel_unref (channel);
Packit ae235b
  g_main_context_unref (context);
Packit ae235b
  g_main_loop_unref (loop);
Packit ae235b
Packit ae235b
  g_free (arg);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_spawn_sync (void)
Packit ae235b
{
Packit ae235b
  int tnum = 1;
Packit ae235b
  GError *error = NULL;
Packit ae235b
  GPtrArray *argv;
Packit ae235b
  char *arg;
Packit ae235b
  char *stdout_str;
Packit ae235b
  int estatus;
Packit ae235b
Packit ae235b
  arg = g_strdup_printf ("thread %d", tnum);
Packit ae235b
Packit ae235b
  argv = g_ptr_array_new ();
Packit ae235b
  g_ptr_array_add (argv, echo_prog_path);
Packit ae235b
  g_ptr_array_add (argv, arg);
Packit ae235b
  g_ptr_array_add (argv, NULL);
Packit ae235b
Packit ae235b
  g_spawn_sync (NULL, (char**)argv->pdata, NULL, 0, NULL, NULL, &stdout_str, NULL, &estatus, &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_assert_cmpstr (arg, ==, stdout_str);
Packit ae235b
  g_free (arg);
Packit ae235b
  g_free (stdout_str);
Packit ae235b
  g_ptr_array_free (argv, TRUE);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_spawn_script (void)
Packit ae235b
{
Packit ae235b
  GError *error = NULL;
Packit ae235b
  GPtrArray *argv;
Packit ae235b
  char *stdout_str;
Packit ae235b
  int estatus;
Packit ae235b
Packit ae235b
  argv = g_ptr_array_new ();
Packit ae235b
  g_ptr_array_add (argv, echo_script_path);
Packit ae235b
  g_ptr_array_add (argv, NULL);
Packit ae235b
Packit ae235b
  g_spawn_sync (NULL, (char**)argv->pdata, NULL, 0, NULL, NULL, &stdout_str, NULL, &estatus, &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_assert_cmpstr ("echo" LINEEND, ==, stdout_str);
Packit ae235b
  g_free (stdout_str);
Packit ae235b
  g_ptr_array_free (argv, TRUE);
Packit ae235b
}
Packit ae235b
Packit ae235b
int
Packit ae235b
main (int   argc,
Packit ae235b
      char *argv[])
Packit ae235b
{
Packit ae235b
  char *dirname;
Packit ae235b
  int ret;
Packit ae235b
Packit ae235b
  g_test_init (&argc, &argv, NULL);
Packit ae235b
Packit ae235b
  dirname = g_path_get_dirname (argv[0]);
Packit ae235b
  echo_prog_path = g_build_filename (dirname, "test-spawn-echo" EXEEXT, NULL);
Packit ae235b
  if (!g_file_test (echo_prog_path, G_FILE_TEST_EXISTS))
Packit ae235b
    {
Packit ae235b
      g_free (echo_prog_path);
Packit ae235b
      echo_prog_path = g_build_filename (dirname, "lt-test-spawn-echo" EXEEXT, NULL);
Packit ae235b
    }
Packit ae235b
  echo_script_path = g_build_filename (dirname, "echo-script" SCRIPT_EXT, NULL);
Packit ae235b
  if (!g_file_test (echo_script_path, G_FILE_TEST_EXISTS))
Packit ae235b
    {
Packit ae235b
      g_free (echo_script_path);
Packit ae235b
      echo_script_path = g_test_build_filename (G_TEST_DIST, "echo-script" SCRIPT_EXT, NULL);
Packit ae235b
    }
Packit ae235b
  g_free (dirname);
Packit ae235b
Packit ae235b
  g_assert (g_file_test (echo_prog_path, G_FILE_TEST_EXISTS));
Packit ae235b
  g_assert (g_file_test (echo_script_path, G_FILE_TEST_EXISTS));
Packit ae235b
Packit ae235b
  g_test_add_func ("/gthread/spawn-single-sync", test_spawn_sync);
Packit ae235b
  g_test_add_func ("/gthread/spawn-single-async", test_spawn_async);
Packit ae235b
  g_test_add_func ("/gthread/spawn-script", test_spawn_script);
Packit ae235b
Packit ae235b
  ret = g_test_run();
Packit ae235b
Packit ae235b
  g_free (echo_script_path);
Packit ae235b
  g_free (echo_prog_path);
Packit ae235b
Packit ae235b
  return ret;
Packit ae235b
}