[glib/wip/tingping/localhost-is-local: 1/2] tests: Don't block mainloop for delays in gnetworkaddress tests



commit 8366eeee256b4b5e59ad191132cb0d04f382c187
Author: Patrick Griffis <pgriffis igalia com>
Date:   Tue Feb 5 08:50:09 2019 -0500

    tests: Don't block mainloop for delays in gnetworkaddress tests

 gio/tests/network-address.c | 25 ++++++++++++++++++++-----
 1 file changed, 20 insertions(+), 5 deletions(-)
---
diff --git a/gio/tests/network-address.c b/gio/tests/network-address.c
index 4a2718e24..f1063789f 100644
--- a/gio/tests/network-address.c
+++ b/gio/tests/network-address.c
@@ -417,10 +417,22 @@ test_loopback_sync (void)
 typedef struct {
   GList/*<owned GSocketAddress> */ *addrs;  /* owned */
   GMainLoop *loop;  /* owned */
+  GSocketAddressEnumerator *enumerator; /* unowned */
   guint delay_ms;
   gint expected_error_code;
 } AsyncData;
 
+static void got_addr (GObject *source_object, GAsyncResult *result, gpointer user_data);
+
+static int
+on_delayed_get_addr (gpointer user_data)
+{
+  AsyncData *data = user_data;
+  g_socket_address_enumerator_next_async (data->enumerator, NULL,
+                                          got_addr, user_data);
+  return G_SOURCE_REMOVE;
+}
+
 static void
 got_addr (GObject *source_object, GAsyncResult *result, gpointer user_data)
 {
@@ -453,11 +465,14 @@ got_addr (GObject *source_object, GAsyncResult *result, gpointer user_data)
       g_assert (G_IS_INET_SOCKET_ADDRESS (a));
       data->addrs = g_list_prepend (data->addrs, a);
 
-      if (data->delay_ms)
-        g_usleep (data->delay_ms * 1000);
-
-      g_socket_address_enumerator_next_async (enumerator, NULL,
-                                              got_addr, user_data);
+      if (!data->delay_ms)
+        g_socket_address_enumerator_next_async (enumerator, NULL,
+                                                got_addr, user_data);
+      else
+        {
+          data->enumerator = enumerator;
+          g_timeout_add (data->delay_ms, on_delayed_get_addr, data);
+        }
     }
 }
 


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