ae2da5aa72437074516ab215574ade6723de7734
[babeltrace.git] / tests / lib / test_graph_topo.c
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 33
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 GRAPH_SRC_SINK_PORTS_CONNECTED,
37 };
38
39 enum test {
40 TEST_EMPTY_GRAPH,
41 TEST_SIMPLE,
42 TEST_SRC_PORT_CONNECTED_ERROR,
43 TEST_SINK_PORT_CONNECTED_ERROR,
44 TEST_SRC_ADDS_PORT_IN_PORT_CONNECTED,
45 };
46
47 struct event {
48 enum event_type type;
49
50 union {
51 struct {
52 const bt_component *comp;
53 const bt_port *self_port;
54 const bt_port *other_port;
55 } src_comp_output_port_connected;
56
57 struct {
58 const bt_component *comp;
59 const bt_port *self_port;
60 const bt_port *other_port;
61 } sink_comp_input_port_connected;
62
63 struct {
64 const bt_component *comp;
65 const bt_port *port;
66 } graph_src_output_port_added;
67
68 struct {
69 const bt_component *comp;
70 const bt_port *port;
71 } graph_sink_input_port_added;
72
73 struct {
74 const bt_component *upstream_comp;
75 const bt_component *downstream_comp;
76 const bt_port *upstream_port;
77 const bt_port *downstream_port;
78 } graph_src_sink_ports_connected;
79 } data;
80 };
81
82 static GArray *events;
83 static bt_component_class_source *src_comp_class;
84 static bt_component_class_sink *sink_comp_class;
85 static enum test current_test;
86
87 static
88 void clear_events(void)
89 {
90 g_array_set_size(events, 0);
91 }
92
93 static
94 void append_event(struct event *event)
95 {
96 g_array_append_val(events, *event);
97 }
98
99 static
100 bool compare_events(struct event *ev_a, struct event *ev_b)
101 {
102 if (ev_a->type != ev_b->type) {
103 return false;
104 }
105
106 switch (ev_a->type) {
107 case SRC_COMP_OUTPUT_PORT_CONNECTED:
108 if (ev_a->data.src_comp_output_port_connected.comp !=
109 ev_b->data.src_comp_output_port_connected.comp) {
110 return false;
111 }
112
113 if (ev_a->data.src_comp_output_port_connected.self_port !=
114 ev_b->data.src_comp_output_port_connected.self_port) {
115 return false;
116 }
117
118 if (ev_a->data.src_comp_output_port_connected.other_port !=
119 ev_b->data.src_comp_output_port_connected.other_port) {
120 return false;
121 }
122 break;
123 case SINK_COMP_INPUT_PORT_CONNECTED:
124 if (ev_a->data.sink_comp_input_port_connected.comp !=
125 ev_b->data.sink_comp_input_port_connected.comp) {
126 return false;
127 }
128
129 if (ev_a->data.sink_comp_input_port_connected.self_port !=
130 ev_b->data.sink_comp_input_port_connected.self_port) {
131 return false;
132 }
133
134 if (ev_a->data.sink_comp_input_port_connected.other_port !=
135 ev_b->data.sink_comp_input_port_connected.other_port) {
136 return false;
137 }
138 break;
139 case GRAPH_SRC_OUTPUT_PORT_ADDED:
140 if (ev_a->data.graph_src_output_port_added.comp !=
141 ev_b->data.graph_src_output_port_added.comp) {
142 return false;
143 }
144
145 if (ev_a->data.graph_src_output_port_added.port !=
146 ev_b->data.graph_src_output_port_added.port) {
147 return false;
148 }
149 break;
150 case GRAPH_SINK_INPUT_PORT_ADDED:
151 if (ev_a->data.graph_sink_input_port_added.comp !=
152 ev_b->data.graph_sink_input_port_added.comp) {
153 return false;
154 }
155
156 if (ev_a->data.graph_sink_input_port_added.port !=
157 ev_b->data.graph_sink_input_port_added.port) {
158 return false;
159 }
160 break;
161 case GRAPH_SRC_SINK_PORTS_CONNECTED:
162 if (ev_a->data.graph_src_sink_ports_connected.upstream_comp !=
163 ev_b->data.graph_src_sink_ports_connected.upstream_comp) {
164 return false;
165 }
166
167 if (ev_a->data.graph_src_sink_ports_connected.downstream_comp !=
168 ev_b->data.graph_src_sink_ports_connected.downstream_comp) {
169 return false;
170 }
171
172 if (ev_a->data.graph_src_sink_ports_connected.upstream_port !=
173 ev_b->data.graph_src_sink_ports_connected.upstream_port) {
174 return false;
175 }
176
177 if (ev_a->data.graph_src_sink_ports_connected.downstream_port !=
178 ev_b->data.graph_src_sink_ports_connected.downstream_port) {
179 return false;
180 }
181 break;
182 default:
183 abort();
184 }
185
186 return true;
187 }
188
189 static
190 bool has_event(struct event *event)
191 {
192 size_t i;
193
194 for (i = 0; i < events->len; i++) {
195 struct event *ev = &g_array_index(events, struct event, i);
196
197 if (compare_events(event, ev)) {
198 return true;
199 }
200 }
201
202 return false;
203 }
204
205 static
206 size_t event_pos(struct event *event)
207 {
208 size_t i;
209
210 for (i = 0; i < events->len; i++) {
211 struct event *ev = &g_array_index(events, struct event, i);
212
213 if (compare_events(event, ev)) {
214 return i;
215 }
216 }
217
218 return SIZE_MAX;
219 }
220
221 static
222 bt_component_class_message_iterator_next_method_status src_iter_next(
223 bt_self_message_iterator *self_iterator,
224 bt_message_array_const msgs, uint64_t capacity,
225 uint64_t *count)
226 {
227 return BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_ERROR;
228 }
229
230 static
231 bt_component_class_port_connected_method_status src_output_port_connected(
232 bt_self_component_source *self_comp,
233 bt_self_component_port_output *self_comp_port,
234 const bt_port_input *other_port)
235 {
236 int ret;
237 struct event event = {
238 .type = SRC_COMP_OUTPUT_PORT_CONNECTED,
239 .data.src_comp_output_port_connected = {
240 .comp = bt_self_component_as_component(
241 bt_self_component_source_as_self_component(
242 self_comp)),
243 .self_port = bt_self_component_port_as_port(
244 bt_self_component_port_output_as_self_component_port(
245 self_comp_port)),
246 .other_port = bt_port_input_as_port_const(other_port),
247 },
248 };
249
250 append_event(&event);
251
252 switch (current_test) {
253 case TEST_SRC_ADDS_PORT_IN_PORT_CONNECTED:
254 ret = bt_self_component_source_add_output_port(
255 self_comp, "hello", NULL, NULL);
256 BT_ASSERT(ret == 0);
257 break;
258 case TEST_SRC_PORT_CONNECTED_ERROR:
259 return BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_ERROR;
260 default:
261 break;
262 }
263
264 return BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_OK;
265 }
266
267 static
268 bt_component_class_port_connected_method_status sink_input_port_connected(
269 bt_self_component_sink *self_comp,
270 bt_self_component_port_input *self_comp_port,
271 const bt_port_output *other_port)
272 {
273 struct event event = {
274 .type = SINK_COMP_INPUT_PORT_CONNECTED,
275 .data.sink_comp_input_port_connected = {
276 .comp = bt_self_component_as_component(
277 bt_self_component_sink_as_self_component(
278 self_comp)),
279 .self_port = bt_self_component_port_as_port(
280 bt_self_component_port_input_as_self_component_port(
281 self_comp_port)),
282 .other_port = bt_port_output_as_port_const(other_port),
283 },
284 };
285
286 append_event(&event);
287
288 if (current_test == TEST_SINK_PORT_CONNECTED_ERROR) {
289 return BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_ERROR;
290 } else {
291 return BT_COMPONENT_CLASS_PORT_CONNECTED_METHOD_STATUS_OK;
292 }
293 }
294
295 static
296 bt_component_class_init_method_status src_init(
297 bt_self_component_source *self_comp,
298 const bt_value *params, void *init_method_data)
299 {
300 int ret;
301
302 ret = bt_self_component_source_add_output_port(
303 self_comp, "out", NULL, NULL);
304 BT_ASSERT(ret == 0);
305 return BT_COMPONENT_CLASS_INIT_METHOD_STATUS_OK;
306 }
307
308 static
309 bt_component_class_init_method_status sink_init(
310 bt_self_component_sink *self_comp,
311 const bt_value *params, void *init_method_data)
312 {
313 int ret;
314
315 ret = bt_self_component_sink_add_input_port(self_comp,
316 "in", NULL, NULL);
317 BT_ASSERT(ret == 0);
318 return BT_COMPONENT_CLASS_INIT_METHOD_STATUS_OK;
319 }
320
321 static
322 bt_component_class_sink_consume_method_status sink_consume(
323 bt_self_component_sink *self_comp)
324 {
325 return BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK;
326 }
327
328 static
329 bt_graph_listener_func_status graph_src_output_port_added(
330 const bt_component_source *comp, const bt_port_output *port,
331 void *data)
332 {
333 struct event event = {
334 .type = GRAPH_SRC_OUTPUT_PORT_ADDED,
335 .data.graph_src_output_port_added = {
336 .comp = bt_component_source_as_component_const(comp),
337 .port = bt_port_output_as_port_const(port),
338 },
339 };
340
341 append_event(&event);
342
343 return BT_GRAPH_LISTENER_FUNC_STATUS_OK;
344 }
345
346 static
347 bt_graph_listener_func_status graph_sink_input_port_added(
348 const bt_component_sink *comp, const bt_port_input *port,
349 void *data)
350 {
351 struct event event = {
352 .type = GRAPH_SINK_INPUT_PORT_ADDED,
353 .data.graph_sink_input_port_added = {
354 .comp = bt_component_sink_as_component_const(comp),
355 .port = bt_port_input_as_port_const(port),
356 },
357 };
358
359 append_event(&event);
360
361 return BT_GRAPH_LISTENER_FUNC_STATUS_OK;
362 }
363
364 static
365 bt_graph_listener_func_status graph_src_sink_ports_connected(
366 const bt_component_source *upstream_comp,
367 const bt_component_sink *downstream_comp,
368 const bt_port_output *upstream_port,
369 const bt_port_input *downstream_port, void *data)
370 {
371 struct event event = {
372 .type = GRAPH_SRC_SINK_PORTS_CONNECTED,
373 .data.graph_src_sink_ports_connected = {
374 .upstream_comp =
375 bt_component_source_as_component_const(upstream_comp),
376 .downstream_comp =
377 bt_component_sink_as_component_const(downstream_comp),
378 .upstream_port =
379 bt_port_output_as_port_const(upstream_port),
380 .downstream_port =
381 bt_port_input_as_port_const(downstream_port),
382 },
383 };
384
385 append_event(&event);
386
387 return BT_GRAPH_LISTENER_FUNC_STATUS_OK;
388 }
389
390 static
391 void init_test(void)
392 {
393 int ret;
394
395 src_comp_class = bt_component_class_source_create(
396 "src", src_iter_next);
397 BT_ASSERT(src_comp_class);
398 ret = bt_component_class_source_set_init_method(
399 src_comp_class, src_init);
400 BT_ASSERT(ret == 0);
401 ret = bt_component_class_source_set_output_port_connected_method(
402 src_comp_class, src_output_port_connected);
403 BT_ASSERT(ret == 0);
404 sink_comp_class = bt_component_class_sink_create("sink",
405 sink_consume);
406 BT_ASSERT(sink_comp_class);
407 ret = bt_component_class_sink_set_init_method(sink_comp_class,
408 sink_init);
409 BT_ASSERT(ret == 0);
410 ret = bt_component_class_sink_set_input_port_connected_method(
411 sink_comp_class, sink_input_port_connected);
412 BT_ASSERT(ret == 0);
413 events = g_array_new(FALSE, TRUE, sizeof(struct event));
414 BT_ASSERT(events);
415 }
416
417 static
418 void fini_test(void)
419 {
420 bt_component_class_source_put_ref(src_comp_class);
421 bt_component_class_sink_put_ref(sink_comp_class);
422 g_array_free(events, TRUE);
423 }
424
425 static
426 const bt_component_source *create_src(bt_graph *graph)
427 {
428 const bt_component_source *comp;
429 int ret;
430
431 ret = bt_graph_add_source_component(graph, src_comp_class,
432 "src-comp", NULL, BT_LOGGING_LEVEL_NONE, &comp);
433 BT_ASSERT(ret == 0);
434 return comp;
435 }
436
437 static
438 const bt_component_sink *create_sink(bt_graph *graph)
439 {
440 const bt_component_sink *comp;
441 int ret;
442
443 ret = bt_graph_add_sink_component(graph, sink_comp_class,
444 "sink-comp", NULL, BT_LOGGING_LEVEL_NONE, &comp);
445 BT_ASSERT(ret == 0);
446 return comp;
447 }
448
449 static
450 bt_graph *create_graph(void)
451 {
452 bt_graph *graph = bt_graph_create(0);
453 int ret;
454
455 BT_ASSERT(graph);
456 ret = bt_graph_add_source_component_output_port_added_listener(
457 graph, graph_src_output_port_added, NULL, NULL, NULL);
458 BT_ASSERT(ret >= 0);
459 ret = bt_graph_add_sink_component_input_port_added_listener(
460 graph, graph_sink_input_port_added, NULL, NULL, NULL);
461 BT_ASSERT(ret >= 0);
462 ret = bt_graph_add_source_sink_component_ports_connected_listener(
463 graph, graph_src_sink_ports_connected, NULL, NULL, NULL);
464 BT_ASSERT(ret >= 0);
465 return graph;
466 }
467
468 static
469 void prepare_test(enum test test, const char *name)
470 {
471 clear_events();
472 current_test = test;
473 diag("test: %s", name);
474 }
475
476 static
477 void test_src_adds_port_in_port_connected(void)
478 {
479 const bt_component_source *src;
480 const bt_component_sink *sink;
481 const bt_component *gsrc;
482 const bt_component *gsink;
483 bt_graph *graph;
484 const bt_port_output *src_def_port;
485 const bt_port_output *src_hello_port;
486 const bt_port_input *sink_def_port;
487 const bt_port *gsrc_def_port;
488 const bt_port *gsrc_hello_port;
489 const bt_port *gsink_def_port;
490 struct event event;
491 bt_graph_connect_ports_status status;
492 size_t src_port_connected_pos;
493 size_t sink_port_connected_pos;
494 size_t graph_ports_connected_pos;
495 size_t graph_port_added_src_pos;
496
497 prepare_test(TEST_SRC_ADDS_PORT_IN_PORT_CONNECTED,
498 "source adds port in port connected");
499 graph = create_graph();
500 BT_ASSERT(graph);
501 src = create_src(graph);
502 sink = create_sink(graph);
503 src_def_port = bt_component_source_borrow_output_port_by_name_const(src,
504 "out");
505 BT_ASSERT(src_def_port);
506 sink_def_port = bt_component_sink_borrow_input_port_by_name_const(sink,
507 "in");
508 BT_ASSERT(sink_def_port);
509 status = bt_graph_connect_ports(graph, src_def_port,
510 sink_def_port, NULL);
511 BT_ASSERT(status == 0);
512 src_hello_port = bt_component_source_borrow_output_port_by_name_const(src,
513 "hello");
514 BT_ASSERT(src_hello_port);
515 gsrc = bt_component_source_as_component_const(src);
516 gsink = bt_component_sink_as_component_const(sink);
517 gsrc_def_port = bt_port_output_as_port_const(src_def_port);
518 gsrc_hello_port = bt_port_output_as_port_const(src_hello_port);
519 gsink_def_port = bt_port_input_as_port_const(sink_def_port);
520
521 /* We're supposed to have 6 events */
522 ok(events->len == 6, "we have the expected number of events");
523
524 /* Source's port added */
525 event.type = GRAPH_SRC_OUTPUT_PORT_ADDED;
526 event.data.graph_src_output_port_added.comp = gsrc;
527 event.data.graph_src_output_port_added.port = gsrc_def_port;
528 ok(has_event(&event), "got the expected graph's port added event (for source, initial)");
529
530 /* Sink's port added */
531 event.type = GRAPH_SINK_INPUT_PORT_ADDED;
532 event.data.graph_sink_input_port_added.comp = gsink;
533 event.data.graph_sink_input_port_added.port = gsink_def_port;
534 ok(has_event(&event), "got the expected graph's port added event (for sink, initial)");
535
536 /* Source's port connected */
537 event.type = SRC_COMP_OUTPUT_PORT_CONNECTED;
538 event.data.src_comp_output_port_connected.comp = gsrc;
539 event.data.src_comp_output_port_connected.self_port = gsrc_def_port;
540 event.data.src_comp_output_port_connected.other_port = gsink_def_port;
541 ok(has_event(&event), "got the expected source's port connected event");
542 src_port_connected_pos = event_pos(&event);
543
544 /* Graph's port added (source) */
545 event.type = GRAPH_SRC_OUTPUT_PORT_ADDED;
546 event.data.graph_src_output_port_added.comp = gsrc;
547 event.data.graph_src_output_port_added.port = gsrc_hello_port;
548 ok(has_event(&event), "got the expected graph's port added event (for source)");
549 graph_port_added_src_pos = event_pos(&event);
550
551 /* Sink's port connected */
552 event.type = SINK_COMP_INPUT_PORT_CONNECTED;
553 event.data.sink_comp_input_port_connected.comp = gsink;
554 event.data.sink_comp_input_port_connected.self_port = gsink_def_port;
555 event.data.sink_comp_input_port_connected.other_port = gsrc_def_port;
556 ok(has_event(&event), "got the expected sink's port connected event");
557 sink_port_connected_pos = event_pos(&event);
558
559 /* Graph's ports connected */
560 event.type = GRAPH_SRC_SINK_PORTS_CONNECTED;
561 event.data.graph_src_sink_ports_connected.upstream_comp = gsrc;
562 event.data.graph_src_sink_ports_connected.downstream_comp = gsink;
563 event.data.graph_src_sink_ports_connected.upstream_port = gsrc_def_port;
564 event.data.graph_src_sink_ports_connected.downstream_port = gsink_def_port;
565 ok(has_event(&event), "got the expected graph's ports connected event");
566 graph_ports_connected_pos = event_pos(&event);
567
568 /* Order of events */
569 ok(src_port_connected_pos < graph_ports_connected_pos,
570 "event order is good (1)");
571 ok(sink_port_connected_pos < graph_ports_connected_pos,
572 "event order is good (2)");
573 ok(src_port_connected_pos < graph_port_added_src_pos,
574 "event order is good (3)");
575 ok(graph_port_added_src_pos < graph_ports_connected_pos,
576 "event order is good (4)");
577
578 bt_component_source_put_ref(src);
579 bt_component_sink_put_ref(sink);
580 bt_graph_put_ref(graph);
581 }
582
583 static
584 void test_simple(void)
585 {
586 const bt_component_source *src;
587 const bt_component_sink *sink;
588 const bt_component *gsrc;
589 const bt_component *gsink;
590 bt_graph *graph;
591 const bt_port_output *src_def_port;
592 const bt_port_input *sink_def_port;
593 const bt_port *gsrc_def_port;
594 const bt_port *gsink_def_port;
595 struct event event;
596 bt_graph_connect_ports_status status;
597 size_t src_port_connected_pos;
598 size_t sink_port_connected_pos;
599 size_t graph_ports_connected_pos;
600
601 prepare_test(TEST_SIMPLE, "simple");
602 graph = create_graph();
603 BT_ASSERT(graph);
604 src = create_src(graph);
605 sink = create_sink(graph);
606 src_def_port = bt_component_source_borrow_output_port_by_name_const(src,
607 "out");
608 BT_ASSERT(src_def_port);
609 sink_def_port = bt_component_sink_borrow_input_port_by_name_const(sink,
610 "in");
611 BT_ASSERT(sink_def_port);
612 status = bt_graph_connect_ports(graph, src_def_port,
613 sink_def_port, NULL);
614 BT_ASSERT(status == 0);
615 gsrc = bt_component_source_as_component_const(src);
616 gsink = bt_component_sink_as_component_const(sink);
617 gsrc_def_port = bt_port_output_as_port_const(src_def_port);
618 gsink_def_port = bt_port_input_as_port_const(sink_def_port);
619
620 /* We're supposed to have 5 events */
621 ok(events->len == 5, "we have the expected number of events");
622
623 /* Source's port added */
624 event.type = GRAPH_SRC_OUTPUT_PORT_ADDED;
625 event.data.graph_src_output_port_added.comp = gsrc;
626 event.data.graph_src_output_port_added.port = gsrc_def_port;
627 ok(has_event(&event), "got the expected graph's port added event (for source, initial)");
628
629 /* Sink's port added */
630 event.type = GRAPH_SINK_INPUT_PORT_ADDED;
631 event.data.graph_sink_input_port_added.comp = gsink;
632 event.data.graph_sink_input_port_added.port = gsink_def_port;
633 ok(has_event(&event), "got the expected graph's port added event (for sink, initial)");
634
635 /* Source's port connected */
636 event.type = SRC_COMP_OUTPUT_PORT_CONNECTED;
637 event.data.src_comp_output_port_connected.comp = gsrc;
638 event.data.src_comp_output_port_connected.self_port = gsrc_def_port;
639 event.data.src_comp_output_port_connected.other_port = gsink_def_port;
640 ok(has_event(&event), "got the expected source's port connected event");
641 src_port_connected_pos = event_pos(&event);
642
643 /* Sink's port connected */
644 event.type = SINK_COMP_INPUT_PORT_CONNECTED;
645 event.data.sink_comp_input_port_connected.comp = gsink;
646 event.data.sink_comp_input_port_connected.self_port = gsink_def_port;
647 event.data.sink_comp_input_port_connected.other_port = gsrc_def_port;
648 ok(has_event(&event), "got the expected sink's port connected event");
649 sink_port_connected_pos = event_pos(&event);
650
651 /* Graph's ports connected */
652 event.type = GRAPH_SRC_SINK_PORTS_CONNECTED;
653 event.data.graph_src_sink_ports_connected.upstream_comp = gsrc;
654 event.data.graph_src_sink_ports_connected.downstream_comp = gsink;
655 event.data.graph_src_sink_ports_connected.upstream_port = gsrc_def_port;
656 event.data.graph_src_sink_ports_connected.downstream_port = gsink_def_port;
657 ok(has_event(&event), "got the expected graph's ports connected event");
658 graph_ports_connected_pos = event_pos(&event);
659
660 /* Order of events */
661 ok(src_port_connected_pos < graph_ports_connected_pos,
662 "event order is good (1)");
663 ok(sink_port_connected_pos < graph_ports_connected_pos,
664 "event order is good (2)");
665
666 bt_component_sink_put_ref(sink);
667 bt_graph_put_ref(graph);
668 bt_component_source_put_ref(src);
669 }
670
671 static
672 void test_src_port_connected_error(void)
673 {
674 const bt_component_source *src;
675 const bt_component_sink *sink;
676 const bt_component *gsrc;
677 const bt_component *gsink;
678 bt_graph *graph;
679 const bt_port_output *src_def_port;
680 const bt_port_input *sink_def_port;
681 const bt_port *gsrc_def_port;
682 const bt_port *gsink_def_port;
683 const bt_connection *conn = NULL;
684 struct event event;
685 bt_graph_connect_ports_status status;
686
687 prepare_test(TEST_SRC_PORT_CONNECTED_ERROR, "port connected error: source");
688 graph = create_graph();
689 BT_ASSERT(graph);
690 src = create_src(graph);
691 sink = create_sink(graph);
692 src_def_port = bt_component_source_borrow_output_port_by_name_const(src,
693 "out");
694 BT_ASSERT(src_def_port);
695 sink_def_port = bt_component_sink_borrow_input_port_by_name_const(sink,
696 "in");
697 BT_ASSERT(sink_def_port);
698 status = bt_graph_connect_ports(graph, src_def_port,
699 sink_def_port, &conn);
700 ok(status != BT_GRAPH_CONNECT_PORTS_STATUS_OK,
701 "bt_graph_connect_ports() returns an error");
702 bt_current_thread_clear_error();
703 ok(!conn, "returned connection is still NULL");
704 gsrc = bt_component_source_as_component_const(src);
705 gsink = bt_component_sink_as_component_const(sink);
706 gsrc_def_port = bt_port_output_as_port_const(src_def_port);
707 gsink_def_port = bt_port_input_as_port_const(sink_def_port);
708
709 /* We're supposed to have 3 events */
710 ok(events->len == 3, "we have the expected number of events");
711
712 /* Source's port added */
713 event.type = GRAPH_SRC_OUTPUT_PORT_ADDED;
714 event.data.graph_src_output_port_added.comp = gsrc;
715 event.data.graph_src_output_port_added.port = gsrc_def_port;
716 ok(has_event(&event), "got the expected graph's port added event (for source, initial)");
717
718 /* Sink's port added */
719 event.type = GRAPH_SINK_INPUT_PORT_ADDED;
720 event.data.graph_sink_input_port_added.comp = gsink;
721 event.data.graph_sink_input_port_added.port = gsink_def_port;
722 ok(has_event(&event), "got the expected graph's port added event (for sink, initial)");
723
724 /* Source's port connected */
725 event.type = SRC_COMP_OUTPUT_PORT_CONNECTED;
726 event.data.src_comp_output_port_connected.comp = gsrc;
727 event.data.src_comp_output_port_connected.self_port = gsrc_def_port;
728 event.data.src_comp_output_port_connected.other_port = gsink_def_port;
729 ok(has_event(&event), "got the expected source's port connected event");
730
731 bt_graph_put_ref(graph);
732 bt_component_sink_put_ref(sink);
733 bt_component_source_put_ref(src);
734 bt_connection_put_ref(conn);
735 }
736
737 static
738 void test_sink_port_connected_error(void)
739 {
740 const bt_component_source *src;
741 const bt_component_sink *sink;
742 const bt_component *gsrc;
743 const bt_component *gsink;
744 bt_graph *graph;
745 const bt_port_output *src_def_port;
746 const bt_port_input *sink_def_port;
747 const bt_port *gsrc_def_port;
748 const bt_port *gsink_def_port;
749 const bt_connection *conn = NULL;
750 struct event event;
751 bt_graph_connect_ports_status status;
752
753 prepare_test(TEST_SINK_PORT_CONNECTED_ERROR, "port connected error: sink");
754 graph = create_graph();
755 BT_ASSERT(graph);
756 src = create_src(graph);
757 sink = create_sink(graph);
758 src_def_port = bt_component_source_borrow_output_port_by_name_const(src,
759 "out");
760 BT_ASSERT(src_def_port);
761 sink_def_port = bt_component_sink_borrow_input_port_by_name_const(sink,
762 "in");
763 BT_ASSERT(sink_def_port);
764 status = bt_graph_connect_ports(graph, src_def_port,
765 sink_def_port, &conn);
766 ok(status != BT_GRAPH_CONNECT_PORTS_STATUS_OK,
767 "bt_graph_connect_ports() returns an error");
768 bt_current_thread_clear_error();
769 ok(!conn, "returned connection is still NULL");
770 gsrc = bt_component_source_as_component_const(src);
771 gsink = bt_component_sink_as_component_const(sink);
772 gsrc_def_port = bt_port_output_as_port_const(src_def_port);
773 gsink_def_port = bt_port_input_as_port_const(sink_def_port);
774
775 /* We're supposed to have 4 events */
776 ok(events->len == 4, "we have the expected number of events");
777
778 /* Source's port added */
779 event.type = GRAPH_SRC_OUTPUT_PORT_ADDED;
780 event.data.graph_src_output_port_added.comp = gsrc;
781 event.data.graph_src_output_port_added.port = gsrc_def_port;
782 ok(has_event(&event), "got the expected graph's port added event (for source, initial)");
783
784 /* Sink's port added */
785 event.type = GRAPH_SINK_INPUT_PORT_ADDED;
786 event.data.graph_sink_input_port_added.comp = gsink;
787 event.data.graph_sink_input_port_added.port = gsink_def_port;
788 ok(has_event(&event), "got the expected graph's port added event (for sink, initial)");
789
790 /* Source's port connected */
791 event.type = SRC_COMP_OUTPUT_PORT_CONNECTED;
792 event.data.src_comp_output_port_connected.comp = gsrc;
793 event.data.src_comp_output_port_connected.self_port = gsrc_def_port;
794 event.data.src_comp_output_port_connected.other_port = gsink_def_port;
795 ok(has_event(&event), "got the expected source's port connected event");
796
797 /* Sink's port connected */
798 event.type = SINK_COMP_INPUT_PORT_CONNECTED;
799 event.data.sink_comp_input_port_connected.comp = gsink;
800 event.data.sink_comp_input_port_connected.self_port = gsink_def_port;
801 event.data.sink_comp_input_port_connected.other_port = gsrc_def_port;
802 ok(has_event(&event), "got the expected sink's port connected event");
803
804 bt_connection_put_ref(conn);
805 bt_graph_put_ref(graph);
806 bt_component_sink_put_ref(sink);
807 bt_component_source_put_ref(src);
808 }
809
810 static
811 void test_empty_graph(void)
812 {
813 bt_graph *graph;
814
815 prepare_test(TEST_EMPTY_GRAPH, "empty graph");
816 graph = create_graph();
817 ok(events->len == 0, "empty graph generates no events");
818 bt_graph_put_ref(graph);
819 }
820
821 int main(int argc, char **argv)
822 {
823 plan_tests(NR_TESTS);
824 init_test();
825 test_empty_graph();
826 test_simple();
827 test_src_port_connected_error();
828 test_sink_port_connected_error();
829 test_src_adds_port_in_port_connected();
830 fini_test();
831 return exit_status();
832 }
This page took 0.046732 seconds and 3 git commands to generate.