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