lib: graph: add "self" and some "private" APIs
[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>
e5be10ef 24#include <babeltrace/babeltrace.h>
25583cd0 25#include <babeltrace/assert-internal.h>
223c70b2
PP
26#include <glib.h>
27
28#include "tap/tap.h"
29
ad847455 30#define NR_TESTS 5
223c70b2
PP
31
32enum test {
33 TEST_NO_AUTO_NOTIFS,
e893886e 34 TEST_OUTPUT_PORT_NOTIFICATION_ITERATOR,
223c70b2
PP
35};
36
37enum test_event_type {
38 TEST_EV_TYPE_NOTIF_UNEXPECTED,
39 TEST_EV_TYPE_NOTIF_EVENT,
223c70b2
PP
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
48struct test_event {
49 enum test_event_type type;
50842bdc
PP
50 struct bt_stream *stream;
51 struct bt_packet *packet;
223c70b2
PP
52};
53
223c70b2
PP
54static bool debug = false;
55static enum test current_test;
56static GArray *test_events;
a2d06fd5 57static struct bt_private_graph *graph;
e5be10ef
PP
58static struct bt_private_stream_class *src_stream_class;
59static struct bt_private_event_class *src_event_class;
60static struct bt_private_stream *src_stream1;
61static struct bt_private_stream *src_stream2;
62static struct bt_private_packet *src_stream1_packet1;
63static struct bt_private_packet *src_stream1_packet2;
64static struct bt_private_packet *src_stream2_packet1;
65static struct bt_private_packet *src_stream2_packet2;
66static struct bt_stream *pub_src_stream1;
67static struct bt_stream *pub_src_stream2;
68static struct bt_packet *pub_src_stream1_packet1;
69static struct bt_packet *pub_src_stream1_packet2;
70static struct bt_packet *pub_src_stream2_packet1;
71static struct bt_packet *pub_src_stream2_packet2;
223c70b2
PP
72
73enum {
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,
223c70b2
PP
91};
92
93struct src_iter_user_data {
94 int64_t *seq;
95 size_t at;
96};
97
98struct sink_user_data {
d94d92ac 99 void *notif_iter;
223c70b2
PP
100};
101
102/*
103 * No automatic notifications generated in this block.
104 * Stream 2 notifications are more indented.
105 */
106static 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
223c70b2
PP
126static
127void clear_test_events(void)
128{
129 g_array_set_size(test_events, 0);
130}
131
132static
133void 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;
223c70b2
PP
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
171static
172void append_test_event(struct test_event *event)
173{
174 g_array_append_val(test_events, *event);
175}
176
177static
178bool 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
211static
212bool 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
25583cd0 217 BT_ASSERT(expected_events);
223c70b2
PP
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
247static
248void init_static_data(void)
249{
e5be10ef 250 struct bt_private_trace *trace;
223c70b2
PP
251
252 /* Test events */
253 test_events = g_array_new(FALSE, TRUE, sizeof(struct test_event));
25583cd0 254 BT_ASSERT(test_events);
223c70b2
PP
255
256 /* Metadata */
e5be10ef 257 trace = bt_private_trace_create();
25583cd0 258 BT_ASSERT(trace);
e5be10ef 259 src_stream_class = bt_private_stream_class_create(trace);
25583cd0 260 BT_ASSERT(src_stream_class);
e5be10ef 261 src_event_class = bt_private_event_class_create(src_stream_class);
44c440bc 262 BT_ASSERT(src_event_class);
e5be10ef 263 src_stream1 = bt_private_stream_create(src_stream_class);
25583cd0 264 BT_ASSERT(src_stream1);
d94d92ac 265 pub_src_stream1 = bt_private_stream_borrow_stream(src_stream1);
e5be10ef 266 src_stream2 = bt_private_stream_create(src_stream_class);
25583cd0 267 BT_ASSERT(src_stream2);
d94d92ac 268 pub_src_stream2 = bt_private_stream_borrow_stream(src_stream2);
e5be10ef 269 src_stream1_packet1 = bt_private_packet_create(src_stream1);
25583cd0 270 BT_ASSERT(src_stream1_packet1);
d94d92ac 271 pub_src_stream1_packet1 = bt_private_packet_borrow_packet(
e5be10ef
PP
272 src_stream1_packet1);
273 src_stream1_packet2 = bt_private_packet_create(src_stream1);
25583cd0 274 BT_ASSERT(src_stream1_packet2);
d94d92ac 275 pub_src_stream1_packet2 = bt_private_packet_borrow_packet(
e5be10ef
PP
276 src_stream1_packet2);
277 src_stream2_packet1 = bt_private_packet_create(src_stream2);
25583cd0 278 BT_ASSERT(src_stream2_packet1);
d94d92ac 279 pub_src_stream2_packet1 = bt_private_packet_borrow_packet(
e5be10ef
PP
280 src_stream2_packet1);
281 src_stream2_packet2 = bt_private_packet_create(src_stream2);
25583cd0 282 BT_ASSERT(src_stream2_packet2);
d94d92ac 283 pub_src_stream2_packet2 = bt_private_packet_borrow_packet(
e5be10ef 284 src_stream2_packet2);
223c70b2
PP
285
286 if (debug) {
287 fprintf(stderr, ":: stream 1: %p\n", src_stream1);
288 fprintf(stderr, ":: stream 2: %p\n", src_stream2);
289 fprintf(stderr, ":: stream 1, packet 1: %p\n", src_stream1_packet1);
290 fprintf(stderr, ":: stream 1, packet 2: %p\n", src_stream1_packet2);
291 fprintf(stderr, ":: stream 2, packet 1: %p\n", src_stream2_packet1);
292 fprintf(stderr, ":: stream 2, packet 2: %p\n", src_stream2_packet2);
293 }
294
65300d60 295 bt_object_put_ref(trace);
223c70b2
PP
296}
297
298static
299void fini_static_data(void)
300{
301 /* Test events */
302 g_array_free(test_events, TRUE);
303
304 /* Metadata */
65300d60
PP
305 bt_object_put_ref(src_stream_class);
306 bt_object_put_ref(src_event_class);
307 bt_object_put_ref(src_stream1);
308 bt_object_put_ref(src_stream2);
309 bt_object_put_ref(src_stream1_packet1);
310 bt_object_put_ref(src_stream1_packet2);
311 bt_object_put_ref(src_stream2_packet1);
312 bt_object_put_ref(src_stream2_packet2);
223c70b2
PP
313}
314
315static
d94d92ac 316void src_iter_finalize(struct bt_self_notification_iterator *self_notif_iter)
223c70b2
PP
317{
318 struct src_iter_user_data *user_data =
d94d92ac
PP
319 bt_self_notification_iterator_get_data(
320 self_notif_iter);
223c70b2
PP
321
322 if (user_data) {
323 g_free(user_data);
324 }
325}
326
327static
d94d92ac
PP
328enum bt_self_notification_iterator_status src_iter_init(
329 struct bt_self_notification_iterator *self_notif_iter,
330 struct bt_self_component_source *self_comp,
331 struct bt_self_component_port_output *self_port)
223c70b2
PP
332{
333 struct src_iter_user_data *user_data =
334 g_new0(struct src_iter_user_data, 1);
223c70b2 335
25583cd0 336 BT_ASSERT(user_data);
d94d92ac 337 bt_self_notification_iterator_set_data(self_notif_iter, user_data);
223c70b2
PP
338
339 switch (current_test) {
340 case TEST_NO_AUTO_NOTIFS:
e893886e 341 case TEST_OUTPUT_PORT_NOTIFICATION_ITERATOR:
223c70b2
PP
342 user_data->seq = seq_no_auto_notifs;
343 break;
223c70b2 344 default:
0fbb9a9f 345 abort();
223c70b2
PP
346 }
347
d94d92ac 348 return BT_SELF_NOTIFICATION_ITERATOR_STATUS_OK;
223c70b2
PP
349}
350
223c70b2 351static
d94d92ac
PP
352void src_iter_next_seq_one(struct bt_self_notification_iterator* notif_iter,
353 struct src_iter_user_data *user_data,
d4393e08 354 struct bt_notification **notif)
223c70b2 355{
e5be10ef 356 struct bt_private_packet *event_packet = NULL;
223c70b2 357
d4393e08 358 switch (user_data->seq[user_data->at]) {
223c70b2 359 case SEQ_STREAM1_BEGIN:
d94d92ac 360 *notif = bt_private_notification_borrow_notification(
e5be10ef 361 bt_private_notification_stream_begin_create(
d94d92ac 362 notif_iter, src_stream1));
223c70b2
PP
363 break;
364 case SEQ_STREAM2_BEGIN:
d94d92ac 365 *notif = bt_private_notification_borrow_notification(
e5be10ef 366 bt_private_notification_stream_begin_create(
d94d92ac 367 notif_iter, src_stream2));
223c70b2
PP
368 break;
369 case SEQ_STREAM1_END:
d94d92ac 370 *notif = bt_private_notification_borrow_notification(
e5be10ef 371 bt_private_notification_stream_end_create(
d94d92ac 372 notif_iter, src_stream1));
223c70b2
PP
373 break;
374 case SEQ_STREAM2_END:
d94d92ac 375 *notif = bt_private_notification_borrow_notification(
e5be10ef 376 bt_private_notification_stream_end_create(
d94d92ac 377 notif_iter, src_stream2));
223c70b2
PP
378 break;
379 case SEQ_STREAM1_PACKET1_BEGIN:
d94d92ac 380 *notif = bt_private_notification_borrow_notification(
e5be10ef 381 bt_private_notification_packet_begin_create(
d94d92ac 382 notif_iter, src_stream1_packet1));
223c70b2
PP
383 break;
384 case SEQ_STREAM1_PACKET2_BEGIN:
d94d92ac 385 *notif = bt_private_notification_borrow_notification(
e5be10ef 386 bt_private_notification_packet_begin_create(
d94d92ac 387 notif_iter, src_stream1_packet2));
223c70b2
PP
388 break;
389 case SEQ_STREAM2_PACKET1_BEGIN:
d94d92ac 390 *notif = bt_private_notification_borrow_notification(
e5be10ef 391 bt_private_notification_packet_begin_create(
d94d92ac 392 notif_iter, src_stream2_packet1));
223c70b2
PP
393 break;
394 case SEQ_STREAM2_PACKET2_BEGIN:
d94d92ac 395 *notif = bt_private_notification_borrow_notification(
e5be10ef 396 bt_private_notification_packet_begin_create(
d94d92ac 397 notif_iter, src_stream2_packet2));
223c70b2
PP
398 break;
399 case SEQ_STREAM1_PACKET1_END:
d94d92ac 400 *notif = bt_private_notification_borrow_notification(
e5be10ef 401 bt_private_notification_packet_end_create(
d94d92ac 402 notif_iter, src_stream1_packet1));
223c70b2
PP
403 break;
404 case SEQ_STREAM1_PACKET2_END:
d94d92ac 405 *notif = bt_private_notification_borrow_notification(
e5be10ef 406 bt_private_notification_packet_end_create(
d94d92ac 407 notif_iter, src_stream1_packet2));
223c70b2
PP
408 break;
409 case SEQ_STREAM2_PACKET1_END:
d94d92ac 410 *notif = bt_private_notification_borrow_notification(
e5be10ef 411 bt_private_notification_packet_end_create(
d94d92ac 412 notif_iter, src_stream2_packet1));
223c70b2
PP
413 break;
414 case SEQ_STREAM2_PACKET2_END:
d94d92ac 415 *notif = bt_private_notification_borrow_notification(
e5be10ef 416 bt_private_notification_packet_end_create(
d94d92ac 417 notif_iter, src_stream2_packet2));
223c70b2
PP
418 break;
419 case SEQ_EVENT_STREAM1_PACKET1:
420 event_packet = src_stream1_packet1;
421 break;
422 case SEQ_EVENT_STREAM1_PACKET2:
423 event_packet = src_stream1_packet2;
424 break;
425 case SEQ_EVENT_STREAM2_PACKET1:
426 event_packet = src_stream2_packet1;
427 break;
428 case SEQ_EVENT_STREAM2_PACKET2:
429 event_packet = src_stream2_packet2;
430 break;
431 default:
0fbb9a9f 432 abort();
223c70b2
PP
433 }
434
435 if (event_packet) {
d94d92ac 436 *notif = bt_private_notification_borrow_notification(
e5be10ef 437 bt_private_notification_event_create(
d94d92ac 438 notif_iter, src_event_class, event_packet));
d4393e08
PP
439 }
440
441 BT_ASSERT(*notif);
442 user_data->at++;
443}
444
445static
d94d92ac
PP
446enum bt_self_notification_iterator_status src_iter_next_seq(
447 struct bt_self_notification_iterator *notif_iter,
d4393e08
PP
448 struct src_iter_user_data *user_data,
449 bt_notification_array notifs, uint64_t capacity,
450 uint64_t *count)
451{
d94d92ac
PP
452 enum bt_self_notification_iterator_status status =
453 BT_SELF_NOTIFICATION_ITERATOR_STATUS_OK;
d4393e08
PP
454 uint64_t i = 0;
455
456 BT_ASSERT(user_data->seq);
457
458 if (user_data->seq[user_data->at] == SEQ_END) {
d94d92ac 459 status = BT_SELF_NOTIFICATION_ITERATOR_STATUS_END;
d4393e08 460 goto end;
223c70b2
PP
461 }
462
d4393e08 463 while (i < capacity && user_data->seq[user_data->at] != SEQ_END) {
d94d92ac 464 src_iter_next_seq_one(notif_iter, user_data, &notifs[i]);
d4393e08 465 i++;
223c70b2
PP
466 }
467
d4393e08
PP
468 BT_ASSERT(i > 0 && i <= capacity);
469 *count = i;
470
471end:
472 return status;
223c70b2
PP
473}
474
475static
d94d92ac
PP
476enum bt_self_notification_iterator_status src_iter_next(
477 struct bt_self_notification_iterator *self_notif_iter,
d4393e08
PP
478 bt_notification_array notifs, uint64_t capacity,
479 uint64_t *count)
223c70b2 480{
223c70b2 481 struct src_iter_user_data *user_data =
d94d92ac 482 bt_self_notification_iterator_get_data(self_notif_iter);
223c70b2 483
25583cd0 484 BT_ASSERT(user_data);
d94d92ac
PP
485 return src_iter_next_seq(self_notif_iter, user_data, notifs,
486 capacity, count);
223c70b2
PP
487}
488
489static
d94d92ac
PP
490enum bt_self_component_status src_init(
491 struct bt_self_component_source *self_comp,
223c70b2
PP
492 struct bt_value *params, void *init_method_data)
493{
147337a3 494 int ret;
b9d103be 495
d94d92ac
PP
496 ret = bt_self_component_source_add_output_port(
497 self_comp, "out", NULL, NULL);
25583cd0 498 BT_ASSERT(ret == 0);
d94d92ac 499 return BT_SELF_COMPONENT_STATUS_OK;
223c70b2
PP
500}
501
502static
d94d92ac 503void src_finalize(struct bt_self_component_source *self_comp)
223c70b2
PP
504{
505}
506
507static
d4393e08 508void append_test_events_from_notification(struct bt_notification *notification)
223c70b2 509{
223c70b2 510 struct test_event test_event = { 0 };
223c70b2
PP
511
512 switch (bt_notification_get_type(notification)) {
513 case BT_NOTIFICATION_TYPE_EVENT:
514 {
50842bdc 515 struct bt_event *event;
223c70b2
PP
516
517 test_event.type = TEST_EV_TYPE_NOTIF_EVENT;
312c056a 518 event = bt_notification_event_borrow_event(notification);
25583cd0 519 BT_ASSERT(event);
312c056a 520 test_event.packet = bt_event_borrow_packet(event);
25583cd0 521 BT_ASSERT(test_event.packet);
223c70b2
PP
522 break;
523 }
223c70b2
PP
524 case BT_NOTIFICATION_TYPE_STREAM_BEGIN:
525 test_event.type = TEST_EV_TYPE_NOTIF_STREAM_BEGIN;
526 test_event.stream =
312c056a 527 bt_notification_stream_begin_borrow_stream(notification);
25583cd0 528 BT_ASSERT(test_event.stream);
223c70b2
PP
529 break;
530 case BT_NOTIFICATION_TYPE_STREAM_END:
531 test_event.type = TEST_EV_TYPE_NOTIF_STREAM_END;
532 test_event.stream =
312c056a 533 bt_notification_stream_end_borrow_stream(notification);
25583cd0 534 BT_ASSERT(test_event.stream);
223c70b2
PP
535 break;
536 case BT_NOTIFICATION_TYPE_PACKET_BEGIN:
537 test_event.type = TEST_EV_TYPE_NOTIF_PACKET_BEGIN;
538 test_event.packet =
312c056a 539 bt_notification_packet_begin_borrow_packet(notification);
25583cd0 540 BT_ASSERT(test_event.packet);
223c70b2
PP
541 break;
542 case BT_NOTIFICATION_TYPE_PACKET_END:
543 test_event.type = TEST_EV_TYPE_NOTIF_PACKET_END;
544 test_event.packet =
312c056a 545 bt_notification_packet_end_borrow_packet(notification);
25583cd0 546 BT_ASSERT(test_event.packet);
223c70b2
PP
547 break;
548 default:
549 test_event.type = TEST_EV_TYPE_NOTIF_UNEXPECTED;
550 break;
551 }
552
553 if (test_event.packet) {
312c056a 554 test_event.stream = bt_packet_borrow_stream(test_event.packet);
25583cd0 555 BT_ASSERT(test_event.stream);
223c70b2
PP
556 }
557
d4393e08
PP
558 append_test_event(&test_event);
559}
560
561static
562enum bt_notification_iterator_status common_consume(
d94d92ac 563 void *notif_iter, bool is_output_port_notif_iter)
d4393e08
PP
564{
565 enum bt_notification_iterator_status ret;
566 bt_notification_array notifications = NULL;
567 uint64_t count = 0;
568 struct test_event test_event = { 0 };
569 uint64_t i;
570
571 BT_ASSERT(notif_iter);
572
573 if (is_output_port_notif_iter) {
d94d92ac 574 ret = bt_port_output_notification_iterator_next(notif_iter,
d4393e08
PP
575 &notifications, &count);
576 } else {
d94d92ac 577 ret = bt_self_component_port_input_notification_iterator_next(
d4393e08
PP
578 notif_iter, &notifications, &count);
579 }
580
581 if (ret < 0) {
582 goto end;
583 }
584
585 switch (ret) {
586 case BT_NOTIFICATION_ITERATOR_STATUS_END:
587 test_event.type = TEST_EV_TYPE_END;
223c70b2 588 append_test_event(&test_event);
d4393e08
PP
589 goto end;
590 case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN:
591 abort();
592 default:
593 break;
223c70b2
PP
594 }
595
d4393e08
PP
596 BT_ASSERT(notifications);
597 BT_ASSERT(count > 0);
598
599 for (i = 0; i < count; i++) {
600 append_test_events_from_notification(notifications[i]);
65300d60 601 bt_object_put_ref(notifications[i]);
d4393e08
PP
602 }
603
604end:
223c70b2
PP
605 return ret;
606}
607
e893886e 608static
d94d92ac
PP
609enum bt_self_component_status sink_consume(
610 struct bt_self_component_sink *self_comp)
e893886e 611{
d94d92ac 612 enum bt_self_component_status ret = BT_SELF_COMPONENT_STATUS_OK;
e893886e 613 struct sink_user_data *user_data =
d94d92ac
PP
614 bt_self_component_get_data(
615 bt_self_component_sink_borrow_self_component(
616 self_comp));
e893886e
PP
617 enum bt_notification_iterator_status it_ret;
618
25583cd0 619 BT_ASSERT(user_data && user_data->notif_iter);
07245ac2 620 it_ret = common_consume(user_data->notif_iter, false);
e893886e
PP
621
622 if (it_ret < 0) {
d94d92ac 623 ret = BT_SELF_COMPONENT_STATUS_ERROR;
e893886e
PP
624 goto end;
625 }
626
627 switch (it_ret) {
628 case BT_NOTIFICATION_ITERATOR_STATUS_END:
d94d92ac 629 ret = BT_SELF_COMPONENT_STATUS_END;
65300d60 630 BT_OBJECT_PUT_REF_AND_RESET(user_data->notif_iter);
e893886e
PP
631 goto end;
632 case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN:
633 abort();
634 default:
635 break;
636 }
637
638end:
639 return ret;
640}
641
223c70b2 642static
d94d92ac
PP
643enum bt_self_component_status sink_port_connected(
644 struct bt_self_component_sink *self_comp,
645 struct bt_self_component_port_input *self_port,
646 struct bt_port_output *other_port)
223c70b2 647{
d94d92ac
PP
648 struct sink_user_data *user_data =
649 bt_self_component_get_data(
650 bt_self_component_sink_borrow_self_component(
651 self_comp));
223c70b2 652
25583cd0 653 BT_ASSERT(user_data);
d94d92ac
PP
654 user_data->notif_iter =
655 bt_self_component_port_input_notification_iterator_create(
656 self_port);
657 return BT_SELF_COMPONENT_STATUS_OK;
223c70b2
PP
658}
659
660static
d94d92ac
PP
661enum bt_self_component_status sink_init(
662 struct bt_self_component_sink *self_comp,
223c70b2
PP
663 struct bt_value *params, void *init_method_data)
664{
665 struct sink_user_data *user_data = g_new0(struct sink_user_data, 1);
666 int ret;
667
25583cd0 668 BT_ASSERT(user_data);
d94d92ac
PP
669 bt_self_component_set_data(
670 bt_self_component_sink_borrow_self_component(self_comp),
223c70b2 671 user_data);
d94d92ac
PP
672 ret = bt_self_component_sink_add_input_port(
673 self_comp, "in", NULL, NULL);
25583cd0 674 BT_ASSERT(ret == 0);
d94d92ac 675 return BT_SELF_COMPONENT_STATUS_OK;
223c70b2
PP
676}
677
678static
d94d92ac 679void sink_finalize(struct bt_self_component_sink *self_comp)
223c70b2 680{
d94d92ac
PP
681 struct sink_user_data *user_data =
682 bt_self_component_get_data(
683 bt_self_component_sink_borrow_self_component(
684 self_comp));
223c70b2
PP
685
686 if (user_data) {
65300d60 687 bt_object_put_ref(user_data->notif_iter);
223c70b2
PP
688 g_free(user_data);
689 }
690}
691
692static
d94d92ac
PP
693void create_source_sink(struct bt_private_graph *graph,
694 struct bt_component_source **source,
695 struct bt_component_sink **sink)
223c70b2 696{
d94d92ac
PP
697 struct bt_private_component_class_source *src_comp_class;
698 struct bt_private_component_class_sink *sink_comp_class;
223c70b2
PP
699 int ret;
700
701 /* Create source component */
e893886e 702 if (source) {
d94d92ac 703 src_comp_class = bt_private_component_class_source_create("src",
e893886e 704 src_iter_next);
25583cd0 705 BT_ASSERT(src_comp_class);
d94d92ac
PP
706 ret = bt_private_component_class_source_set_init_method(
707 src_comp_class, src_init);
25583cd0 708 BT_ASSERT(ret == 0);
d94d92ac
PP
709 ret = bt_private_component_class_source_set_finalize_method(
710 src_comp_class, src_finalize);
25583cd0 711 BT_ASSERT(ret == 0);
d94d92ac 712 ret = bt_private_component_class_source_set_notification_iterator_init_method(
e893886e 713 src_comp_class, src_iter_init);
25583cd0 714 BT_ASSERT(ret == 0);
d94d92ac 715 ret = bt_private_component_class_source_set_notification_iterator_finalize_method(
e893886e 716 src_comp_class, src_iter_finalize);
25583cd0 717 BT_ASSERT(ret == 0);
d94d92ac
PP
718 ret = bt_private_graph_add_source_component(graph,
719 bt_private_component_class_source_borrow_component_class_source(
720 src_comp_class), "source", NULL, source);
25583cd0 721 BT_ASSERT(ret == 0);
65300d60 722 bt_object_put_ref(src_comp_class);
e893886e 723 }
223c70b2
PP
724
725 /* Create sink component */
e893886e 726 if (sink) {
d94d92ac 727 sink_comp_class = bt_private_component_class_sink_create("sink",
e893886e 728 sink_consume);
25583cd0 729 BT_ASSERT(sink_comp_class);
d94d92ac
PP
730 ret = bt_private_component_class_sink_set_init_method(
731 sink_comp_class, sink_init);
25583cd0 732 BT_ASSERT(ret == 0);
d94d92ac
PP
733 ret = bt_private_component_class_sink_set_finalize_method(
734 sink_comp_class, sink_finalize);
735 ret = bt_private_component_class_sink_set_input_port_connected_method(
e893886e 736 sink_comp_class, sink_port_connected);
25583cd0 737 BT_ASSERT(ret == 0);
d94d92ac
PP
738 ret = bt_private_graph_add_sink_component(graph,
739 bt_private_component_class_sink_borrow_component_class_sink(
740 sink_comp_class),
741 "sink", NULL, sink);
25583cd0 742 BT_ASSERT(ret == 0);
65300d60 743 bt_object_put_ref(sink_comp_class);
e893886e 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{
d94d92ac
PP
751 struct bt_component_source *src_comp;
752 struct bt_component_sink *sink_comp;
753 struct bt_port_output *upstream_port;
754 struct bt_port_input *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);
a2d06fd5 761 graph = bt_private_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 */
d94d92ac
PP
766 upstream_port = bt_component_source_borrow_output_port_by_name(
767 src_comp, "out");
25583cd0 768 BT_ASSERT(upstream_port);
d94d92ac
PP
769 downstream_port = bt_component_sink_borrow_input_port_by_name(
770 sink_comp, "in");
25583cd0 771 BT_ASSERT(downstream_port);
a2d06fd5 772 graph_status = bt_private_graph_connect_ports(graph, upstream_port,
a256a42d 773 downstream_port, NULL);
223c70b2
PP
774
775 /* Run the graph until the end */
776 while (graph_status == BT_GRAPH_STATUS_OK ||
777 graph_status == BT_GRAPH_STATUS_AGAIN) {
a2d06fd5 778 graph_status = bt_private_graph_run(graph);
223c70b2
PP
779 }
780
d94d92ac
PP
781 ok(graph_status == BT_GRAPH_STATUS_END,
782 "graph finishes without any error");
223c70b2
PP
783
784 /* Compare the resulting test events */
785 if (expected_test_events) {
786 ok(compare_test_events(expected_test_events),
787 "the produced sequence of test events is the expected one");
788 }
789
65300d60
PP
790 bt_object_put_ref(src_comp);
791 bt_object_put_ref(sink_comp);
792 BT_OBJECT_PUT_REF_AND_RESET(graph);
223c70b2
PP
793}
794
795static
796void test_no_auto_notifs(void)
797{
798 const struct test_event expected_test_events[] = {
e5be10ef
PP
799 { .type = TEST_EV_TYPE_NOTIF_STREAM_BEGIN, .stream = pub_src_stream1, .packet = NULL, },
800 { .type = TEST_EV_TYPE_NOTIF_PACKET_BEGIN, .stream = pub_src_stream1, .packet = pub_src_stream1_packet1, },
801 { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = pub_src_stream1, .packet = pub_src_stream1_packet1, },
802 { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = pub_src_stream1, .packet = pub_src_stream1_packet1, },
803 { .type = TEST_EV_TYPE_NOTIF_STREAM_BEGIN, .stream = pub_src_stream2, .packet = NULL, },
804 { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = pub_src_stream1, .packet = pub_src_stream1_packet1, },
805 { .type = TEST_EV_TYPE_NOTIF_PACKET_BEGIN, .stream = pub_src_stream2, .packet = pub_src_stream2_packet2, },
806 { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = pub_src_stream2, .packet = pub_src_stream2_packet2, },
807 { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = pub_src_stream1, .packet = pub_src_stream1_packet1, },
808 { .type = TEST_EV_TYPE_NOTIF_PACKET_END, .stream = pub_src_stream1, .packet = pub_src_stream1_packet1, },
809 { .type = TEST_EV_TYPE_NOTIF_PACKET_END, .stream = pub_src_stream2, .packet = pub_src_stream2_packet2, },
810 { .type = TEST_EV_TYPE_NOTIF_PACKET_BEGIN, .stream = pub_src_stream1, .packet = pub_src_stream1_packet2, },
811 { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = pub_src_stream1, .packet = pub_src_stream1_packet2, },
812 { .type = TEST_EV_TYPE_NOTIF_STREAM_END, .stream = pub_src_stream2, .packet = NULL, },
813 { .type = TEST_EV_TYPE_NOTIF_PACKET_END, .stream = pub_src_stream1, .packet = pub_src_stream1_packet2, },
814 { .type = TEST_EV_TYPE_NOTIF_STREAM_END, .stream = pub_src_stream1, .packet = NULL, },
223c70b2
PP
815 { .type = TEST_EV_TYPE_END, },
816 { .type = TEST_EV_TYPE_SENTINEL, },
817 };
818
819 do_std_test(TEST_NO_AUTO_NOTIFS, "no automatic notifications",
820 expected_test_events);
821}
822
e893886e
PP
823static
824void test_output_port_notification_iterator(void)
825{
826 const struct test_event expected_test_events[] = {
e5be10ef
PP
827 { .type = TEST_EV_TYPE_NOTIF_STREAM_BEGIN, .stream = pub_src_stream1, .packet = NULL, },
828 { .type = TEST_EV_TYPE_NOTIF_PACKET_BEGIN, .stream = pub_src_stream1, .packet = pub_src_stream1_packet1, },
829 { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = pub_src_stream1, .packet = pub_src_stream1_packet1, },
830 { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = pub_src_stream1, .packet = pub_src_stream1_packet1, },
831 { .type = TEST_EV_TYPE_NOTIF_STREAM_BEGIN, .stream = pub_src_stream2, .packet = NULL, },
832 { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = pub_src_stream1, .packet = pub_src_stream1_packet1, },
833 { .type = TEST_EV_TYPE_NOTIF_PACKET_BEGIN, .stream = pub_src_stream2, .packet = pub_src_stream2_packet2, },
834 { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = pub_src_stream2, .packet = pub_src_stream2_packet2, },
835 { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = pub_src_stream1, .packet = pub_src_stream1_packet1, },
836 { .type = TEST_EV_TYPE_NOTIF_PACKET_END, .stream = pub_src_stream1, .packet = pub_src_stream1_packet1, },
837 { .type = TEST_EV_TYPE_NOTIF_PACKET_END, .stream = pub_src_stream2, .packet = pub_src_stream2_packet2, },
838 { .type = TEST_EV_TYPE_NOTIF_PACKET_BEGIN, .stream = pub_src_stream1, .packet = pub_src_stream1_packet2, },
839 { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = pub_src_stream1, .packet = pub_src_stream1_packet2, },
840 { .type = TEST_EV_TYPE_NOTIF_STREAM_END, .stream = pub_src_stream2, .packet = NULL, },
841 { .type = TEST_EV_TYPE_NOTIF_PACKET_END, .stream = pub_src_stream1, .packet = pub_src_stream1_packet2, },
842 { .type = TEST_EV_TYPE_NOTIF_STREAM_END, .stream = pub_src_stream1, .packet = NULL, },
e893886e
PP
843 { .type = TEST_EV_TYPE_END, },
844 { .type = TEST_EV_TYPE_SENTINEL, },
845 };
d94d92ac
PP
846 struct bt_component_source *src_comp;
847 struct bt_port_output_notification_iterator *notif_iter;
e893886e
PP
848 enum bt_notification_iterator_status iter_status =
849 BT_NOTIFICATION_ITERATOR_STATUS_OK;
d94d92ac 850 struct bt_port_output *upstream_port;
e893886e
PP
851
852 clear_test_events();
853 current_test = TEST_OUTPUT_PORT_NOTIFICATION_ITERATOR;
854 diag("test: output port notification iterator");
25583cd0 855 BT_ASSERT(!graph);
a2d06fd5 856 graph = bt_private_graph_create();
25583cd0 857 BT_ASSERT(graph);
e893886e
PP
858 create_source_sink(graph, &src_comp, NULL);
859
860 /* Create notification iterator on source's output port */
d94d92ac
PP
861 upstream_port = bt_component_source_borrow_output_port_by_name(
862 src_comp, "out");
863 notif_iter = bt_port_output_notification_iterator_create(graph,
864 upstream_port, NULL);
e5be10ef 865 ok(notif_iter, "bt_private_output_port_notification_iterator_create() succeeds");
e893886e
PP
866
867 /* Consume the notification iterator */
868 while (iter_status == BT_NOTIFICATION_ITERATOR_STATUS_OK) {
07245ac2 869 iter_status = common_consume(notif_iter, true);
e893886e
PP
870 }
871
872 ok(iter_status == BT_NOTIFICATION_ITERATOR_STATUS_END,
873 "output port notification iterator finishes without any error");
874
875 /* Compare the resulting test events */
876 ok(compare_test_events(expected_test_events),
877 "the produced sequence of test events is the expected one");
878
65300d60
PP
879 bt_object_put_ref(src_comp);
880 BT_OBJECT_PUT_REF_AND_RESET(graph);
881 bt_object_put_ref(notif_iter);
e893886e
PP
882}
883
223c70b2
PP
884#define DEBUG_ENV_VAR "TEST_BT_NOTIFICATION_ITERATOR_DEBUG"
885
886int main(int argc, char **argv)
887{
888 if (getenv(DEBUG_ENV_VAR) && strcmp(getenv(DEBUG_ENV_VAR), "1") == 0) {
889 debug = true;
890 }
891
892 plan_tests(NR_TESTS);
893 init_static_data();
894 test_no_auto_notifs();
e893886e 895 test_output_port_notification_iterator();
223c70b2
PP
896 fini_static_data();
897 return exit_status();
898}
This page took 0.076634 seconds and 4 git commands to generate.