lib: fully configure graph (add components, connect ports), then run
[babeltrace.git] / tests / lib / test_graph_topo.c
CommitLineData
fb9935cc
PP
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
d94d92ac
PP
20#include <babeltrace/babeltrace.h>
21#include <babeltrace/assert-internal.h>
fb9935cc
PP
22#include <stdlib.h>
23#include <string.h>
24#include <stdbool.h>
fb9935cc
PP
25#include <glib.h>
26
27#include "tap/tap.h"
28
4725a201 29#define NR_TESTS 48
fb9935cc
PP
30
31enum event_type {
d94d92ac
PP
32 SRC_COMP_ACCEPT_OUTPUT_PORT_CONNECTION,
33 SINK_COMP_ACCEPT_INPUT_PORT_CONNECTION,
34 SRC_COMP_OUTPUT_PORT_CONNECTED,
35 SINK_COMP_INPUT_PORT_CONNECTED,
d94d92ac
PP
36 GRAPH_SRC_OUTPUT_PORT_ADDED,
37 GRAPH_SINK_INPUT_PORT_ADDED,
d94d92ac 38 GRAPH_SRC_SINK_PORTS_CONNECTED,
fb9935cc
PP
39};
40
41enum test {
42 TEST_EMPTY_GRAPH,
43 TEST_SIMPLE,
bf55043c
PP
44 TEST_SRC_PORT_CONNECTED_ERROR,
45 TEST_SINK_PORT_CONNECTED_ERROR,
c14181ff 46 TEST_SRC_ADDS_PORT_IN_PORT_CONNECTED,
fb9935cc
PP
47};
48
49struct event {
50 enum event_type type;
51
52 union {
53 struct {
b19ff26f
PP
54 const bt_component *comp;
55 const bt_port *self_port;
56 const bt_port *other_port;
d94d92ac
PP
57 } src_comp_accept_output_port_connection;
58
59 struct {
b19ff26f
PP
60 const bt_component *comp;
61 const bt_port *self_port;
62 const bt_port *other_port;
d94d92ac
PP
63 } sink_comp_accept_input_port_connection;
64
65 struct {
b19ff26f
PP
66 const bt_component *comp;
67 const bt_port *self_port;
68 const bt_port *other_port;
d94d92ac 69 } src_comp_output_port_connected;
fb9935cc 70
c14181ff 71 struct {
b19ff26f
PP
72 const bt_component *comp;
73 const bt_port *self_port;
74 const bt_port *other_port;
d94d92ac
PP
75 } sink_comp_input_port_connected;
76
fb9935cc 77 struct {
b19ff26f
PP
78 const bt_component *comp;
79 const bt_port *port;
d94d92ac 80 } graph_src_output_port_added;
fb9935cc
PP
81
82 struct {
b19ff26f
PP
83 const bt_component *comp;
84 const bt_port *port;
d94d92ac 85 } graph_sink_input_port_added;
fb9935cc 86
fb9935cc 87 struct {
b19ff26f
PP
88 const bt_component *upstream_comp;
89 const bt_component *downstream_comp;
90 const bt_port *upstream_port;
91 const bt_port *downstream_port;
d94d92ac 92 } graph_src_sink_ports_connected;
fb9935cc
PP
93 } data;
94};
95
96static GArray *events;
b19ff26f
PP
97static bt_component_class_source *src_comp_class;
98static bt_component_class_sink *sink_comp_class;
fb9935cc
PP
99static enum test current_test;
100
101static
102void clear_events(void)
103{
104 g_array_set_size(events, 0);
105}
106
107static
108void append_event(struct event *event)
109{
110 g_array_append_val(events, *event);
111}
112
113static
114bool compare_events(struct event *ev_a, struct event *ev_b)
115{
116 if (ev_a->type != ev_b->type) {
117 return false;
118 }
119
120 switch (ev_a->type) {
d94d92ac
PP
121 case SRC_COMP_ACCEPT_OUTPUT_PORT_CONNECTION:
122 if (ev_a->data.src_comp_accept_output_port_connection.comp !=
123 ev_b->data.src_comp_accept_output_port_connection.comp) {
fb9935cc
PP
124 return false;
125 }
126
d94d92ac
PP
127 if (ev_a->data.src_comp_accept_output_port_connection.self_port !=
128 ev_b->data.src_comp_accept_output_port_connection.self_port) {
fb9935cc
PP
129 return false;
130 }
131
d94d92ac
PP
132 if (ev_a->data.src_comp_accept_output_port_connection.other_port !=
133 ev_b->data.src_comp_accept_output_port_connection.other_port) {
fb9935cc
PP
134 return false;
135 }
136 break;
d94d92ac
PP
137 case SINK_COMP_ACCEPT_INPUT_PORT_CONNECTION:
138 if (ev_a->data.sink_comp_accept_input_port_connection.comp !=
139 ev_b->data.sink_comp_accept_input_port_connection.comp) {
c14181ff
PP
140 return false;
141 }
142
d94d92ac
PP
143 if (ev_a->data.sink_comp_accept_input_port_connection.self_port !=
144 ev_b->data.sink_comp_accept_input_port_connection.self_port) {
c14181ff
PP
145 return false;
146 }
147
d94d92ac
PP
148 if (ev_a->data.sink_comp_accept_input_port_connection.other_port !=
149 ev_b->data.sink_comp_accept_input_port_connection.other_port) {
c14181ff
PP
150 return false;
151 }
152 break;
d94d92ac
PP
153 case SRC_COMP_OUTPUT_PORT_CONNECTED:
154 if (ev_a->data.src_comp_output_port_connected.comp !=
155 ev_b->data.src_comp_output_port_connected.comp) {
156 return false;
157 }
158
159 if (ev_a->data.src_comp_output_port_connected.self_port !=
160 ev_b->data.src_comp_output_port_connected.self_port) {
fb9935cc
PP
161 return false;
162 }
163
d94d92ac
PP
164 if (ev_a->data.src_comp_output_port_connected.other_port !=
165 ev_b->data.src_comp_output_port_connected.other_port) {
fb9935cc
PP
166 return false;
167 }
168 break;
d94d92ac
PP
169 case SINK_COMP_INPUT_PORT_CONNECTED:
170 if (ev_a->data.sink_comp_input_port_connected.comp !=
171 ev_b->data.sink_comp_input_port_connected.comp) {
fb9935cc
PP
172 return false;
173 }
174
d94d92ac
PP
175 if (ev_a->data.sink_comp_input_port_connected.self_port !=
176 ev_b->data.sink_comp_input_port_connected.self_port) {
177 return false;
178 }
179
180 if (ev_a->data.sink_comp_input_port_connected.other_port !=
181 ev_b->data.sink_comp_input_port_connected.other_port) {
fb9935cc
PP
182 return false;
183 }
184 break;
d94d92ac
PP
185 case GRAPH_SRC_OUTPUT_PORT_ADDED:
186 if (ev_a->data.graph_src_output_port_added.comp !=
187 ev_b->data.graph_src_output_port_added.comp) {
fb9935cc
PP
188 return false;
189 }
190
d94d92ac
PP
191 if (ev_a->data.graph_src_output_port_added.port !=
192 ev_b->data.graph_src_output_port_added.port) {
193 return false;
194 }
195 break;
196 case GRAPH_SINK_INPUT_PORT_ADDED:
197 if (ev_a->data.graph_sink_input_port_added.comp !=
198 ev_b->data.graph_sink_input_port_added.comp) {
199 return false;
200 }
201
202 if (ev_a->data.graph_sink_input_port_added.port !=
203 ev_b->data.graph_sink_input_port_added.port) {
204 return false;
205 }
206 break;
d94d92ac
PP
207 case GRAPH_SRC_SINK_PORTS_CONNECTED:
208 if (ev_a->data.graph_src_sink_ports_connected.upstream_comp !=
209 ev_b->data.graph_src_sink_ports_connected.upstream_comp) {
210 return false;
211 }
212
213 if (ev_a->data.graph_src_sink_ports_connected.downstream_comp !=
214 ev_b->data.graph_src_sink_ports_connected.downstream_comp) {
fb9935cc
PP
215 return false;
216 }
217
d94d92ac
PP
218 if (ev_a->data.graph_src_sink_ports_connected.upstream_port !=
219 ev_b->data.graph_src_sink_ports_connected.upstream_port) {
fb9935cc
PP
220 return false;
221 }
222
d94d92ac
PP
223 if (ev_a->data.graph_src_sink_ports_connected.downstream_port !=
224 ev_b->data.graph_src_sink_ports_connected.downstream_port) {
fb9935cc
PP
225 return false;
226 }
227 break;
fb9935cc 228 default:
0fbb9a9f 229 abort();
fb9935cc
PP
230 }
231
232 return true;
233}
234
235static
236bool has_event(struct event *event)
237{
238 size_t i;
239
240 for (i = 0; i < events->len; i++) {
241 struct event *ev = &g_array_index(events, struct event, i);
242
243 if (compare_events(event, ev)) {
244 return true;
245 }
246 }
247
248 return false;
249}
250
251static
252size_t event_pos(struct event *event)
253{
254 size_t i;
255
256 for (i = 0; i < events->len; i++) {
257 struct event *ev = &g_array_index(events, struct event, i);
258
259 if (compare_events(event, ev)) {
260 return i;
261 }
262 }
263
1974687e 264 return SIZE_MAX;
fb9935cc
PP
265}
266
267static
4cdfc5e8 268bt_self_message_iterator_status src_iter_next(
d6e69534
PP
269 bt_self_message_iterator *self_iterator,
270 bt_message_array_const msgs, uint64_t capacity,
d4393e08 271 uint64_t *count)
fb9935cc 272{
d6e69534 273 return BT_SELF_MESSAGE_ITERATOR_STATUS_ERROR;
fb9935cc
PP
274}
275
276static
4cdfc5e8 277bt_self_component_status src_accept_output_port_connection(
b19ff26f
PP
278 bt_self_component_source *self_comp,
279 bt_self_component_port_output *self_comp_port,
280 const bt_port_input *other_port)
fb9935cc 281{
fb9935cc 282 struct event event = {
d94d92ac
PP
283 .type = SRC_COMP_ACCEPT_OUTPUT_PORT_CONNECTION,
284 .data.src_comp_accept_output_port_connection = {
707b7d35
PP
285 .comp = bt_self_component_as_component(
286 bt_self_component_source_as_self_component(
d94d92ac 287 self_comp)),
707b7d35
PP
288 .self_port = bt_self_component_port_as_port(
289 bt_self_component_port_output_as_self_component_port(
d94d92ac 290 self_comp_port)),
0d72b8c3 291 .other_port = bt_port_input_as_port_const(other_port),
fb9935cc
PP
292 },
293 };
294
fb9935cc 295 append_event(&event);
d94d92ac 296 return BT_SELF_COMPONENT_STATUS_OK;
c14181ff
PP
297}
298
299static
4cdfc5e8 300bt_self_component_status sink_accept_input_port_connection(
b19ff26f
PP
301 bt_self_component_sink *self_comp,
302 bt_self_component_port_input *self_comp_port,
303 const bt_port_output *other_port)
c14181ff 304{
d94d92ac
PP
305 struct event event = {
306 .type = SINK_COMP_ACCEPT_INPUT_PORT_CONNECTION,
307 .data.sink_comp_accept_input_port_connection = {
707b7d35
PP
308 .comp = bt_self_component_as_component(
309 bt_self_component_sink_as_self_component(
d94d92ac 310 self_comp)),
707b7d35
PP
311 .self_port = bt_self_component_port_as_port(
312 bt_self_component_port_input_as_self_component_port(
d94d92ac 313 self_comp_port)),
0d72b8c3 314 .other_port = bt_port_output_as_port_const(other_port),
d94d92ac
PP
315 },
316 };
c14181ff 317
d94d92ac
PP
318 append_event(&event);
319 return BT_SELF_COMPONENT_STATUS_OK;
320}
321
322static
4cdfc5e8 323bt_self_component_status src_output_port_connected(
b19ff26f
PP
324 bt_self_component_source *self_comp,
325 bt_self_component_port_output *self_comp_port,
326 const bt_port_input *other_port)
d94d92ac
PP
327{
328 int ret;
c14181ff 329 struct event event = {
d94d92ac
PP
330 .type = SRC_COMP_OUTPUT_PORT_CONNECTED,
331 .data.src_comp_output_port_connected = {
707b7d35
PP
332 .comp = bt_self_component_as_component(
333 bt_self_component_source_as_self_component(
d94d92ac 334 self_comp)),
707b7d35
PP
335 .self_port = bt_self_component_port_as_port(
336 bt_self_component_port_output_as_self_component_port(
d94d92ac 337 self_comp_port)),
0d72b8c3 338 .other_port = bt_port_input_as_port_const(other_port),
c14181ff
PP
339 },
340 };
341
c14181ff 342 append_event(&event);
fb9935cc
PP
343
344 switch (current_test) {
c14181ff 345 case TEST_SRC_ADDS_PORT_IN_PORT_CONNECTED:
d94d92ac
PP
346 ret = bt_self_component_source_add_output_port(
347 self_comp, "hello", NULL, NULL);
25583cd0 348 BT_ASSERT(ret == 0);
fb9935cc 349 break;
bf55043c 350 case TEST_SRC_PORT_CONNECTED_ERROR:
d94d92ac 351 return BT_SELF_COMPONENT_STATUS_ERROR;
fb9935cc
PP
352 default:
353 break;
354 }
bf55043c 355
d94d92ac 356 return BT_SELF_COMPONENT_STATUS_OK;
fb9935cc
PP
357}
358
359static
4cdfc5e8 360bt_self_component_status sink_input_port_connected(
b19ff26f
PP
361 bt_self_component_sink *self_comp,
362 bt_self_component_port_input *self_comp_port,
363 const bt_port_output *other_port)
d94d92ac
PP
364{
365 struct event event = {
366 .type = SINK_COMP_INPUT_PORT_CONNECTED,
367 .data.sink_comp_input_port_connected = {
707b7d35
PP
368 .comp = bt_self_component_as_component(
369 bt_self_component_sink_as_self_component(
d94d92ac 370 self_comp)),
707b7d35
PP
371 .self_port = bt_self_component_port_as_port(
372 bt_self_component_port_input_as_self_component_port(
d94d92ac 373 self_comp_port)),
0d72b8c3 374 .other_port = bt_port_output_as_port_const(other_port),
d94d92ac
PP
375 },
376 };
377
378 append_event(&event);
379
380 if (current_test == TEST_SINK_PORT_CONNECTED_ERROR) {
381 return BT_SELF_COMPONENT_STATUS_ERROR;
382 } else {
383 return BT_SELF_COMPONENT_STATUS_OK;
384 }
385}
386
d94d92ac 387static
4cdfc5e8 388bt_self_component_status src_init(
b19ff26f
PP
389 bt_self_component_source *self_comp,
390 const bt_value *params, void *init_method_data)
b9d103be 391{
147337a3 392 int ret;
b9d103be 393
d94d92ac
PP
394 ret = bt_self_component_source_add_output_port(
395 self_comp, "out", NULL, NULL);
25583cd0 396 BT_ASSERT(ret == 0);
d94d92ac 397 return BT_SELF_COMPONENT_STATUS_OK;
b9d103be
PP
398}
399
fb9935cc 400static
4cdfc5e8 401bt_self_component_status sink_init(
b19ff26f
PP
402 bt_self_component_sink *self_comp,
403 const bt_value *params, void *init_method_data)
fb9935cc 404{
d94d92ac
PP
405 int ret;
406
407 ret = bt_self_component_sink_add_input_port(self_comp,
408 "in", NULL, NULL);
409 BT_ASSERT(ret == 0);
410 return BT_SELF_COMPONENT_STATUS_OK;
411}
412
413static
4cdfc5e8 414bt_self_component_status sink_consume(
b19ff26f 415 bt_self_component_sink *self_comp)
d94d92ac 416{
d94d92ac 417 return BT_SELF_COMPONENT_STATUS_OK;
fb9935cc
PP
418}
419
420static
b19ff26f
PP
421void graph_src_output_port_added(const bt_component_source *comp,
422 const bt_port_output *port, void *data)
fb9935cc
PP
423{
424 struct event event = {
d94d92ac
PP
425 .type = GRAPH_SRC_OUTPUT_PORT_ADDED,
426 .data.graph_src_output_port_added = {
0d72b8c3
PP
427 .comp = bt_component_source_as_component_const(comp),
428 .port = bt_port_output_as_port_const(port),
fb9935cc
PP
429 },
430 };
431
fb9935cc 432 append_event(&event);
fb9935cc
PP
433}
434
435static
b19ff26f
PP
436void graph_sink_input_port_added(const bt_component_sink *comp,
437 const bt_port_input *port, void *data)
fb9935cc
PP
438{
439 struct event event = {
d94d92ac
PP
440 .type = GRAPH_SINK_INPUT_PORT_ADDED,
441 .data.graph_sink_input_port_added = {
0d72b8c3
PP
442 .comp = bt_component_sink_as_component_const(comp),
443 .port = bt_port_input_as_port_const(port),
fb9935cc
PP
444 },
445 };
446
fb9935cc
PP
447 append_event(&event);
448}
449
fb9935cc 450static
b19ff26f
PP
451void graph_src_sink_ports_connected(const bt_component_source *upstream_comp,
452 const bt_component_sink *downstream_comp,
453 const bt_port_output *upstream_port,
454 const bt_port_input *downstream_port, void *data)
fb9935cc 455{
fb9935cc 456 struct event event = {
d94d92ac
PP
457 .type = GRAPH_SRC_SINK_PORTS_CONNECTED,
458 .data.graph_src_sink_ports_connected = {
459 .upstream_comp =
0d72b8c3
PP
460 bt_component_source_as_component_const(upstream_comp),
461 .downstream_comp =
462 bt_component_sink_as_component_const(downstream_comp),
463 .upstream_port =
464 bt_port_output_as_port_const(upstream_port),
465 .downstream_port =
466 bt_port_input_as_port_const(downstream_port),
fb9935cc
PP
467 },
468 };
469
470 append_event(&event);
471}
472
fb9935cc
PP
473static
474void init_test(void)
475{
476 int ret;
477
0d72b8c3 478 src_comp_class = bt_component_class_source_create(
d94d92ac 479 "src", src_iter_next);
25583cd0 480 BT_ASSERT(src_comp_class);
0d72b8c3 481 ret = bt_component_class_source_set_init_method(
d94d92ac 482 src_comp_class, src_init);
25583cd0 483 BT_ASSERT(ret == 0);
0d72b8c3 484 ret = bt_component_class_source_set_accept_output_port_connection_method(
d94d92ac 485 src_comp_class, src_accept_output_port_connection);
25583cd0 486 BT_ASSERT(ret == 0);
0d72b8c3 487 ret = bt_component_class_source_set_output_port_connected_method(
d94d92ac 488 src_comp_class, src_output_port_connected);
25583cd0 489 BT_ASSERT(ret == 0);
0d72b8c3 490 sink_comp_class = bt_component_class_sink_create("sink",
d94d92ac 491 sink_consume);
25583cd0 492 BT_ASSERT(sink_comp_class);
0d72b8c3 493 ret = bt_component_class_sink_set_init_method(sink_comp_class,
d94d92ac 494 sink_init);
25583cd0 495 BT_ASSERT(ret == 0);
0d72b8c3 496 ret = bt_component_class_sink_set_accept_input_port_connection_method(
d94d92ac 497 sink_comp_class, sink_accept_input_port_connection);
25583cd0 498 BT_ASSERT(ret == 0);
0d72b8c3 499 ret = bt_component_class_sink_set_input_port_connected_method(
d94d92ac 500 sink_comp_class, sink_input_port_connected);
25583cd0 501 BT_ASSERT(ret == 0);
fb9935cc 502 events = g_array_new(FALSE, TRUE, sizeof(struct event));
25583cd0 503 BT_ASSERT(events);
fb9935cc
PP
504}
505
506static
507void fini_test(void)
508{
c5b9b441
PP
509 bt_component_class_source_put_ref(src_comp_class);
510 bt_component_class_sink_put_ref(sink_comp_class);
fb9935cc
PP
511 g_array_free(events, TRUE);
512}
513
514static
b19ff26f 515const bt_component_source *create_src(bt_graph *graph)
fb9935cc 516{
b19ff26f 517 const bt_component_source *comp;
36712f1d 518 int ret;
fb9935cc 519
0d72b8c3 520 ret = bt_graph_add_source_component(graph, src_comp_class,
d94d92ac 521 "src-comp", NULL, &comp);
25583cd0 522 BT_ASSERT(ret == 0);
fb9935cc
PP
523 return comp;
524}
525
526static
b19ff26f 527const bt_component_sink *create_sink(bt_graph *graph)
fb9935cc 528{
b19ff26f 529 const bt_component_sink *comp;
36712f1d 530 int ret;
fb9935cc 531
0d72b8c3 532 ret = bt_graph_add_sink_component(graph, sink_comp_class,
d94d92ac 533 "sink-comp", NULL, &comp);
25583cd0 534 BT_ASSERT(ret == 0);
fb9935cc
PP
535 return comp;
536}
537
538static
b19ff26f 539bt_graph *create_graph(void)
fb9935cc 540{
b19ff26f 541 bt_graph *graph = bt_graph_create();
fb9935cc
PP
542 int ret;
543
25583cd0 544 BT_ASSERT(graph);
0d72b8c3 545 ret = bt_graph_add_source_component_output_port_added_listener(
d94d92ac
PP
546 graph, graph_src_output_port_added, NULL, NULL, NULL);
547 BT_ASSERT(ret >= 0);
0d72b8c3 548 ret = bt_graph_add_sink_component_input_port_added_listener(
d94d92ac 549 graph, graph_sink_input_port_added, NULL, NULL, NULL);
25583cd0 550 BT_ASSERT(ret >= 0);
0d72b8c3 551 ret = bt_graph_add_source_sink_component_ports_connected_listener(
d94d92ac
PP
552 graph, graph_src_sink_ports_connected, NULL, NULL, NULL);
553 BT_ASSERT(ret >= 0);
fb9935cc
PP
554 return graph;
555}
556
557static
558void prepare_test(enum test test, const char *name)
559{
560 clear_events();
561 current_test = test;
562 diag("test: %s", name);
563}
564
fb9935cc 565static
c14181ff 566void test_src_adds_port_in_port_connected(void)
fb9935cc 567{
b19ff26f
PP
568 const bt_component_source *src;
569 const bt_component_sink *sink;
570 const bt_component *gsrc;
571 const bt_component *gsink;
572 bt_graph *graph;
573 const bt_port_output *src_def_port;
574 const bt_port_output *src_hello_port;
575 const bt_port_input *sink_def_port;
576 const bt_port *gsrc_def_port;
577 const bt_port *gsrc_hello_port;
578 const bt_port *gsink_def_port;
fb9935cc 579 struct event event;
4cdfc5e8 580 bt_graph_status status;
c14181ff
PP
581 size_t src_accept_port_connection_pos;
582 size_t sink_accept_port_connection_pos;
583 size_t src_port_connected_pos;
584 size_t sink_port_connected_pos;
fb9935cc
PP
585 size_t graph_ports_connected_pos;
586 size_t graph_port_added_src_pos;
587
c14181ff
PP
588 prepare_test(TEST_SRC_ADDS_PORT_IN_PORT_CONNECTED,
589 "source adds port in port connected");
fb9935cc 590 graph = create_graph();
25583cd0 591 BT_ASSERT(graph);
36712f1d
PP
592 src = create_src(graph);
593 sink = create_sink(graph);
0d72b8c3
PP
594 src_def_port = bt_component_source_borrow_output_port_by_name_const(src,
595 "out");
25583cd0 596 BT_ASSERT(src_def_port);
0d72b8c3
PP
597 sink_def_port = bt_component_sink_borrow_input_port_by_name_const(sink,
598 "in");
25583cd0 599 BT_ASSERT(sink_def_port);
0d72b8c3 600 status = bt_graph_connect_ports(graph, src_def_port,
d94d92ac 601 sink_def_port, NULL);
25583cd0 602 BT_ASSERT(status == 0);
0d72b8c3
PP
603 src_hello_port = bt_component_source_borrow_output_port_by_name_const(src,
604 "hello");
25583cd0 605 BT_ASSERT(src_hello_port);
0d72b8c3
PP
606 gsrc = bt_component_source_as_component_const(src);
607 gsink = bt_component_sink_as_component_const(sink);
608 gsrc_def_port = bt_port_output_as_port_const(src_def_port);
609 gsrc_hello_port = bt_port_output_as_port_const(src_hello_port);
610 gsink_def_port = bt_port_input_as_port_const(sink_def_port);
fb9935cc 611
36712f1d
PP
612 /* We're supposed to have 8 events */
613 ok(events->len == 8, "we have the expected number of events");
614
615 /* Source's port added */
d94d92ac
PP
616 event.type = GRAPH_SRC_OUTPUT_PORT_ADDED;
617 event.data.graph_src_output_port_added.comp = gsrc;
618 event.data.graph_src_output_port_added.port = gsrc_def_port;
36712f1d
PP
619 ok(has_event(&event), "got the expected graph's port added event (for source, initial)");
620
621 /* Sink's port added */
d94d92ac
PP
622 event.type = GRAPH_SINK_INPUT_PORT_ADDED;
623 event.data.graph_sink_input_port_added.comp = gsink;
624 event.data.graph_sink_input_port_added.port = gsink_def_port;
36712f1d 625 ok(has_event(&event), "got the expected graph's port added event (for sink, initial)");
fb9935cc
PP
626
627 /* Source's accept port connection */
d94d92ac
PP
628 event.type = SRC_COMP_ACCEPT_OUTPUT_PORT_CONNECTION;
629 event.data.src_comp_accept_output_port_connection.comp = gsrc;
630 event.data.src_comp_accept_output_port_connection.self_port = gsrc_def_port;
631 event.data.src_comp_accept_output_port_connection.other_port = gsink_def_port;
fb9935cc 632 ok(has_event(&event), "got the expected source's accept port connection event");
c14181ff
PP
633 src_accept_port_connection_pos = event_pos(&event);
634
635 /* Sink's accept port connection */
d94d92ac
PP
636 event.type = SINK_COMP_ACCEPT_INPUT_PORT_CONNECTION;
637 event.data.sink_comp_accept_input_port_connection.comp = gsink;
638 event.data.sink_comp_accept_input_port_connection.self_port = gsink_def_port;
639 event.data.sink_comp_accept_input_port_connection.other_port = gsrc_def_port;
c14181ff
PP
640 ok(has_event(&event), "got the expected sink's accept port connection event");
641 sink_accept_port_connection_pos = event_pos(&event);
642
643 /* Source's port connected */
d94d92ac
PP
644 event.type = SRC_COMP_OUTPUT_PORT_CONNECTED;
645 event.data.src_comp_output_port_connected.comp = gsrc;
646 event.data.src_comp_output_port_connected.self_port = gsrc_def_port;
647 event.data.src_comp_output_port_connected.other_port = gsink_def_port;
c14181ff
PP
648 ok(has_event(&event), "got the expected source's port connected event");
649 src_port_connected_pos = event_pos(&event);
fb9935cc
PP
650
651 /* Graph's port added (source) */
d94d92ac
PP
652 event.type = GRAPH_SRC_OUTPUT_PORT_ADDED;
653 event.data.graph_src_output_port_added.comp = gsrc;
654 event.data.graph_src_output_port_added.port = gsrc_hello_port;
fb9935cc
PP
655 ok(has_event(&event), "got the expected graph's port added event (for source)");
656 graph_port_added_src_pos = event_pos(&event);
657
c14181ff 658 /* Sink's port connected */
d94d92ac
PP
659 event.type = SINK_COMP_INPUT_PORT_CONNECTED;
660 event.data.sink_comp_input_port_connected.comp = gsink;
661 event.data.sink_comp_input_port_connected.self_port = gsink_def_port;
662 event.data.sink_comp_input_port_connected.other_port = gsrc_def_port;
c14181ff
PP
663 ok(has_event(&event), "got the expected sink's port connected event");
664 sink_port_connected_pos = event_pos(&event);
fb9935cc
PP
665
666 /* Graph's ports connected */
d94d92ac
PP
667 event.type = GRAPH_SRC_SINK_PORTS_CONNECTED;
668 event.data.graph_src_sink_ports_connected.upstream_comp = gsrc;
669 event.data.graph_src_sink_ports_connected.downstream_comp = gsink;
670 event.data.graph_src_sink_ports_connected.upstream_port = gsrc_def_port;
671 event.data.graph_src_sink_ports_connected.downstream_port = gsink_def_port;
672 ok(has_event(&event), "got the expected graph's ports connected event");
fb9935cc
PP
673 graph_ports_connected_pos = event_pos(&event);
674
675 /* Order of events */
c14181ff 676 ok(src_port_connected_pos < graph_ports_connected_pos,
fb9935cc 677 "event order is good (1)");
c14181ff 678 ok(sink_port_connected_pos < graph_ports_connected_pos,
fb9935cc 679 "event order is good (2)");
c14181ff 680 ok(src_accept_port_connection_pos < src_port_connected_pos,
fb9935cc 681 "event order is good (3)");
c14181ff 682 ok(sink_accept_port_connection_pos < sink_port_connected_pos,
fb9935cc 683 "event order is good (4)");
c14181ff
PP
684 ok(src_port_connected_pos < graph_port_added_src_pos,
685 "event order is good (5)");
686 ok(graph_port_added_src_pos < graph_ports_connected_pos,
687 "event order is good (6)");
fb9935cc 688
c5b9b441
PP
689 bt_component_source_put_ref(src);
690 bt_component_sink_put_ref(sink);
691 bt_graph_put_ref(graph);
fb9935cc
PP
692}
693
694static
695void test_simple(void)
696{
b19ff26f
PP
697 const bt_component_source *src;
698 const bt_component_sink *sink;
699 const bt_component *gsrc;
700 const bt_component *gsink;
701 bt_graph *graph;
702 const bt_port_output *src_def_port;
703 const bt_port_input *sink_def_port;
704 const bt_port *gsrc_def_port;
705 const bt_port *gsink_def_port;
fb9935cc 706 struct event event;
4cdfc5e8 707 bt_graph_status status;
c14181ff
PP
708 size_t src_accept_port_connection_pos;
709 size_t sink_accept_port_connection_pos;
710 size_t src_port_connected_pos;
711 size_t sink_port_connected_pos;
fb9935cc
PP
712 size_t graph_ports_connected_pos;
713
714 prepare_test(TEST_SIMPLE, "simple");
fb9935cc 715 graph = create_graph();
25583cd0 716 BT_ASSERT(graph);
36712f1d
PP
717 src = create_src(graph);
718 sink = create_sink(graph);
0d72b8c3
PP
719 src_def_port = bt_component_source_borrow_output_port_by_name_const(src,
720 "out");
25583cd0 721 BT_ASSERT(src_def_port);
0d72b8c3
PP
722 sink_def_port = bt_component_sink_borrow_input_port_by_name_const(sink,
723 "in");
25583cd0 724 BT_ASSERT(sink_def_port);
0d72b8c3 725 status = bt_graph_connect_ports(graph, src_def_port,
d94d92ac 726 sink_def_port, NULL);
25583cd0 727 BT_ASSERT(status == 0);
0d72b8c3
PP
728 gsrc = bt_component_source_as_component_const(src);
729 gsink = bt_component_sink_as_component_const(sink);
730 gsrc_def_port = bt_port_output_as_port_const(src_def_port);
731 gsink_def_port = bt_port_input_as_port_const(sink_def_port);
fb9935cc 732
36712f1d
PP
733 /* We're supposed to have 7 events */
734 ok(events->len == 7, "we have the expected number of events");
735
736 /* Source's port added */
d94d92ac
PP
737 event.type = GRAPH_SRC_OUTPUT_PORT_ADDED;
738 event.data.graph_src_output_port_added.comp = gsrc;
739 event.data.graph_src_output_port_added.port = gsrc_def_port;
36712f1d
PP
740 ok(has_event(&event), "got the expected graph's port added event (for source, initial)");
741
742 /* Sink's port added */
d94d92ac
PP
743 event.type = GRAPH_SINK_INPUT_PORT_ADDED;
744 event.data.graph_sink_input_port_added.comp = gsink;
745 event.data.graph_sink_input_port_added.port = gsink_def_port;
36712f1d 746 ok(has_event(&event), "got the expected graph's port added event (for sink, initial)");
fb9935cc
PP
747
748 /* Source's accept port connection */
d94d92ac
PP
749 event.type = SRC_COMP_ACCEPT_OUTPUT_PORT_CONNECTION;
750 event.data.src_comp_accept_output_port_connection.comp = gsrc;
751 event.data.src_comp_accept_output_port_connection.self_port = gsrc_def_port;
752 event.data.src_comp_accept_output_port_connection.other_port = gsink_def_port;
fb9935cc 753 ok(has_event(&event), "got the expected source's accept port connection event");
c14181ff 754 src_accept_port_connection_pos = event_pos(&event);
fb9935cc
PP
755
756 /* Sink's accept port connection */
d94d92ac
PP
757 event.type = SINK_COMP_ACCEPT_INPUT_PORT_CONNECTION;
758 event.data.sink_comp_accept_input_port_connection.comp = gsink;
759 event.data.sink_comp_accept_input_port_connection.self_port = gsink_def_port;
760 event.data.sink_comp_accept_input_port_connection.other_port = gsrc_def_port;
fb9935cc 761 ok(has_event(&event), "got the expected sink's accept port connection event");
c14181ff
PP
762 sink_accept_port_connection_pos = event_pos(&event);
763
764 /* Source's port connected */
d94d92ac
PP
765 event.type = SRC_COMP_OUTPUT_PORT_CONNECTED;
766 event.data.src_comp_output_port_connected.comp = gsrc;
767 event.data.src_comp_output_port_connected.self_port = gsrc_def_port;
768 event.data.src_comp_output_port_connected.other_port = gsink_def_port;
c14181ff
PP
769 ok(has_event(&event), "got the expected source's port connected event");
770 src_port_connected_pos = event_pos(&event);
771
772 /* Sink's port connected */
d94d92ac
PP
773 event.type = SINK_COMP_INPUT_PORT_CONNECTED;
774 event.data.sink_comp_input_port_connected.comp = gsink;
775 event.data.sink_comp_input_port_connected.self_port = gsink_def_port;
776 event.data.sink_comp_input_port_connected.other_port = gsrc_def_port;
c14181ff
PP
777 ok(has_event(&event), "got the expected sink's port connected event");
778 sink_port_connected_pos = event_pos(&event);
fb9935cc 779
d94d92ac
PP
780 /* Graph's ports connected */
781 event.type = GRAPH_SRC_SINK_PORTS_CONNECTED;
782 event.data.graph_src_sink_ports_connected.upstream_comp = gsrc;
783 event.data.graph_src_sink_ports_connected.downstream_comp = gsink;
784 event.data.graph_src_sink_ports_connected.upstream_port = gsrc_def_port;
785 event.data.graph_src_sink_ports_connected.downstream_port = gsink_def_port;
fb9935cc
PP
786 ok(has_event(&event), "got the expected graph's ports connected event");
787 graph_ports_connected_pos = event_pos(&event);
788
789 /* Order of events */
c14181ff 790 ok(src_port_connected_pos < graph_ports_connected_pos,
fb9935cc 791 "event order is good (1)");
c14181ff 792 ok(sink_port_connected_pos < graph_ports_connected_pos,
fb9935cc 793 "event order is good (2)");
c14181ff
PP
794 ok(src_accept_port_connection_pos < src_port_connected_pos,
795 "event order is good (3)");
796 ok(sink_accept_port_connection_pos < sink_port_connected_pos,
797 "event order is good (4)");
fb9935cc 798
c5b9b441
PP
799 bt_component_sink_put_ref(sink);
800 bt_graph_put_ref(graph);
801 bt_component_source_put_ref(src);
fb9935cc
PP
802}
803
bf55043c
PP
804static
805void test_src_port_connected_error(void)
806{
b19ff26f
PP
807 const bt_component_source *src;
808 const bt_component_sink *sink;
809 const bt_component *gsrc;
810 const bt_component *gsink;
811 bt_graph *graph;
812 const bt_port_output *src_def_port;
813 const bt_port_input *sink_def_port;
814 const bt_port *gsrc_def_port;
815 const bt_port *gsink_def_port;
816 const bt_connection *conn = NULL;
bf55043c 817 struct event event;
4cdfc5e8 818 bt_graph_status status;
bf55043c
PP
819 size_t src_accept_port_connection_pos;
820 size_t src_port_connected_pos;
821
822 prepare_test(TEST_SRC_PORT_CONNECTED_ERROR, "port connected error: source");
823 graph = create_graph();
824 BT_ASSERT(graph);
825 src = create_src(graph);
826 sink = create_sink(graph);
0d72b8c3
PP
827 src_def_port = bt_component_source_borrow_output_port_by_name_const(src,
828 "out");
bf55043c 829 BT_ASSERT(src_def_port);
0d72b8c3
PP
830 sink_def_port = bt_component_sink_borrow_input_port_by_name_const(sink,
831 "in");
bf55043c 832 BT_ASSERT(sink_def_port);
0d72b8c3 833 status = bt_graph_connect_ports(graph, src_def_port,
d94d92ac 834 sink_def_port, &conn);
bf55043c 835 ok(status != BT_GRAPH_STATUS_OK,
0d72b8c3 836 "bt_graph_connect_ports() returns an error");
d94d92ac 837 ok(!conn, "returned connection is still NULL");
0d72b8c3
PP
838 gsrc = bt_component_source_as_component_const(src);
839 gsink = bt_component_sink_as_component_const(sink);
840 gsrc_def_port = bt_port_output_as_port_const(src_def_port);
841 gsink_def_port = bt_port_input_as_port_const(sink_def_port);
bf55043c
PP
842
843 /* We're supposed to have 5 events */
844 ok(events->len == 5, "we have the expected number of events");
845
846 /* Source's port added */
d94d92ac
PP
847 event.type = GRAPH_SRC_OUTPUT_PORT_ADDED;
848 event.data.graph_src_output_port_added.comp = gsrc;
849 event.data.graph_src_output_port_added.port = gsrc_def_port;
bf55043c
PP
850 ok(has_event(&event), "got the expected graph's port added event (for source, initial)");
851
852 /* Sink's port added */
d94d92ac
PP
853 event.type = GRAPH_SINK_INPUT_PORT_ADDED;
854 event.data.graph_sink_input_port_added.comp = gsink;
855 event.data.graph_sink_input_port_added.port = gsink_def_port;
bf55043c
PP
856 ok(has_event(&event), "got the expected graph's port added event (for sink, initial)");
857
858 /* Source's accept port connection */
d94d92ac
PP
859 event.type = SRC_COMP_ACCEPT_OUTPUT_PORT_CONNECTION;
860 event.data.src_comp_accept_output_port_connection.comp = gsrc;
861 event.data.src_comp_accept_output_port_connection.self_port = gsrc_def_port;
862 event.data.src_comp_accept_output_port_connection.other_port = gsink_def_port;
bf55043c
PP
863 ok(has_event(&event), "got the expected source's accept port connection event");
864 src_accept_port_connection_pos = event_pos(&event);
865
866 /* Sink's accept port connection */
d94d92ac
PP
867 event.type = SINK_COMP_ACCEPT_INPUT_PORT_CONNECTION;
868 event.data.sink_comp_accept_input_port_connection.comp = gsink;
869 event.data.sink_comp_accept_input_port_connection.self_port = gsink_def_port;
870 event.data.sink_comp_accept_input_port_connection.other_port = gsrc_def_port;
bf55043c
PP
871 ok(has_event(&event), "got the expected sink's accept port connection event");
872
873 /* Source's port connected */
d94d92ac
PP
874 event.type = SRC_COMP_OUTPUT_PORT_CONNECTED;
875 event.data.src_comp_output_port_connected.comp = gsrc;
876 event.data.src_comp_output_port_connected.self_port = gsrc_def_port;
877 event.data.src_comp_output_port_connected.other_port = gsink_def_port;
bf55043c
PP
878 ok(has_event(&event), "got the expected source's port connected event");
879 src_port_connected_pos = event_pos(&event);
880
881 /* Order of events */
882 ok(src_accept_port_connection_pos < src_port_connected_pos,
883 "event order is good (1)");
884
c5b9b441
PP
885 bt_graph_put_ref(graph);
886 bt_component_sink_put_ref(sink);
887 bt_component_source_put_ref(src);
888 bt_connection_put_ref(conn);
bf55043c
PP
889}
890
891static
892void test_sink_port_connected_error(void)
893{
b19ff26f
PP
894 const bt_component_source *src;
895 const bt_component_sink *sink;
896 const bt_component *gsrc;
897 const bt_component *gsink;
898 bt_graph *graph;
899 const bt_port_output *src_def_port;
900 const bt_port_input *sink_def_port;
901 const bt_port *gsrc_def_port;
902 const bt_port *gsink_def_port;
903 const bt_connection *conn = NULL;
bf55043c 904 struct event event;
4cdfc5e8 905 bt_graph_status status;
bf55043c
PP
906 size_t src_accept_port_connection_pos;
907 size_t sink_accept_port_connection_pos;
908 size_t src_port_connected_pos;
bf55043c
PP
909 size_t sink_port_connected_pos;
910
911 prepare_test(TEST_SINK_PORT_CONNECTED_ERROR, "port connected error: sink");
912 graph = create_graph();
913 BT_ASSERT(graph);
914 src = create_src(graph);
915 sink = create_sink(graph);
0d72b8c3
PP
916 src_def_port = bt_component_source_borrow_output_port_by_name_const(src,
917 "out");
bf55043c 918 BT_ASSERT(src_def_port);
0d72b8c3
PP
919 sink_def_port = bt_component_sink_borrow_input_port_by_name_const(sink,
920 "in");
bf55043c 921 BT_ASSERT(sink_def_port);
0d72b8c3 922 status = bt_graph_connect_ports(graph, src_def_port,
d94d92ac 923 sink_def_port, &conn);
bf55043c 924 ok(status != BT_GRAPH_STATUS_OK,
0d72b8c3 925 "bt_graph_connect_ports() returns an error");
d94d92ac 926 ok(!conn, "returned connection is still NULL");
0d72b8c3
PP
927 gsrc = bt_component_source_as_component_const(src);
928 gsink = bt_component_sink_as_component_const(sink);
929 gsrc_def_port = bt_port_output_as_port_const(src_def_port);
930 gsink_def_port = bt_port_input_as_port_const(sink_def_port);
bf55043c 931
4725a201
PP
932 /* We're supposed to have 6 events */
933 ok(events->len == 6, "we have the expected number of events");
bf55043c
PP
934
935 /* Source's port added */
d94d92ac
PP
936 event.type = GRAPH_SRC_OUTPUT_PORT_ADDED;
937 event.data.graph_src_output_port_added.comp = gsrc;
938 event.data.graph_src_output_port_added.port = gsrc_def_port;
bf55043c
PP
939 ok(has_event(&event), "got the expected graph's port added event (for source, initial)");
940
941 /* Sink's port added */
d94d92ac
PP
942 event.type = GRAPH_SINK_INPUT_PORT_ADDED;
943 event.data.graph_sink_input_port_added.comp = gsink;
944 event.data.graph_sink_input_port_added.port = gsink_def_port;
bf55043c
PP
945 ok(has_event(&event), "got the expected graph's port added event (for sink, initial)");
946
947 /* Source's accept port connection */
d94d92ac
PP
948 event.type = SRC_COMP_ACCEPT_OUTPUT_PORT_CONNECTION;
949 event.data.src_comp_accept_output_port_connection.comp = gsrc;
950 event.data.src_comp_accept_output_port_connection.self_port = gsrc_def_port;
951 event.data.src_comp_accept_output_port_connection.other_port = gsink_def_port;
bf55043c
PP
952 ok(has_event(&event), "got the expected source's accept port connection event");
953 src_accept_port_connection_pos = event_pos(&event);
954
955 /* Sink's accept port connection */
d94d92ac
PP
956 event.type = SINK_COMP_ACCEPT_INPUT_PORT_CONNECTION;
957 event.data.sink_comp_accept_input_port_connection.comp = gsink;
958 event.data.sink_comp_accept_input_port_connection.self_port = gsink_def_port;
959 event.data.sink_comp_accept_input_port_connection.other_port = gsrc_def_port;
bf55043c
PP
960 ok(has_event(&event), "got the expected sink's accept port connection event");
961 sink_accept_port_connection_pos = event_pos(&event);
962
963 /* Source's port connected */
d94d92ac
PP
964 event.type = SRC_COMP_OUTPUT_PORT_CONNECTED;
965 event.data.src_comp_output_port_connected.comp = gsrc;
966 event.data.src_comp_output_port_connected.self_port = gsrc_def_port;
967 event.data.src_comp_output_port_connected.other_port = gsink_def_port;
bf55043c
PP
968 ok(has_event(&event), "got the expected source's port connected event");
969 src_port_connected_pos = event_pos(&event);
970
971 /* Sink's port connected */
d94d92ac
PP
972 event.type = SINK_COMP_INPUT_PORT_CONNECTED;
973 event.data.sink_comp_input_port_connected.comp = gsink;
974 event.data.sink_comp_input_port_connected.self_port = gsink_def_port;
975 event.data.sink_comp_input_port_connected.other_port = gsrc_def_port;
bf55043c
PP
976 ok(has_event(&event), "got the expected sink's port connected event");
977 sink_port_connected_pos = event_pos(&event);
978
bf55043c
PP
979 /* Order of events */
980 ok(src_accept_port_connection_pos < src_port_connected_pos,
981 "event order is good (1)");
982 ok(sink_accept_port_connection_pos < sink_port_connected_pos,
983 "event order is good (2)");
bf55043c 984
c5b9b441
PP
985 bt_connection_put_ref(conn);
986 bt_graph_put_ref(graph);
987 bt_component_sink_put_ref(sink);
988 bt_component_source_put_ref(src);
bf55043c
PP
989}
990
fb9935cc
PP
991static
992void test_empty_graph(void)
993{
b19ff26f 994 bt_graph *graph;
fb9935cc
PP
995
996 prepare_test(TEST_EMPTY_GRAPH, "empty graph");
997 graph = create_graph();
998 ok(events->len == 0, "empty graph generates no events");
c5b9b441 999 bt_graph_put_ref(graph);
fb9935cc
PP
1000}
1001
1002int main(int argc, char **argv)
1003{
1004 plan_tests(NR_TESTS);
1005 init_test();
1006 test_empty_graph();
1007 test_simple();
bf55043c
PP
1008 test_src_port_connected_error();
1009 test_sink_port_connected_error();
c14181ff 1010 test_src_adds_port_in_port_connected();
fb9935cc
PP
1011 fini_test();
1012 return exit_status();
1013}
This page took 0.084881 seconds and 4 git commands to generate.