Skip to content

Commit 6d0760a

Browse files
rodrigopexkartben
authored andcommitted
tests: zbus: Add separate tests for ALLOC NONE
Add tests for ALLOC_NONE configuration. Signed-off-by: Rodrigo Peixoto <rodrigopex@gmail.com>
1 parent f6f2b52 commit 6d0760a

File tree

7 files changed

+273
-29
lines changed

7 files changed

+273
-29
lines changed

tests/subsys/zbus/runtime_observers_registration/prj.conf

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,4 @@ CONFIG_ZBUS=y
44
CONFIG_ZBUS_ASSERT_MOCK=y
55
CONFIG_ZBUS_LOG_LEVEL_DBG=y
66
CONFIG_ZBUS_RUNTIME_OBSERVERS=y
7-
CONFIG_HEAP_MEM_POOL_SIZE=2048
87
CONFIG_ZBUS_OBSERVER_NAME=y

tests/subsys/zbus/runtime_observers_registration/src/main.c

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,29 @@ struct sensor_data_msg {
1414
int b;
1515
};
1616

17-
ZBUS_CHAN_DEFINE(chan1, /* Name */
17+
ZBUS_CHAN_DEFINE(chan1, /* Name */
1818
struct sensor_data_msg, /* Message type */
1919

20-
NULL, /* Validator */
21-
NULL, /* User data */
20+
NULL, /* Validator */
21+
NULL, /* User data */
2222
ZBUS_OBSERVERS_EMPTY, /* observers */
2323
ZBUS_MSG_INIT(0) /* Initial value major 0, minor 1, build 1023 */
2424
);
2525

26-
ZBUS_CHAN_DEFINE(chan2, /* Name */
26+
ZBUS_CHAN_DEFINE(chan2, /* Name */
2727
struct sensor_data_msg, /* Message type */
2828

29-
NULL, /* Validator */
30-
NULL, /* User data */
29+
NULL, /* Validator */
30+
NULL, /* User data */
3131
ZBUS_OBSERVERS(lis2), /* observers */
3232
ZBUS_MSG_INIT(0) /* Initial value major 0, minor 1, build 1023 */
3333
);
3434

35-
ZBUS_CHAN_DEFINE(chan3, /* Name */
35+
ZBUS_CHAN_DEFINE(chan3, /* Name */
3636
struct sensor_data_msg, /* Message type */
3737

38-
NULL, /* Validator */
39-
NULL, /* User data */
38+
NULL, /* Validator */
39+
NULL, /* User data */
4040
ZBUS_OBSERVERS_EMPTY, /* observers */
4141
ZBUS_MSG_INIT(0) /* Initial value major 0, minor 1, build 1023 */
4242
);
@@ -69,16 +69,15 @@ ZTEST(basic, test_specification_based__zbus_obs_add_rm_obs)
6969
{
7070
count_callback1 = 0;
7171
struct sensor_data_msg sd = {.a = 10, .b = 100};
72-
static struct zbus_observer_node n1, n2, n3, n4, n5, n6;
7372

7473
/* Tyring to add same static observer as one dynamic */
75-
zassert_equal(-EEXIST, zbus_chan_add_obs(&chan2, &lis2, &n2, K_MSEC(200)), NULL);
74+
zassert_equal(-EEXIST, zbus_chan_add_obs(&chan2, &lis2, K_MSEC(200)), NULL);
7675

7776
zassert_equal(0, zbus_chan_pub(&chan1, &sd, K_MSEC(500)), NULL);
7877
zassert_equal(count_callback1, 0, "The counter could not be more than zero, no obs");
7978

80-
zassert_equal(0, zbus_chan_add_obs(&chan1, &lis1, &n1, K_MSEC(200)), NULL);
81-
zassert_equal(-EALREADY, zbus_chan_add_obs(&chan1, &lis1, &n1, K_MSEC(200)),
79+
zassert_equal(0, zbus_chan_add_obs(&chan1, &lis1, K_MSEC(200)), NULL);
80+
zassert_equal(-EALREADY, zbus_chan_add_obs(&chan1, &lis1, K_MSEC(200)),
8281
"It cannot be added twice");
8382

8483
zassert_equal(0, zbus_chan_pub(&chan1, &sd, K_MSEC(500)), NULL);
@@ -99,21 +98,32 @@ ZTEST(basic, test_specification_based__zbus_obs_add_rm_obs)
9998
zassert_equal(0, zbus_chan_pub(&chan2, &sd, K_MSEC(500)), NULL);
10099
zassert_equal(count_callback2, 1, "The counter could not be more than zero, no obs");
101100

102-
zassert_equal(0, zbus_chan_add_obs(&chan2, &lis3, &n3, K_MSEC(200)), NULL);
101+
zassert_equal(0, zbus_chan_add_obs(&chan2, &lis3, K_MSEC(200)), NULL);
103102

104-
zassert_equal(-EALREADY, zbus_chan_add_obs(&chan2, &lis3, &n3, K_MSEC(200)),
103+
zassert_equal(-EALREADY, zbus_chan_add_obs(&chan2, &lis3, K_MSEC(200)),
105104
"It cannot be added twice");
106105

107106
zassert_equal(0, zbus_chan_pub(&chan2, &sd, K_MSEC(500)), NULL);
108107
zassert_equal(count_callback2, 3, "The counter could not be more than zero, no obs, %d",
109108
count_callback2);
110109
count_callback2 = 0;
111-
zassert_equal(0, zbus_chan_add_obs(&chan2, &sub1, &n1, K_MSEC(200)), NULL);
112-
zassert_equal(0, zbus_chan_add_obs(&chan2, &sub2, &n2, K_MSEC(200)), NULL);
113-
zassert_equal(0, zbus_chan_add_obs(&chan2, &lis4, &n4, K_MSEC(200)), "It must add the obs");
114-
zassert_equal(0, zbus_chan_add_obs(&chan2, &lis5, &n5, K_MSEC(200)), "It must add the obs");
115-
zassert_equal(0, zbus_chan_add_obs(&chan2, &lis6, &n6, K_MSEC(200)), "It must add the obs");
116-
110+
zassert_equal(0, zbus_chan_add_obs(&chan2, &sub1, K_MSEC(200)), NULL);
111+
zassert_equal(0, zbus_chan_add_obs(&chan2, &sub2, K_MSEC(200)), NULL);
112+
zassert_equal(0, zbus_chan_add_obs(&chan2, &lis4, K_MSEC(200)), "It must add the obs");
113+
zassert_equal(0, zbus_chan_add_obs(&chan2, &lis5, K_MSEC(200)), "It must add the obs");
114+
zassert_equal(0, zbus_chan_add_obs(&chan2, &lis6, K_MSEC(200)), "It must add the obs");
115+
116+
#if defined(CONFIG_ZBUS_RUNTIME_OBSERVERS_NODE_ALLOC_DYNAMIC)
117+
/* Make the heap full */
118+
void *mem;
119+
120+
do {
121+
mem = k_malloc(1);
122+
} while (mem != NULL);
123+
124+
/* With the heap full it will not be possible to add another obs */
125+
#endif
126+
zassert_equal(-EFAULT, zbus_chan_add_obs(&chan2, &lis7, K_MSEC(200)), NULL);
117127
zassert_equal(0, zbus_chan_pub(&chan2, &sd, K_MSEC(500)), NULL);
118128
zassert_equal(count_callback2, 5, NULL);
119129

@@ -134,9 +144,7 @@ static struct aux2_wq_data wq_handler;
134144

135145
static void wq_dh_cb(struct k_work *item)
136146
{
137-
static struct zbus_observer_node node;
138-
139-
zassert_equal(-EAGAIN, zbus_chan_add_obs(&chan2, &sub1, &node, K_MSEC(200)), NULL);
147+
zassert_equal(-EAGAIN, zbus_chan_add_obs(&chan2, &sub1, K_MSEC(200)), NULL);
140148
zassert_equal(-EAGAIN, zbus_chan_rm_obs(&chan2, &sub2, K_MSEC(200)), NULL);
141149
}
142150

@@ -186,12 +194,11 @@ ZBUS_CHAN_ADD_OBS(chan4, prio_lis4, 2);
186194
ZTEST(basic, test_specification_based__zbus_obs_priority)
187195
{
188196
struct sensor_data_msg sd = {.a = 70, .b = 116};
189-
static struct zbus_observer_node n1, n2;
190197

191198
execution_sequence_idx = 0;
192199

193-
zassert_equal(0, zbus_chan_add_obs(&chan4, &prio_lis2, &n1, K_MSEC(200)), NULL);
194-
zassert_equal(0, zbus_chan_add_obs(&chan4, &prio_lis1, &n2, K_MSEC(200)), NULL);
200+
zassert_equal(0, zbus_chan_add_obs(&chan4, &prio_lis2, K_MSEC(200)), NULL);
201+
zassert_equal(0, zbus_chan_add_obs(&chan4, &prio_lis1, K_MSEC(200)), NULL);
195202

196203
zassert_equal(0, zbus_chan_pub(&chan4, &sd, K_MSEC(500)), NULL);
197204

tests/subsys/zbus/runtime_observers_registration/testcase.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
tests:
2-
message_bus.zbus.runtime_obs_reg.add_and_remove_observers:
2+
message_bus.zbus.runtime_obs_reg.add_and_remove_observers_dynamic_memory:
33
tags: zbus
44
integration_platforms:
55
- qemu_x86
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_runtime_observers_registration)
6+
7+
FILE(GLOB app_sources src/*.c)
8+
target_sources(app PRIVATE ${app_sources})
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CONFIG_ZTEST=y
2+
CONFIG_LOG=y
3+
CONFIG_ZBUS=y
4+
CONFIG_ZBUS_ASSERT_MOCK=y
5+
CONFIG_ZBUS_LOG_LEVEL_DBG=y
6+
CONFIG_ZBUS_RUNTIME_OBSERVERS=y
7+
CONFIG_ZBUS_OBSERVER_NAME=y
8+
CONFIG_ZBUS_RUNTIME_OBSERVERS_NODE_ALLOC_NONE=y
Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
/*
2+
* Copyright (c) 2022 Rodrigo Peixoto <rodrigopex@gmail.com>
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include <zephyr/kernel.h>
7+
#include <zephyr/logging/log.h>
8+
#include <zephyr/zbus/zbus.h>
9+
#include <zephyr/ztest.h>
10+
LOG_MODULE_DECLARE(zbus, CONFIG_ZBUS_LOG_LEVEL);
11+
12+
struct sensor_data_msg {
13+
int a;
14+
int b;
15+
};
16+
17+
ZBUS_CHAN_DEFINE(chan1, /* Name */
18+
struct sensor_data_msg, /* Message type */
19+
20+
NULL, /* Validator */
21+
NULL, /* User data */
22+
ZBUS_OBSERVERS_EMPTY, /* observers */
23+
ZBUS_MSG_INIT(0) /* Initial value major 0, minor 1, build 1023 */
24+
);
25+
26+
ZBUS_CHAN_DEFINE(chan2, /* Name */
27+
struct sensor_data_msg, /* Message type */
28+
29+
NULL, /* Validator */
30+
NULL, /* User data */
31+
ZBUS_OBSERVERS(lis2), /* observers */
32+
ZBUS_MSG_INIT(0) /* Initial value major 0, minor 1, build 1023 */
33+
);
34+
35+
ZBUS_CHAN_DEFINE(chan3, /* Name */
36+
struct sensor_data_msg, /* Message type */
37+
38+
NULL, /* Validator */
39+
NULL, /* User data */
40+
ZBUS_OBSERVERS_EMPTY, /* observers */
41+
ZBUS_MSG_INIT(0) /* Initial value major 0, minor 1, build 1023 */
42+
);
43+
44+
ZBUS_SUBSCRIBER_DEFINE(sub1, 1);
45+
ZBUS_SUBSCRIBER_DEFINE(sub2, 1);
46+
47+
static int count_callback1;
48+
static void callback1(const struct zbus_channel *chan)
49+
{
50+
++count_callback1;
51+
}
52+
53+
ZBUS_LISTENER_DEFINE(lis1, callback1);
54+
55+
static int count_callback2;
56+
static void callback2(const struct zbus_channel *chan)
57+
{
58+
++count_callback2;
59+
}
60+
61+
ZBUS_LISTENER_DEFINE(lis2, callback2);
62+
ZBUS_LISTENER_DEFINE(lis3, callback2);
63+
ZBUS_LISTENER_DEFINE(lis4, callback2);
64+
ZBUS_LISTENER_DEFINE(lis5, callback2);
65+
ZBUS_LISTENER_DEFINE(lis6, callback2);
66+
ZBUS_LISTENER_DEFINE(lis7, callback2);
67+
68+
ZTEST(basic, test_specification_based__zbus_obs_add_rm_obs)
69+
{
70+
count_callback1 = 0;
71+
struct sensor_data_msg sd = {.a = 10, .b = 100};
72+
static struct zbus_observer_node n1, n2, n3, n4, n5, n6;
73+
74+
/* Tyring to add same static observer as one dynamic */
75+
zassert_equal(-EEXIST, zbus_chan_add_obs_with_node(&chan2, &lis2, &n2, K_MSEC(200)), NULL);
76+
77+
zassert_equal(0, zbus_chan_pub(&chan1, &sd, K_MSEC(500)), NULL);
78+
zassert_equal(count_callback1, 0, "The counter could not be more than zero, no obs");
79+
80+
zassert_equal(0, zbus_chan_add_obs_with_node(&chan1, &lis1, &n1, K_MSEC(200)), NULL);
81+
82+
/* The node cannot be reused */
83+
zassert_equal(-EBUSY, zbus_chan_add_obs_with_node(&chan2, &lis1, &n1, K_MSEC(200)), NULL);
84+
85+
zassert_equal(-EALREADY, zbus_chan_add_obs_with_node(&chan1, &lis1, &n1, K_MSEC(200)),
86+
"It cannot be added twice");
87+
88+
zassert_equal(0, zbus_chan_pub(&chan1, &sd, K_MSEC(500)), NULL);
89+
zassert_equal(count_callback1, 1, "The counter could not be more than zero, no obs, %d",
90+
count_callback1);
91+
92+
zassert_equal(0, zbus_chan_rm_obs(&chan1, &lis1, K_MSEC(200)), "It must remove the obs");
93+
zassert_equal(0, zbus_chan_add_obs_with_node(&chan1, &lis1, &n1, K_MSEC(200)), NULL);
94+
/* The node cannot be reused */
95+
zassert_equal(-EBUSY, zbus_chan_add_obs_with_node(&chan3, &lis1, &n1, K_MSEC(200)), NULL);
96+
zassert_equal(0, zbus_chan_rm_obs(&chan1, &lis1, K_MSEC(200)), "It must remove the obs");
97+
zassert_equal(-ENODATA, zbus_chan_rm_obs(&chan1, &lis1, K_MSEC(200)),
98+
"It cannot be removed twice");
99+
100+
zassert_equal(0, zbus_chan_pub(&chan1, &sd, K_MSEC(500)), NULL);
101+
zassert_equal(count_callback1, 1, "The counter could not be more than zero, no obs, %d",
102+
count_callback1);
103+
104+
count_callback2 = 0;
105+
106+
zassert_equal(0, zbus_chan_pub(&chan2, &sd, K_MSEC(500)), NULL);
107+
zassert_equal(count_callback2, 1, "The counter could not be more than zero, no obs");
108+
109+
zassert_equal(0, zbus_chan_add_obs_with_node(&chan2, &lis3, &n3, K_MSEC(200)), NULL);
110+
111+
zassert_equal(-EALREADY, zbus_chan_add_obs_with_node(&chan2, &lis3, &n3, K_MSEC(200)),
112+
"It cannot be added twice");
113+
114+
zassert_equal(0, zbus_chan_pub(&chan2, &sd, K_MSEC(500)), NULL);
115+
zassert_equal(count_callback2, 3, "The counter could not be more than zero, no obs, %d",
116+
count_callback2);
117+
count_callback2 = 0;
118+
zassert_equal(0, zbus_chan_add_obs_with_node(&chan2, &sub1, &n1, K_MSEC(200)), NULL);
119+
zassert_equal(0, zbus_chan_add_obs_with_node(&chan2, &sub2, &n2, K_MSEC(200)), NULL);
120+
zassert_equal(0, zbus_chan_add_obs_with_node(&chan2, &lis4, &n4, K_MSEC(200)),
121+
"It must add the obs");
122+
zassert_equal(0, zbus_chan_add_obs_with_node(&chan2, &lis5, &n5, K_MSEC(200)),
123+
"It must add the obs");
124+
zassert_equal(0, zbus_chan_add_obs_with_node(&chan2, &lis6, &n6, K_MSEC(200)),
125+
"It must add the obs");
126+
127+
zassert_equal(0, zbus_chan_pub(&chan2, &sd, K_MSEC(500)), NULL);
128+
zassert_equal(count_callback2, 5, NULL);
129+
130+
/* To cause an error to sub1 and sub2. They have the queue full in this point */
131+
/* ENOMSG must be the result */
132+
zassert_equal(-ENOMSG, zbus_chan_pub(&chan2, &sd, K_MSEC(500)), NULL);
133+
zassert_equal(count_callback2, 10, NULL);
134+
135+
zassert_equal(0, zbus_chan_rm_obs(&chan2, &sub1, K_MSEC(200)), NULL);
136+
zassert_equal(0, zbus_chan_rm_obs(&chan2, &sub2, K_MSEC(200)), NULL);
137+
}
138+
139+
struct aux2_wq_data {
140+
struct k_work work;
141+
};
142+
143+
static struct aux2_wq_data wq_handler;
144+
145+
static void wq_dh_cb(struct k_work *item)
146+
{
147+
static struct zbus_observer_node node;
148+
149+
zassert_equal(-EAGAIN, zbus_chan_add_obs_with_node(&chan2, &sub1, &node, K_MSEC(200)),
150+
NULL);
151+
zassert_equal(-EAGAIN, zbus_chan_rm_obs(&chan2, &sub2, K_MSEC(200)), NULL);
152+
}
153+
154+
ZTEST(basic, test_specification_based__zbus_obs_add_rm_obs_busy)
155+
{
156+
zassert_equal(0, zbus_chan_claim(&chan2, K_NO_WAIT), NULL);
157+
158+
k_work_init(&wq_handler.work, wq_dh_cb);
159+
k_work_submit(&wq_handler.work);
160+
k_msleep(1000);
161+
162+
zassert_equal(0, zbus_chan_finish(&chan2), NULL);
163+
}
164+
165+
ZBUS_CHAN_DEFINE(chan4, /* Name */
166+
struct sensor_data_msg, /* Message type */
167+
168+
NULL, /* Validator */
169+
NULL, /* User data */
170+
ZBUS_OBSERVERS(prio_lis6, prio_lis5), /* observers */
171+
ZBUS_MSG_INIT(0) /* Initial value major 0, minor 1, build 1023 */
172+
);
173+
174+
static int execution_sequence_idx;
175+
static uint8_t execution_sequence[6] = {0};
176+
177+
#define CALLBACK_DEF(_lis, _idx) \
178+
static void _CONCAT(prio_cb, _idx)(const struct zbus_channel *chan) \
179+
{ \
180+
execution_sequence[execution_sequence_idx] = _idx; \
181+
++execution_sequence_idx; \
182+
} \
183+
ZBUS_LISTENER_DEFINE(_lis, _CONCAT(prio_cb, _idx))
184+
185+
CALLBACK_DEF(prio_lis1, 1);
186+
CALLBACK_DEF(prio_lis2, 2);
187+
CALLBACK_DEF(prio_lis3, 3);
188+
CALLBACK_DEF(prio_lis4, 4);
189+
CALLBACK_DEF(prio_lis5, 5);
190+
CALLBACK_DEF(prio_lis6, 6);
191+
192+
ZBUS_CHAN_ADD_OBS(chan4, prio_lis3, 3);
193+
ZBUS_CHAN_ADD_OBS(chan4, prio_lis4, 2);
194+
195+
/* Checking the ZBUS_CHAN_ADD_OBS. The execution sequence must be: 6, 5, 4, 3, 2, 1. */
196+
197+
ZTEST(basic, test_specification_based__zbus_obs_priority)
198+
{
199+
struct sensor_data_msg sd = {.a = 70, .b = 116};
200+
static struct zbus_observer_node n1, n2;
201+
202+
execution_sequence_idx = 0;
203+
204+
zassert_equal(0, zbus_chan_add_obs_with_node(&chan4, &prio_lis2, &n1, K_MSEC(200)), NULL);
205+
zassert_equal(0, zbus_chan_add_obs_with_node(&chan4, &prio_lis1, &n2, K_MSEC(200)), NULL);
206+
207+
zassert_equal(0, zbus_chan_pub(&chan4, &sd, K_MSEC(500)), NULL);
208+
209+
zassert_equal(execution_sequence[0], 6, NULL);
210+
zassert_equal(execution_sequence[1], 5, NULL);
211+
zassert_equal(execution_sequence[2], 4, NULL);
212+
zassert_equal(execution_sequence[3], 3, NULL);
213+
zassert_equal(execution_sequence[4], 2, NULL);
214+
zassert_equal(execution_sequence[5], 1, NULL);
215+
}
216+
217+
ZTEST_SUITE(basic, 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.runtime_obs_reg.add_and_remove_observers_with_node:
3+
tags: zbus
4+
integration_platforms:
5+
- qemu_x86

0 commit comments

Comments
 (0)