Skip to content

Commit 6737f6b

Browse files
Pancakemcarlescufi
authored andcommitted
tests: posix: common: separate posix timer tests into a smaller testsuite
posix.common contains testsuites that can be separated into smaller groups of tests. This change moves clock, sleep, nanosleep and timer into a singular testsuite at tests/posix/timer app directory. Signed-off-by: Marvin Ouma <[email protected]>
1 parent 892e5e1 commit 6737f6b

File tree

9 files changed

+340
-245
lines changed

9 files changed

+340
-245
lines changed

tests/posix/common/src/clock.c

+1-215
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,6 @@
1111
#include <zephyr/ztest.h>
1212
#include <zephyr/logging/log.h>
1313

14-
#define SLEEP_SECONDS 1
15-
#define CLOCK_INVALID -1
16-
17-
LOG_MODULE_REGISTER(clock_test, LOG_LEVEL_DBG);
18-
19-
/* Set a particular time. In this case, the output of: `date +%s -d 2018-01-01T15:45:01Z` */
20-
static const struct timespec ref_ts = {1514821501, NSEC_PER_SEC / 2U};
21-
22-
static const clockid_t clocks[] = {
23-
CLOCK_MONOTONIC,
24-
CLOCK_REALTIME,
25-
};
26-
static const bool settable[] = {
27-
false,
28-
true,
29-
};
30-
3114
static inline int64_t ts_to_ns(const struct timespec *ts)
3215
{
3316
return ts->tv_sec * NSEC_PER_SEC + ts->tv_nsec;
@@ -47,47 +30,7 @@ static inline void tv_to_ts(const struct timeval *tv, struct timespec *ts)
4730
return _tp_op(_a, _b, _op); \
4831
}
4932

50-
_decl_op(bool, tp_eq, ==); /* a == b */
51-
_decl_op(bool, tp_lt, <); /* a < b */
52-
_decl_op(bool, tp_gt, >); /* a > b */
53-
_decl_op(bool, tp_le, <=); /* a <= b */
54-
_decl_op(bool, tp_ge, >=); /* a >= b */
55-
_decl_op(int64_t, tp_diff, -); /* a - b */
56-
57-
/* lo <= (a - b) < hi */
58-
static inline bool tp_diff_in_range_ns(const struct timespec *a, const struct timespec *b,
59-
int64_t lo, int64_t hi)
60-
{
61-
int64_t diff = tp_diff(a, b);
62-
63-
return diff >= lo && diff < hi;
64-
}
65-
66-
ZTEST(clock, test_clock_gettime)
67-
{
68-
struct timespec ts;
69-
70-
/* ensure argument validation is performed */
71-
errno = 0;
72-
zassert_equal(clock_gettime(CLOCK_INVALID, &ts), -1);
73-
zassert_equal(errno, EINVAL);
74-
75-
if (false) {
76-
/* undefined behaviour */
77-
errno = 0;
78-
zassert_equal(clock_gettime(clocks[0], NULL), -1);
79-
zassert_equal(errno, EINVAL);
80-
}
81-
82-
/* verify that we can call clock_gettime() on supported clocks */
83-
ARRAY_FOR_EACH(clocks, i)
84-
{
85-
ts = (struct timespec){-1, -1};
86-
zassert_ok(clock_gettime(clocks[i], &ts));
87-
zassert_not_equal(ts.tv_sec, -1);
88-
zassert_not_equal(ts.tv_nsec, -1);
89-
}
90-
}
33+
_decl_op(bool, tp_ge, >=); /* a >= b */
9134

9235
ZTEST(clock, test_gettimeofday)
9336
{
@@ -114,161 +57,4 @@ ZTEST(clock, test_gettimeofday)
11457
zassert_true(tp_ge(&rts, &ts));
11558
}
11659

117-
ZTEST(clock, test_clock_settime)
118-
{
119-
int64_t diff_ns;
120-
struct timespec ts = {0};
121-
122-
BUILD_ASSERT(ARRAY_SIZE(settable) == ARRAY_SIZE(clocks));
123-
124-
/* ensure argument validation is performed */
125-
errno = 0;
126-
zassert_equal(clock_settime(CLOCK_INVALID, &ts), -1);
127-
zassert_equal(errno, EINVAL);
128-
129-
if (false) {
130-
/* undefined behaviour */
131-
errno = 0;
132-
zassert_equal(clock_settime(CLOCK_REALTIME, NULL), -1);
133-
zassert_equal(errno, EINVAL);
134-
}
135-
136-
/* verify nanoseconds */
137-
errno = 0;
138-
ts = (struct timespec){0, NSEC_PER_SEC};
139-
zassert_equal(clock_settime(CLOCK_REALTIME, &ts), -1);
140-
zassert_equal(errno, EINVAL);
141-
errno = 0;
142-
ts = (struct timespec){0, -1};
143-
zassert_equal(clock_settime(CLOCK_REALTIME, &ts), -1);
144-
zassert_equal(errno, EINVAL);
145-
146-
ARRAY_FOR_EACH(clocks, i)
147-
{
148-
if (!settable[i]) {
149-
/* should fail attempting to set unsettable clocks */
150-
errno = 0;
151-
zassert_equal(clock_settime(clocks[i], &ts), -1);
152-
zassert_equal(errno, EINVAL);
153-
continue;
154-
}
155-
156-
zassert_ok(clock_settime(clocks[i], &ref_ts));
157-
158-
/* read-back the time */
159-
zassert_ok(clock_gettime(clocks[i], &ts));
160-
/* dt should be >= 0, but definitely <= 1s */
161-
diff_ns = tp_diff(&ts, &ref_ts);
162-
zassert_true(diff_ns >= 0 && diff_ns <= NSEC_PER_SEC);
163-
}
164-
}
165-
166-
ZTEST(clock, test_realtime)
167-
{
168-
struct timespec then, now;
169-
/*
170-
* For calculating cumulative moving average
171-
* Note: we do not want to assert any individual samples due to scheduler noise.
172-
* The CMA filters out the noise so we can make an assertion (on average).
173-
* https://en.wikipedia.org/wiki/Moving_average#Cumulative_moving_average
174-
*/
175-
int64_t cma_prev = 0;
176-
int64_t cma;
177-
int64_t x_i;
178-
/* lower and uppoer boundary for assertion */
179-
int64_t lo = CONFIG_TEST_CLOCK_RT_SLEEP_MS;
180-
int64_t hi = CONFIG_TEST_CLOCK_RT_SLEEP_MS + CONFIG_TEST_CLOCK_RT_ERROR_MS;
181-
/* lower and upper watermark */
182-
int64_t lo_wm = INT64_MAX;
183-
int64_t hi_wm = INT64_MIN;
184-
185-
/* Loop n times, sleeping a little bit for each */
186-
(void)clock_gettime(CLOCK_REALTIME, &then);
187-
for (int i = 0; i < CONFIG_TEST_CLOCK_RT_ITERATIONS; ++i) {
188-
189-
zassert_ok(k_usleep(USEC_PER_MSEC * CONFIG_TEST_CLOCK_RT_SLEEP_MS));
190-
(void)clock_gettime(CLOCK_REALTIME, &now);
191-
192-
/* Make the delta milliseconds. */
193-
x_i = tp_diff(&now, &then) / NSEC_PER_MSEC;
194-
then = now;
195-
196-
if (x_i < lo_wm) {
197-
/* update low watermark */
198-
lo_wm = x_i;
199-
}
200-
201-
if (x_i > hi_wm) {
202-
/* update high watermark */
203-
hi_wm = x_i;
204-
}
205-
206-
/* compute cumulative running average */
207-
cma = (x_i + i * cma_prev) / (i + 1);
208-
cma_prev = cma;
209-
}
210-
211-
LOG_INF("n: %d, sleep: %d, margin: %d, lo: %lld, avg: %lld, hi: %lld",
212-
CONFIG_TEST_CLOCK_RT_ITERATIONS, CONFIG_TEST_CLOCK_RT_SLEEP_MS,
213-
CONFIG_TEST_CLOCK_RT_ERROR_MS, lo_wm, cma, hi_wm);
214-
zassert_between_inclusive(cma, lo, hi);
215-
}
216-
217-
ZTEST(clock, test_clock_getcpuclockid)
218-
{
219-
int ret = 0;
220-
clockid_t clock_id = CLOCK_INVALID;
221-
222-
ret = clock_getcpuclockid((pid_t)0, &clock_id);
223-
zassert_equal(ret, 0, "POSIX clock_getcpuclock id failed");
224-
zassert_equal(clock_id, CLOCK_PROCESS_CPUTIME_ID, "POSIX clock_getcpuclock id failed");
225-
226-
ret = clock_getcpuclockid((pid_t)2482, &clock_id);
227-
zassert_equal(ret, EPERM, "POSIX clock_getcpuclock id failed");
228-
}
229-
230-
ZTEST(clock, test_clock_getres)
231-
{
232-
int ret;
233-
struct timespec res;
234-
const struct timespec one_ns = {
235-
.tv_sec = 0,
236-
.tv_nsec = 1,
237-
};
238-
239-
struct arg {
240-
clockid_t clock_id;
241-
struct timespec *res;
242-
int expect;
243-
};
244-
245-
const struct arg args[] = {
246-
/* permuting over "invalid" inputs */
247-
{CLOCK_INVALID, NULL, -1},
248-
{CLOCK_INVALID, &res, -1},
249-
{CLOCK_REALTIME, NULL, 0},
250-
{CLOCK_MONOTONIC, NULL, 0},
251-
{CLOCK_PROCESS_CPUTIME_ID, NULL, 0},
252-
253-
/* all valid inputs */
254-
{CLOCK_REALTIME, &res, 0},
255-
{CLOCK_MONOTONIC, &res, 0},
256-
{CLOCK_PROCESS_CPUTIME_ID, &res, 0},
257-
};
258-
259-
ARRAY_FOR_EACH_PTR(args, arg) {
260-
errno = 0;
261-
res = (struct timespec){0};
262-
ret = clock_getres(arg->clock_id, arg->res);
263-
zassert_equal(ret, arg->expect);
264-
if (ret != 0) {
265-
zassert_equal(errno, EINVAL);
266-
continue;
267-
}
268-
if (arg->res != NULL) {
269-
zassert_true(tp_ge(arg->res, &one_ns));
270-
}
271-
}
272-
}
273-
27460
ZTEST_SUITE(clock, NULL, NULL, NULL, NULL, NULL);

tests/posix/timers/CMakeLists.txt

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
5+
project(posix_timers)
6+
7+
FILE(GLOB app_sources src/*.c)
8+
9+
target_sources(app PRIVATE ${app_sources})
10+
11+
target_compile_options(app PRIVATE -U_POSIX_C_SOURCE -D_POSIX_C_SOURCE=200809L)

tests/posix/timers/Kconfig

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Copyright (c) 2023, Meta
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
# Options specific to clock.c / test_realtime
5+
6+
config TEST_CLOCK_RT_ITERATIONS
7+
int "Number of iterations to check clock_gettime() reliability"
8+
range 10 100
9+
default 20
10+
help
11+
This option is specific to posix_apis.test_realtime in clock.c
12+
13+
config TEST_CLOCK_RT_SLEEP_MS
14+
int "Time to sleep between iterations in milliseconds"
15+
range 50 1000
16+
default 100
17+
help
18+
This option is specific to posix_apis.test_realtime in clock.c
19+
20+
config TEST_CLOCK_RT_ERROR_MS
21+
int "Maximum overshoot (error) in milliseconds"
22+
range 10 500
23+
default 10
24+
help
25+
This option is specific to posix_apis.test_realtime in clock.c
26+
27+
source "Kconfig.zephyr"

tests/posix/timers/prj.conf

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CONFIG_POSIX_API=y
2+
CONFIG_ZTEST=y
3+
4+
CONFIG_POSIX_AEP_CHOICE_BASE=y
5+
CONFIG_POSIX_TIMERS=y

0 commit comments

Comments
 (0)