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