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