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; | |
b19ff26f PP |
50 | const bt_stream *stream; |
51 | const 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; | |
b19ff26f PP |
57 | static bt_graph *graph; |
58 | static bt_stream_class *src_stream_class; | |
59 | static bt_event_class *src_event_class; | |
60 | static bt_stream *src_stream1; | |
61 | static bt_stream *src_stream2; | |
62 | static bt_packet *src_stream1_packet1; | |
63 | static bt_packet *src_stream1_packet2; | |
64 | static bt_packet *src_stream2_packet1; | |
65 | static 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 { | |
b19ff26f | 93 | bt_self_component_port_input_notification_iterator *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 | { | |
b19ff26f PP |
244 | bt_trace_class *trace_class; |
245 | 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 | ||
c5b9b441 PP |
282 | bt_trace_put_ref(trace); |
283 | bt_trace_class_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 */ | |
c5b9b441 PP |
293 | bt_stream_class_put_ref(src_stream_class); |
294 | bt_event_class_put_ref(src_event_class); | |
295 | bt_stream_put_ref(src_stream1); | |
296 | bt_stream_put_ref(src_stream2); | |
297 | bt_packet_put_ref(src_stream1_packet1); | |
298 | bt_packet_put_ref(src_stream1_packet2); | |
299 | bt_packet_put_ref(src_stream2_packet1); | |
300 | bt_packet_put_ref(src_stream2_packet2); | |
223c70b2 PP |
301 | } |
302 | ||
303 | static | |
b19ff26f | 304 | void src_iter_finalize(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 | 316 | enum bt_self_notification_iterator_status src_iter_init( |
b19ff26f PP |
317 | bt_self_notification_iterator *self_notif_iter, |
318 | bt_self_component_source *self_comp, | |
319 | 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 |
b19ff26f | 340 | void src_iter_next_seq_one(bt_self_notification_iterator* notif_iter, |
d94d92ac | 341 | struct src_iter_user_data *user_data, |
b19ff26f | 342 | const bt_notification **notif) |
223c70b2 | 343 | { |
b19ff26f | 344 | bt_packet *event_packet = NULL; |
223c70b2 | 345 | |
d4393e08 | 346 | switch (user_data->seq[user_data->at]) { |
223c70b2 | 347 | case SEQ_STREAM1_BEGIN: |
bb5bb160 | 348 | *notif = bt_notification_stream_beginning_create(notif_iter, |
0d72b8c3 | 349 | src_stream1); |
223c70b2 PP |
350 | break; |
351 | case SEQ_STREAM2_BEGIN: | |
bb5bb160 | 352 | *notif = bt_notification_stream_beginning_create(notif_iter, |
0d72b8c3 | 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: | |
bb5bb160 | 364 | *notif = bt_notification_packet_beginning_create(notif_iter, |
0d72b8c3 | 365 | src_stream1_packet1); |
223c70b2 PP |
366 | break; |
367 | case SEQ_STREAM1_PACKET2_BEGIN: | |
bb5bb160 | 368 | *notif = bt_notification_packet_beginning_create(notif_iter, |
0d72b8c3 | 369 | src_stream1_packet2); |
223c70b2 PP |
370 | break; |
371 | case SEQ_STREAM2_PACKET1_BEGIN: | |
bb5bb160 | 372 | *notif = bt_notification_packet_beginning_create(notif_iter, |
0d72b8c3 | 373 | src_stream2_packet1); |
223c70b2 PP |
374 | break; |
375 | case SEQ_STREAM2_PACKET2_BEGIN: | |
bb5bb160 | 376 | *notif = bt_notification_packet_beginning_create(notif_iter, |
0d72b8c3 | 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 | 422 | enum bt_self_notification_iterator_status src_iter_next_seq( |
b19ff26f | 423 | 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 | 452 | enum bt_self_notification_iterator_status src_iter_next( |
b19ff26f | 453 | 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 | 466 | enum bt_self_component_status src_init( |
b19ff26f PP |
467 | bt_self_component_source *self_comp, |
468 | const 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 | |
b19ff26f | 479 | void src_finalize(bt_self_component_source *self_comp) |
223c70b2 PP |
480 | { |
481 | } | |
482 | ||
483 | static | |
b19ff26f | 484 | void append_test_events_from_notification(const 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 | { | |
b19ff26f | 491 | const 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 | } | |
bb5bb160 | 500 | case BT_NOTIFICATION_TYPE_STREAM_BEGINNING: |
223c70b2 PP |
501 | test_event.type = TEST_EV_TYPE_NOTIF_STREAM_BEGIN; |
502 | test_event.stream = | |
bb5bb160 | 503 | bt_notification_stream_beginning_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 | 511 | break; |
bb5bb160 | 512 | case BT_NOTIFICATION_TYPE_PACKET_BEGINNING: |
223c70b2 PP |
513 | test_event.type = TEST_EV_TYPE_NOTIF_PACKET_BEGIN; |
514 | test_event.packet = | |
bb5bb160 | 515 | bt_notification_packet_beginning_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]); | |
c5b9b441 | 578 | bt_notification_put_ref(notifications[i]); |
d4393e08 PP |
579 | } |
580 | ||
581 | end: | |
223c70b2 PP |
582 | return ret; |
583 | } | |
584 | ||
e893886e | 585 | static |
d94d92ac | 586 | enum bt_self_component_status sink_consume( |
b19ff26f | 587 | 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; |
c5b9b441 PP |
607 | BT_SELF_COMPONENT_PORT_INPUT_NOTIFICATION_ITERATOR_PUT_REF_AND_RESET( |
608 | user_data->notif_iter); | |
e893886e PP |
609 | goto end; |
610 | case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN: | |
611 | abort(); | |
612 | default: | |
613 | break; | |
614 | } | |
615 | ||
616 | end: | |
617 | return ret; | |
618 | } | |
619 | ||
223c70b2 | 620 | static |
d94d92ac | 621 | enum bt_self_component_status sink_port_connected( |
b19ff26f PP |
622 | bt_self_component_sink *self_comp, |
623 | bt_self_component_port_input *self_port, | |
624 | const bt_port_output *other_port) | |
223c70b2 | 625 | { |
d94d92ac PP |
626 | struct sink_user_data *user_data = |
627 | bt_self_component_get_data( | |
707b7d35 | 628 | bt_self_component_sink_as_self_component( |
d94d92ac | 629 | self_comp)); |
223c70b2 | 630 | |
25583cd0 | 631 | BT_ASSERT(user_data); |
d94d92ac PP |
632 | user_data->notif_iter = |
633 | bt_self_component_port_input_notification_iterator_create( | |
634 | self_port); | |
635 | return BT_SELF_COMPONENT_STATUS_OK; | |
223c70b2 PP |
636 | } |
637 | ||
638 | static | |
d94d92ac | 639 | enum bt_self_component_status sink_init( |
b19ff26f PP |
640 | bt_self_component_sink *self_comp, |
641 | const bt_value *params, void *init_method_data) | |
223c70b2 PP |
642 | { |
643 | struct sink_user_data *user_data = g_new0(struct sink_user_data, 1); | |
644 | int ret; | |
645 | ||
25583cd0 | 646 | BT_ASSERT(user_data); |
d94d92ac | 647 | bt_self_component_set_data( |
707b7d35 | 648 | bt_self_component_sink_as_self_component(self_comp), |
223c70b2 | 649 | user_data); |
d94d92ac PP |
650 | ret = bt_self_component_sink_add_input_port( |
651 | self_comp, "in", NULL, NULL); | |
25583cd0 | 652 | BT_ASSERT(ret == 0); |
d94d92ac | 653 | return BT_SELF_COMPONENT_STATUS_OK; |
223c70b2 PP |
654 | } |
655 | ||
656 | static | |
b19ff26f | 657 | void sink_finalize(bt_self_component_sink *self_comp) |
223c70b2 | 658 | { |
d94d92ac PP |
659 | struct sink_user_data *user_data = |
660 | bt_self_component_get_data( | |
707b7d35 | 661 | bt_self_component_sink_as_self_component( |
d94d92ac | 662 | self_comp)); |
223c70b2 PP |
663 | |
664 | if (user_data) { | |
c5b9b441 PP |
665 | BT_SELF_COMPONENT_PORT_INPUT_NOTIFICATION_ITERATOR_PUT_REF_AND_RESET( |
666 | user_data->notif_iter); | |
223c70b2 PP |
667 | g_free(user_data); |
668 | } | |
669 | } | |
670 | ||
671 | static | |
b19ff26f PP |
672 | void create_source_sink(bt_graph *graph, |
673 | const bt_component_source **source, | |
674 | const bt_component_sink **sink) | |
223c70b2 | 675 | { |
b19ff26f PP |
676 | bt_component_class_source *src_comp_class; |
677 | bt_component_class_sink *sink_comp_class; | |
223c70b2 PP |
678 | int ret; |
679 | ||
680 | /* Create source component */ | |
e893886e | 681 | if (source) { |
0d72b8c3 | 682 | src_comp_class = bt_component_class_source_create("src", |
e893886e | 683 | src_iter_next); |
25583cd0 | 684 | BT_ASSERT(src_comp_class); |
0d72b8c3 | 685 | ret = bt_component_class_source_set_init_method( |
d94d92ac | 686 | src_comp_class, src_init); |
25583cd0 | 687 | BT_ASSERT(ret == 0); |
0d72b8c3 | 688 | ret = bt_component_class_source_set_finalize_method( |
d94d92ac | 689 | src_comp_class, src_finalize); |
25583cd0 | 690 | BT_ASSERT(ret == 0); |
0d72b8c3 | 691 | ret = bt_component_class_source_set_notification_iterator_init_method( |
e893886e | 692 | src_comp_class, src_iter_init); |
25583cd0 | 693 | BT_ASSERT(ret == 0); |
0d72b8c3 | 694 | ret = bt_component_class_source_set_notification_iterator_finalize_method( |
e893886e | 695 | src_comp_class, src_iter_finalize); |
25583cd0 | 696 | BT_ASSERT(ret == 0); |
0d72b8c3 PP |
697 | ret = bt_graph_add_source_component(graph, |
698 | src_comp_class, "source", NULL, source); | |
25583cd0 | 699 | BT_ASSERT(ret == 0); |
c5b9b441 | 700 | bt_component_class_source_put_ref(src_comp_class); |
e893886e | 701 | } |
223c70b2 PP |
702 | |
703 | /* Create sink component */ | |
e893886e | 704 | if (sink) { |
0d72b8c3 | 705 | sink_comp_class = bt_component_class_sink_create("sink", |
e893886e | 706 | sink_consume); |
25583cd0 | 707 | BT_ASSERT(sink_comp_class); |
0d72b8c3 | 708 | ret = bt_component_class_sink_set_init_method( |
d94d92ac | 709 | sink_comp_class, sink_init); |
25583cd0 | 710 | BT_ASSERT(ret == 0); |
0d72b8c3 | 711 | ret = bt_component_class_sink_set_finalize_method( |
d94d92ac | 712 | sink_comp_class, sink_finalize); |
0d72b8c3 | 713 | ret = bt_component_class_sink_set_input_port_connected_method( |
e893886e | 714 | sink_comp_class, sink_port_connected); |
25583cd0 | 715 | BT_ASSERT(ret == 0); |
0d72b8c3 PP |
716 | ret = bt_graph_add_sink_component(graph, |
717 | sink_comp_class, | |
d94d92ac | 718 | "sink", NULL, sink); |
25583cd0 | 719 | BT_ASSERT(ret == 0); |
c5b9b441 | 720 | bt_component_class_sink_put_ref(sink_comp_class); |
e893886e | 721 | } |
223c70b2 PP |
722 | } |
723 | ||
724 | static | |
725 | void do_std_test(enum test test, const char *name, | |
726 | const struct test_event *expected_test_events) | |
727 | { | |
b19ff26f PP |
728 | const bt_component_source *src_comp; |
729 | const bt_component_sink *sink_comp; | |
730 | const bt_port_output *upstream_port; | |
731 | const bt_port_input *downstream_port; | |
223c70b2 PP |
732 | enum bt_graph_status graph_status = BT_GRAPH_STATUS_OK; |
733 | ||
734 | clear_test_events(); | |
735 | current_test = test; | |
736 | diag("test: %s", name); | |
25583cd0 | 737 | BT_ASSERT(!graph); |
0d72b8c3 | 738 | graph = bt_graph_create(); |
25583cd0 | 739 | BT_ASSERT(graph); |
36712f1d | 740 | create_source_sink(graph, &src_comp, &sink_comp); |
223c70b2 PP |
741 | |
742 | /* Connect source to sink */ | |
0d72b8c3 PP |
743 | upstream_port = |
744 | bt_component_source_borrow_output_port_by_name_const( | |
745 | src_comp, "out"); | |
25583cd0 | 746 | BT_ASSERT(upstream_port); |
0d72b8c3 | 747 | downstream_port = bt_component_sink_borrow_input_port_by_name_const( |
d94d92ac | 748 | sink_comp, "in"); |
25583cd0 | 749 | BT_ASSERT(downstream_port); |
0d72b8c3 | 750 | graph_status = bt_graph_connect_ports(graph, upstream_port, |
a256a42d | 751 | downstream_port, NULL); |
223c70b2 PP |
752 | |
753 | /* Run the graph until the end */ | |
754 | while (graph_status == BT_GRAPH_STATUS_OK || | |
755 | graph_status == BT_GRAPH_STATUS_AGAIN) { | |
0d72b8c3 | 756 | graph_status = bt_graph_run(graph); |
223c70b2 PP |
757 | } |
758 | ||
d94d92ac PP |
759 | ok(graph_status == BT_GRAPH_STATUS_END, |
760 | "graph finishes without any error"); | |
223c70b2 PP |
761 | |
762 | /* Compare the resulting test events */ | |
763 | if (expected_test_events) { | |
764 | ok(compare_test_events(expected_test_events), | |
765 | "the produced sequence of test events is the expected one"); | |
766 | } | |
767 | ||
c5b9b441 PP |
768 | bt_component_source_put_ref(src_comp); |
769 | bt_component_sink_put_ref(sink_comp); | |
770 | BT_GRAPH_PUT_REF_AND_RESET(graph); | |
223c70b2 PP |
771 | } |
772 | ||
773 | static | |
774 | void test_no_auto_notifs(void) | |
775 | { | |
776 | const struct test_event expected_test_events[] = { | |
862ca4ed PP |
777 | { .type = TEST_EV_TYPE_NOTIF_STREAM_BEGIN, .stream = src_stream1, .packet = NULL, }, |
778 | { .type = TEST_EV_TYPE_NOTIF_PACKET_BEGIN, .stream = src_stream1, .packet = src_stream1_packet1, }, | |
779 | { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = src_stream1, .packet = src_stream1_packet1, }, | |
780 | { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = src_stream1, .packet = src_stream1_packet1, }, | |
781 | { .type = TEST_EV_TYPE_NOTIF_STREAM_BEGIN, .stream = src_stream2, .packet = NULL, }, | |
782 | { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = src_stream1, .packet = src_stream1_packet1, }, | |
783 | { .type = TEST_EV_TYPE_NOTIF_PACKET_BEGIN, .stream = src_stream2, .packet = src_stream2_packet2, }, | |
784 | { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = src_stream2, .packet = src_stream2_packet2, }, | |
785 | { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = src_stream1, .packet = src_stream1_packet1, }, | |
786 | { .type = TEST_EV_TYPE_NOTIF_PACKET_END, .stream = src_stream1, .packet = src_stream1_packet1, }, | |
787 | { .type = TEST_EV_TYPE_NOTIF_PACKET_END, .stream = src_stream2, .packet = src_stream2_packet2, }, | |
788 | { .type = TEST_EV_TYPE_NOTIF_PACKET_BEGIN, .stream = src_stream1, .packet = src_stream1_packet2, }, | |
789 | { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = src_stream1, .packet = src_stream1_packet2, }, | |
790 | { .type = TEST_EV_TYPE_NOTIF_STREAM_END, .stream = src_stream2, .packet = NULL, }, | |
791 | { .type = TEST_EV_TYPE_NOTIF_PACKET_END, .stream = src_stream1, .packet = src_stream1_packet2, }, | |
792 | { .type = TEST_EV_TYPE_NOTIF_STREAM_END, .stream = src_stream1, .packet = NULL, }, | |
223c70b2 PP |
793 | { .type = TEST_EV_TYPE_END, }, |
794 | { .type = TEST_EV_TYPE_SENTINEL, }, | |
795 | }; | |
796 | ||
797 | do_std_test(TEST_NO_AUTO_NOTIFS, "no automatic notifications", | |
798 | expected_test_events); | |
799 | } | |
800 | ||
e893886e PP |
801 | static |
802 | void test_output_port_notification_iterator(void) | |
803 | { | |
804 | const struct test_event expected_test_events[] = { | |
862ca4ed PP |
805 | { .type = TEST_EV_TYPE_NOTIF_STREAM_BEGIN, .stream = src_stream1, .packet = NULL, }, |
806 | { .type = TEST_EV_TYPE_NOTIF_PACKET_BEGIN, .stream = src_stream1, .packet = src_stream1_packet1, }, | |
807 | { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = src_stream1, .packet = src_stream1_packet1, }, | |
808 | { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = src_stream1, .packet = src_stream1_packet1, }, | |
809 | { .type = TEST_EV_TYPE_NOTIF_STREAM_BEGIN, .stream = src_stream2, .packet = NULL, }, | |
810 | { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = src_stream1, .packet = src_stream1_packet1, }, | |
811 | { .type = TEST_EV_TYPE_NOTIF_PACKET_BEGIN, .stream = src_stream2, .packet = src_stream2_packet2, }, | |
812 | { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = src_stream2, .packet = src_stream2_packet2, }, | |
813 | { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = src_stream1, .packet = src_stream1_packet1, }, | |
814 | { .type = TEST_EV_TYPE_NOTIF_PACKET_END, .stream = src_stream1, .packet = src_stream1_packet1, }, | |
815 | { .type = TEST_EV_TYPE_NOTIF_PACKET_END, .stream = src_stream2, .packet = src_stream2_packet2, }, | |
816 | { .type = TEST_EV_TYPE_NOTIF_PACKET_BEGIN, .stream = src_stream1, .packet = src_stream1_packet2, }, | |
817 | { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = src_stream1, .packet = src_stream1_packet2, }, | |
818 | { .type = TEST_EV_TYPE_NOTIF_STREAM_END, .stream = src_stream2, .packet = NULL, }, | |
819 | { .type = TEST_EV_TYPE_NOTIF_PACKET_END, .stream = src_stream1, .packet = src_stream1_packet2, }, | |
820 | { .type = TEST_EV_TYPE_NOTIF_STREAM_END, .stream = src_stream1, .packet = NULL, }, | |
e893886e PP |
821 | { .type = TEST_EV_TYPE_END, }, |
822 | { .type = TEST_EV_TYPE_SENTINEL, }, | |
823 | }; | |
b19ff26f PP |
824 | const bt_component_source *src_comp; |
825 | bt_port_output_notification_iterator *notif_iter; | |
e893886e PP |
826 | enum bt_notification_iterator_status iter_status = |
827 | BT_NOTIFICATION_ITERATOR_STATUS_OK; | |
b19ff26f | 828 | const bt_port_output *upstream_port; |
e893886e PP |
829 | |
830 | clear_test_events(); | |
831 | current_test = TEST_OUTPUT_PORT_NOTIFICATION_ITERATOR; | |
832 | diag("test: output port notification iterator"); | |
25583cd0 | 833 | BT_ASSERT(!graph); |
0d72b8c3 | 834 | graph = bt_graph_create(); |
25583cd0 | 835 | BT_ASSERT(graph); |
e893886e PP |
836 | create_source_sink(graph, &src_comp, NULL); |
837 | ||
838 | /* Create notification iterator on source's output port */ | |
0d72b8c3 PP |
839 | upstream_port = bt_component_source_borrow_output_port_by_name_const(src_comp, |
840 | "out"); | |
d94d92ac | 841 | notif_iter = bt_port_output_notification_iterator_create(graph, |
5fd91d88 | 842 | upstream_port); |
e5be10ef | 843 | ok(notif_iter, "bt_private_output_port_notification_iterator_create() succeeds"); |
e893886e PP |
844 | |
845 | /* Consume the notification iterator */ | |
846 | while (iter_status == BT_NOTIFICATION_ITERATOR_STATUS_OK) { | |
07245ac2 | 847 | iter_status = common_consume(notif_iter, true); |
e893886e PP |
848 | } |
849 | ||
850 | ok(iter_status == BT_NOTIFICATION_ITERATOR_STATUS_END, | |
851 | "output port notification iterator finishes without any error"); | |
852 | ||
853 | /* Compare the resulting test events */ | |
854 | ok(compare_test_events(expected_test_events), | |
855 | "the produced sequence of test events is the expected one"); | |
856 | ||
c5b9b441 PP |
857 | bt_component_source_put_ref(src_comp); |
858 | BT_GRAPH_PUT_REF_AND_RESET(graph); | |
859 | bt_port_output_notification_iterator_put_ref(notif_iter); | |
e893886e PP |
860 | } |
861 | ||
223c70b2 PP |
862 | #define DEBUG_ENV_VAR "TEST_BT_NOTIFICATION_ITERATOR_DEBUG" |
863 | ||
864 | int main(int argc, char **argv) | |
865 | { | |
866 | if (getenv(DEBUG_ENV_VAR) && strcmp(getenv(DEBUG_ENV_VAR), "1") == 0) { | |
867 | debug = true; | |
868 | } | |
869 | ||
870 | plan_tests(NR_TESTS); | |
871 | init_static_data(); | |
872 | test_no_auto_notifs(); | |
e893886e | 873 | test_output_port_notification_iterator(); |
223c70b2 PP |
874 | fini_static_data(); |
875 | return exit_status(); | |
876 | } |