GApplication test: fix testcase to avoid fork()

The GApplication test case tried to fork() while using GMainLoop,
causing problems.  Avoid doing that by splitting the child process into
a separate program and spawning it in the usual way.

https://bugzilla.gnome.org/show_bug.cgi?id=658999
This commit is contained in:
Ryan Lortie 2011-09-14 11:06:02 -04:00
parent 01ed78d525
commit 39294664a6
4 changed files with 108 additions and 131 deletions

View File

@ -2,6 +2,7 @@ actions
appinfo appinfo
appinfo-test appinfo-test
async-close-output-stream async-close-output-stream
basic-application
buffered-input-stream buffered-input-stream
buffered-output-stream buffered-output-stream
cancellable cancellable

View File

@ -113,6 +113,7 @@ TEST_PROGS += \
unix-fd \ unix-fd \
unix-streams \ unix-streams \
gapplication \ gapplication \
basic-application \
gdbus-test-codegen \ gdbus-test-codegen \
$(NULL) $(NULL)
SAMPLE_PROGS += \ SAMPLE_PROGS += \
@ -378,6 +379,8 @@ file_LDADD = $(progs_ldadd)
gapplication_SOURCES = gapplication.c gdbus-sessionbus.c gapplication_SOURCES = gapplication.c gdbus-sessionbus.c
gapplication_LDADD = $(progs_ldadd) gapplication_LDADD = $(progs_ldadd)
basic_application_LDADD = $(progs_ldadd)
gapplication_example_open_SOURCES = gapplication-example-open.c gapplication_example_open_SOURCES = gapplication-example-open.c
gapplication_example_open_LDADD = $(progs_ldadd) gapplication_example_open_LDADD = $(progs_ldadd)

View File

@ -0,0 +1,81 @@
#include <gio/gio.h>
#include <string.h>
static void
activate (GApplication *application)
{
g_application_hold (application);
g_print ("activated\n");
g_application_release (application);
}
static void
open (GApplication *application,
GFile **files,
gint n_files,
const gchar *hint)
{
gint i;
g_application_hold (application);
g_print ("open");
for (i = 0; i < n_files; i++)
{
gchar *uri = g_file_get_uri (files[i]);
g_print (" %s", uri);
g_free (uri);
}
g_application_release (application);
g_print ("\n");
}
static int
command_line (GApplication *application,
GApplicationCommandLine *cmdline)
{
gchar **argv;
gint argc;
argv = g_application_command_line_get_arguments (cmdline, &argc);
g_application_command_line_print (cmdline, "%d + %d = %d\n", 40, 2, 42);
g_assert_cmpint (argc, ==, 3);
g_assert_cmpstr (argv[0], ==, "./cmd");
g_assert_cmpstr (argv[1], ==, "40 +");
g_assert_cmpstr (argv[2], ==, "2");
g_assert (argv[3] == NULL);
g_print ("cmdline '%s' '%s'\n", argv[1], argv[2]);
g_strfreev (argv);
return 42;
}
int
main (int argc, char **argv)
{
GApplication *app;
int status;
app = g_application_new ("org.gtk.TestApplication",
G_APPLICATION_HANDLES_OPEN |
(strcmp (argv[1], "./cmd") == 0 ?
G_APPLICATION_HANDLES_COMMAND_LINE
: 0));
g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
g_signal_connect (app, "open", G_CALLBACK (open), NULL);
g_signal_connect (app, "command-line", G_CALLBACK (command_line), NULL);
#ifdef STANDALONE
g_application_set_inactivity_timeout (app, 10000);
#else
g_application_set_inactivity_timeout (app, 1000);
#endif
status = g_application_run (app, argc - 1, argv + 1);
g_object_unref (app);
g_print ("exit status: %d\n", status);
return 0;
}

View File

@ -1,92 +1,9 @@
#include <gio/gio.h> #include <gio/gio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h>
#include "gdbus-sessionbus.h" #include "gdbus-sessionbus.h"
static void
activate (GApplication *application)
{
g_application_hold (application);
g_print ("activated\n");
g_application_release (application);
}
static void
open (GApplication *application,
GFile **files,
gint n_files,
const gchar *hint)
{
gint i;
g_application_hold (application);
g_print ("open");
for (i = 0; i < n_files; i++)
{
gchar *uri = g_file_get_uri (files[i]);
g_print (" %s", uri);
g_free (uri);
}
g_application_release (application);
g_print ("\n");
}
static int
command_line (GApplication *application,
GApplicationCommandLine *cmdline)
{
gchar **argv;
gint argc;
argv = g_application_command_line_get_arguments (cmdline, &argc);
g_application_command_line_print (cmdline, "%d + %d = %d\n", 40, 2, 42);
g_assert_cmpint (argc, ==, 3);
g_assert_cmpstr (argv[0], ==, "./cmd");
g_assert_cmpstr (argv[1], ==, "40 +");
g_assert_cmpstr (argv[2], ==, "2");
g_assert (argv[3] == NULL);
g_print ("cmdline '%s' '%s'\n", argv[1], argv[2]);
g_strfreev (argv);
return 42;
}
static int
app_main (int argc, char **argv)
{
GApplication *app;
int status;
app = g_application_new ("org.gtk.TestApplication",
G_APPLICATION_HANDLES_OPEN |
(strcmp (argv[0], "./cmd") == 0 ?
G_APPLICATION_HANDLES_COMMAND_LINE
: 0));
g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
g_signal_connect (app, "open", G_CALLBACK (open), NULL);
g_signal_connect (app, "command-line", G_CALLBACK (command_line), NULL);
#ifdef STANDALONE
g_application_set_inactivity_timeout (app, 10000);
#else
g_application_set_inactivity_timeout (app, 1000);
#endif
status = g_application_run (app, argc, argv);
g_object_unref (app);
return status;
}
#ifdef STANDALONE
int
main (int argc, char **argv)
{
return app_main (argc - 1, argv + 1);
}
#else
static gint outstanding_watches; static gint outstanding_watches;
static GMainLoop *main_loop; static GMainLoop *main_loop;
@ -135,57 +52,33 @@ spawn (const gchar *expected_stdout,
const gchar *first_arg, const gchar *first_arg,
...) ...)
{ {
gint pipefd[2]; GError *error = NULL;
const gchar *arg;
GPtrArray *array;
ChildData *data;
gchar **args;
va_list ap;
GPid pid; GPid pid;
int s;
/* We assume that the child will not produce enough stdout va_start (ap, first_arg);
* to deadlock by filling the pipe. array = g_ptr_array_new ();
*/ g_ptr_array_add (array, g_strdup ("./basic-application"));
s = pipe (pipefd); for (arg = first_arg; arg; arg = va_arg (ap, const gchar *))
g_assert_cmpint (s, ==, 0); g_ptr_array_add (array, g_strdup (arg));
g_ptr_array_add (array, NULL);
args = (gchar **) g_ptr_array_free (array, FALSE);
pid = fork (); data = g_slice_new (ChildData);
data->expected_stdout = expected_stdout;
if (pid == 0) g_spawn_async_with_pipes (NULL, args, NULL,
{ G_SPAWN_DO_NOT_REAP_CHILD,
const gchar *arg; NULL, NULL, &pid, NULL,
GPtrArray *array; &data->stdout_pipe, NULL, &error);
gchar **args; g_assert_no_error (error);
int status;
va_list ap;
dup2 (pipefd[1], 1); g_child_watch_add (pid, child_quit, data);
close (pipefd[0]); outstanding_watches++;
close (pipefd[1]);
va_start (ap, first_arg);
array = g_ptr_array_new ();
for (arg = first_arg; arg; arg = va_arg (ap, const gchar *))
g_ptr_array_add (array, g_strdup (arg));
g_ptr_array_add (array, NULL);
args = (gchar **) g_ptr_array_free (array, FALSE);
status = app_main (g_strv_length (args), args);
g_strfreev (args);
g_print ("exit status: %d\n", status);
exit (0);
}
else if (pid > 0)
{
ChildData *data;
data = g_slice_new (ChildData);
data->expected_stdout = expected_stdout;
data->stdout_pipe = pipefd[0];
close (pipefd[1]);
g_child_watch_add (pid, child_quit, data);
outstanding_watches++;
}
else
g_assert_not_reached ();
} }
static void static void
@ -375,4 +268,3 @@ main (int argc, char **argv)
return g_test_run (); return g_test_run ();
} }
#endif