Skip to content

Commit

Permalink
Iterate user table in a sorted way, fix tests with latest glib
Browse files Browse the repository at this point in the history
This is primarily to help test cases which assume that the adopted algorithm
prioritizes the users in the exact order they appear in the test cases (and get
inserted into the session in that order). With older glib version, the five
users being inserted into the user table hash table by the tests happened to
retain the order. With latest glib, due to a minor tweak in hashing strategy,
the order of iteration is different from the order of insertion failing the
tests.

In addition, GHashTable makes no guarantees about the stability of items when
iterating multiple times. Since the algorithm is sensitive to order of users, it
is best to return users in an order that is consistent over multiple calls and
stable over insert/remove operations.

This patch sorts the keys during iteration of users in the user table.

Closes: gobby#22.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
  • Loading branch information
SunilMohanAdapa committed Jun 11, 2020
1 parent e09c5b7 commit cd8a733
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions libinfinity/common/inf-user-table.c
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,13 @@ inf_user_table_lookup_user_by_name_func(gpointer key,
return FALSE;
}

static gint
inf_user_id_list_sort_compare_func(gconstpointer a,
gconstpointer b)
{
return GPOINTER_TO_UINT(a) - GPOINTER_TO_UINT(b);
}

static void
inf_user_table_foreach_user_func(gpointer key,
gpointer value,
Expand Down Expand Up @@ -647,6 +654,11 @@ inf_user_table_foreach_user(InfUserTable* user_table,
{
InfUserTablePrivate* priv;
InfUserTableForeachUserData data;
GList* user_id_list;
GList* item;
InfUser* user;

guint user_id;

g_return_if_fail(INF_IS_USER_TABLE(user_table));
g_return_if_fail(func != NULL);
Expand All @@ -656,11 +668,15 @@ inf_user_table_foreach_user(InfUserTable* user_table,
data.func = func;
data.user_data = user_data;

g_hash_table_foreach(
priv->table,
inf_user_table_foreach_user_func,
&data
);
user_id_list = g_hash_table_get_keys(priv->table);
user_id_list = g_list_sort(user_id_list, inf_user_id_list_sort_compare_func);
for(item = user_id_list; item != NULL; item = g_list_next(item))
{
user_id = GPOINTER_TO_UINT(item->data);
user = inf_user_table_lookup_user_by_id(user_table, user_id);
func(user, user_data);
}
g_list_free(user_id_list);
}

/**
Expand Down

0 comments on commit cd8a733

Please sign in to comment.