lib: use object pool for event and packet notifications
[babeltrace.git] / tests / lib / test_bt_notification_iterator.c
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>
21 #include <stdbool.h>
22 #include <inttypes.h>
23 #include <string.h>
24 #include <assert.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-private-notification-iterator.h>
55 #include <babeltrace/graph/private-port.h>
56 #include <babeltrace/plugin/plugin.h>
57 #include <babeltrace/ref.h>
58 #include <glib.h>
59
60 #include "tap/tap.h"
61
62 #define NR_TESTS 6
63
64 enum test {
65 TEST_NO_AUTO_NOTIFS,
66 TEST_OUTPUT_PORT_NOTIFICATION_ITERATOR,
67 };
68
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,
77 TEST_EV_TYPE_END,
78 TEST_EV_TYPE_SENTINEL,
79 };
80
81 struct test_event {
82 enum test_event_type type;
83 struct bt_stream *stream;
84 struct bt_packet *packet;
85 };
86
87 static bool debug = false;
88 static enum test current_test;
89 static GArray *test_events;
90 static struct bt_graph *graph;
91 static struct bt_clock_class_priority_map *src_empty_cc_prio_map;
92 static struct bt_stream_class *src_stream_class;
93 static struct bt_event_class *src_event_class;
94 static struct bt_stream *src_stream1;
95 static struct bt_stream *src_stream2;
96 static struct bt_packet *src_stream1_packet1;
97 static struct bt_packet *src_stream1_packet2;
98 static struct bt_packet *src_stream2_packet1;
99 static struct bt_packet *src_stream2_packet2;
100
101 enum {
102 SEQ_END = -1,
103 SEQ_STREAM1_BEGIN = -2,
104 SEQ_STREAM2_BEGIN = -3,
105 SEQ_STREAM1_END = -4,
106 SEQ_STREAM2_END = -5,
107 SEQ_STREAM1_PACKET1_BEGIN = -6,
108 SEQ_STREAM1_PACKET2_BEGIN = -7,
109 SEQ_STREAM2_PACKET1_BEGIN = -8,
110 SEQ_STREAM2_PACKET2_BEGIN = -9,
111 SEQ_STREAM1_PACKET1_END = -10,
112 SEQ_STREAM1_PACKET2_END = -11,
113 SEQ_STREAM2_PACKET1_END = -12,
114 SEQ_STREAM2_PACKET2_END = -13,
115 SEQ_EVENT_STREAM1_PACKET1 = -14,
116 SEQ_EVENT_STREAM1_PACKET2 = -15,
117 SEQ_EVENT_STREAM2_PACKET1 = -16,
118 SEQ_EVENT_STREAM2_PACKET2 = -17,
119 SEQ_INACTIVITY = -18,
120 };
121
122 struct src_iter_user_data {
123 int64_t *seq;
124 size_t at;
125 };
126
127 struct sink_user_data {
128 struct bt_notification_iterator *notif_iter;
129 };
130
131 /*
132 * No automatic notifications generated in this block.
133 * Stream 2 notifications are more indented.
134 */
135 static int64_t seq_no_auto_notifs[] = {
136 SEQ_STREAM1_BEGIN,
137 SEQ_STREAM1_PACKET1_BEGIN,
138 SEQ_EVENT_STREAM1_PACKET1,
139 SEQ_EVENT_STREAM1_PACKET1,
140 SEQ_STREAM2_BEGIN,
141 SEQ_EVENT_STREAM1_PACKET1,
142 SEQ_STREAM2_PACKET2_BEGIN,
143 SEQ_EVENT_STREAM2_PACKET2,
144 SEQ_EVENT_STREAM1_PACKET1,
145 SEQ_STREAM1_PACKET1_END,
146 SEQ_STREAM2_PACKET2_END,
147 SEQ_STREAM1_PACKET2_BEGIN,
148 SEQ_EVENT_STREAM1_PACKET2,
149 SEQ_STREAM2_END,
150 SEQ_STREAM1_PACKET2_END,
151 SEQ_STREAM1_END,
152 SEQ_END,
153 };
154
155 static
156 void clear_test_events(void)
157 {
158 g_array_set_size(test_events, 0);
159 }
160
161 static
162 void print_test_event(FILE *fp, const struct test_event *event)
163 {
164 fprintf(fp, "{ type = ");
165
166 switch (event->type) {
167 case TEST_EV_TYPE_NOTIF_UNEXPECTED:
168 fprintf(fp, "TEST_EV_TYPE_NOTIF_UNEXPECTED");
169 break;
170 case TEST_EV_TYPE_NOTIF_EVENT:
171 fprintf(fp, "TEST_EV_TYPE_NOTIF_EVENT");
172 break;
173 case TEST_EV_TYPE_NOTIF_INACTIVITY:
174 fprintf(fp, "TEST_EV_TYPE_NOTIF_INACTIVITY");
175 break;
176 case TEST_EV_TYPE_NOTIF_STREAM_BEGIN:
177 fprintf(fp, "TEST_EV_TYPE_NOTIF_STREAM_BEGIN");
178 break;
179 case TEST_EV_TYPE_NOTIF_STREAM_END:
180 fprintf(fp, "TEST_EV_TYPE_NOTIF_STREAM_END");
181 break;
182 case TEST_EV_TYPE_NOTIF_PACKET_BEGIN:
183 fprintf(fp, "TEST_EV_TYPE_NOTIF_PACKET_BEGIN");
184 break;
185 case TEST_EV_TYPE_NOTIF_PACKET_END:
186 fprintf(fp, "TEST_EV_TYPE_NOTIF_PACKET_END");
187 break;
188 case TEST_EV_TYPE_END:
189 fprintf(fp, "TEST_EV_TYPE_END");
190 break;
191 case TEST_EV_TYPE_SENTINEL:
192 fprintf(fp, "TEST_EV_TYPE_SENTINEL");
193 break;
194 default:
195 fprintf(fp, "(UNKNOWN)");
196 break;
197 }
198
199 fprintf(fp, ", stream = %p, packet = %p }", event->stream,
200 event->packet);
201 }
202
203 static
204 void append_test_event(struct test_event *event)
205 {
206 g_array_append_val(test_events, *event);
207 }
208
209 static
210 bool compare_single_test_events(const struct test_event *ev_a,
211 const struct test_event *ev_b)
212 {
213 if (debug) {
214 fprintf(stderr, ":: Comparing test events: ");
215 print_test_event(stderr, ev_a);
216 fprintf(stderr, " vs. ");
217 print_test_event(stderr, ev_b);
218 fprintf(stderr, "\n");
219 }
220
221 if (ev_a->type != ev_b->type) {
222 return false;
223 }
224
225 switch (ev_a->type) {
226 case TEST_EV_TYPE_END:
227 case TEST_EV_TYPE_SENTINEL:
228 break;
229 default:
230 if (ev_a->stream != ev_b->stream) {
231 return false;
232 }
233
234 if (ev_a->packet != ev_b->packet) {
235 return false;
236 }
237 break;
238 }
239
240 return true;
241 }
242
243 static
244 bool compare_test_events(const struct test_event *expected_events)
245 {
246 const struct test_event *expected_event = expected_events;
247 size_t i = 0;
248
249 assert(expected_events);
250
251 while (true) {
252 const struct test_event *event;
253
254 if (expected_event->type == TEST_EV_TYPE_SENTINEL) {
255 break;
256 }
257
258 if (i >= test_events->len) {
259 return false;
260 }
261
262 event = &g_array_index(test_events, struct test_event, i);
263
264 if (!compare_single_test_events(event, expected_event)) {
265 return false;
266 }
267
268 i++;
269 expected_event++;
270 }
271
272 if (i != test_events->len) {
273 return false;
274 }
275
276 return true;
277 }
278
279 static
280 void init_static_data(void)
281 {
282 int ret;
283 struct bt_trace *trace;
284 struct bt_field_type *empty_struct_ft;
285
286 /* Test events */
287 test_events = g_array_new(FALSE, TRUE, sizeof(struct test_event));
288 assert(test_events);
289
290 /* Metadata */
291 empty_struct_ft = bt_field_type_structure_create();
292 assert(empty_struct_ft);
293 trace = bt_trace_create();
294 assert(trace);
295 ret = bt_trace_set_packet_header_field_type(trace, empty_struct_ft);
296 assert(ret == 0);
297 src_empty_cc_prio_map = bt_clock_class_priority_map_create();
298 assert(src_empty_cc_prio_map);
299 src_stream_class = bt_stream_class_create("my-stream-class");
300 assert(src_stream_class);
301 ret = bt_stream_class_set_packet_context_field_type(src_stream_class,
302 empty_struct_ft);
303 assert(ret == 0);
304 ret = bt_stream_class_set_event_header_field_type(src_stream_class,
305 empty_struct_ft);
306 assert(ret == 0);
307 ret = bt_stream_class_set_event_context_field_type(src_stream_class,
308 empty_struct_ft);
309 assert(ret == 0);
310 src_event_class = bt_event_class_create("my-event-class");
311 ret = bt_event_class_set_context_field_type(src_event_class,
312 empty_struct_ft);
313 assert(ret == 0);
314 ret = bt_event_class_set_payload_field_type(src_event_class,
315 empty_struct_ft);
316 assert(ret == 0);
317 ret = bt_stream_class_add_event_class(src_stream_class,
318 src_event_class);
319 assert(ret == 0);
320 ret = bt_trace_add_stream_class(trace, src_stream_class);
321 assert(ret == 0);
322 src_stream1 = bt_stream_create(src_stream_class, "stream-1", 0);
323 assert(src_stream1);
324 src_stream2 = bt_stream_create(src_stream_class, "stream-2", 1);
325 assert(src_stream2);
326 src_stream1_packet1 = bt_packet_create(src_stream1);
327 assert(src_stream1_packet1);
328 src_stream1_packet2 = bt_packet_create(src_stream1);
329 assert(src_stream1_packet2);
330 src_stream2_packet1 = bt_packet_create(src_stream2);
331 assert(src_stream2_packet1);
332 src_stream2_packet2 = bt_packet_create(src_stream2);
333 assert(src_stream2_packet2);
334
335 if (debug) {
336 fprintf(stderr, ":: stream 1: %p\n", src_stream1);
337 fprintf(stderr, ":: stream 2: %p\n", src_stream2);
338 fprintf(stderr, ":: stream 1, packet 1: %p\n", src_stream1_packet1);
339 fprintf(stderr, ":: stream 1, packet 2: %p\n", src_stream1_packet2);
340 fprintf(stderr, ":: stream 2, packet 1: %p\n", src_stream2_packet1);
341 fprintf(stderr, ":: stream 2, packet 2: %p\n", src_stream2_packet2);
342 }
343
344 bt_put(trace);
345 bt_put(empty_struct_ft);
346 }
347
348 static
349 void fini_static_data(void)
350 {
351 /* Test events */
352 g_array_free(test_events, TRUE);
353
354 /* Metadata */
355 bt_put(src_empty_cc_prio_map);
356 bt_put(src_stream_class);
357 bt_put(src_event_class);
358 bt_put(src_stream1);
359 bt_put(src_stream2);
360 bt_put(src_stream1_packet1);
361 bt_put(src_stream1_packet2);
362 bt_put(src_stream2_packet1);
363 bt_put(src_stream2_packet2);
364 }
365
366 static
367 void src_iter_finalize(
368 struct bt_private_connection_private_notification_iterator *private_notification_iterator)
369 {
370 struct src_iter_user_data *user_data =
371 bt_private_connection_private_notification_iterator_get_user_data(
372 private_notification_iterator);
373
374 if (user_data) {
375 g_free(user_data);
376 }
377 }
378
379 static
380 enum bt_notification_iterator_status src_iter_init(
381 struct bt_private_connection_private_notification_iterator *priv_notif_iter,
382 struct bt_private_port *private_port)
383 {
384 struct src_iter_user_data *user_data =
385 g_new0(struct src_iter_user_data, 1);
386 int ret;
387
388 assert(user_data);
389 ret = bt_private_connection_private_notification_iterator_set_user_data(
390 priv_notif_iter, user_data);
391 assert(ret == 0);
392
393 switch (current_test) {
394 case TEST_NO_AUTO_NOTIFS:
395 case TEST_OUTPUT_PORT_NOTIFICATION_ITERATOR:
396 user_data->seq = seq_no_auto_notifs;
397 break;
398 default:
399 abort();
400 }
401
402 return BT_NOTIFICATION_ITERATOR_STATUS_OK;
403 }
404
405 static
406 struct bt_notification_iterator_next_method_return src_iter_next_seq(
407 struct src_iter_user_data *user_data)
408 {
409 struct bt_notification_iterator_next_method_return next_return = {
410 .status = BT_NOTIFICATION_ITERATOR_STATUS_OK,
411 };
412 int64_t cur_ts_ns;
413 struct bt_packet *event_packet = NULL;
414
415 assert(user_data->seq);
416 cur_ts_ns = user_data->seq[user_data->at];
417
418 switch (cur_ts_ns) {
419 case SEQ_END:
420 next_return.status =
421 BT_NOTIFICATION_ITERATOR_STATUS_END;
422 break;
423 case SEQ_INACTIVITY:
424 next_return.notification =
425 bt_notification_inactivity_create(graph,
426 src_empty_cc_prio_map);
427 assert(next_return.notification);
428 break;
429 case SEQ_STREAM1_BEGIN:
430 next_return.notification =
431 bt_notification_stream_begin_create(graph, src_stream1);
432 assert(next_return.notification);
433 break;
434 case SEQ_STREAM2_BEGIN:
435 next_return.notification =
436 bt_notification_stream_begin_create(graph, src_stream2);
437 assert(next_return.notification);
438 break;
439 case SEQ_STREAM1_END:
440 next_return.notification =
441 bt_notification_stream_end_create(graph, src_stream1);
442 assert(next_return.notification);
443 break;
444 case SEQ_STREAM2_END:
445 next_return.notification =
446 bt_notification_stream_end_create(graph, src_stream2);
447 assert(next_return.notification);
448 break;
449 case SEQ_STREAM1_PACKET1_BEGIN:
450 next_return.notification =
451 bt_notification_packet_begin_create(graph,
452 src_stream1_packet1);
453 assert(next_return.notification);
454 break;
455 case SEQ_STREAM1_PACKET2_BEGIN:
456 next_return.notification =
457 bt_notification_packet_begin_create(graph,
458 src_stream1_packet2);
459 assert(next_return.notification);
460 break;
461 case SEQ_STREAM2_PACKET1_BEGIN:
462 next_return.notification =
463 bt_notification_packet_begin_create(graph,
464 src_stream2_packet1);
465 assert(next_return.notification);
466 break;
467 case SEQ_STREAM2_PACKET2_BEGIN:
468 next_return.notification =
469 bt_notification_packet_begin_create(graph,
470 src_stream2_packet2);
471 assert(next_return.notification);
472 break;
473 case SEQ_STREAM1_PACKET1_END:
474 next_return.notification =
475 bt_notification_packet_end_create(graph,
476 src_stream1_packet1);
477 assert(next_return.notification);
478 break;
479 case SEQ_STREAM1_PACKET2_END:
480 next_return.notification =
481 bt_notification_packet_end_create(graph,
482 src_stream1_packet2);
483 assert(next_return.notification);
484 break;
485 case SEQ_STREAM2_PACKET1_END:
486 next_return.notification =
487 bt_notification_packet_end_create(graph,
488 src_stream2_packet1);
489 assert(next_return.notification);
490 break;
491 case SEQ_STREAM2_PACKET2_END:
492 next_return.notification =
493 bt_notification_packet_end_create(graph,
494 src_stream2_packet2);
495 assert(next_return.notification);
496 break;
497 case SEQ_EVENT_STREAM1_PACKET1:
498 event_packet = src_stream1_packet1;
499 break;
500 case SEQ_EVENT_STREAM1_PACKET2:
501 event_packet = src_stream1_packet2;
502 break;
503 case SEQ_EVENT_STREAM2_PACKET1:
504 event_packet = src_stream2_packet1;
505 break;
506 case SEQ_EVENT_STREAM2_PACKET2:
507 event_packet = src_stream2_packet2;
508 break;
509 default:
510 abort();
511 }
512
513 if (event_packet) {
514 next_return.notification =
515 bt_notification_event_create(graph, src_event_class,
516 event_packet, src_empty_cc_prio_map);
517 assert(next_return.notification);
518 }
519
520 if (next_return.status != BT_NOTIFICATION_ITERATOR_STATUS_END) {
521 user_data->at++;
522 }
523
524 return next_return;
525 }
526
527 static
528 struct bt_notification_iterator_next_method_return src_iter_next(
529 struct bt_private_connection_private_notification_iterator *priv_iterator)
530 {
531 struct bt_notification_iterator_next_method_return next_return = {
532 .status = BT_NOTIFICATION_ITERATOR_STATUS_OK,
533 .notification = NULL,
534 };
535 struct src_iter_user_data *user_data =
536 bt_private_connection_private_notification_iterator_get_user_data(priv_iterator);
537
538 assert(user_data);
539 next_return = src_iter_next_seq(user_data);
540 return next_return;
541 }
542
543 static
544 enum bt_component_status src_init(
545 struct bt_private_component *private_component,
546 struct bt_value *params, void *init_method_data)
547 {
548 int ret;
549
550 ret = bt_private_component_source_add_output_private_port(
551 private_component, "out", NULL, NULL);
552 assert(ret == 0);
553 return BT_COMPONENT_STATUS_OK;
554 }
555
556 static
557 void src_finalize(struct bt_private_component *private_component)
558 {
559 }
560
561 static
562 enum bt_notification_iterator_status common_consume(
563 struct bt_notification_iterator *notif_iter)
564 {
565 enum bt_notification_iterator_status ret;
566 struct bt_notification *notification = NULL;
567 struct test_event test_event = { 0 };
568 bool do_append_test_event = true;
569 assert(notif_iter);
570
571 ret = bt_notification_iterator_next(notif_iter);
572 if (ret < 0) {
573 do_append_test_event = false;
574 goto end;
575 }
576
577 switch (ret) {
578 case BT_NOTIFICATION_ITERATOR_STATUS_END:
579 test_event.type = TEST_EV_TYPE_END;
580 goto end;
581 case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN:
582 abort();
583 default:
584 break;
585 }
586
587 notification = bt_notification_iterator_get_notification(
588 notif_iter);
589 assert(notification);
590
591 switch (bt_notification_get_type(notification)) {
592 case BT_NOTIFICATION_TYPE_EVENT:
593 {
594 struct bt_event *event;
595
596 test_event.type = TEST_EV_TYPE_NOTIF_EVENT;
597 event = bt_notification_event_borrow_event(notification);
598 assert(event);
599 test_event.packet = bt_event_borrow_packet(event);
600 assert(test_event.packet);
601 break;
602 }
603 case BT_NOTIFICATION_TYPE_INACTIVITY:
604 test_event.type = TEST_EV_TYPE_NOTIF_INACTIVITY;
605 break;
606 case BT_NOTIFICATION_TYPE_STREAM_BEGIN:
607 test_event.type = TEST_EV_TYPE_NOTIF_STREAM_BEGIN;
608 test_event.stream =
609 bt_notification_stream_begin_borrow_stream(notification);
610 assert(test_event.stream);
611 break;
612 case BT_NOTIFICATION_TYPE_STREAM_END:
613 test_event.type = TEST_EV_TYPE_NOTIF_STREAM_END;
614 test_event.stream =
615 bt_notification_stream_end_borrow_stream(notification);
616 assert(test_event.stream);
617 break;
618 case BT_NOTIFICATION_TYPE_PACKET_BEGIN:
619 test_event.type = TEST_EV_TYPE_NOTIF_PACKET_BEGIN;
620 test_event.packet =
621 bt_notification_packet_begin_borrow_packet(notification);
622 assert(test_event.packet);
623 break;
624 case BT_NOTIFICATION_TYPE_PACKET_END:
625 test_event.type = TEST_EV_TYPE_NOTIF_PACKET_END;
626 test_event.packet =
627 bt_notification_packet_end_borrow_packet(notification);
628 assert(test_event.packet);
629 break;
630 default:
631 test_event.type = TEST_EV_TYPE_NOTIF_UNEXPECTED;
632 break;
633 }
634
635 if (test_event.packet) {
636 test_event.stream = bt_packet_borrow_stream(test_event.packet);
637 assert(test_event.stream);
638 }
639
640 end:
641 if (do_append_test_event) {
642 append_test_event(&test_event);
643 }
644
645 bt_put(notification);
646 return ret;
647 }
648
649 static
650 enum bt_component_status sink_consume(
651 struct bt_private_component *priv_component)
652 {
653 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
654 struct sink_user_data *user_data =
655 bt_private_component_get_user_data(priv_component);
656 enum bt_notification_iterator_status it_ret;
657
658 assert(user_data && user_data->notif_iter);
659 it_ret = common_consume(user_data->notif_iter);
660
661 if (it_ret < 0) {
662 ret = BT_COMPONENT_STATUS_ERROR;
663 goto end;
664 }
665
666 switch (it_ret) {
667 case BT_NOTIFICATION_ITERATOR_STATUS_END:
668 ret = BT_COMPONENT_STATUS_END;
669 BT_PUT(user_data->notif_iter);
670 goto end;
671 case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN:
672 abort();
673 default:
674 break;
675 }
676
677 end:
678 return ret;
679 }
680
681 static
682 void sink_port_connected(struct bt_private_component *private_component,
683 struct bt_private_port *self_private_port,
684 struct bt_port *other_port)
685 {
686 struct bt_private_connection *priv_conn =
687 bt_private_port_get_private_connection(self_private_port);
688 struct sink_user_data *user_data = bt_private_component_get_user_data(
689 private_component);
690 enum bt_connection_status conn_status;
691
692 assert(user_data);
693 assert(priv_conn);
694 conn_status = bt_private_connection_create_notification_iterator(
695 priv_conn, &user_data->notif_iter);
696 assert(conn_status == 0);
697 bt_put(priv_conn);
698 }
699
700 static
701 enum bt_component_status sink_init(
702 struct bt_private_component *private_component,
703 struct bt_value *params, void *init_method_data)
704 {
705 struct sink_user_data *user_data = g_new0(struct sink_user_data, 1);
706 int ret;
707
708 assert(user_data);
709 ret = bt_private_component_set_user_data(private_component,
710 user_data);
711 assert(ret == 0);
712 ret = bt_private_component_sink_add_input_private_port(
713 private_component, "in", NULL, NULL);
714 assert(ret == 0);
715 return BT_COMPONENT_STATUS_OK;
716 }
717
718 static
719 void sink_finalize(struct bt_private_component *private_component)
720 {
721 struct sink_user_data *user_data = bt_private_component_get_user_data(
722 private_component);
723
724 if (user_data) {
725 bt_put(user_data->notif_iter);
726 g_free(user_data);
727 }
728 }
729
730 static
731 void create_source_sink(struct bt_graph *graph, struct bt_component **source,
732 struct bt_component **sink)
733 {
734 struct bt_component_class *src_comp_class;
735 struct bt_component_class *sink_comp_class;
736 int ret;
737
738 /* Create source component */
739 if (source) {
740 src_comp_class = bt_component_class_source_create("src",
741 src_iter_next);
742 assert(src_comp_class);
743 ret = bt_component_class_set_init_method(src_comp_class,
744 src_init);
745 assert(ret == 0);
746 ret = bt_component_class_set_finalize_method(src_comp_class,
747 src_finalize);
748 assert(ret == 0);
749 ret = bt_component_class_source_set_notification_iterator_init_method(
750 src_comp_class, src_iter_init);
751 assert(ret == 0);
752 ret = bt_component_class_source_set_notification_iterator_finalize_method(
753 src_comp_class, src_iter_finalize);
754 assert(ret == 0);
755 ret = bt_graph_add_component(graph, src_comp_class, "source",
756 NULL, source);
757 assert(ret == 0);
758 bt_put(src_comp_class);
759 }
760
761 /* Create sink component */
762 if (sink) {
763 sink_comp_class = bt_component_class_sink_create("sink",
764 sink_consume);
765 assert(sink_comp_class);
766 ret = bt_component_class_set_init_method(sink_comp_class,
767 sink_init);
768 assert(ret == 0);
769 ret = bt_component_class_set_finalize_method(sink_comp_class,
770 sink_finalize);
771 ret = bt_component_class_set_port_connected_method(
772 sink_comp_class, sink_port_connected);
773 assert(ret == 0);
774 ret = bt_graph_add_component(graph, sink_comp_class, "sink",
775 NULL, sink);
776 assert(ret == 0);
777 bt_put(sink_comp_class);
778 }
779 }
780
781 static
782 void do_std_test(enum test test, const char *name,
783 const struct test_event *expected_test_events)
784 {
785 struct bt_component *src_comp;
786 struct bt_component *sink_comp;
787 struct bt_port *upstream_port;
788 struct bt_port *downstream_port;
789 enum bt_graph_status graph_status = BT_GRAPH_STATUS_OK;
790
791 clear_test_events();
792 current_test = test;
793 diag("test: %s", name);
794 assert(!graph);
795 graph = bt_graph_create();
796 assert(graph);
797 create_source_sink(graph, &src_comp, &sink_comp);
798
799 /* Connect source to sink */
800 upstream_port = bt_component_source_get_output_port_by_name(src_comp, "out");
801 assert(upstream_port);
802 downstream_port = bt_component_sink_get_input_port_by_name(sink_comp, "in");
803 assert(downstream_port);
804 graph_status = bt_graph_connect_ports(graph, upstream_port,
805 downstream_port, NULL);
806 bt_put(upstream_port);
807 bt_put(downstream_port);
808
809 /* Run the graph until the end */
810 while (graph_status == BT_GRAPH_STATUS_OK ||
811 graph_status == BT_GRAPH_STATUS_AGAIN) {
812 graph_status = bt_graph_run(graph);
813 }
814
815 ok(graph_status == BT_GRAPH_STATUS_END, "graph finishes without any error");
816
817 /* Compare the resulting test events */
818 if (expected_test_events) {
819 ok(compare_test_events(expected_test_events),
820 "the produced sequence of test events is the expected one");
821 }
822
823 bt_put(src_comp);
824 bt_put(sink_comp);
825 BT_PUT(graph);
826 }
827
828 static
829 void test_no_auto_notifs(void)
830 {
831 const struct test_event expected_test_events[] = {
832 { .type = TEST_EV_TYPE_NOTIF_STREAM_BEGIN, .stream = src_stream1, .packet = NULL, },
833 { .type = TEST_EV_TYPE_NOTIF_PACKET_BEGIN, .stream = src_stream1, .packet = src_stream1_packet1, },
834 { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = src_stream1, .packet = src_stream1_packet1, },
835 { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = src_stream1, .packet = src_stream1_packet1, },
836 { .type = TEST_EV_TYPE_NOTIF_STREAM_BEGIN, .stream = src_stream2, .packet = NULL, },
837 { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = src_stream1, .packet = src_stream1_packet1, },
838 { .type = TEST_EV_TYPE_NOTIF_PACKET_BEGIN, .stream = src_stream2, .packet = src_stream2_packet2, },
839 { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = src_stream2, .packet = src_stream2_packet2, },
840 { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = src_stream1, .packet = src_stream1_packet1, },
841 { .type = TEST_EV_TYPE_NOTIF_PACKET_END, .stream = src_stream1, .packet = src_stream1_packet1, },
842 { .type = TEST_EV_TYPE_NOTIF_PACKET_END, .stream = src_stream2, .packet = src_stream2_packet2, },
843 { .type = TEST_EV_TYPE_NOTIF_PACKET_BEGIN, .stream = src_stream1, .packet = src_stream1_packet2, },
844 { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = src_stream1, .packet = src_stream1_packet2, },
845 { .type = TEST_EV_TYPE_NOTIF_STREAM_END, .stream = src_stream2, .packet = NULL, },
846 { .type = TEST_EV_TYPE_NOTIF_PACKET_END, .stream = src_stream1, .packet = src_stream1_packet2, },
847 { .type = TEST_EV_TYPE_NOTIF_STREAM_END, .stream = src_stream1, .packet = NULL, },
848 { .type = TEST_EV_TYPE_END, },
849 { .type = TEST_EV_TYPE_SENTINEL, },
850 };
851
852 do_std_test(TEST_NO_AUTO_NOTIFS, "no automatic notifications",
853 expected_test_events);
854 }
855
856 static
857 void test_output_port_notification_iterator(void)
858 {
859 const struct test_event expected_test_events[] = {
860 { .type = TEST_EV_TYPE_NOTIF_STREAM_BEGIN, .stream = src_stream1, .packet = NULL, },
861 { .type = TEST_EV_TYPE_NOTIF_PACKET_BEGIN, .stream = src_stream1, .packet = src_stream1_packet1, },
862 { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = src_stream1, .packet = src_stream1_packet1, },
863 { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = src_stream1, .packet = src_stream1_packet1, },
864 { .type = TEST_EV_TYPE_NOTIF_STREAM_BEGIN, .stream = src_stream2, .packet = NULL, },
865 { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = src_stream1, .packet = src_stream1_packet1, },
866 { .type = TEST_EV_TYPE_NOTIF_PACKET_BEGIN, .stream = src_stream2, .packet = src_stream2_packet2, },
867 { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = src_stream2, .packet = src_stream2_packet2, },
868 { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = src_stream1, .packet = src_stream1_packet1, },
869 { .type = TEST_EV_TYPE_NOTIF_PACKET_END, .stream = src_stream1, .packet = src_stream1_packet1, },
870 { .type = TEST_EV_TYPE_NOTIF_PACKET_END, .stream = src_stream2, .packet = src_stream2_packet2, },
871 { .type = TEST_EV_TYPE_NOTIF_PACKET_BEGIN, .stream = src_stream1, .packet = src_stream1_packet2, },
872 { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = src_stream1, .packet = src_stream1_packet2, },
873 { .type = TEST_EV_TYPE_NOTIF_STREAM_END, .stream = src_stream2, .packet = NULL, },
874 { .type = TEST_EV_TYPE_NOTIF_PACKET_END, .stream = src_stream1, .packet = src_stream1_packet2, },
875 { .type = TEST_EV_TYPE_NOTIF_STREAM_END, .stream = src_stream1, .packet = NULL, },
876 { .type = TEST_EV_TYPE_END, },
877 { .type = TEST_EV_TYPE_SENTINEL, },
878 };
879 struct bt_component *src_comp;
880 struct bt_notification_iterator *notif_iter;
881 enum bt_notification_iterator_status iter_status =
882 BT_NOTIFICATION_ITERATOR_STATUS_OK;
883 struct bt_port *upstream_port;
884
885 clear_test_events();
886 current_test = TEST_OUTPUT_PORT_NOTIFICATION_ITERATOR;
887 diag("test: output port notification iterator");
888 assert(!graph);
889 graph = bt_graph_create();
890 assert(graph);
891 create_source_sink(graph, &src_comp, NULL);
892
893 /* Create notification iterator on source's output port */
894 upstream_port = bt_component_source_get_output_port_by_name(src_comp, "out");
895 notif_iter = bt_output_port_notification_iterator_create(upstream_port,
896 NULL);
897 ok(notif_iter, "bt_output_port_notification_iterator_create() succeeds");
898 bt_put(upstream_port);
899
900 /* Consume the notification iterator */
901 while (iter_status == BT_NOTIFICATION_ITERATOR_STATUS_OK) {
902 iter_status = common_consume(notif_iter);
903 }
904
905 ok(iter_status == BT_NOTIFICATION_ITERATOR_STATUS_END,
906 "output port notification iterator finishes without any error");
907
908 /* Compare the resulting test events */
909 ok(compare_test_events(expected_test_events),
910 "the produced sequence of test events is the expected one");
911
912 bt_put(src_comp);
913 BT_PUT(graph);
914 bt_put(notif_iter);
915 }
916
917 static
918 void test_output_port_notification_iterator_cannot_consume(void)
919 {
920 struct bt_component *src_comp;
921 struct bt_notification_iterator *notif_iter;
922 struct bt_port *upstream_port;
923
924 clear_test_events();
925 current_test = TEST_OUTPUT_PORT_NOTIFICATION_ITERATOR;
926 diag("test: cannot consume graph with existing output port notification iterator");
927 assert(!graph);
928 graph = bt_graph_create();
929 assert(graph);
930 create_source_sink(graph, &src_comp, NULL);
931
932 /* Create notification iterator on source's output port */
933 upstream_port = bt_component_source_get_output_port_by_name(src_comp, "out");
934 notif_iter = bt_output_port_notification_iterator_create(upstream_port,
935 NULL);
936 assert(notif_iter);
937 bt_put(upstream_port);
938
939 /*
940 * This should fail because the graph is now managed by the
941 * notification iterator.
942 */
943 ok(bt_graph_run(graph) == BT_GRAPH_STATUS_CANNOT_CONSUME,
944 "bt_graph_run() returns BT_GRAPH_STATUS_CANNOT_CONSUME when there's an output port notification iterator");
945
946 bt_put(src_comp);
947 BT_PUT(graph);
948 bt_put(notif_iter);
949 }
950
951 #define DEBUG_ENV_VAR "TEST_BT_NOTIFICATION_ITERATOR_DEBUG"
952
953 int main(int argc, char **argv)
954 {
955 if (getenv(DEBUG_ENV_VAR) && strcmp(getenv(DEBUG_ENV_VAR), "1") == 0) {
956 debug = true;
957 }
958
959 plan_tests(NR_TESTS);
960 init_static_data();
961 test_no_auto_notifs();
962 test_output_port_notification_iterator();
963 test_output_port_notification_iterator_cannot_consume();
964 fini_static_data();
965 return exit_status();
966 }
This page took 0.048487 seconds and 5 git commands to generate.