[glib: 2/17] tests: Move refcount checks to the end of each test in gdbus-threading



commit b302ee956e75194982cf42bcc7cd57fe61ff5b8a
Author: Philip Withnall <withnall endlessm com>
Date:   Mon Nov 12 13:20:29 2018 +0000

    tests: Move refcount checks to the end of each test in gdbus-threading
    
    These checks used to be a precondition on test_threaded_singleton(); but
    the earlier tests could leave the refcount of the shared connection in a
    bad state, and this wouldn’t be caught until later.
    
    Factor out the check, increase the iteration count to 1000 (so the check
    blocks for up to 1s rather than 100ms), and call it in more places.
    
    Signed-off-by: Philip Withnall <withnall endlessm com>
    
    https://gitlab.gnome.org/GNOME/glib/issues/1515

 gio/tests/gdbus-threading.c | 42 ++++++++++++++++++++++++++++--------------
 1 file changed, 28 insertions(+), 14 deletions(-)
---
diff --git a/gio/tests/gdbus-threading.c b/gio/tests/gdbus-threading.c
index 48a775ff4..b0183c710 100644
--- a/gio/tests/gdbus-threading.c
+++ b/gio/tests/gdbus-threading.c
@@ -27,6 +27,29 @@
 /* all tests rely on a global connection */
 static GDBusConnection *c = NULL;
 
+/* Check that the given @connection has only one ref, waiting to let any pending
+ * unrefs complete first. This is typically used on the shared connection, to
+ * ensure it’s in a correct state before beginning the next test. */
+static void
+assert_connection_has_one_ref (GDBusConnection *connection)
+{
+  guint j;
+
+  for (j = 0; j < 1000; j++)
+    {
+      guint r = g_atomic_int_get (&G_OBJECT (connection)->ref_count);
+
+      if (r == 1)
+        break;
+
+      g_debug ("refcount of %p is %u, sleeping", connection, r);
+      g_usleep (1000);
+    }
+
+  if (j == 1000)
+    g_error ("connection %p had too many refs", connection);
+}
+
 /* ---------------------------------------------------------------------------------------------------- */
 /* Ensure that signal and method replies are delivered in the right thread */
 /* ---------------------------------------------------------------------------------------------------- */
@@ -229,6 +252,8 @@ test_delivery_in_thread (void)
                          NULL);
 
   g_thread_join (thread);
+
+  assert_connection_has_one_ref (c);
 }
 
 /* ---------------------------------------------------------------------------------------------------- */
@@ -441,6 +466,8 @@ test_method_calls_in_thread (void)
 
   if (g_test_verbose ())
     g_printerr ("\n");
+
+  assert_connection_has_one_ref (c);
 }
 
 #define SLEEP_MIN_USEC 1
@@ -505,24 +532,11 @@ test_threaded_singleton (void)
   for (i = 0; i < n; i++)
     {
       GThread *thread;
-      guint j;
       guint unref_delay, get_delay;
       GDBusConnection *new_conn;
 
       /* We want to be the last ref, so let it finish setting up */
-      for (j = 0; j < 100; j++)
-        {
-          guint r = (guint) g_atomic_int_get (&G_OBJECT (c)->ref_count);
-
-          if (r == 1)
-            break;
-
-          g_debug ("run %u: refcount is %u, sleeping", i, r);
-          g_usleep (1000);
-        }
-
-      if (j == 100)
-        g_error ("connection had too many refs");
+      assert_connection_has_one_ref (c);
 
       if (g_test_verbose () && (i % (n/50)) == 0)
         g_printerr ("%u%%\n", ((i * 100) / n));


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