Skip to content
Permalink
Browse files

tests/kernel/sleep: add tests for k_usleep() API

Test that k_usleep() allows sleep durations near the limit of what
the platform's tick rate will allow.

Signed-off-by: Charles E. Youse <charles.youse@intel.com>
  • Loading branch information...
Charles E. Youse authored and nashif committed May 22, 2019
1 parent a567831 commit 9ab293b919d9d6cd0769906de8881a92133dafad
Showing with 87 additions and 5 deletions.
  1. +12 −4 tests/kernel/sleep/README.txt
  2. +4 −1 tests/kernel/sleep/src/main.c
  3. +71 −0 tests/kernel/sleep/src/usleep.c
@@ -31,15 +31,23 @@ or

Sample Output:

tc_start() - Test kernel Sleep and Wakeup APIs

Running test suite sleep
===================================================================
starting test - test_sleep
Kernel objects initialized
Test thread started: id = 0x00103044
Helper thread started: id = 0x00102f44
Test thread started: id = 0x00400040
Helper thread started: id = 0x00400000
Testing normal expiration of k_sleep()
Testing: test thread sleep + helper thread wakeup test
Testing: test thread sleep + isr offload wakeup test
Testing: test thread sleep + main wakeup test thread
Testing kernel k_sleep()
PASS - test_sleep
===================================================================
starting test - test_usleep
elapsed_ms = 1000
PASS - test_usleep
===================================================================
Test suite sleep succeeded
===================================================================
PROJECT EXECUTION SUCCESSFUL
@@ -233,10 +233,13 @@ void test_sleep(void)
status = TC_PASS;
}

extern void test_usleep(void);

/*test case main entry*/
void test_main(void)
{
ztest_test_suite(sleep,
ztest_unit_test(test_sleep));
ztest_unit_test(test_sleep),
ztest_unit_test(test_usleep));
ztest_run_test_suite(sleep);
}
@@ -0,0 +1,71 @@
/*
* Copyright (c) 2019 Intel Corp.
*
* SPDX-License-Identifier: Apache-2.0
*/

#include <ztest.h>
#include <zephyr.h>

/*
* precision timing tests in an emulation environment are not reliable.
* if the test passes at least once, we know it works properly, so we
* attempt to repeat the test RETRIES times before reporting failure.
*/

#define RETRIES 10

/*
* Theory of operation: we can't use absolute units (e.g., "sleep for 10us")
* in testing k_usleep() because the granularity of sleeps is highly dependent
* on the hardware's capabilities and kernel configuration. Instead, we
* test that k_usleep() actually sleeps for the minimum possible duration.
* (That minimum duration is presently two ticks; see below.) So, we loop
* k_usleep()ing for as many iterations as should comprise a second, and
* check to see that a total of one second has elapsed.
*/

#define LOWER_BOUND_MS 900 /* +/- 10%, might be too lax */
#define UPPER_BOUND_MS 1100

void test_usleep(void)
{
int retries = 0;
s64_t elapsed_ms;

while (retries < RETRIES) {
s64_t start_ms;
s64_t end_ms;
int i;

++retries;
start_ms = k_uptime_get();

for (i = 0; i < (CONFIG_SYS_CLOCK_TICKS_PER_SEC / 2); ++i) {
/*
* this will always sleep for TWO ticks:
*
* the conversion from 1us to ticks is rounded
* up to the nearest tick boundary, and sleeps
* always have _TICK_ALIGN (currently 1) added
* to their durations.
*/

k_usleep(1);
}

end_ms = k_uptime_get();
elapsed_ms = end_ms - start_ms;

/* if at first you don't succeed, keep sucking. */

if ((elapsed_ms >= LOWER_BOUND_MS) &&
(elapsed_ms < UPPER_BOUND_MS)) {
break;
}
}

printk("elapsed_ms = %lld\n", elapsed_ms);
zassert_true(elapsed_ms >= LOWER_BOUND_MS, "short sleep");
zassert_true(elapsed_ms < UPPER_BOUND_MS, "overslept");
}

0 comments on commit 9ab293b

Please sign in to comment.
You can’t perform that action at this time.