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