Skip to content

Commit 361d0eb

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 7501e9f commit 361d0eb

File tree

1 file changed

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

1 file changed

+44
-0
lines changed

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

+44
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,50 @@ 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+
static struct zbus_observer_node node;
142+
struct k_work_delayable publisher;
143+
struct k_work_sync sync;
144+
struct k_sem pub_sem;
145+
146+
ZBUS_RUNTIME_WAITER_DEFINE(waiter, &pub_sem);
147+
148+
/* Start the channel publisher */
149+
k_work_init_delayable(&publisher, chan1_publisher);
150+
k_work_schedule(&publisher, K_NO_WAIT);
151+
k_sleep(K_MSEC(2));
152+
153+
/* Setup semaphore and add waiter to channel */
154+
zassert_equal(0, k_sem_init(&pub_sem, 0, 1));
155+
zassert_equal(0, zbus_chan_add_obs(&chan1, &waiter, &node, K_MSEC(10)), NULL);
156+
157+
/* Wait for channel to be published multiple times */
158+
for (int i = 0; i < 5; i++) {
159+
zassert_equal(-EAGAIN, k_sem_take(&pub_sem, K_MSEC(80)));
160+
zassert_equal(0, k_sem_take(&pub_sem, K_MSEC(30)));
161+
}
162+
163+
/* Cleanup the waiter */
164+
zassert_equal(0, zbus_chan_rm_obs(&chan1, &waiter, K_MSEC(10)));
165+
166+
/* No more semaphore handling */
167+
zassert_equal(-EAGAIN, k_sem_take(&pub_sem, K_MSEC(120)));
168+
169+
/* Cancel the channel publisher */
170+
zassert_true(k_work_cancel_delayable_sync(&publisher, &sync));
171+
}
172+
129173
struct aux2_wq_data {
130174
struct k_work work;
131175
};

0 commit comments

Comments
 (0)