tests/xtimer_now32_overflow: simplify

This commit is contained in:
Kaspar Schleiser 2020-04-10 23:18:39 +02:00
parent c68475eb0d
commit 4ac5e5bd63
3 changed files with 35 additions and 98 deletions

View File

@ -1,6 +1,5 @@
include ../Makefile.tests_common include ../Makefile.tests_common
USEMODULE += fmt
USEMODULE += xtimer USEMODULE += xtimer
DISABLE_MODULE += auto_init_xtimer DISABLE_MODULE += auto_init_xtimer

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2020 Freie Universität Berlin * Copyright (C) 2020 Kaspar Schleiser <kaspar@schleiser.de>
* *
* 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
@ -7,85 +7,53 @@
*/ */
/** /**
* @ingroup tests
* @{ * @{
* *
* @file * @file
* @author Martine Lenders <m.lenders@fu-berlin.de> * @brief timer test application
*
* @author Kaspar Schleiser <kaspar@schleiser.de>
*
* @}
*/ */
#include <stdio.h> #include <stdio.h>
#include "fmt.h"
#include "kernel_defines.h"
#include "msg.h"
#include "test_utils/expect.h"
#include "thread.h"
#include "xtimer.h" #include "xtimer.h"
#define MAIN_MSG_QUEUE_SIZE (4U) static void _callback(void *arg)
#define TIMERS_NUMOF (3U) {
(void)arg;
msg_t _main_msg_queue[MAIN_MSG_QUEUE_SIZE]; }
static const uint64_t _timers_offsets[TIMERS_NUMOF] = {
/* MUST ASCEND */
1 * US_PER_SEC,
2 * US_PER_SEC,
3 * US_PER_SEC,
};
int main(void) int main(void)
{ {
xtimer_t timers[TIMERS_NUMOF]; xtimer_t t1 = { .callback=_callback };
msg_t msgs[TIMERS_NUMOF]; xtimer_t t2 = { .callback=_callback };
uint64_t start;
expect(ARRAY_SIZE(_timers_offsets) == TIMERS_NUMOF); /* ensure that xtimer_now64() is greater than UINT32_MAX
msg_init_queue(_main_msg_queue, MAIN_MSG_QUEUE_SIZE); * and the upper 32bit of xtimer_now64() equal 1 */
/* ensure that xtimer_now64() is greater than UINT32_MAX */ _xtimer_current_time = (1LLU << 32U);
_xtimer_current_time = (2LLU << 32U);
xtimer_init(); xtimer_init();
print_str("Setting ");
print_u32_dec(TIMERS_NUMOF);
print_str(" timers:\n");
for (unsigned i = 0; i < TIMERS_NUMOF; i++) {
msgs[i].content.value = i;
print_str(" #");
print_u32_dec(i);
print_str(" in ");
print_u64_dec(_timers_offsets[i]);
print_str(" usec\n");
}
print_str("now=");
start = xtimer_now64().ticks64;
print_u64_dec(start);
print_str("\n");
expect(start > UINT32_MAX);
/* set timers after all were printed for better timing */
for (unsigned i = 0; i < TIMERS_NUMOF; i++) {
xtimer_set_msg64(&timers[i], _timers_offsets[i], &msgs[i],
thread_getpid());
expect(timers[i].long_start_time > 0);
}
while (1) {
msg_t msg;
msg_receive(&msg); /* set to 100s (far in the future) */
print_str("#"); xtimer_set(&t1, 100000000LU);
print_u32_dec(msg.content.value); xtimer_set(&t2, 100000000LU);
print_str(":now=");
print_u64_dec((uint64_t)xtimer_now64().ticks64); /* sleep 1 ms (uses a third xtimer internally).
print_str("\n"); * shooting this timer did set all following short timer's to have a
for (unsigned i = 0; i <= msg.content.value; i++) { * long_start_time of 0. The timer_callback main loop would correct the
/* all fired timers expired */ * timer_list_head, but not the following.
expect(timers[i].long_start_time == 0); */
} xtimer_usleep(1000);
for (unsigned i = (msg.content.value + 1); i <= TIMERS_NUMOF; i++) {
/* upper half of remaing_timers' start_time stays above 0 as it is if (t2.long_start_time == 1) {
* based on xtimer_now64() during the timer's callback execution */ puts("[SUCCESS]");
expect(timers[i].long_start_time > 0);
}
} }
else {
puts("[FAILED]");
}
return 0; return 0;
} }
/** @} */

View File

@ -1,47 +1,17 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# Copyright (C) 2018 Freie Universität Berlin # Copyright (C) 2020 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
# directory for more details. # directory for more details.
import sys import sys
import time
from testrunner import run from testrunner import run
def _get_largest_timeout_difference(timeouts):
next_timeout = min(timeouts)
# get largest difference between all timeouts
timeout = max(a - b for a in timeouts for b in timeouts)
# check if smallest timeout (difference to 0) is the largest difference
if timeout < next_timeout:
timeout = next_timeout
return timeout
def testfunc(child): def testfunc(child):
timers = {} child.expect_exact("[SUCCESS]")
child.expect(r"Setting (\d+) timers:")
timers_numof = int(child.match.group(1))
for i in range(timers_numof):
child.expect(r" #(\d+) in (\d+) usec")
assert i == int(child.match.group(1))
timers[i] = int(child.match.group(2))
assert timers_numof == len(timers)
check_time = int(time.time())
child.expect(r"now=(\d+)")
offset = int(child.match.group(1))
# get largest possible timeout for expects below
timeout = _get_largest_timeout_difference(timers.values()) / (10**6) + 1
for i in range(timers_numof):
child.expect(r"#(\d):now=(\d+)", timeout=timeout)
t = int(child.match.group(1))
now = int(child.match.group(2))
assert (int(time.time()) - check_time) >= (timers[t] / 10**6)
expected = timers[t] + offset
assert expected <= now
if __name__ == "__main__": if __name__ == "__main__":