diff --git a/tests/posix_time/Makefile b/tests/posix_time/Makefile index c32cb1848919c91b6cf586722f89d0a6bf51a1cf..a988a4fbbeadaf24ca4542febceaaada5faff7bb 100644 --- a/tests/posix_time/Makefile +++ b/tests/posix_time/Makefile @@ -4,3 +4,6 @@ include ../Makefile.tests_common USEMODULE += posix_time include $(RIOTBASE)/Makefile.include + +test: + ./tests/01-run.py diff --git a/tests/posix_time/README.md b/tests/posix_time/README.md new file mode 100644 index 0000000000000000000000000000000000000000..e8d161db4aea463f4495a8545d5fffa07836c242 --- /dev/null +++ b/tests/posix_time/README.md @@ -0,0 +1,10 @@ +# posix_time test application + +This test tests POSIX' `sleep()` and `usleep()`. +The test script also checks the sanity of the timings by comparing the overall +run time of the test with the host's time with an expected jitter of 15%. + +## Usage +``` +make flash test +``` diff --git a/tests/posix_time/main.c b/tests/posix_time/main.c index 02c153766cbc44543c898806b1d7bce2d7d42c34..5c94cbefb528fa129cf161d9db237d207031520f 100644 --- a/tests/posix_time/main.c +++ b/tests/posix_time/main.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2014 Freie Universität Berlin + * Copyright (C) 2014-17 Freie Universität Berlin * * 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 +14,7 @@ * @brief Posix sleep test application * * @author Christian Mehlis <mehlis@inf.fu-berlin.de> + * @author Martine Lenders <m.lenders@fu-berlin.de> * * @} */ @@ -23,22 +24,21 @@ int main(void) { - puts("usleep 1 x 1000*1000"); - for (int i = 0; i < 10; i++) { - useconds_t us = i*1000u*1000u; - printf("calling usleep(%u)\n", (unsigned int) us); + puts("Please hit any key and then ENTER to continue"); + getchar(); + puts("5 x usleep(i++ * 500000)"); + for (unsigned i = 0; i < 5; i++) { + useconds_t us = i * 500000u; usleep(us); puts("wake up"); } - puts("sleep 1"); - for (int i = 0; i < 10; i++) { - unsigned int s = i; - printf("calling sleep(%u)\n", s); - sleep(s); + puts("5 x sleep(i++)"); + for (unsigned i = 0; i < 5; i++) { + sleep(i); puts("wake up"); } - puts("done"); + puts("DONE"); return 0; } diff --git a/tests/posix_time/tests/01-run.py b/tests/posix_time/tests/01-run.py new file mode 100755 index 0000000000000000000000000000000000000000..bff773c37db88b0bb8dd3e0185eec300f0332e49 --- /dev/null +++ b/tests/posix_time/tests/01-run.py @@ -0,0 +1,49 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# vim:fenc=utf-8 + +# Copyright (C) 2017 Freie Universität Berlin +# +# 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 +# directory for more details. + +import os +import sys +import time + +sys.path.append(os.path.join(os.environ['RIOTBASE'], 'dist/tools/testrunner')) +import testrunner + +US_PER_SEC = 1000000 +EXTERNAL_JITTER = 0.15 + +class InvalidTimeout(Exception): + pass + +def testfunc(child): + try: + child.expect_exact("Please hit any key and then ENTER to continue") + child.sendline("a") + start_test = time.time() + child.expect_exact("5 x usleep(i++ * 500000)") + for i in range(5): + child.expect_exact("wake up") + child.expect_exact("5 x sleep(i++)") + for i in range(5): + child.expect_exact("wake up") + child.expect_exact("DONE") + testtime = (time.time() - start_test) * US_PER_SEC + exp = sum(i * 500000 for i in range(5)) + \ + sum(i * US_PER_SEC for i in range(5)) + lower_bound = exp - (exp * EXTERNAL_JITTER) + upper_bound = exp + (exp * EXTERNAL_JITTER) + if not (lower_bound < testtime < upper_bound): + raise InvalidTimeout("Host timer measured %d us (client measured %d us)" % \ + (testtime, exp)); + except InvalidTimeout as e: + print(e) + sys.exit(1) + +if __name__ == "__main__": + sys.exit(testrunner.run(testfunc, echo=True))