Skip to content

Commit

Permalink
tests/evtimer_msg: modify test to delete entries
Browse files Browse the repository at this point in the history
The evtimer_msg test is expanded to also delete entries.

Furthermore the messages that are printed should now show
numbers that are very close (if not equal). Something like
this:
At    740 ms received msg 0: "#2 supposed to be 740"
At   1081 ms received msg 1: "#0 supposed to be 1081"
At   1581 ms received msg 2: "#1 supposed to be 1581"
At   4035 ms received msg 3: "#3 supposed to be 4035"

The function evtimer_print is also called to show the
intermediate status of evtimer entries.
  • Loading branch information
keestux committed Jul 17, 2019
1 parent d2cd666 commit ea11de5
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 15 deletions.
60 changes: 51 additions & 9 deletions tests/evtimer_msg/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,20 @@

static char worker_stack[THREAD_STACKSIZE_MAIN];
static evtimer_t evtimer;
static evtimer_msg_event_t events[] = {
{ .event = { .offset = 1000 }, .msg = { .content = { .ptr = "supposed to be 1000" } } },
{ .event = { .offset = 1500 }, .msg = { .content = { .ptr = "supposed to be 1500" } } },
{ .event = { .offset = 659 }, .msg = { .content = { .ptr = "supposed to be 659" } } },
{ .event = { .offset = 3954 }, .msg = { .content = { .ptr = "supposed to be 3954" } } },
#define NEVENTS (unsigned)(4)
/*
* The events (.offset) are modified by evtimer_add.
* This list of offsets is used to populate the events
* before adding (or re-adding).
*/
static uint32_t offsets[NEVENTS] = {
1000,
1500,
659,
3954,
};

#define NEVENTS ((unsigned)(sizeof(events) / sizeof(evtimer_msg_event_t)))
static evtimer_msg_event_t events[NEVENTS];
static char texts[NEVENTS][40];

/* This thread will print the drift to stdout once per second */
void *worker_thread(void *arg)
Expand All @@ -56,7 +62,7 @@ void *worker_thread(void *arg)

int main(void)
{
uint32_t now = xtimer_now_usec() / US_PER_MS;
uint32_t now;

evtimer_init_msg(&evtimer);

Expand All @@ -65,11 +71,47 @@ int main(void)
THREAD_PRIORITY_MAIN - 1,
THREAD_CREATE_STACKTEST,
worker_thread, NULL, "worker");
printf("Testing generic evtimer (start time = %" PRIu32 " ms)\n", now);

printf("Testing generic evtimer\n");

/* Add all the events */
for (unsigned i = 0; i < NEVENTS; i++) {
events[i].event.offset = offsets[i];
now = xtimer_now_usec() / US_PER_MS;
snprintf(texts[i], sizeof(texts[i]) - 1, "#%u supposed to be %" PRIu32, i, now + events[i].event.offset);
events[i].msg.content.ptr = texts[i];
evtimer_add_msg(&evtimer, &events[i], pid);
}

/* Delete all the events */
/* First we delete the last, to test deleting the last */
/* Then we delete the first, to test deleting the first */
evtimer_del(&evtimer, &events[3].event);
evtimer_del(&evtimer, &events[2].event);
printf("This should list %u items\n", NEVENTS - 2);
evtimer_print(&evtimer);

/* Delete the remaining entries */
for (unsigned i = 0; i < NEVENTS; i++) {
evtimer_del(&evtimer, &events[i].event);
}

/* Add all the events, again */
for (unsigned i = 0; i < NEVENTS; i++) {
events[i].event.offset = offsets[i];
now = xtimer_now_usec() / US_PER_MS;
snprintf(texts[i], sizeof(texts[i]) - 1, "#%u supposed to be %" PRIu32, i, now + events[i].event.offset);
events[i].msg.content.ptr = texts[i];
evtimer_add_msg(&evtimer, &events[i], pid);
}
printf("This should list %u items\n", NEVENTS);
evtimer_print(&evtimer);

printf("Are the reception times of all %u msgs close to the supposed values?\n",
NEVENTS);

/* The last offset is the largest, wait for it and a tiny bit more */
xtimer_usleep((offsets[3] + 10) * US_PER_MS);
puts("By now all msgs should have been received");
puts("If yes, the tests were successful");
}
12 changes: 6 additions & 6 deletions tests/evtimer_msg/tests/01-run.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@
import sys
from testrunner import run

ACCEPTED_ERROR = 20
ACCEPTED_ERROR = 2


def testfunc(child):
child.expect(r"Testing generic evtimer \(start time = (\d+) ms\)")
timer_offset = int(child.match.group(1))
child.expect(r"Testing generic evtimer")
child.expect(r"Are the reception times of all (\d+) msgs close to the supposed values?")
numof = int(child.match.group(1))

for i in range(numof):
child.expect(r'At \s*(\d+) ms received msg %i: "supposed to be (\d+)"' % i)
child.expect(r'At \s*(\d+) ms received msg %i: "#\d+ supposed to be (\d+)"' % i)
# check if output is correct
exp = int(child.match.group(2)) + timer_offset
assert(int(child.match.group(1)) in range(exp - ACCEPTED_ERROR, exp + ACCEPTED_ERROR + 1))
expected = int(child.match.group(2))
actual = int(child.match.group(1))
assert(actual in range(expected - ACCEPTED_ERROR, expected + ACCEPTED_ERROR))
print(".", end="", flush=True)
print("")
print("All tests successful")
Expand Down

0 comments on commit ea11de5

Please sign in to comment.