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