tests: Drop arbitrary and flaky waits from actions tests

The `actions` test previously waited an arbitrary 100ms for various
D-Bus messages to be sent/received, before checking the results of those
messages.

Normally, this would work, but on heavily loaded CI systems, it would
sometimes fail. For example,
https://gitlab.gnome.org/GNOME/glib/-/jobs/1611701.

Fix that by waiting for the condition being checked to evaluate to true,
rather than waiting an arbitrary period of time. On faster machines,
this will speed the tests up too.

Assume that the global default `GMainContext` is in use, so a
`GMainContext*` pointer doesn’t have to be passed around.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
This commit is contained in:
Philip Withnall 2021-11-16 14:01:24 +00:00
parent e9acc6a7f9
commit c8fa295f6a

View File

@ -536,6 +536,8 @@ count_activation (const gchar *action)
count = GPOINTER_TO_INT (g_hash_table_lookup (activation_counts, action));
count++;
g_hash_table_insert (activation_counts, (gpointer)action, GINT_TO_POINTER (count));
g_main_context_wakeup (NULL);
}
static gint
@ -742,6 +744,41 @@ call_describe (gpointer user_data)
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
static void
action_added_removed_cb (GActionGroup *action_group,
char *action_name,
gpointer user_data)
{
guint *counter = user_data;
*counter = *counter + 1;
g_main_context_wakeup (NULL);
}
static void
action_enabled_changed_cb (GActionGroup *action_group,
char *action_name,
gboolean enabled,
gpointer user_data)
{
guint *counter = user_data;
*counter = *counter + 1;
g_main_context_wakeup (NULL);
}
static void
action_state_changed_cb (GActionGroup *action_group,
char *action_name,
GVariant *value,
gpointer user_data)
{
guint *counter = user_data;
*counter = *counter + 1;
g_main_context_wakeup (NULL);
}
static void
test_dbus_export (void)
{
@ -754,6 +791,8 @@ test_dbus_export (void)
GVariant *v;
guint id;
gchar **actions;
guint n_actions_added = 0, n_actions_enabled_changed = 0, n_actions_removed = 0, n_actions_state_changed = 0;
gulong added_signal_id, enabled_changed_signal_id, removed_signal_id, state_changed_signal_id;
loop = g_main_loop_new (NULL, FALSE);
@ -770,13 +809,19 @@ test_dbus_export (void)
g_assert_no_error (error);
proxy = g_dbus_action_group_get (bus, g_dbus_connection_get_unique_name (bus), "/");
added_signal_id = g_signal_connect (proxy, "action-added", G_CALLBACK (action_added_removed_cb), &n_actions_added);
enabled_changed_signal_id = g_signal_connect (proxy, "action-enabled-changed", G_CALLBACK (action_enabled_changed_cb), &n_actions_enabled_changed);
removed_signal_id = g_signal_connect (proxy, "action-removed", G_CALLBACK (action_added_removed_cb), &n_actions_removed);
state_changed_signal_id = g_signal_connect (proxy, "action-state-changed", G_CALLBACK (action_state_changed_cb), &n_actions_state_changed);
actions = g_action_group_list_actions (G_ACTION_GROUP (proxy));
g_assert_cmpint (g_strv_length (actions), ==, 0);
g_strfreev (actions);
g_timeout_add (100, stop_loop, loop);
g_main_loop_run (loop);
/* Actions are queried from the bus asynchronously after the first
* list_actions() call. Wait for the expected signals then check again. */
while (n_actions_added < G_N_ELEMENTS (exported_entries))
g_main_context_iteration (NULL, TRUE);
actions = g_action_group_list_actions (G_ACTION_GROUP (proxy));
g_assert_cmpint (g_strv_length (actions), ==, G_N_ELEMENTS (exported_entries));
@ -795,54 +840,56 @@ test_dbus_export (void)
g_assert_true (compare_action_groups (G_ACTION_GROUP (group), G_ACTION_GROUP (proxy)));
/* test that various changes get propagated from group to proxy */
n_actions_added = 0;
action = g_simple_action_new_stateful ("italic", NULL, g_variant_new_boolean (FALSE));
g_simple_action_group_insert (group, G_ACTION (action));
g_object_unref (action);
g_timeout_add (100, stop_loop, loop);
g_main_loop_run (loop);
while (n_actions_added == 0)
g_main_context_iteration (NULL, TRUE);
g_assert_true (compare_action_groups (G_ACTION_GROUP (group), G_ACTION_GROUP (proxy)));
action = G_SIMPLE_ACTION (g_simple_action_group_lookup (group, "cut"));
g_simple_action_set_enabled (action, FALSE);
g_timeout_add (100, stop_loop, loop);
g_main_loop_run (loop);
while (n_actions_enabled_changed == 0)
g_main_context_iteration (NULL, TRUE);
g_assert_true (compare_action_groups (G_ACTION_GROUP (group), G_ACTION_GROUP (proxy)));
action = G_SIMPLE_ACTION (g_simple_action_group_lookup (group, "bold"));
g_simple_action_set_state (action, g_variant_new_boolean (FALSE));
g_timeout_add (100, stop_loop, loop);
g_main_loop_run (loop);
while (n_actions_state_changed == 0)
g_main_context_iteration (NULL, TRUE);
g_assert_true (compare_action_groups (G_ACTION_GROUP (group), G_ACTION_GROUP (proxy)));
g_simple_action_group_remove (group, "italic");
g_timeout_add (100, stop_loop, loop);
g_main_loop_run (loop);
while (n_actions_removed == 0)
g_main_context_iteration (NULL, TRUE);
g_assert_true (compare_action_groups (G_ACTION_GROUP (group), G_ACTION_GROUP (proxy)));
/* test that activations and state changes propagate the other way */
n_actions_state_changed = 0;
g_assert_cmpint (activation_count ("copy"), ==, 0);
g_action_group_activate_action (G_ACTION_GROUP (proxy), "copy", NULL);
g_timeout_add (100, stop_loop, loop);
g_main_loop_run (loop);
while (activation_count ("copy") == 0)
g_main_context_iteration (NULL, TRUE);
g_assert_cmpint (activation_count ("copy"), ==, 1);
g_assert_true (compare_action_groups (G_ACTION_GROUP (group), G_ACTION_GROUP (proxy)));
n_actions_state_changed = 0;
g_assert_cmpint (activation_count ("bold"), ==, 0);
g_action_group_activate_action (G_ACTION_GROUP (proxy), "bold", NULL);
g_timeout_add (100, stop_loop, loop);
g_main_loop_run (loop);
while (n_actions_state_changed == 0)
g_main_context_iteration (NULL, TRUE);
g_assert_cmpint (activation_count ("bold"), ==, 1);
g_assert_true (compare_action_groups (G_ACTION_GROUP (group), G_ACTION_GROUP (proxy)));
@ -850,10 +897,11 @@ test_dbus_export (void)
g_assert_true (g_variant_get_boolean (v));
g_variant_unref (v);
n_actions_state_changed = 0;
g_action_group_change_action_state (G_ACTION_GROUP (proxy), "bold", g_variant_new_boolean (FALSE));
g_timeout_add (100, stop_loop, loop);
g_main_loop_run (loop);
while (n_actions_state_changed == 0)
g_main_context_iteration (NULL, TRUE);
g_assert_cmpint (activation_count ("bold"), ==, 1);
g_assert_true (compare_action_groups (G_ACTION_GROUP (group), G_ACTION_GROUP (proxy)));
@ -863,6 +911,10 @@ test_dbus_export (void)
g_dbus_connection_unexport_action_group (bus, id);
g_signal_handler_disconnect (proxy, added_signal_id);
g_signal_handler_disconnect (proxy, enabled_changed_signal_id);
g_signal_handler_disconnect (proxy, removed_signal_id);
g_signal_handler_disconnect (proxy, state_changed_signal_id);
g_object_unref (proxy);
g_object_unref (group);
g_main_loop_unref (loop);