Skip to content

Commit d3638ae

Browse files
committed
tests: zbus: test ZBUS_RUNTIME_WAITER_DEFINE
Validate that an observer created with `ZBUS_RUNTIME_WAITER_DEFINE` operates correctly. Signed-off-by: Jordan Yates <[email protected]>
1 parent 6962d21 commit d3638ae

File tree

1 file changed

+43
-0
lines changed
  • tests/subsys/zbus/runtime_observers_registration/src

1 file changed

+43
-0
lines changed

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

+43
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,49 @@ ZTEST(basic, test_specification_based__zbus_obs_add_rm_obs)
126126
zassert_equal(0, zbus_chan_rm_obs(&chan2, &sub2, K_MSEC(200)), NULL);
127127
}
128128

129+
static void chan1_publisher(struct k_work *work)
130+
{
131+
struct k_work_delayable *dwork = k_work_delayable_from_work(work);
132+
struct sensor_data_msg sd = {.a = 10, .b = 100};
133+
134+
zassert_equal(0, zbus_chan_pub(&chan1, &sd, K_MSEC(5)));
135+
136+
k_work_reschedule(dwork, K_MSEC(100));
137+
}
138+
139+
ZTEST(basic, test_specification_based__zbus_obs_stack_waiter)
140+
{
141+
struct k_work_delayable publisher;
142+
struct k_work_sync sync;
143+
struct k_sem pub_sem;
144+
145+
ZBUS_RUNTIME_WAITER_DEFINE(waiter, &pub_sem);
146+
147+
/* Start the channel publisher */
148+
k_work_init_delayable(&publisher, chan1_publisher);
149+
k_work_schedule(&publisher, K_NO_WAIT);
150+
k_sleep(K_MSEC(2));
151+
152+
/* Setup semaphore and add waiter to channel */
153+
zassert_equal(0, k_sem_init(&pub_sem, 0, 1));
154+
zassert_equal(0, zbus_chan_add_obs(&chan1, &waiter, K_MSEC(10)), NULL);
155+
156+
/* Wait for channel to be published multiple times */
157+
for (int i = 0; i < 5; i++) {
158+
zassert_equal(-EAGAIN, k_sem_take(&pub_sem, K_MSEC(80)));
159+
zassert_equal(0, k_sem_take(&pub_sem, K_MSEC(30)));
160+
}
161+
162+
/* Cleanup the waiter */
163+
zassert_equal(0, zbus_chan_rm_obs(&chan1, &waiter, K_MSEC(10)));
164+
165+
/* No more semaphore handling */
166+
zassert_equal(-EAGAIN, k_sem_take(&pub_sem, K_MSEC(120)));
167+
168+
/* Cancel the channel publisher */
169+
zassert_true(k_work_cancel_delayable_sync(&publisher, &sync));
170+
}
171+
129172
struct aux2_wq_data {
130173
struct k_work work;
131174
};

0 commit comments

Comments
 (0)