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