lib: pass config objects to component init methods
[babeltrace.git] / tests / lib / test_graph_topo.c
CommitLineData
21d04293
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
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
31enum 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
39enum 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
47struct 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
82static GArray *events;
83static bt_component_class_source *src_comp_class;
84static bt_component_class_sink *sink_comp_class;
85static enum test current_test;
86
87static
88void clear_events(void)
89{
90 g_array_set_size(events, 0);
91}
92
93static
94void append_event(struct event *event)
95{
96 g_array_append_val(events, *event);
97}
98
99static
100bool 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
189static
190bool 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
205static
206size_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
221static
222bt_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
230static
231bt_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
267static
268bt_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
295static
296bt_component_class_init_method_status src_init(
297 bt_self_component_source *self_comp,
59225a3e 298 bt_self_component_source_configuration *config,
21d04293
PP
299 const bt_value *params, void *init_method_data)
300{
301 int ret;
302
303 ret = bt_self_component_source_add_output_port(
304 self_comp, "out", NULL, NULL);
305 BT_ASSERT(ret == 0);
306 return BT_COMPONENT_CLASS_INIT_METHOD_STATUS_OK;
307}
308
309static
310bt_component_class_init_method_status sink_init(
311 bt_self_component_sink *self_comp,
59225a3e 312 bt_self_component_sink_configuration *config,
21d04293
PP
313 const bt_value *params, void *init_method_data)
314{
315 int ret;
316
317 ret = bt_self_component_sink_add_input_port(self_comp,
318 "in", NULL, NULL);
319 BT_ASSERT(ret == 0);
320 return BT_COMPONENT_CLASS_INIT_METHOD_STATUS_OK;
321}
322
323static
324bt_component_class_sink_consume_method_status sink_consume(
325 bt_self_component_sink *self_comp)
326{
327 return BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK;
328}
329
330static
331bt_graph_listener_func_status graph_src_output_port_added(
332 const bt_component_source *comp, const bt_port_output *port,
333 void *data)
334{
335 struct event event = {
336 .type = GRAPH_SRC_OUTPUT_PORT_ADDED,
337 .data.graph_src_output_port_added = {
338 .comp = bt_component_source_as_component_const(comp),
339 .port = bt_port_output_as_port_const(port),
340 },
341 };
342
343 append_event(&event);
344
345 return BT_GRAPH_LISTENER_FUNC_STATUS_OK;
346}
347
348static
349bt_graph_listener_func_status graph_sink_input_port_added(
350 const bt_component_sink *comp, const bt_port_input *port,
351 void *data)
352{
353 struct event event = {
354 .type = GRAPH_SINK_INPUT_PORT_ADDED,
355 .data.graph_sink_input_port_added = {
356 .comp = bt_component_sink_as_component_const(comp),
357 .port = bt_port_input_as_port_const(port),
358 },
359 };
360
361 append_event(&event);
362
363 return BT_GRAPH_LISTENER_FUNC_STATUS_OK;
364}
365
366static
367bt_graph_listener_func_status graph_src_sink_ports_connected(
368 const bt_component_source *upstream_comp,
369 const bt_component_sink *downstream_comp,
370 const bt_port_output *upstream_port,
371 const bt_port_input *downstream_port, void *data)
372{
373 struct event event = {
374 .type = GRAPH_SRC_SINK_PORTS_CONNECTED,
375 .data.graph_src_sink_ports_connected = {
376 .upstream_comp =
377 bt_component_source_as_component_const(upstream_comp),
378 .downstream_comp =
379 bt_component_sink_as_component_const(downstream_comp),
380 .upstream_port =
381 bt_port_output_as_port_const(upstream_port),
382 .downstream_port =
383 bt_port_input_as_port_const(downstream_port),
384 },
385 };
386
387 append_event(&event);
388
389 return BT_GRAPH_LISTENER_FUNC_STATUS_OK;
390}
391
392static
393void init_test(void)
394{
395 int ret;
396
397 src_comp_class = bt_component_class_source_create(
398 "src", src_iter_next);
399 BT_ASSERT(src_comp_class);
400 ret = bt_component_class_source_set_init_method(
401 src_comp_class, src_init);
402 BT_ASSERT(ret == 0);
403 ret = bt_component_class_source_set_output_port_connected_method(
404 src_comp_class, src_output_port_connected);
405 BT_ASSERT(ret == 0);
406 sink_comp_class = bt_component_class_sink_create("sink",
407 sink_consume);
408 BT_ASSERT(sink_comp_class);
409 ret = bt_component_class_sink_set_init_method(sink_comp_class,
410 sink_init);
411 BT_ASSERT(ret == 0);
412 ret = bt_component_class_sink_set_input_port_connected_method(
413 sink_comp_class, sink_input_port_connected);
414 BT_ASSERT(ret == 0);
415 events = g_array_new(FALSE, TRUE, sizeof(struct event));
416 BT_ASSERT(events);
417}
418
419static
420void fini_test(void)
421{
422 bt_component_class_source_put_ref(src_comp_class);
423 bt_component_class_sink_put_ref(sink_comp_class);
424 g_array_free(events, TRUE);
425}
426
427static
428const bt_component_source *create_src(bt_graph *graph)
429{
430 const bt_component_source *comp;
431 int ret;
432
433 ret = bt_graph_add_source_component(graph, src_comp_class,
434 "src-comp", NULL, BT_LOGGING_LEVEL_NONE, &comp);
435 BT_ASSERT(ret == 0);
436 return comp;
437}
438
439static
440const bt_component_sink *create_sink(bt_graph *graph)
441{
442 const bt_component_sink *comp;
443 int ret;
444
445 ret = bt_graph_add_sink_component(graph, sink_comp_class,
446 "sink-comp", NULL, BT_LOGGING_LEVEL_NONE, &comp);
447 BT_ASSERT(ret == 0);
448 return comp;
449}
450
451static
452bt_graph *create_graph(void)
453{
056deb59 454 bt_graph *graph = bt_graph_create(0);
21d04293
PP
455 int ret;
456
457 BT_ASSERT(graph);
458 ret = bt_graph_add_source_component_output_port_added_listener(
459 graph, graph_src_output_port_added, NULL, NULL, NULL);
460 BT_ASSERT(ret >= 0);
461 ret = bt_graph_add_sink_component_input_port_added_listener(
462 graph, graph_sink_input_port_added, NULL, NULL, NULL);
463 BT_ASSERT(ret >= 0);
464 ret = bt_graph_add_source_sink_component_ports_connected_listener(
465 graph, graph_src_sink_ports_connected, NULL, NULL, NULL);
466 BT_ASSERT(ret >= 0);
467 return graph;
468}
469
470static
471void prepare_test(enum test test, const char *name)
472{
473 clear_events();
474 current_test = test;
475 diag("test: %s", name);
476}
477
478static
479void test_src_adds_port_in_port_connected(void)
480{
481 const bt_component_source *src;
482 const bt_component_sink *sink;
483 const bt_component *gsrc;
484 const bt_component *gsink;
485 bt_graph *graph;
486 const bt_port_output *src_def_port;
487 const bt_port_output *src_hello_port;
488 const bt_port_input *sink_def_port;
489 const bt_port *gsrc_def_port;
490 const bt_port *gsrc_hello_port;
491 const bt_port *gsink_def_port;
492 struct event event;
493 bt_graph_connect_ports_status status;
494 size_t src_port_connected_pos;
495 size_t sink_port_connected_pos;
496 size_t graph_ports_connected_pos;
497 size_t graph_port_added_src_pos;
498
499 prepare_test(TEST_SRC_ADDS_PORT_IN_PORT_CONNECTED,
500 "source adds port in port connected");
501 graph = create_graph();
502 BT_ASSERT(graph);
503 src = create_src(graph);
504 sink = create_sink(graph);
505 src_def_port = bt_component_source_borrow_output_port_by_name_const(src,
506 "out");
507 BT_ASSERT(src_def_port);
508 sink_def_port = bt_component_sink_borrow_input_port_by_name_const(sink,
509 "in");
510 BT_ASSERT(sink_def_port);
511 status = bt_graph_connect_ports(graph, src_def_port,
512 sink_def_port, NULL);
513 BT_ASSERT(status == 0);
514 src_hello_port = bt_component_source_borrow_output_port_by_name_const(src,
515 "hello");
516 BT_ASSERT(src_hello_port);
517 gsrc = bt_component_source_as_component_const(src);
518 gsink = bt_component_sink_as_component_const(sink);
519 gsrc_def_port = bt_port_output_as_port_const(src_def_port);
520 gsrc_hello_port = bt_port_output_as_port_const(src_hello_port);
521 gsink_def_port = bt_port_input_as_port_const(sink_def_port);
522
523 /* We're supposed to have 6 events */
524 ok(events->len == 6, "we have the expected number of events");
525
526 /* Source's port added */
527 event.type = GRAPH_SRC_OUTPUT_PORT_ADDED;
528 event.data.graph_src_output_port_added.comp = gsrc;
529 event.data.graph_src_output_port_added.port = gsrc_def_port;
530 ok(has_event(&event), "got the expected graph's port added event (for source, initial)");
531
532 /* Sink's port added */
533 event.type = GRAPH_SINK_INPUT_PORT_ADDED;
534 event.data.graph_sink_input_port_added.comp = gsink;
535 event.data.graph_sink_input_port_added.port = gsink_def_port;
536 ok(has_event(&event), "got the expected graph's port added event (for sink, initial)");
537
538 /* Source's port connected */
539 event.type = SRC_COMP_OUTPUT_PORT_CONNECTED;
540 event.data.src_comp_output_port_connected.comp = gsrc;
541 event.data.src_comp_output_port_connected.self_port = gsrc_def_port;
542 event.data.src_comp_output_port_connected.other_port = gsink_def_port;
543 ok(has_event(&event), "got the expected source's port connected event");
544 src_port_connected_pos = event_pos(&event);
545
546 /* Graph's port added (source) */
547 event.type = GRAPH_SRC_OUTPUT_PORT_ADDED;
548 event.data.graph_src_output_port_added.comp = gsrc;
549 event.data.graph_src_output_port_added.port = gsrc_hello_port;
550 ok(has_event(&event), "got the expected graph's port added event (for source)");
551 graph_port_added_src_pos = event_pos(&event);
552
553 /* Sink's port connected */
554 event.type = SINK_COMP_INPUT_PORT_CONNECTED;
555 event.data.sink_comp_input_port_connected.comp = gsink;
556 event.data.sink_comp_input_port_connected.self_port = gsink_def_port;
557 event.data.sink_comp_input_port_connected.other_port = gsrc_def_port;
558 ok(has_event(&event), "got the expected sink's port connected event");
559 sink_port_connected_pos = event_pos(&event);
560
561 /* Graph's ports connected */
562 event.type = GRAPH_SRC_SINK_PORTS_CONNECTED;
563 event.data.graph_src_sink_ports_connected.upstream_comp = gsrc;
564 event.data.graph_src_sink_ports_connected.downstream_comp = gsink;
565 event.data.graph_src_sink_ports_connected.upstream_port = gsrc_def_port;
566 event.data.graph_src_sink_ports_connected.downstream_port = gsink_def_port;
567 ok(has_event(&event), "got the expected graph's ports connected event");
568 graph_ports_connected_pos = event_pos(&event);
569
570 /* Order of events */
571 ok(src_port_connected_pos < graph_ports_connected_pos,
572 "event order is good (1)");
573 ok(sink_port_connected_pos < graph_ports_connected_pos,
574 "event order is good (2)");
575 ok(src_port_connected_pos < graph_port_added_src_pos,
576 "event order is good (3)");
577 ok(graph_port_added_src_pos < graph_ports_connected_pos,
578 "event order is good (4)");
579
580 bt_component_source_put_ref(src);
581 bt_component_sink_put_ref(sink);
582 bt_graph_put_ref(graph);
583}
584
585static
586void test_simple(void)
587{
588 const bt_component_source *src;
589 const bt_component_sink *sink;
590 const bt_component *gsrc;
591 const bt_component *gsink;
592 bt_graph *graph;
593 const bt_port_output *src_def_port;
594 const bt_port_input *sink_def_port;
595 const bt_port *gsrc_def_port;
596 const bt_port *gsink_def_port;
597 struct event event;
598 bt_graph_connect_ports_status status;
599 size_t src_port_connected_pos;
600 size_t sink_port_connected_pos;
601 size_t graph_ports_connected_pos;
602
603 prepare_test(TEST_SIMPLE, "simple");
604 graph = create_graph();
605 BT_ASSERT(graph);
606 src = create_src(graph);
607 sink = create_sink(graph);
608 src_def_port = bt_component_source_borrow_output_port_by_name_const(src,
609 "out");
610 BT_ASSERT(src_def_port);
611 sink_def_port = bt_component_sink_borrow_input_port_by_name_const(sink,
612 "in");
613 BT_ASSERT(sink_def_port);
614 status = bt_graph_connect_ports(graph, src_def_port,
615 sink_def_port, NULL);
616 BT_ASSERT(status == 0);
617 gsrc = bt_component_source_as_component_const(src);
618 gsink = bt_component_sink_as_component_const(sink);
619 gsrc_def_port = bt_port_output_as_port_const(src_def_port);
620 gsink_def_port = bt_port_input_as_port_const(sink_def_port);
621
622 /* We're supposed to have 5 events */
623 ok(events->len == 5, "we have the expected number of events");
624
625 /* Source's port added */
626 event.type = GRAPH_SRC_OUTPUT_PORT_ADDED;
627 event.data.graph_src_output_port_added.comp = gsrc;
628 event.data.graph_src_output_port_added.port = gsrc_def_port;
629 ok(has_event(&event), "got the expected graph's port added event (for source, initial)");
630
631 /* Sink's port added */
632 event.type = GRAPH_SINK_INPUT_PORT_ADDED;
633 event.data.graph_sink_input_port_added.comp = gsink;
634 event.data.graph_sink_input_port_added.port = gsink_def_port;
635 ok(has_event(&event), "got the expected graph's port added event (for sink, initial)");
636
637 /* Source's port connected */
638 event.type = SRC_COMP_OUTPUT_PORT_CONNECTED;
639 event.data.src_comp_output_port_connected.comp = gsrc;
640 event.data.src_comp_output_port_connected.self_port = gsrc_def_port;
641 event.data.src_comp_output_port_connected.other_port = gsink_def_port;
642 ok(has_event(&event), "got the expected source's port connected event");
643 src_port_connected_pos = event_pos(&event);
644
645 /* Sink's port connected */
646 event.type = SINK_COMP_INPUT_PORT_CONNECTED;
647 event.data.sink_comp_input_port_connected.comp = gsink;
648 event.data.sink_comp_input_port_connected.self_port = gsink_def_port;
649 event.data.sink_comp_input_port_connected.other_port = gsrc_def_port;
650 ok(has_event(&event), "got the expected sink's port connected event");
651 sink_port_connected_pos = event_pos(&event);
652
653 /* Graph's ports connected */
654 event.type = GRAPH_SRC_SINK_PORTS_CONNECTED;
655 event.data.graph_src_sink_ports_connected.upstream_comp = gsrc;
656 event.data.graph_src_sink_ports_connected.downstream_comp = gsink;
657 event.data.graph_src_sink_ports_connected.upstream_port = gsrc_def_port;
658 event.data.graph_src_sink_ports_connected.downstream_port = gsink_def_port;
659 ok(has_event(&event), "got the expected graph's ports connected event");
660 graph_ports_connected_pos = event_pos(&event);
661
662 /* Order of events */
663 ok(src_port_connected_pos < graph_ports_connected_pos,
664 "event order is good (1)");
665 ok(sink_port_connected_pos < graph_ports_connected_pos,
666 "event order is good (2)");
667
668 bt_component_sink_put_ref(sink);
669 bt_graph_put_ref(graph);
670 bt_component_source_put_ref(src);
671}
672
673static
674void test_src_port_connected_error(void)
675{
676 const bt_component_source *src;
677 const bt_component_sink *sink;
678 const bt_component *gsrc;
679 const bt_component *gsink;
680 bt_graph *graph;
681 const bt_port_output *src_def_port;
682 const bt_port_input *sink_def_port;
683 const bt_port *gsrc_def_port;
684 const bt_port *gsink_def_port;
685 const bt_connection *conn = NULL;
686 struct event event;
687 bt_graph_connect_ports_status status;
688
689 prepare_test(TEST_SRC_PORT_CONNECTED_ERROR, "port connected error: source");
690 graph = create_graph();
691 BT_ASSERT(graph);
692 src = create_src(graph);
693 sink = create_sink(graph);
694 src_def_port = bt_component_source_borrow_output_port_by_name_const(src,
695 "out");
696 BT_ASSERT(src_def_port);
697 sink_def_port = bt_component_sink_borrow_input_port_by_name_const(sink,
698 "in");
699 BT_ASSERT(sink_def_port);
700 status = bt_graph_connect_ports(graph, src_def_port,
701 sink_def_port, &conn);
702 ok(status != BT_GRAPH_CONNECT_PORTS_STATUS_OK,
703 "bt_graph_connect_ports() returns an error");
758932ce 704 bt_current_thread_clear_error();
21d04293
PP
705 ok(!conn, "returned connection is still NULL");
706 gsrc = bt_component_source_as_component_const(src);
707 gsink = bt_component_sink_as_component_const(sink);
708 gsrc_def_port = bt_port_output_as_port_const(src_def_port);
709 gsink_def_port = bt_port_input_as_port_const(sink_def_port);
710
711 /* We're supposed to have 3 events */
712 ok(events->len == 3, "we have the expected number of events");
713
714 /* Source's port added */
715 event.type = GRAPH_SRC_OUTPUT_PORT_ADDED;
716 event.data.graph_src_output_port_added.comp = gsrc;
717 event.data.graph_src_output_port_added.port = gsrc_def_port;
718 ok(has_event(&event), "got the expected graph's port added event (for source, initial)");
719
720 /* Sink's port added */
721 event.type = GRAPH_SINK_INPUT_PORT_ADDED;
722 event.data.graph_sink_input_port_added.comp = gsink;
723 event.data.graph_sink_input_port_added.port = gsink_def_port;
724 ok(has_event(&event), "got the expected graph's port added event (for sink, initial)");
725
726 /* Source's port connected */
727 event.type = SRC_COMP_OUTPUT_PORT_CONNECTED;
728 event.data.src_comp_output_port_connected.comp = gsrc;
729 event.data.src_comp_output_port_connected.self_port = gsrc_def_port;
730 event.data.src_comp_output_port_connected.other_port = gsink_def_port;
731 ok(has_event(&event), "got the expected source's port connected event");
732
733 bt_graph_put_ref(graph);
734 bt_component_sink_put_ref(sink);
735 bt_component_source_put_ref(src);
736 bt_connection_put_ref(conn);
737}
738
739static
740void test_sink_port_connected_error(void)
741{
742 const bt_component_source *src;
743 const bt_component_sink *sink;
744 const bt_component *gsrc;
745 const bt_component *gsink;
746 bt_graph *graph;
747 const bt_port_output *src_def_port;
748 const bt_port_input *sink_def_port;
749 const bt_port *gsrc_def_port;
750 const bt_port *gsink_def_port;
751 const bt_connection *conn = NULL;
752 struct event event;
753 bt_graph_connect_ports_status status;
754
755 prepare_test(TEST_SINK_PORT_CONNECTED_ERROR, "port connected error: sink");
756 graph = create_graph();
757 BT_ASSERT(graph);
758 src = create_src(graph);
759 sink = create_sink(graph);
760 src_def_port = bt_component_source_borrow_output_port_by_name_const(src,
761 "out");
762 BT_ASSERT(src_def_port);
763 sink_def_port = bt_component_sink_borrow_input_port_by_name_const(sink,
764 "in");
765 BT_ASSERT(sink_def_port);
766 status = bt_graph_connect_ports(graph, src_def_port,
767 sink_def_port, &conn);
768 ok(status != BT_GRAPH_CONNECT_PORTS_STATUS_OK,
769 "bt_graph_connect_ports() returns an error");
758932ce 770 bt_current_thread_clear_error();
21d04293
PP
771 ok(!conn, "returned connection is still NULL");
772 gsrc = bt_component_source_as_component_const(src);
773 gsink = bt_component_sink_as_component_const(sink);
774 gsrc_def_port = bt_port_output_as_port_const(src_def_port);
775 gsink_def_port = bt_port_input_as_port_const(sink_def_port);
776
777 /* We're supposed to have 4 events */
778 ok(events->len == 4, "we have the expected number of events");
779
780 /* Source's port added */
781 event.type = GRAPH_SRC_OUTPUT_PORT_ADDED;
782 event.data.graph_src_output_port_added.comp = gsrc;
783 event.data.graph_src_output_port_added.port = gsrc_def_port;
784 ok(has_event(&event), "got the expected graph's port added event (for source, initial)");
785
786 /* Sink's port added */
787 event.type = GRAPH_SINK_INPUT_PORT_ADDED;
788 event.data.graph_sink_input_port_added.comp = gsink;
789 event.data.graph_sink_input_port_added.port = gsink_def_port;
790 ok(has_event(&event), "got the expected graph's port added event (for sink, initial)");
791
792 /* Source's port connected */
793 event.type = SRC_COMP_OUTPUT_PORT_CONNECTED;
794 event.data.src_comp_output_port_connected.comp = gsrc;
795 event.data.src_comp_output_port_connected.self_port = gsrc_def_port;
796 event.data.src_comp_output_port_connected.other_port = gsink_def_port;
797 ok(has_event(&event), "got the expected source's port connected event");
798
799 /* Sink's port connected */
800 event.type = SINK_COMP_INPUT_PORT_CONNECTED;
801 event.data.sink_comp_input_port_connected.comp = gsink;
802 event.data.sink_comp_input_port_connected.self_port = gsink_def_port;
803 event.data.sink_comp_input_port_connected.other_port = gsrc_def_port;
804 ok(has_event(&event), "got the expected sink's port connected event");
805
806 bt_connection_put_ref(conn);
807 bt_graph_put_ref(graph);
808 bt_component_sink_put_ref(sink);
809 bt_component_source_put_ref(src);
810}
811
812static
813void test_empty_graph(void)
814{
815 bt_graph *graph;
816
817 prepare_test(TEST_EMPTY_GRAPH, "empty graph");
818 graph = create_graph();
819 ok(events->len == 0, "empty graph generates no events");
820 bt_graph_put_ref(graph);
821}
822
823int main(int argc, char **argv)
824{
825 plan_tests(NR_TESTS);
826 init_test();
827 test_empty_graph();
828 test_simple();
829 test_src_port_connected_error();
830 test_sink_port_connected_error();
831 test_src_adds_port_in_port_connected();
832 fini_test();
833 return exit_status();
834}
This page took 0.061208 seconds and 4 git commands to generate.