Skip to content

Commit 6880b85

Browse files
rodrigopexcarlescufi
authored andcommitted
tests: zbus: fix tests to work with iterable sections observers
Adjust the `runtime_observers_registration` and `unittests` tests to work with iterable sections channel observation. Add a sequence check for the runtime observers. Add a tests for iterator functions for channels and observer to work with `user_data` pointer. Signed-off-by: Rodrigo Peixoto <[email protected]>
1 parent 7e44469 commit 6880b85

File tree

4 files changed

+195
-87
lines changed

4 files changed

+195
-87
lines changed
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
CONFIG_ZTEST=y
22
CONFIG_ZTEST_NEW_API=y
3-
# CONFIG_ASSERT=y
43
CONFIG_LOG=y
54
CONFIG_ZBUS=y
65
CONFIG_ZBUS_ASSERT_MOCK=y
76
CONFIG_ZBUS_LOG_LEVEL_DBG=y
8-
CONFIG_ZBUS_RUNTIME_OBSERVERS_POOL_SIZE=6
7+
CONFIG_ZBUS_RUNTIME_OBSERVERS=y
8+
CONFIG_HEAP_MEM_POOL_SIZE=2048
99
CONFIG_ZBUS_OBSERVER_NAME=y
10-
CONFIG_MEM_SLAB_TRACE_MAX_UTILIZATION=y

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

Lines changed: 64 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -70,38 +70,25 @@ ZTEST(basic, test_specification_based__zbus_obs_add_rm_obs)
7070
count_callback1 = 0;
7171
struct sensor_data_msg sd = {.a = 10, .b = 100};
7272

73-
zassert_equal(CONFIG_ZBUS_RUNTIME_OBSERVERS_POOL_SIZE,
74-
k_mem_slab_num_free_get(zbus_runtime_obs_pool()), NULL);
7573
/* Tyring to add same static observer as one dynamic */
7674
zassert_equal(-EEXIST, zbus_chan_add_obs(&chan2, &lis2, K_MSEC(200)), NULL);
7775

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

8179
zassert_equal(0, zbus_chan_add_obs(&chan1, &lis1, K_MSEC(200)), NULL);
82-
zassert_equal(CONFIG_ZBUS_RUNTIME_OBSERVERS_POOL_SIZE - 1,
83-
k_mem_slab_num_free_get(zbus_runtime_obs_pool()), NULL);
8480
zassert_equal(-EALREADY, zbus_chan_add_obs(&chan1, &lis1, K_MSEC(200)),
8581
"It cannot be added twice");
8682

87-
zassert_equal(1, k_mem_slab_max_used_get(zbus_runtime_obs_pool()), NULL);
88-
zassert_equal(CONFIG_ZBUS_RUNTIME_OBSERVERS_POOL_SIZE - 1,
89-
k_mem_slab_num_free_get(zbus_runtime_obs_pool()), NULL);
90-
9183
zassert_equal(0, zbus_chan_pub(&chan1, &sd, K_MSEC(500)), NULL);
9284
zassert_equal(count_callback1, 1, "The counter could not be more than zero, no obs, %d",
9385
count_callback1);
9486

9587
zassert_equal(0, zbus_chan_rm_obs(&chan1, &lis1, K_MSEC(200)), "It must remove the obs");
96-
zassert_equal(CONFIG_ZBUS_RUNTIME_OBSERVERS_POOL_SIZE,
97-
k_mem_slab_num_free_get(zbus_runtime_obs_pool()), NULL);
9888

99-
zassert_equal(1, k_mem_slab_max_used_get(zbus_runtime_obs_pool()), NULL);
10089
zassert_equal(-ENODATA, zbus_chan_rm_obs(&chan1, &lis1, K_MSEC(200)),
10190
"It cannot be removed twice");
10291

103-
zassert_equal(CONFIG_ZBUS_RUNTIME_OBSERVERS_POOL_SIZE,
104-
k_mem_slab_num_free_get(zbus_runtime_obs_pool()), NULL);
10592
zassert_equal(0, zbus_chan_pub(&chan1, &sd, K_MSEC(500)), NULL);
10693
zassert_equal(count_callback1, 1, "The counter could not be more than zero, no obs, %d",
10794
count_callback1);
@@ -125,25 +112,26 @@ ZTEST(basic, test_specification_based__zbus_obs_add_rm_obs)
125112
zassert_equal(0, zbus_chan_add_obs(&chan2, &lis4, K_MSEC(200)), "It must add the obs");
126113
zassert_equal(0, zbus_chan_add_obs(&chan2, &lis5, K_MSEC(200)), "It must add the obs");
127114
zassert_equal(0, zbus_chan_add_obs(&chan2, &lis6, K_MSEC(200)), "It must add the obs");
128-
zassert_equal(-EAGAIN, zbus_chan_add_obs(&chan2, &lis7, K_MSEC(200)),
129-
"It must add the obs");
115+
116+
/* Make the heap full */
117+
void *mem;
118+
119+
do {
120+
mem = k_malloc(1);
121+
} while (mem != NULL);
122+
123+
/* With the heap full it will not be possible to add another obs */
124+
zassert_equal(-ENOMEM, zbus_chan_add_obs(&chan2, &lis7, K_MSEC(200)), NULL);
130125
zassert_equal(0, zbus_chan_pub(&chan2, &sd, K_MSEC(500)), NULL);
131126
zassert_equal(count_callback2, 5, NULL);
127+
132128
/* To cause an error to sub1 and sub2. They have the queue full in this point */
133-
/* An error message must be printed saying the */
134-
zassert_equal(-EFAULT, zbus_chan_pub(&chan2, &sd, K_MSEC(500)), NULL);
129+
/* ENOMSG must be the result */
130+
zassert_equal(-ENOMSG, zbus_chan_pub(&chan2, &sd, K_MSEC(500)), NULL);
135131
zassert_equal(count_callback2, 10, NULL);
136132

137-
zassert_equal(CONFIG_ZBUS_RUNTIME_OBSERVERS_POOL_SIZE,
138-
k_mem_slab_max_used_get(zbus_runtime_obs_pool()), NULL);
139-
140-
zassert_equal(0, k_mem_slab_num_free_get(zbus_runtime_obs_pool()), NULL);
141133
zassert_equal(0, zbus_chan_rm_obs(&chan2, &sub1, K_MSEC(200)), NULL);
142134
zassert_equal(0, zbus_chan_rm_obs(&chan2, &sub2, K_MSEC(200)), NULL);
143-
zassert_equal(2, k_mem_slab_num_free_get(zbus_runtime_obs_pool()), NULL);
144-
145-
zassert_equal(CONFIG_ZBUS_RUNTIME_OBSERVERS_POOL_SIZE,
146-
k_mem_slab_max_used_get(zbus_runtime_obs_pool()), NULL);
147135
}
148136

149137
struct aux2_wq_data {
@@ -166,8 +154,58 @@ ZTEST(basic, test_specification_based__zbus_obs_add_rm_obs_busy)
166154
k_work_submit(&wq_handler.work);
167155
k_msleep(1000);
168156

169-
zassert_equal(2, k_mem_slab_num_free_get(zbus_runtime_obs_pool()), NULL);
170157
zassert_equal(0, zbus_chan_finish(&chan2), NULL);
171158
}
172159

160+
ZBUS_CHAN_DEFINE(chan4, /* Name */
161+
struct sensor_data_msg, /* Message type */
162+
163+
NULL, /* Validator */
164+
NULL, /* User data */
165+
ZBUS_OBSERVERS(prio_lis6, prio_lis5), /* observers */
166+
ZBUS_MSG_INIT(0) /* Initial value major 0, minor 1, build 1023 */
167+
);
168+
169+
static int execution_sequence_idx;
170+
static uint8_t execution_sequence[6] = {0};
171+
172+
#define CALLBACK_DEF(_lis, _idx) \
173+
static void _CONCAT(prio_cb, _idx)(const struct zbus_channel *chan) \
174+
{ \
175+
execution_sequence[execution_sequence_idx] = _idx; \
176+
++execution_sequence_idx; \
177+
} \
178+
ZBUS_LISTENER_DEFINE(_lis, _CONCAT(prio_cb, _idx))
179+
180+
CALLBACK_DEF(prio_lis1, 1);
181+
CALLBACK_DEF(prio_lis2, 2);
182+
CALLBACK_DEF(prio_lis3, 3);
183+
CALLBACK_DEF(prio_lis4, 4);
184+
CALLBACK_DEF(prio_lis5, 5);
185+
CALLBACK_DEF(prio_lis6, 6);
186+
187+
ZBUS_CHAN_ADD_OBS(chan4, prio_lis3, 3);
188+
ZBUS_CHAN_ADD_OBS(chan4, prio_lis4, 2);
189+
190+
/* Checking the ZBUS_CHAN_ADD_OBS. The execution sequence must be: 6, 5, 4, 3, 2, 1. */
191+
192+
ZTEST(basic, test_specification_based__zbus_obs_priority)
193+
{
194+
struct sensor_data_msg sd = {.a = 70, .b = 116};
195+
196+
execution_sequence_idx = 0;
197+
198+
zassert_equal(0, zbus_chan_add_obs(&chan4, &prio_lis2, K_MSEC(200)), NULL);
199+
zassert_equal(0, zbus_chan_add_obs(&chan4, &prio_lis1, K_MSEC(200)), NULL);
200+
201+
zassert_equal(0, zbus_chan_pub(&chan4, &sd, K_MSEC(500)), NULL);
202+
203+
zassert_equal(execution_sequence[0], 6, NULL);
204+
zassert_equal(execution_sequence[1], 5, NULL);
205+
zassert_equal(execution_sequence[2], 4, NULL);
206+
zassert_equal(execution_sequence[3], 3, NULL);
207+
zassert_equal(execution_sequence[4], 2, NULL);
208+
zassert_equal(execution_sequence[5], 1, NULL);
209+
}
210+
173211
ZTEST_SUITE(basic, NULL, NULL, NULL, NULL, NULL);

tests/subsys/zbus/unittests/prj.conf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CONFIG_ZBUS=y
66
CONFIG_ZBUS_LOG_LEVEL_DBG=y
77
CONFIG_ZBUS_ASSERT_MOCK=y
88
CONFIG_IRQ_OFFLOAD=y
9-
CONFIG_ZBUS_RUNTIME_OBSERVERS_POOL_SIZE=4
9+
CONFIG_ZBUS_RUNTIME_OBSERVERS=y
10+
CONFIG_HEAP_MEM_POOL_SIZE=2048
1011
CONFIG_ZBUS_CHANNEL_NAME=y
1112
CONFIG_ZBUS_OBSERVER_NAME=y

0 commit comments

Comments
 (0)