Avoid unnecessary inclusions in public headers
[babeltrace.git] / tests / plugins / test-utils-muxer.c
CommitLineData
8deeabc6
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>
8deeabc6
PP
22#include <inttypes.h>
23#include <string.h>
24#include <assert.h>
9d408fca 25#include <babeltrace/babeltrace.h>
8deeabc6
PP
26#include <glib.h>
27
28#include "tap/tap.h"
29
3230ee6b 30#define NR_TESTS 12
8deeabc6
PP
31
32enum test {
33 TEST_NO_TS,
34 TEST_NO_UPSTREAM_CONNECTION,
8deeabc6
PP
35 TEST_SIMPLE_4_PORTS,
36 TEST_4_PORTS_WITH_RETRIES,
37 TEST_SINGLE_END_THEN_MULTIPLE_FULL,
38 TEST_SINGLE_AGAIN_END_THEN_MULTIPLE_FULL,
39};
40
41enum test_event_type {
42 TEST_EV_TYPE_NOTIF_UNEXPECTED,
43 TEST_EV_TYPE_NOTIF_EVENT,
44 TEST_EV_TYPE_NOTIF_INACTIVITY,
45 TEST_EV_TYPE_NOTIF_PACKET_BEGIN,
46 TEST_EV_TYPE_NOTIF_PACKET_END,
3230ee6b
PP
47 TEST_EV_TYPE_NOTIF_STREAM_BEGIN,
48 TEST_EV_TYPE_NOTIF_STREAM_END,
8deeabc6
PP
49 TEST_EV_TYPE_AGAIN,
50 TEST_EV_TYPE_END,
51 TEST_EV_TYPE_SENTINEL,
52};
53
54struct test_event {
55 enum test_event_type type;
56 int64_t ts_ns;
57};
58
59struct source_muxer_sink {
60 struct bt_component *source;
61 struct bt_component *muxer;
62 struct bt_component *sink;
63};
64
65struct graph_listener_data {
66 struct bt_graph *graph;
67 struct bt_component *source;
68 struct bt_component *muxer;
69 struct bt_component *sink;
70};
71
72static bool debug = false;
73static enum test current_test;
74static GArray *test_events;
75static struct bt_clock_class_priority_map *src_cc_prio_map;
76static struct bt_clock_class_priority_map *src_empty_cc_prio_map;
77static struct bt_ctf_clock_class *src_clock_class;
78static struct bt_ctf_stream_class *src_stream_class;
79static struct bt_ctf_event_class *src_event_class;
3230ee6b
PP
80static struct bt_ctf_packet *src_packet0;
81static struct bt_ctf_packet *src_packet1;
82static struct bt_ctf_packet *src_packet2;
83static struct bt_ctf_packet *src_packet3;
8deeabc6
PP
84
85enum {
86 SEQ_END = -1,
87 SEQ_AGAIN = -2,
88 SEQ_PACKET_BEGIN = -3,
89 SEQ_PACKET_END = -4,
90};
91
92struct src_iter_user_data {
93 size_t iter_index;
94 int64_t *seq;
95 size_t at;
3230ee6b 96 struct bt_ctf_packet *packet;
8deeabc6
PP
97};
98
99struct sink_user_data {
100 struct bt_notification_iterator *notif_iter;
101};
102
103static int64_t seq1[] = {
104 24, 53, 97, 105, 119, 210, 222, 240, 292, 317, 353, 407, 433,
105 473, 487, 504, 572, 615, 708, 766, 850, 852, 931, 951, 956, 996,
106 SEQ_END,
107};
108
109static int64_t seq2[] = {
110 51, 59, 68, 77, 91, 121, 139, 170, 179, 266, 352, 454, 478, 631,
111 644, 668, 714, 744, 750, 778, 790, 836, SEQ_END,
112};
113
114static int64_t seq3[] = {
115 8, 71, 209, 254, 298, 320, 350, 393, 419, 624, 651, 678, 717,
116 731, 733, 788, 819, 820, 857, 892, 903, 944, 998, SEQ_END,
117};
118
119static int64_t seq4[] = {
120 41, 56, 120, 138, 154, 228, 471, 479, 481, 525, 591, 605, 612,
121 618, 632, 670, 696, 825, 863, 867, 871, 884, 953, 985, 999,
122 SEQ_END,
123};
124
125static int64_t seq1_with_again[] = {
089717de
PP
126 24, 53, 97, 105, 119, 210, SEQ_AGAIN, 222, 240, 292, 317, 353,
127 407, 433, 473, 487, 504, 572, 615, 708, 766, 850, 852, 931, 951,
128 956, 996, SEQ_END,
8deeabc6
PP
129};
130
131static int64_t seq2_with_again[] = {
132 51, 59, 68, 77, 91, 121, 139, 170, 179, 266, 352, 454, 478, 631,
089717de 133 644, 668, 714, 744, 750, 778, 790, 836, SEQ_AGAIN, SEQ_END,
8deeabc6
PP
134};
135
136static int64_t seq3_with_again[] = {
137 8, 71, 209, 254, 298, 320, 350, 393, 419, 624, 651, SEQ_AGAIN,
089717de
PP
138 678, 717, 731, 733, 788, 819, 820, 857, 892, 903, 944, 998,
139 SEQ_END,
8deeabc6
PP
140};
141
142static int64_t seq4_with_again[] = {
143 SEQ_AGAIN, 41, 56, 120, 138, 154, 228, 471, 479, 481, 525, 591,
144 605, 612, 618, 632, 670, 696, 825, 863, 867, 871, 884, 953, 985,
145 999, SEQ_END,
146};
147
148static int64_t seq5[] = {
149 1, 4, 189, 1001, SEQ_END,
150};
151
8deeabc6
PP
152static
153void clear_test_events(void)
154{
155 g_array_set_size(test_events, 0);
156}
157
158static
159void print_test_event(FILE *fp, const struct test_event *event)
160{
161 fprintf(fp, "{ type = ");
162
163 switch (event->type) {
164 case TEST_EV_TYPE_NOTIF_UNEXPECTED:
165 fprintf(fp, "TEST_EV_TYPE_NOTIF_UNEXPECTED");
166 break;
167 case TEST_EV_TYPE_NOTIF_EVENT:
168 fprintf(fp, "TEST_EV_TYPE_NOTIF_EVENT");
169 break;
170 case TEST_EV_TYPE_NOTIF_INACTIVITY:
171 fprintf(fp, "TEST_EV_TYPE_NOTIF_INACTIVITY");
172 break;
173 case TEST_EV_TYPE_NOTIF_PACKET_BEGIN:
174 fprintf(fp, "TEST_EV_TYPE_NOTIF_PACKET_BEGIN");
175 break;
176 case TEST_EV_TYPE_NOTIF_PACKET_END:
177 fprintf(fp, "TEST_EV_TYPE_NOTIF_PACKET_END");
178 break;
3230ee6b
PP
179 case TEST_EV_TYPE_NOTIF_STREAM_BEGIN:
180 fprintf(fp, "TEST_EV_TYPE_NOTIF_STREAM_BEGIN");
181 break;
182 case TEST_EV_TYPE_NOTIF_STREAM_END:
183 fprintf(fp, "TEST_EV_TYPE_NOTIF_STREAM_END");
184 break;
8deeabc6
PP
185 case TEST_EV_TYPE_AGAIN:
186 fprintf(fp, "TEST_EV_TYPE_AGAIN");
187 break;
188 case TEST_EV_TYPE_END:
189 fprintf(fp, "TEST_EV_TYPE_END");
190 break;
191 case TEST_EV_TYPE_SENTINEL:
192 fprintf(fp, "TEST_EV_TYPE_SENTINEL");
193 break;
194 default:
195 fprintf(fp, "(UNKNOWN)");
196 break;
197 }
198
199 switch (event->type) {
200 case TEST_EV_TYPE_NOTIF_EVENT:
201 case TEST_EV_TYPE_NOTIF_INACTIVITY:
202 fprintf(fp, ", ts-ns = %" PRId64, event->ts_ns);
203 default:
204 break;
205 }
206
207 fprintf(fp, " }");
208}
209
210static
211void append_test_event(struct test_event *event)
212{
213 g_array_append_val(test_events, *event);
214}
215
216static
217bool compare_single_test_events(const struct test_event *ev_a,
218 const struct test_event *ev_b)
219{
220 if (debug) {
221 fprintf(stderr, ":: Comparing test events: ");
222 print_test_event(stderr, ev_a);
223 fprintf(stderr, " vs. ");
224 print_test_event(stderr, ev_b);
225 fprintf(stderr, "\n");
226 }
227
228 if (ev_a->type != ev_b->type) {
229 return false;
230 }
231
232 switch (ev_a->type) {
233 case TEST_EV_TYPE_NOTIF_EVENT:
234 case TEST_EV_TYPE_NOTIF_INACTIVITY:
235 if (ev_a->ts_ns != ev_b->ts_ns) {
236 return false;
237 }
238 break;
239 default:
240 break;
241 }
242
243 return true;
244}
245
246static
247bool compare_test_events(const struct test_event *expected_events)
248{
249 const struct test_event *expected_event = expected_events;
250 size_t i = 0;
251
252 assert(expected_events);
253
254 while (true) {
255 const struct test_event *event;
256
257 if (expected_event->type == TEST_EV_TYPE_SENTINEL) {
258 break;
259 }
260
261 if (i >= test_events->len) {
262 return false;
263 }
264
265 event = &g_array_index(test_events, struct test_event, i);
266
267 if (!compare_single_test_events(event, expected_event)) {
268 return false;
269 }
270
271 i++;
272 expected_event++;
273 }
274
275 if (i != test_events->len) {
089717de
PP
276 if (debug) {
277 fprintf(stderr, ":: Length mismatch\n");
278 }
279
8deeabc6
PP
280 return false;
281 }
282
283 return true;
284}
285
286static
287void init_static_data(void)
288{
289 int ret;
290 struct bt_ctf_trace *trace;
291 struct bt_ctf_stream *stream;
292 struct bt_ctf_field_type *empty_struct_ft;
293
294 /* Test events */
295 test_events = g_array_new(FALSE, TRUE, sizeof(struct test_event));
296 assert(test_events);
297
298 /* Metadata */
299 empty_struct_ft = bt_ctf_field_type_structure_create();
300 assert(empty_struct_ft);
301 trace = bt_ctf_trace_create();
302 assert(trace);
303 ret = bt_ctf_trace_set_native_byte_order(trace,
304 BT_CTF_BYTE_ORDER_LITTLE_ENDIAN);
305 assert(ret == 0);
306 ret = bt_ctf_trace_set_packet_header_type(trace, empty_struct_ft);
307 assert(ret == 0);
f3534905 308 src_clock_class = bt_ctf_clock_class_create("my-clock", 1000000000);
8deeabc6
PP
309 assert(src_clock_class);
310 ret = bt_ctf_clock_class_set_is_absolute(src_clock_class, 1);
311 assert(ret == 0);
312 ret = bt_ctf_trace_add_clock_class(trace, src_clock_class);
313 assert(ret == 0);
314 src_empty_cc_prio_map = bt_clock_class_priority_map_create();
315 assert(src_empty_cc_prio_map);
316 src_cc_prio_map = bt_clock_class_priority_map_create();
317 assert(src_cc_prio_map);
318 ret = bt_clock_class_priority_map_add_clock_class(src_cc_prio_map,
319 src_clock_class, 0);
320 assert(ret == 0);
321 src_stream_class = bt_ctf_stream_class_create("my-stream-class");
322 assert(src_stream_class);
323 ret = bt_ctf_stream_class_set_packet_context_type(src_stream_class,
324 empty_struct_ft);
325 assert(ret == 0);
326 ret = bt_ctf_stream_class_set_event_header_type(src_stream_class,
327 empty_struct_ft);
328 assert(ret == 0);
329 ret = bt_ctf_stream_class_set_event_context_type(src_stream_class,
330 empty_struct_ft);
331 assert(ret == 0);
332 src_event_class = bt_ctf_event_class_create("my-event-class");
333 ret = bt_ctf_event_class_set_context_type(src_event_class,
334 empty_struct_ft);
335 assert(ret == 0);
336 ret = bt_ctf_event_class_set_context_type(src_event_class,
337 empty_struct_ft);
338 assert(ret == 0);
339 ret = bt_ctf_stream_class_add_event_class(src_stream_class,
340 src_event_class);
341 assert(ret == 0);
342 ret = bt_ctf_trace_add_stream_class(trace, src_stream_class);
343 assert(ret == 0);
3230ee6b
PP
344 stream = bt_ctf_stream_create(src_stream_class, "stream0");
345 assert(stream);
346 src_packet0 = bt_ctf_packet_create(stream);
347 assert(src_packet0);
348 bt_put(stream);
349 stream = bt_ctf_stream_create(src_stream_class, "stream1");
8deeabc6 350 assert(stream);
3230ee6b
PP
351 src_packet1 = bt_ctf_packet_create(stream);
352 assert(src_packet0);
353 bt_put(stream);
354 stream = bt_ctf_stream_create(src_stream_class, "stream2");
355 assert(stream);
356 src_packet2 = bt_ctf_packet_create(stream);
357 assert(src_packet0);
358 bt_put(stream);
359 stream = bt_ctf_stream_create(src_stream_class, "stream3");
360 assert(stream);
361 src_packet3 = bt_ctf_packet_create(stream);
362 assert(src_packet0);
363 bt_put(stream);
8deeabc6
PP
364
365 bt_put(trace);
8deeabc6
PP
366 bt_put(empty_struct_ft);
367}
368
369static
370void fini_static_data(void)
371{
372 /* Test events */
373 g_array_free(test_events, TRUE);
374
375 /* Metadata */
376 bt_put(src_empty_cc_prio_map);
377 bt_put(src_cc_prio_map);
378 bt_put(src_clock_class);
379 bt_put(src_stream_class);
380 bt_put(src_event_class);
3230ee6b
PP
381 bt_put(src_packet0);
382 bt_put(src_packet1);
383 bt_put(src_packet2);
384 bt_put(src_packet3);
8deeabc6
PP
385}
386
387static
388void src_iter_finalize(
90157d89 389 struct bt_private_connection_private_notification_iterator *private_notification_iterator)
8deeabc6
PP
390{
391 struct src_iter_user_data *user_data =
90157d89 392 bt_private_connection_private_notification_iterator_get_user_data(
8deeabc6
PP
393 private_notification_iterator);
394
395 if (user_data) {
396 g_free(user_data);
397 }
398}
399
400static
401enum bt_notification_iterator_status src_iter_init(
90157d89 402 struct bt_private_connection_private_notification_iterator *priv_notif_iter,
8deeabc6
PP
403 struct bt_private_port *private_port)
404{
405 struct src_iter_user_data *user_data =
406 g_new0(struct src_iter_user_data, 1);
407 struct bt_port *port = bt_port_from_private_port(private_port);
408 const char *port_name;
409 int ret;
410
411 assert(user_data);
412 assert(port);
90157d89 413 ret = bt_private_connection_private_notification_iterator_set_user_data(priv_notif_iter,
8deeabc6
PP
414 user_data);
415 assert(ret == 0);
416 port_name = bt_port_get_name(port);
417 assert(port_name);
418 user_data->iter_index = port_name[3] - '0';
419 bt_put(port);
420
3230ee6b
PP
421 switch (user_data->iter_index) {
422 case 0:
423 user_data->packet = src_packet0;
424 break;
425 case 1:
426 user_data->packet = src_packet1;
427 break;
428 case 2:
429 user_data->packet = src_packet2;
430 break;
431 case 3:
432 user_data->packet = src_packet3;
433 break;
434 default:
0fbb9a9f 435 abort();
3230ee6b
PP
436 }
437
8deeabc6
PP
438 switch (current_test) {
439 case TEST_NO_TS:
440 if (user_data->iter_index == 1) {
441 user_data->seq = seq5;
442 }
443 break;
8deeabc6
PP
444 case TEST_SIMPLE_4_PORTS:
445 if (user_data->iter_index == 0) {
446 user_data->seq = seq1;
447 } else if (user_data->iter_index == 1) {
448 user_data->seq = seq2;
449 } else if (user_data->iter_index == 2) {
450 user_data->seq = seq3;
451 } else {
452 user_data->seq = seq4;
453 }
454 break;
455 case TEST_4_PORTS_WITH_RETRIES:
456 if (user_data->iter_index == 0) {
457 user_data->seq = seq1_with_again;
458 } else if (user_data->iter_index == 1) {
459 user_data->seq = seq2_with_again;
460 } else if (user_data->iter_index == 2) {
461 user_data->seq = seq3_with_again;
462 } else {
463 user_data->seq = seq4_with_again;
464 }
465 break;
466 case TEST_SINGLE_END_THEN_MULTIPLE_FULL:
467 case TEST_SINGLE_AGAIN_END_THEN_MULTIPLE_FULL:
468 if (user_data->iter_index == 0) {
469 /* Ignore: this iterator only returns END */
470 } else if (user_data->iter_index == 1) {
471 user_data->seq = seq2;
472 } else {
473 user_data->seq = seq3;
474 }
475 break;
476 default:
0fbb9a9f 477 abort();
8deeabc6
PP
478 }
479
480 return BT_NOTIFICATION_ITERATOR_STATUS_OK;
481}
482
483static
3230ee6b
PP
484struct bt_ctf_event *src_create_event(struct bt_ctf_packet *packet,
485 int64_t ts_ns)
8deeabc6
PP
486{
487 struct bt_ctf_event *event = bt_ctf_event_create(src_event_class);
488 int ret;
489
490 assert(event);
3230ee6b 491 ret = bt_ctf_event_set_packet(event, packet);
8deeabc6
PP
492 assert(ret == 0);
493
494 if (ts_ns != -1) {
495 struct bt_ctf_clock_value *clock_value;
496
497 clock_value = bt_ctf_clock_value_create(src_clock_class,
498 (uint64_t) ts_ns);
499 assert(clock_value);
500 ret = bt_ctf_event_set_clock_value(event, clock_value);
11b4920c 501 assert(ret == 0);
8deeabc6
PP
502 bt_put(clock_value);
503 }
504
505 return event;
506}
507
508static
90157d89 509struct bt_notification_iterator_next_method_return src_iter_next_seq(
8deeabc6
PP
510 struct src_iter_user_data *user_data)
511{
90157d89 512 struct bt_notification_iterator_next_method_return next_return = {
8deeabc6
PP
513 .status = BT_NOTIFICATION_ITERATOR_STATUS_OK,
514 };
515 int64_t cur_ts_ns;
516
517 assert(user_data->seq);
518 cur_ts_ns = user_data->seq[user_data->at];
519
520 switch (cur_ts_ns) {
521 case SEQ_END:
522 next_return.status =
523 BT_NOTIFICATION_ITERATOR_STATUS_END;
524 break;
525 case SEQ_AGAIN:
526 next_return.status =
527 BT_NOTIFICATION_ITERATOR_STATUS_AGAIN;
528 break;
529 case SEQ_PACKET_BEGIN:
530 next_return.notification =
3230ee6b 531 bt_notification_packet_begin_create(user_data->packet);
8deeabc6
PP
532 assert(next_return.notification);
533 break;
534 case SEQ_PACKET_END:
535 next_return.notification =
3230ee6b 536 bt_notification_packet_end_create(user_data->packet);
8deeabc6
PP
537 assert(next_return.notification);
538 break;
539 default:
540 {
3230ee6b
PP
541 struct bt_ctf_event *event = src_create_event(
542 user_data->packet, cur_ts_ns);
8deeabc6
PP
543
544 assert(event);
545 next_return.notification = bt_notification_event_create(event,
546 src_cc_prio_map);
547 bt_put(event);
548 assert(next_return.notification);
549 break;
550 }
551 }
552
553 if (next_return.status != BT_NOTIFICATION_ITERATOR_STATUS_END) {
554 user_data->at++;
555 }
556
557 return next_return;
558}
559
560static
90157d89
PP
561struct bt_notification_iterator_next_method_return src_iter_next(
562 struct bt_private_connection_private_notification_iterator *priv_iterator)
8deeabc6 563{
90157d89 564 struct bt_notification_iterator_next_method_return next_return = {
624fa311
PP
565 .notification = NULL,
566 .status = BT_NOTIFICATION_ITERATOR_STATUS_OK,
567 };
8deeabc6 568 struct src_iter_user_data *user_data =
90157d89 569 bt_private_connection_private_notification_iterator_get_user_data(priv_iterator);
8deeabc6 570 struct bt_private_component *private_component =
90157d89 571 bt_private_connection_private_notification_iterator_get_private_component(priv_iterator);
147337a3 572 int ret;
8deeabc6
PP
573
574 assert(user_data);
575 assert(private_component);
576
577 switch (current_test) {
578 case TEST_NO_TS:
579 if (user_data->iter_index == 0) {
580 if (user_data->at == 0) {
581 next_return.notification =
3230ee6b
PP
582 bt_notification_packet_begin_create(
583 user_data->packet);
8deeabc6
PP
584 assert(next_return.notification);
585 } else if (user_data->at < 6) {
3230ee6b
PP
586 struct bt_ctf_event *event = src_create_event(
587 user_data->packet, -1);
8deeabc6
PP
588
589 assert(event);
590 next_return.notification =
591 bt_notification_event_create(event,
592 src_empty_cc_prio_map);
593 assert(next_return.notification);
594 bt_put(event);
595 } else {
596 next_return.status =
597 BT_NOTIFICATION_ITERATOR_STATUS_END;
598 }
599
600 user_data->at++;
601 } else {
602 next_return = src_iter_next_seq(user_data);
603 }
604 break;
8deeabc6
PP
605 case TEST_SIMPLE_4_PORTS:
606 case TEST_4_PORTS_WITH_RETRIES:
607 next_return = src_iter_next_seq(user_data);
608 break;
609 case TEST_SINGLE_END_THEN_MULTIPLE_FULL:
610 if (user_data->iter_index == 0) {
147337a3
PP
611 ret = bt_private_component_source_add_output_private_port(
612 private_component, "out1", NULL, NULL);
613 assert(ret == 0);
614 ret = bt_private_component_source_add_output_private_port(
615 private_component, "out2", NULL, NULL);
616 assert(ret == 0);
8deeabc6
PP
617 next_return.status = BT_NOTIFICATION_ITERATOR_STATUS_END;
618 } else {
619 next_return = src_iter_next_seq(user_data);
620 }
621 break;
622 case TEST_SINGLE_AGAIN_END_THEN_MULTIPLE_FULL:
623 if (user_data->iter_index == 0) {
624 if (user_data->at == 0) {
625 next_return.status = BT_NOTIFICATION_ITERATOR_STATUS_AGAIN;
626 user_data->at++;
627 } else {
147337a3
PP
628 ret = bt_private_component_source_add_output_private_port(
629 private_component, "out1", NULL, NULL);
630 assert(ret == 0);
631 ret = bt_private_component_source_add_output_private_port(
632 private_component, "out2", NULL, NULL);
633 assert(ret == 0);
8deeabc6
PP
634 next_return.status = BT_NOTIFICATION_ITERATOR_STATUS_END;
635 }
636 } else {
637 next_return = src_iter_next_seq(user_data);
638 }
639 break;
640 default:
0fbb9a9f 641 abort();
8deeabc6
PP
642 }
643
644 bt_put(private_component);
645 return next_return;
646}
647
648static
649enum bt_component_status src_init(
650 struct bt_private_component *private_component,
651 struct bt_value *params, void *init_method_data)
652{
147337a3 653 int ret;
8deeabc6
PP
654 size_t nb_ports;
655
8deeabc6
PP
656 switch (current_test) {
657 case TEST_NO_TS:
8deeabc6
PP
658 nb_ports = 2;
659 break;
660 case TEST_SINGLE_END_THEN_MULTIPLE_FULL:
661 case TEST_SINGLE_AGAIN_END_THEN_MULTIPLE_FULL:
662 nb_ports = 1;
663 break;
664 default:
665 nb_ports = 4;
666 break;
667 }
668
669 if (nb_ports >= 1) {
147337a3
PP
670 ret = bt_private_component_source_add_output_private_port(
671 private_component, "out0", NULL, NULL);
672 assert(ret == 0);
8deeabc6
PP
673 }
674
675 if (nb_ports >= 2) {
147337a3
PP
676 ret = bt_private_component_source_add_output_private_port(
677 private_component, "out1", NULL, NULL);
678 assert(ret == 0);
8deeabc6
PP
679 }
680
681 if (nb_ports >= 3) {
147337a3
PP
682 ret = bt_private_component_source_add_output_private_port(
683 private_component, "out2", NULL, NULL);
684 assert(ret == 0);
8deeabc6
PP
685 }
686
687 if (nb_ports >= 4) {
147337a3
PP
688 ret = bt_private_component_source_add_output_private_port(
689 private_component, "out3", NULL, NULL);
690 assert(ret == 0);
8deeabc6
PP
691 }
692
693 return BT_COMPONENT_STATUS_OK;
694}
695
696static
697void src_finalize(struct bt_private_component *private_component)
698{
699}
700
701static
702enum bt_component_status sink_consume(
703 struct bt_private_component *priv_component)
704{
705 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
706 struct bt_notification *notification = NULL;
707 struct sink_user_data *user_data =
708 bt_private_component_get_user_data(priv_component);
709 enum bt_notification_iterator_status it_ret;
710 struct test_event test_event;
711 bool do_append_test_event = true;
712
713 assert(user_data && user_data->notif_iter);
714 it_ret = bt_notification_iterator_next(user_data->notif_iter);
715
716 if (it_ret < 0) {
717 ret = BT_COMPONENT_STATUS_ERROR;
718 do_append_test_event = false;
719 goto end;
720 }
721
722 switch (it_ret) {
723 case BT_NOTIFICATION_ITERATOR_STATUS_END:
724 test_event.type = TEST_EV_TYPE_END;
725 ret = BT_COMPONENT_STATUS_END;
726 BT_PUT(user_data->notif_iter);
727 goto end;
728 case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN:
729 test_event.type = TEST_EV_TYPE_AGAIN;
730 ret = BT_COMPONENT_STATUS_AGAIN;
731 goto end;
732 default:
733 break;
734 }
735
736 notification = bt_notification_iterator_get_notification(
737 user_data->notif_iter);
738 assert(notification);
739
740 switch (bt_notification_get_type(notification)) {
741 case BT_NOTIFICATION_TYPE_EVENT:
742 {
743 struct bt_ctf_event *event;
744 struct bt_clock_class_priority_map *cc_prio_map;
745
746 test_event.type = TEST_EV_TYPE_NOTIF_EVENT;
747 cc_prio_map =
748 bt_notification_event_get_clock_class_priority_map(
749 notification);
750 assert(cc_prio_map);
751 event = bt_notification_event_get_event(notification);
752 assert(event);
753
754 if (bt_clock_class_priority_map_get_clock_class_count(cc_prio_map) > 0) {
755 struct bt_ctf_clock_value *clock_value;
756 struct bt_ctf_clock_class *clock_class =
757 bt_clock_class_priority_map_get_highest_priority_clock_class(
758 cc_prio_map);
759
760 assert(clock_class);
761 clock_value = bt_ctf_event_get_clock_value(event,
762 clock_class);
763 assert(clock_value);
764 ret = bt_ctf_clock_value_get_value_ns_from_epoch(
765 clock_value, &test_event.ts_ns);
766 assert(ret == 0);
767 bt_put(clock_value);
768 bt_put(clock_class);
769 } else {
770 test_event.ts_ns = -1;
771 }
772
773 bt_put(cc_prio_map);
774 bt_put(event);
775 break;
776 }
777 case BT_NOTIFICATION_TYPE_INACTIVITY:
778 {
779 struct bt_clock_class_priority_map *cc_prio_map;
780
781 test_event.type = TEST_EV_TYPE_NOTIF_INACTIVITY;
782 cc_prio_map = bt_notification_event_get_clock_class_priority_map(
783 notification);
784 assert(cc_prio_map);
785
786 if (bt_clock_class_priority_map_get_clock_class_count(cc_prio_map) > 0) {
787 struct bt_ctf_clock_value *clock_value;
788 struct bt_ctf_clock_class *clock_class =
789 bt_clock_class_priority_map_get_highest_priority_clock_class(
790 cc_prio_map);
791
792 assert(clock_class);
793 clock_value =
794 bt_notification_inactivity_get_clock_value(
795 notification, clock_class);
796 assert(clock_value);
797 ret = bt_ctf_clock_value_get_value_ns_from_epoch(
798 clock_value, &test_event.ts_ns);
799 assert(ret == 0);
800 bt_put(clock_value);
801 bt_put(clock_class);
802 } else {
803 test_event.ts_ns = -1;
804 }
805
806 bt_put(cc_prio_map);
807 break;
808 }
809 case BT_NOTIFICATION_TYPE_PACKET_BEGIN:
810 test_event.type = TEST_EV_TYPE_NOTIF_PACKET_BEGIN;
811 break;
812 case BT_NOTIFICATION_TYPE_PACKET_END:
813 test_event.type = TEST_EV_TYPE_NOTIF_PACKET_END;
814 break;
3230ee6b
PP
815 case BT_NOTIFICATION_TYPE_STREAM_BEGIN:
816 test_event.type = TEST_EV_TYPE_NOTIF_STREAM_BEGIN;
817 break;
818 case BT_NOTIFICATION_TYPE_STREAM_END:
819 test_event.type = TEST_EV_TYPE_NOTIF_STREAM_END;
820 break;
8deeabc6
PP
821 default:
822 test_event.type = TEST_EV_TYPE_NOTIF_UNEXPECTED;
823 break;
824 }
825
826end:
827 if (do_append_test_event) {
828 append_test_event(&test_event);
829 }
830
831 bt_put(notification);
832 return ret;
833}
834
835static
836void sink_port_connected(struct bt_private_component *private_component,
837 struct bt_private_port *self_private_port,
838 struct bt_port *other_port)
839{
840 struct bt_private_connection *priv_conn =
841 bt_private_port_get_private_connection(self_private_port);
842 struct sink_user_data *user_data = bt_private_component_get_user_data(
843 private_component);
73d5c1ad 844 enum bt_connection_status conn_status;
8deeabc6
PP
845
846 assert(user_data);
847 assert(priv_conn);
73d5c1ad
PP
848 conn_status = bt_private_connection_create_notification_iterator(
849 priv_conn, NULL, &user_data->notif_iter);
850 assert(conn_status == 0);
8deeabc6
PP
851 bt_put(priv_conn);
852}
853
854static
855enum bt_component_status sink_init(
856 struct bt_private_component *private_component,
857 struct bt_value *params, void *init_method_data)
858{
859 struct sink_user_data *user_data = g_new0(struct sink_user_data, 1);
860 int ret;
861
862 assert(user_data);
863 ret = bt_private_component_set_user_data(private_component,
864 user_data);
865 assert(ret == 0);
147337a3
PP
866 ret = bt_private_component_sink_add_input_private_port(
867 private_component, "in", NULL, NULL);
868 assert(ret == 0);
8deeabc6
PP
869 return BT_COMPONENT_STATUS_OK;
870}
871
872static
873void sink_finalize(struct bt_private_component *private_component)
874{
875 struct sink_user_data *user_data = bt_private_component_get_user_data(
876 private_component);
877
878 if (user_data) {
879 bt_put(user_data->notif_iter);
880 g_free(user_data);
881 }
882}
883
884static
36712f1d
PP
885void create_source_muxer_sink(struct bt_graph *graph,
886 struct bt_component **source,
8deeabc6
PP
887 struct bt_component **muxer,
888 struct bt_component **sink)
889{
890 struct bt_component_class *src_comp_class;
891 struct bt_component_class *muxer_comp_class;
892 struct bt_component_class *sink_comp_class;
893 int ret;
894
895 /* Create source component */
896 src_comp_class = bt_component_class_source_create("src", src_iter_next);
897 assert(src_comp_class);
898 ret = bt_component_class_set_init_method(src_comp_class, src_init);
899 assert(ret == 0);
900 ret = bt_component_class_set_finalize_method(src_comp_class,
901 src_finalize);
902 assert(ret == 0);
903 ret = bt_component_class_source_set_notification_iterator_init_method(
904 src_comp_class, src_iter_init);
905 assert(ret == 0);
906 ret = bt_component_class_source_set_notification_iterator_finalize_method(
907 src_comp_class, src_iter_finalize);
908 assert(ret == 0);
36712f1d
PP
909 ret = bt_graph_add_component(graph, src_comp_class, "source", NULL, source);
910 assert(ret == 0);
8deeabc6
PP
911
912 /* Create muxer component */
913 muxer_comp_class = bt_plugin_find_component_class("utils", "muxer",
914 BT_COMPONENT_CLASS_TYPE_FILTER);
915 assert(muxer_comp_class);
36712f1d
PP
916 ret = bt_graph_add_component(graph, muxer_comp_class, "muxer", NULL, muxer);
917 assert(ret == 0);
8deeabc6
PP
918
919 /* Create sink component */
920 sink_comp_class = bt_component_class_sink_create("sink", sink_consume);
921 assert(sink_comp_class);
922 ret = bt_component_class_set_init_method(sink_comp_class, sink_init);
923 assert(ret == 0);
924 ret = bt_component_class_set_finalize_method(sink_comp_class,
925 sink_finalize);
926 ret = bt_component_class_set_port_connected_method(sink_comp_class,
927 sink_port_connected);
928 assert(ret == 0);
36712f1d
PP
929 ret = bt_graph_add_component(graph, sink_comp_class, "sink", NULL, sink);
930 assert(ret == 0);
8deeabc6
PP
931
932 bt_put(src_comp_class);
933 bt_put(muxer_comp_class);
934 bt_put(sink_comp_class);
935}
936
937static
938void do_std_test(enum test test, const char *name,
939 const struct test_event *expected_test_events,
940 bool with_upstream)
941{
942 struct bt_component *src_comp;
943 struct bt_component *muxer_comp;
944 struct bt_component *sink_comp;
945 struct bt_port *upstream_port;
946 struct bt_port *downstream_port;
947 struct bt_graph *graph;
544d0515
PP
948 int64_t i;
949 int64_t count;
8deeabc6
PP
950 enum bt_graph_status graph_status = BT_GRAPH_STATUS_OK;
951
952 clear_test_events();
953 current_test = test;
954 diag("test: %s", name);
8deeabc6
PP
955 graph = bt_graph_create();
956 assert(graph);
36712f1d 957 create_source_muxer_sink(graph, &src_comp, &muxer_comp, &sink_comp);
8deeabc6
PP
958
959 /* Connect source output ports to muxer input ports */
960 if (with_upstream) {
544d0515
PP
961 count = bt_component_source_get_output_port_count(src_comp);
962 assert(count >= 0);
8deeabc6
PP
963
964 for (i = 0; i < count; i++) {
9ac68eb1 965 upstream_port = bt_component_source_get_output_port_by_index(
8deeabc6
PP
966 src_comp, i);
967 assert(upstream_port);
9ac68eb1 968 downstream_port = bt_component_filter_get_input_port_by_index(
8deeabc6
PP
969 muxer_comp, i);
970 assert(downstream_port);
a256a42d
PP
971 graph_status = bt_graph_connect_ports(graph,
972 upstream_port, downstream_port, NULL);
973 assert(graph_status == 0);
8deeabc6
PP
974 bt_put(upstream_port);
975 bt_put(downstream_port);
976 }
977 }
978
979 /* Connect muxer output port to sink input port */
9ac68eb1 980 upstream_port = bt_component_filter_get_output_port_by_name(muxer_comp,
8deeabc6
PP
981 "out");
982 assert(upstream_port);
b9d103be 983 downstream_port = bt_component_sink_get_input_port_by_name(sink_comp, "in");
8deeabc6 984 assert(downstream_port);
a256a42d
PP
985 graph_status = bt_graph_connect_ports(graph, upstream_port,
986 downstream_port, NULL);
987 assert(graph_status == 0);
8deeabc6
PP
988 bt_put(upstream_port);
989 bt_put(downstream_port);
990
991 while (graph_status == BT_GRAPH_STATUS_OK ||
992 graph_status == BT_GRAPH_STATUS_AGAIN) {
993 graph_status = bt_graph_run(graph);
994 }
995
996 ok(graph_status == BT_GRAPH_STATUS_END, "graph finishes without any error");
997 ok(compare_test_events(expected_test_events),
998 "the produced sequence of test events is the expected one");
999
1000 bt_put(src_comp);
1001 bt_put(muxer_comp);
1002 bt_put(sink_comp);
1003 bt_put(graph);
1004}
1005
1006static
1007void test_no_ts(void)
1008{
1009 const struct test_event expected_test_events[] = {
3230ee6b
PP
1010 { .type = TEST_EV_TYPE_NOTIF_STREAM_BEGIN, },
1011 { .type = TEST_EV_TYPE_NOTIF_PACKET_BEGIN, },
1012 { .type = TEST_EV_TYPE_NOTIF_STREAM_BEGIN, },
8deeabc6
PP
1013 { .type = TEST_EV_TYPE_NOTIF_PACKET_BEGIN, },
1014 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = -1, },
1015 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = -1, },
1016 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = -1, },
1017 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = -1, },
1018 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = -1, },
3230ee6b
PP
1019 { .type = TEST_EV_TYPE_NOTIF_PACKET_END, },
1020 { .type = TEST_EV_TYPE_NOTIF_STREAM_END, },
8deeabc6
PP
1021 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 1, },
1022 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 4, },
1023 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 189, },
1024 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 1001, },
3230ee6b
PP
1025 { .type = TEST_EV_TYPE_NOTIF_PACKET_END, },
1026 { .type = TEST_EV_TYPE_NOTIF_STREAM_END, },
8deeabc6
PP
1027 { .type = TEST_EV_TYPE_END, },
1028 { .type = TEST_EV_TYPE_SENTINEL, },
1029 };
1030
1031 do_std_test(TEST_NO_TS, "event notifications with no time",
1032 expected_test_events, true);
1033}
1034
1035static
1036void test_no_upstream_connection(void)
1037{
1038 const struct test_event expected_test_events[] = {
1039 { .type = TEST_EV_TYPE_END, },
1040 { .type = TEST_EV_TYPE_SENTINEL, },
1041 };
1042
1043 do_std_test(TEST_NO_UPSTREAM_CONNECTION, "no upstream connection",
1044 expected_test_events, false);
1045}
1046
1047static
3230ee6b 1048void test_simple_4_ports(void)
8deeabc6
PP
1049{
1050 const struct test_event expected_test_events[] = {
3230ee6b 1051 { .type = TEST_EV_TYPE_NOTIF_STREAM_BEGIN, },
8deeabc6 1052 { .type = TEST_EV_TYPE_NOTIF_PACKET_BEGIN, },
3230ee6b
PP
1053 { .type = TEST_EV_TYPE_NOTIF_STREAM_BEGIN, },
1054 { .type = TEST_EV_TYPE_NOTIF_PACKET_BEGIN, },
1055 { .type = TEST_EV_TYPE_NOTIF_STREAM_BEGIN, },
1056 { .type = TEST_EV_TYPE_NOTIF_PACKET_BEGIN, },
1057 { .type = TEST_EV_TYPE_NOTIF_STREAM_BEGIN, },
8deeabc6 1058 { .type = TEST_EV_TYPE_NOTIF_PACKET_BEGIN, },
8deeabc6
PP
1059 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 8 },
1060 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 24 },
1061 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 41 },
1062 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 51 },
1063 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 53 },
1064 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 56 },
1065 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 59 },
1066 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 68 },
1067 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 71 },
1068 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 77 },
1069 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 91 },
1070 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 97 },
1071 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 105 },
1072 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 119 },
1073 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 120 },
1074 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 121 },
1075 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 138 },
1076 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 139 },
1077 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 154 },
1078 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 170 },
1079 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 179 },
1080 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 209 },
1081 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 210 },
1082 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 222 },
1083 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 228 },
1084 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 240 },
1085 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 254 },
1086 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 266 },
1087 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 292 },
1088 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 298 },
1089 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 317 },
1090 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 320 },
1091 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 350 },
1092 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 352 },
1093 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 353 },
1094 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 393 },
1095 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 407 },
1096 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 419 },
1097 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 433 },
1098 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 454 },
1099 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 471 },
1100 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 473 },
1101 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 478 },
1102 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 479 },
1103 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 481 },
1104 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 487 },
1105 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 504 },
1106 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 525 },
1107 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 572 },
1108 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 591 },
1109 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 605 },
1110 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 612 },
1111 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 615 },
1112 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 618 },
1113 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 624 },
1114 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 631 },
1115 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 632 },
1116 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 644 },
1117 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 651 },
1118 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 668 },
1119 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 670 },
1120 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 678 },
1121 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 696 },
1122 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 708 },
1123 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 714 },
1124 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 717 },
1125 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 731 },
1126 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 733 },
1127 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 744 },
1128 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 750 },
1129 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 766 },
1130 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 778 },
1131 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 788 },
1132 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 790 },
1133 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 819 },
1134 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 820 },
1135 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 825 },
1136 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 836 },
3230ee6b
PP
1137 { .type = TEST_EV_TYPE_NOTIF_PACKET_END, },
1138 { .type = TEST_EV_TYPE_NOTIF_STREAM_END, },
8deeabc6
PP
1139 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 850 },
1140 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 852 },
1141 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 857 },
1142 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 863 },
1143 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 867 },
1144 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 871 },
1145 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 884 },
1146 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 892 },
1147 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 903 },
1148 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 931 },
1149 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 944 },
1150 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 951 },
1151 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 953 },
1152 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 956 },
1153 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 985 },
1154 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 996 },
3230ee6b
PP
1155 { .type = TEST_EV_TYPE_NOTIF_PACKET_END, },
1156 { .type = TEST_EV_TYPE_NOTIF_STREAM_END, },
8deeabc6 1157 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 998 },
3230ee6b
PP
1158 { .type = TEST_EV_TYPE_NOTIF_PACKET_END, },
1159 { .type = TEST_EV_TYPE_NOTIF_STREAM_END, },
8deeabc6 1160 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 999 },
3230ee6b
PP
1161 { .type = TEST_EV_TYPE_NOTIF_PACKET_END, },
1162 { .type = TEST_EV_TYPE_NOTIF_STREAM_END, },
8deeabc6
PP
1163 { .type = TEST_EV_TYPE_END, },
1164 { .type = TEST_EV_TYPE_SENTINEL, },
1165 };
1166
1167 do_std_test(TEST_SIMPLE_4_PORTS, "simple: 4 ports without retries",
1168 expected_test_events, true);
1169}
1170
1171static
1172void test_4_ports_with_retries(void)
1173{
1174 const struct test_event expected_test_events[] = {
1175 { .type = TEST_EV_TYPE_AGAIN, },
3230ee6b
PP
1176 { .type = TEST_EV_TYPE_NOTIF_STREAM_BEGIN, },
1177 { .type = TEST_EV_TYPE_NOTIF_PACKET_BEGIN, },
1178 { .type = TEST_EV_TYPE_NOTIF_STREAM_BEGIN, },
1179 { .type = TEST_EV_TYPE_NOTIF_PACKET_BEGIN, },
1180 { .type = TEST_EV_TYPE_NOTIF_STREAM_BEGIN, },
1181 { .type = TEST_EV_TYPE_NOTIF_PACKET_BEGIN, },
1182 { .type = TEST_EV_TYPE_NOTIF_STREAM_BEGIN, },
1183 { .type = TEST_EV_TYPE_NOTIF_PACKET_BEGIN, },
8deeabc6
PP
1184 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 8 },
1185 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 24 },
1186 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 41 },
1187 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 51 },
1188 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 53 },
1189 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 56 },
1190 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 59 },
1191 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 68 },
1192 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 71 },
1193 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 77 },
1194 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 91 },
1195 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 97 },
1196 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 105 },
1197 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 119 },
1198 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 120 },
1199 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 121 },
1200 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 138 },
1201 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 139 },
1202 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 154 },
1203 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 170 },
1204 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 179 },
1205 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 209 },
1206 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 210 },
1207 { .type = TEST_EV_TYPE_AGAIN, },
1208 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 222 },
1209 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 228 },
1210 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 240 },
1211 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 254 },
1212 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 266 },
1213 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 292 },
1214 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 298 },
1215 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 317 },
1216 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 320 },
1217 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 350 },
1218 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 352 },
1219 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 353 },
1220 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 393 },
1221 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 407 },
1222 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 419 },
1223 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 433 },
1224 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 454 },
1225 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 471 },
1226 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 473 },
1227 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 478 },
1228 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 479 },
1229 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 481 },
1230 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 487 },
1231 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 504 },
1232 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 525 },
1233 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 572 },
1234 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 591 },
1235 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 605 },
1236 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 612 },
1237 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 615 },
1238 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 618 },
1239 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 624 },
1240 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 631 },
1241 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 632 },
1242 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 644 },
1243 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 651 },
1244 { .type = TEST_EV_TYPE_AGAIN, },
1245 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 668 },
1246 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 670 },
1247 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 678 },
1248 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 696 },
1249 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 708 },
1250 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 714 },
1251 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 717 },
1252 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 731 },
1253 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 733 },
1254 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 744 },
1255 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 750 },
1256 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 766 },
1257 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 778 },
1258 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 788 },
1259 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 790 },
1260 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 819 },
1261 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 820 },
1262 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 825 },
1263 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 836 },
1264 { .type = TEST_EV_TYPE_AGAIN, },
3230ee6b
PP
1265 { .type = TEST_EV_TYPE_NOTIF_PACKET_END, },
1266 { .type = TEST_EV_TYPE_NOTIF_STREAM_END, },
8deeabc6
PP
1267 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 850 },
1268 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 852 },
1269 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 857 },
1270 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 863 },
1271 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 867 },
1272 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 871 },
1273 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 884 },
1274 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 892 },
1275 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 903 },
1276 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 931 },
1277 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 944 },
1278 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 951 },
1279 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 953 },
1280 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 956 },
1281 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 985 },
1282 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 996 },
3230ee6b
PP
1283 { .type = TEST_EV_TYPE_NOTIF_PACKET_END, },
1284 { .type = TEST_EV_TYPE_NOTIF_STREAM_END, },
8deeabc6 1285 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 998 },
3230ee6b
PP
1286 { .type = TEST_EV_TYPE_NOTIF_PACKET_END, },
1287 { .type = TEST_EV_TYPE_NOTIF_STREAM_END, },
8deeabc6 1288 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 999 },
3230ee6b
PP
1289 { .type = TEST_EV_TYPE_NOTIF_PACKET_END, },
1290 { .type = TEST_EV_TYPE_NOTIF_STREAM_END, },
8deeabc6
PP
1291 { .type = TEST_EV_TYPE_END, },
1292 { .type = TEST_EV_TYPE_SENTINEL, },
1293 };
1294
1295 do_std_test(TEST_4_PORTS_WITH_RETRIES, "4 ports with retries",
1296 expected_test_events, true);
1297}
1298
1299static
1300void connect_port_to_first_avail_muxer_port(struct bt_graph *graph,
1301 struct bt_port *source_port,
1302 struct bt_component *muxer_comp)
1303{
1304 struct bt_port *avail_muxer_port = NULL;
544d0515
PP
1305 int64_t i;
1306 int64_t count;
a256a42d 1307 enum bt_graph_status graph_status;
8deeabc6 1308
544d0515
PP
1309 count = bt_component_filter_get_input_port_count(muxer_comp);
1310 assert(count >= 0);
8deeabc6
PP
1311
1312 for (i = 0; i < count; i++) {
1313 struct bt_port *muxer_port =
9ac68eb1 1314 bt_component_filter_get_input_port_by_index(
8deeabc6
PP
1315 muxer_comp, i);
1316
1317 assert(muxer_port);
1318
1319 if (!bt_port_is_connected(muxer_port)) {
1320 BT_MOVE(avail_muxer_port, muxer_port);
1321 break;
1322 } else {
1323 bt_put(muxer_port);
1324 }
1325 }
1326
a256a42d
PP
1327 graph_status = bt_graph_connect_ports(graph, source_port,
1328 avail_muxer_port, NULL);
1329 assert(graph_status == 0);
8deeabc6
PP
1330 bt_put(avail_muxer_port);
1331}
1332
1333static
1334void graph_port_added_listener_connect_to_avail_muxer_port(struct bt_port *port,
1335 void *data)
1336{
1337 struct graph_listener_data *graph_listener_data = data;
1338 struct bt_component *comp;
1339
1340 comp = bt_port_get_component(port);
1341 assert(comp);
1342
1343 if (comp != graph_listener_data->source) {
1344 goto end;
1345 }
1346
1347 connect_port_to_first_avail_muxer_port(graph_listener_data->graph,
1348 port, graph_listener_data->muxer);
1349
1350end:
1351 bt_put(comp);
1352}
1353
1354static
1355void test_single_end_then_multiple_full(void)
1356{
1357 struct bt_component *src_comp;
1358 struct bt_component *muxer_comp;
1359 struct bt_component *sink_comp;
1360 struct bt_port *upstream_port;
1361 struct bt_port *downstream_port;
1362 struct bt_graph *graph;
544d0515
PP
1363 int64_t i;
1364 int64_t count;
8deeabc6
PP
1365 int ret;
1366 enum bt_graph_status graph_status = BT_GRAPH_STATUS_OK;
1367 struct graph_listener_data graph_listener_data;
1368 const struct test_event expected_test_events[] = {
3230ee6b
PP
1369 { .type = TEST_EV_TYPE_NOTIF_STREAM_BEGIN, },
1370 { .type = TEST_EV_TYPE_NOTIF_PACKET_BEGIN, },
1371 { .type = TEST_EV_TYPE_NOTIF_STREAM_BEGIN, },
1372 { .type = TEST_EV_TYPE_NOTIF_PACKET_BEGIN, },
8deeabc6
PP
1373 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 8 },
1374 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 51 },
1375 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 59 },
1376 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 68 },
1377 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 71 },
1378 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 77 },
1379 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 91 },
1380 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 121 },
1381 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 139 },
1382 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 170 },
1383 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 179 },
1384 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 209 },
1385 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 254 },
1386 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 266 },
1387 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 298 },
1388 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 320 },
1389 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 350 },
1390 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 352 },
1391 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 393 },
1392 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 419 },
1393 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 454 },
1394 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 478 },
1395 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 624 },
1396 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 631 },
1397 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 644 },
1398 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 651 },
1399 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 668 },
1400 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 678 },
1401 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 714 },
1402 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 717 },
1403 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 731 },
1404 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 733 },
1405 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 744 },
1406 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 750 },
1407 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 778 },
1408 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 788 },
1409 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 790 },
1410 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 819 },
1411 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 820 },
1412 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 836 },
3230ee6b
PP
1413 { .type = TEST_EV_TYPE_NOTIF_PACKET_END, },
1414 { .type = TEST_EV_TYPE_NOTIF_STREAM_END, },
8deeabc6
PP
1415 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 857 },
1416 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 892 },
1417 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 903 },
1418 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 944 },
1419 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 998 },
3230ee6b
PP
1420 { .type = TEST_EV_TYPE_NOTIF_PACKET_END, },
1421 { .type = TEST_EV_TYPE_NOTIF_STREAM_END, },
8deeabc6
PP
1422 { .type = TEST_EV_TYPE_END, },
1423 { .type = TEST_EV_TYPE_SENTINEL, },
1424 };
1425
1426 clear_test_events();
1427 current_test = TEST_SINGLE_END_THEN_MULTIPLE_FULL;
1428 diag("test: single end then multiple full");
8deeabc6
PP
1429 graph = bt_graph_create();
1430 assert(graph);
36712f1d 1431 create_source_muxer_sink(graph, &src_comp, &muxer_comp, &sink_comp);
8deeabc6
PP
1432 graph_listener_data.graph = graph;
1433 graph_listener_data.source = src_comp;
1434 graph_listener_data.muxer = muxer_comp;
1435 graph_listener_data.sink = sink_comp;
1436 ret = bt_graph_add_port_added_listener(graph,
8cc092c9 1437 graph_port_added_listener_connect_to_avail_muxer_port, NULL,
8deeabc6 1438 &graph_listener_data);
0d107cdd 1439 assert(ret >= 0);
8deeabc6
PP
1440
1441 /* Connect source output ports to muxer input ports */
544d0515 1442 count = bt_component_source_get_output_port_count(src_comp);
8deeabc6
PP
1443 assert(ret == 0);
1444
1445 for (i = 0; i < count; i++) {
9ac68eb1 1446 upstream_port = bt_component_source_get_output_port_by_index(
8deeabc6
PP
1447 src_comp, i);
1448 assert(upstream_port);
1449 connect_port_to_first_avail_muxer_port(graph,
1450 upstream_port, muxer_comp);
1451 bt_put(upstream_port);
1452 }
1453
1454 /* Connect muxer output port to sink input port */
9ac68eb1 1455 upstream_port = bt_component_filter_get_output_port_by_name(muxer_comp,
8deeabc6
PP
1456 "out");
1457 assert(upstream_port);
b9d103be 1458 downstream_port = bt_component_sink_get_input_port_by_name(sink_comp, "in");
8deeabc6 1459 assert(downstream_port);
a256a42d
PP
1460 graph_status = bt_graph_connect_ports(graph, upstream_port,
1461 downstream_port, NULL);
1462 assert(graph_status == 0);
8deeabc6
PP
1463 bt_put(upstream_port);
1464 bt_put(downstream_port);
1465
1466 while (graph_status == BT_GRAPH_STATUS_OK ||
1467 graph_status == BT_GRAPH_STATUS_AGAIN) {
1468 graph_status = bt_graph_run(graph);
1469 }
1470
1471 ok(graph_status == BT_GRAPH_STATUS_END, "graph finishes without any error");
1472 ok(compare_test_events(expected_test_events),
1473 "the produced sequence of test events is the expected one");
1474
1475 bt_put(src_comp);
1476 bt_put(muxer_comp);
1477 bt_put(sink_comp);
1478 bt_put(graph);
1479}
1480
1481static
1482void test_single_again_end_then_multiple_full(void)
1483{
1484 struct bt_component *src_comp;
1485 struct bt_component *muxer_comp;
1486 struct bt_component *sink_comp;
1487 struct bt_port *upstream_port;
1488 struct bt_port *downstream_port;
1489 struct bt_graph *graph;
544d0515
PP
1490 int64_t i;
1491 int64_t count;
8deeabc6
PP
1492 int ret;
1493 enum bt_graph_status graph_status = BT_GRAPH_STATUS_OK;
1494 struct graph_listener_data graph_listener_data;
1495 const struct test_event expected_test_events[] = {
1496 { .type = TEST_EV_TYPE_AGAIN, },
3230ee6b
PP
1497 { .type = TEST_EV_TYPE_NOTIF_STREAM_BEGIN, },
1498 { .type = TEST_EV_TYPE_NOTIF_PACKET_BEGIN, },
1499 { .type = TEST_EV_TYPE_NOTIF_STREAM_BEGIN, },
1500 { .type = TEST_EV_TYPE_NOTIF_PACKET_BEGIN, },
8deeabc6
PP
1501 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 8 },
1502 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 51 },
1503 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 59 },
1504 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 68 },
1505 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 71 },
1506 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 77 },
1507 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 91 },
1508 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 121 },
1509 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 139 },
1510 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 170 },
1511 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 179 },
1512 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 209 },
1513 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 254 },
1514 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 266 },
1515 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 298 },
1516 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 320 },
1517 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 350 },
1518 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 352 },
1519 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 393 },
1520 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 419 },
1521 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 454 },
1522 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 478 },
1523 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 624 },
1524 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 631 },
1525 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 644 },
1526 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 651 },
1527 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 668 },
1528 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 678 },
1529 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 714 },
1530 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 717 },
1531 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 731 },
1532 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 733 },
1533 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 744 },
1534 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 750 },
1535 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 778 },
1536 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 788 },
1537 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 790 },
1538 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 819 },
1539 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 820 },
1540 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 836 },
3230ee6b
PP
1541 { .type = TEST_EV_TYPE_NOTIF_PACKET_END, },
1542 { .type = TEST_EV_TYPE_NOTIF_STREAM_END, },
8deeabc6
PP
1543 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 857 },
1544 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 892 },
1545 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 903 },
1546 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 944 },
1547 { .type = TEST_EV_TYPE_NOTIF_EVENT, .ts_ns = 998 },
3230ee6b
PP
1548 { .type = TEST_EV_TYPE_NOTIF_PACKET_END, },
1549 { .type = TEST_EV_TYPE_NOTIF_STREAM_END, },
8deeabc6
PP
1550 { .type = TEST_EV_TYPE_END, },
1551 { .type = TEST_EV_TYPE_SENTINEL, },
1552 };
1553
1554 clear_test_events();
1555 current_test = TEST_SINGLE_AGAIN_END_THEN_MULTIPLE_FULL;
1556 diag("test: single again then end then multiple full");
8deeabc6
PP
1557 graph = bt_graph_create();
1558 assert(graph);
36712f1d 1559 create_source_muxer_sink(graph, &src_comp, &muxer_comp, &sink_comp);
8deeabc6
PP
1560 graph_listener_data.graph = graph;
1561 graph_listener_data.source = src_comp;
1562 graph_listener_data.muxer = muxer_comp;
1563 graph_listener_data.sink = sink_comp;
1564 ret = bt_graph_add_port_added_listener(graph,
8cc092c9 1565 graph_port_added_listener_connect_to_avail_muxer_port, NULL,
8deeabc6 1566 &graph_listener_data);
0d107cdd 1567 assert(ret >= 0);
8deeabc6
PP
1568
1569 /* Connect source output ports to muxer input ports */
544d0515 1570 count = bt_component_source_get_output_port_count(src_comp);
8deeabc6
PP
1571 assert(ret == 0);
1572
1573 for (i = 0; i < count; i++) {
9ac68eb1 1574 upstream_port = bt_component_source_get_output_port_by_index(
8deeabc6
PP
1575 src_comp, i);
1576 assert(upstream_port);
1577 connect_port_to_first_avail_muxer_port(graph,
1578 upstream_port, muxer_comp);
1579 bt_put(upstream_port);
1580 }
1581
1582 /* Connect muxer output port to sink input port */
9ac68eb1 1583 upstream_port = bt_component_filter_get_output_port_by_name(muxer_comp,
8deeabc6
PP
1584 "out");
1585 assert(upstream_port);
b9d103be 1586 downstream_port = bt_component_sink_get_input_port_by_name(sink_comp, "in");
8deeabc6 1587 assert(downstream_port);
a256a42d
PP
1588 graph_status = bt_graph_connect_ports(graph, upstream_port,
1589 downstream_port, NULL);
1590 assert(graph_status == 0);
8deeabc6
PP
1591 bt_put(upstream_port);
1592 bt_put(downstream_port);
1593
1594 while (graph_status == BT_GRAPH_STATUS_OK ||
1595 graph_status == BT_GRAPH_STATUS_AGAIN) {
1596 graph_status = bt_graph_run(graph);
1597 }
1598
1599 ok(graph_status == BT_GRAPH_STATUS_END, "graph finishes without any error");
1600 ok(compare_test_events(expected_test_events),
1601 "the produced sequence of test events is the expected one");
1602
1603 bt_put(src_comp);
1604 bt_put(muxer_comp);
1605 bt_put(sink_comp);
1606 bt_put(graph);
1607}
1608
1609#define DEBUG_ENV_VAR "TEST_UTILS_MUXER_DEBUG"
1610
1611int main(int argc, char **argv)
1612{
1613 if (getenv(DEBUG_ENV_VAR) && strcmp(getenv(DEBUG_ENV_VAR), "1") == 0) {
1614 debug = true;
1615 }
1616
1617 plan_tests(NR_TESTS);
1618 init_static_data();
1619 test_no_ts();
1620 test_no_upstream_connection();
8deeabc6
PP
1621 test_simple_4_ports();
1622 test_4_ports_with_retries();
1623 test_single_end_then_multiple_full();
1624 test_single_again_end_then_multiple_full();
1625 fini_static_data();
1626 return exit_status();
1627}
This page took 0.09654 seconds and 4 git commands to generate.