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