tests/ztimer_mutex_lock_timeout: new test

This commit is contained in:
Marian Buschsieweke 2020-12-08 22:54:12 +01:00
parent d48fac278b
commit aa4532a2e7
No known key found for this signature in database
GPG Key ID: 61F64C6599B1539F
3 changed files with 120 additions and 0 deletions

View File

@ -0,0 +1,5 @@
include ../Makefile.tests_common
USEMODULE += ztimer_usec
include $(RIOTBASE)/Makefile.include

View File

@ -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 <marian.buschsieweke@ovgu.de>
* @}
*/
#include <errno.h>
#include <stdio.h>
#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;
}

View File

@ -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 <marian.buschsieweke@ovgu.de>
import sys
from testrunner import run
def testfunc(child):
child.expect("TEST PASSED")
if __name__ == "__main__":
sys.exit(run(testfunc))