Merge branch 'wip/pwithnall/network-address-test-fixes' into 'main'

tests: Remove threads from mock-resolver/network-address test

See merge request GNOME/glib!2520
This commit is contained in:
Philip Withnall 2022-02-23 12:03:28 +00:00
commit 7f8c09cd07
2 changed files with 124 additions and 29 deletions

View File

@ -87,18 +87,50 @@ mock_resolver_set_ipv6_error (MockResolver *self, GError *error)
self->ipv6_error = g_error_copy (error);
}
static gboolean lookup_by_name_cb (gpointer user_data);
/* Core of the implementation of `lookup_by_name()` in the mock resolver.
*
* It creates a #GSource which will become ready with the resolver results. It
* will become ready either after a timeout, or as an idle callback. This
* simulates doing some actual network-based resolution work.
*
* A previous implementation of this did the work in a thread, but that made it
* hard to synchronise the timeouts with the #GResolver failure timeouts in the
* calling thread, as spawning a worker thread could be subject to non-trivial
* delays. */
static void
do_lookup_by_name (GTask *task,
gpointer source_object,
gpointer task_data,
GCancellable *cancellable)
do_lookup_by_name (MockResolver *self,
GTask *task,
GResolverNameLookupFlags flags)
{
MockResolver *self = source_object;
GResolverNameLookupFlags flags = GPOINTER_TO_UINT(task_data);
GSource *source = NULL;
g_task_set_task_data (task, GINT_TO_POINTER (flags), NULL);
if (flags == G_RESOLVER_NAME_LOOKUP_FLAGS_IPV4_ONLY)
source = g_timeout_source_new (self->ipv4_delay_ms);
else if (flags == G_RESOLVER_NAME_LOOKUP_FLAGS_IPV6_ONLY)
source = g_timeout_source_new (self->ipv6_delay_ms);
else if (flags == G_RESOLVER_NAME_LOOKUP_FLAGS_DEFAULT)
source = g_idle_source_new ();
else
g_assert_not_reached ();
g_source_set_callback (source, lookup_by_name_cb, g_object_ref (task), g_object_unref);
g_source_attach (source, g_main_context_get_thread_default ());
g_source_unref (source);
}
static gboolean
lookup_by_name_cb (gpointer user_data)
{
GTask *task = G_TASK (user_data);
MockResolver *self = g_task_get_source_object (task);
GResolverNameLookupFlags flags = GPOINTER_TO_INT (g_task_get_task_data (task));
if (flags == G_RESOLVER_NAME_LOOKUP_FLAGS_IPV4_ONLY)
{
g_usleep (self->ipv4_delay_ms * 1000);
if (self->ipv4_error)
g_task_return_error (task, g_error_copy (self->ipv4_error));
else
@ -106,7 +138,6 @@ do_lookup_by_name (GTask *task,
}
else if (flags == G_RESOLVER_NAME_LOOKUP_FLAGS_IPV6_ONLY)
{
g_usleep (self->ipv6_delay_ms * 1000);
if (self->ipv6_error)
g_task_return_error (task, g_error_copy (self->ipv6_error));
else
@ -120,6 +151,8 @@ do_lookup_by_name (GTask *task,
}
else
g_assert_not_reached ();
return G_SOURCE_REMOVE;
}
static void
@ -130,27 +163,65 @@ lookup_by_name_with_flags_async (GResolver *resolver,
GAsyncReadyCallback callback,
gpointer user_data)
{
GTask *task = g_task_new (resolver, cancellable, callback, user_data);
g_task_set_task_data (task, GUINT_TO_POINTER(flags), NULL);
g_task_run_in_thread (task, do_lookup_by_name);
MockResolver *self = MOCK_RESOLVER (resolver);
GTask *task = NULL;
task = g_task_new (resolver, cancellable, callback, user_data);
g_task_set_source_tag (task, lookup_by_name_with_flags_async);
do_lookup_by_name (self, task, flags);
g_object_unref (task);
}
static void
async_result_cb (GObject *source_object,
GAsyncResult *result,
gpointer user_data)
{
GAsyncResult **result_out = user_data;
g_assert (*result_out == NULL);
*result_out = g_object_ref (result);
g_main_context_wakeup (g_main_context_get_thread_default ());
}
static GList *
lookup_by_name (GResolver *resolver,
const gchar *hostname,
GCancellable *cancellable,
lookup_by_name (GResolver *resolver,
const gchar *hostname,
GCancellable *cancellable,
GError **error)
{
MockResolver *self = MOCK_RESOLVER (resolver);
GMainContext *context = NULL;
GList *result = NULL;
GTask *task = g_task_new (resolver, cancellable, NULL, NULL);
g_task_set_task_data (task, GUINT_TO_POINTER (G_RESOLVER_NAME_LOOKUP_FLAGS_DEFAULT), NULL);
g_task_run_in_thread_sync (task, do_lookup_by_name);
result = g_task_propagate_pointer (task, error);
g_object_unref (task);
return result;
}
GAsyncResult *async_result = NULL;
GTask *task = NULL;
context = g_main_context_new ();
g_main_context_push_thread_default (context);
task = g_task_new (resolver, cancellable, async_result_cb, &async_result);
g_task_set_source_tag (task, lookup_by_name);
/* Set up the resolution job. */
do_lookup_by_name (self, task, G_RESOLVER_NAME_LOOKUP_FLAGS_DEFAULT);
/* Wait for it to complete synchronously. */
while (async_result == NULL)
g_main_context_iteration (context, TRUE);
result = g_task_propagate_pointer (G_TASK (async_result), error);
g_object_unref (async_result);
g_assert_finalize_object (task);
g_main_context_pop_thread_default (context);
g_main_context_unref (context);
return g_steal_pointer (&result);
}
static GList *
lookup_by_name_with_flags_finish (GResolver *resolver,

View File

@ -20,7 +20,7 @@ test_basic (void)
g_object_get (address, "hostname", &hostname, "port", &port, "scheme", &scheme, NULL);
g_assert_cmpstr (hostname, ==, "www.gnome.org");
g_assert_cmpint (port, ==, 8080);
g_assert (scheme == NULL);
g_assert_null (scheme);
g_free (hostname);
g_object_unref (address);
@ -284,8 +284,8 @@ test_scope_id (GSocketConnectable *addr)
saddr = g_socket_address_enumerator_next (addr_enum, NULL, &error);
g_assert_no_error (error);
g_assert (saddr != NULL);
g_assert (G_IS_INET_SOCKET_ADDRESS (saddr));
g_assert_nonnull (saddr);
g_assert_true (G_IS_INET_SOCKET_ADDRESS (saddr));
isaddr = G_INET_SOCKET_ADDRESS (saddr);
g_assert_cmpint (g_inet_socket_address_get_scope_id (isaddr), ==, SCOPE_ID_TEST_INDEX);
@ -299,7 +299,7 @@ test_scope_id (GSocketConnectable *addr)
g_object_unref (saddr);
saddr = g_socket_address_enumerator_next (addr_enum, NULL, &error);
g_assert_no_error (error);
g_assert (saddr == NULL);
g_assert_null (saddr);
g_object_unref (addr_enum);
#else
@ -377,7 +377,7 @@ assert_socket_address_matches (GSocketAddress *a,
GInetSocketAddress *sa;
gchar *str; /* owned */
g_assert (G_IS_INET_SOCKET_ADDRESS (a));
g_assert_true (G_IS_INET_SOCKET_ADDRESS (a));
sa = G_INET_SOCKET_ADDRESS (a);
g_assert_cmpint (g_inet_socket_address_get_port (sa), ==, expected_port);
@ -593,7 +593,7 @@ got_addr (GObject *source_object,
}
else
{
g_assert (G_IS_INET_SOCKET_ADDRESS (a));
g_assert_true (G_IS_INET_SOCKET_ADDRESS (a));
data->addrs = g_list_prepend (data->addrs, a);
if (!data->delay_ms)
@ -768,16 +768,22 @@ sort_socket_addresses (gconstpointer a, gconstpointer b)
static void
assert_list_matches_expected (GList *result, GList *expected)
{
GList *result_copy = NULL;
g_assert_cmpint (g_list_length (result), ==, g_list_length (expected));
/* Sort by ipv4 first which matches the expected list */
result = g_list_sort (result, sort_socket_addresses);
/* Sort by ipv4 first which matches the expected list. Do this on a copy of
* @result to avoid modifying the original. */
result_copy = g_list_copy (result);
result = result_copy = g_list_sort (result_copy, sort_socket_addresses);
for (; result != NULL; result = result->next, expected = expected->next)
{
GInetAddress *address = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (result->data));
g_assert_true (g_inet_address_equal (address, expected->data));
}
g_list_free (result_copy);
}
typedef struct {
@ -855,6 +861,8 @@ test_happy_eyeballs_basic (HappyEyeballsFixture *fixture,
g_main_loop_run (fixture->loop);
assert_list_matches_expected (data.addrs, fixture->input_all_results);
g_list_free_full (data.addrs, (GDestroyNotify) g_object_unref);
}
static void
@ -879,6 +887,7 @@ test_happy_eyeballs_parallel (HappyEyeballsFixture *fixture,
/* Run again to ensure the cache from the previous one is correct */
g_list_free_full (data.addrs, (GDestroyNotify) g_object_unref);
data.addrs = NULL;
g_object_unref (enumerator2);
@ -887,7 +896,9 @@ test_happy_eyeballs_parallel (HappyEyeballsFixture *fixture,
g_main_loop_run (fixture->loop);
assert_list_matches_expected (data.addrs, fixture->input_all_results);
g_object_unref (enumerator2);
g_list_free_full (data.addrs, (GDestroyNotify) g_object_unref);
}
static void
@ -905,6 +916,8 @@ test_happy_eyeballs_slow_ipv4 (HappyEyeballsFixture *fixture,
g_main_loop_run (fixture->loop);
assert_list_matches_expected (data.addrs, fixture->input_all_results);
g_list_free_full (data.addrs, (GDestroyNotify) g_object_unref);
}
static void
@ -922,6 +935,8 @@ test_happy_eyeballs_slow_ipv6 (HappyEyeballsFixture *fixture,
g_main_loop_run (fixture->loop);
assert_list_matches_expected (data.addrs, fixture->input_all_results);
g_list_free_full (data.addrs, (GDestroyNotify) g_object_unref);
}
static void
@ -939,6 +954,8 @@ test_happy_eyeballs_very_slow_ipv6 (HappyEyeballsFixture *fixture,
g_main_loop_run (fixture->loop);
assert_list_matches_expected (data.addrs, fixture->input_all_results);
g_list_free_full (data.addrs, (GDestroyNotify) g_object_unref);
}
static void
@ -958,6 +975,8 @@ test_happy_eyeballs_slow_connection_and_ipv4 (HappyEyeballsFixture *fixture,
g_main_loop_run (fixture->loop);
assert_list_matches_expected (data.addrs, fixture->input_all_results);
g_list_free_full (data.addrs, (GDestroyNotify) g_object_unref);
}
static void
@ -979,6 +998,7 @@ test_happy_eyeballs_ipv6_error_ipv4_first (HappyEyeballsFixture *fixture,
assert_list_matches_expected (data.addrs, fixture->input_ipv4_results);
g_list_free_full (data.addrs, (GDestroyNotify) g_object_unref);
g_error_free (ipv6_error);
}
@ -1001,6 +1021,7 @@ test_happy_eyeballs_ipv6_error_ipv6_first (HappyEyeballsFixture *fixture,
assert_list_matches_expected (data.addrs, fixture->input_ipv4_results);
g_list_free_full (data.addrs, (GDestroyNotify) g_object_unref);
g_error_free (ipv6_error);
}
@ -1026,6 +1047,7 @@ test_happy_eyeballs_ipv6_error_ipv4_very_slow (HappyEyeballsFixture *fixture,
assert_list_matches_expected (data.addrs, fixture->input_ipv4_results);
g_list_free_full (data.addrs, (GDestroyNotify) g_object_unref);
g_error_free (ipv6_error);
}
@ -1048,6 +1070,7 @@ test_happy_eyeballs_ipv4_error_ipv4_first (HappyEyeballsFixture *fixture,
assert_list_matches_expected (data.addrs, fixture->input_ipv6_results);
g_list_free_full (data.addrs, (GDestroyNotify) g_object_unref);
g_error_free (ipv4_error);
}
@ -1070,6 +1093,7 @@ test_happy_eyeballs_ipv4_error_ipv6_first (HappyEyeballsFixture *fixture,
assert_list_matches_expected (data.addrs, fixture->input_ipv6_results);
g_list_free_full (data.addrs, (GDestroyNotify) g_object_unref);
g_error_free (ipv4_error);
}