Skip to content

Commit 79f6c53

Browse files
Chris Friedtcfriedt
authored andcommitted
tests: posix: add tests for sleep() and usleep()
Previously, there was no test coverage for `sleep()` and `usleep()`. This change adds full test coverage. Signed-off-by: Chris Friedt <[email protected]> (cherry picked from commit 027b79e)
1 parent 3400e6d commit 79f6c53

File tree

3 files changed

+95
-4
lines changed

3 files changed

+95
-4
lines changed

tests/posix/common/src/clock.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ void test_posix_clock(void)
2323
NULL);
2424
zassert_equal(errno, EINVAL, NULL);
2525

26-
zassert_ok(clock_gettime(CLOCK_MONOTONIC, &ts));
27-
zassert_ok(k_sleep(K_SECONDS(SLEEP_SECONDS)));
28-
zassert_ok(clock_gettime(CLOCK_MONOTONIC, &te));
26+
zassert_ok(clock_gettime(CLOCK_MONOTONIC, &ts), NULL);
27+
zassert_ok(k_sleep(K_SECONDS(SLEEP_SECONDS)), NULL);
28+
zassert_ok(clock_gettime(CLOCK_MONOTONIC, &te), NULL);
2929

3030
if (te.tv_nsec >= ts.tv_nsec) {
3131
secs_elapsed = te.tv_sec - ts.tv_sec;

tests/posix/common/src/main.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ extern void test_nanosleep_0_500000000(void);
3737
extern void test_nanosleep_1_0(void);
3838
extern void test_nanosleep_1_1(void);
3939
extern void test_nanosleep_1_1001(void);
40+
extern void test_sleep(void);
41+
extern void test_usleep(void);
4042

4143
void test_main(void)
4244
{
@@ -69,7 +71,9 @@ void test_main(void)
6971
ztest_unit_test(test_nanosleep_1_0),
7072
ztest_unit_test(test_nanosleep_1_1),
7173
ztest_unit_test(test_nanosleep_1_1001),
72-
ztest_unit_test(test_posix_pthread_create_negative)
74+
ztest_unit_test(test_posix_pthread_create_negative),
75+
ztest_unit_test(test_sleep),
76+
ztest_unit_test(test_usleep)
7377
);
7478
ztest_run_test_suite(posix_apis);
7579
}

tests/posix/common/src/sleep.c

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Copyright (c) 2022, Meta
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <posix/unistd.h>
8+
#include <ztest.h>
9+
10+
struct waker_work {
11+
k_tid_t tid;
12+
struct k_work_delayable dwork;
13+
};
14+
static struct waker_work ww;
15+
16+
static void waker_func(struct k_work *work)
17+
{
18+
struct waker_work *ww;
19+
struct k_work_delayable *dwork = k_work_delayable_from_work(work);
20+
21+
ww = CONTAINER_OF(dwork, struct waker_work, dwork);
22+
k_wakeup(ww->tid);
23+
}
24+
K_WORK_DELAYABLE_DEFINE(waker, waker_func);
25+
26+
void test_sleep(void)
27+
{
28+
uint32_t then;
29+
uint32_t now;
30+
/* call sleep(10), wakeup after 1s, expect >= 8s left */
31+
const uint32_t sleep_min_s = 1;
32+
const uint32_t sleep_max_s = 10;
33+
const uint32_t sleep_rem_s = 8;
34+
35+
/* sleeping for 0s should return 0 */
36+
zassert_ok(sleep(0), NULL);
37+
38+
/* test that sleeping for 1s sleeps for at least 1s */
39+
then = k_uptime_get();
40+
zassert_equal(0, sleep(1), NULL);
41+
now = k_uptime_get();
42+
zassert_true((now - then) >= 1 * MSEC_PER_SEC, NULL);
43+
44+
/* test that sleeping for 2s sleeps for at least 2s */
45+
then = k_uptime_get();
46+
zassert_equal(0, sleep(2), NULL);
47+
now = k_uptime_get();
48+
zassert_true((now - then) >= 2 * MSEC_PER_SEC, NULL);
49+
50+
/* test that sleep reports the remainder */
51+
ww.tid = k_current_get();
52+
k_work_init_delayable(&ww.dwork, waker_func);
53+
zassert_equal(1, k_work_schedule(&ww.dwork, K_SECONDS(sleep_min_s)), NULL);
54+
zassert_true(sleep(sleep_max_s) >= sleep_rem_s, NULL);
55+
}
56+
57+
void test_usleep(void)
58+
{
59+
uint32_t then;
60+
uint32_t now;
61+
62+
/* test usleep works for small values */
63+
/* Note: k_usleep(), an implementation detail, is a cancellation point */
64+
zassert_equal(0, usleep(0), NULL);
65+
zassert_equal(0, usleep(1), NULL);
66+
67+
/* sleep for the spec limit */
68+
then = k_uptime_get();
69+
zassert_equal(0, usleep(USEC_PER_SEC - 1), NULL);
70+
now = k_uptime_get();
71+
zassert_true(((now - then) * USEC_PER_MSEC) / (USEC_PER_SEC - 1) >= 1, NULL);
72+
73+
/* sleep for exactly the limit threshold */
74+
zassert_equal(-1, usleep(USEC_PER_SEC), NULL);
75+
zassert_equal(errno, EINVAL, NULL);
76+
77+
/* sleep for over the spec limit */
78+
zassert_equal(-1, usleep((useconds_t)ULONG_MAX), NULL);
79+
zassert_equal(errno, EINVAL, NULL);
80+
81+
/* test that sleep reports errno = EINTR when woken up */
82+
ww.tid = k_current_get();
83+
k_work_init_delayable(&ww.dwork, waker_func);
84+
zassert_equal(1, k_work_schedule(&ww.dwork, K_USEC(USEC_PER_SEC / 2)), NULL);
85+
zassert_equal(-1, usleep(USEC_PER_SEC - 1), NULL);
86+
zassert_equal(EINTR, errno, NULL);
87+
}

0 commit comments

Comments
 (0)