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