lib: introduce bt_message_iterator_class
[babeltrace.git] / tests / lib / test_simple_sink.c
CommitLineData
8c20dd54
PP
1/*
2 * Copyright (c) 2019 Philippe Proulx <pproulx@efficios.com>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; under version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18#include <babeltrace2/babeltrace.h>
19#include "common/assert.h"
20#include <string.h>
21#include "tap/tap.h"
22
79840741 23#define NR_TESTS 68
8c20dd54
PP
24
25struct test_data {
21a9f056 26 bt_graph_simple_sink_component_initialize_func_status init_status;
8c20dd54
PP
27 bt_graph_simple_sink_component_consume_func_status consume_status;
28};
29
30static
21a9f056 31bt_graph_simple_sink_component_initialize_func_status simple_INITIALIZE_func(
8c20dd54
PP
32 bt_self_component_port_input_message_iterator *iterator,
33 void *data)
34{
35 struct test_data *test_data = data;
36
37 ok(iterator, "Message iterator is not NULL in initialization function");
38 ok(data, "Data is not NULL in initialization function");
39 return test_data->init_status;
40}
41
42static
43bt_graph_simple_sink_component_consume_func_status simple_consume_func(
44 bt_self_component_port_input_message_iterator *iterator,
45 void *data)
46{
47 struct test_data *test_data = data;
48
49 ok(iterator, "Message iterator is not NULL in consume function");
50 ok(data, "Data is not NULL in consume function");
51 return test_data->consume_status;
52}
53
54static
55void simple_fini_func(void *data)
56{
57 ok(data, "Data is not NULL in finalization function");
58}
59
60static
21a9f056 61bt_component_class_initialize_method_status src_init(
8c20dd54 62 bt_self_component_source *self_comp,
59225a3e 63 bt_self_component_source_configuration *config,
8c20dd54
PP
64 const bt_value *params, void *init_method_data)
65{
66 bt_self_component_add_port_status status;
67
68 status = bt_self_component_source_add_output_port(self_comp,
69 "out", NULL, NULL);
70 BT_ASSERT(status == BT_SELF_COMPONENT_ADD_PORT_STATUS_OK);
21a9f056 71 return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK;
8c20dd54
PP
72}
73
74static
a3f0c7db 75bt_message_iterator_class_next_method_status src_iter_next(
8c20dd54
PP
76 bt_self_message_iterator *message_iterator,
77 bt_message_array_const msgs, uint64_t capacity,
78 uint64_t *count)
79{
a3f0c7db 80 return BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END;
8c20dd54
PP
81}
82
83static
84bt_graph *create_graph_with_source(const bt_port_output **out_port)
85{
a3f0c7db 86 bt_message_iterator_class *msg_iter_cls;
8c20dd54
PP
87 bt_component_class_source *src_comp_cls;
88 bt_graph *graph;
89 const bt_component_source *src_comp = NULL;
90 bt_graph_add_component_status add_comp_status;
91 bt_component_class_set_method_status set_method_status;
92
93 BT_ASSERT(out_port);
a3f0c7db
SM
94
95 msg_iter_cls = bt_message_iterator_class_create(src_iter_next);
96 BT_ASSERT(msg_iter_cls);
97
98 src_comp_cls = bt_component_class_source_create("src", msg_iter_cls);
8c20dd54 99 BT_ASSERT(src_comp_cls);
21a9f056 100 set_method_status = bt_component_class_source_set_initialize_method(
8c20dd54
PP
101 src_comp_cls, src_init);
102 BT_ASSERT(set_method_status == BT_COMPONENT_CLASS_SET_METHOD_STATUS_OK);
056deb59 103 graph = bt_graph_create(0);
8c20dd54
PP
104 BT_ASSERT(graph);
105 add_comp_status = bt_graph_add_source_component(graph, src_comp_cls,
106 "src", NULL, BT_LOGGING_LEVEL_NONE, &src_comp);
107 BT_ASSERT(add_comp_status == BT_GRAPH_ADD_COMPONENT_STATUS_OK);
108 BT_ASSERT(src_comp);
109 *out_port = bt_component_source_borrow_output_port_by_index_const(
110 src_comp, 0);
111 BT_ASSERT(*out_port);
112 bt_component_source_put_ref(src_comp);
113 bt_component_class_source_put_ref(src_comp_cls);
a3f0c7db 114 bt_message_iterator_class_put_ref(msg_iter_cls);
8c20dd54
PP
115 return graph;
116}
117
118static
119void test_simple_expect_run_once_status(
21a9f056 120 bt_graph_simple_sink_component_initialize_func_status init_status,
8c20dd54
PP
121 bt_graph_simple_sink_component_consume_func_status consume_status,
122 bt_graph_run_once_status exp_run_once_status)
123{
124 const bt_port_output *src_out_port = NULL;
125 bt_graph *graph;
126 const bt_component_sink *sink_comp = NULL;
127 const bt_port_input *sink_in_port;
128 bt_graph_add_component_status add_comp_status;
129 bt_graph_run_once_status run_once_status;
130 bt_graph_connect_ports_status connect_status;
131 struct test_data test_data = {
132 .init_status = init_status,
133 .consume_status = consume_status,
134 };
79840741 135 const struct bt_error *err;
8c20dd54
PP
136
137 graph = create_graph_with_source(&src_out_port);
138 BT_ASSERT(graph);
139 BT_ASSERT(src_out_port);
79840741 140
8c20dd54 141 add_comp_status = bt_graph_add_simple_sink_component(graph, "sink",
21a9f056 142 simple_INITIALIZE_func, simple_consume_func, simple_fini_func,
8c20dd54
PP
143 &test_data, &sink_comp);
144 BT_ASSERT(add_comp_status == BT_GRAPH_ADD_COMPONENT_STATUS_OK);
145 BT_ASSERT(sink_comp);
79840741 146
8c20dd54
PP
147 sink_in_port = bt_component_sink_borrow_input_port_by_name_const(
148 sink_comp, "in");
149 ok(sink_in_port,
150 "Simple sink component has an input port named \"in\"");
79840741 151
8c20dd54
PP
152 connect_status = bt_graph_connect_ports(graph, src_out_port,
153 sink_in_port, NULL);
154 ok(connect_status == BT_GRAPH_CONNECT_PORTS_STATUS_OK,
155 "Simple sink component's \"in\" port is connectable");
79840741 156
8c20dd54
PP
157 run_once_status = bt_graph_run_once(graph);
158 ok(run_once_status == exp_run_once_status,
159 "Graph \"run once\" status is the expected one (status code: %d)",
160 run_once_status);
79840741
SM
161
162 err = bt_current_thread_take_error();
163 ok((run_once_status < 0) == (err != NULL),
164 "Current thread error is set if bt_graph_run_once returned an error");
165
8c20dd54
PP
166 bt_component_sink_put_ref(sink_comp);
167 bt_graph_put_ref(graph);
79840741
SM
168 if (err) {
169 bt_error_release(err);
170 }
8c20dd54
PP
171}
172
173int main(void)
174{
175 plan_tests(NR_TESTS);
176
177 /* Test initialization function status */
178 test_simple_expect_run_once_status(
21a9f056 179 BT_GRAPH_SIMPLE_SINK_COMPONENT_INITIALIZE_FUNC_STATUS_OK,
8c20dd54
PP
180 BT_GRAPH_SIMPLE_SINK_COMPONENT_CONSUME_FUNC_STATUS_OK,
181 BT_GRAPH_RUN_ONCE_STATUS_OK);
182 test_simple_expect_run_once_status(
21a9f056 183 BT_GRAPH_SIMPLE_SINK_COMPONENT_INITIALIZE_FUNC_STATUS_ERROR,
8c20dd54
PP
184 BT_GRAPH_SIMPLE_SINK_COMPONENT_CONSUME_FUNC_STATUS_OK,
185 BT_GRAPH_RUN_ONCE_STATUS_ERROR);
186 test_simple_expect_run_once_status(
21a9f056 187 BT_GRAPH_SIMPLE_SINK_COMPONENT_INITIALIZE_FUNC_STATUS_MEMORY_ERROR,
8c20dd54
PP
188 BT_GRAPH_SIMPLE_SINK_COMPONENT_CONSUME_FUNC_STATUS_OK,
189 BT_GRAPH_RUN_ONCE_STATUS_MEMORY_ERROR);
190
191 /* Test "consume" function status */
192 test_simple_expect_run_once_status(
21a9f056 193 BT_GRAPH_SIMPLE_SINK_COMPONENT_INITIALIZE_FUNC_STATUS_OK,
8c20dd54
PP
194 BT_GRAPH_SIMPLE_SINK_COMPONENT_CONSUME_FUNC_STATUS_OK,
195 BT_GRAPH_RUN_ONCE_STATUS_OK);
196 test_simple_expect_run_once_status(
21a9f056 197 BT_GRAPH_SIMPLE_SINK_COMPONENT_INITIALIZE_FUNC_STATUS_OK,
8c20dd54
PP
198 BT_GRAPH_SIMPLE_SINK_COMPONENT_CONSUME_FUNC_STATUS_ERROR,
199 BT_GRAPH_RUN_ONCE_STATUS_ERROR);
200 test_simple_expect_run_once_status(
21a9f056 201 BT_GRAPH_SIMPLE_SINK_COMPONENT_INITIALIZE_FUNC_STATUS_OK,
8c20dd54
PP
202 BT_GRAPH_SIMPLE_SINK_COMPONENT_CONSUME_FUNC_STATUS_MEMORY_ERROR,
203 BT_GRAPH_RUN_ONCE_STATUS_MEMORY_ERROR);
204 test_simple_expect_run_once_status(
21a9f056 205 BT_GRAPH_SIMPLE_SINK_COMPONENT_INITIALIZE_FUNC_STATUS_OK,
8c20dd54
PP
206 BT_GRAPH_SIMPLE_SINK_COMPONENT_CONSUME_FUNC_STATUS_AGAIN,
207 BT_GRAPH_RUN_ONCE_STATUS_AGAIN);
208 test_simple_expect_run_once_status(
21a9f056 209 BT_GRAPH_SIMPLE_SINK_COMPONENT_INITIALIZE_FUNC_STATUS_OK,
8c20dd54
PP
210 BT_GRAPH_SIMPLE_SINK_COMPONENT_CONSUME_FUNC_STATUS_END,
211 BT_GRAPH_RUN_ONCE_STATUS_END);
212
213 return exit_status();
214}
This page took 0.042613 seconds and 4 git commands to generate.