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