| 1 | /* |
| 2 | * test_graph_topo.c |
| 3 | * |
| 4 | * Copyright 2017 - Philippe Proulx <pproulx@efficios.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; under version 2 of the License. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License along |
| 16 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | */ |
| 19 | |
| 20 | #include <babeltrace2/babeltrace.h> |
| 21 | #include "common/assert.h" |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | #include <stdbool.h> |
| 25 | #include <glib.h> |
| 26 | |
| 27 | #include "tap/tap.h" |
| 28 | |
| 29 | #define NR_TESTS 26 |
| 30 | |
| 31 | enum event_type { |
| 32 | SRC_COMP_OUTPUT_PORT_CONNECTED, |
| 33 | SINK_COMP_INPUT_PORT_CONNECTED, |
| 34 | GRAPH_SRC_OUTPUT_PORT_ADDED, |
| 35 | GRAPH_SINK_INPUT_PORT_ADDED, |
| 36 | }; |
| 37 | |
| 38 | enum test { |
| 39 | TEST_EMPTY_GRAPH, |
| 40 | TEST_SIMPLE, |
| 41 | TEST_SRC_PORT_CONNECTED_ERROR, |
| 42 | TEST_SINK_PORT_CONNECTED_ERROR, |
| 43 | TEST_SRC_ADDS_PORT_IN_PORT_CONNECTED, |
| 44 | }; |
| 45 | |
| 46 | struct event { |
| 47 | enum event_type type; |
| 48 | |
| 49 | union { |
| 50 | struct { |
| 51 | const bt_component *comp; |
| 52 | const bt_port *self_port; |
| 53 | const bt_port *other_port; |
| 54 | } src_comp_output_port_connected; |
| 55 | |
| 56 | struct { |
| 57 | const bt_component *comp; |
| 58 | const bt_port *self_port; |
| 59 | const bt_port *other_port; |
| 60 | } sink_comp_input_port_connected; |
| 61 | |
| 62 | struct { |
| 63 | const bt_component *comp; |
| 64 | const bt_port *port; |
| 65 | } graph_src_output_port_added; |
| 66 | |
| 67 | struct { |
| 68 | const bt_component *comp; |
| 69 | const bt_port *port; |
| 70 | } graph_sink_input_port_added; |
| 71 | } data; |
| 72 | }; |
| 73 | |
| 74 | static GArray *events; |
| 75 | static bt_message_iterator_class *msg_iter_class; |
| 76 | static bt_component_class_source *src_comp_class; |
| 77 | static bt_component_class_sink *sink_comp_class; |
| 78 | static enum test current_test; |
| 79 | |
| 80 | static |
| 81 | void clear_events(void) |
| 82 | { |
| 83 | g_array_set_size(events, 0); |
| 84 | } |
| 85 | |
| 86 | static |
| 87 | void append_event(struct event *event) |
| 88 | { |
| 89 | g_array_append_val(events, *event); |
| 90 | } |
| 91 | |
| 92 | static |
| 93 | bool compare_events(struct event *ev_a, struct event *ev_b) |
| 94 | { |
| 95 | if (ev_a->type != ev_b->type) { |
| 96 | return false; |
| 97 | } |
| 98 | |
| 99 | switch (ev_a->type) { |
| 100 | case SRC_COMP_OUTPUT_PORT_CONNECTED: |
| 101 | if (ev_a->data.src_comp_output_port_connected.comp != |
| 102 | ev_b->data.src_comp_output_port_connected.comp) { |
| 103 | return false; |
| 104 | } |
| 105 | |
| 106 | if (ev_a->data.src_comp_output_port_connected.self_port != |
| 107 | ev_b->data.src_comp_output_port_connected.self_port) { |
| 108 | return false; |
| 109 | } |
| 110 | |
| 111 | if (ev_a->data.src_comp_output_port_connected.other_port != |
| 112 | ev_b->data.src_comp_output_port_connected.other_port) { |
| 113 | return false; |
| 114 | } |
| 115 | break; |
| 116 | case SINK_COMP_INPUT_PORT_CONNECTED: |
| 117 | if (ev_a->data.sink_comp_input_port_connected.comp != |
| 118 | ev_b->data.sink_comp_input_port_connected.comp) { |
| 119 | return false; |
| 120 | } |
| 121 | |
| 122 | if (ev_a->data.sink_comp_input_port_connected.self_port != |
| 123 | ev_b->data.sink_comp_input_port_connected.self_port) { |
| 124 | return false; |
| 125 | } |
| 126 | |
| 127 | if (ev_a->data.sink_comp_input_port_connected.other_port != |
| 128 | ev_b->data.sink_comp_input_port_connected.other_port) { |
| 129 | return false; |
| 130 | } |
| 131 | break; |
| 132 | case GRAPH_SRC_OUTPUT_PORT_ADDED: |
| 133 | if (ev_a->data.graph_src_output_port_added.comp != |
| 134 | ev_b->data.graph_src_output_port_added.comp) { |
| 135 | return false; |
| 136 | } |
| 137 | |
| 138 | if (ev_a->data.graph_src_output_port_added.port != |
| 139 | ev_b->data.graph_src_output_port_added.port) { |
| 140 | return false; |
| 141 | } |
| 142 | break; |
| 143 | case GRAPH_SINK_INPUT_PORT_ADDED: |
| 144 | if (ev_a->data.graph_sink_input_port_added.comp != |
| 145 | ev_b->data.graph_sink_input_port_added.comp) { |
| 146 | return false; |
| 147 | } |
| 148 | |
| 149 | if (ev_a->data.graph_sink_input_port_added.port != |
| 150 | ev_b->data.graph_sink_input_port_added.port) { |
| 151 | return false; |
| 152 | } |
| 153 | break; |
| 154 | default: |
| 155 | abort(); |
| 156 | } |
| 157 | |
| 158 | return true; |
| 159 | } |
| 160 | |
| 161 | static |
| 162 | bool has_event(struct event *event) |
| 163 | { |
| 164 | size_t i; |
| 165 | |
| 166 | for (i = 0; i < events->len; i++) { |
| 167 | struct event *ev = &g_array_index(events, struct event, i); |
| 168 | |
| 169 | if (compare_events(event, ev)) { |
| 170 | return true; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | return false; |
| 175 | } |
| 176 | |
| 177 | static |
| 178 | size_t event_pos(struct event *event) |
| 179 | { |
| 180 | size_t i; |
| 181 | |
| 182 | for (i = 0; i < events->len; i++) { |
| 183 | struct event *ev = &g_array_index(events, struct event, i); |
| 184 | |
| 185 | if (compare_events(event, ev)) { |
| 186 | return i; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | return SIZE_MAX; |
| 191 | } |
| 192 | |
| 193 | static |
| 194 | bt_message_iterator_class_next_method_status src_iter_next( |
| 195 | bt_self_message_iterator *self_iterator, |
| 196 | bt_message_array_const msgs, uint64_t capacity, |
| 197 | uint64_t *count) |
| 198 | { |
| 199 | return BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_ERROR; |
| 200 | } |
| 201 | |
| 202 | static |
| 203 | bt_component_class_port_connected_method_status src_output_port_connected( |
| 204 | bt_self_component_source *self_comp, |
| 205 | bt_self_component_port_output *self_comp_port, |
| 206 | const bt_port_input *other_port) |
| 207 | { |
| 208 | int ret; |
| 209 | struct event event = { |
| 210 | .type = SRC_COMP_OUTPUT_PORT_CONNECTED, |
| 211 | .data.src_comp_output_port_connected = { |
| 212 | .comp = bt_self_component_as_component( |
| 213 | bt_self_component_source_as_self_component( |
| 214 | self_comp)), |
| 215 | .self_port = bt_self_component_port_as_port( |
| 216 | bt_self_component_port_output_as_self_component_port( |
| 217 | self_comp_port)), |
| 218 | .other_port = bt_port_input_as_port_const(other_port), |
| 219 | }, |
| 220 | }; |
| 221 | |
| 222 | append_event(&event); |
| 223 | |
| 224 | switch (current_test) { |
| 225 | case TEST_SRC_ADDS_PORT_IN_PORT_CONNECTED: |
| 226 | ret = bt_self_component_source_add_output_port( |
| 227 | self_comp, "hello", NULL, NULL); |
| 228 | BT_ASSERT(ret == 0); |
| 229 | break; |
| 230 | case TEST_SRC_PORT_CONNECTED_ERROR: |
| 231 | return BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_ERROR; |
| 232 | default: |
| 233 | break; |
| 234 | } |
| 235 | |
| 236 | return BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_OK; |
| 237 | } |
| 238 | |
| 239 | static |
| 240 | bt_component_class_port_connected_method_status sink_input_port_connected( |
| 241 | bt_self_component_sink *self_comp, |
| 242 | bt_self_component_port_input *self_comp_port, |
| 243 | const bt_port_output *other_port) |
| 244 | { |
| 245 | struct event event = { |
| 246 | .type = SINK_COMP_INPUT_PORT_CONNECTED, |
| 247 | .data.sink_comp_input_port_connected = { |
| 248 | .comp = bt_self_component_as_component( |
| 249 | bt_self_component_sink_as_self_component( |
| 250 | self_comp)), |
| 251 | .self_port = bt_self_component_port_as_port( |
| 252 | bt_self_component_port_input_as_self_component_port( |
| 253 | self_comp_port)), |
| 254 | .other_port = bt_port_output_as_port_const(other_port), |
| 255 | }, |
| 256 | }; |
| 257 | |
| 258 | append_event(&event); |
| 259 | |
| 260 | if (current_test == TEST_SINK_PORT_CONNECTED_ERROR) { |
| 261 | return BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_ERROR; |
| 262 | } else { |
| 263 | return BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_OK; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | static |
| 268 | bt_component_class_initialize_method_status src_init( |
| 269 | bt_self_component_source *self_comp, |
| 270 | bt_self_component_source_configuration *config, |
| 271 | const bt_value *params, void *init_method_data) |
| 272 | { |
| 273 | int ret; |
| 274 | |
| 275 | ret = bt_self_component_source_add_output_port( |
| 276 | self_comp, "out", NULL, NULL); |
| 277 | BT_ASSERT(ret == 0); |
| 278 | return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK; |
| 279 | } |
| 280 | |
| 281 | static |
| 282 | bt_component_class_initialize_method_status sink_init( |
| 283 | bt_self_component_sink *self_comp, |
| 284 | bt_self_component_sink_configuration *config, |
| 285 | const bt_value *params, void *init_method_data) |
| 286 | { |
| 287 | int ret; |
| 288 | |
| 289 | ret = bt_self_component_sink_add_input_port(self_comp, |
| 290 | "in", NULL, NULL); |
| 291 | BT_ASSERT(ret == 0); |
| 292 | return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK; |
| 293 | } |
| 294 | |
| 295 | static |
| 296 | bt_component_class_sink_consume_method_status sink_consume( |
| 297 | bt_self_component_sink *self_comp) |
| 298 | { |
| 299 | return BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK; |
| 300 | } |
| 301 | |
| 302 | static |
| 303 | bt_graph_listener_func_status graph_src_output_port_added( |
| 304 | const bt_component_source *comp, const bt_port_output *port, |
| 305 | void *data) |
| 306 | { |
| 307 | struct event event = { |
| 308 | .type = GRAPH_SRC_OUTPUT_PORT_ADDED, |
| 309 | .data.graph_src_output_port_added = { |
| 310 | .comp = bt_component_source_as_component_const(comp), |
| 311 | .port = bt_port_output_as_port_const(port), |
| 312 | }, |
| 313 | }; |
| 314 | |
| 315 | append_event(&event); |
| 316 | |
| 317 | return BT_GRAPH_LISTENER_FUNC_STATUS_OK; |
| 318 | } |
| 319 | |
| 320 | static |
| 321 | bt_graph_listener_func_status graph_sink_input_port_added( |
| 322 | const bt_component_sink *comp, const bt_port_input *port, |
| 323 | void *data) |
| 324 | { |
| 325 | struct event event = { |
| 326 | .type = GRAPH_SINK_INPUT_PORT_ADDED, |
| 327 | .data.graph_sink_input_port_added = { |
| 328 | .comp = bt_component_sink_as_component_const(comp), |
| 329 | .port = bt_port_input_as_port_const(port), |
| 330 | }, |
| 331 | }; |
| 332 | |
| 333 | append_event(&event); |
| 334 | |
| 335 | return BT_GRAPH_LISTENER_FUNC_STATUS_OK; |
| 336 | } |
| 337 | |
| 338 | static |
| 339 | void init_test(void) |
| 340 | { |
| 341 | int ret; |
| 342 | |
| 343 | msg_iter_class = bt_message_iterator_class_create(src_iter_next); |
| 344 | BT_ASSERT(msg_iter_class); |
| 345 | |
| 346 | src_comp_class = bt_component_class_source_create( |
| 347 | "src", msg_iter_class); |
| 348 | BT_ASSERT(src_comp_class); |
| 349 | ret = bt_component_class_source_set_initialize_method( |
| 350 | src_comp_class, src_init); |
| 351 | BT_ASSERT(ret == 0); |
| 352 | ret = bt_component_class_source_set_output_port_connected_method( |
| 353 | src_comp_class, src_output_port_connected); |
| 354 | BT_ASSERT(ret == 0); |
| 355 | sink_comp_class = bt_component_class_sink_create("sink", |
| 356 | sink_consume); |
| 357 | BT_ASSERT(sink_comp_class); |
| 358 | ret = bt_component_class_sink_set_initialize_method(sink_comp_class, |
| 359 | sink_init); |
| 360 | BT_ASSERT(ret == 0); |
| 361 | ret = bt_component_class_sink_set_input_port_connected_method( |
| 362 | sink_comp_class, sink_input_port_connected); |
| 363 | BT_ASSERT(ret == 0); |
| 364 | events = g_array_new(FALSE, TRUE, sizeof(struct event)); |
| 365 | BT_ASSERT(events); |
| 366 | } |
| 367 | |
| 368 | static |
| 369 | void fini_test(void) |
| 370 | { |
| 371 | bt_component_class_source_put_ref(src_comp_class); |
| 372 | bt_component_class_sink_put_ref(sink_comp_class); |
| 373 | g_array_free(events, TRUE); |
| 374 | } |
| 375 | |
| 376 | static |
| 377 | const bt_component_source *create_src(bt_graph *graph) |
| 378 | { |
| 379 | const bt_component_source *comp; |
| 380 | int ret; |
| 381 | |
| 382 | ret = bt_graph_add_source_component(graph, src_comp_class, |
| 383 | "src-comp", NULL, BT_LOGGING_LEVEL_NONE, &comp); |
| 384 | BT_ASSERT(ret == 0); |
| 385 | return comp; |
| 386 | } |
| 387 | |
| 388 | static |
| 389 | const bt_component_sink *create_sink(bt_graph *graph) |
| 390 | { |
| 391 | const bt_component_sink *comp; |
| 392 | int ret; |
| 393 | |
| 394 | ret = bt_graph_add_sink_component(graph, sink_comp_class, |
| 395 | "sink-comp", NULL, BT_LOGGING_LEVEL_NONE, &comp); |
| 396 | BT_ASSERT(ret == 0); |
| 397 | return comp; |
| 398 | } |
| 399 | |
| 400 | static |
| 401 | bt_graph *create_graph(void) |
| 402 | { |
| 403 | bt_graph *graph = bt_graph_create(0); |
| 404 | int ret; |
| 405 | |
| 406 | BT_ASSERT(graph); |
| 407 | ret = bt_graph_add_source_component_output_port_added_listener( |
| 408 | graph, graph_src_output_port_added, NULL, NULL); |
| 409 | BT_ASSERT(ret >= 0); |
| 410 | ret = bt_graph_add_sink_component_input_port_added_listener( |
| 411 | graph, graph_sink_input_port_added, NULL, NULL); |
| 412 | BT_ASSERT(ret >= 0); |
| 413 | return graph; |
| 414 | } |
| 415 | |
| 416 | static |
| 417 | void prepare_test(enum test test, const char *name) |
| 418 | { |
| 419 | clear_events(); |
| 420 | current_test = test; |
| 421 | diag("test: %s", name); |
| 422 | } |
| 423 | |
| 424 | static |
| 425 | void test_src_adds_port_in_port_connected(void) |
| 426 | { |
| 427 | const bt_component_source *src; |
| 428 | const bt_component_sink *sink; |
| 429 | const bt_component *gsrc; |
| 430 | const bt_component *gsink; |
| 431 | bt_graph *graph; |
| 432 | const bt_port_output *src_def_port; |
| 433 | const bt_port_output *src_hello_port; |
| 434 | const bt_port_input *sink_def_port; |
| 435 | const bt_port *gsrc_def_port; |
| 436 | const bt_port *gsrc_hello_port; |
| 437 | const bt_port *gsink_def_port; |
| 438 | struct event event; |
| 439 | bt_graph_connect_ports_status status; |
| 440 | size_t src_port_connected_pos; |
| 441 | size_t graph_port_added_src_pos; |
| 442 | |
| 443 | prepare_test(TEST_SRC_ADDS_PORT_IN_PORT_CONNECTED, |
| 444 | "source adds port in port connected"); |
| 445 | graph = create_graph(); |
| 446 | BT_ASSERT(graph); |
| 447 | src = create_src(graph); |
| 448 | sink = create_sink(graph); |
| 449 | src_def_port = bt_component_source_borrow_output_port_by_name_const(src, |
| 450 | "out"); |
| 451 | BT_ASSERT(src_def_port); |
| 452 | sink_def_port = bt_component_sink_borrow_input_port_by_name_const(sink, |
| 453 | "in"); |
| 454 | BT_ASSERT(sink_def_port); |
| 455 | status = bt_graph_connect_ports(graph, src_def_port, |
| 456 | sink_def_port, NULL); |
| 457 | BT_ASSERT(status == 0); |
| 458 | src_hello_port = bt_component_source_borrow_output_port_by_name_const(src, |
| 459 | "hello"); |
| 460 | BT_ASSERT(src_hello_port); |
| 461 | gsrc = bt_component_source_as_component_const(src); |
| 462 | gsink = bt_component_sink_as_component_const(sink); |
| 463 | gsrc_def_port = bt_port_output_as_port_const(src_def_port); |
| 464 | gsrc_hello_port = bt_port_output_as_port_const(src_hello_port); |
| 465 | gsink_def_port = bt_port_input_as_port_const(sink_def_port); |
| 466 | |
| 467 | /* We're supposed to have 5 events */ |
| 468 | ok(events->len == 5, "we have the expected number of events"); |
| 469 | |
| 470 | /* Source's port added */ |
| 471 | event.type = GRAPH_SRC_OUTPUT_PORT_ADDED; |
| 472 | event.data.graph_src_output_port_added.comp = gsrc; |
| 473 | event.data.graph_src_output_port_added.port = gsrc_def_port; |
| 474 | ok(has_event(&event), "got the expected graph's port added event (for source, initial)"); |
| 475 | |
| 476 | /* Sink's port added */ |
| 477 | event.type = GRAPH_SINK_INPUT_PORT_ADDED; |
| 478 | event.data.graph_sink_input_port_added.comp = gsink; |
| 479 | event.data.graph_sink_input_port_added.port = gsink_def_port; |
| 480 | ok(has_event(&event), "got the expected graph's port added event (for sink, initial)"); |
| 481 | |
| 482 | /* Source's port connected */ |
| 483 | event.type = SRC_COMP_OUTPUT_PORT_CONNECTED; |
| 484 | event.data.src_comp_output_port_connected.comp = gsrc; |
| 485 | event.data.src_comp_output_port_connected.self_port = gsrc_def_port; |
| 486 | event.data.src_comp_output_port_connected.other_port = gsink_def_port; |
| 487 | ok(has_event(&event), "got the expected source's port connected event"); |
| 488 | src_port_connected_pos = event_pos(&event); |
| 489 | |
| 490 | /* Graph's port added (source) */ |
| 491 | event.type = GRAPH_SRC_OUTPUT_PORT_ADDED; |
| 492 | event.data.graph_src_output_port_added.comp = gsrc; |
| 493 | event.data.graph_src_output_port_added.port = gsrc_hello_port; |
| 494 | ok(has_event(&event), "got the expected graph's port added event (for source)"); |
| 495 | graph_port_added_src_pos = event_pos(&event); |
| 496 | |
| 497 | /* Sink's port connected */ |
| 498 | event.type = SINK_COMP_INPUT_PORT_CONNECTED; |
| 499 | event.data.sink_comp_input_port_connected.comp = gsink; |
| 500 | event.data.sink_comp_input_port_connected.self_port = gsink_def_port; |
| 501 | event.data.sink_comp_input_port_connected.other_port = gsrc_def_port; |
| 502 | ok(has_event(&event), "got the expected sink's port connected event"); |
| 503 | |
| 504 | /* Order of events */ |
| 505 | ok(src_port_connected_pos < graph_port_added_src_pos, |
| 506 | "event order is good"); |
| 507 | |
| 508 | bt_graph_put_ref(graph); |
| 509 | } |
| 510 | |
| 511 | static |
| 512 | void test_simple(void) |
| 513 | { |
| 514 | const bt_component_source *src; |
| 515 | const bt_component_sink *sink; |
| 516 | const bt_component *gsrc; |
| 517 | const bt_component *gsink; |
| 518 | bt_graph *graph; |
| 519 | const bt_port_output *src_def_port; |
| 520 | const bt_port_input *sink_def_port; |
| 521 | const bt_port *gsrc_def_port; |
| 522 | const bt_port *gsink_def_port; |
| 523 | struct event event; |
| 524 | bt_graph_connect_ports_status status; |
| 525 | |
| 526 | prepare_test(TEST_SIMPLE, "simple"); |
| 527 | graph = create_graph(); |
| 528 | BT_ASSERT(graph); |
| 529 | src = create_src(graph); |
| 530 | sink = create_sink(graph); |
| 531 | src_def_port = bt_component_source_borrow_output_port_by_name_const(src, |
| 532 | "out"); |
| 533 | BT_ASSERT(src_def_port); |
| 534 | sink_def_port = bt_component_sink_borrow_input_port_by_name_const(sink, |
| 535 | "in"); |
| 536 | BT_ASSERT(sink_def_port); |
| 537 | status = bt_graph_connect_ports(graph, src_def_port, |
| 538 | sink_def_port, NULL); |
| 539 | BT_ASSERT(status == 0); |
| 540 | gsrc = bt_component_source_as_component_const(src); |
| 541 | gsink = bt_component_sink_as_component_const(sink); |
| 542 | gsrc_def_port = bt_port_output_as_port_const(src_def_port); |
| 543 | gsink_def_port = bt_port_input_as_port_const(sink_def_port); |
| 544 | |
| 545 | /* We're supposed to have 4 events */ |
| 546 | ok(events->len == 4, "we have the expected number of events"); |
| 547 | |
| 548 | /* Source's port added */ |
| 549 | event.type = GRAPH_SRC_OUTPUT_PORT_ADDED; |
| 550 | event.data.graph_src_output_port_added.comp = gsrc; |
| 551 | event.data.graph_src_output_port_added.port = gsrc_def_port; |
| 552 | ok(has_event(&event), "got the expected graph's port added event (for source, initial)"); |
| 553 | |
| 554 | /* Sink's port added */ |
| 555 | event.type = GRAPH_SINK_INPUT_PORT_ADDED; |
| 556 | event.data.graph_sink_input_port_added.comp = gsink; |
| 557 | event.data.graph_sink_input_port_added.port = gsink_def_port; |
| 558 | ok(has_event(&event), "got the expected graph's port added event (for sink, initial)"); |
| 559 | |
| 560 | /* Source's port connected */ |
| 561 | event.type = SRC_COMP_OUTPUT_PORT_CONNECTED; |
| 562 | event.data.src_comp_output_port_connected.comp = gsrc; |
| 563 | event.data.src_comp_output_port_connected.self_port = gsrc_def_port; |
| 564 | event.data.src_comp_output_port_connected.other_port = gsink_def_port; |
| 565 | ok(has_event(&event), "got the expected source's port connected event"); |
| 566 | |
| 567 | /* Sink's port connected */ |
| 568 | event.type = SINK_COMP_INPUT_PORT_CONNECTED; |
| 569 | event.data.sink_comp_input_port_connected.comp = gsink; |
| 570 | event.data.sink_comp_input_port_connected.self_port = gsink_def_port; |
| 571 | event.data.sink_comp_input_port_connected.other_port = gsrc_def_port; |
| 572 | ok(has_event(&event), "got the expected sink's port connected event"); |
| 573 | |
| 574 | bt_graph_put_ref(graph); |
| 575 | } |
| 576 | |
| 577 | static |
| 578 | void test_src_port_connected_error(void) |
| 579 | { |
| 580 | const bt_component_source *src; |
| 581 | const bt_component_sink *sink; |
| 582 | const bt_component *gsrc; |
| 583 | const bt_component *gsink; |
| 584 | bt_graph *graph; |
| 585 | const bt_port_output *src_def_port; |
| 586 | const bt_port_input *sink_def_port; |
| 587 | const bt_port *gsrc_def_port; |
| 588 | const bt_port *gsink_def_port; |
| 589 | const bt_connection *conn = NULL; |
| 590 | struct event event; |
| 591 | bt_graph_connect_ports_status status; |
| 592 | |
| 593 | prepare_test(TEST_SRC_PORT_CONNECTED_ERROR, "port connected error: source"); |
| 594 | graph = create_graph(); |
| 595 | BT_ASSERT(graph); |
| 596 | src = create_src(graph); |
| 597 | sink = create_sink(graph); |
| 598 | src_def_port = bt_component_source_borrow_output_port_by_name_const(src, |
| 599 | "out"); |
| 600 | BT_ASSERT(src_def_port); |
| 601 | sink_def_port = bt_component_sink_borrow_input_port_by_name_const(sink, |
| 602 | "in"); |
| 603 | BT_ASSERT(sink_def_port); |
| 604 | status = bt_graph_connect_ports(graph, src_def_port, |
| 605 | sink_def_port, &conn); |
| 606 | ok(status != BT_GRAPH_CONNECT_PORTS_STATUS_OK, |
| 607 | "bt_graph_connect_ports() returns an error"); |
| 608 | bt_current_thread_clear_error(); |
| 609 | ok(!conn, "returned connection is still NULL"); |
| 610 | gsrc = bt_component_source_as_component_const(src); |
| 611 | gsink = bt_component_sink_as_component_const(sink); |
| 612 | gsrc_def_port = bt_port_output_as_port_const(src_def_port); |
| 613 | gsink_def_port = bt_port_input_as_port_const(sink_def_port); |
| 614 | |
| 615 | /* We're supposed to have 3 events */ |
| 616 | ok(events->len == 3, "we have the expected number of events"); |
| 617 | |
| 618 | /* Source's port added */ |
| 619 | event.type = GRAPH_SRC_OUTPUT_PORT_ADDED; |
| 620 | event.data.graph_src_output_port_added.comp = gsrc; |
| 621 | event.data.graph_src_output_port_added.port = gsrc_def_port; |
| 622 | ok(has_event(&event), "got the expected graph's port added event (for source, initial)"); |
| 623 | |
| 624 | /* Sink's port added */ |
| 625 | event.type = GRAPH_SINK_INPUT_PORT_ADDED; |
| 626 | event.data.graph_sink_input_port_added.comp = gsink; |
| 627 | event.data.graph_sink_input_port_added.port = gsink_def_port; |
| 628 | ok(has_event(&event), "got the expected graph's port added event (for sink, initial)"); |
| 629 | |
| 630 | /* Source's port connected */ |
| 631 | event.type = SRC_COMP_OUTPUT_PORT_CONNECTED; |
| 632 | event.data.src_comp_output_port_connected.comp = gsrc; |
| 633 | event.data.src_comp_output_port_connected.self_port = gsrc_def_port; |
| 634 | event.data.src_comp_output_port_connected.other_port = gsink_def_port; |
| 635 | ok(has_event(&event), "got the expected source's port connected event"); |
| 636 | |
| 637 | bt_graph_put_ref(graph); |
| 638 | } |
| 639 | |
| 640 | static |
| 641 | void test_sink_port_connected_error(void) |
| 642 | { |
| 643 | const bt_component_source *src; |
| 644 | const bt_component_sink *sink; |
| 645 | const bt_component *gsrc; |
| 646 | const bt_component *gsink; |
| 647 | bt_graph *graph; |
| 648 | const bt_port_output *src_def_port; |
| 649 | const bt_port_input *sink_def_port; |
| 650 | const bt_port *gsrc_def_port; |
| 651 | const bt_port *gsink_def_port; |
| 652 | const bt_connection *conn = NULL; |
| 653 | struct event event; |
| 654 | bt_graph_connect_ports_status status; |
| 655 | |
| 656 | prepare_test(TEST_SINK_PORT_CONNECTED_ERROR, "port connected error: sink"); |
| 657 | graph = create_graph(); |
| 658 | BT_ASSERT(graph); |
| 659 | src = create_src(graph); |
| 660 | sink = create_sink(graph); |
| 661 | src_def_port = bt_component_source_borrow_output_port_by_name_const(src, |
| 662 | "out"); |
| 663 | BT_ASSERT(src_def_port); |
| 664 | sink_def_port = bt_component_sink_borrow_input_port_by_name_const(sink, |
| 665 | "in"); |
| 666 | BT_ASSERT(sink_def_port); |
| 667 | status = bt_graph_connect_ports(graph, src_def_port, |
| 668 | sink_def_port, &conn); |
| 669 | ok(status != BT_GRAPH_CONNECT_PORTS_STATUS_OK, |
| 670 | "bt_graph_connect_ports() returns an error"); |
| 671 | bt_current_thread_clear_error(); |
| 672 | ok(!conn, "returned connection is still NULL"); |
| 673 | gsrc = bt_component_source_as_component_const(src); |
| 674 | gsink = bt_component_sink_as_component_const(sink); |
| 675 | gsrc_def_port = bt_port_output_as_port_const(src_def_port); |
| 676 | gsink_def_port = bt_port_input_as_port_const(sink_def_port); |
| 677 | |
| 678 | /* We're supposed to have 4 events */ |
| 679 | ok(events->len == 4, "we have the expected number of events"); |
| 680 | |
| 681 | /* Source's port added */ |
| 682 | event.type = GRAPH_SRC_OUTPUT_PORT_ADDED; |
| 683 | event.data.graph_src_output_port_added.comp = gsrc; |
| 684 | event.data.graph_src_output_port_added.port = gsrc_def_port; |
| 685 | ok(has_event(&event), "got the expected graph's port added event (for source, initial)"); |
| 686 | |
| 687 | /* Sink's port added */ |
| 688 | event.type = GRAPH_SINK_INPUT_PORT_ADDED; |
| 689 | event.data.graph_sink_input_port_added.comp = gsink; |
| 690 | event.data.graph_sink_input_port_added.port = gsink_def_port; |
| 691 | ok(has_event(&event), "got the expected graph's port added event (for sink, initial)"); |
| 692 | |
| 693 | /* Source's port connected */ |
| 694 | event.type = SRC_COMP_OUTPUT_PORT_CONNECTED; |
| 695 | event.data.src_comp_output_port_connected.comp = gsrc; |
| 696 | event.data.src_comp_output_port_connected.self_port = gsrc_def_port; |
| 697 | event.data.src_comp_output_port_connected.other_port = gsink_def_port; |
| 698 | ok(has_event(&event), "got the expected source's port connected event"); |
| 699 | |
| 700 | /* Sink's port connected */ |
| 701 | event.type = SINK_COMP_INPUT_PORT_CONNECTED; |
| 702 | event.data.sink_comp_input_port_connected.comp = gsink; |
| 703 | event.data.sink_comp_input_port_connected.self_port = gsink_def_port; |
| 704 | event.data.sink_comp_input_port_connected.other_port = gsrc_def_port; |
| 705 | ok(has_event(&event), "got the expected sink's port connected event"); |
| 706 | |
| 707 | bt_graph_put_ref(graph); |
| 708 | } |
| 709 | |
| 710 | static |
| 711 | void test_empty_graph(void) |
| 712 | { |
| 713 | bt_graph *graph; |
| 714 | |
| 715 | prepare_test(TEST_EMPTY_GRAPH, "empty graph"); |
| 716 | graph = create_graph(); |
| 717 | ok(events->len == 0, "empty graph generates no events"); |
| 718 | bt_graph_put_ref(graph); |
| 719 | } |
| 720 | |
| 721 | int main(int argc, char **argv) |
| 722 | { |
| 723 | plan_tests(NR_TESTS); |
| 724 | init_test(); |
| 725 | test_empty_graph(); |
| 726 | test_simple(); |
| 727 | test_src_port_connected_error(); |
| 728 | test_sink_port_connected_error(); |
| 729 | test_src_adds_port_in_port_connected(); |
| 730 | fini_test(); |
| 731 | return exit_status(); |
| 732 | } |