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