| 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> |
| 21 | #include <stdbool.h> |
| 22 | #include <inttypes.h> |
| 23 | #include <string.h> |
| 24 | #include <babeltrace/babeltrace.h> |
| 25 | #include <babeltrace/assert-internal.h> |
| 26 | #include <glib.h> |
| 27 | |
| 28 | #include "tap/tap.h" |
| 29 | |
| 30 | #define NR_TESTS 5 |
| 31 | |
| 32 | enum test { |
| 33 | TEST_NO_AUTO_MSGS, |
| 34 | TEST_OUTPUT_PORT_MESSAGE_ITERATOR, |
| 35 | }; |
| 36 | |
| 37 | enum test_event_type { |
| 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, |
| 44 | TEST_EV_TYPE_END, |
| 45 | TEST_EV_TYPE_SENTINEL, |
| 46 | }; |
| 47 | |
| 48 | struct test_event { |
| 49 | enum test_event_type type; |
| 50 | const bt_stream *stream; |
| 51 | const bt_packet *packet; |
| 52 | }; |
| 53 | |
| 54 | static bool debug = false; |
| 55 | static enum test current_test; |
| 56 | static GArray *test_events; |
| 57 | static bt_graph *graph; |
| 58 | static bt_stream_class *src_stream_class; |
| 59 | static bt_event_class *src_event_class; |
| 60 | static bt_stream *src_stream1; |
| 61 | static bt_stream *src_stream2; |
| 62 | static bt_packet *src_stream1_packet1; |
| 63 | static bt_packet *src_stream1_packet2; |
| 64 | static bt_packet *src_stream2_packet1; |
| 65 | static bt_packet *src_stream2_packet2; |
| 66 | |
| 67 | enum { |
| 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, |
| 85 | }; |
| 86 | |
| 87 | struct src_iter_user_data { |
| 88 | int64_t *seq; |
| 89 | size_t at; |
| 90 | }; |
| 91 | |
| 92 | struct sink_user_data { |
| 93 | bt_self_component_port_input_message_iterator *msg_iter; |
| 94 | }; |
| 95 | |
| 96 | /* |
| 97 | * No automatic messages generated in this block. |
| 98 | * Stream 2 messages are more indented. |
| 99 | */ |
| 100 | static int64_t seq_no_auto_msgs[] = { |
| 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 | |
| 120 | static |
| 121 | void clear_test_events(void) |
| 122 | { |
| 123 | g_array_set_size(test_events, 0); |
| 124 | } |
| 125 | |
| 126 | static |
| 127 | void print_test_event(FILE *fp, const struct test_event *event) |
| 128 | { |
| 129 | fprintf(fp, "{ type = "); |
| 130 | |
| 131 | switch (event->type) { |
| 132 | case TEST_EV_TYPE_MSG_UNEXPECTED: |
| 133 | fprintf(fp, "TEST_EV_TYPE_MSG_UNEXPECTED"); |
| 134 | break; |
| 135 | case TEST_EV_TYPE_MSG_EVENT: |
| 136 | fprintf(fp, "TEST_EV_TYPE_MSG_EVENT"); |
| 137 | break; |
| 138 | case TEST_EV_TYPE_MSG_STREAM_BEGIN: |
| 139 | fprintf(fp, "TEST_EV_TYPE_MSG_STREAM_BEGIN"); |
| 140 | break; |
| 141 | case TEST_EV_TYPE_MSG_STREAM_END: |
| 142 | fprintf(fp, "TEST_EV_TYPE_MSG_STREAM_END"); |
| 143 | break; |
| 144 | case TEST_EV_TYPE_MSG_PACKET_BEGIN: |
| 145 | fprintf(fp, "TEST_EV_TYPE_MSG_PACKET_BEGIN"); |
| 146 | break; |
| 147 | case TEST_EV_TYPE_MSG_PACKET_END: |
| 148 | fprintf(fp, "TEST_EV_TYPE_MSG_PACKET_END"); |
| 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 | |
| 165 | static |
| 166 | void append_test_event(struct test_event *event) |
| 167 | { |
| 168 | g_array_append_val(test_events, *event); |
| 169 | } |
| 170 | |
| 171 | static |
| 172 | bool 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 | |
| 205 | static |
| 206 | bool 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 | |
| 211 | BT_ASSERT(expected_events); |
| 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 | |
| 241 | static |
| 242 | void init_static_data(bt_self_component_source *self_comp) |
| 243 | { |
| 244 | bt_trace_class *trace_class; |
| 245 | bt_trace *trace; |
| 246 | |
| 247 | /* Metadata, streams, and packets*/ |
| 248 | trace_class = bt_trace_class_create( |
| 249 | bt_self_component_source_as_self_component(self_comp)); |
| 250 | BT_ASSERT(trace_class); |
| 251 | src_stream_class = bt_stream_class_create(trace_class); |
| 252 | BT_ASSERT(src_stream_class); |
| 253 | src_event_class = bt_event_class_create(src_stream_class); |
| 254 | BT_ASSERT(src_event_class); |
| 255 | trace = bt_trace_create(trace_class); |
| 256 | BT_ASSERT(trace); |
| 257 | src_stream1 = bt_stream_create(src_stream_class, trace); |
| 258 | BT_ASSERT(src_stream1); |
| 259 | src_stream2 = bt_stream_create(src_stream_class, trace); |
| 260 | BT_ASSERT(src_stream2); |
| 261 | src_stream1_packet1 = bt_packet_create(src_stream1); |
| 262 | BT_ASSERT(src_stream1_packet1); |
| 263 | src_stream1_packet2 = bt_packet_create(src_stream1); |
| 264 | BT_ASSERT(src_stream1_packet2); |
| 265 | src_stream2_packet1 = bt_packet_create(src_stream2); |
| 266 | BT_ASSERT(src_stream2_packet1); |
| 267 | src_stream2_packet2 = bt_packet_create(src_stream2); |
| 268 | BT_ASSERT(src_stream2_packet2); |
| 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 | |
| 279 | bt_trace_put_ref(trace); |
| 280 | bt_trace_class_put_ref(trace_class); |
| 281 | } |
| 282 | |
| 283 | static |
| 284 | void fini_static_data(void) |
| 285 | { |
| 286 | /* Metadata */ |
| 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); |
| 295 | } |
| 296 | |
| 297 | static |
| 298 | void src_iter_finalize(bt_self_message_iterator *self_msg_iter) |
| 299 | { |
| 300 | struct src_iter_user_data *user_data = |
| 301 | bt_self_message_iterator_get_data( |
| 302 | self_msg_iter); |
| 303 | |
| 304 | if (user_data) { |
| 305 | g_free(user_data); |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | static |
| 310 | bt_self_message_iterator_status src_iter_init( |
| 311 | bt_self_message_iterator *self_msg_iter, |
| 312 | bt_self_component_source *self_comp, |
| 313 | bt_self_component_port_output *self_port) |
| 314 | { |
| 315 | struct src_iter_user_data *user_data = |
| 316 | g_new0(struct src_iter_user_data, 1); |
| 317 | |
| 318 | BT_ASSERT(user_data); |
| 319 | bt_self_message_iterator_set_data(self_msg_iter, user_data); |
| 320 | |
| 321 | switch (current_test) { |
| 322 | case TEST_NO_AUTO_MSGS: |
| 323 | case TEST_OUTPUT_PORT_MESSAGE_ITERATOR: |
| 324 | user_data->seq = seq_no_auto_msgs; |
| 325 | break; |
| 326 | default: |
| 327 | abort(); |
| 328 | } |
| 329 | |
| 330 | return BT_SELF_MESSAGE_ITERATOR_STATUS_OK; |
| 331 | } |
| 332 | |
| 333 | static |
| 334 | void src_iter_next_seq_one(bt_self_message_iterator* msg_iter, |
| 335 | struct src_iter_user_data *user_data, |
| 336 | const bt_message **msg) |
| 337 | { |
| 338 | bt_packet *event_packet = NULL; |
| 339 | |
| 340 | switch (user_data->seq[user_data->at]) { |
| 341 | case SEQ_STREAM1_BEGIN: |
| 342 | *msg = bt_message_stream_beginning_create(msg_iter, |
| 343 | src_stream1); |
| 344 | break; |
| 345 | case SEQ_STREAM2_BEGIN: |
| 346 | *msg = bt_message_stream_beginning_create(msg_iter, |
| 347 | src_stream2); |
| 348 | break; |
| 349 | case SEQ_STREAM1_END: |
| 350 | *msg = bt_message_stream_end_create(msg_iter, |
| 351 | src_stream1); |
| 352 | break; |
| 353 | case SEQ_STREAM2_END: |
| 354 | *msg = bt_message_stream_end_create(msg_iter, |
| 355 | src_stream2); |
| 356 | break; |
| 357 | case SEQ_STREAM1_PACKET1_BEGIN: |
| 358 | *msg = bt_message_packet_beginning_create(msg_iter, |
| 359 | src_stream1_packet1); |
| 360 | break; |
| 361 | case SEQ_STREAM1_PACKET2_BEGIN: |
| 362 | *msg = bt_message_packet_beginning_create(msg_iter, |
| 363 | src_stream1_packet2); |
| 364 | break; |
| 365 | case SEQ_STREAM2_PACKET1_BEGIN: |
| 366 | *msg = bt_message_packet_beginning_create(msg_iter, |
| 367 | src_stream2_packet1); |
| 368 | break; |
| 369 | case SEQ_STREAM2_PACKET2_BEGIN: |
| 370 | *msg = bt_message_packet_beginning_create(msg_iter, |
| 371 | src_stream2_packet2); |
| 372 | break; |
| 373 | case SEQ_STREAM1_PACKET1_END: |
| 374 | *msg = bt_message_packet_end_create(msg_iter, |
| 375 | src_stream1_packet1); |
| 376 | break; |
| 377 | case SEQ_STREAM1_PACKET2_END: |
| 378 | *msg = bt_message_packet_end_create(msg_iter, |
| 379 | src_stream1_packet2); |
| 380 | break; |
| 381 | case SEQ_STREAM2_PACKET1_END: |
| 382 | *msg = bt_message_packet_end_create(msg_iter, |
| 383 | src_stream2_packet1); |
| 384 | break; |
| 385 | case SEQ_STREAM2_PACKET2_END: |
| 386 | *msg = bt_message_packet_end_create(msg_iter, |
| 387 | src_stream2_packet2); |
| 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: |
| 402 | abort(); |
| 403 | } |
| 404 | |
| 405 | if (event_packet) { |
| 406 | *msg = bt_message_event_create(msg_iter, |
| 407 | src_event_class, |
| 408 | event_packet); |
| 409 | } |
| 410 | |
| 411 | BT_ASSERT(*msg); |
| 412 | user_data->at++; |
| 413 | } |
| 414 | |
| 415 | static |
| 416 | bt_self_message_iterator_status src_iter_next_seq( |
| 417 | bt_self_message_iterator *msg_iter, |
| 418 | struct src_iter_user_data *user_data, |
| 419 | bt_message_array_const msgs, uint64_t capacity, |
| 420 | uint64_t *count) |
| 421 | { |
| 422 | bt_self_message_iterator_status status = |
| 423 | BT_SELF_MESSAGE_ITERATOR_STATUS_OK; |
| 424 | uint64_t i = 0; |
| 425 | |
| 426 | BT_ASSERT(user_data->seq); |
| 427 | |
| 428 | if (user_data->seq[user_data->at] == SEQ_END) { |
| 429 | status = BT_SELF_MESSAGE_ITERATOR_STATUS_END; |
| 430 | goto end; |
| 431 | } |
| 432 | |
| 433 | while (i < capacity && user_data->seq[user_data->at] != SEQ_END) { |
| 434 | src_iter_next_seq_one(msg_iter, user_data, &msgs[i]); |
| 435 | i++; |
| 436 | } |
| 437 | |
| 438 | BT_ASSERT(i > 0 && i <= capacity); |
| 439 | *count = i; |
| 440 | |
| 441 | end: |
| 442 | return status; |
| 443 | } |
| 444 | |
| 445 | static |
| 446 | bt_self_message_iterator_status src_iter_next( |
| 447 | bt_self_message_iterator *self_msg_iter, |
| 448 | bt_message_array_const msgs, uint64_t capacity, |
| 449 | uint64_t *count) |
| 450 | { |
| 451 | struct src_iter_user_data *user_data = |
| 452 | bt_self_message_iterator_get_data(self_msg_iter); |
| 453 | |
| 454 | BT_ASSERT(user_data); |
| 455 | return src_iter_next_seq(self_msg_iter, user_data, msgs, |
| 456 | capacity, count); |
| 457 | } |
| 458 | |
| 459 | static |
| 460 | bt_self_component_status src_init( |
| 461 | bt_self_component_source *self_comp, |
| 462 | const bt_value *params, void *init_method_data) |
| 463 | { |
| 464 | int ret; |
| 465 | |
| 466 | init_static_data(self_comp); |
| 467 | ret = bt_self_component_source_add_output_port( |
| 468 | self_comp, "out", NULL, NULL); |
| 469 | BT_ASSERT(ret == 0); |
| 470 | return BT_SELF_COMPONENT_STATUS_OK; |
| 471 | } |
| 472 | |
| 473 | static |
| 474 | void src_finalize(bt_self_component_source *self_comp) |
| 475 | { |
| 476 | } |
| 477 | |
| 478 | static |
| 479 | void append_test_events_from_message(const bt_message *message) |
| 480 | { |
| 481 | struct test_event test_event = { 0 }; |
| 482 | |
| 483 | switch (bt_message_get_type(message)) { |
| 484 | case BT_MESSAGE_TYPE_EVENT: |
| 485 | { |
| 486 | const bt_event *event; |
| 487 | |
| 488 | test_event.type = TEST_EV_TYPE_MSG_EVENT; |
| 489 | event = bt_message_event_borrow_event_const(message); |
| 490 | BT_ASSERT(event); |
| 491 | test_event.packet = bt_event_borrow_packet_const(event); |
| 492 | BT_ASSERT(test_event.packet); |
| 493 | break; |
| 494 | } |
| 495 | case BT_MESSAGE_TYPE_STREAM_BEGINNING: |
| 496 | test_event.type = TEST_EV_TYPE_MSG_STREAM_BEGIN; |
| 497 | test_event.stream = |
| 498 | bt_message_stream_beginning_borrow_stream_const(message); |
| 499 | BT_ASSERT(test_event.stream); |
| 500 | break; |
| 501 | case BT_MESSAGE_TYPE_STREAM_END: |
| 502 | test_event.type = TEST_EV_TYPE_MSG_STREAM_END; |
| 503 | test_event.stream = |
| 504 | bt_message_stream_end_borrow_stream_const(message); |
| 505 | BT_ASSERT(test_event.stream); |
| 506 | break; |
| 507 | case BT_MESSAGE_TYPE_PACKET_BEGINNING: |
| 508 | test_event.type = TEST_EV_TYPE_MSG_PACKET_BEGIN; |
| 509 | test_event.packet = |
| 510 | bt_message_packet_beginning_borrow_packet_const(message); |
| 511 | BT_ASSERT(test_event.packet); |
| 512 | break; |
| 513 | case BT_MESSAGE_TYPE_PACKET_END: |
| 514 | test_event.type = TEST_EV_TYPE_MSG_PACKET_END; |
| 515 | test_event.packet = |
| 516 | bt_message_packet_end_borrow_packet_const(message); |
| 517 | BT_ASSERT(test_event.packet); |
| 518 | break; |
| 519 | default: |
| 520 | test_event.type = TEST_EV_TYPE_MSG_UNEXPECTED; |
| 521 | break; |
| 522 | } |
| 523 | |
| 524 | if (test_event.packet) { |
| 525 | test_event.stream = bt_packet_borrow_stream_const( |
| 526 | test_event.packet); |
| 527 | BT_ASSERT(test_event.stream); |
| 528 | } |
| 529 | |
| 530 | append_test_event(&test_event); |
| 531 | } |
| 532 | |
| 533 | static |
| 534 | bt_message_iterator_status common_consume( |
| 535 | void *msg_iter, bool is_output_port_msg_iter) |
| 536 | { |
| 537 | bt_message_iterator_status ret; |
| 538 | bt_message_array_const messages = NULL; |
| 539 | uint64_t count = 0; |
| 540 | struct test_event test_event = { 0 }; |
| 541 | uint64_t i; |
| 542 | |
| 543 | BT_ASSERT(msg_iter); |
| 544 | |
| 545 | if (is_output_port_msg_iter) { |
| 546 | ret = bt_port_output_message_iterator_next(msg_iter, |
| 547 | &messages, &count); |
| 548 | } else { |
| 549 | ret = bt_self_component_port_input_message_iterator_next( |
| 550 | msg_iter, &messages, &count); |
| 551 | } |
| 552 | |
| 553 | if (ret < 0) { |
| 554 | goto end; |
| 555 | } |
| 556 | |
| 557 | switch (ret) { |
| 558 | case BT_MESSAGE_ITERATOR_STATUS_END: |
| 559 | test_event.type = TEST_EV_TYPE_END; |
| 560 | append_test_event(&test_event); |
| 561 | goto end; |
| 562 | case BT_MESSAGE_ITERATOR_STATUS_AGAIN: |
| 563 | abort(); |
| 564 | default: |
| 565 | break; |
| 566 | } |
| 567 | |
| 568 | BT_ASSERT(messages); |
| 569 | BT_ASSERT(count > 0); |
| 570 | |
| 571 | for (i = 0; i < count; i++) { |
| 572 | append_test_events_from_message(messages[i]); |
| 573 | bt_message_put_ref(messages[i]); |
| 574 | } |
| 575 | |
| 576 | end: |
| 577 | return ret; |
| 578 | } |
| 579 | |
| 580 | static |
| 581 | bt_self_component_status sink_consume( |
| 582 | bt_self_component_sink *self_comp) |
| 583 | { |
| 584 | bt_self_component_status ret = BT_SELF_COMPONENT_STATUS_OK; |
| 585 | struct sink_user_data *user_data = |
| 586 | bt_self_component_get_data( |
| 587 | bt_self_component_sink_as_self_component( |
| 588 | self_comp)); |
| 589 | bt_message_iterator_status it_ret; |
| 590 | |
| 591 | BT_ASSERT(user_data && user_data->msg_iter); |
| 592 | it_ret = common_consume(user_data->msg_iter, false); |
| 593 | |
| 594 | if (it_ret < 0) { |
| 595 | ret = BT_SELF_COMPONENT_STATUS_ERROR; |
| 596 | goto end; |
| 597 | } |
| 598 | |
| 599 | switch (it_ret) { |
| 600 | case BT_MESSAGE_ITERATOR_STATUS_END: |
| 601 | ret = BT_SELF_COMPONENT_STATUS_END; |
| 602 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_PUT_REF_AND_RESET( |
| 603 | user_data->msg_iter); |
| 604 | goto end; |
| 605 | case BT_MESSAGE_ITERATOR_STATUS_AGAIN: |
| 606 | abort(); |
| 607 | default: |
| 608 | break; |
| 609 | } |
| 610 | |
| 611 | end: |
| 612 | return ret; |
| 613 | } |
| 614 | |
| 615 | static |
| 616 | bt_self_component_status sink_port_connected( |
| 617 | bt_self_component_sink *self_comp, |
| 618 | bt_self_component_port_input *self_port, |
| 619 | const bt_port_output *other_port) |
| 620 | { |
| 621 | struct sink_user_data *user_data = |
| 622 | bt_self_component_get_data( |
| 623 | bt_self_component_sink_as_self_component( |
| 624 | self_comp)); |
| 625 | |
| 626 | BT_ASSERT(user_data); |
| 627 | user_data->msg_iter = |
| 628 | bt_self_component_port_input_message_iterator_create( |
| 629 | self_port); |
| 630 | return BT_SELF_COMPONENT_STATUS_OK; |
| 631 | } |
| 632 | |
| 633 | static |
| 634 | bt_self_component_status sink_init( |
| 635 | bt_self_component_sink *self_comp, |
| 636 | const bt_value *params, void *init_method_data) |
| 637 | { |
| 638 | struct sink_user_data *user_data = g_new0(struct sink_user_data, 1); |
| 639 | int ret; |
| 640 | |
| 641 | BT_ASSERT(user_data); |
| 642 | bt_self_component_set_data( |
| 643 | bt_self_component_sink_as_self_component(self_comp), |
| 644 | user_data); |
| 645 | ret = bt_self_component_sink_add_input_port( |
| 646 | self_comp, "in", NULL, NULL); |
| 647 | BT_ASSERT(ret == 0); |
| 648 | return BT_SELF_COMPONENT_STATUS_OK; |
| 649 | } |
| 650 | |
| 651 | static |
| 652 | void sink_finalize(bt_self_component_sink *self_comp) |
| 653 | { |
| 654 | struct sink_user_data *user_data = |
| 655 | bt_self_component_get_data( |
| 656 | bt_self_component_sink_as_self_component( |
| 657 | self_comp)); |
| 658 | |
| 659 | if (user_data) { |
| 660 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_PUT_REF_AND_RESET( |
| 661 | user_data->msg_iter); |
| 662 | g_free(user_data); |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | static |
| 667 | void create_source_sink(bt_graph *graph, |
| 668 | const bt_component_source **source, |
| 669 | const bt_component_sink **sink) |
| 670 | { |
| 671 | bt_component_class_source *src_comp_class; |
| 672 | bt_component_class_sink *sink_comp_class; |
| 673 | int ret; |
| 674 | |
| 675 | /* Create source component */ |
| 676 | if (source) { |
| 677 | src_comp_class = bt_component_class_source_create("src", |
| 678 | src_iter_next); |
| 679 | BT_ASSERT(src_comp_class); |
| 680 | ret = bt_component_class_source_set_init_method( |
| 681 | src_comp_class, src_init); |
| 682 | BT_ASSERT(ret == 0); |
| 683 | ret = bt_component_class_source_set_finalize_method( |
| 684 | src_comp_class, src_finalize); |
| 685 | BT_ASSERT(ret == 0); |
| 686 | ret = bt_component_class_source_set_message_iterator_init_method( |
| 687 | src_comp_class, src_iter_init); |
| 688 | BT_ASSERT(ret == 0); |
| 689 | ret = bt_component_class_source_set_message_iterator_finalize_method( |
| 690 | src_comp_class, src_iter_finalize); |
| 691 | BT_ASSERT(ret == 0); |
| 692 | ret = bt_graph_add_source_component(graph, |
| 693 | src_comp_class, "source", NULL, source); |
| 694 | BT_ASSERT(ret == 0); |
| 695 | bt_component_class_source_put_ref(src_comp_class); |
| 696 | } |
| 697 | |
| 698 | /* Create sink component */ |
| 699 | if (sink) { |
| 700 | sink_comp_class = bt_component_class_sink_create("sink", |
| 701 | sink_consume); |
| 702 | BT_ASSERT(sink_comp_class); |
| 703 | ret = bt_component_class_sink_set_init_method( |
| 704 | sink_comp_class, sink_init); |
| 705 | BT_ASSERT(ret == 0); |
| 706 | ret = bt_component_class_sink_set_finalize_method( |
| 707 | sink_comp_class, sink_finalize); |
| 708 | ret = bt_component_class_sink_set_input_port_connected_method( |
| 709 | sink_comp_class, sink_port_connected); |
| 710 | BT_ASSERT(ret == 0); |
| 711 | ret = bt_graph_add_sink_component(graph, |
| 712 | sink_comp_class, |
| 713 | "sink", NULL, sink); |
| 714 | BT_ASSERT(ret == 0); |
| 715 | bt_component_class_sink_put_ref(sink_comp_class); |
| 716 | } |
| 717 | } |
| 718 | |
| 719 | typedef void (*compare_func_t)(void); |
| 720 | |
| 721 | static |
| 722 | void do_std_test(enum test test, const char *name, compare_func_t compare_func) |
| 723 | { |
| 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; |
| 728 | bt_graph_status graph_status = BT_GRAPH_STATUS_OK; |
| 729 | |
| 730 | clear_test_events(); |
| 731 | current_test = test; |
| 732 | diag("test: %s", name); |
| 733 | BT_ASSERT(!graph); |
| 734 | graph = bt_graph_create(); |
| 735 | BT_ASSERT(graph); |
| 736 | create_source_sink(graph, &src_comp, &sink_comp); |
| 737 | |
| 738 | /* Connect source to sink */ |
| 739 | upstream_port = |
| 740 | bt_component_source_borrow_output_port_by_name_const( |
| 741 | src_comp, "out"); |
| 742 | BT_ASSERT(upstream_port); |
| 743 | downstream_port = bt_component_sink_borrow_input_port_by_name_const( |
| 744 | sink_comp, "in"); |
| 745 | BT_ASSERT(downstream_port); |
| 746 | graph_status = bt_graph_connect_ports(graph, upstream_port, |
| 747 | downstream_port, NULL); |
| 748 | |
| 749 | /* Run the graph until the end */ |
| 750 | while (graph_status == BT_GRAPH_STATUS_OK || |
| 751 | graph_status == BT_GRAPH_STATUS_AGAIN) { |
| 752 | graph_status = bt_graph_run(graph); |
| 753 | } |
| 754 | |
| 755 | ok(graph_status == BT_GRAPH_STATUS_END, |
| 756 | "graph finishes without any error"); |
| 757 | |
| 758 | /* Compare the resulting test events */ |
| 759 | if (compare_func) { |
| 760 | compare_func(); |
| 761 | } |
| 762 | |
| 763 | bt_component_source_put_ref(src_comp); |
| 764 | bt_component_sink_put_ref(sink_comp); |
| 765 | fini_static_data(); |
| 766 | BT_GRAPH_PUT_REF_AND_RESET(graph); |
| 767 | } |
| 768 | |
| 769 | static |
| 770 | void test_no_auto_msgs_compare(void) |
| 771 | { |
| 772 | const struct test_event expected_test_events[] = { |
| 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, }, |
| 789 | { .type = TEST_EV_TYPE_END, }, |
| 790 | { .type = TEST_EV_TYPE_SENTINEL, }, |
| 791 | }; |
| 792 | |
| 793 | ok(compare_test_events(expected_test_events), |
| 794 | "the produced sequence of test events is the expected one"); |
| 795 | } |
| 796 | |
| 797 | static |
| 798 | void test_no_auto_msgs(void) |
| 799 | { |
| 800 | do_std_test(TEST_NO_AUTO_MSGS, "no automatic messages", |
| 801 | test_no_auto_msgs_compare); |
| 802 | } |
| 803 | |
| 804 | static |
| 805 | void test_output_port_message_iterator(void) |
| 806 | { |
| 807 | const bt_component_source *src_comp; |
| 808 | bt_port_output_message_iterator *msg_iter; |
| 809 | bt_message_iterator_status iter_status = |
| 810 | BT_MESSAGE_ITERATOR_STATUS_OK; |
| 811 | const bt_port_output *upstream_port; |
| 812 | |
| 813 | clear_test_events(); |
| 814 | current_test = TEST_OUTPUT_PORT_MESSAGE_ITERATOR; |
| 815 | diag("test: output port message iterator"); |
| 816 | BT_ASSERT(!graph); |
| 817 | graph = bt_graph_create(); |
| 818 | BT_ASSERT(graph); |
| 819 | create_source_sink(graph, &src_comp, NULL); |
| 820 | |
| 821 | /* Create message iterator on source's output port */ |
| 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); |
| 825 | ok(msg_iter, "bt_private_output_port_message_iterator_create() succeeds"); |
| 826 | |
| 827 | /* Consume the message iterator */ |
| 828 | while (iter_status == BT_MESSAGE_ITERATOR_STATUS_OK) { |
| 829 | iter_status = common_consume(msg_iter, true); |
| 830 | } |
| 831 | |
| 832 | ok(iter_status == BT_MESSAGE_ITERATOR_STATUS_END, |
| 833 | "output port message iterator finishes without any error"); |
| 834 | |
| 835 | /* Compare the resulting test events */ |
| 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 | } |
| 861 | |
| 862 | fini_static_data(); |
| 863 | bt_component_source_put_ref(src_comp); |
| 864 | BT_GRAPH_PUT_REF_AND_RESET(graph); |
| 865 | bt_port_output_message_iterator_put_ref(msg_iter); |
| 866 | } |
| 867 | |
| 868 | #define DEBUG_ENV_VAR "TEST_BT_MESSAGE_ITERATOR_DEBUG" |
| 869 | |
| 870 | int 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); |
| 877 | test_events = g_array_new(FALSE, TRUE, sizeof(struct test_event)); |
| 878 | BT_ASSERT(test_events); |
| 879 | test_no_auto_msgs(); |
| 880 | test_output_port_message_iterator(); |
| 881 | g_array_free(test_events, TRUE); |
| 882 | return exit_status(); |
| 883 | } |