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.
24 #include <babeltrace/assert-internal.h>
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-notification-iterator.h>
55 #include <babeltrace/graph/private-connection-private-notification-iterator.h>
56 #include <babeltrace/graph/private-port.h>
57 #include <babeltrace/plugin/plugin.h>
58 #include <babeltrace/ref.h>
67 TEST_OUTPUT_PORT_NOTIFICATION_ITERATOR
,
70 enum test_event_type
{
71 TEST_EV_TYPE_NOTIF_UNEXPECTED
,
72 TEST_EV_TYPE_NOTIF_EVENT
,
73 TEST_EV_TYPE_NOTIF_INACTIVITY
,
74 TEST_EV_TYPE_NOTIF_STREAM_BEGIN
,
75 TEST_EV_TYPE_NOTIF_PACKET_BEGIN
,
76 TEST_EV_TYPE_NOTIF_PACKET_END
,
77 TEST_EV_TYPE_NOTIF_STREAM_END
,
79 TEST_EV_TYPE_SENTINEL
,
83 enum test_event_type type
;
84 struct bt_stream
*stream
;
85 struct bt_packet
*packet
;
88 static bool debug
= false;
89 static enum test current_test
;
90 static GArray
*test_events
;
91 static struct bt_graph
*graph
;
92 static struct bt_clock_class_priority_map
*src_empty_cc_prio_map
;
93 static struct bt_stream_class
*src_stream_class
;
94 static struct bt_event_class
*src_event_class
;
95 static struct bt_stream
*src_stream1
;
96 static struct bt_stream
*src_stream2
;
97 static struct bt_packet
*src_stream1_packet1
;
98 static struct bt_packet
*src_stream1_packet2
;
99 static struct bt_packet
*src_stream2_packet1
;
100 static struct bt_packet
*src_stream2_packet2
;
104 SEQ_STREAM1_BEGIN
= -2,
105 SEQ_STREAM2_BEGIN
= -3,
106 SEQ_STREAM1_END
= -4,
107 SEQ_STREAM2_END
= -5,
108 SEQ_STREAM1_PACKET1_BEGIN
= -6,
109 SEQ_STREAM1_PACKET2_BEGIN
= -7,
110 SEQ_STREAM2_PACKET1_BEGIN
= -8,
111 SEQ_STREAM2_PACKET2_BEGIN
= -9,
112 SEQ_STREAM1_PACKET1_END
= -10,
113 SEQ_STREAM1_PACKET2_END
= -11,
114 SEQ_STREAM2_PACKET1_END
= -12,
115 SEQ_STREAM2_PACKET2_END
= -13,
116 SEQ_EVENT_STREAM1_PACKET1
= -14,
117 SEQ_EVENT_STREAM1_PACKET2
= -15,
118 SEQ_EVENT_STREAM2_PACKET1
= -16,
119 SEQ_EVENT_STREAM2_PACKET2
= -17,
120 SEQ_INACTIVITY
= -18,
123 struct src_iter_user_data
{
128 struct sink_user_data
{
129 struct bt_notification_iterator
*notif_iter
;
133 * No automatic notifications generated in this block.
134 * Stream 2 notifications are more indented.
136 static int64_t seq_no_auto_notifs
[] = {
138 SEQ_STREAM1_PACKET1_BEGIN
,
139 SEQ_EVENT_STREAM1_PACKET1
,
140 SEQ_EVENT_STREAM1_PACKET1
,
142 SEQ_EVENT_STREAM1_PACKET1
,
143 SEQ_STREAM2_PACKET2_BEGIN
,
144 SEQ_EVENT_STREAM2_PACKET2
,
145 SEQ_EVENT_STREAM1_PACKET1
,
146 SEQ_STREAM1_PACKET1_END
,
147 SEQ_STREAM2_PACKET2_END
,
148 SEQ_STREAM1_PACKET2_BEGIN
,
149 SEQ_EVENT_STREAM1_PACKET2
,
151 SEQ_STREAM1_PACKET2_END
,
157 void clear_test_events(void)
159 g_array_set_size(test_events
, 0);
163 void print_test_event(FILE *fp
, const struct test_event
*event
)
165 fprintf(fp
, "{ type = ");
167 switch (event
->type
) {
168 case TEST_EV_TYPE_NOTIF_UNEXPECTED
:
169 fprintf(fp
, "TEST_EV_TYPE_NOTIF_UNEXPECTED");
171 case TEST_EV_TYPE_NOTIF_EVENT
:
172 fprintf(fp
, "TEST_EV_TYPE_NOTIF_EVENT");
174 case TEST_EV_TYPE_NOTIF_INACTIVITY
:
175 fprintf(fp
, "TEST_EV_TYPE_NOTIF_INACTIVITY");
177 case TEST_EV_TYPE_NOTIF_STREAM_BEGIN
:
178 fprintf(fp
, "TEST_EV_TYPE_NOTIF_STREAM_BEGIN");
180 case TEST_EV_TYPE_NOTIF_STREAM_END
:
181 fprintf(fp
, "TEST_EV_TYPE_NOTIF_STREAM_END");
183 case TEST_EV_TYPE_NOTIF_PACKET_BEGIN
:
184 fprintf(fp
, "TEST_EV_TYPE_NOTIF_PACKET_BEGIN");
186 case TEST_EV_TYPE_NOTIF_PACKET_END
:
187 fprintf(fp
, "TEST_EV_TYPE_NOTIF_PACKET_END");
189 case TEST_EV_TYPE_END
:
190 fprintf(fp
, "TEST_EV_TYPE_END");
192 case TEST_EV_TYPE_SENTINEL
:
193 fprintf(fp
, "TEST_EV_TYPE_SENTINEL");
196 fprintf(fp
, "(UNKNOWN)");
200 fprintf(fp
, ", stream = %p, packet = %p }", event
->stream
,
205 void append_test_event(struct test_event
*event
)
207 g_array_append_val(test_events
, *event
);
211 bool compare_single_test_events(const struct test_event
*ev_a
,
212 const struct test_event
*ev_b
)
215 fprintf(stderr
, ":: Comparing test events: ");
216 print_test_event(stderr
, ev_a
);
217 fprintf(stderr
, " vs. ");
218 print_test_event(stderr
, ev_b
);
219 fprintf(stderr
, "\n");
222 if (ev_a
->type
!= ev_b
->type
) {
226 switch (ev_a
->type
) {
227 case TEST_EV_TYPE_END
:
228 case TEST_EV_TYPE_SENTINEL
:
231 if (ev_a
->stream
!= ev_b
->stream
) {
235 if (ev_a
->packet
!= ev_b
->packet
) {
245 bool compare_test_events(const struct test_event
*expected_events
)
247 const struct test_event
*expected_event
= expected_events
;
250 BT_ASSERT(expected_events
);
253 const struct test_event
*event
;
255 if (expected_event
->type
== TEST_EV_TYPE_SENTINEL
) {
259 if (i
>= test_events
->len
) {
263 event
= &g_array_index(test_events
, struct test_event
, i
);
265 if (!compare_single_test_events(event
, expected_event
)) {
273 if (i
!= test_events
->len
) {
281 void init_static_data(void)
284 struct bt_trace
*trace
;
285 struct bt_field_type
*empty_struct_ft
;
288 test_events
= g_array_new(FALSE
, TRUE
, sizeof(struct test_event
));
289 BT_ASSERT(test_events
);
292 empty_struct_ft
= bt_field_type_structure_create();
293 BT_ASSERT(empty_struct_ft
);
294 trace
= bt_trace_create();
296 ret
= bt_trace_set_packet_header_field_type(trace
, empty_struct_ft
);
298 src_empty_cc_prio_map
= bt_clock_class_priority_map_create();
299 BT_ASSERT(src_empty_cc_prio_map
);
300 src_stream_class
= bt_stream_class_create("my-stream-class");
301 BT_ASSERT(src_stream_class
);
302 ret
= bt_stream_class_set_packet_context_field_type(src_stream_class
,
305 ret
= bt_stream_class_set_event_header_field_type(src_stream_class
,
308 ret
= bt_stream_class_set_event_context_field_type(src_stream_class
,
311 src_event_class
= bt_event_class_create("my-event-class");
312 ret
= bt_event_class_set_context_field_type(src_event_class
,
315 ret
= bt_event_class_set_payload_field_type(src_event_class
,
318 ret
= bt_stream_class_add_event_class(src_stream_class
,
321 ret
= bt_trace_add_stream_class(trace
, src_stream_class
);
323 src_stream1
= bt_stream_create(src_stream_class
, "stream-1", 0);
324 BT_ASSERT(src_stream1
);
325 src_stream2
= bt_stream_create(src_stream_class
, "stream-2", 1);
326 BT_ASSERT(src_stream2
);
327 src_stream1_packet1
= bt_packet_create(src_stream1
);
328 BT_ASSERT(src_stream1_packet1
);
329 src_stream1_packet2
= bt_packet_create(src_stream1
);
330 BT_ASSERT(src_stream1_packet2
);
331 src_stream2_packet1
= bt_packet_create(src_stream2
);
332 BT_ASSERT(src_stream2_packet1
);
333 src_stream2_packet2
= bt_packet_create(src_stream2
);
334 BT_ASSERT(src_stream2_packet2
);
337 fprintf(stderr
, ":: stream 1: %p\n", src_stream1
);
338 fprintf(stderr
, ":: stream 2: %p\n", src_stream2
);
339 fprintf(stderr
, ":: stream 1, packet 1: %p\n", src_stream1_packet1
);
340 fprintf(stderr
, ":: stream 1, packet 2: %p\n", src_stream1_packet2
);
341 fprintf(stderr
, ":: stream 2, packet 1: %p\n", src_stream2_packet1
);
342 fprintf(stderr
, ":: stream 2, packet 2: %p\n", src_stream2_packet2
);
346 bt_put(empty_struct_ft
);
350 void fini_static_data(void)
353 g_array_free(test_events
, TRUE
);
356 bt_put(src_empty_cc_prio_map
);
357 bt_put(src_stream_class
);
358 bt_put(src_event_class
);
361 bt_put(src_stream1_packet1
);
362 bt_put(src_stream1_packet2
);
363 bt_put(src_stream2_packet1
);
364 bt_put(src_stream2_packet2
);
368 void src_iter_finalize(
369 struct bt_private_connection_private_notification_iterator
*private_notification_iterator
)
371 struct src_iter_user_data
*user_data
=
372 bt_private_connection_private_notification_iterator_get_user_data(
373 private_notification_iterator
);
381 enum bt_notification_iterator_status
src_iter_init(
382 struct bt_private_connection_private_notification_iterator
*priv_notif_iter
,
383 struct bt_private_port
*private_port
)
385 struct src_iter_user_data
*user_data
=
386 g_new0(struct src_iter_user_data
, 1);
389 BT_ASSERT(user_data
);
390 ret
= bt_private_connection_private_notification_iterator_set_user_data(
391 priv_notif_iter
, user_data
);
394 switch (current_test
) {
395 case TEST_NO_AUTO_NOTIFS
:
396 case TEST_OUTPUT_PORT_NOTIFICATION_ITERATOR
:
397 user_data
->seq
= seq_no_auto_notifs
;
403 return BT_NOTIFICATION_ITERATOR_STATUS_OK
;
407 struct bt_notification_iterator_next_method_return
src_iter_next_seq(
408 struct src_iter_user_data
*user_data
)
410 struct bt_notification_iterator_next_method_return next_return
= {
411 .status
= BT_NOTIFICATION_ITERATOR_STATUS_OK
,
414 struct bt_packet
*event_packet
= NULL
;
416 BT_ASSERT(user_data
->seq
);
417 cur_ts_ns
= user_data
->seq
[user_data
->at
];
422 BT_NOTIFICATION_ITERATOR_STATUS_END
;
425 next_return
.notification
=
426 bt_notification_inactivity_create(graph
,
427 src_empty_cc_prio_map
);
428 BT_ASSERT(next_return
.notification
);
430 case SEQ_STREAM1_BEGIN
:
431 next_return
.notification
=
432 bt_notification_stream_begin_create(graph
, src_stream1
);
433 BT_ASSERT(next_return
.notification
);
435 case SEQ_STREAM2_BEGIN
:
436 next_return
.notification
=
437 bt_notification_stream_begin_create(graph
, src_stream2
);
438 BT_ASSERT(next_return
.notification
);
440 case SEQ_STREAM1_END
:
441 next_return
.notification
=
442 bt_notification_stream_end_create(graph
, src_stream1
);
443 BT_ASSERT(next_return
.notification
);
445 case SEQ_STREAM2_END
:
446 next_return
.notification
=
447 bt_notification_stream_end_create(graph
, src_stream2
);
448 BT_ASSERT(next_return
.notification
);
450 case SEQ_STREAM1_PACKET1_BEGIN
:
451 next_return
.notification
=
452 bt_notification_packet_begin_create(graph
,
453 src_stream1_packet1
);
454 BT_ASSERT(next_return
.notification
);
456 case SEQ_STREAM1_PACKET2_BEGIN
:
457 next_return
.notification
=
458 bt_notification_packet_begin_create(graph
,
459 src_stream1_packet2
);
460 BT_ASSERT(next_return
.notification
);
462 case SEQ_STREAM2_PACKET1_BEGIN
:
463 next_return
.notification
=
464 bt_notification_packet_begin_create(graph
,
465 src_stream2_packet1
);
466 BT_ASSERT(next_return
.notification
);
468 case SEQ_STREAM2_PACKET2_BEGIN
:
469 next_return
.notification
=
470 bt_notification_packet_begin_create(graph
,
471 src_stream2_packet2
);
472 BT_ASSERT(next_return
.notification
);
474 case SEQ_STREAM1_PACKET1_END
:
475 next_return
.notification
=
476 bt_notification_packet_end_create(graph
,
477 src_stream1_packet1
);
478 BT_ASSERT(next_return
.notification
);
480 case SEQ_STREAM1_PACKET2_END
:
481 next_return
.notification
=
482 bt_notification_packet_end_create(graph
,
483 src_stream1_packet2
);
484 BT_ASSERT(next_return
.notification
);
486 case SEQ_STREAM2_PACKET1_END
:
487 next_return
.notification
=
488 bt_notification_packet_end_create(graph
,
489 src_stream2_packet1
);
490 BT_ASSERT(next_return
.notification
);
492 case SEQ_STREAM2_PACKET2_END
:
493 next_return
.notification
=
494 bt_notification_packet_end_create(graph
,
495 src_stream2_packet2
);
496 BT_ASSERT(next_return
.notification
);
498 case SEQ_EVENT_STREAM1_PACKET1
:
499 event_packet
= src_stream1_packet1
;
501 case SEQ_EVENT_STREAM1_PACKET2
:
502 event_packet
= src_stream1_packet2
;
504 case SEQ_EVENT_STREAM2_PACKET1
:
505 event_packet
= src_stream2_packet1
;
507 case SEQ_EVENT_STREAM2_PACKET2
:
508 event_packet
= src_stream2_packet2
;
515 next_return
.notification
=
516 bt_notification_event_create(graph
, src_event_class
,
517 event_packet
, src_empty_cc_prio_map
);
518 BT_ASSERT(next_return
.notification
);
521 if (next_return
.status
!= BT_NOTIFICATION_ITERATOR_STATUS_END
) {
529 struct bt_notification_iterator_next_method_return
src_iter_next(
530 struct bt_private_connection_private_notification_iterator
*priv_iterator
)
532 struct bt_notification_iterator_next_method_return next_return
= {
533 .status
= BT_NOTIFICATION_ITERATOR_STATUS_OK
,
534 .notification
= NULL
,
536 struct src_iter_user_data
*user_data
=
537 bt_private_connection_private_notification_iterator_get_user_data(priv_iterator
);
539 BT_ASSERT(user_data
);
540 next_return
= src_iter_next_seq(user_data
);
545 enum bt_component_status
src_init(
546 struct bt_private_component
*private_component
,
547 struct bt_value
*params
, void *init_method_data
)
551 ret
= bt_private_component_source_add_output_private_port(
552 private_component
, "out", NULL
, NULL
);
554 return BT_COMPONENT_STATUS_OK
;
558 void src_finalize(struct bt_private_component
*private_component
)
563 enum bt_notification_iterator_status
common_consume(
564 struct bt_notification_iterator
*notif_iter
,
565 bool is_output_port_notif_iter
)
567 enum bt_notification_iterator_status ret
;
568 struct bt_notification
*notification
= NULL
;
569 struct test_event test_event
= { 0 };
570 bool do_append_test_event
= true;
571 BT_ASSERT(notif_iter
);
573 if (is_output_port_notif_iter
) {
574 ret
= bt_output_port_notification_iterator_next(notif_iter
,
577 ret
= bt_private_connection_notification_iterator_next(
578 notif_iter
, ¬ification
);
582 do_append_test_event
= false;
587 case BT_NOTIFICATION_ITERATOR_STATUS_END
:
588 test_event
.type
= TEST_EV_TYPE_END
;
590 case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN
:
596 BT_ASSERT(notification
);
598 switch (bt_notification_get_type(notification
)) {
599 case BT_NOTIFICATION_TYPE_EVENT
:
601 struct bt_event
*event
;
603 test_event
.type
= TEST_EV_TYPE_NOTIF_EVENT
;
604 event
= bt_notification_event_borrow_event(notification
);
606 test_event
.packet
= bt_event_borrow_packet(event
);
607 BT_ASSERT(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_borrow_stream(notification
);
617 BT_ASSERT(test_event
.stream
);
619 case BT_NOTIFICATION_TYPE_STREAM_END
:
620 test_event
.type
= TEST_EV_TYPE_NOTIF_STREAM_END
;
622 bt_notification_stream_end_borrow_stream(notification
);
623 BT_ASSERT(test_event
.stream
);
625 case BT_NOTIFICATION_TYPE_PACKET_BEGIN
:
626 test_event
.type
= TEST_EV_TYPE_NOTIF_PACKET_BEGIN
;
628 bt_notification_packet_begin_borrow_packet(notification
);
629 BT_ASSERT(test_event
.packet
);
631 case BT_NOTIFICATION_TYPE_PACKET_END
:
632 test_event
.type
= TEST_EV_TYPE_NOTIF_PACKET_END
;
634 bt_notification_packet_end_borrow_packet(notification
);
635 BT_ASSERT(test_event
.packet
);
638 test_event
.type
= TEST_EV_TYPE_NOTIF_UNEXPECTED
;
642 if (test_event
.packet
) {
643 test_event
.stream
= bt_packet_borrow_stream(test_event
.packet
);
644 BT_ASSERT(test_event
.stream
);
648 if (do_append_test_event
) {
649 append_test_event(&test_event
);
652 bt_put(notification
);
657 enum bt_component_status
sink_consume(
658 struct bt_private_component
*priv_component
)
660 enum bt_component_status ret
= BT_COMPONENT_STATUS_OK
;
661 struct sink_user_data
*user_data
=
662 bt_private_component_get_user_data(priv_component
);
663 enum bt_notification_iterator_status it_ret
;
665 BT_ASSERT(user_data
&& user_data
->notif_iter
);
666 it_ret
= common_consume(user_data
->notif_iter
, false);
669 ret
= BT_COMPONENT_STATUS_ERROR
;
674 case BT_NOTIFICATION_ITERATOR_STATUS_END
:
675 ret
= BT_COMPONENT_STATUS_END
;
676 BT_PUT(user_data
->notif_iter
);
678 case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN
:
689 void sink_port_connected(struct bt_private_component
*private_component
,
690 struct bt_private_port
*self_private_port
,
691 struct bt_port
*other_port
)
693 struct bt_private_connection
*priv_conn
=
694 bt_private_port_get_private_connection(self_private_port
);
695 struct sink_user_data
*user_data
= bt_private_component_get_user_data(
697 enum bt_connection_status conn_status
;
699 BT_ASSERT(user_data
);
700 BT_ASSERT(priv_conn
);
701 conn_status
= bt_private_connection_create_notification_iterator(
702 priv_conn
, &user_data
->notif_iter
);
703 BT_ASSERT(conn_status
== 0);
708 enum bt_component_status
sink_init(
709 struct bt_private_component
*private_component
,
710 struct bt_value
*params
, void *init_method_data
)
712 struct sink_user_data
*user_data
= g_new0(struct sink_user_data
, 1);
715 BT_ASSERT(user_data
);
716 ret
= bt_private_component_set_user_data(private_component
,
719 ret
= bt_private_component_sink_add_input_private_port(
720 private_component
, "in", NULL
, NULL
);
722 return BT_COMPONENT_STATUS_OK
;
726 void sink_finalize(struct bt_private_component
*private_component
)
728 struct sink_user_data
*user_data
= bt_private_component_get_user_data(
732 bt_put(user_data
->notif_iter
);
738 void create_source_sink(struct bt_graph
*graph
, struct bt_component
**source
,
739 struct bt_component
**sink
)
741 struct bt_component_class
*src_comp_class
;
742 struct bt_component_class
*sink_comp_class
;
745 /* Create source component */
747 src_comp_class
= bt_component_class_source_create("src",
749 BT_ASSERT(src_comp_class
);
750 ret
= bt_component_class_set_init_method(src_comp_class
,
753 ret
= bt_component_class_set_finalize_method(src_comp_class
,
756 ret
= bt_component_class_source_set_notification_iterator_init_method(
757 src_comp_class
, src_iter_init
);
759 ret
= bt_component_class_source_set_notification_iterator_finalize_method(
760 src_comp_class
, src_iter_finalize
);
762 ret
= bt_graph_add_component(graph
, src_comp_class
, "source",
765 bt_put(src_comp_class
);
768 /* Create sink component */
770 sink_comp_class
= bt_component_class_sink_create("sink",
772 BT_ASSERT(sink_comp_class
);
773 ret
= bt_component_class_set_init_method(sink_comp_class
,
776 ret
= bt_component_class_set_finalize_method(sink_comp_class
,
778 ret
= bt_component_class_set_port_connected_method(
779 sink_comp_class
, sink_port_connected
);
781 ret
= bt_graph_add_component(graph
, sink_comp_class
, "sink",
784 bt_put(sink_comp_class
);
789 void do_std_test(enum test test
, const char *name
,
790 const struct test_event
*expected_test_events
)
792 struct bt_component
*src_comp
;
793 struct bt_component
*sink_comp
;
794 struct bt_port
*upstream_port
;
795 struct bt_port
*downstream_port
;
796 enum bt_graph_status graph_status
= BT_GRAPH_STATUS_OK
;
800 diag("test: %s", name
);
802 graph
= bt_graph_create();
804 create_source_sink(graph
, &src_comp
, &sink_comp
);
806 /* Connect source to sink */
807 upstream_port
= bt_component_source_get_output_port_by_name(src_comp
, "out");
808 BT_ASSERT(upstream_port
);
809 downstream_port
= bt_component_sink_get_input_port_by_name(sink_comp
, "in");
810 BT_ASSERT(downstream_port
);
811 graph_status
= bt_graph_connect_ports(graph
, upstream_port
,
812 downstream_port
, NULL
);
813 bt_put(upstream_port
);
814 bt_put(downstream_port
);
816 /* Run the graph until the end */
817 while (graph_status
== BT_GRAPH_STATUS_OK
||
818 graph_status
== BT_GRAPH_STATUS_AGAIN
) {
819 graph_status
= bt_graph_run(graph
);
822 ok(graph_status
== BT_GRAPH_STATUS_END
, "graph finishes without any error");
824 /* Compare the resulting test events */
825 if (expected_test_events
) {
826 ok(compare_test_events(expected_test_events
),
827 "the produced sequence of test events is the expected one");
836 void test_no_auto_notifs(void)
838 const struct test_event expected_test_events
[] = {
839 { .type
= TEST_EV_TYPE_NOTIF_STREAM_BEGIN
, .stream
= src_stream1
, .packet
= NULL
, },
840 { .type
= TEST_EV_TYPE_NOTIF_PACKET_BEGIN
, .stream
= src_stream1
, .packet
= src_stream1_packet1
, },
841 { .type
= TEST_EV_TYPE_NOTIF_EVENT
, .stream
= src_stream1
, .packet
= src_stream1_packet1
, },
842 { .type
= TEST_EV_TYPE_NOTIF_EVENT
, .stream
= src_stream1
, .packet
= src_stream1_packet1
, },
843 { .type
= TEST_EV_TYPE_NOTIF_STREAM_BEGIN
, .stream
= src_stream2
, .packet
= NULL
, },
844 { .type
= TEST_EV_TYPE_NOTIF_EVENT
, .stream
= src_stream1
, .packet
= src_stream1_packet1
, },
845 { .type
= TEST_EV_TYPE_NOTIF_PACKET_BEGIN
, .stream
= src_stream2
, .packet
= src_stream2_packet2
, },
846 { .type
= TEST_EV_TYPE_NOTIF_EVENT
, .stream
= src_stream2
, .packet
= src_stream2_packet2
, },
847 { .type
= TEST_EV_TYPE_NOTIF_EVENT
, .stream
= src_stream1
, .packet
= src_stream1_packet1
, },
848 { .type
= TEST_EV_TYPE_NOTIF_PACKET_END
, .stream
= src_stream1
, .packet
= src_stream1_packet1
, },
849 { .type
= TEST_EV_TYPE_NOTIF_PACKET_END
, .stream
= src_stream2
, .packet
= src_stream2_packet2
, },
850 { .type
= TEST_EV_TYPE_NOTIF_PACKET_BEGIN
, .stream
= src_stream1
, .packet
= src_stream1_packet2
, },
851 { .type
= TEST_EV_TYPE_NOTIF_EVENT
, .stream
= src_stream1
, .packet
= src_stream1_packet2
, },
852 { .type
= TEST_EV_TYPE_NOTIF_STREAM_END
, .stream
= src_stream2
, .packet
= NULL
, },
853 { .type
= TEST_EV_TYPE_NOTIF_PACKET_END
, .stream
= src_stream1
, .packet
= src_stream1_packet2
, },
854 { .type
= TEST_EV_TYPE_NOTIF_STREAM_END
, .stream
= src_stream1
, .packet
= NULL
, },
855 { .type
= TEST_EV_TYPE_END
, },
856 { .type
= TEST_EV_TYPE_SENTINEL
, },
859 do_std_test(TEST_NO_AUTO_NOTIFS
, "no automatic notifications",
860 expected_test_events
);
864 void test_output_port_notification_iterator(void)
866 const struct test_event expected_test_events
[] = {
867 { .type
= TEST_EV_TYPE_NOTIF_STREAM_BEGIN
, .stream
= src_stream1
, .packet
= NULL
, },
868 { .type
= TEST_EV_TYPE_NOTIF_PACKET_BEGIN
, .stream
= src_stream1
, .packet
= src_stream1_packet1
, },
869 { .type
= TEST_EV_TYPE_NOTIF_EVENT
, .stream
= src_stream1
, .packet
= src_stream1_packet1
, },
870 { .type
= TEST_EV_TYPE_NOTIF_EVENT
, .stream
= src_stream1
, .packet
= src_stream1_packet1
, },
871 { .type
= TEST_EV_TYPE_NOTIF_STREAM_BEGIN
, .stream
= src_stream2
, .packet
= NULL
, },
872 { .type
= TEST_EV_TYPE_NOTIF_EVENT
, .stream
= src_stream1
, .packet
= src_stream1_packet1
, },
873 { .type
= TEST_EV_TYPE_NOTIF_PACKET_BEGIN
, .stream
= src_stream2
, .packet
= src_stream2_packet2
, },
874 { .type
= TEST_EV_TYPE_NOTIF_EVENT
, .stream
= src_stream2
, .packet
= src_stream2_packet2
, },
875 { .type
= TEST_EV_TYPE_NOTIF_EVENT
, .stream
= src_stream1
, .packet
= src_stream1_packet1
, },
876 { .type
= TEST_EV_TYPE_NOTIF_PACKET_END
, .stream
= src_stream1
, .packet
= src_stream1_packet1
, },
877 { .type
= TEST_EV_TYPE_NOTIF_PACKET_END
, .stream
= src_stream2
, .packet
= src_stream2_packet2
, },
878 { .type
= TEST_EV_TYPE_NOTIF_PACKET_BEGIN
, .stream
= src_stream1
, .packet
= src_stream1_packet2
, },
879 { .type
= TEST_EV_TYPE_NOTIF_EVENT
, .stream
= src_stream1
, .packet
= src_stream1_packet2
, },
880 { .type
= TEST_EV_TYPE_NOTIF_STREAM_END
, .stream
= src_stream2
, .packet
= NULL
, },
881 { .type
= TEST_EV_TYPE_NOTIF_PACKET_END
, .stream
= src_stream1
, .packet
= src_stream1_packet2
, },
882 { .type
= TEST_EV_TYPE_NOTIF_STREAM_END
, .stream
= src_stream1
, .packet
= NULL
, },
883 { .type
= TEST_EV_TYPE_END
, },
884 { .type
= TEST_EV_TYPE_SENTINEL
, },
886 struct bt_component
*src_comp
;
887 struct bt_notification_iterator
*notif_iter
;
888 enum bt_notification_iterator_status iter_status
=
889 BT_NOTIFICATION_ITERATOR_STATUS_OK
;
890 struct bt_port
*upstream_port
;
893 current_test
= TEST_OUTPUT_PORT_NOTIFICATION_ITERATOR
;
894 diag("test: output port notification iterator");
896 graph
= bt_graph_create();
898 create_source_sink(graph
, &src_comp
, NULL
);
900 /* Create notification iterator on source's output port */
901 upstream_port
= bt_component_source_get_output_port_by_name(src_comp
, "out");
902 notif_iter
= bt_output_port_notification_iterator_create(upstream_port
,
904 ok(notif_iter
, "bt_output_port_notification_iterator_create() succeeds");
905 bt_put(upstream_port
);
907 /* Consume the notification iterator */
908 while (iter_status
== BT_NOTIFICATION_ITERATOR_STATUS_OK
) {
909 iter_status
= common_consume(notif_iter
, true);
912 ok(iter_status
== BT_NOTIFICATION_ITERATOR_STATUS_END
,
913 "output port notification iterator finishes without any error");
915 /* Compare the resulting test events */
916 ok(compare_test_events(expected_test_events
),
917 "the produced sequence of test events is the expected one");
925 void test_output_port_notification_iterator_cannot_consume(void)
927 struct bt_component
*src_comp
;
928 struct bt_notification_iterator
*notif_iter
;
929 struct bt_port
*upstream_port
;
932 current_test
= TEST_OUTPUT_PORT_NOTIFICATION_ITERATOR
;
933 diag("test: cannot consume graph with existing output port notification iterator");
935 graph
= bt_graph_create();
937 create_source_sink(graph
, &src_comp
, NULL
);
939 /* Create notification iterator on source's output port */
940 upstream_port
= bt_component_source_get_output_port_by_name(src_comp
, "out");
941 notif_iter
= bt_output_port_notification_iterator_create(upstream_port
,
943 BT_ASSERT(notif_iter
);
944 bt_put(upstream_port
);
947 * This should fail because the graph is now managed by the
948 * notification iterator.
950 ok(bt_graph_run(graph
) == BT_GRAPH_STATUS_CANNOT_CONSUME
,
951 "bt_graph_run() returns BT_GRAPH_STATUS_CANNOT_CONSUME when there's an output port notification iterator");
958 #define DEBUG_ENV_VAR "TEST_BT_NOTIFICATION_ITERATOR_DEBUG"
960 int main(int argc
, char **argv
)
962 if (getenv(DEBUG_ENV_VAR
) && strcmp(getenv(DEBUG_ENV_VAR
), "1") == 0) {
966 plan_tests(NR_TESTS
);
968 test_no_auto_notifs();
969 test_output_port_notification_iterator();
970 test_output_port_notification_iterator_cannot_consume();
972 return exit_status();