Commit | Line | Data |
---|---|---|
223c70b2 PP |
1 | /* |
2 | * Copyright 2017 - Philippe Proulx <pproulx@efficios.com> | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation; under version 2 of the License. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, | |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 | * GNU General Public License for more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public License along | |
14 | * with this program; if not, write to the Free Software Foundation, Inc., | |
15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
16 | */ | |
17 | ||
18 | #include <stdio.h> | |
19 | #include <stdlib.h> | |
20 | #include <stdint.h> | |
c55a9f58 | 21 | #include <stdbool.h> |
223c70b2 PP |
22 | #include <inttypes.h> |
23 | #include <string.h> | |
351946b4 | 24 | #include <babeltrace/babeltrace.h> |
25583cd0 | 25 | #include <babeltrace/assert-internal.h> |
223c70b2 PP |
26 | #include <glib.h> |
27 | ||
28 | #include "tap/tap.h" | |
29 | ||
ad847455 | 30 | #define NR_TESTS 5 |
223c70b2 PP |
31 | |
32 | enum test { | |
33 | TEST_NO_AUTO_NOTIFS, | |
e893886e | 34 | TEST_OUTPUT_PORT_NOTIFICATION_ITERATOR, |
223c70b2 PP |
35 | }; |
36 | ||
37 | enum test_event_type { | |
38 | TEST_EV_TYPE_NOTIF_UNEXPECTED, | |
39 | TEST_EV_TYPE_NOTIF_EVENT, | |
223c70b2 PP |
40 | TEST_EV_TYPE_NOTIF_STREAM_BEGIN, |
41 | TEST_EV_TYPE_NOTIF_PACKET_BEGIN, | |
42 | TEST_EV_TYPE_NOTIF_PACKET_END, | |
43 | TEST_EV_TYPE_NOTIF_STREAM_END, | |
44 | TEST_EV_TYPE_END, | |
45 | TEST_EV_TYPE_SENTINEL, | |
46 | }; | |
47 | ||
48 | struct test_event { | |
49 | enum test_event_type type; | |
dadf29b9 PP |
50 | const struct bt_stream *stream; |
51 | const struct bt_packet *packet; | |
223c70b2 PP |
52 | }; |
53 | ||
223c70b2 PP |
54 | static bool debug = false; |
55 | static enum test current_test; | |
56 | static GArray *test_events; | |
80a256b6 | 57 | static struct bt_private_graph *graph; |
dadf29b9 PP |
58 | static struct bt_stream_class *src_stream_class; |
59 | static struct bt_event_class *src_event_class; | |
60 | static struct bt_stream *src_stream1; | |
61 | static struct bt_stream *src_stream2; | |
62 | static struct bt_packet *src_stream1_packet1; | |
63 | static struct bt_packet *src_stream1_packet2; | |
64 | static struct bt_packet *src_stream2_packet1; | |
65 | static struct bt_packet *src_stream2_packet2; | |
66 | static const struct bt_stream *pub_src_stream1; | |
67 | static const struct bt_stream *pub_src_stream2; | |
68 | static const struct bt_packet *pub_src_stream1_packet1; | |
69 | static const struct bt_packet *pub_src_stream1_packet2; | |
70 | static const struct bt_packet *pub_src_stream2_packet1; | |
71 | static const struct bt_packet *pub_src_stream2_packet2; | |
223c70b2 PP |
72 | |
73 | enum { | |
74 | SEQ_END = -1, | |
75 | SEQ_STREAM1_BEGIN = -2, | |
76 | SEQ_STREAM2_BEGIN = -3, | |
77 | SEQ_STREAM1_END = -4, | |
78 | SEQ_STREAM2_END = -5, | |
79 | SEQ_STREAM1_PACKET1_BEGIN = -6, | |
80 | SEQ_STREAM1_PACKET2_BEGIN = -7, | |
81 | SEQ_STREAM2_PACKET1_BEGIN = -8, | |
82 | SEQ_STREAM2_PACKET2_BEGIN = -9, | |
83 | SEQ_STREAM1_PACKET1_END = -10, | |
84 | SEQ_STREAM1_PACKET2_END = -11, | |
85 | SEQ_STREAM2_PACKET1_END = -12, | |
86 | SEQ_STREAM2_PACKET2_END = -13, | |
87 | SEQ_EVENT_STREAM1_PACKET1 = -14, | |
88 | SEQ_EVENT_STREAM1_PACKET2 = -15, | |
89 | SEQ_EVENT_STREAM2_PACKET1 = -16, | |
90 | SEQ_EVENT_STREAM2_PACKET2 = -17, | |
223c70b2 PP |
91 | }; |
92 | ||
93 | struct src_iter_user_data { | |
94 | int64_t *seq; | |
95 | size_t at; | |
96 | }; | |
97 | ||
98 | struct sink_user_data { | |
d27a81b2 | 99 | void *notif_iter; |
223c70b2 PP |
100 | }; |
101 | ||
102 | /* | |
103 | * No automatic notifications generated in this block. | |
104 | * Stream 2 notifications are more indented. | |
105 | */ | |
106 | static int64_t seq_no_auto_notifs[] = { | |
107 | SEQ_STREAM1_BEGIN, | |
108 | SEQ_STREAM1_PACKET1_BEGIN, | |
109 | SEQ_EVENT_STREAM1_PACKET1, | |
110 | SEQ_EVENT_STREAM1_PACKET1, | |
111 | SEQ_STREAM2_BEGIN, | |
112 | SEQ_EVENT_STREAM1_PACKET1, | |
113 | SEQ_STREAM2_PACKET2_BEGIN, | |
114 | SEQ_EVENT_STREAM2_PACKET2, | |
115 | SEQ_EVENT_STREAM1_PACKET1, | |
116 | SEQ_STREAM1_PACKET1_END, | |
117 | SEQ_STREAM2_PACKET2_END, | |
118 | SEQ_STREAM1_PACKET2_BEGIN, | |
119 | SEQ_EVENT_STREAM1_PACKET2, | |
120 | SEQ_STREAM2_END, | |
121 | SEQ_STREAM1_PACKET2_END, | |
122 | SEQ_STREAM1_END, | |
123 | SEQ_END, | |
124 | }; | |
125 | ||
223c70b2 PP |
126 | static |
127 | void clear_test_events(void) | |
128 | { | |
129 | g_array_set_size(test_events, 0); | |
130 | } | |
131 | ||
132 | static | |
133 | void print_test_event(FILE *fp, const struct test_event *event) | |
134 | { | |
135 | fprintf(fp, "{ type = "); | |
136 | ||
137 | switch (event->type) { | |
138 | case TEST_EV_TYPE_NOTIF_UNEXPECTED: | |
139 | fprintf(fp, "TEST_EV_TYPE_NOTIF_UNEXPECTED"); | |
140 | break; | |
141 | case TEST_EV_TYPE_NOTIF_EVENT: | |
142 | fprintf(fp, "TEST_EV_TYPE_NOTIF_EVENT"); | |
143 | break; | |
223c70b2 PP |
144 | case TEST_EV_TYPE_NOTIF_STREAM_BEGIN: |
145 | fprintf(fp, "TEST_EV_TYPE_NOTIF_STREAM_BEGIN"); | |
146 | break; | |
147 | case TEST_EV_TYPE_NOTIF_STREAM_END: | |
148 | fprintf(fp, "TEST_EV_TYPE_NOTIF_STREAM_END"); | |
149 | break; | |
150 | case TEST_EV_TYPE_NOTIF_PACKET_BEGIN: | |
151 | fprintf(fp, "TEST_EV_TYPE_NOTIF_PACKET_BEGIN"); | |
152 | break; | |
153 | case TEST_EV_TYPE_NOTIF_PACKET_END: | |
154 | fprintf(fp, "TEST_EV_TYPE_NOTIF_PACKET_END"); | |
155 | break; | |
156 | case TEST_EV_TYPE_END: | |
157 | fprintf(fp, "TEST_EV_TYPE_END"); | |
158 | break; | |
159 | case TEST_EV_TYPE_SENTINEL: | |
160 | fprintf(fp, "TEST_EV_TYPE_SENTINEL"); | |
161 | break; | |
162 | default: | |
163 | fprintf(fp, "(UNKNOWN)"); | |
164 | break; | |
165 | } | |
166 | ||
167 | fprintf(fp, ", stream = %p, packet = %p }", event->stream, | |
168 | event->packet); | |
169 | } | |
170 | ||
171 | static | |
172 | void append_test_event(struct test_event *event) | |
173 | { | |
174 | g_array_append_val(test_events, *event); | |
175 | } | |
176 | ||
177 | static | |
178 | bool compare_single_test_events(const struct test_event *ev_a, | |
179 | const struct test_event *ev_b) | |
180 | { | |
181 | if (debug) { | |
182 | fprintf(stderr, ":: Comparing test events: "); | |
183 | print_test_event(stderr, ev_a); | |
184 | fprintf(stderr, " vs. "); | |
185 | print_test_event(stderr, ev_b); | |
186 | fprintf(stderr, "\n"); | |
187 | } | |
188 | ||
189 | if (ev_a->type != ev_b->type) { | |
190 | return false; | |
191 | } | |
192 | ||
193 | switch (ev_a->type) { | |
194 | case TEST_EV_TYPE_END: | |
195 | case TEST_EV_TYPE_SENTINEL: | |
196 | break; | |
197 | default: | |
198 | if (ev_a->stream != ev_b->stream) { | |
199 | return false; | |
200 | } | |
201 | ||
202 | if (ev_a->packet != ev_b->packet) { | |
203 | return false; | |
204 | } | |
205 | break; | |
206 | } | |
207 | ||
208 | return true; | |
209 | } | |
210 | ||
211 | static | |
212 | bool compare_test_events(const struct test_event *expected_events) | |
213 | { | |
214 | const struct test_event *expected_event = expected_events; | |
215 | size_t i = 0; | |
216 | ||
25583cd0 | 217 | BT_ASSERT(expected_events); |
223c70b2 PP |
218 | |
219 | while (true) { | |
220 | const struct test_event *event; | |
221 | ||
222 | if (expected_event->type == TEST_EV_TYPE_SENTINEL) { | |
223 | break; | |
224 | } | |
225 | ||
226 | if (i >= test_events->len) { | |
227 | return false; | |
228 | } | |
229 | ||
230 | event = &g_array_index(test_events, struct test_event, i); | |
231 | ||
232 | if (!compare_single_test_events(event, expected_event)) { | |
233 | return false; | |
234 | } | |
235 | ||
236 | i++; | |
237 | expected_event++; | |
238 | } | |
239 | ||
240 | if (i != test_events->len) { | |
241 | return false; | |
242 | } | |
243 | ||
244 | return true; | |
245 | } | |
246 | ||
247 | static | |
248 | void init_static_data(void) | |
249 | { | |
dadf29b9 | 250 | struct bt_trace *trace; |
223c70b2 PP |
251 | |
252 | /* Test events */ | |
253 | test_events = g_array_new(FALSE, TRUE, sizeof(struct test_event)); | |
25583cd0 | 254 | BT_ASSERT(test_events); |
223c70b2 PP |
255 | |
256 | /* Metadata */ | |
dadf29b9 | 257 | trace = bt_trace_create(); |
25583cd0 | 258 | BT_ASSERT(trace); |
dadf29b9 | 259 | src_stream_class = bt_stream_class_create(trace); |
25583cd0 | 260 | BT_ASSERT(src_stream_class); |
dadf29b9 | 261 | src_event_class = bt_event_class_create(src_stream_class); |
61f59358 | 262 | BT_ASSERT(src_event_class); |
dadf29b9 | 263 | src_stream1 = bt_stream_create(src_stream_class); |
25583cd0 | 264 | BT_ASSERT(src_stream1); |
dadf29b9 PP |
265 | pub_src_stream1 = src_stream1; |
266 | src_stream2 = bt_stream_create(src_stream_class); | |
25583cd0 | 267 | BT_ASSERT(src_stream2); |
dadf29b9 PP |
268 | pub_src_stream2 = src_stream2; |
269 | src_stream1_packet1 = bt_packet_create(src_stream1); | |
25583cd0 | 270 | BT_ASSERT(src_stream1_packet1); |
dadf29b9 PP |
271 | pub_src_stream1_packet1 = src_stream1_packet1; |
272 | src_stream1_packet2 = bt_packet_create(src_stream1); | |
25583cd0 | 273 | BT_ASSERT(src_stream1_packet2); |
dadf29b9 PP |
274 | pub_src_stream1_packet2 = src_stream1_packet2; |
275 | src_stream2_packet1 = bt_packet_create(src_stream2); | |
25583cd0 | 276 | BT_ASSERT(src_stream2_packet1); |
dadf29b9 PP |
277 | pub_src_stream2_packet1 = src_stream2_packet1; |
278 | src_stream2_packet2 = bt_packet_create(src_stream2); | |
25583cd0 | 279 | BT_ASSERT(src_stream2_packet2); |
dadf29b9 | 280 | pub_src_stream2_packet2 = src_stream2_packet2; |
223c70b2 PP |
281 | |
282 | if (debug) { | |
283 | fprintf(stderr, ":: stream 1: %p\n", src_stream1); | |
284 | fprintf(stderr, ":: stream 2: %p\n", src_stream2); | |
285 | fprintf(stderr, ":: stream 1, packet 1: %p\n", src_stream1_packet1); | |
286 | fprintf(stderr, ":: stream 1, packet 2: %p\n", src_stream1_packet2); | |
287 | fprintf(stderr, ":: stream 2, packet 1: %p\n", src_stream2_packet1); | |
288 | fprintf(stderr, ":: stream 2, packet 2: %p\n", src_stream2_packet2); | |
289 | } | |
290 | ||
e2317362 | 291 | bt_object_put_ref(trace); |
223c70b2 PP |
292 | } |
293 | ||
294 | static | |
295 | void fini_static_data(void) | |
296 | { | |
297 | /* Test events */ | |
298 | g_array_free(test_events, TRUE); | |
299 | ||
300 | /* Metadata */ | |
e2317362 PP |
301 | bt_object_put_ref(src_stream_class); |
302 | bt_object_put_ref(src_event_class); | |
303 | bt_object_put_ref(src_stream1); | |
304 | bt_object_put_ref(src_stream2); | |
305 | bt_object_put_ref(src_stream1_packet1); | |
306 | bt_object_put_ref(src_stream1_packet2); | |
307 | bt_object_put_ref(src_stream2_packet1); | |
308 | bt_object_put_ref(src_stream2_packet2); | |
223c70b2 PP |
309 | } |
310 | ||
311 | static | |
d27a81b2 | 312 | void src_iter_finalize(struct bt_self_notification_iterator *self_notif_iter) |
223c70b2 PP |
313 | { |
314 | struct src_iter_user_data *user_data = | |
d27a81b2 PP |
315 | bt_self_notification_iterator_get_data( |
316 | self_notif_iter); | |
223c70b2 PP |
317 | |
318 | if (user_data) { | |
319 | g_free(user_data); | |
320 | } | |
321 | } | |
322 | ||
323 | static | |
d27a81b2 PP |
324 | enum bt_self_notification_iterator_status src_iter_init( |
325 | struct bt_self_notification_iterator *self_notif_iter, | |
326 | struct bt_self_component_source *self_comp, | |
327 | struct bt_self_component_port_output *self_port) | |
223c70b2 PP |
328 | { |
329 | struct src_iter_user_data *user_data = | |
330 | g_new0(struct src_iter_user_data, 1); | |
223c70b2 | 331 | |
25583cd0 | 332 | BT_ASSERT(user_data); |
d27a81b2 | 333 | bt_self_notification_iterator_set_data(self_notif_iter, user_data); |
223c70b2 PP |
334 | |
335 | switch (current_test) { | |
336 | case TEST_NO_AUTO_NOTIFS: | |
e893886e | 337 | case TEST_OUTPUT_PORT_NOTIFICATION_ITERATOR: |
223c70b2 PP |
338 | user_data->seq = seq_no_auto_notifs; |
339 | break; | |
223c70b2 | 340 | default: |
0fbb9a9f | 341 | abort(); |
223c70b2 PP |
342 | } |
343 | ||
d27a81b2 | 344 | return BT_SELF_NOTIFICATION_ITERATOR_STATUS_OK; |
223c70b2 PP |
345 | } |
346 | ||
223c70b2 | 347 | static |
d27a81b2 PP |
348 | void src_iter_next_seq_one(struct bt_self_notification_iterator* notif_iter, |
349 | struct src_iter_user_data *user_data, | |
d4393e08 | 350 | struct bt_notification **notif) |
223c70b2 | 351 | { |
dadf29b9 | 352 | struct bt_packet *event_packet = NULL; |
223c70b2 | 353 | |
d4393e08 | 354 | switch (user_data->seq[user_data->at]) { |
223c70b2 | 355 | case SEQ_STREAM1_BEGIN: |
27c0f01f | 356 | *notif = bt_private_notification_as_notification( |
351946b4 | 357 | bt_private_notification_stream_begin_create( |
d27a81b2 | 358 | notif_iter, src_stream1)); |
223c70b2 PP |
359 | break; |
360 | case SEQ_STREAM2_BEGIN: | |
27c0f01f | 361 | *notif = bt_private_notification_as_notification( |
351946b4 | 362 | bt_private_notification_stream_begin_create( |
d27a81b2 | 363 | notif_iter, src_stream2)); |
223c70b2 PP |
364 | break; |
365 | case SEQ_STREAM1_END: | |
27c0f01f | 366 | *notif = bt_private_notification_as_notification( |
351946b4 | 367 | bt_private_notification_stream_end_create( |
d27a81b2 | 368 | notif_iter, src_stream1)); |
223c70b2 PP |
369 | break; |
370 | case SEQ_STREAM2_END: | |
27c0f01f | 371 | *notif = bt_private_notification_as_notification( |
351946b4 | 372 | bt_private_notification_stream_end_create( |
d27a81b2 | 373 | notif_iter, src_stream2)); |
223c70b2 PP |
374 | break; |
375 | case SEQ_STREAM1_PACKET1_BEGIN: | |
27c0f01f | 376 | *notif = bt_private_notification_as_notification( |
351946b4 | 377 | bt_private_notification_packet_begin_create( |
d27a81b2 | 378 | notif_iter, src_stream1_packet1)); |
223c70b2 PP |
379 | break; |
380 | case SEQ_STREAM1_PACKET2_BEGIN: | |
27c0f01f | 381 | *notif = bt_private_notification_as_notification( |
351946b4 | 382 | bt_private_notification_packet_begin_create( |
d27a81b2 | 383 | notif_iter, src_stream1_packet2)); |
223c70b2 PP |
384 | break; |
385 | case SEQ_STREAM2_PACKET1_BEGIN: | |
27c0f01f | 386 | *notif = bt_private_notification_as_notification( |
351946b4 | 387 | bt_private_notification_packet_begin_create( |
d27a81b2 | 388 | notif_iter, src_stream2_packet1)); |
223c70b2 PP |
389 | break; |
390 | case SEQ_STREAM2_PACKET2_BEGIN: | |
27c0f01f | 391 | *notif = bt_private_notification_as_notification( |
351946b4 | 392 | bt_private_notification_packet_begin_create( |
d27a81b2 | 393 | notif_iter, src_stream2_packet2)); |
223c70b2 PP |
394 | break; |
395 | case SEQ_STREAM1_PACKET1_END: | |
27c0f01f | 396 | *notif = bt_private_notification_as_notification( |
351946b4 | 397 | bt_private_notification_packet_end_create( |
d27a81b2 | 398 | notif_iter, src_stream1_packet1)); |
223c70b2 PP |
399 | break; |
400 | case SEQ_STREAM1_PACKET2_END: | |
27c0f01f | 401 | *notif = bt_private_notification_as_notification( |
351946b4 | 402 | bt_private_notification_packet_end_create( |
d27a81b2 | 403 | notif_iter, src_stream1_packet2)); |
223c70b2 PP |
404 | break; |
405 | case SEQ_STREAM2_PACKET1_END: | |
27c0f01f | 406 | *notif = bt_private_notification_as_notification( |
351946b4 | 407 | bt_private_notification_packet_end_create( |
d27a81b2 | 408 | notif_iter, src_stream2_packet1)); |
223c70b2 PP |
409 | break; |
410 | case SEQ_STREAM2_PACKET2_END: | |
27c0f01f | 411 | *notif = bt_private_notification_as_notification( |
351946b4 | 412 | bt_private_notification_packet_end_create( |
d27a81b2 | 413 | notif_iter, src_stream2_packet2)); |
223c70b2 PP |
414 | break; |
415 | case SEQ_EVENT_STREAM1_PACKET1: | |
416 | event_packet = src_stream1_packet1; | |
417 | break; | |
418 | case SEQ_EVENT_STREAM1_PACKET2: | |
419 | event_packet = src_stream1_packet2; | |
420 | break; | |
421 | case SEQ_EVENT_STREAM2_PACKET1: | |
422 | event_packet = src_stream2_packet1; | |
423 | break; | |
424 | case SEQ_EVENT_STREAM2_PACKET2: | |
425 | event_packet = src_stream2_packet2; | |
426 | break; | |
427 | default: | |
0fbb9a9f | 428 | abort(); |
223c70b2 PP |
429 | } |
430 | ||
431 | if (event_packet) { | |
27c0f01f | 432 | *notif = bt_private_notification_as_notification( |
351946b4 | 433 | bt_private_notification_event_create( |
d27a81b2 | 434 | notif_iter, src_event_class, event_packet)); |
d4393e08 PP |
435 | } |
436 | ||
437 | BT_ASSERT(*notif); | |
438 | user_data->at++; | |
439 | } | |
440 | ||
441 | static | |
d27a81b2 PP |
442 | enum bt_self_notification_iterator_status src_iter_next_seq( |
443 | struct bt_self_notification_iterator *notif_iter, | |
d4393e08 PP |
444 | struct src_iter_user_data *user_data, |
445 | bt_notification_array notifs, uint64_t capacity, | |
446 | uint64_t *count) | |
447 | { | |
d27a81b2 PP |
448 | enum bt_self_notification_iterator_status status = |
449 | BT_SELF_NOTIFICATION_ITERATOR_STATUS_OK; | |
d4393e08 PP |
450 | uint64_t i = 0; |
451 | ||
452 | BT_ASSERT(user_data->seq); | |
453 | ||
454 | if (user_data->seq[user_data->at] == SEQ_END) { | |
d27a81b2 | 455 | status = BT_SELF_NOTIFICATION_ITERATOR_STATUS_END; |
d4393e08 | 456 | goto end; |
223c70b2 PP |
457 | } |
458 | ||
d4393e08 | 459 | while (i < capacity && user_data->seq[user_data->at] != SEQ_END) { |
d27a81b2 | 460 | src_iter_next_seq_one(notif_iter, user_data, ¬ifs[i]); |
d4393e08 | 461 | i++; |
223c70b2 PP |
462 | } |
463 | ||
d4393e08 PP |
464 | BT_ASSERT(i > 0 && i <= capacity); |
465 | *count = i; | |
466 | ||
467 | end: | |
468 | return status; | |
223c70b2 PP |
469 | } |
470 | ||
471 | static | |
d27a81b2 PP |
472 | enum bt_self_notification_iterator_status src_iter_next( |
473 | struct bt_self_notification_iterator *self_notif_iter, | |
d4393e08 PP |
474 | bt_notification_array notifs, uint64_t capacity, |
475 | uint64_t *count) | |
223c70b2 | 476 | { |
223c70b2 | 477 | struct src_iter_user_data *user_data = |
d27a81b2 | 478 | bt_self_notification_iterator_get_data(self_notif_iter); |
223c70b2 | 479 | |
25583cd0 | 480 | BT_ASSERT(user_data); |
d27a81b2 PP |
481 | return src_iter_next_seq(self_notif_iter, user_data, notifs, |
482 | capacity, count); | |
223c70b2 PP |
483 | } |
484 | ||
485 | static | |
d27a81b2 PP |
486 | enum bt_self_component_status src_init( |
487 | struct bt_self_component_source *self_comp, | |
82fa4539 | 488 | const struct bt_value *params, void *init_method_data) |
223c70b2 | 489 | { |
147337a3 | 490 | int ret; |
b9d103be | 491 | |
d27a81b2 PP |
492 | ret = bt_self_component_source_add_output_port( |
493 | self_comp, "out", NULL, NULL); | |
25583cd0 | 494 | BT_ASSERT(ret == 0); |
d27a81b2 | 495 | return BT_SELF_COMPONENT_STATUS_OK; |
223c70b2 PP |
496 | } |
497 | ||
498 | static | |
d27a81b2 | 499 | void src_finalize(struct bt_self_component_source *self_comp) |
223c70b2 PP |
500 | { |
501 | } | |
502 | ||
503 | static | |
d4393e08 | 504 | void append_test_events_from_notification(struct bt_notification *notification) |
223c70b2 | 505 | { |
223c70b2 | 506 | struct test_event test_event = { 0 }; |
223c70b2 PP |
507 | |
508 | switch (bt_notification_get_type(notification)) { | |
509 | case BT_NOTIFICATION_TYPE_EVENT: | |
510 | { | |
dadf29b9 | 511 | const struct bt_event *event; |
223c70b2 PP |
512 | |
513 | test_event.type = TEST_EV_TYPE_NOTIF_EVENT; | |
312c056a | 514 | event = bt_notification_event_borrow_event(notification); |
25583cd0 | 515 | BT_ASSERT(event); |
dadf29b9 | 516 | test_event.packet = bt_event_borrow_packet_const(event); |
25583cd0 | 517 | BT_ASSERT(test_event.packet); |
223c70b2 PP |
518 | break; |
519 | } | |
223c70b2 PP |
520 | case BT_NOTIFICATION_TYPE_STREAM_BEGIN: |
521 | test_event.type = TEST_EV_TYPE_NOTIF_STREAM_BEGIN; | |
522 | test_event.stream = | |
dadf29b9 PP |
523 | bt_notification_stream_begin_borrow_stream( |
524 | notification); | |
25583cd0 | 525 | BT_ASSERT(test_event.stream); |
223c70b2 PP |
526 | break; |
527 | case BT_NOTIFICATION_TYPE_STREAM_END: | |
528 | test_event.type = TEST_EV_TYPE_NOTIF_STREAM_END; | |
529 | test_event.stream = | |
312c056a | 530 | bt_notification_stream_end_borrow_stream(notification); |
25583cd0 | 531 | BT_ASSERT(test_event.stream); |
223c70b2 PP |
532 | break; |
533 | case BT_NOTIFICATION_TYPE_PACKET_BEGIN: | |
534 | test_event.type = TEST_EV_TYPE_NOTIF_PACKET_BEGIN; | |
535 | test_event.packet = | |
dadf29b9 PP |
536 | bt_notification_packet_begin_borrow_packet( |
537 | notification); | |
25583cd0 | 538 | BT_ASSERT(test_event.packet); |
223c70b2 PP |
539 | break; |
540 | case BT_NOTIFICATION_TYPE_PACKET_END: | |
541 | test_event.type = TEST_EV_TYPE_NOTIF_PACKET_END; | |
542 | test_event.packet = | |
312c056a | 543 | bt_notification_packet_end_borrow_packet(notification); |
25583cd0 | 544 | BT_ASSERT(test_event.packet); |
223c70b2 PP |
545 | break; |
546 | default: | |
547 | test_event.type = TEST_EV_TYPE_NOTIF_UNEXPECTED; | |
548 | break; | |
549 | } | |
550 | ||
551 | if (test_event.packet) { | |
dadf29b9 PP |
552 | test_event.stream = bt_packet_borrow_stream_const( |
553 | test_event.packet); | |
25583cd0 | 554 | BT_ASSERT(test_event.stream); |
223c70b2 PP |
555 | } |
556 | ||
d4393e08 PP |
557 | append_test_event(&test_event); |
558 | } | |
559 | ||
560 | static | |
561 | enum bt_notification_iterator_status common_consume( | |
d27a81b2 | 562 | void *notif_iter, bool is_output_port_notif_iter) |
d4393e08 PP |
563 | { |
564 | enum bt_notification_iterator_status ret; | |
565 | bt_notification_array notifications = NULL; | |
566 | uint64_t count = 0; | |
567 | struct test_event test_event = { 0 }; | |
568 | uint64_t i; | |
569 | ||
570 | BT_ASSERT(notif_iter); | |
571 | ||
572 | if (is_output_port_notif_iter) { | |
d27a81b2 | 573 | ret = bt_port_output_notification_iterator_next(notif_iter, |
d4393e08 PP |
574 | ¬ifications, &count); |
575 | } else { | |
d27a81b2 | 576 | ret = bt_self_component_port_input_notification_iterator_next( |
d4393e08 PP |
577 | notif_iter, ¬ifications, &count); |
578 | } | |
579 | ||
580 | if (ret < 0) { | |
581 | goto end; | |
582 | } | |
583 | ||
584 | switch (ret) { | |
585 | case BT_NOTIFICATION_ITERATOR_STATUS_END: | |
586 | test_event.type = TEST_EV_TYPE_END; | |
223c70b2 | 587 | append_test_event(&test_event); |
d4393e08 PP |
588 | goto end; |
589 | case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN: | |
590 | abort(); | |
591 | default: | |
592 | break; | |
223c70b2 PP |
593 | } |
594 | ||
d4393e08 PP |
595 | BT_ASSERT(notifications); |
596 | BT_ASSERT(count > 0); | |
597 | ||
598 | for (i = 0; i < count; i++) { | |
599 | append_test_events_from_notification(notifications[i]); | |
e2317362 | 600 | bt_object_put_ref(notifications[i]); |
d4393e08 PP |
601 | } |
602 | ||
603 | end: | |
223c70b2 PP |
604 | return ret; |
605 | } | |
606 | ||
e893886e | 607 | static |
d27a81b2 PP |
608 | enum bt_self_component_status sink_consume( |
609 | struct bt_self_component_sink *self_comp) | |
e893886e | 610 | { |
d27a81b2 | 611 | enum bt_self_component_status ret = BT_SELF_COMPONENT_STATUS_OK; |
e893886e | 612 | struct sink_user_data *user_data = |
d27a81b2 | 613 | bt_self_component_get_data( |
27c0f01f | 614 | bt_self_component_sink_as_self_component( |
d27a81b2 | 615 | self_comp)); |
e893886e PP |
616 | enum bt_notification_iterator_status it_ret; |
617 | ||
25583cd0 | 618 | BT_ASSERT(user_data && user_data->notif_iter); |
07245ac2 | 619 | it_ret = common_consume(user_data->notif_iter, false); |
e893886e PP |
620 | |
621 | if (it_ret < 0) { | |
d27a81b2 | 622 | ret = BT_SELF_COMPONENT_STATUS_ERROR; |
e893886e PP |
623 | goto end; |
624 | } | |
625 | ||
626 | switch (it_ret) { | |
627 | case BT_NOTIFICATION_ITERATOR_STATUS_END: | |
d27a81b2 | 628 | ret = BT_SELF_COMPONENT_STATUS_END; |
e2317362 | 629 | BT_OBJECT_PUT_REF_AND_RESET(user_data->notif_iter); |
e893886e PP |
630 | goto end; |
631 | case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN: | |
632 | abort(); | |
633 | default: | |
634 | break; | |
635 | } | |
636 | ||
637 | end: | |
638 | return ret; | |
639 | } | |
640 | ||
223c70b2 | 641 | static |
d27a81b2 PP |
642 | enum bt_self_component_status sink_port_connected( |
643 | struct bt_self_component_sink *self_comp, | |
644 | struct bt_self_component_port_input *self_port, | |
645 | struct bt_port_output *other_port) | |
223c70b2 | 646 | { |
d27a81b2 PP |
647 | struct sink_user_data *user_data = |
648 | bt_self_component_get_data( | |
27c0f01f | 649 | bt_self_component_sink_as_self_component( |
d27a81b2 | 650 | self_comp)); |
223c70b2 | 651 | |
25583cd0 | 652 | BT_ASSERT(user_data); |
d27a81b2 PP |
653 | user_data->notif_iter = |
654 | bt_self_component_port_input_notification_iterator_create( | |
655 | self_port); | |
656 | return BT_SELF_COMPONENT_STATUS_OK; | |
223c70b2 PP |
657 | } |
658 | ||
659 | static | |
d27a81b2 PP |
660 | enum bt_self_component_status sink_init( |
661 | struct bt_self_component_sink *self_comp, | |
82fa4539 | 662 | const struct bt_value *params, void *init_method_data) |
223c70b2 PP |
663 | { |
664 | struct sink_user_data *user_data = g_new0(struct sink_user_data, 1); | |
665 | int ret; | |
666 | ||
25583cd0 | 667 | BT_ASSERT(user_data); |
d27a81b2 | 668 | bt_self_component_set_data( |
27c0f01f | 669 | bt_self_component_sink_as_self_component(self_comp), |
223c70b2 | 670 | user_data); |
d27a81b2 PP |
671 | ret = bt_self_component_sink_add_input_port( |
672 | self_comp, "in", NULL, NULL); | |
25583cd0 | 673 | BT_ASSERT(ret == 0); |
d27a81b2 | 674 | return BT_SELF_COMPONENT_STATUS_OK; |
223c70b2 PP |
675 | } |
676 | ||
677 | static | |
d27a81b2 | 678 | void sink_finalize(struct bt_self_component_sink *self_comp) |
223c70b2 | 679 | { |
d27a81b2 PP |
680 | struct sink_user_data *user_data = |
681 | bt_self_component_get_data( | |
27c0f01f | 682 | bt_self_component_sink_as_self_component( |
d27a81b2 | 683 | self_comp)); |
223c70b2 PP |
684 | |
685 | if (user_data) { | |
e2317362 | 686 | bt_object_put_ref(user_data->notif_iter); |
223c70b2 PP |
687 | g_free(user_data); |
688 | } | |
689 | } | |
690 | ||
691 | static | |
d27a81b2 PP |
692 | void create_source_sink(struct bt_private_graph *graph, |
693 | struct bt_component_source **source, | |
694 | struct bt_component_sink **sink) | |
223c70b2 | 695 | { |
d27a81b2 PP |
696 | struct bt_private_component_class_source *src_comp_class; |
697 | struct bt_private_component_class_sink *sink_comp_class; | |
223c70b2 PP |
698 | int ret; |
699 | ||
700 | /* Create source component */ | |
e893886e | 701 | if (source) { |
d27a81b2 | 702 | src_comp_class = bt_private_component_class_source_create("src", |
e893886e | 703 | src_iter_next); |
25583cd0 | 704 | BT_ASSERT(src_comp_class); |
d27a81b2 PP |
705 | ret = bt_private_component_class_source_set_init_method( |
706 | src_comp_class, src_init); | |
25583cd0 | 707 | BT_ASSERT(ret == 0); |
d27a81b2 PP |
708 | ret = bt_private_component_class_source_set_finalize_method( |
709 | src_comp_class, src_finalize); | |
25583cd0 | 710 | BT_ASSERT(ret == 0); |
d27a81b2 | 711 | ret = bt_private_component_class_source_set_notification_iterator_init_method( |
e893886e | 712 | src_comp_class, src_iter_init); |
25583cd0 | 713 | BT_ASSERT(ret == 0); |
d27a81b2 | 714 | ret = bt_private_component_class_source_set_notification_iterator_finalize_method( |
e893886e | 715 | src_comp_class, src_iter_finalize); |
25583cd0 | 716 | BT_ASSERT(ret == 0); |
d27a81b2 | 717 | ret = bt_private_graph_add_source_component(graph, |
27c0f01f | 718 | bt_private_component_class_source_as_component_class_source( |
d27a81b2 | 719 | src_comp_class), "source", NULL, source); |
25583cd0 | 720 | BT_ASSERT(ret == 0); |
e2317362 | 721 | bt_object_put_ref(src_comp_class); |
e893886e | 722 | } |
223c70b2 PP |
723 | |
724 | /* Create sink component */ | |
e893886e | 725 | if (sink) { |
d27a81b2 | 726 | sink_comp_class = bt_private_component_class_sink_create("sink", |
e893886e | 727 | sink_consume); |
25583cd0 | 728 | BT_ASSERT(sink_comp_class); |
d27a81b2 PP |
729 | ret = bt_private_component_class_sink_set_init_method( |
730 | sink_comp_class, sink_init); | |
25583cd0 | 731 | BT_ASSERT(ret == 0); |
d27a81b2 PP |
732 | ret = bt_private_component_class_sink_set_finalize_method( |
733 | sink_comp_class, sink_finalize); | |
734 | ret = bt_private_component_class_sink_set_input_port_connected_method( | |
e893886e | 735 | sink_comp_class, sink_port_connected); |
25583cd0 | 736 | BT_ASSERT(ret == 0); |
d27a81b2 | 737 | ret = bt_private_graph_add_sink_component(graph, |
27c0f01f | 738 | bt_private_component_class_sink_as_component_class_sink( |
d27a81b2 PP |
739 | sink_comp_class), |
740 | "sink", NULL, sink); | |
25583cd0 | 741 | BT_ASSERT(ret == 0); |
e2317362 | 742 | bt_object_put_ref(sink_comp_class); |
e893886e | 743 | } |
223c70b2 PP |
744 | } |
745 | ||
746 | static | |
747 | void do_std_test(enum test test, const char *name, | |
748 | const struct test_event *expected_test_events) | |
749 | { | |
d27a81b2 PP |
750 | struct bt_component_source *src_comp; |
751 | struct bt_component_sink *sink_comp; | |
752 | struct bt_port_output *upstream_port; | |
753 | struct bt_port_input *downstream_port; | |
223c70b2 PP |
754 | enum bt_graph_status graph_status = BT_GRAPH_STATUS_OK; |
755 | ||
756 | clear_test_events(); | |
757 | current_test = test; | |
758 | diag("test: %s", name); | |
25583cd0 | 759 | BT_ASSERT(!graph); |
80a256b6 | 760 | graph = bt_private_graph_create(); |
25583cd0 | 761 | BT_ASSERT(graph); |
36712f1d | 762 | create_source_sink(graph, &src_comp, &sink_comp); |
223c70b2 PP |
763 | |
764 | /* Connect source to sink */ | |
d27a81b2 PP |
765 | upstream_port = bt_component_source_borrow_output_port_by_name( |
766 | src_comp, "out"); | |
25583cd0 | 767 | BT_ASSERT(upstream_port); |
d27a81b2 PP |
768 | downstream_port = bt_component_sink_borrow_input_port_by_name( |
769 | sink_comp, "in"); | |
25583cd0 | 770 | BT_ASSERT(downstream_port); |
80a256b6 | 771 | graph_status = bt_private_graph_connect_ports(graph, upstream_port, |
a256a42d | 772 | downstream_port, NULL); |
223c70b2 PP |
773 | |
774 | /* Run the graph until the end */ | |
775 | while (graph_status == BT_GRAPH_STATUS_OK || | |
776 | graph_status == BT_GRAPH_STATUS_AGAIN) { | |
80a256b6 | 777 | graph_status = bt_private_graph_run(graph); |
223c70b2 PP |
778 | } |
779 | ||
d27a81b2 PP |
780 | ok(graph_status == BT_GRAPH_STATUS_END, |
781 | "graph finishes without any error"); | |
223c70b2 PP |
782 | |
783 | /* Compare the resulting test events */ | |
784 | if (expected_test_events) { | |
785 | ok(compare_test_events(expected_test_events), | |
786 | "the produced sequence of test events is the expected one"); | |
787 | } | |
788 | ||
e2317362 PP |
789 | bt_object_put_ref(src_comp); |
790 | bt_object_put_ref(sink_comp); | |
791 | BT_OBJECT_PUT_REF_AND_RESET(graph); | |
223c70b2 PP |
792 | } |
793 | ||
794 | static | |
795 | void test_no_auto_notifs(void) | |
796 | { | |
797 | const struct test_event expected_test_events[] = { | |
351946b4 PP |
798 | { .type = TEST_EV_TYPE_NOTIF_STREAM_BEGIN, .stream = pub_src_stream1, .packet = NULL, }, |
799 | { .type = TEST_EV_TYPE_NOTIF_PACKET_BEGIN, .stream = pub_src_stream1, .packet = pub_src_stream1_packet1, }, | |
800 | { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = pub_src_stream1, .packet = pub_src_stream1_packet1, }, | |
801 | { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = pub_src_stream1, .packet = pub_src_stream1_packet1, }, | |
802 | { .type = TEST_EV_TYPE_NOTIF_STREAM_BEGIN, .stream = pub_src_stream2, .packet = NULL, }, | |
803 | { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = pub_src_stream1, .packet = pub_src_stream1_packet1, }, | |
804 | { .type = TEST_EV_TYPE_NOTIF_PACKET_BEGIN, .stream = pub_src_stream2, .packet = pub_src_stream2_packet2, }, | |
805 | { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = pub_src_stream2, .packet = pub_src_stream2_packet2, }, | |
806 | { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = pub_src_stream1, .packet = pub_src_stream1_packet1, }, | |
807 | { .type = TEST_EV_TYPE_NOTIF_PACKET_END, .stream = pub_src_stream1, .packet = pub_src_stream1_packet1, }, | |
808 | { .type = TEST_EV_TYPE_NOTIF_PACKET_END, .stream = pub_src_stream2, .packet = pub_src_stream2_packet2, }, | |
809 | { .type = TEST_EV_TYPE_NOTIF_PACKET_BEGIN, .stream = pub_src_stream1, .packet = pub_src_stream1_packet2, }, | |
810 | { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = pub_src_stream1, .packet = pub_src_stream1_packet2, }, | |
811 | { .type = TEST_EV_TYPE_NOTIF_STREAM_END, .stream = pub_src_stream2, .packet = NULL, }, | |
812 | { .type = TEST_EV_TYPE_NOTIF_PACKET_END, .stream = pub_src_stream1, .packet = pub_src_stream1_packet2, }, | |
813 | { .type = TEST_EV_TYPE_NOTIF_STREAM_END, .stream = pub_src_stream1, .packet = NULL, }, | |
223c70b2 PP |
814 | { .type = TEST_EV_TYPE_END, }, |
815 | { .type = TEST_EV_TYPE_SENTINEL, }, | |
816 | }; | |
817 | ||
818 | do_std_test(TEST_NO_AUTO_NOTIFS, "no automatic notifications", | |
819 | expected_test_events); | |
820 | } | |
821 | ||
e893886e PP |
822 | static |
823 | void test_output_port_notification_iterator(void) | |
824 | { | |
825 | const struct test_event expected_test_events[] = { | |
351946b4 PP |
826 | { .type = TEST_EV_TYPE_NOTIF_STREAM_BEGIN, .stream = pub_src_stream1, .packet = NULL, }, |
827 | { .type = TEST_EV_TYPE_NOTIF_PACKET_BEGIN, .stream = pub_src_stream1, .packet = pub_src_stream1_packet1, }, | |
828 | { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = pub_src_stream1, .packet = pub_src_stream1_packet1, }, | |
829 | { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = pub_src_stream1, .packet = pub_src_stream1_packet1, }, | |
830 | { .type = TEST_EV_TYPE_NOTIF_STREAM_BEGIN, .stream = pub_src_stream2, .packet = NULL, }, | |
831 | { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = pub_src_stream1, .packet = pub_src_stream1_packet1, }, | |
832 | { .type = TEST_EV_TYPE_NOTIF_PACKET_BEGIN, .stream = pub_src_stream2, .packet = pub_src_stream2_packet2, }, | |
833 | { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = pub_src_stream2, .packet = pub_src_stream2_packet2, }, | |
834 | { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = pub_src_stream1, .packet = pub_src_stream1_packet1, }, | |
835 | { .type = TEST_EV_TYPE_NOTIF_PACKET_END, .stream = pub_src_stream1, .packet = pub_src_stream1_packet1, }, | |
836 | { .type = TEST_EV_TYPE_NOTIF_PACKET_END, .stream = pub_src_stream2, .packet = pub_src_stream2_packet2, }, | |
837 | { .type = TEST_EV_TYPE_NOTIF_PACKET_BEGIN, .stream = pub_src_stream1, .packet = pub_src_stream1_packet2, }, | |
838 | { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = pub_src_stream1, .packet = pub_src_stream1_packet2, }, | |
839 | { .type = TEST_EV_TYPE_NOTIF_STREAM_END, .stream = pub_src_stream2, .packet = NULL, }, | |
840 | { .type = TEST_EV_TYPE_NOTIF_PACKET_END, .stream = pub_src_stream1, .packet = pub_src_stream1_packet2, }, | |
841 | { .type = TEST_EV_TYPE_NOTIF_STREAM_END, .stream = pub_src_stream1, .packet = NULL, }, | |
e893886e PP |
842 | { .type = TEST_EV_TYPE_END, }, |
843 | { .type = TEST_EV_TYPE_SENTINEL, }, | |
844 | }; | |
d27a81b2 PP |
845 | struct bt_component_source *src_comp; |
846 | struct bt_port_output_notification_iterator *notif_iter; | |
e893886e PP |
847 | enum bt_notification_iterator_status iter_status = |
848 | BT_NOTIFICATION_ITERATOR_STATUS_OK; | |
d27a81b2 | 849 | struct bt_port_output *upstream_port; |
e893886e PP |
850 | |
851 | clear_test_events(); | |
852 | current_test = TEST_OUTPUT_PORT_NOTIFICATION_ITERATOR; | |
853 | diag("test: output port notification iterator"); | |
25583cd0 | 854 | BT_ASSERT(!graph); |
80a256b6 | 855 | graph = bt_private_graph_create(); |
25583cd0 | 856 | BT_ASSERT(graph); |
e893886e PP |
857 | create_source_sink(graph, &src_comp, NULL); |
858 | ||
859 | /* Create notification iterator on source's output port */ | |
d27a81b2 PP |
860 | upstream_port = bt_component_source_borrow_output_port_by_name( |
861 | src_comp, "out"); | |
862 | notif_iter = bt_port_output_notification_iterator_create(graph, | |
badaa238 | 863 | upstream_port); |
351946b4 | 864 | ok(notif_iter, "bt_private_output_port_notification_iterator_create() succeeds"); |
e893886e PP |
865 | |
866 | /* Consume the notification iterator */ | |
867 | while (iter_status == BT_NOTIFICATION_ITERATOR_STATUS_OK) { | |
07245ac2 | 868 | iter_status = common_consume(notif_iter, true); |
e893886e PP |
869 | } |
870 | ||
871 | ok(iter_status == BT_NOTIFICATION_ITERATOR_STATUS_END, | |
872 | "output port notification iterator finishes without any error"); | |
873 | ||
874 | /* Compare the resulting test events */ | |
875 | ok(compare_test_events(expected_test_events), | |
876 | "the produced sequence of test events is the expected one"); | |
877 | ||
e2317362 PP |
878 | bt_object_put_ref(src_comp); |
879 | BT_OBJECT_PUT_REF_AND_RESET(graph); | |
880 | bt_object_put_ref(notif_iter); | |
e893886e PP |
881 | } |
882 | ||
223c70b2 PP |
883 | #define DEBUG_ENV_VAR "TEST_BT_NOTIFICATION_ITERATOR_DEBUG" |
884 | ||
885 | int main(int argc, char **argv) | |
886 | { | |
887 | if (getenv(DEBUG_ENV_VAR) && strcmp(getenv(DEBUG_ENV_VAR), "1") == 0) { | |
888 | debug = true; | |
889 | } | |
890 | ||
891 | plan_tests(NR_TESTS); | |
892 | init_static_data(); | |
893 | test_no_auto_notifs(); | |
e893886e | 894 | test_output_port_notification_iterator(); |
223c70b2 PP |
895 | fini_static_data(); |
896 | return exit_status(); | |
897 | } |