| 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_NOTIFS, |
| 34 | TEST_OUTPUT_PORT_NOTIFICATION_ITERATOR, |
| 35 | }; |
| 36 | |
| 37 | enum test_event_type { |
| 38 | TEST_EV_TYPE_NOTIF_UNEXPECTED, |
| 39 | TEST_EV_TYPE_NOTIF_EVENT, |
| 40 | TEST_EV_TYPE_NOTIF_STREAM_BEGIN, |
| 41 | TEST_EV_TYPE_NOTIF_PACKET_BEGIN, |
| 42 | TEST_EV_TYPE_NOTIF_PACKET_END, |
| 43 | TEST_EV_TYPE_NOTIF_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 struct bt_stream *stream; |
| 51 | const struct bt_packet *packet; |
| 52 | }; |
| 53 | |
| 54 | static bool debug = false; |
| 55 | static enum test current_test; |
| 56 | static GArray *test_events; |
| 57 | static struct bt_graph *graph; |
| 58 | static struct bt_stream_class *src_stream_class; |
| 59 | static struct bt_event_class *src_event_class; |
| 60 | static struct bt_stream *src_stream1; |
| 61 | static struct bt_stream *src_stream2; |
| 62 | static struct bt_packet *src_stream1_packet1; |
| 63 | static struct bt_packet *src_stream1_packet2; |
| 64 | static struct bt_packet *src_stream2_packet1; |
| 65 | static struct 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 | void *notif_iter; |
| 94 | }; |
| 95 | |
| 96 | /* |
| 97 | * No automatic notifications generated in this block. |
| 98 | * Stream 2 notifications are more indented. |
| 99 | */ |
| 100 | static int64_t seq_no_auto_notifs[] = { |
| 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_NOTIF_UNEXPECTED: |
| 133 | fprintf(fp, "TEST_EV_TYPE_NOTIF_UNEXPECTED"); |
| 134 | break; |
| 135 | case TEST_EV_TYPE_NOTIF_EVENT: |
| 136 | fprintf(fp, "TEST_EV_TYPE_NOTIF_EVENT"); |
| 137 | break; |
| 138 | case TEST_EV_TYPE_NOTIF_STREAM_BEGIN: |
| 139 | fprintf(fp, "TEST_EV_TYPE_NOTIF_STREAM_BEGIN"); |
| 140 | break; |
| 141 | case TEST_EV_TYPE_NOTIF_STREAM_END: |
| 142 | fprintf(fp, "TEST_EV_TYPE_NOTIF_STREAM_END"); |
| 143 | break; |
| 144 | case TEST_EV_TYPE_NOTIF_PACKET_BEGIN: |
| 145 | fprintf(fp, "TEST_EV_TYPE_NOTIF_PACKET_BEGIN"); |
| 146 | break; |
| 147 | case TEST_EV_TYPE_NOTIF_PACKET_END: |
| 148 | fprintf(fp, "TEST_EV_TYPE_NOTIF_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(void) |
| 243 | { |
| 244 | struct bt_trace_class *trace_class; |
| 245 | struct bt_trace *trace; |
| 246 | |
| 247 | /* Test events */ |
| 248 | test_events = g_array_new(FALSE, TRUE, sizeof(struct test_event)); |
| 249 | BT_ASSERT(test_events); |
| 250 | |
| 251 | /* Metadata, streams, and packets*/ |
| 252 | trace_class = bt_trace_class_create(); |
| 253 | BT_ASSERT(trace); |
| 254 | src_stream_class = bt_stream_class_create(trace_class); |
| 255 | BT_ASSERT(src_stream_class); |
| 256 | src_event_class = bt_event_class_create(src_stream_class); |
| 257 | BT_ASSERT(src_event_class); |
| 258 | trace = bt_trace_create(trace_class); |
| 259 | BT_ASSERT(trace); |
| 260 | src_stream1 = bt_stream_create(src_stream_class, trace); |
| 261 | BT_ASSERT(src_stream1); |
| 262 | src_stream2 = bt_stream_create(src_stream_class, trace); |
| 263 | BT_ASSERT(src_stream2); |
| 264 | src_stream1_packet1 = bt_packet_create(src_stream1); |
| 265 | BT_ASSERT(src_stream1_packet1); |
| 266 | src_stream1_packet2 = bt_packet_create(src_stream1); |
| 267 | BT_ASSERT(src_stream1_packet2); |
| 268 | src_stream2_packet1 = bt_packet_create(src_stream2); |
| 269 | BT_ASSERT(src_stream2_packet1); |
| 270 | src_stream2_packet2 = bt_packet_create(src_stream2); |
| 271 | BT_ASSERT(src_stream2_packet2); |
| 272 | |
| 273 | if (debug) { |
| 274 | fprintf(stderr, ":: stream 1: %p\n", src_stream1); |
| 275 | fprintf(stderr, ":: stream 2: %p\n", src_stream2); |
| 276 | fprintf(stderr, ":: stream 1, packet 1: %p\n", src_stream1_packet1); |
| 277 | fprintf(stderr, ":: stream 1, packet 2: %p\n", src_stream1_packet2); |
| 278 | fprintf(stderr, ":: stream 2, packet 1: %p\n", src_stream2_packet1); |
| 279 | fprintf(stderr, ":: stream 2, packet 2: %p\n", src_stream2_packet2); |
| 280 | } |
| 281 | |
| 282 | bt_object_put_ref(trace); |
| 283 | bt_object_put_ref(trace_class); |
| 284 | } |
| 285 | |
| 286 | static |
| 287 | void fini_static_data(void) |
| 288 | { |
| 289 | /* Test events */ |
| 290 | g_array_free(test_events, TRUE); |
| 291 | |
| 292 | /* Metadata */ |
| 293 | bt_object_put_ref(src_stream_class); |
| 294 | bt_object_put_ref(src_event_class); |
| 295 | bt_object_put_ref(src_stream1); |
| 296 | bt_object_put_ref(src_stream2); |
| 297 | bt_object_put_ref(src_stream1_packet1); |
| 298 | bt_object_put_ref(src_stream1_packet2); |
| 299 | bt_object_put_ref(src_stream2_packet1); |
| 300 | bt_object_put_ref(src_stream2_packet2); |
| 301 | } |
| 302 | |
| 303 | static |
| 304 | void src_iter_finalize(struct bt_self_notification_iterator *self_notif_iter) |
| 305 | { |
| 306 | struct src_iter_user_data *user_data = |
| 307 | bt_self_notification_iterator_get_data( |
| 308 | self_notif_iter); |
| 309 | |
| 310 | if (user_data) { |
| 311 | g_free(user_data); |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | static |
| 316 | enum bt_self_notification_iterator_status src_iter_init( |
| 317 | struct bt_self_notification_iterator *self_notif_iter, |
| 318 | struct bt_self_component_source *self_comp, |
| 319 | struct bt_self_component_port_output *self_port) |
| 320 | { |
| 321 | struct src_iter_user_data *user_data = |
| 322 | g_new0(struct src_iter_user_data, 1); |
| 323 | |
| 324 | BT_ASSERT(user_data); |
| 325 | bt_self_notification_iterator_set_data(self_notif_iter, user_data); |
| 326 | |
| 327 | switch (current_test) { |
| 328 | case TEST_NO_AUTO_NOTIFS: |
| 329 | case TEST_OUTPUT_PORT_NOTIFICATION_ITERATOR: |
| 330 | user_data->seq = seq_no_auto_notifs; |
| 331 | break; |
| 332 | default: |
| 333 | abort(); |
| 334 | } |
| 335 | |
| 336 | return BT_SELF_NOTIFICATION_ITERATOR_STATUS_OK; |
| 337 | } |
| 338 | |
| 339 | static |
| 340 | void src_iter_next_seq_one(struct bt_self_notification_iterator* notif_iter, |
| 341 | struct src_iter_user_data *user_data, |
| 342 | const struct bt_notification **notif) |
| 343 | { |
| 344 | struct bt_packet *event_packet = NULL; |
| 345 | |
| 346 | switch (user_data->seq[user_data->at]) { |
| 347 | case SEQ_STREAM1_BEGIN: |
| 348 | *notif = bt_notification_stream_beginning_create(notif_iter, |
| 349 | src_stream1); |
| 350 | break; |
| 351 | case SEQ_STREAM2_BEGIN: |
| 352 | *notif = bt_notification_stream_beginning_create(notif_iter, |
| 353 | src_stream2); |
| 354 | break; |
| 355 | case SEQ_STREAM1_END: |
| 356 | *notif = bt_notification_stream_end_create(notif_iter, |
| 357 | src_stream1); |
| 358 | break; |
| 359 | case SEQ_STREAM2_END: |
| 360 | *notif = bt_notification_stream_end_create(notif_iter, |
| 361 | src_stream2); |
| 362 | break; |
| 363 | case SEQ_STREAM1_PACKET1_BEGIN: |
| 364 | *notif = bt_notification_packet_beginning_create(notif_iter, |
| 365 | src_stream1_packet1); |
| 366 | break; |
| 367 | case SEQ_STREAM1_PACKET2_BEGIN: |
| 368 | *notif = bt_notification_packet_beginning_create(notif_iter, |
| 369 | src_stream1_packet2); |
| 370 | break; |
| 371 | case SEQ_STREAM2_PACKET1_BEGIN: |
| 372 | *notif = bt_notification_packet_beginning_create(notif_iter, |
| 373 | src_stream2_packet1); |
| 374 | break; |
| 375 | case SEQ_STREAM2_PACKET2_BEGIN: |
| 376 | *notif = bt_notification_packet_beginning_create(notif_iter, |
| 377 | src_stream2_packet2); |
| 378 | break; |
| 379 | case SEQ_STREAM1_PACKET1_END: |
| 380 | *notif = bt_notification_packet_end_create(notif_iter, |
| 381 | src_stream1_packet1); |
| 382 | break; |
| 383 | case SEQ_STREAM1_PACKET2_END: |
| 384 | *notif = bt_notification_packet_end_create(notif_iter, |
| 385 | src_stream1_packet2); |
| 386 | break; |
| 387 | case SEQ_STREAM2_PACKET1_END: |
| 388 | *notif = bt_notification_packet_end_create(notif_iter, |
| 389 | src_stream2_packet1); |
| 390 | break; |
| 391 | case SEQ_STREAM2_PACKET2_END: |
| 392 | *notif = bt_notification_packet_end_create(notif_iter, |
| 393 | src_stream2_packet2); |
| 394 | break; |
| 395 | case SEQ_EVENT_STREAM1_PACKET1: |
| 396 | event_packet = src_stream1_packet1; |
| 397 | break; |
| 398 | case SEQ_EVENT_STREAM1_PACKET2: |
| 399 | event_packet = src_stream1_packet2; |
| 400 | break; |
| 401 | case SEQ_EVENT_STREAM2_PACKET1: |
| 402 | event_packet = src_stream2_packet1; |
| 403 | break; |
| 404 | case SEQ_EVENT_STREAM2_PACKET2: |
| 405 | event_packet = src_stream2_packet2; |
| 406 | break; |
| 407 | default: |
| 408 | abort(); |
| 409 | } |
| 410 | |
| 411 | if (event_packet) { |
| 412 | *notif = bt_notification_event_create(notif_iter, |
| 413 | src_event_class, |
| 414 | event_packet); |
| 415 | } |
| 416 | |
| 417 | BT_ASSERT(*notif); |
| 418 | user_data->at++; |
| 419 | } |
| 420 | |
| 421 | static |
| 422 | enum bt_self_notification_iterator_status src_iter_next_seq( |
| 423 | struct bt_self_notification_iterator *notif_iter, |
| 424 | struct src_iter_user_data *user_data, |
| 425 | bt_notification_array_const notifs, uint64_t capacity, |
| 426 | uint64_t *count) |
| 427 | { |
| 428 | enum bt_self_notification_iterator_status status = |
| 429 | BT_SELF_NOTIFICATION_ITERATOR_STATUS_OK; |
| 430 | uint64_t i = 0; |
| 431 | |
| 432 | BT_ASSERT(user_data->seq); |
| 433 | |
| 434 | if (user_data->seq[user_data->at] == SEQ_END) { |
| 435 | status = BT_SELF_NOTIFICATION_ITERATOR_STATUS_END; |
| 436 | goto end; |
| 437 | } |
| 438 | |
| 439 | while (i < capacity && user_data->seq[user_data->at] != SEQ_END) { |
| 440 | src_iter_next_seq_one(notif_iter, user_data, ¬ifs[i]); |
| 441 | i++; |
| 442 | } |
| 443 | |
| 444 | BT_ASSERT(i > 0 && i <= capacity); |
| 445 | *count = i; |
| 446 | |
| 447 | end: |
| 448 | return status; |
| 449 | } |
| 450 | |
| 451 | static |
| 452 | enum bt_self_notification_iterator_status src_iter_next( |
| 453 | struct bt_self_notification_iterator *self_notif_iter, |
| 454 | bt_notification_array_const notifs, uint64_t capacity, |
| 455 | uint64_t *count) |
| 456 | { |
| 457 | struct src_iter_user_data *user_data = |
| 458 | bt_self_notification_iterator_get_data(self_notif_iter); |
| 459 | |
| 460 | BT_ASSERT(user_data); |
| 461 | return src_iter_next_seq(self_notif_iter, user_data, notifs, |
| 462 | capacity, count); |
| 463 | } |
| 464 | |
| 465 | static |
| 466 | enum bt_self_component_status src_init( |
| 467 | struct bt_self_component_source *self_comp, |
| 468 | const struct bt_value *params, void *init_method_data) |
| 469 | { |
| 470 | int ret; |
| 471 | |
| 472 | ret = bt_self_component_source_add_output_port( |
| 473 | self_comp, "out", NULL, NULL); |
| 474 | BT_ASSERT(ret == 0); |
| 475 | return BT_SELF_COMPONENT_STATUS_OK; |
| 476 | } |
| 477 | |
| 478 | static |
| 479 | void src_finalize(struct bt_self_component_source *self_comp) |
| 480 | { |
| 481 | } |
| 482 | |
| 483 | static |
| 484 | void append_test_events_from_notification(const struct bt_notification *notification) |
| 485 | { |
| 486 | struct test_event test_event = { 0 }; |
| 487 | |
| 488 | switch (bt_notification_get_type(notification)) { |
| 489 | case BT_NOTIFICATION_TYPE_EVENT: |
| 490 | { |
| 491 | const struct bt_event *event; |
| 492 | |
| 493 | test_event.type = TEST_EV_TYPE_NOTIF_EVENT; |
| 494 | event = bt_notification_event_borrow_event_const(notification); |
| 495 | BT_ASSERT(event); |
| 496 | test_event.packet = bt_event_borrow_packet_const(event); |
| 497 | BT_ASSERT(test_event.packet); |
| 498 | break; |
| 499 | } |
| 500 | case BT_NOTIFICATION_TYPE_STREAM_BEGINNING: |
| 501 | test_event.type = TEST_EV_TYPE_NOTIF_STREAM_BEGIN; |
| 502 | test_event.stream = |
| 503 | bt_notification_stream_beginning_borrow_stream_const(notification); |
| 504 | BT_ASSERT(test_event.stream); |
| 505 | break; |
| 506 | case BT_NOTIFICATION_TYPE_STREAM_END: |
| 507 | test_event.type = TEST_EV_TYPE_NOTIF_STREAM_END; |
| 508 | test_event.stream = |
| 509 | bt_notification_stream_end_borrow_stream_const(notification); |
| 510 | BT_ASSERT(test_event.stream); |
| 511 | break; |
| 512 | case BT_NOTIFICATION_TYPE_PACKET_BEGINNING: |
| 513 | test_event.type = TEST_EV_TYPE_NOTIF_PACKET_BEGIN; |
| 514 | test_event.packet = |
| 515 | bt_notification_packet_beginning_borrow_packet_const(notification); |
| 516 | BT_ASSERT(test_event.packet); |
| 517 | break; |
| 518 | case BT_NOTIFICATION_TYPE_PACKET_END: |
| 519 | test_event.type = TEST_EV_TYPE_NOTIF_PACKET_END; |
| 520 | test_event.packet = |
| 521 | bt_notification_packet_end_borrow_packet_const(notification); |
| 522 | BT_ASSERT(test_event.packet); |
| 523 | break; |
| 524 | default: |
| 525 | test_event.type = TEST_EV_TYPE_NOTIF_UNEXPECTED; |
| 526 | break; |
| 527 | } |
| 528 | |
| 529 | if (test_event.packet) { |
| 530 | test_event.stream = bt_packet_borrow_stream_const( |
| 531 | test_event.packet); |
| 532 | BT_ASSERT(test_event.stream); |
| 533 | } |
| 534 | |
| 535 | append_test_event(&test_event); |
| 536 | } |
| 537 | |
| 538 | static |
| 539 | enum bt_notification_iterator_status common_consume( |
| 540 | void *notif_iter, bool is_output_port_notif_iter) |
| 541 | { |
| 542 | enum bt_notification_iterator_status ret; |
| 543 | bt_notification_array_const notifications = NULL; |
| 544 | uint64_t count = 0; |
| 545 | struct test_event test_event = { 0 }; |
| 546 | uint64_t i; |
| 547 | |
| 548 | BT_ASSERT(notif_iter); |
| 549 | |
| 550 | if (is_output_port_notif_iter) { |
| 551 | ret = bt_port_output_notification_iterator_next(notif_iter, |
| 552 | ¬ifications, &count); |
| 553 | } else { |
| 554 | ret = bt_self_component_port_input_notification_iterator_next( |
| 555 | notif_iter, ¬ifications, &count); |
| 556 | } |
| 557 | |
| 558 | if (ret < 0) { |
| 559 | goto end; |
| 560 | } |
| 561 | |
| 562 | switch (ret) { |
| 563 | case BT_NOTIFICATION_ITERATOR_STATUS_END: |
| 564 | test_event.type = TEST_EV_TYPE_END; |
| 565 | append_test_event(&test_event); |
| 566 | goto end; |
| 567 | case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN: |
| 568 | abort(); |
| 569 | default: |
| 570 | break; |
| 571 | } |
| 572 | |
| 573 | BT_ASSERT(notifications); |
| 574 | BT_ASSERT(count > 0); |
| 575 | |
| 576 | for (i = 0; i < count; i++) { |
| 577 | append_test_events_from_notification(notifications[i]); |
| 578 | bt_object_put_ref(notifications[i]); |
| 579 | } |
| 580 | |
| 581 | end: |
| 582 | return ret; |
| 583 | } |
| 584 | |
| 585 | static |
| 586 | enum bt_self_component_status sink_consume( |
| 587 | struct bt_self_component_sink *self_comp) |
| 588 | { |
| 589 | enum bt_self_component_status ret = BT_SELF_COMPONENT_STATUS_OK; |
| 590 | struct sink_user_data *user_data = |
| 591 | bt_self_component_get_data( |
| 592 | bt_self_component_sink_as_self_component( |
| 593 | self_comp)); |
| 594 | enum bt_notification_iterator_status it_ret; |
| 595 | |
| 596 | BT_ASSERT(user_data && user_data->notif_iter); |
| 597 | it_ret = common_consume(user_data->notif_iter, false); |
| 598 | |
| 599 | if (it_ret < 0) { |
| 600 | ret = BT_SELF_COMPONENT_STATUS_ERROR; |
| 601 | goto end; |
| 602 | } |
| 603 | |
| 604 | switch (it_ret) { |
| 605 | case BT_NOTIFICATION_ITERATOR_STATUS_END: |
| 606 | ret = BT_SELF_COMPONENT_STATUS_END; |
| 607 | BT_OBJECT_PUT_REF_AND_RESET(user_data->notif_iter); |
| 608 | goto end; |
| 609 | case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN: |
| 610 | abort(); |
| 611 | default: |
| 612 | break; |
| 613 | } |
| 614 | |
| 615 | end: |
| 616 | return ret; |
| 617 | } |
| 618 | |
| 619 | static |
| 620 | enum bt_self_component_status sink_port_connected( |
| 621 | struct bt_self_component_sink *self_comp, |
| 622 | struct bt_self_component_port_input *self_port, |
| 623 | const struct bt_port_output *other_port) |
| 624 | { |
| 625 | struct sink_user_data *user_data = |
| 626 | bt_self_component_get_data( |
| 627 | bt_self_component_sink_as_self_component( |
| 628 | self_comp)); |
| 629 | |
| 630 | BT_ASSERT(user_data); |
| 631 | user_data->notif_iter = |
| 632 | bt_self_component_port_input_notification_iterator_create( |
| 633 | self_port); |
| 634 | return BT_SELF_COMPONENT_STATUS_OK; |
| 635 | } |
| 636 | |
| 637 | static |
| 638 | enum bt_self_component_status sink_init( |
| 639 | struct bt_self_component_sink *self_comp, |
| 640 | const struct bt_value *params, void *init_method_data) |
| 641 | { |
| 642 | struct sink_user_data *user_data = g_new0(struct sink_user_data, 1); |
| 643 | int ret; |
| 644 | |
| 645 | BT_ASSERT(user_data); |
| 646 | bt_self_component_set_data( |
| 647 | bt_self_component_sink_as_self_component(self_comp), |
| 648 | user_data); |
| 649 | ret = bt_self_component_sink_add_input_port( |
| 650 | self_comp, "in", NULL, NULL); |
| 651 | BT_ASSERT(ret == 0); |
| 652 | return BT_SELF_COMPONENT_STATUS_OK; |
| 653 | } |
| 654 | |
| 655 | static |
| 656 | void sink_finalize(struct bt_self_component_sink *self_comp) |
| 657 | { |
| 658 | struct sink_user_data *user_data = |
| 659 | bt_self_component_get_data( |
| 660 | bt_self_component_sink_as_self_component( |
| 661 | self_comp)); |
| 662 | |
| 663 | if (user_data) { |
| 664 | bt_object_put_ref(user_data->notif_iter); |
| 665 | g_free(user_data); |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | static |
| 670 | void create_source_sink(struct bt_graph *graph, |
| 671 | const struct bt_component_source **source, |
| 672 | const struct bt_component_sink **sink) |
| 673 | { |
| 674 | struct bt_component_class_source *src_comp_class; |
| 675 | struct bt_component_class_sink *sink_comp_class; |
| 676 | int ret; |
| 677 | |
| 678 | /* Create source component */ |
| 679 | if (source) { |
| 680 | src_comp_class = bt_component_class_source_create("src", |
| 681 | src_iter_next); |
| 682 | BT_ASSERT(src_comp_class); |
| 683 | ret = bt_component_class_source_set_init_method( |
| 684 | src_comp_class, src_init); |
| 685 | BT_ASSERT(ret == 0); |
| 686 | ret = bt_component_class_source_set_finalize_method( |
| 687 | src_comp_class, src_finalize); |
| 688 | BT_ASSERT(ret == 0); |
| 689 | ret = bt_component_class_source_set_notification_iterator_init_method( |
| 690 | src_comp_class, src_iter_init); |
| 691 | BT_ASSERT(ret == 0); |
| 692 | ret = bt_component_class_source_set_notification_iterator_finalize_method( |
| 693 | src_comp_class, src_iter_finalize); |
| 694 | BT_ASSERT(ret == 0); |
| 695 | ret = bt_graph_add_source_component(graph, |
| 696 | src_comp_class, "source", NULL, source); |
| 697 | BT_ASSERT(ret == 0); |
| 698 | bt_object_put_ref(src_comp_class); |
| 699 | } |
| 700 | |
| 701 | /* Create sink component */ |
| 702 | if (sink) { |
| 703 | sink_comp_class = bt_component_class_sink_create("sink", |
| 704 | sink_consume); |
| 705 | BT_ASSERT(sink_comp_class); |
| 706 | ret = bt_component_class_sink_set_init_method( |
| 707 | sink_comp_class, sink_init); |
| 708 | BT_ASSERT(ret == 0); |
| 709 | ret = bt_component_class_sink_set_finalize_method( |
| 710 | sink_comp_class, sink_finalize); |
| 711 | ret = bt_component_class_sink_set_input_port_connected_method( |
| 712 | sink_comp_class, sink_port_connected); |
| 713 | BT_ASSERT(ret == 0); |
| 714 | ret = bt_graph_add_sink_component(graph, |
| 715 | sink_comp_class, |
| 716 | "sink", NULL, sink); |
| 717 | BT_ASSERT(ret == 0); |
| 718 | bt_object_put_ref(sink_comp_class); |
| 719 | } |
| 720 | } |
| 721 | |
| 722 | static |
| 723 | void do_std_test(enum test test, const char *name, |
| 724 | const struct test_event *expected_test_events) |
| 725 | { |
| 726 | const struct bt_component_source *src_comp; |
| 727 | const struct bt_component_sink *sink_comp; |
| 728 | const struct bt_port_output *upstream_port; |
| 729 | const struct bt_port_input *downstream_port; |
| 730 | enum bt_graph_status graph_status = BT_GRAPH_STATUS_OK; |
| 731 | |
| 732 | clear_test_events(); |
| 733 | current_test = test; |
| 734 | diag("test: %s", name); |
| 735 | BT_ASSERT(!graph); |
| 736 | graph = bt_graph_create(); |
| 737 | BT_ASSERT(graph); |
| 738 | create_source_sink(graph, &src_comp, &sink_comp); |
| 739 | |
| 740 | /* Connect source to sink */ |
| 741 | upstream_port = |
| 742 | bt_component_source_borrow_output_port_by_name_const( |
| 743 | src_comp, "out"); |
| 744 | BT_ASSERT(upstream_port); |
| 745 | downstream_port = bt_component_sink_borrow_input_port_by_name_const( |
| 746 | sink_comp, "in"); |
| 747 | BT_ASSERT(downstream_port); |
| 748 | graph_status = bt_graph_connect_ports(graph, upstream_port, |
| 749 | downstream_port, NULL); |
| 750 | |
| 751 | /* Run the graph until the end */ |
| 752 | while (graph_status == BT_GRAPH_STATUS_OK || |
| 753 | graph_status == BT_GRAPH_STATUS_AGAIN) { |
| 754 | graph_status = bt_graph_run(graph); |
| 755 | } |
| 756 | |
| 757 | ok(graph_status == BT_GRAPH_STATUS_END, |
| 758 | "graph finishes without any error"); |
| 759 | |
| 760 | /* Compare the resulting test events */ |
| 761 | if (expected_test_events) { |
| 762 | ok(compare_test_events(expected_test_events), |
| 763 | "the produced sequence of test events is the expected one"); |
| 764 | } |
| 765 | |
| 766 | bt_object_put_ref(src_comp); |
| 767 | bt_object_put_ref(sink_comp); |
| 768 | BT_OBJECT_PUT_REF_AND_RESET(graph); |
| 769 | } |
| 770 | |
| 771 | static |
| 772 | void test_no_auto_notifs(void) |
| 773 | { |
| 774 | const struct test_event expected_test_events[] = { |
| 775 | { .type = TEST_EV_TYPE_NOTIF_STREAM_BEGIN, .stream = src_stream1, .packet = NULL, }, |
| 776 | { .type = TEST_EV_TYPE_NOTIF_PACKET_BEGIN, .stream = src_stream1, .packet = src_stream1_packet1, }, |
| 777 | { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = src_stream1, .packet = src_stream1_packet1, }, |
| 778 | { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = src_stream1, .packet = src_stream1_packet1, }, |
| 779 | { .type = TEST_EV_TYPE_NOTIF_STREAM_BEGIN, .stream = src_stream2, .packet = NULL, }, |
| 780 | { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = src_stream1, .packet = src_stream1_packet1, }, |
| 781 | { .type = TEST_EV_TYPE_NOTIF_PACKET_BEGIN, .stream = src_stream2, .packet = src_stream2_packet2, }, |
| 782 | { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = src_stream2, .packet = src_stream2_packet2, }, |
| 783 | { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = src_stream1, .packet = src_stream1_packet1, }, |
| 784 | { .type = TEST_EV_TYPE_NOTIF_PACKET_END, .stream = src_stream1, .packet = src_stream1_packet1, }, |
| 785 | { .type = TEST_EV_TYPE_NOTIF_PACKET_END, .stream = src_stream2, .packet = src_stream2_packet2, }, |
| 786 | { .type = TEST_EV_TYPE_NOTIF_PACKET_BEGIN, .stream = src_stream1, .packet = src_stream1_packet2, }, |
| 787 | { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = src_stream1, .packet = src_stream1_packet2, }, |
| 788 | { .type = TEST_EV_TYPE_NOTIF_STREAM_END, .stream = src_stream2, .packet = NULL, }, |
| 789 | { .type = TEST_EV_TYPE_NOTIF_PACKET_END, .stream = src_stream1, .packet = src_stream1_packet2, }, |
| 790 | { .type = TEST_EV_TYPE_NOTIF_STREAM_END, .stream = src_stream1, .packet = NULL, }, |
| 791 | { .type = TEST_EV_TYPE_END, }, |
| 792 | { .type = TEST_EV_TYPE_SENTINEL, }, |
| 793 | }; |
| 794 | |
| 795 | do_std_test(TEST_NO_AUTO_NOTIFS, "no automatic notifications", |
| 796 | expected_test_events); |
| 797 | } |
| 798 | |
| 799 | static |
| 800 | void test_output_port_notification_iterator(void) |
| 801 | { |
| 802 | const struct test_event expected_test_events[] = { |
| 803 | { .type = TEST_EV_TYPE_NOTIF_STREAM_BEGIN, .stream = src_stream1, .packet = NULL, }, |
| 804 | { .type = TEST_EV_TYPE_NOTIF_PACKET_BEGIN, .stream = src_stream1, .packet = src_stream1_packet1, }, |
| 805 | { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = src_stream1, .packet = src_stream1_packet1, }, |
| 806 | { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = src_stream1, .packet = src_stream1_packet1, }, |
| 807 | { .type = TEST_EV_TYPE_NOTIF_STREAM_BEGIN, .stream = src_stream2, .packet = NULL, }, |
| 808 | { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = src_stream1, .packet = src_stream1_packet1, }, |
| 809 | { .type = TEST_EV_TYPE_NOTIF_PACKET_BEGIN, .stream = src_stream2, .packet = src_stream2_packet2, }, |
| 810 | { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = src_stream2, .packet = src_stream2_packet2, }, |
| 811 | { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = src_stream1, .packet = src_stream1_packet1, }, |
| 812 | { .type = TEST_EV_TYPE_NOTIF_PACKET_END, .stream = src_stream1, .packet = src_stream1_packet1, }, |
| 813 | { .type = TEST_EV_TYPE_NOTIF_PACKET_END, .stream = src_stream2, .packet = src_stream2_packet2, }, |
| 814 | { .type = TEST_EV_TYPE_NOTIF_PACKET_BEGIN, .stream = src_stream1, .packet = src_stream1_packet2, }, |
| 815 | { .type = TEST_EV_TYPE_NOTIF_EVENT, .stream = src_stream1, .packet = src_stream1_packet2, }, |
| 816 | { .type = TEST_EV_TYPE_NOTIF_STREAM_END, .stream = src_stream2, .packet = NULL, }, |
| 817 | { .type = TEST_EV_TYPE_NOTIF_PACKET_END, .stream = src_stream1, .packet = src_stream1_packet2, }, |
| 818 | { .type = TEST_EV_TYPE_NOTIF_STREAM_END, .stream = src_stream1, .packet = NULL, }, |
| 819 | { .type = TEST_EV_TYPE_END, }, |
| 820 | { .type = TEST_EV_TYPE_SENTINEL, }, |
| 821 | }; |
| 822 | const struct bt_component_source *src_comp; |
| 823 | struct bt_port_output_notification_iterator *notif_iter; |
| 824 | enum bt_notification_iterator_status iter_status = |
| 825 | BT_NOTIFICATION_ITERATOR_STATUS_OK; |
| 826 | const struct bt_port_output *upstream_port; |
| 827 | |
| 828 | clear_test_events(); |
| 829 | current_test = TEST_OUTPUT_PORT_NOTIFICATION_ITERATOR; |
| 830 | diag("test: output port notification iterator"); |
| 831 | BT_ASSERT(!graph); |
| 832 | graph = bt_graph_create(); |
| 833 | BT_ASSERT(graph); |
| 834 | create_source_sink(graph, &src_comp, NULL); |
| 835 | |
| 836 | /* Create notification iterator on source's output port */ |
| 837 | upstream_port = bt_component_source_borrow_output_port_by_name_const(src_comp, |
| 838 | "out"); |
| 839 | notif_iter = bt_port_output_notification_iterator_create(graph, |
| 840 | upstream_port); |
| 841 | ok(notif_iter, "bt_private_output_port_notification_iterator_create() succeeds"); |
| 842 | |
| 843 | /* Consume the notification iterator */ |
| 844 | while (iter_status == BT_NOTIFICATION_ITERATOR_STATUS_OK) { |
| 845 | iter_status = common_consume(notif_iter, true); |
| 846 | } |
| 847 | |
| 848 | ok(iter_status == BT_NOTIFICATION_ITERATOR_STATUS_END, |
| 849 | "output port notification iterator finishes without any error"); |
| 850 | |
| 851 | /* Compare the resulting test events */ |
| 852 | ok(compare_test_events(expected_test_events), |
| 853 | "the produced sequence of test events is the expected one"); |
| 854 | |
| 855 | bt_object_put_ref(src_comp); |
| 856 | BT_OBJECT_PUT_REF_AND_RESET(graph); |
| 857 | bt_object_put_ref(notif_iter); |
| 858 | } |
| 859 | |
| 860 | #define DEBUG_ENV_VAR "TEST_BT_NOTIFICATION_ITERATOR_DEBUG" |
| 861 | |
| 862 | int main(int argc, char **argv) |
| 863 | { |
| 864 | if (getenv(DEBUG_ENV_VAR) && strcmp(getenv(DEBUG_ENV_VAR), "1") == 0) { |
| 865 | debug = true; |
| 866 | } |
| 867 | |
| 868 | plan_tests(NR_TESTS); |
| 869 | init_static_data(); |
| 870 | test_no_auto_notifs(); |
| 871 | test_output_port_notification_iterator(); |
| 872 | fini_static_data(); |
| 873 | return exit_status(); |
| 874 | } |