Commit | Line | Data |
---|---|---|
8c20dd54 | 1 | /* |
0235b0db | 2 | * SPDX-License-Identifier: GPL-2.0-only |
8c20dd54 | 3 | * |
0235b0db | 4 | * Copyright (C) 2019 Philippe Proulx <pproulx@efficios.com> |
8c20dd54 PP |
5 | */ |
6 | ||
7 | #include <babeltrace2/babeltrace.h> | |
8 | #include "common/assert.h" | |
9 | #include <string.h> | |
10 | #include "tap/tap.h" | |
11 | ||
79840741 | 12 | #define NR_TESTS 68 |
8c20dd54 PP |
13 | |
14 | struct test_data { | |
21a9f056 | 15 | bt_graph_simple_sink_component_initialize_func_status init_status; |
8c20dd54 PP |
16 | bt_graph_simple_sink_component_consume_func_status consume_status; |
17 | }; | |
18 | ||
19 | static | |
21a9f056 | 20 | bt_graph_simple_sink_component_initialize_func_status simple_INITIALIZE_func( |
9a2c8b8e | 21 | bt_message_iterator *iterator, |
8c20dd54 PP |
22 | void *data) |
23 | { | |
24 | struct test_data *test_data = data; | |
25 | ||
26 | ok(iterator, "Message iterator is not NULL in initialization function"); | |
27 | ok(data, "Data is not NULL in initialization function"); | |
28 | return test_data->init_status; | |
29 | } | |
30 | ||
31 | static | |
32 | bt_graph_simple_sink_component_consume_func_status simple_consume_func( | |
9a2c8b8e | 33 | bt_message_iterator *iterator, |
8c20dd54 PP |
34 | void *data) |
35 | { | |
36 | struct test_data *test_data = data; | |
37 | ||
38 | ok(iterator, "Message iterator is not NULL in consume function"); | |
39 | ok(data, "Data is not NULL in consume function"); | |
40 | return test_data->consume_status; | |
41 | } | |
42 | ||
43 | static | |
44 | void simple_fini_func(void *data) | |
45 | { | |
46 | ok(data, "Data is not NULL in finalization function"); | |
47 | } | |
48 | ||
49 | static | |
21a9f056 | 50 | bt_component_class_initialize_method_status src_init( |
8c20dd54 | 51 | bt_self_component_source *self_comp, |
ecd7492f MJ |
52 | bt_self_component_source_configuration *config __attribute__((unused)), |
53 | const bt_value *params __attribute__((unused)), | |
54 | void *init_method_data __attribute__((unused))) | |
8c20dd54 PP |
55 | { |
56 | bt_self_component_add_port_status status; | |
57 | ||
58 | status = bt_self_component_source_add_output_port(self_comp, | |
59 | "out", NULL, NULL); | |
60 | BT_ASSERT(status == BT_SELF_COMPONENT_ADD_PORT_STATUS_OK); | |
21a9f056 | 61 | return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK; |
8c20dd54 PP |
62 | } |
63 | ||
64 | static | |
a3f0c7db | 65 | bt_message_iterator_class_next_method_status src_iter_next( |
ecd7492f MJ |
66 | bt_self_message_iterator *message_iterator __attribute__((unused)), |
67 | bt_message_array_const msgs __attribute__((unused)), | |
68 | uint64_t capacity __attribute__((unused)), | |
69 | uint64_t *count __attribute__((unused))) | |
8c20dd54 | 70 | { |
a3f0c7db | 71 | return BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END; |
8c20dd54 PP |
72 | } |
73 | ||
74 | static | |
75 | bt_graph *create_graph_with_source(const bt_port_output **out_port) | |
76 | { | |
a3f0c7db | 77 | bt_message_iterator_class *msg_iter_cls; |
8c20dd54 PP |
78 | bt_component_class_source *src_comp_cls; |
79 | bt_graph *graph; | |
80 | const bt_component_source *src_comp = NULL; | |
81 | bt_graph_add_component_status add_comp_status; | |
82 | bt_component_class_set_method_status set_method_status; | |
83 | ||
84 | BT_ASSERT(out_port); | |
a3f0c7db SM |
85 | |
86 | msg_iter_cls = bt_message_iterator_class_create(src_iter_next); | |
87 | BT_ASSERT(msg_iter_cls); | |
88 | ||
89 | src_comp_cls = bt_component_class_source_create("src", msg_iter_cls); | |
8c20dd54 | 90 | BT_ASSERT(src_comp_cls); |
21a9f056 | 91 | set_method_status = bt_component_class_source_set_initialize_method( |
8c20dd54 PP |
92 | src_comp_cls, src_init); |
93 | BT_ASSERT(set_method_status == BT_COMPONENT_CLASS_SET_METHOD_STATUS_OK); | |
056deb59 | 94 | graph = bt_graph_create(0); |
8c20dd54 PP |
95 | BT_ASSERT(graph); |
96 | add_comp_status = bt_graph_add_source_component(graph, src_comp_cls, | |
97 | "src", NULL, BT_LOGGING_LEVEL_NONE, &src_comp); | |
98 | BT_ASSERT(add_comp_status == BT_GRAPH_ADD_COMPONENT_STATUS_OK); | |
99 | BT_ASSERT(src_comp); | |
100 | *out_port = bt_component_source_borrow_output_port_by_index_const( | |
101 | src_comp, 0); | |
102 | BT_ASSERT(*out_port); | |
8c20dd54 | 103 | bt_component_class_source_put_ref(src_comp_cls); |
a3f0c7db | 104 | bt_message_iterator_class_put_ref(msg_iter_cls); |
8c20dd54 PP |
105 | return graph; |
106 | } | |
107 | ||
108 | static | |
109 | void test_simple_expect_run_once_status( | |
21a9f056 | 110 | bt_graph_simple_sink_component_initialize_func_status init_status, |
8c20dd54 PP |
111 | bt_graph_simple_sink_component_consume_func_status consume_status, |
112 | bt_graph_run_once_status exp_run_once_status) | |
113 | { | |
114 | const bt_port_output *src_out_port = NULL; | |
115 | bt_graph *graph; | |
116 | const bt_component_sink *sink_comp = NULL; | |
117 | const bt_port_input *sink_in_port; | |
118 | bt_graph_add_component_status add_comp_status; | |
119 | bt_graph_run_once_status run_once_status; | |
120 | bt_graph_connect_ports_status connect_status; | |
121 | struct test_data test_data = { | |
122 | .init_status = init_status, | |
123 | .consume_status = consume_status, | |
124 | }; | |
79840741 | 125 | const struct bt_error *err; |
8c20dd54 PP |
126 | |
127 | graph = create_graph_with_source(&src_out_port); | |
128 | BT_ASSERT(graph); | |
129 | BT_ASSERT(src_out_port); | |
79840741 | 130 | |
8c20dd54 | 131 | add_comp_status = bt_graph_add_simple_sink_component(graph, "sink", |
21a9f056 | 132 | simple_INITIALIZE_func, simple_consume_func, simple_fini_func, |
8c20dd54 PP |
133 | &test_data, &sink_comp); |
134 | BT_ASSERT(add_comp_status == BT_GRAPH_ADD_COMPONENT_STATUS_OK); | |
135 | BT_ASSERT(sink_comp); | |
79840741 | 136 | |
8c20dd54 PP |
137 | sink_in_port = bt_component_sink_borrow_input_port_by_name_const( |
138 | sink_comp, "in"); | |
139 | ok(sink_in_port, | |
140 | "Simple sink component has an input port named \"in\""); | |
79840741 | 141 | |
8c20dd54 PP |
142 | connect_status = bt_graph_connect_ports(graph, src_out_port, |
143 | sink_in_port, NULL); | |
144 | ok(connect_status == BT_GRAPH_CONNECT_PORTS_STATUS_OK, | |
145 | "Simple sink component's \"in\" port is connectable"); | |
79840741 | 146 | |
8c20dd54 PP |
147 | run_once_status = bt_graph_run_once(graph); |
148 | ok(run_once_status == exp_run_once_status, | |
149 | "Graph \"run once\" status is the expected one (status code: %d)", | |
150 | run_once_status); | |
79840741 SM |
151 | |
152 | err = bt_current_thread_take_error(); | |
153 | ok((run_once_status < 0) == (err != NULL), | |
154 | "Current thread error is set if bt_graph_run_once returned an error"); | |
155 | ||
8c20dd54 | 156 | bt_graph_put_ref(graph); |
79840741 SM |
157 | if (err) { |
158 | bt_error_release(err); | |
159 | } | |
8c20dd54 PP |
160 | } |
161 | ||
162 | int main(void) | |
163 | { | |
164 | plan_tests(NR_TESTS); | |
165 | ||
166 | /* Test initialization function status */ | |
167 | test_simple_expect_run_once_status( | |
21a9f056 | 168 | BT_GRAPH_SIMPLE_SINK_COMPONENT_INITIALIZE_FUNC_STATUS_OK, |
8c20dd54 PP |
169 | BT_GRAPH_SIMPLE_SINK_COMPONENT_CONSUME_FUNC_STATUS_OK, |
170 | BT_GRAPH_RUN_ONCE_STATUS_OK); | |
171 | test_simple_expect_run_once_status( | |
21a9f056 | 172 | BT_GRAPH_SIMPLE_SINK_COMPONENT_INITIALIZE_FUNC_STATUS_ERROR, |
8c20dd54 PP |
173 | BT_GRAPH_SIMPLE_SINK_COMPONENT_CONSUME_FUNC_STATUS_OK, |
174 | BT_GRAPH_RUN_ONCE_STATUS_ERROR); | |
175 | test_simple_expect_run_once_status( | |
21a9f056 | 176 | BT_GRAPH_SIMPLE_SINK_COMPONENT_INITIALIZE_FUNC_STATUS_MEMORY_ERROR, |
8c20dd54 PP |
177 | BT_GRAPH_SIMPLE_SINK_COMPONENT_CONSUME_FUNC_STATUS_OK, |
178 | BT_GRAPH_RUN_ONCE_STATUS_MEMORY_ERROR); | |
179 | ||
180 | /* Test "consume" function status */ | |
181 | test_simple_expect_run_once_status( | |
21a9f056 | 182 | BT_GRAPH_SIMPLE_SINK_COMPONENT_INITIALIZE_FUNC_STATUS_OK, |
8c20dd54 PP |
183 | BT_GRAPH_SIMPLE_SINK_COMPONENT_CONSUME_FUNC_STATUS_OK, |
184 | BT_GRAPH_RUN_ONCE_STATUS_OK); | |
185 | test_simple_expect_run_once_status( | |
21a9f056 | 186 | BT_GRAPH_SIMPLE_SINK_COMPONENT_INITIALIZE_FUNC_STATUS_OK, |
8c20dd54 PP |
187 | BT_GRAPH_SIMPLE_SINK_COMPONENT_CONSUME_FUNC_STATUS_ERROR, |
188 | BT_GRAPH_RUN_ONCE_STATUS_ERROR); | |
189 | test_simple_expect_run_once_status( | |
21a9f056 | 190 | BT_GRAPH_SIMPLE_SINK_COMPONENT_INITIALIZE_FUNC_STATUS_OK, |
8c20dd54 PP |
191 | BT_GRAPH_SIMPLE_SINK_COMPONENT_CONSUME_FUNC_STATUS_MEMORY_ERROR, |
192 | BT_GRAPH_RUN_ONCE_STATUS_MEMORY_ERROR); | |
193 | test_simple_expect_run_once_status( | |
21a9f056 | 194 | BT_GRAPH_SIMPLE_SINK_COMPONENT_INITIALIZE_FUNC_STATUS_OK, |
8c20dd54 PP |
195 | BT_GRAPH_SIMPLE_SINK_COMPONENT_CONSUME_FUNC_STATUS_AGAIN, |
196 | BT_GRAPH_RUN_ONCE_STATUS_AGAIN); | |
197 | test_simple_expect_run_once_status( | |
21a9f056 | 198 | BT_GRAPH_SIMPLE_SINK_COMPONENT_INITIALIZE_FUNC_STATUS_OK, |
8c20dd54 PP |
199 | BT_GRAPH_SIMPLE_SINK_COMPONENT_CONSUME_FUNC_STATUS_END, |
200 | BT_GRAPH_RUN_ONCE_STATUS_END); | |
201 | ||
202 | return exit_status(); | |
203 | } |