From 9127deb6fe65a742b61caf7383fc2593460838ee Mon Sep 17 00:00:00 2001 From: Marian Buschsieweke Date: Fri, 13 Nov 2020 20:58:38 +0100 Subject: [PATCH] tests: Add test for core_mutex/mutex_cancel --- tests/mutex_cancel/Makefile | 5 ++ tests/mutex_cancel/main.c | 79 ++++++++++++++++++++++++++++++ tests/mutex_cancel/tests/01-run.py | 20 ++++++++ 3 files changed, 104 insertions(+) create mode 100644 tests/mutex_cancel/Makefile create mode 100644 tests/mutex_cancel/main.c create mode 100755 tests/mutex_cancel/tests/01-run.py diff --git a/tests/mutex_cancel/Makefile b/tests/mutex_cancel/Makefile new file mode 100644 index 0000000000..8bff23ceea --- /dev/null +++ b/tests/mutex_cancel/Makefile @@ -0,0 +1,5 @@ +include ../Makefile.tests_common + +USEMODULE += xtimer + +include $(RIOTBASE)/Makefile.include diff --git a/tests/mutex_cancel/main.c b/tests/mutex_cancel/main.c new file mode 100644 index 0000000000..8261ea9b7f --- /dev/null +++ b/tests/mutex_cancel/main.c @@ -0,0 +1,79 @@ +/* + * 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 the mutex_cancel function in + * core_mutex + * + * @author Marian Buschsieweke + * @} + */ + +#include +#include + +#include "mutex.h" +#include "test_utils/expect.h" +#include "thread.h" +#include "xtimer.h" + +static mutex_t testlock = MUTEX_INIT; + +static void cb_unlock(void *mutex) +{ + mutex_unlock(mutex); +} + +static void cb_cancel(void *mc) +{ + mutex_cancel(mc); +} + +int main(void) +{ + xtimer_t xt; + puts( + "Test Application for mutex_cancel / mutex_lock_cancelable\n" + "=========================================================\n" + ); + + printf("%s: ", "Test without cancellation"); + mutex_cancel_t mc = mutex_cancel_init(&testlock); + expect(mutex_lock_cancelable(&mc) == 0); + puts("OK"); + + printf("%s: ", "Test early cancellation"); + mc = mutex_cancel_init(&testlock); + mutex_cancel(&mc); + expect(mutex_lock_cancelable(&mc) == -ECANCELED); + puts("OK"); + + printf("%s: ", "Verify no side effects on subsequent calls"); + mc = mutex_cancel_init(&testlock); + xt.callback = cb_unlock; + xt.arg = &testlock; + xtimer_set(&xt, US_PER_MS * 10); + expect(mutex_lock_cancelable(&mc) == 0); + puts("OK"); + + printf("%s: ", "Test late cancellation"); + mc = mutex_cancel_init(&testlock); + xt.callback = cb_cancel; + xt.arg = &mc; + xtimer_set(&xt, US_PER_MS * 10); + expect(mutex_lock_cancelable(&mc) == -ECANCELED); + puts("OK"); + + puts("TEST PASSED"); + + return 0; +} diff --git a/tests/mutex_cancel/tests/01-run.py b/tests/mutex_cancel/tests/01-run.py new file mode 100755 index 0000000000..60dd8f94f3 --- /dev/null +++ b/tests/mutex_cancel/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))