[gcr/wip/nielsdg/fix-tests-with-racy-gtask] Create a workaround for tests and GTask




commit 9c65f7dae1cc2468165bc9e1637e65da92df1c51
Author: Niels De Graef <nielsdegraef gmail com>
Date:   Fri Jan 15 20:46:18 2021 +0100

    Create a workaround for tests and GTask
    
    It's possible that a GTask that has been called with
    `g_task_run_in_thread()` still has a reference held by that thread after
    the `_finish()` function has been called. This is a little problematic
    for tests that check for the final reference, so use this as a
    workaround instead.
    
    See https://gitlab.gnome.org/GNOME/glib/-/issues/1346 for more info

 egg/egg-testing.h             | 13 +++++++++++++
 gcr/test-subject-public-key.c | 15 +++++++++------
 2 files changed, 22 insertions(+), 6 deletions(-)
---
diff --git a/egg/egg-testing.h b/egg/egg-testing.h
index 73939b37..a98a7821 100644
--- a/egg/egg-testing.h
+++ b/egg/egg-testing.h
@@ -61,4 +61,17 @@ gboolean   egg_test_wait_until                 (int timeout);
 
 gint       egg_tests_run_with_loop             (void);
 
+/* It's possible that a GTask that has been called with g_task_run_in_thread()
+ * still has a reference held by that thread after the _finish() function has
+ * been called. This is a little problematic for tests that check for the
+ * final reference, so use this as a workaround instead.
+ *
+ * See https://gitlab.gnome.org/GNOME/glib/-/issues/1346 for more info
+ */
+#define    egg_test_wait_for_gtask_thread(expr)  G_STMT_START                                \
+                                                 while (expr) {                              \
+                                                     g_main_context_iteration (NULL, FALSE); \
+                                                 }                                           \
+                                                 G_STMT_END
+
 #endif /* EGG_DH_H_ */
diff --git a/gcr/test-subject-public-key.c b/gcr/test-subject-public-key.c
index f92995ac..4d16a7b5 100644
--- a/gcr/test-subject-public-key.c
+++ b/gcr/test-subject-public-key.c
@@ -255,9 +255,11 @@ teardown_module (TestModule *test,
        CK_RV rv;
 
        g_object_unref (test->session);
-       g_assert (test->session == NULL);
-
        g_object_unref (test->module);
+
+       egg_test_wait_for_gtask_thread (test->session || test->module);
+
+       g_assert (test->session == NULL);
        g_assert (test->module == NULL);
 
        rv = (test->funcs.C_Finalize) (NULL);
@@ -307,12 +309,13 @@ teardown_loading (TestLoading *test,
                   gconstpointer fixture)
 {
        g_object_unref (test->crt_object);
-       g_assert (test->crt_object == NULL);
-
        g_object_unref (test->prv_object);
-       g_assert (test->prv_object == NULL);
-
        g_object_unref (test->pub_object);
+
+       egg_test_wait_for_gtask_thread (test->crt_object || test->prv_object || test->pub_object);
+
+       g_assert (test->crt_object == NULL);
+       g_assert (test->prv_object == NULL);
        g_assert (test->pub_object == NULL);
 
        teardown_module (&test->mo, NULL);


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