diff --git a/tests/xtimer_usleep_short/main.c b/tests/xtimer_usleep_short/main.c index 7493c68a1a52..84fd2da0b562 100644 --- a/tests/xtimer_usleep_short/main.c +++ b/tests/xtimer_usleep_short/main.c @@ -1,5 +1,6 @@ /* * Copyright (C) 2017 HAW-Hamburg + * 2018 Josua Arndt * * This file is subject to the terms and conditions of the GNU Lesser * General Public License v2.1. See the file LICENSE in the top level @@ -14,6 +15,7 @@ * @brief xtimer_usleep_short test application * * @author Michel Rottleuthner + * @author Josua Arndt * @} */ #include @@ -22,18 +24,56 @@ #define TEST_USLEEP_MIN (0) #define TEST_USLEEP_MAX (500) +#ifdef BOARD_NATIVE +/* native can sometime take more time to respond as it is not real time */ +#define TEST_XTIMER_USLEEP_SHORT_SLEEP_MARGIN_US (1000) +#else +#define TEST_XTIMER_USLEEP_SHORT_SLEEP_MARGIN_US (20) +#endif /* BOARD_NATIVE */ + int main(void) { xtimer_sleep(3); - printf("This test will call xtimer_usleep for values from %d down to %d\n", - TEST_USLEEP_MAX, TEST_USLEEP_MIN); + printf("This test will call xtimer_usleep for values from %d us down to %d us.\n", + TEST_USLEEP_MAX, TEST_USLEEP_MIN); + + printf("Expected delay margin is %d us.\n", + TEST_XTIMER_USLEEP_SHORT_SLEEP_MARGIN_US); + + uint32_t test_time = 0, sleeping_time = 0, margin_fault = 0; for (int i = TEST_USLEEP_MAX; i >= TEST_USLEEP_MIN; i--) { - printf("going to sleep %d usecs...\n", i); + + uint32_t start = xtimer_now_usec(); xtimer_usleep(i); + uint32_t slept = xtimer_now_usec() - start; + + if (slept < (unsigned int)i || + slept > (unsigned int)i + TEST_XTIMER_USLEEP_SHORT_SLEEP_MARGIN_US) { + printf("ERROR: Slept for %" PRIu32 " us not in range [%d, %d] us.\n", + slept, i, i + TEST_XTIMER_USLEEP_SHORT_SLEEP_MARGIN_US); + ++margin_fault; + } + else { + printf("OK: Slept for %" PRIu32 " us not in range [%d, %d] us.\n", + slept, i, i + TEST_XTIMER_USLEEP_SHORT_SLEEP_MARGIN_US); + } + + sleeping_time += slept; + test_time += i; } - puts("[SUCCESS]"); + printf("Slept for %" PRIu32 " us expected %" PRIu32 " us.\n", + sleeping_time, test_time); - return 0; + if (margin_fault != 0) { + printf("Sleep delay margin was not kept for %" PRIu32 " times.\n", + margin_fault); + puts("[FAILED]"); + return 1; + } + else { + puts("[SUCCESS]"); + return 0; + } } diff --git a/tests/xtimer_usleep_short/tests/01-run.py b/tests/xtimer_usleep_short/tests/01-run.py index d242289512f1..e98375d6fd99 100755 --- a/tests/xtimer_usleep_short/tests/01-run.py +++ b/tests/xtimer_usleep_short/tests/01-run.py @@ -12,21 +12,30 @@ def testfunc(child): - child.expect(u"This test will call xtimer_usleep for values from \\d+ down to \\d+\r\n") + child.expect("This test will call xtimer_usleep for values from (\d+) us down to (\d+) us.") + sleep_max = int(child.match.group(1)) + sleep_min = int(child.match.group(2)) + delay = sleep_max - sleep_min - i = 500 + child.expect("Expected delay margin is (\d+) us.") + sleep_margin = int(child.match.group(1)) - while (i >= 0): + for delay in range(sleep_max, sleep_min, -1): try: - child.expect(u"going to sleep \\d+ usecs...\r\n", timeout=3) + child.expect("\w+: Slept for \d+ us not in range \[\d+, \d+\] us.", + timeout=20) + except pexpect.TIMEOUT: - print("xtimer stuck when trying to sleep %d usecs" % (i+1)) + print("xtimer stuck when trying to sleep %d us or failed.".format(delay)) print("[FAILED]") - break - i = i - 1 - - child.expect(u"[SUCCESS]", timeout=3) - + exit(1) + + child.expect("Slept for \d+ us expected \d+ us.") + child.expect("((Sleep delay margin was not kept for \d+ times.)|(\[SUCCESS\]))") + msg = child.match.group(1) + if msg != "[SUCCESS]" : + child.expect("[FAILED]") + exit(1) if __name__ == "__main__": sys.path.append(os.path.join(os.environ['RIOTBASE'], 'dist/tools/testrunner'))