Fix: cli: Acquire reference on bt_value_null while parsing args
[babeltrace.git] / tests / lib / test_bt_message_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>
9e550e5f 24#include <babeltrace/babeltrace.h>
b8f13b8b 25#include <babeltrace/assert-internal.h>
223c70b2
PP
26#include <glib.h>
27
28#include "tap/tap.h"
29
371361a1 30#define NR_TESTS 5
223c70b2
PP
31
32enum test {
b09a5592
PP
33 TEST_NO_AUTO_MSGS,
34 TEST_OUTPUT_PORT_MESSAGE_ITERATOR,
223c70b2
PP
35};
36
37enum test_event_type {
b09a5592
PP
38 TEST_EV_TYPE_MSG_UNEXPECTED,
39 TEST_EV_TYPE_MSG_EVENT,
40 TEST_EV_TYPE_MSG_STREAM_BEGIN,
41 TEST_EV_TYPE_MSG_PACKET_BEGIN,
42 TEST_EV_TYPE_MSG_PACKET_END,
43 TEST_EV_TYPE_MSG_STREAM_END,
223c70b2
PP
44 TEST_EV_TYPE_END,
45 TEST_EV_TYPE_SENTINEL,
46};
47
48struct test_event {
49 enum test_event_type type;
8eee8ea2
PP
50 const bt_stream *stream;
51 const bt_packet *packet;
223c70b2
PP
52};
53
223c70b2
PP
54static bool debug = false;
55static enum test current_test;
56static GArray *test_events;
8eee8ea2
PP
57static bt_graph *graph;
58static bt_stream_class *src_stream_class;
59static bt_event_class *src_event_class;
60static bt_stream *src_stream1;
61static bt_stream *src_stream2;
62static bt_packet *src_stream1_packet1;
63static bt_packet *src_stream1_packet2;
64static bt_packet *src_stream2_packet1;
65static bt_packet *src_stream2_packet2;
223c70b2
PP
66
67enum {
68 SEQ_END = -1,
69 SEQ_STREAM1_BEGIN = -2,
70 SEQ_STREAM2_BEGIN = -3,
71 SEQ_STREAM1_END = -4,
72 SEQ_STREAM2_END = -5,
73 SEQ_STREAM1_PACKET1_BEGIN = -6,
74 SEQ_STREAM1_PACKET2_BEGIN = -7,
75 SEQ_STREAM2_PACKET1_BEGIN = -8,
76 SEQ_STREAM2_PACKET2_BEGIN = -9,
77 SEQ_STREAM1_PACKET1_END = -10,
78 SEQ_STREAM1_PACKET2_END = -11,
79 SEQ_STREAM2_PACKET1_END = -12,
80 SEQ_STREAM2_PACKET2_END = -13,
81 SEQ_EVENT_STREAM1_PACKET1 = -14,
82 SEQ_EVENT_STREAM1_PACKET2 = -15,
83 SEQ_EVENT_STREAM2_PACKET1 = -16,
84 SEQ_EVENT_STREAM2_PACKET2 = -17,
223c70b2
PP
85};
86
87struct src_iter_user_data {
88 int64_t *seq;
89 size_t at;
90};
91
92struct sink_user_data {
b09a5592 93 bt_self_component_port_input_message_iterator *msg_iter;
223c70b2
PP
94};
95
96/*
b09a5592
PP
97 * No automatic messages generated in this block.
98 * Stream 2 messages are more indented.
223c70b2 99 */
b09a5592 100static int64_t seq_no_auto_msgs[] = {
223c70b2
PP
101 SEQ_STREAM1_BEGIN,
102 SEQ_STREAM1_PACKET1_BEGIN,
103 SEQ_EVENT_STREAM1_PACKET1,
104 SEQ_EVENT_STREAM1_PACKET1,
105 SEQ_STREAM2_BEGIN,
106 SEQ_EVENT_STREAM1_PACKET1,
107 SEQ_STREAM2_PACKET2_BEGIN,
108 SEQ_EVENT_STREAM2_PACKET2,
109 SEQ_EVENT_STREAM1_PACKET1,
110 SEQ_STREAM1_PACKET1_END,
111 SEQ_STREAM2_PACKET2_END,
112 SEQ_STREAM1_PACKET2_BEGIN,
113 SEQ_EVENT_STREAM1_PACKET2,
114 SEQ_STREAM2_END,
115 SEQ_STREAM1_PACKET2_END,
116 SEQ_STREAM1_END,
117 SEQ_END,
118};
119
223c70b2
PP
120static
121void clear_test_events(void)
122{
123 g_array_set_size(test_events, 0);
124}
125
126static
127void print_test_event(FILE *fp, const struct test_event *event)
128{
129 fprintf(fp, "{ type = ");
130
131 switch (event->type) {
b09a5592
PP
132 case TEST_EV_TYPE_MSG_UNEXPECTED:
133 fprintf(fp, "TEST_EV_TYPE_MSG_UNEXPECTED");
223c70b2 134 break;
b09a5592
PP
135 case TEST_EV_TYPE_MSG_EVENT:
136 fprintf(fp, "TEST_EV_TYPE_MSG_EVENT");
223c70b2 137 break;
b09a5592
PP
138 case TEST_EV_TYPE_MSG_STREAM_BEGIN:
139 fprintf(fp, "TEST_EV_TYPE_MSG_STREAM_BEGIN");
223c70b2 140 break;
b09a5592
PP
141 case TEST_EV_TYPE_MSG_STREAM_END:
142 fprintf(fp, "TEST_EV_TYPE_MSG_STREAM_END");
223c70b2 143 break;
b09a5592
PP
144 case TEST_EV_TYPE_MSG_PACKET_BEGIN:
145 fprintf(fp, "TEST_EV_TYPE_MSG_PACKET_BEGIN");
223c70b2 146 break;
b09a5592
PP
147 case TEST_EV_TYPE_MSG_PACKET_END:
148 fprintf(fp, "TEST_EV_TYPE_MSG_PACKET_END");
223c70b2
PP
149 break;
150 case TEST_EV_TYPE_END:
151 fprintf(fp, "TEST_EV_TYPE_END");
152 break;
153 case TEST_EV_TYPE_SENTINEL:
154 fprintf(fp, "TEST_EV_TYPE_SENTINEL");
155 break;
156 default:
157 fprintf(fp, "(UNKNOWN)");
158 break;
159 }
160
161 fprintf(fp, ", stream = %p, packet = %p }", event->stream,
162 event->packet);
163}
164
165static
166void append_test_event(struct test_event *event)
167{
168 g_array_append_val(test_events, *event);
169}
170
171static
172bool compare_single_test_events(const struct test_event *ev_a,
173 const struct test_event *ev_b)
174{
175 if (debug) {
176 fprintf(stderr, ":: Comparing test events: ");
177 print_test_event(stderr, ev_a);
178 fprintf(stderr, " vs. ");
179 print_test_event(stderr, ev_b);
180 fprintf(stderr, "\n");
181 }
182
183 if (ev_a->type != ev_b->type) {
184 return false;
185 }
186
187 switch (ev_a->type) {
188 case TEST_EV_TYPE_END:
189 case TEST_EV_TYPE_SENTINEL:
190 break;
191 default:
192 if (ev_a->stream != ev_b->stream) {
193 return false;
194 }
195
196 if (ev_a->packet != ev_b->packet) {
197 return false;
198 }
199 break;
200 }
201
202 return true;
203}
204
205static
206bool compare_test_events(const struct test_event *expected_events)
207{
208 const struct test_event *expected_event = expected_events;
209 size_t i = 0;
210
b8f13b8b 211 BT_ASSERT(expected_events);
223c70b2
PP
212
213 while (true) {
214 const struct test_event *event;
215
216 if (expected_event->type == TEST_EV_TYPE_SENTINEL) {
217 break;
218 }
219
220 if (i >= test_events->len) {
221 return false;
222 }
223
224 event = &g_array_index(test_events, struct test_event, i);
225
226 if (!compare_single_test_events(event, expected_event)) {
227 return false;
228 }
229
230 i++;
231 expected_event++;
232 }
233
234 if (i != test_events->len) {
235 return false;
236 }
237
238 return true;
239}
240
241static
e5e71bf8 242void init_static_data(bt_self_component_source *self_comp)
223c70b2 243{
8eee8ea2
PP
244 bt_trace_class *trace_class;
245 bt_trace *trace;
223c70b2 246
10b7a2e4 247 /* Metadata, streams, and packets*/
e5e71bf8
PP
248 trace_class = bt_trace_class_create(
249 bt_self_component_source_as_self_component(self_comp));
250 BT_ASSERT(trace_class);
10b7a2e4 251 src_stream_class = bt_stream_class_create(trace_class);
b8f13b8b 252 BT_ASSERT(src_stream_class);
78cf9df6 253 src_event_class = bt_event_class_create(src_stream_class);
7b33a0e0 254 BT_ASSERT(src_event_class);
10b7a2e4
PP
255 trace = bt_trace_create(trace_class);
256 BT_ASSERT(trace);
257 src_stream1 = bt_stream_create(src_stream_class, trace);
b8f13b8b 258 BT_ASSERT(src_stream1);
10b7a2e4 259 src_stream2 = bt_stream_create(src_stream_class, trace);
b8f13b8b 260 BT_ASSERT(src_stream2);
78cf9df6 261 src_stream1_packet1 = bt_packet_create(src_stream1);
b8f13b8b 262 BT_ASSERT(src_stream1_packet1);
78cf9df6 263 src_stream1_packet2 = bt_packet_create(src_stream1);
b8f13b8b 264 BT_ASSERT(src_stream1_packet2);
78cf9df6 265 src_stream2_packet1 = bt_packet_create(src_stream2);
b8f13b8b 266 BT_ASSERT(src_stream2_packet1);
78cf9df6 267 src_stream2_packet2 = bt_packet_create(src_stream2);
b8f13b8b 268 BT_ASSERT(src_stream2_packet2);
223c70b2
PP
269
270 if (debug) {
271 fprintf(stderr, ":: stream 1: %p\n", src_stream1);
272 fprintf(stderr, ":: stream 2: %p\n", src_stream2);
273 fprintf(stderr, ":: stream 1, packet 1: %p\n", src_stream1_packet1);
274 fprintf(stderr, ":: stream 1, packet 2: %p\n", src_stream1_packet2);
275 fprintf(stderr, ":: stream 2, packet 1: %p\n", src_stream2_packet1);
276 fprintf(stderr, ":: stream 2, packet 2: %p\n", src_stream2_packet2);
277 }
278
8c6884d9
PP
279 bt_trace_put_ref(trace);
280 bt_trace_class_put_ref(trace_class);
223c70b2
PP
281}
282
283static
284void fini_static_data(void)
285{
223c70b2 286 /* Metadata */
8c6884d9
PP
287 bt_stream_class_put_ref(src_stream_class);
288 bt_event_class_put_ref(src_event_class);
289 bt_stream_put_ref(src_stream1);
290 bt_stream_put_ref(src_stream2);
291 bt_packet_put_ref(src_stream1_packet1);
292 bt_packet_put_ref(src_stream1_packet2);
293 bt_packet_put_ref(src_stream2_packet1);
294 bt_packet_put_ref(src_stream2_packet2);
223c70b2
PP
295}
296
297static
b09a5592 298void src_iter_finalize(bt_self_message_iterator *self_msg_iter)
223c70b2
PP
299{
300 struct src_iter_user_data *user_data =
b09a5592
PP
301 bt_self_message_iterator_get_data(
302 self_msg_iter);
223c70b2
PP
303
304 if (user_data) {
305 g_free(user_data);
306 }
307}
308
309static
ee78f405 310bt_self_message_iterator_status src_iter_init(
b09a5592 311 bt_self_message_iterator *self_msg_iter,
8eee8ea2
PP
312 bt_self_component_source *self_comp,
313 bt_self_component_port_output *self_port)
223c70b2
PP
314{
315 struct src_iter_user_data *user_data =
316 g_new0(struct src_iter_user_data, 1);
223c70b2 317
b8f13b8b 318 BT_ASSERT(user_data);
b09a5592 319 bt_self_message_iterator_set_data(self_msg_iter, user_data);
223c70b2
PP
320
321 switch (current_test) {
b09a5592
PP
322 case TEST_NO_AUTO_MSGS:
323 case TEST_OUTPUT_PORT_MESSAGE_ITERATOR:
324 user_data->seq = seq_no_auto_msgs;
223c70b2 325 break;
223c70b2 326 default:
0fbb9a9f 327 abort();
223c70b2
PP
328 }
329
b09a5592 330 return BT_SELF_MESSAGE_ITERATOR_STATUS_OK;
223c70b2
PP
331}
332
223c70b2 333static
b09a5592 334void src_iter_next_seq_one(bt_self_message_iterator* msg_iter,
834e9996 335 struct src_iter_user_data *user_data,
b09a5592 336 const bt_message **msg)
223c70b2 337{
8eee8ea2 338 bt_packet *event_packet = NULL;
223c70b2 339
3fd7b79d 340 switch (user_data->seq[user_data->at]) {
223c70b2 341 case SEQ_STREAM1_BEGIN:
b09a5592 342 *msg = bt_message_stream_beginning_create(msg_iter,
7b53201c 343 src_stream1);
223c70b2
PP
344 break;
345 case SEQ_STREAM2_BEGIN:
b09a5592 346 *msg = bt_message_stream_beginning_create(msg_iter,
7b53201c 347 src_stream2);
223c70b2
PP
348 break;
349 case SEQ_STREAM1_END:
b09a5592 350 *msg = bt_message_stream_end_create(msg_iter,
7b53201c 351 src_stream1);
223c70b2
PP
352 break;
353 case SEQ_STREAM2_END:
b09a5592 354 *msg = bt_message_stream_end_create(msg_iter,
7b53201c 355 src_stream2);
223c70b2
PP
356 break;
357 case SEQ_STREAM1_PACKET1_BEGIN:
b09a5592 358 *msg = bt_message_packet_beginning_create(msg_iter,
7b53201c 359 src_stream1_packet1);
223c70b2
PP
360 break;
361 case SEQ_STREAM1_PACKET2_BEGIN:
b09a5592 362 *msg = bt_message_packet_beginning_create(msg_iter,
7b53201c 363 src_stream1_packet2);
223c70b2
PP
364 break;
365 case SEQ_STREAM2_PACKET1_BEGIN:
b09a5592 366 *msg = bt_message_packet_beginning_create(msg_iter,
7b53201c 367 src_stream2_packet1);
223c70b2
PP
368 break;
369 case SEQ_STREAM2_PACKET2_BEGIN:
b09a5592 370 *msg = bt_message_packet_beginning_create(msg_iter,
7b53201c 371 src_stream2_packet2);
223c70b2
PP
372 break;
373 case SEQ_STREAM1_PACKET1_END:
b09a5592 374 *msg = bt_message_packet_end_create(msg_iter,
7b53201c 375 src_stream1_packet1);
223c70b2
PP
376 break;
377 case SEQ_STREAM1_PACKET2_END:
b09a5592 378 *msg = bt_message_packet_end_create(msg_iter,
7b53201c 379 src_stream1_packet2);
223c70b2
PP
380 break;
381 case SEQ_STREAM2_PACKET1_END:
b09a5592 382 *msg = bt_message_packet_end_create(msg_iter,
7b53201c 383 src_stream2_packet1);
223c70b2
PP
384 break;
385 case SEQ_STREAM2_PACKET2_END:
b09a5592 386 *msg = bt_message_packet_end_create(msg_iter,
7b53201c 387 src_stream2_packet2);
223c70b2
PP
388 break;
389 case SEQ_EVENT_STREAM1_PACKET1:
390 event_packet = src_stream1_packet1;
391 break;
392 case SEQ_EVENT_STREAM1_PACKET2:
393 event_packet = src_stream1_packet2;
394 break;
395 case SEQ_EVENT_STREAM2_PACKET1:
396 event_packet = src_stream2_packet1;
397 break;
398 case SEQ_EVENT_STREAM2_PACKET2:
399 event_packet = src_stream2_packet2;
400 break;
401 default:
0fbb9a9f 402 abort();
223c70b2
PP
403 }
404
405 if (event_packet) {
b09a5592 406 *msg = bt_message_event_create(msg_iter,
7b53201c
PP
407 src_event_class,
408 event_packet);
3fd7b79d
PP
409 }
410
b09a5592 411 BT_ASSERT(*msg);
3fd7b79d
PP
412 user_data->at++;
413}
414
415static
ee78f405 416bt_self_message_iterator_status src_iter_next_seq(
b09a5592 417 bt_self_message_iterator *msg_iter,
3fd7b79d 418 struct src_iter_user_data *user_data,
b09a5592 419 bt_message_array_const msgs, uint64_t capacity,
3fd7b79d
PP
420 uint64_t *count)
421{
ee78f405 422 bt_self_message_iterator_status status =
b09a5592 423 BT_SELF_MESSAGE_ITERATOR_STATUS_OK;
3fd7b79d
PP
424 uint64_t i = 0;
425
426 BT_ASSERT(user_data->seq);
427
428 if (user_data->seq[user_data->at] == SEQ_END) {
b09a5592 429 status = BT_SELF_MESSAGE_ITERATOR_STATUS_END;
3fd7b79d 430 goto end;
223c70b2
PP
431 }
432
3fd7b79d 433 while (i < capacity && user_data->seq[user_data->at] != SEQ_END) {
b09a5592 434 src_iter_next_seq_one(msg_iter, user_data, &msgs[i]);
3fd7b79d 435 i++;
223c70b2
PP
436 }
437
3fd7b79d
PP
438 BT_ASSERT(i > 0 && i <= capacity);
439 *count = i;
440
441end:
442 return status;
223c70b2
PP
443}
444
445static
ee78f405 446bt_self_message_iterator_status src_iter_next(
b09a5592
PP
447 bt_self_message_iterator *self_msg_iter,
448 bt_message_array_const msgs, uint64_t capacity,
3fd7b79d 449 uint64_t *count)
223c70b2 450{
223c70b2 451 struct src_iter_user_data *user_data =
b09a5592 452 bt_self_message_iterator_get_data(self_msg_iter);
223c70b2 453
b8f13b8b 454 BT_ASSERT(user_data);
b09a5592 455 return src_iter_next_seq(self_msg_iter, user_data, msgs,
834e9996 456 capacity, count);
223c70b2
PP
457}
458
459static
ee78f405 460bt_self_component_status src_init(
8eee8ea2
PP
461 bt_self_component_source *self_comp,
462 const bt_value *params, void *init_method_data)
223c70b2 463{
147337a3 464 int ret;
b9d103be 465
e5e71bf8 466 init_static_data(self_comp);
834e9996
PP
467 ret = bt_self_component_source_add_output_port(
468 self_comp, "out", NULL, NULL);
b8f13b8b 469 BT_ASSERT(ret == 0);
834e9996 470 return BT_SELF_COMPONENT_STATUS_OK;
223c70b2
PP
471}
472
473static
8eee8ea2 474void src_finalize(bt_self_component_source *self_comp)
223c70b2
PP
475{
476}
477
478static
b09a5592 479void append_test_events_from_message(const bt_message *message)
223c70b2 480{
223c70b2 481 struct test_event test_event = { 0 };
223c70b2 482
b09a5592
PP
483 switch (bt_message_get_type(message)) {
484 case BT_MESSAGE_TYPE_EVENT:
223c70b2 485 {
8eee8ea2 486 const bt_event *event;
223c70b2 487
b09a5592
PP
488 test_event.type = TEST_EV_TYPE_MSG_EVENT;
489 event = bt_message_event_borrow_event_const(message);
b8f13b8b 490 BT_ASSERT(event);
78cf9df6 491 test_event.packet = bt_event_borrow_packet_const(event);
b8f13b8b 492 BT_ASSERT(test_event.packet);
223c70b2
PP
493 break;
494 }
b09a5592
PP
495 case BT_MESSAGE_TYPE_STREAM_BEGINNING:
496 test_event.type = TEST_EV_TYPE_MSG_STREAM_BEGIN;
223c70b2 497 test_event.stream =
b09a5592 498 bt_message_stream_beginning_borrow_stream_const(message);
b8f13b8b 499 BT_ASSERT(test_event.stream);
223c70b2 500 break;
b09a5592
PP
501 case BT_MESSAGE_TYPE_STREAM_END:
502 test_event.type = TEST_EV_TYPE_MSG_STREAM_END;
223c70b2 503 test_event.stream =
b09a5592 504 bt_message_stream_end_borrow_stream_const(message);
b8f13b8b 505 BT_ASSERT(test_event.stream);
223c70b2 506 break;
b09a5592
PP
507 case BT_MESSAGE_TYPE_PACKET_BEGINNING:
508 test_event.type = TEST_EV_TYPE_MSG_PACKET_BEGIN;
223c70b2 509 test_event.packet =
b09a5592 510 bt_message_packet_beginning_borrow_packet_const(message);
b8f13b8b 511 BT_ASSERT(test_event.packet);
223c70b2 512 break;
b09a5592
PP
513 case BT_MESSAGE_TYPE_PACKET_END:
514 test_event.type = TEST_EV_TYPE_MSG_PACKET_END;
223c70b2 515 test_event.packet =
b09a5592 516 bt_message_packet_end_borrow_packet_const(message);
b8f13b8b 517 BT_ASSERT(test_event.packet);
223c70b2
PP
518 break;
519 default:
b09a5592 520 test_event.type = TEST_EV_TYPE_MSG_UNEXPECTED;
223c70b2
PP
521 break;
522 }
523
524 if (test_event.packet) {
78cf9df6
PP
525 test_event.stream = bt_packet_borrow_stream_const(
526 test_event.packet);
b8f13b8b 527 BT_ASSERT(test_event.stream);
223c70b2
PP
528 }
529
3fd7b79d
PP
530 append_test_event(&test_event);
531}
532
533static
ee78f405 534bt_message_iterator_status common_consume(
b09a5592 535 void *msg_iter, bool is_output_port_msg_iter)
3fd7b79d 536{
ee78f405 537 bt_message_iterator_status ret;
b09a5592 538 bt_message_array_const messages = NULL;
3fd7b79d
PP
539 uint64_t count = 0;
540 struct test_event test_event = { 0 };
541 uint64_t i;
542
b09a5592 543 BT_ASSERT(msg_iter);
3fd7b79d 544
b09a5592
PP
545 if (is_output_port_msg_iter) {
546 ret = bt_port_output_message_iterator_next(msg_iter,
547 &messages, &count);
3fd7b79d 548 } else {
b09a5592
PP
549 ret = bt_self_component_port_input_message_iterator_next(
550 msg_iter, &messages, &count);
3fd7b79d
PP
551 }
552
553 if (ret < 0) {
554 goto end;
555 }
556
557 switch (ret) {
b09a5592 558 case BT_MESSAGE_ITERATOR_STATUS_END:
3fd7b79d 559 test_event.type = TEST_EV_TYPE_END;
223c70b2 560 append_test_event(&test_event);
3fd7b79d 561 goto end;
b09a5592 562 case BT_MESSAGE_ITERATOR_STATUS_AGAIN:
3fd7b79d
PP
563 abort();
564 default:
565 break;
223c70b2
PP
566 }
567
b09a5592 568 BT_ASSERT(messages);
3fd7b79d
PP
569 BT_ASSERT(count > 0);
570
571 for (i = 0; i < count; i++) {
b09a5592
PP
572 append_test_events_from_message(messages[i]);
573 bt_message_put_ref(messages[i]);
3fd7b79d
PP
574 }
575
576end:
223c70b2
PP
577 return ret;
578}
579
71afbadd 580static
ee78f405 581bt_self_component_status sink_consume(
8eee8ea2 582 bt_self_component_sink *self_comp)
71afbadd 583{
ee78f405 584 bt_self_component_status ret = BT_SELF_COMPONENT_STATUS_OK;
71afbadd 585 struct sink_user_data *user_data =
834e9996 586 bt_self_component_get_data(
bb61965b 587 bt_self_component_sink_as_self_component(
834e9996 588 self_comp));
ee78f405 589 bt_message_iterator_status it_ret;
71afbadd 590
b09a5592
PP
591 BT_ASSERT(user_data && user_data->msg_iter);
592 it_ret = common_consume(user_data->msg_iter, false);
71afbadd
PP
593
594 if (it_ret < 0) {
834e9996 595 ret = BT_SELF_COMPONENT_STATUS_ERROR;
71afbadd
PP
596 goto end;
597 }
598
599 switch (it_ret) {
b09a5592 600 case BT_MESSAGE_ITERATOR_STATUS_END:
834e9996 601 ret = BT_SELF_COMPONENT_STATUS_END;
b09a5592
PP
602 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_PUT_REF_AND_RESET(
603 user_data->msg_iter);
71afbadd 604 goto end;
b09a5592 605 case BT_MESSAGE_ITERATOR_STATUS_AGAIN:
71afbadd
PP
606 abort();
607 default:
608 break;
609 }
610
611end:
612 return ret;
613}
614
223c70b2 615static
ee78f405 616bt_self_component_status sink_port_connected(
8eee8ea2
PP
617 bt_self_component_sink *self_comp,
618 bt_self_component_port_input *self_port,
619 const bt_port_output *other_port)
223c70b2 620{
834e9996
PP
621 struct sink_user_data *user_data =
622 bt_self_component_get_data(
bb61965b 623 bt_self_component_sink_as_self_component(
834e9996 624 self_comp));
223c70b2 625
b8f13b8b 626 BT_ASSERT(user_data);
b09a5592
PP
627 user_data->msg_iter =
628 bt_self_component_port_input_message_iterator_create(
834e9996
PP
629 self_port);
630 return BT_SELF_COMPONENT_STATUS_OK;
223c70b2
PP
631}
632
633static
ee78f405 634bt_self_component_status sink_init(
8eee8ea2
PP
635 bt_self_component_sink *self_comp,
636 const bt_value *params, void *init_method_data)
223c70b2
PP
637{
638 struct sink_user_data *user_data = g_new0(struct sink_user_data, 1);
639 int ret;
640
b8f13b8b 641 BT_ASSERT(user_data);
834e9996 642 bt_self_component_set_data(
bb61965b 643 bt_self_component_sink_as_self_component(self_comp),
223c70b2 644 user_data);
834e9996
PP
645 ret = bt_self_component_sink_add_input_port(
646 self_comp, "in", NULL, NULL);
b8f13b8b 647 BT_ASSERT(ret == 0);
834e9996 648 return BT_SELF_COMPONENT_STATUS_OK;
223c70b2
PP
649}
650
651static
8eee8ea2 652void sink_finalize(bt_self_component_sink *self_comp)
223c70b2 653{
834e9996
PP
654 struct sink_user_data *user_data =
655 bt_self_component_get_data(
bb61965b 656 bt_self_component_sink_as_self_component(
834e9996 657 self_comp));
223c70b2
PP
658
659 if (user_data) {
b09a5592
PP
660 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_PUT_REF_AND_RESET(
661 user_data->msg_iter);
223c70b2
PP
662 g_free(user_data);
663 }
664}
665
666static
8eee8ea2
PP
667void create_source_sink(bt_graph *graph,
668 const bt_component_source **source,
669 const bt_component_sink **sink)
223c70b2 670{
8eee8ea2
PP
671 bt_component_class_source *src_comp_class;
672 bt_component_class_sink *sink_comp_class;
223c70b2
PP
673 int ret;
674
675 /* Create source component */
71afbadd 676 if (source) {
7b53201c 677 src_comp_class = bt_component_class_source_create("src",
71afbadd 678 src_iter_next);
b8f13b8b 679 BT_ASSERT(src_comp_class);
7b53201c 680 ret = bt_component_class_source_set_init_method(
834e9996 681 src_comp_class, src_init);
b8f13b8b 682 BT_ASSERT(ret == 0);
7b53201c 683 ret = bt_component_class_source_set_finalize_method(
834e9996 684 src_comp_class, src_finalize);
b8f13b8b 685 BT_ASSERT(ret == 0);
b09a5592 686 ret = bt_component_class_source_set_message_iterator_init_method(
71afbadd 687 src_comp_class, src_iter_init);
b8f13b8b 688 BT_ASSERT(ret == 0);
b09a5592 689 ret = bt_component_class_source_set_message_iterator_finalize_method(
71afbadd 690 src_comp_class, src_iter_finalize);
b8f13b8b 691 BT_ASSERT(ret == 0);
7b53201c
PP
692 ret = bt_graph_add_source_component(graph,
693 src_comp_class, "source", NULL, source);
b8f13b8b 694 BT_ASSERT(ret == 0);
8c6884d9 695 bt_component_class_source_put_ref(src_comp_class);
71afbadd 696 }
223c70b2
PP
697
698 /* Create sink component */
71afbadd 699 if (sink) {
7b53201c 700 sink_comp_class = bt_component_class_sink_create("sink",
71afbadd 701 sink_consume);
b8f13b8b 702 BT_ASSERT(sink_comp_class);
7b53201c 703 ret = bt_component_class_sink_set_init_method(
834e9996 704 sink_comp_class, sink_init);
b8f13b8b 705 BT_ASSERT(ret == 0);
7b53201c 706 ret = bt_component_class_sink_set_finalize_method(
834e9996 707 sink_comp_class, sink_finalize);
7b53201c 708 ret = bt_component_class_sink_set_input_port_connected_method(
71afbadd 709 sink_comp_class, sink_port_connected);
b8f13b8b 710 BT_ASSERT(ret == 0);
7b53201c
PP
711 ret = bt_graph_add_sink_component(graph,
712 sink_comp_class,
834e9996 713 "sink", NULL, sink);
b8f13b8b 714 BT_ASSERT(ret == 0);
8c6884d9 715 bt_component_class_sink_put_ref(sink_comp_class);
71afbadd 716 }
223c70b2
PP
717}
718
e5e71bf8
PP
719typedef void (*compare_func_t)(void);
720
223c70b2 721static
e5e71bf8 722void do_std_test(enum test test, const char *name, compare_func_t compare_func)
223c70b2 723{
8eee8ea2
PP
724 const bt_component_source *src_comp;
725 const bt_component_sink *sink_comp;
726 const bt_port_output *upstream_port;
727 const bt_port_input *downstream_port;
ee78f405 728 bt_graph_status graph_status = BT_GRAPH_STATUS_OK;
223c70b2
PP
729
730 clear_test_events();
731 current_test = test;
732 diag("test: %s", name);
b8f13b8b 733 BT_ASSERT(!graph);
7b53201c 734 graph = bt_graph_create();
b8f13b8b 735 BT_ASSERT(graph);
36712f1d 736 create_source_sink(graph, &src_comp, &sink_comp);
223c70b2
PP
737
738 /* Connect source to sink */
7b53201c
PP
739 upstream_port =
740 bt_component_source_borrow_output_port_by_name_const(
741 src_comp, "out");
b8f13b8b 742 BT_ASSERT(upstream_port);
7b53201c 743 downstream_port = bt_component_sink_borrow_input_port_by_name_const(
834e9996 744 sink_comp, "in");
b8f13b8b 745 BT_ASSERT(downstream_port);
7b53201c 746 graph_status = bt_graph_connect_ports(graph, upstream_port,
a256a42d 747 downstream_port, NULL);
223c70b2
PP
748
749 /* Run the graph until the end */
750 while (graph_status == BT_GRAPH_STATUS_OK ||
751 graph_status == BT_GRAPH_STATUS_AGAIN) {
7b53201c 752 graph_status = bt_graph_run(graph);
223c70b2
PP
753 }
754
834e9996
PP
755 ok(graph_status == BT_GRAPH_STATUS_END,
756 "graph finishes without any error");
223c70b2
PP
757
758 /* Compare the resulting test events */
e5e71bf8
PP
759 if (compare_func) {
760 compare_func();
223c70b2
PP
761 }
762
8c6884d9
PP
763 bt_component_source_put_ref(src_comp);
764 bt_component_sink_put_ref(sink_comp);
e5e71bf8 765 fini_static_data();
8c6884d9 766 BT_GRAPH_PUT_REF_AND_RESET(graph);
223c70b2
PP
767}
768
769static
e5e71bf8 770void test_no_auto_msgs_compare(void)
223c70b2
PP
771{
772 const struct test_event expected_test_events[] = {
b09a5592
PP
773 { .type = TEST_EV_TYPE_MSG_STREAM_BEGIN, .stream = src_stream1, .packet = NULL, },
774 { .type = TEST_EV_TYPE_MSG_PACKET_BEGIN, .stream = src_stream1, .packet = src_stream1_packet1, },
775 { .type = TEST_EV_TYPE_MSG_EVENT, .stream = src_stream1, .packet = src_stream1_packet1, },
776 { .type = TEST_EV_TYPE_MSG_EVENT, .stream = src_stream1, .packet = src_stream1_packet1, },
777 { .type = TEST_EV_TYPE_MSG_STREAM_BEGIN, .stream = src_stream2, .packet = NULL, },
778 { .type = TEST_EV_TYPE_MSG_EVENT, .stream = src_stream1, .packet = src_stream1_packet1, },
779 { .type = TEST_EV_TYPE_MSG_PACKET_BEGIN, .stream = src_stream2, .packet = src_stream2_packet2, },
780 { .type = TEST_EV_TYPE_MSG_EVENT, .stream = src_stream2, .packet = src_stream2_packet2, },
781 { .type = TEST_EV_TYPE_MSG_EVENT, .stream = src_stream1, .packet = src_stream1_packet1, },
782 { .type = TEST_EV_TYPE_MSG_PACKET_END, .stream = src_stream1, .packet = src_stream1_packet1, },
783 { .type = TEST_EV_TYPE_MSG_PACKET_END, .stream = src_stream2, .packet = src_stream2_packet2, },
784 { .type = TEST_EV_TYPE_MSG_PACKET_BEGIN, .stream = src_stream1, .packet = src_stream1_packet2, },
785 { .type = TEST_EV_TYPE_MSG_EVENT, .stream = src_stream1, .packet = src_stream1_packet2, },
786 { .type = TEST_EV_TYPE_MSG_STREAM_END, .stream = src_stream2, .packet = NULL, },
787 { .type = TEST_EV_TYPE_MSG_PACKET_END, .stream = src_stream1, .packet = src_stream1_packet2, },
788 { .type = TEST_EV_TYPE_MSG_STREAM_END, .stream = src_stream1, .packet = NULL, },
223c70b2
PP
789 { .type = TEST_EV_TYPE_END, },
790 { .type = TEST_EV_TYPE_SENTINEL, },
791 };
792
e5e71bf8
PP
793 ok(compare_test_events(expected_test_events),
794 "the produced sequence of test events is the expected one");
795}
796
797static
798void test_no_auto_msgs(void)
799{
b09a5592 800 do_std_test(TEST_NO_AUTO_MSGS, "no automatic messages",
e5e71bf8 801 test_no_auto_msgs_compare);
223c70b2
PP
802}
803
71afbadd 804static
b09a5592 805void test_output_port_message_iterator(void)
71afbadd 806{
8eee8ea2 807 const bt_component_source *src_comp;
b09a5592 808 bt_port_output_message_iterator *msg_iter;
ee78f405 809 bt_message_iterator_status iter_status =
b09a5592 810 BT_MESSAGE_ITERATOR_STATUS_OK;
8eee8ea2 811 const bt_port_output *upstream_port;
71afbadd
PP
812
813 clear_test_events();
b09a5592
PP
814 current_test = TEST_OUTPUT_PORT_MESSAGE_ITERATOR;
815 diag("test: output port message iterator");
b8f13b8b 816 BT_ASSERT(!graph);
7b53201c 817 graph = bt_graph_create();
b8f13b8b 818 BT_ASSERT(graph);
71afbadd
PP
819 create_source_sink(graph, &src_comp, NULL);
820
b09a5592 821 /* Create message iterator on source's output port */
e5e71bf8
PP
822 upstream_port = bt_component_source_borrow_output_port_by_name_const(
823 src_comp, "out");
824 msg_iter = bt_port_output_message_iterator_create(graph, upstream_port);
b09a5592 825 ok(msg_iter, "bt_private_output_port_message_iterator_create() succeeds");
71afbadd 826
b09a5592
PP
827 /* Consume the message iterator */
828 while (iter_status == BT_MESSAGE_ITERATOR_STATUS_OK) {
829 iter_status = common_consume(msg_iter, true);
71afbadd
PP
830 }
831
b09a5592
PP
832 ok(iter_status == BT_MESSAGE_ITERATOR_STATUS_END,
833 "output port message iterator finishes without any error");
71afbadd
PP
834
835 /* Compare the resulting test events */
e5e71bf8
PP
836 {
837 const struct test_event expected_test_events[] = {
838 { .type = TEST_EV_TYPE_MSG_STREAM_BEGIN, .stream = src_stream1, .packet = NULL, },
839 { .type = TEST_EV_TYPE_MSG_PACKET_BEGIN, .stream = src_stream1, .packet = src_stream1_packet1, },
840 { .type = TEST_EV_TYPE_MSG_EVENT, .stream = src_stream1, .packet = src_stream1_packet1, },
841 { .type = TEST_EV_TYPE_MSG_EVENT, .stream = src_stream1, .packet = src_stream1_packet1, },
842 { .type = TEST_EV_TYPE_MSG_STREAM_BEGIN, .stream = src_stream2, .packet = NULL, },
843 { .type = TEST_EV_TYPE_MSG_EVENT, .stream = src_stream1, .packet = src_stream1_packet1, },
844 { .type = TEST_EV_TYPE_MSG_PACKET_BEGIN, .stream = src_stream2, .packet = src_stream2_packet2, },
845 { .type = TEST_EV_TYPE_MSG_EVENT, .stream = src_stream2, .packet = src_stream2_packet2, },
846 { .type = TEST_EV_TYPE_MSG_EVENT, .stream = src_stream1, .packet = src_stream1_packet1, },
847 { .type = TEST_EV_TYPE_MSG_PACKET_END, .stream = src_stream1, .packet = src_stream1_packet1, },
848 { .type = TEST_EV_TYPE_MSG_PACKET_END, .stream = src_stream2, .packet = src_stream2_packet2, },
849 { .type = TEST_EV_TYPE_MSG_PACKET_BEGIN, .stream = src_stream1, .packet = src_stream1_packet2, },
850 { .type = TEST_EV_TYPE_MSG_EVENT, .stream = src_stream1, .packet = src_stream1_packet2, },
851 { .type = TEST_EV_TYPE_MSG_STREAM_END, .stream = src_stream2, .packet = NULL, },
852 { .type = TEST_EV_TYPE_MSG_PACKET_END, .stream = src_stream1, .packet = src_stream1_packet2, },
853 { .type = TEST_EV_TYPE_MSG_STREAM_END, .stream = src_stream1, .packet = NULL, },
854 { .type = TEST_EV_TYPE_END, },
855 { .type = TEST_EV_TYPE_SENTINEL, },
856 };
857
858 ok(compare_test_events(expected_test_events),
859 "the produced sequence of test events is the expected one");
860 }
71afbadd 861
e5e71bf8 862 fini_static_data();
8c6884d9
PP
863 bt_component_source_put_ref(src_comp);
864 BT_GRAPH_PUT_REF_AND_RESET(graph);
b09a5592 865 bt_port_output_message_iterator_put_ref(msg_iter);
71afbadd
PP
866}
867
b09a5592 868#define DEBUG_ENV_VAR "TEST_BT_MESSAGE_ITERATOR_DEBUG"
223c70b2
PP
869
870int main(int argc, char **argv)
871{
872 if (getenv(DEBUG_ENV_VAR) && strcmp(getenv(DEBUG_ENV_VAR), "1") == 0) {
873 debug = true;
874 }
875
876 plan_tests(NR_TESTS);
e5e71bf8
PP
877 test_events = g_array_new(FALSE, TRUE, sizeof(struct test_event));
878 BT_ASSERT(test_events);
b09a5592
PP
879 test_no_auto_msgs();
880 test_output_port_message_iterator();
e5e71bf8 881 g_array_free(test_events, TRUE);
223c70b2
PP
882 return exit_status();
883}
This page took 0.08113 seconds and 4 git commands to generate.