tests: Fix an intermittent timing error with testing g_usleep(0)

The code to test that `g_usleep(0)` returns immediately assumes that
`g_usleep(1)` always takes longer, but that’s not necessarily always the
case. Even if no sleeping happens, the function call for `g_usleep(0)`
could get descheduled and take longer than normal.

This results in occasional failures like this one:
```
GLib:ERROR:../glib/tests/timer.c:367:test_usleep_with_zero_wait: assertion failed (elapsed0 <= elapsed1): (0.000206 <= 0.000202)
```

(Source: https://gitlab.gnome.org/GNOME/glib/-/jobs/2898468)

I can’t think of a suitable invariant comparison which can be done with
the timers, but running the comparison 10 times and allowing it to fail
once should work. A probabilistic test of `g_usleep(0)`.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>
This commit is contained in:
Philip Withnall 2023-06-21 11:49:31 +01:00
parent b5d995e6b8
commit 679560feaa

View File

@ -350,10 +350,18 @@ static void
test_usleep_with_zero_wait (void)
{
GTimer *timer;
gdouble elapsed0, elapsed1;
unsigned int n_times_shorter = 0;
timer = g_timer_new ();
/* Test that g_usleep(0) sleeps for less time than g_usleep(1). We cant
* actually guarantee this, since the exact length of g_usleep(1) is not
* guaranteed, but we can say that it probably should be longer 9 times out
* of 10. */
for (unsigned int i = 0; i < 10; i++)
{
gdouble elapsed0, elapsed1;
g_timer_start (timer);
g_usleep (0);
elapsed0 = g_timer_elapsed (timer, NULL);
@ -364,7 +372,11 @@ test_usleep_with_zero_wait (void)
elapsed1 = g_timer_elapsed (timer, NULL);
g_timer_stop (timer);
g_assert_cmpfloat (elapsed0, <=, elapsed1);
if (elapsed0 <= elapsed1)
n_times_shorter++;
}
g_assert_cmpuint (n_times_shorter, >=, 9);
g_clear_pointer (&timer, g_timer_destroy);
}