mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2025-12-31 09:21:19 +01:00
tests/thread_msg_bus: add test application for message bus
This commit is contained in:
parent
b3eb59c30e
commit
950c18db42
5
tests/thread_msg_bus/Makefile
Normal file
5
tests/thread_msg_bus/Makefile
Normal file
@ -0,0 +1,5 @@
|
||||
include ../Makefile.tests_common
|
||||
|
||||
USEMODULE += core_msg_bus
|
||||
|
||||
include $(RIOTBASE)/Makefile.include
|
||||
10
tests/thread_msg_bus/Makefile.ci
Normal file
10
tests/thread_msg_bus/Makefile.ci
Normal file
@ -0,0 +1,10 @@
|
||||
BOARD_INSUFFICIENT_MEMORY := \
|
||||
arduino-duemilanove \
|
||||
arduino-leonardo \
|
||||
arduino-nano \
|
||||
arduino-uno \
|
||||
atmega328p \
|
||||
nucleo-f031k6 \
|
||||
nucleo-f042k6 \
|
||||
stm32f030f4-demo \
|
||||
#
|
||||
129
tests/thread_msg_bus/main.c
Normal file
129
tests/thread_msg_bus/main.c
Normal file
@ -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 <benjamin.valentin@ml-pa.com>
|
||||
*
|
||||
* @}
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
#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;
|
||||
}
|
||||
26
tests/thread_msg_bus/tests/01-run.py
Executable file
26
tests/thread_msg_bus/tests/01-run.py
Executable file
@ -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))
|
||||
Loading…
x
Reference in New Issue
Block a user