Skip to content
Snippets Groups Projects
Commit bf70d8e8 authored by Hauke Petersen's avatar Hauke Petersen Committed by GitHub
Browse files

Merge pull request #7818 from miri64/tests/fix/xtimer_usleep

tests: xtimer_usleep: remove timer testing in pexpect
parents 0224af1e 24fa7183
No related branches found
No related tags found
No related merge requests found
# xtimer_usleep test application
This test tests `xtimer_usleep()` both against the timings of
`xtimer_now_usec()` and by providing capabilities to compare against an external
timer.
## Usage
```
make flash test
```
/* /*
* Copyright (C) 2017 Inria * Copyright (C) 2017 Inria
* 2017 Freie Universität Berlin
* *
* This file is subject to the terms and conditions of the GNU Lesser * 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 * General Public License v2.1. See the file LICENSE in the top level
...@@ -14,35 +15,44 @@ ...@@ -14,35 +15,44 @@
* @brief xtimer_usleep test application * @brief xtimer_usleep test application
* *
* @author Francisco Acosta <francisco.acosta@inria.fr> * @author Francisco Acosta <francisco.acosta@inria.fr>
* @author Martine Lenders <m.lenders@fu-berlin.de>
* @} * @}
*/ */
#include <inttypes.h>
#include <stdio.h> #include <stdio.h>
#include "xtimer.h" #include "xtimer.h"
#include "timex.h" #include "timex.h"
#define ONE_SEC_SLEEP (1 * US_PER_SEC) #define RUNS (5U)
#define FIVE_SEC_SLEEP (5 * US_PER_SEC) #define SLEEP_TIMES_NUMOF (sizeof(sleep_times) / sizeof(sleep_times[0]))
#define TEN_SEC_SLEEP (10 * US_PER_SEC)
static const uint32_t sleep_times[] = { 10000, 50000, 100000 };
int main(void) int main(void)
{ {
int t = 9; uint32_t start_test, testtime;
printf("This test will print \"Slept for X sec...\" every 1, 5 and 10 seconds.\n"); printf("Running test %u times with %u distinct sleep times\n", RUNS,
printf("\n<======== If using pyterm, this is the time when started.\n\n"); (unsigned)SLEEP_TIMES_NUMOF);
puts("Please hit any key and then ENTER to continue");
while (t) { getchar();
xtimer_usleep(ONE_SEC_SLEEP); start_test = xtimer_now_usec();
printf("Slept for 1 sec...\n"); for (unsigned m = 0; m < RUNS; m++) {
xtimer_usleep(FIVE_SEC_SLEEP); for (unsigned n = 0;
printf("Slept for 5 sec...\n"); n < sizeof(sleep_times) / sizeof(sleep_times[0]);
xtimer_usleep(TEN_SEC_SLEEP); n++) {
printf("Slept for 10 sec...\n"); uint32_t diff, start;
t--; start = xtimer_now_usec();
xtimer_usleep(sleep_times[n]);
diff = xtimer_now_usec() - start;
printf("Slept for %" PRIu32 " us (expected: %" PRIu32 " us)\n",
diff, sleep_times[n]);
}
} }
printf("Test end.\n"); testtime = xtimer_now_usec() - start_test;
printf("Test ran for %" PRIu32 " us\n", testtime);
return 0; return 0;
} }
#!/usr/bin/env python #!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
# Copyright (C) 2017 Francisco Acosta <francisco.acosta@inria.fr> # Copyright (C) 2017 Francisco Acosta <francisco.acosta@inria.fr>
# 2017 Freie Universität Berlin
# #
# This file is subject to the terms and conditions of the GNU Lesser # 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 # General Public License v2.1. See the file LICENSE in the top level
...@@ -8,48 +11,46 @@ ...@@ -8,48 +11,46 @@
import os import os
import sys import sys
import time
sys.path.append(os.path.join(os.environ['RIOTBASE'], 'dist/tools/testrunner')) sys.path.append(os.path.join(os.environ['RIOTBASE'], 'dist/tools/testrunner'))
import testrunner import testrunner
from datetime import datetime US_PER_SEC = 1000000
INTERNAL_JITTER = 0.05
EXTERNAL_JITTER = 0.15
class InvalidTimeout(Exception): class InvalidTimeout(Exception):
pass pass
def testfunc(child): def testfunc(child):
exp_diff1 = 1000000 child.expect(u"Running test (\\d+) times with (\\d+) distinct sleep times")
exp_diff5 = 5000000 RUNS = int(child.match.group(1))
exp_diff10 = 10000000 SLEEP_TIMES_NUMOF = int(child.match.group(2))
child.expect(u"This test will print \"Slept for X sec...\" every 1, 5 and 10 seconds.\r\n") try:
child.expect(u"\r\n") child.expect_exact(u"Please hit any key and then ENTER to continue")
child.expect(u"<======== If using pyterm, this is the time when started.") child.sendline(u"a")
child.expect(u"\r\n") start_test = time.time()
m = 9 for m in range(RUNS):
while (m): for n in range(SLEEP_TIMES_NUMOF):
n = 3 child.expect(u"Slept for (\\d+) us \\(expected: (\\d+) us\\)")
while (n): sleep_time = int(child.match.group(1))
if n == 3: exp = int(child.match.group(2))
exp_diff = exp_diff1 lower_bound = exp - (exp * INTERNAL_JITTER)
if n == 2: upper_bound = exp + (exp * INTERNAL_JITTER)
exp_diff = exp_diff5 if not (lower_bound < sleep_time < upper_bound):
elif n == 1: raise InvalidTimeout("Invalid timeout %d (expected %d)" % (sleep_time, exp));
exp_diff = exp_diff10 testtime = (time.time() - start_test) * US_PER_SEC
start = datetime.now() child.expect(u"Test ran for (\\d+) us")
child.expect(u"Slept for \\d+ sec...", timeout=11) exp = int(child.match.group(1))
stop = datetime.now() lower_bound = exp - (exp * EXTERNAL_JITTER)
diff = (stop - start) upper_bound = exp + (exp * EXTERNAL_JITTER)
diff = (diff.seconds * 1000000) + diff.microseconds if not (lower_bound < testtime < upper_bound):
# fail within 5% of expected raise InvalidTimeout("Host timer measured %d us (client measured %d us)" % \
if diff > (exp_diff + (exp_diff1 * 0.05)) or \ (testtime, exp));
diff < (exp_diff - (exp_diff1 * 0.05)): except InvalidTimeout as e:
raise InvalidTimeout("Invalid timeout %d (expected %d)" % (diff, exp_diff)); print(e)
else: sys.exit(1)
print("Timed out correctly: %d (expected %d)" % (diff, exp_diff))
n = n - 1
m = m -1
child.expect(u"Test end.", timeout=15)
if __name__ == "__main__": if __name__ == "__main__":
sys.exit(testrunner.run(testfunc)) sys.exit(testrunner.run(testfunc, echo=True))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment