lib: remove clock class priority map, use default clock value
[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;
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
25583cd0 248 BT_ASSERT(expected_events);
223c70b2
PP
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));
25583cd0 287 BT_ASSERT(test_events);
223c70b2
PP
288
289 /* Metadata */
50842bdc 290 empty_struct_ft = bt_field_type_structure_create();
25583cd0 291 BT_ASSERT(empty_struct_ft);
50842bdc 292 trace = bt_trace_create();
25583cd0 293 BT_ASSERT(trace);
3dca2276 294 ret = bt_trace_set_packet_header_field_type(trace, empty_struct_ft);
25583cd0 295 BT_ASSERT(ret == 0);
50842bdc 296 src_stream_class = bt_stream_class_create("my-stream-class");
25583cd0 297 BT_ASSERT(src_stream_class);
3dca2276 298 ret = bt_stream_class_set_packet_context_field_type(src_stream_class,
223c70b2 299 empty_struct_ft);
25583cd0 300 BT_ASSERT(ret == 0);
3dca2276 301 ret = bt_stream_class_set_event_header_field_type(src_stream_class,
223c70b2 302 empty_struct_ft);
25583cd0 303 BT_ASSERT(ret == 0);
3dca2276 304 ret = bt_stream_class_set_event_context_field_type(src_stream_class,
223c70b2 305 empty_struct_ft);
25583cd0 306 BT_ASSERT(ret == 0);
50842bdc 307 src_event_class = bt_event_class_create("my-event-class");
3dca2276 308 ret = bt_event_class_set_context_field_type(src_event_class,
223c70b2 309 empty_struct_ft);
25583cd0 310 BT_ASSERT(ret == 0);
3dca2276 311 ret = bt_event_class_set_payload_field_type(src_event_class,
223c70b2 312 empty_struct_ft);
25583cd0 313 BT_ASSERT(ret == 0);
50842bdc 314 ret = bt_stream_class_add_event_class(src_stream_class,
223c70b2 315 src_event_class);
25583cd0 316 BT_ASSERT(ret == 0);
50842bdc 317 ret = bt_trace_add_stream_class(trace, src_stream_class);
25583cd0 318 BT_ASSERT(ret == 0);
3dca2276 319 src_stream1 = bt_stream_create(src_stream_class, "stream-1", 0);
25583cd0 320 BT_ASSERT(src_stream1);
3dca2276 321 src_stream2 = bt_stream_create(src_stream_class, "stream-2", 1);
25583cd0 322 BT_ASSERT(src_stream2);
50842bdc 323 src_stream1_packet1 = bt_packet_create(src_stream1);
25583cd0 324 BT_ASSERT(src_stream1_packet1);
50842bdc 325 src_stream1_packet2 = bt_packet_create(src_stream1);
25583cd0 326 BT_ASSERT(src_stream1_packet2);
50842bdc 327 src_stream2_packet1 = bt_packet_create(src_stream2);
25583cd0 328 BT_ASSERT(src_stream2_packet1);
50842bdc 329 src_stream2_packet2 = bt_packet_create(src_stream2);
25583cd0 330 BT_ASSERT(src_stream2_packet2);
223c70b2
PP
331
332 if (debug) {
333 fprintf(stderr, ":: stream 1: %p\n", src_stream1);
334 fprintf(stderr, ":: stream 2: %p\n", src_stream2);
335 fprintf(stderr, ":: stream 1, packet 1: %p\n", src_stream1_packet1);
336 fprintf(stderr, ":: stream 1, packet 2: %p\n", src_stream1_packet2);
337 fprintf(stderr, ":: stream 2, packet 1: %p\n", src_stream2_packet1);
338 fprintf(stderr, ":: stream 2, packet 2: %p\n", src_stream2_packet2);
339 }
340
341 bt_put(trace);
342 bt_put(empty_struct_ft);
343}
344
345static
346void fini_static_data(void)
347{
348 /* Test events */
349 g_array_free(test_events, TRUE);
350
351 /* Metadata */
223c70b2
PP
352 bt_put(src_stream_class);
353 bt_put(src_event_class);
354 bt_put(src_stream1);
355 bt_put(src_stream2);
356 bt_put(src_stream1_packet1);
357 bt_put(src_stream1_packet2);
358 bt_put(src_stream2_packet1);
359 bt_put(src_stream2_packet2);
360}
361
362static
363void src_iter_finalize(
90157d89 364 struct bt_private_connection_private_notification_iterator *private_notification_iterator)
223c70b2
PP
365{
366 struct src_iter_user_data *user_data =
90157d89 367 bt_private_connection_private_notification_iterator_get_user_data(
223c70b2
PP
368 private_notification_iterator);
369
370 if (user_data) {
371 g_free(user_data);
372 }
373}
374
375static
376enum bt_notification_iterator_status src_iter_init(
90157d89 377 struct bt_private_connection_private_notification_iterator *priv_notif_iter,
223c70b2
PP
378 struct bt_private_port *private_port)
379{
380 struct src_iter_user_data *user_data =
381 g_new0(struct src_iter_user_data, 1);
382 int ret;
383
25583cd0 384 BT_ASSERT(user_data);
e893886e
PP
385 ret = bt_private_connection_private_notification_iterator_set_user_data(
386 priv_notif_iter, user_data);
25583cd0 387 BT_ASSERT(ret == 0);
223c70b2
PP
388
389 switch (current_test) {
390 case TEST_NO_AUTO_NOTIFS:
e893886e 391 case TEST_OUTPUT_PORT_NOTIFICATION_ITERATOR:
223c70b2
PP
392 user_data->seq = seq_no_auto_notifs;
393 break;
223c70b2 394 default:
0fbb9a9f 395 abort();
223c70b2
PP
396 }
397
398 return BT_NOTIFICATION_ITERATOR_STATUS_OK;
399}
400
223c70b2 401static
d4393e08
PP
402void src_iter_next_seq_one(struct src_iter_user_data *user_data,
403 struct bt_notification **notif)
223c70b2 404{
50842bdc 405 struct bt_packet *event_packet = NULL;
223c70b2 406
d4393e08 407 switch (user_data->seq[user_data->at]) {
223c70b2 408 case SEQ_INACTIVITY:
e22b45d0 409 *notif = bt_notification_inactivity_create(graph);
223c70b2
PP
410 break;
411 case SEQ_STREAM1_BEGIN:
d4393e08
PP
412 *notif = bt_notification_stream_begin_create(graph,
413 src_stream1);
223c70b2
PP
414 break;
415 case SEQ_STREAM2_BEGIN:
d4393e08
PP
416 *notif = bt_notification_stream_begin_create(graph,
417 src_stream2);
223c70b2
PP
418 break;
419 case SEQ_STREAM1_END:
d4393e08 420 *notif = bt_notification_stream_end_create(graph, src_stream1);
223c70b2
PP
421 break;
422 case SEQ_STREAM2_END:
d4393e08 423 *notif = bt_notification_stream_end_create(graph, src_stream2);
223c70b2
PP
424 break;
425 case SEQ_STREAM1_PACKET1_BEGIN:
d4393e08
PP
426 *notif = bt_notification_packet_begin_create(graph,
427 src_stream1_packet1);
223c70b2
PP
428 break;
429 case SEQ_STREAM1_PACKET2_BEGIN:
d4393e08
PP
430 *notif = bt_notification_packet_begin_create(graph,
431 src_stream1_packet2);
223c70b2
PP
432 break;
433 case SEQ_STREAM2_PACKET1_BEGIN:
d4393e08
PP
434 *notif = bt_notification_packet_begin_create(graph,
435 src_stream2_packet1);
223c70b2
PP
436 break;
437 case SEQ_STREAM2_PACKET2_BEGIN:
d4393e08
PP
438 *notif = bt_notification_packet_begin_create(graph,
439 src_stream2_packet2);
223c70b2
PP
440 break;
441 case SEQ_STREAM1_PACKET1_END:
d4393e08
PP
442 *notif = bt_notification_packet_end_create(graph,
443 src_stream1_packet1);
223c70b2
PP
444 break;
445 case SEQ_STREAM1_PACKET2_END:
d4393e08
PP
446 *notif = bt_notification_packet_end_create(graph,
447 src_stream1_packet2);
223c70b2
PP
448 break;
449 case SEQ_STREAM2_PACKET1_END:
d4393e08
PP
450 *notif = bt_notification_packet_end_create(graph,
451 src_stream2_packet1);
223c70b2
PP
452 break;
453 case SEQ_STREAM2_PACKET2_END:
d4393e08
PP
454 *notif = bt_notification_packet_end_create(graph,
455 src_stream2_packet2);
223c70b2
PP
456 break;
457 case SEQ_EVENT_STREAM1_PACKET1:
458 event_packet = src_stream1_packet1;
459 break;
460 case SEQ_EVENT_STREAM1_PACKET2:
461 event_packet = src_stream1_packet2;
462 break;
463 case SEQ_EVENT_STREAM2_PACKET1:
464 event_packet = src_stream2_packet1;
465 break;
466 case SEQ_EVENT_STREAM2_PACKET2:
467 event_packet = src_stream2_packet2;
468 break;
469 default:
0fbb9a9f 470 abort();
223c70b2
PP
471 }
472
473 if (event_packet) {
d4393e08 474 *notif = bt_notification_event_create(graph, src_event_class,
e22b45d0 475 event_packet);
d4393e08
PP
476 }
477
478 BT_ASSERT(*notif);
479 user_data->at++;
480}
481
482static
483enum bt_notification_iterator_status src_iter_next_seq(
484 struct src_iter_user_data *user_data,
485 bt_notification_array notifs, uint64_t capacity,
486 uint64_t *count)
487{
488 enum bt_notification_iterator_status status =
489 BT_NOTIFICATION_ITERATOR_STATUS_OK;
490 uint64_t i = 0;
491
492 BT_ASSERT(user_data->seq);
493
494 if (user_data->seq[user_data->at] == SEQ_END) {
495 status = BT_NOTIFICATION_ITERATOR_STATUS_END;
496 goto end;
223c70b2
PP
497 }
498
d4393e08
PP
499 while (i < capacity && user_data->seq[user_data->at] != SEQ_END) {
500 src_iter_next_seq_one(user_data, &notifs[i]);
501 i++;
223c70b2
PP
502 }
503
d4393e08
PP
504 BT_ASSERT(i > 0 && i <= capacity);
505 *count = i;
506
507end:
508 return status;
223c70b2
PP
509}
510
511static
d4393e08
PP
512enum bt_notification_iterator_status src_iter_next(
513 struct bt_private_connection_private_notification_iterator *priv_iterator,
514 bt_notification_array notifs, uint64_t capacity,
515 uint64_t *count)
223c70b2 516{
223c70b2 517 struct src_iter_user_data *user_data =
90157d89 518 bt_private_connection_private_notification_iterator_get_user_data(priv_iterator);
223c70b2 519
25583cd0 520 BT_ASSERT(user_data);
d4393e08 521 return src_iter_next_seq(user_data, notifs, capacity, count);
223c70b2
PP
522}
523
524static
525enum bt_component_status src_init(
526 struct bt_private_component *private_component,
527 struct bt_value *params, void *init_method_data)
528{
147337a3 529 int ret;
b9d103be 530
147337a3
PP
531 ret = bt_private_component_source_add_output_private_port(
532 private_component, "out", NULL, NULL);
25583cd0 533 BT_ASSERT(ret == 0);
223c70b2
PP
534 return BT_COMPONENT_STATUS_OK;
535}
536
537static
538void src_finalize(struct bt_private_component *private_component)
539{
540}
541
542static
d4393e08 543void append_test_events_from_notification(struct bt_notification *notification)
223c70b2 544{
223c70b2 545 struct test_event test_event = { 0 };
223c70b2
PP
546
547 switch (bt_notification_get_type(notification)) {
548 case BT_NOTIFICATION_TYPE_EVENT:
549 {
50842bdc 550 struct bt_event *event;
223c70b2
PP
551
552 test_event.type = TEST_EV_TYPE_NOTIF_EVENT;
312c056a 553 event = bt_notification_event_borrow_event(notification);
25583cd0 554 BT_ASSERT(event);
312c056a 555 test_event.packet = bt_event_borrow_packet(event);
25583cd0 556 BT_ASSERT(test_event.packet);
223c70b2
PP
557 break;
558 }
559 case BT_NOTIFICATION_TYPE_INACTIVITY:
560 test_event.type = TEST_EV_TYPE_NOTIF_INACTIVITY;
561 break;
562 case BT_NOTIFICATION_TYPE_STREAM_BEGIN:
563 test_event.type = TEST_EV_TYPE_NOTIF_STREAM_BEGIN;
564 test_event.stream =
312c056a 565 bt_notification_stream_begin_borrow_stream(notification);
25583cd0 566 BT_ASSERT(test_event.stream);
223c70b2
PP
567 break;
568 case BT_NOTIFICATION_TYPE_STREAM_END:
569 test_event.type = TEST_EV_TYPE_NOTIF_STREAM_END;
570 test_event.stream =
312c056a 571 bt_notification_stream_end_borrow_stream(notification);
25583cd0 572 BT_ASSERT(test_event.stream);
223c70b2
PP
573 break;
574 case BT_NOTIFICATION_TYPE_PACKET_BEGIN:
575 test_event.type = TEST_EV_TYPE_NOTIF_PACKET_BEGIN;
576 test_event.packet =
312c056a 577 bt_notification_packet_begin_borrow_packet(notification);
25583cd0 578 BT_ASSERT(test_event.packet);
223c70b2
PP
579 break;
580 case BT_NOTIFICATION_TYPE_PACKET_END:
581 test_event.type = TEST_EV_TYPE_NOTIF_PACKET_END;
582 test_event.packet =
312c056a 583 bt_notification_packet_end_borrow_packet(notification);
25583cd0 584 BT_ASSERT(test_event.packet);
223c70b2
PP
585 break;
586 default:
587 test_event.type = TEST_EV_TYPE_NOTIF_UNEXPECTED;
588 break;
589 }
590
591 if (test_event.packet) {
312c056a 592 test_event.stream = bt_packet_borrow_stream(test_event.packet);
25583cd0 593 BT_ASSERT(test_event.stream);
223c70b2
PP
594 }
595
d4393e08
PP
596 append_test_event(&test_event);
597}
598
599static
600enum bt_notification_iterator_status common_consume(
601 struct bt_notification_iterator *notif_iter,
602 bool is_output_port_notif_iter)
603{
604 enum bt_notification_iterator_status ret;
605 bt_notification_array notifications = NULL;
606 uint64_t count = 0;
607 struct test_event test_event = { 0 };
608 uint64_t i;
609
610 BT_ASSERT(notif_iter);
611
612 if (is_output_port_notif_iter) {
613 ret = bt_output_port_notification_iterator_next(notif_iter,
614 &notifications, &count);
615 } else {
616 ret = bt_private_connection_notification_iterator_next(
617 notif_iter, &notifications, &count);
618 }
619
620 if (ret < 0) {
621 goto end;
622 }
623
624 switch (ret) {
625 case BT_NOTIFICATION_ITERATOR_STATUS_END:
626 test_event.type = TEST_EV_TYPE_END;
223c70b2 627 append_test_event(&test_event);
d4393e08
PP
628 goto end;
629 case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN:
630 abort();
631 default:
632 break;
223c70b2
PP
633 }
634
d4393e08
PP
635 BT_ASSERT(notifications);
636 BT_ASSERT(count > 0);
637
638 for (i = 0; i < count; i++) {
639 append_test_events_from_notification(notifications[i]);
640 bt_put(notifications[i]);
641 }
642
643end:
223c70b2
PP
644 return ret;
645}
646
e893886e
PP
647static
648enum bt_component_status sink_consume(
649 struct bt_private_component *priv_component)
650{
651 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
652 struct sink_user_data *user_data =
653 bt_private_component_get_user_data(priv_component);
654 enum bt_notification_iterator_status it_ret;
655
25583cd0 656 BT_ASSERT(user_data && user_data->notif_iter);
07245ac2 657 it_ret = common_consume(user_data->notif_iter, false);
e893886e
PP
658
659 if (it_ret < 0) {
660 ret = BT_COMPONENT_STATUS_ERROR;
661 goto end;
662 }
663
664 switch (it_ret) {
665 case BT_NOTIFICATION_ITERATOR_STATUS_END:
666 ret = BT_COMPONENT_STATUS_END;
667 BT_PUT(user_data->notif_iter);
668 goto end;
669 case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN:
670 abort();
671 default:
672 break;
673 }
674
675end:
676 return ret;
677}
678
223c70b2 679static
bf55043c
PP
680enum bt_component_status sink_port_connected(
681 struct bt_private_component *private_component,
223c70b2
PP
682 struct bt_private_port *self_private_port,
683 struct bt_port *other_port)
684{
685 struct bt_private_connection *priv_conn =
686 bt_private_port_get_private_connection(self_private_port);
687 struct sink_user_data *user_data = bt_private_component_get_user_data(
688 private_component);
73d5c1ad 689 enum bt_connection_status conn_status;
223c70b2 690
25583cd0
PP
691 BT_ASSERT(user_data);
692 BT_ASSERT(priv_conn);
73d5c1ad 693 conn_status = bt_private_connection_create_notification_iterator(
f42867e2 694 priv_conn, &user_data->notif_iter);
25583cd0 695 BT_ASSERT(conn_status == 0);
223c70b2 696 bt_put(priv_conn);
bf55043c 697 return BT_COMPONENT_STATUS_OK;
223c70b2
PP
698}
699
700static
701enum bt_component_status sink_init(
702 struct bt_private_component *private_component,
703 struct bt_value *params, void *init_method_data)
704{
705 struct sink_user_data *user_data = g_new0(struct sink_user_data, 1);
706 int ret;
707
25583cd0 708 BT_ASSERT(user_data);
223c70b2
PP
709 ret = bt_private_component_set_user_data(private_component,
710 user_data);
25583cd0 711 BT_ASSERT(ret == 0);
147337a3
PP
712 ret = bt_private_component_sink_add_input_private_port(
713 private_component, "in", NULL, NULL);
25583cd0 714 BT_ASSERT(ret == 0);
223c70b2
PP
715 return BT_COMPONENT_STATUS_OK;
716}
717
718static
719void sink_finalize(struct bt_private_component *private_component)
720{
721 struct sink_user_data *user_data = bt_private_component_get_user_data(
722 private_component);
723
724 if (user_data) {
725 bt_put(user_data->notif_iter);
726 g_free(user_data);
727 }
728}
729
730static
36712f1d 731void create_source_sink(struct bt_graph *graph, struct bt_component **source,
223c70b2
PP
732 struct bt_component **sink)
733{
734 struct bt_component_class *src_comp_class;
735 struct bt_component_class *sink_comp_class;
736 int ret;
737
738 /* Create source component */
e893886e
PP
739 if (source) {
740 src_comp_class = bt_component_class_source_create("src",
741 src_iter_next);
25583cd0 742 BT_ASSERT(src_comp_class);
e893886e
PP
743 ret = bt_component_class_set_init_method(src_comp_class,
744 src_init);
25583cd0 745 BT_ASSERT(ret == 0);
e893886e
PP
746 ret = bt_component_class_set_finalize_method(src_comp_class,
747 src_finalize);
25583cd0 748 BT_ASSERT(ret == 0);
e893886e
PP
749 ret = bt_component_class_source_set_notification_iterator_init_method(
750 src_comp_class, src_iter_init);
25583cd0 751 BT_ASSERT(ret == 0);
e893886e
PP
752 ret = bt_component_class_source_set_notification_iterator_finalize_method(
753 src_comp_class, src_iter_finalize);
25583cd0 754 BT_ASSERT(ret == 0);
e893886e
PP
755 ret = bt_graph_add_component(graph, src_comp_class, "source",
756 NULL, source);
25583cd0 757 BT_ASSERT(ret == 0);
e893886e
PP
758 bt_put(src_comp_class);
759 }
223c70b2
PP
760
761 /* Create sink component */
e893886e
PP
762 if (sink) {
763 sink_comp_class = bt_component_class_sink_create("sink",
764 sink_consume);
25583cd0 765 BT_ASSERT(sink_comp_class);
e893886e
PP
766 ret = bt_component_class_set_init_method(sink_comp_class,
767 sink_init);
25583cd0 768 BT_ASSERT(ret == 0);
e893886e
PP
769 ret = bt_component_class_set_finalize_method(sink_comp_class,
770 sink_finalize);
771 ret = bt_component_class_set_port_connected_method(
772 sink_comp_class, sink_port_connected);
25583cd0 773 BT_ASSERT(ret == 0);
e893886e
PP
774 ret = bt_graph_add_component(graph, sink_comp_class, "sink",
775 NULL, sink);
25583cd0 776 BT_ASSERT(ret == 0);
e893886e
PP
777 bt_put(sink_comp_class);
778 }
223c70b2
PP
779}
780
781static
782void do_std_test(enum test test, const char *name,
783 const struct test_event *expected_test_events)
784{
785 struct bt_component *src_comp;
786 struct bt_component *sink_comp;
787 struct bt_port *upstream_port;
788 struct bt_port *downstream_port;
223c70b2
PP
789 enum bt_graph_status graph_status = BT_GRAPH_STATUS_OK;
790
791 clear_test_events();
792 current_test = test;
793 diag("test: %s", name);
25583cd0 794 BT_ASSERT(!graph);
223c70b2 795 graph = bt_graph_create();
25583cd0 796 BT_ASSERT(graph);
36712f1d 797 create_source_sink(graph, &src_comp, &sink_comp);
223c70b2
PP
798
799 /* Connect source to sink */
b9d103be 800 upstream_port = bt_component_source_get_output_port_by_name(src_comp, "out");
25583cd0 801 BT_ASSERT(upstream_port);
b9d103be 802 downstream_port = bt_component_sink_get_input_port_by_name(sink_comp, "in");
25583cd0 803 BT_ASSERT(downstream_port);
a256a42d
PP
804 graph_status = bt_graph_connect_ports(graph, upstream_port,
805 downstream_port, NULL);
223c70b2
PP
806 bt_put(upstream_port);
807 bt_put(downstream_port);
808
809 /* Run the graph until the end */
810 while (graph_status == BT_GRAPH_STATUS_OK ||
811 graph_status == BT_GRAPH_STATUS_AGAIN) {
812 graph_status = bt_graph_run(graph);
813 }
814
815 ok(graph_status == BT_GRAPH_STATUS_END, "graph finishes without any error");
816
817 /* Compare the resulting test events */
818 if (expected_test_events) {
819 ok(compare_test_events(expected_test_events),
820 "the produced sequence of test events is the expected one");
821 }
822
823 bt_put(src_comp);
824 bt_put(sink_comp);
5c563278 825 BT_PUT(graph);
223c70b2
PP
826}
827
828static
829void test_no_auto_notifs(void)
830{
831 const struct test_event expected_test_events[] = {
832 { .type = TEST_EV_TYPE_NOTIF_STREAM_BEGIN, .stream = src_stream1, .packet = NULL, },
833 { .type = TEST_EV_TYPE_NOTIF_PACKET_BEGIN, .stream = src_stream1, .packet = src_stream1_packet1, },
834 { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = src_stream1, .packet = src_stream1_packet1, },
835 { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = src_stream1, .packet = src_stream1_packet1, },
836 { .type = TEST_EV_TYPE_NOTIF_STREAM_BEGIN, .stream = src_stream2, .packet = NULL, },
837 { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = src_stream1, .packet = src_stream1_packet1, },
838 { .type = TEST_EV_TYPE_NOTIF_PACKET_BEGIN, .stream = src_stream2, .packet = src_stream2_packet2, },
839 { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = src_stream2, .packet = src_stream2_packet2, },
840 { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = src_stream1, .packet = src_stream1_packet1, },
841 { .type = TEST_EV_TYPE_NOTIF_PACKET_END, .stream = src_stream1, .packet = src_stream1_packet1, },
842 { .type = TEST_EV_TYPE_NOTIF_PACKET_END, .stream = src_stream2, .packet = src_stream2_packet2, },
843 { .type = TEST_EV_TYPE_NOTIF_PACKET_BEGIN, .stream = src_stream1, .packet = src_stream1_packet2, },
844 { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = src_stream1, .packet = src_stream1_packet2, },
845 { .type = TEST_EV_TYPE_NOTIF_STREAM_END, .stream = src_stream2, .packet = NULL, },
846 { .type = TEST_EV_TYPE_NOTIF_PACKET_END, .stream = src_stream1, .packet = src_stream1_packet2, },
847 { .type = TEST_EV_TYPE_NOTIF_STREAM_END, .stream = src_stream1, .packet = NULL, },
848 { .type = TEST_EV_TYPE_END, },
849 { .type = TEST_EV_TYPE_SENTINEL, },
850 };
851
852 do_std_test(TEST_NO_AUTO_NOTIFS, "no automatic notifications",
853 expected_test_events);
854}
855
e893886e
PP
856static
857void test_output_port_notification_iterator(void)
858{
859 const struct test_event expected_test_events[] = {
860 { .type = TEST_EV_TYPE_NOTIF_STREAM_BEGIN, .stream = src_stream1, .packet = NULL, },
861 { .type = TEST_EV_TYPE_NOTIF_PACKET_BEGIN, .stream = src_stream1, .packet = src_stream1_packet1, },
862 { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = src_stream1, .packet = src_stream1_packet1, },
863 { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = src_stream1, .packet = src_stream1_packet1, },
864 { .type = TEST_EV_TYPE_NOTIF_STREAM_BEGIN, .stream = src_stream2, .packet = NULL, },
865 { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = src_stream1, .packet = src_stream1_packet1, },
866 { .type = TEST_EV_TYPE_NOTIF_PACKET_BEGIN, .stream = src_stream2, .packet = src_stream2_packet2, },
867 { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = src_stream2, .packet = src_stream2_packet2, },
868 { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = src_stream1, .packet = src_stream1_packet1, },
869 { .type = TEST_EV_TYPE_NOTIF_PACKET_END, .stream = src_stream1, .packet = src_stream1_packet1, },
870 { .type = TEST_EV_TYPE_NOTIF_PACKET_END, .stream = src_stream2, .packet = src_stream2_packet2, },
871 { .type = TEST_EV_TYPE_NOTIF_PACKET_BEGIN, .stream = src_stream1, .packet = src_stream1_packet2, },
872 { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = src_stream1, .packet = src_stream1_packet2, },
873 { .type = TEST_EV_TYPE_NOTIF_STREAM_END, .stream = src_stream2, .packet = NULL, },
874 { .type = TEST_EV_TYPE_NOTIF_PACKET_END, .stream = src_stream1, .packet = src_stream1_packet2, },
875 { .type = TEST_EV_TYPE_NOTIF_STREAM_END, .stream = src_stream1, .packet = NULL, },
876 { .type = TEST_EV_TYPE_END, },
877 { .type = TEST_EV_TYPE_SENTINEL, },
878 };
879 struct bt_component *src_comp;
880 struct bt_notification_iterator *notif_iter;
881 enum bt_notification_iterator_status iter_status =
882 BT_NOTIFICATION_ITERATOR_STATUS_OK;
883 struct bt_port *upstream_port;
e893886e
PP
884
885 clear_test_events();
886 current_test = TEST_OUTPUT_PORT_NOTIFICATION_ITERATOR;
887 diag("test: output port notification iterator");
25583cd0 888 BT_ASSERT(!graph);
e893886e 889 graph = bt_graph_create();
25583cd0 890 BT_ASSERT(graph);
e893886e
PP
891 create_source_sink(graph, &src_comp, NULL);
892
893 /* Create notification iterator on source's output port */
894 upstream_port = bt_component_source_get_output_port_by_name(src_comp, "out");
895 notif_iter = bt_output_port_notification_iterator_create(upstream_port,
f42867e2 896 NULL);
e893886e
PP
897 ok(notif_iter, "bt_output_port_notification_iterator_create() succeeds");
898 bt_put(upstream_port);
899
900 /* Consume the notification iterator */
901 while (iter_status == BT_NOTIFICATION_ITERATOR_STATUS_OK) {
07245ac2 902 iter_status = common_consume(notif_iter, true);
e893886e
PP
903 }
904
905 ok(iter_status == BT_NOTIFICATION_ITERATOR_STATUS_END,
906 "output port notification iterator finishes without any error");
907
908 /* Compare the resulting test events */
909 ok(compare_test_events(expected_test_events),
910 "the produced sequence of test events is the expected one");
911
912 bt_put(src_comp);
5c563278 913 BT_PUT(graph);
e893886e
PP
914 bt_put(notif_iter);
915}
916
223c70b2
PP
917#define DEBUG_ENV_VAR "TEST_BT_NOTIFICATION_ITERATOR_DEBUG"
918
919int main(int argc, char **argv)
920{
921 if (getenv(DEBUG_ENV_VAR) && strcmp(getenv(DEBUG_ENV_VAR), "1") == 0) {
922 debug = true;
923 }
924
925 plan_tests(NR_TESTS);
926 init_static_data();
927 test_no_auto_notifs();
e893886e 928 test_output_port_notification_iterator();
223c70b2
PP
929 fini_static_data();
930 return exit_status();
931}
This page took 0.075121 seconds and 4 git commands to generate.