[gnome-bluetooth/wip/hadess/more-timeout-fixes] tests: Check for overly long waits in battery test




commit a5fbbb94e0bd64487372551d0a8207ef0499fd17
Author: Bastien Nocera <hadess hadess net>
Date:   Wed Feb 23 14:33:47 2022 +0100

    tests: Check for overly long waits in battery test
    
    Check whether we've started the test more than ~28 seconds ago to avoid
    having the whole test time out at the meson level.

 tests/test_battery.c | 25 ++++++++++++++++++++++---
 1 file changed, 22 insertions(+), 3 deletions(-)
---
diff --git a/tests/test_battery.c b/tests/test_battery.c
index 35029ef4..3ba7dea9 100644
--- a/tests/test_battery.c
+++ b/tests/test_battery.c
@@ -30,17 +30,29 @@ remove_upower_device (GDBusConnection *bus,
        g_assert_nonnull (ret);
 }
 
+#define DEFAULT_MESON_TIMEOUT 30.0
+#define EXIT_SLACK             2.0
+static gboolean
+check_timed_out (GTimer *timer)
+{
+       return (g_timer_elapsed (timer, NULL) - (DEFAULT_MESON_TIMEOUT - EXIT_SLACK)) > 0.0;
+}
+
 int main (int argc, char **argv)
 {
        BluetoothClient *client;
        GListStore *list_store;
+       GTimer *timer;
 
+       timer = g_timer_new ();
        client = bluetooth_client_new ();
        list_store = bluetooth_client_get_devices (client);
 
        /* Wait for bluez */
-       while (g_list_model_get_n_items (G_LIST_MODEL (list_store)) != 2)
+       while (g_list_model_get_n_items (G_LIST_MODEL (list_store)) != 2) {
+               g_assert_false (check_timed_out (timer));
                g_main_context_iteration (NULL, TRUE);
+       }
        g_assert_cmpuint (g_list_model_get_n_items (G_LIST_MODEL (list_store)), ==, 2);
 
        BluetoothDevice *device;
@@ -50,6 +62,7 @@ int main (int argc, char **argv)
        /* Wait for upower */
        g_object_get (G_OBJECT (device), "battery-type", &battery_type, NULL);
        while (battery_type != BLUETOOTH_BATTERY_TYPE_PERCENTAGE) {
+               g_assert_false (check_timed_out (timer));
                g_main_context_iteration (NULL, TRUE);
                g_object_get (G_OBJECT (device), "battery-type", &battery_type, NULL);
        }
@@ -83,14 +96,17 @@ int main (int argc, char **argv)
        client2 = bluetooth_client_new ();
        list_store2 = bluetooth_client_get_devices (client2);
 
-       while (g_list_model_get_n_items (G_LIST_MODEL (list_store2)) != 2)
+       while (g_list_model_get_n_items (G_LIST_MODEL (list_store2)) != 2) {
+               g_assert_false (check_timed_out (timer));
                g_main_context_iteration (NULL, TRUE);
+       }
        g_assert_cmpuint (g_list_model_get_n_items (G_LIST_MODEL (list_store2)), ==, 2);
 
        BluetoothDevice *device2;
        device2 = g_list_model_get_item (G_LIST_MODEL (list_store2), 0);
        g_object_get (G_OBJECT (device2), "battery-type", &battery_type, NULL);
        while (battery_type != BLUETOOTH_BATTERY_TYPE_PERCENTAGE) {
+               g_assert_false (check_timed_out (timer));
                g_main_context_iteration (NULL, TRUE);
                g_object_get (G_OBJECT (device2), "battery-type", &battery_type, NULL);
        }
@@ -100,6 +116,7 @@ int main (int argc, char **argv)
        remove_upower_device (bus, "/org/freedesktop/UPower/devices/mouse_dev_11_22_33_44_55_66");
        g_object_get (G_OBJECT (device2), "battery-type", &battery_type, NULL);
        while (battery_type != BLUETOOTH_BATTERY_TYPE_NONE) {
+               g_assert_false (check_timed_out (timer));
                g_main_context_iteration (NULL, TRUE);
                g_object_get (G_OBJECT (device2), "battery-type", &battery_type, NULL);
        }
@@ -117,8 +134,10 @@ int main (int argc, char **argv)
                                     NULL,
                                     NULL);
        remove_upower_device (bus, "/org/freedesktop/UPower/devices/mouse_dev_11_22_33_44_55_67");
-       while (g_list_model_get_n_items (G_LIST_MODEL (list_store2)) != 1)
+       while (g_list_model_get_n_items (G_LIST_MODEL (list_store2)) != 1) {
+               g_assert_false (check_timed_out (timer));
                g_main_context_iteration (NULL, TRUE);
+       }
        g_assert_cmpuint (g_list_model_get_n_items (G_LIST_MODEL (list_store2)), ==, 1);
 
        g_object_unref (G_OBJECT (list_store2));


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