[glib: 5/6] tests: Iterate the GMainContext directly rather than using a GMainLoop



commit ee3216b31b098a543e32ec3444d69605d07d0eae
Author: Philip Withnall <withnall endlessm com>
Date:   Wed Jun 17 18:32:28 2020 +0100

    tests: Iterate the GMainContext directly rather than using a GMainLoop
    
    This speeds up the `cancellable` test a little by stopping waiting for
    the threads to start up as soon as they have started, rather than after
    an arbitrary timeout.
    
    Signed-off-by: Philip Withnall <withnall endlessm com>
    
    Helps: #1764

 gio/tests/cancellable.c | 28 ++++++++--------------------
 1 file changed, 8 insertions(+), 20 deletions(-)
---
diff --git a/gio/tests/cancellable.c b/gio/tests/cancellable.c
index 62a10702e..d6b52c1ea 100644
--- a/gio/tests/cancellable.c
+++ b/gio/tests/cancellable.c
@@ -152,8 +152,6 @@ mock_operation_finish (GAsyncResult  *result,
   return g_atomic_int_get (&data->iterations_done);
 }
 
-GMainLoop *loop;
-
 static void
 on_mock_operation_ready (GObject      *source,
                          GAsyncResult *result,
@@ -171,17 +169,7 @@ on_mock_operation_ready (GObject      *source,
 
   g_assert_cmpint (iterations_requested, >, iterations_done);
   num_async_operations--;
-
-  if (!num_async_operations)
-    g_main_loop_quit (loop);
-}
-
-static gboolean
-on_main_loop_timeout_quit (gpointer user_data)
-{
-  GMainLoop *loop = user_data;
-  g_main_loop_quit (loop);
-  return FALSE;
+  g_main_context_wakeup (NULL);
 }
 
 static void
@@ -197,7 +185,6 @@ test_cancel_multiple_concurrent (void)
     }
 
   cancellable = g_cancellable_new ();
-  loop = g_main_loop_new (NULL, FALSE);
 
   for (i = 0; i < 45; i++)
     {
@@ -207,21 +194,22 @@ test_cancel_multiple_concurrent (void)
       num_async_operations++;
     }
 
-  /* Wait for two iterations, to give threads a chance to start up */
-  g_timeout_add (WAIT_ITERATION * 2, on_main_loop_timeout_quit, loop);
-  g_main_loop_run (loop);
-  g_assert_cmpint (num_async_operations, ==, 45);
+  /* Wait for the threads to start up */
+  while (num_async_operations != 45)
+    g_main_context_iteration (NULL, TRUE);
+  g_assert_cmpint (num_async_operations, ==, 45);\
+
   if (g_test_verbose ())
     g_test_message ("CANCEL: %d operations", num_async_operations);
   g_cancellable_cancel (cancellable);
   g_assert_true (g_cancellable_is_cancelled (cancellable));
 
   /* Wait for all operations to be cancelled */
-  g_main_loop_run (loop);
+  while (num_async_operations != 0)
+    g_main_context_iteration (NULL, TRUE);
   g_assert_cmpint (num_async_operations, ==, 0);
 
   g_object_unref (cancellable);
-  g_main_loop_unref (loop);
 }
 
 static void


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