tests: Drop use of GMainLoop in testlibrary

Use g_main_context_iteration() manually in a loop instead; this makes
the termination conditions more obvious. This does not change the
behaviour of the test.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
tingping/wmclass
Philip Withnall 2017-08-11 13:22:25 +01:00 committed by Alexander Larsson
parent 7b13ff2880
commit 1932c66e0f
1 changed files with 34 additions and 29 deletions

View File

@ -330,8 +330,6 @@ progress_cb (const char *status,
*count += 1; *count += 1;
} }
static int changed_count;
static void static void
changed_cb (GFileMonitor *monitor, changed_cb (GFileMonitor *monitor,
GFile *file, GFile *file,
@ -339,16 +337,15 @@ changed_cb (GFileMonitor *monitor,
GFileMonitorEvent event_type, GFileMonitorEvent event_type,
gpointer user_data) gpointer user_data)
{ {
GMainLoop *loop = user_data; int *count = user_data;
g_main_loop_quit (loop); *count += 1;
changed_count += 1;
} }
static gboolean static gboolean
quit (gpointer data) timeout_cb (gpointer data)
{ {
GMainLoop *loop = data; gboolean *timeout_reached = data;
g_main_loop_quit (loop); *timeout_reached = TRUE;
return G_SOURCE_CONTINUE; return G_SOURCE_CONTINUE;
} }
@ -364,9 +361,9 @@ test_install_launch_uninstall (void)
GPtrArray *refs = NULL; GPtrArray *refs = NULL;
g_autofree char *s = NULL; g_autofree char *s = NULL;
g_autofree char *s1 = NULL; g_autofree char *s1 = NULL;
int progress_count; int progress_count, changed_count;
g_autoptr(GMainLoop) loop = NULL; gboolean timeout_reached;
guint quit_id; guint timeout_id;
gboolean res; gboolean res;
const char *bwrap = g_getenv ("FLATPAK_BWRAP"); const char *bwrap = g_getenv ("FLATPAK_BWRAP");
@ -391,9 +388,7 @@ test_install_launch_uninstall (void)
g_assert (G_IS_FILE_MONITOR (monitor)); g_assert (G_IS_FILE_MONITOR (monitor));
g_file_monitor_set_rate_limit (monitor, 100); g_file_monitor_set_rate_limit (monitor, 100);
loop = g_main_loop_new (NULL, TRUE); g_signal_connect (monitor, "changed", G_CALLBACK (changed_cb), &changed_count);
g_signal_connect (monitor, "changed", G_CALLBACK (changed_cb), loop);
refs = flatpak_installation_list_installed_refs (inst, NULL, &error); refs = flatpak_installation_list_installed_refs (inst, NULL, &error);
g_assert_no_error (error); g_assert_no_error (error);
@ -402,6 +397,7 @@ test_install_launch_uninstall (void)
changed_count = 0; changed_count = 0;
progress_count = 0; progress_count = 0;
timeout_reached = FALSE;
ref = flatpak_installation_install (inst, ref = flatpak_installation_install (inst,
repo_name, repo_name,
FLATPAK_REF_KIND_RUNTIME, FLATPAK_REF_KIND_RUNTIME,
@ -416,9 +412,10 @@ test_install_launch_uninstall (void)
g_assert (FLATPAK_IS_INSTALLED_REF (ref)); g_assert (FLATPAK_IS_INSTALLED_REF (ref));
g_assert_cmpint (progress_count, >, 0); g_assert_cmpint (progress_count, >, 0);
quit_id = g_timeout_add (1000, quit, loop); timeout_id = g_timeout_add (1000, timeout_cb, &timeout_reached);
g_main_loop_run (loop); while (!timeout_reached && changed_count == 0)
g_source_remove (quit_id); g_main_context_iteration (NULL, TRUE);
g_source_remove (timeout_id);
g_assert_cmpint (changed_count, >, 0); g_assert_cmpint (changed_count, >, 0);
@ -450,6 +447,7 @@ test_install_launch_uninstall (void)
changed_count = 0; changed_count = 0;
progress_count = 0; progress_count = 0;
timeout_reached = FALSE;
ref = flatpak_installation_install (inst, ref = flatpak_installation_install (inst,
repo_name, repo_name,
FLATPAK_REF_KIND_APP, FLATPAK_REF_KIND_APP,
@ -464,9 +462,10 @@ test_install_launch_uninstall (void)
g_assert (FLATPAK_IS_INSTALLED_REF (ref)); g_assert (FLATPAK_IS_INSTALLED_REF (ref));
g_assert_cmpint (progress_count, >, 0); g_assert_cmpint (progress_count, >, 0);
quit_id = g_timeout_add (1000, quit, loop); timeout_id = g_timeout_add (1000, timeout_cb, &timeout_reached);
g_main_loop_run (loop); while (!timeout_reached && changed_count == 0)
g_source_remove (quit_id); g_main_context_iteration (NULL, TRUE);
g_source_remove (timeout_id);
g_assert_cmpint (changed_count, >, 0); g_assert_cmpint (changed_count, >, 0);
@ -491,9 +490,11 @@ test_install_launch_uninstall (void)
g_assert_no_error (error); g_assert_no_error (error);
g_assert_true (res); g_assert_true (res);
quit_id = g_timeout_add (500, quit, loop); timeout_reached = FALSE;
g_main_loop_run (loop); timeout_id = g_timeout_add (500, timeout_cb, &timeout_reached);
g_source_remove (quit_id); while (!timeout_reached)
g_main_context_iteration (NULL, TRUE);
g_source_remove (timeout_id);
changed_count = 0; changed_count = 0;
progress_count = 0; progress_count = 0;
@ -511,9 +512,11 @@ test_install_launch_uninstall (void)
//FIXME: no progress for uninstall //FIXME: no progress for uninstall
//g_assert_cmpint (progress_count, >, 0); //g_assert_cmpint (progress_count, >, 0);
quit_id = g_timeout_add (500, quit, loop); timeout_reached = FALSE;
g_main_loop_run (loop); timeout_id = g_timeout_add (500, timeout_cb, &timeout_reached);
g_source_remove (quit_id); while (!timeout_reached && changed_count == 0)
g_main_context_iteration (NULL, TRUE);
g_source_remove (timeout_id);
refs = flatpak_installation_list_installed_refs (inst, NULL, &error); refs = flatpak_installation_list_installed_refs (inst, NULL, &error);
g_assert_no_error (error); g_assert_no_error (error);
@ -537,9 +540,11 @@ test_install_launch_uninstall (void)
//FIXME: no progress for uninstall //FIXME: no progress for uninstall
//g_assert_cmpint (progress_count, >, 0); //g_assert_cmpint (progress_count, >, 0);
quit_id = g_timeout_add (500, quit, loop); timeout_reached = FALSE;
g_main_loop_run (loop); timeout_id = g_timeout_add (500, timeout_cb, &timeout_reached);
g_source_remove (quit_id); while (!timeout_reached && changed_count == 0)
g_main_context_iteration (NULL, TRUE);
g_source_remove (timeout_id);
refs = flatpak_installation_list_installed_refs (inst, NULL, &error); refs = flatpak_installation_list_installed_refs (inst, NULL, &error);
g_assert_no_error (error); g_assert_no_error (error);