From 950c18db4299acc97b3975c4e07938313cea87fa Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Wed, 22 Apr 2020 23:31:13 +0200 Subject: [PATCH] tests/thread_msg_bus: add test application for message bus --- tests/thread_msg_bus/Makefile | 5 ++ tests/thread_msg_bus/Makefile.ci | 10 +++ tests/thread_msg_bus/main.c | 129 +++++++++++++++++++++++++++ tests/thread_msg_bus/tests/01-run.py | 26 ++++++ 4 files changed, 170 insertions(+) create mode 100644 tests/thread_msg_bus/Makefile create mode 100644 tests/thread_msg_bus/Makefile.ci create mode 100644 tests/thread_msg_bus/main.c create mode 100755 tests/thread_msg_bus/tests/01-run.py diff --git a/tests/thread_msg_bus/Makefile b/tests/thread_msg_bus/Makefile new file mode 100644 index 0000000000..aa0b972871 --- /dev/null +++ b/tests/thread_msg_bus/Makefile @@ -0,0 +1,5 @@ +include ../Makefile.tests_common + +USEMODULE += core_msg_bus + +include $(RIOTBASE)/Makefile.include diff --git a/tests/thread_msg_bus/Makefile.ci b/tests/thread_msg_bus/Makefile.ci new file mode 100644 index 0000000000..73c8498472 --- /dev/null +++ b/tests/thread_msg_bus/Makefile.ci @@ -0,0 +1,10 @@ +BOARD_INSUFFICIENT_MEMORY := \ + arduino-duemilanove \ + arduino-leonardo \ + arduino-nano \ + arduino-uno \ + atmega328p \ + nucleo-f031k6 \ + nucleo-f042k6 \ + stm32f030f4-demo \ + # diff --git a/tests/thread_msg_bus/main.c b/tests/thread_msg_bus/main.c new file mode 100644 index 0000000000..35351bfb2e --- /dev/null +++ b/tests/thread_msg_bus/main.c @@ -0,0 +1,129 @@ +/* + * Copyright (C) 2020 ML!PA Consulting GmbH + * + * 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 Message bus test application + * + * @author Benjamin Valentin + * + * @} + */ + +#include +#include + +#include "thread.h" +#include "msg.h" +#include "msg_bus.h" + +char t1_stack[THREAD_STACKSIZE_MAIN]; +char t2_stack[THREAD_STACKSIZE_MAIN]; +char t3_stack[THREAD_STACKSIZE_MAIN]; + +kernel_pid_t p_main, p1, p2, p3; + +void *thread1(void *arg) +{ + msg_t msg; + msg_bus_entry_t sub; + + puts("THREAD 1 start"); + + msg_bus_attach(arg, &sub); + msg_bus_subscribe(&sub, 23); + msg_bus_subscribe(&sub, 24); + + msg_receive(&msg); + + /* check if the message came from the right bus */ + assert(msg_is_from_bus(arg, &msg)); + + printf("T1 recv: %s (type=%d)\n", + (char*) msg.content.ptr, msg_bus_get_type(&msg)); + + msg_bus_detach(arg, &sub); + + return NULL; +} + +void *thread2(void *arg) +{ + msg_t msg; + msg_bus_entry_t sub; + + puts("THREAD 2 start"); + + msg_bus_attach(arg, &sub); + msg_bus_subscribe(&sub, 24); + + msg_receive(&msg); + + /* check if the message came from the right bus */ + assert(msg_is_from_bus(arg, &msg)); + + printf("T2 recv: %s (type=%d)\n", + (char*) msg.content.ptr, msg_bus_get_type(&msg)); + + msg_bus_detach(arg, &sub); + + return NULL; +} + +void *thread3(void *arg) +{ + msg_t msg; + msg_bus_entry_t sub; + + puts("THREAD 3 start"); + + msg_bus_attach(arg, &sub); + msg_bus_subscribe(&sub, 23); + + msg_receive(&msg); + + /* check if the message came from the right bus */ + assert(msg_is_from_bus(arg, &msg)); + + printf("T3 recv: %s (type=%d)\n", + (char*) msg.content.ptr, msg_bus_get_type(&msg)); + + msg_bus_detach(arg, &sub); + + return NULL; +} + +int main(void) +{ + msg_bus_t my_bus; + + msg_bus_init(&my_bus); + + p_main = sched_active_pid; + p1 = thread_create(t1_stack, sizeof(t1_stack), THREAD_PRIORITY_MAIN - 3, + THREAD_CREATE_STACKTEST, thread1, &my_bus, "nr1"); + p2 = thread_create(t2_stack, sizeof(t2_stack), THREAD_PRIORITY_MAIN - 2, + THREAD_CREATE_STACKTEST, thread2, &my_bus, "nr2"); + p3 = thread_create(t3_stack, sizeof(t3_stack), THREAD_PRIORITY_MAIN - 1, + THREAD_CREATE_STACKTEST, thread3, &my_bus, "nr3"); + puts("THREADS CREATED"); + + const char hello[] = "Hello Threads!"; + + for (int id = 22; id < 25; ++id) { + int woken = msg_bus_post(&my_bus, id, (void*)hello); + printf("Posted event %d to %d threads\n", id, woken); + } + + puts("SUCCESS"); + + return 0; +} diff --git a/tests/thread_msg_bus/tests/01-run.py b/tests/thread_msg_bus/tests/01-run.py new file mode 100755 index 0000000000..3193ac9a34 --- /dev/null +++ b/tests/thread_msg_bus/tests/01-run.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python3 + +import sys +from testrunner import run + + +def testfunc(child): + child.expect_exact('THREAD 1 start') + child.expect_exact('THREAD 2 start') + child.expect_exact('THREAD 3 start') + child.expect_exact('THREADS CREATED') + + child.expect_exact('Posted event 22 to 0 threads') + + child.expect_exact('T1 recv: Hello Threads! (type=23)') + child.expect_exact('T3 recv: Hello Threads! (type=23)') + child.expect_exact('Posted event 23 to 2 threads') + + child.expect_exact('T2 recv: Hello Threads! (type=24)') + child.expect_exact('Posted event 24 to 1 threads') + + child.expect_exact('SUCCESS') + + +if __name__ == "__main__": + sys.exit(run(testfunc))