2 * Copyright 2017 - Philippe Proulx <pproulx@efficios.com>
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.
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.
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.
25 #include <babeltrace/ctf-ir/event-class.h>
26 #include <babeltrace/ctf-ir/event.h>
27 #include <babeltrace/ctf-ir/field-types.h>
28 #include <babeltrace/ctf-ir/fields.h>
29 #include <babeltrace/ctf-ir/packet.h>
30 #include <babeltrace/ctf-ir/stream-class.h>
31 #include <babeltrace/ctf-ir/stream.h>
32 #include <babeltrace/ctf-ir/trace.h>
33 #include <babeltrace/graph/clock-class-priority-map.h>
34 #include <babeltrace/graph/component-class-filter.h>
35 #include <babeltrace/graph/component-class-sink.h>
36 #include <babeltrace/graph/component-class-source.h>
37 #include <babeltrace/graph/component-class.h>
38 #include <babeltrace/graph/component-sink.h>
39 #include <babeltrace/graph/component-source.h>
40 #include <babeltrace/graph/component.h>
41 #include <babeltrace/graph/connection.h>
42 #include <babeltrace/graph/graph.h>
43 #include <babeltrace/graph/notification-event.h>
44 #include <babeltrace/graph/notification-inactivity.h>
45 #include <babeltrace/graph/notification-iterator.h>
46 #include <babeltrace/graph/notification-packet.h>
47 #include <babeltrace/graph/notification-stream.h>
48 #include <babeltrace/graph/output-port-notification-iterator.h>
49 #include <babeltrace/graph/port.h>
50 #include <babeltrace/graph/private-component-source.h>
51 #include <babeltrace/graph/private-component-sink.h>
52 #include <babeltrace/graph/private-component.h>
53 #include <babeltrace/graph/private-connection.h>
54 #include <babeltrace/graph/private-connection-private-notification-iterator.h>
55 #include <babeltrace/graph/private-port.h>
56 #include <babeltrace/plugin/plugin.h>
57 #include <babeltrace/ref.h>
66 TEST_OUTPUT_PORT_NOTIFICATION_ITERATOR
,
69 enum test_event_type
{
70 TEST_EV_TYPE_NOTIF_UNEXPECTED
,
71 TEST_EV_TYPE_NOTIF_EVENT
,
72 TEST_EV_TYPE_NOTIF_INACTIVITY
,
73 TEST_EV_TYPE_NOTIF_STREAM_BEGIN
,
74 TEST_EV_TYPE_NOTIF_PACKET_BEGIN
,
75 TEST_EV_TYPE_NOTIF_PACKET_END
,
76 TEST_EV_TYPE_NOTIF_STREAM_END
,
78 TEST_EV_TYPE_SENTINEL
,
82 enum test_event_type type
;
83 struct bt_stream
*stream
;
84 struct bt_packet
*packet
;
87 static bool debug
= false;
88 static enum test current_test
;
89 static GArray
*test_events
;
90 static struct bt_clock_class_priority_map
*src_empty_cc_prio_map
;
91 static struct bt_stream_class
*src_stream_class
;
92 static struct bt_event_class
*src_event_class
;
93 static struct bt_stream
*src_stream1
;
94 static struct bt_stream
*src_stream2
;
95 static struct bt_packet
*src_stream1_packet1
;
96 static struct bt_packet
*src_stream1_packet2
;
97 static struct bt_packet
*src_stream2_packet1
;
98 static struct bt_packet
*src_stream2_packet2
;
102 SEQ_STREAM1_BEGIN
= -2,
103 SEQ_STREAM2_BEGIN
= -3,
104 SEQ_STREAM1_END
= -4,
105 SEQ_STREAM2_END
= -5,
106 SEQ_STREAM1_PACKET1_BEGIN
= -6,
107 SEQ_STREAM1_PACKET2_BEGIN
= -7,
108 SEQ_STREAM2_PACKET1_BEGIN
= -8,
109 SEQ_STREAM2_PACKET2_BEGIN
= -9,
110 SEQ_STREAM1_PACKET1_END
= -10,
111 SEQ_STREAM1_PACKET2_END
= -11,
112 SEQ_STREAM2_PACKET1_END
= -12,
113 SEQ_STREAM2_PACKET2_END
= -13,
114 SEQ_EVENT_STREAM1_PACKET1
= -14,
115 SEQ_EVENT_STREAM1_PACKET2
= -15,
116 SEQ_EVENT_STREAM2_PACKET1
= -16,
117 SEQ_EVENT_STREAM2_PACKET2
= -17,
118 SEQ_INACTIVITY
= -18,
121 struct src_iter_user_data
{
126 struct sink_user_data
{
127 struct bt_notification_iterator
*notif_iter
;
131 * No automatic notifications generated in this block.
132 * Stream 2 notifications are more indented.
134 static int64_t seq_no_auto_notifs
[] = {
136 SEQ_STREAM1_PACKET1_BEGIN
,
137 SEQ_EVENT_STREAM1_PACKET1
,
138 SEQ_EVENT_STREAM1_PACKET1
,
140 SEQ_EVENT_STREAM1_PACKET1
,
141 SEQ_STREAM2_PACKET2_BEGIN
,
142 SEQ_EVENT_STREAM2_PACKET2
,
143 SEQ_EVENT_STREAM1_PACKET1
,
144 SEQ_STREAM1_PACKET1_END
,
145 SEQ_STREAM2_PACKET2_END
,
146 SEQ_STREAM1_PACKET2_BEGIN
,
147 SEQ_EVENT_STREAM1_PACKET2
,
149 SEQ_STREAM1_PACKET2_END
,
155 void clear_test_events(void)
157 g_array_set_size(test_events
, 0);
161 void print_test_event(FILE *fp
, const struct test_event
*event
)
163 fprintf(fp
, "{ type = ");
165 switch (event
->type
) {
166 case TEST_EV_TYPE_NOTIF_UNEXPECTED
:
167 fprintf(fp
, "TEST_EV_TYPE_NOTIF_UNEXPECTED");
169 case TEST_EV_TYPE_NOTIF_EVENT
:
170 fprintf(fp
, "TEST_EV_TYPE_NOTIF_EVENT");
172 case TEST_EV_TYPE_NOTIF_INACTIVITY
:
173 fprintf(fp
, "TEST_EV_TYPE_NOTIF_INACTIVITY");
175 case TEST_EV_TYPE_NOTIF_STREAM_BEGIN
:
176 fprintf(fp
, "TEST_EV_TYPE_NOTIF_STREAM_BEGIN");
178 case TEST_EV_TYPE_NOTIF_STREAM_END
:
179 fprintf(fp
, "TEST_EV_TYPE_NOTIF_STREAM_END");
181 case TEST_EV_TYPE_NOTIF_PACKET_BEGIN
:
182 fprintf(fp
, "TEST_EV_TYPE_NOTIF_PACKET_BEGIN");
184 case TEST_EV_TYPE_NOTIF_PACKET_END
:
185 fprintf(fp
, "TEST_EV_TYPE_NOTIF_PACKET_END");
187 case TEST_EV_TYPE_END
:
188 fprintf(fp
, "TEST_EV_TYPE_END");
190 case TEST_EV_TYPE_SENTINEL
:
191 fprintf(fp
, "TEST_EV_TYPE_SENTINEL");
194 fprintf(fp
, "(UNKNOWN)");
198 fprintf(fp
, ", stream = %p, packet = %p }", event
->stream
,
203 void append_test_event(struct test_event
*event
)
205 g_array_append_val(test_events
, *event
);
209 bool compare_single_test_events(const struct test_event
*ev_a
,
210 const struct test_event
*ev_b
)
213 fprintf(stderr
, ":: Comparing test events: ");
214 print_test_event(stderr
, ev_a
);
215 fprintf(stderr
, " vs. ");
216 print_test_event(stderr
, ev_b
);
217 fprintf(stderr
, "\n");
220 if (ev_a
->type
!= ev_b
->type
) {
224 switch (ev_a
->type
) {
225 case TEST_EV_TYPE_END
:
226 case TEST_EV_TYPE_SENTINEL
:
229 if (ev_a
->stream
!= ev_b
->stream
) {
233 if (ev_a
->packet
!= ev_b
->packet
) {
243 bool compare_test_events(const struct test_event
*expected_events
)
245 const struct test_event
*expected_event
= expected_events
;
248 assert(expected_events
);
251 const struct test_event
*event
;
253 if (expected_event
->type
== TEST_EV_TYPE_SENTINEL
) {
257 if (i
>= test_events
->len
) {
261 event
= &g_array_index(test_events
, struct test_event
, i
);
263 if (!compare_single_test_events(event
, expected_event
)) {
271 if (i
!= test_events
->len
) {
279 void init_static_data(void)
282 struct bt_trace
*trace
;
283 struct bt_field_type
*empty_struct_ft
;
286 test_events
= g_array_new(FALSE
, TRUE
, sizeof(struct test_event
));
290 empty_struct_ft
= bt_field_type_structure_create();
291 assert(empty_struct_ft
);
292 trace
= bt_trace_create();
294 ret
= bt_trace_set_packet_header_field_type(trace
, empty_struct_ft
);
296 src_empty_cc_prio_map
= bt_clock_class_priority_map_create();
297 assert(src_empty_cc_prio_map
);
298 src_stream_class
= bt_stream_class_create("my-stream-class");
299 assert(src_stream_class
);
300 ret
= bt_stream_class_set_packet_context_field_type(src_stream_class
,
303 ret
= bt_stream_class_set_event_header_field_type(src_stream_class
,
306 ret
= bt_stream_class_set_event_context_field_type(src_stream_class
,
309 src_event_class
= bt_event_class_create("my-event-class");
310 ret
= bt_event_class_set_context_field_type(src_event_class
,
313 ret
= bt_event_class_set_payload_field_type(src_event_class
,
316 ret
= bt_stream_class_add_event_class(src_stream_class
,
319 ret
= bt_trace_add_stream_class(trace
, src_stream_class
);
321 src_stream1
= bt_stream_create(src_stream_class
, "stream-1", 0);
323 src_stream2
= bt_stream_create(src_stream_class
, "stream-2", 1);
325 src_stream1_packet1
= bt_packet_create(src_stream1
);
326 assert(src_stream1_packet1
);
327 src_stream1_packet2
= bt_packet_create(src_stream1
);
328 assert(src_stream1_packet2
);
329 src_stream2_packet1
= bt_packet_create(src_stream2
);
330 assert(src_stream2_packet1
);
331 src_stream2_packet2
= bt_packet_create(src_stream2
);
332 assert(src_stream2_packet2
);
335 fprintf(stderr
, ":: stream 1: %p\n", src_stream1
);
336 fprintf(stderr
, ":: stream 2: %p\n", src_stream2
);
337 fprintf(stderr
, ":: stream 1, packet 1: %p\n", src_stream1_packet1
);
338 fprintf(stderr
, ":: stream 1, packet 2: %p\n", src_stream1_packet2
);
339 fprintf(stderr
, ":: stream 2, packet 1: %p\n", src_stream2_packet1
);
340 fprintf(stderr
, ":: stream 2, packet 2: %p\n", src_stream2_packet2
);
344 bt_put(empty_struct_ft
);
348 void fini_static_data(void)
351 g_array_free(test_events
, TRUE
);
354 bt_put(src_empty_cc_prio_map
);
355 bt_put(src_stream_class
);
356 bt_put(src_event_class
);
359 bt_put(src_stream1_packet1
);
360 bt_put(src_stream1_packet2
);
361 bt_put(src_stream2_packet1
);
362 bt_put(src_stream2_packet2
);
366 void src_iter_finalize(
367 struct bt_private_connection_private_notification_iterator
*private_notification_iterator
)
369 struct src_iter_user_data
*user_data
=
370 bt_private_connection_private_notification_iterator_get_user_data(
371 private_notification_iterator
);
379 enum bt_notification_iterator_status
src_iter_init(
380 struct bt_private_connection_private_notification_iterator
*priv_notif_iter
,
381 struct bt_private_port
*private_port
)
383 struct src_iter_user_data
*user_data
=
384 g_new0(struct src_iter_user_data
, 1);
388 ret
= bt_private_connection_private_notification_iterator_set_user_data(
389 priv_notif_iter
, user_data
);
392 switch (current_test
) {
393 case TEST_NO_AUTO_NOTIFS
:
394 case TEST_OUTPUT_PORT_NOTIFICATION_ITERATOR
:
395 user_data
->seq
= seq_no_auto_notifs
;
401 return BT_NOTIFICATION_ITERATOR_STATUS_OK
;
405 struct bt_event
*src_create_event(struct bt_packet
*packet
)
407 struct bt_event
*event
= bt_event_create(src_event_class
);
411 ret
= bt_event_set_packet(event
, packet
);
417 struct bt_notification_iterator_next_method_return
src_iter_next_seq(
418 struct src_iter_user_data
*user_data
)
420 struct bt_notification_iterator_next_method_return next_return
= {
421 .status
= BT_NOTIFICATION_ITERATOR_STATUS_OK
,
424 struct bt_packet
*event_packet
= NULL
;
426 assert(user_data
->seq
);
427 cur_ts_ns
= user_data
->seq
[user_data
->at
];
432 BT_NOTIFICATION_ITERATOR_STATUS_END
;
435 next_return
.notification
=
436 bt_notification_inactivity_create(src_empty_cc_prio_map
);
437 assert(next_return
.notification
);
439 case SEQ_STREAM1_BEGIN
:
440 next_return
.notification
=
441 bt_notification_stream_begin_create(src_stream1
);
442 assert(next_return
.notification
);
444 case SEQ_STREAM2_BEGIN
:
445 next_return
.notification
=
446 bt_notification_stream_begin_create(src_stream2
);
447 assert(next_return
.notification
);
449 case SEQ_STREAM1_END
:
450 next_return
.notification
=
451 bt_notification_stream_end_create(src_stream1
);
452 assert(next_return
.notification
);
454 case SEQ_STREAM2_END
:
455 next_return
.notification
=
456 bt_notification_stream_end_create(src_stream2
);
457 assert(next_return
.notification
);
459 case SEQ_STREAM1_PACKET1_BEGIN
:
460 next_return
.notification
=
461 bt_notification_packet_begin_create(src_stream1_packet1
);
462 assert(next_return
.notification
);
464 case SEQ_STREAM1_PACKET2_BEGIN
:
465 next_return
.notification
=
466 bt_notification_packet_begin_create(src_stream1_packet2
);
467 assert(next_return
.notification
);
469 case SEQ_STREAM2_PACKET1_BEGIN
:
470 next_return
.notification
=
471 bt_notification_packet_begin_create(src_stream2_packet1
);
472 assert(next_return
.notification
);
474 case SEQ_STREAM2_PACKET2_BEGIN
:
475 next_return
.notification
=
476 bt_notification_packet_begin_create(src_stream2_packet2
);
477 assert(next_return
.notification
);
479 case SEQ_STREAM1_PACKET1_END
:
480 next_return
.notification
=
481 bt_notification_packet_end_create(src_stream1_packet1
);
482 assert(next_return
.notification
);
484 case SEQ_STREAM1_PACKET2_END
:
485 next_return
.notification
=
486 bt_notification_packet_end_create(src_stream1_packet2
);
487 assert(next_return
.notification
);
489 case SEQ_STREAM2_PACKET1_END
:
490 next_return
.notification
=
491 bt_notification_packet_end_create(src_stream2_packet1
);
492 assert(next_return
.notification
);
494 case SEQ_STREAM2_PACKET2_END
:
495 next_return
.notification
=
496 bt_notification_packet_end_create(src_stream2_packet2
);
497 assert(next_return
.notification
);
499 case SEQ_EVENT_STREAM1_PACKET1
:
500 event_packet
= src_stream1_packet1
;
502 case SEQ_EVENT_STREAM1_PACKET2
:
503 event_packet
= src_stream1_packet2
;
505 case SEQ_EVENT_STREAM2_PACKET1
:
506 event_packet
= src_stream2_packet1
;
508 case SEQ_EVENT_STREAM2_PACKET2
:
509 event_packet
= src_stream2_packet2
;
516 struct bt_event
*event
= src_create_event(event_packet
);
519 next_return
.notification
= bt_notification_event_create(event
,
520 src_empty_cc_prio_map
);
522 assert(next_return
.notification
);
525 if (next_return
.status
!= BT_NOTIFICATION_ITERATOR_STATUS_END
) {
533 struct bt_notification_iterator_next_method_return
src_iter_next(
534 struct bt_private_connection_private_notification_iterator
*priv_iterator
)
536 struct bt_notification_iterator_next_method_return next_return
= {
537 .status
= BT_NOTIFICATION_ITERATOR_STATUS_OK
,
538 .notification
= NULL
,
540 struct src_iter_user_data
*user_data
=
541 bt_private_connection_private_notification_iterator_get_user_data(priv_iterator
);
544 next_return
= src_iter_next_seq(user_data
);
549 enum bt_component_status
src_init(
550 struct bt_private_component
*private_component
,
551 struct bt_value
*params
, void *init_method_data
)
555 ret
= bt_private_component_source_add_output_private_port(
556 private_component
, "out", NULL
, NULL
);
558 return BT_COMPONENT_STATUS_OK
;
562 void src_finalize(struct bt_private_component
*private_component
)
567 enum bt_notification_iterator_status
common_consume(
568 struct bt_notification_iterator
*notif_iter
)
570 enum bt_notification_iterator_status ret
;
571 struct bt_notification
*notification
= NULL
;
572 struct test_event test_event
= { 0 };
573 bool do_append_test_event
= true;
576 ret
= bt_notification_iterator_next(notif_iter
);
578 do_append_test_event
= false;
583 case BT_NOTIFICATION_ITERATOR_STATUS_END
:
584 test_event
.type
= TEST_EV_TYPE_END
;
586 case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN
:
592 notification
= bt_notification_iterator_get_notification(
594 assert(notification
);
596 switch (bt_notification_get_type(notification
)) {
597 case BT_NOTIFICATION_TYPE_EVENT
:
599 struct bt_event
*event
;
601 test_event
.type
= TEST_EV_TYPE_NOTIF_EVENT
;
602 event
= bt_notification_event_get_event(notification
);
604 test_event
.packet
= bt_event_get_packet(event
);
606 assert(test_event
.packet
);
607 bt_put(test_event
.packet
);
610 case BT_NOTIFICATION_TYPE_INACTIVITY
:
611 test_event
.type
= TEST_EV_TYPE_NOTIF_INACTIVITY
;
613 case BT_NOTIFICATION_TYPE_STREAM_BEGIN
:
614 test_event
.type
= TEST_EV_TYPE_NOTIF_STREAM_BEGIN
;
616 bt_notification_stream_begin_get_stream(notification
);
617 assert(test_event
.stream
);
618 bt_put(test_event
.stream
);
620 case BT_NOTIFICATION_TYPE_STREAM_END
:
621 test_event
.type
= TEST_EV_TYPE_NOTIF_STREAM_END
;
623 bt_notification_stream_end_get_stream(notification
);
624 assert(test_event
.stream
);
625 bt_put(test_event
.stream
);
627 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
628 test_event
.type
= TEST_EV_TYPE_NOTIF_PACKET_BEGIN
;
630 bt_notification_packet_begin_get_packet(notification
);
631 assert(test_event
.packet
);
632 bt_put(test_event
.packet
);
634 case BT_NOTIFICATION_TYPE_PACKET_END
:
635 test_event
.type
= TEST_EV_TYPE_NOTIF_PACKET_END
;
637 bt_notification_packet_end_get_packet(notification
);
638 assert(test_event
.packet
);
639 bt_put(test_event
.packet
);
642 test_event
.type
= TEST_EV_TYPE_NOTIF_UNEXPECTED
;
646 if (test_event
.packet
) {
647 test_event
.stream
= bt_packet_get_stream(test_event
.packet
);
648 assert(test_event
.stream
);
649 bt_put(test_event
.stream
);
653 if (do_append_test_event
) {
654 append_test_event(&test_event
);
657 bt_put(notification
);
662 enum bt_component_status
sink_consume(
663 struct bt_private_component
*priv_component
)
665 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
666 struct sink_user_data
*user_data
=
667 bt_private_component_get_user_data(priv_component
);
668 enum bt_notification_iterator_status it_ret
;
670 assert(user_data
&& user_data
->notif_iter
);
671 it_ret
= common_consume(user_data
->notif_iter
);
674 ret
= BT_COMPONENT_STATUS_ERROR
;
679 case BT_NOTIFICATION_ITERATOR_STATUS_END
:
680 ret
= BT_COMPONENT_STATUS_END
;
681 BT_PUT(user_data
->notif_iter
);
683 case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN
:
694 void sink_port_connected(struct bt_private_component
*private_component
,
695 struct bt_private_port
*self_private_port
,
696 struct bt_port
*other_port
)
698 struct bt_private_connection
*priv_conn
=
699 bt_private_port_get_private_connection(self_private_port
);
700 struct sink_user_data
*user_data
= bt_private_component_get_user_data(
702 enum bt_connection_status conn_status
;
706 conn_status
= bt_private_connection_create_notification_iterator(
707 priv_conn
, &user_data
->notif_iter
);
708 assert(conn_status
== 0);
713 enum bt_component_status
sink_init(
714 struct bt_private_component
*private_component
,
715 struct bt_value
*params
, void *init_method_data
)
717 struct sink_user_data
*user_data
= g_new0(struct sink_user_data
, 1);
721 ret
= bt_private_component_set_user_data(private_component
,
724 ret
= bt_private_component_sink_add_input_private_port(
725 private_component
, "in", NULL
, NULL
);
727 return BT_COMPONENT_STATUS_OK
;
731 void sink_finalize(struct bt_private_component
*private_component
)
733 struct sink_user_data
*user_data
= bt_private_component_get_user_data(
737 bt_put(user_data
->notif_iter
);
743 void create_source_sink(struct bt_graph
*graph
, struct bt_component
**source
,
744 struct bt_component
**sink
)
746 struct bt_component_class
*src_comp_class
;
747 struct bt_component_class
*sink_comp_class
;
750 /* Create source component */
752 src_comp_class
= bt_component_class_source_create("src",
754 assert(src_comp_class
);
755 ret
= bt_component_class_set_init_method(src_comp_class
,
758 ret
= bt_component_class_set_finalize_method(src_comp_class
,
761 ret
= bt_component_class_source_set_notification_iterator_init_method(
762 src_comp_class
, src_iter_init
);
764 ret
= bt_component_class_source_set_notification_iterator_finalize_method(
765 src_comp_class
, src_iter_finalize
);
767 ret
= bt_graph_add_component(graph
, src_comp_class
, "source",
770 bt_put(src_comp_class
);
773 /* Create sink component */
775 sink_comp_class
= bt_component_class_sink_create("sink",
777 assert(sink_comp_class
);
778 ret
= bt_component_class_set_init_method(sink_comp_class
,
781 ret
= bt_component_class_set_finalize_method(sink_comp_class
,
783 ret
= bt_component_class_set_port_connected_method(
784 sink_comp_class
, sink_port_connected
);
786 ret
= bt_graph_add_component(graph
, sink_comp_class
, "sink",
789 bt_put(sink_comp_class
);
794 void do_std_test(enum test test
, const char *name
,
795 const struct test_event
*expected_test_events
)
797 struct bt_component
*src_comp
;
798 struct bt_component
*sink_comp
;
799 struct bt_port
*upstream_port
;
800 struct bt_port
*downstream_port
;
801 struct bt_graph
*graph
;
802 enum bt_graph_status graph_status
= BT_GRAPH_STATUS_OK
;
806 diag("test: %s", name
);
807 graph
= bt_graph_create();
809 create_source_sink(graph
, &src_comp
, &sink_comp
);
811 /* Connect source to sink */
812 upstream_port
= bt_component_source_get_output_port_by_name(src_comp
, "out");
813 assert(upstream_port
);
814 downstream_port
= bt_component_sink_get_input_port_by_name(sink_comp
, "in");
815 assert(downstream_port
);
816 graph_status
= bt_graph_connect_ports(graph
, upstream_port
,
817 downstream_port
, NULL
);
818 bt_put(upstream_port
);
819 bt_put(downstream_port
);
821 /* Run the graph until the end */
822 while (graph_status
== BT_GRAPH_STATUS_OK
||
823 graph_status
== BT_GRAPH_STATUS_AGAIN
) {
824 graph_status
= bt_graph_run(graph
);
827 ok(graph_status
== BT_GRAPH_STATUS_END
, "graph finishes without any error");
829 /* Compare the resulting test events */
830 if (expected_test_events
) {
831 ok(compare_test_events(expected_test_events
),
832 "the produced sequence of test events is the expected one");
841 void test_no_auto_notifs(void)
843 const struct test_event expected_test_events
[] = {
844 { .type
= TEST_EV_TYPE_NOTIF_STREAM_BEGIN
, .stream
= src_stream1
, .packet
= NULL
, },
845 { .type
= TEST_EV_TYPE_NOTIF_PACKET_BEGIN
, .stream
= src_stream1
, .packet
= src_stream1_packet1
, },
846 { .type
= TEST_EV_TYPE_NOTIF_EVENT
, .stream
= src_stream1
, .packet
= src_stream1_packet1
, },
847 { .type
= TEST_EV_TYPE_NOTIF_EVENT
, .stream
= src_stream1
, .packet
= src_stream1_packet1
, },
848 { .type
= TEST_EV_TYPE_NOTIF_STREAM_BEGIN
, .stream
= src_stream2
, .packet
= NULL
, },
849 { .type
= TEST_EV_TYPE_NOTIF_EVENT
, .stream
= src_stream1
, .packet
= src_stream1_packet1
, },
850 { .type
= TEST_EV_TYPE_NOTIF_PACKET_BEGIN
, .stream
= src_stream2
, .packet
= src_stream2_packet2
, },
851 { .type
= TEST_EV_TYPE_NOTIF_EVENT
, .stream
= src_stream2
, .packet
= src_stream2_packet2
, },
852 { .type
= TEST_EV_TYPE_NOTIF_EVENT
, .stream
= src_stream1
, .packet
= src_stream1_packet1
, },
853 { .type
= TEST_EV_TYPE_NOTIF_PACKET_END
, .stream
= src_stream1
, .packet
= src_stream1_packet1
, },
854 { .type
= TEST_EV_TYPE_NOTIF_PACKET_END
, .stream
= src_stream2
, .packet
= src_stream2_packet2
, },
855 { .type
= TEST_EV_TYPE_NOTIF_PACKET_BEGIN
, .stream
= src_stream1
, .packet
= src_stream1_packet2
, },
856 { .type
= TEST_EV_TYPE_NOTIF_EVENT
, .stream
= src_stream1
, .packet
= src_stream1_packet2
, },
857 { .type
= TEST_EV_TYPE_NOTIF_STREAM_END
, .stream
= src_stream2
, .packet
= NULL
, },
858 { .type
= TEST_EV_TYPE_NOTIF_PACKET_END
, .stream
= src_stream1
, .packet
= src_stream1_packet2
, },
859 { .type
= TEST_EV_TYPE_NOTIF_STREAM_END
, .stream
= src_stream1
, .packet
= NULL
, },
860 { .type
= TEST_EV_TYPE_END
, },
861 { .type
= TEST_EV_TYPE_SENTINEL
, },
864 do_std_test(TEST_NO_AUTO_NOTIFS
, "no automatic notifications",
865 expected_test_events
);
869 void test_output_port_notification_iterator(void)
871 const struct test_event expected_test_events
[] = {
872 { .type
= TEST_EV_TYPE_NOTIF_STREAM_BEGIN
, .stream
= src_stream1
, .packet
= NULL
, },
873 { .type
= TEST_EV_TYPE_NOTIF_PACKET_BEGIN
, .stream
= src_stream1
, .packet
= src_stream1_packet1
, },
874 { .type
= TEST_EV_TYPE_NOTIF_EVENT
, .stream
= src_stream1
, .packet
= src_stream1_packet1
, },
875 { .type
= TEST_EV_TYPE_NOTIF_EVENT
, .stream
= src_stream1
, .packet
= src_stream1_packet1
, },
876 { .type
= TEST_EV_TYPE_NOTIF_STREAM_BEGIN
, .stream
= src_stream2
, .packet
= NULL
, },
877 { .type
= TEST_EV_TYPE_NOTIF_EVENT
, .stream
= src_stream1
, .packet
= src_stream1_packet1
, },
878 { .type
= TEST_EV_TYPE_NOTIF_PACKET_BEGIN
, .stream
= src_stream2
, .packet
= src_stream2_packet2
, },
879 { .type
= TEST_EV_TYPE_NOTIF_EVENT
, .stream
= src_stream2
, .packet
= src_stream2_packet2
, },
880 { .type
= TEST_EV_TYPE_NOTIF_EVENT
, .stream
= src_stream1
, .packet
= src_stream1_packet1
, },
881 { .type
= TEST_EV_TYPE_NOTIF_PACKET_END
, .stream
= src_stream1
, .packet
= src_stream1_packet1
, },
882 { .type
= TEST_EV_TYPE_NOTIF_PACKET_END
, .stream
= src_stream2
, .packet
= src_stream2_packet2
, },
883 { .type
= TEST_EV_TYPE_NOTIF_PACKET_BEGIN
, .stream
= src_stream1
, .packet
= src_stream1_packet2
, },
884 { .type
= TEST_EV_TYPE_NOTIF_EVENT
, .stream
= src_stream1
, .packet
= src_stream1_packet2
, },
885 { .type
= TEST_EV_TYPE_NOTIF_STREAM_END
, .stream
= src_stream2
, .packet
= NULL
, },
886 { .type
= TEST_EV_TYPE_NOTIF_PACKET_END
, .stream
= src_stream1
, .packet
= src_stream1_packet2
, },
887 { .type
= TEST_EV_TYPE_NOTIF_STREAM_END
, .stream
= src_stream1
, .packet
= NULL
, },
888 { .type
= TEST_EV_TYPE_END
, },
889 { .type
= TEST_EV_TYPE_SENTINEL
, },
891 struct bt_component
*src_comp
;
892 struct bt_notification_iterator
*notif_iter
;
893 enum bt_notification_iterator_status iter_status
=
894 BT_NOTIFICATION_ITERATOR_STATUS_OK
;
895 struct bt_port
*upstream_port
;
896 struct bt_graph
*graph
;
899 current_test
= TEST_OUTPUT_PORT_NOTIFICATION_ITERATOR
;
900 diag("test: output port notification iterator");
901 graph
= bt_graph_create();
903 create_source_sink(graph
, &src_comp
, NULL
);
905 /* Create notification iterator on source's output port */
906 upstream_port
= bt_component_source_get_output_port_by_name(src_comp
, "out");
907 notif_iter
= bt_output_port_notification_iterator_create(upstream_port
,
909 ok(notif_iter
, "bt_output_port_notification_iterator_create() succeeds");
910 bt_put(upstream_port
);
912 /* Consume the notification iterator */
913 while (iter_status
== BT_NOTIFICATION_ITERATOR_STATUS_OK
) {
914 iter_status
= common_consume(notif_iter
);
917 ok(iter_status
== BT_NOTIFICATION_ITERATOR_STATUS_END
,
918 "output port notification iterator finishes without any error");
920 /* Compare the resulting test events */
921 ok(compare_test_events(expected_test_events
),
922 "the produced sequence of test events is the expected one");
930 void test_output_port_notification_iterator_cannot_consume(void)
932 struct bt_component
*src_comp
;
933 struct bt_notification_iterator
*notif_iter
;
934 struct bt_port
*upstream_port
;
935 struct bt_graph
*graph
;
938 current_test
= TEST_OUTPUT_PORT_NOTIFICATION_ITERATOR
;
939 diag("test: cannot consume graph with existing output port notification iterator");
940 graph
= bt_graph_create();
942 create_source_sink(graph
, &src_comp
, NULL
);
944 /* Create notification iterator on source's output port */
945 upstream_port
= bt_component_source_get_output_port_by_name(src_comp
, "out");
946 notif_iter
= bt_output_port_notification_iterator_create(upstream_port
,
949 bt_put(upstream_port
);
952 * This should fail because the graph is now managed by the
953 * notification iterator.
955 ok(bt_graph_run(graph
) == BT_GRAPH_STATUS_CANNOT_CONSUME
,
956 "bt_graph_run() returns BT_GRAPH_STATUS_CANNOT_CONSUME when there's an output port notification iterator");
963 #define DEBUG_ENV_VAR "TEST_BT_NOTIFICATION_ITERATOR_DEBUG"
965 int main(int argc
, char **argv
)
967 if (getenv(DEBUG_ENV_VAR
) && strcmp(getenv(DEBUG_ENV_VAR
), "1") == 0) {
971 plan_tests(NR_TESTS
);
973 test_no_auto_notifs();
974 test_output_port_notification_iterator();
975 test_output_port_notification_iterator_cannot_consume();
977 return exit_status();