Fix the gdbus-proxy async test
authorMike Gorse <mgorse@suse.com>
Thu, 26 Apr 2012 21:06:34 +0000 (16:06 -0500)
committerMatthias Clasen <mclasen@redhat.com>
Tue, 28 Aug 2012 04:08:04 +0000 (00:08 -0400)
The async test had several problems:
- It created a proxy and did not launch a main loop, meaning that its
  callback would usually not get called, or, if it did get called, the
test harness would have taken down the connection already, causing an
assertion failure when the proxy had an error.
- It was dependent on the proxy test to set up the server and would fail
  because some properties were modified by that test.

https://bugzilla.gnome.org/show_bug.cgi?id=674805

gio/tests/gdbus-proxy.c

index 0d3866644ee025966fa866df0146940222ec54e1..255e85584c360f4bdd10221f34fe0714bdab81c9 100644 (file)
@@ -732,6 +732,30 @@ test_basic (GDBusProxy *proxy)
   g_object_unref (connection);
 }
 
+static void
+kill_test_service (GDBusConnection *connection)
+{
+  guint pid;
+  GVariant *ret;
+  GError *error = NULL;
+  const gchar *name = "com.example.TestService";
+
+  ret = g_dbus_connection_call_sync (connection,
+                                     "org.freedesktop.DBus",
+                                     "/org/freedesktop/DBus",
+                                     "org.freedesktop.DBus",
+                                     "GetConnectionUnixProcessID",
+                                     g_variant_new ("(s)", name),
+                                     NULL,
+                                     G_DBUS_CALL_FLAGS_NONE,
+                                     -1,
+                                     NULL,
+                                     &error);
+  g_variant_get (ret, "(u)", &pid);
+  g_variant_unref (ret);
+  kill (pid, SIGTERM);
+}
+
 static void
 test_proxy (void)
 {
@@ -767,6 +791,7 @@ test_proxy (void)
   test_expected_interface (proxy);
 
   g_object_unref (proxy);
+  kill_test_service (connection);
   g_object_unref (connection);
 }
 
@@ -784,13 +809,23 @@ proxy_ready (GObject      *source,
   proxy = g_dbus_proxy_new_for_bus_finish (result, &error);
   g_assert_no_error (error);
 
+  _g_assert_property_notify (proxy, "g-name-owner");
+
   test_basic (proxy);
   test_methods (proxy);
   test_properties (proxy);
   test_signals (proxy);
   test_expected_interface (proxy);
 
+  kill_test_service (g_dbus_proxy_get_connection (proxy));
   g_object_unref (proxy);
+  g_main_loop_quit (loop);
+}
+
+static gboolean
+fail_test (gpointer user_data)
+{
+  g_assert_not_reached ();
 }
 
 static void
@@ -807,6 +842,12 @@ test_async (void)
                             NULL, /* GCancellable */
                             proxy_ready,
                             NULL);
+
+  /* this is safe; testserver will exit once the bus goes away */
+  g_assert (g_spawn_command_line_async (SRCDIR "/gdbus-testserver.py", NULL));
+
+  g_timeout_add (10000, fail_test, NULL);
+  g_main_loop_run (loop);
 }
 
 static void
@@ -831,12 +872,6 @@ test_no_properties (void)
   g_object_unref (proxy);
 }
 
-static gboolean
-fail_test (gpointer user_data)
-{
-  g_assert_not_reached ();
-}
-
 static void
 check_error (GObject      *source,
              GAsyncResult *result,