From aa4532a2e7e9b1522c9d4bd78e0f822b58de1006 Mon Sep 17 00:00:00 2001 From: Marian Buschsieweke Date: Tue, 8 Dec 2020 22:54:12 +0100 Subject: [PATCH] tests/ztimer_mutex_lock_timeout: new test --- tests/ztimer_mutex_lock_timeout/Makefile | 5 + tests/ztimer_mutex_lock_timeout/main.c | 95 +++++++++++++++++++ .../ztimer_mutex_lock_timeout/tests/01-run.py | 20 ++++ 3 files changed, 120 insertions(+) create mode 100644 tests/ztimer_mutex_lock_timeout/Makefile create mode 100644 tests/ztimer_mutex_lock_timeout/main.c create mode 100755 tests/ztimer_mutex_lock_timeout/tests/01-run.py diff --git a/tests/ztimer_mutex_lock_timeout/Makefile b/tests/ztimer_mutex_lock_timeout/Makefile new file mode 100644 index 0000000000..9b50442b8c --- /dev/null +++ b/tests/ztimer_mutex_lock_timeout/Makefile @@ -0,0 +1,5 @@ +include ../Makefile.tests_common + +USEMODULE += ztimer_usec + +include $(RIOTBASE)/Makefile.include diff --git a/tests/ztimer_mutex_lock_timeout/main.c b/tests/ztimer_mutex_lock_timeout/main.c new file mode 100644 index 0000000000..2c59815b0e --- /dev/null +++ b/tests/ztimer_mutex_lock_timeout/main.c @@ -0,0 +1,95 @@ +/* + * Copyright (C) 2020 Otto-von-Guericke-Universität Magdeburg + * + * 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. + */ + +/** + * @ingroup tests + * @{ + * + * @file + * @brief Test application for testing ztimer_mutex_lock_timeout_timeout + * + * @author Marian Buschsieweke + * @} + */ + +#include +#include + +#include "mutex.h" +#include "test_utils/expect.h" +#include "timex.h" +#include "ztimer.h" + +#ifndef TEST_CLOCK +#define TEST_CLOCK ZTIMER_USEC +#endif + +#ifndef TIMEOUT_SMALL +#define TIMEOUT_SMALL (100U) +#endif + +#ifndef TIMEOUT_LARGE +#define TIMEOUT_LARGE (US_PER_SEC / 2) +#endif + +static mutex_t testlock = MUTEX_INIT; + +int main(void) +{ + ztimer_now_t pre, post; + puts( + "Test Application for ztimer_mutex_lock_timeout_timeout()\n" + "================================================\n" + ); + + printf("%s: ", "Test on unlocked mutex with zero timeout"); + expect(ztimer_mutex_lock_timeout(TEST_CLOCK, &testlock, 0) == 0); + puts("OK"); + + printf("%s: ", "Test on unlocked mutex with small timeout"); + mutex_unlock(&testlock); + expect(ztimer_mutex_lock_timeout(TEST_CLOCK, &testlock, TIMEOUT_SMALL) == 0); + puts("OK"); + + printf("%s: ", "Test on unlocked mutex with large timeout"); + mutex_unlock(&testlock); + pre = ztimer_now(TEST_CLOCK); + expect(ztimer_mutex_lock_timeout(TEST_CLOCK, &testlock, US_PER_SEC / 2) == 0); + post = ztimer_now(TEST_CLOCK); + /* Call shouldn't block on unlocked mutex. So the duration spent on the + * function call should be short. Let's take a very generous definition of + * "short duration" here to not get false failures on slow boards */ + expect(post - pre < TIMEOUT_LARGE); + puts("OK"); + + printf("%s: ", "Test on locked mutex with zero timeout"); + pre = ztimer_now(TEST_CLOCK); + expect(ztimer_mutex_lock_timeout(TEST_CLOCK, &testlock, 0) == -ECANCELED); + post = ztimer_now(TEST_CLOCK); + expect(post - pre < TIMEOUT_LARGE); + puts("OK"); + + printf("%s: ", "Test on locked mutex with small timeout"); + pre = ztimer_now(TEST_CLOCK); + expect(ztimer_mutex_lock_timeout(TEST_CLOCK, &testlock, TIMEOUT_SMALL) == -ECANCELED); + post = ztimer_now(TEST_CLOCK); + expect((post - pre > TIMEOUT_SMALL) && (post - pre < TIMEOUT_LARGE)); + puts("OK"); + + printf("%s: ", "Test on locked mutex with large timeout"); + pre = ztimer_now(TEST_CLOCK); + expect(ztimer_mutex_lock_timeout(TEST_CLOCK, &testlock, TIMEOUT_LARGE) == -ECANCELED); + post = ztimer_now(TEST_CLOCK); + expect(post - pre > TIMEOUT_LARGE); + puts("OK"); + + + puts("TEST PASSED"); + + return 0; +} diff --git a/tests/ztimer_mutex_lock_timeout/tests/01-run.py b/tests/ztimer_mutex_lock_timeout/tests/01-run.py new file mode 100755 index 0000000000..60dd8f94f3 --- /dev/null +++ b/tests/ztimer_mutex_lock_timeout/tests/01-run.py @@ -0,0 +1,20 @@ +#!/usr/bin/env python3 + +# Copyright (C) 2020 Otto-von-Guericke-Universität Magdeburg +# +# 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. + +# @author Marian Buschsieweke + +import sys +from testrunner import run + + +def testfunc(child): + child.expect("TEST PASSED") + + +if __name__ == "__main__": + sys.exit(run(testfunc))