[glib: 13/17] tests: Use TestSignal rather than NameOwnerChanged to test signals



commit f74a987d21512173cc0428ff662e5d5109e33319
Author: Philip Withnall <withnall endlessm com>
Date:   Fri Feb 21 14:14:57 2020 +0000

    tests: Use TestSignal rather than NameOwnerChanged to test signals
    
    When testing that signals are delivered to the correct thread, and are
    delivered the correct number of times, call `EmitSignal()` on the
    `gdbus-testserver` to trigger a signal emission, and listen for that.
    
    Previously, the code listened for `NameOwnerChanged` and connected to
    the bus again to trigger emission of that. The problem with that is that
    other things happening on the bus (for example, an old
    `gdbus-testserver` instance disconnecting) can cause `NameOwnerChanged`
    signal emissions. Sometimes, the `gdbus-threading` test was failing the
    `signal_count == 1` assertion due to receiving more than one
    `NameOwnerChanged` emission.
    
    Signed-off-by: Philip Withnall <withnall endlessm com>
    
    Helps: #1515

 gio/tests/gdbus-threading.c | 39 +++++++++++++++++++++++++--------------
 1 file changed, 25 insertions(+), 14 deletions(-)
---
diff --git a/gio/tests/gdbus-threading.c b/gio/tests/gdbus-threading.c
index 80ae4b626..0a2fe281f 100644
--- a/gio/tests/gdbus-threading.c
+++ b/gio/tests/gdbus-threading.c
@@ -137,7 +137,6 @@ test_delivery_in_thread_func (gpointer _data)
   DeliveryData data;
   GCancellable *ca;
   guint subscription_id;
-  GDBusConnection *priv_c;
   GError *error = NULL;
   GVariant *result_variant = NULL;
 
@@ -237,15 +236,14 @@ test_delivery_in_thread_func (gpointer _data)
   /*
    * Check that signals are delivered to the correct thread.
    *
-   * First we subscribe to the signal, then we create a a private
-   * connection. This should cause a NameOwnerChanged message from
-   * the message bus.
+   * First we subscribe to the signal, then we call EmitSignal(). This should
+   * cause a TestSignal emission from the testserver.
    */
   subscription_id = g_dbus_connection_signal_subscribe (c,
-                                                        "org.freedesktop.DBus",  /* sender */
-                                                        "org.freedesktop.DBus",  /* interface */
-                                                        "NameOwnerChanged",      /* member */
-                                                        "/org/freedesktop/DBus", /* path */
+                                                        "com.example.TestService", /* sender */
+                                                        "com.example.Frob",        /* interface */
+                                                        "TestSignal",              /* member */
+                                                        "/com/example/TestObject", /* path */
                                                         NULL,
                                                         G_DBUS_SIGNAL_FLAGS_NONE,
                                                         signal_handler,
@@ -254,16 +252,29 @@ test_delivery_in_thread_func (gpointer _data)
   g_assert_cmpuint (subscription_id, !=, 0);
   g_assert_cmpuint (data.signal_count, ==, 0);
 
-  priv_c = _g_bus_get_priv (G_BUS_TYPE_SESSION, NULL, &error);
+  g_dbus_connection_call (c,
+                          "com.example.TestService", /* bus_name */
+                          "/com/example/TestObject", /* object path */
+                          "com.example.Frob",        /* interface name */
+                          "EmitSignal",              /* method name */
+                          g_variant_new_parsed ("('hello', @o '/com/example/TestObject')"),
+                          NULL,
+                          G_DBUS_CALL_FLAGS_NONE,
+                          -1,
+                          NULL,
+                          (GAsyncReadyCallback) async_result_cb,
+                          &data);
+  while (data.async_result == NULL || data.signal_count < 1)
+    g_main_context_iteration (thread_context, TRUE);
+
+  result_variant = g_dbus_connection_call_finish (c, data.async_result, &error);
   g_assert_no_error (error);
-  g_assert_nonnull (priv_c);
+  g_assert_nonnull (result_variant);
+  g_clear_pointer (&result_variant, g_variant_unref);
+  g_clear_object (&data.async_result);
 
-  while (data.signal_count < 1)
-    g_main_context_iteration (thread_context, TRUE);
   g_assert_cmpuint (data.signal_count, ==, 1);
 
-  g_object_unref (priv_c);
-
   g_dbus_connection_signal_unsubscribe (c, subscription_id);
   subscription_id = 0;
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]