[Xfce4-commits] <xfconf:master> fix test framework, hopefully for the last time

Brian J. Tarricone noreply at xfce.org
Sat Sep 19 00:50:02 CEST 2009


Updating branch refs/heads/master
         to 2841ba12edb0281757d456ea398d4750e28a7467 (commit)
       from 65d1fe5e7ec68bc69aab890a074a89bc73b80444 (commit)

commit 2841ba12edb0281757d456ea398d4750e28a7467
Author: Brian J. Tarricone <brian at tarricone.org>
Date:   Fri Sep 18 15:48:19 2009 -0700

    fix test framework, hopefully for the last time
    
    add a --daemon option to xfconfd that forks(), prints its new child pid,
    and quits.  this lets us move xfconfd launching into the test script and
    out of the C file.  also we make sure to kill xfconfd and dbus-daemon
    after each test run.  this seems to work without any weird dbus errors.

 tests/Makefile.inc        |    3 +--
 tests/test-template.sh.in |   35 +++++++++++++++++++++++++++++++----
 tests/tests-common.h      |   19 ++-----------------
 xfconfd/main.c            |   23 +++++++++++++++++++++--
 4 files changed, 55 insertions(+), 25 deletions(-)

diff --git a/tests/Makefile.inc b/tests/Makefile.inc
index 408b15f..59fda73 100644
--- a/tests/Makefile.inc
+++ b/tests/Makefile.inc
@@ -1,10 +1,9 @@
 check_SCRIPTS = $(addsuffix .sh,$(check_PROGRAMS))
 
 TESTS = $(check_SCRIPTS)
-TESTS_ENVIRONMENT = XDG_CONFIG_HOME="$(top_builddir)/tests/test-xdg_config_home"
+TESTS_ENVIRONMENT = XDG_CONFIG_HOME="$(top_builddir)/tests/test-xdg_config_home" XFCONFD="$(top_builddir)/xfconfd/xfconfd"
 
 AM_CFLAGS = \
-	-DXFCONFD=\"$(top_builddir)/xfconfd/xfconfd\" \
 	-I$(top_srcdir) \
 	-I$(top_srcdir)/tests \
 	$(GLIB_CFLAGS) \
diff --git a/tests/test-template.sh.in b/tests/test-template.sh.in
index c1753a2..5d2a3a6 100644
--- a/tests/test-template.sh.in
+++ b/tests/test-template.sh.in
@@ -1,15 +1,42 @@
 #!/bin/bash
 
+cleanup() {
+    if [ "$XFCONFD_PID" ]; then
+        kill -TERM $XFCONFD_PID 2>/dev/null
+        sleep 1
+        kill -KILL $XFCONFD_PID 2>/dev/null
+    fi
+
+    kill -TERM $DBUS_SESSION_BUS_PID 2>/dev/null
+    sleep 1
+    kill -KILL $DBUS_SESSION_BUS_PID 2>/dev/null
+}
+
+die() {
+    [ "$1" ] && echo "$1" >&2
+    cleanup
+    exit 1
+}
+
+unset DBUS_SESSION_BUS_ADDRESS
+unset DBUS_SESSION_BUS_PID
+unset XFCONFD_PID
+
 eval `dbus-launch --sh-syntax`
 export DBUS_SESSION_BUS_ADDRESS
 export DBUS_SESSION_BUS_PID
 
+[ "$DBUS_SESSION_BUS_PID" ] || die "DBus failed to start"
+
+trap "die Interrupted" INT
+
+eval `$XFCONFD --daemon 2>/dev/null` || die "Failed to start xfconfd"
+
 export XDG_CONFIG_HOME  # make sure it's exported from the makefile
 export XDG_CONFIG_DIRS=""
 
-./@TEST_NAME@
-ret=$?
+./@TEST_NAME@ || die "Test Failed"
 
-kill -TERM $DBUS_SESSION_BUS_PID
+cleanup
 
-exit $ret
+exit 0
diff --git a/tests/tests-common.h b/tests/tests-common.h
index fc221d1..3c036e0 100644
--- a/tests/tests-common.h
+++ b/tests/tests-common.h
@@ -55,8 +55,6 @@
     } \
 }G_STMT_END
 
-static GPid xfconfd_pid = -1;
-
 /* don't use static to avoid compiler warnings in tests that don't use
  * all of them */
 const gchar *test_string_property = "/test/stringtest/string";
@@ -76,21 +74,14 @@ const gchar *test_array_property = "/test/arrayprop";
 static inline void xfconf_tests_end();
 
 static inline gboolean
-xfconf_tests_start()
+xfconf_tests_start(void)
 {
-    gchar *argv[2] = { XFCONFD, NULL };
     DBusConnection *dbus_conn;
     DBusMessage *msg, *ret;
     DBusError derror;
     GTimeVal start, now;
     GError *error = NULL;
 
-    if(!g_spawn_async(NULL, argv, NULL, 0, NULL, NULL, &xfconfd_pid, &error)) {
-        g_critical("Failed to launch xfconfd (%s): %s", XFCONFD, error->message);
-        g_error_free(error);
-        return FALSE;
-    }
-
     /* wait until xfconfd finishes starting */
     dbus_error_init(&derror);
     dbus_conn = dbus_bus_get(DBUS_BUS_SESSION, NULL);
@@ -132,15 +123,9 @@ xfconf_tests_start()
 }
 
 static inline void
-xfconf_tests_end()
+xfconf_tests_end(void)
 {
     xfconf_shutdown();
-    
-    if(xfconfd_pid != -1) {
-        kill(xfconfd_pid, SIGTERM);
-        waitpid(xfconfd_pid, NULL, 0);
-        xfconfd_pid = -1;
-    }
 }
 
 #endif  /* __XFCONF_TESTS_COMMON_H__ */
diff --git a/xfconfd/main.c b/xfconfd/main.c
index 7ca223e..cbdc269 100644
--- a/xfconfd/main.c
+++ b/xfconfd/main.c
@@ -127,12 +127,16 @@ main(int argc,
     GOptionContext *opt_ctx;
     gchar **backends = NULL;
     gboolean print_version = FALSE;
+    gboolean do_daemon = FALSE;
     GOptionEntry options[] = {
-        { "version", 'V', 0, G_OPTION_ARG_NONE, &print_version,
+        { "version", 'V', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE, &print_version,
             N_("Prints the xfconfd version."), NULL },
-        { "backends", 'b', 0, G_OPTION_ARG_STRING_ARRAY, &backends,
+        { "backends", 'b', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_STRING_ARRAY, &backends,
             N_("Configuration backends to use.  The first backend specified " \
                "is opened read/write; the others, read-only."), NULL },
+        { "daemon", 0, G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE, &do_daemon,
+            N_("Fork into background after starting; only useful for " \
+                "testing purposes"), NULL },
         { NULL, 0, 0, 0, 0, NULL, NULL },
     };
 
@@ -208,6 +212,21 @@ main(int argc,
         return EXIT_FAILURE;
     }
     g_strfreev(backends);
+
+    if(do_daemon) {
+        pid_t child_pid;
+
+        child_pid = fork();
+        if(child_pid < 0) {
+            g_printerr("Failed to fork()\n");
+            return 1;
+        } else if(child_pid > 0) {
+            fprintf(stdout, "XFCONFD_PID=%d; export XFCONFD_PID;", child_pid);
+            exit(0);
+        }
+
+        close(fileno(stdout));
+    }
     
     g_main_loop_run(mloop);
     



More information about the Xfce4-commits mailing list