[gnome-software] tests: Fix racy assertion in flatpak test



commit 4370c6a39788d3ac64969e7bef07923f59189aa4
Author: Philip Withnall <withnall endlessm com>
Date:   Thu May 14 23:26:57 2020 +0100

    tests: Fix racy assertion in flatpak test
    
    The progress is updated from another thread, so it’s not possible to
    guarantee that we’re going to check it at the right time. Instead,
    monitor `notify` signals for changes and wait for the value we want.
    
    Signed-off-by: Philip Withnall <withnall endlessm com>

 plugins/flatpak/gs-self-test.c | 26 +++++++++++++++++++++++---
 1 file changed, 23 insertions(+), 3 deletions(-)
---
diff --git a/plugins/flatpak/gs-self-test.c b/plugins/flatpak/gs-self-test.c
index 1a1ad445..4545827b 100644
--- a/plugins/flatpak/gs-self-test.c
+++ b/plugins/flatpak/gs-self-test.c
@@ -200,6 +200,16 @@ gs_plugins_flatpak_repo_func (GsPluginLoader *plugin_loader)
        g_assert_cmpint (gs_app_get_progress (app), ==, GS_APP_PROGRESS_UNKNOWN);
 }
 
+static void
+progress_notify_cb (GObject *obj, GParamSpec *pspec, gpointer user_data)
+{
+       gboolean *seen_unknown = user_data;
+       GsApp *app = GS_APP (obj);
+
+       if (gs_app_get_progress (app) == GS_APP_PROGRESS_UNKNOWN)
+               *seen_unknown = TRUE;
+}
+
 static void
 gs_plugins_flatpak_app_with_runtime_func (GsPluginLoader *plugin_loader)
 {
@@ -225,6 +235,8 @@ gs_plugins_flatpak_app_with_runtime_func (GsPluginLoader *plugin_loader)
        g_autoptr(GsAppList) list = NULL;
        g_autoptr(GsAppList) sources = NULL;
        g_autoptr(GsPluginJob) plugin_job = NULL;
+       gulong signal_id;
+       gboolean seen_unknown;
 
        /* drop all caches */
        gs_utils_rmtree (g_getenv ("GS_SELF_TEST_CACHEDIR"), NULL);
@@ -418,7 +430,13 @@ gs_plugins_flatpak_app_with_runtime_func (GsPluginLoader *plugin_loader)
        g_assert (!g_file_test (metadata_fn, G_FILE_TEST_IS_REGULAR));
        g_assert (!g_file_test (desktop_fn, G_FILE_TEST_IS_REGULAR));
 
-       /* install again, to check whether the progress gets initialized */
+       /* install again, to check whether the progress gets initialized;
+        * since installation happens in another thread, we have to monitor all
+        * changes to the progress and see if we see the one we want */
+       seen_unknown = (gs_app_get_progress (app) == GS_APP_PROGRESS_UNKNOWN);
+       signal_id = g_signal_connect (app, "notify::progress",
+                                     G_CALLBACK (progress_notify_cb), &seen_unknown);
+
        g_object_unref (plugin_job);
        plugin_job = gs_plugin_job_newv (GS_PLUGIN_ACTION_INSTALL,
                                         "app", app,
@@ -426,13 +444,15 @@ gs_plugins_flatpak_app_with_runtime_func (GsPluginLoader *plugin_loader)
        ret = gs_plugin_loader_job_action (plugin_loader, plugin_job, NULL, &error);
 
        /* progress should be set to unknown right before installing */
-       gs_test_flush_main_context ();
-       g_assert_cmpint (gs_app_get_progress (app), ==, GS_APP_PROGRESS_UNKNOWN);
+       while (!seen_unknown)
+               g_main_context_iteration (NULL, TRUE);
+       g_assert_true (seen_unknown);
        g_assert_no_error (error);
        g_assert (ret);
        g_assert_cmpint (gs_app_get_state (app), ==, AS_APP_STATE_INSTALLED);
        g_assert_cmpstr (gs_app_get_version (app), ==, "1.2.3");
        g_assert_cmpint (gs_app_get_progress (app), ==, GS_APP_PROGRESS_UNKNOWN);
+       g_signal_handler_disconnect (app, signal_id);
 
        /* remove the application */
        g_object_unref (plugin_job);


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