lib: make public reference count functions have strict types
[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 <babeltrace/babeltrace.h>
25 #include <babeltrace/assert-internal.h>
26 #include <glib.h>
27
28 #include "tap/tap.h"
29
30 #define NR_TESTS 5
31
32 enum test {
33 TEST_NO_AUTO_NOTIFS,
34 TEST_OUTPUT_PORT_NOTIFICATION_ITERATOR,
35 };
36
37 enum test_event_type {
38 TEST_EV_TYPE_NOTIF_UNEXPECTED,
39 TEST_EV_TYPE_NOTIF_EVENT,
40 TEST_EV_TYPE_NOTIF_STREAM_BEGIN,
41 TEST_EV_TYPE_NOTIF_PACKET_BEGIN,
42 TEST_EV_TYPE_NOTIF_PACKET_END,
43 TEST_EV_TYPE_NOTIF_STREAM_END,
44 TEST_EV_TYPE_END,
45 TEST_EV_TYPE_SENTINEL,
46 };
47
48 struct test_event {
49 enum test_event_type type;
50 const struct bt_stream *stream;
51 const struct bt_packet *packet;
52 };
53
54 static bool debug = false;
55 static enum test current_test;
56 static GArray *test_events;
57 static struct bt_graph *graph;
58 static struct bt_stream_class *src_stream_class;
59 static struct bt_event_class *src_event_class;
60 static struct bt_stream *src_stream1;
61 static struct bt_stream *src_stream2;
62 static struct bt_packet *src_stream1_packet1;
63 static struct bt_packet *src_stream1_packet2;
64 static struct bt_packet *src_stream2_packet1;
65 static struct bt_packet *src_stream2_packet2;
66
67 enum {
68 SEQ_END = -1,
69 SEQ_STREAM1_BEGIN = -2,
70 SEQ_STREAM2_BEGIN = -3,
71 SEQ_STREAM1_END = -4,
72 SEQ_STREAM2_END = -5,
73 SEQ_STREAM1_PACKET1_BEGIN = -6,
74 SEQ_STREAM1_PACKET2_BEGIN = -7,
75 SEQ_STREAM2_PACKET1_BEGIN = -8,
76 SEQ_STREAM2_PACKET2_BEGIN = -9,
77 SEQ_STREAM1_PACKET1_END = -10,
78 SEQ_STREAM1_PACKET2_END = -11,
79 SEQ_STREAM2_PACKET1_END = -12,
80 SEQ_STREAM2_PACKET2_END = -13,
81 SEQ_EVENT_STREAM1_PACKET1 = -14,
82 SEQ_EVENT_STREAM1_PACKET2 = -15,
83 SEQ_EVENT_STREAM2_PACKET1 = -16,
84 SEQ_EVENT_STREAM2_PACKET2 = -17,
85 };
86
87 struct src_iter_user_data {
88 int64_t *seq;
89 size_t at;
90 };
91
92 struct sink_user_data {
93 struct bt_self_component_port_input_notification_iterator *notif_iter;
94 };
95
96 /*
97 * No automatic notifications generated in this block.
98 * Stream 2 notifications are more indented.
99 */
100 static int64_t seq_no_auto_notifs[] = {
101 SEQ_STREAM1_BEGIN,
102 SEQ_STREAM1_PACKET1_BEGIN,
103 SEQ_EVENT_STREAM1_PACKET1,
104 SEQ_EVENT_STREAM1_PACKET1,
105 SEQ_STREAM2_BEGIN,
106 SEQ_EVENT_STREAM1_PACKET1,
107 SEQ_STREAM2_PACKET2_BEGIN,
108 SEQ_EVENT_STREAM2_PACKET2,
109 SEQ_EVENT_STREAM1_PACKET1,
110 SEQ_STREAM1_PACKET1_END,
111 SEQ_STREAM2_PACKET2_END,
112 SEQ_STREAM1_PACKET2_BEGIN,
113 SEQ_EVENT_STREAM1_PACKET2,
114 SEQ_STREAM2_END,
115 SEQ_STREAM1_PACKET2_END,
116 SEQ_STREAM1_END,
117 SEQ_END,
118 };
119
120 static
121 void clear_test_events(void)
122 {
123 g_array_set_size(test_events, 0);
124 }
125
126 static
127 void print_test_event(FILE *fp, const struct test_event *event)
128 {
129 fprintf(fp, "{ type = ");
130
131 switch (event->type) {
132 case TEST_EV_TYPE_NOTIF_UNEXPECTED:
133 fprintf(fp, "TEST_EV_TYPE_NOTIF_UNEXPECTED");
134 break;
135 case TEST_EV_TYPE_NOTIF_EVENT:
136 fprintf(fp, "TEST_EV_TYPE_NOTIF_EVENT");
137 break;
138 case TEST_EV_TYPE_NOTIF_STREAM_BEGIN:
139 fprintf(fp, "TEST_EV_TYPE_NOTIF_STREAM_BEGIN");
140 break;
141 case TEST_EV_TYPE_NOTIF_STREAM_END:
142 fprintf(fp, "TEST_EV_TYPE_NOTIF_STREAM_END");
143 break;
144 case TEST_EV_TYPE_NOTIF_PACKET_BEGIN:
145 fprintf(fp, "TEST_EV_TYPE_NOTIF_PACKET_BEGIN");
146 break;
147 case TEST_EV_TYPE_NOTIF_PACKET_END:
148 fprintf(fp, "TEST_EV_TYPE_NOTIF_PACKET_END");
149 break;
150 case TEST_EV_TYPE_END:
151 fprintf(fp, "TEST_EV_TYPE_END");
152 break;
153 case TEST_EV_TYPE_SENTINEL:
154 fprintf(fp, "TEST_EV_TYPE_SENTINEL");
155 break;
156 default:
157 fprintf(fp, "(UNKNOWN)");
158 break;
159 }
160
161 fprintf(fp, ", stream = %p, packet = %p }", event->stream,
162 event->packet);
163 }
164
165 static
166 void append_test_event(struct test_event *event)
167 {
168 g_array_append_val(test_events, *event);
169 }
170
171 static
172 bool compare_single_test_events(const struct test_event *ev_a,
173 const struct test_event *ev_b)
174 {
175 if (debug) {
176 fprintf(stderr, ":: Comparing test events: ");
177 print_test_event(stderr, ev_a);
178 fprintf(stderr, " vs. ");
179 print_test_event(stderr, ev_b);
180 fprintf(stderr, "\n");
181 }
182
183 if (ev_a->type != ev_b->type) {
184 return false;
185 }
186
187 switch (ev_a->type) {
188 case TEST_EV_TYPE_END:
189 case TEST_EV_TYPE_SENTINEL:
190 break;
191 default:
192 if (ev_a->stream != ev_b->stream) {
193 return false;
194 }
195
196 if (ev_a->packet != ev_b->packet) {
197 return false;
198 }
199 break;
200 }
201
202 return true;
203 }
204
205 static
206 bool compare_test_events(const struct test_event *expected_events)
207 {
208 const struct test_event *expected_event = expected_events;
209 size_t i = 0;
210
211 BT_ASSERT(expected_events);
212
213 while (true) {
214 const struct test_event *event;
215
216 if (expected_event->type == TEST_EV_TYPE_SENTINEL) {
217 break;
218 }
219
220 if (i >= test_events->len) {
221 return false;
222 }
223
224 event = &g_array_index(test_events, struct test_event, i);
225
226 if (!compare_single_test_events(event, expected_event)) {
227 return false;
228 }
229
230 i++;
231 expected_event++;
232 }
233
234 if (i != test_events->len) {
235 return false;
236 }
237
238 return true;
239 }
240
241 static
242 void init_static_data(void)
243 {
244 struct bt_trace_class *trace_class;
245 struct bt_trace *trace;
246
247 /* Test events */
248 test_events = g_array_new(FALSE, TRUE, sizeof(struct test_event));
249 BT_ASSERT(test_events);
250
251 /* Metadata, streams, and packets*/
252 trace_class = bt_trace_class_create();
253 BT_ASSERT(trace);
254 src_stream_class = bt_stream_class_create(trace_class);
255 BT_ASSERT(src_stream_class);
256 src_event_class = bt_event_class_create(src_stream_class);
257 BT_ASSERT(src_event_class);
258 trace = bt_trace_create(trace_class);
259 BT_ASSERT(trace);
260 src_stream1 = bt_stream_create(src_stream_class, trace);
261 BT_ASSERT(src_stream1);
262 src_stream2 = bt_stream_create(src_stream_class, trace);
263 BT_ASSERT(src_stream2);
264 src_stream1_packet1 = bt_packet_create(src_stream1);
265 BT_ASSERT(src_stream1_packet1);
266 src_stream1_packet2 = bt_packet_create(src_stream1);
267 BT_ASSERT(src_stream1_packet2);
268 src_stream2_packet1 = bt_packet_create(src_stream2);
269 BT_ASSERT(src_stream2_packet1);
270 src_stream2_packet2 = bt_packet_create(src_stream2);
271 BT_ASSERT(src_stream2_packet2);
272
273 if (debug) {
274 fprintf(stderr, ":: stream 1: %p\n", src_stream1);
275 fprintf(stderr, ":: stream 2: %p\n", src_stream2);
276 fprintf(stderr, ":: stream 1, packet 1: %p\n", src_stream1_packet1);
277 fprintf(stderr, ":: stream 1, packet 2: %p\n", src_stream1_packet2);
278 fprintf(stderr, ":: stream 2, packet 1: %p\n", src_stream2_packet1);
279 fprintf(stderr, ":: stream 2, packet 2: %p\n", src_stream2_packet2);
280 }
281
282 bt_trace_put_ref(trace);
283 bt_trace_class_put_ref(trace_class);
284 }
285
286 static
287 void fini_static_data(void)
288 {
289 /* Test events */
290 g_array_free(test_events, TRUE);
291
292 /* Metadata */
293 bt_stream_class_put_ref(src_stream_class);
294 bt_event_class_put_ref(src_event_class);
295 bt_stream_put_ref(src_stream1);
296 bt_stream_put_ref(src_stream2);
297 bt_packet_put_ref(src_stream1_packet1);
298 bt_packet_put_ref(src_stream1_packet2);
299 bt_packet_put_ref(src_stream2_packet1);
300 bt_packet_put_ref(src_stream2_packet2);
301 }
302
303 static
304 void src_iter_finalize(struct bt_self_notification_iterator *self_notif_iter)
305 {
306 struct src_iter_user_data *user_data =
307 bt_self_notification_iterator_get_data(
308 self_notif_iter);
309
310 if (user_data) {
311 g_free(user_data);
312 }
313 }
314
315 static
316 enum bt_self_notification_iterator_status src_iter_init(
317 struct bt_self_notification_iterator *self_notif_iter,
318 struct bt_self_component_source *self_comp,
319 struct bt_self_component_port_output *self_port)
320 {
321 struct src_iter_user_data *user_data =
322 g_new0(struct src_iter_user_data, 1);
323
324 BT_ASSERT(user_data);
325 bt_self_notification_iterator_set_data(self_notif_iter, user_data);
326
327 switch (current_test) {
328 case TEST_NO_AUTO_NOTIFS:
329 case TEST_OUTPUT_PORT_NOTIFICATION_ITERATOR:
330 user_data->seq = seq_no_auto_notifs;
331 break;
332 default:
333 abort();
334 }
335
336 return BT_SELF_NOTIFICATION_ITERATOR_STATUS_OK;
337 }
338
339 static
340 void src_iter_next_seq_one(struct bt_self_notification_iterator* notif_iter,
341 struct src_iter_user_data *user_data,
342 const struct bt_notification **notif)
343 {
344 struct bt_packet *event_packet = NULL;
345
346 switch (user_data->seq[user_data->at]) {
347 case SEQ_STREAM1_BEGIN:
348 *notif = bt_notification_stream_beginning_create(notif_iter,
349 src_stream1);
350 break;
351 case SEQ_STREAM2_BEGIN:
352 *notif = bt_notification_stream_beginning_create(notif_iter,
353 src_stream2);
354 break;
355 case SEQ_STREAM1_END:
356 *notif = bt_notification_stream_end_create(notif_iter,
357 src_stream1);
358 break;
359 case SEQ_STREAM2_END:
360 *notif = bt_notification_stream_end_create(notif_iter,
361 src_stream2);
362 break;
363 case SEQ_STREAM1_PACKET1_BEGIN:
364 *notif = bt_notification_packet_beginning_create(notif_iter,
365 src_stream1_packet1);
366 break;
367 case SEQ_STREAM1_PACKET2_BEGIN:
368 *notif = bt_notification_packet_beginning_create(notif_iter,
369 src_stream1_packet2);
370 break;
371 case SEQ_STREAM2_PACKET1_BEGIN:
372 *notif = bt_notification_packet_beginning_create(notif_iter,
373 src_stream2_packet1);
374 break;
375 case SEQ_STREAM2_PACKET2_BEGIN:
376 *notif = bt_notification_packet_beginning_create(notif_iter,
377 src_stream2_packet2);
378 break;
379 case SEQ_STREAM1_PACKET1_END:
380 *notif = bt_notification_packet_end_create(notif_iter,
381 src_stream1_packet1);
382 break;
383 case SEQ_STREAM1_PACKET2_END:
384 *notif = bt_notification_packet_end_create(notif_iter,
385 src_stream1_packet2);
386 break;
387 case SEQ_STREAM2_PACKET1_END:
388 *notif = bt_notification_packet_end_create(notif_iter,
389 src_stream2_packet1);
390 break;
391 case SEQ_STREAM2_PACKET2_END:
392 *notif = bt_notification_packet_end_create(notif_iter,
393 src_stream2_packet2);
394 break;
395 case SEQ_EVENT_STREAM1_PACKET1:
396 event_packet = src_stream1_packet1;
397 break;
398 case SEQ_EVENT_STREAM1_PACKET2:
399 event_packet = src_stream1_packet2;
400 break;
401 case SEQ_EVENT_STREAM2_PACKET1:
402 event_packet = src_stream2_packet1;
403 break;
404 case SEQ_EVENT_STREAM2_PACKET2:
405 event_packet = src_stream2_packet2;
406 break;
407 default:
408 abort();
409 }
410
411 if (event_packet) {
412 *notif = bt_notification_event_create(notif_iter,
413 src_event_class,
414 event_packet);
415 }
416
417 BT_ASSERT(*notif);
418 user_data->at++;
419 }
420
421 static
422 enum bt_self_notification_iterator_status src_iter_next_seq(
423 struct bt_self_notification_iterator *notif_iter,
424 struct src_iter_user_data *user_data,
425 bt_notification_array_const notifs, uint64_t capacity,
426 uint64_t *count)
427 {
428 enum bt_self_notification_iterator_status status =
429 BT_SELF_NOTIFICATION_ITERATOR_STATUS_OK;
430 uint64_t i = 0;
431
432 BT_ASSERT(user_data->seq);
433
434 if (user_data->seq[user_data->at] == SEQ_END) {
435 status = BT_SELF_NOTIFICATION_ITERATOR_STATUS_END;
436 goto end;
437 }
438
439 while (i < capacity && user_data->seq[user_data->at] != SEQ_END) {
440 src_iter_next_seq_one(notif_iter, user_data, &notifs[i]);
441 i++;
442 }
443
444 BT_ASSERT(i > 0 && i <= capacity);
445 *count = i;
446
447 end:
448 return status;
449 }
450
451 static
452 enum bt_self_notification_iterator_status src_iter_next(
453 struct bt_self_notification_iterator *self_notif_iter,
454 bt_notification_array_const notifs, uint64_t capacity,
455 uint64_t *count)
456 {
457 struct src_iter_user_data *user_data =
458 bt_self_notification_iterator_get_data(self_notif_iter);
459
460 BT_ASSERT(user_data);
461 return src_iter_next_seq(self_notif_iter, user_data, notifs,
462 capacity, count);
463 }
464
465 static
466 enum bt_self_component_status src_init(
467 struct bt_self_component_source *self_comp,
468 const struct bt_value *params, void *init_method_data)
469 {
470 int ret;
471
472 ret = bt_self_component_source_add_output_port(
473 self_comp, "out", NULL, NULL);
474 BT_ASSERT(ret == 0);
475 return BT_SELF_COMPONENT_STATUS_OK;
476 }
477
478 static
479 void src_finalize(struct bt_self_component_source *self_comp)
480 {
481 }
482
483 static
484 void append_test_events_from_notification(const struct bt_notification *notification)
485 {
486 struct test_event test_event = { 0 };
487
488 switch (bt_notification_get_type(notification)) {
489 case BT_NOTIFICATION_TYPE_EVENT:
490 {
491 const struct bt_event *event;
492
493 test_event.type = TEST_EV_TYPE_NOTIF_EVENT;
494 event = bt_notification_event_borrow_event_const(notification);
495 BT_ASSERT(event);
496 test_event.packet = bt_event_borrow_packet_const(event);
497 BT_ASSERT(test_event.packet);
498 break;
499 }
500 case BT_NOTIFICATION_TYPE_STREAM_BEGINNING:
501 test_event.type = TEST_EV_TYPE_NOTIF_STREAM_BEGIN;
502 test_event.stream =
503 bt_notification_stream_beginning_borrow_stream_const(notification);
504 BT_ASSERT(test_event.stream);
505 break;
506 case BT_NOTIFICATION_TYPE_STREAM_END:
507 test_event.type = TEST_EV_TYPE_NOTIF_STREAM_END;
508 test_event.stream =
509 bt_notification_stream_end_borrow_stream_const(notification);
510 BT_ASSERT(test_event.stream);
511 break;
512 case BT_NOTIFICATION_TYPE_PACKET_BEGINNING:
513 test_event.type = TEST_EV_TYPE_NOTIF_PACKET_BEGIN;
514 test_event.packet =
515 bt_notification_packet_beginning_borrow_packet_const(notification);
516 BT_ASSERT(test_event.packet);
517 break;
518 case BT_NOTIFICATION_TYPE_PACKET_END:
519 test_event.type = TEST_EV_TYPE_NOTIF_PACKET_END;
520 test_event.packet =
521 bt_notification_packet_end_borrow_packet_const(notification);
522 BT_ASSERT(test_event.packet);
523 break;
524 default:
525 test_event.type = TEST_EV_TYPE_NOTIF_UNEXPECTED;
526 break;
527 }
528
529 if (test_event.packet) {
530 test_event.stream = bt_packet_borrow_stream_const(
531 test_event.packet);
532 BT_ASSERT(test_event.stream);
533 }
534
535 append_test_event(&test_event);
536 }
537
538 static
539 enum bt_notification_iterator_status common_consume(
540 void *notif_iter, bool is_output_port_notif_iter)
541 {
542 enum bt_notification_iterator_status ret;
543 bt_notification_array_const notifications = NULL;
544 uint64_t count = 0;
545 struct test_event test_event = { 0 };
546 uint64_t i;
547
548 BT_ASSERT(notif_iter);
549
550 if (is_output_port_notif_iter) {
551 ret = bt_port_output_notification_iterator_next(notif_iter,
552 &notifications, &count);
553 } else {
554 ret = bt_self_component_port_input_notification_iterator_next(
555 notif_iter, &notifications, &count);
556 }
557
558 if (ret < 0) {
559 goto end;
560 }
561
562 switch (ret) {
563 case BT_NOTIFICATION_ITERATOR_STATUS_END:
564 test_event.type = TEST_EV_TYPE_END;
565 append_test_event(&test_event);
566 goto end;
567 case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN:
568 abort();
569 default:
570 break;
571 }
572
573 BT_ASSERT(notifications);
574 BT_ASSERT(count > 0);
575
576 for (i = 0; i < count; i++) {
577 append_test_events_from_notification(notifications[i]);
578 bt_notification_put_ref(notifications[i]);
579 }
580
581 end:
582 return ret;
583 }
584
585 static
586 enum bt_self_component_status sink_consume(
587 struct bt_self_component_sink *self_comp)
588 {
589 enum bt_self_component_status ret = BT_SELF_COMPONENT_STATUS_OK;
590 struct sink_user_data *user_data =
591 bt_self_component_get_data(
592 bt_self_component_sink_as_self_component(
593 self_comp));
594 enum bt_notification_iterator_status it_ret;
595
596 BT_ASSERT(user_data && user_data->notif_iter);
597 it_ret = common_consume(user_data->notif_iter, false);
598
599 if (it_ret < 0) {
600 ret = BT_SELF_COMPONENT_STATUS_ERROR;
601 goto end;
602 }
603
604 switch (it_ret) {
605 case BT_NOTIFICATION_ITERATOR_STATUS_END:
606 ret = BT_SELF_COMPONENT_STATUS_END;
607 BT_SELF_COMPONENT_PORT_INPUT_NOTIFICATION_ITERATOR_PUT_REF_AND_RESET(
608 user_data->notif_iter);
609 goto end;
610 case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN:
611 abort();
612 default:
613 break;
614 }
615
616 end:
617 return ret;
618 }
619
620 static
621 enum bt_self_component_status sink_port_connected(
622 struct bt_self_component_sink *self_comp,
623 struct bt_self_component_port_input *self_port,
624 const struct bt_port_output *other_port)
625 {
626 struct sink_user_data *user_data =
627 bt_self_component_get_data(
628 bt_self_component_sink_as_self_component(
629 self_comp));
630
631 BT_ASSERT(user_data);
632 user_data->notif_iter =
633 bt_self_component_port_input_notification_iterator_create(
634 self_port);
635 return BT_SELF_COMPONENT_STATUS_OK;
636 }
637
638 static
639 enum bt_self_component_status sink_init(
640 struct bt_self_component_sink *self_comp,
641 const struct bt_value *params, void *init_method_data)
642 {
643 struct sink_user_data *user_data = g_new0(struct sink_user_data, 1);
644 int ret;
645
646 BT_ASSERT(user_data);
647 bt_self_component_set_data(
648 bt_self_component_sink_as_self_component(self_comp),
649 user_data);
650 ret = bt_self_component_sink_add_input_port(
651 self_comp, "in", NULL, NULL);
652 BT_ASSERT(ret == 0);
653 return BT_SELF_COMPONENT_STATUS_OK;
654 }
655
656 static
657 void sink_finalize(struct bt_self_component_sink *self_comp)
658 {
659 struct sink_user_data *user_data =
660 bt_self_component_get_data(
661 bt_self_component_sink_as_self_component(
662 self_comp));
663
664 if (user_data) {
665 BT_SELF_COMPONENT_PORT_INPUT_NOTIFICATION_ITERATOR_PUT_REF_AND_RESET(
666 user_data->notif_iter);
667 g_free(user_data);
668 }
669 }
670
671 static
672 void create_source_sink(struct bt_graph *graph,
673 const struct bt_component_source **source,
674 const struct bt_component_sink **sink)
675 {
676 struct bt_component_class_source *src_comp_class;
677 struct bt_component_class_sink *sink_comp_class;
678 int ret;
679
680 /* Create source component */
681 if (source) {
682 src_comp_class = bt_component_class_source_create("src",
683 src_iter_next);
684 BT_ASSERT(src_comp_class);
685 ret = bt_component_class_source_set_init_method(
686 src_comp_class, src_init);
687 BT_ASSERT(ret == 0);
688 ret = bt_component_class_source_set_finalize_method(
689 src_comp_class, src_finalize);
690 BT_ASSERT(ret == 0);
691 ret = bt_component_class_source_set_notification_iterator_init_method(
692 src_comp_class, src_iter_init);
693 BT_ASSERT(ret == 0);
694 ret = bt_component_class_source_set_notification_iterator_finalize_method(
695 src_comp_class, src_iter_finalize);
696 BT_ASSERT(ret == 0);
697 ret = bt_graph_add_source_component(graph,
698 src_comp_class, "source", NULL, source);
699 BT_ASSERT(ret == 0);
700 bt_component_class_source_put_ref(src_comp_class);
701 }
702
703 /* Create sink component */
704 if (sink) {
705 sink_comp_class = bt_component_class_sink_create("sink",
706 sink_consume);
707 BT_ASSERT(sink_comp_class);
708 ret = bt_component_class_sink_set_init_method(
709 sink_comp_class, sink_init);
710 BT_ASSERT(ret == 0);
711 ret = bt_component_class_sink_set_finalize_method(
712 sink_comp_class, sink_finalize);
713 ret = bt_component_class_sink_set_input_port_connected_method(
714 sink_comp_class, sink_port_connected);
715 BT_ASSERT(ret == 0);
716 ret = bt_graph_add_sink_component(graph,
717 sink_comp_class,
718 "sink", NULL, sink);
719 BT_ASSERT(ret == 0);
720 bt_component_class_sink_put_ref(sink_comp_class);
721 }
722 }
723
724 static
725 void do_std_test(enum test test, const char *name,
726 const struct test_event *expected_test_events)
727 {
728 const struct bt_component_source *src_comp;
729 const struct bt_component_sink *sink_comp;
730 const struct bt_port_output *upstream_port;
731 const struct bt_port_input *downstream_port;
732 enum bt_graph_status graph_status = BT_GRAPH_STATUS_OK;
733
734 clear_test_events();
735 current_test = test;
736 diag("test: %s", name);
737 BT_ASSERT(!graph);
738 graph = bt_graph_create();
739 BT_ASSERT(graph);
740 create_source_sink(graph, &src_comp, &sink_comp);
741
742 /* Connect source to sink */
743 upstream_port =
744 bt_component_source_borrow_output_port_by_name_const(
745 src_comp, "out");
746 BT_ASSERT(upstream_port);
747 downstream_port = bt_component_sink_borrow_input_port_by_name_const(
748 sink_comp, "in");
749 BT_ASSERT(downstream_port);
750 graph_status = bt_graph_connect_ports(graph, upstream_port,
751 downstream_port, NULL);
752
753 /* Run the graph until the end */
754 while (graph_status == BT_GRAPH_STATUS_OK ||
755 graph_status == BT_GRAPH_STATUS_AGAIN) {
756 graph_status = bt_graph_run(graph);
757 }
758
759 ok(graph_status == BT_GRAPH_STATUS_END,
760 "graph finishes without any error");
761
762 /* Compare the resulting test events */
763 if (expected_test_events) {
764 ok(compare_test_events(expected_test_events),
765 "the produced sequence of test events is the expected one");
766 }
767
768 bt_component_source_put_ref(src_comp);
769 bt_component_sink_put_ref(sink_comp);
770 BT_GRAPH_PUT_REF_AND_RESET(graph);
771 }
772
773 static
774 void test_no_auto_notifs(void)
775 {
776 const struct test_event expected_test_events[] = {
777 { .type = TEST_EV_TYPE_NOTIF_STREAM_BEGIN, .stream = src_stream1, .packet = NULL, },
778 { .type = TEST_EV_TYPE_NOTIF_PACKET_BEGIN, .stream = src_stream1, .packet = src_stream1_packet1, },
779 { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = src_stream1, .packet = src_stream1_packet1, },
780 { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = src_stream1, .packet = src_stream1_packet1, },
781 { .type = TEST_EV_TYPE_NOTIF_STREAM_BEGIN, .stream = src_stream2, .packet = NULL, },
782 { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = src_stream1, .packet = src_stream1_packet1, },
783 { .type = TEST_EV_TYPE_NOTIF_PACKET_BEGIN, .stream = src_stream2, .packet = src_stream2_packet2, },
784 { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = src_stream2, .packet = src_stream2_packet2, },
785 { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = src_stream1, .packet = src_stream1_packet1, },
786 { .type = TEST_EV_TYPE_NOTIF_PACKET_END, .stream = src_stream1, .packet = src_stream1_packet1, },
787 { .type = TEST_EV_TYPE_NOTIF_PACKET_END, .stream = src_stream2, .packet = src_stream2_packet2, },
788 { .type = TEST_EV_TYPE_NOTIF_PACKET_BEGIN, .stream = src_stream1, .packet = src_stream1_packet2, },
789 { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = src_stream1, .packet = src_stream1_packet2, },
790 { .type = TEST_EV_TYPE_NOTIF_STREAM_END, .stream = src_stream2, .packet = NULL, },
791 { .type = TEST_EV_TYPE_NOTIF_PACKET_END, .stream = src_stream1, .packet = src_stream1_packet2, },
792 { .type = TEST_EV_TYPE_NOTIF_STREAM_END, .stream = src_stream1, .packet = NULL, },
793 { .type = TEST_EV_TYPE_END, },
794 { .type = TEST_EV_TYPE_SENTINEL, },
795 };
796
797 do_std_test(TEST_NO_AUTO_NOTIFS, "no automatic notifications",
798 expected_test_events);
799 }
800
801 static
802 void test_output_port_notification_iterator(void)
803 {
804 const struct test_event expected_test_events[] = {
805 { .type = TEST_EV_TYPE_NOTIF_STREAM_BEGIN, .stream = src_stream1, .packet = NULL, },
806 { .type = TEST_EV_TYPE_NOTIF_PACKET_BEGIN, .stream = src_stream1, .packet = src_stream1_packet1, },
807 { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = src_stream1, .packet = src_stream1_packet1, },
808 { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = src_stream1, .packet = src_stream1_packet1, },
809 { .type = TEST_EV_TYPE_NOTIF_STREAM_BEGIN, .stream = src_stream2, .packet = NULL, },
810 { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = src_stream1, .packet = src_stream1_packet1, },
811 { .type = TEST_EV_TYPE_NOTIF_PACKET_BEGIN, .stream = src_stream2, .packet = src_stream2_packet2, },
812 { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = src_stream2, .packet = src_stream2_packet2, },
813 { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = src_stream1, .packet = src_stream1_packet1, },
814 { .type = TEST_EV_TYPE_NOTIF_PACKET_END, .stream = src_stream1, .packet = src_stream1_packet1, },
815 { .type = TEST_EV_TYPE_NOTIF_PACKET_END, .stream = src_stream2, .packet = src_stream2_packet2, },
816 { .type = TEST_EV_TYPE_NOTIF_PACKET_BEGIN, .stream = src_stream1, .packet = src_stream1_packet2, },
817 { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = src_stream1, .packet = src_stream1_packet2, },
818 { .type = TEST_EV_TYPE_NOTIF_STREAM_END, .stream = src_stream2, .packet = NULL, },
819 { .type = TEST_EV_TYPE_NOTIF_PACKET_END, .stream = src_stream1, .packet = src_stream1_packet2, },
820 { .type = TEST_EV_TYPE_NOTIF_STREAM_END, .stream = src_stream1, .packet = NULL, },
821 { .type = TEST_EV_TYPE_END, },
822 { .type = TEST_EV_TYPE_SENTINEL, },
823 };
824 const struct bt_component_source *src_comp;
825 struct bt_port_output_notification_iterator *notif_iter;
826 enum bt_notification_iterator_status iter_status =
827 BT_NOTIFICATION_ITERATOR_STATUS_OK;
828 const struct bt_port_output *upstream_port;
829
830 clear_test_events();
831 current_test = TEST_OUTPUT_PORT_NOTIFICATION_ITERATOR;
832 diag("test: output port notification iterator");
833 BT_ASSERT(!graph);
834 graph = bt_graph_create();
835 BT_ASSERT(graph);
836 create_source_sink(graph, &src_comp, NULL);
837
838 /* Create notification iterator on source's output port */
839 upstream_port = bt_component_source_borrow_output_port_by_name_const(src_comp,
840 "out");
841 notif_iter = bt_port_output_notification_iterator_create(graph,
842 upstream_port);
843 ok(notif_iter, "bt_private_output_port_notification_iterator_create() succeeds");
844
845 /* Consume the notification iterator */
846 while (iter_status == BT_NOTIFICATION_ITERATOR_STATUS_OK) {
847 iter_status = common_consume(notif_iter, true);
848 }
849
850 ok(iter_status == BT_NOTIFICATION_ITERATOR_STATUS_END,
851 "output port notification iterator finishes without any error");
852
853 /* Compare the resulting test events */
854 ok(compare_test_events(expected_test_events),
855 "the produced sequence of test events is the expected one");
856
857 bt_component_source_put_ref(src_comp);
858 BT_GRAPH_PUT_REF_AND_RESET(graph);
859 bt_port_output_notification_iterator_put_ref(notif_iter);
860 }
861
862 #define DEBUG_ENV_VAR "TEST_BT_NOTIFICATION_ITERATOR_DEBUG"
863
864 int main(int argc, char **argv)
865 {
866 if (getenv(DEBUG_ENV_VAR) && strcmp(getenv(DEBUG_ENV_VAR), "1") == 0) {
867 debug = true;
868 }
869
870 plan_tests(NR_TESTS);
871 init_static_data();
872 test_no_auto_notifs();
873 test_output_port_notification_iterator();
874 fini_static_data();
875 return exit_status();
876 }
This page took 0.047453 seconds and 4 git commands to generate.