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