Skip to content

Commit 33996ab

Browse files
committed
tests: zbus: Adjust runtime observers tests
Reverse the changes the affected API and add test to the new dynamic and static variations. Signed-off-by: Rodrigo Peixoto <[email protected]>
1 parent d70345c commit 33996ab

File tree

4 files changed

+49
-40
lines changed

4 files changed

+49
-40
lines changed

tests/subsys/zbus/runtime_observers_registration/prj.conf

-1
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

+34-27
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

Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
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
- native_sim
6+
extra_configs:
7+
- CONFIG_HEAP_MEM_POOL_SIZE=2048
8+
message_bus.zbus.runtime_obs_reg.add_and_remove_observers_static_memory:
9+
tags: zbus
10+
integration_platforms:
11+
- native_sim
12+
extra_configs:
13+
- CONFIG_ZBUS_RUNTIME_OBSERVERS_NODE_POOL_SIZE=6

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

+6-11
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@ static struct action_msg ga;
137137

138138
static void isr_handler(const void *operation)
139139
{
140-
static struct zbus_observer_node fast_node, aux_node;
141140
enum operation *op = (enum operation *)operation;
142141

143142
switch (*op) {
@@ -157,7 +156,7 @@ static void isr_handler(const void *operation)
157156
isr_return = zbus_chan_finish(NULL);
158157
break;
159158
case ADD_OBS_ISR_INVAL:
160-
isr_return = zbus_chan_add_obs(&aux2_chan, &fast_lis, &fast_node, K_MSEC(200));
159+
isr_return = zbus_chan_add_obs(&aux2_chan, &fast_lis, K_MSEC(200));
161160
break;
162161
case RM_OBS_ISR_INVAL:
163162
isr_return = zbus_chan_rm_obs(&aux2_chan, &fast_lis, K_MSEC(200));
@@ -178,7 +177,7 @@ static void isr_handler(const void *operation)
178177
isr_return = zbus_chan_finish(&aux2_chan);
179178
break;
180179
case ADD_OBS_ISR:
181-
isr_return = zbus_chan_add_obs(&aux2_chan, NULL, &aux_node, K_MSEC(200));
180+
isr_return = zbus_chan_add_obs(&aux2_chan, NULL, K_MSEC(200));
182181
break;
183182
case RM_OBS_ISR:
184183
isr_return = zbus_chan_rm_obs(&aux2_chan, NULL, K_MSEC(200));
@@ -245,7 +244,6 @@ const STRUCT_SECTION_ITERABLE(zbus_observer, invalid_obs) = {
245244

246245
ZTEST(basic, test_specification_based__zbus_chan)
247246
{
248-
static struct zbus_observer_node node;
249247
struct action_msg a = {0};
250248

251249
/* Trying invalid parameters */
@@ -271,11 +269,9 @@ ZTEST(basic, test_specification_based__zbus_chan)
271269

272270
zassert_equal(-EFAULT, zbus_chan_finish(NULL), "It must be -EFAULT");
273271

274-
zassert_equal(-EFAULT, zbus_chan_add_obs(NULL, &sub1, &node, K_MSEC(200)), NULL);
272+
zassert_equal(-EFAULT, zbus_chan_add_obs(NULL, &sub1, K_MSEC(200)), NULL);
275273

276-
zassert_equal(-EFAULT, zbus_chan_add_obs(&aux2_chan, NULL, &node, K_MSEC(200)), NULL);
277-
278-
zassert_equal(-EFAULT, zbus_chan_add_obs(&aux2_chan, &sub1, NULL, K_MSEC(200)), NULL);
274+
zassert_equal(-EFAULT, zbus_chan_add_obs(&aux2_chan, NULL, K_MSEC(200)), NULL);
279275

280276
zassert_equal(-EFAULT, zbus_chan_rm_obs(NULL, &sub1, K_MSEC(200)), NULL);
281277

@@ -330,7 +326,7 @@ ZTEST(basic, test_specification_based__zbus_chan)
330326

331327
k_msleep(100);
332328

333-
zassert_equal(0, zbus_chan_add_obs(&stuck_chan, &sub1, &node, K_MSEC(200)), NULL);
329+
zassert_equal(0, zbus_chan_add_obs(&stuck_chan, &sub1, K_MSEC(200)), NULL);
334330

335331
zassert_equal(0, zbus_chan_notify(&stuck_chan, K_MSEC(200)), "It must finish correctly");
336332

@@ -603,7 +599,6 @@ ZTEST(basic, test_hard_channel)
603599

604600
ZTEST(basic, test_specification_based__zbus_obs_set_enable)
605601
{
606-
struct zbus_observer_node node;
607602
bool enable;
608603

609604
count_fast = 0;
@@ -622,7 +617,7 @@ ZTEST(basic, test_specification_based__zbus_obs_set_enable)
622617
zbus_obs_is_enabled(&rt_fast_lis, &enable);
623618
zassert_equal(false, enable);
624619

625-
zassert_equal(0, zbus_chan_add_obs(&aux1_chan, &rt_fast_lis, &node, K_MSEC(200)), NULL);
620+
zassert_equal(0, zbus_chan_add_obs(&aux1_chan, &rt_fast_lis, K_MSEC(200)), NULL);
626621

627622
zassert_equal(0, zbus_obs_set_enable(&fast_lis, false),
628623
"Must be zero. The observer must be disabled");

0 commit comments

Comments
 (0)