Skip to content

Commit 74dedbd

Browse files
tests: zbus: add shadow channel test suite
Add test suite to verify the shadow channel functionality when CONFIG_ZBUS_MULTIDOMAIN is enabled. Signed-off-by: Trond F. Christiansen <[email protected]>
1 parent c426b22 commit 74dedbd

File tree

4 files changed

+108
-0
lines changed

4 files changed

+108
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
cmake_minimum_required(VERSION 3.20.0)
3+
4+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
5+
project(test_shadow_channels)
6+
7+
FILE(GLOB app_sources src/main.c)
8+
target_sources(app PRIVATE ${app_sources})
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CONFIG_ZTEST=y
2+
CONFIG_ASSERT=n
3+
CONFIG_LOG=y
4+
CONFIG_ZBUS=y
5+
CONFIG_ZBUS_CHANNEL_ID=y
6+
CONFIG_ZBUS_CHANNEL_NAME=y
7+
CONFIG_ZBUS_MULTIDOMAIN=y
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright (c) 2024 Embeint Inc
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include <zephyr/zbus/zbus.h>
7+
#include <zephyr/ztest.h>
8+
#include <zephyr/ztest_assert.h>
9+
10+
struct msg {
11+
int x;
12+
};
13+
14+
enum channel_ids {
15+
CHAN_B = 123,
16+
CHAN_D = 125,
17+
};
18+
19+
/* Normal channels */
20+
ZBUS_CHAN_DEFINE(chan_a, struct msg, NULL, NULL, ZBUS_OBSERVERS_EMPTY, ZBUS_MSG_INIT(0));
21+
ZBUS_CHAN_DEFINE_WITH_ID(chan_b, CHAN_B, struct msg, NULL, NULL, ZBUS_OBSERVERS_EMPTY,
22+
ZBUS_MSG_INIT(0));
23+
24+
/* Shadow channels */
25+
ZBUS_SHADOW_CHAN_DEFINE(chan_c, struct msg, NULL, NULL, ZBUS_OBSERVERS_EMPTY, ZBUS_MSG_INIT(0));
26+
ZBUS_SHADOW_CHAN_DEFINE_WITH_ID(chan_d, CHAN_D, struct msg, NULL, NULL, ZBUS_OBSERVERS_EMPTY,
27+
ZBUS_MSG_INIT(0));
28+
29+
/* Multidomain channels */
30+
ZBUS_MULTIDOMAIN_CHAN_DEFINE(chan_e, struct msg, NULL, NULL, ZBUS_OBSERVERS_EMPTY, ZBUS_MSG_INIT(0),
31+
true, true);
32+
ZBUS_MULTIDOMAIN_CHAN_DEFINE(chan_f, struct msg, NULL, NULL, ZBUS_OBSERVERS_EMPTY, ZBUS_MSG_INIT(0),
33+
false, true);
34+
ZBUS_MULTIDOMAIN_CHAN_DEFINE(chan_g, struct msg, NULL, NULL, ZBUS_OBSERVERS_EMPTY, ZBUS_MSG_INIT(0),
35+
false, false);
36+
37+
ZTEST(shadow_channels, test_shadow_channel_identification)
38+
{
39+
/* Check shadow channel identification */
40+
zassert_false(ZBUS_CHANNEL_IS_SHADOW(&chan_a));
41+
zassert_false(ZBUS_CHANNEL_IS_SHADOW(&chan_b));
42+
43+
zassert_true(ZBUS_CHANNEL_IS_SHADOW(&chan_c));
44+
zassert_true(ZBUS_CHANNEL_IS_SHADOW(&chan_d));
45+
46+
zassert_false(ZBUS_CHANNEL_IS_SHADOW(&chan_e));
47+
zassert_true(ZBUS_CHANNEL_IS_SHADOW(&chan_f));
48+
49+
/* Check master channel identification */
50+
zassert_true(ZBUS_CHANNEL_IS_MASTER(&chan_a));
51+
zassert_true(ZBUS_CHANNEL_IS_MASTER(&chan_b));
52+
53+
zassert_false(ZBUS_CHANNEL_IS_MASTER(&chan_c));
54+
zassert_false(ZBUS_CHANNEL_IS_MASTER(&chan_d));
55+
56+
zassert_true(ZBUS_CHANNEL_IS_MASTER(&chan_e));
57+
zassert_false(ZBUS_CHANNEL_IS_MASTER(&chan_f));
58+
}
59+
60+
ZTEST(shadow_channels, test_shadow_channel_exclusion)
61+
{
62+
/* NOTE: chan_g should not be defined as _is_included in the macro is false
63+
* Therefore, zbus_chan_from_name("chan_g") should return NULL
64+
*/
65+
zassert_is_null(zbus_chan_from_name("chan_g"));
66+
}
67+
68+
ZTEST(shadow_channels, test_pub)
69+
{
70+
struct msg msg = {42};
71+
72+
/* normal publish cannot be used on shadow channels */
73+
zassert_equal(-EPERM, zbus_chan_pub(&chan_c, &msg, K_NO_WAIT));
74+
zassert_equal(-EPERM, zbus_chan_pub(&chan_d, &msg, K_NO_WAIT));
75+
zassert_equal(-EPERM, zbus_chan_pub(&chan_f, &msg, K_NO_WAIT));
76+
77+
/* shadow publish can be used on shadow channels */
78+
zassert_equal(0, zbus_chan_pub_shadow(&chan_c, &msg, K_NO_WAIT));
79+
zassert_equal(0, zbus_chan_pub_shadow(&chan_d, &msg, K_NO_WAIT));
80+
zassert_equal(0, zbus_chan_pub_shadow(&chan_f, &msg, K_NO_WAIT));
81+
82+
/* shadow publish cannot be used on normal channels */
83+
zassert_equal(-EPERM, zbus_chan_pub_shadow(&chan_a, NULL, K_NO_WAIT));
84+
zassert_equal(-EPERM, zbus_chan_pub_shadow(&chan_b, NULL, K_NO_WAIT));
85+
zassert_equal(-EPERM, zbus_chan_pub_shadow(&chan_e, NULL, K_NO_WAIT));
86+
}
87+
88+
ZTEST_SUITE(shadow_channels, NULL, NULL, NULL, NULL, NULL);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
tests:
2+
message_bus.zbus.shadow_channels:
3+
tags: zbus
4+
integration_platforms:
5+
- native_sim

0 commit comments

Comments
 (0)