Debian Codegraph @

tests/wp/events.c

raw
1 /* WirePlumber
2 *
3 * Copyright © 2022 Collabora Ltd.
4 * @author George Kiagiadakis <[email protected]>
5 *
6 * SPDX-License-Identifier: MIT
7 */
8
9 #include "../common/base-test-fixture.h"
10
11 typedef struct {
12 WpBaseTestFixture base;
13 GPtrArray *hooks_executed;
14 GPtrArray *events;
15 WpTransition *transition;
16 } TestFixture;
17
18 static void
19 test_events_setup (TestFixture *self, gconstpointer user_data)
20 {
21 wp_base_test_fixture_setup (&self->base, 0);
22 self->hooks_executed = g_ptr_array_new ();
23 self->events = g_ptr_array_new ();
24 }
25
26 static void
27 test_events_teardown (TestFixture *self, gconstpointer user_data)
28 {
29 g_clear_pointer (&self->hooks_executed, g_ptr_array_unref);
30 g_clear_pointer (&self->events, g_ptr_array_unref);
31 wp_base_test_fixture_teardown (&self->base);
32 }
33
34 #define HOOK_FUNC(x) \
35 static void \
36 hook_##x (WpEvent * event, TestFixture * self) \
37 { \
38 g_debug ("in hook_" #x); \
39 g_ptr_array_add (self->hooks_executed, hook_##x); \
40 g_ptr_array_add (self->events, event); \
41 }
42
43 HOOK_FUNC (a)
44 HOOK_FUNC (b)
45 HOOK_FUNC (c)
46 HOOK_FUNC (d)
47
48 static void
49 hook_quit (WpEvent *event, TestFixture *self)
50 {
51 g_debug ("in hook_quit");
52 g_ptr_array_add (self->hooks_executed, hook_quit);
53 g_ptr_array_add (self->events, event);
54 g_main_loop_quit (self->base.loop);
55 }
56
57 static void
58 test_events_order (TestFixture *self, gconstpointer user_data)
59 {
60 g_autoptr (WpEventDispatcher) dispatcher = NULL;
61 g_autoptr (WpEventHook) hook = NULL;
62 WpEvent *event1 = NULL, *event2 = NULL, *event3 = NULL, *event4;
63
64 dispatcher = wp_event_dispatcher_get_instance (self->base.core);
65 g_assert_nonnull (dispatcher);
66
67 hook = wp_simple_event_hook_new ("hook-a", NULL, NULL,
68 g_cclosure_new ((GCallback) hook_a, self, NULL));
69 wp_interest_event_hook_add_interest (WP_INTEREST_EVENT_HOOK (hook),
70 WP_CONSTRAINT_TYPE_PW_PROPERTY, "event.type", "=s", "type1", NULL);
71 wp_event_dispatcher_register_hook (dispatcher, hook);
72 g_clear_object (&hook);
73
74 hook = wp_simple_event_hook_new ("hook-quit", NULL, NULL,
75 g_cclosure_new ((GCallback) hook_quit, self, NULL));
76 wp_interest_event_hook_add_interest (WP_INTEREST_EVENT_HOOK (hook),
77 WP_CONSTRAINT_TYPE_PW_PROPERTY, "event.type", "=s", "quit", NULL);
78 wp_event_dispatcher_register_hook (dispatcher, hook);
79 g_clear_object (&hook);
80
81 event1 = wp_event_new ("type1", 20, NULL, NULL, NULL);
82 event2 = wp_event_new ("type1", 20, NULL, NULL, NULL);
83 event3 = wp_event_new ("type1", 30, NULL, NULL, NULL);
84 event4 = wp_event_new ("quit", 10, NULL, NULL, NULL);
85 wp_event_dispatcher_push_event (dispatcher, event1);
86 wp_event_dispatcher_push_event (dispatcher, event2);
87 wp_event_dispatcher_push_event (dispatcher, event3);
88 wp_event_dispatcher_push_event (dispatcher, event4);
89
90 g_main_loop_run (self->base.loop);
91 g_assert_cmpint (self->hooks_executed->len, == , 4);
92 g_assert_cmpint (self->events->len, == , 4);
93
94 g_assert_true (hook_a == self->hooks_executed->pdata [0]);
95 g_assert_true (event3 == self->events->pdata [0]);
96 g_assert_true (hook_a == self->hooks_executed->pdata [1]);
97 g_assert_true (event1 == self->events->pdata [1]);
98 g_assert_true (hook_a == self->hooks_executed->pdata [2]);
99 g_assert_true (event2 == self->events->pdata [2]);
100 g_assert_true (hook_quit == self->hooks_executed->pdata [3]);
101 g_assert_true (event4 == self->events->pdata [3]);
102 }
103
104 static void
105 test_events_basic (TestFixture *self, gconstpointer user_data)
106 {
107 g_autoptr (WpEventDispatcher) dispatcher = NULL;
108 g_autoptr (WpEventHook) hook = NULL;
109 WpEvent *event1 = NULL, *event2 = NULL, *event3 = NULL;
110 const gchar **before, **after;
111
112 dispatcher = wp_event_dispatcher_get_instance (self->base.core);
113 g_assert_nonnull (dispatcher);
114
115 before = (const gchar *[]) { "hook-d", "hook-b", NULL };
116 after = (const gchar *[]) { "hook-c", NULL };
117 hook = wp_simple_event_hook_new ("hook-a", before, after,
118 g_cclosure_new ((GCallback) hook_a, self, NULL));
119 wp_interest_event_hook_add_interest (WP_INTEREST_EVENT_HOOK (hook),
120 WP_CONSTRAINT_TYPE_PW_PROPERTY, "event.type", "=s", "type1", NULL);
121 wp_event_dispatcher_register_hook (dispatcher, hook);
122 g_clear_object (&hook);
123
124 before = NULL;
125 after = NULL;
126 hook = wp_simple_event_hook_new ("hook-b", before, after,
127 g_cclosure_new ((GCallback) hook_b, self, NULL));
128 wp_interest_event_hook_add_interest (WP_INTEREST_EVENT_HOOK (hook),
129 WP_CONSTRAINT_TYPE_PW_PROPERTY, "event.type", "=s", "type1", NULL);
130 wp_event_dispatcher_register_hook (dispatcher, hook);
131 g_clear_object (&hook);
132
133 before = NULL;
134 after = NULL;
135 hook = wp_simple_event_hook_new ("hook-c", before, after,
136 g_cclosure_new ((GCallback) hook_c, self, NULL));
137 wp_interest_event_hook_add_interest (WP_INTEREST_EVENT_HOOK (hook),
138 WP_CONSTRAINT_TYPE_PW_PROPERTY, "event.type", "=s", "type1", NULL);
139 wp_event_dispatcher_register_hook (dispatcher, hook);
140 g_clear_object (&hook);
141
142 /* wrong before/after dependencies shouldn't matter as this hook is not used */
143 before = (const gchar *[]) { "hook-c", "hook-a", NULL };
144 after = (const gchar *[]) { "hook-b", NULL };
145 hook = wp_simple_event_hook_new ("hook-d", before, after,
146 g_cclosure_new ((GCallback) hook_d, self, NULL));
147 wp_interest_event_hook_add_interest (WP_INTEREST_EVENT_HOOK (hook),
148 WP_CONSTRAINT_TYPE_PW_PROPERTY, "event.type", "=s", "type2", NULL);
149 wp_event_dispatcher_register_hook (dispatcher, hook);
150 g_clear_object (&hook);
151
152 before = NULL;
153 after = (const gchar *[]) { "hook-a", "hook-b", "hook-c", "hook-d", NULL };
154 hook = wp_simple_event_hook_new ("hook-quit", before, after,
155 g_cclosure_new ((GCallback) hook_quit, self, NULL));
156 wp_interest_event_hook_add_interest (WP_INTEREST_EVENT_HOOK (hook),
157 WP_CONSTRAINT_TYPE_PW_PROPERTY, "event.type", "=s", "type1", NULL);
158 wp_interest_event_hook_add_interest (WP_INTEREST_EVENT_HOOK (hook),
159 WP_CONSTRAINT_TYPE_PW_PROPERTY, "event.type", "=s", "type3", NULL);
160 wp_event_dispatcher_register_hook (dispatcher, hook);
161 g_clear_object (&hook);
162
163 /* first event run */
164 event1 = wp_event_new ("type1", 10, NULL, NULL, NULL);
165 wp_event_dispatcher_push_event (dispatcher, event1);
166
167 g_assert_cmpint (self->hooks_executed->len, == , 0);
168 g_assert_cmpint (self->events->len, == , 0);
169 g_main_loop_run (self->base.loop);
170 g_assert_cmpint (self->hooks_executed->len, == , 4);
171 g_assert_cmpint (self->events->len, == , 4);
172 g_assert_true (hook_c == self->hooks_executed->pdata [0]);
173 g_assert_true (event1 == self->events->pdata [0]);
174 g_assert_true (hook_a == self->hooks_executed->pdata [1]);
175 g_assert_true (event1 == self->events->pdata [1]);
176 g_assert_true (hook_b == self->hooks_executed->pdata [2]);
177 g_assert_true (event1 == self->events->pdata [2]);
178 g_assert_true (hook_quit == self->hooks_executed->pdata [3]);
179 g_assert_true (event1 == self->events->pdata [3]);
180
181 g_ptr_array_remove_range (self->hooks_executed, 0, self->hooks_executed->len);
182 g_assert_cmpint (self->hooks_executed->len, == , 0);
183 g_ptr_array_remove_range (self->events, 0, self->events->len);
184 g_assert_cmpint (self->events->len, == , 0);
185
186 /* second event run */
187 event1 = wp_event_new ("type1", 10,
188 wp_properties_new ("test.prop", "some-val", NULL), NULL, NULL);
189 event2 = wp_event_new ("type2", 100,
190 wp_properties_new ("test.prop", "some-val", NULL), NULL, NULL);
191
192 wp_event_dispatcher_push_event (dispatcher, event1);
193 wp_event_dispatcher_push_event (dispatcher, event2);
194
195 g_assert_cmpint (self->hooks_executed->len, == , 0);
196 g_assert_cmpint (self->events->len, == , 0);
197 g_main_loop_run (self->base.loop);
198 g_assert_cmpint (self->hooks_executed->len, == , 5);
199 g_assert_cmpint (self->events->len, == , 5);
200 g_assert_true (hook_d == self->hooks_executed->pdata [0]);
201 g_assert_true (event2 == self->events->pdata [0]);
202 g_assert_true (hook_c == self->hooks_executed->pdata [1]);
203 g_assert_true (event1 == self->events->pdata [1]);
204 g_assert_true (hook_a == self->hooks_executed->pdata [2]);
205 g_assert_true (event1 == self->events->pdata [2]);
206 g_assert_true (hook_b == self->hooks_executed->pdata [3]);
207 g_assert_true (event1 == self->events->pdata [3]);
208 g_assert_true (hook_quit == self->hooks_executed->pdata [4]);
209 g_assert_true (event1 == self->events->pdata [4]);
210
211 g_ptr_array_remove_range (self->hooks_executed, 0, self->hooks_executed->len);
212 g_assert_cmpint (self->hooks_executed->len, == , 0);
213 g_ptr_array_remove_range (self->events, 0, self->events->len);
214 g_assert_cmpint (self->events->len, == , 0);
215
216 /* third event run */
217 event1 = wp_event_new ("type1", 10,
218 wp_properties_new ("test.prop", "some-val", NULL), NULL, NULL);
219 event2 = wp_event_new ("type2", 100,
220 wp_properties_new ("test.prop", "some-val", NULL), NULL, NULL);
221 event3 = wp_event_new ("type3", 50,
222 wp_properties_new ("test.prop", "some-val", NULL), NULL, NULL);
223
224 wp_event_dispatcher_push_event (dispatcher, event3);
225 wp_event_dispatcher_push_event (dispatcher, event2);
226 wp_event_dispatcher_push_event (dispatcher, event1);
227 wp_event_stop_processing (event1);
228
229 g_assert_cmpint (self->hooks_executed->len, == , 0);
230 g_assert_cmpint (self->events->len, == , 0);
231 g_main_loop_run (self->base.loop);
232 g_assert_cmpint (self->hooks_executed->len, == , 2);
233 g_assert_cmpint (self->events->len, == , 2);
234 g_assert_true (hook_d == self->hooks_executed->pdata [0]);
235 g_assert_true (event2 == self->events->pdata [0]);
236 g_assert_true (hook_quit == self->hooks_executed->pdata [1]);
237 g_assert_true (event3 == self->events->pdata [1]);
238 }
239
240 enum {
241 STEP_ONE = WP_TRANSITION_STEP_CUSTOM_START,
242 STEP_TWO,
243 };
244
245 static guint
246 async_hook_get_next_step (WpTransition *transition, guint step,
247 TestFixture *self)
248 {
249 switch (step) {
250 case WP_TRANSITION_STEP_NONE: return STEP_ONE;
251 case STEP_ONE: return STEP_TWO;
252 case STEP_TWO: return WP_TRANSITION_STEP_NONE;
253 default: return WP_TRANSITION_STEP_ERROR;
254 }
255 }
256
257 static void
258 async_hook_execute_step (WpTransition *transition, guint step,
259 TestFixture *self)
260 {
261 switch (step) {
262 case STEP_ONE:
263 g_ptr_array_add (self->hooks_executed, async_hook_execute_step);
264 self->transition = transition;
265 g_main_loop_quit (self->base.loop);
266 break;
267 case STEP_TWO:
268 self->transition = NULL;
269 wp_transition_advance (transition);
270 break;
271 default:
272 g_assert_not_reached ();
273 }
274 }
275
276 static void
277 test_events_async_hook (TestFixture *self, gconstpointer user_data)
278 {
279 g_autoptr (WpEventDispatcher) dispatcher = NULL;
280 g_autoptr (WpEventHook) hook = NULL;
281 const gchar **before, **after;
282
283 dispatcher = wp_event_dispatcher_get_instance (self->base.core);
284 g_assert_nonnull (dispatcher);
285
286 before = NULL;
287 after = NULL;
288 hook = wp_simple_event_hook_new ("hook-a", before, after,
289 g_cclosure_new ((GCallback) hook_a, self, NULL));
290 wp_interest_event_hook_add_interest (WP_INTEREST_EVENT_HOOK (hook),
291 WP_CONSTRAINT_TYPE_PW_PROPERTY, "event.type", "=s", "type1", NULL);
292 wp_event_dispatcher_register_hook (dispatcher, hook);
293 g_clear_object (&hook);
294
295 before = (const gchar *[]) { "hook-quit", NULL };
296 after = (const gchar *[]) { "hook-a", NULL };
297 hook = wp_simple_event_hook_new ("hook-b", before, after,
298 g_cclosure_new ((GCallback) hook_b, self, NULL));
299 wp_interest_event_hook_add_interest (WP_INTEREST_EVENT_HOOK (hook),
300 WP_CONSTRAINT_TYPE_PW_PROPERTY, "event.type", "=s", "type1", NULL);
301 wp_event_dispatcher_register_hook (dispatcher, hook);
302 g_clear_object (&hook);
303
304 before = (const gchar *[]) { "hook-a", NULL };
305 after = NULL;
306 hook = wp_simple_event_hook_new ("hook-c", before, after,
307 g_cclosure_new ((GCallback) hook_c, self, NULL));
308 wp_interest_event_hook_add_interest (WP_INTEREST_EVENT_HOOK (hook),
309 WP_CONSTRAINT_TYPE_PW_PROPERTY, "event.type", "=s", "type1", NULL);
310 wp_event_dispatcher_register_hook (dispatcher, hook);
311 g_clear_object (&hook);
312
313 before = NULL;
314 after = NULL;
315 hook = wp_simple_event_hook_new ("hook-quit", before, after,
316 g_cclosure_new ((GCallback) hook_quit, self, NULL));
317 wp_interest_event_hook_add_interest (WP_INTEREST_EVENT_HOOK (hook),
318 WP_CONSTRAINT_TYPE_PW_PROPERTY, "event.type", "=s", "type1", NULL);
319 wp_interest_event_hook_add_interest (WP_INTEREST_EVENT_HOOK (hook),
320 WP_CONSTRAINT_TYPE_PW_PROPERTY, "event.type", "=s", "type3", NULL);
321 wp_event_dispatcher_register_hook (dispatcher, hook);
322 g_clear_object (&hook);
323
324 before = (const gchar *[]) { "hook-a", NULL };
325 after = (const gchar *[]) { "hook-c", NULL };
326 hook = wp_async_event_hook_new ("async-test-hook", before, after,
327 g_cclosure_new ((GCallback) async_hook_get_next_step, self, NULL),
328 g_cclosure_new ((GCallback) async_hook_execute_step, self, NULL));
329 wp_interest_event_hook_add_interest (WP_INTEREST_EVENT_HOOK (hook),
330 WP_CONSTRAINT_TYPE_PW_PROPERTY, "event.type", "=s", "type1", NULL);
331 wp_event_dispatcher_register_hook (dispatcher, hook);
332 g_clear_object (&hook);
333
334 wp_event_dispatcher_push_event (dispatcher,
335 wp_event_new ("type1", 10, NULL, NULL, NULL));
336
337 g_assert_cmpint (self->hooks_executed->len, == , 0);
338 g_main_loop_run (self->base.loop);
339 g_assert_cmpint (self->hooks_executed->len, == , 2);
340 g_assert_true (hook_c == self->hooks_executed->pdata [0]);
341 g_assert_true (async_hook_execute_step == self->hooks_executed->pdata [1]);
342
343 g_assert_nonnull (self->transition);
344 wp_transition_advance (self->transition);
345 g_assert_null (self->transition);
346
347 g_assert_cmpint (self->hooks_executed->len, == , 2);
348 g_main_loop_run (self->base.loop);
349 g_assert_cmpint (self->hooks_executed->len, == , 5);
350 g_assert_true (hook_c == self->hooks_executed->pdata [0]);
351 g_assert_true (async_hook_execute_step == self->hooks_executed->pdata [1]);
352 g_assert_true (hook_a == self->hooks_executed->pdata [2]);
353 g_assert_true (hook_b == self->hooks_executed->pdata [3]);
354 g_assert_true (hook_quit == self->hooks_executed->pdata [4]);
355 }
356
357 static void
358 test_events_glob_deps (TestFixture *self, gconstpointer user_data)
359 {
360 g_autoptr (WpEventDispatcher) dispatcher = NULL;
361 g_autoptr (WpEventHook) hook = NULL;
362 WpEvent *event1 = NULL;
363 const gchar **before, **after;
364
365 dispatcher = wp_event_dispatcher_get_instance (self->base.core);
366 g_assert_nonnull (dispatcher);
367
368 before = (const gchar *[]) { "*-d", "*-b", NULL };
369 after = (const gchar *[]) { "hook-c", NULL };
370 hook = wp_simple_event_hook_new ("hook-a", before, after,
371 g_cclosure_new ((GCallback) hook_a, self, NULL));
372 wp_interest_event_hook_add_interest (WP_INTEREST_EVENT_HOOK (hook),
373 WP_CONSTRAINT_TYPE_PW_PROPERTY, "event.type", "=s", "type1", NULL);
374 wp_event_dispatcher_register_hook (dispatcher, hook);
375 g_clear_object (&hook);
376
377 before = NULL;
378 after = NULL;
379 hook = wp_simple_event_hook_new ("hook-b", before, after,
380 g_cclosure_new ((GCallback) hook_b, self, NULL));
381 wp_interest_event_hook_add_interest (WP_INTEREST_EVENT_HOOK (hook),
382 WP_CONSTRAINT_TYPE_PW_PROPERTY, "event.type", "=s", "type1", NULL);
383 wp_event_dispatcher_register_hook (dispatcher, hook);
384 g_clear_object (&hook);
385
386 before = NULL;
387 after = NULL;
388 hook = wp_simple_event_hook_new ("hook-c", before, after,
389 g_cclosure_new ((GCallback) hook_c, self, NULL));
390 wp_interest_event_hook_add_interest (WP_INTEREST_EVENT_HOOK (hook),
391 WP_CONSTRAINT_TYPE_PW_PROPERTY, "event.type", "=s", "type1", NULL);
392 wp_event_dispatcher_register_hook (dispatcher, hook);
393 g_clear_object (&hook);
394
395 before = NULL;
396 after = (const gchar *[]) { "h*-b", NULL };
397 hook = wp_simple_event_hook_new ("hook-d", before, after,
398 g_cclosure_new ((GCallback) hook_d, self, NULL));
399 wp_interest_event_hook_add_interest (WP_INTEREST_EVENT_HOOK (hook),
400 WP_CONSTRAINT_TYPE_PW_PROPERTY, "event.type", "=s", "type1", NULL);
401 wp_event_dispatcher_register_hook (dispatcher, hook);
402 g_clear_object (&hook);
403
404 before = NULL;
405 after = (const gchar *[]) { "hook-a", "hook-b", "hook-c", "hook-d", NULL };
406 hook = wp_simple_event_hook_new ("hook-quit", before, after,
407 g_cclosure_new ((GCallback) hook_quit, self, NULL));
408 wp_interest_event_hook_add_interest (WP_INTEREST_EVENT_HOOK (hook),
409 WP_CONSTRAINT_TYPE_PW_PROPERTY, "event.type", "=s", "type1", NULL);
410 wp_event_dispatcher_register_hook (dispatcher, hook);
411 g_clear_object (&hook);
412
413 /* first event run */
414 event1 = wp_event_new ("type1", 10, NULL, NULL, NULL);
415 wp_event_dispatcher_push_event (dispatcher, event1);
416
417 g_assert_cmpint (self->hooks_executed->len, == , 0);
418 g_main_loop_run (self->base.loop);
419 g_assert_cmpint (self->hooks_executed->len, == , 5);
420 g_assert_true (hook_c == self->hooks_executed->pdata [0]);
421 g_assert_true (hook_a == self->hooks_executed->pdata [1]);
422 g_assert_true (hook_b == self->hooks_executed->pdata [2]);
423 g_assert_true (hook_d == self->hooks_executed->pdata [3]);
424 g_assert_true (hook_quit == self->hooks_executed->pdata [4]);
425 }
426
427 gint
428 main (gint argc, gchar *argv[])
429 {
430 g_test_init (&argc, &argv, NULL);
431 wp_init (WP_INIT_ALL);
432
433 g_test_add ("/wp/events/order", TestFixture, NULL,
434 test_events_setup, test_events_order, test_events_teardown);
435 g_test_add ("/wp/events/basic", TestFixture, NULL,
436 test_events_setup, test_events_basic, test_events_teardown);
437 g_test_add ("/wp/events/async_hook", TestFixture, NULL,
438 test_events_setup, test_events_async_hook, test_events_teardown);
439 g_test_add ("/wp/events/glob_deps", TestFixture, NULL,
440 test_events_setup, test_events_glob_deps, test_events_teardown);
441
442 return g_test_run ();
443 }