tests/kernel timer_api: Test absolute timers with loosing ticks

Let's also explicitly test that absolute timeouts trigger
at the correct time, even if the kernel has not seen
system clock timer announcements for a while.

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
This commit is contained in:
Alberto Escolar Piedras 2024-05-07 17:07:49 +02:00 committed by Anas Nashif
commit a1576af7af

View file

@ -803,10 +803,30 @@ ZTEST_USER(timer_api, test_sleep_abs)
* time slop or more depending on the time to resume * time slop or more depending on the time to resume
*/ */
k_ticks_t late = end - (start + sleep_ticks); k_ticks_t late = end - (start + sleep_ticks);
int slop = MAX(2, k_us_to_ticks_ceil32(250));
zassert_true(late >= 0 && late <= MAX(2, k_us_to_ticks_ceil32(250)), zassert_true(late >= 0 && late <= slop,
"expected wakeup at %lld, got %lld (late %lld)", "expected wakeup at %lld, got %lld (late %lld)",
start + sleep_ticks, end, late); start + sleep_ticks, end, late);
/* Let's test that an absolute delay awakes at the correct time
* even if the system did not get some ticks announcements
*/
int tickless_wait = 5;
start = end;
k_busy_wait(k_ticks_to_us_ceil32(tickless_wait));
/* We expect to not have got <tickless_wait> tick announcements,
* as there is currently nothing scheduled
*/
k_sleep(K_TIMEOUT_ABS_TICKS(start + sleep_ticks));
end = k_uptime_ticks();
late = end - (start + sleep_ticks);
zassert_true(late >= 0 && late <= slop,
"expected wakeup at %lld, got %lld (late %lld)",
start + sleep_ticks, end, late);
} }
static void timer_init(struct k_timer *timer, k_timer_expiry_t expiry_fn, static void timer_init(struct k_timer *timer, k_timer_expiry_t expiry_fn,